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);
67 allow access to loadparm variables from inside esp scripts in swat
69 can be called in 4 ways:
71 v = lpGet("type:parm"); gets a parametric variable
72 v = lpGet("share", "type:parm"); gets a parametric variable on a share
73 v = lpGet("parm"); gets a global variable
74 v = lpGet("share", "parm"); gets a share variable
76 the returned variable is a ejs object. It is an array object for lists.
78 static int esp_lpGet(struct EspRequest *ep, int argc, char **argv)
80 struct parm_struct *parm = NULL;
81 void *parm_ptr = NULL;
84 if (argc < 1) return -1;
87 /* its a share parameter */
88 int snum = lp_servicenumber(argv[0]);
92 if (strchr(argv[1], ':')) {
93 /* its a parametric option on a share */
94 const char *type = talloc_strndup(ep, argv[1], strcspn(argv[1], ":"));
95 const char *option = strchr(argv[1], ':') + 1;
97 if (type == NULL || option == NULL) return -1;
98 value = lp_get_parametric(snum, type, option);
99 if (value == NULL) return -1;
100 espSetReturnString(ep, value);
104 parm = lp_parm_struct(argv[1]);
105 if (parm == NULL || parm->class == P_GLOBAL) {
108 parm_ptr = lp_parm_ptr(snum, parm);
109 } else if (strchr(argv[0], ':')) {
110 /* its a global parametric option */
111 const char *type = talloc_strndup(ep, argv[0], strcspn(argv[0], ":"));
112 const char *option = strchr(argv[0], ':') + 1;
114 if (type == NULL || option == NULL) return -1;
115 value = lp_get_parametric(-1, type, option);
116 if (value == NULL) return -1;
117 espSetReturnString(ep, value);
120 /* its a global parameter */
121 parm = lp_parm_struct(argv[0]);
122 if (parm == NULL) return -1;
123 parm_ptr = parm->ptr;
126 if (parm == NULL || parm_ptr == NULL) {
130 /* construct and return the right type of ejs object */
131 switch (parm->type) {
134 espSetReturnString(ep, *(char **)parm_ptr);
137 espSetReturn(ep, mprCreateBoolVar(*(BOOL *)parm_ptr));
140 espSetReturn(ep, mprCreateIntegerVar(*(int *)parm_ptr));
143 for (i=0; parm->enum_list[i].name; i++) {
144 if (*(int *)parm_ptr == parm->enum_list[i].value) {
145 espSetReturnString(ep, parm->enum_list[i].name);
152 const char **list = *(const char ***)parm_ptr;
154 var = mprCreateObjVar(parm->label, 10);
155 for (i=0;list[i];i++) {
158 mprItoa(i, idx, sizeof(idx));
159 val = mprCreateStringVar(list[i], 1);
160 mprCreateProperty(&var, idx, &val);
162 espSetReturn(ep, var);
172 setup the C functions that be called from ejs
174 void http_setup_ejs_functions(void)
176 espDefineStringCFunction(NULL, "lpGet", esp_lpGet, NULL);
177 espDefineCFunction(NULL, "typeof", esp_typeof, NULL);