annotate lib-src/getopt1.c @ 255:084402c475ba r20-5b26

Import from CVS: tag r20-5b26
author cvs
date Mon, 13 Aug 2007 10:21:18 +0200
parents 2d532a89d707
children 74fd4e045ea6
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
175
2d532a89d707 Import from CVS: tag r20-3b14
cvs
parents: 169
diff changeset
23 #include <../src/config.h>
0
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 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 /* 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
31 actually compiling the library itself. This code is part of the GNU C
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 Library, but also included in many other GNU distributions. Compiling
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 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
34 (especially if it is a shared library). Rather than having every GNU
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 program understand `configure --with-gnu-libc' and omit the object files,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 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
37
155
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
38 #define GETOPT_INTERFACE_VERSION 2
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
39 #if !defined (_LIBC) && defined (__GLIBC__) && __GLIBC__ >= 2
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
40 #include <gnu-versions.h>
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
41 #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
42 #define ELIDE_CODE
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
43 #endif
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
44 #endif
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
45
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
46 #ifndef ELIDE_CODE
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 /* This needs to come after some library #include
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 to get __GNU_LIBRARY__ defined. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 #ifdef __GNU_LIBRARY__
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 #include <stdlib.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 #ifndef NULL
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 #define NULL 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 getopt_long (int argc, char *const *argv, const char *options,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 const struct option *long_options, int *opt_index)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 If an option that starts with '-' (not '--') doesn't match a long option,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 but does match a short option, it is parsed as a short option
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 instead. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 int
155
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
72 getopt_long_only (int argc, char *const *argv, const char *options,
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
73 const struct option *long_options, int *opt_index)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78
155
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
79 #endif /* Not ELIDE_CODE. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 #ifdef TEST
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 int
155
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
86 main (int argc, char **argv)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 int c;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 int digit_optind = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 while (1)
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 this_option_optind = optind ? optind : 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 int option_index = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 static struct option long_options[] =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 {"add", 1, 0, 0},
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 {"append", 0, 0, 0},
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 {"delete", 1, 0, 0},
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 {"verbose", 0, 0, 0},
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 {"create", 0, 0, 0},
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 {"file", 1, 0, 0},
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 {0, 0, 0, 0}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 };
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 c = getopt_long (argc, argv, "abc:d:0123456789",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 long_options, &option_index);
155
43dd3413c7c7 Import from CVS: tag r20-3b4
cvs
parents: 0
diff changeset
108 if (c == -1)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 switch (c)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 case 0:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 printf ("option %s", long_options[option_index].name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 if (optarg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 printf (" with arg %s", optarg);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 printf ("\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 case '0':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 case '1':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 case '2':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 case '3':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 case '4':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 case '5':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 case '6':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 case '7':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 case '8':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 case '9':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 if (digit_optind != 0 && digit_optind != this_option_optind)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 printf ("digits occur in two different argv-elements.\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 digit_optind = this_option_optind;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 printf ("option %c\n", c);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 case 'a':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 printf ("option a\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 case 'b':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 printf ("option b\n");
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 'c':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 printf ("option c with value `%s'\n", optarg);
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 'd':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 printf ("option d with value `%s'\n", optarg);
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 '?':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 default:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 printf ("?? getopt returned character code 0%o ??\n", c);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 if (optind < argc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 printf ("non-option ARGV-elements: ");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 while (optind < argc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 printf ("%s ", argv[optind++]);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 printf ("\n");
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 exit (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 #endif /* TEST */