Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

parse_args.c File Reference

#include "stdlib.h"
#include <stdio.h>
#include <stddef.h>
#include "string.h"
#include "parse.h"
#include <math.h>
#include <ctype.h>

Go to the source code of this file.


Defines

#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)
#define TRUE   1
#define FALSE   0

Typedefs

typedef int boolean

Functions

int arg_parse Argdcl ((char *, arg_info *))
char *lower_string Argdcl ((char *, char *))
int match Argdcl ((char *, char *, arg_info *, boolean))
int put_one_arg Argdcl ((int, char *, char **, char *, char *))
boolean parse_args (int argc, char **argv, arg_info *table, int entries, char **others, int other_count)
boolean arg_verify (char **argv, arg_info *table, int entries)
int match_table (register char *norm_input, arg_info *table, int entries, boolean use_prefix, int *length)
int match (char *norm_input, char *low_input, arg_info *entry, boolean use_prefix)
char * lower_string (char *dest, char *src)
int arg_parse (char *str, arg_info *entry)
int put_one_arg (int type, char *str, char **store, char *prefix, char *string)
void init_store (arg_info *table, int entries)

Variables

char * this_program = ""
int badargs

Define Documentation

#define arg_count      ((x).count)
 

Definition at line 78 of file parse_args.c.

Referenced by arg_parse(), arg_verify(), init_store(), and parse_args().

#define arg_flags      ((x).flags)
 

Definition at line 77 of file parse_args.c.

Referenced by arg_verify(), and match().

#define arg_prefix      ((x).prefix)
 

Definition at line 75 of file parse_args.c.

Referenced by arg_parse(), and match().

#define arg_result_ptr      ((x).result_ptr)
 

Definition at line 80 of file parse_args.c.

Referenced by arg_parse(), arg_verify(), init_store(), and parse_args().

#define arg_result_type      ((x).result_type)
 

Definition at line 79 of file parse_args.c.

Referenced by arg_parse(), and arg_verify().

#define arg_string      ((x).string)
 

Definition at line 76 of file parse_args.c.

Referenced by arg_parse(), and match().

#define arg_table_size      ((x).table_size)
 

Definition at line 81 of file parse_args.c.

Referenced by arg_verify(), and parse_args().

#define FALSE   0
 

Definition at line 87 of file parse_args.c.

Referenced by match(), and parse_args().

#define MAX_INPUT_SIZE   1000
 

Definition at line 73 of file parse_args.c.

Referenced by match(), and match_table().

#define TRUE   1
 

Definition at line 84 of file parse_args.c.

Referenced by arg_verify(), and parse_args().


Typedef Documentation

typedef int boolean
 

Definition at line 89 of file parse_args.c.


Function Documentation

int arg_parse char *    str,
arg_info   entry
[static]
 

Definition at line 430 of file parse_args.c.

References arg_count, arg_prefix, arg_result_ptr, arg_result_type, arg_string, P_ONE_ARG, and put_one_arg().

Referenced by parse_args().

00432 {
00433     int length = 0;
00434 
00435     if (arg_count (*entry) == P_ONE_ARG) {
00436         char **store = (char **) arg_result_ptr (*entry);
00437 
00438         length = put_one_arg (arg_result_type (*entry), str, store,
00439                 arg_prefix (*entry), arg_string (*entry));
00440 
00441     } /* if (arg_count == P_ONE_ARG) */
00442       else { /* Must be a table of arguments */
00443         char **store = (char **) arg_result_ptr (*entry);
00444 
00445         if (store) {
00446             while (*store)
00447                 store++;
00448 
00449             length = put_one_arg(arg_result_type (*entry), str, store++,
00450                     arg_prefix (*entry), arg_string (*entry));
00451 
00452             *store = (char *) NULL;
00453         } /* if (store) */
00454     } /* else */
00455 
00456     return length;
00457 } /* arg_parse */

boolean arg_verify char **    argv,
arg_info   table,
int    entries
 

Definition at line 207 of file parse_args.c.

References arg, arg_count, arg_flags, arg_result_ptr, arg_result_type, arg_table_size, badargs, i, P_CASE_INSENSITIVE, P_DOUBLE, P_INFINITE_ARGS, P_NO_ARGS, P_ONE_ARG, P_REQUIRED_PREFIX, this_program, and TRUE.

Referenced by parse_args().

