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 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 "param/param.h"
25 #include "scripting/ejs/smbcalls.h"
30 return the type of a variable
32 static int ejs_typeof(MprVarHandle eid, int argc, struct MprVar **argv)
38 { MPR_TYPE_UNDEFINED, "undefined" },
39 { MPR_TYPE_NULL, "object" },
40 { MPR_TYPE_BOOL, "boolean" },
41 { MPR_TYPE_CFUNCTION, "function" },
42 { MPR_TYPE_FLOAT, "number" },
43 { MPR_TYPE_INT, "number" },
44 { MPR_TYPE_INT64, "number" },
45 { MPR_TYPE_OBJECT, "object" },
46 { MPR_TYPE_FUNCTION, "function" },
47 { MPR_TYPE_STRING, "string" },
48 { MPR_TYPE_STRING_CFUNCTION, "function" },
49 { MPR_TYPE_PTR, "pointer" }
52 const char *type = NULL;
54 if (argc != 1) return -1;
56 for (i=0;i<ARRAY_SIZE(types);i++) {
57 if (argv[0]->type == types[i].type) {
62 if (type == NULL) return -1;
64 mpr_ReturnString(eid, type);
69 return the native type of a variable
71 static int ejs_typeof_native(MprVarHandle eid, int argc, struct MprVar **argv)
77 { MPR_TYPE_UNDEFINED, "undefined" },
78 { MPR_TYPE_NULL, "null" },
79 { MPR_TYPE_BOOL, "boolean" },
80 { MPR_TYPE_CFUNCTION, "c_function" },
81 { MPR_TYPE_FLOAT, "float" },
82 { MPR_TYPE_INT, "integer" },
83 { MPR_TYPE_INT64, "integer64" },
84 { MPR_TYPE_OBJECT, "object" },
85 { MPR_TYPE_FUNCTION, "js_function" },
86 { MPR_TYPE_STRING, "string" },
87 { MPR_TYPE_STRING_CFUNCTION, "string_c_function" },
88 { MPR_TYPE_PTR, "pointer" }
91 const char *type = NULL;
93 if (argc != 1) return -1;
95 for (i=0;i<ARRAY_SIZE(types);i++) {
96 if (argv[0]->type == types[i].type) {
101 if (type == NULL) return -1;
103 mpr_ReturnString(eid, type);
108 libinclude() allows you to include js files using a search path specified
109 in "js include =" in smb.conf.
111 static int ejs_libinclude(int eid, int argc, char **argv)
114 const char **js_include = lp_js_include(global_loadparm);
116 if (js_include == NULL || js_include[0] == NULL) {
117 ejsSetErrorMsg(eid, "js include path not set");
121 for (i = 0; i < argc; i++) {
122 const char *script = argv[i];
123 struct MprVar result;
127 /* use specfied path to search for requested file */
128 for (j=0;js_include[j];j++) {
129 path = talloc_asprintf(mprMemCtx(), "%s/%s", js_include[j], script);
133 if (file_exist(path)) {
135 ret = ejsEvalFile(eid, path, &result, &emsg);
138 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);
164 static void (*ejs_exception_handler) (const char *) = NULL;
166 _PUBLIC_ void ejs_exception(const char *reason)
168 ejs_exception_handler(reason);
172 setup C functions that be called from ejs
174 void smb_setup_ejs_functions(void (*exception_handler)(const char *))
176 init_module_fn static_init[] = STATIC_smbcalls_MODULES;
177 init_module_fn *shared_init;
179 ejs_exception_handler = exception_handler;
182 smb_setup_ejs_options();
183 smb_setup_ejs_credentials();
184 smb_setup_ejs_param();
185 smb_setup_ejs_literal();
187 shared_init = load_samba_modules(NULL, "smbcalls");
189 run_init_functions(static_init);
190 run_init_functions(shared_init);
192 talloc_free(shared_init);
194 ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE);
195 ejsDefineCFunction(-1, "nativeTypeOf", ejs_typeof_native, NULL, MPR_VAR_SCRIPT_HANDLE);
196 ejsDefineStringCFunction(-1, "libinclude", ejs_libinclude, NULL, MPR_VAR_SCRIPT_HANDLE);
197 ejsDefineCFunction(-1, "version", ejs_version, NULL, MPR_VAR_SCRIPT_HANDLE);