getopt.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /****************************************************************************
  2. getopt.h - Read command line options
  3. AUTHOR: Gregory Pietsch
  4. CREATED Thu Jan 09 22:37:00 1997
  5. DESCRIPTION:
  6. The getopt() function parses the command line arguments. Its arguments argc
  7. and argv are the argument count and array as passed to the main() function
  8. on program invocation. The argument optstring is a list of available option
  9. characters. If such a character is followed by a colon (`:'), the option
  10. takes an argument, which is placed in optarg. If such a character is
  11. followed by two colons, the option takes an optional argument, which is
  12. placed in optarg. If the option does not take an argument, optarg is NULL.
  13. The external variable optind is the index of the next array element of argv
  14. to be processed; it communicates from one call to the next which element to
  15. process.
  16. The getopt_long() function works like getopt() except that it also accepts
  17. long options started by two dashes `--'. If these take values, it is either
  18. in the form
  19. --arg=value
  20. or
  21. --arg value
  22. It takes the additional arguments longopts which is a pointer to the first
  23. element of an array of type GETOPT_LONG_OPTION_T, defined below. The last
  24. element of the array has to be filled with NULL for the name field.
  25. The longind pointer points to the index of the current long option relative
  26. to longopts if it is non-NULL.
  27. The getopt() function returns the option character if the option was found
  28. successfully, `:' if there was a missing parameter for one of the options,
  29. `?' for an unknown option character, and EOF for the end of the option list.
  30. The getopt_long() function's return value is described below.
  31. The function getopt_long_only() is identical to getopt_long(), except that a
  32. plus sign `+' can introduce long options as well as `--'.
  33. Describe how to deal with options that follow non-option ARGV-elements.
  34. If the caller did not specify anything, the default is REQUIRE_ORDER if the
  35. environment variable POSIXLY_CORRECT is defined, PERMUTE otherwise.
  36. REQUIRE_ORDER means don't recognize them as options; stop option processing
  37. when the first non-option is seen. This is what Unix does. This mode of
  38. operation is selected by either setting the environment variable
  39. POSIXLY_CORRECT, or using `+' as the first character of the optstring
  40. parameter.
  41. PERMUTE is the default. We permute the contents of ARGV as we scan, so that
  42. eventually all the non-options are at the end. This allows options to be
  43. given in any order, even with programs that were not written to expect this.
  44. RETURN_IN_ORDER is an option available to programs that were written to
  45. expect options and other ARGV-elements in any order and that care about the
  46. ordering of the two. We describe each non-option ARGV-element as if it were
  47. the argument of an option with character code 1. Using `-' as the first
  48. character of the optstring parameter selects this mode of operation.
  49. The special argument `--' forces an end of option-scanning regardless of the
  50. value of `ordering'. In the case of RETURN_IN_ORDER, only `--' can cause
  51. getopt() and friends to return EOF with optind != argc.
  52. COPYRIGHT NOTICE AND DISCLAIMER:
  53. Copyright (C) 1997 Gregory Pietsch
  54. This file and the accompanying getopt.c implementation file are hereby
  55. placed in the public domain without restrictions. Just give the author
  56. credit, don't claim you wrote it or prevent anyone else from using it.
  57. Gregory Pietsch's current e-mail address:
  58. gpietsch@comcast.net
  59. ****************************************************************************/
  60. /* This is a glibc-extension header file. */
  61. #ifndef GETOPT_H
  62. #define GETOPT_H
  63. #include <_ansi.h>
  64. /* include files needed by this include file */
  65. #define no_argument 0
  66. #define required_argument 1
  67. #define optional_argument 2
  68. #ifdef __cplusplus
  69. extern "C"
  70. {
  71. #endif /* __cplusplus */
  72. /* types defined by this include file */
  73. struct option
  74. {
  75. const char *name; /* the name of the long option */
  76. int has_arg; /* one of the above macros */
  77. int *flag; /* determines if getopt_long() returns a
  78. * value for a long option; if it is
  79. * non-NULL, 0 is returned as a function
  80. * value and the value of val is stored in
  81. * the area pointed to by flag. Otherwise,
  82. * val is returned. */
  83. int val; /* determines the value to return if flag is
  84. * NULL. */
  85. };
  86. /* While getopt.h is a glibc extension, the following are newlib extensions.
  87. * They are optionally included via the __need_getopt_newlib flag. */
  88. #ifdef __need_getopt_newlib
  89. /* macros defined by this include file */
  90. #define NO_ARG no_argument
  91. #define REQUIRED_ARG required_argument
  92. #define OPTIONAL_ARG optional_argument
  93. /* The GETOPT_DATA_INITIALIZER macro is used to initialize a statically-
  94. allocated variable of type struct getopt_data. */
  95. #define GETOPT_DATA_INITIALIZER {0,0,0,0,0,0,0}
  96. /* These #defines are to make accessing the reentrant functions easier. */
  97. #define getopt_r __getopt_r
  98. #define getopt_long_r __getopt_long_r
  99. #define getopt_long_only_r __getopt_long_only_r
  100. /* The getopt_data structure is for reentrancy. Its members are similar to
  101. the externally-defined variables. */
  102. typedef struct getopt_data
  103. {
  104. char *optarg;
  105. int optind, opterr, optopt, optwhere;
  106. int permute_from, num_nonopts;
  107. } getopt_data;
  108. #endif /* __need_getopt_newlib */
  109. /* externally-defined variables */
  110. extern char *optarg;
  111. extern int optind;
  112. extern int opterr;
  113. extern int optopt;
  114. /* function prototypes */
  115. int getopt (int __argc, char *const __argv[], const char *__optstring);
  116. int getopt_long (int __argc, char *const __argv[], const char *__shortopts,
  117. const struct option * __longopts, int *__longind);
  118. int getopt_long_only (int __argc, char *const __argv[], const char *__shortopts,
  119. const struct option * __longopts, int *__longind);
  120. #ifdef __need_getopt_newlib
  121. int __getopt_r (int __argc, char *const __argv[], const char *__optstring,
  122. struct getopt_data * __data);
  123. int __getopt_long_r (int __argc, char *const __argv[], const char *__shortopts,
  124. const struct option * __longopts, int *__longind,
  125. struct getopt_data * __data);
  126. int __getopt_long_only_r (int __argc, char *const __argv[], const char *__shortopts,
  127. const struct option * __longopts, int *__longind,
  128. struct getopt_data * __data);
  129. #endif /* __need_getopt_newlib */
  130. #ifdef __cplusplus
  131. };
  132. #endif /* __cplusplus */
  133. #endif /* GETOPT_H */
  134. /* END OF FILE getopt.h */