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