/****************************************************************
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 function will parse command line input into appropriate data
structures, output error messages when appropriate and provide some
Input to the function consists of the standard argc,argv
values, and a table which directs the parser. Each table entry has the
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
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 <math.h> /* For atof */
#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)
char *lower_string (/* char [], char * */);
static char *this_program
= "";
static int arg_parse (/* char *, arg_info * */);
boolean
parse_args (argc
, argv
, table
, entries
, others
, other_count
)
boolean
arg_verify (/* argv, table, entries */);
void init_store (/* table, entries */);
/* Check the validity of the table and its parameters */
result
= arg_verify (argv
, table
, entries
);
/* Initialize the storage values */
init_store (table
, entries
);
boolean use_prefix
= TRUE
;
index
= match_table (*argv
, table
, entries
, use_prefix
, &length
);
/* The argument doesn't match anything in the table */
*--*argv
= '-'; /* complain at invalid flag */
fprintf (stderr
, "%s: too many parameters: ",
fprintf (stderr
, "'%s' ignored\n", *argv
);
if (length
>= strlen (*argv
)) {
/* 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
]);
else if (length
>= strlen (*argv
)) {
} /* if (argv_count != P_NO_ARGS) */
*arg_result_ptr(table
[index
]) =
arg_table_size(table
[index
]);
boolean
arg_verify (argv
, table
, entries
)
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
,
/* Check the argument count */
{ int count
= arg_count (*arg
);
if (count
!= P_NO_ARGS
&& count
!= P_ONE_ARG
&& count
!=
fprintf (stderr
, "%s [arg_verify]: invalid ", this_program
);
fprintf (stderr
, "argument count in entry %d: '%d'\n", i
,
} /* if count != P_NO_ARGS ... */
/* Check the result field; want to be able to store results */
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
)
"%s [arg_verify]: bad arg type in entry %d: '%d'\n",
{ 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
,
} /* if (arg_count == P_INFINITE_ARGS && size < 1) */
/* 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
;
extern int match (/* char *, char *, arg_info *, boolean */);
char low_input
[MAX_INPUT_SIZE
];
int best_index
= -1, best_length
= 0;
(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_length
= this_length
;
} /* if (this_length > best_length) */
if (best_index
> -1 && length
!= (int *) NULL
)
/* match -- takes an input string and table entry, and returns the length
0 ==> input doesn't match
INPUT PREFIX STRING RESULT
----------------------------------------------------------------------
"-d" "-" "d" 2 (i.e. "-d")
"dout" "-" "d" 1 (i.e. "d")
"-d" "" "-d" 2 (i.e. "-d")
"dd" "d" "d" 2 <= here's the weird one
int match (norm_input
, low_input
, entry
, use_prefix
)
char *norm_input
, *low_input
;
char *norm_prefix
= arg_prefix (*entry
);
char *norm_string
= arg_string (*entry
);
boolean prefix_match
= FALSE
, string_match
= FALSE
;
/* Buffers for the lowercased versions of the strings being compared.
These are used when the switch is to be case insensitive */
static char low_prefix
[MAX_INPUT_SIZE
];
static char low_string
[MAX_INPUT_SIZE
];
int prefix_length
= strlen (norm_prefix
);
int string_length
= strlen (norm_string
);
/* Pointers for the required strings (lowered or nonlowered) */
register char *input
, *prefix
, *string
;
/* Use the appropriate strings to handle case sensitivity */
if (arg_flags (*entry
) & P_CASE_INSENSITIVE
) {
prefix
= lower_string (low_prefix
, norm_prefix
);
string
= lower_string (low_string
, norm_string
);
/* First, check the string formed by concatenating the prefix onto the
switch string, but only when the prefix is not being ignored */
if (use_prefix
&& prefix
!= NULL
&& *prefix
!= '\0')
prefix_match
= (strncmp (input
, prefix
, prefix_length
) == 0) &&
(strncmp (input
+ prefix_length
, string
, string_length
) == 0);
/* Next, check just the switch string, if that's allowed */
if (!use_prefix
&& (arg_flags (*entry
) & P_REQUIRED_PREFIX
) == 0)
string_match
= strncmp (input
, string
, string_length
) == 0;
result
= prefix_length
+ string_length
;
char *lower_string (dest
, src
)
if (dest
== NULL
|| src
== NULL
)
while (*dest
++ = (c
= *src
++) >= 'A' && c
<= 'Z' ? tolower(c
) : c
);
/* arg_parse -- returns the number of characters parsed for this entry */
static int arg_parse (str
, entry
)
if (arg_count (*entry
) == P_ONE_ARG
) {
char **store
= (char **) arg_result_ptr (*entry
);
length
= put_one_arg (arg_result_type (*entry
), str
, store
,
arg_prefix (*entry
), arg_string (*entry
));
} /* if (arg_count == P_ONE_ARG) */
else { /* Must be a table of arguments */
char **store
= (char **) arg_result_ptr (*entry
);
length
= put_one_arg (arg_result_type (*entry
), str
, store
++,
arg_prefix (*entry
), arg_string (*entry
));
int put_one_arg (type
, str
, store
, prefix
, string
)
fprintf (stderr
, "%s: Missing argument after '%s%s'\n",
this_program
, prefix
, string
);
length
= str
? strlen (str
) : 0;
*((char *) store
) = *str
;
*(short *)store
= (short) L
;
if (L
!= *(short *)store
)
"%s%s parameter '%ld' is not a SHORT INT (truncating to %d)\n",
prefix
, string
, L
, *(short *)store
);
"%s%s parameter '%ld' is not an INT (truncating to %d)\n",
prefix
, string
, L
, *(int *)store
);
*(long *)store
= atol(str
);
*((float *) store
) = (float) atof (str
);
*((double *) store
) = (double) atof (str
);
fprintf (stderr
, "put_one_arg: bad type '%d'\n",
void init_store (table
, entries
)
for (index
= 0; index
< entries
; index
++)
if (arg_count (table
[index
]) == P_INFINITE_ARGS
) {
char **place
= (char **) arg_result_ptr (table
[index
]);
} /* if arg_count == P_INFINITE_ARGS */