usb 2.17.0 → 2.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +14 -0
  3. package/binding.gyp +2 -9
  4. package/dist/usb/bindings.d.ts +27 -2
  5. package/dist/usb/bindings.js.map +1 -1
  6. package/dist/usb/index.d.ts +0 -29
  7. package/dist/usb/index.js +4 -18
  8. package/dist/usb/index.js.map +1 -1
  9. package/libusb/.clang-tidy +5 -3
  10. package/libusb/.private/ci-build.sh +5 -1
  11. package/libusb/AUTHORS +14 -0
  12. package/libusb/ChangeLog +15 -2
  13. package/libusb/README +8 -5
  14. package/libusb/Xcode/libusb.xcodeproj/project.pbxproj +4 -0
  15. package/libusb/configure.ac +12 -2
  16. package/libusb/examples/hotplugtest.c +19 -11
  17. package/libusb/examples/listdevs.c +41 -3
  18. package/libusb/examples/xusb.c +6 -1
  19. package/libusb/libusb/Makefile.am +4 -0
  20. package/libusb/libusb/core.c +175 -14
  21. package/libusb/libusb/descriptor.c +163 -14
  22. package/libusb/libusb/io.c +7 -3
  23. package/libusb/libusb/libusb-1.0.def +10 -0
  24. package/libusb/libusb/libusb.h +59 -9
  25. package/libusb/libusb/libusbi.h +89 -25
  26. package/libusb/libusb/os/darwin_usb.c +126 -46
  27. package/libusb/libusb/os/darwin_usb.h +10 -8
  28. package/libusb/libusb/os/emscripten_webusb.cpp +31 -10
  29. package/libusb/libusb/os/haiku_usb_raw.cpp +4 -0
  30. package/libusb/libusb/os/linux_usbfs.c +73 -25
  31. package/libusb/libusb/os/netbsd_usb.c +2 -0
  32. package/libusb/libusb/os/openbsd_usb.c +2 -0
  33. package/libusb/libusb/os/sunos_usb.c +2 -0
  34. package/libusb/libusb/os/threads_posix.c +3 -3
  35. package/libusb/libusb/os/threads_posix.h +8 -2
  36. package/libusb/libusb/os/threads_windows.h +2 -1
  37. package/libusb/libusb/os/windows_common.c +86 -1
  38. package/libusb/libusb/os/windows_common.h +20 -1
  39. package/libusb/libusb/os/windows_hotplug.c +321 -0
  40. package/libusb/libusb/os/windows_hotplug.h +28 -0
  41. package/libusb/libusb/os/windows_usbdk.c +16 -8
  42. package/libusb/libusb/os/windows_winusb.c +753 -41
  43. package/libusb/libusb/os/windows_winusb.h +11 -6
  44. package/libusb/libusb/version.h +1 -1
  45. package/libusb/libusb/version_nano.h +1 -1
  46. package/libusb/msvc/Base.props +1 -1
  47. package/libusb/msvc/Configuration.Base.props +2 -1
  48. package/libusb/msvc/Configuration.DynamicLibrary.props +12 -0
  49. package/libusb/msvc/ProjectConfigurations.Base.props +69 -16
  50. package/libusb/msvc/build_all.ps1 +2 -2
  51. package/libusb/msvc/config.h +4 -0
  52. package/libusb/msvc/getopt/bits/getopt_core.h +96 -0
  53. package/libusb/msvc/getopt/bits/getopt_ext.h +77 -0
  54. package/libusb/msvc/getopt/features.h +21 -0
  55. package/libusb/msvc/getopt/getopt.c +456 -705
  56. package/libusb/msvc/getopt/getopt.h +16 -158
  57. package/libusb/msvc/getopt/getopt1.c +40 -69
  58. package/libusb/msvc/getopt/getopt_int.h +118 -0
  59. package/libusb/msvc/getopt/gettext.h +7 -0
  60. package/libusb/msvc/getopt/unistd.h +5 -0
  61. package/libusb/msvc/getopt.vcxproj +11 -4
  62. package/libusb/msvc/libusb.sln +515 -268
  63. package/libusb/msvc/libusb_dll.vcxproj +2 -0
  64. package/libusb/msvc/libusb_static.vcxproj +2 -0
  65. package/libusb/msvc/xusb.vcxproj +1 -1
  66. package/libusb/tests/Makefile.am +10 -1
  67. package/libusb/tests/fuzz/corpus/bos/min.bos +0 -0
  68. package/libusb/tests/fuzz/corpus/descriptor_parsers/min_valid_config.bin +0 -0
  69. package/libusb/tests/fuzz/corpus/descriptor_parsers/regression_bug_a_endpoint_null.bin +0 -0
  70. package/libusb/tests/fuzz/corpus/descriptor_parsers/regression_bug_b_iad_oob.bin +0 -0
  71. package/libusb/tests/fuzz/fuzz_bos_descriptor.c +49 -0
  72. package/libusb/tests/fuzz/fuzz_descriptor_parsers.c +83 -0
  73. package/libusb/tests/stress_mt.c +2 -1
  74. package/libusb/tests/webusb-test-shim/index.js +6 -5
  75. package/libusb.gypi +5 -0
  76. package/package.json +1 -1
  77. package/prebuilds/android-arm/node.napi.armv7.node +0 -0
  78. package/prebuilds/android-arm64/node.napi.armv8.node +0 -0
  79. package/prebuilds/darwin-x64+arm64/node.napi.node +0 -0
  80. package/prebuilds/linux-arm/node.napi.armv6.node +0 -0
  81. package/prebuilds/linux-arm/node.napi.armv7.node +0 -0
  82. package/prebuilds/linux-arm64/node.napi.armv8.node +0 -0
  83. package/prebuilds/linux-ia32/node.napi.node +0 -0
  84. package/prebuilds/linux-x64/node.napi.glibc.node +0 -0
  85. package/prebuilds/linux-x64/node.napi.musl.node +0 -0
  86. package/prebuilds/win32-arm64/node.napi.node +0 -0
  87. package/prebuilds/win32-ia32/node.napi.node +0 -0
  88. package/prebuilds/win32-x64/node.napi.node +0 -0
  89. package/src/{hotplug/libusb.cc → hotplug.cc} +2 -3
  90. package/src/{hotplug/hotplug.h → hotplug.h} +2 -6
  91. package/src/node_usb.cc +3 -3
  92. package/test/usb.coffee +4 -4
  93. package/test/webusb.coffee +22 -12
  94. package/src/hotplug/windows.cc +0 -168
@@ -1,10 +1,7 @@
1
1
  /* Getopt for GNU.
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
4
- before changing it!
5
- Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001
6
- Free Software Foundation, Inc.
7
- This file is part of the GNU C Library.
2
+ Copyright (C) 1987-2026 Free Software Foundation, Inc.
3
+ This file is part of the GNU C Library and is also part of gnulib.
4
+ Patches to this file should be submitted to both projects.
8
5
 
9
6
  The GNU C Library is free software; you can redistribute it and/or
10
7
  modify it under the terms of the GNU Lesser General Public
@@ -17,131 +14,95 @@
17
14
  Lesser General Public License for more details.
18
15
 
19
16
  You should have received a copy of the GNU Lesser General Public
20
- License along with the GNU C Library; if not, write to the Free
21
- Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22
- 02111-1307 USA. */
23
-
24
- /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
25
- Ditto for AIX 3.2 and <stdlib.h>. */
26
- #ifndef _NO_PROTO
27
- # define _NO_PROTO
28
- #endif
17
+ License along with the GNU C Library; if not, see
18
+ <https://www.gnu.org/licenses/>. */
29
19
 
