geo_coder 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (119) hide show
  1. data/Gemfile +12 -0
  2. data/Gemfile.lock +32 -0
  3. data/History.txt +6 -0
  4. data/Makefile +13 -0
  5. data/Manifest.txt +18 -0
  6. data/README.rdoc +197 -0
  7. data/Rakefile +53 -0
  8. data/TODO.txt +8 -0
  9. data/VERSION +1 -0
  10. data/bin/build_indexes +8 -0
  11. data/bin/rebuild_cluster +22 -0
  12. data/bin/rebuild_metaphones +23 -0
  13. data/bin/tiger_import +59 -0
  14. data/demos/demo/app/ext/geocodewrap.rb +84 -0
  15. data/demos/demo/app/views/index.builder +13 -0
  16. data/demos/demo/app/views/index.erb +71 -0
  17. data/demos/demo/config.ru +12 -0
  18. data/demos/demo/config/bootstraps.rb +130 -0
  19. data/demos/demo/config/geoenvironment.rb +25 -0
  20. data/demos/demo/geocoder_helper.rb +12 -0
  21. data/demos/demo/geocom_geocode.rb +10 -0
  22. data/demos/demo/main.rb +3 -0
  23. data/demos/demo/rakefile.rb +17 -0
  24. data/demos/demo/tmp/restart.txt +0 -0
  25. data/demos/simpledemo/views/index.builder +13 -0
  26. data/demos/simpledemo/views/index.erb +69 -0
  27. data/demos/simpledemo/ws.rb +83 -0
  28. data/doc/Makefile +7 -0
  29. data/doc/html4css1.css +279 -0
  30. data/doc/lookup.rst +193 -0
  31. data/doc/parsing.rst +125 -0
  32. data/doc/voidspace.css +147 -0
  33. data/geo_coder.gemspec +172 -0
  34. data/lib/geocoder/us.rb +21 -0
  35. data/lib/geocoder/us/address.rb +290 -0
  36. data/lib/geocoder/us/constants.rb +670 -0
  37. data/lib/geocoder/us/database.rb +745 -0
  38. data/lib/geocoder/us/import.rb +181 -0
  39. data/lib/geocoder/us/import/tiger.rb +13 -0
  40. data/lib/geocoder/us/numbers.rb +58 -0
  41. data/navteq/README +4 -0
  42. data/navteq/convert.sql +37 -0
  43. data/navteq/navteq_import +39 -0
  44. data/navteq/prepare.sql +92 -0
  45. data/sql/cluster.sql +16 -0
  46. data/sql/convert.sql +80 -0
  47. data/sql/create.sql +37 -0
  48. data/sql/index.sql +12 -0
  49. data/sql/place.csv +104944 -0
  50. data/sql/place.sql +104948 -0
  51. data/sql/setup.sql +78 -0
  52. data/src/Makefile +13 -0
  53. data/src/README +14 -0
  54. data/src/liblwgeom/Makefile +75 -0
  55. data/src/liblwgeom/box2d.c +54 -0
  56. data/src/liblwgeom/lex.yy.c +4799 -0
  57. data/src/liblwgeom/liblwgeom.h +1405 -0
  58. data/src/liblwgeom/lwalgorithm.c +946 -0
  59. data/src/liblwgeom/lwalgorithm.h +52 -0
  60. data/src/liblwgeom/lwcircstring.c +759 -0
  61. data/src/liblwgeom/lwcollection.c +541 -0
  62. data/src/liblwgeom/lwcompound.c +118 -0
  63. data/src/liblwgeom/lwcurvepoly.c +86 -0
  64. data/src/liblwgeom/lwgeom.c +886 -0
  65. data/src/liblwgeom/lwgeom_api.c +2201 -0
  66. data/src/liblwgeom/lwgparse.c +1219 -0
  67. data/src/liblwgeom/lwgunparse.c +1054 -0
  68. data/src/liblwgeom/lwline.c +525 -0
  69. data/src/liblwgeom/lwmcurve.c +125 -0
  70. data/src/liblwgeom/lwmline.c +137 -0
  71. data/src/liblwgeom/lwmpoint.c +138 -0
  72. data/src/liblwgeom/lwmpoly.c +141 -0
  73. data/src/liblwgeom/lwmsurface.c +129 -0
  74. data/src/liblwgeom/lwpoint.c +439 -0
  75. data/src/liblwgeom/lwpoly.c +579 -0
  76. data/src/liblwgeom/lwsegmentize.c +1047 -0
  77. data/src/liblwgeom/lwutil.c +369 -0
  78. data/src/liblwgeom/measures.c +861 -0
  79. data/src/liblwgeom/postgis_config.h +93 -0
  80. data/src/liblwgeom/ptarray.c +847 -0
  81. data/src/liblwgeom/vsprintf.c +179 -0
  82. data/src/liblwgeom/wktparse.h +126 -0
  83. data/src/liblwgeom/wktparse.lex +74 -0
  84. data/src/liblwgeom/wktparse.tab.c +2353 -0
  85. data/src/liblwgeom/wktparse.tab.h +145 -0
  86. data/src/liblwgeom/wktparse.y +385 -0
  87. data/src/libsqlite3_geocoder/Makefile +22 -0
  88. data/src/libsqlite3_geocoder/Makefile.nix +15 -0
  89. data/src/libsqlite3_geocoder/Makefile.redhat +15 -0
  90. data/src/libsqlite3_geocoder/extension.c +121 -0
  91. data/src/libsqlite3_geocoder/extension.h +13 -0
  92. data/src/libsqlite3_geocoder/levenshtein.c +42 -0
  93. data/src/libsqlite3_geocoder/metaphon.c +278 -0
  94. data/src/libsqlite3_geocoder/util.c +37 -0
  95. data/src/libsqlite3_geocoder/wkb_compress.c +54 -0
  96. data/src/metaphone/Makefile +7 -0
  97. data/src/metaphone/README +49 -0
  98. data/src/metaphone/extension.c +37 -0
  99. data/src/metaphone/metaphon.c +251 -0
  100. data/src/shp2sqlite/Makefile +37 -0
  101. data/src/shp2sqlite/Makefile.nix +36 -0
  102. data/src/shp2sqlite/Makefile.redhat +35 -0
  103. data/src/shp2sqlite/dbfopen.c +1595 -0
  104. data/src/shp2sqlite/getopt.c +695 -0
  105. data/src/shp2sqlite/getopt.h +127 -0
  106. data/src/shp2sqlite/shapefil.h +500 -0
  107. data/src/shp2sqlite/shp2sqlite.c +1974 -0
  108. data/src/shp2sqlite/shpopen.c +1894 -0
  109. data/tests/address.rb +236 -0
  110. data/tests/benchmark.rb +20 -0
  111. data/tests/constants.rb +57 -0
  112. data/tests/data/address-sample.csv +52 -0
  113. data/tests/data/db-test.csv +57 -0
  114. data/tests/data/locations.csv +4 -0
  115. data/tests/database.rb +137 -0
  116. data/tests/generate.rb +34 -0
  117. data/tests/numbers.rb +46 -0
  118. data/tests/run.rb +11 -0
  119. metadata +237 -0
