My Project
Macros | Enumerations | Functions | Variables
fegetopt.c File Reference
#include <stdio.h>
#include "fegetopt.h"

Go to the source code of this file.

Macros

#define const
 
#define _NO_PROTO
 
#define BAD_OPTION   '\0'
 

Enumerations

enum  { REQUIRE_ORDER , PERMUTE , RETURN_IN_ORDER }
 

Functions

char * getenv ()
 
static size_t my_strlen (const char *str)
 
static const char * my_index (const char *str, int chr)
 
static void exchange (char **argv)
 
int _fe_getopt_internal (int argc, char *const *argv, const char *optstring, const struct fe_option *longopts, int *longind, int long_only)
 
int fe_getopt (int argc, char *const *argv, const char *optstring)
 
int fe_getopt_long (int argc, char *const *argv, const char *options, const struct fe_option *long_options, int *opt_index)
 
int fe_getopt_long_only (int argc, char *const *argv, const char *options, const struct fe_option *long_options, int *opt_index)
 

Variables

char * fe_optarg = 0
 
int fe_optind = 0
 
static char * nextchar
 
int fe_opterr = 1
 
int fe_optopt = BAD_OPTION
 
static enum { ... }  ordering
 
static int first_nonopt
 
static int last_nonopt
 

Macro Definition Documentation

◆ _NO_PROTO

#define _NO_PROTO

Definition at line 45 of file fegetopt.c.

◆ BAD_OPTION

#define BAD_OPTION   '\0'

Definition at line 129 of file fegetopt.c.

◆ const

#define const

Definition at line 39 of file fegetopt.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
REQUIRE_ORDER 
PERMUTE 
RETURN_IN_ORDER 

Definition at line 161 of file fegetopt.c.

162 {
164 } ordering;
@ REQUIRE_ORDER
Definition: fegetopt.c:163
@ RETURN_IN_ORDER
Definition: fegetopt.c:163
@ PERMUTE
Definition: fegetopt.c:163
static enum @0 ordering

Function Documentation

◆ _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 at line 325 of file fegetopt.c.

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

◆ exchange()

static void exchange ( char **  argv)
static

Definition at line 243 of file fegetopt.c.

244 {
245  char *temp, **first, **last;
246 
247  /* Reverse all the elements [first_nonopt, fe_optind) */
248  first = &argv[first_nonopt];
249  last = &argv[fe_optind-1];
250  while (first < last) {
251  temp = *first; *first = *last; *last = temp; first++; last--;
252  }
253  /* Put back the options in order */
254  first = &argv[first_nonopt];
256  last = &argv[first_nonopt - 1];
257  while (first < last) {
258  temp = *first; *first = *last; *last = temp; first++; last--;
259  }
260 
261  /* Put back the non options in order */
262  first = &argv[first_nonopt];
264  last = &argv[last_nonopt-1];
265  while (first < last) {
266  temp = *first; *first = *last; *last = temp; first++; last--;
267  }
268 }
STATIC_VAR poly last
Definition: hdegree.cc:1151

◆ fe_getopt()

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

Definition at line 653 of file fegetopt.c.

657 {
658  return _fe_getopt_internal (argc, argv, optstring,
659  (const struct fe_option *) 0,
660  (int *) 0,
661  0);
662 }
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:325

◆ fe_getopt_long()

int fe_getopt_long ( int  argc,
char *const argv,
const char *  options,
const struct fe_option long_options,
int *  opt_index 
)

Definition at line 664 of file fegetopt.c.

670 {
671  return _fe_getopt_internal (argc, argv, options, long_options, opt_index, 0);
672 }

◆ fe_getopt_long_only()

int fe_getopt_long_only ( int  argc,
char *const argv,
const char *  options,
const struct fe_option long_options,
int *  opt_index 
)

Definition at line 674 of file fegetopt.c.

680 {
681  return _fe_getopt_internal (argc, argv, options, long_options, opt_index, 1);
682 }

◆ getenv()

char* getenv ( )

◆ my_index()

static const char* my_index ( const char *  str,
int  chr 
)
static

Definition at line 198 of file fegetopt.c.

199 {
200  while (*str)
201  {
202  if (*str == chr)
203  return (const char *) str;
204  str++;
205  }
206  return 0;
207 }
char * str(leftv arg)
Definition: shared.cc:704

◆ my_strlen()

static size_t my_strlen ( const char *  str)
static

Definition at line 190 of file fegetopt.c.

191 {
192  size_t n = 0;
193  while (*str++)
194  n++;
195  return n;
196 }

Variable Documentation

◆ fe_optarg

char* fe_optarg = 0

Definition at line 94 of file fegetopt.c.

◆ fe_opterr

int fe_opterr = 1

Definition at line 123 of file fegetopt.c.

◆ fe_optind

int fe_optind = 0

Definition at line 109 of file fegetopt.c.

◆ fe_optopt

int fe_optopt = BAD_OPTION

Definition at line 130 of file fegetopt.c.

◆ first_nonopt

int first_nonopt
static

Definition at line 217 of file fegetopt.c.

◆ last_nonopt

int last_nonopt
static

Definition at line 218 of file fegetopt.c.

◆ nextchar

char* nextchar
static

Definition at line 118 of file fegetopt.c.

◆ 

enum { ... } ordering