2 Unix SMB/CIFS implementation.
4 utility functions for manipulating mpr variables in ejs calls
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 "lib/ejs/ejs.h"
25 #include "lib/ldb/include/ldb.h"
28 find a mpr component, allowing for sub objects, using the '.' convention
30 NTSTATUS mprGetVar(struct MprVar **v, const char *name)
32 const char *p = strchr(name, '.');
36 *v = mprGetProperty(*v, name, NULL);
38 DEBUG(1,("mprGetVar unable to find '%s'\n", name));
39 return NT_STATUS_INVALID_PARAMETER;
43 objname = talloc_strndup(mprMemCtx(), name, p-name);
44 NT_STATUS_HAVE_NO_MEMORY(objname);
45 *v = mprGetProperty(*v, objname, NULL);
46 NT_STATUS_HAVE_NO_MEMORY(*v);
47 status = mprGetVar(v, p+1);
54 set a mpr component, allowing for sub objects, using the '.' convention
55 destroys 'val' after setting
57 NTSTATUS mprSetVar(struct MprVar *v, const char *name, struct MprVar val)
59 const char *p = strchr(name, '.');
64 v2 = mprSetProperty(v, name, &val);
66 DEBUG(1,("mprSetVar unable to set '%s'\n", name));
67 return NT_STATUS_INVALID_PARAMETER_MIX;
72 objname = talloc_strndup(mprMemCtx(), name, p-name);
73 if (objname == NULL) {
74 return NT_STATUS_NO_MEMORY;
76 v2 = mprGetProperty(v, objname, NULL);
78 mprSetVar(v, objname, mprCreateObjVar(objname, MPR_DEFAULT_HASH_SIZE));
79 v2 = mprGetProperty(v, objname, NULL);
81 status = mprSetVar(v2, p+1, val);
89 add an indexed array element to a property
91 void mprAddArray(struct MprVar *var, int i, struct MprVar v)
94 mprItoa(i, idx, sizeof(idx));
95 mprSetVar(var, idx, v);
96 mprSetVar(var, "length", mprCreateIntegerVar(i+1));
100 construct a MprVar from a list
102 struct MprVar mprList(const char *name, const char **list)
107 var = mprCreateObjVar(name, MPR_DEFAULT_HASH_SIZE);
108 for (i=0;list && list[i];i++) {
109 mprAddArray(&var, i, mprCreateStringVar(list[i], 1));
112 mprSetVar(&var, "length", mprCreateIntegerVar(i));
118 construct a string MprVar from a lump of data
120 struct MprVar mprData(const uint8_t *p, size_t length)
123 char *s = talloc_strndup(mprMemCtx(), p, length);
125 return mprCreateUndefinedVar();
127 var = mprCreateStringVar(s, 1);
133 turn a ldb_message into a ejs object variable
135 struct MprVar mprLdbMessage(struct ldb_message *msg)
139 /* we force some attributes to always be an array in the
140 returned structure. This makes the scripting easier, as you don't
141 need a special case for the single value case */
142 const char *multivalued[] = { "objectClass", "memberOf", "privilege",
145 var = mprCreateObjVar(msg->dn, MPR_DEFAULT_HASH_SIZE);
147 for (i=0;i<msg->num_elements;i++) {
148 struct ldb_message_element *el = &msg->elements[i];
150 if (el->num_values == 1 &&
151 !str_list_check_ci(multivalued, el->name)) {
152 val = mprData(el->values[0].data, el->values[0].length);
155 val = mprCreateObjVar(el->name, MPR_DEFAULT_HASH_SIZE);
156 for (j=0;j<el->num_values;j++) {
158 mprData(el->values[j].data,
159 el->values[j].length));
162 mprSetVar(&var, el->name, val);
165 /* add the dn if it is not already specified */
166 if (mprGetProperty(&var, "dn", 0) == 0) {
167 mprSetVar(&var, "dn", mprCreateStringVar(msg->dn, 1));
175 turn an array of ldb_messages into a ejs object variable
177 struct MprVar mprLdbArray(struct ldb_message **msg, int count, const char *name)
182 res = mprCreateObjVar(name?name:"(NULL)", MPR_DEFAULT_HASH_SIZE);
183 for (i=0;i<count;i++) {
184 mprAddArray(&res, i, mprLdbMessage(msg[i]));
191 turn a MprVar string variable into a const char *
193 const char *mprToString(const struct MprVar *v)
195 if (v->type != MPR_TYPE_STRING) return NULL;
200 turn a MprVar integer variable into an int
202 int mprToInt(const struct MprVar *v)
204 if (v->type != MPR_TYPE_INT) return 0;
209 turn a MprVar object variable into a string list
210 this assumes the object variable consists only of strings
212 const char **mprToList(TALLOC_CTX *mem_ctx, struct MprVar *v)
214 const char **list = NULL;
217 if (v->type != MPR_TYPE_OBJECT ||
218 v->properties == NULL) {
221 for (el=mprGetFirstProperty(v, MPR_ENUM_DATA);
223 el=mprGetNextProperty(v, el, MPR_ENUM_DATA)) {
224 const char *s = mprToString(el);
226 list = str_list_add(list, s);
229 talloc_steal(mem_ctx, list);
234 turn a NTSTATUS into a MprVar object with lots of funky properties
236 struct MprVar mprNTSTATUS(NTSTATUS status)
240 res = mprCreateObjVar("ntstatus", MPR_DEFAULT_HASH_SIZE);
242 mprSetVar(&res, "errstr", mprCreateStringVar(nt_errstr(status), 1));
243 mprSetVar(&res, "v", mprCreateIntegerVar(NT_STATUS_V(status)));
244 mprSetVar(&res, "is_ok", mprCreateBoolVar(NT_STATUS_IS_OK(status)));
245 mprSetVar(&res, "is_err", mprCreateBoolVar(NT_STATUS_IS_ERR(status)));
251 turn a WERROR into a MprVar object with lots of funky properties
253 struct MprVar mprWERROR(WERROR status)
257 res = mprCreateObjVar("werror", MPR_DEFAULT_HASH_SIZE);
259 mprSetVar(&res, "errstr", mprCreateStringVar(win_errstr(status), 1));
260 mprSetVar(&res, "v", mprCreateIntegerVar(W_ERROR_V(status)));
261 mprSetVar(&res, "is_ok", mprCreateBoolVar(W_ERROR_IS_OK(status)));
262 mprSetVar(&res, "is_err", mprCreateBoolVar(!W_ERROR_IS_OK(status)));
269 set a pointer in a existing MprVar
271 void mprSetPtr(struct MprVar *v, const char *propname, const void *p)
273 mprSetVar(v, propname, mprCreatePtrVar(discard_const(p), NULL));
277 set a pointer in a existing MprVar, making it a child of the property
279 void mprSetPtrChild(struct MprVar *v, const char *propname, const void *p)
281 mprSetVar(v, propname, mprCreatePtrVar(discard_const(p), NULL));
282 talloc_steal(mprGetProperty(v, propname, NULL), p);
286 get a pointer from a MprVar
288 void *mprGetPtr(struct MprVar *v, const char *propname)
291 val = mprGetProperty(v, propname, NULL);
295 if (val->type != MPR_TYPE_PTR) {
302 set the return value then free the variable
304 void mpr_Return(int eid, struct MprVar v)
306 ejsSetReturnValue(eid, v);
311 set the return value then free the variable
313 void mpr_ReturnString(int eid, const char *s)
315 mpr_Return(eid, mprCreateStringVar(s, False));