getopt.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. /* Getopt for Microsoft C
  2. This code is a modification of the Free Software Foundation, Inc.
  3. Getopt library for parsing command line argument the purpose was
  4. to provide a Microsoft Visual C friendly derivative. This code
  5. provides functionality for both Unicode and Multibyte builds.
  6. Date: 02/03/2011 - Ludvik Jerabek - Initial Release
  7. Version: 1.0
  8. Comment: Supports getopt, getopt_long, and getopt_long_only
  9. and POSIXLY_CORRECT environment flag
  10. License: LGPL
  11. Revisions:
  12. 02/03/2011 - Ludvik Jerabek - Initial Release
  13. 02/20/2011 - Ludvik Jerabek - Fixed compiler warnings at Level 4
  14. 07/05/2011 - Ludvik Jerabek - Added no_argument, required_argument, optional_argument defs
  15. 08/03/2011 - Ludvik Jerabek - Fixed non-argument runtime bug which caused runtime exception
  16. 08/09/2011 - Ludvik Jerabek - Added code to export functions for DLL and LIB
  17. 02/15/2012 - Ludvik Jerabek - Fixed _GETOPT_THROW definition missing in implementation file
  18. 08/01/2012 - Ludvik Jerabek - Created separate functions for char and wchar_t characters so single dll can do both unicode and ansi
  19. 10/15/2012 - Ludvik Jerabek - Modified to match latest GNU features
  20. 06/19/2015 - Ludvik Jerabek - Fixed maximum option limitation caused by option_a (255) and option_w (65535) structure val variable
  21. **DISCLAIMER**
  22. THIS MATERIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  23. EITHER EXPRESS OR IMPLIED, INCLUDING, BUT Not LIMITED TO, THE
  24. IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  25. PURPOSE, OR NON-INFRINGEMENT. SOME JURISDICTIONS DO NOT ALLOW THE
  26. EXCLUSION OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY NOT
  27. APPLY TO YOU. IN NO EVENT WILL I BE LIABLE TO ANY PARTY FOR ANY
  28. DIRECT, INDIRECT, SPECIAL OR OTHER CONSEQUENTIAL DAMAGES FOR ANY
  29. USE OF THIS MATERIAL INCLUDING, WITHOUT LIMITATION, ANY LOST
  30. PROFITS, BUSINESS INTERRUPTION, LOSS OF PROGRAMS OR OTHER DATA ON
  31. YOUR INFORMATION HANDLING SYSTEM OR OTHERWISE, EVEN If WE ARE
  32. EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  33. */
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #ifdef _MSC_VER
  37. #include <malloc.h>
  38. #endif
  39. #include "getopt.h"
  40. #ifdef __cplusplus
  41. #define _GETOPT_THROW throw()
  42. #else
  43. #define _GETOPT_THROW
  44. #endif
  45. int optind = 1;
  46. int opterr = 1;
  47. int optopt = '?';
  48. enum ENUM_ORDERING { REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER };
  49. static struct _getopt_data_a
  50. {
  51. int optind;
  52. int opterr;
  53. int optopt;
  54. char *optarg;
  55. int __initialized;
  56. char *__nextchar;
  57. enum ENUM_ORDERING __ordering;
  58. int __posixly_correct;
  59. int __first_nonopt;
  60. int __last_nonopt;
  61. } getopt_data_a;
  62. char *optarg_a;
  63. static void exchange_a(char **argv, struct _getopt_data_a *d)
  64. {
  65. int bottom = d->__first_nonopt;
  66. int middle = d->__last_nonopt;
  67. int top = d->optind;
  68. char *tem;
  69. while (top > middle && middle > bottom)
  70. {
  71. if (top - middle > middle - bottom)
  72. {
  73. int len = middle - bottom;
  74. register int i;
  75. for (i = 0; i < len; i++)
  76. {
  77. tem = argv[bottom + i];
  78. argv[bottom + i] = argv[top - (middle - bottom) + i];
  79. argv[top - (middle - bottom) + i] = tem;
  80. }
  81. top -= len;
  82. }
  83. else
  84. {
  85. int len = top - middle;
  86. register int i;
  87. for (i = 0; i < len; i++)
  88. {
  89. tem = argv[bottom + i];
  90. argv[bottom + i] = argv[middle + i];
  91. argv[middle + i] = tem;
  92. }
  93. bottom += len;
  94. }
  95. }
  96. d->__first_nonopt += (d->optind - d->__last_nonopt);
  97. d->__last_nonopt = d->optind;
  98. }
  99. static const char *_getopt_initialize_a(const char *optstring, struct _getopt_data_a *d, int posixly_correct)
  100. {
  101. d->__first_nonopt = d->__last_nonopt = d->optind;
  102. d->__nextchar = NULL;
  103. d->__posixly_correct = posixly_correct | !!getenv("POSIXLY_CORRECT");
  104. if (optstring[0] == '-')
  105. {
  106. d->__ordering = RETURN_IN_ORDER;
  107. ++optstring;
  108. }
  109. else if (optstring[0] == '+')
  110. {
  111. d->__ordering = REQUIRE_ORDER;
  112. ++optstring;
  113. }
  114. else if (d->__posixly_correct)
  115. d->__ordering = REQUIRE_ORDER;
  116. else
  117. d->__ordering = PERMUTE;
  118. return optstring;
  119. }
  120. int _getopt_internal_r_a(int argc, char *const *argv, const char *optstring, const struct option_a *longopts, int *longind, int long_only, struct _getopt_data_a *d, int posixly_correct)
  121. {
  122. int print_errors = d->opterr;
  123. if (argc < 1)
  124. return -1;
  125. d->optarg = NULL;
  126. if (d->optind == 0 || !d->__initialized)
  127. {
  128. if (d->optind == 0)
  129. d->optind = 1;
  130. optstring = _getopt_initialize_a(optstring, d, posixly_correct);
  131. d->__initialized = 1;
  132. }
  133. else if (optstring[0] == '-' || optstring[0] == '+')
  134. optstring++;
  135. if (optstring[0] == ':')
  136. print_errors = 0;
  137. if (d->__nextchar == NULL || *d->__nextchar == '\0')
  138. {
  139. if (d->__last_nonopt > d->optind)
  140. d->__last_nonopt = d->optind;
  141. if (d->__first_nonopt > d->optind)
  142. d->__first_nonopt = d->optind;
  143. if (d->__ordering == PERMUTE)
  144. {
  145. if (d->__first_nonopt != d->__last_nonopt && d->__last_nonopt != d->optind)
  146. exchange_a((char **)argv, d);
  147. else if (d->__last_nonopt != d->optind)
  148. d->__first_nonopt = d->optind;
  149. while (d->optind < argc && (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0'))
  150. d->optind++;
  151. d->__last_nonopt = d->optind;
  152. }
  153. if (d->optind != argc && !strcmp(argv[d->optind], "--"))
  154. {
  155. d->optind++;
  156. if (d->__first_nonopt != d->__last_nonopt && d->__last_nonopt != d->optind)
  157. exchange_a((char **)argv, d);
  158. else if (d->__first_nonopt == d->__last_nonopt)
  159. d->__first_nonopt = d->optind;
  160. d->__last_nonopt = argc;
  161. d->optind = argc;
  162. }
  163. if (d->optind == argc)
  164. {
  165. if (d->__first_nonopt != d->__last_nonopt)
  166. d->optind = d->__first_nonopt;
  167. return -1;
  168. }
  169. if ((argv[d->optind][0] != '-' || argv[d->optind][1] == '\0'))
  170. {
  171. if (d->__ordering == REQUIRE_ORDER)
  172. return -1;
  173. d->optarg = argv[d->optind++];
  174. return 1;
  175. }
  176. d->__nextchar = (argv[d->optind] + 1 + (longopts != NULL && argv[d->optind][1] == '-'));
  177. }
  178. if (longopts != NULL && (argv[d->optind][1] == '-' || (long_only && (argv[d->optind][2] || !strchr(optstring, argv[d->optind][1])))))
  179. {
  180. char *nameend;
  181. unsigned int namelen;
  182. const struct option_a *p;
  183. const struct option_a *pfound = NULL;
  184. struct option_list
  185. {
  186. const struct option_a *p;
  187. struct option_list *next;
  188. } *ambig_list = NULL;
  189. int exact = 0;
  190. int indfound = -1;
  191. int option_index;
  192. for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++);
  193. namelen = (unsigned int)(nameend - d->__nextchar);
  194. for (p = longopts, option_index = 0; p->name; p++, option_index++)
  195. if (!strncmp(p->name, d->__nextchar, namelen))
  196. {
  197. if (namelen == (unsigned int)strlen(p->name))
  198. {
  199. pfound = p;
  200. indfound = option_index;
  201. exact = 1;
  202. break;
  203. }
  204. else if (pfound == NULL)
  205. {
  206. pfound = p;
  207. indfound = option_index;
  208. }
  209. else if (long_only || pfound->has_arg != p->has_arg || pfound->flag != p->flag || pfound->val != p->val)
  210. {
  211. struct option_list *newp = (struct option_list*)alloca(sizeof(*newp));
  212. newp->p = p;
  213. newp->next = ambig_list;
  214. ambig_list = newp;
  215. }
  216. }
  217. if (ambig_list != NULL && !exact)
  218. {
  219. if (print_errors)
  220. {
  221. struct option_list first;
  222. first.p = pfound;
  223. first.next = ambig_list;
  224. ambig_list = &first;
  225. fprintf(stderr, "%s: option '%s' is ambiguous; possibilities:", argv[0], argv[d->optind]);
  226. do
  227. {
  228. fprintf(stderr, " '--%s'", ambig_list->p->name);
  229. ambig_list = ambig_list->next;
  230. } while (ambig_list != NULL);
  231. fputc('\n', stderr);
  232. }
  233. d->__nextchar += strlen(d->__nextchar);
  234. d->optind++;
  235. d->optopt = 0;
  236. return '?';
  237. }
  238. if (pfound != NULL)
  239. {
  240. option_index = indfound;
  241. d->optind++;
  242. if (*nameend)
  243. {
  244. if (pfound->has_arg)
  245. d->optarg = nameend + 1;
  246. else
  247. {
  248. if (print_errors)
  249. {
  250. if (argv[d->optind - 1][1] == '-')
  251. {
  252. fprintf(stderr, "%s: option '--%s' doesn't allow an argument\n", argv[0], pfound->name);
  253. }
  254. else
  255. {
  256. fprintf(stderr, "%s: option '%c%s' doesn't allow an argument\n", argv[0], argv[d->optind - 1][0], pfound->name);
  257. }
  258. }
  259. d->__nextchar += strlen(d->__nextchar);
  260. d->optopt = pfound->val;
  261. return '?';
  262. }
  263. }
  264. else if (pfound->has_arg == 1)
  265. {
  266. if (d->optind < argc)
  267. d->optarg = argv[d->optind++];
  268. else
  269. {
  270. if (print_errors)
  271. {
  272. fprintf(stderr, "%s: option '--%s' requires an argument\n", argv[0], pfound->name);
  273. }
  274. d->__nextchar += strlen(d->__nextchar);
  275. d->optopt = pfound->val;
  276. return optstring[0] == ':' ? ':' : '?';
  277. }
  278. }
  279. d->__nextchar += strlen(d->__nextchar);
  280. if (longind != NULL)
  281. *longind = option_index;
  282. if (pfound->flag)
  283. {
  284. *(pfound->flag) = pfound->val;
  285. return 0;
  286. }
  287. return pfound->val;
  288. }
  289. if (!long_only || argv[d->optind][1] == '-' || strchr(optstring, *d->__nextchar) == NULL)
  290. {
  291. if (print_errors)
  292. {
  293. if (argv[d->optind][1] == '-')
  294. {
  295. fprintf(stderr, "%s: unrecognized option '--%s'\n", argv[0], d->__nextchar);
  296. }
  297. else
  298. {
  299. fprintf(stderr, "%s: unrecognized option '%c%s'\n", argv[0], argv[d->optind][0], d->__nextchar);
  300. }
  301. }
  302. d->__nextchar = (char *)"";
  303. d->optind++;
  304. d->optopt = 0;
  305. return '?';
  306. }
  307. }
  308. {
  309. char c = *d->__nextchar++;
  310. char *temp = (char*)strchr(optstring, c);
  311. if (*d->__nextchar == '\0')
  312. ++d->optind;
  313. if (temp == NULL || c == ':' || c == ';')
  314. {
  315. if (print_errors)
  316. {
  317. fprintf(stderr, "%s: invalid option -- '%c'\n", argv[0], c);
  318. }
  319. d->optopt = c;
  320. return '?';
  321. }
  322. if (temp[0] == 'W' && temp[1] == ';')
  323. {
  324. char *nameend;
  325. const struct option_a *p;
  326. const struct option_a *pfound = NULL;
  327. int exact = 0;
  328. int ambig = 0;
  329. int indfound = 0;
  330. int option_index;
  331. if (longopts == NULL)
  332. goto no_longs;
  333. if (*d->__nextchar != '\0')
  334. {
  335. d->optarg = d->__nextchar;
  336. d->optind++;
  337. }
  338. else if (d->optind == argc)
  339. {
  340. if (print_errors)
  341. {
  342. fprintf(stderr, "%s: option requires an argument -- '%c'\n", argv[0], c);
  343. }
  344. d->optopt = c;
  345. if (optstring[0] == ':')
  346. c = ':';
  347. else
  348. c = '?';
  349. return c;
  350. }
  351. else
  352. d->optarg = argv[d->optind++];
  353. for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '='; nameend++);
  354. for (p = longopts, option_index = 0; p->name; p++, option_index++)
  355. if (!strncmp(p->name, d->__nextchar, nameend - d->__nextchar))
  356. {
  357. if ((unsigned int)(nameend - d->__nextchar) == strlen(p->name))
  358. {
  359. pfound = p;
  360. indfound = option_index;
  361. exact = 1;
  362. break;
  363. }
  364. else if (pfound == NULL)
  365. {
  366. pfound = p;
  367. indfound = option_index;
  368. }
  369. else if (long_only || pfound->has_arg != p->has_arg || pfound->flag != p->flag || pfound->val != p->val)
  370. ambig = 1;
  371. }
  372. if (ambig && !exact)
  373. {
  374. if (print_errors)
  375. {
  376. fprintf(stderr, "%s: option '-W %s' is ambiguous\n", argv[0], d->optarg);
  377. }
  378. d->__nextchar += strlen(d->__nextchar);
  379. d->optind++;
  380. return '?';
  381. }
  382. if (pfound != NULL)
  383. {
  384. option_index = indfound;
  385. if (*nameend)
  386. {
  387. if (pfound->has_arg)
  388. d->optarg = nameend + 1;
  389. else
  390. {
  391. if (print_errors)
  392. {
  393. fprintf(stderr, "%s: option '-W %s' doesn't allow an argument\n", argv[0], pfound->name);
  394. }
  395. d->__nextchar += strlen(d->__nextchar);
  396. return '?';
  397. }
  398. }
  399. else if (pfound->has_arg == 1)
  400. {
  401. if (d->optind < argc)
  402. d->optarg = argv[d->optind++];
  403. else
  404. {
  405. if (print_errors)
  406. {
  407. fprintf(stderr, "%s: option '-W %s' requires an argument\n", argv[0], pfound->name);
  408. }
  409. d->__nextchar += strlen(d->__nextchar);
  410. return optstring[0] == ':' ? ':' : '?';
  411. }
  412. }
  413. else
  414. d->optarg = NULL;
  415. d->__nextchar += strlen(d->__nextchar);
  416. if (longind != NULL)
  417. *longind = option_index;
  418. if (pfound->flag)
  419. {
  420. *(pfound->flag) = pfound->val;
  421. return 0;
  422. }
  423. return pfound->val;
  424. }
  425. no_longs:
  426. d->__nextchar = NULL;
  427. return 'W';
  428. }
  429. if (temp[1] == ':')
  430. {
  431. if (temp[2] == ':')
  432. {
  433. if (*d->__nextchar != '\0')
  434. {
  435. d->optarg = d->__nextchar;
  436. d->optind++;
  437. }
  438. else
  439. d->optarg = NULL;
  440. d->__nextchar = NULL;
  441. }
  442. else
  443. {
  444. if (*d->__nextchar != '\0')
  445. {
  446. d->optarg = d->__nextchar;
  447. d->optind++;
  448. }
  449. else if (d->optind == argc)
  450. {
  451. if (print_errors)
  452. {
  453. fprintf(stderr, "%s: option requires an argument -- '%c'\n", argv[0], c);
  454. }
  455. d->optopt = c;
  456. if (optstring[0] == ':')
  457. c = ':';
  458. else
  459. c = '?';
  460. }
  461. else
  462. d->optarg = argv[d->optind++];
  463. d->__nextchar = NULL;
  464. }
  465. }
  466. return c;
  467. }
  468. }
  469. int _getopt_internal_a(int argc, char *const *argv, const char *optstring, const struct option_a *longopts, int *longind, int long_only, int posixly_correct)
  470. {
  471. int result;
  472. getopt_data_a.optind = optind;
  473. getopt_data_a.opterr = opterr;
  474. result = _getopt_internal_r_a(argc, argv, optstring, longopts, longind, long_only, &getopt_data_a, posixly_correct);
  475. optind = getopt_data_a.optind;
  476. optarg_a = getopt_data_a.optarg;
  477. optopt = getopt_data_a.optopt;
  478. return result;
  479. }
  480. int getopt_a(int argc, char *const *argv, const char *optstring) _GETOPT_THROW
  481. {
  482. return _getopt_internal_a(argc, argv, optstring, (const struct option_a *) 0, (int *)0, 0, 0);
  483. }
  484. int getopt_long_a(int argc, char *const *argv, const char *options, const struct option_a *long_options, int *opt_index) _GETOPT_THROW
  485. {
  486. return _getopt_internal_a(argc, argv, options, long_options, opt_index, 0, 0);
  487. }
  488. int getopt_long_only_a(int argc, char *const *argv, const char *options, const struct option_a *long_options, int *opt_index) _GETOPT_THROW
  489. {
  490. return _getopt_internal_a(argc, argv, options, long_options, opt_index, 1, 0);
  491. }
  492. int _getopt_long_r_a(int argc, char *const *argv, const char *options, const struct option_a *long_options, int *opt_index, struct _getopt_data_a *d)
  493. {
  494. return _getopt_internal_r_a(argc, argv, options, long_options, opt_index, 0, d, 0);
  495. }
  496. int _getopt_long_only_r_a(int argc, char *const *argv, const char *options, const struct option_a *long_options, int *opt_index, struct _getopt_data_a *d)
  497. {
  498. return _getopt_internal_r_a(argc, argv, options, long_options, opt_index, 1, d, 0);
  499. }
  500. static struct _getopt_data_w
  501. {
  502. int optind;
  503. int opterr;
  504. int optopt;
  505. wchar_t *optarg;
  506. int __initialized;
  507. wchar_t *__nextchar;
  508. enum ENUM_ORDERING __ordering;
  509. int __posixly_correct;
  510. int __first_nonopt;
  511. int __last_nonopt;
  512. } getopt_data_w;
  513. wchar_t *optarg_w;
  514. static void exchange_w(wchar_t **argv, struct _getopt_data_w *d)
  515. {
  516. int bottom = d->__first_nonopt;
  517. int middle = d->__last_nonopt;
  518. int top = d->optind;
  519. wchar_t *tem;
  520. while (top > middle && middle > bottom)
  521. {
  522. if (top - middle > middle - bottom)
  523. {
  524. int len = middle - bottom;
  525. register int i;
  526. for (i = 0; i < len; i++)
  527. {
  528. tem = argv[bottom + i];
  529. argv[bottom + i] = argv[top - (middle - bottom) + i];
  530. argv[top - (middle - bottom) + i] = tem;
  531. }
  532. top -= len;
  533. }
  534. else
  535. {
  536. int len = top - middle;
  537. register int i;
  538. for (i = 0; i < len; i++)
  539. {
  540. tem = argv[bottom + i];
  541. argv[bottom + i] = argv[middle + i];
  542. argv[middle + i] = tem;
  543. }
  544. bottom += len;
  545. }
  546. }
  547. d->__first_nonopt += (d->optind - d->__last_nonopt);
  548. d->__last_nonopt = d->optind;
  549. }
  550. static const wchar_t *_getopt_initialize_w(const wchar_t *optstring, struct _getopt_data_w *d, int posixly_correct)
  551. {
  552. d->__first_nonopt = d->__last_nonopt = d->optind;
  553. d->__nextchar = NULL;
  554. d->__posixly_correct = posixly_correct | !!_wgetenv(L"POSIXLY_CORRECT");
  555. if (optstring[0] == L'-')
  556. {
  557. d->__ordering = RETURN_IN_ORDER;
  558. ++optstring;
  559. }
  560. else if (optstring[0] == L'+')
  561. {
  562. d->__ordering = REQUIRE_ORDER;
  563. ++optstring;
  564. }
  565. else if (d->__posixly_correct)
  566. d->__ordering = REQUIRE_ORDER;
  567. else
  568. d->__ordering = PERMUTE;
  569. return optstring;
  570. }
  571. int _getopt_internal_r_w(int argc, wchar_t *const *argv, const wchar_t *optstring, const struct option_w *longopts, int *longind, int long_only, struct _getopt_data_w *d, int posixly_correct)
  572. {
  573. int print_errors = d->opterr;
  574. if (argc < 1)
  575. return -1;
  576. d->optarg = NULL;
  577. if (d->optind == 0 || !d->__initialized)
  578. {
  579. if (d->optind == 0)
  580. d->optind = 1;
  581. optstring = _getopt_initialize_w(optstring, d, posixly_correct);
  582. d->__initialized = 1;
  583. }
  584. else if (optstring[0] == L'-' || optstring[0] == L'+')
  585. optstring++;
  586. if (optstring[0] == L':')
  587. print_errors = 0;
  588. if (d->__nextchar == NULL || *d->__nextchar == L'\0')
  589. {
  590. if (d->__last_nonopt > d->optind)
  591. d->__last_nonopt = d->optind;
  592. if (d->__first_nonopt > d->optind)
  593. d->__first_nonopt = d->optind;
  594. if (d->__ordering == PERMUTE)
  595. {
  596. if (d->__first_nonopt != d->__last_nonopt && d->__last_nonopt != d->optind)
  597. exchange_w((wchar_t **)argv, d);
  598. else if (d->__last_nonopt != d->optind)
  599. d->__first_nonopt = d->optind;
  600. while (d->optind < argc && (argv[d->optind][0] != L'-' || argv[d->optind][1] == L'\0'))
  601. d->optind++;
  602. d->__last_nonopt = d->optind;
  603. }
  604. if (d->optind != argc && !wcscmp(argv[d->optind], L"--"))
  605. {
  606. d->optind++;
  607. if (d->__first_nonopt != d->__last_nonopt && d->__last_nonopt != d->optind)
  608. exchange_w((wchar_t **)argv, d);
  609. else if (d->__first_nonopt == d->__last_nonopt)
  610. d->__first_nonopt = d->optind;
  611. d->__last_nonopt = argc;
  612. d->optind = argc;
  613. }
  614. if (d->optind == argc)
  615. {
  616. if (d->__first_nonopt != d->__last_nonopt)
  617. d->optind = d->__first_nonopt;
  618. return -1;
  619. }
  620. if ((argv[d->optind][0] != L'-' || argv[d->optind][1] == L'\0'))
  621. {
  622. if (d->__ordering == REQUIRE_ORDER)
  623. return -1;
  624. d->optarg = argv[d->optind++];
  625. return 1;
  626. }
  627. d->__nextchar = (argv[d->optind] + 1 + (longopts != NULL && argv[d->optind][1] == L'-'));
  628. }
  629. if (longopts != NULL && (argv[d->optind][1] == L'-' || (long_only && (argv[d->optind][2] || !wcschr(optstring, argv[d->optind][1])))))
  630. {
  631. wchar_t *nameend;
  632. unsigned int namelen;
  633. const struct option_w *p;
  634. const struct option_w *pfound = NULL;
  635. struct option_list
  636. {
  637. const struct option_w *p;
  638. struct option_list *next;
  639. } *ambig_list = NULL;
  640. int exact = 0;
  641. int indfound = -1;
  642. int option_index;
  643. for (nameend = d->__nextchar; *nameend && *nameend != L'='; nameend++);
  644. namelen = (unsigned int)(nameend - d->__nextchar);
  645. for (p = longopts, option_index = 0; p->name; p++, option_index++)
  646. if (!wcsncmp(p->name, d->__nextchar, namelen))
  647. {
  648. if (namelen == (unsigned int)wcslen(p->name))
  649. {
  650. pfound = p;
  651. indfound = option_index;
  652. exact = 1;
  653. break;
  654. }
  655. else if (pfound == NULL)
  656. {
  657. pfound = p;
  658. indfound = option_index;
  659. }
  660. else if (long_only || pfound->has_arg != p->has_arg || pfound->flag != p->flag || pfound->val != p->val)
  661. {
  662. struct option_list *newp = (struct option_list*)alloca(sizeof(*newp));
  663. newp->p = p;
  664. newp->next = ambig_list;
  665. ambig_list = newp;
  666. }
  667. }
  668. if (ambig_list != NULL && !exact)
  669. {
  670. if (print_errors)
  671. {
  672. struct option_list first;
  673. first.p = pfound;
  674. first.next = ambig_list;
  675. ambig_list = &first;
  676. fwprintf(stderr, L"%s: option '%s' is ambiguous; possibilities:", argv[0], argv[d->optind]);
  677. do
  678. {
  679. fwprintf(stderr, L" '--%s'", ambig_list->p->name);
  680. ambig_list = ambig_list->next;
  681. } while (ambig_list != NULL);
  682. fputwc(L'\n', stderr);
  683. }
  684. d->__nextchar += wcslen(d->__nextchar);
  685. d->optind++;
  686. d->optopt = 0;
  687. return L'?';
  688. }
  689. if (pfound != NULL)
  690. {
  691. option_index = indfound;
  692. d->optind++;
  693. if (*nameend)
  694. {
  695. if (pfound->has_arg)
  696. d->optarg = nameend + 1;
  697. else
  698. {
  699. if (print_errors)
  700. {
  701. if (argv[d->optind - 1][1] == L'-')
  702. {
  703. fwprintf(stderr, L"%s: option '--%s' doesn't allow an argument\n", argv[0], pfound->name);
  704. }
  705. else
  706. {
  707. fwprintf(stderr, L"%s: option '%c%s' doesn't allow an argument\n", argv[0], argv[d->optind - 1][0], pfound->name);
  708. }
  709. }
  710. d->__nextchar += wcslen(d->__nextchar);
  711. d->optopt = pfound->val;
  712. return L'?';
  713. }
  714. }
  715. else if (pfound->has_arg == 1)
  716. {
  717. if (d->optind < argc)
  718. d->optarg = argv[d->optind++];
  719. else
  720. {
  721. if (print_errors)
  722. {
  723. fwprintf(stderr, L"%s: option '--%s' requires an argument\n", argv[0], pfound->name);
  724. }
  725. d->__nextchar += wcslen(d->__nextchar);
  726. d->optopt = pfound->val;
  727. return optstring[0] == L':' ? L':' : L'?';
  728. }
  729. }
  730. d->__nextchar += wcslen(d->__nextchar);
  731. if (longind != NULL)
  732. *longind = option_index;
  733. if (pfound->flag)
  734. {
  735. *(pfound->flag) = pfound->val;
  736. return 0;
  737. }
  738. return pfound->val;
  739. }
  740. if (!long_only || argv[d->optind][1] == L'-' || wcschr(optstring, *d->__nextchar) == NULL)
  741. {
  742. if (print_errors)
  743. {
  744. if (argv[d->optind][1] == L'-')
  745. {
  746. fwprintf(stderr, L"%s: unrecognized option '--%s'\n", argv[0], d->__nextchar);
  747. }
  748. else
  749. {
  750. fwprintf(stderr, L"%s: unrecognized option '%c%s'\n", argv[0], argv[d->optind][0], d->__nextchar);
  751. }
  752. }
  753. d->__nextchar = (wchar_t *)L"";
  754. d->optind++;
  755. d->optopt = 0;
  756. return L'?';
  757. }
  758. }
  759. {
  760. wchar_t c = *d->__nextchar++;
  761. wchar_t *temp = (wchar_t*)wcschr(optstring, c);
  762. if (*d->__nextchar == L'\0')
  763. ++d->optind;
  764. if (temp == NULL || c == L':' || c == L';')
  765. {
  766. if (print_errors)
  767. {
  768. fwprintf(stderr, L"%s: invalid option -- '%c'\n", argv[0], c);
  769. }
  770. d->optopt = c;
  771. return L'?';
  772. }
  773. if (temp[0] == L'W' && temp[1] == L';')
  774. {
  775. wchar_t *nameend;
  776. const struct option_w *p;
  777. const struct option_w *pfound = NULL;
  778. int exact = 0;
  779. int ambig = 0;
  780. int indfound = 0;
  781. int option_index;
  782. if (longopts == NULL)
  783. goto no_longs;
  784. if (*d->__nextchar != L'\0')
  785. {
  786. d->optarg = d->__nextchar;
  787. d->optind++;
  788. }
  789. else if (d->optind == argc)
  790. {
  791. if (print_errors)
  792. {
  793. fwprintf(stderr, L"%s: option requires an argument -- '%c'\n", argv[0], c);
  794. }
  795. d->optopt = c;
  796. if (optstring[0] == L':')
  797. c = L':';
  798. else
  799. c = L'?';
  800. return c;
  801. }
  802. else
  803. d->optarg = argv[d->optind++];
  804. for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != L'='; nameend++);
  805. for (p = longopts, option_index = 0; p->name; p++, option_index++)
  806. if (!wcsncmp(p->name, d->__nextchar, nameend - d->__nextchar))
  807. {
  808. if ((unsigned int)(nameend - d->__nextchar) == wcslen(p->name))
  809. {
  810. pfound = p;
  811. indfound = option_index;
  812. exact = 1;
  813. break;
  814. }
  815. else if (pfound == NULL)
  816. {
  817. pfound = p;
  818. indfound = option_index;
  819. }
  820. else if (long_only || pfound->has_arg != p->has_arg || pfound->flag != p->flag || pfound->val != p->val)
  821. ambig = 1;
  822. }
  823. if (ambig && !exact)
  824. {
  825. if (print_errors)
  826. {
  827. fwprintf(stderr, L"%s: option '-W %s' is ambiguous\n", argv[0], d->optarg);
  828. }
  829. d->__nextchar += wcslen(d->__nextchar);
  830. d->optind++;
  831. return L'?';
  832. }
  833. if (pfound != NULL)
  834. {
  835. option_index = indfound;
  836. if (*nameend)
  837. {
  838. if (pfound->has_arg)
  839. d->optarg = nameend + 1;
  840. else
  841. {
  842. if (print_errors)
  843. {
  844. fwprintf(stderr, L"%s: option '-W %s' doesn't allow an argument\n", argv[0], pfound->name);
  845. }
  846. d->__nextchar += wcslen(d->__nextchar);
  847. return L'?';
  848. }
  849. }
  850. else if (pfound->has_arg == 1)
  851. {
  852. if (d->optind < argc)
  853. d->optarg = argv[d->optind++];
  854. else
  855. {
  856. if (print_errors)
  857. {
  858. fwprintf(stderr, L"%s: option '-W %s' requires an argument\n", argv[0], pfound->name);
  859. }
  860. d->__nextchar += wcslen(d->__nextchar);
  861. return optstring[0] == L':' ? L':' : L'?';
  862. }
  863. }
  864. else
  865. d->optarg = NULL;
  866. d->__nextchar += wcslen(d->__nextchar);
  867. if (longind != NULL)
  868. *longind = option_index;
  869. if (pfound->flag)
  870. {
  871. *(pfound->flag) = pfound->val;
  872. return 0;
  873. }
  874. return pfound->val;
  875. }
  876. no_longs:
  877. d->__nextchar = NULL;
  878. return L'W';
  879. }
  880. if (temp[1] == L':')
  881. {
  882. if (temp[2] == L':')
  883. {
  884. if (*d->__nextchar != L'\0')
  885. {
  886. d->optarg = d->__nextchar;
  887. d->optind++;
  888. }
  889. else
  890. d->optarg = NULL;
  891. d->__nextchar = NULL;
  892. }
  893. else
  894. {
  895. if (*d->__nextchar != L'\0')
  896. {
  897. d->optarg = d->__nextchar;
  898. d->optind++;
  899. }
  900. else if (d->optind == argc)
  901. {
  902. if (print_errors)
  903. {
  904. fwprintf(stderr, L"%s: option requires an argument -- '%c'\n", argv[0], c);
  905. }
  906. d->optopt = c;
  907. if (optstring[0] == L':')
  908. c = L':';
  909. else
  910. c = L'?';
  911. }
  912. else
  913. d->optarg = argv[d->optind++];
  914. d->__nextchar = NULL;
  915. }
  916. }
  917. return c;
  918. }
  919. }
  920. int _getopt_internal_w(int argc, wchar_t *const *argv, const wchar_t *optstring, const struct option_w *longopts, int *longind, int long_only, int posixly_correct)
  921. {
  922. int result;
  923. getopt_data_w.optind = optind;
  924. getopt_data_w.opterr = opterr;
  925. result = _getopt_internal_r_w(argc, argv, optstring, longopts, longind, long_only, &getopt_data_w, posixly_correct);
  926. optind = getopt_data_w.optind;
  927. optarg_w = getopt_data_w.optarg;
  928. optopt = getopt_data_w.optopt;
  929. return result;
  930. }
  931. int getopt_w(int argc, wchar_t *const *argv, const wchar_t *optstring) _GETOPT_THROW
  932. {
  933. return _getopt_internal_w(argc, argv, optstring, (const struct option_w *) 0, (int *)0, 0, 0);
  934. }
  935. int getopt_long_w(int argc, wchar_t *const *argv, const wchar_t *options, const struct option_w *long_options, int *opt_index) _GETOPT_THROW
  936. {
  937. return _getopt_internal_w(argc, argv, options, long_options, opt_index, 0, 0);
  938. }
  939. int getopt_long_only_w(int argc, wchar_t *const *argv, const wchar_t *options, const struct option_w *long_options, int *opt_index) _GETOPT_THROW
  940. {
  941. return _getopt_internal_w(argc, argv, options, long_options, opt_index, 1, 0);
  942. }
  943. int _getopt_long_r_w(int argc, wchar_t *const *argv, const wchar_t *options, const struct option_w *long_options, int *opt_index, struct _getopt_data_w *d)
  944. {
  945. return _getopt_internal_r_w(argc, argv, options, long_options, opt_index, 0, d, 0);
  946. }
  947. int _getopt_long_only_r_w(int argc, wchar_t *const *argv, const wchar_t *options, const struct option_w *long_options, int *opt_index, struct _getopt_data_w *d)
  948. {
  949. return _getopt_internal_r_w(argc, argv, options, long_options, opt_index, 1, d, 0);
  950. }