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