Fix static module list generation for ldb.
[jelmer/samba4-debian.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 #include "dynconfig.h"
30
31 static EjsId eid;
32
33 _NORETURN_ static void smbscript_ejs_exception(const char *reason)
34 {
35         Ejs *ep = ejsPtr(eid);
36         ejsSetErrorMsg(eid, "%s", reason);
37         fprintf(stderr, "%s", ep->error);
38         exit(127);
39 }
40
41 int main(int argc, const char **argv)
42 {
43         EjsHandle handle = 0;
44         MprVar result;
45         char *emsg, *script;
46         size_t script_size;
47         TALLOC_CTX *mem_ctx = talloc_new(NULL);
48         const char **argv_list = NULL;
49         const char *fname;
50         struct MprVar *return_var;
51         int exit_status, i;
52         struct loadparm_context *lp_ctx;
53
54         fault_setup(argv[0]);
55
56         global_loadparm = lp_ctx = loadparm_init(talloc_autofree_context());
57
58         if (getenv("SMB_CONF_PATH")) {
59                 lp_load(lp_ctx, getenv("SMB_CONF_PATH"));
60         } else {
61                 lp_load(lp_ctx, dyn_CONFIGFILE);
62         }
63
64         gensec_init(lp_ctx);
65         mprSetCtx(mem_ctx);
66
67
68         if (argc < 2) {
69                 fprintf(stderr, "You must supply a script name\n");
70                 exit(1);
71         }
72
73         fname = argv[1];
74
75         if (ejsOpen(NULL, NULL, NULL) != 0) {
76                 fprintf(stderr, "smbscript: ejsOpen(): unable to initialise "
77                         "EJS subsystem\n");
78                 exit(127);
79         }
80
81         smb_setup_ejs_functions(smbscript_ejs_exception);
82
83         if ((eid = ejsOpenEngine(handle, 0)) == (EjsId)-1) {
84                 fprintf(stderr, "smbscript: ejsOpenEngine(): unable to "
85                         "initialise an EJS engine\n");
86                 exit(127);
87         }
88
89         /* setup ARGV[] in the ejs environment */
90         for (i=1;argv[i];i++) {
91                 argv_list = str_list_add(argv_list, argv[i]);
92         }
93         talloc_steal(mem_ctx, argv_list);
94         mprSetVar(ejsGetGlobalObject(eid), "ARGV", mprList("ARGV", argv_list));
95
96         /* load the script and advance past interpreter line*/
97         script = file_load(fname, &script_size, mem_ctx);
98
99         if (!script) {
100                 fprintf(stderr, "Unable to load script from '%s'\n", fname);
101                 exit(1);
102         }
103
104         /* allow scriptable js */
105         if (strncmp(script, "#!", 2) == 0) {
106                 script += strcspn(script, "\r\n");
107                 script += strspn(script, "\r\n");
108         }
109         /* and this copes with the ugly exec hack */
110         if (strncmp(script, "exec ", 5) == 0) {
111                 script += strcspn(script, "\r\n");
112                 script += strspn(script, "\r\n");
113         }
114
115         /* run the script */
116         if (ejsEvalScript(eid, script, &result, &emsg) == -1) {
117                 fprintf(stderr, "smbscript: ejsEvalScript(): %s\n", emsg);
118                 exit(127);
119         }
120
121         return_var = ejsGetReturnValue(eid);
122         exit_status = mprVarToNumber(return_var);
123
124         ejsClose();
125
126         talloc_free(mem_ctx);
127
128         return exit_status;
129 }