getopt.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* Declarations for getopt.
  2. Copyright (C) 1989, 1990, 1991, 1992, 1993 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. /* Set to an option character which was unrecognized. */
  38. extern int optopt;
  39. /* Describe the long-named options requested by the application.
  40. The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
  41. of `struct option' terminated by an element containing a name which is
  42. zero.
  43. The field `has_arg' is:
  44. no_argument (or 0) if the option does not take an argument,
  45. required_argument (or 1) if the option requires an argument,
  46. optional_argument (or 2) if the option takes an optional argument.
  47. If the field `flag' is not NULL, it points to a variable that is set
  48. to the value given in the field `val' when the option is found, but
  49. left unchanged if the option is not found.
  50. To have a long-named option do something other than set an `int' to
  51. a compiled-in constant, such as set a value from `optarg', set the
  52. option's `flag' field to zero and its `val' field to a nonzero
  53. value (the equivalent single-letter option character, if there is
  54. one). For long options that have a zero `flag' field, `getopt'
  55. returns the contents of the `val' field. */
  56. struct option
  57. {
  58. #if __STDC__
  59. const char *name;
  60. #else
  61. char *name;
  62. #endif
  63. /* has_arg can't be an enum because some compilers complain about
  64. type mismatches in all the code that assumes it is an int. */
  65. int has_arg;
  66. int *flag;
  67. int val;
  68. };
  69. /* Names for the values of the `has_arg' field of `struct option'. */
  70. #define no_argument 0
  71. #define required_argument 1
  72. #define optional_argument 2
  73. #if __STDC__
  74. #if defined(__GNU_LIBRARY__)
  75. /* Many other libraries have conflicting prototypes for getopt, with
  76. differences in the consts, in stdlib.h. To avoid compilation
  77. errors, only prototype getopt for the GNU C library. */
  78. extern int getopt (int argc, char *const *argv, const char *shortopts);
  79. #else /* not __GNU_LIBRARY__ */
  80. extern int getopt ();
  81. #endif /* not __GNU_LIBRARY__ */
  82. extern int getopt_long (int argc, char *const *argv, const char *shortopts,
  83. const struct option *longopts, int *longind);
  84. extern int getopt_long_only (int argc, char *const *argv,
  85. const char *shortopts,
  86. const struct option *longopts, int *longind);
  87. /* Internal only. Users should not call this directly. */
  88. extern int _getopt_internal (int argc, char *const *argv,
  89. const char *shortopts,
  90. const struct option *longopts, int *longind,
  91. int long_only);
  92. #else /* not __STDC__ */
  93. extern int getopt ();
  94. extern int getopt_long ();
  95. extern int getopt_long_only ();
  96. extern int _getopt_internal ();
  97. #endif /* not __STDC__ */
  98. #ifdef __cplusplus
  99. }
  100. #endif
  101. #endif /* _GETOPT_H */