annotate lib-src/getopt1.c @ 155:43dd3413c7c7 r20-3b4

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