summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2005-07-12 05:53:51 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 13:20:08 -0500
commit9f86e202865807ed898eff684d7cf3be7daae0fc (patch)
tree3f00468ba059e780c51b611ae7ebf5dd0d608648
parent1bb2b0603126188e639d7adb6309166c64b5e9df (diff)
downloadsamba-9f86e202865807ed898eff684d7cf3be7daae0fc.tar.gz
samba-9f86e202865807ed898eff684d7cf3be7daae0fc.tar.bz2
samba-9f86e202865807ed898eff684d7cf3be7daae0fc.zip
r8340: - added sys_gmtime()
- added sys_unlink() - added sys_file_load() and sys_file_save() - use mprString() instead of mprCreateStringVar() to cope with NULL strings - removed smbcalls_irpc.c as its not needed any more - allow ldbAdd() and ldbModify() to take multiple ldif records - added a sprintf() function to ejs. Quite complex, but very useful! (This used to be commit 625628a3f6e78349d2240ebcc79081f350672070)
-rw-r--r--source4/scripting/ejs/ejsrpc.c6
-rw-r--r--source4/scripting/ejs/mprutil.c14
-rw-r--r--source4/scripting/ejs/smbcalls_auth.c12
-rw-r--r--source4/scripting/ejs/smbcalls_cli.c2
-rw-r--r--source4/scripting/ejs/smbcalls_irpc.c197
-rw-r--r--source4/scripting/ejs/smbcalls_ldb.c8
-rw-r--r--source4/scripting/ejs/smbcalls_nbt.c3
-rw-r--r--source4/scripting/ejs/smbcalls_options.c4
-rw-r--r--source4/scripting/ejs/smbcalls_string.c133
-rw-r--r--source4/scripting/ejs/smbcalls_sys.c91
10 files changed, 236 insertions, 234 deletions
diff --git a/source4/scripting/ejs/ejsrpc.c b/source4/scripting/ejs/ejsrpc.c
index fe61eac502..e84fcbeb32 100644
--- a/source4/scripting/ejs/ejsrpc.c
+++ b/source4/scripting/ejs/ejsrpc.c
@@ -226,7 +226,7 @@ NTSTATUS ejs_pull_string(struct ejs_rpc *ejs,
NTSTATUS ejs_push_string(struct ejs_rpc *ejs,
struct MprVar *v, const char *name, const char *s)
{
- return mprSetVar(v, name, mprCreateStringVar(s, True));
+ return mprSetVar(v, name, mprString(s));
}
/*
@@ -264,7 +264,7 @@ NTSTATUS ejs_push_dom_sid(struct ejs_rpc *ejs,
{
char *sidstr = dom_sid_string(ejs, r);
NT_STATUS_HAVE_NO_MEMORY(sidstr);
- return mprSetVar(v, name, mprCreateStringVar(sidstr, True));
+ return mprSetVar(v, name, mprString(sidstr));
}
NTSTATUS ejs_pull_GUID(struct ejs_rpc *ejs,
@@ -279,7 +279,7 @@ NTSTATUS ejs_push_GUID(struct ejs_rpc *ejs,
{
char *guid = GUID_string(ejs, r);
NT_STATUS_HAVE_NO_MEMORY(guid);
- return mprSetVar(v, name, mprCreateStringVar(guid, True));
+ return mprSetVar(v, name, mprString(guid));
}
NTSTATUS ejs_push_null(struct ejs_rpc *ejs, struct MprVar *v, const char *name)
diff --git a/source4/scripting/ejs/mprutil.c b/source4/scripting/ejs/mprutil.c
index 95571da245..30dec22af1 100644
--- a/source4/scripting/ejs/mprutil.c
+++ b/source4/scripting/ejs/mprutil.c
@@ -114,7 +114,7 @@ struct MprVar mprList(const char *name, const char **list)
var = mprObject(name);
for (i=0;list && list[i];i++) {
- mprAddArray(&var, i, mprCreateStringVar(list[i], 1));
+ mprAddArray(&var, i, mprString(list[i]));
}
if (i==0) {
mprSetVar(&var, "length", mprCreateIntegerVar(i));
@@ -130,7 +130,7 @@ struct MprVar mprString(const char *s)
if (s == NULL) {
return mprCreatePtrVar(NULL);
}
- return mprCreateStringVar(s, 1);
+ return mprCreateStringVar(s, True);
}
/*
@@ -143,7 +143,7 @@ struct MprVar mprData(const uint8_t *p, size_t length)
if (s == NULL) {
return mprCreateUndefinedVar();
}
- var = mprCreateStringVar(s, 1);
+ var = mprString(s);
talloc_free(s);
return var;
}
@@ -183,7 +183,7 @@ struct MprVar mprLdbMessage(struct ldb_message *msg)
/* add the dn if it is not already specified */
if (mprGetProperty(&var, "dn", 0) == 0) {
- mprSetVar(&var, "dn", mprCreateStringVar(msg->dn, 1));
+ mprSetVar(&var, "dn", mprString(msg->dn));
}
return var;
@@ -290,7 +290,7 @@ struct MprVar mprNTSTATUS(NTSTATUS status)
res = mprObject("ntstatus");
- mprSetVar(&res, "errstr", mprCreateStringVar(nt_errstr(status), 1));
+ mprSetVar(&res, "errstr", mprString(nt_errstr(status)));
mprSetVar(&res, "v", mprCreateIntegerVar(NT_STATUS_V(status)));
mprSetVar(&res, "is_ok", mprCreateBoolVar(NT_STATUS_IS_OK(status)));
mprSetVar(&res, "is_err", mprCreateBoolVar(NT_STATUS_IS_ERR(status)));
@@ -307,7 +307,7 @@ struct MprVar mprWERROR(WERROR status)
res = mprObject("werror");
- mprSetVar(&res, "errstr", mprCreateStringVar(win_errstr(status), 1));
+ mprSetVar(&res, "errstr", mprString(win_errstr(status)));
mprSetVar(&res, "v", mprCreateIntegerVar(W_ERROR_V(status)));
mprSetVar(&res, "is_ok", mprCreateBoolVar(W_ERROR_IS_OK(status)));
mprSetVar(&res, "is_err", mprCreateBoolVar(!W_ERROR_IS_OK(status)));
@@ -363,7 +363,7 @@ void *mprGetPtr(struct MprVar *v, const char *propname)
*/
void mpr_ReturnString(int eid, const char *s)
{
- mpr_Return(eid, mprCreateStringVar(s, False));
+ mpr_Return(eid, mprString(s));
}
diff --git a/source4/scripting/ejs/smbcalls_auth.c b/source4/scripting/ejs/smbcalls_auth.c
index 089d4b6bea..81a7ee1bac 100644
--- a/source4/scripting/ejs/smbcalls_auth.c
+++ b/source4/scripting/ejs/smbcalls_auth.c
@@ -41,7 +41,7 @@ static int ejs_systemAuth(TALLOC_CTX *tmp_ctx, struct MprVar *auth, const char *
nt_status = auth_context_create(tmp_ctx, auth_unix, &auth_context, NULL);
if (!NT_STATUS_IS_OK(nt_status)) {
mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
- mprSetPropertyValue(auth, "report", mprCreateStringVar("Auth System Failure", 1));
+ mprSetPropertyValue(auth, "report", mprString("Auth System Failure"));
goto done;
}
@@ -58,13 +58,13 @@ static int ejs_systemAuth(TALLOC_CTX *tmp_ctx, struct MprVar *auth, const char *
nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info);
if (!NT_STATUS_IS_OK(nt_status)) {
mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
- mprSetPropertyValue(auth, "report", mprCreateStringVar("Login Failed", 1));
+ mprSetPropertyValue(auth, "report", mprString("Login Failed"));
goto done;
}
mprSetPropertyValue(auth, "result", mprCreateBoolVar(server_info->authenticated));
- mprSetPropertyValue(auth, "username", mprCreateStringVar(server_info->account_name, 1));
- mprSetPropertyValue(auth, "domain", mprCreateStringVar(server_info->domain_name, 1));
+ mprSetPropertyValue(auth, "username", mprString(server_info->account_name));
+ mprSetPropertyValue(auth, "domain", mprString(server_info->domain_name));
done:
return 0;
@@ -109,7 +109,7 @@ static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv)
} else {
mprSetPropertyValue(&auth, "result", mprCreateBoolVar(False));
- mprSetPropertyValue(&auth, "report", mprCreateStringVar("Unknown Domain", 1));
+ mprSetPropertyValue(&auth, "report", mprString("Unknown Domain"));
}
mpr_Return(eid, auth);
@@ -127,7 +127,7 @@ static int ejs_domain_list(MprVarHandle eid, int argc, char **argv)
}
list = mprObject("list");
- mprSetVar(&list, "0", mprCreateStringVar("System User", 1));
+ mprSetVar(&list, "0", mprString("System User"));
mpr_Return(eid, list);
diff --git a/source4/scripting/ejs/smbcalls_cli.c b/source4/scripting/ejs/smbcalls_cli.c
index 2433b33352..fb18d442a0 100644
--- a/source4/scripting/ejs/smbcalls_cli.c
+++ b/source4/scripting/ejs/smbcalls_cli.c
@@ -635,7 +635,7 @@ static void ejs_list_helper(struct clilist_file_info *info, const char *mask,
char idx[16];
mprItoa(result->properties->numDataItems, idx, sizeof(idx));
- mprSetVar(result, idx, mprCreateStringVar(info->name, 1));
+ mprSetVar(result, idx, mprString(info->name));
}
static int ejs_list(MprVarHandle eid, int argc, MprVar **argv)
diff --git a/source4/scripting/ejs/smbcalls_irpc.c b/source4/scripting/ejs/smbcalls_irpc.c
deleted file mode 100644
index 4fd4671732..0000000000
--- a/source4/scripting/ejs/smbcalls_irpc.c
+++ /dev/null
@@ -1,197 +0,0 @@
-/*
- Unix SMB/CIFS implementation.
-
- provide hooks into IRPC calls from ejs scripts.
-
- Copyright (C) Andrew Tridgell 2005
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-*/
-/*
- I hope that this code will eventually be autogenerated.
-
-
- In this code, the convention is:
-
- ejs_pull_*() from MprVar -> C structure
- ejs_push_*() from C structure -> MprVar
-
- note that for ejs calls, pull only ever needs to do
- NDR_IN, and push only ever needs to do NDR_OUT. This is
- because ejs code is (at least for the moment) only used for
- the client, not the server
-
- also note that we don't need the NDR_SCALARS/NDR_BUFFERS stuff, as
- we aren't dealing with wire marshalling where scalars and buffers
- are separated
-*/
-
-#include "includes.h"
-#include "lib/ejs/ejs.h"
-#include "librpc/gen_ndr/ndr_irpc.h"
-
-struct ndr_ejs {
- /* nothing here yet */
- int dummy;
-};
-
-struct enum_table {
- uint32_t evalue;
- const char *name;
-};
-
-#define EJS_ASSERT(condition) do { \
- if (!(condition)) { \
- DEBUG(0,("ejs assert failed at %s: %s\n", __location__, #condition)); \
- return NT_STATUS_INVALID_PARAMETER; \
- } \
-} while (0)
-
-
-/* first some common helper functions */
-
-static NTSTATUS ejs_pull_enum(struct ndr_ejs *ndr,
- uint32_t *evalue,
- struct MprVar *v,
- const struct enum_table *etable)
-{
- const char *s;
- int i;
- EJS_ASSERT(v->type == MPR_TYPE_STRING);
- s = v->string;
- for (i=0;etable[i].name;i++) {
- if (strcmp(s, etable[i].name) == 0) {
- *evalue = etable[i].evalue;
- return NT_STATUS_OK;
- }
- }
- return NT_STATUS_INVALID_PARAMETER;
-}
-
-static NTSTATUS ejs_push_enum(struct ndr_ejs *ndr,
- uint32_t evalue,
- struct MprVar *v,
- const struct enum_table *etable)
-{
- int i;
- for (i=0;etable[i].name;i++) {
- if (evalue == etable[i].evalue) {
- *v = mprCreateStringVar(etable[i].name, 0);
- return NT_STATUS_OK;
- }
- }
- return NT_STATUS_INVALID_PARAMETER;
-}
-
-static NTSTATUS ejs_push_hyper(struct ndr_ejs *ndr,
- uint64_t evalue,
- struct MprVar *v,
- const char *name)
-{
-}
-
-
-NTSTATUS ejs_element(struct ndr_ejs *ndr,
- struct MprVar *v,
- const char *name,
- struct MprVar **e)
-{
- *e = mprGetProperty(v, name, NULL);
- if (*e == NULL) {
- return NT_STATUS_INVALID_PARAMETER;
- }
- return NT_STATUS_OK;
-}
-
-
-/* when we auto-generate, the enum tables should also be used by
- the ndr_print_*() functions for non-ejs handling of enums
-*/
-static const struct enum_table enum_table_nbdt_info_level[] = {
- {NBTD_INFO_STATISTICS, "NBTD_INFO_STATISTICS"},
- {-1, NULL}
-};
-
-
-
-/* pull-side functions for nbtd_information call */
-
-static NTSTATUS ejs_pull_nbtd_info_level(struct ndr_ejs *ndr,
- enum nbtd_info_level *r,
- struct MprVar *v)
-{
- uint32_t e;
- NDR_CHECK(ejs_pull_enum(ndr, &e, v, enum_table_nbdt_info_level));
- *r = e;
- return NT_STATUS_OK;
-}
-
-static NTSTATUS ejs_pull_nbtd_information(struct ndr_ejs *ndr,
- struct nbtd_information *r,
- struct MprVar *v)
-{
- struct MprVar *e;
- NDR_CHECK(ejs_element(ndr, v, "level", &e));
- NDR_CHECK(ejs_pull_nbtd_info_level(ndr, &r->in.level, e));
- return NT_STATUS_OK;
-}
-
-
-/* push side functions for nbtd_information */
-
-static NTSTATUS ejs_push_nbtd_info_level(struct ndr_ejs *ndr, enum nbtd_info_level r,
- struct MprVar *v)
-{
- NDR_CHECK(ejs_push_enum(ndr, r, v, enum_table_nbdt_info_level));
- return NT_STATUS_OK;
-}
-
-static NTSTATUS ejs_push_nbtd_statistics(struct ndr_ejs *ndr, struct nbtd_statistics *r,
- struct MprVar *v)
-{
- NDR_CHECK(ejs_push_hyper(ndr, r->total_received, v, "total_received"));
- NDR_CHECK(ejs_push_hyper(ndr, r->total_sent, v, "total_sent"));
- NDR_CHECK(ejs_push_hyper(ndr, r->query_count, v, "query_count"));
- NDR_CHECK(ejs_push_hyper(ndr, r->register_count, v, "register_count"));
- NDR_CHECK(ejs_push_hyper(ndr, r->release_count, v, "release_count"));
- NDR_CHECK(ejs_push_hyper(ndr, r->refresh_count, v, "refresh_count"));
- return NT_STATUS_OK;
-}
-
-static NTSTATUS ejs_push_nbtd_info(struct ndr_ejs *ndr, union nbtd_info *r,
- struct MprVar *v)
-{
- int level;
- level = ejs_push_get_switch_value(ndr, r);
- switch (level) {
- case NBTD_INFO_STATISTICS:
- NDR_CHECK(ejs_push_object(ndr, r->stats,
- (ejs_push_fn_t)ejs_push_nbtd_statistics,
- v, "stats"));
- break;
- default:
- return ejs_push_error(ndr, NDR_ERR_BAD_SWITCH, "Bad switch value %u", level);
- }
- return NT_STATUS_OK;
-}
-
-static NTSTATUS ndr_push_nbtd_information(struct ndr_ejs *ndr,
- struct nbtd_information *r, struct MprVar *v)
-{
- NDR_CHECK(ejs_push_set_switch_value(ndr, &r->out.info, r->in.level));
- NDR_CHECK(ejs_push_object(ndr, &r->out.info,
- (ejs_push_fn_t)ejs_push_nbtd_info, v, "info"));
- return NT_STATUS_OK;
-}
diff --git a/source4/scripting/ejs/smbcalls_ldb.c b/source4/scripting/ejs/smbcalls_ldb.c
index 309271a29d..9876fe151f 100644
--- a/source4/scripting/ejs/smbcalls_ldb.c
+++ b/source4/scripting/ejs/smbcalls_ldb.c
@@ -111,12 +111,10 @@ static int ejs_ldbAddModify(MprVarHandle eid, int argc, char **argv,
goto failed;
}
- ldif = ldb_ldif_read_string(ldb, ldifstring);
- if (ldif == NULL) {
- ejsSetErrorMsg(eid, "ldbAddModify invalid ldif");
- goto failed;
+ while ((ldif = ldb_ldif_read_string(ldb, &ldifstring))) {
+ ret = fn(ldb, ldif->msg);
+ talloc_free(ldif);
}
- ret = fn(ldb, ldif->msg);
mpr_Return(eid, mprCreateBoolVar(ret == 0));
talloc_free(ldb);
diff --git a/source4/scripting/ejs/smbcalls_nbt.c b/source4/scripting/ejs/smbcalls_nbt.c
index 16c4e6aaee..721a5beb22 100644
--- a/source4/scripting/ejs/smbcalls_nbt.c
+++ b/source4/scripting/ejs/smbcalls_nbt.c
@@ -72,8 +72,7 @@ static int ejs_resolve_name(MprVarHandle eid, int argc, struct MprVar **argv)
nt_status = resolve_name(&name, tmp_ctx, &reply_addr);
if (NT_STATUS_IS_OK(nt_status)) {
- mprSetPropertyValue(argv[0], "value",
- mprCreateStringVar(reply_addr, 1));
+ mprSetPropertyValue(argv[0], "value", mprString(reply_addr));
}
mpr_Return(eid, mprNTSTATUS(nt_status));
diff --git a/source4/scripting/ejs/smbcalls_options.c b/source4/scripting/ejs/smbcalls_options.c
index df871cbb96..005a3bcb9a 100644
--- a/source4/scripting/ejs/smbcalls_options.c
+++ b/source4/scripting/ejs/smbcalls_options.c
@@ -155,7 +155,7 @@ static int ejs_GetOptions(MprVarHandle eid, int argc, struct MprVar **argv)
err = talloc_asprintf(tmp_ctx, "%s: %s",
poptBadOption(pc, POPT_BADOPTION_NOALIAS),
poptStrerror(opt));
- mprSetVar(options, "ERROR", mprCreateStringVar(err, 1));
+ mprSetVar(options, "ERROR", mprString(err));
talloc_free(tmp_ctx);
mpr_Return(eid, mprCreateBoolVar(0));
return 0;
@@ -168,7 +168,7 @@ static int ejs_GetOptions(MprVarHandle eid, int argc, struct MprVar **argv)
int v = strtol(arg, NULL, 0);
mprSetVar(options, opt_names[opt], mprCreateIntegerVar(v));
} else {
- mprSetVar(options, opt_names[opt], mprCreateStringVar(arg, 1));
+ mprSetVar(options, opt_names[opt], mprString(arg));
}
}
diff --git a/source4/scripting/ejs/smbcalls_string.c b/source4/scripting/ejs/smbcalls_string.c
index b729f5dd3a..7a19ecdf2c 100644
--- a/source4/scripting/ejs/smbcalls_string.c
+++ b/source4/scripting/ejs/smbcalls_string.c
@@ -141,23 +141,134 @@ failed:
return -1;
}
+
/*
- load a file as a string
+ blergh, C certainly makes this hard!
usage:
- string = FileLoad(filename);
+ str = sprintf("i=%d s=%7s", 7, "foo");
*/
-static int ejs_FileLoad(MprVarHandle eid, int argc, char **argv)
+static int ejs_sprintf(MprVarHandle eid, int argc, struct MprVar **argv)
{
- char *s;
- if (argc != 1) {
- ejsSetErrorMsg(eid, "FileLoad invalid arguments");
+ const char *format;
+ const char *p;
+ char *ret;
+ int a = 1;
+ char *(*_asprintf_append)(char *, const char *, ...);
+ TALLOC_CTX *tmp_ctx;
+ if (argc < 1 || argv[0]->type != MPR_TYPE_STRING) {
+ ejsSetErrorMsg(eid, "sprintf invalid arguments");
return -1;
}
+ format = mprToString(argv[0]);
+ tmp_ctx = talloc_new(mprMemCtx());
+ ret = talloc_strdup(tmp_ctx, "");
- s = file_load(argv[0], NULL, mprMemCtx());
- mpr_Return(eid, mprString(s));
- talloc_free(s);
- return 0;
+ /* avoid all the format string warnings */
+ _asprintf_append = talloc_asprintf_append;
+
+ /*
+ hackity hack ...
+ */
+ while ((p = strchr(format, '%'))) {
+ char *fmt2;
+ int len, len_count=0;
+ char *tstr;
+ ret = talloc_asprintf_append(ret, "%*.*s",
+ (int)(p-format), (int)(p-format),
+ format);
+ if (ret == NULL) goto failed;
+ format += (int)(p-format);
+ len = strcspn(p+1, "dxuiofgGpXeEFcs%") + 1;
+ fmt2 = talloc_strndup(tmp_ctx, p, len+1);
+ if (fmt2 == NULL) goto failed;
+ len_count = count_chars(fmt2, '*');
+ /* find the type string */
+ tstr = &fmt2[len];
+ while (tstr > fmt2 && isalpha(tstr[-1])) {
+ tstr--;
+ }
+ if (strcmp(tstr, "%") == 0) {
+ ret = talloc_asprintf_append(ret, "%%");
+ if (ret == NULL) {
+ goto failed;
+ }
+ format += len+1;
+ continue;
+ }
+ if (len_count > 2 ||
+ argc < a + len_count + 1) {
+ ejsSetErrorMsg(eid, "sprintf: not enough arguments for format");
+ goto failed;
+ }
+#define FMT_ARG(fn, type) do { \
+ switch (len_count) { \
+ case 0: \
+ ret = _asprintf_append(ret, fmt2, \
+ (type)fn(argv[a])); \
+ break; \
+ case 1: \
+ ret = _asprintf_append(ret, fmt2, \
+ (int)mprVarToNumber(argv[a]), \
+ (type)fn(argv[a+1])); \
+ break; \
+ case 2: \
+ ret = _asprintf_append(ret, fmt2, \
+ (int)mprVarToNumber(argv[a]), \
+ (int)mprVarToNumber(argv[a+1]), \
+ (type)fn(argv[a+2])); \
+ break; \
+ } \
+ a += len_count + 1; \
+ if (ret == NULL) { \
+ goto failed; \
+ } \
+} while (0)
+
+ if (strcmp(tstr, "s")==0) FMT_ARG(mprToString, const char *);
+ else if (strcmp(tstr, "c")==0) FMT_ARG(*mprToString, char);
+ else if (strcmp(tstr, "d")==0) FMT_ARG(mprVarToNumber, int);
+ else if (strcmp(tstr, "ld")==0) FMT_ARG(mprVarToNumber, long);
+ else if (strcmp(tstr, "lld")==0) FMT_ARG(mprVarToNumber, long long);
+ else if (strcmp(tstr, "x")==0) FMT_ARG(mprVarToNumber, int);
+ else if (strcmp(tstr, "lx")==0) FMT_ARG(mprVarToNumber, long);
+ else if (strcmp(tstr, "llx")==0) FMT_ARG(mprVarToNumber, long long);
+ else if (strcmp(tstr, "X")==0) FMT_ARG(mprVarToNumber, int);
+ else if (strcmp(tstr, "lX")==0) FMT_ARG(mprVarToNumber, long);
+ else if (strcmp(tstr, "llX")==0) FMT_ARG(mprVarToNumber, long long);
+ else if (strcmp(tstr, "u")==0) FMT_ARG(mprVarToNumber, int);
+ else if (strcmp(tstr, "lu")==0) FMT_ARG(mprVarToNumber, long);
+ else if (strcmp(tstr, "llu")==0) FMT_ARG(mprVarToNumber, long long);
+ else if (strcmp(tstr, "i")==0) FMT_ARG(mprVarToNumber, int);
+ else if (strcmp(tstr, "li")==0) FMT_ARG(mprVarToNumber, long);
+ else if (strcmp(tstr, "lli")==0) FMT_ARG(mprVarToNumber, long long);
+ else if (strcmp(tstr, "o")==0) FMT_ARG(mprVarToNumber, int);
+ else if (strcmp(tstr, "lo")==0) FMT_ARG(mprVarToNumber, long);
+ else if (strcmp(tstr, "llo")==0) FMT_ARG(mprVarToNumber, long long);
+ else if (strcmp(tstr, "f")==0) FMT_ARG(mprVarToFloat, double);
+ else if (strcmp(tstr, "lf")==0) FMT_ARG(mprVarToFloat, double);
+ else if (strcmp(tstr, "g")==0) FMT_ARG(mprVarToFloat, double);
+ else if (strcmp(tstr, "lg")==0) FMT_ARG(mprVarToFloat, double);
+ else if (strcmp(tstr, "e")==0) FMT_ARG(mprVarToFloat, double);
+ else if (strcmp(tstr, "le")==0) FMT_ARG(mprVarToFloat, double);
+ else if (strcmp(tstr, "E")==0) FMT_ARG(mprVarToFloat, double);
+ else if (strcmp(tstr, "lE")==0) FMT_ARG(mprVarToFloat, double);
+ else if (strcmp(tstr, "F")==0) FMT_ARG(mprVarToFloat, double);
+ else if (strcmp(tstr, "lF")==0) FMT_ARG(mprVarToFloat, double);
+ else {
+ ejsSetErrorMsg(eid, "sprintf: unknown format string '%s'", fmt2);
+ goto failed;
+ }
+ format += len+1;
+ }
+
+ ret = talloc_asprintf_append(ret, "%s", format);
+ mpr_Return(eid, mprString(ret));
+ talloc_free(tmp_ctx);
+ return 0;
+
+failed:
+ talloc_free(tmp_ctx);
+ return -1;
}
/*
@@ -168,6 +279,6 @@ void smb_setup_ejs_string(void)
ejsDefineStringCFunction(-1, "strlower", ejs_strlower, NULL, MPR_VAR_SCRIPT_HANDLE);
ejsDefineStringCFunction(-1, "strupper", ejs_strupper, NULL, MPR_VAR_SCRIPT_HANDLE);
ejsDefineStringCFunction(-1, "split", ejs_split, NULL, MPR_VAR_SCRIPT_HANDLE);
- ejsDefineStringCFunction(-1, "FileLoad", ejs_FileLoad, NULL, MPR_VAR_SCRIPT_HANDLE);
ejsDefineCFunction(-1, "join", ejs_join, NULL, MPR_VAR_SCRIPT_HANDLE);
+ ejsDefineCFunction(-1, "sprintf", ejs_sprintf, NULL, MPR_VAR_SCRIPT_HANDLE);
}
diff --git a/source4/scripting/ejs/smbcalls_sys.c b/source4/scripting/ejs/smbcalls_sys.c
index a76690b597..f098f884bb 100644
--- a/source4/scripting/ejs/smbcalls_sys.c
+++ b/source4/scripting/ejs/smbcalls_sys.c
@@ -23,6 +23,7 @@
#include "includes.h"
#include "scripting/ejs/smbcalls.h"
#include "lib/ejs/ejs.h"
+#include "system/time.h"
/*
return the list of configured network interfaces
@@ -65,6 +66,40 @@ static int ejs_sys_nttime(MprVarHandle eid, int argc, struct MprVar **argv)
}
/*
+ return the given time as a gmtime structure
+*/
+static int ejs_sys_gmtime(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_gmtime invalid arguments");
+ return -1;
+ }
+ t = nt_time_to_unix(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);
+
+ mpr_Return(eid, ret);
+ return 0;
+}
+
+/*
return a ldap time string from a nttime
*/
static int ejs_sys_ldaptime(MprVarHandle eid, int argc, struct MprVar **argv)
@@ -82,6 +117,58 @@ static int ejs_sys_ldaptime(MprVarHandle eid, int argc, struct MprVar **argv)
return 0;
}
+/*
+ unlink a file
+ ok = unlink(fname);
+*/
+static int ejs_sys_unlink(MprVarHandle eid, int argc, char **argv)
+{
+ int ret;
+ if (argc != 1) {
+ ejsSetErrorMsg(eid, "sys_unlink invalid arguments");
+ return -1;
+ }
+ ret = unlink(argv[0]);
+ mpr_Return(eid, mprCreateBoolVar(ret == 0));
+ return 0;
+}
+
+/*
+ load a file as a string
+ usage:
+ string = sys_file_load(filename);
+*/
+static int ejs_sys_file_load(MprVarHandle eid, int argc, char **argv)
+{
+ char *s;
+ if (argc != 1) {
+ ejsSetErrorMsg(eid, "sys_file_load invalid arguments");
+ return -1;
+ }
+
+ s = file_load(argv[0], NULL, mprMemCtx());
+ mpr_Return(eid, mprString(s));
+ talloc_free(s);
+ return 0;
+}
+
+/*
+ save a file from a string
+ usage:
+ ok = sys_file_save(filename, str);
+*/
+static int ejs_sys_file_save(MprVarHandle eid, int argc, char **argv)
+{
+ BOOL ret;
+ if (argc != 2) {
+ ejsSetErrorMsg(eid, "sys_file_save invalid arguments");
+ return -1;
+ }
+ ret = file_save(argv[0], argv[1], strlen(argv[1]));
+ mpr_Return(eid, mprCreateBoolVar(ret));
+ return 0;
+}
+
/*
setup C functions that be called from ejs
@@ -91,5 +178,9 @@ void smb_setup_ejs_system(void)
ejsDefineCFunction(-1, "sys_interfaces", ejs_sys_interfaces, NULL, MPR_VAR_SCRIPT_HANDLE);
ejsDefineCFunction(-1, "sys_hostname", ejs_sys_hostname, NULL, MPR_VAR_SCRIPT_HANDLE);
ejsDefineCFunction(-1, "sys_nttime", ejs_sys_nttime, NULL, MPR_VAR_SCRIPT_HANDLE);
+ ejsDefineCFunction(-1, "sys_gmtime", ejs_sys_gmtime, NULL, MPR_VAR_SCRIPT_HANDLE);
ejsDefineCFunction(-1, "sys_ldaptime", ejs_sys_ldaptime, NULL, MPR_VAR_SCRIPT_HANDLE);
+ ejsDefineStringCFunction(-1, "sys_unlink", ejs_sys_unlink, NULL, MPR_VAR_SCRIPT_HANDLE);
+ ejsDefineStringCFunction(-1, "sys_file_load", ejs_sys_file_load, NULL, MPR_VAR_SCRIPT_HANDLE);
+ ejsDefineStringCFunction(-1, "sys_file_save", ejs_sys_file_save, NULL, MPR_VAR_SCRIPT_HANDLE);
}