30
- #ifdef HAVE_CONFIG_H
20
+ #ifndef _LIBC
31
21
  # include <config.h>
32
22
  #endif
33
23
 
34
- #if !defined __STDC__ || !__STDC__
35
- /* This is a separate conditional since some stdc systems
36
- reject `defined (const)'. */
37
- # ifndef const
38
- # define const
39
- # endif
40
- #endif
24
+ #include "getopt.h"
41
25
 
42
26
  #include <stdio.h>
27
+ #include <stdlib.h>
28
+ #include <string.h>
29
+ #include <unistd.h>
43
30
 
44
- /* Comment out all this code if we are using the GNU C Library, and are not
45
- actually compiling the library itself. This code is part of the GNU C
46
- Library, but also included in many other GNU distributions. Compiling
47
- and linking in this code is a waste when using the GNU C library
48
- (especially if it is a shared library). Rather than having every GNU
49
- program understand `configure --with-gnu-libc' and omit the object files,
50
- it is simpler to just do this in the source for each such file. */
51
-
52
- #define GETOPT_INTERFACE_VERSION 2
53
- #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
54
- # include <gnu-versions.h>
55
- # if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
56
- # define ELIDE_CODE
57
- # endif
58
- #endif
59
-
60
- #ifndef ELIDE_CODE
61
-
62
-
63
- /* This needs to come after some library #include
64
- to get __GNU_LIBRARY__ defined. */
65
- #ifdef __GNU_LIBRARY__
66
- /* Don't include stdlib.h for non-GNU C libraries because some of them
67
- contain conflicting prototypes for getopt. */
68
- # include <stdlib.h>
69
- # include <unistd.h>
70
- #endif /* GNU C library. */
71
-
72
- #ifdef VMS
73
- # include <unixlib.h>
74
- # if HAVE_STRING_H - 0
75
- # include <string.h>
76
- # endif
77
- #endif
78
-
79
- #ifndef _
80
- /* This is for other GNU distributions with internationalized messages. */
81
- # if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
82
- # include <libintl.h>
83
- # ifndef _
84
- # define _(msgid) gettext (msgid)
85
- # endif
86
- # else
87
- # define _(msgid) (msgid)
31
+ #ifdef _LIBC
32
+ /* When used as part of glibc, error printing must be done differently
33
+ for standards compliance. getopt is not a cancellation point, so
34
+ it must not call functions that are, and it is specified by an
35
+ older standard than stdio locking, so it must not refer to
36
+ functions in the "user namespace" related to stdio locking.
37
+ Finally, it must use glibc's internal message translation so that
38
+ the messages are looked up in the proper text domain. */
39
+ # include <libintl.h>
40
+ # define fprintf __fxprintf_nocancel
41
+ # define flockfile(fp) _IO_flockfile (fp)
42
+ # define funlockfile(fp) _IO_funlockfile (fp)
43
+ #else
44
+ # include "gettext.h"
45
+ # define _(msgid) gettext (msgid)
46
+ /* When used standalone, flockfile and funlockfile might not be
47
+ available. */
48
+ # if (!defined _POSIX_THREAD_SAFE_FUNCTIONS \
49
+ || (defined _WIN32 && ! defined __CYGWIN__))
50
+ # define flockfile(fp) /* nop */
51
+ # define funlockfile(fp) /* nop */
88
52
  # endif
53
+ /* When used standalone, do not attempt to use alloca. */
54
+ # define __libc_use_alloca(size) 0
55
+ # undef alloca
56
+ # define alloca(size) (abort (), (void *)0)
89
57
  #endif
90
58
 
91
- /* This version of `getopt' appears to the caller like standard Unix `getopt'
92
- but it behaves differently for the user, since it allows the user
93
- to intersperse the options with the other arguments.
94
-
95
- As `getopt' works, it permutes the elements of ARGV so that,
96
- when it is done, all the options precede everything else. Thus
97
- all application programs are extended to handle flexible argument order.
98
-
99
- Setting the environment variable POSIXLY_CORRECT disables permutation.
100
- Then the behavior is completely standard.
101
-
102
- GNU application programs can use a third alternative mode in which
103
- they can distinguish the relative order of options and other arguments. */
104
-
105
- #include "getopt.h"
106
-
107
- /* For communication from `getopt' to the caller.
108
- When `getopt' finds an option that takes an argument,
59
+ /* This implementation of 'getopt' has three modes for handling
60
+ options interspersed with non-option arguments. It can stop
61
+ scanning for options at the first non-option argument encountered,
62
+ as POSIX specifies. It can continue scanning for options after the
63
+ first non-option argument, but permute 'argv' as it goes so that,
64
+ after 'getopt' is done, all the options precede all the non-option
65
+ arguments and 'optind' points to the first non-option argument.
66
+ Or, it can report non-option arguments as if they were arguments to
67
+ the option character '\x01'.
68
+
69
+ The default behavior of 'getopt_long' is to permute the argument list.
70
+ When this implementation is used standalone, the default behavior of
71
+ 'getopt' is to stop at the first non-option argument, but when it is
72
+ used as part of GNU libc it also permutes the argument list. In both
73
+ cases, setting the environment variable POSIXLY_CORRECT to any value
74
+ disables permutation.
75
+
76
+ If the first character of the OPTSTRING argument to 'getopt' or
77
+ 'getopt_long' is '+', both functions will stop at the first
78
+ non-option argument. If it is '-', both functions will report
79
+ non-option arguments as arguments to the option character '\x01'. */
80
+
81
+ #include "getopt_int.h"
82
+
83
+ /* For communication from 'getopt' to the caller.
84
+ When 'getopt' finds an option that takes an argument,
109
85
  the argument value is returned here.
110
- Also, when `ordering' is RETURN_IN_ORDER,
86
+ Also, when 'ordering' is RETURN_IN_ORDER,
111
87
  each non-option ARGV-element is returned here. */
112
88
 
113
89
  char *optarg;
114
90
 
115
91
  /* Index in ARGV of the next element to be scanned.
116
92
  This is used for communication to and from the caller
117
- and for communication between successive calls to `getopt'.
93
+ and for communication between successive calls to 'getopt'.
118
94
 
119
- On entry to `getopt', zero means this is the first call; initialize.
95
+ On entry to 'getopt', zero means this is the first call; initialize.
120
96
 
121
- When `getopt' returns -1, this is the index of the first of the
97
+ When 'getopt' returns -1, this is the index of the first of the
122
98
  non-option elements that the caller should itself scan.
123
99
 
124
- Otherwise, `optind' communicates from one call to the next
100
+ Otherwise, 'optind' communicates from one call to the next
125
101
  how much of ARGV has been scanned so far. */
126
102
 
127
103
  /* 1003.2 says this must be 1 before any call. */
128
104
  int optind = 1;
129
105
 
130
- /* Formerly, initialization of getopt depended on optind==0, which
131
- causes problems with re-calling getopt as programs generally don't
132
- know that. */
133
-
134
- int __getopt_initialized;
135
-
136
- /* The next char to be scanned in the option-element
137
- in which the last option character we returned was found.
138
- This allows us to pick up the scan where we left off.
139
-
140
- If this is zero, or a null string, it means resume the scan
141
- by advancing to the next ARGV-element. */
142
-
143
- static char *nextchar;
144
-
145
106
  /* Callers store zero here to inhibit the error message
146
107
  for unrecognized options. */
147
108
 
@@ -153,139 +114,9 @@ int opterr = 1;
153
114
 
154
115
  int optopt = '?';
155
116
 
