getopt.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* Declarations for getopt.
  2. Copyright (C) 1989,90,91,92,93,94,96,97 Free Software Foundation, Inc.
  3. NOTE: The canonical source of this file is maintained with the GNU C Library.
  4. Bugs can be reported to bug-glibc@gnu.org.
  5. This program is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option) any
  8. later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. USA. */
  17. #ifndef _GETOPT_H
  18. #define _GETOPT_H 1
  19. /*
  20. * The operating system may or may not provide getopt_long(), and if
  21. * so it may or may not be a version we are willing to use. Our
  22. * strategy is to declare getopt here, and then provide code unless
  23. * the supplied version is adequate. The difficult case is when a
  24. * declaration for getopt is provided, as our declaration must match.
  25. *
  26. * XXX Arguably this version should be named differently, and the
  27. * local names defined to refer to the system version when we choose
  28. * to use the system version.
  29. */
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /* For communication from `getopt' to the caller.
  34. When `getopt' finds an option that takes an argument,
  35. the argument value is returned here.
  36. Also, when `ordering' is RETURN_IN_ORDER,
  37. each non-option ARGV-element is returned here. */
  38. extern char *optarg;
  39. /* Index in ARGV of the next element to be scanned.
  40. This is used for communication to and from the caller
  41. and for communication between successive calls to `getopt'.
  42. On entry to `getopt', zero means this is the first call; initialize.
  43. When `getopt' returns -1, this is the index of the first of the
  44. non-option elements that the caller should itself scan.
  45. Otherwise, `optind' communicates from one call to the next
  46. how much of ARGV has been scanned so far. */
  47. extern int optind;
  48. /* Callers store zero here to inhibit the error message `getopt' prints
  49. for unrecognized options. */
  50. extern int opterr;
  51. /* Set to an option character which was unrecognized. */
  52. extern int optopt;
  53. /* Describe the long-named options requested by the application.
  54. The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
  55. of `struct option' terminated by an element containing a name which is
  56. zero.
  57. The field `has_arg' is:
  58. no_argument (or 0) if the option does not take an argument,
  59. required_argument (or 1) if the option requires an argument,
  60. optional_argument (or 2) if the option takes an optional argument.
  61. If the field `flag' is not NULL, it points to a variable that is set
  62. to the value given in the field `val' when the option is found, but
  63. left unchanged if the option is not found.
  64. To have a long-named option do something other than set an `int' to
  65. a compiled-in constant, such as set a value from `optarg', set the
  66. option's `flag' field to zero and its `val' field to a nonzero
  67. value (the equivalent single-letter option character, if there is
  68. one). For long options that have a zero `flag' field, `getopt'
  69. returns the contents of the `val' field. */
  70. struct option
  71. {
  72. #if defined (__STDC__) && __STDC__
  73. const char *name;
  74. #else
  75. char *name;
  76. #endif
  77. /* has_arg can't be an enum because some compilers complain about
  78. type mismatches in all the code that assumes it is an int. */
  79. int has_arg;
  80. int *flag;
  81. int val;
  82. };
  83. /* Names for the values of the `has_arg' field of `struct option'. */
  84. #define no_argument 0
  85. #define required_argument 1
  86. #define optional_argument 2
  87. #if defined (__STDC__) && __STDC__
  88. #if REALLY_NEED_PLAIN_GETOPT
  89. /*
  90. * getopt is defined in POSIX.2. Assume that if the system defines
  91. * getopt that it complies with POSIX.2. If not, an autoconf test
  92. * should be written to define NONPOSIX_GETOPT_DEFINITION.
  93. */
  94. #ifndef NONPOSIX_GETOPT_DEFINITION
  95. extern int getopt (int argc, char *const *argv, const char *shortopts);
  96. #else /* NONPOSIX_GETOPT_DEFINITION */
  97. extern int getopt (void);
  98. #endif /* NONPOSIX_GETOPT_DEFINITION */
  99. #endif
  100. extern int getopt_long (int argc, char *const *argv, const char *shortopts,
  101. const struct option *longopts, int *longind);
  102. extern int getopt_long_only (int argc, char *const *argv,
  103. const char *shortopts,
  104. const struct option *longopts, int *longind);
  105. /* Internal only. Users should not call this directly. */
  106. extern int _getopt_internal (int argc, char *const *argv,
  107. const char *shortopts,
  108. const struct option *longopts, int *longind,
  109. int long_only);
  110. #else /* not __STDC__ */
  111. #ifdef REALLY_NEED_PLAIN_GETOPT
  112. extern int getopt ();
  113. #endif /* REALLY_NEED_PLAIN_GETOPT */
  114. extern int getopt_long ();
  115. extern int getopt_long_only ();
  116. extern int _getopt_internal ();
  117. #endif /* __STDC__ */
  118. #ifdef __cplusplus
  119. }
  120. #endif
  121. #endif /* getopt.h */