This contains my bachelors thesis and associated tex files, code snippets and maybe more. Topic: Data Movement in Heterogeneous Memories with Intel Data Streaming Accelerator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1051 lines
29 KiB

  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 bug-glibc@gnu.org
  4. before changing it!
  5. Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98
  6. Free Software Foundation, Inc.
  7. NOTE: The canonical source of this file is maintained with the GNU C Library.
  8. Bugs can be reported to bug-glibc@gnu.org.
  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. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  20. USA. */
  21. /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
  22. Ditto for AIX 3.2 and <stdlib.h>. */
  23. #ifndef _NO_PROTO
  24. #define _NO_PROTO
  25. #endif
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #if !defined (__STDC__) || !__STDC__
  30. /* This is a separate conditional since some stdc systems
  31. reject `defined (const)'. */
  32. #ifndef const
  33. #define const
  34. #endif
  35. #endif
  36. #include <stdio.h>
  37. /* Comment out all this code if we are using the GNU C Library, and are not
  38. actually compiling the library itself. This code is part of the GNU C
  39. Library, but also included in many other GNU distributions. Compiling
  40. and linking in this code is a waste when using the GNU C library
  41. (especially if it is a shared library). Rather than having every GNU
  42. program understand `configure --with-gnu-libc' and omit the object files,
  43. it is simpler to just do this in the source for each such file. */
  44. #define GETOPT_INTERFACE_VERSION 2
  45. #if !defined (_LIBC) && defined (__GLIBC__) && __GLIBC__ >= 2
  46. #include <gnu-versions.h>
  47. #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
  48. #define ELIDE_CODE
  49. #endif
  50. #endif
  51. #ifndef ELIDE_CODE
  52. /* This needs to come after some library #include
  53. to get __GNU_LIBRARY__ defined. */
  54. #ifdef __GNU_LIBRARY__
  55. /* Don't include stdlib.h for non-GNU C libraries because some of them
  56. contain conflicting prototypes for getopt. */
  57. #include <stdlib.h>
  58. #include <unistd.h>
  59. #endif /* GNU C library. */
  60. #ifdef VMS
  61. #include <unixlib.h>
  62. #if HAVE_STRING_H - 0
  63. #include <string.h>
  64. #endif
  65. #endif
  66. #if defined (WINDOWS32) && !defined (__CYGWIN32__)
  67. /* It's not Unix, really. See? Capital letters. */
  68. #include <windows.h>
  69. #define getpid() GetCurrentProcessId()
  70. #endif
  71. /* This is for other GNU distributions with internationalized messages.
  72. When compiling libc, the _ macro is predefined. */
  73. #ifdef USE_NLS
  74. # include <libintl.h>
  75. # define _(msgid) gettext (msgid)
  76. #else
  77. # define _(msgid) (msgid)
  78. #endif
  79. /* This version of `getopt' appears to the caller like standard Unix `getopt'
  80. but it behaves differently for the user, since it allows the user
  81. to intersperse the options with the other arguments.
  82. As `getopt' works, it permutes the elements of ARGV so that,
  83. when it is done, all the options precede everything else. Thus
  84. all application programs are extended to handle flexible argument order.
  85. Setting the environment variable POSIXLY_CORRECT disables permutation.
  86. Then the behavior is completely standard.
  87. GNU application programs can use a third alternative mode in which
  88. they can distinguish the relative order of options and other arguments. */
  89. #include "getopt.h"
  90. /* For communication from `getopt' to the caller.
  91. When `getopt' finds an option that takes an argument,
  92. the argument value is returned here.
  93. Also, when `ordering' is RETURN_IN_ORDER,
  94. each non-option ARGV-element is returned here. */
  95. char *optarg = NULL;
  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. On entry to `getopt', zero means this is the first call; initialize.
  100. When `getopt' returns -1, this is the index of the first of the
  101. non-option elements that the caller should itself scan.
  102. Otherwise, `optind' communicates from one call to the next
  103. how much of ARGV has been scanned so far. */
  104. /* 1003.2 says this must be 1 before any call. */
  105. int optind = 1;
  106. /* Formerly, initialization of getopt depended on optind==0, which
  107. causes problems with re-calling getopt as programs generally don't
  108. know that. */
  109. int __getopt_initialized = 0;
  110. /* The next char to be scanned in the option-element
  111. in which the last option character we returned was found.
  112. This allows us to pick up the scan where we left off.
  113. If this is zero, or a null string, it means resume the scan
  114. by advancing to the next ARGV-element. */
  115. static char *nextchar;
  116. /* Callers store zero here to inhibit the error message
  117. for unrecognized options. */
  118. int opterr = 1;
  119. /* Set to an option character which was unrecognized.
  120. This must be initialized on some systems to avoid linking in the
  121. system's own getopt implementation. */
  122. int optopt = '?';
  123. /* Describe how to deal with options that follow non-option ARGV-elements.
  124. If the caller did not specify anything,
  125. the default is REQUIRE_ORDER if the environment variable
  126. POSIXLY_CORRECT is defined, PERMUTE otherwise.
  127. REQUIRE_ORDER means don't recognize them as options;
  128. stop option processing when the first non-option is seen.
  129. This is what Unix does.
  130. This mode of operation is selected by either setting the environment
  131. variable POSIXLY_CORRECT, or using `+' as the first character
  132. of the list of option characters.
  133. PERMUTE is the default. We permute the contents of ARGV as we scan,
  134. so that eventually all the non-options are at the end. This allows options
  135. to be given in any order, even with programs that were not written to
  136. expect this.
  137. RETURN_IN_ORDER is an option available to programs that were written
  138. to expect options and other ARGV-elements in any order and that care about
  139. the ordering of the two. We describe each non-option ARGV-element
  140. as if it were the argument of an option with character code 1.
  141. Using `-' as the first character of the list of option characters
  142. selects this mode of operation.
  143. The special argument `--' forces an end of option-scanning regardless
  144. of the value of `ordering'. In the case of RETURN_IN_ORDER, only
  145. `--' can cause `getopt' to return -1 with `optind' != ARGC. */
  146. static enum
  147. {
  148. REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
  149. } ordering;
  150. /* Value of POSIXLY_CORRECT environment variable. */
  151. static char *posixly_correct;
  152. #ifdef __GNU_LIBRARY__
  153. /* We want to avoid inclusion of string.h with non-GNU libraries
  154. because there are many ways it can cause trouble.
  155. On some systems, it contains special magic macros that don't work
  156. in GCC. */
  157. #include <string.h>
  158. #define my_index strchr
  159. #else
  160. /* Avoid depending on library functions or files
  161. whose names are inconsistent. */
  162. char *getenv ();
  163. static char *
  164. my_index (str, chr)
  165. const char *str;
  166. int chr;
  167. {
  168. while (*str)
  169. {
  170. if (*str == chr)
  171. return (char *) str;
  172. str++;
  173. }
  174. return 0;
  175. }
  176. /* If using GCC, we can safely declare strlen this way.
  177. If not using GCC, it is ok not to declare it. */
  178. #ifdef __GNUC__
  179. /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
  180. That was relevant to code that was here before. */
  181. #if !defined (__STDC__) || !__STDC__
  182. /* gcc with -traditional declares the built-in strlen to return int,
  183. and has done so at least since version 2.4.5. -- rms. */
  184. extern int strlen (const char *);
  185. #endif /* not __STDC__ */
  186. #endif /* __GNUC__ */
  187. #endif /* not __GNU_LIBRARY__ */
  188. /* Handle permutation of arguments. */
  189. /* Describe the part of ARGV that contains non-options that have
  190. been skipped. `first_nonopt' is the index in ARGV of the first of them;
  191. `last_nonopt' is the index after the last of them. */
  192. static int first_nonopt;
  193. static int last_nonopt;
  194. #ifdef _LIBC
  195. /* Bash 2.0 gives us an environment variable containing flags
  196. indicating ARGV elements that should not be considered arguments. */
  197. /* Defined in getopt_init.c */
  198. extern char *__getopt_nonoption_flags;
  199. static int nonoption_flags_max_len;
  200. static int nonoption_flags_len;
  201. static int original_argc;
  202. static char *const *original_argv;
  203. extern pid_t __libc_pid;
  204. /* Make sure the environment variable bash 2.0 puts in the environment
  205. is valid for the getopt call we must make sure that the ARGV passed
  206. to getopt is that one passed to the process. */
  207. static void
  208. __attribute__ ((unused))
  209. store_args_and_env (int argc, char *const *argv)
  210. {
  211. /* XXX This is no good solution. We should rather copy the args so
  212. that we can compare them later. But we must not use malloc(3). */
  213. original_argc = argc;
  214. original_argv = argv;
  215. }
  216. text_set_element (__libc_subinit, store_args_and_env);
  217. # define SWAP_FLAGS(ch1, ch2) \
  218. if (nonoption_flags_len > 0) \
  219. { \
  220. char __tmp = __getopt_nonoption_flags[ch1]; \
  221. __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
  222. __getopt_nonoption_flags[ch2] = __tmp; \
  223. }
  224. #else /* !_LIBC */
  225. # define SWAP_FLAGS(ch1, ch2)
  226. #endif /* _LIBC */
  227. /* Exchange two adjacent subsequences of ARGV.
  228. One subsequence is elements [first_nonopt,last_nonopt)
  229. which contains all the non-options that have been skipped so far.
  230. The other is elements [last_nonopt,optind), which contains all
  231. the options processed since those non-options were skipped.
  232. `first_nonopt' and `last_nonopt' are relocated so that they describe
  233. the new indices of the non-options in ARGV after they are moved. */
  234. #if defined (__STDC__) && __STDC__
  235. static void exchange (char **);
  236. #endif
  237. static void
  238. exchange (argv)
  239. char **argv;
  240. {
  241. int bottom = first_nonopt;
  242. int middle = last_nonopt;
  243. int top = optind;
  244. char *tem;
  245. /* Exchange the shorter segment with the far end of the longer segment.
  246. That puts the shorter segment into the right place.
  247. It leaves the longer segment in the right place overall,
  248. but it consists of two parts that need to be swapped next. */
  249. #ifdef _LIBC
  250. /* First make sure the handling of the `__getopt_nonoption_flags'
  251. string can work normally. Our top argument must be in the range
  252. of the string. */
  253. if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
  254. {
  255. /* We must extend the array. The user plays games with us and
  256. presents new arguments. */
  257. char *new_str = malloc (top + 1);
  258. if (new_str == NULL)
  259. nonoption_flags_len = nonoption_flags_max_len = 0;
  260. else
  261. {
  262. memcpy (new_str, __getopt_nonoption_flags, nonoption_flags_max_len);
  263. memset (&new_str[nonoption_flags_max_len], '\0',
  264. top + 1 - nonoption_flags_max_len);
  265. nonoption_flags_max_len = top + 1;
  266. __getopt_nonoption_flags = new_str;
  267. }
  268. }
  269. #endif
  270. while (top > middle && middle > bottom)
  271. {
  272. if (top - middle > middle - bottom)
  273. {
  274. /* Bottom segment is the short one. */
  275. int len = middle - bottom;
  276. register int i;
  277. /* Swap it with the top part of the top segment. */
  278. for (i = 0; i < len; i++)
  279. {
  280. tem = argv[bottom + i];
  281. argv[bottom + i] = argv[top - (middle - bottom) + i];
  282. argv[top - (middle - bottom) + i] = tem;
  283. SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
  284. }
  285. /* Exclude the moved bottom segment from further swapping. */
  286. top -= len;
  287. }
  288. else
  289. {
  290. /* Top segment is the short one. */
  291. int len = top - middle;
  292. register int i;
  293. /* Swap it with the bottom part of the bottom segment. */
  294. for (i = 0; i < len; i++)
  295. {
  296. tem = argv[bottom + i];
  297. argv[bottom + i] = argv[middle + i];
  298. argv[middle + i] = tem;
  299. SWAP_FLAGS (bottom + i, middle + i);
  300. }
  301. /* Exclude the moved top segment from further swapping. */
  302. bottom += len;
  303. }
  304. }
  305. /* Update records for the slots the non-options now occupy. */
  306. first_nonopt += (optind - last_nonopt);
  307. last_nonopt = optind;
  308. }
  309. /* Initialize the internal data when the first call is made. */
  310. #if defined (__STDC__) && __STDC__
  311. static const char *_getopt_initialize (int, char *const *, const char *);
  312. #endif
  313. static const char *
  314. _getopt_initialize (argc, argv, optstring)
  315. int argc;
  316. char *const *argv;
  317. const char *optstring;
  318. {
  319. /* Start processing options with ARGV-element 1 (since ARGV-element 0
  320. is the program name); the sequence of previously skipped
  321. non-option ARGV-elements is empty. */
  322. first_nonopt = last_nonopt = optind;
  323. nextchar = NULL;
  324. posixly_correct = getenv ("POSIXLY_CORRECT");
  325. /* Determine how to handle the ordering of options and nonoptions. */
  326. if (optstring[0] == '-')
  327. {
  328. ordering = RETURN_IN_ORDER;
  329. ++optstring;
  330. }
  331. else if (optstring[0] == '+')
  332. {
  333. ordering = REQUIRE_ORDER;
  334. ++optstring;
  335. }
  336. else if (posixly_correct != NULL)
  337. ordering = REQUIRE_ORDER;
  338. else
  339. ordering = PERMUTE;
  340. #ifdef _LIBC
  341. if (posixly_correct == NULL
  342. && argc == original_argc && argv == original_argv)
  343. {
  344. if (nonoption_flags_max_len == 0)
  345. {
  346. if (__getopt_nonoption_flags == NULL
  347. || __getopt_nonoption_flags[0] == '\0')
  348. nonoption_flags_max_len = -1;
  349. else
  350. {
  351. const char *orig_str = __getopt_nonoption_flags;
  352. int len = nonoption_flags_max_len = strlen (orig_str);
  353. if (nonoption_flags_max_len < argc)
  354. nonoption_flags_max_len = argc;
  355. __getopt_nonoption_flags =
  356. (char *) malloc (nonoption_flags_max_len);
  357. if (__getopt_nonoption_flags == NULL)
  358. nonoption_flags_max_len = -1;
  359. else
  360. {
  361. memcpy (__getopt_nonoption_flags, orig_str, len);
  362. memset (&__getopt_nonoption_flags[len], '\0',
  363. nonoption_flags_max_len - len);
  364. }
  365. }
  366. }
  367. nonoption_flags_len = nonoption_flags_max_len;
  368. }
  369. else
  370. nonoption_flags_len = 0;
  371. #endif
  372. return optstring;
  373. }
  374. /* Scan elements of ARGV (whose length is ARGC) for option characters
  375. given in OPTSTRING.
  376. If an element of ARGV starts with '-', and is not exactly "-" or "--",
  377. then it is an option element. The characters of this element
  378. (aside from the initial '-') are option characters. If `getopt'
  379. is called repeatedly, it returns successively each of the option characters
  380. from each of the option elements.
  381. If `getopt' finds another option character, it returns that character,
  382. updating `optind' and `nextchar' so that the next call to `getopt' can
  383. resume the scan with the following option character or ARGV-element.
  384. If there are no more option characters, `getopt' returns -1.
  385. Then `optind' is the index in ARGV of the first ARGV-element
  386. that is not an option. (The ARGV-elements have been permuted
  387. so that those that are not options now come last.)
  388. OPTSTRING is a string containing the legitimate option characters.
  389. If an option character is seen that is not listed in OPTSTRING,
  390. return '?' after printing an error message. If you set `opterr' to
  391. zero, the error message is suppressed but we still return '?'.
  392. If a char in OPTSTRING is followed by a colon, that means it wants an arg,
  393. so the following text in the same ARGV-element, or the text of the following
  394. ARGV-element, is returned in `optarg'. Two colons mean an option that
  395. wants an optional arg; if there is text in the current ARGV-element,
  396. it is returned in `optarg', otherwise `optarg' is set to zero.
  397. If OPTSTRING starts with `-' or `+', it requests different methods of
  398. handling the non-option ARGV-elements.
  399. See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
  400. Long-named options begin with `--' instead of `-'.
  401. Their names may be abbreviated as long as the abbreviation is unique
  402. or is an exact match for some defined option. If they have an
  403. argument, it follows the option name in the same ARGV-element, separated
  404. from the option name by a `=', or else the in next ARGV-element.
  405. When `getopt' finds a long-named option, it returns 0 if that option's
  406. `flag' field is nonzero, the value of the option's `val' field
  407. if the `flag' field is zero.
  408. The elements of ARGV aren't really const, because we permute them.
  409. But we pretend they're const in the prototype to be compatible
  410. with other systems.
  411. LONGOPTS is a vector of `struct option' terminated by an
  412. element containing a name which is zero.
  413. LONGIND returns the index in LONGOPT of the long-named option found.
  414. It is only valid when a long-named option has been found by the most
  415. recent call.
  416. If LONG_ONLY is nonzero, '-' as well as '--' can introduce
  417. long-named options. */
  418. int
  419. _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
  420. int argc;
  421. char *const *argv;
  422. const char *optstring;
  423. const struct option *longopts;
  424. int *longind;
  425. int long_only;
  426. {
  427. optarg = NULL;
  428. if (optind == 0 || !__getopt_initialized)
  429. {
  430. if (optind == 0)
  431. optind = 1; /* Don't scan ARGV[0], the program name. */
  432. optstring = _getopt_initialize (argc, argv, optstring);
  433. __getopt_initialized = 1;
  434. }
  435. /* Test whether ARGV[optind] points to a non-option argument.
  436. Either it does not have option syntax, or there is an environment flag
  437. from the shell indicating it is not an option. The later information
  438. is only used when the used in the GNU libc. */
  439. #ifdef _LIBC
  440. #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
  441. || (optind < nonoption_flags_len \
  442. && __getopt_nonoption_flags[optind] == '1'))
  443. #else
  444. #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
  445. #endif
  446. if (nextchar == NULL || *nextchar == '\0')
  447. {
  448. /* Advance to the next ARGV-element. */
  449. /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
  450. moved back by the user (who may also have changed the arguments). */
  451. if (last_nonopt > optind)
  452. last_nonopt = optind;
  453. if (first_nonopt > optind)
  454. first_nonopt = optind;
  455. if (ordering == PERMUTE)
  456. {
  457. /* If we have just processed some options following some non-options,
  458. exchange them so that the options come first. */
  459. if (first_nonopt != last_nonopt && last_nonopt != optind)
  460. exchange ((char **) argv);
  461. else if (last_nonopt != optind)
  462. first_nonopt = optind;
  463. /* Skip any additional non-options
  464. and extend the range of non-options previously skipped. */
  465. while (optind < argc && NONOPTION_P)
  466. optind++;
  467. last_nonopt = optind;
  468. }
  469. /* The special ARGV-element `--' means premature end of options.
  470. Skip it like a null option,
  471. then exchange with previous non-options as if it were an option,
  472. then skip everything else like a non-option. */
  473. if (optind != argc && !strcmp (argv[optind], "--"))
  474. {
  475. optind++;
  476. if (first_nonopt != last_nonopt && last_nonopt != optind)
  477. exchange ((char **) argv);
  478. else if (first_nonopt == last_nonopt)
  479. first_nonopt = optind;
  480. last_nonopt = argc;
  481. optind = argc;
  482. }
  483. /* If we have done all the ARGV-elements, stop the scan
  484. and back over any non-options that we skipped and permuted. */
  485. if (optind == argc)
  486. {
  487. /* Set the next-arg-index to point at the non-options
  488. that we previously skipped, so the caller will digest them. */
  489. if (first_nonopt != last_nonopt)
  490. optind = first_nonopt;
  491. return -1;
  492. }
  493. /* If we have come to a non-option and did not permute it,
  494. either stop the scan or describe it to the caller and pass it by. */
  495. if (NONOPTION_P)
  496. {
  497. if (ordering == REQUIRE_ORDER)
  498. return -1;
  499. optarg = argv[optind++];
  500. return 1;
  501. }
  502. /* We have found another option-ARGV-element.
  503. Skip the initial punctuation. */
  504. nextchar = (argv[optind] + 1
  505. + (longopts != NULL && argv[optind][1] == '-'));
  506. }
  507. /* Decode the current option-ARGV-element. */
  508. /* Check whether the ARGV-element is a long option.
  509. If long_only and the ARGV-element has the form "-f", where f is
  510. a valid short option, don't consider it an abbreviated form of
  511. a long option that starts with f. Otherwise there would be no
  512. way to give the -f short option.
  513. On the other hand, if there's a long option "fubar" and
  514. the ARGV-element is "-fu", do consider that an abbreviation of
  515. the long option, just like "--fu", and not "-f" with arg "u".
  516. This distinction seems to be the most useful approach. */
  517. if (longopts != NULL
  518. && (argv[optind][1] == '-'
  519. || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
  520. {
  521. char *nameend;
  522. const struct option *p;
  523. const struct option *pfound = NULL;
  524. int exact = 0;
  525. int ambig = 0;
  526. int indfound = -1;
  527. int option_index;
  528. for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
  529. /* Do nothing. */ ;
  530. /* Test all long options for either exact match
  531. or abbreviated matches. */
  532. for (p = longopts, option_index = 0; p->name; p++, option_index++)
  533. if (!strncmp (p->name, nextchar, nameend - nextchar))
  534. {
  535. if ((unsigned int) (nameend - nextchar)
  536. == (unsigned int) strlen (p->name))
  537. {
  538. /* Exact match found. */
  539. pfound = p;
  540. indfound = option_index;
  541. exact = 1;
  542. break;
  543. }
  544. else if (pfound == NULL)
  545. {
  546. /* First nonexact match found. */
  547. pfound = p;
  548. indfound = option_index;
  549. }
  550. else
  551. /* Second or later nonexact match found. */
  552. ambig = 1;
  553. }
  554. if (ambig && !exact)
  555. {
  556. if (opterr)
  557. fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
  558. argv[0], argv[optind]);
  559. nextchar += strlen (nextchar);
  560. optind++;
  561. optopt = 0;
  562. return '?';
  563. }
  564. if (pfound != NULL)
  565. {
  566. option_index = indfound;
  567. optind++;
  568. if (*nameend)
  569. {
  570. /* Don't test has_arg with >, because some C compilers don't
  571. allow it to be used on enums. */
  572. if (pfound->has_arg)
  573. optarg = nameend + 1;
  574. else
  575. {
  576. if (opterr)
  577. if (argv[optind - 1][1] == '-')
  578. /* --option */
  579. fprintf (stderr,
  580. _("%s: option `--%s' doesn't allow an argument\n"),
  581. argv[0], pfound->name);
  582. else
  583. /* +option or -option */
  584. fprintf (stderr,
  585. _("%s: option `%c%s' doesn't allow an argument\n"),
  586. argv[0], argv[optind - 1][0], pfound->name);
  587. nextchar += strlen (nextchar);
  588. optopt = pfound->val;
  589. return '?';
  590. }
  591. }
  592. else if (pfound->has_arg == 1)
  593. {
  594. if (optind < argc)
  595. optarg = argv[optind++];
  596. else
  597. {
  598. if (opterr)
  599. fprintf (stderr,
  600. _("%s: option `%s' requires an argument\n"),
  601. argv[0], argv[optind - 1]);
  602. nextchar += strlen (nextchar);
  603. optopt = pfound->val;
  604. return optstring[0] == ':' ? ':' : '?';
  605. }
  606. }
  607. nextchar += strlen (nextchar);
  608. if (longind != NULL)
  609. *longind = option_index;
  610. if (pfound->flag)
  611. {
  612. *(pfound->flag) = pfound->val;
  613. return 0;
  614. }
  615. return pfound->val;
  616. }
  617. /* Can't find it as a long option. If this is not getopt_long_only,
  618. or the option starts with '--' or is not a valid short
  619. option, then it's an error.
  620. Otherwise interpret it as a short option. */
  621. if (!long_only || argv[optind][1] == '-'
  622. || my_index (optstring, *nextchar) == NULL)
  623. {
  624. if (opterr)
  625. {
  626. if (argv[optind][1] == '-')
  627. /* --option */
  628. fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
  629. argv[0], nextchar);
  630. else
  631. /* +option or -option */
  632. fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
  633. argv[0], argv[optind][0], nextchar);
  634. }
  635. nextchar = (char *) "";
  636. optind++;
  637. optopt = 0;
  638. return '?';
  639. }
  640. }
  641. /* Look at and handle the next short option-character. */
  642. {
  643. char c = *nextchar++;
  644. char *temp = my_index (optstring, c);
  645. /* Increment `optind' when we start to process its last character. */
  646. if (*nextchar == '\0')
  647. ++optind;
  648. if (temp == NULL || c == ':')
  649. {
  650. if (opterr)
  651. {
  652. if (posixly_correct)
  653. /* 1003.2 specifies the format of this message. */
  654. fprintf (stderr, _("%s: illegal option -- %c\n"),
  655. argv[0], c);
  656. else
  657. fprintf (stderr, _("%s: invalid option -- %c\n"),
  658. argv[0], c);
  659. }
  660. optopt = c;
  661. return '?';
  662. }
  663. /* Convenience. Treat POSIX -W foo same as long option --foo */
  664. if (temp[0] == 'W' && temp[1] == ';')
  665. {
  666. char *nameend;
  667. const struct option *p;
  668. const struct option *pfound = NULL;
  669. int exact = 0;
  670. int ambig = 0;
  671. int indfound = 0;
  672. int option_index;
  673. /* This is an option that requires an argument. */
  674. if (*nextchar != '\0')
  675. {
  676. optarg = nextchar;
  677. /* If we end this ARGV-element by taking the rest as an arg,
  678. we must advance to the next element now. */
  679. optind++;
  680. }
  681. else if (optind == argc)
  682. {
  683. if (opterr)
  684. {
  685. /* 1003.2 specifies the format of this message. */
  686. fprintf (stderr, _("%s: option requires an argument -- %c\n"),
  687. argv[0], c);
  688. }
  689. optopt = c;
  690. if (optstring[0] == ':')
  691. c = ':';
  692. else
  693. c = '?';
  694. return c;
  695. }
  696. else
  697. /* We already incremented `optind' once;
  698. increment it again when taking next ARGV-elt as argument. */
  699. optarg = argv[optind++];
  700. /* optarg is now the argument, see if it's in the
  701. table of longopts. */
  702. for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
  703. /* Do nothing. */ ;
  704. /* Test all long options for either exact match
  705. or abbreviated matches. */
  706. for (p = longopts, option_index = 0; p->name; p++, option_index++)
  707. if (!strncmp (p->name, nextchar, nameend - nextchar))
  708. {
  709. if ((unsigned int) (nameend - nextchar) == strlen (p->name))
  710. {
  711. /* Exact match found. */
  712. pfound = p;
  713. indfound = option_index;
  714. exact = 1;
  715. break;
  716. }
  717. else if (pfound == NULL)
  718. {
  719. /* First nonexact match found. */
  720. pfound = p;
  721. indfound = option_index;
  722. }
  723. else
  724. /* Second or later nonexact match found. */
  725. ambig = 1;
  726. }
  727. if (ambig && !exact)
  728. {
  729. if (opterr)
  730. fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
  731. argv[0], argv[optind]);
  732. nextchar += strlen (nextchar);
  733. optind++;
  734. return '?';
  735. }
  736. if (pfound != NULL)
  737. {
  738. option_index = indfound;
  739. if (*nameend)
  740. {
  741. /* Don't test has_arg with >, because some C compilers don't
  742. allow it to be used on enums. */
  743. if (pfound->has_arg)
  744. optarg = nameend + 1;
  745. else
  746. {
  747. if (opterr)
  748. fprintf (stderr, _("\
  749. %s: option `-W %s' doesn't allow an argument\n"),
  750. argv[0], pfound->name);
  751. nextchar += strlen (nextchar);
  752. return '?';
  753. }
  754. }
  755. else if (pfound->has_arg == 1)
  756. {
  757. if (optind < argc)
  758. optarg = argv[optind++];
  759. else
  760. {
  761. if (opterr)
  762. fprintf (stderr,
  763. _("%s: option `%s' requires an argument\n"),
  764. argv[0], argv[optind - 1]);
  765. nextchar += strlen (nextchar);
  766. return optstring[0] == ':' ? ':' : '?';
  767. }
  768. }
  769. nextchar += strlen (nextchar);
  770. if (longind != NULL)
  771. *longind = option_index;
  772. if (pfound->flag)
  773. {
  774. *(pfound->flag) = pfound->val;
  775. return 0;
  776. }
  777. return pfound->val;
  778. }
  779. nextchar = NULL;
  780. return 'W'; /* Let the application handle it. */
  781. }
  782. if (temp[1] == ':')
  783. {
  784. if (temp[2] == ':')
  785. {
  786. /* This is an option that accepts an argument optionally. */
  787. if (*nextchar != '\0')
  788. {
  789. optarg = nextchar;
  790. optind++;
  791. }
  792. else
  793. optarg = NULL;
  794. nextchar = NULL;
  795. }
  796. else
  797. {
  798. /* This is an option that requires an argument. */
  799. if (*nextchar != '\0')
  800. {
  801. optarg = nextchar;
  802. /* If we end this ARGV-element by taking the rest as an arg,
  803. we must advance to the next element now. */
  804. optind++;
  805. }
  806. else if (optind == argc)
  807. {
  808. if (opterr)
  809. {
  810. /* 1003.2 specifies the format of this message. */
  811. fprintf (stderr,
  812. _("%s: option requires an argument -- %c\n"),
  813. argv[0], c);
  814. }
  815. optopt = c;
  816. if (optstring[0] == ':')
  817. c = ':';
  818. else
  819. c = '?';
  820. }
  821. else
  822. /* We already incremented `optind' once;
  823. increment it again when taking next ARGV-elt as argument. */
  824. optarg = argv[optind++];
  825. nextchar = NULL;
  826. }
  827. }
  828. return c;
  829. }
  830. }
  831. int
  832. getopt (argc, argv, optstring)
  833. int argc;
  834. char *const *argv;
  835. const char *optstring;
  836. {
  837. return _getopt_internal (argc, argv, optstring,
  838. (const struct option *) 0,
  839. (int *) 0,
  840. 0);
  841. }
  842. #endif /* Not ELIDE_CODE. */
  843. #ifdef TEST
  844. /* Compile with -DTEST to make an executable for use in testing
  845. the above definition of `getopt'. */
  846. int
  847. main (argc, argv)
  848. int argc;
  849. char **argv;
  850. {
  851. int c;
  852. int digit_optind = 0;
  853. while (1)
  854. {
  855. int this_option_optind = optind ? optind : 1;
  856. c = getopt (argc, argv, "abc:d:0123456789");
  857. if (c == -1)
  858. break;
  859. switch (c)
  860. {
  861. case '0':
  862. case '1':
  863. case '2':
  864. case '3':
  865. case '4':
  866. case '5':
  867. case '6':
  868. case '7':
  869. case '8':
  870. case '9':
  871. if (digit_optind != 0 && digit_optind != this_option_optind)
  872. printf ("digits occur in two different argv-elements.\n");
  873. digit_optind = this_option_optind;
  874. printf ("option %c\n", c);
  875. break;
  876. case 'a':
  877. printf ("option a\n");
  878. break;
  879. case 'b':
  880. printf ("option b\n");
  881. break;
  882. case 'c':
  883. printf ("option c with value `%s'\n", optarg);
  884. break;
  885. case '?':
  886. break;
  887. default:
  888. printf ("?? getopt returned character code 0%o ??\n", c);
  889. }
  890. }
  891. if (optind < argc)
  892. {
  893. printf ("non-option ARGV-elements: ");
  894. while (optind < argc)
  895. printf ("%s ", argv[optind++]);
  896. printf ("\n");
  897. }
  898. exit (0);
  899. }
  900. #endif /* TEST */