2 Unix SMB/CIFS implementation.
4 provide hooks into smbd C calls from ejs scripts
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Tim Potter 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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "lib/appweb/ejs/ejs.h"
26 #include "scripting/ejs/smbcalls.h"
31 return the type of a variable
33 static int ejs_typeof(MprVarHandle eid, int argc, struct MprVar **argv)
39 { MPR_TYPE_UNDEFINED, "undefined" },
40 { MPR_TYPE_NULL, "object" },
41 { MPR_TYPE_BOOL, "boolean" },
42 { MPR_TYPE_CFUNCTION, "function" },
43 { MPR_TYPE_FLOAT, "number" },
44 { MPR_TYPE_INT, "number" },
45 { MPR_TYPE_INT64, "number" },
46 { MPR_TYPE_OBJECT, "object" },
47 { MPR_TYPE_FUNCTION, "function" },
48 { MPR_TYPE_STRING, "string" },
49 { MPR_TYPE_STRING_CFUNCTION, "function" },
50 { MPR_TYPE_PTR, "pointer" }
53 const char *type = NULL;
55 if (argc != 1) return -1;
57 for (i=0;i<ARRAY_SIZE(types);i++) {
58 if (argv[0]->type == types[i].type) {
63 if (type == NULL) return -1;
65 mpr_ReturnString(eid, type);
70 return the native type of a variable
72 static int ejs_typeof_native(MprVarHandle eid, int argc, struct MprVar **argv)
78 { MPR_TYPE_UNDEFINED, "undefined" },
79 { MPR_TYPE_NULL, "null" },
80 { MPR_TYPE_BOOL, "boolean" },
81 { MPR_TYPE_CFUNCTION, "c_function" },
82 { MPR_TYPE_FLOAT, "float" },
83 { MPR_TYPE_INT, "integer" },
84 { MPR_TYPE_INT64, "integer64" },
85 { MPR_TYPE_OBJECT, "object" },
86 { MPR_TYPE_FUNCTION, "js_function" },
87 { MPR_TYPE_STRING, "string" },
88 { MPR_TYPE_STRING_CFUNCTION, "string_c_function" },
89 { MPR_TYPE_PTR, "pointer" }
92 const char *type = NULL;
94 if (argc != 1) return -1;
96 for (i=0;i<ARRAY_SIZE(types);i++) {
97 if (argv[0]->type == types[i].type) {
102 if (type == NULL) return -1;
104 mpr_ReturnString(eid, type);
109 libinclude() allows you to include js files using a search path specified
110 in "js include =" in smb.conf.
112 static int ejs_libinclude(int eid, int argc, char **argv)
115 const char **js_include = lp_js_include();
117 if (js_include == NULL || js_include[0] == NULL) {
118 ejsSetErrorMsg(eid, "js include path not set");
122 for (i = 0; i < argc; i++) {
123 const char *script = argv[i];
125 for (j=0;js_include[j];j++) {
127 path = talloc_asprintf(mprMemCtx(), "%s/%s", js_include[j], script);
131 if (file_exist(path)) {
133 struct MprVar result;
136 ret = ejsEvalFile(eid, path, &result, &emsg);
139 ejsSetErrorMsg(eid, "%s: %s", script, emsg);
146 if (js_include[j] == NULL) {
147 ejsSetErrorMsg(eid, "unable to include '%s'", script);
155 return the current version
157 static int ejs_version(MprVarHandle eid, int argc, struct MprVar **argv)
159 mpr_ReturnString(eid, SAMBA_VERSION_STRING);
165 * jsonrpc_include() allows you to include jsonrpc files from a path based at
166 * "jsonrpc services directory =" in smb.conf.
168 static int jsonrpc_include(int eid, int argc, char **argv)
173 const char *jsonrpc_services_dir = lp_jsonrpc_services_dir();
174 struct MprVar result;
177 if (jsonrpc_services_dir == NULL || jsonrpc_services_dir == NULL) {
178 ejsSetErrorMsg(eid, "'jsonrpc services directory' not set");
183 mpr_Return(eid, mprCreateIntegerVar(-1));
187 path = talloc_asprintf(mprMemCtx(), "%s/%s",
188 jsonrpc_services_dir,
191 mpr_Return(eid, mprCreateIntegerVar(-1));
195 if (file_exist(path)) {
196 ret = ejsEvalFile(eid, path, &result, &emsg);
198 ejsSetErrorMsg(eid, "Could not eval file");
199 printf("file found; ret=%d (%s)\n", ret, emsg);
203 mpr_Return(eid, mprCreateIntegerVar(ret));
209 static void (*ejs_exception_handler) (const char *) = NULL;
211 _PUBLIC_ void ejs_exception(const char *reason)
213 ejs_exception_handler(reason);
217 setup C functions that be called from ejs
219 void smb_setup_ejs_functions(void (*exception_handler)(const char *))
221 init_module_fn static_init[] = STATIC_smbcalls_MODULES;
222 init_module_fn *shared_init;
224 ejs_exception_handler = exception_handler;
227 smb_setup_ejs_options();
228 smb_setup_ejs_credentials();
229 smb_setup_ejs_param();
230 smb_setup_ejs_literal();
234 shared_init = load_samba_modules(NULL, "smbcalls");
236 run_init_functions(static_init);
237 run_init_functions(shared_init);
239 talloc_free(shared_init);
241 ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE);
242 ejsDefineCFunction(-1, "nativeTypeOf", ejs_typeof_native, NULL, MPR_VAR_SCRIPT_HANDLE);
243 ejsDefineStringCFunction(-1, "libinclude", ejs_libinclude, NULL, MPR_VAR_SCRIPT_HANDLE);
244 ejsDefineCFunction(-1, "version", ejs_version, NULL, MPR_VAR_SCRIPT_HANDLE);
245 ejsDefineStringCFunction(-1, "jsonrpc_include", jsonrpc_include, NULL, MPR_VAR_SCRIPT_HANDLE);