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