@@ -0,0 +1,695 @@
1
+
2
+ /* This version of `getopt' appears to the caller like standard Unix `getopt'
3
+ but it behaves differently for the user, since it allows the user
4
+ to intersperse the options with the other arguments.
5
+
6
+ As `getopt' works, it permutes the elements of ARGV so that,
7
+ when it is done, all the options precede everything else. Thus
8
+ all application programs are extended to handle flexible argument order.
9
+
10
+ Setting the environment variable POSIXLY_CORRECT disables permutation.
11
+ Then the behavior is completely standard.
12
+
13
+ GNU application programs can use a third alternative mode in which
14
+ they can distinguish the relative order of options and other arguments. */
15
+
16
+ #include <stdio.h>
17
+
18
+ #include "getopt.h"
19
+
20
+
21
+ /* For communication from `getopt' to the caller.
22
+ When `getopt' finds an option that takes an argument,
23
+ the argument value is returned here.
24
+ Also, when `ordering' is RETURN_IN_ORDER,
25
+ each non-option ARGV-element is returned here. */
26
+
27
+ char *optarg = 0;
28
+
29
+ /* Index in ARGV of the next element to be scanned.
30
+ This is used for communication to and from the caller
31
+ and for communication between successive calls to `getopt'.
32
+
33
+ On entry to `getopt', zero means this is the first call; initialize.
34
+
35
+ When `getopt' returns EOF, this is the index of the first of the
36
+ non-option elements that the caller should itself scan.
37
+
38
+ Otherwise, `optind' communicates from one call to the next
39
+ how much of ARGV has been scanned so far. */
40
+
41
+ /* XXX 1003.2 says this must be 1 before any call. */
42
+ int optind = 0;
43
+
44
+ /* The next char to be scanned in the option-element
45
+ in which the last option character we returned was found.
46
+ This allows us to pick up the scan where we left off.
47
+
48
+ If this is zero, or a null string, it means resume the scan
49
+ by advancing to the next ARGV-element. */
50
+
51
+ static char *nextchar;
52
+
53
+ /* Callers store zero here to inhibit the error message
54
+ for unrecognized options. */
55
+
56
+ int opterr = 1;
57
+
58
+ /* Set to an option character which was unrecognized.
59
+ This must be initialized on some systems to avoid linking in the
60
+ system's own getopt implementation. */
61
+
62
+ #define BAD_OPTION '\0'
63
+ int optopt = BAD_OPTION;
64
+
65
+ /* Describe how to deal with options that follow non-option ARGV-elements.
66
+
67
+ If the caller did not specify anything,
68
+ the default is REQUIRE_ORDER if the environment variable
69
+ POSIXLY_CORRECT is defined, PERMUTE otherwise.
70
+
71
+ REQUIRE_ORDER means don't recognize them as options;
72
+ stop option processing when the first non-option is seen.
73
+ This is what Unix does.
74
+ This mode of operation is selected by either setting the environment
75
+ variable POSIXLY_CORRECT, or using `+' as the first character
76
+ of the list of option characters.
77
+
78
+ PERMUTE is the default. We permute the contents of ARGV as we scan,
79
+ so that eventually all the non-options are at the end. This allows options
80
+ to be given in any order, even with programs that were not written to
81
+ expect this.
82
+
83
+ RETURN_IN_ORDER is an option available to programs that were written
84
+ to expect options and other ARGV-elements in any order and that care about
85
+ the ordering of the two. We describe each non-option ARGV-element
86
+ as if it were the argument of an option with character code 1.
87
+ Using `-' as the first character of the list of option characters
88
+ selects this mode of operation.
89
+
90
+ The special argument `--' forces an end of option-scanning regardless
91
+ of the value of `ordering'. In the case of RETURN_IN_ORDER, only
92
+ `--' can cause `getopt' to return EOF with `optind' != ARGC. */
93
+
94
+ static enum
95
+ {
96
+ REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
97
+ } ordering;
98
+
99
+ #ifdef __GNU_LIBRARY__
100
+ /* We want to avoid inclusion of string.h with non-GNU libraries
101
+ because there are many ways it can cause trouble.
102
+ On some systems, it contains special magic macros that don't work
103
+ in GCC. */
104
+
105
+ #include <string.h>
106
+ #define my_index strchr
107
+ #define my_strlen strlen
108
+ #else
109
+
110
+ /* Avoid depending on library functions or files
111
+ whose names are inconsistent. */
112
+
113
+ #if __STDC__ || defined(PROTO)
114
+ extern int strcmp (const char *s1, const char *s2);
115
+ extern int strncmp(const char *s1, const char *s2, int n);
116
+
117
+ static int my_strlen(const char *s);
118
+ static char *my_index (const char *str, int chr);
119
+ #endif
120
+
121
+ static int
122
+ my_strlen (str)
123
+ const char *str;
124
+ {
125
+ int n = 0;
126
+ while (*str++)
127
+ n++;
128
+ return n;
129
+ }
130
+
131
+ static char *
132
+ my_index (str, chr)
133
+ const char *str;
134
+ int chr;
135
+ {
136
+ while (*str)
137
+ {
138
+ if (*str == chr)
139
+ return (char *) str;
140
+ str++;
141
+ }
142
+ return 0;
143
+ }
144
+
145
+ #endif /* GNU C library. */
146
+
147
+ extern char *getenv(const char *name);
148
+
149
+ /* Handle permutation of arguments. */
150
+
151
+ /* Describe the part of ARGV that contains non-options that have
152
+ been skipped. `first_nonopt' is the index in ARGV of the first of them;
153
+ `last_nonopt' is the index after the last of them. */
154
+
155
+ static int first_nonopt;
156
+ static int last_nonopt;
157
+
158
+ /* Exchange two adjacent subsequences of ARGV.
159
+ One subsequence is elements [first_nonopt,last_nonopt)
160
+ which contains all the non-options that have been skipped so far.
161
+ The other is elements [last_nonopt,optind), which contains all
162
+ the options processed since those non-options were skipped.
163
+
164
+ `first_nonopt' and `last_nonopt' are relocated so that they describe
165
+ the new indices of the non-options in ARGV after they are moved.
166
+
167
+ To perform the swap, we first reverse the order of all elements. So
168
+ all options now come before all non options, but they are in the
169
+ wrong order. So we put back the options and non options in original
170
+ order by reversing them again. For example:
171
+ original input: a b c -x -y
172
+ reverse all: -y -x c b a
173
+ reverse options: -x -y c b a
174
+ reverse non options: -x -y a b c
175
+ */
176
+
177
+ #if __STDC__ || defined(PROTO)
178
+ static void exchange (char **argv);
179
+ #endif
180
+
181
+ static void
182
+ exchange (argv)
183
+ char **argv;
184
+ {
185
+ char *temp, **first, **last;
186
+
187
+ /* Reverse all the elements [first_nonopt, optind) */
188
+ first = &argv[first_nonopt];
189
+ last = &argv[optind-1];
190
+ while (first < last) {
191
+ temp = *first; *first = *last; *last = temp; first++; last--;
192
+ }
193
+ /* Put back the options in order */
194
+ first = &argv[first_nonopt];
195
+ first_nonopt += (optind - last_nonopt);
196
+ last = &argv[first_nonopt - 1];
197
+ while (first < last) {
198
+ temp = *first; *first = *last; *last = temp; first++; last--;
199
+ }
200
+
201
+ /* Put back the non options in order */
202
+ first = &argv[first_nonopt];
203
+ last_nonopt = optind;
204
+ last = &argv[last_nonopt-1];
205
+ while (first < last) {
206
+ temp = *first; *first = *last; *last = temp; first++; last--;
207
+ }
208
+ }
209
+
210
+ /* Scan elements of ARGV (whose length is ARGC) for option characters
211
+ given in OPTSTRING.
212
+
213
+ If an element of ARGV starts with '-', and is not exactly "-" or "--",
214
+ then it is an option element. The characters of this element
215
+ (aside from the initial '-') are option characters. If `getopt'
216
+ is called repeatedly, it returns successively each of the option characters
217
+ from each of the option elements.
218
+
219
+ If `getopt' finds another option character, it returns that character,
220
+ updating `optind' and `nextchar' so that the next call to `getopt' can
221
+ resume the scan with the following option character or ARGV-element.
222
+
223
+ If there are no more option characters, `getopt' returns `EOF'.
224
+ Then `optind' is the index in ARGV of the first ARGV-element
225
+ that is not an option. (The ARGV-elements have been permuted
226
+ so that those that are not options now come last.)
227
+
228
+ OPTSTRING is a string containing the legitimate option characters.
229
+ If an option character is seen that is not listed in OPTSTRING,
230
+ return BAD_OPTION after printing an error message. If you set `opterr' to
231
+ zero, the error message is suppressed but we still return BAD_OPTION.
232
+
233
+ If a char in OPTSTRING is followed by a colon, that means it wants an arg,
234
+ so the following text in the same ARGV-element, or the text of the following
235
+ ARGV-element, is returned in `optarg'. Two colons mean an option that
236
+ wants an optional arg; if there is text in the current ARGV-element,
237
+ it is returned in `optarg', otherwise `optarg' is set to zero.
238
+
239
+ If OPTSTRING starts with `-' or `+', it requests different methods of
240
+ handling the non-option ARGV-elements.
241
+ See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
242
+
243
+ Long-named options begin with `--' instead of `-'.
244
+ Their names may be abbreviated as long as the abbreviation is unique
245
+ or is an exact match for some defined option. If they have an
246
+ argument, it follows the option name in the same ARGV-element, separated
247
+ from the option name by a `=', or else the in next ARGV-element.
248
+ When `getopt' finds a long-named option, it returns 0 if that option's
249
+ `flag' field is nonzero, the value of the option's `val' field
250
+ if the `flag' field is zero.
251
+
252
+ The elements of ARGV aren't really const, because we permute them.
253
+ But we pretend they're const in the prototype to be compatible
254
+ with other systems.
255
+
256
+ LONGOPTS is a vector of `struct option' terminated by an
257
+ element containing a name which is zero.
258
+
259
+ LONGIND returns the index in LONGOPT of the long-named option found.
260
+ It is only valid when a long-named option has been found by the most
261
+ recent call.
262
+
263
+ If LONG_ONLY is nonzero, '-' as well as '--' can introduce
264
+ long-named options. */
265
+
266
+ int
267
+ _pgis_getopt_internal (argc, argv, optstring, longopts, longind, long_only)
268
+ int argc;
269
+ char *const *argv;
270
+ const char *optstring;
271
+ const struct option *longopts;
272
+ int *longind;
273
+ int long_only;
274
+ {
275
+ int option_index;
276
+
277
+ optarg = 0;
278
+
279
+ /* Initialize the internal data when the first call is made.
280
+ Start processing options with ARGV-element 1 (since ARGV-element 0
281
+ is the program name); the sequence of previously skipped
282
+ non-option ARGV-elements is empty. */
283
+
284
+ if (optind == 0)
285
+ {
286
+ first_nonopt = last_nonopt = optind = 1;
287
+
288
+ nextchar = NULL;
289
+
290
+ /* Determine how to handle the ordering of options and nonoptions. */
291
+
292
+ if (optstring[0] == '-')
293
+ {
294
+ ordering = RETURN_IN_ORDER;
295
+ ++optstring;
296
+ }
297
+ else if (optstring[0] == '+')
298
+ {
299
+ ordering = REQUIRE_ORDER;
300
+ ++optstring;
301
+ }
302
+ else if (getenv ("POSIXLY_CORRECT") != NULL)
303
+ ordering = REQUIRE_ORDER;
304
+ else
305
+ ordering = PERMUTE;
306
+ }
307
+
308
+ if (nextchar == NULL || *nextchar == '\0')
309
+ {
310
+ if (ordering == PERMUTE)
311
+ {
312
+ /* If we have just processed some options following some non-options,
313
+ exchange them so that the options come first. */
314
+
315
+ if (first_nonopt != last_nonopt && last_nonopt != optind)
316
+ exchange ((char **) argv);
317
+ else if (last_nonopt != optind)
318
+ first_nonopt = optind;
319
+
320
+ /* Now skip any additional non-options
321
+ and extend the range of non-options previously skipped. */
322
+
323
+ while (optind < argc
324
+ && (argv[optind][0] != '-' || argv[optind][1] == '\0')
325
+ #ifdef GETOPT_COMPAT
326
+ && (longopts == NULL
327
+ || argv[optind][0] != '+' || argv[optind][1] == '\0')
328
+ #endif /* GETOPT_COMPAT */
329
+ )
330
+ optind++;
331
+ last_nonopt = optind;
332
+ }
333
+
334
+ /* Special ARGV-element `--' means premature end of options.
335
+ Skip it like a null option,
336
+ then exchange with previous non-options as if it were an option,
337
+ then skip everything else like a non-option. */
338
+
339
+ if (optind != argc && !strcmp (argv[optind], "--"))
340
+ {
341
+ optind++;
342
+
343
+ if (first_nonopt != last_nonopt && last_nonopt != optind)
344
+ exchange ((char **) argv);
345
+ else if (first_nonopt == last_nonopt)
346
+ first_nonopt = optind;
347
+ last_nonopt = argc;
348
+
349
+ optind = argc;
350
+ }
351
+
352
+ /* If we have done all the ARGV-elements, stop the scan
353
+ and back over any non-options that we skipped and permuted. */
354
+
355
+ if (optind == argc)
356
+ {
357
+ /* Set the next-arg-index to point at the non-options
358
+ that we previously skipped, so the caller will digest them. */
359
+ if (first_nonopt != last_nonopt)
360
+ optind = first_nonopt;
361
+ return EOF;
362
+ }
363
+
364
+ /* If we have come to a non-option and did not permute it,
365
+ either stop the scan or describe it to the caller and pass it by. */
366
+
367
+ if ((argv[optind][0] != '-' || argv[optind][1] == '\0')
368
+ #ifdef GETOPT_COMPAT
369
+ && (longopts == NULL
370
+ || argv[optind][0] != '+' || argv[optind][1] == '\0')
371
+ #endif /* GETOPT_COMPAT */
372
+ )
373
+ {
374
+ if (ordering == REQUIRE_ORDER)
375
+ return EOF;
376
+ optarg = argv[optind++];
377
+ return 1;
378
+ }
379
+
380
+ /* We have found another option-ARGV-element.
381
+ Start decoding its characters. */
382
+
383
+ nextchar = (argv[optind] + 1
384
+ + (longopts != NULL && argv[optind][1] == '-'));
385
+ }
386
+
387
+ if (longopts != NULL
388
+ && ((argv[optind][0] == '-'
389
+ && (argv[optind][1] == '-' || long_only))
390
+ #ifdef GETOPT_COMPAT
391
+ || argv[optind][0] == '+'
392
+ #endif /* GETOPT_COMPAT */
393
+ ))
394
+ {
395
+ const struct option *p;
396
+ char *s = nextchar;
397
+ int exact = 0;
398
+ int ambig = 0;
399
+ const struct option *pfound = NULL;
400
+ int indfound = 0;
401
+
402
+ while (*s && *s != '=')
403
+ s++;
404
+
405
+ /* Test all options for either exact match or abbreviated matches. */
406
+ for (p = longopts, option_index = 0; p->name;
407
+ p++, option_index++)
408
+ if (!strncmp (p->name, nextchar, s - nextchar))
409
+ {
410
+ if (s - nextchar == my_strlen (p->name))
411
+ {
412
+ /* Exact match found. */
413
+ pfound = p;
414
+ indfound = option_index;
415
+ exact = 1;
416
+ break;
417
+ }
418
+ else if (pfound == NULL)
419
+ {
420
+ /* First nonexact match found. */
421
+ pfound = p;
422
+ indfound = option_index;
423
+ }
424
+ else
425
+ /* Second nonexact match found. */
426
+ ambig = 1;
427
+ }
428
+
429
+ if (ambig && !exact)
430
+ {
431
+ if (opterr)
432
+ fprintf (stderr, "%s: option `%s' is ambiguous\n",
433
+ argv[0], argv[optind]);
434
+ nextchar += my_strlen (nextchar);
435
+ optind++;
436
+ return BAD_OPTION;
437
+ }
438
+
439
+ if (pfound != NULL)
440
+ {
441
+ option_index = indfound;
442
+ optind++;
443
+ if (*s)
444
+ {
445
+ /* Don't test has_arg with >, because some C compilers don't
446
+ allow it to be used on enums. */
447
+ if (pfound->has_arg)
448
+ optarg = s + 1;
449
+ else
450
+ {
451
+ if (opterr)
452
+ {
453
+ if (argv[optind - 1][1] == '-')
454
+ /* --option */
455
+ fprintf (stderr,
456
+ "%s: option `--%s' doesn't allow an argument\n",
457
+ argv[0], pfound->name);
458
+ else
459
+ /* +option or -option */
460
+ fprintf (stderr,
461
+ "%s: option `%c%s' doesn't allow an argument\n",
462
+ argv[0], argv[optind - 1][0], pfound->name);
463
+ }
464
+ nextchar += my_strlen (nextchar);
465
+ return BAD_OPTION;
466
+ }
467
+ }
468
+ else if (pfound->has_arg == 1)
469
+ {
470
+ if (optind < argc)
471
+ optarg = argv[optind++];
472
+ else
473
+ {
474
+ if (opterr)
475
+ fprintf (stderr, "%s: option `%s' requires an argument\n",
476
+ argv[0], argv[optind - 1]);
477
+ nextchar += my_strlen (nextchar);
478
+ return optstring[0] == ':' ? ':' : BAD_OPTION;
479
+ }
480
+ }
481
+ nextchar += my_strlen (nextchar);
482
+ if (longind != NULL)
483
+ *longind = option_index;
484
+ if (pfound->flag)
485
+ {
486
+ *(pfound->flag) = pfound->val;
487
+ return 0;
488
+ }
489
+ return pfound->val;
490
+ }
491
+ /* Can't find it as a long option. If this is not getopt_long_only,
492
+ or the option starts with '--' or is not a valid short
493
+ option, then it's an error.
494
+ Otherwise interpret it as a short option. */
495
+ if (!long_only || argv[optind][1] == '-'
496
+ #ifdef GETOPT_COMPAT
497
+ || argv[optind][0] == '+'
498
+ #endif /* GETOPT_COMPAT */
499
+ || my_index (optstring, *nextchar) == NULL)
500
+ {
501
+ if (opterr)
502
+ {
503
+ if (argv[optind][1] == '-')
504
+ /* --option */
505
+ fprintf (stderr, "%s: unrecognized option `--%s'\n",
506
+ argv[0], nextchar);
507
+ else
508
+ /* +option or -option */
509
+ fprintf (stderr, "%s: unrecognized option `%c%s'\n",
510
+ argv[0], argv[optind][0], nextchar);
511
+ }
512
+ nextchar = (char *) "";
513
+ optind++;
514
+ return BAD_OPTION;
515
+ }
516
+ }
517
+
518
+ /* Look at and handle the next option-character. */
519
+
520
+ {
521
+ char c = *nextchar++;
522
+ char *temp = my_index (optstring, c);
523
+
524
+ /* Increment `optind' when we start to process its last character. */
525
+ if (*nextchar == '\0')
526
+ ++optind;
527
+
528
+ if (temp == NULL || c == ':')
529
+ {
530
+ if (opterr)
531
+ {
532
+ #if 0
533
+ if (c < 040 || c >= 0177)
534
+ fprintf (stderr, "%s: unrecognized option, character code 0%o\n",
535
+ argv[0], c);
536
+ else
537
+ fprintf (stderr, "%s: unrecognized option `-%c'\n", argv[0], c);
538
+ #else
539
+ /* 1003.2 specifies the format of this message. */
540
+ fprintf (stderr, "%s: illegal option -- %c\n", argv[0], c);
541
+ #endif
542
+ }
543
+ optopt = c;
544
+ return BAD_OPTION;
545
+ }
546
+ if (temp[1] == ':')
547
+ {
548
+ if (temp[2] == ':')
549
+ {
550
+ /* This is an option that accepts an argument optionally. */
551
+ if (*nextchar != '\0')
552
+ {
553
+ optarg = nextchar;
554
+ optind++;
555
+ }
556
+ else
557
+ optarg = 0;
558
+ nextchar = NULL;
559
+ }
560
+ else
561
+ {
562
+ /* This is an option that requires an argument. */
563
+ if (*nextchar != '\0')
564
+ {
565
+ optarg = nextchar;
566
+ /* If we end this ARGV-element by taking the rest as an arg,
567
+ we must advance to the next element now. */
568
+ optind++;
569
+ }
570
+ else if (optind == argc)
571
+ {
572
+ if (opterr)
573
+ {
574
+ #if 0
575
+ fprintf (stderr, "%s: option `-%c' requires an argument\n",
576
+ argv[0], c);
577
+ #else
578
+ /* 1003.2 specifies the format of this message. */
579
+ fprintf (stderr, "%s: option requires an argument -- %c\n",
580
+ argv[0], c);
581
+ #endif
582
+ }
583
+ optopt = c;
584
+ if (optstring[0] == ':')
585
+ c = ':';
586
+ else
587
+ c = BAD_OPTION;
588
+ }
589
+ else
590
+ /* We already incremented `optind' once;
591
+ increment it again when taking next ARGV-elt as argument. */
592
+ optarg = argv[optind++];
593
+ nextchar = NULL;
594
+ }
595
+ }
596
+ return c;
597
+ }
598
+ }
599
+
600
+ int
601
+ pgis_getopt (argc, argv, optstring)
602
+ int argc;
603
+ char *const *argv;
604
+ const char *optstring;
605
+ {
606
+ return _pgis_getopt_internal (argc, argv, optstring,
607
+ (const struct option *) 0,
608
+ (int *) 0,
609
+ 0);
610
+ }
611
+
612
+ int
613
+ pgis_getopt_long (argc, argv, options, long_options, opt_index)
614
+ int argc;
615
+ char *const *argv;
616
+ const char *options;
617
+ const struct option *long_options;
618
+ int *opt_index;
619
+ {
620
+ return _pgis_getopt_internal (argc, argv, options, long_options, opt_index, 0);
621
+ }
622
+
623
+ /* #endif _LIBC or not __GNU_LIBRARY__. */
624
+
625
+ #ifdef TEST
626
+
627
+ /* Compile with -DTEST to make an executable for use in testing
628
+ the above definition of `getopt'. */
629
+
630
+ int
631
+ main (argc, argv)
632
+ int argc;
633
+ char **argv;
634
+ {
635
+ int c;
636
+ int digit_optind = 0;
637
+
638
+ while (1)
639
+ {
640
+ int this_option_optind = optind ? optind : 1;
641
+
642
+ c = pgis_getopt (argc, argv, "abc:d:0123456789");
643
+ if (c == EOF)
644
+ break;
645
+
646
+ switch (c)
647
+ {
648
+ case '0':
649
+ case '1':
650
+ case '2':
651
+ case '3':
652
+ case '4':
653
+ case '5':
654
+ case '6':
655
+ case '7':
656
+ case '8':
657
+ case '9':
658
+ if (digit_optind != 0 && digit_optind != this_option_optind)
659
+ printf ("digits occur in two different argv-elements.\n");
660
+ digit_optind = this_option_optind;
661
+ printf ("option %c\n", c);
662
+ break;
663
+
664
+ case 'a':
665
+ printf ("option a\n");
666
+ break;
667
+
668
+ case 'b':
669
+ printf ("option b\n");
670
+ break;
671
+
672
+ case 'c':
673
+ printf ("option c with value `%s'\n", optarg);
674
+ break;
675
+
676
+ case BAD_OPTION:
677
+ break;
678
+
679
+ default:
680
+ printf ("?? getopt returned character code 0%o ??\n", c);
681
+ }
682
+ }
683
+
684
+ if (optind < argc)
685
+ {
686
+ printf ("non-option ARGV-elements: ");
687
+ while (optind < argc)
688
+ printf ("%s ", argv[optind++]);
689
+ printf ("\n");
690
+ }
691
+
692
+ exit (0);
693
+ }
694
+
695
+ #endif /* TEST */