Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / flex / dist / scanopt.c
bloba37b9cbf42c741b0944a7b145e98ce6c3ad84c5d
1 /* $NetBSD: scanopt.c,v 1.1.1.1 2009/10/26 00:26:56 christos Exp $ */
3 /* flex - tool to generate fast lexical analyzers */
5 /* Copyright (c) 1990 The Regents of the University of California. */
6 /* All rights reserved. */
8 /* This code is derived from software contributed to Berkeley by */
9 /* Vern Paxson. */
11 /* The United States Government has rights in this work pursuant */
12 /* to contract no. DE-AC03-76SF00098 between the United States */
13 /* Department of Energy and the University of California. */
15 /* This file is part of flex. */
17 /* Redistribution and use in source and binary forms, with or without */
18 /* modification, are permitted provided that the following conditions */
19 /* are met: */
21 /* 1. Redistributions of source code must retain the above copyright */
22 /* notice, this list of conditions and the following disclaimer. */
23 /* 2. Redistributions in binary form must reproduce the above copyright */
24 /* notice, this list of conditions and the following disclaimer in the */
25 /* documentation and/or other materials provided with the distribution. */
27 /* Neither the name of the University nor the names of its contributors */
28 /* may be used to endorse or promote products derived from this software */
29 /* without specific prior written permission. */
31 /* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
32 /* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
33 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
34 /* PURPOSE. */
36 #include "flexdef.h"
37 #include "scanopt.h"
40 /* Internal structures */
42 #ifdef HAVE_STRCASECMP
43 #define STRCASECMP(a,b) strcasecmp(a,b)
44 #else
45 static int STRCASECMP PROTO ((const char *, const char *));
47 static int STRCASECMP (a, b)
48 const char *a;
49 const char *b;
51 while (tolower ((unsigned char)*a++) == tolower ((unsigned char)*b++)) ;
52 return b - a;
54 #endif
56 #define ARG_NONE 0x01
57 #define ARG_REQ 0x02
58 #define ARG_OPT 0x04
59 #define IS_LONG 0x08
61 struct _aux {
62 int flags; /* The above hex flags. */
63 int namelen; /* Length of the actual option word, e.g., "--file[=foo]" is 4 */
64 int printlen; /* Length of entire string, e.g., "--file[=foo]" is 12 */
68 struct _scanopt_t {
69 const optspec_t *options; /* List of options. */
70 struct _aux *aux; /* Auxiliary data about options. */
71 int optc; /* Number of options. */
72 int argc; /* Number of args. */
73 char **argv; /* Array of strings. */
74 int index; /* Used as: argv[index][subscript]. */
75 int subscript;
76 char no_err_msg; /* If true, do not print errors. */
77 char has_long;
78 char has_short;
81 /* Accessor functions. These WOULD be one-liners, but portability calls. */
82 static const char *NAME PROTO ((struct _scanopt_t *, int));
83 static int PRINTLEN PROTO ((struct _scanopt_t *, int));
84 static int RVAL PROTO ((struct _scanopt_t *, int));
85 static int FLAGS PROTO ((struct _scanopt_t *, int));
86 static const char *DESC PROTO ((struct _scanopt_t *, int));
87 static int scanopt_err PROTO ((struct _scanopt_t *, int, int, int));
88 static int matchlongopt PROTO ((char *, char **, int *, char **, int *));
89 static int find_opt
90 PROTO ((struct _scanopt_t *, int, char *, int, int *, int *opt_offset));
92 static const char *NAME (s, i)
93 struct _scanopt_t *s;
94 int i;
96 return s->options[i].opt_fmt +
97 ((s->aux[i].flags & IS_LONG) ? 2 : 1);
100 static int PRINTLEN (s, i)
101 struct _scanopt_t *s;
102 int i;
104 return s->aux[i].printlen;
107 static int RVAL (s, i)
108 struct _scanopt_t *s;
109 int i;
111 return s->options[i].r_val;
114 static int FLAGS (s, i)
115 struct _scanopt_t *s;
116 int i;
118 return s->aux[i].flags;
121 static const char *DESC (s, i)
122 struct _scanopt_t *s;
123 int i;
125 return s->options[i].desc ? s->options[i].desc : "";
128 #ifndef NO_SCANOPT_USAGE
129 static int get_cols PROTO ((void));
131 static int get_cols ()
133 char *env;
134 int cols = 80; /* default */
136 #ifdef HAVE_NCURSES_H
137 initscr ();
138 endwin ();
139 if (COLS > 0)
140 return COLS;
141 #endif
143 if ((env = getenv ("COLUMNS")) != NULL)
144 cols = atoi (env);
146 return cols;
148 #endif
150 /* Macro to check for NULL before assigning a value. */
151 #define SAFE_ASSIGN(ptr,val) \
152 do{ \
153 if((ptr)!=NULL) \
154 *(ptr) = val; \
155 }while(0)
157 /* Macro to assure we reset subscript whenever we adjust s->index.*/
158 #define INC_INDEX(s,n) \
159 do{ \
160 (s)->index += (n); \
161 (s)->subscript= 0; \
162 }while(0)
164 scanopt_t *scanopt_init (options, argc, argv, flags)
165 const optspec_t *options;
166 int argc;
167 char **argv;
168 int flags;
170 int i;
171 struct _scanopt_t *s;
172 s = (struct _scanopt_t *) malloc (sizeof (struct _scanopt_t));
174 s->options = options;
175 s->optc = 0;
176 s->argc = argc;
177 s->argv = (char **) argv;
178 s->index = 1;
179 s->subscript = 0;
180 s->no_err_msg = (flags & SCANOPT_NO_ERR_MSG);
181 s->has_long = 0;
182 s->has_short = 0;
184 /* Determine option count. (Find entry with all zeros). */
185 s->optc = 0;
186 while (options[s->optc].opt_fmt
187 || options[s->optc].r_val || options[s->optc].desc)
188 s->optc++;
190 /* Build auxiliary data */
191 s->aux = (struct _aux *) malloc (s->optc * sizeof (struct _aux));
193 for (i = 0; i < s->optc; i++) {
194 const char *p, *pname;
195 const struct optspec_t *opt;
196 struct _aux *aux;
198 opt = s->options + i;
199 aux = s->aux + i;
201 aux->flags = ARG_NONE;
203 if (opt->opt_fmt[0] == '-' && opt->opt_fmt[1] == '-') {
204 aux->flags |= IS_LONG;
205 pname = opt->opt_fmt + 2;
206 s->has_long = 1;
208 else {
209 pname = opt->opt_fmt + 1;
210 s->has_short = 1;
212 aux->printlen = strlen (opt->opt_fmt);
214 aux->namelen = 0;
215 for (p = pname + 1; *p; p++) {
216 /* detect required arg */
217 if (*p == '=' || isspace ((unsigned char)*p)
218 || !(aux->flags & IS_LONG)) {
219 if (aux->namelen == 0)
220 aux->namelen = p - pname;
221 aux->flags |= ARG_REQ;
222 aux->flags &= ~ARG_NONE;
224 /* detect optional arg. This overrides required arg. */
225 if (*p == '[') {
226 if (aux->namelen == 0)
227 aux->namelen = p - pname;
228 aux->flags &= ~(ARG_REQ | ARG_NONE);
229 aux->flags |= ARG_OPT;
230 break;
233 if (aux->namelen == 0)
234 aux->namelen = p - pname;
236 return (scanopt_t *) s;
239 #ifndef NO_SCANOPT_USAGE
240 /* these structs are for scanopt_usage(). */
241 struct usg_elem {
242 int idx;
243 struct usg_elem *next;
244 struct usg_elem *alias;
246 typedef struct usg_elem usg_elem;
249 /* Prints a usage message based on contents of optlist.
250 * Parameters:
251 * scanner - The scanner, already initialized with scanopt_init().
252 * fp - The file stream to write to.
253 * usage - Text to be prepended to option list.
254 * Return: Always returns 0 (zero).
255 * The output looks something like this:
257 [indent][option, alias1, alias2...][indent][description line1
258 description line2...]
260 int scanopt_usage (scanner, fp, usage)
261 scanopt_t *scanner;
262 FILE *fp;
263 const char *usage;
265 struct _scanopt_t *s;
266 int i, columns, indent = 2;
267 usg_elem *byr_val = NULL; /* option indices sorted by r_val */
268 usg_elem *store; /* array of preallocated elements. */
269 int store_idx = 0;
270 usg_elem *ue;
271 int maxlen[2];
272 int desccol = 0;
273 int print_run = 0;
275 maxlen[0] = 0;
276 maxlen[1] = 0;
278 s = (struct _scanopt_t *) scanner;
280 if (usage) {
281 fprintf (fp, "%s\n", usage);
283 else {
284 /* Find the basename of argv[0] */
285 const char *p;
287 p = s->argv[0] + strlen (s->argv[0]);
288 while (p != s->argv[0] && *p != '/')
289 --p;
290 if (*p == '/')
291 p++;
293 fprintf (fp, _("Usage: %s [OPTIONS]...\n"), p);
295 fprintf (fp, "\n");
297 /* Sort by r_val and string. Yes, this is O(n*n), but n is small. */
298 store = (usg_elem *) malloc (s->optc * sizeof (usg_elem));
299 for (i = 0; i < s->optc; i++) {
301 /* grab the next preallocate node. */
302 ue = store + store_idx++;
303 ue->idx = i;
304 ue->next = ue->alias = NULL;
306 /* insert into list. */
307 if (!byr_val)
308 byr_val = ue;
309 else {
310 int found_alias = 0;
311 usg_elem **ue_curr, **ptr_if_no_alias = NULL;
313 ue_curr = &byr_val;
314 while (*ue_curr) {
315 if (RVAL (s, (*ue_curr)->idx) ==
316 RVAL (s, ue->idx)) {
317 /* push onto the alias list. */
318 ue_curr = &((*ue_curr)->alias);
319 found_alias = 1;
320 break;
322 if (!ptr_if_no_alias
324 STRCASECMP (NAME (s, (*ue_curr)->idx),
325 NAME (s, ue->idx)) > 0) {
326 ptr_if_no_alias = ue_curr;
328 ue_curr = &((*ue_curr)->next);
330 if (!found_alias && ptr_if_no_alias)
331 ue_curr = ptr_if_no_alias;
332 ue->next = *ue_curr;
333 *ue_curr = ue;
337 #if 0
338 if (1) {
339 printf ("ORIGINAL:\n");
340 for (i = 0; i < s->optc; i++)
341 printf ("%2d: %s\n", i, NAME (s, i));
342 printf ("SORTED:\n");
343 ue = byr_val;
344 while (ue) {
345 usg_elem *ue2;
347 printf ("%2d: %s\n", ue->idx, NAME (s, ue->idx));
348 for (ue2 = ue->alias; ue2; ue2 = ue2->next)
349 printf (" +---> %2d: %s\n", ue2->idx,
350 NAME (s, ue2->idx));
351 ue = ue->next;
354 #endif
356 /* Now build each row of output. */
358 /* first pass calculate how much room we need. */
359 for (ue = byr_val; ue; ue = ue->next) {
360 usg_elem *ap;
361 int len = 0;
362 int nshort = 0, nlong = 0;
365 #define CALC_LEN(i) do {\
366 if(FLAGS(s,i) & IS_LONG) \
367 len += (nlong++||nshort) ? 2+PRINTLEN(s,i) : PRINTLEN(s,i);\
368 else\
369 len += (nshort++||nlong)? 2+PRINTLEN(s,i) : PRINTLEN(s,i);\
370 }while(0)
372 if (!(FLAGS (s, ue->idx) & IS_LONG))
373 CALC_LEN (ue->idx);
375 /* do short aliases first. */
376 for (ap = ue->alias; ap; ap = ap->next) {
377 if (FLAGS (s, ap->idx) & IS_LONG)
378 continue;
379 CALC_LEN (ap->idx);
382 if (FLAGS (s, ue->idx) & IS_LONG)
383 CALC_LEN (ue->idx);
385 /* repeat the above loop, this time for long aliases. */
386 for (ap = ue->alias; ap; ap = ap->next) {
387 if (!(FLAGS (s, ap->idx) & IS_LONG))
388 continue;
389 CALC_LEN (ap->idx);
392 if (len > maxlen[0])
393 maxlen[0] = len;
395 /* It's much easier to calculate length for description column! */
396 len = strlen (DESC (s, ue->idx));
397 if (len > maxlen[1])
398 maxlen[1] = len;
401 /* Determine how much room we have, and how much we will allocate to each col.
402 * Do not address pathological cases. Output will just be ugly. */
403 columns = get_cols () - 1;
404 if (maxlen[0] + maxlen[1] + indent * 2 > columns) {
405 /* col 0 gets whatever it wants. we'll wrap the desc col. */
406 maxlen[1] = columns - (maxlen[0] + indent * 2);
407 if (maxlen[1] < 14) /* 14 is arbitrary lower limit on desc width. */
408 maxlen[1] = INT_MAX;
410 desccol = maxlen[0] + indent * 2;
412 #define PRINT_SPACES(fp,n)\
413 do{\
414 int _n;\
415 _n=(n);\
416 while(_n-- > 0)\
417 fputc(' ',(fp));\
418 }while(0)
421 /* Second pass (same as above loop), this time we print. */
422 /* Sloppy hack: We iterate twice. The first time we print short and long options.
423 The second time we print those lines that have ONLY long options. */
424 while (print_run++ < 2) {
425 for (ue = byr_val; ue; ue = ue->next) {
426 usg_elem *ap;
427 int nwords = 0, nchars = 0, has_short = 0;
429 /* TODO: get has_short schtick to work */
430 has_short = !(FLAGS (s, ue->idx) & IS_LONG);
431 for (ap = ue->alias; ap; ap = ap->next) {
432 if (!(FLAGS (s, ap->idx) & IS_LONG)) {
433 has_short = 1;
434 break;
437 if ((print_run == 1 && !has_short) ||
438 (print_run == 2 && has_short))
439 continue;
441 PRINT_SPACES (fp, indent);
442 nchars += indent;
444 /* Print, adding a ", " between aliases. */
445 #define PRINT_IT(i) do{\
446 if(nwords++)\
447 nchars+=fprintf(fp,", ");\
448 nchars+=fprintf(fp,"%s",s->options[i].opt_fmt);\
449 }while(0)
451 if (!(FLAGS (s, ue->idx) & IS_LONG))
452 PRINT_IT (ue->idx);
454 /* print short aliases first. */
455 for (ap = ue->alias; ap; ap = ap->next) {
456 if (!(FLAGS (s, ap->idx) & IS_LONG))
457 PRINT_IT (ap->idx);
461 if (FLAGS (s, ue->idx) & IS_LONG)
462 PRINT_IT (ue->idx);
464 /* repeat the above loop, this time for long aliases. */
465 for (ap = ue->alias; ap; ap = ap->next) {
466 if (FLAGS (s, ap->idx) & IS_LONG)
467 PRINT_IT (ap->idx);
470 /* pad to desccol */
471 PRINT_SPACES (fp, desccol - nchars);
473 /* Print description, wrapped to maxlen[1] columns. */
474 if (1) {
475 const char *pstart;
477 pstart = DESC (s, ue->idx);
478 while (1) {
479 int n = 0;
480 const char *lastws = NULL, *p;
482 p = pstart;
484 while (*p && n < maxlen[1]
485 && *p != '\n') {
486 if (isspace ((unsigned char)*p)
487 || *p == '-') lastws =
489 n++;
490 p++;
493 if (!*p) { /* hit end of desc. done. */
494 fprintf (fp, "%s\n",
495 pstart);
496 break;
498 else if (*p == '\n') { /* print everything up to here then wrap. */
499 fprintf (fp, "%.*s\n", n,
500 pstart);
501 PRINT_SPACES (fp, desccol);
502 pstart = p + 1;
503 continue;
505 else { /* we hit the edge of the screen. wrap at space if possible. */
506 if (lastws) {
507 fprintf (fp,
508 "%.*s\n",
509 (int)(lastws -
510 pstart),
511 pstart);
512 pstart =
513 lastws + 1;
515 else {
516 fprintf (fp,
517 "%.*s\n",
519 pstart);
520 pstart = p + 1;
522 PRINT_SPACES (fp, desccol);
523 continue;
528 } /* end while */
529 free (store);
530 return 0;
532 #endif /* no scanopt_usage */
535 static int scanopt_err (s, opt_offset, is_short, err)
536 struct _scanopt_t *s;
537 int opt_offset;
538 int is_short;
539 int err;
541 const char *optname = "";
542 char optchar[2];
543 const optspec_t *opt = NULL;
545 if (opt_offset >= 0)
546 opt = s->options + opt_offset;
548 if (!s->no_err_msg) {
550 if (s->index > 0 && s->index < s->argc) {
551 if (is_short) {
552 optchar[0] =
553 s->argv[s->index][s->subscript];
554 optchar[1] = '\0';
555 optname = optchar;
557 else {
558 optname = s->argv[s->index];
562 fprintf (stderr, "%s: ", s->argv[0]);
563 switch (err) {
564 case SCANOPT_ERR_ARG_NOT_ALLOWED:
565 fprintf (stderr,
567 ("option `%s' doesn't allow an argument\n"),
568 optname);
569 break;
570 case SCANOPT_ERR_ARG_NOT_FOUND:
571 fprintf (stderr,
572 _("option `%s' requires an argument\n"),
573 optname);
574 break;
575 case SCANOPT_ERR_OPT_AMBIGUOUS:
576 fprintf (stderr, _("option `%s' is ambiguous\n"),
577 optname);
578 break;
579 case SCANOPT_ERR_OPT_UNRECOGNIZED:
580 fprintf (stderr, _("Unrecognized option `%s'\n"),
581 optname);
582 break;
583 default:
584 fprintf (stderr, _("Unknown error=(%d)\n"), err);
585 break;
588 return err;
592 /* Internal. Match str against the regex ^--([^=]+)(=(.*))?
593 * return 1 if *looks* like a long option.
594 * 'str' is the only input argument, the rest of the arguments are output only.
595 * optname will point to str + 2
598 static int matchlongopt (str, optname, optlen, arg, arglen)
599 char *str;
600 char **optname;
601 int *optlen;
602 char **arg;
603 int *arglen;
605 char *p;
607 *optname = *arg = (char *) 0;
608 *optlen = *arglen = 0;
610 /* Match regex /--./ */
611 p = str;
612 if (p[0] != '-' || p[1] != '-' || !p[2])
613 return 0;
615 p += 2;
616 *optname = (char *) p;
618 /* find the end of optname */
619 while (*p && *p != '=')
620 ++p;
622 *optlen = p - *optname;
624 if (!*p)
625 /* an option with no '=...' part. */
626 return 1;
629 /* We saw an '=' char. The rest of p is the arg. */
630 p++;
631 *arg = p;
632 while (*p)
633 ++p;
634 *arglen = p - *arg;
636 return 1;
640 /* Internal. Look up long or short option by name.
641 * Long options must match a non-ambiguous prefix, or exact match.
642 * Short options must be exact.
643 * Return boolean true if found and no error.
644 * Error stored in err_code or zero if no error. */
645 static int find_opt (s, lookup_long, optstart, len, err_code, opt_offset)
646 struct _scanopt_t *s;
647 int lookup_long;
648 char *optstart;
649 int len;
650 int *err_code;
651 int *opt_offset;
653 int nmatch = 0, lastr_val = 0, i;
655 *err_code = 0;
656 *opt_offset = -1;
658 if (!optstart)
659 return 0;
661 for (i = 0; i < s->optc; i++) {
662 char *optname;
664 optname =
665 (char *) (s->options[i].opt_fmt +
666 (lookup_long ? 2 : 1));
668 if (lookup_long && (s->aux[i].flags & IS_LONG)) {
669 if (len > s->aux[i].namelen)
670 continue;
672 if (strncmp (optname, optstart, len) == 0) {
673 nmatch++;
674 *opt_offset = i;
676 /* exact match overrides all. */
677 if (len == s->aux[i].namelen) {
678 nmatch = 1;
679 break;
682 /* ambiguity is ok between aliases. */
683 if (lastr_val
684 && lastr_val ==
685 s->options[i].r_val) nmatch--;
686 lastr_val = s->options[i].r_val;
689 else if (!lookup_long && !(s->aux[i].flags & IS_LONG)) {
690 if (optname[0] == optstart[0]) {
691 nmatch++;
692 *opt_offset = i;
697 if (nmatch == 0) {
698 *err_code = SCANOPT_ERR_OPT_UNRECOGNIZED;
699 *opt_offset = -1;
701 else if (nmatch > 1) {
702 *err_code = SCANOPT_ERR_OPT_AMBIGUOUS;
703 *opt_offset = -1;
706 return *err_code ? 0 : 1;
710 int scanopt (svoid, arg, optindex)
711 scanopt_t *svoid;
712 char **arg;
713 int *optindex;
715 char *optname = NULL, *optarg = NULL, *pstart;
716 int namelen = 0, arglen = 0;
717 int errcode = 0, has_next;
718 const optspec_t *optp;
719 struct _scanopt_t *s;
720 struct _aux *auxp;
721 int is_short;
722 int opt_offset = -1;
724 s = (struct _scanopt_t *) svoid;
726 /* Normalize return-parameters. */
727 SAFE_ASSIGN (arg, NULL);
728 SAFE_ASSIGN (optindex, s->index);
730 if (s->index >= s->argc)
731 return 0;
733 /* pstart always points to the start of our current scan. */
734 pstart = s->argv[s->index] + s->subscript;
735 if (!pstart)
736 return 0;
738 if (s->subscript == 0) {
740 /* test for exact match of "--" */
741 if (pstart[0] == '-' && pstart[1] == '-' && !pstart[2]) {
742 SAFE_ASSIGN (optindex, s->index + 1);
743 INC_INDEX (s, 1);
744 return 0;
747 /* Match an opt. */
748 if (matchlongopt
749 (pstart, &optname, &namelen, &optarg, &arglen)) {
751 /* it LOOKS like an opt, but is it one?! */
752 if (!find_opt
753 (s, 1, optname, namelen, &errcode,
754 &opt_offset)) {
755 scanopt_err (s, opt_offset, 0, errcode);
756 return errcode;
758 /* We handle this below. */
759 is_short = 0;
761 /* Check for short opt. */
763 else if (pstart[0] == '-' && pstart[1]) {
764 /* Pass through to below. */
765 is_short = 1;
766 s->subscript++;
767 pstart++;
770 else {
771 /* It's not an option. We're done. */
772 return 0;
776 /* We have to re-check the subscript status because it
777 * may have changed above. */
779 if (s->subscript != 0) {
781 /* we are somewhere in a run of short opts,
782 * e.g., at the 'z' in `tar -xzf` */
784 optname = pstart;
785 namelen = 1;
786 is_short = 1;
788 if (!find_opt
789 (s, 0, pstart, namelen, &errcode, &opt_offset)) {
790 return scanopt_err (s, opt_offset, 1, errcode);
793 optarg = pstart + 1;
794 if (!*optarg) {
795 optarg = NULL;
796 arglen = 0;
798 else
799 arglen = strlen (optarg);
802 /* At this point, we have a long or short option matched at opt_offset into
803 * the s->options array (and corresponding aux array).
804 * A trailing argument is in {optarg,arglen}, if any.
807 /* Look ahead in argv[] to see if there is something
808 * that we can use as an argument (if needed). */
809 has_next = s->index + 1 < s->argc
810 && strcmp ("--", s->argv[s->index + 1]) != 0;
812 optp = s->options + opt_offset;
813 auxp = s->aux + opt_offset;
815 /* case: no args allowed */
816 if (auxp->flags & ARG_NONE) {
817 if (optarg && !is_short) {
818 scanopt_err (s, opt_offset, is_short, errcode =
819 SCANOPT_ERR_ARG_NOT_ALLOWED);
820 INC_INDEX (s, 1);
821 return errcode;
823 else if (!optarg)
824 INC_INDEX (s, 1);
825 else
826 s->subscript++;
827 return optp->r_val;
830 /* case: required */
831 if (auxp->flags & ARG_REQ) {
832 if (!optarg && !has_next)
833 return scanopt_err (s, opt_offset, is_short,
834 SCANOPT_ERR_ARG_NOT_FOUND);
836 if (!optarg) {
837 /* Let the next argv element become the argument. */
838 SAFE_ASSIGN (arg, s->argv[s->index + 1]);
839 INC_INDEX (s, 2);
841 else {
842 SAFE_ASSIGN (arg, (char *) optarg);
843 INC_INDEX (s, 1);
845 return optp->r_val;
848 /* case: optional */
849 if (auxp->flags & ARG_OPT) {
850 SAFE_ASSIGN (arg, optarg);
851 INC_INDEX (s, 1);
852 return optp->r_val;
856 /* Should not reach here. */
857 return 0;
861 int scanopt_destroy (svoid)
862 scanopt_t *svoid;
864 struct _scanopt_t *s;
866 s = (struct _scanopt_t *) svoid;
867 if (s) {
868 if (s->aux)
869 free (s->aux);
870 free (s);
872 return 0;
876 /* vim:set tabstop=8 softtabstop=4 shiftwidth=4: */