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