r8357: Call lp_load() so we can access the various lp_functions().
[sfrench/samba-autobuild/.git] / source4 / 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/ejs/ejs.h"
27 #include "scripting/ejs/smbcalls.h"
28
29 void ejs_exception(const char *reason)
30 {
31         fprintf(stderr, "smbscript exception: %s", reason);
32         exit(127);
33 }
34
35  int main(int argc, const char **argv)
36 {
37         EjsId eid;
38         EjsHandle handle = 0;
39         MprVar result;
40         char *emsg, *script;
41         size_t script_size;
42         TALLOC_CTX *mem_ctx = talloc_new(NULL);
43         const char **argv_list = NULL;
44         const char *fname;
45         struct MprVar *return_var;
46         int exit_status, i;
47
48         smbscript_init_subsystems;
49         mprSetCtx(mem_ctx);
50
51         lp_load(dyn_CONFIGFILE);
52
53         if (argc < 2) {
54                 fprintf(stderr, "You must supply a script name\n");
55                 exit(1);
56         }
57
58         fname = argv[1];
59
60         if (ejsOpen(NULL, NULL, NULL) != 0) {
61                 fprintf(stderr, "smbscript: ejsOpen(): unable to initialise "
62                         "EJ subsystem\n");
63                 exit(127);
64         }
65
66         smb_setup_ejs_functions();
67
68         if ((eid = ejsOpenEngine(handle, 0)) == (EjsId)-1) {
69                 fprintf(stderr, "smbscript: ejsOpenEngine(): unable to "
70                         "initialise an EJS engine\n");
71                 exit(127);
72         }
73
74         smb_setup_ejs_constants(eid);
75
76         /* setup ARGV[] in the ejs environment */
77         for (i=1;argv[i];i++) {
78                 argv_list = str_list_add(argv_list, argv[i]);
79         }
80         talloc_steal(mem_ctx, argv_list);
81         mprSetVar(ejsGetGlobalObject(eid), "ARGV", mprList("ARGV", argv_list));
82
83         /* load the script and advance past interpreter line*/
84         script = file_load(fname, &script_size, mem_ctx);
85
86         if ((script_size > 2) && script[0] == '#' && script[1] == '!') {
87                 script += 2;
88                 script_size -= 2;
89                 while (script_size) {
90                         if (*script == '\r' || *script == '\n')
91                                 break;
92                         script++;
93                         script_size--;
94                 }
95         }
96
97         /* run the script */
98         if (ejsEvalScript(eid, script, &result, &emsg) == -1) {
99                 fprintf(stderr, "smbscript: ejsEvalScript(): %s\n", emsg);
100                 exit(127);
101         }
102
103         return_var = ejsGetReturnValue(eid);
104         exit_status = return_var->integer;
105
106         ejsClose();
107
108         talloc_free(mem_ctx);
109
110         return exit_status;
111 }