getopt.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* Declarations for getopt.
  2. Copyright (C) 1989-2022 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
  16. USA. */
  17. #ifndef _GETOPT_H
  18. #define _GETOPT_H 1
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /* For communication from `getopt' to the caller.
  23. When `getopt' finds an option that takes an argument,
  24. the argument value is returned here.
  25. Also, when `ordering' is RETURN_IN_ORDER,
  26. each non-option ARGV-element is returned here. */
  27. extern char *optarg;
  28. /* Index in ARGV of the next element to be scanned.
  29. This is used for communication to and from the caller
  30. and for communication between successive calls to `getopt'.
  31. On entry to `getopt', zero means this is the first call; initialize.
  32. When `getopt' returns -1, this is the index of the first of the
  33. non-option elements that the caller should itself scan.
  34. Otherwise, `optind' communicates from one call to the next
  35. how much of ARGV has been scanned so far. */
  36. extern int optind;
  37. /* Callers store zero here to inhibit the error message `getopt' prints
  38. for unrecognized options. */
  39. extern int opterr;
  40. /* Set to an option character which was unrecognized. */
  41. extern int optopt;
  42. /* Describe the long-named options requested by the application.
  43. The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
  44. of `struct option' terminated by an element containing a name which is
  45. zero.
  46. The field `has_arg' is:
  47. no_argument (or 0) if the option does not take an argument,
  48. required_argument (or 1) if the option requires an argument,
  49. optional_argument (or 2) if the option takes an optional argument.
  50. If the field `flag' is not NULL, it points to a variable that is set
  51. to the value given in the field `val' when the option is found, but
  52. left unchanged if the option is not found.
  53. To have a long-named option do something other than set an `int' to
  54. a compiled-in constant, such as set a value from `optarg', set the
  55. option's `flag' field to zero and its `val' field to a nonzero
  56. value (the equivalent single-letter option character, if there is
  57. one). For long options that have a zero `flag' field, `getopt'
  58. returns the contents of the `val' field. */
  59. struct option
  60. {
  61. #if defined (__STDC__) && __STDC__
  62. const char *name;
  63. #else
  64. char *name;
  65. #endif
  66. /* has_arg can't be an enum because some compilers complain about
  67. type mismatches in all the code that assumes it is an int. */
  68. int has_arg;
  69. int *flag;
  70. int val;
  71. };
  72. /* Names for the values of the `has_arg' field of `struct option'. */
  73. #define no_argument 0
  74. #define required_argument 1
  75. #define optional_argument 2
  76. #if defined (__STDC__) && __STDC__
  77. /* HAVE_DECL_* is a three-state macro: undefined, 0 or 1. If it is
  78. undefined, we haven't run the autoconf check so provide the
  79. declaration without arguments. If it is 0, we checked and failed
  80. to find the declaration so provide a fully prototyped one. If it
  81. is 1, we found it so don't provide any declaration at all. */
  82. #if !HAVE_DECL_GETOPT
  83. #if defined (__GNU_LIBRARY__) || defined (HAVE_DECL_GETOPT)
  84. /* Many other libraries have conflicting prototypes for getopt, with
  85. differences in the consts, in unistd.h. To avoid compilation
  86. errors, only prototype getopt for the GNU C library. */
  87. extern int getopt (int argc, char *const *argv, const char *shortopts);
  88. #else
  89. #ifndef __cplusplus
  90. extern int getopt ();
  91. #endif /* __cplusplus */
  92. #endif
  93. #endif /* !HAVE_DECL_GETOPT */
  94. extern int getopt_long (int argc, char *const *argv, const char *shortopts,
  95. const struct option *longopts, int *longind);
  96. extern int getopt_long_only (int argc, char *const *argv,
  97. const char *shortopts,
  98. const struct option *longopts, int *longind);
  99. /* Internal only. Users should not call this directly. */
  100. extern int _getopt_internal (int argc, char *const *argv,
  101. const char *shortopts,
  102. const struct option *longopts, int *longind,
  103. int long_only);
  104. #else /* not __STDC__ */
  105. extern int getopt ();
  106. extern int getopt_long ();
  107. extern int getopt_long_only ();
  108. extern int _getopt_internal ();
  109. #endif /* __STDC__ */
  110. #ifdef __cplusplus
  111. }
  112. #endif
  113. #endif /* getopt.h */