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