home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / IRIT / POLY3DS.ZIP / GETARG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-05  |  24.8 KB  |  620 lines

  1. /***************************************************************************
  2. *  Routines to grab the    parameters from    the command line :           *
  3. * All the routines except the main one,    starts with GA (Get Arguments) to  *
  4. * prevent from names conflicts.                           *
  5. * It is    assumed    in these routine that any pointer, for any type    has the       *
  6. * same length (i.e. length of int pointer is equal to char pointer etc.)   *
  7. *                                       *
  8. *  The following routines are available    in this    module:               *
  9. * 1. int GAGetArgs(argc, argv, CtrlStr, Variables...)               *
  10. *    where argc, argv as received on entry.                   *
  11. *       CtrlStr is the contrl string    (see below)               *
  12. *       Variables are all the variables to be set according to CtrlStr. *
  13. *       Note    that all the variables MUST be transfered by address.       *
  14. *    return 0 on correct parsing, otherwise error number (see GetArg.h).   *
  15. * 2. GAPrintHowTo(CtrlStr)                           *
  16. *    Print the control string to stderr, in the    correct    format needed.       *
  17. *    This feature is very useful in case of error during GetArgs parsing.  *
  18. *    Chars equal to SPACE_CHAR are not printed (regular spaces are NOT     *
  19. *    allowed, and so using SPACE_CHAR you can create space in PrintHowTo). *
  20. * 3. GAPrintErrMsg(Error)                           *
  21. *    Print the error to    stderr,    according to Error (usually returned by       *
  22. *    GAGetArgs).                               *
  23. *                                       *
  24. *     CtrlStr format:                               *
  25. *   The    control    string passed to GetArgs controls the way argv (argc) are  *
  26. * parsed. Each entry in    this string must not have any spaces in    it. The       *
  27. * First    Entry is the name of the program which is usually ignored except   *
  28. * when GAPrintHowTo is called. All the other entries (except the last one  *
  29. * which    we will    come back to it    later) must have the following format:       *
  30. * 1. One letter    which sets the option letter.                   *
  31. * 2. '!' or '%'    to determines if this option is    really optional    ('%') or   *
  32. *    it    must exists ('!')...                           *
  33. * 3. '-' allways.                               *
  34. * 4. Alpha numeric string, usually ignored, but    used by    GAPrintHowTo to       *
  35. *    print the meaning of this input.                       *
  36. * 5. Sequences starts with '!' or '%'. Again if    '!' then this sequence       *
  37. *    must exists (only if its option flag is given of course), and if '%'  *
  38. *    it    is optional. Each sequence will    continue with one or two       *
  39. *    characters    which defines the kind of the input:               *
  40. *    a.    d, x, o, u - integer is expected (decimal, hex, octal base or       *
  41. *          unsigned).                           *
  42. *    b.    D, X, O, U - long integer is expected (same as above).           *
  43. *    c.    f    - float    number is expected.                   *
  44. *    d.    F    - double number    is expected.                   *
  45. *    e.    s    - string is expected.                       *
  46. *    f.    *?    - any number of    '?' kind (d, x, o, u, D, X, O, U, f, F, s) *
  47. *          will match this one. If '?' is numeric, it scans until   *
  48. *          none numeric input is given. If '?' is 's' then it scans *
  49. *          up to the next option or end of argv.               *
  50. *                                       *
  51. *   If the last    parameter given    in the CtrlStr,    is not an option (i.e. the *
  52. * second char is not in    ['!', '%'] and the third one is not '-'), all what *
  53. * remained from    argv is    linked to it.                       *
  54. *                                       *
  55. *   The    variables passed to GAGetArgs (starting    from 4th parameter) MUST   *
  56. * match    the order of the CtrlStr:                       *
  57. *   For    each option, one integer address must be passed. This integer must *
  58. * initialized by 0. If that option is given in the command line, it will   *
  59. * be set to one.                               *
  60. *   In addition, the sequences that might follow an option require the       *
  61. * following parameters to pass:                           *
  62. * 1. d, x, o, u - pointer to integer (int *).                   *
  63. * 2. D, X, O, U - pointer to long (long *).                   *
  64. * 3. f         - pointer to float      (float *).                   *
  65. * 4. F         - pointer to double  (double *).                   *
  66. * 5. s         - pointer to char      (char    *). NO allocation is needed!       *
  67. * 6. *?         - TWO variables are passed    for each wild request. the first   *
  68. *           one is (address of) integer, and    it will    return number of   *
  69. *           parameters actually matched this    sequence, and the second   *
  70. *           one is a    pointer    to pointer to ?    (? **),    and will return    an *
  71. *           address to a block of pointers to ? kind, terminated with   *
  72. *           NULL pointer. NO    pre-allocation is needed.           *
  73. *           note that these two variables are pretty    like the argv/argc *
  74. *           pair...                               *
  75. *                                       *
  76. *   Examples:                                   *
  77. *                                       *
  78. *    "Example1  i%-OneInteger!d  s%-Strings!*s  j%-  k!-Float!f  Files"       *
  79. * Will match: Example1 -i 77 -s    String1    String2    String3    -k 88.2    File1 File2*
  80. *   or match: Example1 -s String1 -k 88.3 -i 999 -j               *
  81. *    but not: Example1 -i 77 78    (option    i expects one integer, k must be). *
  82. * Note the option k must exists, and that the order of the options is not  *
  83. * not important. In the    first examples File1 & File2 will match    the Files  *
  84. * in the command line.                               *
  85. * A call to GAPrintHowTo with this CtrlStr will    print to stderr:       *
  86. * Example1 [-i OneIngeter] [-s Strings...] [-j]    -k Float Files...       *
  87. *                                       *
  88. *   Notes:                                   *
  89. *                                       *
  90. * 1. This module assumes that all the pointers to all kind of data types   *
  91. *    have the same length and format, i.e. sizeof(int *) == sizeof(char    *).*
  92. *                                       *
  93. *                      Gershon Elber    Ver 0.2     Mar 88       *
  94. ****************************************************************************
  95. * History:                                   *
  96. * 11 Mar 88 - Version 1.0 by Gershon Elber.                   *
  97. ***************************************************************************/
  98.  
  99. #ifdef __MSDOS__
  100. #include <stdlib.h>
  101. #include <alloc.h>
  102. #endif /* __MSDOS__ */
  103.  
  104. #ifdef USE_VARARGS
  105. #include <varargs.h>
  106. #endif /* USE_VARARGS */
  107.  
  108. #include <stdio.h>
  109. #include <string.h>
  110. #include "getarg.h"
  111.  
  112. #define    MYMALLOC       /* If no "MyAlloc" routine elsewhere define this. */
  113.  
  114. #define    MAX_PARAM    100        /* maximum number of parameters allowed. */
  115. #define    CTRL_STR_MAX_LEN    1024
  116.  
  117. #define SPACE_CHAR    '|'      /* The character not to print using HowTo. */
  118.  
  119. #ifndef    TRUE
  120. #define    TRUE -1
  121. #define    FALSE 0
  122. #define    OK    0
  123. #endif /* TRUE */
  124.  
  125. #define    ISSPACE(x) ((x)    <= ' ')           /* Not conventional - but works fine! */
  126. /* The two characters '%' and '!' are used in the control string: */
  127. #define    ISCTRLCHAR(x) (((x) == '%') || ((x) == '!'))
  128.  
  129. static char *GAErrorToken;/* On error code, ErrorToken is set to point on it.*/
  130.  
  131. static int GATestAllSatis(char *CtrlStrCopy, char *CtrlStr, int *argc,
  132.     char ***argv, int *Parameters[MAX_PARAM], int *ParamCount);
  133. static int GAUpdateParameters(int *Parameters[], int *ParamCount,
  134.     char *Option, char *CtrlStrCopy, char *CtrlStr, int *argc,
  135.     char ***argv);
  136. static int GAGetParmeters(int *Parameters[], int *ParamCount,
  137.     char *CtrlStrCopy , char *Option, int *argc, char ***argv);
  138. static int GAGetMultiParmeters(int *Parameters[], int *ParamCount,
  139.     char *CtrlStrCopy, int *argc, char ***argv);
  140. static void GASetParamCount(char *CtrlStr, int Max, int *ParamCount);
  141. static void GAByteCopy(char *Dst, char *Src, unsigned n);
  142. static int GAOptionExists(int argc, char **argv);
  143. #ifdef    MYMALLOC
  144. static char *MyMalloc(unsigned size);
  145. #endif    /* MYMALLOC */
  146.  
  147. /***************************************************************************
  148. * Routine to access the    command    line argument and interpret them:       *
  149. * Return OK (0)    is case    of succesfull parsing, error code else...       *
  150. ***************************************************************************/
  151. #ifdef USE_VARARGS
  152. int GAGetArgs(int va_alist, ...)
  153. {
  154.     va_list ap;
  155.     int argc, i, Error = FALSE, ParamCount = 0,
  156.     *Parameters[MAX_PARAM];           /* Save here parameter addresses. */
  157.     char **argv, *CtrlStr, *Option, CtrlStrCopy[CTRL_STR_MAX_LEN];
  158.  
  159.     va_start(ap);
  160.  
  161.     argc = va_arg(ap, int);
  162.     argv = va_arg(ap, char **);
  163.     CtrlStr = va_arg(ap, char *);
  164.     
  165.     va_end(ap);
  166.  
  167.     strcpy(CtrlStrCopy, CtrlStr);
  168.  
  169.     /* Using base address of parameters we access other parameters addr:  */
  170.     /* Note that me (for sure!) samples data beyond the current function  */
  171.     /* frame, but we accesson these set address only by demand.          */
  172.     for (i=1; i<=MAX_PARAM; i++) Parameters[i-1] = va_arg(ap, int *);
  173. #else
  174. int GAGetArgs(int argc, char **argv, char *CtrlStr, ...)
  175. {
  176.     int i, Error = FALSE, ParamCount = 0,
  177.     *Parameters[MAX_PARAM];           /* Save here parameter addresses. */
  178.     char *Option, CtrlStrCopy[CTRL_STR_MAX_LEN];
  179.  
  180.     strcpy(CtrlStrCopy, CtrlStr);
  181.  
  182.     /* Using base address of parameters we access other parameters addr:  */
  183.     /* Note that me (for sure!) samples data beyond the current function  */
  184.     /* frame, but we accesson these set address only by demand.          */
  185.     for (i=1; i<=MAX_PARAM; i++) Parameters[i-1] = (int *) * (i+&CtrlStr);
  186. #endif /* USE_VARARG */
  187.  
  188.     --argc; argv++;        /* Skip the program name (first in argv/c list). */
  189.     while (argc >= 0) {
  190.     if (!GAOptionExists(argc, argv)) break;            /* The loop. */
  191.     argc--;
  192.     Option    = *(argv++);
  193.     if ((Error = GAUpdateParameters(Parameters, &ParamCount, Option,
  194.          CtrlStrCopy, CtrlStr, &argc, &argv)) != FALSE) return Error;
  195.     }
  196.     /*    Check for results and update trail of command line: */
  197.     return GATestAllSatis(CtrlStrCopy, CtrlStr, &argc, &argv, Parameters,
  198.                                  &ParamCount);
  199. }
  200.  
  201. /***************************************************************************
  202. * Routine to search for    unsatisfied flags - simply scan    the list for !-       *
  203. * sequence. Before this    scan, this routine updates the rest of the command *
  204. * line into the    last two parameters if it is requested by the CtrlStr       *
  205. * (last    item in    CtrlStr    is NOT an option).                   *
  206. * Return OK if all satisfied, CMD_ERR_AllSatis error else.           *
  207. ***************************************************************************/
  208. static int GATestAllSatis(char *CtrlStrCopy, char *CtrlStr, int *argc,
  209.     char ***argv, int *Parameters[MAX_PARAM], int *ParamCount)
  210. {
  211.     int    i;
  212.     static char    *LocalToken = NULL;
  213.  
  214.     /* If LocalToken is not initialized - do it now. Note that this string */
  215.     /* should be writable as well so we can not assign it directly.        */
  216.     if (LocalToken == NULL) {
  217.         LocalToken = (char *) malloc(3);
  218.     strcpy(LocalToken, "-?");
  219.     }
  220.  
  221.     /* Check is    last item is an    option.    If not then copy rest of command */
  222.     /* line into it as 1. NumOfprm, 2. pointer to block    of pointers.     */
  223.     for    (i=strlen(CtrlStr)-1; ((i>0) &&    (!ISSPACE(CtrlStr[i]))); i--);
  224.     if (!ISCTRLCHAR(CtrlStr[i+2])) {
  225.     GASetParamCount(CtrlStr, i, ParamCount);   /* Point in correct prm.. */
  226.     *Parameters[(*ParamCount)++] = *argc;
  227.     GAByteCopy((char *) Parameters[(*ParamCount)++], (char *) argv,
  228.                             sizeof(char *));
  229.     }
  230.  
  231.     i =    0;
  232.     while (++i < strlen(CtrlStrCopy))
  233.     if ((CtrlStrCopy[i] == '-') && (CtrlStrCopy[i-1] == '!')) {
  234.         GAErrorToken = LocalToken;
  235.         LocalToken[1] = CtrlStrCopy[i-2];        /* Set the corrent flag. */
  236.         return CMD_ERR_AllSatis;
  237.     }
  238.  
  239.     return OK;
  240. }
  241.  
  242. /***************************************************************************
  243. * Routine to update the    parameters according to    the given Option:       *
  244. ***************************************************************************/
  245. static int GAUpdateParameters(int *Parameters[], int *ParamCount,
  246.        char *Option, char *CtrlStrCopy, char *CtrlStr, int *argc, char ***argv)
  247. {
  248.     int    i;
  249.  
  250.     if (Option[0] != '-') {
  251.     GAErrorToken = Option;
  252.     return CMD_ERR_NotAnOpt;
  253.     }
  254.     i =    0;                /* Scan the CtrlStrCopy for that option: */
  255.     while (i+2 < strlen(CtrlStrCopy)) {
  256.     if ((CtrlStrCopy[i] == Option[1]) && (ISCTRLCHAR(CtrlStrCopy[i+1]))
  257.         && (CtrlStrCopy[i+2] == '-')) {
  258.         /* We found    that option! */
  259.         break;
  260.     }
  261.     i++;
  262.     }
  263.     if (i+2 >= strlen(CtrlStrCopy)) {
  264.     GAErrorToken = Option;
  265.     return CMD_ERR_NoSuchOpt;
  266.     }
  267.  
  268.     /* If we are here, then we found that option in CtrlStr - Strip it off:  */
  269.     CtrlStrCopy[i] = CtrlStrCopy[i+1] =    CtrlStrCopy[i+2] = (char) ' ';
  270.     GASetParamCount(CtrlStr, i, ParamCount);/*Set it to point in correct prm.*/
  271.     i += 3;
  272.     *Parameters[(*ParamCount)++] = 1;    /* Set boolean flag for that option. */
  273.     if (ISSPACE(CtrlStrCopy[i])) return    OK;/* Only a boolean flag is needed. */
  274.  
  275.     /* Skip the    text between the bolean    option and data    follows: */
  276.     while (!ISCTRLCHAR(CtrlStrCopy[i]))    i++;
  277.     /* Get the parameters and return the propriete return code:    */
  278.     return GAGetParmeters(Parameters, ParamCount, &CtrlStrCopy[i],
  279.                               Option, argc, argv);
  280. }
  281.  
  282. /***************************************************************************
  283. * Routine to get parameters according to the CtrlStr given from    argv/c :   *
  284. ***************************************************************************/
  285. static int GAGetParmeters(int *Parameters[], int *ParamCount,
  286.     char *CtrlStrCopy , char *Option, int *argc, char ***argv)
  287. {
  288.     int    i = 0, ScanRes;
  289.  
  290.     while (!(ISSPACE(CtrlStrCopy[i]))) {
  291.     switch (CtrlStrCopy[i+1]) {
  292.         case 'd':                     /* Get signed integers. */
  293.         ScanRes    = sscanf(*((*argv)++), "%d",
  294.                      (int *) Parameters[(*ParamCount)++]);
  295.         break;
  296.         case 'u':                   /* Get unsigned integers. */
  297.         ScanRes    = sscanf(*((*argv)++), "%u",
  298.                     (unsigned *) Parameters[(*ParamCount)++]);
  299.         break;
  300.         case 'x':                    /* Get hex integers. */
  301.         ScanRes    = sscanf(*((*argv)++), "%x",
  302.                      (int *) Parameters[(*ParamCount)++]);
  303.         break;
  304.         case 'o':                      /* Get octal integers. */
  305.         ScanRes    = sscanf(*((*argv)++), "%o",
  306.                      (int *) Parameters[(*ParamCount)++]);
  307.         break;
  308.         case 'D':                /* Get signed long integers. */
  309.         ScanRes    = sscanf(*((*argv)++), "%ld",
  310.                     (long *) Parameters[(*ParamCount)++]);
  311.         break;
  312.         case 'U':                  /* Get unsigned long integers. */
  313.         ScanRes    = sscanf(*((*argv)++), "%lu",
  314.                    (unsigned long *) Parameters[(*ParamCount)++]);
  315.         break;
  316.         case 'X':                   /* Get hex long integers. */
  317.         ScanRes    = sscanf(*((*argv)++), "%lx",
  318.                     (long *) Parameters[(*ParamCount)++]);
  319.         break;
  320.         case 'O':                 /* Get octal long integers. */
  321.         ScanRes    = sscanf(*((*argv)++), "%lo",
  322.                     (long *) Parameters[(*ParamCount)++]);
  323.         break;
  324.         case 'f':                    /* Get float number. */
  325.         ScanRes    = sscanf(*((*argv)++), "%f",
  326.                        (float *) Parameters[(*ParamCount)++]);
  327.         case 'F':                 /* Get double float number. */
  328.         ScanRes    = sscanf(*((*argv)++), "%lf",
  329.                       (double *) Parameters[(*ParamCount)++]);
  330.         break;
  331.         case 's':                      /* It as a string. */
  332.         ScanRes    = 1;                     /* Allways O.K. */
  333.         GAByteCopy((char *) Parameters[(*ParamCount)++],
  334.                     (char *) ((*argv)++), sizeof(char *));
  335.         break;
  336.         case '*':                 /* Get few parameters into one: */
  337.         ScanRes    = GAGetMultiParmeters(Parameters, ParamCount,
  338.                           &CtrlStrCopy[i], argc, argv);
  339.         if ((ScanRes ==    0) && (CtrlStrCopy[i] == '!')) {
  340.             GAErrorToken = Option;
  341.             return CMD_ERR_WildEmpty;
  342.         }
  343.         break;
  344.         default:
  345.         ScanRes = 0;               /* Make optimizer warning silent. */
  346.     }
  347.     /* If reading fails and    this number is a must (!) then error: */
  348.     if ((ScanRes ==    0) && (CtrlStrCopy[i] == '!')) {
  349.         GAErrorToken = Option;
  350.         return CMD_ERR_NumRead;
  351.     }
  352.     if (CtrlStrCopy[i+1] !=    '*') {
  353.          (*argc)--;         /* Everything is OK - update to next parameter: */
  354.          i += 2;             /* Skip to next parameter (if any). */
  355.     }
  356.     else i += 3;                       /* Skip the '*' also! */
  357.     }
  358.  
  359.     return OK;
  360. }
  361.  
  362. /***************************************************************************
  363. * Routine to get few parameters    into one pointer such that the returned       *
  364. * pointer actually points on a block of    pointers to the    parameters...       *
  365. * For example *F means a pointer to pointers on    floats.               *
  366. * Returns number of parameters actually    read.                   *
  367. * This routine assumes that all    pointers (on any kind of scalar) has the   *
  368. * same size (and the union below is totally ovelapped bteween dif. arrays) *
  369. ***************************************************************************/
  370. static int GAGetMultiParmeters(int *Parameters[], int *ParamCount,
  371.     char *CtrlStrCopy, int *argc, char ***argv)
  372. {
  373.     int    i = 0, ScanRes,    NumOfPrm = 0, **Pmain, **Ptemp;
  374.     union TmpArray {    /* Save here the temporary data before copying it to */
  375.     int    *IntArray[MAX_PARAM];          /* the returned pointer block. */
  376.     long   *LngArray[MAX_PARAM];
  377.     float  *FltArray[MAX_PARAM];
  378.     double *DblArray[MAX_PARAM];
  379.     char   *ChrArray[MAX_PARAM];
  380.     } TmpArray;
  381.  
  382.     do {
  383.     switch(CtrlStrCopy[2]) {    /* CtrlStr == '!*?' or '%*?' where ? is. */
  384.         case 'd':               /* Format to read the parameters: */
  385.         TmpArray.IntArray[NumOfPrm] = (int *) MyMalloc(sizeof(int));
  386.         ScanRes    = sscanf(*((*argv)++), "%d",
  387.                        (int *) TmpArray.IntArray[NumOfPrm++]);
  388.         break;
  389.         case 'u':
  390.         TmpArray.IntArray[NumOfPrm] = (int *) MyMalloc(sizeof(int));
  391.         ScanRes    = sscanf(*((*argv)++), "%u",
  392.                   (unsigned    int *) TmpArray.IntArray[NumOfPrm++]);
  393.         break;
  394.         case 'o':
  395.         TmpArray.IntArray[NumOfPrm] = (int *) MyMalloc(sizeof(int));
  396.         ScanRes    = sscanf(*((*argv)++), "%o",
  397.                        (int *) TmpArray.IntArray[NumOfPrm++]);
  398.         break;
  399.         case 'x':
  400.         TmpArray.IntArray[NumOfPrm] = (int *) MyMalloc(sizeof(int));
  401.         ScanRes    = sscanf(*((*argv)++), "%x",
  402.                        (int *) TmpArray.IntArray[NumOfPrm++]);
  403.         break;
  404.         case 'D':
  405.         TmpArray.LngArray[NumOfPrm] = (long *) MyMalloc(sizeof(long));
  406.         ScanRes    = sscanf(*((*argv)++), "%ld",
  407.                       (long *) TmpArray.IntArray[NumOfPrm++]);
  408.         break;
  409.         case 'U':
  410.         TmpArray.LngArray[NumOfPrm] = (long *) MyMalloc(sizeof(long));
  411.         ScanRes    = sscanf(*((*argv)++), "%lu",
  412.                  (unsigned long *) TmpArray.IntArray[NumOfPrm++]);
  413.         break;
  414.         case 'O':
  415.         TmpArray.LngArray[NumOfPrm] = (long *) MyMalloc(sizeof(long));
  416.         ScanRes    = sscanf(*((*argv)++), "%lo",
  417.                       (long *) TmpArray.IntArray[NumOfPrm++]);
  418.         break;
  419.         case 'X':
  420.         TmpArray.LngArray[NumOfPrm] = (long *) MyMalloc(sizeof(long));
  421.         ScanRes    = sscanf(*((*argv)++), "%lx",
  422.                       (long *) TmpArray.IntArray[NumOfPrm++]);
  423.         break;
  424.         case 'f':
  425.         TmpArray.FltArray[NumOfPrm] = (float *)    MyMalloc(sizeof(float));
  426.         ScanRes    = sscanf(*((*argv)++), "%f",
  427.                      (float *) TmpArray.LngArray[NumOfPrm++]);
  428.         break;
  429.         case 'F':
  430.         TmpArray.DblArray[NumOfPrm] =
  431.                        (double *) MyMalloc(sizeof(double));
  432.         ScanRes    = sscanf(*((*argv)++), "%lf",
  433.                     (double *) TmpArray.LngArray[NumOfPrm++]);
  434.         break;
  435.         case 's':
  436.         while ((*argc) && ((**argv)[0] != '-'))    {
  437.             TmpArray.ChrArray[NumOfPrm++] = *((*argv)++);
  438.             (*argc)--;
  439.         }
  440.         ScanRes    = 0;               /* Force quit from do - loop. */
  441.         NumOfPrm++;        /* Updated again immediately after loop! */
  442.         (*argv)++;                           /* "" */
  443.         break;
  444.         default:
  445.         ScanRes = 0;               /* Make optimizer warning silent. */
  446.     }
  447.     (*argc)--;
  448.     }
  449.     while (ScanRes == 1);          /* Exactly one parameter was read. */
  450.     (*argv)--; NumOfPrm--; (*argc)++;
  451.  
  452.     /* Now allocate the    block with the exact size, and set it: */
  453.     Ptemp = Pmain = (int **) MyMalloc((unsigned) (NumOfPrm+1) *    sizeof(int *));
  454.     /* And here    we use the assumption that all pointers    are the    same: */
  455.     for    (i=0; i<NumOfPrm; i++) *(Ptemp++) = TmpArray.IntArray[i];
  456.     *(Ptemp) = (int *) NULL;           /* Close the block with NULL pointer. */
  457.  
  458.     /* That it save the    number of parameters read as first parameter to    */
  459.     /* return and the pointer to the block as second, and return:    */
  460.     *Parameters[(*ParamCount)++] = NumOfPrm;
  461.     GAByteCopy((char *)    Parameters[(*ParamCount)++], (char *) &Pmain,
  462.                                  sizeof(char *));
  463.     return NumOfPrm;
  464. }
  465.  
  466. /***************************************************************************
  467. * Routine to scan the CtrlStr, upto Max    and count the number of    parameters *
  468. * to that point:                               *
  469. * 1. Each option is counted as one parameter - boolean variable    (int)       *
  470. * 2. Within an option, each %? or !? is    counted    once - pointer to something*
  471. * 3. Within an option, %*? or !*? is counted twice - one for item count       *
  472. *    and one for pointer to block pointers.                   *
  473. * Note ALL variables are passed    by address and so of fixed size    (address). *
  474. ***************************************************************************/
  475. static void GASetParamCount(char *CtrlStr, int Max, int *ParamCount)
  476. {
  477.     int    i;
  478.  
  479.     *ParamCount    = 0;
  480.     for    (i=0; i<Max; i++) if (ISCTRLCHAR(CtrlStr[i])) {
  481.     if (CtrlStr[i+1] == '*') *ParamCount +=    2;
  482.     else             (*ParamCount)++;
  483.     }
  484. }
  485.  
  486. /***************************************************************************
  487. * Routine to copy exactly n bytes fro Src to Dst. Note system library       *
  488. * routine strncpy should do the    same, but it stops on NULL char    !       *
  489. ***************************************************************************/
  490. static void GAByteCopy(char *Dst, char *Src, unsigned n)
  491. {
  492.     while (n--)    *(Dst++) = *(Src++);
  493. }
  494.  
  495. /***************************************************************************
  496. * Routine to check if more option (i.e.    first char == '-') exists in the   *
  497. * given    list argc, argv:                           *
  498. ***************************************************************************/
  499. static int GAOptionExists(int argc, char **argv)
  500. {
  501.     while (argc--) if ((*(argv++))[0] == '-') return TRUE;
  502.     return FALSE;
  503. }
  504.  
  505. /***************************************************************************
  506. * Routine to print some    error messages,    for this module:           *
  507. ***************************************************************************/
  508. void GAPrintErrMsg(int Error)
  509. {
  510.     fprintf(stderr, "Error in command line parsing - ");
  511.     switch (Error) {
  512.     case 0:;
  513.         break;
  514.     case CMD_ERR_NotAnOpt:
  515.         fprintf(stderr, "None option Found");
  516.         break;
  517.     case CMD_ERR_NoSuchOpt:
  518.         fprintf(stderr, "Undefined option Found");
  519.         break;
  520.     case CMD_ERR_WildEmpty:
  521.         fprintf(stderr, "Empty input for '!*?' seq.");
  522.         break;
  523.     case CMD_ERR_NumRead:
  524.         fprintf(stderr, "Failed on reading number");
  525.         break;
  526.     case CMD_ERR_AllSatis:
  527.         fprintf(stderr, "Fail to satisfy");
  528.         break;
  529.     }
  530.     fprintf(stderr, " - '%s'\n", GAErrorToken);
  531. }
  532.  
  533. /***************************************************************************
  534. * Routine to print correct format of command line allowed:           *
  535. ***************************************************************************/
  536. void GAPrintHowTo(char *CtrlStr)
  537. {
  538.     int    i = 0, SpaceFlag;
  539.  
  540.     fprintf(stderr, "Usage: ");
  541.     /* Print program name - first word in ctrl.    str. (optional): */
  542.     while (!(ISSPACE(CtrlStr[i])) && (!ISCTRLCHAR(CtrlStr[i+1])))
  543.     fprintf(stderr, "%c", CtrlStr[i++]);
  544.  
  545.     while (i < strlen(CtrlStr))    {
  546.     while ((ISSPACE(CtrlStr[i])) &&    (i < strlen(CtrlStr))) i++;
  547.     switch (CtrlStr[i+1]) {
  548.         case '%':
  549.         fprintf(stderr, " [-%c", CtrlStr[i++]);
  550.         i += 2;             /* Skip the '%-' or '!- after the char! */
  551.         SpaceFlag = TRUE;
  552.         while (!ISCTRLCHAR(CtrlStr[i]) && (i < strlen(CtrlStr))    &&
  553.             (!ISSPACE(CtrlStr[i])))
  554.             if (SpaceFlag) {
  555.             if (CtrlStr[i++] == SPACE_CHAR)
  556.                  fprintf(stderr, " ");
  557.             else fprintf(stderr, " %c", CtrlStr[i-1]);
  558.             SpaceFlag = FALSE;
  559.             }
  560.             else if (CtrlStr[i++] == SPACE_CHAR)
  561.                   fprintf(stderr, " ");
  562.              else fprintf(stderr, "%c", CtrlStr[i-1]);
  563.         while (!ISSPACE(CtrlStr[i]) && (i < strlen(CtrlStr))) {
  564.             if (CtrlStr[i] == '*') fprintf(stderr, "...");
  565.             i++;                 /* Skip the rest of it. */
  566.         }
  567.         fprintf(stderr, "]");
  568.         break;
  569.         case '!':
  570.         fprintf(stderr, " -%c", CtrlStr[i++]);
  571.         i += 2;             /* Skip the '%-' or '!- after the char! */
  572.         SpaceFlag = TRUE;
  573.         while (!ISCTRLCHAR(CtrlStr[i]) && (i < strlen(CtrlStr))    &&
  574.             (!ISSPACE(CtrlStr[i])))
  575.             if (SpaceFlag) {
  576.             if (CtrlStr[i++] == SPACE_CHAR)
  577.                  fprintf(stderr, " ");
  578.             else fprintf(stderr, " %c", CtrlStr[i-1]);
  579.             SpaceFlag = FALSE;
  580.             }
  581.             else if (CtrlStr[i++] == SPACE_CHAR)
  582.                   fprintf(stderr, " ");
  583.              else fprintf(stderr, "%c", CtrlStr[i-1]);
  584.         while (!ISSPACE(CtrlStr[i]) && (i < strlen(CtrlStr))) {
  585.             if (CtrlStr[i] == '*') fprintf(stderr, "...");
  586.             i++;                 /* Skip the rest of it. */
  587.         }
  588.         break;
  589.         default:               /* Not checked, but must be last one! */
  590.         fprintf(stderr, " ");
  591.         while (!ISSPACE(CtrlStr[i]) && (i < strlen(CtrlStr)) &&
  592.                !ISCTRLCHAR(CtrlStr[i]))
  593.             fprintf(stderr, "%c", CtrlStr[i++]);
  594.         fprintf(stderr, "\n");
  595.         return;
  596.     }
  597.     }
  598.     fprintf(stderr, "\n");
  599. }
  600.  
  601. #ifdef    MYMALLOC
  602.  
  603. /***************************************************************************
  604. * My Routine to    allocate dynamic memory. All program requests must call    *
  605. * this routine (no direct call to malloc). Dies if no memory.           *
  606. ***************************************************************************/
  607. static char *MyMalloc(unsigned size)
  608. {
  609.     char *p;
  610.  
  611.     if ((p = (char *) malloc(size)) != NULL) return p;
  612.  
  613.     fprintf(stderr, "Not enough memory, exit\n");
  614.     exit(2);
  615.  
  616.     return NULL;                    /* Makes warning silent. */
  617. }
  618.  
  619. #endif    /* MYMALLOC */
  620.