156
- /* Describe how to deal with options that follow non-option ARGV-elements.
157
-
158
- If the caller did not specify anything,
159
- the default is REQUIRE_ORDER if the environment variable
160
- POSIXLY_CORRECT is defined, PERMUTE otherwise.
161
-
162
- REQUIRE_ORDER means don't recognize them as options;
163
- stop option processing when the first non-option is seen.
164
- This is what Unix does.
165
- This mode of operation is selected by either setting the environment
166
- variable POSIXLY_CORRECT, or using `+' as the first character
167
- of the list of option characters.
168
-
169
- PERMUTE is the default. We permute the contents of ARGV as we scan,
170
- so that eventually all the non-options are at the end. This allows options
171
- to be given in any order, even with programs that were not written to
172
- expect this.
117
+ /* Keep a global copy of all internal members of getopt_data. */
173
118
 
174
- RETURN_IN_ORDER is an option available to programs that were written
175
- to expect options and other ARGV-elements in any order and that care about
176
- the ordering of the two. We describe each non-option ARGV-element
177
- as if it were the argument of an option with character code 1.
178
- Using `-' as the first character of the list of option characters
179
- selects this mode of operation.
180
-
181
- The special argument `--' forces an end of option-scanning regardless
182
- of the value of `ordering'. In the case of RETURN_IN_ORDER, only
183
- `--' can cause `getopt' to return -1 with `optind' != ARGC. */
184
-
185
- static enum
186
- {
187
- REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
188
- } ordering;
189
-
190
- /* Value of POSIXLY_CORRECT environment variable. */
191
- static char *posixly_correct;
192
-
193
- #ifdef __GNU_LIBRARY__
194
- /* We want to avoid inclusion of string.h with non-GNU libraries
195
- because there are many ways it can cause trouble.
196
- On some systems, it contains special magic macros that don't work
197
- in GCC. */
198
- # include <string.h>
199
- # define my_index strchr
200
- #else
201
-
202
- # if HAVE_STRING_H
203
- # include <string.h>
204
- # else
205
- # include <strings.h>
206
- # endif
207
-
208
- /* Avoid depending on library functions or files
209
- whose names are inconsistent. */
210
-
211
- #ifndef getenv
212
- #ifdef _MSC_VER
213
- // DDK will complain if you don't use the stdlib defined getenv
214
- #include <stdlib.h>
215
- #else
216
- extern char *getenv ();
217
- #endif
218
- #endif
219
-
220
- static char *
221
- my_index (str, chr)
222
- const char *str;
223
- int chr;
224
- {
225
- while (*str)
226
- {
227
- if (*str == chr)
228
- return (char *) str;
229
- str++;
230
- }
231
- return 0;
232
- }
233
-
234
- /* If using GCC, we can safely declare strlen this way.
235
- If not using GCC, it is ok not to declare it. */
236
- #ifdef __GNUC__
237
- /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
238
- That was relevant to code that was here before. */
239
- # if (!defined __STDC__ || !__STDC__) && !defined strlen
240
- /* gcc with -traditional declares the built-in strlen to return int,
241
- and has done so at least since version 2.4.5. -- rms. */
242
- extern int strlen (const char *);
243
- # endif /* not __STDC__ */
244
- #endif /* __GNUC__ */
245
-
246
- #endif /* not __GNU_LIBRARY__ */
247
-
248
- /* Handle permutation of arguments. */
249
-
250
- /* Describe the part of ARGV that contains non-options that have
251
- been skipped. `first_nonopt' is the index in ARGV of the first of them;
252
- `last_nonopt' is the index after the last of them. */
253
-
254
- static int first_nonopt;
255
- static int last_nonopt;
256
-
257
- #ifdef _LIBC
258
- /* Stored original parameters.
259
- XXX This is no good solution. We should rather copy the args so
260
- that we can compare them later. But we must not use malloc(3). */
261
- extern int __libc_argc;
262
- extern char **__libc_argv;
263
-
264
- /* Bash 2.0 gives us an environment variable containing flags
265
- indicating ARGV elements that should not be considered arguments. */
266
-
267
- # ifdef USE_NONOPTION_FLAGS
268
- /* Defined in getopt_init.c */
269
- extern char *__getopt_nonoption_flags;
270
-
271
- static int nonoption_flags_max_len;
272
- static int nonoption_flags_len;
273
- # endif
274
-
275
- # ifdef USE_NONOPTION_FLAGS
276
- # define SWAP_FLAGS(ch1, ch2) \
277
- if (nonoption_flags_len > 0) \
278
- { \
279
- char __tmp = __getopt_nonoption_flags[ch1]; \
280
- __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
281
- __getopt_nonoption_flags[ch2] = __tmp; \
282
- }
283
- # else
284
- # define SWAP_FLAGS(ch1, ch2)
285
- # endif
286
- #else /* !_LIBC */
287
- # define SWAP_FLAGS(ch1, ch2)
288
- #endif /* _LIBC */
119
+ static struct _getopt_data getopt_data;
289
120
 
290
121
  /* Exchange two adjacent subsequences of ARGV.
291
122
  One subsequence is elements [first_nonopt,last_nonopt)
@@ -293,20 +124,15 @@ static int nonoption_flags_len;
293
124
  The other is elements [last_nonopt,optind), which contains all
294
125
  the options processed since those non-options were skipped.
295
126
 
296
- `first_nonopt' and `last_nonopt' are relocated so that they describe
127
+ 'first_nonopt' and 'last_nonopt' are relocated so that they describe
297
128
  the new indices of the non-options in ARGV after they are moved. */
298
129
 
299
- #if defined __STDC__ && __STDC__
300
- static void exchange (char **);
301
- #endif
302
-
303
130
  static void
