aboutsummaryrefslogtreecommitdiffstats
path: root/lib-src
diff options
context:
space:
mode:
authorPaul Eggert2005-07-26 21:43:13 +0000
committerPaul Eggert2005-07-26 21:43:13 +0000
commit9eff9fe3e8ab53b7a8ad2c1633b0681beb3b655c (patch)
tree36f83598b89a1567622ee38b623f7888a4c2e904 /lib-src
parent1c9916a1f9dc944ee733f5c77db39013123da8da (diff)
downloademacs-9eff9fe3e8ab53b7a8ad2c1633b0681beb3b655c.tar.gz
emacs-9eff9fe3e8ab53b7a8ad2c1633b0681beb3b655c.zip
Merge gnulib getopt implementation into Emacs.
* Makefile.in (AUTOCONF_INPUTS): New macro. ($(srcdir)/configure, $(srcdir)/src/stamp-h.in): Depend on it, so that these files also depend on m4/getopt.m4. * configure.in: Configure getopt by including m4/getopt.m4, and configuring a getopt replacement if necessary. * make-dist: Add m4 subdirectory. Unlink lib-src/getopt.h. * m4/getopt.m4: New file. * lib-src/Makefile.in (mostlyclean): Remove getopt.h, getopt.h-t. (GETOPT_H): New macro, from gnulib. (getopt.h): New rule, from gnulib. (GETOPTOBJS): Now autoconfigured. (GETOPTDEPS): getopt.h is now autoconfigured. (getopt.o, getopt1.o): Depend on $(GETOPT_H), not ${srcdir}/getopt.h. (getopt.o): Depend on ${srcdir}/gettext.h. (movemail.o): Depend on $(GETOPT_H). * lib-src/getopt.c, lib-src/getopt1.c: Sync from gnulib. * lib-src/getopt_.h, lib-src/getopt_int.h, lib-src/gettext.h: New files, from gnulib. * lib-src/getopt.h: Removed (now is getopt_.h). * nt/inc/gettext.h: Remove; no longer needed now that lib-src/gettext.h exists. * src/s/cygwin.h (C_SWITCH_SYSTEM): Remove, since gettext.h is now part of lib-src.
Diffstat (limited to 'lib-src')
-rw-r--r--lib-src/ChangeLog16
-rw-r--r--lib-src/Makefile.in21
-rw-r--r--lib-src/getopt.c635
-rw-r--r--lib-src/getopt1.c82
-rw-r--r--lib-src/getopt_.h227
-rw-r--r--lib-src/getopt_int.h131
-rw-r--r--lib-src/gettext.h78
7 files changed, 789 insertions, 401 deletions
diff --git a/lib-src/ChangeLog b/lib-src/ChangeLog
index dd4dc62a562..33a39fb68e1 100644
--- a/lib-src/ChangeLog
+++ b/lib-src/ChangeLog
@@ -1,3 +1,19 @@
12005-07-26 Paul Eggert <eggert@cs.ucla.edu>
2
3 Merge gnulib getopt implementation into Emacs.
4
5 * Makefile.in (mostlyclean): Remove getopt.h, getopt.h-t.
6 (GETOPT_H): New macro, from gnulib.
7 (getopt.h): New rule, from gnulib.
8 (GETOPTOBJS): Now autoconfigured.
9 (GETOPTDEPS): getopt.h is now autoconfigured.
10 (getopt.o, getopt1.o): Depend on $(GETOPT_H), not ${srcdir}/getopt.h.
11 (getopt.o): Depend on ${srcdir}/gettext.h.
12 (movemail.o): Depend on $(GETOPT_H).
13 * getopt.c, getopt1.c: Sync from gnulib.
14 * getopt_.h, getopt_int.h, gettext.h: New files, from gnulib.
15 * getopt.h: Removed (now is getopt_.h).
16
12005-07-13 Ken Raeburn <raeburn@gnu.org> 172005-07-13 Ken Raeburn <raeburn@gnu.org>
2 18
3 * pop.c: Don't include des.h (or variants thereof); krb.h will do 19 * pop.c: Don't include des.h (or variants thereof); krb.h will do
diff --git a/lib-src/Makefile.in b/lib-src/Makefile.in
index 372983221a3..6fa89e728c6 100644
--- a/lib-src/Makefile.in
+++ b/lib-src/Makefile.in
@@ -1,5 +1,5 @@
1# Makefile for lib-src subdirectory in GNU Emacs. 1# Makefile for lib-src subdirectory in GNU Emacs.
2# Copyright (C) 1985, 1987, 1988, 1993, 1994, 2002, 2003, 2004 2# Copyright (C) 1985, 1987, 1988, 1993, 1994, 2002, 2003, 2004, 2005
3# Free Software Foundation, Inc. 3# Free Software Foundation, Inc.
4 4
5# This file is part of GNU Emacs. 5# This file is part of GNU Emacs.
@@ -344,7 +344,7 @@ uninstall:
344 rm -f ${UTILITIES} ${INSTALLABLES} ${SCRIPTS} ${INSTALLABLE_SCRIPTS}) 344 rm -f ${UTILITIES} ${INSTALLABLES} ${SCRIPTS} ${INSTALLABLE_SCRIPTS})
345 345
346mostlyclean: 346mostlyclean:
347 -rm -f core *.o 347 -rm -f core *.o getopt.h getopt.h-t
348 348
349clean: mostlyclean 349clean: mostlyclean
350 -rm -f ${INSTALLABLES} ${UTILITIES} ${DONT_INSTALL} 350 -rm -f ${INSTALLABLES} ${UTILITIES} ${DONT_INSTALL}
@@ -382,11 +382,18 @@ test-distrib${EXEEXT}: ${srcdir}/test-distrib.c
382 $(CC) ${ALL_CFLAGS} -o test-distrib ${srcdir}/test-distrib.c 382 $(CC) ${ALL_CFLAGS} -o test-distrib ${srcdir}/test-distrib.c
383 ./test-distrib ${srcdir}/testfile 383 ./test-distrib ${srcdir}/testfile
384 384
385GETOPTOBJS = getopt.o getopt1.o 385/* We need the following in order to create a <getopt.h> when the system
386GETOPTDEPS = $(GETOPTOBJS) ${srcdir}/getopt.h 386 doesn't have one that works with the given compiler. */
387getopt.o: ${srcdir}/getopt.c ${srcdir}/getopt.h 387GETOPT_H = @GETOPT_H@
388getopt.h: getopt_.h
389 cp $(srcdir)/getopt_.h $@-t
390 mv $@-t $@
391
392GETOPTOBJS = @GETOPTOBJS@
393GETOPTDEPS = $(GETOPTOBJS) $(GETOPT_H)
394getopt.o: ${srcdir}/getopt.c $(GETOPT_H) ${srcdir}/gettext.h
388 ${CC} -c ${CPP_CFLAGS} ${srcdir}/getopt.c 395 ${CC} -c ${CPP_CFLAGS} ${srcdir}/getopt.c
389getopt1.o: ${srcdir}/getopt1.c ${srcdir}/getopt.h 396getopt1.o: ${srcdir}/getopt1.c $(GETOPT_H)
390 ${CC} -c ${CPP_CFLAGS} ${srcdir}/getopt1.c 397 ${CC} -c ${CPP_CFLAGS} ${srcdir}/getopt1.c
391 398
392#ifdef REGEXP_IN_LIBC 399#ifdef REGEXP_IN_LIBC
@@ -430,7 +437,7 @@ b2m${EXEEXT}: ${srcdir}/b2m.c ../src/config.h $(GETOPTDEPS)
430movemail${EXEEXT}: movemail.o pop.o $(GETOPTDEPS) 437movemail${EXEEXT}: movemail.o pop.o $(GETOPTDEPS)
431 $(CC) ${LINK_CFLAGS} ${MOVE_FLAGS} movemail.o pop.o $(GETOPTOBJS) $(LOADLIBES) $(LIBS_MAIL) $(LIBS_MOVE) -o movemail 438 $(CC) ${LINK_CFLAGS} ${MOVE_FLAGS} movemail.o pop.o $(GETOPTOBJS) $(LOADLIBES) $(LIBS_MAIL) $(LIBS_MOVE) -o movemail
432 439
433movemail.o: ${srcdir}/movemail.c ../src/config.h 440movemail.o: ${srcdir}/movemail.c ../src/config.h $(GETOPT_H)
434 $(CC) -c ${CPP_CFLAGS} -Demacs ${MOVE_FLAGS} ${srcdir}/movemail.c 441 $(CC) -c ${CPP_CFLAGS} -Demacs ${MOVE_FLAGS} ${srcdir}/movemail.c
435 442
436pop.o: ${srcdir}/pop.c ../src/config.h 443pop.o: ${srcdir}/pop.c ../src/config.h
diff --git a/lib-src/getopt.c b/lib-src/getopt.c
index 6799045bde8..bcb81c83fe2 100644
--- a/lib-src/getopt.c
+++ b/lib-src/getopt.c
@@ -2,8 +2,8 @@
2 NOTE: getopt is now part of the C library, so if you don't know what 2 NOTE: getopt is now part of the C library, so if you don't know what
3 "Keep this file name-space clean" means, talk to drepper@gnu.org 3 "Keep this file name-space clean" means, talk to drepper@gnu.org
4 before changing it! 4 before changing it!
5 Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002 5 Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002,2003,2004
6 Free Software Foundation, Inc. 6 Free Software Foundation, Inc.
7 This file is part of the GNU C Library. 7 This file is part of the GNU C Library.
8 8
9 This program is free software; you can redistribute it and/or modify 9 This program is free software; you can redistribute it and/or modify
@@ -30,35 +30,8 @@
30# include <config.h> 30# include <config.h>
31#endif 31#endif
32 32
33#if !defined __STDC__ || !__STDC__
34/* This is a separate conditional since some stdc systems
35 reject `defined (const)'. */
36# ifndef const
37# define const
38# endif
39#endif
40
41#include <stdio.h> 33#include <stdio.h>
42 34
43/* Comment out all this code if we are using the GNU C Library, and are not
44 actually compiling the library itself. This code is part of the GNU C
45 Library, but also included in many other GNU distributions. Compiling
46 and linking in this code is a waste when using the GNU C library
47 (especially if it is a shared library). Rather than having every GNU
48 program understand `configure --with-gnu-libc' and omit the object files,
49 it is simpler to just do this in the source for each such file. */
50
51#define GETOPT_INTERFACE_VERSION 2
52#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
53# include <gnu-versions.h>
54# if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
55# define ELIDE_CODE
56# endif
57#endif
58
59#ifndef ELIDE_CODE
60
61
62/* This needs to come after some library #include 35/* This needs to come after some library #include
63 to get __GNU_LIBRARY__ defined. */ 36 to get __GNU_LIBRARY__ defined. */
64#ifdef __GNU_LIBRARY__ 37#ifdef __GNU_LIBRARY__
@@ -68,32 +41,18 @@
68# include <unistd.h> 41# include <unistd.h>
69#endif /* GNU C library. */ 42#endif /* GNU C library. */
70 43
44#include <string.h>
45
71#ifdef VMS 46#ifdef VMS
72# include <unixlib.h> 47# include <unixlib.h>
73# if HAVE_STRING_H - 0
74# include <string.h>
75# endif
76#endif 48#endif
77 49
78#if HAVE_LIBINTL_H 50#ifdef _LIBC
79# include <libintl.h> 51# include <libintl.h>
80#endif /* HAVE_LIBINTL_H */ 52#else
81 53# include "gettext.h"
82#if 0 54# define _(msgid) gettext (msgid)
83# ifdef _LIBC 55#endif
84# include <libintl.h>
85# else /* not #ifdef _LIBC */
86/* This is for other GNU distributions with internationalized messages. */
87# include "gettext.h"
88# endif /* end #ifdef _LIBC */
89#endif /* end #if 0 */
90
91#if HAVE_LIBINTL_H
92 /* Should I include libintl.h here as in regex.c ? */
93# define _(msgid) gettext (msgid)
94#else /* not #if HAVE_LIBINTL_H */
95# define _(msgid) (msgid)
96#endif /* end #if HAVE_LIBINTL_H */
97 56
98#if defined _LIBC && defined USE_IN_LIBIO 57#if defined _LIBC && defined USE_IN_LIBIO
99# include <wchar.h> 58# include <wchar.h>
@@ -103,21 +62,22 @@
103# define attribute_hidden 62# define attribute_hidden
104#endif 63#endif
105 64
106/* This version of `getopt' appears to the caller like standard Unix `getopt' 65/* Unlike standard Unix `getopt', functions like `getopt_long'
107 but it behaves differently for the user, since it allows the user 66 let the user intersperse the options with the other arguments.
108 to intersperse the options with the other arguments.
109 67
110 As `getopt' works, it permutes the elements of ARGV so that, 68 As `getopt_long' works, it permutes the elements of ARGV so that,
111 when it is done, all the options precede everything else. Thus 69 when it is done, all the options precede everything else. Thus
112 all application programs are extended to handle flexible argument order. 70 all application programs are extended to handle flexible argument order.
113 71
114 Setting the environment variable POSIXLY_CORRECT disables permutation. 72 Using `getopt' or setting the environment variable POSIXLY_CORRECT
115 Then the behavior is completely standard. 73 disables permutation.
74 Then the application's behavior is completely standard.
116 75
117 GNU application programs can use a third alternative mode in which 76 GNU application programs can use a third alternative mode in which
118 they can distinguish the relative order of options and other arguments. */ 77 they can distinguish the relative order of options and other arguments. */
119 78
120#include "getopt.h" 79#include "getopt.h"
80#include "getopt_int.h"
121 81
122/* For communication from `getopt' to the caller. 82/* For communication from `getopt' to the caller.
123 When `getopt' finds an option that takes an argument, 83 When `getopt' finds an option that takes an argument,
@@ -142,21 +102,6 @@ char *optarg;
142/* 1003.2 says this must be 1 before any call. */ 102/* 1003.2 says this must be 1 before any call. */
143int optind = 1; 103int optind = 1;
144 104
145/* Formerly, initialization of getopt depended on optind==0, which
146 causes problems with re-calling getopt as programs generally don't
147 know that. */
148
149int __getopt_initialized attribute_hidden;
150
151/* The next char to be scanned in the option-element
152 in which the last option character we returned was found.
153 This allows us to pick up the scan where we left off.
154
155 If this is zero, or a null string, it means resume the scan
156 by advancing to the next ARGV-element. */
157
158static char *nextchar;
159
160/* Callers store zero here to inhibit the error message 105/* Callers store zero here to inhibit the error message
161 for unrecognized options. */ 106 for unrecognized options. */
162 107
@@ -168,57 +113,12 @@ int opterr = 1;
168 113
169int optopt = '?'; 114int optopt = '?';
170 115
171/* Describe how to deal with options that follow non-option ARGV-elements. 116/* Keep a global copy of all internal members of getopt_data. */
172
173 If the caller did not specify anything,
174 the default is REQUIRE_ORDER if the environment variable
175 POSIXLY_CORRECT is defined, PERMUTE otherwise.
176
177 REQUIRE_ORDER means don't recognize them as options;
178 stop option processing when the first non-option is seen.
179 This is what Unix does.
180 This mode of operation is selected by either setting the environment
181 variable POSIXLY_CORRECT, or using `+' as the first character
182 of the list of option characters.
183
184 PERMUTE is the default. We permute the contents of ARGV as we scan,
185 so that eventually all the non-options are at the end. This allows options
186 to be given in any order, even with programs that were not written to
187 expect this.
188
189 RETURN_IN_ORDER is an option available to programs that were written
190 to expect options and other ARGV-elements in any order and that care about
191 the ordering of the two. We describe each non-option ARGV-element
192 as if it were the argument of an option with character code 1.
193 Using `-' as the first character of the list of option characters
194 selects this mode of operation.
195 117
196 The special argument `--' forces an end of option-scanning regardless 118static struct _getopt_data getopt_data;
197 of the value of `ordering'. In the case of RETURN_IN_ORDER, only
198 `--' can cause `getopt' to return -1 with `optind' != ARGC. */
199 119
200static enum
201{
202 REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
203} ordering;
204
205/* Value of POSIXLY_CORRECT environment variable. */
206static char *posixly_correct;
207 120
208#ifdef __GNU_LIBRARY__ 121#ifndef __GNU_LIBRARY__
209/* We want to avoid inclusion of string.h with non-GNU libraries
210 because there are many ways it can cause trouble.
211 On some systems, it contains special magic macros that don't work
212 in GCC. */
213# include <string.h>
214# define my_index strchr
215#else
216
217# if HAVE_STRING_H
218# include <string.h>
219# else
220# include <strings.h>
221# endif
222 122
223/* Avoid depending on library functions or files 123/* Avoid depending on library functions or files
224 whose names are inconsistent. */ 124 whose names are inconsistent. */
@@ -227,43 +127,8 @@ static char *posixly_correct;
227extern char *getenv (); 127extern char *getenv ();
228#endif 128#endif
229 129
230static char *
231my_index (str, chr)
232 const char *str;
233 int chr;
234{
235 while (*str)
236 {
237 if (*str == chr)
238 return (char *) str;
239 str++;
240 }
241 return 0;
242}
243
244/* If using GCC, we can safely declare strlen this way.
245 If not using GCC, it is ok not to declare it. */
246#ifdef __GNUC__
247/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
248 That was relevant to code that was here before. */
249# if (!defined __STDC__ || !__STDC__) && !defined strlen
250/* gcc with -traditional declares the built-in strlen to return int,
251 and has done so at least since version 2.4.5. -- rms. */
252extern int strlen (const char *);
253# endif /* not __STDC__ */
254#endif /* __GNUC__ */
255
256#endif /* not __GNU_LIBRARY__ */ 130#endif /* not __GNU_LIBRARY__ */
257 131
258/* Handle permutation of arguments. */
259
260/* Describe the part of ARGV that contains non-options that have
261 been skipped. `first_nonopt' is the index in ARGV of the first of them;
262 `last_nonopt' is the index after the last of them. */
263
264static int first_nonopt;
265static int last_nonopt;
266
267#ifdef _LIBC 132#ifdef _LIBC
268/* Stored original parameters. 133/* Stored original parameters.
269 XXX This is no good solution. We should rather copy the args so 134 XXX This is no good solution. We should rather copy the args so
@@ -277,14 +142,11 @@ extern char **__libc_argv;
277# ifdef USE_NONOPTION_FLAGS 142# ifdef USE_NONOPTION_FLAGS
278/* Defined in getopt_init.c */ 143/* Defined in getopt_init.c */
279extern char *__getopt_nonoption_flags; 144extern char *__getopt_nonoption_flags;
280
281static int nonoption_flags_max_len;
282static int nonoption_flags_len;
283# endif 145# endif
284 146
285# ifdef USE_NONOPTION_FLAGS 147# ifdef USE_NONOPTION_FLAGS
286# define SWAP_FLAGS(ch1, ch2) \ 148# define SWAP_FLAGS(ch1, ch2) \
287 if (nonoption_flags_len > 0) \ 149 if (d->__nonoption_flags_len > 0) \
288 { \ 150 { \
289 char __tmp = __getopt_nonoption_flags[ch1]; \ 151 char __tmp = __getopt_nonoption_flags[ch1]; \
290 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \ 152 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
@@ -306,17 +168,12 @@ static int nonoption_flags_len;
306 `first_nonopt' and `last_nonopt' are relocated so that they describe 168 `first_nonopt' and `last_nonopt' are relocated so that they describe
307 the new indices of the non-options in ARGV after they are moved. */ 169 the new indices of the non-options in ARGV after they are moved. */
308 170
309#if defined __STDC__ && __STDC__
310static void exchange (char **);
311#endif
312
313static void 171static void
314exchange (argv) 172exchange (char **argv, struct _getopt_data *d)
315 char **argv;
316{ 173{
317 int bottom = first_nonopt; 174 int bottom = d->__first_nonopt;
318 int middle = last_nonopt; 175 int middle = d->__last_nonopt;
319 int top = optind; 176 int top = d->optind;
320 char *tem; 177 char *tem;
321 178
322 /* Exchange the shorter segment with the far end of the longer segment. 179 /* Exchange the shorter segment with the far end of the longer segment.
@@ -328,19 +185,19 @@ exchange (argv)
328 /* First make sure the handling of the `__getopt_nonoption_flags' 185 /* First make sure the handling of the `__getopt_nonoption_flags'
329 string can work normally. Our top argument must be in the range 186 string can work normally. Our top argument must be in the range
330 of the string. */ 187 of the string. */
331 if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len) 188 if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
332 { 189 {
333 /* We must extend the array. The user plays games with us and 190 /* We must extend the array. The user plays games with us and
334 presents new arguments. */ 191 presents new arguments. */
335 char *new_str = malloc (top + 1); 192 char *new_str = malloc (top + 1);
336 if (new_str == NULL) 193 if (new_str == NULL)
337 nonoption_flags_len = nonoption_flags_max_len = 0; 194 d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
338 else 195 else
339 { 196 {
340 memset (__mempcpy (new_str, __getopt_nonoption_flags, 197 memset (__mempcpy (new_str, __getopt_nonoption_flags,
341 nonoption_flags_max_len), 198 d->__nonoption_flags_max_len),
342 '\0', top + 1 - nonoption_flags_max_len); 199 '\0', top + 1 - d->__nonoption_flags_max_len);
343 nonoption_flags_max_len = top + 1; 200 d->__nonoption_flags_max_len = top + 1;
344 __getopt_nonoption_flags = new_str; 201 __getopt_nonoption_flags = new_str;
345 } 202 }
346 } 203 }
@@ -386,76 +243,71 @@ exchange (argv)
386 243
387 /* Update records for the slots the non-options now occupy. */ 244 /* Update records for the slots the non-options now occupy. */
388 245
389 first_nonopt += (optind - last_nonopt); 246 d->__first_nonopt += (d->optind - d->__last_nonopt);
390 last_nonopt = optind; 247 d->__last_nonopt = d->optind;
391} 248}
392 249
393/* Initialize the internal data when the first call is made. */ 250/* Initialize the internal data when the first call is made. */
394 251
395#if defined __STDC__ && __STDC__
396static const char *_getopt_initialize (int, char *const *, const char *);
397#endif
398static const char * 252static const char *
399_getopt_initialize (argc, argv, optstring) 253_getopt_initialize (int argc, char **argv, const char *optstring,
400 int argc; 254 int posixly_correct, struct _getopt_data *d)
401 char *const *argv;
402 const char *optstring;
403{ 255{
404 /* Start processing options with ARGV-element 1 (since ARGV-element 0 256 /* Start processing options with ARGV-element 1 (since ARGV-element 0
405 is the program name); the sequence of previously skipped 257 is the program name); the sequence of previously skipped
406 non-option ARGV-elements is empty. */ 258 non-option ARGV-elements is empty. */
407 259
408 first_nonopt = last_nonopt = optind; 260 d->__first_nonopt = d->__last_nonopt = d->optind;
409 261
410 nextchar = NULL; 262 d->__nextchar = NULL;
411 263
412 posixly_correct = getenv ("POSIXLY_CORRECT"); 264 d->__posixly_correct = posixly_correct || !!getenv ("POSIXLY_CORRECT");
413 265
414 /* Determine how to handle the ordering of options and nonoptions. */ 266 /* Determine how to handle the ordering of options and nonoptions. */
415 267
416 if (optstring[0] == '-') 268 if (optstring[0] == '-')
417 { 269 {
418 ordering = RETURN_IN_ORDER; 270 d->__ordering = RETURN_IN_ORDER;
419 ++optstring; 271 ++optstring;
420 } 272 }
421 else if (optstring[0] == '+') 273 else if (optstring[0] == '+')
422 { 274 {
423 ordering = REQUIRE_ORDER; 275 d->__ordering = REQUIRE_ORDER;
424 ++optstring; 276 ++optstring;
425 } 277 }
426 else if (posixly_correct != NULL) 278 else if (d->__posixly_correct)
427 ordering = REQUIRE_ORDER; 279 d->__ordering = REQUIRE_ORDER;
428 else 280 else
429 ordering = PERMUTE; 281 d->__ordering = PERMUTE;
430 282
431#if defined _LIBC && defined USE_NONOPTION_FLAGS 283#if defined _LIBC && defined USE_NONOPTION_FLAGS
432 if (posixly_correct == NULL 284 if (!d->__posixly_correct
433 && argc == __libc_argc && argv == __libc_argv) 285 && argc == __libc_argc && argv == __libc_argv)
434 { 286 {
435 if (nonoption_flags_max_len == 0) 287 if (d->__nonoption_flags_max_len == 0)
436 { 288 {
437 if (__getopt_nonoption_flags == NULL 289 if (__getopt_nonoption_flags == NULL
438 || __getopt_nonoption_flags[0] == '\0') 290 || __getopt_nonoption_flags[0] == '\0')
439 nonoption_flags_max_len = -1; 291 d->__nonoption_flags_max_len = -1;
440 else 292 else
441 { 293 {
442 const char *orig_str = __getopt_nonoption_flags; 294 const char *orig_str = __getopt_nonoption_flags;
443 int len = nonoption_flags_max_len = strlen (orig_str); 295 int len = d->__nonoption_flags_max_len = strlen (orig_str);
444 if (nonoption_flags_max_len < argc) 296 if (d->__nonoption_flags_max_len < argc)
445 nonoption_flags_max_len = argc; 297 d->__nonoption_flags_max_len = argc;
446 __getopt_nonoption_flags = 298 __getopt_nonoption_flags =
447 (char *) malloc (nonoption_flags_max_len); 299 (char *) malloc (d->__nonoption_flags_max_len);
448 if (__getopt_nonoption_flags == NULL) 300 if (__getopt_nonoption_flags == NULL)
449 nonoption_flags_max_len = -1; 301 d->__nonoption_flags_max_len = -1;
450 else 302 else
451 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len), 303 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
452 '\0', nonoption_flags_max_len - len); 304 '\0', d->__nonoption_flags_max_len - len);
453 } 305 }
454 } 306 }
455 nonoption_flags_len = nonoption_flags_max_len; 307 d->__nonoption_flags_len = d->__nonoption_flags_max_len;
456 } 308 }
457 else 309 else
458 nonoption_flags_len = 0; 310 d->__nonoption_flags_len = 0;
459#endif 311#endif
460 312
461 return optstring; 313 return optstring;
@@ -503,10 +355,6 @@ _getopt_initialize (argc, argv, optstring)
503 `flag' field is nonzero, the value of the option's `val' field 355 `flag' field is nonzero, the value of the option's `val' field
504 if the `flag' field is zero. 356 if the `flag' field is zero.
505 357
506 The elements of ARGV aren't really const, because we permute them.
507 But we pretend they're const in the prototype to be compatible
508 with other systems.
509
510 LONGOPTS is a vector of `struct option' terminated by an 358 LONGOPTS is a vector of `struct option' terminated by an
511 element containing a name which is zero. 359 element containing a name which is zero.
512 360
@@ -515,32 +363,32 @@ _getopt_initialize (argc, argv, optstring)
515 recent call. 363 recent call.
516 364
517 If LONG_ONLY is nonzero, '-' as well as '--' can introduce 365 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
518 long-named options. */ 366 long-named options.
367
368 If POSIXLY_CORRECT is nonzero, behave as if the POSIXLY_CORRECT
369 environment variable were set. */
519 370
520int 371int
521_getopt_internal (argc, argv, optstring, longopts, longind, long_only) 372_getopt_internal_r (int argc, char **argv, const char *optstring,
522 int argc; 373 const struct option *longopts, int *longind,
523 char *const *argv; 374 int long_only, int posixly_correct, struct _getopt_data *d)
524 const char *optstring;
525 const struct option *longopts;
526 int *longind;
527 int long_only;
528{ 375{
529 int print_errors = opterr; 376 int print_errors = d->opterr;
530 if (optstring[0] == ':') 377 if (optstring[0] == ':')
531 print_errors = 0; 378 print_errors = 0;
532 379
533 if (argc < 1) 380 if (argc < 1)
534 return -1; 381 return -1;
535 382
536 optarg = NULL; 383 d->optarg = NULL;
537 384
538 if (optind == 0 || !__getopt_initialized) 385 if (d->optind == 0 || !d->__initialized)
539 { 386 {
540 if (optind == 0) 387 if (d->optind == 0)
541 optind = 1; /* Don't scan ARGV[0], the program name. */ 388 d->optind = 1; /* Don't scan ARGV[0], the program name. */
542 optstring = _getopt_initialize (argc, argv, optstring); 389 optstring = _getopt_initialize (argc, argv, optstring,
543 __getopt_initialized = 1; 390 posixly_correct, d);
391 d->__initialized = 1;
544 } 392 }
545 393
546 /* Test whether ARGV[optind] points to a non-option argument. 394 /* Test whether ARGV[optind] points to a non-option argument.
@@ -548,40 +396,41 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
548 from the shell indicating it is not an option. The later information 396 from the shell indicating it is not an option. The later information
549 is only used when the used in the GNU libc. */ 397 is only used when the used in the GNU libc. */
550#if defined _LIBC && defined USE_NONOPTION_FLAGS 398#if defined _LIBC && defined USE_NONOPTION_FLAGS
551# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \ 399# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
552 || (optind < nonoption_flags_len \ 400 || (d->optind < d->__nonoption_flags_len \
553 && __getopt_nonoption_flags[optind] == '1')) 401 && __getopt_nonoption_flags[d->optind] == '1'))
554#else 402#else
555# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0') 403# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
556#endif 404#endif
557 405
558 if (nextchar == NULL || *nextchar == '\0') 406 if (d->__nextchar == NULL || *d->__nextchar == '\0')
559 { 407 {
560 /* Advance to the next ARGV-element. */ 408 /* Advance to the next ARGV-element. */
561 409
562 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been 410 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
563 moved back by the user (who may also have changed the arguments). */ 411 moved back by the user (who may also have changed the arguments). */
564 if (last_nonopt > optind) 412 if (d->__last_nonopt > d->optind)
565 last_nonopt = optind; 413 d->__last_nonopt = d->optind;
566 if (first_nonopt > optind) 414 if (d->__first_nonopt > d->optind)
567 first_nonopt = optind; 415 d->__first_nonopt = d->optind;
568 416
569 if (ordering == PERMUTE) 417 if (d->__ordering == PERMUTE)
570 { 418 {
571 /* If we have just processed some options following some non-options, 419 /* If we have just processed some options following some non-options,
572 exchange them so that the options come first. */ 420 exchange them so that the options come first. */
573 421
574 if (first_nonopt != last_nonopt && last_nonopt != optind) 422 if (d->__first_nonopt != d->__last_nonopt
575 exchange ((char **) argv); 423 && d->__last_nonopt != d->optind)
576 else if (last_nonopt != optind) 424 exchange ((char **) argv, d);
577 first_nonopt = optind; 425 else if (d->__last_nonopt != d->optind)
426 d->__first_nonopt = d->optind;
578 427
579 /* Skip any additional non-options 428 /* Skip any additional non-options
580 and extend the range of non-options previously skipped. */ 429 and extend the range of non-options previously skipped. */
581 430
582 while (optind < argc && NONOPTION_P) 431 while (d->optind < argc && NONOPTION_P)
583 optind++; 432 d->optind++;
584 last_nonopt = optind; 433 d->__last_nonopt = d->optind;
585 } 434 }
586 435
587 /* The special ARGV-element `--' means premature end of options. 436 /* The special ARGV-element `--' means premature end of options.
@@ -589,28 +438,29 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
589 then exchange with previous non-options as if it were an option, 438 then exchange with previous non-options as if it were an option,
590 then skip everything else like a non-option. */ 439 then skip everything else like a non-option. */
591 440
592 if (optind != argc && !strcmp (argv[optind], "--")) 441 if (d->optind != argc && !strcmp (argv[d->optind], "--"))
593 { 442 {
594 optind++; 443 d->optind++;
595 444
596 if (first_nonopt != last_nonopt && last_nonopt != optind) 445 if (d->__first_nonopt != d->__last_nonopt
597 exchange ((char **) argv); 446 && d->__last_nonopt != d->optind)
598 else if (first_nonopt == last_nonopt) 447 exchange ((char **) argv, d);
599 first_nonopt = optind; 448 else if (d->__first_nonopt == d->__last_nonopt)
600 last_nonopt = argc; 449 d->__first_nonopt = d->optind;
450 d->__last_nonopt = argc;
601 451
602 optind = argc; 452 d->optind = argc;
603 } 453 }
604 454
605 /* If we have done all the ARGV-elements, stop the scan 455 /* If we have done all the ARGV-elements, stop the scan
606 and back over any non-options that we skipped and permuted. */ 456 and back over any non-options that we skipped and permuted. */
607 457
608 if (optind == argc) 458 if (d->optind == argc)
609 { 459 {
610 /* Set the next-arg-index to point at the non-options 460 /* Set the next-arg-index to point at the non-options
611 that we previously skipped, so the caller will digest them. */ 461 that we previously skipped, so the caller will digest them. */
612 if (first_nonopt != last_nonopt) 462 if (d->__first_nonopt != d->__last_nonopt)
613 optind = first_nonopt; 463 d->optind = d->__first_nonopt;
614 return -1; 464 return -1;
615 } 465 }
616 466
@@ -619,17 +469,17 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
619 469
620 if (NONOPTION_P) 470 if (NONOPTION_P)
621 { 471 {
622 if (ordering == REQUIRE_ORDER) 472 if (d->__ordering == REQUIRE_ORDER)
623 return -1; 473 return -1;
624 optarg = argv[optind++]; 474 d->optarg = argv[d->optind++];
625 return 1; 475 return 1;
626 } 476 }
627 477
628 /* We have found another option-ARGV-element. 478 /* We have found another option-ARGV-element.
629 Skip the initial punctuation. */ 479 Skip the initial punctuation. */
630 480
631 nextchar = (argv[optind] + 1 481 d->__nextchar = (argv[d->optind] + 1
632 + (longopts != NULL && argv[optind][1] == '-')); 482 + (longopts != NULL && argv[d->optind][1] == '-'));
633 } 483 }
634 484
635 /* Decode the current option-ARGV-element. */ 485 /* Decode the current option-ARGV-element. */
@@ -648,8 +498,9 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
648 This distinction seems to be the most useful approach. */ 498 This distinction seems to be the most useful approach. */
649 499
650 if (longopts != NULL 500 if (longopts != NULL
651 && (argv[optind][1] == '-' 501 && (argv[d->optind][1] == '-'
652 || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1]))))) 502 || (long_only && (argv[d->optind][2]
503 || !strchr (optstring, argv[d->optind][1])))))
653 { 504 {
654 char *nameend; 505 char *nameend;
655 const struct option *p; 506 const struct option *p;
@@ -659,15 +510,15 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
659 int indfound = -1; 510 int indfound = -1;
660 int option_index; 511 int option_index;
661 512
662 for (nameend = nextchar; *nameend && *nameend != '='; nameend++) 513 for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
663 /* Do nothing. */ ; 514 /* Do nothing. */ ;
664 515
665 /* Test all long options for either exact match 516 /* Test all long options for either exact match
666 or abbreviated matches. */ 517 or abbreviated matches. */
667 for (p = longopts, option_index = 0; p->name; p++, option_index++) 518 for (p = longopts, option_index = 0; p->name; p++, option_index++)
668 if (!strncmp (p->name, nextchar, nameend - nextchar)) 519 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
669 { 520 {
670 if ((unsigned int) (nameend - nextchar) 521 if ((unsigned int) (nameend - d->__nextchar)
671 == (unsigned int) strlen (p->name)) 522 == (unsigned int) strlen (p->name))
672 { 523 {
673 /* Exact match found. */ 524 /* Exact match found. */
@@ -698,37 +549,44 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
698 char *buf; 549 char *buf;
699 550
700 if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"), 551 if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
701 argv[0], argv[optind]) >= 0) 552 argv[0], argv[d->optind]) >= 0)
702 { 553 {
554 _IO_flockfile (stderr);
555
556 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
557 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
703 558
704 if (_IO_fwide (stderr, 0) > 0) 559 if (_IO_fwide (stderr, 0) > 0)
705 __fwprintf (stderr, L"%s", buf); 560 __fwprintf (stderr, L"%s", buf);
706 else 561 else
707 fputs (buf, stderr); 562 fputs (buf, stderr);
708 563
564 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
565 _IO_funlockfile (stderr);
566
709 free (buf); 567 free (buf);
710 } 568 }
711#else 569#else
712 fprintf (stderr, _("%s: option `%s' is ambiguous\n"), 570 fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
713 argv[0], argv[optind]); 571 argv[0], argv[d->optind]);
714#endif 572#endif
715 } 573 }
716 nextchar += strlen (nextchar); 574 d->__nextchar += strlen (d->__nextchar);
717 optind++; 575 d->optind++;
718 optopt = 0; 576 d->optopt = 0;
719 return '?'; 577 return '?';
720 } 578 }
721 579
722 if (pfound != NULL) 580 if (pfound != NULL)
723 { 581 {
724 option_index = indfound; 582 option_index = indfound;
725 optind++; 583 d->optind++;
726 if (*nameend) 584 if (*nameend)
727 { 585 {
728 /* Don't test has_arg with >, because some C compilers don't 586 /* Don't test has_arg with >, because some C compilers don't
729 allow it to be used on enums. */ 587 allow it to be used on enums. */
730 if (pfound->has_arg) 588 if (pfound->has_arg)
731 optarg = nameend + 1; 589 d->optarg = nameend + 1;
732 else 590 else
733 { 591 {
734 if (print_errors) 592 if (print_errors)
@@ -738,7 +596,7 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
738 int n; 596 int n;
739#endif 597#endif
740 598
741 if (argv[optind - 1][1] == '-') 599 if (argv[d->optind - 1][1] == '-')
742 { 600 {
743 /* --option */ 601 /* --option */
744#if defined _LIBC && defined USE_IN_LIBIO 602#if defined _LIBC && defined USE_IN_LIBIO
@@ -757,38 +615,48 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
757#if defined _LIBC && defined USE_IN_LIBIO 615#if defined _LIBC && defined USE_IN_LIBIO
758 n = __asprintf (&buf, _("\ 616 n = __asprintf (&buf, _("\
759%s: option `%c%s' doesn't allow an argument\n"), 617%s: option `%c%s' doesn't allow an argument\n"),
760 argv[0], argv[optind - 1][0], 618 argv[0], argv[d->optind - 1][0],
761 pfound->name); 619 pfound->name);
762#else 620#else
763 fprintf (stderr, _("\ 621 fprintf (stderr, _("\
764%s: option `%c%s' doesn't allow an argument\n"), 622%s: option `%c%s' doesn't allow an argument\n"),
765 argv[0], argv[optind - 1][0], pfound->name); 623 argv[0], argv[d->optind - 1][0],
624 pfound->name);
766#endif 625#endif
767 } 626 }
768 627
769#if defined _LIBC && defined USE_IN_LIBIO 628#if defined _LIBC && defined USE_IN_LIBIO
770 if (n >= 0) 629 if (n >= 0)
771 { 630 {
631 _IO_flockfile (stderr);
632
633 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
634 ((_IO_FILE *) stderr)->_flags2
635 |= _IO_FLAGS2_NOTCANCEL;
636
772 if (_IO_fwide (stderr, 0) > 0) 637 if (_IO_fwide (stderr, 0) > 0)
773 __fwprintf (stderr, L"%s", buf); 638 __fwprintf (stderr, L"%s", buf);
774 else 639 else
775 fputs (buf, stderr); 640 fputs (buf, stderr);
776 641
642 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
643 _IO_funlockfile (stderr);
644
777 free (buf); 645 free (buf);
778 } 646 }
779#endif 647#endif
780 } 648 }
781 649
782 nextchar += strlen (nextchar); 650 d->__nextchar += strlen (d->__nextchar);
783 651
784 optopt = pfound->val; 652 d->optopt = pfound->val;
785 return '?'; 653 return '?';
786 } 654 }
787 } 655 }
788 else if (pfound->has_arg == 1) 656 else if (pfound->has_arg == 1)
789 { 657 {
790 if (optind < argc) 658 if (d->optind < argc)
791 optarg = argv[optind++]; 659 d->optarg = argv[d->optind++];
792 else 660 else
793 { 661 {
794 if (print_errors) 662 if (print_errors)
@@ -798,27 +666,36 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
798 666
799 if (__asprintf (&buf, _("\ 667 if (__asprintf (&buf, _("\
800%s: option `%s' requires an argument\n"), 668%s: option `%s' requires an argument\n"),
801 argv[0], argv[optind - 1]) >= 0) 669 argv[0], argv[d->optind - 1]) >= 0)
802 { 670 {
671 _IO_flockfile (stderr);
672
673 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
674 ((_IO_FILE *) stderr)->_flags2
675 |= _IO_FLAGS2_NOTCANCEL;
676
803 if (_IO_fwide (stderr, 0) > 0) 677 if (_IO_fwide (stderr, 0) > 0)
804 __fwprintf (stderr, L"%s", buf); 678 __fwprintf (stderr, L"%s", buf);
805 else 679 else
806 fputs (buf, stderr); 680 fputs (buf, stderr);
807 681
682 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
683 _IO_funlockfile (stderr);
684
808 free (buf); 685 free (buf);
809 } 686 }
810#else 687#else
811 fprintf (stderr, 688 fprintf (stderr,
812 _("%s: option `%s' requires an argument\n"), 689 _("%s: option `%s' requires an argument\n"),
813 argv[0], argv[optind - 1]); 690 argv[0], argv[d->optind - 1]);
814#endif 691#endif
815 } 692 }
816 nextchar += strlen (nextchar); 693 d->__nextchar += strlen (d->__nextchar);
817 optopt = pfound->val; 694 d->optopt = pfound->val;
818 return optstring[0] == ':' ? ':' : '?'; 695 return optstring[0] == ':' ? ':' : '?';
819 } 696 }
820 } 697 }
821 nextchar += strlen (nextchar); 698 d->__nextchar += strlen (d->__nextchar);
822 if (longind != NULL) 699 if (longind != NULL)
823 *longind = option_index; 700 *longind = option_index;
824 if (pfound->flag) 701 if (pfound->flag)
@@ -833,8 +710,8 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
833 or the option starts with '--' or is not a valid short 710 or the option starts with '--' or is not a valid short
834 option, then it's an error. 711 option, then it's an error.
835 Otherwise interpret it as a short option. */ 712 Otherwise interpret it as a short option. */
836 if (!long_only || argv[optind][1] == '-' 713 if (!long_only || argv[d->optind][1] == '-'
837 || my_index (optstring, *nextchar) == NULL) 714 || strchr (optstring, *d->__nextchar) == NULL)
838 { 715 {
839 if (print_errors) 716 if (print_errors)
840 { 717 {
@@ -843,15 +720,15 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
843 int n; 720 int n;
844#endif 721#endif
845 722
846 if (argv[optind][1] == '-') 723 if (argv[d->optind][1] == '-')
847 { 724 {
848 /* --option */ 725 /* --option */
849#if defined _LIBC && defined USE_IN_LIBIO 726#if defined _LIBC && defined USE_IN_LIBIO
850 n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"), 727 n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
851 argv[0], nextchar); 728 argv[0], d->__nextchar);
852#else 729#else
853 fprintf (stderr, _("%s: unrecognized option `--%s'\n"), 730 fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
854 argv[0], nextchar); 731 argv[0], d->__nextchar);
855#endif 732#endif
856 } 733 }
857 else 734 else
@@ -859,28 +736,36 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
859 /* +option or -option */ 736 /* +option or -option */
860#if defined _LIBC && defined USE_IN_LIBIO 737#if defined _LIBC && defined USE_IN_LIBIO
861 n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"), 738 n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
862 argv[0], argv[optind][0], nextchar); 739 argv[0], argv[d->optind][0], d->__nextchar);
863#else 740#else
864 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), 741 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
865 argv[0], argv[optind][0], nextchar); 742 argv[0], argv[d->optind][0], d->__nextchar);
866#endif 743#endif
867 } 744 }
868 745
869#if defined _LIBC && defined USE_IN_LIBIO 746#if defined _LIBC && defined USE_IN_LIBIO
870 if (n >= 0) 747 if (n >= 0)
871 { 748 {
749 _IO_flockfile (stderr);
750
751 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
752 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
753
872 if (_IO_fwide (stderr, 0) > 0) 754 if (_IO_fwide (stderr, 0) > 0)
873 __fwprintf (stderr, L"%s", buf); 755 __fwprintf (stderr, L"%s", buf);
874 else 756 else
875 fputs (buf, stderr); 757 fputs (buf, stderr);
876 758
759 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
760 _IO_funlockfile (stderr);
761
877 free (buf); 762 free (buf);
878 } 763 }
879#endif 764#endif
880 } 765 }
881 nextchar = (char *) ""; 766 d->__nextchar = (char *) "";
882 optind++; 767 d->optind++;
883 optopt = 0; 768 d->optopt = 0;
884 return '?'; 769 return '?';
885 } 770 }
886 } 771 }
@@ -888,12 +773,12 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
888 /* Look at and handle the next short option-character. */ 773 /* Look at and handle the next short option-character. */
889 774
890 { 775 {
891 char c = *nextchar++; 776 char c = *d->__nextchar++;
892 char *temp = my_index (optstring, c); 777 char *temp = strchr (optstring, c);
893 778
894 /* Increment `optind' when we start to process its last character. */ 779 /* Increment `optind' when we start to process its last character. */
895 if (*nextchar == '\0') 780 if (*d->__nextchar == '\0')
896 ++optind; 781 ++d->optind;
897 782
898 if (temp == NULL || c == ':') 783 if (temp == NULL || c == ':')
899 { 784 {
@@ -904,7 +789,7 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
904 int n; 789 int n;
905#endif 790#endif
906 791
907 if (posixly_correct) 792 if (d->__posixly_correct)
908 { 793 {
909 /* 1003.2 specifies the format of this message. */ 794 /* 1003.2 specifies the format of this message. */
910#if defined _LIBC && defined USE_IN_LIBIO 795#if defined _LIBC && defined USE_IN_LIBIO
@@ -927,16 +812,24 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
927#if defined _LIBC && defined USE_IN_LIBIO 812#if defined _LIBC && defined USE_IN_LIBIO
928 if (n >= 0) 813 if (n >= 0)
929 { 814 {
815 _IO_flockfile (stderr);
816
817 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
818 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
819
930 if (_IO_fwide (stderr, 0) > 0) 820 if (_IO_fwide (stderr, 0) > 0)
931 __fwprintf (stderr, L"%s", buf); 821 __fwprintf (stderr, L"%s", buf);
932 else 822 else
933 fputs (buf, stderr); 823 fputs (buf, stderr);
934 824
825 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
826 _IO_funlockfile (stderr);
827
935 free (buf); 828 free (buf);
936 } 829 }
937#endif 830#endif
938 } 831 }
939 optopt = c; 832 d->optopt = c;
940 return '?'; 833 return '?';
941 } 834 }
942 /* Convenience. Treat POSIX -W foo same as long option --foo */ 835 /* Convenience. Treat POSIX -W foo same as long option --foo */
@@ -951,14 +844,14 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
951 int option_index; 844 int option_index;
952 845
953 /* This is an option that requires an argument. */ 846 /* This is an option that requires an argument. */
954 if (*nextchar != '\0') 847 if (*d->__nextchar != '\0')
955 { 848 {
956 optarg = nextchar; 849 d->optarg = d->__nextchar;
957 /* If we end this ARGV-element by taking the rest as an arg, 850 /* If we end this ARGV-element by taking the rest as an arg,
958 we must advance to the next element now. */ 851 we must advance to the next element now. */
959 optind++; 852 d->optind++;
960 } 853 }
961 else if (optind == argc) 854 else if (d->optind == argc)
962 { 855 {
963 if (print_errors) 856 if (print_errors)
964 { 857 {
@@ -970,11 +863,19 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
970 _("%s: option requires an argument -- %c\n"), 863 _("%s: option requires an argument -- %c\n"),
971 argv[0], c) >= 0) 864 argv[0], c) >= 0)
972 { 865 {
866 _IO_flockfile (stderr);
867
868 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
869 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
870
973 if (_IO_fwide (stderr, 0) > 0) 871 if (_IO_fwide (stderr, 0) > 0)
974 __fwprintf (stderr, L"%s", buf); 872 __fwprintf (stderr, L"%s", buf);
975 else 873 else
976 fputs (buf, stderr); 874 fputs (buf, stderr);
977 875
876 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
877 _IO_funlockfile (stderr);
878
978 free (buf); 879 free (buf);
979 } 880 }
980#else 881#else
@@ -982,7 +883,7 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
982 argv[0], c); 883 argv[0], c);
983#endif 884#endif
984 } 885 }
985 optopt = c; 886 d->optopt = c;
986 if (optstring[0] == ':') 887 if (optstring[0] == ':')
987 c = ':'; 888 c = ':';
988 else 889 else
@@ -990,22 +891,23 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
990 return c; 891 return c;
991 } 892 }
992 else 893 else
993 /* We already incremented `optind' once; 894 /* We already incremented `d->optind' once;
994 increment it again when taking next ARGV-elt as argument. */ 895 increment it again when taking next ARGV-elt as argument. */
995 optarg = argv[optind++]; 896 d->optarg = argv[d->optind++];
996 897
997 /* optarg is now the argument, see if it's in the 898 /* optarg is now the argument, see if it's in the
998 table of longopts. */ 899 table of longopts. */
999 900
1000 for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++) 901 for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
902 nameend++)
1001 /* Do nothing. */ ; 903 /* Do nothing. */ ;
1002 904
1003 /* Test all long options for either exact match 905 /* Test all long options for either exact match
1004 or abbreviated matches. */ 906 or abbreviated matches. */
1005 for (p = longopts, option_index = 0; p->name; p++, option_index++) 907 for (p = longopts, option_index = 0; p->name; p++, option_index++)
1006 if (!strncmp (p->name, nextchar, nameend - nextchar)) 908 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
1007 { 909 {
1008 if ((unsigned int) (nameend - nextchar) == strlen (p->name)) 910 if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
1009 { 911 {
1010 /* Exact match found. */ 912 /* Exact match found. */
1011 pfound = p; 913 pfound = p;
@@ -1031,22 +933,30 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
1031 char *buf; 933 char *buf;
1032 934
1033 if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"), 935 if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
1034 argv[0], argv[optind]) >= 0) 936 argv[0], argv[d->optind]) >= 0)
1035 { 937 {
938 _IO_flockfile (stderr);
939
940 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
941 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
942
1036 if (_IO_fwide (stderr, 0) > 0) 943 if (_IO_fwide (stderr, 0) > 0)
1037 __fwprintf (stderr, L"%s", buf); 944 __fwprintf (stderr, L"%s", buf);
1038 else 945 else
1039 fputs (buf, stderr); 946 fputs (buf, stderr);
1040 947
948 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
949 _IO_funlockfile (stderr);
950
1041 free (buf); 951 free (buf);
1042 } 952 }
1043#else 953#else
1044 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), 954 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
1045 argv[0], argv[optind]); 955 argv[0], argv[d->optind]);
1046#endif 956#endif
1047 } 957 }
1048 nextchar += strlen (nextchar); 958 d->__nextchar += strlen (d->__nextchar);
1049 optind++; 959 d->optind++;
1050 return '?'; 960 return '?';
1051 } 961 }
1052 if (pfound != NULL) 962 if (pfound != NULL)
@@ -1057,7 +967,7 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
1057 /* Don't test has_arg with >, because some C compilers don't 967 /* Don't test has_arg with >, because some C compilers don't
1058 allow it to be used on enums. */ 968 allow it to be used on enums. */
1059 if (pfound->has_arg) 969 if (pfound->has_arg)
1060 optarg = nameend + 1; 970 d->optarg = nameend + 1;
1061 else 971 else
1062 { 972 {
1063 if (print_errors) 973 if (print_errors)
@@ -1069,11 +979,20 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
1069%s: option `-W %s' doesn't allow an argument\n"), 979%s: option `-W %s' doesn't allow an argument\n"),
1070 argv[0], pfound->name) >= 0) 980 argv[0], pfound->name) >= 0)
1071 { 981 {
982 _IO_flockfile (stderr);
983
984 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
985 ((_IO_FILE *) stderr)->_flags2
986 |= _IO_FLAGS2_NOTCANCEL;
987
1072 if (_IO_fwide (stderr, 0) > 0) 988 if (_IO_fwide (stderr, 0) > 0)
1073 __fwprintf (stderr, L"%s", buf); 989 __fwprintf (stderr, L"%s", buf);
1074 else 990 else
1075 fputs (buf, stderr); 991 fputs (buf, stderr);
1076 992
993 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
994 _IO_funlockfile (stderr);
995
1077 free (buf); 996 free (buf);
1078 } 997 }
1079#else 998#else
@@ -1083,14 +1002,14 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
1083#endif 1002#endif
1084 } 1003 }
1085 1004
1086 nextchar += strlen (nextchar); 1005 d->__nextchar += strlen (d->__nextchar);
1087 return '?'; 1006 return '?';
1088 } 1007 }
1089 } 1008 }
1090 else if (pfound->has_arg == 1) 1009 else if (pfound->has_arg == 1)
1091 { 1010 {
1092 if (optind < argc) 1011 if (d->optind < argc)
1093 optarg = argv[optind++]; 1012 d->optarg = argv[d->optind++];
1094 else 1013 else
1095 { 1014 {
1096 if (print_errors) 1015 if (print_errors)
@@ -1100,26 +1019,35 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
1100 1019
1101 if (__asprintf (&buf, _("\ 1020 if (__asprintf (&buf, _("\
1102%s: option `%s' requires an argument\n"), 1021%s: option `%s' requires an argument\n"),
1103 argv[0], argv[optind - 1]) >= 0) 1022 argv[0], argv[d->optind - 1]) >= 0)
1104 { 1023 {
1024 _IO_flockfile (stderr);
1025
1026 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1027 ((_IO_FILE *) stderr)->_flags2
1028 |= _IO_FLAGS2_NOTCANCEL;
1029
1105 if (_IO_fwide (stderr, 0) > 0) 1030 if (_IO_fwide (stderr, 0) > 0)
1106 __fwprintf (stderr, L"%s", buf); 1031 __fwprintf (stderr, L"%s", buf);
1107 else 1032 else
1108 fputs (buf, stderr); 1033 fputs (buf, stderr);
1109 1034
1035 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1036 _IO_funlockfile (stderr);
1037
1110 free (buf); 1038 free (buf);
1111 } 1039 }
1112#else 1040#else
1113 fprintf (stderr, 1041 fprintf (stderr,
1114 _("%s: option `%s' requires an argument\n"), 1042 _("%s: option `%s' requires an argument\n"),
1115 argv[0], argv[optind - 1]); 1043 argv[0], argv[d->optind - 1]);
1116#endif 1044#endif
1117 } 1045 }
1118 nextchar += strlen (nextchar); 1046 d->__nextchar += strlen (d->__nextchar);
1119 return optstring[0] == ':' ? ':' : '?'; 1047 return optstring[0] == ':' ? ':' : '?';
1120 } 1048 }
1121 } 1049 }
1122 nextchar += strlen (nextchar); 1050 d->__nextchar += strlen (d->__nextchar);
1123 if (longind != NULL) 1051 if (longind != NULL)
1124 *longind = option_index; 1052 *longind = option_index;
1125 if (pfound->flag) 1053 if (pfound->flag)
@@ -1129,7 +1057,7 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
1129 } 1057 }
1130 return pfound->val; 1058 return pfound->val;
1131 } 1059 }
1132 nextchar = NULL; 1060 d->__nextchar = NULL;
1133 return 'W'; /* Let the application handle it. */ 1061 return 'W'; /* Let the application handle it. */
1134 } 1062 }
1135 if (temp[1] == ':') 1063 if (temp[1] == ':')
@@ -1137,26 +1065,26 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
1137 if (temp[2] == ':') 1065 if (temp[2] == ':')
1138 { 1066 {
1139 /* This is an option that accepts an argument optionally. */ 1067 /* This is an option that accepts an argument optionally. */
1140 if (*nextchar != '\0') 1068 if (*d->__nextchar != '\0')
1141 { 1069 {
1142 optarg = nextchar; 1070 d->optarg = d->__nextchar;
1143 optind++; 1071 d->optind++;
1144 } 1072 }
1145 else 1073 else
1146 optarg = NULL; 1074 d->optarg = NULL;
1147 nextchar = NULL; 1075 d->__nextchar = NULL;
1148 } 1076 }
1149 else 1077 else
1150 { 1078 {
1151 /* This is an option that requires an argument. */ 1079 /* This is an option that requires an argument. */
1152 if (*nextchar != '\0') 1080 if (*d->__nextchar != '\0')
1153 { 1081 {
1154 optarg = nextchar; 1082 d->optarg = d->__nextchar;
1155 /* If we end this ARGV-element by taking the rest as an arg, 1083 /* If we end this ARGV-element by taking the rest as an arg,
1156 we must advance to the next element now. */ 1084 we must advance to the next element now. */
1157 optind++; 1085 d->optind++;
1158 } 1086 }
1159 else if (optind == argc) 1087 else if (d->optind == argc)
1160 { 1088 {
1161 if (print_errors) 1089 if (print_errors)
1162 { 1090 {
@@ -1168,11 +1096,19 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
1168%s: option requires an argument -- %c\n"), 1096%s: option requires an argument -- %c\n"),
1169 argv[0], c) >= 0) 1097 argv[0], c) >= 0)
1170 { 1098 {
1099 _IO_flockfile (stderr);
1100
1101 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1102 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
1103
1171 if (_IO_fwide (stderr, 0) > 0) 1104 if (_IO_fwide (stderr, 0) > 0)
1172 __fwprintf (stderr, L"%s", buf); 1105 __fwprintf (stderr, L"%s", buf);
1173 else 1106 else
1174 fputs (buf, stderr); 1107 fputs (buf, stderr);
1175 1108
1109 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1110 _IO_funlockfile (stderr);
1111
1176 free (buf); 1112 free (buf);
1177 } 1113 }
1178#else 1114#else
@@ -1181,7 +1117,7 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
1181 argv[0], c); 1117 argv[0], c);
1182#endif 1118#endif
1183 } 1119 }
1184 optopt = c; 1120 d->optopt = c;
1185 if (optstring[0] == ':') 1121 if (optstring[0] == ':')
1186 c = ':'; 1122 c = ':';
1187 else 1123 else
@@ -1190,8 +1126,8 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
1190 else 1126 else
1191 /* We already incremented `optind' once; 1127 /* We already incremented `optind' once;
1192 increment it again when taking next ARGV-elt as argument. */ 1128 increment it again when taking next ARGV-elt as argument. */
1193 optarg = argv[optind++]; 1129 d->optarg = argv[d->optind++];
1194 nextchar = NULL; 1130 d->__nextchar = NULL;
1195 } 1131 }
1196 } 1132 }
1197 return c; 1133 return c;
@@ -1199,18 +1135,40 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
1199} 1135}
1200 1136
1201int 1137int
1202getopt (argc, argv, optstring) 1138_getopt_internal (int argc, char **argv, const char *optstring,
1203 int argc; 1139 const struct option *longopts, int *longind,
1204 char *const *argv; 1140 int long_only, int posixly_correct)
1205 const char *optstring;
1206{ 1141{
1207 return _getopt_internal (argc, argv, optstring, 1142 int result;
1208 (const struct option *) 0, 1143
1209 (int *) 0, 1144 getopt_data.optind = optind;
1210 0); 1145 getopt_data.opterr = opterr;
1146
1147 result = _getopt_internal_r (argc, argv, optstring, longopts, longind,
1148 long_only, posixly_correct, &getopt_data);
1149
1150 optind = getopt_data.optind;
1151 optarg = getopt_data.optarg;
1152 optopt = getopt_data.optopt;
1153
1154 return result;
1155}
1156
1157/* glibc gets a LSB-compliant getopt.
1158 Standalone applications get a POSIX-compliant getopt. */
1159#if _LIBC
1160enum { POSIXLY_CORRECT = 0 };
1161#else
1162enum { POSIXLY_CORRECT = 1 };
1163#endif
1164
1165int
1166getopt (int argc, char *const *argv, const char *optstring)
1167{
1168 return _getopt_internal (argc, (char **) argv, optstring, NULL, NULL, 0,
1169 POSIXLY_CORRECT);
1211} 1170}
1212 1171
1213#endif /* Not ELIDE_CODE. */
1214 1172
1215#ifdef TEST 1173#ifdef TEST
1216 1174
@@ -1218,9 +1176,7 @@ getopt (argc, argv, optstring)
1218 the above definition of `getopt'. */ 1176 the above definition of `getopt'. */
1219 1177
1220int 1178int
1221main (argc, argv) 1179main (int argc, char **argv)
1222 int argc;
1223 char **argv;
1224{ 1180{
1225 int c; 1181 int c;
1226 int digit_optind = 0; 1182 int digit_optind = 0;
@@ -1283,6 +1239,3 @@ main (argc, argv)
1283} 1239}
1284 1240
1285#endif /* TEST */ 1241#endif /* TEST */
1286
1287/* arch-tag: 0e6da124-7269-4785-a9de-094c263d20dc
1288 (do not change this comment) */
diff --git a/lib-src/getopt1.c b/lib-src/getopt1.c
index 4ec1e50b830..5acd2245af0 100644
--- a/lib-src/getopt1.c
+++ b/lib-src/getopt1.c
@@ -1,5 +1,5 @@
1/* getopt_long and getopt_long_only entry points for GNU getopt. 1/* getopt_long and getopt_long_only entry points for GNU getopt.
2 Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98 2 Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98,2004
3 Free Software Foundation, Inc. 3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library. 4 This file is part of the GNU C Library.
5 5
@@ -26,36 +26,10 @@
26#else 26#else
27# include "getopt.h" 27# include "getopt.h"
28#endif 28#endif
29 29#include "getopt_int.h"
30#if !defined __STDC__ || !__STDC__
31/* This is a separate conditional since some stdc systems
32 reject `defined (const)'. */
33#ifndef const
34#define const
35#endif
36#endif
37 30
38#include <stdio.h> 31#include <stdio.h>
39 32
40/* Comment out all this code if we are using the GNU C Library, and are not
41 actually compiling the library itself. This code is part of the GNU C
42 Library, but also included in many other GNU distributions. Compiling
43 and linking in this code is a waste when using the GNU C library
44 (especially if it is a shared library). Rather than having every GNU
45 program understand `configure --with-gnu-libc' and omit the object files,
46 it is simpler to just do this in the source for each such file. */
47
48#define GETOPT_INTERFACE_VERSION 2
49#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
50#include <gnu-versions.h>
51#if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
52#define ELIDE_CODE
53#endif
54#endif
55
56#ifndef ELIDE_CODE
57
58
59/* This needs to come after some library #include 33/* This needs to come after some library #include
60 to get __GNU_LIBRARY__ defined. */ 34 to get __GNU_LIBRARY__ defined. */
61#ifdef __GNU_LIBRARY__ 35#ifdef __GNU_LIBRARY__
@@ -67,14 +41,20 @@
67#endif 41#endif
68 42
69int 43int
70getopt_long (argc, argv, options, long_options, opt_index) 44getopt_long (int argc, char *__getopt_argv_const *argv, const char *options,
71 int argc; 45 const struct option *long_options, int *opt_index)
72 char *const *argv;
73 const char *options;
74 const struct option *long_options;
75 int *opt_index;
76{ 46{
77 return _getopt_internal (argc, argv, options, long_options, opt_index, 0); 47 return _getopt_internal (argc, (char **) argv, options, long_options,
48 opt_index, 0, 0);
49}
50
51int
52_getopt_long_r (int argc, char **argv, const char *options,
53 const struct option *long_options, int *opt_index,
54 struct _getopt_data *d)
55{
56 return _getopt_internal_r (argc, argv, options, long_options, opt_index,
57 0, 0, d);
78} 58}
79 59
80/* Like getopt_long, but '-' as well as '--' can indicate a long option. 60/* Like getopt_long, but '-' as well as '--' can indicate a long option.
@@ -83,31 +63,30 @@ getopt_long (argc, argv, options, long_options, opt_index)
83 instead. */ 63 instead. */
84 64
85int 65int
86getopt_long_only (argc, argv, options, long_options, opt_index) 66getopt_long_only (int argc, char *__getopt_argv_const *argv,
87 int argc; 67 const char *options,
88 char *const *argv; 68 const struct option *long_options, int *opt_index)
89 const char *options;
90 const struct option *long_options;
91 int *opt_index;
92{ 69{
93 return _getopt_internal (argc, argv, options, long_options, opt_index, 1); 70 return _getopt_internal (argc, (char **) argv, options, long_options,
71 opt_index, 1, 0);
94} 72}
95 73
96# ifdef _LIBC 74int
97libc_hidden_def (getopt_long) 75_getopt_long_only_r (int argc, char **argv, const char *options,
98libc_hidden_def (getopt_long_only) 76 const struct option *long_options, int *opt_index,
99# endif 77 struct _getopt_data *d)
78{
79 return _getopt_internal_r (argc, argv, options, long_options, opt_index,
80 1, 0, d);
81}
100 82
101#endif /* Not ELIDE_CODE. */
102 83
103#ifdef TEST 84#ifdef TEST
104 85
105#include <stdio.h> 86#include <stdio.h>
106 87
107int 88int
108main (argc, argv) 89main (int argc, char **argv)
109 int argc;
110 char **argv;
111{ 90{
112 int c; 91 int c;
113 int digit_optind = 0; 92 int digit_optind = 0;
@@ -193,6 +172,3 @@ main (argc, argv)
193} 172}
194 173
195#endif /* TEST */ 174#endif /* TEST */
196
197/* arch-tag: 28a5c558-b0c0-4bff-b5bc-e2e20291d4b6
198 (do not change this comment) */
diff --git a/lib-src/getopt_.h b/lib-src/getopt_.h
new file mode 100644
index 00000000000..a4e78cb06bf
--- /dev/null
+++ b/lib-src/getopt_.h
@@ -0,0 +1,227 @@
1/* Declarations for getopt.
2 Copyright (C) 1989-1994,1996-1999,2001,2003,2004
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19
20#ifndef _GETOPT_H
21
22#ifndef __need_getopt
23# define _GETOPT_H 1
24#endif
25
26/* Standalone applications should #define __GETOPT_PREFIX to an
27 identifier that prefixes the external functions and variables
28 defined in this header. When this happens, include the
29 headers that might declare getopt so that they will not cause
30 confusion if included after this file. Then systematically rename
31 identifiers so that they do not collide with the system functions
32 and variables. Renaming avoids problems with some compilers and
33 linkers. */
34#if defined __GETOPT_PREFIX && !defined __need_getopt
35# include <stdlib.h>
36# include <stdio.h>
37# if HAVE_UNISTD_H
38# include <unistd.h>
39# endif
40# undef __need_getopt
41# undef getopt
42# undef getopt_long
43# undef getopt_long_only
44# undef optarg
45# undef opterr
46# undef optind
47# undef optopt
48# define __GETOPT_CONCAT(x, y) x ## y
49# define __GETOPT_XCONCAT(x, y) __GETOPT_CONCAT (x, y)
50# define __GETOPT_ID(y) __GETOPT_XCONCAT (__GETOPT_PREFIX, y)
51# define getopt __GETOPT_ID (getopt)
52# define getopt_long __GETOPT_ID (getopt_long)
53# define getopt_long_only __GETOPT_ID (getopt_long_only)
54# define optarg __GETOPT_ID (optarg)
55# define opterr __GETOPT_ID (opterr)
56# define optind __GETOPT_ID (optind)
57# define optopt __GETOPT_ID (optopt)
58#endif
59
60/* Standalone applications get correct prototypes for getopt_long and
61 getopt_long_only; they declare "char **argv". libc uses prototypes
62 with "char *const *argv" that are incorrect because getopt_long and
63 getopt_long_only can permute argv; this is required for backward
64 compatibility (e.g., for LSB 2.0.1).
65
66 This used to be `#if defined __GETOPT_PREFIX && !defined __need_getopt',
67 but it caused redefinition warnings if both unistd.h and getopt.h were
68 included, since unistd.h includes getopt.h having previously defined
69 __need_getopt.
70
71 The only place where __getopt_argv_const is used is in definitions
72 of getopt_long and getopt_long_only below, but these are visible
73 only if __need_getopt is not defined, so it is quite safe to rewrite
74 the conditional as follows:
75*/
76#if !defined __need_getopt
77# if defined __GETOPT_PREFIX
78# define __getopt_argv_const /* empty */
79# else
80# define __getopt_argv_const const
81# endif
82#endif
83
84/* If __GNU_LIBRARY__ is not already defined, either we are being used
85 standalone, or this is the first header included in the source file.
86 If we are being used with glibc, we need to include <features.h>, but
87 that does not exist if we are standalone. So: if __GNU_LIBRARY__ is
88 not defined, include <ctype.h>, which will pull in <features.h> for us
89 if it's from glibc. (Why ctype.h? It's guaranteed to exist and it
90 doesn't flood the namespace with stuff the way some other headers do.) */
91#if !defined __GNU_LIBRARY__
92# include <ctype.h>
93#endif
94
95#ifndef __THROW
96# ifndef __GNUC_PREREQ
97# define __GNUC_PREREQ(maj, min) (0)
98# endif
99# if defined __cplusplus && __GNUC_PREREQ (2,8)
100# define __THROW throw ()
101# else
102# define __THROW
103# endif
104#endif
105
106#ifdef __cplusplus
107extern "C" {
108#endif
109
110/* For communication from `getopt' to the caller.
111 When `getopt' finds an option that takes an argument,
112 the argument value is returned here.
113 Also, when `ordering' is RETURN_IN_ORDER,
114 each non-option ARGV-element is returned here. */
115
116extern char *optarg;
117
118/* Index in ARGV of the next element to be scanned.
119 This is used for communication to and from the caller
120 and for communication between successive calls to `getopt'.
121
122 On entry to `getopt', zero means this is the first call; initialize.
123
124 When `getopt' returns -1, this is the index of the first of the
125 non-option elements that the caller should itself scan.
126
127 Otherwise, `optind' communicates from one call to the next
128 how much of ARGV has been scanned so far. */
129
130extern int optind;
131
132/* Callers store zero here to inhibit the error message `getopt' prints
133 for unrecognized options. */
134
135extern int opterr;
136
137/* Set to an option character which was unrecognized. */
138
139extern int optopt;
140
141#ifndef __need_getopt
142/* Describe the long-named options requested by the application.
143 The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
144 of `struct option' terminated by an element containing a name which is
145 zero.
146
147 The field `has_arg' is:
148 no_argument (or 0) if the option does not take an argument,
149 required_argument (or 1) if the option requires an argument,
150 optional_argument (or 2) if the option takes an optional argument.
151
152 If the field `flag' is not NULL, it points to a variable that is set
153 to the value given in the field `val' when the option is found, but
154 left unchanged if the option is not found.
155
156 To have a long-named option do something other than set an `int' to
157 a compiled-in constant, such as set a value from `optarg', set the
158 option's `flag' field to zero and its `val' field to a nonzero
159 value (the equivalent single-letter option character, if there is
160 one). For long options that have a zero `flag' field, `getopt'
161 returns the contents of the `val' field. */
162
163struct option
164{
165 const char *name;
166 /* has_arg can't be an enum because some compilers complain about
167 type mismatches in all the code that assumes it is an int. */
168 int has_arg;
169 int *flag;
170 int val;
171};
172
173/* Names for the values of the `has_arg' field of `struct option'. */
174
175# define no_argument 0
176# define required_argument 1
177# define optional_argument 2
178#endif /* need getopt */
179
180
181/* Get definitions and prototypes for functions to process the
182 arguments in ARGV (ARGC of them, minus the program name) for
183 options given in OPTS.
184
185 Return the option character from OPTS just read. Return -1 when
186 there are no more options. For unrecognized options, or options
187 missing arguments, `optopt' is set to the option letter, and '?' is
188 returned.
189
190 The OPTS string is a list of characters which are recognized option
191 letters, optionally followed by colons, specifying that that letter
192 takes an argument, to be placed in `optarg'.
193
194 If a letter in OPTS is followed by two colons, its argument is
195 optional. This behavior is specific to the GNU `getopt'.
196
197 The argument `--' causes premature termination of argument
198 scanning, explicitly telling `getopt' that there are no more
199 options.
200
201 If OPTS begins with `--', then non-option arguments are treated as
202 arguments to the option '\0'. This behavior is specific to the GNU
203 `getopt'. */
204
205extern int getopt (int ___argc, char *const *___argv, const char *__shortopts)
206 __THROW;
207
208#ifndef __need_getopt
209extern int getopt_long (int ___argc, char *__getopt_argv_const *___argv,
210 const char *__shortopts,
211 const struct option *__longopts, int *__longind)
212 __THROW;
213extern int getopt_long_only (int ___argc, char *__getopt_argv_const *___argv,
214 const char *__shortopts,
215 const struct option *__longopts, int *__longind)
216 __THROW;
217
218#endif
219
220#ifdef __cplusplus
221}
222#endif
223
224/* Make sure we later can get all the definitions and declarations. */
225#undef __need_getopt
226
227#endif /* getopt.h */
diff --git a/lib-src/getopt_int.h b/lib-src/getopt_int.h
new file mode 100644
index 00000000000..401579fd289
--- /dev/null
+++ b/lib-src/getopt_int.h
@@ -0,0 +1,131 @@
1/* Internal declarations for getopt.
2 Copyright (C) 1989-1994,1996-1999,2001,2003,2004
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19
20#ifndef _GETOPT_INT_H
21#define _GETOPT_INT_H 1
22
23extern int _getopt_internal (int ___argc, char **___argv,
24 const char *__shortopts,
25 const struct option *__longopts, int *__longind,
26 int __long_only, int __posixly_correct);
27
28
29/* Reentrant versions which can handle parsing multiple argument
30 vectors at the same time. */
31
32/* Data type for reentrant functions. */
33struct _getopt_data
34{
35 /* These have exactly the same meaning as the corresponding global
36 variables, except that they are used for the reentrant
37 versions of getopt. */
38 int optind;
39 int opterr;
40 int optopt;
41 char *optarg;
42
43 /* Internal members. */
44
45 /* True if the internal members have been initialized. */
46 int __initialized;
47
48 /* The next char to be scanned in the option-element
49 in which the last option character we returned was found.
50 This allows us to pick up the scan where we left off.
51
52 If this is zero, or a null string, it means resume the scan
53 by advancing to the next ARGV-element. */
54 char *__nextchar;
55
56 /* Describe how to deal with options that follow non-option ARGV-elements.
57
58 If the caller did not specify anything,
59 the default is REQUIRE_ORDER if the environment variable
60 POSIXLY_CORRECT is defined, PERMUTE otherwise.
61
62 REQUIRE_ORDER means don't recognize them as options;
63 stop option processing when the first non-option is seen.
64 This is what Unix does.
65 This mode of operation is selected by either setting the environment
66 variable POSIXLY_CORRECT, or using `+' as the first character
67 of the list of option characters, or by calling getopt.
68
69 PERMUTE is the default. We permute the contents of ARGV as we
70 scan, so that eventually all the non-options are at the end.
71 This allows options to be given in any order, even with programs
72 that were not written to expect this.
73
74 RETURN_IN_ORDER is an option available to programs that were
75 written to expect options and other ARGV-elements in any order
76 and that care about the ordering of the two. We describe each
77 non-option ARGV-element as if it were the argument of an option
78 with character code 1. Using `-' as the first character of the
79 list of option characters selects this mode of operation.
80
81 The special argument `--' forces an end of option-scanning regardless
82 of the value of `ordering'. In the case of RETURN_IN_ORDER, only
83 `--' can cause `getopt' to return -1 with `optind' != ARGC. */
84
85 enum
86 {
87 REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
88 } __ordering;
89
90 /* If the POSIXLY_CORRECT environment variable is set
91 or getopt was called. */
92 int __posixly_correct;
93
94
95 /* Handle permutation of arguments. */
96
97 /* Describe the part of ARGV that contains non-options that have
98 been skipped. `first_nonopt' is the index in ARGV of the first
99 of them; `last_nonopt' is the index after the last of them. */
100
101 int __first_nonopt;
102 int __last_nonopt;
103
104#if defined _LIBC && defined USE_NONOPTION_FLAGS
105 int __nonoption_flags_max_len;
106 int __nonoption_flags_len;
107# endif
108};
109
110/* The initializer is necessary to set OPTIND and OPTERR to their
111 default values and to clear the initialization flag. */
112#define _GETOPT_DATA_INITIALIZER { 1, 1 }
113
114extern int _getopt_internal_r (int ___argc, char **___argv,
115 const char *__shortopts,
116 const struct option *__longopts, int *__longind,
117 int __long_only, int __posixly_correct,
118 struct _getopt_data *__data);
119
120extern int _getopt_long_r (int ___argc, char **___argv,
121 const char *__shortopts,
122 const struct option *__longopts, int *__longind,
123 struct _getopt_data *__data);
124
125extern int _getopt_long_only_r (int ___argc, char **___argv,
126 const char *__shortopts,
127 const struct option *__longopts,
128 int *__longind,
129 struct _getopt_data *__data);
130
131#endif /* getopt_int.h */
diff --git a/lib-src/gettext.h b/lib-src/gettext.h
new file mode 100644
index 00000000000..285cb314ce9
--- /dev/null
+++ b/lib-src/gettext.h
@@ -0,0 +1,78 @@
1/* Convenience header for conditional use of GNU <libintl.h>.
2 Copyright (C) 1995-1998, 2000-2002, 2004 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17
18#ifndef _LIBGETTEXT_H
19#define _LIBGETTEXT_H 1
20
21/* NLS can be disabled through the configure --disable-nls option. */
22#if ENABLE_NLS
23
24/* Get declarations of GNU message catalog functions. */
25# include <libintl.h>
26
27#else
28
29/* Solaris /usr/include/locale.h includes /usr/include/libintl.h, which
30 chokes if dcgettext is defined as a macro. So include it now, to make
31 later inclusions of <locale.h> a NOP. We don't include <libintl.h>
32 as well because people using "gettext.h" will not include <libintl.h>,
33 and also including <libintl.h> would fail on SunOS 4, whereas <locale.h>
34 is OK. */
35#if defined(__sun)
36# include <locale.h>
37#endif
38
39/* Many header files from the libstdc++ coming with g++ 3.3 or newer include
40 <libintl.h>, which chokes if dcgettext is defined as a macro. So include
41 it now, to make later inclusions of <libintl.h> a NOP. */
42#if defined(__cplusplus) && defined(__GNUG__) && (__GNUC__ >= 3)
43# include <cstdlib>
44# if (__GLIBC__ >= 2) || _GLIBCXX_HAVE_LIBINTL_H
45# include <libintl.h>
46# endif
47#endif
48
49/* Disabled NLS.
50 The casts to 'const char *' serve the purpose of producing warnings
51 for invalid uses of the value returned from these functions.
52 On pre-ANSI systems without 'const', the config.h file is supposed to
53 contain "#define const". */
54# define gettext(Msgid) ((const char *) (Msgid))
55# define dgettext(Domainname, Msgid) ((const char *) (Msgid))
56# define dcgettext(Domainname, Msgid, Category) ((const char *) (Msgid))
57# define ngettext(Msgid1, Msgid2, N) \
58 ((N) == 1 ? (const char *) (Msgid1) : (const char *) (Msgid2))
59# define dngettext(Domainname, Msgid1, Msgid2, N) \
60 ((N) == 1 ? (const char *) (Msgid1) : (const char *) (Msgid2))
61# define dcngettext(Domainname, Msgid1, Msgid2, N, Category) \
62 ((N) == 1 ? (const char *) (Msgid1) : (const char *) (Msgid2))
63# define textdomain(Domainname) ((const char *) (Domainname))
64# define bindtextdomain(Domainname, Dirname) ((const char *) (Dirname))
65# define bind_textdomain_codeset(Domainname, Codeset) ((const char *) (Codeset))
66
67#endif
68
69/* A pseudo function call that serves as a marker for the automated
70 extraction of messages, but does not call gettext(). The run-time
71 translation is done at a different place in the code.
72 The argument, String, should be a literal string. Concatenated strings
73 and other string expressions won't work.
74 The macro's expansion is not parenthesized, so that it is suitable as
75 initializer for static 'char[]' or 'const char[]' variables. */
76#define gettext_noop(String) String
77
78#endif /* _LIBGETTEXT_H */