home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / XAP / XFIG / TRANSFIG.2 / TRANSFIG / transfig / fig2dev / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-14  |  4.0 KB  |  153 lines

  1. /*
  2.  * TransFig: Facility for Translating Fig code
  3.  * Copyright (c) 1985 Supoj Sutantavibul
  4.  * Copyright (c) 1991 Micah Beck
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation. The authors make no representations about the suitability 
  11.  * of this software for any purpose.  It is provided "as is" without express 
  12.  * or implied warranty.
  13.  *
  14.  * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16.  * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19.  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  20.  * PERFORMANCE OF THIS SOFTWARE.
  21.  *
  22.  */
  23.  
  24. /*
  25.     I got this off net.sources from Henry Spencer.
  26.     It is a public domain getopt(3) like in System V.
  27.     I have made the following modifications:
  28.  
  29.     index(s,c) was added because too many people could
  30.     not compile getopt without it.
  31.  
  32.     A test main program was added, ifdeffed by GETOPT.
  33.     This main program is a public domain implementation
  34.     of the getopt(1) program like in System V.  The getopt
  35.     program can be used to standardize shell option handling.
  36.         e.g.  cc -DGETOPT getopt.c -o getopt
  37. */
  38. #include <stdio.h>
  39.  
  40. #ifndef lint
  41. static    char    sccsfid[] = "@(#) getopt.c 5.0 (UTZoo) 1985";
  42. #endif
  43.  
  44. #define    ARGCH    (int)':'
  45. #define BADCH     (int)'?'
  46. #define EMSG     ""
  47. #define    ENDARGS  "--"
  48.  
  49. /* this is included because index is not on some UNIX systems */
  50. static
  51. char *
  52. index (s, c)
  53. register    char    *s;
  54. register    int     c;
  55.     {
  56.     while (*s)
  57.         if (c == *s) return (s);
  58.         else s++;
  59.     return (NULL);
  60.     }
  61.  
  62. /*
  63.  * get option letter from argument vector
  64.  */
  65. int    opterr = 1,        /* useless, never set or used */
  66.     optind = 1,        /* index into parent argv vector */
  67.     optopt;            /* character checked for validity */
  68. char    *optarg;        /* argument associated with option */
  69.  
  70. #define tell(s)    fputs(*nargv,stderr);fputs(s,stderr); \
  71.         fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);
  72.  
  73.  
  74. getopt(nargc,nargv,ostr)
  75. int    nargc;
  76. char    **nargv,
  77.     *ostr;
  78. {
  79.     static char    *place = EMSG;    /* option letter processing */
  80.     register char    *oli;        /* option letter list index */
  81.     char    *index();
  82.  
  83.     if(!*place) {            /* update scanning pointer */
  84.         if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) return(EOF);
  85.         if (*place == '-') {    /* found "--" */
  86.             ++optind;
  87.             return(EOF);
  88.         }
  89.     }                /* option letter okay? */
  90.     if ((optopt = (int)*place++) == ARGCH || !(oli = index(ostr,optopt))) {
  91.         if(!*place) ++optind;
  92.         tell(": illegal option -- ");
  93.     }
  94.     if (*++oli != ARGCH) {        /* don't need argument */
  95.         optarg = NULL;
  96.         if (!*place) ++optind;
  97.     }
  98.     else {                /* need an argument */
  99.         if (*place) optarg = place;    /* no white space */
  100.         else if (nargc <= ++optind) {    /* no arg */
  101.             place = EMSG;
  102.             tell(": option requires an argument -- ");
  103.         }
  104.          else optarg = nargv[optind];    /* white space */
  105.         place = EMSG;
  106.         ++optind;
  107.     }
  108.     return(optopt);            /* dump back option letter */
  109. }
  110.  
  111.  
  112. #ifdef GETOPT
  113.  
  114. #ifndef lint
  115. static    char    sccspid[] = "@(#) getopt.c 5.1 (WangInst) 6/15/85";
  116. #endif
  117.  
  118. main (argc, argv) char **argv;
  119.     {
  120.     char    *optstring = argv[1];
  121.     char    *argv0 = argv[0];
  122.     extern    int     optind;
  123.     extern    char    *optarg;
  124.     int     opterr = 0;
  125.     int     C;
  126.     char    *opi;
  127.     if (argc == 1)
  128.         {
  129.         fprintf (stderr, "Usage: %s optstring args\n", argv0);
  130.         exit (1);
  131.         }
  132.     argv++;
  133.     argc--;
  134.     argv[0] = argv0;
  135.     while ((C = getopt (argc, argv, optstring)) != EOF)
  136.         {
  137.         if (C == BADCH) opterr++;
  138.         printf ("-%c ", C);
  139.         opi = index (optstring, C);
  140.         if (opi && opi[1] == ARGCH)
  141.             if (optarg)
  142.                 printf ("\"%s\" ", optarg);
  143.             else opterr++;
  144.         }
  145.     printf ("%s", ENDARGS);
  146.     while (optind < argc)
  147.         printf (" \"%s\"", argv[optind++]);
  148.     putchar ('\n');
  149.     exit (opterr);
  150.     }
  151.  
  152. #endif
  153.