r8452: allow for the ugly hack:
[jra/samba/.git] / source4 / scripting / ejs / smbscript.c
index a15fce57b09f32f1bcf08df916d6c4ce161ca08f..d71d0847792fa79ed898251b73b4ed3057812053 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);
+
+       /* allow scriptable js */
+       if (strncmp(script, "#!", 2) == 0) {
+               script += strcspn(script, "\r\n");
+               script += strspn(script, "\r\n");
+       }
+       /* and this copes with the ugly exec hack */
+       if (strncmp(script, "exec ", 5) == 0) {
+               script += strcspn(script, "\r\n");
+               script += strspn(script, "\r\n");
+       }
 
        /* run the script */
-       if (ejsEvalFile(eid, discard_const_p(char, argv[1]), &result,
-                       &emsg) == -1) {
-               fprintf(stderr, "smbscript: ejsEvalFile(): %s\n", emsg);
-               exit(1);
+       if (ejsEvalScript(eid, script, &result, &emsg) == -1) {
+               fprintf(stderr, "smbscript: ejsEvalScript(): %s\n", emsg);
+               exit(127);
        }
 
+       return_var = ejsGetReturnValue(eid);
+       exit_status = mprVarToNumber(return_var);
+
        ejsClose();
 
        talloc_free(mem_ctx);
 
-       return 0;
+       return exit_status;
 }