getopt.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* Declarations for getopt.
  2. Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the
  5. Free Software Foundation; either version 2, or (at your option) any
  6. later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  14. #ifndef _GETOPT_H
  15. #define _GETOPT_H 1
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /* For communication from `getopt' to the caller.
  20. When `getopt' finds an option that takes an argument,
  21. the argument value is returned here.
  22. Also, when `ordering' is RETURN_IN_ORDER,
  23. each non-option ARGV-element is returned here. */
  24. extern char *optarg;
  25. /* Index in ARGV of the next element to be scanned.
  26. This is used for communication to and from the caller
  27. and for communication between successive calls to `getopt'.
  28. On entry to `getopt', zero means this is the first call; initialize.
  29. When `getopt' returns EOF, this is the index of the first of the
  30. non-option elements that the caller should itself scan.
  31. Otherwise, `optind' communicates from one call to the next
  32. how much of ARGV has been scanned so far. */
  33. extern int optind;
  34. /* Callers store zero here to inhibit the error message `getopt' prints
  35. for unrecognized options. */
  36. extern int opterr;
  37. /* Describe the long-named options requested by the application.
  38. The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
  39. of `struct option' terminated by an element containing a name which is
  40. zero.
  41. The field `has_arg' is:
  42. no_argument (or 0) if the option does not take an argument,
  43. required_argument (or 1) if the option requires an argument,
  44. optional_argument (or 2) if the option takes an optional argument.
  45. If the field `flag' is not NULL, it points to a variable that is set
  46. to the value given in the field `val' when the option is found, but
  47. left unchanged if the option is not found.
  48. To have a long-named option do something other than set an `int' to
  49. a compiled-in constant, such as set a value from `optarg', set the
  50. option's `flag' field to zero and its `val' field to a nonzero
  51. value (the equivalent single-letter option character, if there is
  52. one). For long options that have a zero `flag' field, `getopt'
  53. returns the contents of the `val' field. */
  54. struct option
  55. {
  56. #if __STDC__
  57. const char *name;
  58. #else
  59. char *name;
  60. #endif
  61. /* has_arg can't be an enum because some compilers complain about
  62. type mismatches in all the code that assumes it is an int. */
  63. int has_arg;
  64. int *flag;
  65. int val;
  66. };
  67. /* Names for the values of the `has_arg' field of `struct option'. */
  68. enum _argtype
  69. {
  70. no_argument,
  71. required_argument,
  72. optional_argument
  73. };
  74. #if __STDC__
  75. #if defined(__GNU_LIBRARY__)
  76. /* Many other libraries have conflicting prototypes for getopt, with
  77. differences in the consts, in stdlib.h. To avoid compilation
  78. errors, only prototype getopt for the GNU C library. */
  79. extern int getopt (int argc, char *const *argv, const char *shortopts);
  80. #else /* not __GNU_LIBRARY__ */
  81. extern int getopt ();
  82. #endif /* not __GNU_LIBRARY__ */
  83. extern int getopt_long (int argc, char *const *argv, const char *shortopts,
  84. const struct option *longopts, int *longind);
  85. extern int getopt_long_only (int argc, char *const *argv,
  86. const char *shortopts,
  87. const struct option *longopts, int *longind);
  88. /* Internal only. Users should not call this directly. */
  89. extern int _getopt_internal (int argc, char *const *argv,
  90. const char *shortopts,
  91. const struct option *longopts, int *longind,
  92. int long_only);
  93. #else /* not __STDC__ */
  94. extern int getopt ();
  95. extern int getopt_long ();
  96. extern int getopt_long_only ();
  97. extern int _getopt_internal ();
  98. #endif /* not __STDC__ */
  99. #ifdef __cplusplus
  100. }
  101. #endif
  102. #endif /* _GETOPT_H */