apr_getopt.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef APR_GETOPT_H
  17. #define APR_GETOPT_H
  18. /**
  19. * @file apr_getopt.h
  20. * @brief APR Command Arguments (getopt)
  21. */
  22. #include "apr_pools.h"
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif /* __cplusplus */
  26. /**
  27. * @defgroup apr_getopt Command Argument Parsing
  28. * @ingroup APR
  29. * @{
  30. */
  31. /**
  32. * An @c apr_getopt_t error callback function.
  33. *
  34. * @a arg is this @c apr_getopt_t's @c errarg member.
  35. */
  36. typedef void (apr_getopt_err_fn_t)(void *arg, const char *err, ...);
  37. /** @see apr_getopt_t */
  38. typedef struct apr_getopt_t apr_getopt_t;
  39. /**
  40. * Structure to store command line argument information.
  41. */
  42. struct apr_getopt_t {
  43. /** context for processing */
  44. apr_pool_t *cont;
  45. /** function to print error message (NULL == no messages) */
  46. apr_getopt_err_fn_t *errfn;
  47. /** user defined first arg to pass to error message */
  48. void *errarg;
  49. /** index into parent argv vector */
  50. int ind;
  51. /** character checked for validity */
  52. int opt;
  53. /** reset getopt */
  54. int reset;
  55. /** count of arguments */
  56. int argc;
  57. /** array of pointers to arguments */
  58. const char **argv;
  59. /** argument associated with option */
  60. char const* place;
  61. /** set to nonzero to support interleaving options with regular args */
  62. int interleave;
  63. /** start of non-option arguments skipped for interleaving */
  64. int skip_start;
  65. /** end of non-option arguments skipped for interleaving */
  66. int skip_end;
  67. };
  68. /** @see apr_getopt_option_t */
  69. typedef struct apr_getopt_option_t apr_getopt_option_t;
  70. /**
  71. * Structure used to describe options that getopt should search for.
  72. */
  73. struct apr_getopt_option_t {
  74. /** long option name, or NULL if option has no long name */
  75. const char *name;
  76. /** option letter, or a value greater than 255 if option has no letter */
  77. int optch;
  78. /** nonzero if option takes an argument */
  79. int has_arg;
  80. /** a description of the option */
  81. const char *description;
  82. };
  83. /**
  84. * Initialize the arguments for parsing by apr_getopt().
  85. * @param os The options structure created for apr_getopt()
  86. * @param cont The pool to operate on
  87. * @param argc The number of arguments to parse
  88. * @param argv The array of arguments to parse
  89. * @remark Arguments 3 and 4 are most commonly argc and argv from main(argc, argv)
  90. * The (*os)->errfn is initialized to fprintf(stderr... but may be overridden.
  91. */
  92. APR_DECLARE(apr_status_t) apr_getopt_init(apr_getopt_t **os, apr_pool_t *cont,
  93. int argc, const char * const *argv);
  94. /**
  95. * Parse the options initialized by apr_getopt_init().
  96. * @param os The apr_opt_t structure returned by apr_getopt_init()
  97. * @param opts A string of characters that are acceptable options to the
  98. * program. Characters followed by ":" are required to have an
  99. * option associated
  100. * @param option_ch The next option character parsed
  101. * @param option_arg The argument following the option character:
  102. * @return There are four potential status values on exit. They are:
  103. * <PRE>
  104. * APR_EOF -- No more options to parse
  105. * APR_BADCH -- Found a bad option character
  106. * APR_BADARG -- No argument followed the option flag
  107. * APR_SUCCESS -- The next option was found.
  108. * </PRE>
  109. */
  110. APR_DECLARE(apr_status_t) apr_getopt(apr_getopt_t *os, const char *opts,
  111. char *option_ch, const char **option_arg);
  112. /**
  113. * Parse the options initialized by apr_getopt_init(), accepting long
  114. * options beginning with "--" in addition to single-character
  115. * options beginning with "-".
  116. * @param os The apr_getopt_t structure created by apr_getopt_init()
  117. * @param opts A pointer to a list of apr_getopt_option_t structures, which
  118. * can be initialized with { "name", optch, has_args }. has_args
  119. * is nonzero if the option requires an argument. A structure
  120. * with an optch value of 0 terminates the list.
  121. * @param option_ch Receives the value of "optch" from the apr_getopt_option_t
  122. * structure corresponding to the next option matched.
  123. * @param option_arg Receives the argument following the option, if any.
  124. * @return There are four potential status values on exit. They are:
  125. * <PRE>
  126. * APR_EOF -- No more options to parse
  127. * APR_BADCH -- Found a bad option character
  128. * APR_BADARG -- No argument followed the option flag
  129. * APR_SUCCESS -- The next option was found.
  130. * </PRE>
  131. * When APR_SUCCESS is returned, os->ind gives the index of the first
  132. * non-option argument. On error, a message will be printed to stdout unless
  133. * os->err is set to 0. If os->interleave is set to nonzero, options can come
  134. * after arguments, and os->argv will be permuted to leave non-option arguments
  135. * at the end (the original argv is unaffected).
  136. */
  137. APR_DECLARE(apr_status_t) apr_getopt_long(apr_getopt_t *os,
  138. const apr_getopt_option_t *opts,
  139. int *option_ch,
  140. const char **option_arg);
  141. /** @} */
  142. #ifdef __cplusplus
  143. }
  144. #endif
  145. #endif /* ! APR_GETOPT_H */