My Project  debian-1:4.1.1-p2+ds-4
Data Structures | Macros | Enumerations | Functions | Variables
fegetopt.h File Reference

Go to the source code of this file.

Data Structures

struct  fe_option
 

Macros

#define no_argument   0
 
#define required_argument   1
 
#define optional_argument   2
 

Enumerations

enum  feOptType { feOptUntyped, feOptBool, feOptInt, feOptString }
 

Functions

int fe_getopt (int argc, char *const *argv, const char *shortopts)
 
int fe_getopt_long (int argc, char *const *argv, const char *shortopts, const struct fe_option *longopts, int *longind)
 
int fe_getopt_long_only (int argc, char *const *argv, const char *shortopts, const struct fe_option *longopts, int *longind)
 
int _fe_getopt_internal (int argc, char *const *argv, const char *shortopts, const struct fe_option *longopts, int *longind, int long_only)
 

Variables

char * fe_optarg
 
int fe_optind
 
int fe_opterr
 
int fe_optopt
 

Data Structure Documentation

◆ fe_option

struct fe_option

Definition at line 78 of file fegetopt.h.

Data Fields
const char * arg_name
int has_arg
const char * help
char * name
int set
feOptType type
int val
void * value

Macro Definition Documentation

◆ no_argument

#define no_argument   0

Definition at line 99 of file fegetopt.h.

◆ optional_argument

#define optional_argument   2

Definition at line 101 of file fegetopt.h.

◆ required_argument

#define required_argument   1

Definition at line 100 of file fegetopt.h.

Enumeration Type Documentation

◆ feOptType

enum feOptType
Enumerator
feOptUntyped 
feOptBool 
feOptInt 
feOptString 

Definition at line 77 of file fegetopt.h.

Function Documentation

◆ _fe_getopt_internal()

int _fe_getopt_internal ( int  argc,
char *const argv,
const char *  shortopts,
const struct fe_option longopts,
int *  longind,
int  long_only 
)

Definition at line 326 of file fegetopt.c.