00209 {
00210     int i;
00211     char *this_program = "";
00212 
00213     if (argv)
00214         this_program = argv[0];
00215 
00216     for (i = 0; i < entries; i++) {
00217         arg_info *arg = &table[i];
00218 
00219 /* Check the argument flags */
00220 
00221         if (arg_flags (*arg) & ~(P_CASE_INSENSITIVE | P_REQUIRED_PREFIX)) {
00222             fprintf (stderr, "%s [arg_verify]:  too many ", this_program);
00223             fprintf (stderr, "flags in entry %d:  '%x' (hex)\n", i,
00224                     arg_flags (*arg));
00225             badargs++;
00226         } /* if */
00227 
00228 /* Check the argument count */
00229 
00230         { int count = arg_count (*arg);
00231 
00232             if (count != P_NO_ARGS && count != P_ONE_ARG && count !=
00233                     P_INFINITE_ARGS) {
00234                 fprintf (stderr, "%s [arg_verify]:  invalid ", this_program);
00235                 fprintf (stderr, "argument count in entry %d:  '%d'\n", i,
00236                         count);
00237                 badargs++;
00238             } /* if count != P_NO_ARGS ... */
00239 
00240 /* Check the result field; want to be able to store results */
00241 
00242               else
00243                 if (arg_result_ptr (*arg) == (int *) NULL) {
00244                     fprintf (stderr, "%s [arg_verify]:  ", this_program);
00245                     fprintf (stderr, "no argument storage given for ");
00246                     fprintf (stderr, "entry %d\n", i);
00247                     badargs++;
00248                 } /* if arg_result_ptr */
00249         }
00250 
00251 /* Check the argument type */
00252 
00253         { int type = arg_result_type (*arg);
00254 
00255             if (type < P_STRING || type > P_DOUBLE) {
00256                     fprintf(stderr,
00257                         "%s [arg_verify]:  bad arg type in entry %d:  '%d'\n",
00258                         this_program, i, type);
00259                     badargs++;
00260                     }
00261         }
00262 
00263 /* Check table size */
00264 
00265         { int size = arg_table_size (*arg);
00266 
00267             if (arg_count (*arg) == P_INFINITE_ARGS && size < 1) {
00268                 fprintf (stderr, "%s [arg_verify]:  bad ", this_program);
00269                 fprintf (stderr, "table size in entry %d:  '%d'\n", i,
00270                         size);
00271                 badargs++;
00272             } /* if (arg_count == P_INFINITE_ARGS && size < 1) */
00273         }
00274 
00275     } /* for i = 0 */
00276 
00277     return TRUE;
00278 } /* arg_verify */

int put_one_arg Argdcl (int, char *, char **, char *, char *)    [static]
 