304
- exchange (argv)
305
- char **argv;
131
+ exchange (char **argv, struct _getopt_data *d)
306
132
  {
307
- int bottom = first_nonopt;
308
- int middle = last_nonopt;
309
- int top = optind;
133
+ int bottom = d->__first_nonopt;
134
+ int middle = d->__last_nonopt;
135
+ int top = d->optind;
310
136
  char *tem;
311
137
 
312
138
  /* Exchange the shorter segment with the far end of the longer segment.
@@ -314,35 +140,13 @@ exchange (argv)
314
140
  It leaves the longer segment in the right place overall,
315
141
  but it consists of two parts that need to be swapped next. */
316
142
 
317
- #if defined _LIBC && defined USE_NONOPTION_FLAGS
318
- /* First make sure the handling of the `__getopt_nonoption_flags'
319
- string can work normally. Our top argument must be in the range
320
- of the string. */
321
- if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
322
- {
323
- /* We must extend the array. The user plays games with us and
324
- presents new arguments. */
325
- char *new_str = malloc (top + 1);
326
- if (new_str == NULL)
327
- nonoption_flags_len = nonoption_flags_max_len = 0;
328
- else
329
- {
330
- memset (__mempcpy (new_str, __getopt_nonoption_flags,
331
- nonoption_flags_max_len),
332
- '\0', top + 1 - nonoption_flags_max_len);
333
- nonoption_flags_max_len = top + 1;
334
- __getopt_nonoption_flags = new_str;
335
- }
336
- }
337
- #endif
338
-
339
143
  while (top > middle && middle > bottom)
340
144
  {
341
145
  if (top - middle > middle - bottom)
342
146
  {
343
147
  /* Bottom segment is the short one. */
344
148
  int len = middle - bottom;
345
- register int i;
149
+ int i;
346
150
 
347
151
  /* Swap it with the top part of the top segment. */
348
152
  for (i = 0; i < len; i++)
@@ -350,7 +154,6 @@ exchange (argv)
350
154
  tem = argv[bottom + i];
351
155
  argv[bottom + i] = argv[top - (middle - bottom) + i];
352
156
  argv[top - (middle - bottom) + i] = tem;
353
- SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
354
157
  }
355
158
  /* Exclude the moved bottom segment from further swapping. */
356
159
  top -= len;
@@ -359,7 +162,7 @@ exchange (argv)
359
162
  {
360
163
  /* Top segment is the short one. */
361
164
  int len = top - middle;
362
- register int i;
165
+ int i;
363
166
 
364
167
  /* Swap it with the bottom part of the bottom segment. */
365
168
  for (i = 0; i < len; i++)
@@ -367,7 +170,6 @@ exchange (argv)
367
170
  tem = argv[bottom + i];
368
171
  argv[bottom + i] = argv[middle + i];
369
172
  argv[middle + i] = tem;
370
- SWAP_FLAGS (bottom + i, middle + i);
371
173
  }
372
174
  /* Exclude the moved top segment from further swapping. */
373
175
  bottom += len;
@@ -376,128 +178,286 @@ exchange (argv)
376
178
 
377
179
  /* Update records for the slots the non-options now occupy. */
378
180
 
379
- first_nonopt += (optind - last_nonopt);
380
- last_nonopt = optind;
181
+ d->__first_nonopt += (d->optind - d->__last_nonopt);
182
+ d->__last_nonopt = d->optind;
381
183
  }
382
184
 
383
- /* Initialize the internal data when the first call is made. */
185
+ /* Process the argument starting with d->__nextchar as a long option.
186
+ d->optind should *not* have been advanced over this argument.
187
+
188
+ If the value returned is -1, it was not actually a long option, the
189
+ state is unchanged, and the argument should be processed as a set
190
+ of short options (this can only happen when long_only is true).
191
+ Otherwise, the option (and its argument, if any) have been consumed
192
+ and the return value is the value to return from _getopt_internal_r. */
193
+ static int
194
+ process_long_option (int argc, char **argv, const char *optstring,
195
+ const struct option *longopts, int *longind,
196
+ int long_only, struct _getopt_data *d,
197
+ int print_errors, const char *prefix)
198
+ {
199
+ char *nameend;
200
+ size_t namelen;
201
+ const struct option *p;
202
+ const struct option *pfound = NULL;
203
+ int n_options;
204
+ int option_index = 0;
205
+
206
+ for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
207
+ /* Do nothing. */ ;
208
+ namelen = nameend - d->__nextchar;
209
+
210
+ /* First look for an exact match, counting the options as a side
211
+ effect. */
212
+ for (p = longopts, n_options = 0; p->name; p++, n_options++)
213
+ if (!strncmp (p->name, d->__nextchar, namelen)
214
+ && namelen == strlen (p->name))
215
+ {
216
+ /* Exact match found. */
217
+ pfound = p;
218
+ option_index = n_options;
219
+ break;
220
+ }
221
+
222
+ if (pfound == NULL)
223
+ {
224
+ /* Didn't find an exact match, so look for abbreviations. */
225
+ unsigned char *ambig_set = NULL;
226
+ int ambig_malloced = 0;
227
+ int ambig_fallback = 0;
228
+ int indfound = -1;
229
+
230
+ for (p = longopts, option_index = 0; p->name; p++, option_index++)
231
+ if (!strncmp (p->name, d->__nextchar, namelen))
232
+ {
233
+ if (pfound == NULL)
234
+ {
235
+ /* First nonexact match found. */
236
+ pfound = p;
237
+ indfound = option_index;
238
+ }
239
+ else if (long_only
240
+ || pfound->has_arg != p->has_arg
241
+ || pfound->flag != p->flag
242
+ || pfound->val != p->val)
243
+ {
244
+ /* Second or later nonexact match found. */
245
+ if (!ambig_fallback)
246
+ {
247
+ if (!print_errors)
248
+ /* Don't waste effort tracking the ambig set if
249
+ we're not going to print it anyway. */
250
+ ambig_fallback = 1;
251
+ else if (!ambig_set)
252
+ {
253
+ if (__libc_use_alloca (n_options))
254
+ ambig_set = alloca (n_options);
255
+ else if ((ambig_set = malloc (n_options)) == NULL)
256
+ /* Fall back to simpler error message. */
257
+ ambig_fallback = 1;
258
+ else
259
+ ambig_malloced = 1;
260
+
261
+ if (ambig_set)
262
+ {
263
+ memset (ambig_set, 0, n_options);
264
+ ambig_set[indfound] = 1;
265
+ }
266
+ }
267
+ if (ambig_set)
268
+ ambig_set[option_index] = 1;
269
+ }
270
+ }
271
+ }
272
+
273
+ if (ambig_set || ambig_fallback)
274
+ {
275
+ if (print_errors)
276
+ {
277
+ if (ambig_fallback)
278
+ fprintf (stderr, _("%s: option '%s%s' is ambiguous\n"),
279
+ argv[0], prefix, d->__nextchar);
280
+ else
281
+ {
282
+ flockfile (stderr);
283
+ fprintf (stderr,
284
+ _("%s: option '%s%s' is ambiguous; possibilities:"),
285
+ argv[0], prefix, d->__nextchar);
286
+
287
+ for (option_index = 0; option_index < n_options; option_index++)
288
+ if (ambig_set[option_index])
289
+ fprintf (stderr, " '%s%s'",
290
+ prefix, longopts[option_index].name);
291
+
292
+ /* This must use 'fprintf' even though it's only
293
+ printing a single character, so that it goes through
294
+ __fxprintf_nocancel when compiled as part of glibc. */
295
+ fprintf (stderr, "\n");
296
+ funlockfile (stderr);
297
+ }
298
+ }
299
+ if (ambig_malloced)
300
+ free (ambig_set);
301
+ d->__nextchar += strlen (d->__nextchar);
302
+ d->optind++;
303
+ d->optopt = 0;
304
+ return '?';
305
+ }
306
+
307
+ option_index = indfound;
308
+ }
309
+
310
+ if (pfound == NULL)
311
+ {
312
+ /* Can't find it as a long option. If this is not getopt_long_only,
313
+ or the option starts with '--' or is not a valid short option,
314
+ then it's an error. */
315
+ if (!long_only || argv[d->optind][1] == '-'
316
+ || strchr (optstring, *d->__nextchar) == NULL)
317
+ {
318
+ if (print_errors)
319
+ fprintf (stderr, _("%s: unrecognized option '%s%s'\n"),
320
+ argv[0], prefix, d->__nextchar);
321
+
322
+ d->__nextchar = NULL;
323
+ d->optind++;
324
+ d->optopt = 0;
325
+ return '?';
326
+ }
327
+
328
+ /* Otherwise interpret it as a short option. */
329
+ return -1;
330
+ }
331
+
332
+ /* We have found a matching long option. Consume it. */
333
+ d->optind++;
334
+ d->__nextchar = NULL;
335
+ if (*nameend)
336
+ {
337
+ /* Don't test has_arg with >, because some C compilers don't
338
+ allow it to be used on enums. */
339
+ if (pfound->has_arg)
340
+ d->optarg = nameend + 1;
341
+ else
342
+ {
343
+ if (print_errors)
344
+ fprintf (stderr,
345
+ _("%s: option '%s%s' doesn't allow an argument\n"),
346
+ argv[0], prefix, pfound->name);
347
+
348
+ d->optopt = pfound->val;
349
+ return '?';
350
+ }
351
+ }
352
+ else if (pfound->has_arg == 1)
353
+ {
354
+ if (d->optind < argc)
355
+ d->optarg = argv[d->optind++];
356
+ else
357
+ {
358
+ if (print_errors)
359
+ fprintf (stderr,
360
+ _("%s: option '%s%s' requires an argument\n"),
361
+ argv[0], prefix, pfound->name);
362
+
363
+ d->optopt = pfound->val;
364
+ return optstring[0] == ':' ? ':' : '?';
365
+ }
366
+ }
367
+
368
+ if (longind != NULL)
369
+ *longind = option_index;
370
+ if (pfound->flag)
371
+ {
372
+ *(pfound->flag) = pfound->val;
373
+ return 0;
374
+ }
375
+ return pfound->val;
376
+ }
377
+
378
+ /* Initialize internal data upon the first call to getopt. */
384
379
 
385
- #if defined __STDC__ && __STDC__
386
- static const char *_getopt_initialize (int, char *const *, const char *);
387
- #endif
388
380
  static const char *
389
- _getopt_initialize (argc, argv, optstring)
390
- int argc;
391
- char *const *argv;
392
- const char *optstring;
381
+ _getopt_initialize (_GL_UNUSED int argc,
382
+ _GL_UNUSED char **argv, const char *optstring,
383
+ struct _getopt_data *d, int posixly_correct)
393
384
  {
394
385
  /* Start processing options with ARGV-element 1 (since ARGV-element 0
395
386
  is the program name); the sequence of previously skipped
396
387
  non-option ARGV-elements is empty. */
388
+ if (d->optind == 0)
389
+ d->optind = 1;
397
390
 
398
- first_nonopt = last_nonopt = optind;
399
-
400
- nextchar = NULL;
401
-
402
- posixly_correct = getenv ("POSIXLY_CORRECT");
391
+ d->__first_nonopt = d->__last_nonopt = d->optind;
392
+ d->__nextchar = NULL;
403
393
 
404
394
  /* Determine how to handle the ordering of options and nonoptions. */
405
-
406
395
  if (optstring[0] == '-')
407
396
  {
408
- ordering = RETURN_IN_ORDER;
397
+ d->__ordering = RETURN_IN_ORDER;
409
398
  ++optstring;
410
399
  }
411
400
  else if (optstring[0] == '+')
412
401
  {
413
- ordering = REQUIRE_ORDER;
402
+ d->__ordering = REQUIRE_ORDER;
414
403
  ++optstring;
415
404
  }
416
- else if (posixly_correct != NULL)
417
- ordering = REQUIRE_ORDER;
405
+ else if (posixly_correct || !!getenv ("POSIXLY_CORRECT"))
406
+ d->__ordering = REQUIRE_ORDER;
418
407
  else
419
- ordering = PERMUTE;
420
-
421
- #if defined _LIBC && defined USE_NONOPTION_FLAGS
422
- if (posixly_correct == NULL
423
- && argc == __libc_argc && argv == __libc_argv)
424
- {
425
- if (nonoption_flags_max_len == 0)
426
- {
427
- if (__getopt_nonoption_flags == NULL
428
- || __getopt_nonoption_flags[0] == '\0')
429
- nonoption_flags_max_len = -1;
430
- else
431
- {
432
- const char *orig_str = __getopt_nonoption_flags;
433
- int len = nonoption_flags_max_len = strlen (orig_str);
434
- if (nonoption_flags_max_len < argc)
435
- nonoption_flags_max_len = argc;
436
- __getopt_nonoption_flags =
437
- (char *) malloc (nonoption_flags_max_len);
438
- if (__getopt_nonoption_flags == NULL)
439
- nonoption_flags_max_len = -1;
440
- else
441
- memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
442
- '\0', nonoption_flags_max_len - len);
443
- }
444
- }
445
- nonoption_flags_len = nonoption_flags_max_len;
446
- }
447
- else
448
- nonoption_flags_len = 0;
449
- #endif
408
+ d->__ordering = PERMUTE;
450
409
 
410
+ d->__initialized = 1;
451
411
  return optstring;
452
412
  }
