/contrib/cvs/lib/getopt.h

https://bitbucket.org/freebsd/freebsd-head/ · C++ Header · 149 lines · 53 code · 21 blank · 75 comment · 1 complexity · ad00289ee4a66de8361f70c2a2c028c2 MD5 · raw file

  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 _GETOPT_H
  12. #define _GETOPT_H 1
  13. /* CVS - DRP
  14. *
  15. * If the OS defines this, just redefine the names to avoid namespace
  16. * clashes. In theory, we should be testing the built in functions to
  17. * see if they do what we want and use them if possible, but this is
  18. * easier...
  19. *
  20. * Namely, this was occurring under Mac OS X. This is a Mac OS X (or
  21. * OS X related) bug.
  22. *
  23. * Oops. We avoid compiling this with ifdefs because pretty much all of
  24. * getopt.c is switched on the same macros... this isn't right, but I think
  25. * this isn't our file. Probably best not to mess with it too much.
  26. */
  27. #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
  28. # ifdef HAVE_GETOPT
  29. # define getopt cvs_getopt
  30. # define optarg cvs_optarg
  31. # define opterr cvs_opterr
  32. # define optind cvs_optind
  33. # define optopt cvs_optopt
  34. # endif /* HAVE_GETOPT */
  35. #endif /* _LIBC or not __GNU_LIBRARY__. */
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. /* For communication from `getopt' to the caller.
  40. When `getopt' finds an option that takes an argument,
  41. the argument value is returned here.
  42. Also, when `ordering' is RETURN_IN_ORDER,
  43. each non-option ARGV-element is returned here. */
  44. extern char *optarg;
  45. /* Index in ARGV of the next element to be scanned.
  46. This is used for communication to and from the caller
  47. and for communication between successive calls to `getopt'.
  48. On entry to `getopt', zero means this is the first call; initialize.
  49. When `getopt' returns EOF, this is the index of the first of the
  50. non-option elements that the caller should itself scan.
  51. Otherwise, `optind' communicates from one call to the next
  52. how much of ARGV has been scanned so far. */
  53. extern int optind;
  54. /* Callers store zero here to inhibit the error message `getopt' prints
  55. for unrecognized options. */
  56. extern int opterr;
  57. /* Set to an option character which was unrecognized. */
  58. extern int optopt;
  59. /* Describe the long-named options requested by the application.
  60. The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
  61. of `struct option' terminated by an element containing a name which is
  62. zero.
  63. The field `has_arg' is:
  64. no_argument (or 0) if the option does not take an argument,
  65. required_argument (or 1) if the option requires an argument,
  66. optional_argument (or 2) if the option takes an optional argument.
  67. If the field `flag' is not NULL, it points to a variable that is set
  68. to the value given in the field `val' when the option is found, but
  69. left unchanged if the option is not found.
  70. To have a long-named option do something other than set an `int' to
  71. a compiled-in constant, such as set a value from `optarg', set the
  72. option's `flag' field to zero and its `val' field to a nonzero
  73. value (the equivalent single-letter option character, if there is
  74. one). For long options that have a zero `flag' field, `getopt'
  75. returns the contents of the `val' field. */
  76. struct option
  77. {
  78. #if __STDC__
  79. const char *name;
  80. #else
  81. char *name;
  82. #endif
  83. /* has_arg can't be an enum because some compilers complain about
  84. type mismatches in all the code that assumes it is an int. */
  85. int has_arg;
  86. int *flag;
  87. int val;
  88. };
  89. /* Names for the values of the `has_arg' field of `struct option'. */
  90. #define no_argument 0
  91. #define required_argument 1
  92. #define optional_argument 2
  93. #if __STDC__
  94. /* Many other libraries have conflicting prototypes for getopt, with
  95. differences in the consts, in stdlib.h. We used to try to prototype
  96. it if __GNU_LIBRARY__ but that wasn't problem free either (I'm not sure
  97. exactly why), and there is no particular need to prototype it.
  98. We really shouldn't be trampling on the system's namespace at all by
  99. declaring getopt() but that is a bigger issue. */
  100. extern int getopt ();
  101. extern int getopt_long (int argc, char *const *argv, const char *shortopts,
  102. const struct option *longopts, int *longind);
  103. extern int getopt_long_only (int argc, char *const *argv,
  104. const char *shortopts,
  105. const struct option *longopts, int *longind);
  106. /* Internal only. Users should not call this directly. */
  107. extern int _getopt_internal (int argc, char *const *argv,
  108. const char *shortopts,
  109. const struct option *longopts, int *longind,
  110. int long_only);
  111. #else /* not __STDC__ */
  112. extern int getopt ();
  113. extern int getopt_long ();
  114. extern int getopt_long_only ();
  115. extern int _getopt_internal ();
  116. #endif /* not __STDC__ */
  117. #ifdef __cplusplus
  118. }
  119. #endif
  120. #endif /* _GETOPT_H */