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