fspr_getopt.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 fspr_getopt.h
  20. * @brief APR Command Arguments (getopt)
  21. */
  22. #include "fspr_pools.h"
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif /* __cplusplus */
  26. /**
  27. * @defgroup fspr_getopt Command Argument Parsing
  28. * @ingroup APR
  29. * @{
  30. */
  31. /**
  32. * defintion of a error function
  33. */
  34. typedef void (fspr_getopt_err_fn_t)(void *arg, const char *err, ...);
  35. /** @see fspr_getopt_t */
  36. typedef struct fspr_getopt_t fspr_getopt_t;
  37. /**
  38. * Structure to store command line argument information.
  39. */
  40. struct fspr_getopt_t {
  41. /** context for processing */
  42. fspr_pool_t *cont;
  43. /** function to print error message (NULL == no messages) */
  44. fspr_getopt_err_fn_t *errfn;
  45. /** user defined first arg to pass to error message */
  46. void *errarg;
  47. /** index into parent argv vector */
  48. int ind;
  49. /** character checked for validity */
  50. int opt;
  51. /** reset getopt */
  52. int reset;
  53. /** count of arguments */
  54. int argc;
  55. /** array of pointers to arguments */
  56. const char **argv;
  57. /** argument associated with option */
  58. char const* place;
  59. /** set to nonzero to support interleaving options with regular args */
  60. int interleave;
  61. /** start of non-option arguments skipped for interleaving */
  62. int skip_start;
  63. /** end of non-option arguments skipped for interleaving */
  64. int skip_end;
  65. };
  66. /** @see fspr_getopt_option_t */
  67. typedef struct fspr_getopt_option_t fspr_getopt_option_t;
  68. /**
  69. * Structure used to describe options that getopt should search for.
  70. */
  71. struct fspr_getopt_option_t {
  72. /** long option name, or NULL if option has no long name */
  73. const char *name;
  74. /** option letter, or a value greater than 255 if option has no letter */
  75. int optch;
  76. /** nonzero if option takes an argument */
  77. int has_arg;
  78. /** a description of the option */
  79. const char *description;
  80. };
  81. /**
  82. * Initialize the arguments for parsing by fspr_getopt().
  83. * @param os The options structure created for fspr_getopt()
  84. * @param cont The pool to operate on
  85. * @param argc The number of arguments to parse
  86. * @param argv The array of arguments to parse
  87. * @remark Arguments 2 and 3 are most commonly argc and argv from main(argc, argv)
  88. * The errfn is initialized to fprintf(stderr... but may be overridden.
  89. */
  90. APR_DECLARE(fspr_status_t) fspr_getopt_init(fspr_getopt_t **os, fspr_pool_t *cont,
  91. int argc, const char * const *argv);
  92. /**
  93. * Parse the options initialized by fspr_getopt_init().
  94. * @param os The fspr_opt_t structure returned by fspr_getopt_init()
  95. * @param opts A string of characters that are acceptable options to the
  96. * program. Characters followed by ":" are required to have an
  97. * option associated
  98. * @param option_ch The next option character parsed
  99. * @param option_arg The argument following the option character:
  100. * @return There are four potential status values on exit. They are:
  101. * <PRE>
  102. * APR_EOF -- No more options to parse
  103. * APR_BADCH -- Found a bad option character
  104. * APR_BADARG -- No argument followed the option flag
  105. * APR_SUCCESS -- The next option was found.
  106. * </PRE>
  107. */
  108. APR_DECLARE(fspr_status_t) fspr_getopt(fspr_getopt_t *os, const char *opts,
  109. char *option_ch, const char **option_arg);
  110. /**
  111. * Parse the options initialized by fspr_getopt_init(), accepting long
  112. * options beginning with "--" in addition to single-character
  113. * options beginning with "-".
  114. * @param os The fspr_getopt_t structure created by fspr_getopt_init()
  115. * @param opts A pointer to a list of fspr_getopt_option_t structures, which
  116. * can be initialized with { "name", optch, has_args }. has_args
  117. * is nonzero if the option requires an argument. A structure
  118. * with an optch value of 0 terminates the list.
  119. * @param option_ch Receives the value of "optch" from the fspr_getopt_option_t
  120. * structure corresponding to the next option matched.
  121. * @param option_arg Receives the argument following the option, if any.
  122. * @return There are four potential status values on exit. They are:
  123. * <PRE>
  124. * APR_EOF -- No more options to parse
  125. * APR_BADCH -- Found a bad option character
  126. * APR_BADARG -- No argument followed the option flag
  127. * APR_SUCCESS -- The next option was found.
  128. * </PRE>
  129. * When APR_SUCCESS is returned, os->ind gives the index of the first
  130. * non-option argument. On error, a message will be printed to stdout unless
  131. * os->err is set to 0. If os->interleave is set to nonzero, options can come
  132. * after arguments, and os->argv will be permuted to leave non-option arguments
  133. * at the end (the original argv is unaffected).
  134. */
  135. APR_DECLARE(fspr_status_t) fspr_getopt_long(fspr_getopt_t *os,
  136. const fspr_getopt_option_t *opts,
  137. int *option_ch,
  138. const char **option_arg);
  139. /** @} */
  140. #ifdef __cplusplus
  141. }
  142. #endif
  143. #endif /* ! APR_GETOPT_H */