Loading...
Searching...
No Matches
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.

{
REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
} ordering;
ordering
static enum @256101167365300262336157210120037050216034370362 ordering
REQUIRE_ORDER
@ REQUIRE_ORDER
Definition fegetopt.c:163
RETURN_IN_ORDER
@ RETURN_IN_ORDER
Definition fegetopt.c:163
PERMUTE
@ PERMUTE
Definition fegetopt.c:163

## 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.

{
int option_index;
fe_optarg = 0;
/* Initialize the internal data when the first call is made.
Start processing options with ARGV-element 1 (since ARGV-element 0
is the program name); the sequence of previously skipped
non-option ARGV-elements is empty. */
if (fe_optind == 0)
{
first_nonopt = last_nonopt = fe_optind = 1;
nextchar = NULL;
/* Determine how to handle the ordering of options and nonoptions. */
if (optstring[0] == '-')
{
ordering = RETURN_IN_ORDER;
++optstring;
}
else if (optstring[0] == '+')
{
ordering = REQUIRE_ORDER;
++optstring;
}
else if (getenv ("POSIXLY_CORRECT") != NULL)
ordering = REQUIRE_ORDER;
else
ordering = PERMUTE;
}
if (nextchar == NULL || *nextchar == '\0')
{
if (ordering == PERMUTE)
{
/* If we have just processed some options following some non-options,
exchange them so that the options come first. */
if (first_nonopt != last_nonopt && last_nonopt != fe_optind)
exchange ((char **) argv);
else if (last_nonopt != fe_optind)
first_nonopt = fe_optind;
/* Now skip any additional non-options
and extend the range of non-options previously skipped. */
while (fe_optind < argc
&& (argv[fe_optind][0] != '-' || argv[fe_optind][1] == '\0')
#ifdef GETOPT_COMPAT
&& (longopts == NULL
|| argv[fe_optind][0] != '+' || argv[fe_optind][1] == '\0')
#endif /* GETOPT_COMPAT */
)
fe_optind++;
last_nonopt = fe_optind;
}
/* Special ARGV-element `--' means premature end of options.
Skip it like a null option,
then exchange with previous non-options as if it were an option,
then skip everything else like a non-option. */
if (fe_optind != argc && !strcmp (argv[fe_optind], "--"))
{
fe_optind++;
if (first_nonopt != last_nonopt && last_nonopt != fe_optind)
exchange ((char **) argv);
else if (first_nonopt == last_nonopt)
first_nonopt = fe_optind;
last_nonopt = argc;
fe_optind = argc;
}
/* If we have done all the ARGV-elements, stop the scan
and back over any non-options that we skipped and permuted. */
if (fe_optind == argc)
{
/* Set the next-arg-index to point at the non-options
that we previously skipped, so the caller will digest them. */
if (first_nonopt != last_nonopt)
fe_optind = first_nonopt;
return EOF;
}
/* If we have come to a non-option and did not permute it,
either stop the scan or describe it to the caller and pass it by. */
if ((argv[fe_optind][0] != '-' || argv[fe_optind][1] == '\0')
#ifdef GETOPT_COMPAT
&& (longopts == NULL
|| argv[fe_optind][0] != '+' || argv[fe_optind][1] == '\0')
#endif /* GETOPT_COMPAT */
)
{
if (ordering == REQUIRE_ORDER)
return EOF;
fe_optarg = argv[fe_optind++];
return 1;
}
/* We have found another option-ARGV-element.
Start decoding its characters. */
nextchar = (argv[fe_optind] + 1
+ (longopts != NULL && argv[fe_optind][1] == '-'));
}
if (longopts != NULL
&& ((argv[fe_optind][0] == '-'
&& (argv[fe_optind][1] == '-' || long_only))
#ifdef GETOPT_COMPAT
|| argv[fe_optind][0] == '+'
#endif /* GETOPT_COMPAT */
))
{
const struct fe_option *p;
char *s = nextchar;
int exact = 0;
int ambig = 0;
const struct fe_option *pfound = NULL;
int indfound = 0;
while (*s && *s != '=')
s++;
/* Test all options for either exact match or abbreviated matches. */
for (p = longopts, option_index = 0; p->name;
p++, option_index++)
if (!strncmp (p->name, nextchar, s - nextchar))
{
if (s - nextchar == (long)my_strlen (p->name))
{
/* Exact match found. */
pfound = p;
indfound = option_index;
exact = 1;
break;
}
else if (pfound == NULL)
{
/* First nonexact match found. */
pfound = p;
indfound = option_index;
}
else
/* Second nonexact match found. */
ambig = 1;
}
if (ambig && !exact)
{
if (fe_opterr)
fprintf (stderr, "%s: option `%s' is ambiguous\n",
argv[0], argv[fe_optind]);
nextchar += my_strlen (nextchar);
fe_optind++;
return BAD_OPTION;
}
if (pfound != NULL)
{
option_index = indfound;
fe_optind++;
if (*s)
{
/* Don't test has_arg with >, because some C compilers don't
allow it to be used on enums. */
if (pfound->has_arg)
fe_optarg = s + 1;
else
{
if (fe_opterr)
{
if (argv[fe_optind - 1][1] == '-')
/* --option */
fprintf (stderr,
"%s: option `--%s' doesn't allow an argument\n",
argv[0], pfound->name);
else
/* +option or -option */
fprintf (stderr,
"%s: option `%c%s' doesn't allow an argument\n",
argv[0], argv[fe_optind - 1][0], pfound->name);
}
nextchar += my_strlen (nextchar);
return BAD_OPTION;
}
}
else if (pfound->has_arg == 1)
{
if (fe_optind < argc)
fe_optarg = argv[fe_optind++];
else
{
if (fe_opterr)
fprintf (stderr, "%s: option `%s' requires an argument\n",
argv[0], argv[fe_optind - 1]);
nextchar += my_strlen (nextchar);
return optstring[0] == ':' ? ':' : BAD_OPTION;
}
}
nextchar += my_strlen (nextchar);
if (longind != NULL)
*longind = option_index;
return pfound->val;
}
/* Can't find it as a long option. If this is not getopt_long_only,
or the option starts with '--' or is not a valid short
option, then it's an error.
Otherwise interpret it as a short option. */
if (!long_only || argv[fe_optind][1] == '-'
#ifdef GETOPT_COMPAT
|| argv[fe_optind][0] == '+'
#endif /* GETOPT_COMPAT */
|| my_index (optstring, *nextchar) == NULL)
{
if (fe_opterr)
{
if (argv[fe_optind][1] == '-')
/* --option */
fprintf (stderr, "%s: unrecognized option `--%s'\n",
argv[0], nextchar);
else
/* +option or -option */
fprintf (stderr, "%s: unrecognized option `%c%s'\n",
argv[0], argv[fe_optind][0], nextchar);
}
nextchar = (char *) "";
fe_optind++;
return BAD_OPTION;
}
}
/* Look at and handle the next option-character. */
{
char c = *nextchar++;
const char *temp = my_index (optstring, c);
/* Increment `fe_optind' when we start to process its last character. */
if (*nextchar == '\0')
++fe_optind;
if (temp == NULL || c == ':')
{
if (fe_opterr)
{
#if 0
if (c < 040 || c >= 0177)
fprintf (stderr, "%s: unrecognized option, character code 0%o\n",
argv[0], c);
else
fprintf (stderr, "%s: unrecognized option `-%c'\n", argv[0], c);
#else
/* 1003.2 specifies the format of this message. */
fprintf (stderr, "%s: illegal option -- %c\n", argv[0], c);
#endif
}
fe_optopt = c;
return BAD_OPTION;
}
if (temp[1] == ':')
{
if (temp[2] == ':')
{
/* This is an option that accepts an argument optionally. */
if (*nextchar != '\0')
{
fe_optarg = nextchar;
fe_optind++;
}
else
fe_optarg = 0;
nextchar = NULL;
}
else
{
/* This is an option that requires an argument. */
if (*nextchar != '\0')
{
fe_optarg = nextchar;
/* If we end this ARGV-element by taking the rest as an arg,
we must advance to the next element now. */
fe_optind++;
}
else if (fe_optind == argc)
{
if (fe_opterr)
{
#if 0
fprintf (stderr, "%s: option `-%c' requires an argument\n",
argv[0], c);
#else
/* 1003.2 specifies the format of this message. */
fprintf (stderr, "%s: option requires an argument -- %c\n",
argv[0], c);
#endif
}
fe_optopt = c;
if (optstring[0] == ':')
c = ':';
else
c = BAD_OPTION;
}
else
/* We already incremented `fe_optind' once;
increment it again when taking next ARGV-elt as argument. */
fe_optarg = argv[fe_optind++];
nextchar = NULL;
}
}
return c;
}
}
p
int p
Definition cfModGcd.cc:4086
s
const CanonicalForm int s
Definition facAbsFact.cc:51
BAD_OPTION
#define BAD_OPTION
Definition fegetopt.c:129
exchange
static void exchange(char **argv)
Definition fegetopt.c:243
my_strlen
static size_t my_strlen(const char *str)
Definition fegetopt.c:190
nextchar
static char * nextchar
Definition fegetopt.c:118
fe_optarg
char * fe_optarg
Definition fegetopt.c:94
last_nonopt
static int last_nonopt
Definition fegetopt.c:218
my_index
static const char * my_index(const char *str, int chr)
Definition fegetopt.c:198
fe_opterr
int fe_opterr
Definition fegetopt.c:123
first_nonopt
static int first_nonopt
Definition fegetopt.c:217
fe_optind
int fe_optind
Definition fegetopt.c:109
fe_optopt
int fe_optopt
Definition fegetopt.c:130
getenv
char * getenv()
fe_option::val
int val
Definition fegetopt.h:88
fe_option::name
char * name
Definition fegetopt.h:83
fe_option::has_arg
int has_arg
Definition fegetopt.h:87
fe_option
Definition fegetopt.h:79
NULL
#define NULL
Definition omList.c:12

