annotate lib-src/getopt1.c @ 428:3ecd8885ac67 r21-2-22

Import from CVS: tag r21-2-22
author cvs
date Mon, 13 Aug 2007 11:28:15 +0200
parents
children 84b14dcb0985
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1 /* getopt_long and getopt_long_only entry points for GNU getopt.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2 Copyright (C) 1987,88,89,90,91,92,93,94,96,97 Free Software Foundation, Inc.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
3
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
4 NOTE: The canonical source of this file is maintained with the GNU C Library.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5 Bugs can be reported to bug-glibc@prep.ai.mit.edu.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7 This program is free software; you can redistribute it and/or modify it
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9 Free Software Foundation; either version 2, or (at your option) any
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10 later version.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
12 This program is distributed in the hope that it will be useful,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
15 GNU General Public License for more details.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
18 along with this program; if not, write to the Free Software
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20 USA. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22 #ifdef HAVE_CONFIG_H
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23 #include <../src/config.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
26 #include "getopt.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
27
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
28 #include <stdio.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
30 /* Comment out all this code if we are using the GNU C Library, and are not
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31 actually compiling the library itself. This code is part of the GNU C
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
32 Library, but also included in many other GNU distributions. Compiling
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33 and linking in this code is a waste when using the GNU C library
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34 (especially if it is a shared library). Rather than having every GNU
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35 program understand `configure --with-gnu-libc' and omit the object files,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36 it is simpler to just do this in the source for each such file. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38 #define GETOPT_INTERFACE_VERSION 2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39 #if !defined (_LIBC) && defined (__GLIBC__) && __GLIBC__ >= 2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40 #include <gnu-versions.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41 #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42 #define ELIDE_CODE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
43 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
44 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
45
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
46 #ifndef ELIDE_CODE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
49 /* This needs to come after some library #include
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
50 to get __GNU_LIBRARY__ defined. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
51 #ifdef __GNU_LIBRARY__
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52 #include <stdlib.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55 #ifndef NULL
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56 #define NULL 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59 int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60 getopt_long (int argc, char *const *argv, const char *options,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61 const struct option *long_options, int *opt_index)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63 return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
64 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
65
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
66 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
67 If an option that starts with '-' (not '--') doesn't match a long option,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68 but does match a short option, it is parsed as a short option
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
69 instead. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
70
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
71 int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
72 getopt_long_only (int argc, char *const *argv, const char *options,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
73 const struct option *long_options, int *opt_index)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
74 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
75 return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
76 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
77
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
79 #endif /* Not ELIDE_CODE. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
81 #ifdef TEST
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83 #include <stdio.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
84
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
85 int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
86 main (int argc, char **argv)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
87 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
88 int c;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
89 int digit_optind = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
90
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
91 while (1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
92 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
93 int this_option_optind = optind ? optind : 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
94 int option_index = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
95 static struct option long_options[] =
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
96 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
97 {"add", 1, 0, 0},
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
98 {"append", 0, 0, 0},
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
99 {"delete", 1, 0, 0},
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
100 {"verbose", 0, 0, 0},
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
101 {"create", 0, 0, 0},
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
102 {"file", 1, 0, 0},
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
103 {0, 0, 0, 0}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
104 };
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
105
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
106 c = getopt_long (argc, argv, "abc:d:0123456789",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
107 long_options, &option_index);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
108 if (c == -1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
109 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
110
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
111 switch (c)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
112 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
113 case 0:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
114 printf ("option %s", long_options[option_index].name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
115 if (optarg)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
116 printf (" with arg %s", optarg);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
117 printf ("\n");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
118 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
119
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
120 case '0':
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
121 case '1':
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
122 case '2':
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
123 case '3':
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
124 case '4':
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
125 case '5':
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
126 case '6':
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
127 case '7':
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
128 case '8':
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
129 case '9':
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
130 if (digit_optind != 0 && digit_optind != this_option_optind)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
131 printf ("digits occur in two different argv-elements.\n");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
132 digit_optind = this_option_optind;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
133 printf ("option %c\n", c);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
134 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
135
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
136 case 'a':
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
137 printf ("option a\n");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
138 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
139
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
140 case 'b':
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
141 printf ("option b\n");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
142 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
143
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
144 case 'c':
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
145 printf ("option c with value `%s'\n", optarg);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
146 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
147
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
148 case 'd':
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
149 printf ("option d with value `%s'\n", optarg);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
150 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
151
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
152 case '?':
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
153 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
154
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
155 default:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
156 printf ("?? getopt returned character code 0%o ??\n", c);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
157 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
158 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
159
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
160 if (optind < argc)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
161 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
162 printf ("non-option ARGV-elements: ");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
163 while (optind < argc)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
164 printf ("%s ", argv[optind++]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
165 printf ("\n");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
166 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
167
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
168 exit (0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
169 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
170
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
171 #endif /* TEST */