getopt1.c 4.1 KB

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