gnu_getopt.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #ifndef _GNU_GETOPT_H
  12. #define _GNU_GETOPT_H 1
  13. /* We don't want to collide with a system getopt() it if it exists.
  14. Redefine our symbols accordingly. */
  15. #define getopt gnu_getopt
  16. #define optind gnu_optind
  17. #define opterr gnu_opterr
  18. #define optopt gnu_optopt
  19. #define optarg gnu_optarg
  20. #define getopt_long gnu_getopt_long
  21. #define getopt_long_only gnu_getopt_long_only
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /* For communication from `getopt' to the caller.
  26. When `getopt' finds an option that takes an argument,
  27. the argument value is returned here.
  28. Also, when `ordering' is RETURN_IN_ORDER,
  29. each non-option ARGV-element is returned here. */
  30. extern char *optarg;
  31. /* Index in ARGV of the next element to be scanned.
  32. This is used for communication to and from the caller
  33. and for communication between successive calls to `getopt'.
  34. On entry to `getopt', zero means this is the first call; initialize.
  35. When `getopt' returns EOF, this is the index of the first of the
  36. non-option elements that the caller should itself scan.
  37. Otherwise, `optind' communicates from one call to the next
  38. how much of ARGV has been scanned so far. */
  39. extern int optind;
  40. /* Callers store zero here to inhibit the error message `getopt' prints
  41. for unrecognized options. */
  42. extern int opterr;
  43. /* Set to an option character which was unrecognized. */
  44. extern int optopt;
  45. /* Describe the long-named options requested by the application.
  46. The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
  47. of `struct option' terminated by an element containing a name which is
  48. zero.
  49. The field `has_arg' is:
  50. no_argument (or 0) if the option does not take an argument,
  51. required_argument (or 1) if the option requires an argument,
  52. optional_argument (or 2) if the option takes an optional argument.
  53. If the field `flag' is not NULL, it points to a variable that is set
  54. to the value given in the field `val' when the option is found, but
  55. left unchanged if the option is not found.
  56. To have a long-named option do something other than set an `int' to
  57. a compiled-in constant, such as set a value from `optarg', set the
  58. option's `flag' field to zero and its `val' field to a nonzero
  59. value (the equivalent single-letter option character, if there is
  60. one). For long options that have a zero `flag' field, `getopt'
  61. returns the contents of the `val' field. */
  62. struct option
  63. {
  64. #if __STDC__
  65. const char *name;
  66. #else
  67. char *name;
  68. #endif
  69. /* has_arg can't be an enum because some compilers complain about
  70. type mismatches in all the code that assumes it is an int. */
  71. int has_arg;
  72. int *flag;
  73. int val;
  74. };
  75. /* Names for the values of the `has_arg' field of `struct option'. */
  76. #define no_argument 0
  77. #define required_argument 1
  78. #define optional_argument 2
  79. extern int
  80. getopt (int argc, char *const *argv, const char *shortopts);
  81. extern int
  82. getopt_long (int argc, char *const *argv, const char *shortopts,
  83. const struct option *long_options, int *opt_index);
  84. extern int
  85. getopt_long_only (int argc, char *const *argv,
  86. const char *shortopts,
  87. const struct option *long_options,
  88. int *opt_index);
  89. /* Internal only. Users should not call this directly. */
  90. extern int _getopt_internal (int argc, char *const *argv,
  91. const char *shortopts,
  92. const struct option *longopts, int *longind,
  93. int long_only);
  94. #ifdef __cplusplus
  95. }
  96. #endif
  97. #endif /* _GETOPT_H */