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