Debian Codegraph @

getopt.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*****************************************************************
2 * @LICENSE@
3 *****************************************************************/
4
5 /* CVS $Id: getopt.c,v 1.8 2003/04/14 16:00:16 eddy Exp $
6 */
7
8 #include "squidconf.h"
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13
14 #include "squid.h"
15
16 /* Function: Getopt()
17 *
18 * Purpose: Portable command line option parsing with abbreviated
19 * option switches. Replaces UNIX getopt(). Using UNIX getopt()
20 * hinders portability to non-UNIX platforms, and getopt()
21 * is also limited to single letter options.
22 *
23 * Getopt() implements a superset of UNIX getopt().
24 * All of getopt()'s single-character switch behavior
25 * is emulated, and "--" by itself terminates the options.
26 * Additionally, Getopt() provides extended switches
27 * like "--youroptionhere", and Getopt() type checks
28 * arguments.
29 *
30 * Extended options must start with "--", as in "--option1".
31 * Normal options must start with "-", as in "-o".
32 * Normal options may be concatenated, as in "-a -b" == "-ab".
33 *
34 * See bottom of this .c file after #fdef GETOPT_TESTDRIVER
35 * for an example of calling Getopt().
36 *
37 * Args: argc - from main(). number of elems in argv.
38 * argv - from main(). argv[0] is the name of the command.
39 * opt - array of opt_s structures, defining option switches
40 * nopts - number of switches in opt
41 * usage - a (possibly long) string to print if usage error.
42 * ret_optind - RETURN: the index in argv[] of the next
43 * valid command-line token.
44 * ret_optname- RETURN: ptr to the name of option switch
45 * seen, or NULL if no option was seen.
46 * ret_optarg - RETURN: ptr to the optional argument, if any;
47 * NULL if option takes no argument.
48 *
49 * Return: 1 if a valid option was parsed.
50 * 0 if no option was found, and command-line parsing is complete.
51 * Die()'s here if an error is detected.
52 */
53 int
54 Getopt(int argc, char **argv, struct opt_s *opt, int nopts, char *usage,
55 int *ret_optind, char **ret_optname, char **ret_optarg)
56 {
57 int i;
58 int arglen;
59 int nmatch;
60 static int optind = 1; /* init to 1 on first call */
61 static char *optptr = NULL; /* ptr to next valid switch */
62 int opti = 0; /* init only to silence gcc uninit warnings */
63
64 /* Check to see if we've run out of options.
65 * A '-' by itself is an argument (e.g. "read from stdin")
66 * not an option.
67 */
68 if (optind >= argc || argv[optind][0] != '-' || strcmp(argv[optind], "-") == 0)
69 {
70 *ret_optind = optind;
71 *ret_optarg = NULL;
72 *ret_optname = NULL;
73 return 0;
74 }
75
76 /* Check to see if we're being told that this is the end
77 * of the options with the special "--" flag.
78 */
79 if (strcmp(argv[optind], "--") == 0)
80 {
81 optind++;
82 *ret_optind = optind;
83 *ret_optname = NULL;
84 *ret_optarg = NULL;
85 return 0;
86 }
87
88 /* We have a real option. Find which one it is.
89 * We handle single letter switches "-o" separately
90 * from full switches "--option", based on the "-" vs. "--"
91 * prefix -- single letter switches can be concatenated
92 * as long as they don't have arguments.
93 */
94 /* full option */
95 if (optptr == NULL && strncmp(argv[optind], "--", 2) == 0)
96 {
97 /* Use optptr to parse argument in options of form "--foo=666"
98 */
99 if ((optptr = strchr(argv[optind], '=')) != NULL)
100 { *optptr = '\0'; optptr++; }
101
102 arglen = strlen(argv[optind]);
103 nmatch = 0;
104 for (i = 0; i < nopts; i++)
105 if (opt[i].single == FALSE &&
106 strncmp(opt[i].name, argv[optind], arglen) == 0)
107 {
108 nmatch++;
109 opti = i;
110 if (arglen == strlen(opt[i].name)) break; /* exact match, stop now */
111 }
•112 if (nmatch > 1 // got several prefix matches
113 && i == nopts) // with none of them being an exact match
114 Die("Option \"%s\" is ambiguous; please be more specific.\n%s",
115 argv[optind], usage);
116 if (nmatch == 0)
117 Die("No such option \"%s\".\n%s", argv[optind], usage);
118
119 *ret_optname = opt[opti].name;
120
121 /* Set the argument, if there is one
122 */
123 if (opt[opti].argtype != sqdARG_NONE)
124 {
125 if (optptr != NULL)
126 { /* --foo=666 style */
127 *ret_optarg = optptr;
128 optptr = NULL;
129 optind++;
130 }
131 else if (optind+1 >= argc)
132 Die("Option %s requires an argument\n%s", opt[opti].name, usage);
133 else /* "--foo 666" style */
134 {
135 *ret_optarg = argv[optind+1];
136 optind+=2;
137 }
138 }
139 else /* sqdARG_NONE */
140 {
141 if (optptr != NULL)
142 Die("Option %s does not take an argument\n%s", opt[opti].name, usage);
143 *ret_optarg = NULL;
144 optind++;
145 }
146 }
147 else /* else, a single letter option "-o" */
148 {
149 /* find the option */
150 if (optptr == NULL)
151 optptr = argv[optind]+1;
152 for (opti = -1, i = 0; i < nopts; i++)
153 if (opt[i].single == TRUE && *optptr == opt[i].name[1])
154 { opti = i; break; }
155 if (opti == -1)
156 Die("No such option \"%c\".\n%s", *optptr, usage);
157 *ret_optname = opt[opti].name;
158
159 /* set the argument, if there is one */
160 if (opt[opti].argtype != sqdARG_NONE)
161 {
162 if (*(optptr+1) != '\0') /* attached argument */
163 {
164 *ret_optarg = optptr+1;
165 optind++;
166 }
167 else if (optind+1 < argc) /* unattached argument */
168 {
169 *ret_optarg = argv[optind+1];
170 optind+=2;
171 }
172 else Die("Option %s requires an argument\n%s", opt[opti].name, usage);
173
174 optptr = NULL; /* can't concatenate after an argument */
175 }
176 else /* sqdARG_NONE */
177 {
178 *ret_optarg = NULL;
179 if (*(optptr+1) != '\0') /* concatenation */
180 optptr++;
181 else
182 {
183 optind++; /* move to next field */
184 optptr = NULL;
185 }
186 }
187
188 }
189
190 /* Type check the argument, if there is one
191 */
192 if (opt[opti].argtype != sqdARG_NONE)
193 {
194 if (opt[opti].argtype == sqdARG_INT && ! IsInt(*ret_optarg))
195 Die("Option %s requires an integer argument\n%s",
196 opt[opti].name, usage);
197 else if (opt[opti].argtype == sqdARG_FLOAT && ! IsReal(*ret_optarg))
198 Die("Option %s requires a numerical argument\n%s",
199 opt[opti].name, usage);
200 else if (opt[opti].argtype == sqdARG_CHAR && strlen(*ret_optarg) != 1)
201 Die("Option %s requires a single-character argument\n%s",
202 opt[opti].name, usage);
203 /* sqdARG_STRING is always ok, no type check necessary */
204 }
205
206 *ret_optind = optind;
207 return 1;
208 }
209
210
211
212 #ifdef GETOPT_TESTDRIVER
213 /* cc -DGETOPT_TESTDRIVER -L ~/lib/squid.linux/ getopt.c -lsquid
214 */
215 struct opt_s OPTIONS[] = {
216 { "--test1", FALSE, sqdARG_INT },
217 { "--test2", FALSE, sqdARG_FLOAT },
218 { "--test3", FALSE, sqdARG_STRING },
219 { "--test4", FALSE, sqdARG_CHAR },
220 { "-a", TRUE, sqdARG_NONE },
221 { "-b", TRUE, sqdARG_INT },
222 };
223 #define NOPTIONS (sizeof(OPTIONS) / sizeof(struct opt_s))
224
225 int
226 main(int argc, char **argv)
227 {
228 int optind;
229 char *optarg;
230 char *optname;
231
232 while (Getopt(argc, argv, OPTIONS, NOPTIONS, "Usage/help here",
233 &optind, &optname, &optarg))
234 {
235 printf("Option: index: %d name: %s argument: %s\n",
236 optind, optname, optarg);
237 }
238 while (optind < argc)
239 {
240 printf("Argument: index: %d name: %s\n", optind, argv[optind]);
241 optind++;
242 }
243
244
245 }
246
247
248 #endif /*GETOPT_TESTDRIVER*/