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