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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "lib/cmdline/popt_common.h"
25 #include "scripting/ejs/smbcalls.h"
30 options = GetOptions(argv,
35 the special options POPT_COMMON_* options are recognised and replaced
36 with the Samba internal options
38 resulting parsed options are placed in the options object
40 additional command line arguments are placed in options.ARGV
43 static int ejs_GetOptions(MprVarHandle eid, int argc, struct MprVar **argv)
49 struct poptOption *table;
50 const char *description;
52 { "POPT_AUTOHELP", poptHelpOptions, "Help options:" },
53 { "POPT_COMMON_SAMBA", popt_common_samba, "Common Samba options:" },
54 { "POPT_COMMON_CONNECTION", popt_common_connection, "Connection options:" },
55 { "POPT_COMMON_CREDENTIALS", popt_common_credentials, "Authentication options:" },
56 { "POPT_COMMON_VERSION", popt_common_version, "Common Samba options:" }
59 struct MprVar *options = mprInitObject(eid, "options", 0, NULL);
61 TALLOC_CTX *tmp_ctx = talloc_new(mprMemCtx());
62 struct poptOption *long_options = NULL;
63 int i, num_options = 0;
65 const char **opt_argv;
66 const char **opt_names = NULL;
67 const int BASE_OPTNUM = 0x100000;
69 /* validate arguments */
70 if (argc < 1 || argv[0]->type != MPR_TYPE_OBJECT) {
71 ejsSetErrorMsg(eid, "GetOptions invalid arguments");
75 opt_argv = mprToArray(tmp_ctx, argv[0]);
76 opt_argc = str_list_length(opt_argv);
78 long_options = talloc_array(tmp_ctx, struct poptOption, 1);
79 if (long_options == NULL) {
83 /* create the long_options array */
84 for (i=1;i<argc;i++) {
85 const char *optstr = mprToString(argv[i]);
86 int t, opt_type = POPT_ARG_NONE;
88 if (argv[i]->type != MPR_TYPE_STRING) {
89 ejsSetErrorMsg(eid, "GetOptions string argument");
93 long_options = talloc_realloc(tmp_ctx, long_options,
94 struct poptOption, num_options+2);
95 if (long_options == NULL) {
98 ZERO_STRUCT(long_options[num_options]);
100 /* see if its one of the special samba option tables */
101 for (t=0;t<ARRAY_SIZE(tables);t++) {
102 if (strcmp(tables[t].name, optstr) == 0) {
106 if (t < ARRAY_SIZE(tables)) {
107 opt_names = str_list_add(opt_names, optstr);
108 talloc_steal(tmp_ctx, opt_names);
109 long_options[num_options].argInfo = POPT_ARG_INCLUDE_TABLE;
110 long_options[num_options].arg = tables[t].table;
111 long_options[num_options].descrip = tables[t].description;
116 s = strchr(optstr, '=');
118 char *name = talloc_strndup(tmp_ctx, optstr, (int)(s-optstr));
119 opt_names = str_list_add(opt_names, name);
121 opt_type = POPT_ARG_STRING;
122 } else if (s[1] == 'i') {
123 opt_type = POPT_ARG_INT;
125 ejsSetErrorMsg(eid, "GetOptions invalid option type");
130 opt_names = str_list_add(opt_names, optstr);
132 talloc_steal(tmp_ctx, opt_names);
133 if (strlen(opt_names[num_options]) == 1) {
134 long_options[num_options].shortName = opt_names[num_options][0];
136 long_options[num_options].longName = opt_names[num_options];
138 long_options[num_options].argInfo = opt_type;
139 long_options[num_options].val = num_options + BASE_OPTNUM;
143 ZERO_STRUCT(long_options[num_options]);
145 pc = poptGetContext("smbscript", opt_argc, opt_argv, long_options, 0);
147 /* parse the options */
148 while((opt = poptGetNextOpt(pc)) != -1) {
151 if (opt < BASE_OPTNUM || opt >= num_options + BASE_OPTNUM) {
153 err = talloc_asprintf(tmp_ctx, "%s: %s",
154 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
156 mprSetVar(options, "ERROR", mprString(err));
157 talloc_free(tmp_ctx);
158 mpr_Return(eid, mprCreateUndefinedVar());
162 arg = poptGetOptArg(pc);
164 mprSetVar(options, opt_names[opt], mprCreateBoolVar(1));
165 } else if (long_options[opt].argInfo == POPT_ARG_INT) {
166 int v = strtol(arg, NULL, 0);
167 mprSetVar(options, opt_names[opt], mprCreateIntegerVar(v));
169 mprSetVar(options, opt_names[opt], mprString(arg));
173 /* setup options.argv list */
174 mprSetVar(options, "ARGV", mprList("ARGV", poptGetArgs(pc)));
178 talloc_free(tmp_ctx);
181 mprSetCFunction(options, "get_credentials", ejs_credentials_cmdline);
189 setup C functions that be called from ejs
191 void smb_setup_ejs_options(void)
193 ejsDefineCFunction(-1, "GetOptions", ejs_GetOptions, NULL, MPR_VAR_SCRIPT_HANDLE);