47
59
#include <getopt.h>
51
64
/* NON_OPT is the code that is returned when a non-option is found in '+'
54
67
/* LONG_OPT is the code that is returned when a long option is found. */
57
70
/* The shells recognized. */
58
typedef enum {BASH,TCSH} shell_t;
71
typedef enum { BASH, TCSH } shell_t;
61
74
/* Some global variables that tells us how to parse. */
62
shell_t shell=BASH; /* The shell we generate output for. */
63
int quiet_errors=0; /* 0 is not quiet. */
64
int quiet_output=0; /* 0 is not quiet. */
65
int quote=1; /* 1 is do quote. */
66
int alternative=0; /* 0 is getopt_long, 1 is getopt_long_only */
75
static shell_t shell = BASH; /* The shell we generate output for. */
76
static int quiet_errors = 0; /* 0 is not quiet. */
77
static int quiet_output = 0; /* 0 is not quiet. */
78
static int quote = 1; /* 1 is do quote. */
80
/* Allow changing which getopt is in use with function pointer */
81
int (*getopt_long_fp) (int argc, char *const *argv, const char *optstr,
82
const struct option * longopts, int *longindex);
68
84
/* Function prototypes */
69
void *our_malloc(size_t size);
70
void *our_realloc(void *ptr, size_t size);
71
const char *normalize(const char *arg);
72
int generate_output(char * argv[],int argc,const char *optstr,
73
const struct option *longopts);
85
static const char *normalize(const char *arg);
86
static int generate_output(char *argv[], int argc, const char *optstr,
87
const struct option *longopts);
74
88
int main(int argc, char *argv[]);
75
void parse_error(const char *message);
76
void add_long_options(char *options);
77
void add_longopt(const char *name,int has_arg);
78
void print_help(void);
79
void set_shell(const char *new_shell);
80
void set_initial_shell(void);
82
void *our_malloc(size_t size)
84
void *ret=malloc(size);
86
fprintf(stderr,_("%s: Out of memory!\n"),"getopt");
92
void *our_realloc(void *ptr, size_t size)
94
void *ret=realloc(ptr,size);
96
fprintf(stderr,_("%s: Out of memory!\n"),"getopt");
89
static void parse_error(const char *message);
90
static void add_long_options(char *options);
91
static void add_longopt(const char *name, int has_arg);
92
static void print_help(void);
93
static void set_shell(const char *new_shell);
103
* This function 'normalizes' a single argument: it puts single quotes around
104
* it and escapes other special characters. If quote is false, it just
105
* returns its argument.
96
* This function 'normalizes' a single argument: it puts single quotes
97
* around it and escapes other special characters. If quote is false, it
98
* just returns its argument.
106
100
* Bash only needs special treatment for single quotes; tcsh also recognizes
107
* exclamation marks within single quotes, and nukes whitespace.
108
* This function returns a pointer to a buffer that is overwritten by
101
* exclamation marks within single quotes, and nukes whitespace. This
102
* function returns a pointer to a buffer that is overwritten by each call.
111
const char *normalize(const char *arg)
104
static const char *normalize(const char *arg)
113
static char *BUFFER=NULL;
114
const char *argptr=arg;
106
static char *BUFFER = NULL;
107
const char *argptr = arg;
119
if (!quote) { /* Just copy arg */
120
BUFFER=our_malloc(strlen(arg)+1);
112
BUFFER = xmalloc(strlen(arg) + 1);
126
/* Each character in arg may take upto four characters in the result:
127
For a quote we need a closing quote, a backslash, a quote and an
128
opening quote! We need also the global opening and closing quote,
129
and one extra character for '\0'. */
130
BUFFER=our_malloc(strlen(arg)*4+3);
118
* Each character in arg may take upto four characters in the
119
* result: For a quote we need a closing quote, a backslash, a quote
120
* and an opening quote! We need also the global opening and closing
121
* quote, and one extra character for '\0'.
123
BUFFER = xmalloc(strlen(arg) * 4 + 3);
135
128
while (*argptr) {
136
129
if (*argptr == '\'') {
137
130
/* Quote: replace it with: '\'' */
142
} else if (shell==TCSH && *argptr=='!') {
135
} else if (shell == TCSH && *argptr == '!') {
143
136
/* Exclamation mark: replace it with: \! */
148
} else if (shell==TCSH && *argptr=='\n') {
141
} else if (shell == TCSH && *argptr == '\n') {
149
142
/* Newline: replace it with: \n */
152
} else if (shell==TCSH && isspace(*argptr)) {
145
} else if (shell == TCSH && isspace(*argptr)) {
153
146
/* Non-newline whitespace: replace it with \<ws> */
172
165
* optstr must contain the short options, and longopts the long options.
173
166
* Other settings are found in global variables.
175
int generate_output(char * argv[],int argc,const char *optstr,
176
const struct option *longopts)
168
static int generate_output(char *argv[], int argc, const char *optstr,
169
const struct option *longopts)
178
int exit_code = 0; /* We assume everything will be OK */
171
int exit_code = EXIT_SUCCESS; /* Assume everything will be OK */
181
174
const char *charptr;
183
if (quiet_errors) /* No error reporting from getopt(3) */
185
optind=0; /* Reset getopt(3) */
177
/* No error reporting from getopt(3) */
179
/* Reset getopt(3) */
187
while ((opt = (alternative?
188
getopt_long_only(argc,argv,optstr,longopts,&longindex):
189
getopt_long(argc,argv,optstr,longopts,&longindex)))
191
if (opt == '?' || opt == ':' )
193
else if (!quiet_output)
183
(getopt_long_fp(argc, argv, optstr, longopts, &longindex)))
185
if (opt == '?' || opt == ':')
186
exit_code = GETOPT_EXIT_CODE;
187
else if (!quiet_output) {
195
188
if (opt == LONG_OPT) {
196
printf(" --%s",longopts[longindex].name);
197
if (longopts[longindex].has_arg)
199
normalize(optarg?optarg:""));
200
} else if (opt == NON_OPT)
201
printf(" %s",normalize(optarg));
189
printf(" --%s", longopts[longindex].name);
190
if (longopts[longindex].has_arg)
191
printf(" %s", normalize(optarg ? optarg : ""));
192
} else if (opt == NON_OPT)
193
printf(" %s", normalize(optarg));
204
charptr = strchr(optstr,opt);
196
charptr = strchr(optstr, opt);
205
197
if (charptr != NULL && *++charptr == ':')
207
normalize(optarg?optarg:""));
198
printf(" %s", normalize(optarg ? optarg : ""));
211
if (! quiet_output) {
213
while (optind < argc)
214
printf(" %s",normalize(argv[optind++]));
204
while (optind < argc)
205
printf(" %s", normalize(argv[optind++]));
217
208
return exit_code;
221
* Report an error when parsing getopt's own arguments.
222
* If message is NULL, we already sent a message, we just exit with a helpful
212
* Report an error when parsing getopt's own arguments. If message is NULL,
213
* we already sent a message, we just exit with a helpful hint.
225
void parse_error(const char *message)
215
static void __attribute__ ((__noreturn__)) parse_error(const char *message)
228
fprintf(stderr,"getopt: %s\n",message);
229
fputs(_("Try `getopt --help' for more information.\n"),stderr);
218
warnx("%s", message);
219
fprintf(stderr, _("Try `%s --help' for more information.\n"),
220
program_invocation_short_name);
221
exit(PARAMETER_EXIT_CODE);
233
static struct option *long_options=NULL;
234
static int long_options_length=0; /* Length of array */
235
static int long_options_nr=0; /* Nr of used elements in array */
224
static struct option *long_options = NULL;
225
static int long_options_length = 0; /* Length of array */
226
static int long_options_nr = 0; /* Nr of used elements in array */
236
227
#define LONG_OPTIONS_INCR 10
237
228
#define init_longopt() add_longopt(NULL,0)
239
230
/* Register a long option. The contents of name is copied. */
240
void add_longopt(const char *name,int has_arg)
231
static void add_longopt(const char *name, int has_arg)
243
if (!name) { /* init */
244
236
free(long_options);
246
long_options_length=0;
238
long_options_length = 0;
250
242
if (long_options_nr == long_options_length) {
251
243
long_options_length += LONG_OPTIONS_INCR;
252
long_options=our_realloc(long_options,
253
sizeof(struct option) *
254
long_options_length);
244
long_options = xrealloc(long_options,
245
sizeof(struct option) *
246
long_options_length);
257
long_options[long_options_nr].name=NULL;
258
long_options[long_options_nr].has_arg=0;
259
long_options[long_options_nr].flag=NULL;
260
long_options[long_options_nr].val=0;
249
long_options[long_options_nr].name = NULL;
250
long_options[long_options_nr].has_arg = 0;
251
long_options[long_options_nr].flag = NULL;
252
long_options[long_options_nr].val = 0;
262
if (long_options_nr) { /* Not for init! */
263
long_options[long_options_nr-1].has_arg=has_arg;
264
long_options[long_options_nr-1].flag=NULL;
265
long_options[long_options_nr-1].val=LONG_OPT;
266
tmp = our_malloc(strlen(name)+1);
268
long_options[long_options_nr-1].name=tmp;
254
if (long_options_nr) {
256
long_options[long_options_nr - 1].has_arg = has_arg;
257
long_options[long_options_nr - 1].flag = NULL;
258
long_options[long_options_nr - 1].val = LONG_OPT;
259
tmp = xmalloc(strlen(name) + 1);
261
long_options[long_options_nr - 1].name = tmp;
270
263
long_options_nr++;
275
* Register several long options. options is a string of long options,
276
* separated by commas or whitespace.
277
* This nukes options!
268
* Register several long options. options is a string of long options,
269
* separated by commas or whitespace. This nukes options!
279
void add_long_options(char *options)
271
static void add_long_options(char *options)
282
char *tokptr=strtok(options,", \t\n");
274
char *tokptr = strtok(options, ", \t\n");
276
arg_opt = no_argument;
285
277
if (strlen(tokptr) > 0) {
286
if (tokptr[strlen(tokptr)-1] == ':') {
287
if (tokptr[strlen(tokptr)-2] == ':') {
288
tokptr[strlen(tokptr)-2]='\0';
289
arg_opt=optional_argument;
278
if (tokptr[strlen(tokptr) - 1] == ':') {
279
if (tokptr[strlen(tokptr) - 2] == ':') {
280
tokptr[strlen(tokptr) - 2] = '\0';
281
arg_opt = optional_argument;
291
tokptr[strlen(tokptr)-1]='\0';
292
arg_opt=required_argument;
283
tokptr[strlen(tokptr) - 1] = '\0';
284
arg_opt = required_argument;
294
286
if (strlen(tokptr) == 0)
295
parse_error(_("empty long option after "
296
"-l or --long argument"));
288
("empty long option after "
289
"-l or --long argument"));
298
add_longopt(tokptr,arg_opt);
291
add_longopt(tokptr, arg_opt);
300
tokptr=strtok(NULL,", \t\n");
293
tokptr = strtok(NULL, ", \t\n");
304
void set_shell(const char *new_shell)
297
static void set_shell(const char *new_shell)
306
if (!strcmp(new_shell,"bash"))
308
else if (!strcmp(new_shell,"tcsh"))
310
else if (!strcmp(new_shell,"sh"))
312
else if (!strcmp(new_shell,"csh"))
299
if (!strcmp(new_shell, "bash"))
301
else if (!strcmp(new_shell, "tcsh"))
303
else if (!strcmp(new_shell, "sh"))
305
else if (!strcmp(new_shell, "csh"))
315
parse_error(_("unknown shell after -s or --shell argument"));
309
("unknown shell after -s or --shell argument"));
318
void print_help(void)
312
static void __attribute__ ((__noreturn__)) print_help(void)
320
fputs(_("Usage: getopt optstring parameters\n"),stderr);
321
fputs(_(" getopt [options] [--] optstring parameters\n"),stderr);
322
fputs(_(" getopt [options] -o|--options optstring [options] [--]\n"),stderr);
323
fputs(_(" parameters\n"),stderr);
324
fputs(_(" -a, --alternative Allow long options starting with single -\n"),stderr);
325
fputs(_(" -h, --help This small usage guide\n"),stderr);
326
fputs(_(" -l, --longoptions=longopts Long options to be recognized\n"),stderr);
327
fputs(_(" -n, --name=progname The name under which errors are reported\n"),stderr);
328
fputs(_(" -o, --options=optstring Short options to be recognized\n"),stderr);
329
fputs(_(" -q, --quiet Disable error reporting by getopt(3)\n"),stderr);
330
fputs(_(" -Q, --quiet-output No normal output\n"),stderr);
331
fputs(_(" -s, --shell=shell Set shell quoting conventions\n"),stderr);
332
fputs(_(" -T, --test Test for getopt(1) version\n"),stderr);
333
fputs(_(" -u, --unqote Do not quote the output\n"),stderr);
334
fputs(_(" -V, --version Output version information\n"),stderr);
314
fputs(_("\nUsage:\n"), stderr);
317
" %1$s optstring parameters\n"
318
" %1$s [options] [--] optstring parameters\n"
319
" %1$s [options] -o|--options optstring [options] [--] parameters\n"),
320
program_invocation_short_name);
322
fputs(_("\nOptions:\n"), stderr);
323
fputs(_(" -a, --alternative Allow long options starting with single -\n"), stderr);
324
fputs(_(" -h, --help This small usage guide\n"), stderr);
325
fputs(_(" -l, --longoptions <longopts> Long options to be recognized\n"), stderr);
326
fputs(_(" -n, --name <progname> The name under which errors are reported\n"), stderr);
327
fputs(_(" -o, --options <optstring> Short options to be recognized\n"), stderr);
328
fputs(_(" -q, --quiet Disable error reporting by getopt(3)\n"), stderr);
329
fputs(_(" -Q, --quiet-output No normal output\n"), stderr);
330
fputs(_(" -s, --shell <shell> Set shell quoting conventions\n"), stderr);
331
fputs(_(" -T, --test Test for getopt(1) version\n"), stderr);
332
fputs(_(" -u, --unquote Do not quote the output\n"), stderr);
333
fputs(_(" -V, --version Output version information\n"), stderr);
336
exit(PARAMETER_EXIT_CODE);
339
* 0) No errors, succesful operation.
340
* 1) getopt(3) returned an error.
341
* 2) A problem with parameter parsing for getopt(1).
342
* 3) Internal error, out of memory
346
static struct option longopts[]={ {"options",required_argument,NULL,'o'},
347
{"longoptions",required_argument,NULL,'l'},
348
{"quiet",no_argument,NULL,'q'},
349
{"quiet-output",no_argument,NULL,'Q'},
350
{"shell",required_argument,NULL,'s'},
351
{"test",no_argument,NULL,'T'},
352
{"unquoted",no_argument,NULL,'u'},
353
{"help",no_argument,NULL,'h'},
354
{"alternative",no_argument,NULL,'a'},
355
{"name",required_argument,NULL,'n'},
356
{"version",no_argument,NULL,'V'},
360
/* Stop scanning as soon as a non-option argument is found! */
361
static const char *shortopts="+ao:l:n:qQs:TuhV";
363
339
int main(int argc, char *argv[])
370
setlocale(LC_ALL,"");
346
/* Stop scanning as soon as a non-option argument is found! */
347
static const char *shortopts = "+ao:l:n:qQs:TuhV";
348
static const struct option longopts[] = {
349
{"options", required_argument, NULL, 'o'},
350
{"longoptions", required_argument, NULL, 'l'},
351
{"quiet", no_argument, NULL, 'q'},
352
{"quiet-output", no_argument, NULL, 'Q'},
353
{"shell", required_argument, NULL, 's'},
354
{"test", no_argument, NULL, 'T'},
355
{"unquoted", no_argument, NULL, 'u'},
356
{"help", no_argument, NULL, 'h'},
357
{"alternative", no_argument, NULL, 'a'},
358
{"name", required_argument, NULL, 'n'},
359
{"version", no_argument, NULL, 'V'},
363
setlocale(LC_ALL, "");
371
364
bindtextdomain(PACKAGE, LOCALEDIR);
372
365
textdomain(PACKAGE);
376
if (getenv("GETOPT_COMPATIBLE"))
368
getopt_long_fp = getopt_long;
370
if (getenv("GETOPT_COMPATIBLE"))
381
374
if (compatible) {
382
/* For some reason, the original getopt gave no error
383
when there were no arguments. */
376
* For some reason, the original getopt gave no
377
* error when there were no arguments.
388
382
parse_error(_("missing optstring argument"));
391
385
if (argv[1][0] != '-' || compatible) {
393
optstr=our_malloc(strlen(argv[1])+1);
394
strcpy(optstr,argv[1]+strspn(argv[1],"-+"));
396
exit(generate_output(argv+1,argc-1,optstr,long_options));
387
optstr = xmalloc(strlen(argv[1]) + 1);
388
strcpy(optstr, argv[1] + strspn(argv[1], "-+"));
390
return generate_output(argv + 1, argc - 1, optstr,
399
while ((opt=getopt_long(argc,argv,shortopts,longopts,NULL)) != EOF)
395
getopt_long(argc, argv, shortopts, longopts, NULL)) != EOF)
398
getopt_long_fp = getopt_long_only;
409
optstr=our_malloc(strlen(optarg)+1);
410
strcpy(optstr,optarg);
404
optstr = xmalloc(strlen(optarg) + 1);
405
strcpy(optstr, optarg);
413
408
add_long_options(optarg);
417
name=our_malloc(strlen(optarg)+1);
412
name = xmalloc(strlen(optarg) + 1);
413
strcpy(name, optarg);
427
422
set_shell(optarg);
425
return TEST_EXIT_CODE;
435
printf(_("getopt (enhanced) 1.1.4\n"));
430
printf(_("%s from %s\n"),
431
program_invocation_short_name,
439
436
parse_error(NULL);