int match Argdcl (char *, char *, arg_info *, boolean   [static]
 

char* lower_string Argdcl (char *, char *)    [static]
 

int arg_parse Argdcl (char *, arg_info *)    [static]
 

void init_store arg_info   table,
int    entries
 

Definition at line 545 of file parse_args.c.

References arg_count, arg_result_ptr, and P_INFINITE_ARGS.

Referenced by parse_args().

00547 {
00548     int index;
00549 
00550     for (index = 0; index < entries; index++)
00551         if (arg_count (table[index]) == P_INFINITE_ARGS) {
00552             char **place = (char **) arg_result_ptr (table[index]);
00553 
00554             if (place)
00555                 *place = (char *) NULL;
00556         } /* if arg_count == P_INFINITE_ARGS */
00557 
00558 } /* init_store */

char* lower_string char *    dest,
char *    src
[static]
 

Definition at line 407 of file parse_args.c.

References c.

Referenced by match(), and match_table().

00409 {
00410     char *result = dest;
00411     register int c;
00412 
00413     if (dest == NULL || src == NULL)
00414         result = NULL;
00415     else
00416         while (*dest++ = (c = *src++) >= 'A' && c <= 'Z' ? tolower(c) : c);
00417 
00418     return result;
00419 } /* lower_string */

int match char *    norm_input,
char *    low_input,
arg_info   entry,
boolean    use_prefix
[static]
 

Definition at line 346 of file parse_args.c.

References arg_flags, arg_prefix, arg_string, FALSE, lower_string(), MAX_INPUT_SIZE, P_CASE_INSENSITIVE, and P_REQUIRED_PREFIX.

00348 {
00349     char *norm_prefix = arg_prefix (*entry);
00350     char *norm_string = arg_string (*entry);
00351     boolean prefix_match = FALSE, string_match = FALSE;
00352     int result = 0;
00353 
00354 /* Buffers for the lowercased versions of the strings being compared.
00355    These are used when the switch is to be case insensitive */
00356 
00357     static char low_prefix[MAX_INPUT_SIZE];
00358     static char low_string[MAX_INPUT_SIZE];
00359     int prefix_length = strlen (norm_prefix);
00360     int string_length = strlen (norm_string);
00361 
00362 /* Pointers for the required strings (lowered or nonlowered) */
00363 
00364     register char *input, *prefix, *string;
00365 
00366 /* FUNCTION BODY */
00367 
00368 /* Use the appropriate strings to handle case sensitivity */
00369 
00370     if (arg_flags (*entry) & P_CASE_INSENSITIVE) {
00371         input = low_input;
00372         prefix = lower_string (low_prefix, norm_prefix);
00373         string = lower_string (low_string, norm_string);
00374     } else {
00375         input = norm_input;
00376         prefix = norm_prefix;
00377         string = norm_string;
00378     } /* else */
00379 
00380 /* First, check the string formed by concatenating the prefix onto the
00381    switch string, but only when the prefix is not being ignored */
00382 
00383     if (use_prefix && prefix != NULL && *prefix != '\0')
00384          prefix_match = (strncmp (input, prefix, prefix_length) == 0) &&
00385                 (strncmp (input + prefix_length, string, string_length) == 0);
00386 
00387 /* Next, check just the switch string, if that's allowed */
00388 
00389     if (!use_prefix && (arg_flags (*entry) & P_REQUIRED_PREFIX) == 0)
00390         string_match = strncmp (input, string, string_length) == 0;
00391 
00392     if (prefix_match)
00393         result = prefix_length + string_length;
00394     else if (string_match)
00395         result = string_length;
00396 
00397     return result;
00398 } /* match */

int match_table register char *    norm_input,
arg_info   table,
int    entries,
boolean    use_prefix,
int *    length
 

Definition at line 295 of file parse_args.c.

References i, lower_string(), match(), and MAX_INPUT_SIZE.

Referenced by parse_args().

00297 {
00298     char low_input[MAX_INPUT_SIZE];
00299     register int i;
00300     int best_index = -1, best_length = 0;
00301 
00302 /* FUNCTION BODY */
00303 
00304     (void) lower_string (low_input, norm_input);
00305 
00306     for (i = 0; i < entries; i++) {
00307         int this_length = match(norm_input, low_input, &table[i], use_prefix);
00308 
00309         if (this_length > best_length) {
00310             best_index = i;
00311             best_length = this_length;
00312         } /* if (this_length > best_length) */
00313     } /* for (i = 0) */
00314 
00315     if (best_index > -1 && length != (int *) NULL)
00316         *length = best_length;
00317 
00318     return best_index;
00319 } /* match_table */

boolean parse_args int    argc,
char **    argv,
arg_info   table,
int    entries,
char **    others,
int    other_count
 

Definition at line 111 of file parse_args.c.

References arg_count, arg_parse(), arg_result_ptr, arg_table_size, arg_verify(), argc, badargs, FALSE, init_store(), match_table(), P_NO_ARGS, this_program, and TRUE.

Referenced by main().

00113 {
00114     boolean result;
00115 
00116     if (argv)
00117         this_program = argv[0];
00118 
00119 /* Check the validity of the table and its parameters */
00120 
00121     result = arg_verify (argv, table, entries);
00122 
00123 /* Initialize the storage values */
00124 
00125     init_store (table, entries);
00126 
00127     if (result) {
00128         boolean use_prefix = TRUE;
00129         char *argv0;
00130 
00131         argc--;
00132         argv0 = *++argv;
00133         while (argc) {
00134             int index, length;
00135 
00136             index = match_table (*argv, table, entries, use_prefix, &length);
00137             if (index < 0) {
00138 
00139 /* The argument doesn't match anything in the table */
00140 
00141                 if (others) {
00142 
00143                     if (*argv > argv0)
00144                         *--*argv = '-'; /* complain at invalid flag */
00145 
00146                     if (other_count > 0) {
00147                         *others++ = *argv;
00148                         other_count--;
00149                     } else {
00150                         fprintf (stderr, "%s:  too many parameters: ",
00151                                 this_program);
00152                         fprintf (stderr, "'%s' ignored\n", *argv);
00153                         badargs++;
00154                     } /* else */
00155                 } /* if (others) */
00156                 argv0 = *++argv;
00157                 argc--;
00158             } else {
00159 
00160 /* A match was found */
00161 
00162                 if (length >= strlen (*argv)) {
00163                     argc--;
00164                     argv0 = *++argv;
00165                     use_prefix = TRUE;
00166                 } else {
00167                     (*argv) += length;
00168                     use_prefix = FALSE;
00169                 } /* else */
00170 
00171 /* Parse any necessary arguments */
00172 
00173                 if (arg_count (table[index]) != P_NO_ARGS) {
00174 
00175 /* Now   length   will be used to store the number of parsed characters */
00176 
00177                     length = arg_parse(*argv, &table[index]);
00178                     if (*argv == NULL)
00179                         argc = 0;
00180                     else if (length >= strlen (*argv)) {
00181                         argc--;
00182                         argv0 = *++argv;
00183                         use_prefix = TRUE;
00184                     } else {
00185                         (*argv) += length;
00186                         use_prefix = FALSE;
00187                     } /* else */
00188                 } /* if (argv_count != P_NO_ARGS) */
00189                   else
00190                     *arg_result_ptr(table[index]) =
00191                             arg_table_size(table[index]);
00192             } /* else */
00193         } /* while (argc) */
00194     } /* if (result) */
00195 
00196     return result;
00197 } /* parse_args */

int put_one_arg int    type,
char *    str,
char **    store,
char *    prefix,
char *    string
[static]
 

Definition at line 469 of file parse_args.c.

References badargs, L, P_CHAR, P_DOUBLE, P_FILE, P_FLOAT, P_INT, P_LONG, P_NEW_FILE, P_OLD_FILE, P_SHORT, P_STRING, and this_program.

Referenced by arg_parse().

00471 {
00472     int length = 0;
00473     long L;
00474 
00475     if (store) {
00476         switch (type) {
00477             case P_STRING:
00478             case P_FILE:
00479             case P_OLD_FILE:
00480             case P_NEW_FILE:
00481                 if (str == NULL) {
00482                         fprintf(stderr, "%s: Missing argument after '%s%s'\n",
00483                                 this_program, prefix, string);
00484                         length = 0;
00485                         badargs++;
00486                         }
00487                 else
00488                         length = strlen(*store = str);
00489                 break;
00490             case P_CHAR:
00491                 *((char *) store) = *str;
00492                 length = 1;
00493                 break;
00494             case P_SHORT:
00495                 L = atol(str);
00496                 *(short *)store = (short) L;
00497                 if (L != *(short *)store) {
00498                     fprintf(stderr,
00499         "%s%s parameter '%ld' is not a SHORT INT (truncating to %d)\n",
00500                             prefix, string, L, *(short *)store);
00501                     badargs++;
00502                     }
00503                 length = strlen (str);
00504                 break;
00505             case P_INT:
00506                 L = atol(str);
00507                 *(int *)store = (int)L;
00508                 if (L != *(int *)store) {
00509                     fprintf(stderr,
00510         "%s%s parameter '%ld' is not an INT (truncating to %d)\n",
00511                             prefix, string, L, *(int *)store);
00512                     badargs++;
00513                     }
00514                 length = strlen (str);
00515                 break;
00516             case P_LONG:
00517                 *(long *)store = atol(str);
00518                 length = strlen (str);
00519                 break;
00520             case P_FLOAT:
00521                 *((float *) store) = (float) atof(str);
00522                 length = strlen (str);
00523                 break;
00524             case P_DOUBLE:
00525                 *((double *) store) = (double) atof(str);
00526                 length = strlen (str);
00527                 break;
00528             default:
00529                 fprintf (stderr, "put_one_arg:  bad type '%d'\n", type);
00530                 badargs++;
00531                 break;
00532         } /* switch */
00533     } /* if (store) */
00534 
00535     return length;
00536 } /* put_one_arg */

Variable Documentation

int badargs
 

Definition at line 98 of file parse_args.c.

Referenced by arg_verify(), parse_args(), and put_one_arg().

char* this_program = "" [static]
 

Definition at line 92 of file parse_args.c.

Referenced by arg_verify(), parse_args(), and put_one_arg().

 

Powered by Plone

This site conforms to the following standards: