Annotation of parser3/src/targets/cgi/getopt.c, revision 1.1

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

E-mail: