lsqpack 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +19 -0
  3. data/Gemfile.lock +82 -0
  4. data/LICENSE +21 -0
  5. data/README.md +36 -0
  6. data/Rakefile +19 -0
  7. data/Steepfile +6 -0
  8. data/ext/lsqpack/extconf.rb +18 -0
  9. data/ext/lsqpack/lsqpack.c +426 -0
  10. data/ext/lsqpack/lsqpack.h +6 -0
  11. data/lib/lsqpack/version.rb +5 -0
  12. data/lib/lsqpack.rb +30 -0
  13. data/ls-qpack/.appveyor.yml +14 -0
  14. data/ls-qpack/.cirrus.yml +6 -0
  15. data/ls-qpack/.travis.yml +32 -0
  16. data/ls-qpack/CMakeLists.txt +66 -0
  17. data/ls-qpack/LICENSE +21 -0
  18. data/ls-qpack/README.md +65 -0
  19. data/ls-qpack/bin/CMakeLists.txt +21 -0
  20. data/ls-qpack/bin/encode-int.c +87 -0
  21. data/ls-qpack/bin/fuzz-decode.c +247 -0
  22. data/ls-qpack/bin/interop-decode.c +433 -0
  23. data/ls-qpack/bin/interop-encode.c +554 -0
  24. data/ls-qpack/deps/xxhash/xxhash.c +941 -0
  25. data/ls-qpack/deps/xxhash/xxhash.h +160 -0
  26. data/ls-qpack/fuzz/decode/a/README +3 -0
  27. data/ls-qpack/fuzz/decode/a/preamble +0 -0
  28. data/ls-qpack/fuzz/decode/a/test-cases/id_000000,sig_06,src_000390,op_havoc,rep_4 +0 -0
  29. data/ls-qpack/fuzz/decode/a/test-cases/id_000000,sig_06,src_000579,op_flip1,pos_14 +0 -0
  30. data/ls-qpack/fuzz/decode/a/test-cases/id_000000,src_000000,op_flip2,pos_12 +0 -0
  31. data/ls-qpack/fuzz/decode/a/test-cases/id_000001,sig_11,src_000579,op_havoc,rep_4 +0 -0
  32. data/ls-qpack/fuzz/decode/a/test-cases/id_000002,sig_11,src_000481,op_int16,pos_15,val_-1 +0 -0
  33. data/ls-qpack/fuzz/decode/a/test-cases/id_000002,src_000000,op_havoc,rep_8 +0 -0
  34. data/ls-qpack/fuzz/decode/a/test-cases/id_000006,src_000285,op_flip2,pos_14 +0 -0
  35. data/ls-qpack/fuzz/decode/a/test-cases/id_000008,src_000285,op_flip2,pos_20 +0 -0
  36. data/ls-qpack/fuzz/decode/a/test-cases/id_000010,src_000306,op_flip2,pos_75 +0 -0
  37. data/ls-qpack/fuzz/decode/a/test-cases/id_000011,src_000344,op_havoc,rep_2 +0 -0
  38. data/ls-qpack/fuzz/decode/a/test-cases/id_000014,src_000366,op_flip2,pos_28 +0 -0
  39. data/ls-qpack/fuzz/decode/b/README +1 -0
  40. data/ls-qpack/fuzz/decode/b/preamble +0 -0
  41. data/ls-qpack/fuzz/decode/b/test-cases/seed +0 -0
  42. data/ls-qpack/fuzz/decode/c/setup.sh +3 -0
  43. data/ls-qpack/fuzz/decode/c/test-cases/fb-req.qif.proxygen.out.256.100.0-chopped +0 -0
  44. data/ls-qpack/fuzz/decode/d/preamble +0 -0
  45. data/ls-qpack/fuzz/decode/d/setup.sh +3 -0
  46. data/ls-qpack/fuzz/decode/d/test-cases/fb-resp.minhq.256.128.0.ack +0 -0
  47. data/ls-qpack/fuzz/input/256.100.1/fb-req.out.256.100.1 +0 -0
  48. data/ls-qpack/fuzz/input/256.100.1/fb-resp.out.256.100.1 +0 -0
  49. data/ls-qpack/fuzz/input/256.100.1/netbsd.out.256.100.1 +0 -0
  50. data/ls-qpack/huff-tables.h +136247 -0
  51. data/ls-qpack/lsqpack.c +5547 -0
  52. data/ls-qpack/lsqpack.h +768 -0
  53. data/ls-qpack/test/CMakeLists.txt +76 -0
  54. data/ls-qpack/test/lsqpack-test.h +43 -0
  55. data/ls-qpack/test/qifs/fb-req.qif +4917 -0
  56. data/ls-qpack/test/qifs/fb-resp.qif +5982 -0
  57. data/ls-qpack/test/qifs/long-codes.qif +5984 -0
  58. data/ls-qpack/test/qifs/netbsd.qif +235 -0
  59. data/ls-qpack/test/run-qif.pl +97 -0
  60. data/ls-qpack/test/run-scenario.sh +68 -0
  61. data/ls-qpack/test/scenarios/0.95-reset.sce +10 -0
  62. data/ls-qpack/test/scenarios/cancel-stream.sce +22 -0
  63. data/ls-qpack/test/scenarios/drain-2.sce +37 -0
  64. data/ls-qpack/test/scenarios/drain.sce +37 -0
  65. data/ls-qpack/test/scenarios/end-dst-2.sce +14 -0
  66. data/ls-qpack/test/scenarios/end-dst.sce +14 -0
  67. data/ls-qpack/test/scenarios/incl-name.sce +13 -0
  68. data/ls-qpack/test/scenarios/multi-byte-int-dyn-ref-1.sce +110 -0
  69. data/ls-qpack/test/scenarios/multi-byte-int-dyn-ref-2.sce +161 -0
  70. data/ls-qpack/test/scenarios/post-base-1.sce +10 -0
  71. data/ls-qpack/test/scenarios/post-base-2.sce +13 -0
  72. data/ls-qpack/test/scenarios/post-base-nr.sce +10 -0
  73. data/ls-qpack/test/scenarios/set-max-cap.sce +15 -0
  74. data/ls-qpack/test/test_enc_str.c +139 -0
  75. data/ls-qpack/test/test_get_stx_id.c +144 -0
  76. data/ls-qpack/test/test_huff_dec.c +399 -0
  77. data/ls-qpack/test/test_int.c +220 -0
  78. data/ls-qpack/test/test_qpack.c +856 -0
  79. data/ls-qpack/test/test_read_enc_stream.c +256 -0
  80. data/ls-qpack/tools/har2qif.pl +139 -0
  81. data/ls-qpack/tools/randomize-cookies.pl +41 -0
  82. data/ls-qpack/tools/sort-qif.pl +31 -0
  83. data/ls-qpack/wincompat/getopt.c +758 -0
  84. data/ls-qpack/wincompat/getopt.h +131 -0
  85. data/ls-qpack/wincompat/getopt1.c +188 -0
  86. data/ls-qpack/wincompat/sys/queue.h +859 -0
  87. data/lsqpack.gemspec +39 -0
  88. data/sig/lsqpack.rbs +29 -0
  89. metadata +135 -0
