annotate lib/getopt1.c @ 112400:c729a73f3e52

Merge: aclocal.m4: tweaks to regenerate more conveniently
author Paul Eggert <eggert@cs.ucla.edu>
date Fri, 21 Jan 2011 12:24:31 -0800
parents babd014aab81
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
112311
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
1 /* getopt_long and getopt_long_only entry points for GNU getopt.
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
2 Copyright (C) 1987-1994, 1996-1998, 2004, 2006, 2009-2011 Free Software
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
3 Foundation, Inc.
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
4 This file is part of the GNU C Library.
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
5
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
6 This program is free software: you can redistribute it and/or modify
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
7 it under the terms of the GNU General Public License as published by
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
8 the Free Software Foundation; either version 3 of the License, or
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
9 (at your option) any later version.
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
10
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
11 This program is distributed in the hope that it will be useful,
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
14 GNU General Public License for more details.
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
15
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
18
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
19 #ifdef _LIBC
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
20 # include <getopt.h>
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
21 #else
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
22 # include <config.h>
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
23 # include "getopt.h"
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
24 #endif
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
25 #include "getopt_int.h"
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
26
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
27 #include <stdio.h>
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
28
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
29 /* This needs to come after some library #include
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
30 to get __GNU_LIBRARY__ defined. */
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
31 #ifdef __GNU_LIBRARY__
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
32 #include <stdlib.h>
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
33 #endif
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
34
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
35 #ifndef NULL
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
36 #define NULL 0
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
37 #endif
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
38
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
39 int
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
40 getopt_long (int argc, char *__getopt_argv_const *argv, const char *options,
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
41 const struct option *long_options, int *opt_index)
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
42 {
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
43 return _getopt_internal (argc, (char **) argv, options, long_options,
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
44 opt_index, 0, 0);
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
45 }
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
46
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
47 int
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
48 _getopt_long_r (int argc, char **argv, const char *options,
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
49 const struct option *long_options, int *opt_index,
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
50 struct _getopt_data *d)
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
51 {
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
52 return _getopt_internal_r (argc, argv, options, long_options, opt_index,
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
53 0, d, 0);
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
54 }
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
55
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
56 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
57 If an option that starts with '-' (not '--') doesn't match a long option,
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
58 but does match a short option, it is parsed as a short option
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
59 instead. */
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
60
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
61 int
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
62 getopt_long_only (int argc, char *__getopt_argv_const *argv,
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
63 const char *options,
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
64 const struct option *long_options, int *opt_index)
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
65 {
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
66 return _getopt_internal (argc, (char **) argv, options, long_options,
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
67 opt_index, 1, 0);
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
68 }
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
69
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
70 int
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
71 _getopt_long_only_r (int argc, char **argv, const char *options,
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
72 const struct option *long_options, int *opt_index,
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
73 struct _getopt_data *d)
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
74 {
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
75 return _getopt_internal_r (argc, argv, options, long_options, opt_index,
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
76 1, d, 0);
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
77 }
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
78
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
79
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
80 #ifdef TEST
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
81
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
82 #include <stdio.h>
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
83
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
84 int
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
85 main (int argc, char **argv)
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
86 {
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
87 int c;
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
88 int digit_optind = 0;
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
89
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
90 while (1)
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
91 {
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
92 int this_option_optind = optind ? optind : 1;
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
93 int option_index = 0;
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
94 static const struct option long_options[] =
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
95 {
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
96 {"add", 1, 0, 0},
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
97 {"append", 0, 0, 0},
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
98 {"delete", 1, 0, 0},
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
99 {"verbose", 0, 0, 0},
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
100 {"create", 0, 0, 0},
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
101 {"file", 1, 0, 0},
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
102 {0, 0, 0, 0}
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
103 };
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
104
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
105 c = getopt_long (argc, argv, "abc:d:0123456789",
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
106 long_options, &option_index);
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
107 if (c == -1)
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
108 break;
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
109
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
110 switch (c)
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
111 {
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
112 case 0:
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
113 printf ("option %s", long_options[option_index].name);
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
114 if (optarg)
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
115 printf (" with arg %s", optarg);
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
116 printf ("\n");
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
117 break;
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
118
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
119 case '0':
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
120 case '1':
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
121 case '2':
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
122 case '3':
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
123 case '4':
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
124 case '5':
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
125 case '6':
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
126 case '7':
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
127 case '8':
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
128 case '9':
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
129 if (digit_optind != 0 && digit_optind != this_option_optind)
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
130 printf ("digits occur in two different argv-elements.\n");
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
131 digit_optind = this_option_optind;
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
132 printf ("option %c\n", c);
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
133 break;
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
134
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
135 case 'a':
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
136 printf ("option a\n");
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
137 break;
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
138
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
139 case 'b':
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
140 printf ("option b\n");
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
141 break;
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
142
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
143 case 'c':
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
144 printf ("option c with value `%s'\n", optarg);
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
145 break;
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
146
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
147 case 'd':
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
148 printf ("option d with value `%s'\n", optarg);
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
149 break;
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
150
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
151 case '?':
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
152 break;
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
153
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
154 default:
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
155 printf ("?? getopt returned character code 0%o ??\n", c);
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
156 }
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
157 }
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
158
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
159 if (optind < argc)
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
160 {
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
161 printf ("non-option ARGV-elements: ");
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
162 while (optind < argc)
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
163 printf ("%s ", argv[optind++]);
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
164 printf ("\n");
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
165 }
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
166
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
167 exit (0);
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
168 }
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
169
babd014aab81 Regenerate.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
170 #endif /* TEST */