home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD2.bin
/
bbs
/
gnu
/
f2c-1993.04.28-src.lha
/
f2c-1993.04.28
/
src
/
parse_args.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-04-28
|
13KB
|
500 lines
/****************************************************************
Copyright 1990 by AT&T Bell Laboratories and Bellcore.
Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the names of AT&T Bell Laboratories or
Bellcore or any of their entities not be used in advertising or
publicity pertaining to distribution of the software without
specific, written prior permission.
AT&T and Bellcore disclaim all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall AT&T or Bellcore be liable for
any special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
****************************************************************/
/* parse_args
This function will parse command line input into appropriate data
structures, output error messages when appropriate and provide some
minimal type conversion.
Input to the function consists of the standard argc,argv
values, and a table which directs the parser. Each table entry has the
following components:
prefix -- the (optional) switch character string, e.g. "-" "/" "="
switch -- the command string, e.g. "o" "data" "file" "F"
flags -- control flags, e.g. CASE_INSENSITIVE, REQUIRED_PREFIX
arg_count -- number of arguments this command requires, e.g. 0 for
booleans, 1 for filenames, INFINITY for input files
result_type -- how to interpret the switch arguments, e.g. STRING,
CHAR, FILE, OLD_FILE, NEW_FILE
result_ptr -- pointer to storage for the result, be it a table or
a string or whatever
table_size -- if the arguments fill a table, the maximum number of
entries; if there are no arguments, the value to
load into the result storage
Although the table can be used to hold a list of filenames, only
scalar values (e.g. pointers) can be stored in the table. No vector
processing will be done, only pointers to string storage will be moved.
An example entry, which could be used to parse input filenames, is:
"-", "o", 0, oo, OLD_FILE, infilenames, INFILE_TABLE_SIZE
*/
#include <stdio.h>
#ifndef NULL
/* ANSI C */
#include <stddef.h>
#endif
#include "parse.h"
#include <math.h> /* For atof */
#include <ctype.h>
#define MAX_INPUT_SIZE 1000
#define arg_prefix(x) ((x).prefix)
#define arg_string(x) ((x).string)
#define arg_flags(x) ((x).flags)
#define arg_count(x) ((x).count)
#define arg_result_type(x) ((x).result_type)
#define arg_result_ptr(x) ((x).result_ptr)
#define arg_table_size(x) ((x).table_size)
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
typedef int boolean;
char *lower_string (/* char [], char * */);
static char *this_program = "";
extern long atol();
static int arg_parse (/* char *, arg_info * */);
boolean parse_args (argc, argv, table, entries, others, other_count)
int argc;
char *argv[];
arg_info table[];
int entries;
char *others[];
int other_count;
{
boolean arg_verify (/* argv, table, entries */);
void init_store (/* table, entries */);
boolean result;
if (argv)
this_program = argv[0];
/* Check the validity of the table and its parameters */
result = arg_verify (argv, table, entries);
/* Initialize the storage values */
init_store (table, entries);
if (result) {
boolean use_prefix = TRUE;
char *argv0;
argc--;
argv0 = *++argv;
while (argc) {
int index, length;
index = match_table (*argv, table, entries, use_prefix, &length);
if (index < 0) {
/* The argument doesn't match anything in the table */
if (others) {
if (*argv > argv0)
*--*argv = '-'; /* complain at invalid flag */
if (other_count > 0) {
*others++ = *argv;
other_count--;
} else {
fprintf (stderr, "%s: too many parameters: ",
this_program);
fprintf (stderr, "'%s' ignored\n", *argv);
} /* else */
} /* if (others) */
argv0 = *++argv;
argc--;
} else {
/* A match was found */
if (length >= strlen (*argv)) {
argc--;
argv0 = *++argv;
use_prefix = TRUE;
} else {
(*argv) += length;
use_prefix = FALSE;
} /* else */
/* Parse any necessary arguments */
if (arg_count (table[index]) != P_NO_ARGS) {
/* Now length will be used to store the number of parsed characters */
length = arg_parse(*argv, &table[index]);
if (*argv == NULL)
argc = 0;
else if (length >= strlen (*argv)) {
argc--;
argv0 = *++argv;
use_prefix = TRUE;
} else {
(*argv) += length;
use_prefix = FALSE;
} /* else */
} /* if (argv_count != P_NO_ARGS) */
else
*arg_result_ptr(table[index]) =
arg_table_size(table[index]);
} /* else */
} /* while (argc) */
} /* if (result) */
return result;
} /* parse_args */
boolean arg_verify (argv, table, entries)
char *argv[];
arg_info table[];
int entries;
{
int i;
char *this_program = "";
if (argv)
this_program = argv[0];
for (i = 0; i < entries; i++) {
arg_info *arg = &table[i];
/* Check the argument flags */
if (arg_flags (*arg) & ~(P_CASE_INSENSITIVE | P_REQUIRED_PREFIX)) {
fprintf (stderr, "%s [arg_verify]: too many ", this_program);
fprintf (stderr, "flags in entry %d: '%x' (hex)\n", i,
arg_flags (*arg));
} /* if */
/* Check the argument count */
{ int count = arg_count (*arg);
if (count != P_NO_ARGS && count != P_ONE_ARG && count !=
P_INFINITE_ARGS) {
fprintf (stderr, "%s [arg_verify]: invalid ", this_program);
fprintf (stderr, "argument count in entry %d: '%d'\n", i,
count);
} /* if count != P_NO_ARGS ... */
/* Check the result field; want to be able to store results */
else
if (arg_result_ptr (*arg) == (int *) NULL) {
fprintf (stderr, "%s [arg_verify]: ", this_program);
fprintf (stderr, "no argument storage given for ");
fprintf (stderr, "entry %d\n", i);
} /* if arg_result_ptr */
}
/* Check the argument type */
{ int type = arg_result_type (*arg);
if (type < P_STRING || type > P_DOUBLE)
fprintf(stderr,
"%s [arg_verify]: bad arg type in entry %d: '%d'\n",
this_program, i, type);
}
/* Check table size */
{ int size = arg_table_size (*arg);
if (arg_count (*arg) == P_INFINITE_ARGS && size < 1) {
fprintf (stderr, "%s [arg_verify]: bad ", this_program);
fprintf (stderr, "table size in entry %d: '%d'\n", i,
size);
} /* if (arg_count == P_INFINITE_ARGS && size < 1) */
}
} /* for i = 0 */
return TRUE;
} /* arg_verify */
/* match_table -- returns the index of the best entry matching the input,
-1 if no match. The best match is the one of longest length which
appears lowest in the table. The length of the match will be returned
in length ONLY IF a match was found. */
int match_table (norm_input, table, entries, use_prefix, length)
register char *norm_input;
arg_info table[];
int entries;
boolean use_prefix;
int *length;
{
extern int match (/* char *, char *, arg_info *, boolean */);
char low_input[MAX_INPUT_SIZE];
register int i;
int best_index = -1, best_length = 0;
/* FUNCTION BODY */
(void) lower_string (low_input, norm_input);
for (i = 0; i < entries; i++) {
int this_length = match (norm_input, low_input, &table[i], use_prefix);
if (this_length > best_length) {
best_index = i;
best_length = this_length;
} /* if (this_length > best_length) */
} /* for (i = 0) */
if (best_index > -1 && length != (int *) NULL)
*length = best_length;
return best_index;
} /* match_table */
/* match -- takes an input string and table entry, and returns the length
of the longer match.
0 ==> input doesn't match
For example:
INPUT PREFI