0169e0970975770f935833919c01b3aa41ddc987
[metze/samba/wip.git] / source4 / scripting / ejs / smbcalls_param.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    provide hooks into smbd C calls from ejs scripts
5
6    Copyright (C) Jelmer Vernooij 2005
7    
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.
12    
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.
17    
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.
21 */
22
23 #include "includes.h"
24 #include "scripting/ejs/smbcalls.h"
25 #include "lib/appweb/ejs/ejs.h"
26 #include "param/param.h"
27
28 /*
29   get parameter
30
31   value = param.get("name");
32   value = param.get("section", "name");
33 */
34 static int ejs_param_get(MprVarHandle eid, int argc, char **argv)
35 {
36         struct param_context *ctx;
37         const char *ret;
38         if (argc != 1 && argc != 2) {
39                 ejsSetErrorMsg(eid, "param.get invalid argument count");
40                 return -1;
41         }
42
43         ctx = mprGetThisPtr(eid, "param");
44         mprAssert(ctx);
45         
46         if (argc == 2) {
47                 ret = param_get_string(ctx, argv[0], argv[1]);
48         } else {
49                 ret = param_get_string(ctx, NULL, argv[0]);
50         }
51
52         if (ret) {
53                 mpr_Return(eid, mprString(ret));
54         } else {
55                 mpr_Return(eid, mprCreateUndefinedVar());
56         }
57         return 0;
58 }
59
60 /*
61   get list parameter
62
63   ok = param.get_list("name");
64   ok = param.get_list("section", "name");
65 */
66 static int ejs_param_get_list(MprVarHandle eid, int argc, char **argv)
67 {
68         struct param_context *ctx;
69         const char **ret;
70
71         if (argc != 1 && argc != 2) {
72                 ejsSetErrorMsg(eid, "param.get_list invalid argument count");
73                 return -1;
74         }
75
76         ctx = mprGetThisPtr(eid, "param");
77         mprAssert(ctx);
78         
79         if (argc == 2) {
80                 ret = param_get_string_list(ctx, argv[0], argv[1], NULL);
81         } else {
82                 ret = param_get_string_list(ctx, NULL, argv[0], NULL);
83         }
84
85         if (ret != NULL) {
86                 mpr_Return(eid, mprList("array", ret));
87         } else {
88                 mpr_Return(eid, mprCreateUndefinedVar());
89         }
90         return 0;
91 }
92
93 /*
94   set parameter
95
96   ok = param.set("name", "value");
97   ok = param.set("section", "name", "value");
98 */
99 static int ejs_param_set(MprVarHandle eid, int argc, struct MprVar **argv)
100 {
101         struct param_context *ctx;
102         const char **list;
103         const char *section, *paramname;
104         struct MprVar *value;
105         bool ret;
106         if (argc != 2 && argc != 3) {
107                 ejsSetErrorMsg(eid, "param.set invalid argument count");
108                 return -1;
109         }
110
111         ctx = mprGetThisPtr(eid, "param");
112         mprAssert(ctx);
113
114         
115         if (argc == 3) {
116                 section = mprToString(argv[0]);
117                 paramname = mprToString(argv[1]);
118                 value = argv[2];
119         } else {
120                 section = NULL;
121                 paramname = mprToString(argv[0]);
122                 value = argv[1];
123         }
124         
125         list = mprToList(mprMemCtx(), value);
126         if (list) {
127                 ret = param_set_string_list(ctx, section, paramname, list);
128         } else {
129                 ret = param_set_string(ctx, section, paramname, mprToString(value));
130         }
131
132         mpr_Return(eid, mprCreateBoolVar(ret));
133         return 0;
134 }
135
136 /* 
137   param data as a two-level array
138
139   data = param.data;
140   */
141 static int ejs_param_data(MprVarHandle eid, int argc, char **argv)
142 {
143         struct param_context *ctx;
144         struct MprVar ret;
145         struct param_section *sec;
146
147         if (argc != 0) {
148                 ejsSetErrorMsg(eid, "param.data does not take arguments");
149                 return -1;
150         }
151
152         ctx = mprGetThisPtr(eid, "param");
153         mprAssert(ctx);
154
155         ret = mprObject("array");
156
157         for (sec = ctx->sections; sec; sec = sec->next) {
158                 struct MprVar ps = mprObject("array");
159                 struct param *p;
160
161                 for (p = sec->parameters; p; p = p->next) {
162                         mprSetVar(&ps, p->name, mprString(p->value));
163                 }
164                 
165                 mprSetVar(&ret, sec->name, ps);
166         }
167
168         mpr_Return(eid, ret);
169         
170         return 0;
171 }
172
173 /*
174   load file
175   
176   ok = param.load(file);
177 */
178 static int ejs_param_load(MprVarHandle eid, int argc, char **argv)
179 {
180         struct param_context *ctx;
181         bool ret;
182
183         if (argc != 1) {
184                 ejsSetErrorMsg(eid, "param.load invalid argument count");
185                 return -1;
186         }
187
188         ctx = mprGetThisPtr(eid, "param");
189         mprAssert(ctx);
190
191         ret = param_read(ctx, argv[0]);
192         
193         mpr_Return(eid, mprCreateBoolVar(ret));
194         return 0;
195 }
196
197
198 /*
199   save file
200   
201   ok = param.save(file);
202 */
203 static int ejs_param_save(MprVarHandle eid, int argc, char **argv)
204 {
205         struct param_context *ctx;
206         bool ret;
207
208         if (argc != 1) {
209                 ejsSetErrorMsg(eid, "param.save invalid argument count");
210                 return -1;
211         }
212
213         ctx = mprGetThisPtr(eid, "param");
214         mprAssert(ctx);
215
216         ret = param_write(ctx, argv[0]);
217         
218         mpr_Return(eid, mprCreateBoolVar(ret));
219         return 0;
220 }
221
222 static void param_add_members(struct MprVar *obj)
223 {
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);
230 }
231
232 /*
233   initialise param ejs subsystem
234 */
235 static int ejs_param_init(MprVarHandle eid, int argc, struct MprVar **argv)
236 {
237         struct MprVar *obj = mprInitObject(eid, "param", argc, argv);
238
239         mprSetPtrChild(obj, "param", param_init(mprMemCtx()));
240
241         param_add_members(obj);
242
243         return 0;
244 }
245
246 struct MprVar mprParam(struct param_context *ctx)
247 {
248         struct MprVar mpv = mprObject("param");
249         mprSetPtrChild(&mpv, "param", ctx);
250         param_add_members(&mpv);
251         return mpv;
252 }
253
254 /*
255   setup C functions that be called from ejs
256 */
257 void smb_setup_ejs_param(void)
258 {
259         ejsDefineCFunction(-1, "param_init", ejs_param_init, NULL, MPR_VAR_SCRIPT_HANDLE);
260 }