2
0

getopt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (c) 1987, 1993, 1994
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by the University of
  16. * California, Berkeley and its contributors.
  17. * 4. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include "apr_arch_misc.h"
  34. #include "apr_strings.h"
  35. #include "apr_lib.h"
  36. #define EMSG ""
  37. APR_DECLARE(apr_status_t) apr_getopt_init(apr_getopt_t **os, apr_pool_t *cont,
  38. int argc, const char *const *argv)
  39. {
  40. void *argv_buff;
  41. *os = apr_palloc(cont, sizeof(apr_getopt_t));
  42. (*os)->cont = cont;
  43. (*os)->reset = 0;
  44. (*os)->errfn = (apr_getopt_err_fn_t*)(fprintf);
  45. (*os)->errarg = (void*)(stderr);
  46. (*os)->place = EMSG;
  47. (*os)->argc = argc;
  48. /* The argv parameter must be compatible with main()'s argv, since
  49. that's the primary purpose of this function. But people might
  50. want to use this function with arrays other than the main argv,
  51. and we shouldn't touch the caller's data. So we copy. */
  52. argv_buff = apr_palloc(cont, (argc + 1) * sizeof(const char *));
  53. memcpy(argv_buff, argv, argc * sizeof(const char *));
  54. (*os)->argv = argv_buff;
  55. (*os)->argv[argc] = NULL;
  56. (*os)->interleave = 0;
  57. (*os)->ind = 1;
  58. (*os)->skip_start = 1;
  59. (*os)->skip_end = 1;
  60. return APR_SUCCESS;
  61. }
  62. APR_DECLARE(apr_status_t) apr_getopt(apr_getopt_t *os, const char *opts,
  63. char *optch, const char **optarg)
  64. {
  65. const char *oli; /* option letter list index */
  66. if (os->reset || !*os->place) { /* update scanning pointer */
  67. os->reset = 0;
  68. if (os->ind >= os->argc || *(os->place = os->argv[os->ind]) != '-') {
  69. os->place = EMSG;
  70. *optch = os->opt;
  71. return (APR_EOF);
  72. }
  73. if (os->place[1] && *++os->place == '-') { /* found "--" */
  74. ++os->ind;
  75. os->place = EMSG;
  76. *optch = os->opt;
  77. return (APR_EOF);
  78. }
  79. } /* option letter okay? */
  80. if ((os->opt = (int) *os->place++) == (int) ':' ||
  81. !(oli = strchr(opts, os->opt))) {
  82. /*
  83. * if the user didn't specify '-' as an option,
  84. * assume it means -1.
  85. */
  86. if (os->opt == (int) '-') {
  87. *optch = os->opt;
  88. return (APR_EOF);
  89. }
  90. if (!*os->place)
  91. ++os->ind;
  92. if (os->errfn && *opts != ':') {
  93. (os->errfn)(os->errarg, "%s: illegal option -- %c\n",
  94. apr_filepath_name_get(*os->argv), os->opt);
  95. }
  96. *optch = os->opt;
  97. return (APR_BADCH);
  98. }
  99. if (*++oli != ':') { /* don't need argument */
  100. *optarg = NULL;
  101. if (!*os->place)
  102. ++os->ind;
  103. }
  104. else { /* need an argument */
  105. if (*os->place) /* no white space */
  106. *optarg = os->place;
  107. else if (os->argc <= ++os->ind) { /* no arg */
  108. os->place = EMSG;
  109. if (*opts == ':') {
  110. *optch = os->opt;
  111. return (APR_BADARG);
  112. }
  113. if (os->errfn) {
  114. (os->errfn)(os->errarg,
  115. "%s: option requires an argument -- %c\n",
  116. apr_filepath_name_get(*os->argv), os->opt);
  117. }
  118. *optch = os->opt;
  119. return (APR_BADCH);
  120. }
  121. else /* white space */
  122. *optarg = os->argv[os->ind];
  123. os->place = EMSG;
  124. ++os->ind;
  125. }
  126. *optch = os->opt;
  127. return APR_SUCCESS;
  128. }
  129. /* Reverse the sequence argv[start..start+len-1]. */
  130. static void reverse(const char **argv, int start, int len)
  131. {
  132. const char *temp;
  133. for (; len >= 2; start++, len -= 2) {
  134. temp = argv[start];
  135. argv[start] = argv[start + len - 1];
  136. argv[start + len - 1] = temp;
  137. }
  138. }
  139. /*
  140. * Permute os->argv with the goal that non-option arguments will all
  141. * appear at the end. os->skip_start is where we started skipping
  142. * non-option arguments, os->skip_end is where we stopped, and os->ind
  143. * is where we are now.
  144. */
  145. static void permute(apr_getopt_t *os)
  146. {
  147. int len1 = os->skip_end - os->skip_start;
  148. int len2 = os->ind - os->skip_end;
  149. if (os->interleave) {
  150. /*
  151. * Exchange the sequences argv[os->skip_start..os->skip_end-1] and
  152. * argv[os->skip_end..os->ind-1]. The easiest way to do that is
  153. * to reverse the entire range and then reverse the two
  154. * sub-ranges.
  155. */
  156. reverse(os->argv, os->skip_start, len1 + len2);
  157. reverse(os->argv, os->skip_start, len2);
  158. reverse(os->argv, os->skip_start + len2, len1);
  159. }
  160. /* Reset skip range to the new location of the non-option sequence. */
  161. os->skip_start += len2;
  162. os->skip_end += len2;
  163. }
  164. /* Helper function to print out an error involving a long option */
  165. static apr_status_t serr(apr_getopt_t *os, const char *err, const char *str,
  166. apr_status_t status)
  167. {
  168. if (os->errfn)
  169. (os->errfn)(os->errarg, "%s: %s: %s\n",
  170. apr_filepath_name_get(*os->argv), err, str);
  171. return status;
  172. }
  173. /* Helper function to print out an error involving a short option */
  174. static apr_status_t cerr(apr_getopt_t *os, const char *err, int ch,
  175. apr_status_t status)
  176. {
  177. if (os->errfn)
  178. (os->errfn)(os->errarg, "%s: %s: %c\n",
  179. apr_filepath_name_get(*os->argv), err, ch);
  180. return status;
  181. }
  182. APR_DECLARE(apr_status_t) apr_getopt_long(apr_getopt_t *os,
  183. const apr_getopt_option_t *opts,
  184. int *optch, const char **optarg)
  185. {
  186. const char *p;
  187. int i;
  188. /* Let the calling program reset option processing. */
  189. if (os->reset) {
  190. os->place = EMSG;
  191. os->ind = 1;
  192. os->reset = 0;
  193. }
  194. /*
  195. * We can be in one of two states: in the middle of processing a
  196. * run of short options, or about to process a new argument.
  197. * Since the second case can lead to the first one, handle that
  198. * one first. */
  199. p = os->place;
  200. if (*p == '\0') {
  201. /* If we are interleaving, skip non-option arguments. */
  202. if (os->interleave) {
  203. while (os->ind < os->argc && *os->argv[os->ind] != '-')
  204. os->ind++;
  205. os->skip_end = os->ind;
  206. }
  207. if (os->ind >= os->argc || *os->argv[os->ind] != '-') {
  208. os->ind = os->skip_start;
  209. return APR_EOF;
  210. }
  211. p = os->argv[os->ind++] + 1;
  212. if (*p == '-' && p[1] != '\0') { /* Long option */
  213. /* Search for the long option name in the caller's table. */
  214. apr_size_t len = 0;
  215. p++;
  216. for (i = 0; ; i++) {
  217. if (opts[i].optch == 0) /* No match */
  218. return serr(os, "invalid option", p - 2, APR_BADCH);
  219. if (opts[i].name) {
  220. len = strlen(opts[i].name);
  221. if (strncmp(p, opts[i].name, len) == 0
  222. && (p[len] == '\0' || p[len] == '='))
  223. break;
  224. }
  225. }
  226. *optch = opts[i].optch;
  227. if (opts[i].has_arg) {
  228. if (p[len] == '=') /* Argument inline */
  229. *optarg = p + len + 1;
  230. else {
  231. if (os->ind >= os->argc) /* Argument missing */
  232. return serr(os, "missing argument", p - 2, APR_BADARG);
  233. else /* Argument in next arg */
  234. *optarg = os->argv[os->ind++];
  235. }
  236. } else {
  237. *optarg = NULL;
  238. if (p[len] == '=')
  239. return serr(os, "erroneous argument", p - 2, APR_BADARG);
  240. }
  241. permute(os);
  242. return APR_SUCCESS;
  243. } else {
  244. if (*p == '-') { /* Bare "--"; we're done */
  245. permute(os);
  246. os->ind = os->skip_start;
  247. return APR_EOF;
  248. }
  249. else
  250. if (*p == '\0') /* Bare "-" is illegal */
  251. return serr(os, "invalid option", p, APR_BADCH);
  252. }
  253. }
  254. /*
  255. * Now we're in a run of short options, and *p is the next one.
  256. * Look for it in the caller's table.
  257. */
  258. for (i = 0; ; i++) {
  259. if (opts[i].optch == 0) /* No match */
  260. return cerr(os, "invalid option character", *p, APR_BADCH);
  261. if (*p == opts[i].optch)
  262. break;
  263. }
  264. *optch = *p++;
  265. if (opts[i].has_arg) {
  266. if (*p != '\0') /* Argument inline */
  267. *optarg = p;
  268. else {
  269. if (os->ind >= os->argc) /* Argument missing */
  270. return cerr(os, "missing argument", *optch, APR_BADARG);
  271. else /* Argument in next arg */
  272. *optarg = os->argv[os->ind++];
  273. }
  274. os->place = EMSG;
  275. } else {
  276. *optarg = NULL;
  277. os->place = p;
  278. }
  279. permute(os);
  280. return APR_SUCCESS;
  281. }