@@ -0,0 +1,758 @@
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 roland@gnu.ai.mit.edu
4
+ before changing it!
5
+
6
+ Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94
7
+ Free Software Foundation, Inc.
8
+
9
+ This program is free software; you can redistribute it and/or modify it
10
+ under the terms of the GNU General Public License as published by the
11
+ Free Software Foundation; either version 2, or (at your option) any
12
+ later version.
13
+
14
+ This program is distributed in the hope that it will be useful,
15
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ GNU General Public License for more details.
18
+
19
+ You should have received a copy of the GNU General Public License
20
+ along with this program; if not, write to the Free Software
21
+ Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22
+
23
+ /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
24
+ Ditto for AIX 3.2 and <stdlib.h>. */
25
+ #ifndef _NO_PROTO
26
+ #define _NO_PROTO
27
+ #endif
28
+ #pragma warning(disable:4131)
29
+
30
+ #ifdef HAVE_CONFIG_H
31
+ #if defined (emacs) || defined (CONFIG_BROKETS)
32
+ /* We use <config.h> instead of "config.h" so that a compilation
33
+ using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
34
+ (which it would do because it found this file in $srcdir). */
35
+ #include <config.h>
36
+ #else
37
+ #include "config.h"
38
+ #endif
39
+ #endif
40
+
41
+ #ifndef __STDC__
42
+ /* This is a separate conditional since some stdc systems
43
+ reject `defined (const)'. */
44
+ #ifndef const
45
+ #define const
46
+ #endif
47
+ #endif
48
+
49
+ #include <stdio.h>
50
+
51
+ #include <string.h>
52
+
53
+ /* Comment out all this code if we are using the GNU C Library, and are not
54
+ actually compiling the library itself. This code is part of the GNU C
55
+ Library, but also included in many other GNU distributions. Compiling
56
+ and linking in this code is a waste when using the GNU C library
57
+ (especially if it is a shared library). Rather than having every GNU
58
+ program understand `configure --with-gnu-libc' and omit the object files,
59
+ it is simpler to just do this in the source for each such file. */
60
+
61
+ #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
62
+
63
+
64
+ /* This needs to come after some library #include
65
+ to get __GNU_LIBRARY__ defined. */
66
+ #ifdef __GNU_LIBRARY__
67
+ /* Don't include stdlib.h for non-GNU C libraries because some of them
68
+ contain conflicting prototypes for getopt. */
69
+ #include <stdlib.h>
70
+ #endif /* GNU C library. */
71
+
72
+ /* This version of `getopt' appears to the caller like standard Unix `getopt'
73
+ but it behaves differently for the user, since it allows the user
74
+ to intersperse the options with the other arguments.
75
+
76
+ As `getopt' works, it permutes the elements of ARGV so that,
77
+ when it is done, all the options precede everything else. Thus
78
+ all application programs are extended to handle flexible argument order.
79
+
80
+ Setting the environment variable POSIXLY_CORRECT disables permutation.
81
+ Then the behavior is completely standard.
82
+
83
+ GNU application programs can use a third alternative mode in which
84
+ they can distinguish the relative order of options and other arguments. */
85
+
86
+ #include "getopt.h"
87
+
88
+ /* For communication from `getopt' to the caller.
89
+ When `getopt' finds an option that takes an argument,
90
+ the argument value is returned here.
91
+ Also, when `ordering' is RETURN_IN_ORDER,
92
+ each non-option ARGV-element is returned here. */
93
+
94
+ char *optarg = NULL;
95
+
96
+ /* Index in ARGV of the next element to be scanned.
97
+ This is used for communication to and from the caller
98
+ and for communication between successive calls to `getopt'.
99
+
100
+ On entry to `getopt', zero means this is the first call; initialize.
101
+
102
+ When `getopt' returns EOF, this is the index of the first of the
103
+ non-option elements that the caller should itself scan.
104
+
105
+ Otherwise, `optind' communicates from one call to the next
106
+ how much of ARGV has been scanned so far. */
107
+
108
+ /* XXX 1003.2 says this must be 1 before any call. */
109
+ int optind = 0;
110
+
111
+ /* The next char to be scanned in the option-element
112
+ in which the last option character we returned was found.
113
+ This allows us to pick up the scan where we left off.
114
+
115
+ If this is zero, or a null string, it means resume the scan
116
+ by advancing to the next ARGV-element. */
117
+
118
+ static char *nextchar;
119
+
120
+ /* Callers store zero here to inhibit the error message
121
+ for unrecognized options. */
122
+
123
+ int opterr = 1;
124
+
125
+ /* Set to an option character which was unrecognized.
126
+ This must be initialized on some systems to avoid linking in the
127
+ system's own getopt implementation. */
128
+
129
+ int optopt = '?';
130
+
131
+ /* Describe how to deal with options that follow non-option ARGV-elements.
132
+
133
+ If the caller did not specify anything,
134
+ the default is REQUIRE_ORDER if the environment variable
135
+ POSIXLY_CORRECT is defined, PERMUTE otherwise.
136
+
137
+ REQUIRE_ORDER means don't recognize them as options;
138
+ stop option processing when the first non-option is seen.
139
+ This is what Unix does.
140
+ This mode of operation is selected by either setting the environment
141
+ variable POSIXLY_CORRECT, or using `+' as the first character
142
+ of the list of option characters.
143
+
144
+ PERMUTE is the default. We permute the contents of ARGV as we scan,
145
+ so that eventually all the non-options are at the end. This allows options
146
+ to be given in any order, even with programs that were not written to
147
+ expect this.
148
+
149
+ RETURN_IN_ORDER is an option available to programs that were written
150
+ to expect options and other ARGV-elements in any order and that care about
151
+ the ordering of the two. We describe each non-option ARGV-element
152
+ as if it were the argument of an option with character code 1.
153
+ Using `-' as the first character of the list of option characters
154
+ selects this mode of operation.
155
+
156
+ The special argument `--' forces an end of option-scanning regardless
157
+ of the value of `ordering'. In the case of RETURN_IN_ORDER, only
158
+ `--' can cause `getopt' to return EOF with `optind' != ARGC. */
159
+
160
+ static enum
161
+ {
162
+ REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
163
+ } ordering;
164
+
165
+ /* Value of POSIXLY_CORRECT environment variable. */
166
+ static char *posixly_correct;
167
+
168
+ #ifdef __GNU_LIBRARY__
169
+ /* We want to avoid inclusion of string.h with non-GNU libraries
170
+ because there are many ways it can cause trouble.
171
+ On some systems, it contains special magic macros that don't work
172
+ in GCC. */
173
+ #include <string.h>
174
+ #define my_index strchr
175
+ #else
176
+
177
+ /* Avoid depending on library functions or files
178
+ whose names are inconsistent. */
179
+
180
+ char *getenv ();
181
+
182
+ static char *
183
+ my_index (str, chr)
184
+ const char *str;
185
+ int chr;
186
+ {
187
+ while (*str)
188
+ {
189
+ if (*str == chr)
190
+ return (char *) str;
191
+ str++;
192
+ }
193
+ return 0;
194
+ }
195
+
196
+ /* If using GCC, we can safely declare strlen this way.
197
+ If not using GCC, it is ok not to declare it. */
198
+ #ifdef __GNUC__
199
+ /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
200
+ That was relevant to code that was here before. */
201
+ #ifndef __STDC__
202
+ /* gcc with -traditional declares the built-in strlen to return int,
203
+ and has done so at least since version 2.4.5. -- rms. */
204
+ extern int strlen (const char *);
205
+ #endif /* not __STDC__ */
206
+ #endif /* __GNUC__ */
207
+
208
+ #endif /* not __GNU_LIBRARY__ */
209
+
210
+ /* Handle permutation of arguments. */
211
+
212
+ /* Describe the part of ARGV that contains non-options that have
213
+ been skipped. `first_nonopt' is the index in ARGV of the first of them;
214
+ `last_nonopt' is the index after the last of them. */
215
+
216
+ static int first_nonopt;
217
+ static int last_nonopt;
218
+
219
+ /* Exchange two adjacent subsequences of ARGV.
220
+ One subsequence is elements [first_nonopt,last_nonopt)
221
+ which contains all the non-options that have been skipped so far.
222
+ The other is elements [last_nonopt,optind), which contains all
223
+ the options processed since those non-options were skipped.
224
+
225
+ `first_nonopt' and `last_nonopt' are relocated so that they describe
226
+ the new indices of the non-options in ARGV after they are moved. */
227
+
228
+ static void
229
+ exchange (argv)
230
+ char **argv;
231
+ {
232
+ int bottom = first_nonopt;
233
+ int middle = last_nonopt;
234
+ int top = optind;
235
+ char *tem;
236
+
237
+ /* Exchange the shorter segment with the far end of the longer segment.
238
+ That puts the shorter segment into the right place.
239
+ It leaves the longer segment in the right place overall,
240
+ but it consists of two parts that need to be swapped next. */
241
+
242
+ while (top > middle && middle > bottom)
243
+ {
244
+ if (top - middle > middle - bottom)
245
+ {
246
+ /* Bottom segment is the short one. */
247
+ int len = middle - bottom;
248
+ register int i;
249
+
250
+ /* Swap it with the top part of the top segment. */
251
+ for (i = 0; i < len; i++)
252
+ {
253
+ tem = argv[bottom + i];
254
+ argv[bottom + i] = argv[top - (middle - bottom) + i];
255
+ argv[top - (middle - bottom) + i] = tem;
256
+ }
257
+ /* Exclude the moved bottom segment from further swapping. */
258
+ top -= len;
259
+ }
260
+ else
261
+ {
262
+ /* Top segment is the short one. */
263
+ int len = top - middle;
264
+ register int i;
265
+
266
+ /* Swap it with the bottom part of the bottom segment. */
267
+ for (i = 0; i < len; i++)
268
+ {
269
+ tem = argv[bottom + i];
270
+ argv[bottom + i] = argv[middle + i];
271
+ argv[middle + i] = tem;
272
+ }
273
+ /* Exclude the moved top segment from further swapping. */
274
+ bottom += len;
275
+ }
276
+ }
277
+
278
+ /* Update records for the slots the non-options now occupy. */
279
+
280
+ first_nonopt += (optind - last_nonopt);
281
+ last_nonopt = optind;
282
+ }
283
+
284
+ /* Initialize the internal data when the first call is made. */
285
+
286
+ static const char *
287
+ _getopt_initialize (optstring)
288
+ const char *optstring;
289
+ {
290
+ /* Start processing options with ARGV-element 1 (since ARGV-element 0
291
+ is the program name); the sequence of previously skipped
292
+ non-option ARGV-elements is empty. */
293
+
294
+ first_nonopt = last_nonopt = optind = 1;
295
+
296
+ nextchar = NULL;
297
+
298
+ posixly_correct = getenv ("POSIXLY_CORRECT");
299
+
300
+ /* Determine how to handle the ordering of options and nonoptions. */
301
+
302
+ if (optstring[0] == '-')
303
+ {
304
+ ordering = RETURN_IN_ORDER;
305
+ ++optstring;
306
+ }
307
+ else if (optstring[0] == '+')
308
+ {
309
+ ordering = REQUIRE_ORDER;
310
+ ++optstring;
311
+ }
312
+ else if (posixly_correct != NULL)
313
+ ordering = REQUIRE_ORDER;
314
+ else
315
+ ordering = PERMUTE;
316
+
317
+ return optstring;
318
+ }
319
+
320
+ /* Scan elements of ARGV (whose length is ARGC) for option characters
321
+ given in OPTSTRING.
322
+
323
+ If an element of ARGV starts with '-', and is not exactly "-" or "--",
324
+ then it is an option element. The characters of this element
325
+ (aside from the initial '-') are option characters. If `getopt'
326
+ is called repeatedly, it returns successively each of the option characters
327
+ from each of the option elements.
328
+
329
+ If `getopt' finds another option character, it returns that character,
330
+ updating `optind' and `nextchar' so that the next call to `getopt' can
331
+ resume the scan with the following option character or ARGV-element.
332
+
333
+ If there are no more option characters, `getopt' returns `EOF'.
334
+ Then `optind' is the index in ARGV of the first ARGV-element
335
+ that is not an option. (The ARGV-elements have been permuted
336
+ so that those that are not options now come last.)
337
+
338
+ OPTSTRING is a string containing the legitimate option characters.
339
+ If an option character is seen that is not listed in OPTSTRING,
340
+ return '?' after printing an error message. If you set `opterr' to
341
+ zero, the error message is suppressed but we still return '?'.
342
+
343
+ If a char in OPTSTRING is followed by a colon, that means it wants an arg,
344
+ so the following text in the same ARGV-element, or the text of the following
345
+ ARGV-element, is returned in `optarg'. Two colons mean an option that
346
+ wants an optional arg; if there is text in the current ARGV-element,
347
+ it is returned in `optarg', otherwise `optarg' is set to zero.
348
+
349
+ If OPTSTRING starts with `-' or `+', it requests different methods of
350
+ handling the non-option ARGV-elements.
351
+ See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
352
+
353
+ Long-named options begin with `--' instead of `-'.
354
+ Their names may be abbreviated as long as the abbreviation is unique
355
+ or is an exact match for some defined option. If they have an
356
+ argument, it follows the option name in the same ARGV-element, separated
357
+ from the option name by a `=', or else the in next ARGV-element.
358
+ When `getopt' finds a long-named option, it returns 0 if that option's
359
+ `flag' field is nonzero, the value of the option's `val' field
360
+ if the `flag' field is zero.
361
+
362
+ The elements of ARGV aren't really const, because we permute them.
363
+ But we pretend they're const in the prototype to be compatible
364
+ with other systems.
365
+
366
+ LONGOPTS is a vector of `struct option' terminated by an
367
+ element containing a name which is zero.
368
+
369
+ LONGIND returns the index in LONGOPT of the long-named option found.
370
+ It is only valid when a long-named option has been found by the most
371
+ recent call.
372
+
373
+ If LONG_ONLY is nonzero, '-' as well as '--' can introduce
374
+ long-named options. */
375
+
376
+ int
377
+ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
378
+ int argc;
379
+ char *const *argv;
380
+ const char *optstring;
381
+ const struct option *longopts;
382
+ int *longind;
383
+ int long_only;
384
+ {
385
+ optarg = NULL;
386
+
387
+ if (optind == 0)
388
+ optstring = _getopt_initialize (optstring);
389
+
390
+ if (nextchar == NULL || *nextchar == '\0')
391
+ {
392
+ /* Advance to the next ARGV-element. */
393
+
394
+ if (ordering == PERMUTE)
395
+ {
396
+ /* If we have just processed some options following some non-options,
397
+ exchange them so that the options come first. */
398
+
399
+ if (first_nonopt != last_nonopt && last_nonopt != optind)
400
+ exchange ((char **) argv);
401
+ else if (last_nonopt != optind)
402
+ first_nonopt = optind;
403
+
404
+ /* Skip any additional non-options
405
+ and extend the range of non-options previously skipped. */
406
+
407
+ while (optind < argc
408
+ && (argv[optind][0] != '-' || argv[optind][1] == '\0'))
409
+ optind++;
410
+ last_nonopt = optind;
411
+ }
412
+
413
+ /* The special ARGV-element `--' means premature end of options.
414
+ Skip it like a null option,
415
+ then exchange with previous non-options as if it were an option,
416
+ then skip everything else like a non-option. */
417
+
418
+ if (optind != argc && !strcmp (argv[optind], "--"))
419
+ {
420
+ optind++;
421
+
422
+ if (first_nonopt != last_nonopt && last_nonopt != optind)
423
+ exchange ((char **) argv);
424
+ else if (first_nonopt == last_nonopt)
425
+ first_nonopt = optind;
426
+ last_nonopt = argc;
427
+
428
+ optind = argc;
429
+ }
430
+
431
+ /* If we have done all the ARGV-elements, stop the scan
432
+ and back over any non-options that we skipped and permuted. */
433
+
434
+ if (optind == argc)
435
+ {
436
+ /* Set the next-arg-index to point at the non-options
437
+ that we previously skipped, so the caller will digest them. */
438
+ if (first_nonopt != last_nonopt)
439
+ optind = first_nonopt;
440
+ return EOF;
441
+ }
442
+
443
+ /* If we have come to a non-option and did not permute it,
444
+ either stop the scan or describe it to the caller and pass it by. */
445
+
446
+ if ((argv[optind][0] != '-' || argv[optind][1] == '\0'))
447
+ {
448
+ if (ordering == REQUIRE_ORDER)
449
+ return EOF;
450
+ optarg = argv[optind++];
451
+ return 1;
452
+ }
453
+
454
+ /* We have found another option-ARGV-element.
455
+ Skip the initial punctuation. */
456
+
457
+ nextchar = (argv[optind] + 1
458
+ + (longopts != NULL && argv[optind][1] == '-'));
459
+ }
460
+
461
+ /* Decode the current option-ARGV-element. */
462
+
463
+ /* Check whether the ARGV-element is a long option.
464
+
465
+ If long_only and the ARGV-element has the form "-f", where f is
466
+ a valid short option, don't consider it an abbreviated form of
467
+ a long option that starts with f. Otherwise there would be no
468
+ way to give the -f short option.
469
+
470
+ On the other hand, if there's a long option "fubar" and
471
+ the ARGV-element is "-fu", do consider that an abbreviation of
472
+ the long option, just like "--fu", and not "-f" with arg "u".
473
+
474
+ This distinction seems to be the most useful approach. */
475
+
476
+ if (longopts != NULL
477
+ && (argv[optind][1] == '-'
478
+ || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
479
+ {
480
+ char *nameend;
481
+ const struct option *p;
482
+ const struct option *pfound = NULL;
483
+ int exact = 0;
484
+ int ambig = 0;
485
+ int indfound = 0;
486
+ int option_index;
487
+
488
+ for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
489
+ /* Do nothing. */ ;
490
+
491
+ /* Test all long options for either exact match
492
+ or abbreviated matches. */
493
+ for (p = longopts, option_index = 0; p->name; p++, option_index++)
494
+ if (!strncmp (p->name, nextchar, nameend - nextchar))
495
+ {
496
+ if (nameend - nextchar == (int) strlen (p->name))
497
+ {
498
+ /* Exact match found. */
499
+ pfound = p;
500
+ indfound = option_index;
501
+ exact = 1;
502
+ break;
503
+ }
504
+ else if (pfound == NULL)
505
+ {
506
+ /* First nonexact match found. */
507
+ pfound = p;
508
+ indfound = option_index;
509
+ }
510
+ else
511
+ /* Second or later nonexact match found. */
512
+ ambig = 1;
513
+ }
514
+
515
+ if (ambig && !exact)
516
+ {
517
+ if (opterr)
518
+ fprintf (stderr, "%s: option `%s' is ambiguous\n",
519
+ argv[0], argv[optind]);
520
+ nextchar += strlen (nextchar);
521
+ optind++;
522
+ return '?';
523
+ }
524
+
525
+ if (pfound != NULL)
526
+ {
527
+ option_index = indfound;
528
+ optind++;
529
+ if (*nameend)
530
+ {
531
+ /* Don't test has_arg with >, because some C compilers don't
532
+ allow it to be used on enums. */
533
+ if (pfound->has_arg)
534
+ optarg = nameend + 1;
535
+ else
536
+ {
537
+ if (opterr)
538
+ {
539
+ if (argv[optind - 1][1] == '-')
540
+ /* --option */
541
+ fprintf (stderr,
542
+ "%s: option `--%s' doesn't allow an argument\n",
543
+ argv[0], pfound->name);
544
+ else
545
+ /* +option or -option */
546
+ fprintf (stderr,
547
+ "%s: option `%c%s' doesn't allow an argument\n",
548
+ argv[0], argv[optind - 1][0], pfound->name);
549
+ }
550
+ nextchar += strlen (nextchar);
551
+ return '?';
552
+ }
553
+ }
554
+ else if (pfound->has_arg == 1)
555
+ {
556
+ if (optind < argc)
557
+ optarg = argv[optind++];
558
+ else
559
+ {
560
+ if (opterr)
561
+ fprintf (stderr, "%s: option `%s' requires an argument\n",
562
+ argv[0], argv[optind - 1]);
563
+ nextchar += strlen (nextchar);
564
+ return optstring[0] == ':' ? ':' : '?';
565
+ }
566
+ }
567
+ nextchar += strlen (nextchar);
568
+ if (longind != NULL)
569
+ *longind = option_index;
570
+ if (pfound->flag)
571
+ {
572
+ *(pfound->flag) = pfound->val;
573
+ return 0;
574
+ }
575
+ return pfound->val;
576
+ }
577
+
578
+ /* Can't find it as a long option. If this is not getopt_long_only,
579
+ or the option starts with '--' or is not a valid short
580
+ option, then it's an error.
581
+ Otherwise interpret it as a short option. */
582
+ if (!long_only || argv[optind][1] == '-'
583
+ || my_index (optstring, *nextchar) == NULL)
584
+ {
585
+ if (opterr)
586
+ {
587
+ if (argv[optind][1] == '-')
588
+ /* --option */
589
+ fprintf (stderr, "%s: unrecognized option `--%s'\n",
590
+ argv[0], nextchar);
591
+ else
592
+ /* +option or -option */
593
+ fprintf (stderr, "%s: unrecognized option `%c%s'\n",
594
+ argv[0], argv[optind][0], nextchar);
595
+ }
596
+ nextchar = (char *) "";
597
+ optind++;
598
+ return '?';
599
+ }
600
+ }
601
+
602
+ /* Look at and handle the next short option-character. */
603
+
604
+ {
605
+ char c = *nextchar++;
606
+ char *temp = my_index (optstring, c);
607
+
608
+ /* Increment `optind' when we start to process its last character. */
609
+ if (*nextchar == '\0')
610
+ ++optind;
611
+
612
+ if (temp == NULL || c == ':')
613
+ {
614
+ if (opterr)
615
+ {
616
+ if (posixly_correct)
617
+ /* 1003.2 specifies the format of this message. */
618
+ fprintf (stderr, "%s: illegal option -- %c\n", argv[0], c);
619
+ else
620
+ fprintf (stderr, "%s: invalid option -- %c\n", argv[0], c);
621
+ }
622
+ optopt = c;
623
+ return '?';
624
+ }
625
+ if (temp[1] == ':')
626
+ {
627
+ if (temp[2] == ':')
628
+ {
629
+ /* This is an option that accepts an argument optionally. */
630
+ if (*nextchar != '\0')
631
+ {
632
+ optarg = nextchar;
633
+ optind++;
634
+ }
635
+ else
636
+ optarg = NULL;
637
+ nextchar = NULL;
638
+ }
639
+ else
640
+ {
641
+ /* This is an option that requires an argument. */
642
+ if (*nextchar != '\0')
643
+ {
644
+ optarg = nextchar;
645
+ /* If we end this ARGV-element by taking the rest as an arg,
646
+ we must advance to the next element now. */
647
+ optind++;
648
+ }
649
+ else if (optind == argc)
650
+ {
651
+ if (opterr)
652
+ {
653
+ /* 1003.2 specifies the format of this message. */
654
+ fprintf (stderr, "%s: option requires an argument -- %c\n",
655
+ argv[0], c);
656
+ }
657
+ optopt = c;
658
+ if (optstring[0] == ':')
659
+ c = ':';
660
+ else
661
+ c = '?';
662
+ }
663
+ else
664
+ /* We already incremented `optind' once;
665
+ increment it again when taking next ARGV-elt as argument. */
666
+ optarg = argv[optind++];
667
+ nextchar = NULL;
668
+ }
669
+ }
670
+ return c;
671
+ }
672
+ }
673
+
674
+ int
675
+ getopt (argc, argv, optstring)
676
+ int argc;
677
+ char *const *argv;
678
+ const char *optstring;
679
+ {
680
+ return _getopt_internal (argc, argv, optstring,
681
+ (const struct option *) 0,
682
+ (int *) 0,
683
+ 0);
684
+ }
685
+
686
+ #endif /* _LIBC or not __GNU_LIBRARY__. */
687
+
688
+ #ifdef TEST
689
+
690
+ /* Compile with -DTEST to make an executable for use in testing
691
+ the above definition of `getopt'. */
692
+
693
+ int
694
+ main (argc, argv)
695
+ int argc;
696
+ char **argv;
697
+ {
698
+ int c;
699
+ int digit_optind = 0;
700
+
701
+ while (1)
702
+ {
703
+ int this_option_optind = optind ? optind : 1;
704
+
705
+ c = getopt (argc, argv, "abc:d:0123456789");
706
+ if (c == EOF)
707
+ break;
708
+
709
+ switch (c)
710
+ {
711
+ case '0':
712
+ case '1':
713
+ case '2':
714
+ case '3':
715
+ case '4':
716
+ case '5':
717
+ case '6':
718
+ case '7':
719
+ case '8':
720
+ case '9':
721
+ if (digit_optind != 0 && digit_optind != this_option_optind)
722
+ printf ("digits occur in two different argv-elements.\n");
723
+ digit_optind = this_option_optind;
724
+ printf ("option %c\n", c);
725
+ break;
726
+
727
+ case 'a':
728
+ printf ("option a\n");
729
+ break;
730
+
731
+ case 'b':
732
+ printf ("option b\n");
733
+ break;
734
+
735
+ case 'c':
736
+ printf ("option c with value `%s'\n", optarg);
737
+ break;
738
+
739
+ case '?':
740
+ break;
741
+
742
+ default:
743
+ printf ("?? getopt returned character code 0%o ??\n", c);
744
+ }
745
+ }
746
+
747
+ if (optind < argc)
748
+ {
749
+ printf ("non-option ARGV-elements: ");
750
+ while (optind < argc)
751
+ printf ("%s ", argv[optind++]);
752
+ printf ("\n");
753
+ }
754
+
755
+ exit (0);
756
+ }
757
+
758
+ #endif /* TEST */