2 Unix SMB/CIFS implementation.
4 Standalone client for ejs scripting.
6 Copyright (C) Tim Potter <tpot@samba.org> 2005
7 Copyright (C) Andrew Tridgell 2005
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.
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.
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/>.
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"
33 _NORETURN_ static void smbscript_ejs_exception(const char *reason)
35 Ejs *ep = ejsPtr(eid);
36 ejsSetErrorMsg(eid, "%s", reason);
37 fprintf(stderr, "%s", ep->error);
41 int main(int argc, const char **argv)
47 TALLOC_CTX *mem_ctx = talloc_new(NULL);
48 const char **argv_list = NULL;
50 struct MprVar *return_var;
55 if (getenv("SMB_CONF_PATH")) {
56 lp_load(getenv("SMB_CONF_PATH"));
58 lp_load(dyn_CONFIGFILE);
68 fprintf(stderr, "You must supply a script name\n");
74 if (ejsOpen(NULL, NULL, NULL) != 0) {
75 fprintf(stderr, "smbscript: ejsOpen(): unable to initialise "
80 smb_setup_ejs_functions(smbscript_ejs_exception);
82 if ((eid = ejsOpenEngine(handle, 0)) == (EjsId)-1) {
83 fprintf(stderr, "smbscript: ejsOpenEngine(): unable to "
84 "initialise an EJS engine\n");
88 /* setup ARGV[] in the ejs environment */
89 for (i=1;argv[i];i++) {
90 argv_list = str_list_add(argv_list, argv[i]);
92 talloc_steal(mem_ctx, argv_list);
93 mprSetVar(ejsGetGlobalObject(eid), "ARGV", mprList("ARGV", argv_list));
95 /* load the script and advance past interpreter line*/
96 script = file_load(fname, &script_size, mem_ctx);
99 fprintf(stderr, "Unable to load script from '%s'\n", fname);
103 /* allow scriptable js */
104 if (strncmp(script, "#!", 2) == 0) {
105 script += strcspn(script, "\r\n");
106 script += strspn(script, "\r\n");
108 /* and this copes with the ugly exec hack */
109 if (strncmp(script, "exec ", 5) == 0) {
110 script += strcspn(script, "\r\n");
111 script += strspn(script, "\r\n");
115 if (ejsEvalScript(eid, script, &result, &emsg) == -1) {
116 fprintf(stderr, "smbscript: ejsEvalScript(): %s\n", emsg);
120 return_var = ejsGetReturnValue(eid);
121 exit_status = mprVarToNumber(return_var);
125 talloc_free(mem_ctx);