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/appweb/ejs/ejs.h"
25 #include "lib/ldb/include/ldb.h"
28 return a default mpr object
30 struct MprVar mprObject(const char *name)
32 return ejsCreateObj(name && *name?name:"(NULL)", MPR_DEFAULT_HASH_SIZE);
36 return a empty mpr array
38 struct MprVar mprArray(const char *name)
40 return ejsCreateArray(name && *name?name:"(NULL)", 0);
44 find a mpr component, allowing for sub objects, using the '.' convention
46 NTSTATUS mprGetVar(struct MprVar **v, const char *name)
48 const char *p = strchr(name, '.');
52 *v = mprGetProperty(*v, name, NULL);
54 DEBUG(1,("mprGetVar unable to find '%s'\n", name));
55 return NT_STATUS_INVALID_PARAMETER;
59 objname = talloc_strndup(mprMemCtx(), name, p-name);
60 NT_STATUS_HAVE_NO_MEMORY(objname);
61 *v = mprGetProperty(*v, objname, NULL);
62 NT_STATUS_HAVE_NO_MEMORY(*v);
63 status = mprGetVar(v, p+1);
70 set a mpr component, allowing for sub objects, using the '.' convention
71 destroys 'val' after setting
73 NTSTATUS mprSetVar(struct MprVar *v, const char *name, struct MprVar val)
75 const char *p = strchr(name, '.');
80 v2 = mprSetProperty(v, name, &val);
82 DEBUG(1,("mprSetVar unable to set '%s'\n", name));
83 return NT_STATUS_INVALID_PARAMETER_MIX;
88 objname = talloc_strndup(mprMemCtx(), name, p-name);
89 if (objname == NULL) {
90 return NT_STATUS_NO_MEMORY;
92 v2 = mprGetProperty(v, objname, NULL);
94 mprSetVar(v, objname, mprObject(objname));
95 v2 = mprGetProperty(v, objname, NULL);
97 status = mprSetVar(v2, p+1, val);
105 add an indexed array element to a property
107 void mprAddArray(struct MprVar *var, int i, struct MprVar v)
110 mprItoa(i, idx, sizeof(idx));
111 mprSetVar(var, idx, v);
115 construct a MprVar from a list
117 struct MprVar mprList(const char *name, const char **list)
122 var = mprArray(name);
123 for (i=0;list && list[i];i++) {
124 mprAddArray(&var, i, mprString(list[i]));
130 construct a MprVar from a string, using NULL if needed
132 struct MprVar mprString(const char *s)
135 return mprCreatePtrVar(NULL);
137 return mprCreateStringVar(s, True);
141 construct a string MprVar from a lump of data
143 struct MprVar mprData(const uint8_t *p, size_t length)
146 char *s = talloc_strndup(mprMemCtx(), p, length);
148 return mprCreateUndefinedVar();
156 turn a ldb_message into a ejs object variable
158 static struct MprVar mprLdbMessage(struct ldb_context *ldb, struct ldb_message *msg)
162 /* we force some attributes to always be an array in the
163 returned structure. This makes the scripting easier, as you don't
164 need a special case for the single value case */
165 const char *multivalued[] = { "objectClass", "memberOf", "privilege",
168 var = mprObject(ldb_dn_linearize(msg, msg->dn));
170 for (i=0;i<msg->num_elements;i++) {
171 struct ldb_message_element *el = &msg->elements[i];
173 const struct ldb_attrib_handler *attr;
176 attr = ldb_attrib_handler(ldb, el->name);
181 if (el->num_values == 1 &&
182 !str_list_check_ci(multivalued, el->name)) {
183 if (attr->ldif_write_fn(ldb, msg, &el->values[0], &v) != 0) {
186 val = mprData(v.data, v.length);
189 val = mprArray(el->name);
190 for (j=0;j<el->num_values;j++) {
191 if (attr->ldif_write_fn(ldb, msg,
192 &el->values[j], &v) != 0) {
195 mprAddArray(&val, j, mprData(v.data, v.length));
198 mprSetVar(&var, el->name, val);
201 /* add the dn if it is not already specified */
202 if (mprGetProperty(&var, "dn", 0) == 0) {
203 mprSetVar(&var, "dn", mprString(ldb_dn_linearize(msg, msg->dn)));
208 return mprCreateUndefinedVar();
213 turn an array of ldb_messages into a ejs object variable
215 struct MprVar mprLdbArray(struct ldb_context *ldb,
216 struct ldb_message **msg, int count, const char *name)
221 res = mprArray(name);
222 for (i=0;i<count;i++) {
223 mprAddArray(&res, i, mprLdbMessage(ldb, msg[i]));
230 turn a MprVar string variable into a const char *
232 const char *mprToString(const struct MprVar *v)
235 mprReadProperty(v, 0);
237 if (!mprVarIsString(v->type)) return NULL;
242 turn a MprVar integer variable into an int
244 int mprToInt(const struct MprVar *v)
247 mprReadProperty(v, 0);
249 if (!mprVarIsNumber(v->type)) return 0;
250 return mprVarToNumber(v);
254 turn a MprVar object variable into a string list
255 this assumes the object variable consists only of strings
257 const char **mprToList(TALLOC_CTX *mem_ctx, struct MprVar *v)
259 const char **list = NULL;
262 if (v->type != MPR_TYPE_OBJECT ||
263 v->properties == NULL) {
266 for (el=mprGetFirstProperty(v, MPR_ENUM_DATA);
268 el=mprGetNextProperty(v, el, MPR_ENUM_DATA)) {
269 const char *s = mprToString(el);
271 list = str_list_add(list, s);
274 talloc_steal(mem_ctx, list);
280 turn a MprVar object variable into a string list
281 this assumes the object variable is an array of strings
283 const char **mprToArray(TALLOC_CTX *mem_ctx, struct MprVar *v)
285 const char **list = NULL;
289 len = mprGetProperty(v, "length", NULL);
293 length = mprToInt(len);
295 for (i=0;i<length;i++) {
298 mprItoa(i, idx, sizeof(idx));
299 vs = mprGetProperty(v, idx, NULL);
300 if (vs == NULL || vs->type != MPR_TYPE_STRING) {
304 list = str_list_add(list, mprToString(vs));
306 talloc_steal(mem_ctx, list);
311 turn a NTSTATUS into a MprVar object with lots of funky properties
313 struct MprVar mprNTSTATUS(NTSTATUS status)
317 res = mprObject("ntstatus");
319 mprSetVar(&res, "errstr", mprString(nt_errstr(status)));
320 mprSetVar(&res, "v", mprCreateIntegerVar(NT_STATUS_V(status)));
321 mprSetVar(&res, "is_ok", mprCreateBoolVar(NT_STATUS_IS_OK(status)));
322 mprSetVar(&res, "is_err", mprCreateBoolVar(NT_STATUS_IS_ERR(status)));
328 create a data-blob in a mpr variable
330 struct MprVar mprDataBlob(DATA_BLOB blob)
333 struct datablob *pblob = talloc(mprMemCtx(), struct datablob);
334 *pblob = data_blob_talloc(pblob, blob.data, blob.length);
336 res = mprObject("DATA_BLOB");
338 mprSetVar(&res, "size", mprCreateIntegerVar(blob.length));
339 mprSetPtrChild(&res, "blob", pblob);
345 return a data blob from a mpr var created using mprDataBlob
347 struct datablob *mprToDataBlob(struct MprVar *v)
349 return talloc_get_type(mprGetPtr(v, "blob"), struct datablob);
353 turn a WERROR into a MprVar object with lots of funky properties
355 struct MprVar mprWERROR(WERROR status)
359 res = mprObject("werror");
361 mprSetVar(&res, "errstr", mprString(win_errstr(status)));
362 mprSetVar(&res, "v", mprCreateIntegerVar(W_ERROR_V(status)));
363 mprSetVar(&res, "is_ok", mprCreateBoolVar(W_ERROR_IS_OK(status)));
364 mprSetVar(&res, "is_err", mprCreateBoolVar(!W_ERROR_IS_OK(status)));
371 set a pointer in a existing MprVar
373 void mprSetPtr(struct MprVar *v, const char *propname, const void *p)
375 mprSetVar(v, propname, mprCreatePtrVar(discard_const(p)));
379 set a pointer in a existing MprVar, freeing it when the property goes away
381 void mprSetPtrChild(struct MprVar *v, const char *propname, const void *p)
383 mprSetVar(v, propname, mprCreatePtrVar(discard_const(p)));
384 v = mprGetProperty(v, propname, NULL);
385 v->allocatedData = 1;
386 talloc_steal(mprMemCtx(), p);
390 get a pointer from a MprVar
392 void *mprGetPtr(struct MprVar *v, const char *propname)
395 val = mprGetProperty(v, propname, NULL);
399 if (val->type != MPR_TYPE_PTR) {
406 set the return value then free the variable
408 void mpr_Return(int eid, struct MprVar v)
410 ejsSetReturnValue(eid, v);
415 set the return value then free the variable
417 void mpr_ReturnString(int eid, const char *s)
419 mpr_Return(eid, mprString(s));
424 set a C function in a variable
426 void mprSetCFunction(struct MprVar *obj, const char *name, MprCFunction fn)
428 mprSetVar(obj, name, mprCreateCFunctionVar(fn, obj, MPR_VAR_SCRIPT_HANDLE));
432 set a string C function in a variable
434 void mprSetStringCFunction(struct MprVar *obj, const char *name, MprStringCFunction fn)
436 mprSetVar(obj, name, mprCreateStringCFunctionVar(fn, obj, MPR_VAR_SCRIPT_HANDLE));
440 get a pointer in the current object
442 void *mprGetThisPtr(int eid, const char *name)
444 struct MprVar *this = mprGetProperty(ejsGetLocalObject(eid), "this", 0);
445 return mprGetPtr(this, name);
449 set a pointer as a child of the local object
451 void mprSetThisPtr(int eid, const char *name, void *ptr)
453 struct MprVar *this = mprGetProperty(ejsGetLocalObject(eid), "this", 0);
454 mprSetPtrChild(this, name, ptr);
458 used by object xxx_init() routines to allow for the caller
459 to supply a pre-existing object to add properties to,
460 or create a new object. This makes inheritance easy
462 struct MprVar *mprInitObject(int eid, const char *name, int argc, struct MprVar **argv)
464 if (argc > 0 && mprVarIsObject(argv[0]->type)) {
467 mpr_Return(eid, mprObject(name));
468 return ejsGetReturnValue(eid);