b91e2391 |
1 | /* -*-c-*- |
2 | * |
379c3233 |
3 | * $Id: mdwopt.c,v 1.5 1999/05/19 20:23:59 mdw Exp $ |
b91e2391 |
4 | * |
5 | * Options parsing, similar to GNU @getopt_long@ |
6 | * |
9cecacb1 |
7 | * (c) 1996 Straylight/Edgeware |
b91e2391 |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of many programs. |
13 | * |
14 | * `mdwopt' is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU Library General Public License as |
16 | * published by the Free Software Foundation; either version 2 of the |
17 | * License, or (at your option) any later version. |
18 | * |
19 | * `mdwopt' is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU Library General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU Library General Public |
4033d96b |
25 | * License along with `mdwopt'; if not, write to the Free |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
27 | * MA 02111-1307, USA. |
b91e2391 |
28 | */ |
29 | |
30 | /*----- Revision history --------------------------------------------------* |
31 | * |
32 | * $Log: mdwopt.c,v $ |
379c3233 |
33 | * Revision 1.5 1999/05/19 20:23:59 mdw |
34 | * Change naming to match newer mLib conventions. |
35 | * |
9cecacb1 |
36 | * Revision 1.4 1999/05/15 10:25:38 mdw |
37 | * Fix copyright information. |
38 | * |
4033d96b |
39 | * Revision 1.3 1999/05/14 18:51:42 mdw |
40 | * Reformat the LGPL notice slightly. |
41 | * |
5b59e227 |
42 | * Revision 1.2 1999/05/13 22:57:23 mdw |
43 | * Change `-ise' to `-ize' throughout. |
44 | * |
45 | * Revision 1.1.1.1 1999/05/05 19:23:47 mdw |
46 | * New import. The old CVS repository was lost in a disk disaster. |
b91e2391 |
47 | * |
48 | * --- Previous lives --- |
49 | * |
50 | * %Log: mdwopt.c,v % |
51 | * Revision 1.7 1997/09/11 09:19:11 mdw |
52 | * (mo__nextWord): Arrrgh. Don't free the environment variable buffer! |
53 | * People are still using it! |
54 | * |
55 | * Revision 1.6 1997/09/11 09:05:54 mdw |
56 | * (mo__nextWord): Fix bug which returns too many words from environment |
57 | * variables. |
58 | * |
59 | * Revision 1.5 1997/08/09 20:27:59 mdw |
60 | * Fix spelling of `Licensing'. |
61 | * |
62 | * Revision 1.4 1997/07/29 21:11:35 mdw |
63 | * Reformatted. Fixed buffer overflow when dealing with environment |
64 | * variables. Included NT in list of daft operating systems with `\' as a |
65 | * path separator. Fixed address of the FSF. |
66 | * |
67 | * Revision 1.3 1997/02/26 00:41:10 mdw |
68 | * Added GPL notice to the top. Slight formatting changes. |
69 | * |
70 | * Revision 1.2 1996/10/28 13:12:13 mdw |
71 | * Fixed calls to ctype.h routines. Arguments are cast to unsigned char |
72 | * to avoid invoking undefined behaviour caused by signedness of chars. |
73 | * |
74 | * Revision 1.1 1996/09/24 18:01:28 mdw |
75 | * Initial revision |
76 | * |
77 | */ |
78 | |
79 | /*----- External dependencies ---------------------------------------------*/ |
80 | |
81 | #include <ctype.h> |
82 | #include <stdio.h> |
83 | #include <stdlib.h> |
84 | #include <string.h> |
85 | |
86 | #include "mdwopt.h" |
87 | |
88 | /*----- Configuration things ----------------------------------------------*/ |
89 | |
90 | #if defined(__riscos) |
91 | # define PATHSEP '.' |
92 | #elif defined(__OS2__) || defined(__MSDOS__) || defined(__WINNT__) |
93 | # define PATHSEP '\\' |
94 | #else /* Assume a sane filing system */ |
95 | # define PATHSEP '/' |
96 | #endif |
97 | |
98 | /*----- Global variables --------------------------------------------------*/ |
99 | |
100 | mdwopt_data mdwopt_global = {0, 0, 0, 1, 0, 0, 0, 0, 0}; |
101 | |
102 | enum { |
379c3233 |
103 | ORD_PERMUTE = 0, /* Permute the options (default) */ |
104 | ORD_RETURN = 1, /* Return non-option things */ |
105 | ORD_POSIX = 2, /* Do POSIX-type hacking */ |
106 | ORD_NEGATE = 4 /* Magic negate-next-thing flag */ |
b91e2391 |
107 | }; |
108 | |
109 | /*----- Main code ---------------------------------------------------------*/ |
110 | |
379c3233 |
111 | /* --- @nextWord@ --- * |
b91e2391 |
112 | * |
113 | * Arguments: @int argc@ = number of command line options |
114 | * @char *argv[]@ = pointer to command line options |
115 | * @mdwopt_data *data@ = pointer to persistent state |
116 | * |
117 | * Returns: Pointer to the next word to handle, or 0 |
118 | * |
119 | * Use: Extracts the next word from the command line or environment |
120 | * variable. |
121 | */ |
122 | |
379c3233 |
123 | static char *nextWord(int argc, char *const *argv, mdwopt_data *data) |
b91e2391 |
124 | { |
125 | if (data->ind == -1) { |
126 | char *p = data->env; |
127 | char *q; |
128 | while (isspace((unsigned char)*p)) |
129 | p++; |
130 | q = p; |
131 | while (*p && !isspace((unsigned char)*p)) |
132 | p++; |
133 | data->env = p; |
134 | if (*p) |
135 | *p++ = 0; |
136 | if (p != q) |
137 | return (q); |
138 | data->env = 0; |
139 | data->ind = 1; |
140 | } |
141 | |
142 | if (data->next == argc) |
143 | return (0); |
144 | return (argv[data->next++]); |
145 | } |
146 | |
379c3233 |
147 | /* --- @permute@ --- * |
b91e2391 |
148 | * |
149 | * Arguments: @char *argv[]@ = pointer to command line arguments |
150 | * @mdwopt_data *data@ = pointer to persistent data |
151 | * |
152 | * Returns: -- |
153 | * |
154 | * Use: Moves a command line option into the right place. |
155 | */ |
156 | |
379c3233 |
157 | static void permute(char *const *argv, mdwopt_data *data) |
b91e2391 |
158 | { |
159 | char **v = (char **)argv; |
160 | if (data->ind != -1) { |
161 | int i = data->next - 1; |
162 | char *p = v[i]; |
163 | while (i > data->ind) { |
164 | v[i] = v[i - 1]; |
165 | i--; |
166 | } |
167 | v[i] = p; |
168 | data->ind++; |
169 | } |
170 | } |
171 | |
379c3233 |
172 | /* --- @findOpt@ --- * |
b91e2391 |
173 | * |
174 | * Arguments: @int o@ = which option to search for |
175 | * @const char *shortopt@ = short options string to search |
176 | * @mdwopt_data *data@ = pointer to persistant state |
177 | * |
178 | * Returns: Pointer to rest of short options string (including magic |
179 | * characters) |
180 | * |
181 | * Use: Looks up a short option in the given string. |
182 | */ |
183 | |
379c3233 |
184 | static const char *findOpt(int o, const char *shortopt, |
b91e2391 |
185 | mdwopt_data *data) |
186 | { |
187 | const char *p = shortopt; /* Point to short opts table */ |
188 | for (;;) { |
189 | if (!*p) /* No more options left */ |
190 | return (0); |
191 | |
379c3233 |
192 | if (o != *p || (p[1] != '+' && data->order & ORD_NEGATE)) { |
b91e2391 |
193 | p++; /* Skip this option entry */ |
194 | while (*p == '+') /* Jump a `%|+|%' sign */ |
195 | p++; |
196 | while (*p == ':') /* And jump any `%|:|%' characters */ |
197 | p++; /* Just in case there are any */ |
198 | } |
199 | else |
200 | return (p + 1); |
201 | } |
202 | } |
203 | |
204 | /* --- @mdwopt@ --- * |
205 | * |
206 | * Arguments: @int argc@ = number of command line arguments |
207 | * @char * const *argv@ = pointer to command line arguments |
208 | * @const char *shortopt@ = pointer to short options information |
209 | * @const struct option *longopts@ = pointer to long opts info |
210 | * @int *longind@ = where to store matched longopt |
211 | * @mdwopt_data *data@ = persistent state for the parser |
212 | * @int flags@ = various useful flags |
213 | * |
214 | * Returns: Value of option found next, or an error character, or |
215 | * @EOF@ for the last thing. |
216 | * |
217 | * Use: Reads options. The routine should be more-or-less compatible |
218 | * with standard getopts, although it provides many more |
219 | * features even than the standard GNU implementation. |
220 | * |
221 | * The precise manner of options parsing is determined by |
222 | * various flag settings, which are described below. By setting |
223 | * flag values appropriately, you can achieve behaviour very |
224 | * similar to most other getopt routines. |
225 | * |
226 | * |
227 | * How options parsing appears to users |
228 | * |
229 | * A command line consists of a number of `words' (which may |
230 | * contain spaces, according to various shell quoting |
231 | * conventions). A word may be an option, an argument to an |
232 | * option, or a non-option. An option begins with a special |
233 | * character, usually `%|-|%', although `%|+|%' is also used |
234 | * sometimes. As special exceptions, the word containing only a |
235 | * `%|-|%' is considered to be a non-option, since it usually |
236 | * represents standard input or output as a filename, and the |
237 | * word containing a double-dash `%|--|%' is used to mark all |
238 | * following words as being non-options regardless of their |
239 | * initial character. |
240 | * |
241 | * Traditionally, all words after the first non-option have been |
242 | * considered to be non-options automatically, so that options |
243 | * must be specified before filenames. However, this |
244 | * implementation can extract all the options from the command |
245 | * line regardless of their position. This can usually be |
246 | * disabled by setting one of the environment variables |
247 | * `%|POSIXLY_CORRECT|%' or `%|_POSIX_OPTION_ORDER|%'. |
248 | * |
249 | * There are two different styles of options: `short' and |
250 | * `long'. |
251 | * |
252 | * Short options are the sort which Unix has known for ages: an |
253 | * option is a single letter, preceded by a `%|-|%'. Short |
254 | * options can be joined together to save space (and possibly to |
255 | * make silly words): e.g., instead of giving options |
256 | * `%|-x -y|%', a user could write `%|-xy|%'. Some short |
257 | * options can have arguments, which appear after the option |
258 | * letter, either immediately following, or in the next `word' |
259 | * (so an option with an argument could be written as |
260 | * `%|-o foo|%' or as `%|-ofoo|%'). Note that options with |
261 | * optional arguments must be written in the second style. |
262 | * |
263 | * When a short option controls a flag setting, it is sometimes |
264 | * possible to explicitly turn the flag off, as well as turning |
265 | * it on, (usually to override default options). This is |
266 | * usually done by using a `%|+|%' instead of a `%|-|%' to |
267 | * introduce the option. |
268 | * |
5b59e227 |
269 | * Long options, as popularized by the GNU utilities, are given |
b91e2391 |
270 | * long-ish memorable names, preceded by a double-dash `%|--|%'. |
271 | * Since their names are more than a single character, long |
272 | * options can't be combined in the same way as short options. |
273 | * Arguments to long options may be given either in the same |
274 | * `word', separated from the option name by an equals sign, or |
275 | * in the following `word'. |
276 | * |
277 | * Long option names can be abbreviated if necessary, as long |
278 | * as the abbreviation is unique. This means that options can |
279 | * have sensible and memorable names but still not require much |
280 | * typing from an experienced user. |
281 | * |
282 | * Like short options, long options can control flag settings. |
283 | * The options to manipulate these settings come in pairs: an |
284 | * option of the form `%|--set-flag|%' might set the flag, while |
285 | * an option of the form `%|--no-set-flag|%' might clear it. |
286 | * |
287 | * It is usual for applications to provide both short and long |
288 | * options with identical behaviour. Some applications with |
289 | * lots of options may only provide long options (although they |
290 | * will often be only two or three characters long). In this |
291 | * case, long options can be preceded with a single `%|-|%' |
292 | * character, and negated by a `%|+|%' character. |
293 | * |
294 | * Finally, some (older) programs accept arguments of the form |
295 | * `%%@.{"-"<number>}%%', to set some numerical parameter, |
296 | * typically a line count of some kind. |
297 | * |
298 | * |
299 | * How programs parse options |
300 | * |
301 | * An application parses its options by calling mdwopt |
302 | * repeatedly. Each time it is called, mdwopt returns a value |
303 | * describing the option just read, and stores information about |
304 | * the option in a data block. The value %$-1$% is returned |
305 | * when there are no more options to be read. The `%|?|%' |
306 | * character is returned when an error is encountered. |
307 | * |
308 | * Before starting to parse options, the value @data->ind@ must |
309 | * be set to 0 or 1. The value of @data->err@ can also be set, |
310 | * to choose whether errors are reported by mdwopt. |
311 | * |
312 | * The program's `@argc@' and `@argv@' arguments are passed to |
313 | * the options parser, so that it can read the command line. A |
314 | * flags word is also passed, allowing the program fine control |
315 | * over parsing. The flags are described above. |
316 | * |
317 | * Short options are described by a string, which once upon a |
318 | * time just contained the permitted option characters. Now the |
319 | * options string begins with a collection of flag characters, |
320 | * and various flag characters can be put after options |
321 | * characters to change their properties. |
322 | * |
323 | * If the first character of the short options string is |
324 | * `%|+|%', `%|-|%' or `%|!|%', the order in which options are |
325 | * read is modified, as follows: |
326 | * |
327 | * `%|+|%' forces the POSIX order to be used. As soon as a non- |
328 | * option is found, mdwopt returns %$-1$%. |
329 | * |
330 | * `%|-|%' makes mdwopt treat non-options as being `special' |
331 | * sorts of option. When a non-option word is found, the |
332 | * value 0 is returned, and the actual text of the word |
333 | * is stored as being the option's argument. |
334 | * |
335 | * `%|!|%' forces the default order to be used. The entire |
336 | * command line is scanned for options, which are |
337 | * returned in order. However, during this process, |
338 | * the options are moved in the @argv@ array, so that |
339 | * they appear before the non- options. |
340 | * |
341 | * A `%|:|%' character may be placed after the ordering flag (or |
342 | * at the very beginning if no ordering flag is given) which |
343 | * indicates that the character `%|:|%', rather than `%|?|%', |
344 | * should be returned if a missing argument error is detected. |
345 | * |
346 | * Each option in the string can be followed by a `%|+|%' sign, |
347 | * indicating that it can be negated, a `%|:|%' sign indicating |
348 | * that it requires an argument, or a `%|::|%' string, |
349 | * indicating an optional argument. Both `%|+|%' and `%|:|%' or |
350 | * `%|::|%' may be given, although the `%|+|%' must come first. |
351 | * |
352 | * If an option is found, the option character is returned to |
353 | * the caller. A pointer to an argument is stored in |
354 | * @data->arg@, or @NULL@ is stored if there was no argument. |
355 | * If a negated option was found, the option character is |
379c3233 |
356 | * returned ORred with @OPTF_NEGATED@ (bit 8 set). |
b91e2391 |
357 | * |
358 | * Long options are described in a table. Each entry in the |
359 | * table is of type @struct option@, and the table is terminated |
360 | * by an entry whose @name@ field is null. Each option has |
361 | * a flags word which, due to historical reasons, is called |
362 | * @has_arg@. This describes various properties of the option, |
363 | * such as what sort of argument it takes, and whether it can |
364 | * be negated. |
365 | * |
366 | * When mdwopt finds a long option, it looks the name up in the |
367 | * table. The index of the matching entry is stored in the |
368 | * @longind@ variable, passed to mdwopt (unless @longind@ is 0): |
369 | * a value of %$-1$% indicates that no long option was |
370 | * found. The behaviour is then dependent on the values in the |
371 | * table entry. If @flag@ is nonzero, it points to an integer |
372 | * to be modified by mdwopt. Usually the value in the @val@ |
373 | * field is simply stored in the @flag@ variable. If the flag |
379c3233 |
374 | * @OPTF_SWITCH@ is set, however, the value is combined with |
b91e2391 |
375 | * the existing value of the flags using a bitwise OR. If |
379c3233 |
376 | * @OPTF_NEGATE@ is set, then the flag bit will be cleared if a |
b91e2391 |
377 | * matching negated long option is found. The value 0 is |
378 | * returned. |
379 | * |
380 | * If @flag@ is zero, the value in @val@ is returned by mdwopt, |
381 | * possibly with bit 8 set if the option was negated. |
382 | * |
383 | * Arguments for long options are stored in @data->arg@, as |
384 | * before. |
385 | * |
386 | * Numeric options, if enabled, cause the value `%|#|%' to be |
387 | * returned, and the numeric value to be stored in @data->opt@. |
388 | * |
379c3233 |
389 | * If the flag @OPTF_ENVVAR@ is set on entry, options will be |
b91e2391 |
390 | * extracted from an environment variable whose name is built by |
391 | * capitalising all the letters of the program's name. (This |
392 | * allows a user to have different default settings for a |
393 | * program, by calling it through different symbolic links.) */ |
394 | |
395 | int mdwopt(int argc, char *const *argv, |
396 | const char *shortopt, |
397 | const struct option *longopts, int *longind, |
398 | mdwopt_data *data, int flags) |
399 | { |
400 | /* --- Local variables --- */ |
401 | |
402 | char *p, *q, *r; /* Some useful things to have */ |
403 | char *prefix; /* Prefix from this option */ |
404 | int i; /* Always useful */ |
405 | char noarg = '?'; /* Standard missing-arg char */ |
406 | |
407 | /* --- Sort out our data --- */ |
408 | |
409 | if (!data) /* If default data requested */ |
410 | data = &mdwopt_global; /* Then use the global stuff */ |
411 | |
412 | /* --- See if this is the first time --- */ |
413 | |
379c3233 |
414 | if (data->ind == 0 || (data->ind == 1 && ~flags & OPTF_NOPROGNAME)) { |
b91e2391 |
415 | |
416 | /* --- Sort out default returning order --- */ |
417 | |
418 | if (getenv("_POSIX_OPTION_ORDER") || /* Examine environment for opts */ |
419 | getenv("POSIXLY_CORRECT")) /* To see if we disable features */ |
379c3233 |
420 | data->order = ORD_POSIX; /* If set, use POSIX ordering */ |
b91e2391 |
421 | else |
379c3233 |
422 | data->order = ORD_PERMUTE; /* Otherwise mangle the options */ |
b91e2391 |
423 | |
424 | /* --- Now see what the caller actually wants --- */ |
425 | |
426 | switch (shortopt[0]) { /* Look at the first character */ |
427 | case '-': /* `%|-|%' turns on in-orderness */ |
379c3233 |
428 | data->order = ORD_RETURN; |
b91e2391 |
429 | break; |
430 | case '+': /* `%|+|%' turns on POSIXness */ |
379c3233 |
431 | data->order = ORD_POSIX; |
b91e2391 |
432 | break; |
433 | case '!': /* `%|!|%' ignores POSIXness */ |
379c3233 |
434 | data->order = ORD_PERMUTE; |
b91e2391 |
435 | break; |
436 | } |
437 | |
438 | /* --- Now decide on the program's name --- */ |
439 | |
379c3233 |
440 | if (~flags & OPTF_NOPROGNAME) { |
b91e2391 |
441 | p = q = (char *)argv[0]; |
442 | while (*p) { |
443 | if (*p++ == PATHSEP) |
444 | q = p; |
445 | } |
446 | data->prog = q; |
447 | |
448 | data->ind = data->next = 1; |
449 | data->list = 0; |
450 | |
451 | /* --- See about environment variables --- * |
452 | * |
453 | * Be careful. The program may be setuid, and an attacker might have |
454 | * given us a long name in @argv[0]@. If the name is very long, don't |
455 | * support this option. |
456 | */ |
457 | |
379c3233 |
458 | if (flags & OPTF_ENVVAR && strlen(data->prog) < 48) { |
b91e2391 |
459 | |
460 | char buf[64]; |
461 | |
462 | /* --- For RISC OS, support a different format --- * |
463 | * |
464 | * Acorn's RISC OS tends to put settings in variables named |
465 | * `App$Options' rather than `APP'. Under RISC OS, I'll support |
466 | * both methods, just to avoid confuddlement. |
467 | */ |
468 | |
469 | #ifdef __riscos |
470 | sprintf(buf, "%s$Options", data->prog); |
471 | p = getenv(buf); |
472 | if (!p) { |
473 | #endif |
474 | |
475 | p = buf; /* Point to a buffer */ |
476 | q = data->prog; /* Point to program name */ |
477 | while (*q) /* While characters left here */ |
478 | *p++ = toupper(*q++); /* Copy and uppercase */ |
479 | *p++ = 0; /* Terminate my copy of this */ |
480 | p = getenv(buf); /* Get the value of the variable */ |
481 | |
482 | #ifdef __riscos |
483 | } |
484 | #endif |
485 | |
486 | /* --- Copy the options string into a buffer --- */ |
487 | |
488 | if (p) { /* If it is defined */ |
489 | q = malloc(strlen(p) + 1); /* Allocate space for a copy */ |
490 | if (!q) { /* If that failed */ |
491 | fprintf(stderr, /* Report a nice error */ |
492 | "%s: Not enough memory to read settings in " |
493 | "environment variable\n", |
494 | data->prog); |
495 | } else { /* Otherwise */ |
496 | strcpy(q, p); /* Copy the text over */ |
497 | data->ind = -1; /* Mark that we're parsing envvar */ |
498 | data->env = data->estart = q; /* And store the pointer away */ |
499 | } |
500 | } |
501 | |
502 | } |
503 | } |
504 | else |
505 | data->ind = data->next = 0; |
506 | } |
507 | |
508 | /* --- Do some initial bodgery --- * |
509 | * |
510 | * The @shortopt@ string can have some interesting characters at the |
511 | * beginning. We'll skip past them. |
512 | */ |
513 | |
514 | switch (shortopt[0]) { |
515 | case '+': |
516 | case '-': |
517 | case '!': |
518 | shortopt++; |
519 | break; |
520 | } |
521 | |
522 | if (shortopt[0] == ':') { |
523 | noarg = shortopt[0]; |
524 | shortopt++; |
525 | } |
526 | |
527 | if (longind) /* Allow longind to be null */ |
528 | *longind = -1; /* Clear this to avoid confusion */ |
529 | data->opt = -1; /* And this too */ |
530 | data->arg = 0; /* No option set up here */ |
531 | |
532 | /* --- Now go off and search for an option --- */ |
533 | |
534 | if (!data->list || !*data->list) { |
535 | data->order &= 3; /* Clear negation flag */ |
536 | |
537 | /* --- Now we need to find the next option --- * |
538 | * |
539 | * Exactly how we do this depends on the settings of the order variable. |
540 | * We identify options as being things starting with `%|-|%', and which |
541 | * aren't equal to `%|-|%' or `%|--|%'. We'll look for options until: |
542 | * |
379c3233 |
543 | * * We find something which isn't an option AND @order == ORD_POSIX@ |
b91e2391 |
544 | * * We find a `%|--|%' |
545 | * * We reach the end of the list |
546 | * |
547 | * There are some added little wrinkles, which we'll meet as we go. |
548 | */ |
549 | |
550 | for (;;) { /* Keep looping for a while */ |
379c3233 |
551 | p = nextWord(argc, argv, data); /* Get the next word out */ |
b91e2391 |
552 | if (!p) /* If there's no next word */ |
553 | return (EOF); /* There's no more now */ |
554 | |
555 | /* --- See if we've found an option --- */ |
556 | |
379c3233 |
557 | if ((p[0] == '-' || (p[0] == '+' && flags & OPTF_NEGATION)) && |
b91e2391 |
558 | p[1] != 0) { |
559 | if (strcmp(p, "--") == 0) { /* If this is the magic marker */ |
379c3233 |
560 | permute(argv, data); /* Stow the magic marker item */ |
b91e2391 |
561 | return (EOF); /* There's nothing else to do */ |
562 | } |
563 | break; /* We've found something! */ |
564 | } |
565 | |
566 | /* --- Figure out how to proceed --- */ |
567 | |
568 | switch (data->order & 3) { |
379c3233 |
569 | case ORD_POSIX: /* POSIX option order */ |
b91e2391 |
570 | return (EOF); /* This is easy */ |
571 | break; |
379c3233 |
572 | case ORD_PERMUTE: /* Permute the option order */ |
b91e2391 |
573 | break; |
379c3233 |
574 | case ORD_RETURN: /* Return each argument */ |
575 | permute(argv, data); /* Insert word in same place */ |
b91e2391 |
576 | data->arg = p; /* Point to the argument */ |
577 | return (0); /* Return the value */ |
578 | } |
579 | } |
580 | |
581 | /* --- We found an option --- */ |
582 | |
379c3233 |
583 | permute(argv, data); /* Do any permuting necessary */ |
b91e2391 |
584 | |
585 | /* --- Check for a numeric option --- * |
586 | * |
587 | * We only check the first character (or the second if the first is a |
588 | * sign). This ought to be enough. |
589 | */ |
590 | |
379c3233 |
591 | if (flags & OPTF_NUMBERS && (p[0] == '-' || flags & OPTF_NEGNUMBER)) { |
b91e2391 |
592 | if (((p[1] == '+' || p[1] == '-') && isdigit((unsigned char)p[2])) || |
593 | isdigit((unsigned char)p[1])) { |
594 | data->opt = strtol(p + 1, &data->arg, 10); |
595 | while (isspace((unsigned char)data->arg[0])) |
596 | data->arg++; |
597 | if (!data->arg[0]) |
598 | data->arg = 0; |
379c3233 |
599 | return (p[0] == '-' ? '#' : '#' | OPTF_NEGATED); |
b91e2391 |
600 | } |
601 | } |
602 | |
603 | /* --- Check for a long option --- */ |
604 | |
605 | if (p[0] == '+') |
379c3233 |
606 | data->order |= ORD_NEGATE; |
b91e2391 |
607 | |
608 | if (((p[0] == '-' && p[1] == '-') || |
379c3233 |
609 | (flags & OPTF_NOSHORTS && !findOpt(p[1], shortopt, data))) && |
610 | (~flags & OPTF_NOLONGS)) /* Is this a long option? */ |
b91e2391 |
611 | { |
612 | int match = -1; /* Count matches as we go */ |
613 | |
614 | if (p[0] == '+') { /* If it's negated */ |
379c3233 |
615 | data->order |= ORD_NEGATE; /* Set the negate flag */ |
b91e2391 |
616 | p++; /* Point to the main text */ |
617 | prefix = "+"; /* Set the prefix string up */ |
618 | } else if (p[1] == '-') { /* If this is a `%|--|%' option */ |
379c3233 |
619 | if ((flags & OPTF_NEGATION) && strncmp(p + 2, "no-", 3) == 0) { |
b91e2391 |
620 | p += 5; /* Point to main text */ |
621 | prefix = "--no-"; /* And set the prefix */ |
379c3233 |
622 | data->order |= ORD_NEGATE; /* Set the negatedness flag */ |
b91e2391 |
623 | } else { |
624 | p += 2; /* Point to the main text */ |
625 | prefix = "--"; /* Remember the prefix string */ |
626 | } |
627 | } else { |
379c3233 |
628 | if ((flags & OPTF_NEGATION) && strncmp(p + 1, "no-", 3) == 0) { |
b91e2391 |
629 | p += 4; /* Find the text */ |
630 | prefix = "-no-"; /* Set the prefix */ |
379c3233 |
631 | data->order |= ORD_NEGATE; /* Set negatedness flag */ |
b91e2391 |
632 | } else { |
633 | p++; /* Otherwise find the text */ |
634 | prefix = "-"; /* And remember the prefix */ |
635 | } |
636 | } |
637 | |
638 | for (i = 0; longopts[i].name; i++) { /* Loop through the options */ |
379c3233 |
639 | if ((data->order & ORD_NEGATE) && |
640 | (~longopts[i].has_arg & OPTF_NEGATE)) |
b91e2391 |
641 | continue; /* If neg and opt doesn't allow */ |
642 | |
643 | r = (char *) longopts[i].name; /* Point to the name string */ |
644 | q = p; /* Point to the string start */ |
645 | for (;;) { /* Do a loop here */ |
646 | if (*q == 0 || *q == '=') { /* End of the option string? */ |
647 | if (*r == 0) { /* If end of other string */ |
648 | match = i; /* This is the match */ |
649 | goto botched; /* And exit the loop now */ |
650 | } |
651 | if (match == -1) { /* If no match currently */ |
652 | match = i; /* Then this is it, here */ |
653 | break; /* Stop looking now */ |
654 | } else { |
655 | match = -1; /* Else it's ambiguous */ |
656 | goto botched; /* So give up right now */ |
657 | } |
658 | } |
659 | else if (*q != *r) /* Otherwise if mismatch */ |
660 | break; /* Abort this loop */ |
661 | q++, r++; /* Increment the counters */ |
662 | } |
663 | } |
664 | |
665 | botched: |
666 | if (match == -1) { /* If we couldn't find a match */ |
667 | if (data->err) { |
5b59e227 |
668 | fprintf(stderr, "%s: unrecognized option `%s%s'\n", |
b91e2391 |
669 | data->prog, |
670 | prefix, p); |
671 | } |
672 | return ('?'); |
673 | } |
674 | |
675 | if (longind) /* Allow longind to be null */ |
676 | *longind = match; /* Store the match away */ |
677 | |
678 | /* --- Handle argument behaviour --- */ |
679 | |
680 | while (*p != 0 && *p != '=') /* Find the argument string */ |
681 | p++; |
682 | p = (*p ? p + 1 : 0); /* Sort out argument presence */ |
683 | q = (char *) longopts[match].name; /* Remember the name here */ |
684 | |
685 | switch (longopts[match].has_arg & 3) { |
686 | case no_argument: |
687 | if (p) { |
688 | if (data->err) { |
689 | fprintf(stderr, |
690 | "%s: option `%s%s' does not accept arguments\n", |
691 | data->prog, |
692 | prefix, q); |
693 | } |
694 | return ('?'); |
695 | } |
696 | break; |
697 | |
698 | case required_argument: |
699 | if (!p) { /* If no argument given */ |
379c3233 |
700 | p = nextWord(argc, argv, data); |
b91e2391 |
701 | |
702 | if (!p) { /* If no more arguments */ |
703 | if (data->err) { |
704 | fprintf(stderr, "%s: option `%s%s' requires an argument\n", |
705 | data->prog, |
706 | prefix, q); |
707 | } |
708 | return (noarg); |
709 | } |
710 | |
379c3233 |
711 | permute(argv, data); |
b91e2391 |
712 | } |
713 | break; |
714 | |
715 | case optional_argument: |
716 | /* Who cares? */ |
717 | break; |
718 | } |
719 | data->arg = p; |
720 | |
721 | /* --- Do correct things now we have a match --- */ |
722 | |
723 | if (longopts[match].flag) { /* If he has a @flag@ argument */ |
379c3233 |
724 | if (longopts[match].has_arg & OPTF_SWITCH) { |
725 | if (data->order & ORD_NEGATE) |
b91e2391 |
726 | *longopts[match].flag &= ~longopts[match].val; |
727 | else |
728 | *longopts[match].flag |= longopts[match].val; |
729 | } else { |
379c3233 |
730 | if (data->order & ORD_NEGATE) |
b91e2391 |
731 | *longopts[match].flag = 0; |
732 | else |
733 | *longopts[match].flag = longopts[match].val; |
734 | } |
735 | return (0); /* And return something */ |
736 | } else { |
379c3233 |
737 | if (data->order & ORD_NEGATE) |
738 | return (longopts[match].val | OPTF_NEGATED); |
b91e2391 |
739 | else |
740 | return (longopts[match].val); |
741 | } |
742 | } |
743 | |
744 | /* --- Do short options things --- */ |
745 | |
746 | else { |
747 | if (p[0] == '+') /* If starts with a `%|+|%' */ |
379c3233 |
748 | data->order |= ORD_NEGATE; |
b91e2391 |
749 | data->list = p + 1; /* Omit leading `%|-|%'/`%|+|%' */ |
750 | } |
751 | } |
752 | |
753 | /* --- Now process the short options --- */ |
754 | |
755 | i = *data->list++; /* Get the next option letter */ |
756 | data->opt = i; /* Store this away nicely */ |
757 | |
379c3233 |
758 | p = (char *) findOpt(i, shortopt, data); |
b91e2391 |
759 | if (!p) { /* No more options left */ |
760 | if (data->err) { |
761 | fprintf(stderr, "%s: unknown option `%c%c'\n", |
762 | data->prog, |
379c3233 |
763 | data->order & ORD_NEGATE ? '+' : '-', |
b91e2391 |
764 | i); |
765 | } |
766 | return ('?'); |
767 | } |
768 | |
769 | data->opt = i; /* Store this for the caller */ |
770 | |
771 | /* --- Sort out an argument, if we expect one --- */ |
772 | |
773 | if (p[0] == ':') { /* If we expect an option */ |
774 | q = (data->list[0] ? data->list : 0); /* If argument expected, use it */ |
775 | data->list = 0; /* Kill the remaining options */ |
776 | if (p[1] != ':' && !q) { /* If no arg, and not optional */ |
777 | |
778 | /* --- Same code as before --- */ |
779 | |
379c3233 |
780 | q = nextWord(argc, argv, data); /* Read the next word */ |
b91e2391 |
781 | if (!q) { /* If no more arguments */ |
782 | if (data->err) { |
783 | fprintf(stderr, "%s: option `%c%c' requires an argument\n", |
784 | data->prog, |
379c3233 |
785 | data->order & ORD_NEGATE ? '+' : '-', |
b91e2391 |
786 | i); |
787 | } |
788 | return (noarg); |
789 | } |
379c3233 |
790 | permute(argv, data); |
b91e2391 |
791 | } |
792 | |
793 | data->arg = q; |
794 | } |
379c3233 |
795 | return ((data->order & ORD_NEGATE) ? i | OPTF_NEGATED : i); |
b91e2391 |
796 | } |
797 | |
798 | /*----- That's all, folks -------------------------------------------------*/ |