getopt.cpp 26 KB

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