r9722: Initial attempt at converting samba3dump to EJS..
[samba.git] / source / scripting / ejs / smbscript.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Standalone client for ejs scripting.
5
6    Copyright (C) Tim Potter <tpot@samba.org> 2005
7    Copyright (C) Andrew Tridgell 2005
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "dynconfig.h"
26 #include "lib/appweb/ejs/ejs.h"
27 #include "lib/appweb/ejs/ejsInternal.h"
28 #include "scripting/ejs/smbcalls.h"
29
30 static EjsId eid;
31
32 void ejs_exception(const char *reason)
33 {
34         Ejs *ep = ejsPtr(eid);
35         ejsSetErrorMsg(eid, "%s", reason);
36         fprintf(stderr, "%s", ep->error);
37         exit(127);
38 }
39
40  int main(int argc, const char **argv)
41 {
42         EjsHandle handle = 0;
43         MprVar result;
44         char *emsg, *script;
45         size_t script_size;
46         TALLOC_CTX *mem_ctx = talloc_new(NULL);
47         const char **argv_list = NULL;
48         const char *fname;
49         struct MprVar *return_var;
50         int exit_status, i;
51
52         fault_setup(argv[0]);
53         smbscript_init_subsystems;
54         mprSetCtx(mem_ctx);
55
56         lp_load();
57
58         if (argc < 2) {
59                 fprintf(stderr, "You must supply a script name\n");
60                 exit(1);
61         }
62
63         fname = argv[1];
64
65         if (ejsOpen(NULL, NULL, NULL) != 0) {
66                 fprintf(stderr, "smbscript: ejsOpen(): unable to initialise "
67                         "EJ subsystem\n");
68                 exit(127);
69         }
70
71         smb_setup_ejs_functions();
72
73         if ((eid = ejsOpenEngine(handle, 0)) == (EjsId)-1) {
74                 fprintf(stderr, "smbscript: ejsOpenEngine(): unable to "
75                         "initialise an EJS engine\n");
76                 exit(127);
77         }
78
79         /* setup ARGV[] in the ejs environment */
80         for (i=1;argv[i];i++) {
81                 argv_list = str_list_add(argv_list, argv[i]);
82         }
83         talloc_steal(mem_ctx, argv_list);
84         mprSetVar(ejsGetGlobalObject(eid), "ARGV", mprList("ARGV", argv_list));
85
86         /* load the script and advance past interpreter line*/
87         script = file_load(fname, &script_size, mem_ctx);
88
89         if (!script) {
90                 fprintf(stderr, "Unable to load script from '%s'\n", fname);
91                 exit(1);
92         }
93
94         /* allow scriptable js */
95         if (strncmp(script, "#!", 2) == 0) {
96                 script += strcspn(script, "\r\n");
97                 script += strspn(script, "\r\n");
98         }
99         /* and this copes with the ugly exec hack */
100         if (strncmp(script, "exec ", 5) == 0) {
101                 script += strcspn(script, "\r\n");
102                 script += strspn(script, "\r\n");
103         }
104
105         /* run the script */
106         if (ejsEvalScript(eid, script, &result, &emsg) == -1) {
107                 fprintf(stderr, "smbscript: ejsEvalScript(): %s\n", emsg);
108                 exit(127);
109         }
110
111         return_var = ejsGetReturnValue(eid);
112         exit_status = mprVarToNumber(return_var);
113
114         ejsClose();
115
116         talloc_free(mem_ctx);
117
118         return exit_status;
119 }