453
-
413
+
454
414
  /* Scan elements of ARGV (whose length is ARGC) for option characters
455
415
  given in OPTSTRING.
456
416
 
457
417
  If an element of ARGV starts with '-', and is not exactly "-" or "--",
458
418
  then it is an option element. The characters of this element
459
- (aside from the initial '-') are option characters. If `getopt'
419
+ (aside from the initial '-') are option characters. If 'getopt'
460
420
  is called repeatedly, it returns successively each of the option characters
461
421
  from each of the option elements.
462
422
 
463
- If `getopt' finds another option character, it returns that character,
464
- updating `optind' and `nextchar' so that the next call to `getopt' can
423
+ If 'getopt' finds another option character, it returns that character,
424
+ updating 'optind' and 'nextchar' so that the next call to 'getopt' can
465
425
  resume the scan with the following option character or ARGV-element.
466
426
 
467
- If there are no more option characters, `getopt' returns -1.
468
- Then `optind' is the index in ARGV of the first ARGV-element
427
+ If there are no more option characters, 'getopt' returns -1.
428
+ Then 'optind' is the index in ARGV of the first ARGV-element
469
429
  that is not an option. (The ARGV-elements have been permuted
470
430
  so that those that are not options now come last.)
471
431
 
472
432
  OPTSTRING is a string containing the legitimate option characters.
473
433
  If an option character is seen that is not listed in OPTSTRING,
474
- return '?' after printing an error message. If you set `opterr' to
434
+ return '?' after printing an error message. If you set 'opterr' to
475
435
  zero, the error message is suppressed but we still return '?'.
476
436
 
477
437
  If a char in OPTSTRING is followed by a colon, that means it wants an arg,
478
438
  so the following text in the same ARGV-element, or the text of the following
479
- ARGV-element, is returned in `optarg'. Two colons mean an option that
439
+ ARGV-element, is returned in 'optarg'. Two colons mean an option that
480
440
  wants an optional arg; if there is text in the current ARGV-element,
481
- it is returned in `optarg', otherwise `optarg' is set to zero.
441
+ it is returned in 'optarg', otherwise 'optarg' is set to zero.
482
442
 
483
- If OPTSTRING starts with `-' or `+', it requests different methods of
443
+ If OPTSTRING starts with '-' or '+', it requests different methods of
484
444
  handling the non-option ARGV-elements.
485
445
  See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
486
446
 
487
- Long-named options begin with `--' instead of `-'.
447
+ Long-named options begin with '--' instead of '-'.
488
448
  Their names may be abbreviated as long as the abbreviation is unique
489
449
  or is an exact match for some defined option. If they have an
490
450
  argument, it follows the option name in the same ARGV-element, separated
491
- from the option name by a `=', or else the in next ARGV-element.
492
- When `getopt' finds a long-named option, it returns 0 if that option's
493
- `flag' field is nonzero, the value of the option's `val' field
494
- if the `flag' field is zero.
451
+ from the option name by a '=', or else the in next ARGV-element.
452
+ When 'getopt' finds a long-named option, it returns 0 if that option's
453
+ 'flag' field is nonzero, the value of the option's 'val' field
454
+ if the 'flag' field is zero.
495
455
 
496
456
  The elements of ARGV aren't really const, because we permute them.
497
457
  But we pretend they're const in the prototype to be compatible
498
458
  with other systems.
499
459
 
500
- LONGOPTS is a vector of `struct option' terminated by an
460
+ LONGOPTS is a vector of 'struct option' terminated by an
501
461
  element containing a name which is zero.
502
462
 
503
463
  LONGIND returns the index in LONGOPT of the long-named option found.
@@ -508,99 +468,86 @@ _getopt_initialize (argc, argv, optstring)
508
468
  long-named options. */
509
469
 
