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