"Fossies" - the Fresh Open Source Software Archive

Member "xc/getopts.c" (8 Aug 1995, 4421 Bytes) of package /linux/privat/old/laspack.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /****************************************************************************/
    2 /*                                getopts.c                                 */
    3 /****************************************************************************/
    4 /*                                                                          */
    5 /* GET OPTionS from command line                                            */
    6 /*                                                                          */
    7 /* Copyright (C) 1992-1994 Tomas Skalicky. All rights reserved.             */
    8 /*                                                                         */
    9 /****************************************************************************/
   10 
   11 #include <ctype.h>
   12 #include <string.h>
   13 
   14 #include "xc/xtypes.h"
   15 #include "xc/getopts.h"
   16 
   17 static size_t MaxStrLen = 16;
   18 static OptErrType OptErr;
   19 
   20 static OptDescrType *SearchOpt(char PtrArg, OptTabType *OptTab);
   21 static char *SetOpt(OptDescrType *PtrOptDescr, char *PtrArg);
   22 
   23 void GetOpts(int *argc, char **argv, OptTabType *OptTab)
   24 /* get options from command line */
   25 {
   26     int ArgOrig, ArgNeu;
   27     char *PtrArg;
   28     OptDescrType *PtrOptDescr;
   29 
   30     OptErr = OptOK;
   31     ArgNeu = 0;
   32     for (ArgOrig = 1; ArgOrig < *argc; ArgOrig++) {
   33         /* test if switch character found */
   34         if (argv[ArgOrig][0] == '-' || argv[ArgOrig][0] == '/') {
   35             /* analyse argument */
   36             PtrArg = argv[ArgOrig] + 1;
   37             while (PtrArg[0] != '\0') {
   38                 /* search option description in options table */
   39                 PtrOptDescr = SearchOpt(PtrArg[0], OptTab);
   40                 PtrArg++;
   41                 /* test if option description exists */
   42                 if (PtrOptDescr != NULL) {
   43                     /* set option resp. assign option argument */
   44                     PtrArg = SetOpt(PtrOptDescr, PtrArg);
   45                 } else {
   46                     /* set OptErr */
   47                     OptErr = OptNotDefErr;
   48                 }
   49             }
   50         } else {
   51             /* copy argument in new argv */
   52             ArgNeu++;
   53             argv[ArgNeu] = argv[ArgOrig];
   54         }
   55     }
   56     *argc = ArgNeu + 1;
   57 }
   58 
   59 void SetMaxStrLen(size_t Len)
   60 /* set maximal string lenght for string option type */
   61 {
   62     MaxStrLen = Len;
   63 }
   64 
   65 OptErrType OptResult(void)
   66 /* get result of options analysis */
   67 {
   68     return(OptErr);
   69 }
   70 
   71 static OptDescrType *SearchOpt(char SearchChar, OptTabType *OptTab)
   72 /* search option description for a given character */
   73 {
   74     OptDescrType *PtrOptDescr; /* pointer to option description */
   75 
   76     int Opt; /* option */
   77 
   78     PtrOptDescr = NULL;
   79     for (Opt = 0; Opt < OptTab->No; Opt++) {
   80         /* test if option description found */
   81         if (OptTab->Descr[Opt].KeyChar == SearchChar)
   82             /* set pointer to this description */
   83             PtrOptDescr = &(OptTab->Descr[Opt]);
   84     }
   85     return(PtrOptDescr);
   86 }
   87 
   88 static char *SetOpt(OptDescrType *PtrOptDescr, char *PtrArg)
   89 /* set option or assign option argument to variable in options table */
   90 {
   91     int IntVal;
   92     size_t SizeVal;
   93 
   94     switch (PtrOptDescr->Type) {
   95         case BoolOptType:
   96             *((Boolean *)PtrOptDescr->Variable) = True;
   97             break;
   98         case CharOptType:
   99             *((char *)PtrOptDescr->Variable) = *PtrArg;
  100             if (*PtrArg != '\0')
  101                 PtrArg++;
  102             break;
  103         case IntOptType:
  104             while (isspace(*PtrArg))
  105                 PtrArg++;
  106             IntVal = 0;
  107             if (!(isdigit(*PtrArg)) || *PtrArg == '\0')
  108                 OptErr = OptSyntaxErr;
  109             while (isdigit(*PtrArg)) {
  110                 IntVal = 10 * IntVal + (*PtrArg - '0');
  111                 PtrArg++;
  112             }
  113             *((int *)PtrOptDescr->Variable) = IntVal;
  114             break;
  115         case SizeOptType:
  116             while (isspace(*PtrArg))
  117                 PtrArg++;
  118             SizeVal = 0;
  119             if (!(isdigit(*PtrArg)) || *PtrArg == '\0')
  120                 OptErr = OptSyntaxErr;
  121             while (isdigit(*PtrArg)) {
  122                 SizeVal = 10 * SizeVal + (*PtrArg - '0');
  123                 PtrArg++;
  124             }
  125             *((int *)PtrOptDescr->Variable) = SizeVal;
  126             break;
  127         case StrOptType:
  128             strncpy((char *)PtrOptDescr->Variable, PtrArg, MaxStrLen);
  129             PtrArg += strlen(PtrArg);
  130             break;
  131         default:
  132             OptErr = OptDescrErr;
  133             break;
  134     }
  135     return(PtrArg);
  136 }