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