getopt1.c 4.4 KB

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