annotate lib-src/getopt1.c @ 76:c0c698873ce1 r20-0b33

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