summaryrefslogtreecommitdiff
path: root/source4/scripting
diff options
context:
space:
mode:
authorDerrell Lipman <derrell@samba.org>2006-09-25 02:49:56 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 14:20:17 -0500
commitc5718959e6a6d0454a870cbd311e707e69c98e85 (patch)
tree0ff02ec691bce1ef1181d1000466937feb264e43 /source4/scripting
parent4d3434ff6e83799a1690b3ad3475beb3ef6f12ba (diff)
downloadsamba-c5718959e6a6d0454a870cbd311e707e69c98e85.tar.gz
samba-c5718959e6a6d0454a870cbd311e707e69c98e85.tar.bz2
samba-c5718959e6a6d0454a870cbd311e707e69c98e85.zip
r18880: JSON-RPC work in progress
(This used to be commit 34bffbaebf50c2a75c91285d5ec82e8f377981cc)
Diffstat (limited to 'source4/scripting')
-rw-r--r--source4/scripting/ejs/smbcalls.c82
-rw-r--r--source4/scripting/ejs/smbcalls_sys.c84
-rw-r--r--source4/scripting/libjs/provision.js2
3 files changed, 167 insertions, 1 deletions
diff --git a/source4/scripting/ejs/smbcalls.c b/source4/scripting/ejs/smbcalls.c
index 1bfbd3b47a..85cc5f7027 100644
--- a/source4/scripting/ejs/smbcalls.c
+++ b/source4/scripting/ejs/smbcalls.c
@@ -67,6 +67,45 @@ static int ejs_typeof(MprVarHandle eid, int argc, struct MprVar **argv)
}
/*
+ return the native type of a variable
+*/
+static int ejs_typeof_native(MprVarHandle eid, int argc, struct MprVar **argv)
+{
+ const struct {
+ MprType type;
+ const char *name;
+ } types[] = {
+ { MPR_TYPE_UNDEFINED, "undefined" },
+ { MPR_TYPE_NULL, "null" },
+ { MPR_TYPE_BOOL, "boolean" },
+ { MPR_TYPE_CFUNCTION, "c_function" },
+ { MPR_TYPE_FLOAT, "float" },
+ { MPR_TYPE_INT, "integer" },
+ { MPR_TYPE_INT64, "integer64" },
+ { MPR_TYPE_OBJECT, "object" },
+ { MPR_TYPE_FUNCTION, "function" },
+ { MPR_TYPE_STRING, "string" },
+ { MPR_TYPE_STRING_CFUNCTION, "string_c_function" },
+ { MPR_TYPE_PTR, "pointer" }
+ };
+ int i;
+ const char *type = NULL;
+
+ if (argc != 1) return -1;
+
+ for (i=0;i<ARRAY_SIZE(types);i++) {
+ if (argv[0]->type == types[i].type) {
+ type = types[i].name;
+ break;
+ }
+ }
+ if (type == NULL) return -1;
+
+ mpr_ReturnString(eid, type);
+ return 0;
+}
+
+/*
libinclude() allows you to include js files using a search path specified
in "js include =" in smb.conf.
*/
@@ -113,6 +152,47 @@ static int ejs_libinclude(int eid, int argc, char **argv)
}
/*
+ jsonrpc_include() allows you to include jsonrpc files from a path
+ based at "jsonrpc base =" in smb.conf.
+*/
+static int ejs_jsonrpc_include(int eid, int argc, char **argv)
+{
+ int ret = -1;
+ char *path;
+ char *emsg;
+ const char *jsonrpc_base = lp_jsonrpc_base();
+ struct MprVar result;
+
+
+ if (jsonrpc_base == NULL || jsonrpc_base == NULL) {
+ ejsSetErrorMsg(eid, "js include path not set");
+ return -1;
+ }
+
+ if (argc != 1) {
+ mpr_Return(eid, mprCreateIntegerVar(-1));
+ return 0;
+ }
+
+ path = talloc_asprintf(mprMemCtx(), "%s/%s", jsonrpc_base, argv[0]);
+ if (path == NULL) {
+ mpr_Return(eid, mprCreateIntegerVar(-1));
+ return 0;
+ }
+
+ if (file_exist(path)) {
+ ret = ejsEvalFile(eid, path, &result, &emsg);
+ if (ret < 0) {
+ printf("file found; ret=%d (%s)\n", ret, emsg);
+ }
+ }
+
+ mpr_Return(eid, mprCreateIntegerVar(ret));
+ talloc_free(path);
+ return 0;
+}
+
+/*
return the current version
*/
static int ejs_version(MprVarHandle eid, int argc, struct MprVar **argv)
@@ -153,7 +233,9 @@ void smb_setup_ejs_functions(void (*exception_handler)(const char *))
talloc_free(shared_init);
ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE);
+ ejsDefineCFunction(-1, "nativeTypeOf", ejs_typeof_native, NULL, MPR_VAR_SCRIPT_HANDLE);
ejsDefineStringCFunction(-1, "libinclude", ejs_libinclude, NULL, MPR_VAR_SCRIPT_HANDLE);
+ ejsDefineStringCFunction(-1, "jsonrpc_include", ejs_jsonrpc_include, NULL, MPR_VAR_SCRIPT_HANDLE);
ejsDefineCFunction(-1, "version", ejs_version, NULL, MPR_VAR_SCRIPT_HANDLE);
}
diff --git a/source4/scripting/ejs/smbcalls_sys.c b/source4/scripting/ejs/smbcalls_sys.c
index d8aaf3898a..42990f49c0 100644
--- a/source4/scripting/ejs/smbcalls_sys.c
+++ b/source4/scripting/ejs/smbcalls_sys.c
@@ -58,6 +58,22 @@ static int ejs_sys_hostname(MprVarHandle eid, int argc, struct MprVar **argv)
/*
+ return current time as seconds and microseconds
+*/
+static int ejs_sys_gettimeofday(MprVarHandle eid, int argc, struct MprVar **argv)
+{
+ struct timeval tv = timeval_current();
+ struct MprVar v = mprObject("timeval");
+ struct MprVar sec = mprCreateIntegerVar(tv.tv_sec);
+ struct MprVar usec = mprCreateIntegerVar(tv.tv_usec);
+
+ mprCreateProperty(&v, "sec", &sec);
+ mprCreateProperty(&v, "usec", &usec);
+ mpr_Return(eid, v);
+ return 0;
+}
+
+/*
return current time as a 64 bit nttime value
*/
static int ejs_sys_nttime(MprVarHandle eid, int argc, struct MprVar **argv)
@@ -86,6 +102,35 @@ static int ejs_sys_unix2nttime(MprVarHandle eid, int argc, struct MprVar **argv)
}
/*
+ return the GMT time represented by the struct tm argument, as a time_t value
+*/
+static int ejs_sys_gmmktime(MprVarHandle eid, int argc, struct MprVar **argv)
+{
+ struct MprVar *o;
+ struct tm tm;
+ if (argc != 1 || !mprVarIsObject(argv[0]->type)) {
+ ejsSetErrorMsg(eid, "sys_gmmktime invalid arguments");
+ return -1;
+ }
+
+ o = argv[0];
+#define TM_EL(n) tm.n = mprVarToNumber(mprGetProperty(o, #n, NULL))
+ TM_EL(tm_sec);
+ TM_EL(tm_min);
+ TM_EL(tm_hour);
+ TM_EL(tm_mday);
+ TM_EL(tm_mon);
+ TM_EL(tm_year);
+ TM_EL(tm_wday);
+ TM_EL(tm_yday);
+ TM_EL(tm_isdst);
+#undef TM_EL
+
+ mpr_Return(eid, mprCreateIntegerVar(mktime(&tm)));
+ return 0;
+}
+
+/*
return the given time as a gmtime structure
*/
static int ejs_sys_gmtime(MprVarHandle eid, int argc, struct MprVar **argv)
@@ -97,6 +142,41 @@ static int ejs_sys_gmtime(MprVarHandle eid, int argc, struct MprVar **argv)
ejsSetErrorMsg(eid, "sys_gmtime invalid arguments");
return -1;
}
+ t = (time_t) mprVarToNumber(argv[0]);
+ tm = gmtime(&t);
+ if (tm == NULL) {
+ mpr_Return(eid, mprCreateUndefinedVar());
+ return 0;
+ }
+ ret = mprObject("gmtime");
+#define TM_EL(n) mprSetVar(&ret, #n, mprCreateIntegerVar(tm->n))
+ TM_EL(tm_sec);
+ TM_EL(tm_min);
+ TM_EL(tm_hour);
+ TM_EL(tm_mday);
+ TM_EL(tm_mon);
+ TM_EL(tm_year);
+ TM_EL(tm_wday);
+ TM_EL(tm_yday);
+ TM_EL(tm_isdst);
+#undef TM_EL
+
+ mpr_Return(eid, ret);
+ return 0;
+}
+
+/*
+ return the given NT time as a gmtime structure
+*/
+static int ejs_sys_ntgmtime(MprVarHandle eid, int argc, struct MprVar **argv)
+{
+ time_t t;
+ struct MprVar ret;
+ struct tm *tm;
+ if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
+ ejsSetErrorMsg(eid, "sys_ntgmtime invalid arguments");
+ return -1;
+ }
t = nt_time_to_unix(mprVarToNumber(argv[0]));
tm = gmtime(&t);
if (tm == NULL) {
@@ -114,6 +194,7 @@ static int ejs_sys_gmtime(MprVarHandle eid, int argc, struct MprVar **argv)
TM_EL(tm_wday);
TM_EL(tm_yday);
TM_EL(tm_isdst);
+#undef TM_EL
mpr_Return(eid, ret);
return 0;
@@ -332,8 +413,11 @@ static int ejs_sys_init(MprVarHandle eid, int argc, struct MprVar **argv)
mprSetCFunction(obj, "interfaces", ejs_sys_interfaces);
mprSetCFunction(obj, "hostname", ejs_sys_hostname);
mprSetCFunction(obj, "nttime", ejs_sys_nttime);
+ mprSetCFunction(obj, "getTimeOfDay", ejs_sys_gettimeofday);
mprSetCFunction(obj, "unix2nttime", ejs_sys_unix2nttime);
+ mprSetCFunction(obj, "gmmktime", ejs_sys_gmmktime);
mprSetCFunction(obj, "gmtime", ejs_sys_gmtime);
+ mprSetCFunction(obj, "ntgmtime", ejs_sys_ntgmtime);
mprSetCFunction(obj, "ldaptime", ejs_sys_ldaptime);
mprSetCFunction(obj, "httptime", ejs_sys_httptime);
mprSetStringCFunction(obj, "unlink", ejs_sys_unlink);
diff --git a/source4/scripting/libjs/provision.js b/source4/scripting/libjs/provision.js
index 1328cfe8fe..bba3d124ff 100644
--- a/source4/scripting/libjs/provision.js
+++ b/source4/scripting/libjs/provision.js
@@ -113,7 +113,7 @@ function ldaptime()
*/
function datestring()
{
- var t = sys.gmtime(sys.nttime());
+ var t = sys.ntgmtime(sys.nttime());
return sprintf("%04u%02u%02u%02u",
t.tm_year+1900, t.tm_mon+1, t.tm_mday, t.tm_hour);
}