home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / f2csrc.zip / f2csrc / src / parse_args.c < prev    next >
C/C++ Source or Header  |  1994-05-10  |  14KB  |  543 lines

  1. /****************************************************************
  2. Copyright 1990, 1994 by AT&T Bell Laboratories and Bellcore.
  3.  
  4. Permission to use, copy, modify, and distribute this software
  5. and its documentation for any purpose and without fee is hereby
  6. granted, provided that the above copyright notice appear in all
  7. copies and that both that the copyright notice and this
  8. permission notice and warranty disclaimer appear in supporting
  9. documentation, and that the names of AT&T Bell Laboratories or
  10. Bellcore or any of their entities not be used in advertising or
  11. publicity pertaining to distribution of the software without
  12. specific, written prior permission.
  13.  
  14. AT&T and Bellcore disclaim all warranties with regard to this
  15. software, including all implied warranties of merchantability
  16. and fitness.  In no event shall AT&T or Bellcore be liable for
  17. any special, indirect or consequential damages or any damages
  18. whatsoever resulting from loss of use, data or profits, whether
  19. in an action of contract, negligence or other tortious action,
  20. arising out of or in connection with the use or performance of
  21. this software.
  22. ****************************************************************/
  23.  
  24. /* parse_args
  25.  
  26.     This function will parse command line input into appropriate data
  27.    structures, output error messages when appropriate and provide some
  28.    minimal type conversion.
  29.  
  30.     Input to the function consists of the standard   argc,argv
  31.    values, and a table which directs the parser.  Each table entry has the
  32.    following components:
  33.  
  34.     prefix -- the (optional) switch character string, e.g. "-" "/" "="
  35.     switch -- the command string, e.g. "o" "data" "file" "F"
  36.     flags -- control flags, e.g.   CASE_INSENSITIVE, REQUIRED_PREFIX
  37.     arg_count -- number of arguments this command requires, e.g. 0 for
  38.              booleans, 1 for filenames, INFINITY for input files
  39.     result_type -- how to interpret the switch arguments, e.g. STRING,
  40.                CHAR, FILE, OLD_FILE, NEW_FILE
  41.     result_ptr -- pointer to storage for the result, be it a table or
  42.               a string or whatever
  43.     table_size -- if the arguments fill a table, the maximum number of
  44.               entries; if there are no arguments, the value to
  45.               load into the result storage
  46.  
  47.     Although the table can be used to hold a list of filenames, only
  48.    scalar values (e.g. pointers) can be stored in the table.  No vector
  49.    processing will be done, only pointers to string storage will be moved.
  50.  
  51.     An example entry, which could be used to parse input filenames, is:
  52.  
  53.     "-", "o", 0, oo, OLD_FILE, infilenames, INFILE_TABLE_SIZE
  54.  
  55. */
  56.  
  57. #include <stdio.h>
  58. #ifndef NULL
  59. /* ANSI C */
  60. #include <stddef.h>
  61. #endif
  62. #ifdef KR_headers
  63. extern double atof();
  64. #else
  65. #include "stdlib.h"
  66. #include "string.h"
  67. #endif
  68. #include "parse.h"
  69. #include <math.h>         /* For atof */
  70. #include <ctype.h>
  71.  
  72. #define MAX_INPUT_SIZE 1000
  73.  
  74. #define arg_prefix(x) ((x).prefix)
  75. #define arg_string(x) ((x).string)
  76. #define arg_flags(x) ((x).flags)
  77. #define arg_count(x) ((x).count)
  78. #define arg_result_type(x) ((x).result_type)
  79. #define arg_result_ptr(x) ((x).result_ptr)
  80. #define arg_table_size(x) ((x).table_size)
  81.  
  82. #ifndef TRUE
  83. #define TRUE 1
  84. #endif
  85. #ifndef FALSE
  86. #define FALSE 0
  87. #endif
  88. typedef int boolean;
  89.  
  90.  
  91. static char *this_program = "";
  92.  
  93. static int arg_parse Argdcl((char*, arg_info*));
  94. static char *lower_string Argdcl((char*, char*));
  95. static int match Argdcl((char*, char*, arg_info*, boolean));
  96. static int put_one_arg Argdcl((int, char*, char**, char*, char*));
  97.  
  98.  
  99.  boolean
  100. #ifdef KR_headers
  101. parse_args(argc, argv, table, entries, others, other_count)
  102.     int argc;
  103.     char **argv;
  104.     arg_info *table;
  105.     int entries;
  106.     char **others;
  107.     int other_count;
  108. #else
  109. parse_args(int argc, char **argv, arg_info *table, int entries, char **others, int other_count)
  110. #endif
  111. {
  112.     boolean result;
  113.  
  114.     if (argv)
  115.     this_program = argv[0];
  116.  
  117. /* Check the validity of the table and its parameters */
  118.  
  119.     result = arg_verify (argv, table, entries);
  120.  
  121. /* Initialize the storage values */
  122.  
  123.     init_store (table, entries);
  124.  
  125.     if (result) {
  126.     boolean use_prefix = TRUE;
  127.     char *argv0;
  128.  
  129.     argc--;
  130.     argv0 = *++argv;
  131.     while (argc) {
  132.         int index, length;
  133.  
  134.         index = match_table (*argv, table, entries, use_prefix, &length);
  135.         if (index < 0) {
  136.  
  137. /* The argument doesn't match anything in the table */
  138.  
  139.         if (others) {
  140.  
  141.             if (*argv > argv0)
  142.             *--*argv = '-';    /* complain at invalid flag */
  143.  
  144.             if (other_count > 0) {
  145.             *others++ = *argv;
  146.             other_count--;
  147.             } else {
  148.             fprintf (stderr, "%s:  too many parameters: ",
  149.                 this_program);
  150.             fprintf (stderr, "'%s' ignored\n", *argv);
  151.             } /* else */
  152.         } /* if (others) */
  153.         argv0 = *++argv;
  154.         argc--;
  155.         } else {
  156.  
  157. /* A match was found */
  158.  
  159.         if (length >= strlen (*argv)) {
  160.             argc--;
  161.             argv0 = *++argv;
  162.             use_prefix = TRUE;
  163.         } else {
  164.             (*argv) += length;
  165.             use_prefix = FALSE;
  166.         } /* else */
  167.  
  168. /* Parse any necessary arguments */
  169.  
  170.         if (arg_count (table[index]) != P_NO_ARGS) {
  171.  
  172. /* Now   length   will be used to store the number of parsed characters */
  173.  
  174.             length = arg_parse(*argv, &table[index]);
  175.             if (*argv == NULL)
  176.             argc = 0;
  177.             else if (length >= strlen (*argv)) {
  178.             argc--;
  179.             argv0 = *++argv;
  180.             use_prefix = TRUE;
  181.             } else {
  182.             (*argv) += length;
  183.             use_prefix = FALSE;
  184.             } /* else */
  185.         } /* if (argv_count != P_NO_ARGS) */
  186.           else
  187.             *arg_result_ptr(table[index]) =
  188.                 arg_table_size(table[index]);
  189.         } /* else */
  190.     } /* while (argc) */
  191.     } /* if (result) */
  192.  
  193.     return result;
  194. } /* parse_args */
  195.  
  196.  
  197.  boolean
  198. #ifdef KR_headers
  199. arg_verify(argv, table, entries)
  200.     char **argv;
  201.     arg_info *table;
  202.     int entries;
  203. #else
  204. arg_verify(char **argv, arg_info *table, int entries)
  205. #endif
  206. {
  207.     int i;
  208.     char *this_program = "";
  209.  
  210.     if (argv)
  211.     this_program = argv[0];
  212.  
  213.     for (i = 0; i < entries; i++) {
  214.     arg_info *arg = &table[i];
  215.  
  216. /* Check the argument flags */
  217.  
  218.     if (arg_flags (*arg) & ~(P_CASE_INSENSITIVE | P_REQUIRED_PREFIX)) {
  219.         fprintf (stderr, "%s [arg_verify]:  too many ", this_program);
  220.         fprintf (stderr, "flags in entry %d:  '%x' (hex)\n", i,
  221.             arg_flags (*arg));
  222.     } /* if */
  223.  
  224. /* Check the argument count */
  225.  
  226.     { int count = arg_count (*arg);
  227.  
  228.         if (count != P_NO_ARGS && count != P_ONE_ARG && count !=
  229.             P_INFINITE_ARGS) {
  230.         fprintf (stderr, "%s [arg_verify]:  invalid ", this_program);
  231.         fprintf (stderr, "argument count in entry %d:  '%d'\n", i,
  232.             count);
  233.         } /* if count != P_NO_ARGS ... */
  234.  
  235. /* Check the result field; want to be able to store results */
  236.  
  237.           else
  238.         if (arg_result_ptr (*arg) == (int *) NULL) {
  239.             fprintf (stderr, "%s [arg_verify]:  ", this_program);
  240.             fprintf (stderr, "no argument storage given for ");
  241.             fprintf (stderr, "entry %d\n", i);
  242.         } /* if arg_result_ptr */
  243.     }
  244.  
  245. /* Check the argument type */
  246.  
  247.     { int type = arg_result_type (*arg);
  248.  
  249.         if (type < P_STRING || type > P_DOUBLE)
  250.             fprintf(stderr,
  251.             "%s [arg_verify]:  bad arg type in entry %d:  '%d'\n",
  252.             this_program, i, type);
  253.     }
  254.  
  255. /* Check table size */
  256.  
  257.     { int size = arg_table_size (*arg);
  258.  
  259.         if (arg_count (*arg) == P_INFINITE_ARGS && size < 1) {
  260.         fprintf (stderr, "%s [arg_verify]:  bad ", this_program);
  261.         fprintf (stderr, "table size in entry %d:  '%d'\n", i,
  262.             size);
  263.         } /* if (arg_count == P_INFINITE_ARGS && size < 1) */
  264.     }
  265.  
  266.     } /* for i = 0 */
  267.  
  268.     return TRUE;
  269. } /* arg_verify */
  270.  
  271.  
  272. /* match_table -- returns the index of the best entry matching the input,
  273.    -1 if no match.  The best match is the one of longest length which
  274.    appears lowest in the table.  The length of the match will be returned
  275.    in   length   ONLY IF a match was found.   */
  276.  
  277.  int
  278. #ifdef KR_headers
  279. match_table(norm_input, table, entries, use_prefix, length)
  280.     register char *norm_input;
  281.     arg_info *table;
  282.     int entries;
  283.     boolean use_prefix;
  284.     int *length;
  285. #else
  286. match_table(register char *norm_input, arg_info *table, int entries, boolean use_prefix, int *length)
  287. #endif
  288. {
  289.     char low_input[MAX_INPUT_SIZE];
  290.     register int i;
  291.     int best_index = -1, best_length = 0;
  292.  
  293. /* FUNCTION BODY */
  294.  
  295.     (void) lower_string (low_input, norm_input);
  296.  
  297.     for (i = 0; i < entries; i++) {
  298.     int this_length = match(norm_input, low_input, &table[i], use_prefix);
  299.  
  300.     if (this_length > best_length) {
  301.         best_index = i;
  302.         best_length = this_length;
  303.     } /* if (this_length > best_length) */
  304.     } /* for (i = 0) */
  305.  
  306.     if (best_index > -1 && length != (int *) NULL)
  307.     *length = best_length;
  308.  
  309.     return best_index;
  310. } /* match_table */
  311.  
  312.  
  313. /* match -- takes an input string and table entry, and returns the length
  314.    of the longer match.
  315.  
  316.     0 ==> input doesn't match
  317.  
  318.    For example:
  319.  
  320.     INPUT    PREFIX    STRING    RESULT
  321. ----------------------------------------------------------------------
  322.     "abcd"    "-"    "d"    0
  323.     "-d"    "-"    "d"    2    (i.e. "-d")
  324.     "dout"    "-"    "d"    1    (i.e. "d")
  325.     "-d"    ""    "-d"    2    (i.e. "-d")
  326.     "dd"    "d"    "d"    2    <= here's the weird one
  327. */
  328.  
  329.  static int
  330. #ifdef KR_headers
  331. match(norm_input, low_input, entry, use_prefix)
  332.     char *norm_input;
  333.     char *low_input;
  334.     arg_info *entry;
  335.     boolean use_prefix;
  336. #else
  337. match(char *norm_input, char *low_input, arg_info *entry, boolean use_prefix)
  338. #endif
  339. {
  340.     char *norm_prefix = arg_prefix (*entry);
  341.     char *norm_string = arg_string (*entry);
  342.     boolean prefix_match = FALSE, string_match = FALSE;
  343.     int result = 0;
  344.  
  345. /* Buffers for the lowercased versions of the strings being compared.
  346.    These are used when the switch is to be case insensitive */
  347.  
  348.     static char low_prefix[MAX_INPUT_SIZE];
  349.     static char low_string[MAX_INPUT_SIZE];
  350.     int prefix_length = strlen (norm_prefix);
  351.     int string_length = strlen (norm_string);
  352.  
  353. /* Pointers for the required strings (lowered or nonlowered) */
  354.  
  355.     register char *input, *prefix, *string;
  356.  
  357. /* FUNCTION BODY */
  358.  
  359. /* Use the appropriate strings to handle case sensitivity */
  360.  
  361.     if (arg_flags (*entry) & P_CASE_INSENSITIVE) {
  362.     input = low_input;
  363.     prefix = lower_string (low_prefix, norm_prefix);
  364.     string = lower_string (low_string, norm_string);
  365.     } else {
  366.     input = norm_input;
  367.     prefix = norm_prefix;
  368.     string = norm_string;
  369.     } /* else */
  370.  
  371. /* First, check the string formed by concatenating the prefix onto the
  372.    switch string, but only when the prefix is not being ignored */
  373.  
  374.     if (use_prefix && prefix != NULL && *prefix != '\0')
  375.      prefix_match = (strncmp (input, prefix, prefix_length) == 0) &&
  376.         (strncmp (input + prefix_length, string, string_length) == 0);
  377.  
  378. /* Next, check just the switch string, if that's allowed */
  379.  
  380.     if (!use_prefix && (arg_flags (*entry) & P_REQUIRED_PREFIX) == 0)
  381.     string_match = strncmp (input, string, string_length) == 0;
  382.  
  383.     if (prefix_match)
  384.     result = prefix_length + string_length;
  385.     else if (string_match)
  386.     result = string_length;
  387.  
  388.     return result;
  389. } /* match */
  390.  
  391.  
  392.  static char *
  393. #ifdef KR_headers
  394. lower_string(dest, src)
  395.     char *dest;
  396.     char *src;
  397. #else
  398. lower_string(char *dest, char *src)
  399. #endif
  400. {
  401.     char *result = dest;
  402.     register int c;
  403.  
  404.     if (dest == NULL || src == NULL)
  405.     result = NULL;
  406.     else
  407.     while (*dest++ = (c = *src++) >= 'A' && c <= 'Z' ? tolower(c) : c);
  408.  
  409.     return result;
  410. } /* lower_string */
  411.  
  412.  
  413. /* arg_parse -- returns the number of characters parsed for this entry */
  414.  
  415.  static int
  416. #ifdef KR_headers
  417. arg_parse(str, entry)
  418.     char *str;
  419.     arg_info *entry;
  420. #else
  421. arg_parse(char *str, arg_info *entry)
  422. #endif
  423. {
  424.     int length = 0;
  425.  
  426.     if (arg_count (*entry) == P_ONE_ARG) {
  427.     char **store = (char **) arg_result_ptr (*entry);
  428.  
  429.     length = put_one_arg (arg_result_type (*entry), str, store,
  430.         arg_prefix (*entry), arg_string (*entry));
  431.  
  432.     } /* if (arg_count == P_ONE_ARG) */
  433.       else { /* Must be a table of arguments */
  434.     char **store = (char **) arg_result_ptr (*entry);
  435.  
  436.     if (store) {
  437.         while (*store)
  438.         store++;
  439.  
  440.         length = put_one_arg(arg_result_type (*entry), str, store++,
  441.             arg_prefix (*entry), arg_string (*entry));
  442.  
  443.         *store = (char *) NULL;
  444.     } /* if (store) */
  445.     } /* else */
  446.  
  447.     return length;
  448. } /* arg_parse */
  449.  
  450.  
  451.  static int
  452. #ifdef KR_headers
  453. put_one_arg(type, str, store, prefix, string)
  454.     int type;
  455.     char *str;
  456.     char **store;
  457.     char *prefix;
  458.     char *string;
  459. #else
  460. put_one_arg(int type, char *str, char **store, char *prefix, char *string)
  461. #endif
  462. {
  463.     int length = 0;
  464.     long L;
  465.  
  466.     if (store) {
  467.     switch (type) {
  468.         case P_STRING:
  469.         case P_FILE:
  470.         case P_OLD_FILE:
  471.         case P_NEW_FILE:
  472.         *store = str;
  473.         if (str == NULL)
  474.             fprintf (stderr, "%s: Missing argument after '%s%s'\n",
  475.                 this_program, prefix, string);
  476.         length = str ? strlen (str) : 0;
  477.         break;
  478.         case P_CHAR:
  479.         *((char *) store) = *str;
  480.         length = 1;
  481.         break;
  482.         case P_SHORT:
  483.         L = atol(str);
  484.         *(short *)store = (short) L;
  485.         if (L != *(short *)store)
  486.             fprintf(stderr,
  487.     "%s%s parameter '%ld' is not a SHORT INT (truncating to %d)\n",
  488.                 prefix, string, L, *(short *)store);
  489.         length = strlen (str);
  490.         break;
  491.         case P_INT:
  492.         L = atol(str);
  493.         *(int *)store = (int)L;
  494.         if (L != *(int *)store)
  495.             fprintf(stderr,
  496.     "%s%s parameter '%ld' is not an INT (truncating to %d)\n",
  497.                 prefix, string, L, *(int *)store);
  498.         length = strlen (str);
  499.         break;
  500.         case P_LONG:
  501.         *(long *)store = atol(str);
  502.         length = strlen (str);
  503.         break;
  504.         case P_FLOAT:
  505.         *((float *) store) = (float) atof(str);
  506.         length = strlen (str);
  507.         break;
  508.         case P_DOUBLE:
  509.         *((double *) store) = (double) atof(str);
  510.         length = strlen (str);
  511.         break;
  512.         default:
  513.         fprintf (stderr, "put_one_arg:  bad type '%d'\n",
  514.             type);
  515.         break;
  516.     } /* switch */
  517.     } /* if (store) */
  518.  
  519.     return length;
  520. } /* put_one_arg */
  521.  
  522.  
  523.  void
  524. #ifdef KR_headers
  525. init_store(table, entries)
  526.     arg_info *table;
  527.     int entries;
  528. #else
  529. init_store(arg_info *table, int entries)
  530. #endif
  531. {
  532.     int index;
  533.  
  534.     for (index = 0; index < entries; index++)
  535.     if (arg_count (table[index]) == P_INFINITE_ARGS) {
  536.         char **place = (char **) arg_result_ptr (table[index]);
  537.  
  538.         if (place)
  539.         *place = (char *) NULL;
  540.     } /* if arg_count == P_INFINITE_ARGS */
  541.  
  542. } /* init_store */
  543.