getopt.c 34 KB

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