home *** CD-ROM | disk | FTP | other *** search
/ modiromppu / modiromppu.iso / PROGRAMS / ORGPACKS / MPG12304.ZIP / GETLOPT.C < prev    next >
C/C++ Source or Header  |  1997-04-08  |  2KB  |  111 lines

  1. /*
  2.  *   getlopt.c
  3.  *
  4.  *   Oliver Fromme  <oliver.fromme@heim3.tu-clausthal.de>
  5.  *   Tue Apr  8 07:15:13 MET DST 1997
  6.  */
  7.  
  8. #include "getlopt.h"
  9.  
  10. int loptind = 1;    /* index in argv[] */
  11. int loptchr = 0;    /* index in argv[loptind] */
  12. char *loptarg;        /* points to argument if present, else to option */
  13.  
  14. topt *findopt (int islong, char *opt, topt *opts)
  15. {
  16.     if (!opts)
  17.         return (0);
  18.     while (opts->lname) {
  19.         if (islong) {
  20.             if (!strcmp(opts->lname, opt))
  21.                 return (opts);
  22.         }
  23.         else
  24.             if (opts->sname == *opt)
  25.                 return (opts);
  26.         opts++;
  27.     }
  28.     return (0);
  29. }
  30.  
  31. int performoption (int argc, char *argv[], topt *opt)
  32. {
  33.     int result = GLO_CONTINUE;
  34.  
  35.     if (!(opt->flags & 1)) /* doesn't take argument */
  36.         if (opt->var)
  37.             if (opt->flags & 2) /* var is *char */
  38.                 *((char *) opt->var) = (char) opt->value;
  39.             else
  40.                 *((int *) opt->var) = opt->value;
  41.         else
  42.             result = opt->value ? opt->value : opt->sname;
  43.     else { /* requires argument */
  44.         if (loptind >= argc)
  45.             return (GLO_NOARG);
  46.         loptarg = argv[loptind++]+loptchr;
  47.         loptchr = 0;
  48.         if (opt->var)
  49.             if (opt->flags & 2) /* var is *char */
  50.                 *((char **) opt->var) = strdup(loptarg);
  51.             else
  52.                 *((int *) opt->var) = atoi(loptarg);
  53.         else
  54.             result = opt->value ? opt->value : opt->sname;
  55.     }
  56.     if (opt->func)
  57.         opt->func(loptarg);
  58.     return (result);
  59. }
  60.  
  61. int getsingleopt (int argc, char *argv[], topt *opts)
  62. {
  63.     char *thisopt;
  64.     topt *opt;
  65.     static char shortopt[2] = {0, 0};
  66.  
  67.     if (loptind >= argc)
  68.         return (GLO_END);
  69.     thisopt = argv[loptind];
  70.     if (!loptchr) { /* start new option string */
  71.         if (thisopt[0] != '-' || !thisopt[1]) /* no more options */
  72.             return (GLO_END);
  73.         if (thisopt[1] == '-') /* "--" */
  74.             if (thisopt[2]) { /* long option */
  75.                 loptarg = thisopt+2;
  76.                 loptind++;
  77.                 if (!(opt = findopt(1, thisopt+2, opts)))
  78.                     return (GLO_UNKNOWN);
  79.                 else
  80.                     return (performoption(argc, argv, opt));
  81.             }
  82.             else { /* "--" == end of options */
  83.                 loptind++;
  84.                 return (GLO_END);
  85.             }
  86.         else /* start short option(s) */
  87.             loptchr = 1;
  88.     }
  89.     shortopt[0] = thisopt[loptchr];
  90.     loptarg = shortopt;
  91.     opt = findopt(0, thisopt+(loptchr++), opts);
  92.     if (!thisopt[loptchr]) {
  93.         loptind++;
  94.         loptchr = 0;
  95.     }
  96.     if (!opt)
  97.         return (GLO_UNKNOWN);
  98.     else
  99.         return (performoption(argc, argv, opt));
  100. }
  101.  
  102. int getlopt (int argc, char *argv[], topt *opts)
  103. {
  104.     int result;
  105.     
  106.     while ((result = getsingleopt(argc, argv, opts)) == GLO_CONTINUE);
  107.     return (result);
  108. }
  109.  
  110. /* EOF */
  111.