510
470
  int
511
- _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
512
- int argc;
513
- char *const *argv;
514
- const char *optstring;
515
- const struct option *longopts;
516
- int *longind;
517
- int long_only;
471
+ _getopt_internal_r (int argc, char **argv, const char *optstring,
472
+ const struct option *longopts, int *longind,
473
+ int long_only, struct _getopt_data *d, int posixly_correct)
518
474
  {
519
- int print_errors = opterr;
520
- if (optstring[0] == ':')
521
- print_errors = 0;
475
+ int print_errors = d->opterr;
522
476
 
523
477
  if (argc < 1)
524
478
  return -1;
525
479
 
526
- optarg = NULL;
480
+ d->optarg = NULL;
527
481
 
528
- if (optind == 0 || !__getopt_initialized)
529
- {
530
- if (optind == 0)
531
- optind = 1; /* Don't scan ARGV[0], the program name. */
532
- optstring = _getopt_initialize (argc, argv, optstring);
533
- __getopt_initialized = 1;
534
- }
482
+ if (d->optind == 0 || !d->__initialized)
483
+ optstring = _getopt_initialize (argc, argv, optstring, d, posixly_correct);
484
+ else if (optstring[0] == '-' || optstring[0] == '+')
485
+ optstring++;
535
486
 
536
- /* Test whether ARGV[optind] points to a non-option argument.
537
- Either it does not have option syntax, or there is an environment flag
538
- from the shell indicating it is not an option. The later information
539
- is only used when the used in the GNU libc. */
540
- #if defined _LIBC && defined USE_NONOPTION_FLAGS
541
- # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
542
- || (optind < nonoption_flags_len \
543
- && __getopt_nonoption_flags[optind] == '1'))
544
- #else
545
- # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
546
- #endif
487
+ if (optstring[0] == ':')
488
+ print_errors = 0;
547
489
 
548
- if (nextchar == NULL || *nextchar == '\0')
490
+ /* Test whether ARGV[optind] points to a non-option argument. */
491
+ #define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
492
+
493
+ if (d->__nextchar == NULL || *d->__nextchar == '\0')
549
494
  {
550
495
  /* Advance to the next ARGV-element. */
551
496
 
552
497
  /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
553
498
  moved back by the user (who may also have changed the arguments). */
554
- if (last_nonopt > optind)
555
- last_nonopt = optind;
556
- if (first_nonopt > optind)
557
- first_nonopt = optind;
499
+ if (d->__last_nonopt > d->optind)
500
+ d->__last_nonopt = d->optind;
501
+ if (d->__first_nonopt > d->optind)
502
+ d->__first_nonopt = d->optind;
558
503
 
559
- if (ordering == PERMUTE)
504
+ if (d->__ordering == PERMUTE)
560
505
  {
561
506
  /* If we have just processed some options following some non-options,
562
507
  exchange them so that the options come first. */
563
508
 
564
- if (first_nonopt != last_nonopt && last_nonopt != optind)
565
- exchange ((char **) argv);
566
- else if (last_nonopt != optind)
567
- first_nonopt = optind;
509
+ if (d->__first_nonopt != d->__last_nonopt
510
+ && d->__last_nonopt != d->optind)
511
+ exchange (argv, d);
512
+ else if (d->__last_nonopt != d->optind)
513
+ d->__first_nonopt = d->optind;
568
514
 
569
515
  /* Skip any additional non-options
570
516
  and extend the range of non-options previously skipped. */
571
517
 
572
- while (optind < argc && NONOPTION_P)
573
- optind++;
574
- last_nonopt = optind;
518
+ while (d->optind < argc && NONOPTION_P)
519
+ d->optind++;
520
+ d->__last_nonopt = d->optind;
575
521
  }
576
522
 
577
- /* The special ARGV-element `--' means premature end of options.
523
+ /* The special ARGV-element '--' means premature end of options.
578
524
  Skip it like a null option,
579
525
  then exchange with previous non-options as if it were an option,
580
526
  then skip everything else like a non-option. */
581
527
 
582
- if (optind != argc && !strcmp (argv[optind], "--"))
528
+ if (d->optind != argc && !strcmp (argv[d->optind], "--"))
583
529
  {
584
- optind++;
530
+ d->optind++;
585
531
 
586
- if (first_nonopt != last_nonopt && last_nonopt != optind)
587
- exchange ((char **) argv);
588
- else if (first_nonopt == last_nonopt)
589
- first_nonopt = optind;
590
- last_nonopt = argc;
532
+ if (d->__first_nonopt != d->__last_nonopt
533
+ && d->__last_nonopt != d->optind)
534
+ exchange (argv, d);
535
+ else if (d->__first_nonopt == d->__last_nonopt)
536
+ d->__first_nonopt = d->optind;
537
+ d->__last_nonopt = argc;
591
538
 
592
- optind = argc;
539
+ d->optind = argc;
593
540
  }
594
541
 
595
542
  /* If we have done all the ARGV-elements, stop the scan
596
543
  and back over any non-options that we skipped and permuted. */
597
544
 
598
- if (optind == argc)
545
+ if (d->optind == argc)
599
546
  {
600
547
  /* Set the next-arg-index to point at the non-options
601
548
  that we previously skipped, so the caller will digest them. */
602
- if (first_nonopt != last_nonopt)
603
- optind = first_nonopt;
549
+ if (d->__first_nonopt != d->__last_nonopt)
550
+ d->optind = d->__first_nonopt;
604
551
  return -1;
605
552
  }
606
553
 
@@ -609,224 +556,87 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
609
556
 
610
557
  if (NONOPTION_P)
611
558
  {
612
- if (ordering == REQUIRE_ORDER)
559
+ if (d->__ordering == REQUIRE_ORDER)
613
560
  return -1;
614
- optarg = argv[optind++];
561
+ d->optarg = argv[d->optind++];
615
562
  return 1;
616
563
  }
617
564
 
618
565
  /* We have found another option-ARGV-element.
619
- Skip the initial punctuation. */
620
-
621
- nextchar = (argv[optind] + 1
622
- + (longopts != NULL && argv[optind][1] == '-'));
623
- }
624
-
625
- /* Decode the current option-ARGV-element. */
626
-
627
- /* Check whether the ARGV-element is a long option.
628
-
629
- If long_only and the ARGV-element has the form "-f", where f is
630
- a valid short option, don't consider it an abbreviated form of
631
- a long option that starts with f. Otherwise there would be no
632
- way to give the -f short option.
633
-
634
- On the other hand, if there's a long option "fubar" and
635
- the ARGV-element is "-fu", do consider that an abbreviation of
636
- the long option, just like "--fu", and not "-f" with arg "u".
637
-
638
- This distinction seems to be the most useful approach. */
639
-
640
- if (longopts != NULL
641
- && (argv[optind][1] == '-'
642
- || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
643
- {
644
- char *nameend;
645
- const struct option *p;
646
- const struct option *pfound = NULL;
647
- int exact = 0;
648
- int ambig = 0;
649
- int indfound = -1;
650
- int option_index;
651
-
652
- for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
653
- /* Do nothing. */ ;
654
-
655
- /* Test all long options for either exact match
656
- or abbreviated matches. */
657
- for (p = longopts, option_index = 0; p->name; p++, option_index++)
658
- if (!strncmp (p->name, nextchar, nameend - nextchar))
659
- {
660
- if ((unsigned int) (nameend - nextchar)
661
- == (unsigned int) strlen (p->name))
662
- {
663
- /* Exact match found. */
664
- pfound = p;
665
- indfound = option_index;
666
- exact = 1;
667
- break;
668
- }
669
- else if (pfound == NULL)
670
- {
671
- /* First nonexact match found. */
672
- pfound = p;
673
- indfound = option_index;
674
- }
675
- else if (long_only
676
- || pfound->has_arg != p->has_arg
677
- || pfound->flag != p->flag
678
- || pfound->val != p->val)
679
- /* Second or later nonexact match found. */
680
- ambig = 1;
681
- }
682
-
683
- if (ambig && !exact)
684
- {
685
- if (print_errors)
686
- fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
687
- argv[0], argv[optind]);
688
- nextchar += strlen (nextchar);
689
- optind++;
690
- optopt = 0;
691
- return '?';
692
- }
693
-
694
- if (pfound != NULL)
566
+ Check whether it might be a long option. */
567
+ if (longopts)
695
568
  {
696
- option_index = indfound;
697
- optind++;
698
- if (*nameend)
569
+ if (argv[d->optind][1] == '-')
699
570
  {
700
- /* Don't test has_arg with >, because some C compilers don't
701
- allow it to be used on enums. */
702
- if (pfound->has_arg)
703
- optarg = nameend + 1;
704
- else
705
- {
706
- if (print_errors)
707
- {
708
- if (argv[optind - 1][1] == '-')
709
- /* --option */
710
- fprintf (stderr,
711
- _("%s: option `--%s' doesn't allow an argument\n"),
712
- argv[0], pfound->name);
713
- else
714
- /* +option or -option */
715
- fprintf (stderr,
716
- _("%s: option `%c%s' doesn't allow an argument\n"),
717
- argv[0], argv[optind - 1][0], pfound->name);
718
- }
719
-
720
- nextchar += strlen (nextchar);
721
-
722
- optopt = pfound->val;
723
- return '?';
724
- }
725
- }
726
- else if (pfound->has_arg == 1)
727
- {
728
- if (optind < argc)
729
- optarg = argv[optind++];
730
- else
731
- {
732
- if (print_errors)
733
- fprintf (stderr,
734
- _("%s: option `%s' requires an argument\n"),
735
- argv[0], argv[optind - 1]);
736
- nextchar += strlen (nextchar);
737
- optopt = pfound->val;
738
- return optstring[0] == ':' ? ':' : '?';
739
- }
571
+ /* "--foo" is always a long option. The special option
572
+ "--" was handled above. */
573
+ d->__nextchar = argv[d->optind] + 2;
574
+ return process_long_option (argc, argv, optstring, longopts,
575
+ longind, long_only, d,
576
+ print_errors, "--");
740
577
  }
741
- nextchar += strlen (nextchar);
742
- if (longind != NULL)
743
- *longind = option_index;
744
- if (pfound->flag)
745
- {
746
- *(pfound->flag) = pfound->val;
747
- return 0;
748
- }
749
- return pfound->val;
750
- }
751
578
 
752
- /* Can't find it as a long option. If this is not getopt_long_only,
753
- or the option starts with '--' or is not a valid short
754
- option, then it's an error.
755
- Otherwise interpret it as a short option. */
756
- if (!long_only || argv[optind][1] == '-'
757
- || my_index (optstring, *nextchar) == NULL)
758
- {
759
- if (print_errors)
579
+ /* If long_only and the ARGV-element has the form "-f",
580
+ where f is a valid short option, don't consider it an
581
+ abbreviated form of a long option that starts with f.
582
+ Otherwise there would be no way to give the -f short
583
+ option.
584
+
585
+ On the other hand, if there's a long option "fubar" and
586
+ the ARGV-element is "-fu", do consider that an
587
+ abbreviation of the long option, just like "--fu", and
588
+ not "-f" with arg "u".
589
+
590
+ This distinction seems to be the most useful approach. */
591
+ if (long_only && (argv[d->optind][2]
592
+ || !strchr (optstring, argv[d->optind][1])))
760
593
  {
761
- if (argv[optind][1] == '-')
762
- /* --option */
763
- fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
764
- argv[0], nextchar);
765
- else
766
- /* +option or -option */
767
- fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
768
- argv[0], argv[optind][0], nextchar);
594
+ int code;
595
+ d->__nextchar = argv[d->optind] + 1;
596
+ code = process_long_option (argc, argv, optstring, longopts,
597
+ longind, long_only, d,
598
+ print_errors, "-");
599
+ if (code != -1)
600
+ return code;
769
601
  }
770
- nextchar = (char *) "";
771
- optind++;
772
- optopt = 0;
773
- return '?';
774
602
  }
