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/param.h"
31 value = param.get("name");
32 value = param.get("section", "name");
34 static int ejs_param_get(MprVarHandle eid, int argc, char **argv)
36 struct param_context *ctx;
38 if (argc != 1 && argc != 2) {
39 ejsSetErrorMsg(eid, "param.get invalid argument count");
43 ctx = mprGetThisPtr(eid, "param");
47 ret = param_get_string(ctx, argv[0], argv[1]);
49 ret = param_get_string(ctx, NULL, argv[0]);
53 mpr_Return(eid, mprString(ret));
55 mpr_Return(eid, mprCreateUndefinedVar());
63 ok = param.get_list("name");
64 ok = param.get_list("section", "name");
66 static int ejs_param_get_list(MprVarHandle eid, int argc, char **argv)
68 struct param_context *ctx;
71 if (argc != 1 && argc != 2) {
72 ejsSetErrorMsg(eid, "param.get_list invalid argument count");
76 ctx = mprGetThisPtr(eid, "param");
80 ret = param_get_string_list(ctx, argv[0], argv[1], NULL);
82 ret = param_get_string_list(ctx, NULL, argv[0], NULL);
86 mpr_Return(eid, mprList("array", ret));
88 mpr_Return(eid, mprCreateUndefinedVar());
96 ok = param.set("name", "value");
97 ok = param.set("section", "name", "value");
99 static int ejs_param_set(MprVarHandle eid, int argc, struct MprVar **argv)
101 struct param_context *ctx;
103 const char *section, *paramname;
104 struct MprVar *value;
106 if (argc != 2 && argc != 3) {
107 ejsSetErrorMsg(eid, "param.set invalid argument count");
111 ctx = mprGetThisPtr(eid, "param");
116 section = mprToString(argv[0]);
117 paramname = mprToString(argv[1]);
121 paramname = mprToString(argv[0]);
125 list = mprToList(mprMemCtx(), value);
127 ret = param_set_string_list(ctx, section, paramname, list);
129 ret = param_set_string(ctx, section, paramname, mprToString(value));
132 mpr_Return(eid, mprCreateBoolVar(ret));
137 param data as a two-level array
141 static int ejs_param_data(MprVarHandle eid, int argc, char **argv)
143 struct param_context *ctx;
145 struct param_section *sec;
148 ejsSetErrorMsg(eid, "param.data does not take arguments");
152 ctx = mprGetThisPtr(eid, "param");
155 ret = mprObject("array");
157 for (sec = ctx->sections; sec; sec = sec->next) {
158 struct MprVar ps = mprObject("array");
161 for (p = sec->parameters; p; p = p->next) {
162 mprSetVar(&ps, p->name, mprString(p->value));
165 mprSetVar(&ret, sec->name, ps);
168 mpr_Return(eid, ret);
176 ok = param.load(file);
178 static int ejs_param_load(MprVarHandle eid, int argc, char **argv)
180 struct param_context *ctx;
184 ejsSetErrorMsg(eid, "param.load invalid argument count");
188 ctx = mprGetThisPtr(eid, "param");
191 ret = param_read(ctx, argv[0]);
193 mpr_Return(eid, mprCreateBoolVar(ret));
201 ok = param.save(file);
203 static int ejs_param_save(MprVarHandle eid, int argc, char **argv)
205 struct param_context *ctx;
209 ejsSetErrorMsg(eid, "param.save invalid argument count");
213 ctx = mprGetThisPtr(eid, "param");
216 ret = param_write(ctx, argv[0]);
218 mpr_Return(eid, mprCreateBoolVar(ret));
222 static void param_add_members(struct MprVar *obj)
224 mprSetStringCFunction(obj, "get", ejs_param_get);
225 mprSetStringCFunction(obj, "get_list", ejs_param_get_list);
226 mprSetCFunction(obj, "set", ejs_param_set);
227 mprSetStringCFunction(obj, "load", ejs_param_load);
228 mprSetStringCFunction(obj, "save", ejs_param_save);
229 mprSetStringCFunction(obj, "data", ejs_param_data);
233 initialise param ejs subsystem
235 static int ejs_param_init(MprVarHandle eid, int argc, struct MprVar **argv)
237 struct MprVar *obj = mprInitObject(eid, "param", argc, argv);
239 mprSetPtrChild(obj, "param", param_init(mprMemCtx()));
241 param_add_members(obj);
246 struct MprVar mprParam(struct param_context *ctx)
248 struct MprVar mpv = mprObject("param");
249 mprSetPtrChild(&mpv, "param", ctx);
250 param_add_members(&mpv);
255 setup C functions that be called from ejs
257 void smb_setup_ejs_param(void)
259 ejsDefineCFunction(-1, "param_init", ejs_param_init, NULL, MPR_VAR_SCRIPT_HANDLE);