334 {
335  int option_index;
336 
337  fe_optarg = 0;
338 
339  /* Initialize the internal data when the first call is made.
340  Start processing options with ARGV-element 1 (since ARGV-element 0
341  is the program name); the sequence of previously skipped
342  non-option ARGV-elements is empty. */
343 
344  if (fe_optind == 0)
345  {
347 
348  nextchar = NULL;
349 
350  /* Determine how to handle the ordering of options and nonoptions. */
351 
352  if (optstring[0] == '-')
353  {
355  ++optstring;
356  }
357  else if (optstring[0] == '+')
358  {
360  ++optstring;
361  }
362  else if (getenv ("POSIXLY_CORRECT") != NULL)
364  else
365  ordering = PERMUTE;
366  }
367 
368  if (nextchar == NULL || *nextchar == '\0')
369  {
370  if (ordering == PERMUTE)
371  {
372  /* If we have just processed some options following some non-options,
373  exchange them so that the options come first. */
374 
376  exchange ((char **) argv);
377  else if (last_nonopt != fe_optind)
379 
380  /* Now skip any additional non-options
381  and extend the range of non-options previously skipped. */
382 
383  while (fe_optind < argc
384  && (argv[fe_optind][0] != '-' || argv[fe_optind][1] == '\0')
385 #ifdef GETOPT_COMPAT
386  && (longopts == NULL
387  || argv[fe_optind][0] != '+' || argv[fe_optind][1] == '\0')
388 #endif /* GETOPT_COMPAT */
389  )
390  fe_optind++;
392  }
393 
394  /* Special ARGV-element `--' means premature end of options.
395  Skip it like a null option,
396  then exchange with previous non-options as if it were an option,
397  then skip everything else like a non-option. */
398 
399  if (fe_optind != argc && !strcmp (argv[fe_optind], "--"))
400  {
401  fe_optind++;
402 
404  exchange ((char **) argv);
405  else if (first_nonopt == last_nonopt)
407  last_nonopt = argc;
408 
409  fe_optind = argc;
410  }
411 
412  /* If we have done all the ARGV-elements, stop the scan
413  and back over any non-options that we skipped and permuted. */
414 
415  if (fe_optind == argc)
416  {
417  /* Set the next-arg-index to point at the non-options
418  that we previously skipped, so the caller will digest them. */
419  if (first_nonopt != last_nonopt)
421  return EOF;
422  }
423 
424  /* If we have come to a non-option and did not permute it,
425  either stop the scan or describe it to the caller and pass it by. */
426 
427  if ((argv[fe_optind][0] != '-' || argv[fe_optind][1] == '\0')
428 #ifdef GETOPT_COMPAT
429  && (longopts == NULL
430  || argv[fe_optind][0] != '+' || argv[fe_optind][1] == '\0')
431 #endif /* GETOPT_COMPAT */
432  )
433  {
434  if (ordering == REQUIRE_ORDER)
435  return EOF;
436  fe_optarg = argv[fe_optind++];
437  return 1;
438  }
439 
440  /* We have found another option-ARGV-element.
441  Start decoding its characters. */
442 
443  nextchar = (argv[fe_optind] + 1
444  + (longopts != NULL && argv[fe_optind][1] == '-'));
445  }
446 
447  if (longopts != NULL
448  && ((argv[fe_optind][0] == '-'
449  && (argv[fe_optind][1] == '-' || long_only))
450 #ifdef GETOPT_COMPAT
451  || argv[fe_optind][0] == '+'
452 #endif /* GETOPT_COMPAT */
453  ))
454  {
455  const struct fe_option *p;
456  char *s = nextchar;
457  int exact = 0;
458  int ambig = 0;
459  const struct fe_option *pfound = NULL;
460  int indfound = 0;
461 
462  while (*s && *s != '=')
463  s++;
464 
465  /* Test all options for either exact match or abbreviated matches. */
466  for (p = longopts, option_index = 0; p->name;
467  p++, option_index++)
468  if (!strncmp (p->name, nextchar, s - nextchar))
469  {
470  if (s - nextchar == my_strlen (p->name))
471  {
472  /* Exact match found. */
473  pfound = p;
474  indfound = option_index;
475  exact = 1;
476  break;
477  }
478  else if (pfound == NULL)
479  {
480  /* First nonexact match found. */
481  pfound = p;
482  indfound = option_index;
483  }
484  else
485  /* Second nonexact match found. */
486  ambig = 1;
487  }
488 
489  if (ambig && !exact)
490  {
491  if (fe_opterr)
492  fprintf (stderr, "%s: option `%s' is ambiguous\n",
493  argv[0], argv[fe_optind]);
495  fe_optind++;
496  return BAD_OPTION;
497  }
498 
499  if (pfound != NULL)
500  {
501  option_index = indfound;
502  fe_optind++;
503  if (*s)
504  {
505  /* Don't test has_arg with >, because some C compilers don't
506  allow it to be used on enums. */
507  if (pfound->has_arg)
508  fe_optarg = s + 1;
509  else
510  {
511  if (fe_opterr)
512  {
513  if (argv[fe_optind - 1][1] == '-')
514  /* --option */
515  fprintf (stderr,
516  "%s: option `--%s' doesn't allow an argument\n",
517  argv[0], pfound->name);
518  else
519  /* +option or -option */
520  fprintf (stderr,
521  "%s: option `%c%s' doesn't allow an argument\n",
522  argv[0], argv[fe_optind - 1][0], pfound->name);
523  }
525  return BAD_OPTION;
526  }
527  }
528  else if (pfound->has_arg == 1)
529  {
530  if (fe_optind < argc)
531  fe_optarg = argv[fe_optind++];
532  else
533  {
534  if (fe_opterr)
535  fprintf (stderr, "%s: option `%s' requires an argument\n",
536  argv[0], argv[fe_optind - 1]);
538  return optstring[0] == ':' ? ':' : BAD_OPTION;
539  }
540  }
542  if (longind != NULL)
543  *longind = option_index;
544  return pfound->val;
545  }
546  /* Can't find it as a long option. If this is not getopt_long_only,
547  or the option starts with '--' or is not a valid short
548  option, then it's an error.
549  Otherwise interpret it as a short option. */
550  if (!long_only || argv[fe_optind][1] == '-'
551 #ifdef GETOPT_COMPAT
552  || argv[fe_optind][0] == '+'
553 #endif /* GETOPT_COMPAT */
554  || my_index (optstring, *nextchar) == NULL)
555  {
556  if (fe_opterr)
557  {
558  if (argv[fe_optind][1] == '-')
559  /* --option */
560  fprintf (stderr, "%s: unrecognized option `--%s'\n",
561  argv[0], nextchar);
562  else
563  /* +option or -option */
564  fprintf (stderr, "%s: unrecognized option `%c%s'\n",
565  argv[0], argv[fe_optind][0], nextchar);
566  }
567  nextchar = (char *) "";
568  fe_optind++;
569  return BAD_OPTION;
570  }
571  }
572 
573  /* Look at and handle the next option-character. */
574 
575  {
576  char c = *nextchar++;
577  const char *temp = my_index (optstring, c);
578 
579  /* Increment `fe_optind' when we start to process its last character. */
580  if (*nextchar == '\0')
581  ++fe_optind;
582 
583  if (temp == NULL || c == ':')
584  {
585  if (fe_opterr)
586  {
587 #if 0
588  if (c < 040 || c >= 0177)
589  fprintf (stderr, "%s: unrecognized option, character code 0%o\n",
590  argv[0], c);
591  else
592  fprintf (stderr, "%s: unrecognized option `-%c'\n", argv[0], c);
593 #else
594  /* 1003.2 specifies the format of this message. */
595  fprintf (stderr, "%s: illegal option -- %c\n", argv[0], c);
596 #endif
597  }
598  fe_optopt = c;
599  return BAD_OPTION;
600  }
601  if (temp[1] == ':')
602  {
603  if (temp[2] == ':')
604  {
605  /* This is an option that accepts an argument optionally. */
606  if (*nextchar != '\0')
607  {
609  fe_optind++;
610  }
611  else
612  fe_optarg = 0;
613  nextchar = NULL;
614  }
615  else
616  {
617  /* This is an option that requires an argument. */
618  if (*nextchar != '\0')
619  {
621  /* If we end this ARGV-element by taking the rest as an arg,
622  we must advance to the next element now. */
623  fe_optind++;
624  }
625  else if (fe_optind == argc)
626  {
627  if (fe_opterr)
628  {
629 #if 0
630  fprintf (stderr, "%s: option `-%c' requires an argument\n",
631  argv[0], c);
632 #else
633  /* 1003.2 specifies the format of this message. */
634  fprintf (stderr, "%s: option requires an argument -- %c\n",
635  argv[0], c);
636 #endif
637  }
638  fe_optopt = c;
639  if (optstring[0] == ':')
640  c = ':';
641  else
642  c = BAD_OPTION;
643  }
644  else
645  /* We already incremented `fe_optind' once;
646  increment it again when taking next ARGV-elt as argument. */
647  fe_optarg = argv[fe_optind++];
648  nextchar = NULL;
649  }
650  }
651  return c;
652  }

◆ fe_getopt()

int fe_getopt ( int  argc,
char *const argv,
const char *  shortopts 
)

Definition at line 654 of file fegetopt.c.

659 {
660  return _fe_getopt_internal (argc, argv, optstring,
661  (const struct fe_option *) 0,
662  (int *) 0,
663  0);

◆ fe_getopt_long()

int fe_getopt_long ( int  argc,
char *const argv,
const char *  shortopts,
const struct fe_option longopts,
int *  longind 
)

Definition at line 665 of file fegetopt.c.

672 {
673  return _fe_getopt_internal (argc, argv, options, long_options, opt_index, 0);

◆ fe_getopt_long_only()

int fe_getopt_long_only ( int  argc,
char *const argv,
const char *  shortopts,
const struct fe_option longopts,
int *  longind 
)

Definition at line 675 of file fegetopt.c.

682 {
683  return _fe_getopt_internal (argc, argv, options, long_options, opt_index, 1);

Variable Documentation

◆ fe_optarg

char* fe_optarg

Definition at line 95 of file fegetopt.c.

◆ fe_opterr

int fe_opterr

Definition at line 124 of file fegetopt.c.

◆ fe_optind

int fe_optind

Definition at line 110 of file fegetopt.c.

◆ fe_optopt

int fe_optopt

Definition at line 131 of file fegetopt.c.

feOptBool
Definition: fegetopt.h:77
getenv
char * getenv()
BAD_OPTION
#define BAD_OPTION
Definition: fegetopt.c:130
fe_optind
int fe_optind
Definition: fegetopt.c:110
fe_option::val
int val
Definition: fegetopt.h:88
feOptType
feOptType
Definition: fegetopt.h:77
feOptUntyped
Definition: fegetopt.h:77
fe_opterr
int fe_opterr
Definition: fegetopt.c:124
fe_optarg
char * fe_optarg
Definition: fegetopt.c:95
fe_option::name
char * name
Definition: fegetopt.h:83
ordering
static enum @13 ordering
REQUIRE_ORDER
Definition: fegetopt.c:164
nextchar
static char * nextchar
Definition: fegetopt.c:119
my_strlen
static size_t my_strlen(const char *str)
Definition: fegetopt.c:191
fe_option
Definition: fegetopt.h:78
last_nonopt
static int last_nonopt
Definition: fegetopt.c:219
PERMUTE
Definition: fegetopt.c:164
_fe_getopt_internal
int _fe_getopt_internal(int argc, char *const *argv, const char *optstring, const struct fe_option *longopts, int *longind, int long_only)
Definition: fegetopt.c:326
RETURN_IN_ORDER
Definition: fegetopt.c:164
NULL
#define NULL
Definition: omList.c:9
exchange
static void exchange(char **argv)
Definition: fegetopt.c:244
my_index
static const char * my_index(const char *str, int chr)
Definition: fegetopt.c:199
feOptInt
Definition: fegetopt.h:77
p
int p
Definition: cfModGcd.cc:4019
s
const CanonicalForm int s
Definition: facAbsFact.cc:55
fe_optopt
int fe_optopt
Definition: fegetopt.c:131
first_nonopt
static int first_nonopt
Definition: fegetopt.c:218
fe_option::has_arg
int has_arg
Definition: fegetopt.h:87
feOptString
Definition: fegetopt.h:77