eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

getopt.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /* $NetBSD: getopt_long.c,v 1.27 2015/09/01 19:39:57 kamil Exp $ */
  2. /*-
  3. * Copyright (c) 2000 The NetBSD Foundation, Inc.
  4. * All rights reserved.
  5. *
  6. * This code is derived from software contributed to The NetBSD Foundation
  7. * by Dieter Baron and Thomas Klausner.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  20. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  21. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  22. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "config.h"
  31. #include <assert.h>
  32. #include <errno.h>
  33. #include <getopt.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #define REPLACE_GETOPT
  38. #define _DIAGASSERT(x) assert(x)
  39. #define __UNCONST(x) (char *)(x)
  40. #ifdef REPLACE_GETOPT
  41. #ifdef __weak_alias
  42. __weak_alias(getopt,_getopt)
  43. #endif
  44. int opterr = 1; /* if error message should be printed */
  45. int optind = 1; /* index into parent argv vector */
  46. int optopt = '?'; /* character checked for validity */
  47. int optreset; /* reset getopt */
  48. char *optarg; /* argument associated with option */
  49. #elif HAVE_NBTOOL_CONFIG_H && !HAVE_DECL_OPTRESET
  50. static int optreset;
  51. #endif
  52. #ifdef __weak_alias
  53. __weak_alias(getopt_long,_getopt_long)
  54. #endif
  55. #define IGNORE_FIRST (*options == '-' || *options == '+')
  56. #define PRINT_ERROR ((opterr) && ((*options != ':') \
  57. || (IGNORE_FIRST && options[1] != ':')))
  58. #define IS_POSIXLY_CORRECT (getenv("POSIXLY_CORRECT") != NULL)
  59. #define PERMUTE (!IS_POSIXLY_CORRECT && !IGNORE_FIRST)
  60. /* XXX: GNU ignores PC if *options == '-' */
  61. #define IN_ORDER (!IS_POSIXLY_CORRECT && *options == '-')
  62. /* return values */
  63. #define BADCH (int)'?'
  64. #define BADARG ((IGNORE_FIRST && options[1] == ':') \
  65. || (*options == ':') ? (int)':' : (int)'?')
  66. #define INORDER (int)1
  67. #define EMSG ""
  68. static int getopt_internal(int, char **, const char *);
  69. static int gcd(int, int);
  70. static void permute_args(int, int, int, char **);
  71. static const char *place = EMSG; /* option letter processing */
  72. /* XXX: set optreset to 1 rather than these two */
  73. static int nonopt_start = -1; /* first non option argument (for permute) */
  74. static int nonopt_end = -1; /* first option after non options (for permute) */
  75. /* Error messages */
  76. static const char recargchar[] = "option requires an argument -- %c";
  77. static const char recargstring[] = "option requires an argument -- %s";
  78. static const char ambig[] = "ambiguous option -- %.*s";
  79. static const char noarg[] = "option doesn't take an argument -- %.*s";
  80. static const char illoptchar[] = "unknown option -- %c";
  81. static const char illoptstring[] = "unknown option -- %s";
  82. /*
  83. * Compute the greatest common divisor of a and b.
  84. */
  85. static int
  86. gcd(int a, int b)
  87. {
  88. int c;
  89. c = a % b;
  90. while (c != 0) {
  91. a = b;
  92. b = c;
  93. c = a % b;
  94. }
  95. return b;
  96. }
  97. /*
  98. * Exchange the block from nonopt_start to nonopt_end with the block
  99. * from nonopt_end to opt_end (keeping the same order of arguments
  100. * in each block).
  101. */
  102. static void
  103. permute_args(int panonopt_start, int panonopt_end, int opt_end, char **nargv)
  104. {
  105. int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
  106. char *swap;
  107. _DIAGASSERT(nargv != NULL);
  108. /*
  109. * compute lengths of blocks and number and size of cycles
  110. */
  111. nnonopts = panonopt_end - panonopt_start;
  112. nopts = opt_end - panonopt_end;
  113. ncycle = gcd(nnonopts, nopts);
  114. cyclelen = (opt_end - panonopt_start) / ncycle;
  115. for (i = 0; i < ncycle; i++) {
  116. cstart = panonopt_end+i;
  117. pos = cstart;
  118. for (j = 0; j < cyclelen; j++) {
  119. if (pos >= panonopt_end)
  120. pos -= nnonopts;
  121. else
  122. pos += nopts;
  123. swap = nargv[pos];
  124. nargv[pos] = nargv[cstart];
  125. nargv[cstart] = swap;
  126. }
  127. }
  128. }
  129. /*
  130. * getopt_internal --
  131. * Parse argc/argv argument vector. Called by user level routines.
  132. * Returns -2 if -- is found (can be long option or end of options marker).
  133. */
  134. static int
  135. getopt_internal(int nargc, char **nargv, const char *options)
  136. {
  137. char *oli; /* option letter list index */
  138. int optchar;
  139. _DIAGASSERT(nargv != NULL);
  140. _DIAGASSERT(options != NULL);
  141. optarg = NULL;
  142. /*
  143. * XXX Some programs (like rsyncd) expect to be able to
  144. * XXX re-initialize optind to 0 and have getopt_long(3)
  145. * XXX properly function again. Work around this braindamage.
  146. */
  147. if (optind == 0)
  148. optind = 1;
  149. if (optreset)
  150. nonopt_start = nonopt_end = -1;
  151. start:
  152. if (optreset || !*place) { /* update scanning pointer */
  153. optreset = 0;
  154. if (optind >= nargc) { /* end of argument vector */
  155. place = EMSG;
  156. if (nonopt_end != -1) {
  157. /* do permutation, if we have to */
  158. permute_args(nonopt_start, nonopt_end,
  159. optind, nargv);
  160. optind -= nonopt_end - nonopt_start;
  161. }
  162. else if (nonopt_start != -1) {
  163. /*
  164. * If we skipped non-options, set optind
  165. * to the first of them.
  166. */
  167. optind = nonopt_start;
  168. }
  169. nonopt_start = nonopt_end = -1;
  170. return -1;
  171. }
  172. if ((*(place = nargv[optind]) != '-')
  173. || (place[1] == '\0')) { /* found non-option */
  174. place = EMSG;
  175. if (IN_ORDER) {
  176. /*
  177. * GNU extension:
  178. * return non-option as argument to option 1
  179. */
  180. optarg = nargv[optind++];
  181. return INORDER;
  182. }
  183. if (!PERMUTE) {
  184. /*
  185. * if no permutation wanted, stop parsing
  186. * at first non-option
  187. */
  188. return -1;
  189. }
  190. /* do permutation */
  191. if (nonopt_start == -1)
  192. nonopt_start = optind;
  193. else if (nonopt_end != -1) {
  194. permute_args(nonopt_start, nonopt_end,
  195. optind, nargv);
  196. nonopt_start = optind -
  197. (nonopt_end - nonopt_start);
  198. nonopt_end = -1;
  199. }
  200. optind++;
  201. /* process next argument */
  202. goto start;
  203. }
  204. if (nonopt_start != -1 && nonopt_end == -1)
  205. nonopt_end = optind;
  206. if (place[1] && *++place == '-') { /* found "--" */
  207. place++;
  208. return -2;
  209. }
  210. }
  211. if ((optchar = (int)*place++) == (int)':' ||
  212. (oli = strchr(options + (IGNORE_FIRST ? 1 : 0), optchar)) == NULL) {
  213. /* option letter unknown or ':' */
  214. if (!*place)
  215. ++optind;
  216. if (PRINT_ERROR)
  217. fprintf(stderr, illoptchar, optchar);
  218. optopt = optchar;
  219. return BADCH;
  220. }
  221. if (optchar == 'W' && oli[1] == ';') { /* -W long-option */
  222. /* XXX: what if no long options provided (called by getopt)? */
  223. if (*place)
  224. return -2;
  225. if (++optind >= nargc) { /* no arg */
  226. place = EMSG;
  227. if (PRINT_ERROR)
  228. fprintf(stderr, recargchar, optchar);
  229. optopt = optchar;
  230. return BADARG;
  231. } else /* white space */
  232. place = nargv[optind];
  233. /*
  234. * Handle -W arg the same as --arg (which causes getopt to
  235. * stop parsing).
  236. */
  237. return -2;
  238. }
  239. if (*++oli != ':') { /* doesn't take argument */
  240. if (!*place)
  241. ++optind;
  242. } else { /* takes (optional) argument */
  243. optarg = NULL;
  244. if (*place) /* no white space */
  245. optarg = __UNCONST(place);
  246. /* XXX: disable test for :: if PC? (GNU doesn't) */
  247. else if (oli[1] != ':') { /* arg not optional */
  248. if (++optind >= nargc) { /* no arg */
  249. place = EMSG;
  250. if (PRINT_ERROR)
  251. fprintf(stderr, recargchar, optchar);
  252. optopt = optchar;
  253. return BADARG;
  254. } else
  255. optarg = nargv[optind];
  256. }
  257. place = EMSG;
  258. ++optind;
  259. }
  260. /* dump back option letter */
  261. return optchar;
  262. }
  263. #ifdef REPLACE_GETOPT
  264. /*
  265. * getopt --
  266. * Parse argc/argv argument vector.
  267. *
  268. * [eventually this will replace the real getopt]
  269. */
  270. int
  271. getopt(int nargc, char * const *nargv, const char *options)
  272. {
  273. int retval;
  274. _DIAGASSERT(nargv != NULL);
  275. _DIAGASSERT(options != NULL);
  276. retval = getopt_internal(nargc, __UNCONST(nargv), options);
  277. if (retval == -2) {
  278. ++optind;
  279. /*
  280. * We found an option (--), so if we skipped non-options,
  281. * we have to permute.
  282. */
  283. if (nonopt_end != -1) {
  284. permute_args(nonopt_start, nonopt_end, optind,
  285. __UNCONST(nargv));
  286. optind -= nonopt_end - nonopt_start;
  287. }
  288. nonopt_start = nonopt_end = -1;
  289. retval = -1;
  290. }
  291. return retval;
  292. }
  293. #endif
  294. /*
  295. * getopt_long --
  296. * Parse argc/argv argument vector.
  297. */
  298. int
  299. getopt_long(int nargc, char * const *nargv, const char *options,
  300. const struct option *long_options, int *idx)
  301. {
  302. int retval;
  303. #define IDENTICAL_INTERPRETATION(_x, _y) \
  304. (long_options[(_x)].has_arg == long_options[(_y)].has_arg && \
  305. long_options[(_x)].flag == long_options[(_y)].flag && \
  306. long_options[(_x)].val == long_options[(_y)].val)
  307. _DIAGASSERT(nargv != NULL);
  308. _DIAGASSERT(options != NULL);
  309. _DIAGASSERT(long_options != NULL);
  310. /* idx may be NULL */
  311. retval = getopt_internal(nargc, __UNCONST(nargv), options);
  312. if (retval == -2) {
  313. char *current_argv, *has_equal;
  314. size_t current_argv_len;
  315. int i, ambiguous, match;
  316. current_argv = __UNCONST(place);
  317. match = -1;
  318. ambiguous = 0;
  319. optind++;
  320. place = EMSG;
  321. if (*current_argv == '\0') { /* found "--" */
  322. /*
  323. * We found an option (--), so if we skipped
  324. * non-options, we have to permute.
  325. */
  326. if (nonopt_end != -1) {
  327. permute_args(nonopt_start, nonopt_end,
  328. optind, __UNCONST(nargv));
  329. optind -= nonopt_end - nonopt_start;
  330. }
  331. nonopt_start = nonopt_end = -1;
  332. return -1;
  333. }
  334. if ((has_equal = strchr(current_argv, '=')) != NULL) {
  335. /* argument found (--option=arg) */
  336. current_argv_len = has_equal - current_argv;
  337. has_equal++;
  338. } else
  339. current_argv_len = strlen(current_argv);
  340. for (i = 0; long_options[i].name; i++) {
  341. /* find matching long option */
  342. if (strncmp(current_argv, long_options[i].name,
  343. current_argv_len))
  344. continue;
  345. if (strlen(long_options[i].name) ==
  346. (unsigned)current_argv_len) {
  347. /* exact match */
  348. match = i;
  349. ambiguous = 0;
  350. break;
  351. }
  352. if (match == -1) /* partial match */
  353. match = i;
  354. else if (!IDENTICAL_INTERPRETATION(i, match))
  355. ambiguous = 1;
  356. }
  357. if (ambiguous) {
  358. /* ambiguous abbreviation */
  359. if (PRINT_ERROR)
  360. fprintf(stderr, ambig, (int)current_argv_len,
  361. current_argv);
  362. optopt = 0;
  363. return BADCH;
  364. }
  365. if (match != -1) { /* option found */
  366. if (long_options[match].has_arg == no_argument
  367. && has_equal) {
  368. if (PRINT_ERROR)
  369. fprintf(stderr, noarg, (int)current_argv_len,
  370. current_argv);
  371. /*
  372. * XXX: GNU sets optopt to val regardless of
  373. * flag
  374. */
  375. if (long_options[match].flag == NULL)
  376. optopt = long_options[match].val;
  377. else
  378. optopt = 0;
  379. return BADARG;
  380. }
  381. if (long_options[match].has_arg == required_argument ||
  382. long_options[match].has_arg == optional_argument) {
  383. if (has_equal)
  384. optarg = has_equal;
  385. else if (long_options[match].has_arg ==
  386. required_argument) {
  387. /*
  388. * optional argument doesn't use
  389. * next nargv
  390. */
  391. optarg = nargv[optind++];
  392. }
  393. }
  394. if ((long_options[match].has_arg == required_argument)
  395. && (optarg == NULL)) {
  396. /*
  397. * Missing argument; leading ':'
  398. * indicates no error should be generated
  399. */
  400. if (PRINT_ERROR)
  401. fprintf(stderr, recargstring, current_argv);
  402. /*
  403. * XXX: GNU sets optopt to val regardless
  404. * of flag
  405. */
  406. if (long_options[match].flag == NULL)
  407. optopt = long_options[match].val;
  408. else
  409. optopt = 0;
  410. --optind;
  411. return BADARG;
  412. }
  413. } else { /* unknown option */
  414. if (PRINT_ERROR)
  415. fprintf(stderr, illoptstring, current_argv);
  416. optopt = 0;
  417. return BADCH;
  418. }
  419. if (long_options[match].flag) {
  420. *long_options[match].flag = long_options[match].val;
  421. retval = 0;
  422. } else
  423. retval = long_options[match].val;
  424. if (idx)
  425. *idx = match;
  426. }
  427. return retval;
  428. #undef IDENTICAL_INTERPRETATION
  429. }