1
0
mirror of https://github.com/samba-team/samba.git synced 2025-10-24 15:33:20 +03:00

r18880: JSON-RPC work in progress

This commit is contained in:
Derrell Lipman
2006-09-25 02:49:56 +00:00
committed by Gerald (Jerry) Carter
parent 5a2e103ea0
commit 34bffbaebf
8 changed files with 1152 additions and 26 deletions

View File

@@ -66,6 +66,45 @@ static int ejs_typeof(MprVarHandle eid, int argc, struct MprVar **argv)
return 0;
}
/*
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.
@@ -112,6 +151,47 @@ static int ejs_libinclude(int eid, int argc, char **argv)
return 0;
}
/*
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
*/
@@ -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);
}

View File

@@ -57,6 +57,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
*/
@@ -85,6 +101,35 @@ static int ejs_sys_unix2nttime(MprVarHandle eid, int argc, struct MprVar **argv)
return 0;
}
/*
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
*/
@@ -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);