## exchange()

void exchange | ( | char ** | argv | ) |

static

Definition at line 243 of file fegetopt.c.

{
char *temp, **first, **last;
/* Reverse all the elements [first_nonopt, fe_optind) */
first = &argv[first_nonopt];
last = &argv[fe_optind-1];
while (first < last) {
temp = *first; *first = *last; *last = temp; first++; last--;
}
/* Put back the options in order */
first = &argv[first_nonopt];
first_nonopt += (fe_optind - last_nonopt);
last = &argv[first_nonopt - 1];
while (first < last) {
temp = *first; *first = *last; *last = temp; first++; last--;
}
/* Put back the non options in order */
first = &argv[first_nonopt];
last_nonopt = fe_optind;
last = &argv[last_nonopt-1];
while (first < last) {
temp = *first; *first = *last; *last = temp; first++; last--;
}
}
last
STATIC_VAR poly last
Definition hdegree.cc:1138

## fe_getopt()

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

Definition at line 653 of file fegetopt.c.

{
return _fe_getopt_internal (argc, argv, optstring,
(const struct fe_option *) 0,
(int *) 0,
0);
}
_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: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.

{
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 * | options,
|  | const struct fe_option * | long_options,
|  | int * | opt_index )

Definition at line 674 of file fegetopt.c.

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

## getenv()

char * getenv | ( |  | ) |

extern

## my_index()

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

static

Definition at line 198 of file fegetopt.c.

{
while (*str)
{
if (*str == chr)
return (const char *) str;
str++;
}
return 0;
}
LibThread::str
char * str(leftv arg)
Definition shared.cc:699

## my_strlen()

size_t my_strlen | ( | const char * | str | ) |

static

Definition at line 190 of file fegetopt.c.

{
size_t n = 0;
while (*str++)
n++;
return n;
}

## 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