603
+
604
+ /* It is not a long option. Skip the initial punctuation. */
605
+ d->__nextchar = argv[d->optind] + 1;
775
606
  }
776
607
 
777
608
  /* Look at and handle the next short option-character. */
778
609
 
779
610
  {
780
- char c = *nextchar++;
781
- char *temp = my_index (optstring, c);
611
+ char c = *d->__nextchar++;
612
+ const char *temp = strchr (optstring, c);
782
613
 
783
- /* Increment `optind' when we start to process its last character. */
784
- if (*nextchar == '\0')
785
- ++optind;
614
+ /* Increment 'optind' when we start to process its last character. */
615
+ if (*d->__nextchar == '\0')
616
+ ++d->optind;
786
617
 
787
- if (temp == NULL || c == ':')
618
+ if (temp == NULL || c == ':' || c == ';')
788
619
  {
789
620
  if (print_errors)
790
- {
791
- if (posixly_correct)
792
- /* 1003.2 specifies the format of this message. */
793
- fprintf (stderr, _("%s: illegal option -- %c\n"),
794
- argv[0], c);
795
- else
796
- fprintf (stderr, _("%s: invalid option -- %c\n"),
797
- argv[0], c);
798
- }
799
- optopt = c;
621
+ fprintf (stderr, _("%s: invalid option -- '%c'\n"), argv[0], c);
622
+ d->optopt = c;
800
623
  return '?';
801
624
  }
625
+
802
626
  /* Convenience. Treat POSIX -W foo same as long option --foo */
803
- if (temp[0] == 'W' && temp[1] == ';')
627
+ if (temp[0] == 'W' && temp[1] == ';' && longopts != NULL)
804
628
  {
805
- char *nameend;
806
- const struct option *p;
807
- const struct option *pfound = NULL;
808
- int exact = 0;
809
- int ambig = 0;
810
- int indfound = 0;
811
- int option_index;
812
-
813
629
  /* This is an option that requires an argument. */
814
- if (*nextchar != '\0')
815
- {
816
- optarg = nextchar;
817
- /* If we end this ARGV-element by taking the rest as an arg,
818
- we must advance to the next element now. */
819
- optind++;
820
- }
821
- else if (optind == argc)
630
+ if (*d->__nextchar != '\0')
631
+ d->optarg = d->__nextchar;
632
+ else if (d->optind == argc)
822
633
  {
823
634
  if (print_errors)
824
- {
825
- /* 1003.2 specifies the format of this message. */
826
- fprintf (stderr, _("%s: option requires an argument -- %c\n"),
827
- argv[0], c);
828
- }
829
- optopt = c;
635
+ fprintf (stderr,
636
+ _("%s: option requires an argument -- '%c'\n"),
637
+ argv[0], c);
638
+
639
+ d->optopt = c;
830
640
  if (optstring[0] == ':')
831
641
  c = ':';
832
642
  else
@@ -834,139 +644,55 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
834
644
  return c;
835
645
  }
836
646
  else
837
- /* We already incremented `optind' once;
838
- increment it again when taking next ARGV-elt as argument. */
839
- optarg = argv[optind++];
840
-
841
- /* optarg is now the argument, see if it's in the
842
- table of longopts. */
843
-
844
- for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
845
- /* Do nothing. */ ;
846
-
847
- /* Test all long options for either exact match
848
- or abbreviated matches. */
849
- for (p = longopts, option_index = 0; p != NULL && p->name; p++, option_index++)
850
- if (!strncmp (p->name, nextchar, nameend - nextchar))
851
- {
852
- if ((unsigned int) (nameend - nextchar) == strlen (p->name))
853
- {
854
- /* Exact match found. */
855
- pfound = p;
856
- indfound = option_index;
857
- exact = 1;
858
- break;
859
- }
860
- else if (pfound == NULL)
861
- {
862
- /* First nonexact match found. */
863
- pfound = p;
864
- indfound = option_index;
865
- }
866
- else
867
- /* Second or later nonexact match found. */
868
- ambig = 1;
869
- }
870
- if (ambig && !exact)
871
- {
872
- if (print_errors)
873
- fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
874
- argv[0], argv[optind]);
875
- nextchar += strlen (nextchar);
876
- optind++;
877
- return '?';
878
- }
879
- if (pfound != NULL)
880
- {
881
- option_index = indfound;
882
- if (*nameend)
883
- {
884
- /* Don't test has_arg with >, because some C compilers don't
885
- allow it to be used on enums. */
886
- if (pfound->has_arg)
887
- optarg = nameend + 1;
888
- else
889
- {
890
- if (print_errors)
891
- fprintf (stderr, _("\
892
- %s: option `-W %s' doesn't allow an argument\n"),
893
- argv[0], pfound->name);
647
+ d->optarg = argv[d->optind];
894
648
 
895
- nextchar += strlen (nextchar);
896
- return '?';
897
- }
898
- }
899
- else if (pfound->has_arg == 1)
900
- {
901
- if (optind < argc)
902
- optarg = argv[optind++];
903
- else
904
- {
905
- if (print_errors)
906
- fprintf (stderr,
907
- _("%s: option `%s' requires an argument\n"),
908
- argv[0], argv[optind - 1]);
909
- nextchar += strlen (nextchar);
910
- return optstring[0] == ':' ? ':' : '?';
911
- }
912
- }
913
- nextchar += strlen (nextchar);
914
- if (longind != NULL)
915
- *longind = option_index;
916
- if (pfound->flag)
917
- {
918
- *(pfound->flag) = pfound->val;
919
- return 0;
920
- }
921
- return pfound->val;
922
- }
923
- nextchar = NULL;
924
- return 'W'; /* Let the application handle it. */
649
+ d->__nextchar = d->optarg;
650
+ d->optarg = NULL;
651
+ return process_long_option (argc, argv, optstring, longopts, longind,
652
+ 0 /* long_only */, d, print_errors, "-W ");
925
653
  }
926
654
  if (temp[1] == ':')
927
655
  {
928
656
  if (temp[2] == ':')
929
657
  {
930
658
  /* This is an option that accepts an argument optionally. */
931
- if (*nextchar != '\0')
659
+ if (*d->__nextchar != '\0')
932
660
  {
933
- optarg = nextchar;
934
- optind++;
661
+ d->optarg = d->__nextchar;
662
+ d->optind++;
935
663
  }
936
664
  else
937
- optarg = NULL;
938
- nextchar = NULL;
665
+ d->optarg = NULL;
666
+ d->__nextchar = NULL;
939
667
  }
940
668
  else
941
669
  {
942
670
  /* This is an option that requires an argument. */
943
- if (*nextchar != '\0')
671
+ if (*d->__nextchar != '\0')
944
672
  {
945
- optarg = nextchar;
673
+ d->optarg = d->__nextchar;
946
674
  /* If we end this ARGV-element by taking the rest as an arg,
947
675
  we must advance to the next element now. */
948
- optind++;
676
+ d->optind++;
949
677
  }
950
- else if (optind == argc)
678
+ else if (d->optind == argc)
951
679
  {
952
680
  if (print_errors)
953
- {
954
- /* 1003.2 specifies the format of this message. */
955
- fprintf (stderr,
956
- _("%s: option requires an argument -- %c\n"),
957
- argv[0], c);
958
- }
959
- optopt = c;
681
+ fprintf (stderr,
682
+ _("%s: option requires an argument -- '%c'\n"),
683
+ argv[0], c);
684
+
685
+ d->optopt = c;
960
686
  if (optstring[0] == ':')
961
687
  c = ':';
962
688
  else
963
689
  c = '?';
964
690
  }
965
691
  else
966
- /* We already incremented `optind' once;
692
+ /* We already incremented 'optind' once;
967
693
  increment it again when taking next ARGV-elt as argument. */
968
- optarg = argv[optind++];
969
- nextchar = NULL;
694
+ d->optarg = argv[d->optind++];
695
+ d->__nextchar = NULL;
970
696
  }
971
697
  }
972
698
  return c;
@@ -974,28 +700,53 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
974
700
  }
