]>
Commit | Line | Data |
---|---|---|
d7091d84 CW |
1 | /* |
2 | getopt.c - Enhanced implementation of BSD getopt(1) | |
3 | Copyright (c) 1997, 1998, 1999, 2000 Frodo Looijaard <frodol@dds.nl> | |
4 | ||
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 2 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 | */ | |
19 | ||
20 | /* | |
21 | * Version 1.0-b4: Tue Sep 23 1997. First public release. | |
22 | * Version 1.0: Wed Nov 19 1997. | |
23 | * Bumped up the version number to 1.0 | |
24 | * Fixed minor typo (CSH instead of TCSH) | |
25 | * Version 1.0.1: Tue Jun 3 1998 | |
26 | * Fixed sizeof instead of strlen bug | |
27 | * Bumped up the version number to 1.0.1 | |
28 | * Version 1.0.2: Thu Jun 11 1998 (not present) | |
29 | * Fixed gcc-2.8.1 warnings | |
30 | * Fixed --version/-V option (not present) | |
31 | * Version 1.0.5: Tue Jun 22 1999 | |
32 | * Make -u option work (not present) | |
33 | * Version 1.0.6: Tue Jun 27 2000 | |
34 | * No important changes | |
35 | */ | |
36 | #if HAVE_CONFIG_H | |
37 | #include "config.h" | |
38 | #endif | |
39 | #include "common.h" | |
40 | ||
41 | /* common.h does this */ | |
42 | /* | |
43 | #include <stdio.h> | |
44 | #include <stdlib.h> | |
45 | #include <string.h> | |
46 | #include <unistd.h> | |
47 | #include <ctype.h> | |
48 | ||
49 | #if LIBCGETOPT | |
50 | #include <getopt.h> | |
51 | #else | |
52 | #include "getopt.h" | |
53 | #endif | |
54 | */ | |
55 | ||
56 | #define _(x) x | |
57 | ||
58 | /* NON_OPT is the code that is returned when a non-option is found in '+' | |
59 | mode */ | |
60 | #define NON_OPT 1 | |
61 | /* LONG_OPT is the code that is returned when a long option is found. */ | |
62 | #define LONG_OPT 2 | |
63 | ||
64 | /* The shells recognized. */ | |
65 | typedef enum {BASH,TCSH} shell_t; | |
66 | ||
67 | ||
68 | /* Some global variables that tells us how to parse. */ | |
69 | shell_t shell=BASH; /* The shell we generate output for. */ | |
70 | int quiet_errors=0; /* 0 is not quiet. */ | |
71 | int quiet_output=0; /* 0 is not quiet. */ | |
72 | int quote=1; /* 1 is do quote. */ | |
73 | #ifdef HAVE_GETOPT_LONG_ONLY | |
74 | int alternative=0; /* 0 is getopt_long, 1 is getopt_long_only */ | |
75 | #endif | |
76 | ||
77 | /* Function prototypes */ | |
78 | void *our_malloc(size_t size); | |
79 | void *our_realloc(void *ptr, size_t size); | |
80 | const char *normalize(const char *arg); | |
81 | int generate_output(char * argv[],int argc,const char *optstr, | |
82 | const struct option *longopts); | |
83 | int main(int argc, char *argv[]); | |
84 | void parse_error(const char *message); | |
85 | void add_long_options(char *options); | |
86 | void add_longopt(const char *name,int has_arg); | |
87 | void print_help(void); | |
88 | void set_shell(const char *new_shell); | |
89 | void set_initial_shell(void); | |
90 | ||
91 | void *our_malloc(size_t size) | |
92 | { | |
93 | void *ret=malloc(size); | |
94 | if (! ret) { | |
95 | fprintf(stderr,_("%s: Out of memory!\n"),"getopt"); | |
96 | exit(3); | |
97 | } | |
98 | return(ret); | |
99 | } | |
100 | ||
101 | void *our_realloc(void *ptr, size_t size) | |
102 | { | |
103 | void *ret=realloc(ptr,size); | |
104 | if (! ret && size) { | |
105 | fprintf(stderr,_("%s: Out of memory!\n"),"getopt"); | |
106 | exit(3); | |
107 | } | |
108 | return(ret); | |
109 | } | |
110 | ||
111 | /* | |
112 | * This function 'normalizes' a single argument: it puts single quotes around | |
113 | * it and escapes other special characters. If quote is false, it just | |
114 | * returns its argument. | |
115 | * Bash only needs special treatment for single quotes; tcsh also recognizes | |
116 | * exclamation marks within single quotes, and nukes whitespace. | |
117 | * This function returns a pointer to a buffer that is overwritten by | |
118 | * each call. | |
119 | */ | |
120 | const char *normalize(const char *arg) | |
121 | { | |
122 | static char *BUFFER=NULL; | |
123 | const char *argptr=arg; | |
124 | char *bufptr; | |
125 | ||
126 | if (BUFFER != NULL) | |
127 | free(BUFFER); | |
128 | ||
129 | if (!quote) { /* Just copy arg */ | |
130 | BUFFER=our_malloc(strlen(arg)+1); | |
131 | ||
132 | strcpy(BUFFER,arg); | |
133 | return BUFFER; | |
134 | } | |
135 | ||
136 | /* Each character in arg may take upto four characters in the result: | |
137 | For a quote we need a closing quote, a backslash, a quote and an | |
138 | opening quote! We need also the global opening and closing quote, | |
139 | and one extra character for '\0'. */ | |
140 | BUFFER=our_malloc(strlen(arg)*4+3); | |
141 | ||
142 | bufptr=BUFFER; | |
143 | *bufptr++='\''; | |
144 | ||
145 | while (*argptr) { | |
146 | if (*argptr == '\'') { | |
147 | /* Quote: replace it with: '\'' */ | |
148 | *bufptr++='\''; | |
149 | *bufptr++='\\'; | |
150 | *bufptr++='\''; | |
151 | *bufptr++='\''; | |
152 | } else if (shell==TCSH && *argptr=='!') { | |
153 | /* Exclamation mark: replace it with: \! */ | |
154 | *bufptr++='\''; | |
155 | *bufptr++='\\'; | |
156 | *bufptr++='!'; | |
157 | *bufptr++='\''; | |
158 | } else if (shell==TCSH && *argptr=='\n') { | |
159 | /* Newline: replace it with: \n */ | |
160 | *bufptr++='\\'; | |
161 | *bufptr++='n'; | |
162 | } else if (shell==TCSH && isspace(*argptr)) { | |
163 | /* Non-newline whitespace: replace it with \<ws> */ | |
164 | *bufptr++='\''; | |
165 | *bufptr++='\\'; | |
166 | *bufptr++=*argptr; | |
167 | *bufptr++='\''; | |
168 | } else | |
169 | /* Just copy */ | |
170 | *bufptr++=*argptr; | |
171 | argptr++; | |
172 | } | |
173 | *bufptr++='\''; | |
174 | *bufptr++='\0'; | |
175 | return BUFFER; | |
176 | } | |
177 | ||
178 | /* | |
179 | * Generate the output. argv[0] is the program name (used for reporting errors). | |
180 | * argv[1..] contains the options to be parsed. argc must be the number of | |
181 | * elements in argv (ie. 1 if there are no options, only the program name), | |
182 | * optstr must contain the short options, and longopts the long options. | |
183 | * Other settings are found in global variables. | |
184 | */ | |
185 | int generate_output(char * argv[],int argc,const char *optstr, | |
186 | const struct option *longopts) | |
187 | { | |
188 | int exit_code = 0; /* We assume everything will be OK */ | |
189 | int opt; | |
190 | int longindex; | |
191 | const char *charptr; | |
192 | ||
193 | if (quiet_errors) /* No error reporting from getopt(3) */ | |
194 | opterr=0; | |
195 | optind=0; /* Reset getopt(3) */ | |
196 | ||
197 | #ifdef HAVE_GETOPT_LONG_ONLY | |
198 | while ((opt = (alternative? | |
199 | getopt_long_only(argc,argv,optstr,longopts,&longindex): | |
200 | #else | |
201 | while ((opt = ( | |
202 | #endif | |
203 | getopt_long(argc,argv,optstr,longopts,&longindex))) | |
204 | != EOF) | |
205 | if (opt == '?' || opt == ':' ) | |
206 | exit_code = 1; | |
207 | else if (!quiet_output) | |
208 | { | |
209 | if (opt == LONG_OPT) { | |
210 | printf(" --%s",longopts[longindex].name); | |
211 | if (longopts[longindex].has_arg) | |
212 | printf(" %s", | |
213 | normalize(optarg?optarg:"")); | |
214 | } else if (opt == NON_OPT) | |
215 | printf(" %s",normalize(optarg)); | |
216 | else { | |
217 | printf(" -%c",opt); | |
218 | charptr = strchr(optstr,opt); | |
219 | if (charptr != NULL && *++charptr == ':') | |
220 | printf(" %s", | |
221 | normalize(optarg?optarg:"")); | |
222 | } | |
223 | } | |
224 | ||
225 | if (! quiet_output) { | |
226 | printf(" --"); | |
227 | while (optind < argc) | |
228 | printf(" %s",normalize(argv[optind++])); | |
229 | printf("\n"); | |
230 | } | |
231 | return exit_code; | |
232 | } | |
233 | ||
234 | /* | |
235 | * Report an error when parsing getopt's own arguments. | |
236 | * If message is NULL, we already sent a message, we just exit with a helpful | |
237 | * hint. | |
238 | */ | |
239 | void parse_error(const char *message) | |
240 | { | |
241 | if (message) | |
242 | fprintf(stderr,"getopt: %s\n",message); | |
243 | fputs(_("Try `getopt --help' for more information.\n"),stderr); | |
244 | exit(2); | |
245 | } | |
246 | ||
247 | static struct option *long_options=NULL; | |
248 | static int long_options_length=0; /* Length of array */ | |
249 | static int long_options_nr=0; /* Nr of used elements in array */ | |
250 | #define LONG_OPTIONS_INCR 10 | |
251 | #define init_longopt() add_longopt(NULL,0) | |
252 | ||
253 | /* Register a long option. The contents of name is copied. */ | |
254 | void add_longopt(const char *name,int has_arg) | |
255 | { | |
256 | char *tmp; | |
257 | if (!name) { /* init */ | |
258 | free(long_options); | |
259 | long_options=NULL; | |
260 | long_options_length=0; | |
261 | long_options_nr=0; | |
262 | } | |
263 | ||
264 | if (long_options_nr == long_options_length) { | |
265 | long_options_length += LONG_OPTIONS_INCR; | |
266 | long_options=our_realloc(long_options, | |
267 | sizeof(struct option) * | |
268 | long_options_length); | |
269 | } | |
270 | ||
271 | long_options[long_options_nr].name=NULL; | |
272 | long_options[long_options_nr].has_arg=0; | |
273 | long_options[long_options_nr].flag=NULL; | |
274 | long_options[long_options_nr].val=0; | |
275 | ||
276 | if (long_options_nr) { /* Not for init! */ | |
277 | long_options[long_options_nr-1].has_arg=has_arg; | |
278 | long_options[long_options_nr-1].flag=NULL; | |
279 | long_options[long_options_nr-1].val=LONG_OPT; | |
280 | tmp = our_malloc(strlen(name)+1); | |
281 | strcpy(tmp,name); | |
282 | long_options[long_options_nr-1].name=tmp; | |
283 | } | |
284 | long_options_nr++; | |
285 | } | |
286 | ||
287 | ||
288 | /* | |
289 | * Register several long options. options is a string of long options, | |
290 | * separated by commas or whitespace. | |
291 | * This nukes options! | |
292 | */ | |
293 | void add_long_options(char *options) | |
294 | { | |
295 | int arg_opt; | |
296 | char *tokptr=strtok(options,", \t\n"); | |
297 | while (tokptr) { | |
298 | arg_opt=no_argument; | |
299 | if (strlen(tokptr) > 0) { | |
300 | if (tokptr[strlen(tokptr)-1] == ':') { | |
301 | if (tokptr[strlen(tokptr)-2] == ':') { | |
302 | tokptr[strlen(tokptr)-2]='\0'; | |
303 | arg_opt=optional_argument; | |
304 | } else { | |
305 | tokptr[strlen(tokptr)-1]='\0'; | |
306 | arg_opt=required_argument; | |
307 | } | |
308 | if (strlen(tokptr) == 0) | |
309 | parse_error(_("empty long option after " | |
310 | "-l or --long argument")); | |
311 | } | |
312 | add_longopt(tokptr,arg_opt); | |
313 | } | |
314 | tokptr=strtok(NULL,", \t\n"); | |
315 | } | |
316 | } | |
317 | ||
318 | void set_shell(const char *new_shell) | |
319 | { | |
320 | if (!strcmp(new_shell,"bash")) | |
321 | shell=BASH; | |
322 | else if (!strcmp(new_shell,"tcsh")) | |
323 | shell=TCSH; | |
324 | else if (!strcmp(new_shell,"sh")) | |
325 | shell=BASH; | |
326 | else if (!strcmp(new_shell,"csh")) | |
327 | shell=TCSH; | |
328 | else | |
329 | parse_error(_("unknown shell after -s or --shell argument")); | |
330 | } | |
331 | ||
332 | void print_help(void) | |
333 | { | |
334 | fputs(_("Usage: getopt optstring parameters\n"),stderr); | |
335 | fputs(_(" getopt [options] [--] optstring parameters\n"),stderr); | |
336 | fputs(_(" getopt [options] -o|--options optstring [options] [--]\n"),stderr); | |
337 | fputs(_(" parameters\n"),stderr); | |
338 | #ifdef HAVE_GETOPT_LONG_ONLY | |
339 | fputs(_(" -a, --alternative Allow long options starting with single -\n"),stderr); | |
340 | #endif | |
341 | fputs(_(" -h, --help This small usage guide\n"),stderr); | |
342 | fputs(_(" -l, --longoptions=longopts Long options to be recognized\n"),stderr); | |
343 | fputs(_(" -n, --name=progname The name under which errors are reported\n"),stderr); | |
344 | fputs(_(" -o, --options=optstring Short options to be recognized\n"),stderr); | |
345 | fputs(_(" -q, --quiet Disable error reporting by getopt(3)\n"),stderr); | |
346 | fputs(_(" -Q, --quiet-output No normal output\n"),stderr); | |
347 | fputs(_(" -s, --shell=shell Set shell quoting conventions\n"),stderr); | |
348 | fputs(_(" -T, --test Test for getopt(1) version\n"),stderr); | |
349 | fputs(_(" -u, --unqote Do not quote the output\n"),stderr); | |
350 | fputs(_(" -V, --version Output version information\n"),stderr); | |
351 | exit(2); | |
352 | } | |
353 | ||
354 | /* Exit codes: | |
355 | * 0) No errors, succesful operation. | |
356 | * 1) getopt(3) returned an error. | |
357 | * 2) A problem with parameter parsing for getopt(1). | |
358 | * 3) Internal error, out of memory | |
359 | * 4) Returned for -T | |
360 | */ | |
361 | ||
362 | static struct option longopts[]={ {"options",required_argument,NULL,'o'}, | |
363 | {"longoptions",required_argument,NULL,'l'}, | |
364 | {"quiet",no_argument,NULL,'q'}, | |
365 | {"quiet-output",no_argument,NULL,'Q'}, | |
366 | {"shell",required_argument,NULL,'s'}, | |
367 | {"test",no_argument,NULL,'T'}, | |
368 | {"unquoted",no_argument,NULL,'u'}, | |
369 | {"help",no_argument,NULL,'h'}, | |
370 | #ifdef HAVE_GETOPT_LONG_ONLY | |
371 | {"alternative",no_argument,NULL,'a'}, | |
372 | #endif | |
373 | {"name",required_argument,NULL,'n'}, | |
374 | {"version",no_argument,NULL,'V'}, | |
375 | {NULL,0,NULL,0} | |
376 | }; | |
377 | ||
378 | /* Stop scanning as soon as a non-option argument is found! */ | |
379 | static const char *shortopts="+ao:l:n:qQs:TuhV"; | |
380 | ||
381 | int main(int argc, char *argv[]) | |
382 | { | |
383 | char *optstr=NULL; | |
384 | char *name=NULL; | |
385 | int opt; | |
386 | int compatible=0; | |
387 | ||
388 | init_longopt(); | |
389 | ||
390 | if (getenv("GETOPT_COMPATIBLE")) | |
391 | compatible=1; | |
392 | ||
393 | if (argc == 1) | |
394 | { | |
395 | if (compatible) { | |
396 | /* For some reason, the original getopt gave no error | |
397 | when there were no arguments. */ | |
398 | printf(" --\n"); | |
399 | exit(0); | |
400 | } | |
401 | else | |
402 | parse_error(_("missing optstring argument")); | |
403 | } | |
404 | ||
405 | if (argv[1][0] != '-' || compatible) { | |
406 | quote=0; | |
407 | optstr=our_malloc(strlen(argv[1])+1); | |
408 | strcpy(optstr,argv[1]+strspn(argv[1],"-+")); | |
409 | argv[1]=argv[0]; | |
410 | exit(generate_output(argv+1,argc-1,optstr,long_options)); | |
411 | } | |
412 | ||
413 | while ((opt=getopt_long(argc,argv,shortopts,longopts,NULL)) != EOF) | |
414 | switch (opt) { | |
415 | #ifdef HAVE_GETOPT_LONG_ONLY | |
416 | case 'a': | |
417 | alternative=1; | |
418 | break; | |
419 | #endif | |
420 | case 'h': | |
421 | print_help(); | |
422 | exit(0); | |
423 | case 'o': | |
424 | if (optstr) | |
425 | free(optstr); | |
426 | optstr=our_malloc(strlen(optarg)+1); | |
427 | strcpy(optstr,optarg); | |
428 | break; | |
429 | case 'l': | |
430 | add_long_options(optarg); | |
431 | break; | |
432 | case 'n': | |
433 | if (name) | |
434 | free(name); | |
435 | name=our_malloc(strlen(optarg)+1); | |
436 | strcpy(name,optarg); | |
437 | break; | |
438 | case 'q': | |
439 | quiet_errors=1; | |
440 | break; | |
441 | case 'Q': | |
442 | quiet_output=1; | |
443 | break; | |
444 | case 's': | |
445 | set_shell(optarg); | |
446 | break; | |
447 | case 'T': | |
448 | exit(4); | |
449 | case 'u': | |
450 | quote=0; | |
451 | break; | |
452 | case 'V': | |
453 | printf(_("getopt (enhanced) 1.1.2\n")); | |
454 | exit(0); | |
455 | case '?': | |
456 | case ':': | |
457 | parse_error(NULL); | |
458 | default: | |
459 | parse_error(_("internal error, contact the author.")); | |
460 | } | |
461 | ||
462 | if (!optstr) | |
463 | { | |
464 | if (optind >= argc) | |
465 | parse_error(_("missing optstring argument")); | |
466 | else { | |
467 | optstr=our_malloc(strlen(argv[optind])+1); | |
468 | strcpy(optstr,argv[optind]); | |
469 | optind++; | |
470 | } | |
471 | } | |
472 | if (name) | |
473 | argv[optind-1]=name; | |
474 | else | |
475 | argv[optind-1]=argv[0]; | |
476 | exit(generate_output(argv+optind-1,argc-optind+1,optstr,long_options)); | |
477 | } |