getopt1.c 4.5 KB

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