My Project  debian-1:4.1.1-p2+ds-4build3
Macros | Enumerations | Functions | Variables
fegetopt.c File Reference
#include "kernel/mod2.h"
#include <stdio.h>
#include "Singular/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 48 of file fegetopt.c.

◆ BAD_OPTION

#define BAD_OPTION   '\0'

Definition at line 132 of file fegetopt.c.

◆ const

#define const

Definition at line 42 of file fegetopt.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
REQUIRE_ORDER 
PERMUTE 
RETURN_IN_ORDER 

Definition at line 163 of file fegetopt.c.

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 327 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  }
653 }

◆ exchange()

static void exchange ( char **  argv)
static

Definition at line 245 of file fegetopt.c.

246 {
247  char *temp, **first, **last;
248 
249  /* Reverse all the elements [first_nonopt, fe_optind) */
250  first = &argv[first_nonopt];
251  last = &argv[fe_optind-1];
252  while (first < last) {
253  temp = *first; *first = *last; *last = temp; first++; last--;
254  }
255  /* Put back the options in order */
256  first = &argv[first_nonopt];
258  last = &argv[first_nonopt - 1];
259  while (first < last) {
260  temp = *first; *first = *last; *last = temp; first++; last--;
261  }
262 
263  /* Put back the non options in order */
264  first = &argv[first_nonopt];
266  last = &argv[last_nonopt-1];
267  while (first < last) {
268  temp = *first; *first = *last; *last = temp; first++; last--;
269  }
270 }

◆ fe_getopt()

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

Definition at line 655 of file fegetopt.c.

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

◆ 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 666 of file fegetopt.c.

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

◆ 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 676 of file fegetopt.c.

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

◆ getenv()

char* getenv ( )

◆ my_index()

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

Definition at line 200 of file fegetopt.c.

201 {
202  while (*str)
203  {
204  if (*str == chr)
205  return (const char *) str;
206  str++;
207  }
208  return 0;
209 }

◆ my_strlen()

static size_t my_strlen ( const char *  str)
static

Definition at line 192 of file fegetopt.c.

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

Variable Documentation

◆ fe_optarg

char* fe_optarg = 0

Definition at line 96 of file fegetopt.c.

◆ fe_opterr

int fe_opterr = 1

Definition at line 125 of file fegetopt.c.

◆ fe_optind

int fe_optind = 0

Definition at line 111 of file fegetopt.c.

◆ fe_optopt

int fe_optopt = BAD_OPTION

Definition at line 132 of file fegetopt.c.

◆ first_nonopt

int first_nonopt
static

Definition at line 219 of file fegetopt.c.

◆ last_nonopt

int last_nonopt
static

Definition at line 220 of file fegetopt.c.

◆ nextchar

char* nextchar
static

Definition at line 120 of file fegetopt.c.

◆ ordering

enum { ... } ordering
getenv
char * getenv()
ordering
static enum @13 ordering
BAD_OPTION
#define BAD_OPTION
Definition: fegetopt.c:131
fe_optind
int fe_optind
Definition: fegetopt.c:111
fe_option::val
int val
Definition: fegetopt.h:88
fe_opterr
int fe_opterr
Definition: fegetopt.c:125
fe_optarg
char * fe_optarg
Definition: fegetopt.c:96
fe_option::name
char * name
Definition: fegetopt.h:83
REQUIRE_ORDER
@ REQUIRE_ORDER
Definition: fegetopt.c:165
nextchar
static char * nextchar
Definition: fegetopt.c:120
my_strlen
static size_t my_strlen(const char *str)
Definition: fegetopt.c:192
fe_option
Definition: fegetopt.h:79
last_nonopt
static int last_nonopt
Definition: fegetopt.c:220
last
static poly last
Definition: hdegree.cc:1077
PERMUTE
@ PERMUTE
Definition: fegetopt.c:165
_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:327
RETURN_IN_ORDER
@ RETURN_IN_ORDER
Definition: fegetopt.c:165
NULL
#define NULL
Definition: omList.c:10
exchange
static void exchange(char **argv)
Definition: fegetopt.c:245
my_index
static const char * my_index(const char *str, int chr)
Definition: fegetopt.c:200
p
int p
Definition: cfModGcd.cc:4019
s
const CanonicalForm int s
Definition: facAbsFact.cc:55
fe_optopt
int fe_optopt
Definition: fegetopt.c:132
first_nonopt
static int first_nonopt
Definition: fegetopt.c:219
fe_option::has_arg
int has_arg
Definition: fegetopt.h:87