summaryrefslogtreecommitdiff
path: root/source4
diff options
context:
space:
mode:
Diffstat (limited to 'source4')
-rw-r--r--source4/scripting/ejs/config.mk1
-rw-r--r--source4/scripting/ejs/mprutil.c3
-rw-r--r--source4/scripting/ejs/smbcalls.c1
-rw-r--r--source4/scripting/ejs/smbcalls_options.c193
-rw-r--r--source4/scripting/ejs/smbscript.c33
5 files changed, 204 insertions, 27 deletions
diff --git a/source4/scripting/ejs/config.mk b/source4/scripting/ejs/config.mk
index c9afc3d0e1..7afcacbdf5 100644
--- a/source4/scripting/ejs/config.mk
+++ b/source4/scripting/ejs/config.mk
@@ -19,6 +19,7 @@ OBJ_FILES = \
scripting/ejs/smbcalls_cli.o \
scripting/ejs/smbcalls_rpc.o \
scripting/ejs/smbcalls_auth.o \
+ scripting/ejs/smbcalls_options.o \
scripting/ejs/mprutil.o
REQUIRED_SUBSYSTEMS = AUTH EJS LIBBASIC EJSRPC MESSAGING
# End SUBSYSTEM SMBCALLS
diff --git a/source4/scripting/ejs/mprutil.c b/source4/scripting/ejs/mprutil.c
index c915174126..6b5c02c48d 100644
--- a/source4/scripting/ejs/mprutil.c
+++ b/source4/scripting/ejs/mprutil.c
@@ -108,6 +108,9 @@ struct MprVar mprList(const char *name, const char **list)
for (i=0;list && list[i];i++) {
mprAddArray(&var, i, mprCreateStringVar(list[i], 1));
}
+ if (i==0) {
+ mprSetVar(&var, "length", mprCreateIntegerVar(i));
+ }
return var;
}
diff --git a/source4/scripting/ejs/smbcalls.c b/source4/scripting/ejs/smbcalls.c
index 1abbb52819..328bd14ab5 100644
--- a/source4/scripting/ejs/smbcalls.c
+++ b/source4/scripting/ejs/smbcalls.c
@@ -122,6 +122,7 @@ void smb_setup_ejs_functions(void)
smb_setup_ejs_cli();
smb_setup_ejs_rpc();
smb_setup_ejs_auth();
+ smb_setup_ejs_options();
ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE);
ejsDefineStringCFunction(-1, "libinclude", ejs_libinclude, NULL, MPR_VAR_SCRIPT_HANDLE);
diff --git a/source4/scripting/ejs/smbcalls_options.c b/source4/scripting/ejs/smbcalls_options.c
new file mode 100644
index 0000000000..335663acfe
--- /dev/null
+++ b/source4/scripting/ejs/smbcalls_options.c
@@ -0,0 +1,193 @@
+/*
+ Unix SMB/CIFS implementation.
+
+ provide a command line options parsing function for ejs
+
+ 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.
+*/
+
+#include "includes.h"
+#include "lib/cmdline/popt_common.h"
+#include "scripting/ejs/smbcalls.h"
+#include "lib/ejs/ejs.h"
+
+
+/*
+ usage:
+ var options = new Object();
+ result = GetOptions(argv, options,
+ "realm=s",
+ "enablexx",
+ "myint=i");
+
+ the special options POPT_COMMON_* options are recognised and replaced
+ with the Samba internal options
+
+ resulting parsed options are placed in the options object
+
+ additional command line arguments are placed in options.ARGV
+*/
+static int ejs_GetOptions(MprVarHandle eid, int argc, struct MprVar **argv)
+{
+ poptContext pc;
+ int opt;
+ struct {
+ const char *name;
+ struct poptOption *table;
+ const char *description;
+ } tables[] = {
+ { "POPT_AUTOHELP", poptHelpOptions, "Help options:" },
+ { "POPT_COMMON_SAMBA", popt_common_samba, "Common Samba options:" },
+ { "POPT_COMMON_CONNECTION", popt_common_connection, "Connection options:" },
+ { "POPT_COMMON_CREDENTIALS", popt_common_credentials, "Authentication options:" },
+ { "POPT_COMMON_VERSION", popt_common_version, "Common Samba options:" }
+ };
+ TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx());
+ struct poptOption *long_options = NULL;
+ struct MprVar *options;
+ int i, num_options = 0;
+ int opt_argc;
+ const char **opt_argv;
+ const char **opt_names = NULL;
+ const int BASE_OPTNUM = 0x100000;
+
+ /* validate arguments */
+ if (argc < 2 ||
+ argv[0]->type != MPR_TYPE_OBJECT ||
+ argv[1]->type != MPR_TYPE_OBJECT) {
+ ejsSetErrorMsg(eid, "GetOptions invalid arguments");
+ return -1;
+ }
+
+ opt_argv = mprToList(tmp_ctx, argv[0]);
+ options = argv[1];
+ opt_argc = str_list_length(opt_argv);
+
+ long_options = talloc_array(tmp_ctx, struct poptOption, 1);
+ if (long_options == NULL) {
+ return -1;
+ }
+
+ /* create the long_options array */
+ for (i=2;i<argc;i++) {
+ const char *optstr = mprToString(argv[i]);
+ int t, opt_type = POPT_ARG_NONE;
+ const char *s;
+ if (argv[i]->type != MPR_TYPE_STRING) {
+ ejsSetErrorMsg(eid, "GetOptions string argument");
+ return -1;
+ }
+
+ long_options = talloc_realloc(tmp_ctx, long_options,
+ struct poptOption, num_options+2);
+ if (long_options == NULL) {
+ return -1;
+ }
+ ZERO_STRUCT(long_options[num_options]);
+
+ /* see if its one of the special samba option tables */
+ for (t=0;t<ARRAY_SIZE(tables);t++) {
+ if (strcmp(tables[t].name, optstr) == 0) {
+ break;
+ }
+ }
+ if (t < ARRAY_SIZE(tables)) {
+ opt_names = str_list_add(opt_names, optstr);
+ talloc_steal(tmp_ctx, opt_names);
+ long_options[num_options].argInfo = POPT_ARG_INCLUDE_TABLE;
+ long_options[num_options].arg = tables[t].table;
+ long_options[num_options].descrip = tables[t].description;
+ num_options++;
+ continue;
+ }
+
+ s = strchr(optstr, '=');
+ if (s) {
+ char *name = talloc_strndup(tmp_ctx, optstr, (int)(s-optstr));
+ opt_names = str_list_add(opt_names, name);
+ if (s[1] == 's') {
+ opt_type = POPT_ARG_STRING;
+ } else if (s[1] == 'i') {
+ opt_type = POPT_ARG_INT;
+ } else {
+ ejsSetErrorMsg(eid, "GetOptions invalid option type");
+ return -1;
+ }
+ talloc_free(name);
+ } else {
+ opt_names = str_list_add(opt_names, optstr);
+ }
+ talloc_steal(tmp_ctx, opt_names);
+ if (strlen(opt_names[num_options]) == 1) {
+ long_options[num_options].shortName = opt_names[num_options][0];
+ } else {
+ long_options[num_options].longName = opt_names[num_options];
+ }
+ long_options[num_options].argInfo = opt_type;
+ long_options[num_options].val = num_options + BASE_OPTNUM;
+ num_options++;
+ }
+
+ ZERO_STRUCT(long_options[num_options]);
+
+ pc = poptGetContext("smbscript", opt_argc, opt_argv, long_options, 0);
+
+ /* parse the options */
+ while((opt = poptGetNextOpt(pc)) != -1) {
+ const char *arg;
+
+ if (opt < BASE_OPTNUM || opt >= num_options + BASE_OPTNUM) {
+ char *err;
+ err = talloc_asprintf(tmp_ctx, "%s: %s",
+ poptBadOption(pc, POPT_BADOPTION_NOALIAS),
+ poptStrerror(opt));
+ mprSetVar(options, "ERROR", mprCreateStringVar(err, 1));
+ talloc_free(tmp_ctx);
+ mpr_Return(eid, mprCreateBoolVar(0));
+ return 0;
+ }
+ opt -= BASE_OPTNUM;
+ arg = poptGetOptArg(pc);
+ if (arg == NULL) {
+ mprSetVar(options, opt_names[opt], mprCreateBoolVar(1));
+ } else if (long_options[opt].argInfo == POPT_ARG_INT) {
+ int v = strtol(arg, NULL, 0);
+ mprSetVar(options, opt_names[opt], mprCreateIntegerVar(v));
+ } else {
+ mprSetVar(options, opt_names[opt], mprCreateStringVar(arg, 1));
+ }
+ }
+
+ /* setup options.argv list */
+ mprSetVar(options, "ARGV", mprList("ARGV", poptGetArgs(pc)));
+
+ poptFreeContext(pc);
+
+ talloc_free(tmp_ctx);
+ mpr_Return(eid, mprCreateBoolVar(1));
+ return 0;
+}
+
+
+
+/*
+ setup C functions that be called from ejs
+*/
+void smb_setup_ejs_options(void)
+{
+ ejsDefineCFunction(-1, "GetOptions", ejs_GetOptions, NULL, MPR_VAR_SCRIPT_HANDLE);
+}
diff --git a/source4/scripting/ejs/smbscript.c b/source4/scripting/ejs/smbscript.c
index bf9049af36..691cd44730 100644
--- a/source4/scripting/ejs/smbscript.c
+++ b/source4/scripting/ejs/smbscript.c
@@ -22,7 +22,6 @@
*/
#include "includes.h"
-#include "lib/cmdline/popt_common.h"
#include "dynconfig.h"
#include "lib/ejs/ejs.h"
#include "scripting/ejs/smbcalls.h"
@@ -43,37 +42,18 @@ void ejs_exception(const char *reason)
TALLOC_CTX *mem_ctx = talloc_new(NULL);
const char **argv_list = NULL;
const char *fname;
- struct MprVar v, *return_var;
+ struct MprVar *return_var;
int exit_status, i;
- poptContext pc;
- int opt;
- struct poptOption long_options[] = {
- POPT_AUTOHELP
- POPT_COMMON_SAMBA
- POPT_COMMON_CREDENTIALS
- POPT_COMMON_VERSION
- POPT_TABLEEND
- };
-
- popt_common_dont_ask();
-
- pc = poptGetContext("smbscript", argc, argv, long_options, 0);
-
- poptSetOtherOptionHelp(pc, "<script> <args> ...");
-
- while((opt = poptGetNextOpt(pc)) != -1) /* noop */ ;
smbscript_init_subsystems;
-
mprSetCtx(mem_ctx);
- argv = poptGetArgs(pc);
- if (argv == NULL ||
- argv[0] == NULL) {
- poptPrintUsage(pc, stdout, 0);
+ if (argc < 2) {
+ fprintf(stderr, "You must supply a script name\n");
exit(1);
}
- fname = argv[0];
+
+ fname = argv[1];
if (ejsOpen(NULL, NULL, NULL) != 0) {
fprintf(stderr, "smbscript: ejsOpen(): unable to initialise "
@@ -96,8 +76,7 @@ void ejs_exception(const char *reason)
argv_list = str_list_add(argv_list, argv[i]);
}
talloc_steal(mem_ctx, argv_list);
- v = mprList("ARGV", argv_list);
- mprSetVar(ejsGetGlobalObject(eid), "ARGV", v);
+ mprSetVar(ejsGetGlobalObject(eid), "ARGV", mprList("ARGV", argv_list));
/* load the script and advance past interpreter line*/
script = file_load(fname, &script_size, mem_ctx);