975
701
 
976
702
  int
977
- getopt (argc, argv, optstring)
978
- int argc;
979
- char *const *argv;
980
- const char *optstring;
703
+ _getopt_internal (int argc, char **argv, const char *optstring,
704
+ const struct option *longopts, int *longind, int long_only,
705
+ int posixly_correct)
981
706
  {
982
- return _getopt_internal (argc, argv, optstring,
983
- (const struct option *) 0,
984
- (int *) 0,
985
- 0);
707
+ int result;
708
+
709
+ getopt_data.optind = optind;
710
+ getopt_data.opterr = opterr;
711
+
712
+ result = _getopt_internal_r (argc, argv, optstring, longopts,
713
+ longind, long_only, &getopt_data,
714
+ posixly_correct);
715
+
716
+ optind = getopt_data.optind;
717
+ optarg = getopt_data.optarg;
718
+ optopt = getopt_data.optopt;
719
+
720
+ return result;
986
721
  }
987
722
 
988
- #endif /* Not ELIDE_CODE. */
989
-
723
+ /* glibc gets a LSB-compliant getopt and a POSIX-complaint __posix_getopt.
724
+ Standalone applications just get a POSIX-compliant getopt.
725
+ POSIX and LSB both require these functions to take 'char *const *argv'
726
+ even though this is incorrect (because of the permutation). */
727
+ #define GETOPT_ENTRY(NAME, POSIXLY_CORRECT) \
728
+ int \
729
+ NAME (int argc, char *const *argv, const char *optstring) \
730
+ { \
731
+ return _getopt_internal (argc, (char **)argv, optstring, \
732
+ NULL, NULL, 0, POSIXLY_CORRECT); \
733
+ }
734
+
735
+ #ifdef _LIBC
736
+ GETOPT_ENTRY(getopt, 0)
737
+ GETOPT_ENTRY(__posix_getopt, 1)
738
+ #else
739
+ GETOPT_ENTRY(getopt, 1)
740
+ #endif
741
+
742
+
990
743
  #ifdef TEST
991
744
 
992
745
  /* Compile with -DTEST to make an executable for use in testing
993
- the above definition of `getopt'. */
746
+ the above definition of 'getopt'. */
994
747
 
995
748
  int
996
- main (argc, argv)
997
- int argc;
998
- char **argv;
749
+ main (int argc, char **argv)
999
750
  {
1000
751
  int c;
1001
752
  int digit_optind = 0;
@@ -1035,7 +786,7 @@ main (argc, argv)
1035
786
  break;
1036
787
 
1037
788
  case 'c':
1038
- printf ("option c with value `%s'\n", optarg);
789
+ printf ("option c with value '%s'\n", optarg);
1039
790
  break;
1040
791
 
1041
792
  case '?':