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