This source file includes following definitions.
- argp_default_parser
- argp_version_parser
- find_long_option
- group_parse
- convert_options
- parser_convert
- calc_sizes
- parser_init
- parser_finalize
- parser_parse_arg
- parser_parse_opt
- parser_parse_next
- __argp_parse
- weak_alias
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <alloca.h>
24 #include <stdalign.h>
25 #include <stddef.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <limits.h>
30 #include <getopt.h>
31 #include <getopt_int.h>
32
33 #ifdef _LIBC
34 # include <libintl.h>
35 # undef dgettext
36 # define dgettext(domain, msgid) \
37 __dcgettext (domain, msgid, LC_MESSAGES)
38 #else
39 # include "gettext.h"
40 #endif
41 #define N_(msgid) msgid
42
43 #include "argp.h"
44 #include "argp-namefrob.h"
45
46 #define alignto(n, d) ((((n) + (d) - 1) / (d)) * (d))
47
48
49 #define KEY_END (-1)
50 #define KEY_ARG 1
51 #define KEY_ERR '?'
52
53
54
55 #define QUOTE "--"
56
57
58 #define GROUP_BITS CHAR_BIT
59
60
61 #define USER_BITS ((sizeof ((struct option *)0)->val * CHAR_BIT) - GROUP_BITS)
62 #define USER_MASK ((1 << USER_BITS) - 1)
63
64
65 #define EBADKEY ARGP_ERR_UNKNOWN
66
67
68
69
70
71
72
73 static volatile int _argp_hang;
74
75 #define OPT_PROGNAME -2
76 #define OPT_USAGE -3
77 #define OPT_HANG -4
78
79 static const struct argp_option argp_default_options[] =
80 {
81 {"help", '?', 0, 0, N_("give this help list"), -1},
82 {"usage", OPT_USAGE, 0, 0, N_("give a short usage message"), 0},
83 {"program-name",OPT_PROGNAME, N_("NAME"), OPTION_HIDDEN,
84 N_("set the program name"), 0},
85 {"HANG", OPT_HANG, N_("SECS"), OPTION_ARG_OPTIONAL | OPTION_HIDDEN,
86 N_("hang for SECS seconds (default 3600)"), 0},
87 {NULL, 0, 0, 0, NULL, 0}
88 };
89
90 static error_t
91 argp_default_parser (int key, char *arg, struct argp_state *state)
92 {
93 switch (key)
94 {
95 case '?':
96 __argp_state_help (state, state->out_stream, ARGP_HELP_STD_HELP);
97 break;
98 case OPT_USAGE:
99 __argp_state_help (state, state->out_stream,
100 ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK);
101 break;
102
103 case OPT_PROGNAME:
104 #if defined _LIBC || HAVE_DECL_PROGRAM_INVOCATION_NAME
105 program_invocation_name = arg;
106 #endif
107
108
109
110
111
112 state->name = __argp_base_name (arg);
113
114 #if defined _LIBC || HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME
115 program_invocation_short_name = state->name;
116 #endif
117
118 if ((state->flags & (ARGP_PARSE_ARGV0 | ARGP_NO_ERRS))
119 == ARGP_PARSE_ARGV0)
120
121 state->argv[0] = arg;
122
123 break;
124
125 case OPT_HANG:
126 _argp_hang = atoi (arg ? arg : "3600");
127 while (_argp_hang-- > 0)
128 __sleep (1);
129 break;
130
131 default:
132 return EBADKEY;
133 }
134 return 0;
135 }
136
137 static const struct argp argp_default_argp =
138 {argp_default_options, &argp_default_parser, NULL, NULL, NULL, NULL, "libc"};
139
140
141 static const struct argp_option argp_version_options[] =
142 {
143 {"version", 'V', 0, 0, N_("print program version"), -1},
144 {NULL, 0, 0, 0, NULL, 0}
145 };
146
147 static error_t
148 argp_version_parser (int key, char *arg, struct argp_state *state)
149 {
150 switch (key)
151 {
152 case 'V':
153 if (argp_program_version_hook)
154 (*argp_program_version_hook) (state->out_stream, state);
155 else if (argp_program_version)
156 fprintf (state->out_stream, "%s\n", argp_program_version);
157 else
158 __argp_error (state, "%s",
159 dgettext (state->root_argp->argp_domain,
160 "(PROGRAM ERROR) No version known!?"));
161 if (! (state->flags & ARGP_NO_EXIT))
162 exit (0);
163 break;
164 default:
165 return EBADKEY;
166 }
167 return 0;
168 }
169
170 static const struct argp argp_version_argp =
171 {argp_version_options, &argp_version_parser, NULL, NULL, NULL, NULL, "libc"};
172
173
174
175
176 static int
177 find_long_option (struct option *long_options, const char *name)
178 {
179 struct option *l = long_options;
180 while (l->name != NULL)
181 if (name != NULL && strcmp (l->name, name) == 0)
182 return l - long_options;
183 else
184 l++;
185 if (name == NULL)
186 return l - long_options;
187 else
188 return -1;
189 }
190
191
192
193
194
195 struct group
196 {
197
198 argp_parser_t parser;
199
200
201 const struct argp *argp;
202
203
204
205
206 char *short_end;
207
208
209 unsigned args_processed;
210
211
212 struct group *parent;
213 unsigned parent_index;
214
215
216
217 void *input, **child_inputs;
218 void *hook;
219 };
220
221
222
223
224 static error_t
225 group_parse (struct group *group, struct argp_state *state, int key, char *arg)
226 {
227 if (group->parser)
228 {
229 error_t err;
230 state->hook = group->hook;
231 state->input = group->input;
232 state->child_inputs = group->child_inputs;
233 state->arg_num = group->args_processed;
234 err = (*group->parser)(key, arg, state);
235 group->hook = state->hook;
236 return err;
237 }
238 else
239 return EBADKEY;
240 }
241
242 struct parser
243 {
244 const struct argp *argp;
245
246
247
248 char *short_opts;
249
250
251 struct option *long_opts;
252
253 struct _getopt_data opt_data;
254
255
256 struct group *groups;
257
258 struct group *egroup;
259
260 void **child_inputs;
261
262
263
264
265
266 int try_getopt;
267
268
269 struct argp_state state;
270
271
272 void *storage;
273 };
274
275
276
277 struct parser_convert_state
278 {
279 struct parser *parser;
280 char *short_end;
281 struct option *long_end;
282 void **child_inputs_end;
283 };
284
285
286
287
288
289 static struct group *
290 convert_options (const struct argp *argp,
291 struct group *parent, unsigned parent_index,
292 struct group *group, struct parser_convert_state *cvt)
293 {
294
295 const struct argp_option *real = argp->options;
296 const struct argp_child *children = argp->children;
297
298 if (real || argp->parser)
299 {
300 const struct argp_option *opt;
301
302 if (real)
303 for (opt = real; !__option_is_end (opt); opt++)
304 {
305 if (! (opt->flags & OPTION_ALIAS))
306
307 real = opt;
308
309 if (! (real->flags & OPTION_DOC))
310
311 {
312 if (__option_is_short (opt))
313
314 {
315 *cvt->short_end++ = opt->key;
316 if (real->arg)
317 {
318 *cvt->short_end++ = ':';
319 if (real->flags & OPTION_ARG_OPTIONAL)
320 *cvt->short_end++ = ':';
321 }
322 *cvt->short_end = '\0';
323 }
324
325 if (opt->name
326 && find_long_option (cvt->parser->long_opts, opt->name) < 0)
327
328 {
329 cvt->long_end->name = opt->name;
330 cvt->long_end->has_arg =
331 (real->arg
332 ? (real->flags & OPTION_ARG_OPTIONAL
333 ? optional_argument
334 : required_argument)
335 : no_argument);
336 cvt->long_end->flag = 0;
337
338
339
340
341
342
343 cvt->long_end->val =
344 ((opt->key ? opt->key : real->key) & USER_MASK)
345 + (((group - cvt->parser->groups) + 1) << USER_BITS);
346
347
348 (++cvt->long_end)->name = NULL;
349 }
350 }
351 }
352
353 group->parser = argp->parser;
354 group->argp = argp;
355 group->short_end = cvt->short_end;
356 group->args_processed = 0;
357 group->parent = parent;
358 group->parent_index = parent_index;
359 group->input = 0;
360 group->hook = 0;
361 group->child_inputs = 0;
362
363 if (children)
364
365
366 {
367 unsigned num_children = 0;
368 while (children[num_children].argp)
369 num_children++;
370 group->child_inputs = cvt->child_inputs_end;
371 cvt->child_inputs_end += num_children;
372 }
373
374 parent = group++;
375 }
376 else
377 parent = 0;
378
379 if (children)
380 {
381 unsigned index = 0;
382 while (children->argp)
383 group =
384 convert_options (children++->argp, parent, index++, group, cvt);
385 }
386
387 return group;
388 }
389
390
391 static void
392 parser_convert (struct parser *parser, const struct argp *argp, int flags)
393 {
394 struct parser_convert_state cvt;
395
396 cvt.parser = parser;
397 cvt.short_end = parser->short_opts;
398 cvt.long_end = parser->long_opts;
399 cvt.child_inputs_end = parser->child_inputs;
400
401 if (flags & ARGP_IN_ORDER)
402 *cvt.short_end++ = '-';
403 else if (flags & ARGP_NO_ARGS)
404 *cvt.short_end++ = '+';
405 *cvt.short_end = '\0';
406
407 cvt.long_end->name = NULL;
408
409 parser->argp = argp;
410
411 if (argp)
412 parser->egroup = convert_options (argp, 0, 0, parser->groups, &cvt);
413 else
414 parser->egroup = parser->groups;
415 }
416
417
418 struct parser_sizes
419 {
420 size_t short_len;
421 size_t long_len;
422 size_t num_groups;
423 size_t num_child_inputs;
424 };
425
426
427
428
429
430 static void
431 calc_sizes (const struct argp *argp, struct parser_sizes *szs)
432 {
433 const struct argp_child *child = argp->children;
434 const struct argp_option *opt = argp->options;
435
436 if (opt || argp->parser)
437 {
438 szs->num_groups++;
439 if (opt)
440 {
441 int num_opts = 0;
442 while (!__option_is_end (opt++))
443 num_opts++;
444 szs->short_len += num_opts * 3;
445 szs->long_len += num_opts;
446 }
447 }
448
449 if (child)
450 while (child->argp)
451 {
452 calc_sizes ((child++)->argp, szs);
453 szs->num_child_inputs++;
454 }
455 }
456
457
458 static error_t
459 parser_init (struct parser *parser, const struct argp *argp,
460 int argc, char **argv, int flags, void *input)
461 {
462 error_t err = 0;
463 struct group *group;
464 struct parser_sizes szs;
465 struct _getopt_data opt_data = _GETOPT_DATA_INITIALIZER;
466 char *storage;
467 size_t glen, gsum;
468 size_t clen, csum;
469 size_t llen, lsum;
470 size_t slen, ssum;
471
472 szs.short_len = (flags & ARGP_NO_ARGS) ? 0 : 1;
473 szs.long_len = 0;
474 szs.num_groups = 0;
475 szs.num_child_inputs = 0;
476
477 if (argp)
478 calc_sizes (argp, &szs);
479
480
481 glen = (szs.num_groups + 1) * sizeof (struct group);
482 clen = szs.num_child_inputs * sizeof (void *);
483 llen = (szs.long_len + 1) * sizeof (struct option);
484 slen = szs.short_len + 1;
485
486
487
488
489
490
491 gsum = glen;
492 csum = alignto (gsum + clen, alignof (struct option));
493 lsum = csum + llen;
494 ssum = lsum + slen;
495
496 parser->storage = malloc (ssum);
497 if (! parser->storage)
498 return ENOMEM;
499
500 storage = parser->storage;
501 parser->groups = parser->storage;
502 parser->child_inputs = (void **) (storage + gsum);
503 parser->long_opts = (struct option *) (storage + csum);
504 parser->short_opts = storage + lsum;
505 parser->opt_data = opt_data;
506
507 memset (parser->child_inputs, 0, clen);
508 parser_convert (parser, argp, flags);
509
510 memset (&parser->state, 0, sizeof (struct argp_state));
511 parser->state.root_argp = parser->argp;
512 parser->state.argc = argc;
513 parser->state.argv = argv;
514 parser->state.flags = flags;
515 parser->state.err_stream = stderr;
516 parser->state.out_stream = stdout;
517 parser->state.next = 0;
518 parser->state.pstate = parser;
519
520 parser->try_getopt = 1;
521
522
523
524 if (parser->groups < parser->egroup)
525 parser->groups->input = input;
526 for (group = parser->groups;
527 group < parser->egroup && (!err || err == EBADKEY);
528 group++)
529 {
530 if (group->parent)
531
532 group->input = group->parent->child_inputs[group->parent_index];
533
534 if (!group->parser
535 && group->argp->children && group->argp->children->argp)
536
537
538
539 group->child_inputs[0] = group->input;
540
541 err = group_parse (group, &parser->state, ARGP_KEY_INIT, 0);
542 }
543 if (err == EBADKEY)
544 err = 0;
545
546 if (err)
547 return err;
548
549 if (parser->state.flags & ARGP_NO_ERRS)
550 {
551 parser->opt_data.opterr = 0;
552 if (parser->state.flags & ARGP_PARSE_ARGV0)
553
554
555 parser->state.argv--, parser->state.argc++;
556 }
557 else
558 parser->opt_data.opterr = 1;
559
560 if (parser->state.argv == argv && argv[0])
561
562 parser->state.name = __argp_base_name (argv[0]);
563 else
564 parser->state.name = __argp_short_program_name ();
565
566 return 0;
567 }
568
569
570 static error_t
571 parser_finalize (struct parser *parser,
572 error_t err, int arg_ebadkey, int *end_index)
573 {
574 struct group *group;
575
576 if (err == EBADKEY && arg_ebadkey)
577
578 err = 0;
579
580 if (! err)
581 {
582 if (parser->state.next == parser->state.argc)
583
584
585 {
586 for (group = parser->groups;
587 group < parser->egroup && (!err || err==EBADKEY);
588 group++)
589 if (group->args_processed == 0)
590 err = group_parse (group, &parser->state, ARGP_KEY_NO_ARGS, 0);
591 for (group = parser->egroup - 1;
592 group >= parser->groups && (!err || err==EBADKEY);
593 group--)
594 err = group_parse (group, &parser->state, ARGP_KEY_END, 0);
595
596 if (err == EBADKEY)
597 err = 0;
598
599
600 if (end_index)
601 *end_index = parser->state.next;
602 }
603 else if (end_index)
604
605 *end_index = parser->state.next;
606 else
607
608 {
609 if (!(parser->state.flags & ARGP_NO_ERRS)
610 && parser->state.err_stream)
611 fprintf (parser->state.err_stream,
612 dgettext (parser->argp->argp_domain,
613 "%s: Too many arguments\n"),
614 parser->state.name);
615 err = EBADKEY;
616 }
617 }
618
619
620
621
622 if (err)
623 {
624
625 if (err == EBADKEY)
626
627
628 __argp_state_help (&parser->state, parser->state.err_stream,
629 ARGP_HELP_STD_ERR);
630
631
632 for (group = parser->groups; group < parser->egroup; group++)
633 group_parse (group, &parser->state, ARGP_KEY_ERROR, 0);
634 }
635 else
636
637 {
638
639
640
641 for (group = parser->egroup - 1
642 ; group >= parser->groups && (!err || err == EBADKEY)
643 ; group--)
644 err = group_parse (group, &parser->state, ARGP_KEY_SUCCESS, 0);
645 if (err == EBADKEY)
646 err = 0;
647 }
648
649
650 for (group = parser->egroup - 1; group >= parser->groups; group--)
651 group_parse (group, &parser->state, ARGP_KEY_FINI, 0);
652
653 if (err == EBADKEY)
654 err = EINVAL;
655
656 free (parser->storage);
657
658 return err;
659 }
660
661
662
663
664
665
666 static error_t
667 parser_parse_arg (struct parser *parser, char *val)
668 {
669
670
671 int index = --parser->state.next;
672 error_t err = EBADKEY;
673 struct group *group;
674 int key = 0;
675
676
677 for (group = parser->groups
678 ; group < parser->egroup && err == EBADKEY
679 ; group++)
680 {
681 parser->state.next++;
682 key = ARGP_KEY_ARG;
683 err = group_parse (group, &parser->state, key, val);
684
685 if (err == EBADKEY)
686
687 {
688 parser->state.next--;
689 key = ARGP_KEY_ARGS;
690 err = group_parse (group, &parser->state, key, 0);
691 }
692 }
693
694 if (! err)
695 {
696 if (key == ARGP_KEY_ARGS)
697
698
699
700 parser->state.next = parser->state.argc;
701
702 if (parser->state.next > index)
703
704
705
706 (--group)->args_processed += (parser->state.next - index);
707 else
708
709 parser->try_getopt = 1;
710 }
711
712 return err;
713 }
714
715
716
717 static error_t
718 parser_parse_opt (struct parser *parser, int opt, char *val)
719 {
720
721
722 int group_key = opt >> USER_BITS;
723 error_t err = EBADKEY;
724
725 if (group_key == 0)
726
727
728
729 {
730 struct group *group;
731 char *short_index = strchr (parser->short_opts, opt);
732
733 if (short_index)
734 for (group = parser->groups; group < parser->egroup; group++)
735 if (group->short_end > short_index)
736 {
737 err = group_parse (group, &parser->state, opt,
738 parser->opt_data.optarg);
739 break;
740 }
741 }
742 else
743
744
745 {
746 int user_key =
747 ((opt & (1 << (USER_BITS - 1))) ? ~USER_MASK : 0) | (opt & USER_MASK);
748 err =
749 group_parse (&parser->groups[group_key - 1], &parser->state,
750 user_key, parser->opt_data.optarg);
751 }
752
753 if (err == EBADKEY)
754
755
756
757 {
758 static const char bad_key_err[] =
759 N_("(PROGRAM ERROR) Option should have been recognized!?");
760 if (group_key == 0)
761 __argp_error (&parser->state, "-%c: %s", opt,
762 dgettext (parser->argp->argp_domain, bad_key_err));
763 else
764 {
765 struct option *long_opt = parser->long_opts;
766 while (long_opt->val != opt && long_opt->name)
767 long_opt++;
768 __argp_error (&parser->state, "--%s: %s",
769 long_opt->name ? long_opt->name : "???",
770 dgettext (parser->argp->argp_domain, bad_key_err));
771 }
772 }
773
774 return err;
775 }
776
777
778
779
780
781 static error_t
782 parser_parse_next (struct parser *parser, int *arg_ebadkey)
783 {
784 int opt;
785 error_t err = 0;
786
787 if (parser->state.quoted && parser->state.next < parser->state.quoted)
788
789
790
791
792 parser->state.quoted = 0;
793
794 if (parser->try_getopt && !parser->state.quoted)
795
796 {
797
798 parser->opt_data.optind = parser->state.next;
799
800 parser->opt_data.optopt = KEY_END;
801 if (parser->state.flags & ARGP_LONG_ONLY)
802 opt = _getopt_long_only_r (parser->state.argc, parser->state.argv,
803 parser->short_opts, parser->long_opts, 0,
804 &parser->opt_data);
805 else
806 opt = _getopt_long_r (parser->state.argc, parser->state.argv,
807 parser->short_opts, parser->long_opts, 0,
808 &parser->opt_data);
809
810 parser->state.next = parser->opt_data.optind;
811
812 if (opt == KEY_END)
813
814
815 {
816 parser->try_getopt = 0;
817 if (parser->state.next > 1
818 && strcmp (parser->state.argv[parser->state.next - 1], QUOTE)
819 == 0)
820
821
822
823
824 parser->state.quoted = parser->state.next;
825 }
826 else if (opt == KEY_ERR && parser->opt_data.optopt != KEY_END)
827
828
829
830 {
831 *arg_ebadkey = 0;
832 return EBADKEY;
833 }
834 }
835 else
836 opt = KEY_END;
837
838 if (opt == KEY_END)
839 {
840
841 if (parser->state.next >= parser->state.argc
842 || (parser->state.flags & ARGP_NO_ARGS))
843
844 {
845 *arg_ebadkey = 1;
846 return EBADKEY;
847 }
848 else
849
850 {
851 opt = KEY_ARG;
852 parser->opt_data.optarg = parser->state.argv[parser->state.next++];
853 }
854 }
855
856 if (opt == KEY_ARG)
857
858 err = parser_parse_arg (parser, parser->opt_data.optarg);
859 else
860 err = parser_parse_opt (parser, opt, parser->opt_data.optarg);
861
862 if (err == EBADKEY)
863 *arg_ebadkey = (opt == KEY_END || opt == KEY_ARG);
864
865 return err;
866 }
867
868
869
870
871
872
873 error_t
874 __argp_parse (const struct argp *argp, int argc, char **argv, unsigned flags,
875 int *end_index, void *input)
876 {
877 error_t err;
878 struct parser parser;
879
880
881
882 int arg_ebadkey = 0;
883
884 #ifndef _LIBC
885 if (!(flags & ARGP_PARSE_ARGV0))
886 {
887 #if HAVE_DECL_PROGRAM_INVOCATION_NAME
888 if (!program_invocation_name)
889 program_invocation_name = argv[0];
890 #endif
891 #if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME
892 if (!program_invocation_short_name)
893 program_invocation_short_name = __argp_base_name (argv[0]);
894 #endif
895 }
896 #endif
897
898 if (! (flags & ARGP_NO_HELP))
899
900 {
901 struct argp_child *child = alloca (4 * sizeof (struct argp_child));
902 struct argp *top_argp = alloca (sizeof (struct argp));
903
904
905
906 memset (top_argp, 0, sizeof (*top_argp));
907 top_argp->children = child;
908
909 memset (child, 0, 4 * sizeof (struct argp_child));
910
911 if (argp)
912 (child++)->argp = argp;
913 (child++)->argp = &argp_default_argp;
914 if (argp_program_version || argp_program_version_hook)
915 (child++)->argp = &argp_version_argp;
916 child->argp = 0;
917
918 argp = top_argp;
919 }
920
921
922 err = parser_init (&parser, argp, argc, argv, flags, input);
923
924 if (! err)
925
926 {
927 while (! err)
928 err = parser_parse_next (&parser, &arg_ebadkey);
929 err = parser_finalize (&parser, err, arg_ebadkey, end_index);
930 }
931
932 return err;
933 }
934 #ifdef weak_alias
935 weak_alias (__argp_parse, argp_parse)
936 #endif
937
938
939
940 void *
941 __argp_input (const struct argp *argp, const struct argp_state *state)
942 {
943 if (state)
944 {
945 struct group *group;
946 struct parser *parser = state->pstate;
947
948 for (group = parser->groups; group < parser->egroup; group++)
949 if (group->argp == argp)
950 return group->input;
951 }
952
953 return 0;
954 }
955 #ifdef weak_alias
956 weak_alias (__argp_input, _argp_input)
957 #endif