getopt.h 3.2 KB

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