home *** CD-ROM | disk | FTP | other *** search
/ Phoenix Heaven Sunny 2 / APPARE2.BIN / oh_towns / dic / src / getopt.c < prev    next >
C/C++ Source or Header  |  1995-06-20  |  3KB  |  166 lines

  1. #include    <stdio.h>
  2. #include    <stdlib.h>
  3. #include    <string.h>
  4. #include    <ctype.h>
  5. #include    "getopt.h"
  6.  
  7. extern    char    *progname();
  8.  
  9.     char    *optarg = "";
  10.     int    optind = 1;
  11. static  char    *optstr = NULL;
  12.  
  13. static  char    *optcmp(str, ptn)
  14. char    *str;
  15. char    *ptn;
  16. {
  17.     while ( *ptn != '\0' ) {
  18.     if ( *str != *ptn )
  19.         return NULL;
  20.     str++;
  21.     ptn++;
  22.     }
  23.     return str;
  24. }
  25. int    getopt_long(ac, av, tab)
  26. int    ac;
  27. char    *av[];
  28. struct option *tab;
  29. {
  30.     int     n;
  31.     char    *p;
  32.  
  33.     for ( ; ; ) {
  34.     if ( optstr != NULL && *optstr != '\0' ) {
  35.         for ( n = 0 ; tab[n].str != NULL ; n++ ) {
  36.         if ( tab[n].opt == *optstr ) {
  37.             optstr++;
  38.             if ( tab[n].pfg != 0 ) {
  39.             if ( *optstr == '=' )
  40.                 optstr++;
  41.  
  42.             if ( *optstr != '\0' )
  43.                 optarg = optstr;
  44.             else if ( optind < ac && *av[optind] != '-' )
  45.                 optarg = av[optind++];
  46.             else if ( tab[n].pfg != 2 ) {
  47.                 fprintf(stderr,
  48.                 "%s: option `-%c' requires an argument\n",
  49.                 progname(av[0]), tab[n].opt);
  50.                 optstr = NULL;
  51.                 return '\0';
  52.             } else
  53.                 optarg = NULL;
  54.  
  55.             optstr = NULL;
  56.             }
  57.             return tab[n].opt;
  58.         }
  59.         }
  60.  
  61.         fprintf(stderr, "%s: unrecognized option `-%c'\n",
  62.                 progname(av[0]), *optstr);
  63.         return *(optstr++);
  64.     }
  65.  
  66.     if ( optind >= ac || *av[optind] != '-' )
  67.         return EOF;
  68.  
  69.     optstr = av[optind++] + 1;
  70.  
  71.     if ( *optstr == '-' ) {     /* -- */
  72.         optstr++;
  73.         for ( n = 0 ; tab[n].str != NULL ; n++ ) {
  74.         if ( (p = optcmp(optstr, tab[n].str)) != NULL ) {
  75.             if ( tab[n].pfg != 0 ) {
  76.             optstr = p;
  77.             if ( *optstr == '=' )
  78.                 optstr++;
  79.  
  80.             if ( *optstr != '\0' )
  81.                 optarg = optstr;
  82.             else if ( optind < ac && *av[optind] != '-' )
  83.                 optarg = av[optind++];
  84.             else if ( tab[n].pfg != 2 ) {
  85.                 fprintf(stderr,
  86.                 "%s: option `--%s' requires an argument\n",
  87.                 progname(av[0]), tab[n].str);
  88.                 optstr = NULL;
  89.                 return '\0';
  90.             } else
  91.                 optarg = NULL;
  92.  
  93.             } else if ( *p != '\0' )
  94.             continue;
  95.  
  96.             optstr = NULL;
  97.             return tab[n].opt;
  98.         }
  99.         }
  100.  
  101.         fprintf(stderr, "%s: unrecognized option `--%s'\n",
  102.             progname(av[0]), optstr);
  103.         optstr = NULL;
  104.         return '\0';
  105.     }
  106.     }
  107. }
  108. void    insert_opt(int *acp, char ***avp, int no, char *opt)
  109. {
  110.     int     n;
  111.     int     ac;
  112.     char    **av;
  113.     char    **nv;
  114.     char    *p;
  115.  
  116.     if ( opt == NULL )
  117.     return;
  118.  
  119.     ac = *acp;
  120.     av = *avp;
  121.  
  122.     p = opt;
  123.     while ( *p != '\0' ) {
  124.     while ( isspace(*p) )
  125.         p++;
  126.     if ( *p == '\0' )
  127.         break;
  128.     ac++;
  129.     while ( *p != '\0' && !isspace(*p) )
  130.         p++;
  131.     }
  132.  
  133.     if ( *acp == ac )
  134.     return;
  135.  
  136.     if ( (nv = (char **)malloc(sizeof(char *) * ac + 1)) == NULL )
  137.     return;
  138.  
  139.     if ( (p = (char *)malloc(strlen(opt) + 1)) == NULL )
  140.     return;
  141.  
  142.     strcpy(p, opt);
  143.     for ( n = 0 ; n < no ; n++ )
  144.     nv[n] = av[n];
  145.  
  146.     while ( *p != '\0' ) {
  147.     while ( isspace(*p) )
  148.         p++;
  149.     if ( *p == '\0' )
  150.         break;
  151.     nv[n++] = p;
  152.     while ( *p != '\0' && !isspace(*p) )
  153.         p++;
  154.     if ( *p != '\0' )
  155.         *(p++) = '\0';
  156.     }
  157.  
  158.     ac = *acp;
  159.     while ( no < ac )
  160.     nv[n++] = av[no++];
  161.  
  162.     nv[n] = NULL;
  163.     *acp = n;
  164.     *avp = nv;
  165. }
  166.