4
** Copyright (c) 1993-2001 by Hans-Ulrich Kiel & Joerg Czeranski
5
** All rights reserved.
7
** Redistribution and use in source and binary forms, with or without
8
** modification, are permitted provided that the following conditions are
11
** 1. Redistributions of source code must retain the above copyright
12
** notice, this list of conditions and the following disclaimer.
13
** 2. Redistributions in binary form must reproduce the above copyright
14
** notice, this list of conditions and the following disclaimer in the
15
** documentation and/or other materials provided with the distribution.
16
** 3. The name of the authors may not be used to endorse or promote
17
** products derived from this software without specific prior written
19
** 4. The name ``iMaze'' may not be used for products derived from this
20
** software unless a prefix or a suffix is added to the name.
22
** THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
23
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25
** DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT,
26
** INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27
** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28
** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30
** STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31
** IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
** POSSIBILITY OF SUCH DAMAGE.
38
** Parses command line parameters
48
static char sccsid[] = "@(#)argv.c 3.9 12/3/01";
54
static int want_help = 0;
55
static struct arg_option all_opts[] =
57
{ Arg_Simple, "h", &want_help, "help" },
58
{ Arg_Include, NULL, argv_opts },
63
char *program_name = "(unknown)";
66
/* prints enough spaces, help (if not NULL) and a newline */
67
static void print_help(int opt_length, char *help)
72
fprintf(stderr, "\n");
73
else if (opt_length < OPT_LENGTH)
75
sprintf(help_format, "%%%ds%%s\n", OPT_LENGTH - opt_length);
76
fprintf(stderr, help_format, " ", help);
79
fprintf(stderr, " %s\n", help);
83
static void print_opts(struct arg_option *opts)
95
print_opts(opts->data);
99
fprintf(stderr, "\t-%s", opts->name);
100
print_help(2 + strlen(opts->name), opts->help);
106
opts->param_name == NULL ? "(parameter)" :
108
fprintf(stderr, "\t-%s %s", opts->name, param_name);
109
print_help(2 + strlen(opts->name) + 1
110
+ strlen(param_name), opts->help);
122
static struct arg_option *get_arg_end(void)
124
struct arg_option *opt;
126
for (opt = argv_opts; opt->type != Arg_End; opt++);
131
static void usage(void)
133
struct arg_option *opt;
137
fprintf(stderr, "usage: %s [options]", program_name);
138
if (opt->data != NULL)
139
fprintf(stderr, " %s",
140
opt->param_name == NULL ? "(parameter)" :
142
fprintf(stderr, "\n\tvalid options:\n");
143
print_opts(all_opts);
148
static struct arg_option *match_option(struct arg_option *opts, char *name)
159
struct arg_option *result;
161
result = match_option(opts->data, name);
170
if (!strcmp(opts->name, name))
183
/* up to here local part */
184
/***********************************************/
185
/* from here on global part */
188
void process_args(int *argc, char **argv)
192
struct arg_option *opt;
196
program_name = strrchr(argv[0], '/');
197
if (program_name == NULL || program_name[1] == 0)
198
program_name = argv[0];
206
while (arg_i < *argc)
215
if (argv[arg_i][0] != '-')
218
if (!strcmp(argv[arg_i], "-") ||
219
!strcmp(argv[arg_i], "--"))
228
if (argv[arg_i][ofs] == 0)
235
/* only single char options for now */
236
name[0] = argv[arg_i][ofs];
238
opt = match_option(all_opts, name);
241
fprintf(stderr, "%s: unknown option -%s\n\n",
250
*(int *)opt->data = 1;
255
if (argv[arg_i][ofs + 1] == 0 && arg_i + 1 >= *argc)
258
"%s: parameter missing for -%s\n\n",
259
program_name, opt->name);
264
if (argv[arg_i][ofs] == 0)
270
str = argv[arg_i] + ofs;
274
if (opt->type == Arg_String)
275
*(char **)opt->data = str;
276
else /* Arg_Callback */
280
f = (int (*)())opt->data;
284
"%s: bad parameter for -%s: %s\n\n",
285
program_name, opt->name, str);
297
/* there can be no partial string left */
302
if (opt->data == NULL)
306
fprintf(stderr, "%s: too many parameters\n\n",
315
fprintf(stderr, "%s: parameter missing\n\n",
319
else if (arg_i + 1 < *argc)
321
fprintf(stderr, "%s: too many parameters\n\n",
326
*(char **)opt->data = argv[arg_i++];
329
*argc = *argc < 1 ? 0 : 1;