getopt1.c 4.3 KB

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