Back to index

php5  5.3.10
getopt.c
Go to the documentation of this file.
00001 /* Borrowed from Apache NT Port */
00002 
00003 #include <stdio.h>
00004 #include <string.h>
00005 #include <assert.h>
00006 #include <stdlib.h>
00007 #include "getopt.h"
00008 #define OPTERRCOLON (1)
00009 #define OPTERRNF (2)
00010 #define OPTERRARG (3)
00011 
00012 
00013 char *ap_optarg;
00014 int ap_optind = 1;
00015 static int ap_opterr = 1;
00016 static int ap_optopt;
00017 
00018 static int
00019 ap_optiserr(int argc, char * const *argv, int oint, const char *optstr,
00020          int optchr, int err)
00021 {
00022     if (ap_opterr)
00023     {
00024         fprintf(stderr, "Error in argument %d, char %d: ", oint, optchr+1);
00025         switch(err)
00026         {
00027         case OPTERRCOLON:
00028             fprintf(stderr, ": in flags\n");
00029             break;
00030         case OPTERRNF:
00031             fprintf(stderr, "option not found %c\n", argv[oint][optchr]);
00032             break;
00033         case OPTERRARG:
00034             fprintf(stderr, "no argument for option %c\n", argv[oint][optchr]);
00035             break;
00036         default:
00037             fprintf(stderr, "unknown\n");
00038             break;
00039         }
00040     }
00041     ap_optopt = argv[oint][optchr];
00042     return('?');
00043 }
00044     
00045 int ap_getopt(int argc, char* const *argv, const char *optstr)
00046 {
00047     static int optchr = 0;
00048     static int dash = 0; /* have already seen the - */
00049 
00050     char *cp;
00051 
00052     if (ap_optind >= argc)
00053         return(EOF);
00054     if (!dash && (argv[ap_optind][0] !=  '-'))
00055         return(EOF);
00056     if (!dash && (argv[ap_optind][0] ==  '-') && !argv[ap_optind][1])
00057     {
00058         /*
00059          * use to specify stdin. Need to let pgm process this and
00060          * the following args
00061          */
00062         return(EOF);
00063     }
00064     if ((argv[ap_optind][0] == '-') && (argv[ap_optind][1] == '-'))
00065     {
00066         /* -- indicates end of args */
00067         ap_optind++;
00068         return(EOF);
00069     }
00070     if (!dash)
00071     {
00072         assert((argv[ap_optind][0] == '-') && argv[ap_optind][1]);
00073         dash = 1;
00074         optchr = 1;
00075     }
00076 
00077     /* Check if the guy tries to do a -: kind of flag */
00078     assert(dash);
00079     if (argv[ap_optind][optchr] == ':')
00080     {
00081         dash = 0;
00082         ap_optind++;
00083         return(ap_optiserr(argc, argv, ap_optind-1, optstr, optchr, OPTERRCOLON));
00084     }
00085     if (!(cp = strchr(optstr, argv[ap_optind][optchr])))
00086     {
00087         int errind = ap_optind;
00088         int errchr = optchr;
00089 
00090         if (!argv[ap_optind][optchr+1])
00091         {
00092             dash = 0;
00093             ap_optind++;
00094         }
00095         else
00096             optchr++;
00097         return(ap_optiserr(argc, argv, errind, optstr, errchr, OPTERRNF));
00098     }
00099     if (cp[1] == ':')
00100     {
00101         /* Check for cases where the value of the argument 
00102            is in the form -<arg> <val> or in the form -<arg><val> */
00103         dash = 0;
00104         if(!argv[ap_optind][2]) {
00105             ap_optind++;
00106             if (ap_optind == argc)
00107                 return(ap_optiserr(argc, argv, ap_optind-1, optstr, optchr, OPTERRARG));
00108             ap_optarg = argv[ap_optind++];
00109         }
00110         else
00111         {
00112             ap_optarg = &argv[ap_optind][2];
00113             ap_optind++;
00114         }
00115         return(*cp);
00116     }
00117     else
00118     {
00119         if (!argv[ap_optind][optchr+1])
00120         {
00121             dash = 0;
00122             ap_optind++;
00123         }
00124         else
00125             optchr++;
00126         return(*cp);
00127     }
00128     assert(0);
00129     return(0);
00130 }
00131 
00132 #ifdef TESTGETOPT
00133 int
00134  main (int argc, char **argv)
00135  {
00136       int c;
00137       extern char *ap_optarg;
00138       extern int ap_optind;
00139       int aflg = 0;
00140       int bflg = 0;
00141       int errflg = 0;
00142       char *ofile = NULL;
00143 
00144       while ((c = ap_getopt(argc, argv, "abo:")) != EOF)
00145            switch (c) {
00146            case 'a':
00147                 if (bflg)
00148                      errflg++;
00149                 else
00150                      aflg++;
00151                 break;
00152            case 'b':
00153                 if (aflg)
00154                      errflg++;
00155                 else
00156                      bflg++;
00157                 break;
00158            case 'o':
00159                 ofile = ap_optarg;
00160                 (void)printf("ofile = %s\n", ofile);
00161                 break;
00162            case '?':
00163                 errflg++;
00164            }
00165       if (errflg) {
00166            (void)fprintf(stderr,
00167                 "usage: cmd [-a|-b] [-o <filename>] files...\n");
00168            exit (2);
00169       }
00170       for ( ; ap_optind < argc; ap_optind++)
00171            (void)printf("%s\n", argv[ap_optind]);
00172       return 0;
00173  }
00174 
00175 #endif /* TESTGETOPT */