getopt.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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@prep.ai.mit.edu.
  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. #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. #ifdef __GNU_LIBRARY__
  78. /* Many other libraries have conflicting prototypes for getopt, with
  79. differences in the consts, in stdlib.h. To avoid compilation
  80. errors, only prototype getopt for the GNU C library. */
  81. extern int getopt (int argc, char *const *argv, const char *shortopts);
  82. #else /* not __GNU_LIBRARY__ */
  83. extern int getopt ();
  84. #endif /* __GNU_LIBRARY__ */
  85. extern int getopt_long (int argc, char *const *argv, const char *shortopts,
  86. const struct option *longopts, int *longind);
  87. extern int getopt_long_only (int argc, char *const *argv,
  88. const char *shortopts,
  89. const struct option *longopts, int *longind);
  90. /* Internal only. Users should not call this directly. */
  91. extern int _getopt_internal (int argc, char *const *argv,
  92. const char *shortopts,
  93. const struct option *longopts, int *longind,
  94. int long_only);
  95. #else /* not __STDC__ */
  96. extern int getopt ();
  97. extern int getopt_long ();
  98. extern int getopt_long_only ();
  99. extern int _getopt_internal ();
  100. #endif /* __STDC__ */
  101. #ifdef __cplusplus
  102. }
  103. #endif
  104. #endif /* _GETOPT_H */