/[public]/getopt/trunk/getopt.c
ViewVC logotype

Diff of /getopt/trunk/getopt.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 317 Revision 318
37 * <misiek@pld.org.pl>) 37 * <misiek@pld.org.pl>)
38 * Version 1.1.4: Mon Nov 7 2005 38 * Version 1.1.4: Mon Nov 7 2005
39 * Fixed a few type's in the manpage 39 * Fixed a few type's in the manpage
40 */ 40 */
41 41
42/* Exit codes:
43 * 0) No errors, succesful operation.
44 * 1) getopt(3) returned an error.
45 * 2) A problem with parameter parsing for getopt(1).
46 * 3) Internal error, out of memory
47 * 4) Returned for -T
48 */
49#define GETOPT_EXIT_CODE 1
50#define PARAMETER_EXIT_CODE 2
51#define XALLOC_EXIT_CODE 3
52#define TEST_EXIT_CODE 4
53
54
42#include <stdio.h> 55#include <stdio.h>
43#include <stdlib.h> 56#include <stdlib.h>
44#include <string.h> 57#include <string.h>
45#include <unistd.h> 58#include <unistd.h>
46#include <ctype.h> 59#include <ctype.h>
87void *our_malloc(size_t size) 100void *our_malloc(size_t size)
88{ 101{
89 void *ret=malloc(size); 102 void *ret=malloc(size);
90 if (! ret) { 103 if (! ret) {
91 fprintf(stderr,_("%s: Out of memory!\n"),"getopt"); 104 fprintf(stderr,_("%s: Out of memory!\n"),"getopt");
92 exit(3); 105 exit(XALLOC_EXIT_CODE);
93 } 106 }
94 return(ret); 107 return(ret);
95} 108}
96 109
97void *our_realloc(void *ptr, size_t size) 110void *our_realloc(void *ptr, size_t size)
98{ 111{
99 void *ret=realloc(ptr,size); 112 void *ret=realloc(ptr,size);
100 if (! ret && size) { 113 if (! ret && size) {
101 fprintf(stderr,_("%s: Out of memory!\n"),"getopt"); 114 fprintf(stderr,_("%s: Out of memory!\n"),"getopt");
102 exit(3); 115 exit(XALLOC_EXIT_CODE);
103 } 116 }
104 return(ret); 117 return(ret);
105} 118}
106 119
107/* 120/*
182 * Other settings are found in global variables. 195 * Other settings are found in global variables.
183 */ 196 */
184int generate_output(char * argv[],int argc,const char *optstr, 197int generate_output(char * argv[],int argc,const char *optstr,
185 const struct option *longopts) 198 const struct option *longopts)
186{ 199{
187 int exit_code = 0; /* Assume everything will be OK */ 200 int exit_code = EXIT_SUCCESS; /* Assume everything will be OK */
188 int opt; 201 int opt;
189 int longindex; 202 int longindex;
190 const char *charptr; 203 const char *charptr;
191 204
192 if (quiet_errors) 205 if (quiet_errors)
198 while ((opt = (alternative? 211 while ((opt = (alternative?
199 getopt_long_only(argc,argv,optstr,longopts,&longindex): 212 getopt_long_only(argc,argv,optstr,longopts,&longindex):
200 getopt_long(argc,argv,optstr,longopts,&longindex))) 213 getopt_long(argc,argv,optstr,longopts,&longindex)))
201 != EOF) 214 != EOF)
202 if (opt == '?' || opt == ':' ) 215 if (opt == '?' || opt == ':' )
203 exit_code = 1; 216 exit_code = GETOPT_EXIT_CODE;
204 else if (!quiet_output) 217 else if (!quiet_output)
205 { 218 {
206 if (opt == LONG_OPT) { 219 if (opt == LONG_OPT) {
207 printf(" --%s",longopts[longindex].name); 220 printf(" --%s",longopts[longindex].name);
208 if (longopts[longindex].has_arg) 221 if (longopts[longindex].has_arg)
235void parse_error(const char *message) 248void parse_error(const char *message)
236{ 249{
237 if (message) 250 if (message)
238 fprintf(stderr,"getopt: %s\n",message); 251 fprintf(stderr,"getopt: %s\n",message);
239 fputs(_("Try `getopt --help' for more information.\n"),stderr); 252 fputs(_("Try `getopt --help' for more information.\n"),stderr);
240 exit(2); 253 exit(PARAMETER_EXIT_CODE);
241} 254}
242 255
243static struct option *long_options=NULL; 256static struct option *long_options=NULL;
244static int long_options_length=0; /* Length of array */ 257static int long_options_length=0; /* Length of array */
245static int long_options_nr=0; /* Nr of used elements in array */ 258static int long_options_nr=0; /* Nr of used elements in array */
341 fputs(_(" -Q, --quiet-output No normal output\n"),stderr); 354 fputs(_(" -Q, --quiet-output No normal output\n"),stderr);
342 fputs(_(" -s, --shell=shell Set shell quoting conventions\n"),stderr); 355 fputs(_(" -s, --shell=shell Set shell quoting conventions\n"),stderr);
343 fputs(_(" -T, --test Test for getopt(1) version\n"),stderr); 356 fputs(_(" -T, --test Test for getopt(1) version\n"),stderr);
344 fputs(_(" -u, --unqote Do not quote the output\n"),stderr); 357 fputs(_(" -u, --unqote Do not quote the output\n"),stderr);
345 fputs(_(" -V, --version Output version information\n"),stderr); 358 fputs(_(" -V, --version Output version information\n"),stderr);
346 exit(2); 359 exit(PARAMETER_EXIT_CODE);
347} 360}
348 361
349/* Exit codes:
350 * 0) No errors, succesful operation.
351 * 1) getopt(3) returned an error.
352 * 2) A problem with parameter parsing for getopt(1).
353 * 3) Internal error, out of memory
354 * 4) Returned for -T
355 */
356
357static struct option longopts[]={ {"options",required_argument,NULL,'o'}, 362static struct option longopts[]={ {"options",required_argument,NULL,'o'},
358 {"longoptions",required_argument,NULL,'l'}, 363 {"longoptions",required_argument,NULL,'l'},
359 {"quiet",no_argument,NULL,'q'}, 364 {"quiet",no_argument,NULL,'q'},
360 {"quiet-output",no_argument,NULL,'Q'}, 365 {"quiet-output",no_argument,NULL,'Q'},
361 {"shell",required_argument,NULL,'s'}, 366 {"shell",required_argument,NULL,'s'},
396 /* 401 /*
397 * For some reason, the original getopt gave no error 402 * For some reason, the original getopt gave no error
398 * when there were no arguments. 403 * when there were no arguments.
399 */ 404 */
400 printf(" --\n"); 405 printf(" --\n");
401 exit(0); 406 return EXIT_SUCCESS;
402 } 407 }
403 else 408 else
404 parse_error(_("missing optstring argument")); 409 parse_error(_("missing optstring argument"));
405 } 410
406 411
407 if (argv[1][0] != '-' || compatible) { 412 if (argv[1][0] != '-' || compatible) {
408 quote=0; 413 quote=0;
409 optstr=our_malloc(strlen(argv[1])+1); 414 optstr=our_malloc(strlen(argv[1])+1);
410 strcpy(optstr,argv[1]+strspn(argv[1],"-+")); 415 strcpy(optstr,argv[1]+strspn(argv[1],"-+"));
411 argv[1]=argv[0]; 416 argv[1]=argv[0];
412 exit(generate_output(argv+1,argc-1,optstr,long_options)); 417 return generate_output(argv+1,argc-1,optstr,long_options);
413 } 418 }
414 419
415 while ((opt=getopt_long(argc,argv,shortopts,longopts,NULL)) != EOF) 420 while ((opt=getopt_long(argc,argv,shortopts,longopts,NULL)) != EOF)
416 switch (opt) { 421 switch (opt) {
417 case 'a': 422 case 'a':
418 alternative=1; 423 alternative=1;
419 break; 424 break;
420 case 'h': 425 case 'h':
421 print_help(); 426 print_help();
422 exit(0);
423 case 'o': 427 case 'o':
424 if (optstr) 428 if (optstr)
425 free(optstr); 429 free(optstr);
426 optstr=our_malloc(strlen(optarg)+1); 430 optstr=our_malloc(strlen(optarg)+1);
427 strcpy(optstr,optarg); 431 strcpy(optstr,optarg);
443 break; 447 break;
444 case 's': 448 case 's':
445 set_shell(optarg); 449 set_shell(optarg);
446 break; 450 break;
447 case 'T': 451 case 'T':
448 exit(4); 452 return TEST_EXIT_CODE;
449 case 'u': 453 case 'u':
450 quote=0; 454 quote=0;
451 break; 455 break;
452 case 'V': 456 case 'V':
453 printf(_("getopt (enhanced) 1.1.4\n")); 457 printf(_("getopt (enhanced) 1.1.4\n"));
454 exit(0); 458 return EXIT_SUCCESS;
455 case '?': 459 case '?':
456 case ':': 460 case ':':
457 parse_error(NULL); 461 parse_error(NULL);
458 default: 462 default:
459 parse_error(_("internal error, contact the author.")); 463 parse_error(_("internal error, contact the author."));
471 } 475 }
472 if (name) 476 if (name)
473 argv[optind-1]=name; 477 argv[optind-1]=name;
474 else 478 else
475 argv[optind-1]=argv[0]; 479 argv[optind-1]=argv[0];
476 exit(generate_output(argv+optind-1,argc-optind+1,optstr,long_options)); 480 return generate_output(argv+optind-1,argc-optind+1,optstr,long_options);
477} 481}

Legend:
Removed from v.317  
changed lines
  Added in v.318

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26