home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / MM1 / GRAPHICS / jpegshow.lzh / JPEGSHOW / getargs.c next >
Text File  |  1990-03-29  |  6KB  |  217 lines

  1. /*      GETARGS.C       command line argument processor for C programs
  2.  *
  3.  *                      Dr. Dobb's Journal, May 1985, p. 32
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include "getargs.h"
  8.  
  9. typedef int     (*PFI)();
  10.  
  11. static char     *setarg( argp, linep )
  12. ARG             *argp;
  13. char            *linep;
  14. {
  15.         /*      Set an argument. argp points at the argument table entry
  16.          *      corresponding to *linep. Return linep, updated to point
  17.          *      past the argument being set.
  18.          */
  19.  
  20.         ++linep;
  21.  
  22.         switch( argp->type )
  23.         {
  24.         case INTEGER:
  25.                 *argp->variable = stoi( &linep );
  26.                 break;
  27.  
  28.         case BOOLEAN:
  29.                 *argp->variable = 1;
  30.                 break;
  31.  
  32.         case CHARACTER:
  33.                 *argp->variable = *linep++ ;
  34.                 break;
  35.  
  36.         case STRING:
  37.                 *(char **)argp->variable = linep ;
  38.                 linep = "";
  39.                 break;
  40.  
  41.         case PROC:
  42.                 (* (PFI)(argp->variable ) )( linep );
  43.                 linep = "";
  44.                 break;
  45.  
  46.         default:
  47.                 fprintf(stderr, "INTERNAL ERROR: BAD ARGUMENT TYPE\n");
  48.                 break;
  49.         }
  50.         return ( linep );
  51. }
  52.  
  53. /*----------------------------------------------------------------------*/
  54.  
  55. static ARG      *findarg( c, tabp, tabsize )
  56. int             c, tabsize;
  57. ARG             *tabp;
  58. {
  59.         /*      Return pointer to argument table entry corresponding
  60.          *      to c (or 0 if c isn't in table).
  61.          */
  62.  
  63.         for(; --tabsize >= 0 ; tabp++ )
  64.                 if( tabp->arg == c )
  65.                         return tabp;
  66.  
  67.         return 0;
  68. }
  69. static pr_usage( tabp, tabsize )
  70. ARG     *tabp;
  71. register    int tabsize;
  72. {
  73.         /*      Print the argtab in the form:
  74.          *              -<arg> <errmsg>     (value is <*variable>)
  75.          */
  76.  
  77.         for(; --tabsize >= 0 ; tabp++ )
  78.         {
  79.                 switch( tabp->type )
  80.                 {
  81.                 case INTEGER:
  82.                         fprintf(stderr, "-%c<num> %-40s (value is ",
  83.                                                 tabp->arg, tabp->errmsg);
  84.                         fprintf(stderr, "%-5d)\n", *(tabp->variable) );
  85.                         break;
  86.  
  87.                 case BOOLEAN:
  88.                         fprintf(stderr,"-%c      %-40s (value is ",
  89.                                                 tabp->arg, tabp->errmsg);
  90.                         fprintf(stderr, "%-5s)\n", *(tabp->variable)
  91.                                                  ? "TRUE": "FALSE");
  92.                         break;
  93.  
  94.                 case CHARACTER:
  95.                         fprintf(stderr, "-%c<c>   %-40s (value is ",
  96.                                                 tabp->arg, tabp->errmsg);
  97.                         fprintf(stderr, "%-5c)\n", *(tabp->variable) );
  98.                         break;
  99.  
  100.                 case STRING:
  101.                         fprintf(stderr, "-%c<str> %-40s (value is ",
  102.                                                 tabp->arg, tabp->errmsg);
  103.                         fprintf(stderr, "<%s>)\n",
  104.                                                 *(char **)tabp->variable);
  105.                         break;
  106.  
  107.                 case PROC:
  108.                         fprintf(stderr, "-%c<str> %-40s\n",
  109.                                                 tabp->arg, tabp->errmsg);
  110.                         break;
  111.                 }
  112.         }
  113. }
  114. #define ERRMSG  "Illegal argument <%c>.   Legal arguments are:\n\n"
  115.  
  116. int     getargs(argc, argv, tabp, tabsize )
  117. int     argc, tabsize ;
  118. char    **argv ;
  119. ARG     *tabp  ;
  120. {
  121.         /* Process command line arguments. Stripping all command line
  122.          * switches out of argv. Return a new argc. If an error is found
  123.          * exit(1) is called (getargs won't return) and a usage message
  124.          * is printed showing all arguments in the table.
  125.          */
  126.  
  127.         int       nargc       ;
  128.         register char   *p    ;
  129.         char   **nargv        ;
  130.         ARG    *argp          ;
  131.  
  132.         nargc = 1 ;
  133.         for(nargv = ++argv ; --argc > 0 ; argv++ )
  134.         {
  135.                 if( **argv != '-' )
  136.                 {
  137.                         *nargv++ = *argv ;
  138.                         nargc++;
  139.                 }
  140.                 else
  141.                 {
  142.                         p = (*argv) + 1 ;
  143.  
  144.                         while( *p )
  145.                         {
  146.                                 if(argp = findarg(*p, tabp, tabsize))
  147.                                         p = setarg( argp, p );
  148.                                 else
  149.                                 {
  150.                                         fprintf(stderr, ERRMSG, *p );
  151.                                         pr_usage( tabp, tabsize );
  152.                                         exit( 1 );
  153.                                 }
  154.                         }
  155.                 }
  156.         }
  157.         return nargc ;
  158. }
  159.  
  160. /*------------------------------------------------------------------------*/
  161.  
  162. int stoi(instr)
  163. char **instr;
  164. {
  165.     int num = 0  ;
  166.     int sign = 1 ;
  167.     register char *str ;
  168.  
  169.     str = *instr;
  170.  
  171.     while (*str == ' ' || *str == '\t' || *str == '\n' )
  172.             str++ ;
  173.  
  174.     if( *str == '-' )
  175.     {
  176.         sign = -1 ;
  177.         str++;
  178.     }
  179.     if( *str == '0' )
  180.     {
  181.         ++str;
  182.         if( *str == 'x' || *str == 'X' )
  183.         {
  184.             str++;
  185.             while(  ( '0' <= *str && *str <= '9' ) ||
  186.                     ( 'a' <= *str && *str <= 'f' ) ||
  187.                     ( 'A' <= *str && *str <= 'F' )  )
  188.             {
  189.                     num *= 16 ;
  190.                     num += ( '0' <= *str && *str <= '9' ) ?
  191.                             *str - '0'                    :
  192.                             toupper (*str) - 'A' + 10     ;
  193.                     str++ ;
  194.             }
  195.         }
  196.         else
  197.         {
  198.             while( '0' <= *str && *str <= '7' )
  199.             {
  200.                 num *= 8 ;
  201.                 num += *str++ = '0' ;
  202.             }
  203.         }
  204.     }
  205.     else
  206.     {
  207.         while( '0' <= *str && *str <= '9' )
  208.         {
  209.             num *= 10;
  210.             num += *str++ - '0' ;
  211.         }
  212.     }
  213.  
  214.     *instr = str ;
  215.     return( num*sign );
  216. }
  217.