home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 19 Printer / 19-Printer.zip / lj2up2.zip / GETARGS.C < prev    next >
Text File  |  1988-07-13  |  5KB  |  190 lines

  1. /*  GETARGS.C   Command line argument processor for C programs
  2.  *
  3.  *     (C) Copyright 1985, Allen I. Holub.  All rights reserved.
  4.  *    This program may be copied for personal, non-profit use only.
  5.  *
  6.  *  Changed to ignore case of switch characters.  Joe Barnhart  2-27-87
  7.  *
  8.  *  Minor changes for MSC 5.1 asthetics           Steve Coles   7-1-88
  9.  */
  10.  
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include "getargs.h"
  14. #include "stoi.h"           /* For prototype */
  15.  
  16. /*----------------------------------------------------------------------*/
  17.  
  18. typedef int (*PFI)();
  19.  
  20. /*----------------------------------------------------------------------*/
  21.  
  22. /* Prototypes for this module */
  23. extern  int getargs(int argc,char * *argv,ARG *tabp,int tabsize);
  24. static char *setarg(ARG *argp,char *linep);
  25. static ARG *findarg(int c,ARG *tabp,int tabsize);
  26. static void pr_usage(ARG *tabp,int tabsize);
  27.  
  28. static char *setarg( argp, linep )
  29. ARG     *argp;
  30. char        *linep;
  31. {
  32.     /*  Set an argument. argp points at the argument table entry
  33.      *  corresponding to *linep. Return linep, updated to point
  34.      *  past the argument being set.
  35.      */
  36.  
  37.     ++linep;
  38.  
  39.     switch( argp->type )
  40.     {
  41.     case INTEGER:
  42.         *argp->variable = stoi( &linep );
  43.         break;
  44.  
  45.     case BOOLEAN:
  46.         *argp->variable = 1;
  47.         break;
  48.  
  49.     case CHARACTER:
  50.         *argp->variable = *linep++ ;
  51.         break;
  52.  
  53.     case STRING:
  54.         *(char **)argp->variable = linep ;
  55.         linep = "";
  56.         break;
  57.  
  58. /* Removed because it is not needed and with W3 error checking, MSC 5.1
  59.     does not like the cast from int to function pointer and I am too lazy
  60.     to clean it up right now.
  61.  
  62.     case PROC:
  63.         (* (PFI)(argp->variable) )( linep );
  64.         linep = "";
  65.         break;      */
  66.  
  67.     default:
  68.         fprintf(stderr,"INTERNAL ERROR: BAD ARGUMENT TYPE\n");
  69.         break;
  70.     }
  71.  
  72.     return( linep );
  73. }
  74.  
  75. /*----------------------------------------------------------------------*/
  76.  
  77. static ARG  *findarg( c, tabp, tabsize )
  78. int     c, tabsize;
  79. ARG     *tabp;
  80. {
  81.     /*  Return pointer to argument table entry corresponding
  82.      *  to c (or 0 if c isn't in table).
  83.      */
  84.     
  85.     for(; --tabsize >= 0 ; tabp++ )
  86.         if( tabp->arg == c )
  87.             return tabp;
  88.  
  89.     return 0;
  90. }
  91.  
  92.  
  93. static void pr_usage( tabp, tabsize )
  94. ARG *tabp;
  95. int tabsize;
  96. {
  97.     /*  Print the argtab in the form:
  98.      *      -<arg> <errmsg>     (value is <*variable>)
  99.      */
  100.  
  101.     for(; --tabsize >= 0 ;  tabp++ )
  102.     {
  103.         switch( tabp->type )
  104.         {
  105.         case INTEGER:
  106.             fprintf(stderr, "-%c<num> %-40s (value is ",
  107.                         tabp->arg, tabp->errmsg);
  108.             fprintf(stderr, "%-5d)\n", *(tabp->variable) );
  109.             break;
  110.  
  111.         case BOOLEAN:
  112.             fprintf(stderr,"-%c      %-40s (value is ",
  113.                         tabp->arg, tabp->errmsg);
  114.             fprintf(stderr, "%-5s)\n", *(tabp->variable)
  115.                          ? "TRUE": "FALSE");
  116.             break;
  117.  
  118.         case CHARACTER: 
  119.             fprintf(stderr, "-%c<c>   %-40s (value is ",
  120.                         tabp->arg, tabp->errmsg);
  121.             fprintf(stderr, "%-5c)\n", *(tabp->variable) );
  122.             break;
  123.  
  124.         case STRING:    
  125.             fprintf(stderr, "-%c<str> %-40s (value is ",
  126.                         tabp->arg, tabp->errmsg);
  127.             fprintf(stderr, "<%s>)\n",
  128.                         *(char **)tabp->variable);
  129.             break;
  130.  
  131.         case PROC:
  132.             fprintf(stderr, "-%c<str> %-40s\n",
  133.                         tabp->arg, tabp->errmsg);
  134.             break;
  135.         }
  136.     }
  137. }
  138.  
  139.  
  140. #define ERRMSG  "Illegal argument <%c>.   Legal arguments are:\n\n"
  141.  
  142. int getargs(argc, argv, tabp, tabsize )
  143. int argc, tabsize ;
  144. char    **argv ;
  145. ARG *tabp  ;
  146. {
  147.     /* Process command line arguments. Stripping all command line 
  148.      * switches out of argv. Return a new argc. If an error is found
  149.      * exit(1) is called (getargs won't return) and a usage message
  150.      * is printed showing all arguments in the table.
  151.      */
  152.  
  153.     register int    nargc       ;
  154.     register char   **nargv, *p ;
  155.     register ARG    *argp       ;
  156.  
  157.     if( argc == 1 )
  158.     {
  159.         pr_usage( tabp, tabsize );
  160.         exit( 1 );
  161.     }
  162.     nargc = 1 ;
  163.     for(nargv = ++argv ; --argc > 0 ; argv++ )
  164.     {
  165.         if( **argv != '-' )
  166.         {
  167.             *nargv++ = *argv ;
  168.             nargc++;
  169.         }
  170.         else
  171.         {
  172.             p = (*argv) + 1 ;
  173.  
  174.             while( *p )
  175.             {
  176.                 if(argp = findarg(tolower(*p), tabp, tabsize))
  177.                     p = setarg( argp, p );
  178.                 else
  179.                 {
  180.                     fprintf(stderr, ERRMSG, *p );
  181.                     pr_usage( tabp, tabsize );
  182.                     exit( 1 );
  183.                 }
  184.             }
  185.         }   
  186.     }
  187.     return nargc ;
  188. }
  189. 
  190.