/contrib/cvs/lib/getopt1.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 183 lines · 124 code · 28 blank · 31 comment · 13 complexity · 980b173001c6bd893bd5192b318e4b6b MD5 · raw file

  1. /* getopt_long and getopt_long_only entry points for GNU getopt.
  2. Copyright (C) 1987, 88, 89, 90, 91, 92, 1993
  3. Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details. */
  12. #ifdef HAVE_CONFIG_H
  13. #if defined (emacs) || defined (CONFIG_BROKETS)
  14. /* We use <config.h> instead of "config.h" so that a compilation
  15. using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
  16. (which it would do because it found this file in $srcdir). */
  17. #include <config.h>
  18. #else
  19. #include "config.h"
  20. #endif
  21. #endif
  22. #include "getopt.h"
  23. #ifndef __STDC__
  24. /* This is a separate conditional since some stdc systems
  25. reject `defined (const)'. */
  26. #ifndef const
  27. #define const
  28. #endif
  29. #endif
  30. #include <stdio.h>
  31. /* Comment out all this code if we are using the GNU C Library, and are not
  32. actually compiling the library itself. This code is part of the GNU C
  33. Library, but also included in many other GNU distributions. Compiling
  34. and linking in this code is a waste when using the GNU C library
  35. (especially if it is a shared library). Rather than having every GNU
  36. program understand `configure --with-gnu-libc' and omit the object files,
  37. it is simpler to just do this in the source for each such file. */
  38. #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
  39. /* This needs to come after some library #include
  40. to get __GNU_LIBRARY__ defined. */
  41. #ifdef __GNU_LIBRARY__
  42. #include <stdlib.h>
  43. #else
  44. char *getenv ();
  45. #endif
  46. #ifndef NULL
  47. #define NULL 0
  48. #endif
  49. int
  50. getopt_long (argc, argv, options, long_options, opt_index)
  51. int argc;
  52. char *const *argv;
  53. const char *options;
  54. const struct option *long_options;
  55. int *opt_index;
  56. {
  57. return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
  58. }
  59. /* Like getopt_long, but '-' as well as '--' can indicate a long option.
  60. If an option that starts with '-' (not '--') doesn't match a long option,
  61. but does match a short option, it is parsed as a short option
  62. instead. */
  63. int
  64. getopt_long_only (argc, argv, options, long_options, opt_index)
  65. int argc;
  66. char *const *argv;
  67. const char *options;
  68. const struct option *long_options;
  69. int *opt_index;
  70. {
  71. return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
  72. }
  73. #endif /* _LIBC or not __GNU_LIBRARY__. */
  74. #ifdef TEST
  75. #include <stdio.h>
  76. int
  77. main (argc, argv)
  78. int argc;
  79. char **argv;
  80. {
  81. int c;
  82. int digit_optind = 0;
  83. while (1)
  84. {
  85. int this_option_optind = optind ? optind : 1;
  86. int option_index = 0;
  87. static struct option long_options[] =
  88. {
  89. {"add", 1, 0, 0},
  90. {"append", 0, 0, 0},
  91. {"delete", 1, 0, 0},
  92. {"verbose", 0, 0, 0},
  93. {"create", 0, 0, 0},
  94. {"file", 1, 0, 0},
  95. {0, 0, 0, 0}
  96. };
  97. c = getopt_long (argc, argv, "abc:d:0123456789",
  98. long_options, &option_index);
  99. if (c == EOF)
  100. break;
  101. switch (c)
  102. {
  103. case 0:
  104. printf ("option %s", long_options[option_index].name);
  105. if (optarg)
  106. printf (" with arg %s", optarg);
  107. printf ("\n");
  108. break;
  109. case '0':
  110. case '1':
  111. case '2':
  112. case '3':
  113. case '4':
  114. case '5':
  115. case '6':
  116. case '7':
  117. case '8':
  118. case '9':
  119. if (digit_optind != 0 && digit_optind != this_option_optind)
  120. printf ("digits occur in two different argv-elements.\n");
  121. digit_optind = this_option_optind;
  122. printf ("option %c\n", c);
  123. break;
  124. case 'a':
  125. printf ("option a\n");
  126. break;
  127. case 'b':
  128. printf ("option b\n");
  129. break;
  130. case 'c':
  131. printf ("option c with value `%s'\n", optarg);
  132. break;
  133. case 'd':
  134. printf ("option d with value `%s'\n", optarg);
  135. break;
  136. case '?':
  137. break;
  138. default:
  139. printf ("?? getopt returned character code 0%o ??\n", c);
  140. }
  141. }
  142. if (optind < argc)
  143. {
  144. printf ("non-option ARGV-elements: ");
  145. while (optind < argc)
  146. printf ("%s ", argv[optind++]);
  147. printf ("\n");
  148. }
  149. exit (0);
  150. }
  151. #endif /* TEST */