2 Unix SMB/CIFS implementation.
4 provide hooks into smbd C calls from ejs scripts
6 Copyright (C) Jelmer Vernooij 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "scripting/ejs/smbcalls.h"
25 #include "lib/appweb/ejs/ejs.h"
26 #include "param/generic.h"
27 #include "dynconfig.h"
32 value = param.get("name");
33 value = param.get("section", "name");
35 static int ejs_param_get(MprVarHandle eid, int argc, char **argv)
37 struct param_context *ctx;
39 if (argc != 1 && argc != 2) {
40 ejsSetErrorMsg(eid, "param.get invalid argument count");
44 ctx = mprGetThisPtr(eid, "param");
48 ret = param_get_string(ctx, argv[0], argv[1]);
50 ret = param_get_string(ctx, NULL, argv[0]);
54 mpr_Return(eid, mprString(ret));
56 mpr_Return(eid, mprCreateUndefinedVar());
64 ok = param.get_list("name");
65 ok = param.get_list("section", "name");
67 static int ejs_param_get_list(MprVarHandle eid, int argc, char **argv)
69 struct param_context *ctx;
72 if (argc != 1 && argc != 2) {
73 ejsSetErrorMsg(eid, "param.get_list invalid argument count");
77 ctx = mprGetThisPtr(eid, "param");
81 ret = param_get_string_list(ctx, argv[0], argv[1], NULL);
83 ret = param_get_string_list(ctx, NULL, argv[0], NULL);
87 mpr_Return(eid, mprList("array", ret));
89 mpr_Return(eid, mprCreateUndefinedVar());
97 ok = param.set("name", "value");
98 ok = param.set("section", "name", "value");
100 static int ejs_param_set(MprVarHandle eid, int argc, struct MprVar **argv)
102 struct param_context *ctx;
104 const char *section, *paramname;
105 struct MprVar *value;
107 if (argc != 2 && argc != 3) {
108 ejsSetErrorMsg(eid, "param.set invalid argument count");
112 ctx = mprGetThisPtr(eid, "param");
117 section = mprToString(argv[0]);
118 paramname = mprToString(argv[1]);
122 paramname = mprToString(argv[0]);
126 list = mprToList(mprMemCtx(), value);
128 ret = param_set_string_list(ctx, section, paramname, list);
130 ret = param_set_string(ctx, section, paramname, mprToString(value));
133 mpr_Return(eid, mprCreateBoolVar(ret));
138 param data as a two-level array
142 static int ejs_param_data(MprVarHandle eid, int argc, char **argv)
144 struct param_context *ctx;
146 struct param_section *sec;
149 ejsSetErrorMsg(eid, "param.data does not take arguments");
153 ctx = mprGetThisPtr(eid, "param");
156 ret = mprObject("array");
158 for (sec = ctx->sections; sec; sec = sec->next) {
159 struct MprVar ps = mprObject("array");
162 for (p = sec->parameters; p; p = p->next) {
163 mprSetVar(&ps, p->name, mprString(p->value));
166 mprSetVar(&ret, sec->name, ps);
169 mpr_Return(eid, ret);
177 ok = param.load(file);
179 static int ejs_param_load(MprVarHandle eid, int argc, char **argv)
181 struct param_context *ctx;
185 ejsSetErrorMsg(eid, "param.load invalid argument count");
189 ctx = mprGetThisPtr(eid, "param");
192 ret = param_read(ctx, argv[0]);
194 mpr_Return(eid, mprCreateBoolVar(ret));
202 ok = param.save(file);
204 static int ejs_param_save(MprVarHandle eid, int argc, char **argv)
206 struct param_context *ctx;
210 ejsSetErrorMsg(eid, "param.save invalid argument count");
214 ctx = mprGetThisPtr(eid, "param");
217 ret = param_write(ctx, argv[0]);
219 mpr_Return(eid, mprCreateBoolVar(ret));
223 static void param_add_members(struct MprVar *obj)
225 mprSetStringCFunction(obj, "get", ejs_param_get);
226 mprSetStringCFunction(obj, "get_list", ejs_param_get_list);
227 mprSetCFunction(obj, "set", ejs_param_set);
228 mprSetStringCFunction(obj, "load", ejs_param_load);
229 mprSetStringCFunction(obj, "save", ejs_param_save);
230 mprSetStringCFunction(obj, "data", ejs_param_data);
234 initialise param ejs subsystem
236 static int ejs_param_init(MprVarHandle eid, int argc, struct MprVar **argv)
238 struct MprVar *obj = mprInitObject(eid, "param", argc, argv);
240 mprSetPtrChild(obj, "param", param_init(mprMemCtx()));
242 param_add_members(obj);
247 struct MprVar mprParam(struct param_context *ctx)
249 struct MprVar mpv = mprObject("param");
250 mprSetPtrChild(&mpv, "param", ctx);
251 param_add_members(&mpv);
256 setup C functions that be called from ejs
258 void smb_setup_ejs_param(void)
260 ejsDefineCFunction(-1, "param_init", ejs_param_init, NULL, MPR_VAR_SCRIPT_HANDLE);