r9800: Add EJS interface to param. tridge, sorry this overlaps a bit
[kai/samba.git] / source / 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 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) {
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, char **argv)
101 {
102         struct param_context *ctx;
103         bool ret;
104         if (argc != 2 && argc != 3) {
105                 ejsSetErrorMsg(eid, "param.set invalid argument count");
106                 return -1;
107         }
108
109         ctx = mprGetThisPtr(eid, "param");
110         mprAssert(ctx);
111         
112         if (argc == 3) {
113                 ret = param_set_string(ctx, argv[0], argv[1], argv[2]);
114         } else {
115                 ret = param_set_string(ctx, NULL, argv[0], argv[2]);
116         }
117
118         mpr_Return(eid, mprCreateBoolVar(ret));
119         return 0;
120 }
121
122 /*
123   load file
124   
125   ok = param.load(file);
126 */
127 static int ejs_param_load(MprVarHandle eid, int argc, char **argv)
128 {
129         struct param_context *ctx;
130         bool ret;
131
132         if (argc != 1) {
133                 ejsSetErrorMsg(eid, "param.load invalid argument count");
134                 return -1;
135         }
136
137         ctx = mprGetThisPtr(eid, "param");
138         mprAssert(ctx);
139
140         ret = param_read(ctx, argv[0]);
141         
142         mpr_Return(eid, mprCreateBoolVar(ret));
143         return 0;
144 }
145
146 /*
147   save file
148   
149   ok = param.save(file);
150 */
151 static int ejs_param_save(MprVarHandle eid, int argc, char **argv)
152 {
153         struct param_context *ctx;
154         bool ret;
155
156         if (argc != 1) {
157                 ejsSetErrorMsg(eid, "param.save invalid argument count");
158                 return -1;
159         }
160
161         ctx = mprGetThisPtr(eid, "param");
162         mprAssert(ctx);
163
164         ret = param_write(ctx, argv[0]);
165         
166         mpr_Return(eid, mprCreateBoolVar(ret));
167         return 0;
168 }
169
170 static void param_add_members(struct MprVar *obj)
171 {
172         mprSetStringCFunction(obj, "get", ejs_param_get);
173         mprSetStringCFunction(obj, "get_list", ejs_param_get_list);
174         mprSetStringCFunction(obj, "set", ejs_param_set);
175         mprSetStringCFunction(obj, "load", ejs_param_load);
176         mprSetStringCFunction(obj, "save", ejs_param_save);
177 }
178
179 /*
180   initialise param ejs subsystem
181 */
182 static int ejs_param_init(MprVarHandle eid, int argc, struct MprVar **argv)
183 {
184         struct MprVar *obj = mprInitObject(eid, "param", argc, argv);
185
186         mprSetPtrChild(obj, "param", param_init(mprMemCtx()));
187
188         param_add_members(obj);
189
190         return 0;
191 }
192
193 struct MprVar mprParam(struct param_context *ctx)
194 {
195         struct MprVar mpv = mprObject("param");
196         mprSetPtrChild(&mpv, "param", ctx);
197         param_add_members(&mpv);
198         return mpv;
199 }
200
201 /*
202   setup C functions that be called from ejs
203 */
204 void smb_setup_ejs_param(void)
205 {
206         ejsDefineCFunction(-1, "param_init", ejs_param_init, NULL, MPR_VAR_SCRIPT_HANDLE);
207 }