getopt.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. /* $CVSid: @(#)getopt.h 1.7 94/09/21 $ */
  15. #ifndef _GETOPT_H
  16. #define _GETOPT_H 1
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /* For communication from `getopt' to the caller.
  21. When `getopt' finds an option that takes an argument,
  22. the argument value is returned here.
  23. Also, when `ordering' is RETURN_IN_ORDER,
  24. each non-option ARGV-element is returned here. */
  25. extern char *optarg;
  26. /* Index in ARGV of the next element to be scanned.
  27. This is used for communication to and from the caller
  28. and for communication between successive calls to `getopt'.
  29. On entry to `getopt', zero means this is the first call; initialize.
  30. When `getopt' returns EOF, this is the index of the first of the
  31. non-option elements that the caller should itself scan.
  32. Otherwise, `optind' communicates from one call to the next
  33. how much of ARGV has been scanned so far. */
  34. extern int optind;
  35. /* Callers store zero here to inhibit the error message `getopt' prints
  36. for unrecognized options. */
  37. extern int opterr;
  38. /* Set to an option character which was unrecognized. */
  39. extern int optopt;
  40. /* Describe the long-named options requested by the application.
  41. The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
  42. of `struct option' terminated by an element containing a name which is
  43. zero.
  44. The field `has_arg' is:
  45. no_argument (or 0) if the option does not take an argument,
  46. required_argument (or 1) if the option requires an argument,
  47. optional_argument (or 2) if the option takes an optional argument.
  48. If the field `flag' is not NULL, it points to a variable that is set
  49. to the value given in the field `val' when the option is found, but
  50. left unchanged if the option is not found.
  51. To have a long-named option do something other than set an `int' to
  52. a compiled-in constant, such as set a value from `optarg', set the
  53. option's `flag' field to zero and its `val' field to a nonzero
  54. value (the equivalent single-letter option character, if there is
  55. one). For long options that have a zero `flag' field, `getopt'
  56. returns the contents of the `val' field. */
  57. struct option
  58. {
  59. #if __STDC__
  60. const char *name;
  61. #else
  62. char *name;
  63. #endif
  64. /* has_arg can't be an enum because some compilers complain about
  65. type mismatches in all the code that assumes it is an int. */
  66. int has_arg;
  67. int *flag;
  68. int val;
  69. };
  70. /* Names for the values of the `has_arg' field of `struct option'. */
  71. #define no_argument 0
  72. #define required_argument 1
  73. #define optional_argument 2
  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 */