2 Unix SMB/CIFS implementation.
4 provide hooks into C calls from esp scripts
6 Copyright (C) Andrew Tridgell 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 "web_server/esp/esp.h"
25 #include "param/loadparm.h"
29 return the type of a variable
31 static int esp_typeof(struct EspRequest *ep, int argc, struct MprVar **argv)
37 { MPR_TYPE_UNDEFINED, "undefined" },
38 { MPR_TYPE_NULL, "null" },
39 { MPR_TYPE_BOOL, "boolean" },
40 { MPR_TYPE_CFUNCTION, "function" },
41 { MPR_TYPE_FLOAT, "float" },
42 { MPR_TYPE_INT, "int" },
43 { MPR_TYPE_INT64, "int64" },
44 { MPR_TYPE_OBJECT, "object" },
45 { MPR_TYPE_FUNCTION, "function" },
46 { MPR_TYPE_STRING, "string" },
47 { MPR_TYPE_STRING_CFUNCTION, "function" }
50 const char *type = "unknown";
52 if (argc != 1) return -1;
54 for (i=0;i<ARRAY_SIZE(types);i++) {
55 if (argv[0]->type == types[i].type) {
61 espSetReturnString(ep, type);
66 setup a return of a string list
68 static void esp_returnlist(struct EspRequest *ep,
69 const char *name, const char **list)
74 var = mprCreateObjVar(name, ESP_HASH_SIZE);
75 for (i=0;list[i];i++) {
78 mprItoa(i, idx, sizeof(idx));
79 val = mprCreateStringVar(list[i], 1);
80 mprCreateProperty(&var, idx, &val);
82 espSetReturn(ep, var);
86 return a list of defined services
88 static int esp_lpServices(struct EspRequest *ep, int argc, char **argv)
92 if (argc != 0) return -1;
94 for (i=0;i<lp_numservices();i++) {
95 list = str_list_add(list, lp_servicename(i));
97 talloc_steal(ep, list);
98 esp_returnlist(ep, "services", list);
104 allow access to loadparm variables from inside esp scripts in swat
106 can be called in 4 ways:
108 v = lpGet("type:parm"); gets a parametric variable
109 v = lpGet("share", "type:parm"); gets a parametric variable on a share
110 v = lpGet("parm"); gets a global variable
111 v = lpGet("share", "parm"); gets a share variable
113 the returned variable is a ejs object. It is an array object for lists.
115 static int esp_lpGet(struct EspRequest *ep, int argc, char **argv)
117 struct parm_struct *parm = NULL;
118 void *parm_ptr = NULL;
121 if (argc < 1) return -1;
124 /* its a share parameter */
125 int snum = lp_servicenumber(argv[0]);
129 if (strchr(argv[1], ':')) {
130 /* its a parametric option on a share */
131 const char *type = talloc_strndup(ep, argv[1], strcspn(argv[1], ":"));
132 const char *option = strchr(argv[1], ':') + 1;
134 if (type == NULL || option == NULL) return -1;
135 value = lp_get_parametric(snum, type, option);
136 if (value == NULL) return -1;
137 espSetReturnString(ep, value);
141 parm = lp_parm_struct(argv[1]);
142 if (parm == NULL || parm->class == P_GLOBAL) {
145 parm_ptr = lp_parm_ptr(snum, parm);
146 } else if (strchr(argv[0], ':')) {
147 /* its a global parametric option */
148 const char *type = talloc_strndup(ep, argv[0], strcspn(argv[0], ":"));
149 const char *option = strchr(argv[0], ':') + 1;
151 if (type == NULL || option == NULL) return -1;
152 value = lp_get_parametric(-1, type, option);
153 if (value == NULL) return -1;
154 espSetReturnString(ep, value);
157 /* its a global parameter */
158 parm = lp_parm_struct(argv[0]);
159 if (parm == NULL) return -1;
160 parm_ptr = parm->ptr;
163 if (parm == NULL || parm_ptr == NULL) {
167 /* construct and return the right type of ejs object */
168 switch (parm->type) {
171 espSetReturnString(ep, *(char **)parm_ptr);
174 espSetReturn(ep, mprCreateBoolVar(*(BOOL *)parm_ptr));
177 espSetReturn(ep, mprCreateIntegerVar(*(int *)parm_ptr));
180 for (i=0; parm->enum_list[i].name; i++) {
181 if (*(int *)parm_ptr == parm->enum_list[i].value) {
182 espSetReturnString(ep, parm->enum_list[i].name);
188 esp_returnlist(ep, parm->label, *(const char ***)parm_ptr);
199 setup the C functions that be called from ejs
201 void http_setup_ejs_functions(void)
203 espDefineStringCFunction(NULL, "lpGet", esp_lpGet, NULL);
204 espDefineStringCFunction(NULL, "lpServices", esp_lpServices, NULL);
205 espDefineCFunction(NULL, "typeof", esp_typeof, NULL);