r23792: convert Samba4 to GPLv3
[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 3 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, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "lib/appweb/ejs/ejs.h"
25 #include "lib/appweb/ejs/ejsInternal.h"
26 #include "scripting/ejs/smbcalls.h"
27 #include "auth/gensec/gensec.h"
28 #include "ldb/include/ldb.h"
29
30 static EjsId eid;
31
32 static void smbscript_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
54         if (getenv("SMB_CONF_PATH")) {
55                 lp_set_cmdline("config file", getenv("SMB_CONF_PATH"));
56         }
57
58         ldb_global_init();
59
60         gensec_init();
61         mprSetCtx(mem_ctx);
62
63         lp_load();
64
65         if (argc < 2) {
66                 fprintf(stderr, "You must supply a script name\n");
67                 exit(1);
68         }
69
70         fname = argv[1];
71
72         if (ejsOpen(NULL, NULL, NULL) != 0) {
73                 fprintf(stderr, "smbscript: ejsOpen(): unable to initialise "
74                         "EJS subsystem\n");
75                 exit(127);
76         }
77
78         smb_setup_ejs_functions(smbscript_ejs_exception);
79
80         if ((eid = ejsOpenEngine(handle, 0)) == (EjsId)-1) {
81                 fprintf(stderr, "smbscript: ejsOpenEngine(): unable to "
82                         "initialise an EJS engine\n");
83                 exit(127);
84         }
85
86         /* setup ARGV[] in the ejs environment */
87         for (i=1;argv[i];i++) {
88                 argv_list = str_list_add(argv_list, argv[i]);
89         }
90         talloc_steal(mem_ctx, argv_list);
91         mprSetVar(ejsGetGlobalObject(eid), "ARGV", mprList("ARGV", argv_list));
92
93         /* load the script and advance past interpreter line*/
94         script = file_load(fname, &script_size, mem_ctx);
95
96         if (!script) {
97                 fprintf(stderr, "Unable to load script from '%s'\n", fname);
98                 exit(1);
99         }
100
101         /* allow scriptable js */
102         if (strncmp(script, "#!", 2) == 0) {
103                 script += strcspn(script, "\r\n");
104                 script += strspn(script, "\r\n");
105         }
106         /* and this copes with the ugly exec hack */
107         if (strncmp(script, "exec ", 5) == 0) {
108                 script += strcspn(script, "\r\n");
109                 script += strspn(script, "\r\n");
110         }
111
112         /* run the script */
113         if (ejsEvalScript(eid, script, &result, &emsg) == -1) {
114                 fprintf(stderr, "smbscript: ejsEvalScript(): %s\n", emsg);
115                 exit(127);
116         }
117
118         return_var = ejsGetReturnValue(eid);
119         exit_status = mprVarToNumber(return_var);
120
121         ejsClose();
122
123         talloc_free(mem_ctx);
124
125         return exit_status;
126 }