summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2005-07-11 21:52:06 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 13:20:06 -0500
commitc30c711292321b70f5c7c468a8bc245217be67ba (patch)
treecb91fcd68914260b74e8b96e9c8c8adbf178ee7c
parent3e0aa2e756ec5fb3f03c9029ee442ed0aede5c53 (diff)
downloadsamba-c30c711292321b70f5c7c468a8bc245217be67ba.tar.gz
samba-c30c711292321b70f5c7c468a8bc245217be67ba.tar.bz2
samba-c30c711292321b70f5c7c468a8bc245217be67ba.zip
r8331: added split(), join() and FileLoad() functions to ejs.
this gets me most of the way through a ejs provision script (This used to be commit 0ef8f0282e0490328bbcdc8c6daff3f8fac99087)
-rw-r--r--source4/scripting/ejs/smbcalls_string.c103
1 files changed, 103 insertions, 0 deletions
diff --git a/source4/scripting/ejs/smbcalls_string.c b/source4/scripting/ejs/smbcalls_string.c
index 7817de1b0d..b52a6d2182 100644
--- a/source4/scripting/ejs/smbcalls_string.c
+++ b/source4/scripting/ejs/smbcalls_string.c
@@ -59,6 +59,106 @@ static int ejs_strupper(MprVarHandle eid, int argc, char **argv)
return 0;
}
+/*
+ usage:
+ list = split(".", "a.foo.bar");
+
+ NOTE: does not take a regular expression, unlink perl split()
+*/
+static int ejs_split(MprVarHandle eid, int argc, char **argv)
+{
+ const char *separator;
+ char *s, *p;
+ struct MprVar ret;
+ int count = 0;
+ TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx());
+ if (argc != 2) {
+ ejsSetErrorMsg(eid, "split invalid arguments");
+ return -1;
+ }
+ separator = argv[0];
+ s = argv[1];
+
+ ret = mprObject("list");
+
+ while ((p = strstr(s, separator))) {
+ char *s2 = talloc_strndup(tmp_ctx, s, (int)(p-s));
+ mprAddArray(&ret, count++, mprString(s2));
+ talloc_free(s2);
+ s = p + strlen(separator);
+ }
+ if (*s) {
+ mprAddArray(&ret, count++, mprString(s));
+ }
+ talloc_free(tmp_ctx);
+ mpr_Return(eid, ret);
+ return 0;
+}
+
+
+/*
+ usage:
+ str = join("DC=", list);
+*/
+static int ejs_join(MprVarHandle eid, int argc, struct MprVar **argv)
+{
+ int i;
+ const char *separator;
+ char *ret = NULL;
+ const char **list;
+ TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx());
+ if (argc != 2 ||
+ argv[0]->type != MPR_TYPE_STRING ||
+ argv[1]->type != MPR_TYPE_OBJECT) {
+ ejsSetErrorMsg(eid, "join invalid arguments");
+ return -1;
+ }
+
+ separator = mprToString(argv[0]);
+ list = mprToList(tmp_ctx, argv[1]);
+
+ if (list == NULL || list[0] == NULL) {
+ talloc_free(tmp_ctx);
+ mpr_Return(eid, mprString(NULL));
+ return 0;
+ }
+
+ ret = talloc_strdup(tmp_ctx, list[0]);
+ if (ret == NULL) {
+ goto failed;
+ }
+ for (i=1;list[i];i++) {
+ ret = talloc_asprintf_append(ret, "%s%s", separator, list[i]);
+ if (ret == NULL) {
+ goto failed;
+ }
+ }
+ mpr_Return(eid, mprString(ret));
+ talloc_free(tmp_ctx);
+ return 0;
+failed:
+ ejsSetErrorMsg(eid, "out of memory");
+ return -1;
+}
+
+/*
+ load a file as a string
+ usage:
+ string = FileLoad(filename);
+*/
+static int ejs_FileLoad(MprVarHandle eid, int argc, char **argv)
+{
+ char *s;
+ if (argc != 1) {
+ ejsSetErrorMsg(eid, "FileLoad invalid arguments");
+ return -1;
+ }
+
+ s = file_load(argv[0], NULL, mprMemCtx());
+ mpr_Return(eid, mprString(s));
+ talloc_free(s);
+ return 0;
+}
/*
setup C functions that be called from ejs
@@ -67,4 +167,7 @@ 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);
}