r8399: move the ejs and esp code closer to the directory layout used by the
[bbaumbach/samba-autobuild/.git] / source4 / scripting / ejs / smbscript.c
index 021dafa4aef5fd59b7d2a3acdd6b3be925701779..7e014716c2372046ce207110c0a790a887f24c23 100644 (file)
@@ -1,9 +1,10 @@
 /* 
    Unix SMB/CIFS implementation.
 
-   Standalone client for ESP scripting.
+   Standalone client for ejs scripting.
 
    Copyright (C) Tim Potter <tpot@samba.org> 2005
+   Copyright (C) Andrew Tridgell 2005
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 
 #include "includes.h"
 #include "dynconfig.h"
-#include "lib/ejs/ejs.h"
+#include "lib/appweb/ejs/ejs.h"
+#include "scripting/ejs/smbcalls.h"
 
 void ejs_exception(const char *reason)
 {
        fprintf(stderr, "smbscript exception: %s", reason);
-       exit(1);
+       exit(127);
 }
 
- int main(int argc, const char *argv[])
+ int main(int argc, const char **argv)
 {
        EjsId eid;
        EjsHandle handle = 0;
        MprVar result;
-       char *emsg;
+       char *emsg, *script;
+       size_t script_size;
        TALLOC_CTX *mem_ctx = talloc_new(NULL);
        const char **argv_list = NULL;
-       struct MprVar v;
-       int i;
+       const char *fname;
+       struct MprVar *return_var;
+       int exit_status, i;
 
-       if (argc < 2) {
-               fprintf(stderr, "Usage: %s <scriptfile>\n", argv[0]);
-               exit(1);
-       }
+       smbscript_init_subsystems;
+       mprSetCtx(mem_ctx);
 
-       setup_logging(argv[0],DEBUG_STDOUT);
+       lp_load(dyn_CONFIGFILE);
 
-       if (!lp_load(dyn_CONFIGFILE, False, False, False)) {
-               fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
-                       argv[0], dyn_CONFIGFILE);
+       if (argc < 2) {
+               fprintf(stderr, "You must supply a script name\n");
                exit(1);
        }
 
-       mprSetCtx(mem_ctx);
+       fname = argv[1];
 
        if (ejsOpen(NULL, NULL, NULL) != 0) {
                fprintf(stderr, "smbscript: ejsOpen(): unable to initialise "
                        "EJ subsystem\n");
-               exit(1);
+               exit(127);
        }
 
        smb_setup_ejs_functions();
@@ -67,26 +68,44 @@ void ejs_exception(const char *reason)
        if ((eid = ejsOpenEngine(handle, 0)) == (EjsId)-1) {
                fprintf(stderr, "smbscript: ejsOpenEngine(): unable to "
                        "initialise an EJS engine\n");
-               exit(1);
+               exit(127);
        }
 
+       smb_setup_ejs_constants(eid);
+
        /* setup ARGV[] in the ejs environment */
-       for (i=2;i<argc;i++) {
+       for (i=1;argv[i];i++) {
                argv_list = str_list_add(argv_list, argv[i]);
        }
-       v = mprList("ARGV", argv_list);
-       mprCreateProperty(ejsGetGlobalObject(eid), "ARGV", &v);
+       talloc_steal(mem_ctx, argv_list);
+       mprSetVar(ejsGetGlobalObject(eid), "ARGV", mprList("ARGV", argv_list));
+
+       /* load the script and advance past interpreter line*/
+       script = file_load(fname, &script_size, mem_ctx);
+
+       if ((script_size > 2) && script[0] == '#' && script[1] == '!') {
+               script += 2;
+               script_size -= 2;
+               while (script_size) {
+                       if (*script == '\r' || *script == '\n')
+                               break;
+                       script++;
+                       script_size--;
+               }
+       }
 
        /* run the script */
-       if (ejsEvalFile(eid, discard_const_p(char, argv[1]), &result,
-                       &emsg) == -1) {
+       if (ejsEvalScript(eid, script, &result, &emsg) == -1) {
                fprintf(stderr, "smbscript: ejsEvalScript(): %s\n", emsg);
-               exit(1);
+               exit(127);
        }
 
+       return_var = ejsGetReturnValue(eid);
+       exit_status = return_var->integer;
+
        ejsClose();
 
        talloc_free(mem_ctx);
 
-       return 0;
+       return exit_status;
 }