getopt.c 26 KB

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