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 static void mprAddArray(struct MprVar *var, int i, struct MprVar v)
94 mprItoa(i, idx, sizeof(idx));
95 mprSetVar(var, idx, v);
99 construct a MprVar from a list
101 struct MprVar mprList(const char *name, const char **list)
106 var = mprCreateObjVar(name, MPR_DEFAULT_HASH_SIZE);
107 for (i=0;list && list[i];i++) {
108 mprAddArray(&var, i, mprCreateStringVar(list[i], 1));
114 construct a string MprVar from a lump of data
116 struct MprVar mprData(const uint8_t *p, size_t length)
119 char *s = talloc_strndup(mprMemCtx(), p, length);
121 return mprCreateUndefinedVar();
123 var = mprCreateStringVar(s, 1);
129 turn a ldb_message into a ejs object variable
131 struct MprVar mprLdbMessage(struct ldb_message *msg)
135 /* we force some attributes to always be an array in the
136 returned structure. This makes the scripting easier, as you don't
137 need a special case for the single value case */
138 const char *multivalued[] = { "objectClass", "memberOf", "privilege",
141 var = mprCreateObjVar(msg->dn, MPR_DEFAULT_HASH_SIZE);
143 for (i=0;i<msg->num_elements;i++) {
144 struct ldb_message_element *el = &msg->elements[i];
146 if (el->num_values == 1 &&
147 !str_list_check_ci(multivalued, el->name)) {
148 val = mprData(el->values[0].data, el->values[0].length);
151 val = mprCreateObjVar(el->name, MPR_DEFAULT_HASH_SIZE);
152 for (j=0;j<el->num_values;j++) {
154 mprData(el->values[j].data,
155 el->values[j].length));
158 mprSetVar(&var, el->name, val);
161 /* add the dn if it is not already specified */
162 if (mprGetProperty(&var, "dn", 0) == 0) {
163 mprSetVar(&var, "dn", mprCreateStringVar(msg->dn, 1));
171 turn an array of ldb_messages into a ejs object variable
173 struct MprVar mprLdbArray(struct ldb_message **msg, int count, const char *name)
178 res = mprCreateObjVar(name?name:"(NULL)", MPR_DEFAULT_HASH_SIZE);
179 for (i=0;i<count;i++) {
180 mprAddArray(&res, i, mprLdbMessage(msg[i]));
182 mprSetPropertyValue(&res, "length", mprCreateIntegerVar(i));
188 turn a MprVar string variable into a const char *
190 const char *mprToString(const struct MprVar *v)
192 if (v->type != MPR_TYPE_STRING) return NULL;
197 turn a MprVar integer variable into an int
199 int mprToInt(const struct MprVar *v)
201 if (v->type != MPR_TYPE_INT) return 0;
206 turn a MprVar object variable into a string list
207 this assumes the object variable consists only of strings
209 const char **mprToList(TALLOC_CTX *mem_ctx, struct MprVar *v)
211 const char **list = NULL;
214 if (v->type != MPR_TYPE_OBJECT ||
215 v->properties == NULL) {
218 for (el=mprGetFirstProperty(v, MPR_ENUM_DATA);
220 el=mprGetNextProperty(v, el, MPR_ENUM_DATA)) {
221 const char *s = mprToString(el);
223 list = str_list_add(list, s);
226 talloc_steal(mem_ctx, list);
231 turn a NTSTATUS into a MprVar object with lots of funky properties
233 struct MprVar mprNTSTATUS(NTSTATUS status)
237 res = mprCreateObjVar("ntstatus", MPR_DEFAULT_HASH_SIZE);
239 mprSetVar(&res, "errstr", mprCreateStringVar(nt_errstr(status), 1));
240 mprSetVar(&res, "v", mprCreateIntegerVar(NT_STATUS_V(status)));
241 mprSetVar(&res, "is_ok", mprCreateBoolVar(NT_STATUS_IS_OK(status)));
242 mprSetVar(&res, "is_err", mprCreateBoolVar(NT_STATUS_IS_ERR(status)));
248 turn a WERROR into a MprVar object with lots of funky properties
250 struct MprVar mprWERROR(WERROR status)
254 res = mprCreateObjVar("werror", MPR_DEFAULT_HASH_SIZE);
256 mprSetVar(&res, "errstr", mprCreateStringVar(win_errstr(status), 1));
257 mprSetVar(&res, "v", mprCreateIntegerVar(W_ERROR_V(status)));
258 mprSetVar(&res, "is_ok", mprCreateBoolVar(W_ERROR_IS_OK(status)));
259 mprSetVar(&res, "is_err", mprCreateBoolVar(!W_ERROR_IS_OK(status)));
266 set a pointer in a existing MprVar
268 void mprSetPtr(struct MprVar *v, const char *propname, const void *p)
270 mprSetVar(v, propname, mprCreatePtrVar(discard_const(p), NULL));
274 set a pointer in a existing MprVar, making it a child of the property
276 void mprSetPtrChild(struct MprVar *v, const char *propname, const void *p)
278 mprSetVar(v, propname, mprCreatePtrVar(discard_const(p), NULL));
279 talloc_steal(mprGetProperty(v, propname, NULL), p);
283 get a pointer from a MprVar
285 void *mprGetPtr(struct MprVar *v, const char *propname)
288 val = mprGetProperty(v, propname, NULL);
292 if (val->type != MPR_TYPE_PTR) {
299 set the return value then free the variable
301 void mpr_Return(int eid, struct MprVar v)
303 ejsSetReturnValue(eid, v);
308 set the return value then free the variable
310 void mpr_ReturnString(int eid, const char *s)
312 mpr_Return(eid, mprCreateStringVar(s, False));