Annotation of embedaddon/php/sapi/isapi/stresstest/getopt.c, revision 1.1.1.1

1.1       misho       1: /* Borrowed from Apache NT Port */
                      2: 
                      3: #include <stdio.h>
                      4: #include <string.h>
                      5: #include <assert.h>
                      6: #include <stdlib.h>
                      7: #include "getopt.h"
                      8: #define OPTERRCOLON (1)
                      9: #define OPTERRNF (2)
                     10: #define OPTERRARG (3)
                     11: 
                     12: 
                     13: char *ap_optarg;
                     14: int ap_optind = 1;
                     15: static int ap_opterr = 1;
                     16: static int ap_optopt;
                     17: 
                     18: static int
                     19: ap_optiserr(int argc, char * const *argv, int oint, const char *optstr,
                     20:          int optchr, int err)
                     21: {
                     22:     if (ap_opterr)
                     23:     {
                     24:         fprintf(stderr, "Error in argument %d, char %d: ", oint, optchr+1);
                     25:         switch(err)
                     26:         {
                     27:         case OPTERRCOLON:
                     28:             fprintf(stderr, ": in flags\n");
                     29:             break;
                     30:         case OPTERRNF:
                     31:             fprintf(stderr, "option not found %c\n", argv[oint][optchr]);
                     32:             break;
                     33:         case OPTERRARG:
                     34:             fprintf(stderr, "no argument for option %c\n", argv[oint][optchr]);
                     35:             break;
                     36:         default:
                     37:             fprintf(stderr, "unknown\n");
                     38:             break;
                     39:         }
                     40:     }
                     41:     ap_optopt = argv[oint][optchr];
                     42:     return('?');
                     43: }
                     44:     
                     45: int ap_getopt(int argc, char* const *argv, const char *optstr)
                     46: {
                     47:     static int optchr = 0;
                     48:     static int dash = 0; /* have already seen the - */
                     49: 
                     50:     char *cp;
                     51: 
                     52:     if (ap_optind >= argc)
                     53:         return(EOF);
                     54:     if (!dash && (argv[ap_optind][0] !=  '-'))
                     55:         return(EOF);
                     56:     if (!dash && (argv[ap_optind][0] ==  '-') && !argv[ap_optind][1])
                     57:     {
                     58:         /*
                     59:          * use to specify stdin. Need to let pgm process this and
                     60:          * the following args
                     61:          */
                     62:         return(EOF);
                     63:     }
                     64:     if ((argv[ap_optind][0] == '-') && (argv[ap_optind][1] == '-'))
                     65:     {
                     66:         /* -- indicates end of args */
                     67:         ap_optind++;
                     68:         return(EOF);
                     69:     }
                     70:     if (!dash)
                     71:     {
                     72:         assert((argv[ap_optind][0] == '-') && argv[ap_optind][1]);
                     73:         dash = 1;
                     74:         optchr = 1;
                     75:     }
                     76: 
                     77:     /* Check if the guy tries to do a -: kind of flag */
                     78:     assert(dash);
                     79:     if (argv[ap_optind][optchr] == ':')
                     80:     {
                     81:         dash = 0;
                     82:         ap_optind++;
                     83:         return(ap_optiserr(argc, argv, ap_optind-1, optstr, optchr, OPTERRCOLON));
                     84:     }
                     85:     if (!(cp = strchr(optstr, argv[ap_optind][optchr])))
                     86:     {
                     87:         int errind = ap_optind;
                     88:         int errchr = optchr;
                     89: 
                     90:         if (!argv[ap_optind][optchr+1])
                     91:         {
                     92:             dash = 0;
                     93:             ap_optind++;
                     94:         }
                     95:         else
                     96:             optchr++;
                     97:         return(ap_optiserr(argc, argv, errind, optstr, errchr, OPTERRNF));
                     98:     }
                     99:     if (cp[1] == ':')
                    100:     {
                    101:         /* Check for cases where the value of the argument 
                    102:            is in the form -<arg> <val> or in the form -<arg><val> */
                    103:         dash = 0;
                    104:         if(!argv[ap_optind][2]) {
                    105:             ap_optind++;
                    106:             if (ap_optind == argc)
                    107:                 return(ap_optiserr(argc, argv, ap_optind-1, optstr, optchr, OPTERRARG));
                    108:             ap_optarg = argv[ap_optind++];
                    109:         }
                    110:         else
                    111:         {
                    112:             ap_optarg = &argv[ap_optind][2];
                    113:             ap_optind++;
                    114:         }
                    115:         return(*cp);
                    116:     }
                    117:     else
                    118:     {
                    119:         if (!argv[ap_optind][optchr+1])
                    120:         {
                    121:             dash = 0;
                    122:             ap_optind++;
                    123:         }
                    124:         else
                    125:             optchr++;
                    126:         return(*cp);
                    127:     }
                    128:     assert(0);
                    129:     return(0);
                    130: }
                    131: 
                    132: #ifdef TESTGETOPT
                    133: int
                    134:  main (int argc, char **argv)
                    135:  {
                    136:       int c;
                    137:       extern char *ap_optarg;
                    138:       extern int ap_optind;
                    139:       int aflg = 0;
                    140:       int bflg = 0;
                    141:       int errflg = 0;
                    142:       char *ofile = NULL;
                    143: 
                    144:       while ((c = ap_getopt(argc, argv, "abo:")) != EOF)
                    145:            switch (c) {
                    146:            case 'a':
                    147:                 if (bflg)
                    148:                      errflg++;
                    149:                 else
                    150:                      aflg++;
                    151:                 break;
                    152:            case 'b':
                    153:                 if (aflg)
                    154:                      errflg++;
                    155:                 else
                    156:                      bflg++;
                    157:                 break;
                    158:            case 'o':
                    159:                 ofile = ap_optarg;
                    160:                 (void)printf("ofile = %s\n", ofile);
                    161:                 break;
                    162:            case '?':
                    163:                 errflg++;
                    164:            }
                    165:       if (errflg) {
                    166:            (void)fprintf(stderr,
                    167:                 "usage: cmd [-a|-b] [-o <filename>] files...\n");
                    168:            exit (2);
                    169:       }
                    170:       for ( ; ap_optind < argc; ap_optind++)
                    171:            (void)printf("%s\n", argv[ap_optind]);
                    172:       return 0;
                    173:  }
                    174: 
                    175: #endif /* TESTGETOPT */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>