2 Unix SMB/CIFS implementation.
4 provide a command line options parsing function for ejs
6 Copyright (C) Andrew Tridgell 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "lib/cmdline/popt_common.h"
24 #include "scripting/ejs/smbcalls.h"
29 options = GetOptions(argv,
34 the special options POPT_COMMON_* options are recognised and replaced
35 with the Samba internal options
37 resulting parsed options are placed in the options object
39 additional command line arguments are placed in options.ARGV
42 static int ejs_GetOptions(MprVarHandle eid, int argc, struct MprVar **argv)
48 struct poptOption *table;
49 const char *description;
51 { "POPT_AUTOHELP", poptHelpOptions, "Help options:" },
52 { "POPT_COMMON_SAMBA", popt_common_samba, "Common Samba options:" },
53 { "POPT_COMMON_CONNECTION", popt_common_connection, "Connection options:" },
54 { "POPT_COMMON_CREDENTIALS", popt_common_credentials, "Authentication options:" },
55 { "POPT_COMMON_VERSION", popt_common_version, "Common Samba options:" }
58 struct MprVar *options = mprInitObject(eid, "options", 0, NULL);
60 TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx());
61 struct poptOption *long_options = NULL;
62 int i, num_options = 0;
64 const char **opt_argv;
65 const char **opt_names = NULL;
66 const int BASE_OPTNUM = 0x100000;
68 /* validate arguments */
69 if (argc < 1 || argv[0]->type != MPR_TYPE_OBJECT) {
70 ejsSetErrorMsg(eid, "GetOptions invalid arguments");
74 opt_argv = mprToArray(tmp_ctx, argv[0]);
75 opt_argc = str_list_length(opt_argv);
77 long_options = talloc_array(tmp_ctx, struct poptOption, 1);
78 if (long_options == NULL) {
82 /* create the long_options array */
83 for (i=1;i<argc;i++) {
84 const char *optstr = mprToString(argv[i]);
85 int t, opt_type = POPT_ARG_NONE;
87 if (argv[i]->type != MPR_TYPE_STRING) {
88 ejsSetErrorMsg(eid, "GetOptions string argument");
92 long_options = talloc_realloc(tmp_ctx, long_options,
93 struct poptOption, num_options+2);
94 if (long_options == NULL) {
97 ZERO_STRUCT(long_options[num_options]);
99 /* see if its one of the special samba option tables */
100 for (t=0;t<ARRAY_SIZE(tables);t++) {
101 if (strcmp(tables[t].name, optstr) == 0) {
105 if (t < ARRAY_SIZE(tables)) {
106 opt_names = str_list_add(opt_names, optstr);
107 talloc_steal(tmp_ctx, opt_names);
108 long_options[num_options].argInfo = POPT_ARG_INCLUDE_TABLE;
109 long_options[num_options].arg = tables[t].table;
110 long_options[num_options].descrip = tables[t].description;
115 s = strchr(optstr, '=');
117 char *name = talloc_strndup(tmp_ctx, optstr, (int)(s-optstr));
118 opt_names = str_list_add(opt_names, name);
120 opt_type = POPT_ARG_STRING;
121 } else if (s[1] == 'i') {
122 opt_type = POPT_ARG_INT;
124 ejsSetErrorMsg(eid, "GetOptions invalid option type");
129 opt_names = str_list_add(opt_names, optstr);
131 talloc_steal(tmp_ctx, opt_names);
132 if (strlen(opt_names[num_options]) == 1) {
133 long_options[num_options].shortName = opt_names[num_options][0];
135 long_options[num_options].longName = opt_names[num_options];
137 long_options[num_options].argInfo = opt_type;
138 long_options[num_options].val = num_options + BASE_OPTNUM;
142 ZERO_STRUCT(long_options[num_options]);
144 pc = poptGetContext("smbscript", opt_argc, opt_argv, long_options, 0);
146 /* parse the options */
147 while((opt = poptGetNextOpt(pc)) != -1) {
150 if (opt < BASE_OPTNUM || opt >= num_options + BASE_OPTNUM) {
152 err = talloc_asprintf(tmp_ctx, "%s: %s",
153 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
155 mprSetVar(options, "ERROR", mprString(err));
156 talloc_free(tmp_ctx);
157 mpr_Return(eid, mprCreateUndefinedVar());
161 arg = poptGetOptArg(pc);
163 mprSetVar(options, opt_names[opt], mprCreateBoolVar(1));
164 } else if (long_options[opt].argInfo == POPT_ARG_INT) {
165 int v = strtol(arg, NULL, 0);
166 mprSetVar(options, opt_names[opt], mprCreateIntegerVar(v));
168 mprSetVar(options, opt_names[opt], mprString(arg));
172 /* setup options.argv list */
173 mprSetVar(options, "ARGV", mprList("ARGV", poptGetArgs(pc)));
177 talloc_free(tmp_ctx);
180 mprSetCFunction(options, "get_credentials", ejs_credentials_cmdline);
188 setup C functions that be called from ejs
190 void smb_setup_ejs_options(void)
192 ejsDefineCFunction(-1, "GetOptions", ejs_GetOptions, NULL, MPR_VAR_SCRIPT_HANDLE);