getopt1.c 4.4 KB

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