getopt1.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* getopt_long and getopt_long_only entry points for GNU getopt.
  2. Copyright (C) 1987,88,89,90,91,92,93,94,96,97 Free Software Foundation, Inc.
  3. the C library, however. The master source lives in /gd/gnu/lib.
  4. NOTE: The canonical source of this file is maintained with the GNU C Library.
  5. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
  6. This program is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. USA. */
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include "getopt.h"
  22. #if !defined (__STDC__) || !__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. #define GETOPT_INTERFACE_VERSION 2
  38. #if !defined (_LIBC) && defined (__GLIBC__) && __GLIBC__ >= 2
  39. #include <gnu-versions.h>
  40. #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
  41. #define ELIDE_CODE
  42. #endif
  43. #endif
  44. #ifndef ELIDE_CODE
  45. /* This needs to come after some library #include
  46. to get __GNU_LIBRARY__ defined. */
  47. #ifdef __GNU_LIBRARY__
  48. #include <stdlib.h>
  49. #endif
  50. #ifndef NULL
  51. #define NULL 0
  52. #endif
  53. int
  54. getopt_long (argc, argv, options, long_options, opt_index)
  55. int argc;
  56. char *const *argv;
  57. const char *options;
  58. const struct option *long_options;
  59. int *opt_index;
  60. {
  61. return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
  62. }
  63. /* Like getopt_long, but '-' as well as '--' can indicate a long option.
  64. If an option that starts with '-' (not '--') doesn't match a long option,
  65. but does match a short option, it is parsed as a short option
  66. instead. */
  67. int
  68. getopt_long_only (argc, argv, options, long_options, opt_index)
  69. int argc;
  70. char *const *argv;
  71. const char *options;
  72. const struct option *long_options;
  73. int *opt_index;
  74. {
  75. return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
  76. }
  77. #endif /* Not ELIDE_CODE. */
  78. #ifdef TEST
  79. #include <stdio.h>
  80. int
  81. main (argc, argv)
  82. int argc;
  83. char **argv;
  84. {
  85. int c;
  86. int digit_optind = 0;
  87. while (1)
  88. {
  89. int this_option_optind = optind ? optind : 1;
  90. int option_index = 0;
  91. static struct option long_options[] =
  92. {
  93. {"add", 1, 0, 0},
  94. {"append", 0, 0, 0},
  95. {"delete", 1, 0, 0},
  96. {"verbose", 0, 0, 0},
  97. {"create", 0, 0, 0},
  98. {"file", 1, 0, 0},
  99. {0, 0, 0, 0}
  100. };
  101. c = getopt_long (argc, argv, "abc:d:0123456789",
  102. long_options, &option_index);
  103. if (c == -1)
  104. break;
  105. switch (c)
  106. {
  107. case 0:
  108. printf ("option %s", long_options[option_index].name);
  109. if (optarg)
  110. printf (" with arg %s", optarg);
  111. printf ("\n");
  112. break;
  113. case '0':
  114. case '1':
  115. case '2':
  116. case '3':
  117. case '4':
  118. case '5':
  119. case '6':
  120. case '7':
  121. case '8':
  122. case '9':
  123. if (digit_optind != 0 && digit_optind != this_option_optind)
  124. printf ("digits occur in two different argv-elements.\n");
  125. digit_optind = this_option_optind;
  126. printf ("option %c\n", c);
  127. break;
  128. case 'a':
  129. printf ("option a\n");
  130. break;
  131. case 'b':
  132. printf ("option b\n");
  133. break;
  134. case 'c':
  135. printf ("option c with value `%s'\n", optarg);
  136. break;
  137. case 'd':
  138. printf ("option d with value `%s'\n", optarg);
  139. break;
  140. case '?':
  141. break;
  142. default:
  143. printf ("?? getopt returned character code 0%o ??\n", c);
  144. }
  145. }
  146. if (optind < argc)
  147. {
  148. printf ("non-option ARGV-elements: ");
  149. while (optind < argc)
  150. printf ("%s ", argv[optind++]);
  151. printf ("\n");
  152. }
  153. exit (0);
  154. }
  155. #endif /* TEST */