getopt.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. 24/10/2020 - Paul-Louis Ageneau - Removed Unicode version
  22. **DISCLAIMER**
  23. THIS MATERIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  24. EITHER EXPRESS OR IMPLIED, INCLUDING, BUT Not LIMITED TO, THE
  25. IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  26. PURPOSE, OR NON-INFRINGEMENT. SOME JURISDICTIONS DO NOT ALLOW THE
  27. EXCLUSION OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY NOT
  28. APPLY TO YOU. IN NO EVENT WILL I BE LIABLE TO ANY PARTY FOR ANY
  29. DIRECT, INDIRECT, SPECIAL OR OTHER CONSEQUENTIAL DAMAGES FOR ANY
  30. USE OF THIS MATERIAL INCLUDING, WITHOUT LIMITATION, ANY LOST
  31. PROFITS, BUSINESS INTERRUPTION, LOSS OF PROGRAMS OR OTHER DATA ON
  32. YOUR INFORMATION HANDLING SYSTEM OR OTHERWISE, EVEN If WE ARE
  33. EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34. */
  35. #ifndef _CRT_SECURE_NO_WARNINGS
  36. #define _CRT_SECURE_NO_WARNINGS
  37. #endif
  38. #include <stdlib.h>
  39. #include <stdio.h>
  40. #include <malloc.h>
  41. #include "getopt.h"
  42. #ifdef __cplusplus
  43. #define _GETOPT_THROW throw()
  44. #else
  45. #define _GETOPT_THROW
  46. #endif
  47. int optind = 1;
  48. int opterr = 1;
  49. int optopt = '?';
  50. enum ENUM_ORDERING { REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER };
  51. static struct _getopt_data_a
  52. {
  53. int optind;
  54. int opterr;
  55. int optopt;
  56. char *optarg;
  57. int __initialized;
  58. char *__nextchar;
  59. enum ENUM_ORDERING __ordering;
  60. int __posixly_correct;
  61. int __first_nonopt;
  62. int __last_nonopt;
  63. } getopt_data_a;
  64. char *optarg_a;
  65. static void exchange_a(char **argv, struct _getopt_data_a *d)
  66. {
  67. int bottom = d->__first_nonopt;
  68. int middle = d->__last_nonopt;
  69. int top = d->optind;
  70. char *tem;
  71. while (top > middle && middle > bottom)
  72. {
  73. if (top - middle > middle - bottom)
  74. {
  75. int len = middle - bottom;
  76. int i;
  77. for (i = 0; i < len; i++)
  78. {
  79. tem = argv[bottom + i];
  80. argv[bottom + i] = argv[top - (middle - bottom) + i];
  81. argv[top - (middle - bottom) + i] = tem;
  82. }
  83. top -= len;
  84. }
  85. else
  86. {
  87. int len = top - middle;
  88. int i;
  89. for (i = 0; i < len; i++)
  90. {
  91. tem = argv[bottom + i];
  92. argv[bottom + i] = argv[middle + i];
  93. argv[middle + i] = tem;
  94. }
  95. bottom += len;
  96. }
  97. }
  98. d->__first_nonopt += (d->optind - d->__last_nonopt);
  99. d->__last_nonopt = d->optind;
  100. }
  101. static const char *_getopt_initialize_a (const char *optstring, struct _getopt_data_a *d, int posixly_correct)
  102. {
  103. d->__first_nonopt = d->__last_nonopt = d->optind;
  104. d->__nextchar = NULL;
  105. d->__posixly_correct = posixly_correct | !!getenv("POSIXLY_CORRECT");
  106. if (optstring[0] == '-')
  107. {
  108. d->__ordering = RETURN_IN_ORDER;
  109. ++optstring;
  110. }
  111. else if (optstring[0] == '+')
  112. {
  113. d->__ordering = REQUIRE_ORDER;
  114. ++optstring;
  115. }
  116. else if (d->__posixly_correct)
  117. d->__ordering = REQUIRE_ORDER;
  118. else
  119. d->__ordering = PERMUTE;
  120. return optstring;
  121. }
  122. 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)
  123. {
  124. int print_errors = d->opterr;
  125. if (argc < 1)
  126. return -1;
  127. d->optarg = NULL;
  128. if (d->optind == 0 || !d->__initialized)
  129. {
  130. if (d->optind == 0)
  131. d->optind = 1;
  132. optstring = _getopt_initialize_a (optstring, d, posixly_correct);
  133. d->__initialized = 1;
  134. }
  135. else if (optstring[0] == '-' || optstring[0] == '+')
  136. optstring++;
  137. if (optstring[0] == ':')
  138. print_errors = 0;
  139. if (d->__nextchar == NULL || *d->__nextchar == '\0')
  140. {
  141. if (d->__last_nonopt > d->optind)
  142. d->__last_nonopt = d->optind;
  143. if (d->__first_nonopt > d->optind)
  144. d->__first_nonopt = d->optind;
  145. if (d->__ordering == PERMUTE)
  146. {
  147. if (d->__first_nonopt != d->__last_nonopt && d->__last_nonopt != d->optind)
  148. exchange_a ((char **) argv, d);
  149. else if (d->__last_nonopt != d->optind)
  150. d->__first_nonopt = d->optind;
  151. while (d->optind < argc && (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0'))
  152. d->optind++;
  153. d->__last_nonopt = d->optind;
  154. }
  155. if (d->optind != argc && !strcmp(argv[d->optind], "--"))
  156. {
  157. d->optind++;
  158. if (d->__first_nonopt != d->__last_nonopt && d->__last_nonopt != d->optind)
  159. exchange_a((char **) argv, d);
  160. else if (d->__first_nonopt == d->__last_nonopt)
  161. d->__first_nonopt = d->optind;
  162. d->__last_nonopt = argc;
  163. d->optind = argc;
  164. }
  165. if (d->optind == argc)
  166. {
  167. if (d->__first_nonopt != d->__last_nonopt)
  168. d->optind = d->__first_nonopt;
  169. return -1;
  170. }
  171. if ((argv[d->optind][0] != '-' || argv[d->optind][1] == '\0'))
  172. {
  173. if (d->__ordering == REQUIRE_ORDER)
  174. return -1;
  175. d->optarg = argv[d->optind++];
  176. return 1;
  177. }
  178. d->__nextchar = (argv[d->optind] + 1 + (longopts != NULL && argv[d->optind][1] == '-'));
  179. }
  180. if (longopts != NULL && (argv[d->optind][1] == '-' || (long_only && (argv[d->optind][2] || !strchr(optstring, argv[d->optind][1])))))
  181. {
  182. char *nameend;
  183. unsigned int namelen;
  184. const struct option_a *p;
  185. const struct option_a *pfound = NULL;
  186. struct option_list
  187. {
  188. const struct option_a *p;
  189. struct option_list *next;
  190. } *ambig_list = NULL;
  191. int exact = 0;
  192. int indfound = -1;
  193. int option_index;
  194. for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++);
  195. namelen = (unsigned int)(nameend - d->__nextchar);
  196. for (p = longopts, option_index = 0; p->name; p++, option_index++)
  197. if (!strncmp(p->name, d->__nextchar, namelen))
  198. {
  199. if (namelen == (unsigned int)strlen(p->name))
  200. {
  201. pfound = p;
  202. indfound = option_index;
  203. exact = 1;
  204. break;
  205. }
  206. else if (pfound == NULL)
  207. {
  208. pfound = p;
  209. indfound = option_index;
  210. }
  211. else if (long_only || pfound->has_arg != p->has_arg || pfound->flag != p->flag || pfound->val != p->val)
  212. {
  213. struct option_list *newp = (struct option_list*)alloca(sizeof(*newp));
  214. newp->p = p;
  215. newp->next = ambig_list;
  216. ambig_list = newp;
  217. }
  218. }
  219. if (ambig_list != NULL && !exact)
  220. {
  221. if (print_errors)
  222. {
  223. struct option_list first;
  224. first.p = pfound;
  225. first.next = ambig_list;
  226. ambig_list = &first;
  227. fprintf (stderr, "%s: option '%s' is ambiguous; possibilities:", argv[0], argv[d->optind]);
  228. do
  229. {
  230. fprintf (stderr, " '--%s'", ambig_list->p->name);
  231. ambig_list = ambig_list->next;
  232. }
  233. while (ambig_list != NULL);
  234. fputc ('\n', stderr);
  235. }
  236. d->__nextchar += strlen(d->__nextchar);
  237. d->optind++;
  238. d->optopt = 0;
  239. return '?';
  240. }
  241. if (pfound != NULL)
  242. {
  243. option_index = indfound;
  244. d->optind++;
  245. if (*nameend)
  246. {
  247. if (pfound->has_arg)
  248. d->optarg = nameend + 1;
  249. else
  250. {
  251. if (print_errors)
  252. {
  253. if (argv[d->optind - 1][1] == '-')
  254. {
  255. fprintf(stderr, "%s: option '--%s' doesn't allow an argument\n",argv[0], pfound->name);
  256. }
  257. else
  258. {
  259. fprintf(stderr, "%s: option '%c%s' doesn't allow an argument\n",argv[0], argv[d->optind - 1][0],pfound->name);
  260. }
  261. }
  262. d->__nextchar += strlen(d->__nextchar);
  263. d->optopt = pfound->val;
  264. return '?';
  265. }
  266. }
  267. else if (pfound->has_arg == 1)
  268. {
  269. if (d->optind < argc)
  270. d->optarg = argv[d->optind++];
  271. else
  272. {
  273. if (print_errors)
  274. {
  275. fprintf(stderr,"%s: option '--%s' requires an argument\n",argv[0], pfound->name);
  276. }
  277. d->__nextchar += strlen(d->__nextchar);
  278. d->optopt = pfound->val;
  279. return optstring[0] == ':' ? ':' : '?';
  280. }
  281. }
  282. d->__nextchar += strlen(d->__nextchar);
  283. if (longind != NULL)
  284. *longind = option_index;
  285. if (pfound->flag)
  286. {
  287. *(pfound->flag) = pfound->val;
  288. return 0;
  289. }
  290. return pfound->val;
  291. }
  292. if (!long_only || argv[d->optind][1] == '-' || strchr(optstring, *d->__nextchar) == NULL)
  293. {
  294. if (print_errors)
  295. {
  296. if (argv[d->optind][1] == '-')
  297. {
  298. fprintf(stderr, "%s: unrecognized option '--%s'\n",argv[0], d->__nextchar);
  299. }
  300. else
  301. {
  302. fprintf(stderr, "%s: unrecognized option '%c%s'\n",argv[0], argv[d->optind][0], d->__nextchar);
  303. }
  304. }
  305. d->__nextchar = (char *)"";
  306. d->optind++;
  307. d->optopt = 0;
  308. return '?';
  309. }
  310. }
  311. {
  312. char c = *d->__nextchar++;
  313. char *temp = (char*)strchr(optstring, c);
  314. if (*d->__nextchar == '\0')
  315. ++d->optind;
  316. if (temp == NULL || c == ':' || c == ';')
  317. {
  318. if (print_errors)
  319. {
  320. fprintf(stderr, "%s: invalid option -- '%c'\n", argv[0], c);
  321. }
  322. d->optopt = c;
  323. return '?';
  324. }
  325. if (temp[0] == 'W' && temp[1] == ';')
  326. {
  327. char *nameend;
  328. const struct option_a *p;
  329. const struct option_a *pfound = NULL;
  330. int exact = 0;
  331. int ambig = 0;
  332. int indfound = 0;
  333. int option_index;
  334. if (longopts == NULL)
  335. goto no_longs;
  336. if (*d->__nextchar != '\0')
  337. {
  338. d->optarg = d->__nextchar;
  339. d->optind++;
  340. }
  341. else if (d->optind == argc)
  342. {
  343. if (print_errors)
  344. {
  345. fprintf(stderr,"%s: option requires an argument -- '%c'\n",argv[0], c);
  346. }
  347. d->optopt = c;
  348. if (optstring[0] == ':')
  349. c = ':';
  350. else
  351. c = '?';
  352. return c;
  353. }
  354. else
  355. d->optarg = argv[d->optind++];
  356. for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '='; nameend++);
  357. for (p = longopts, option_index = 0; p->name; p++, option_index++)
  358. if (!strncmp(p->name, d->__nextchar, nameend - d->__nextchar))
  359. {
  360. if ((unsigned int) (nameend - d->__nextchar) == strlen(p->name))
  361. {
  362. pfound = p;
  363. indfound = option_index;
  364. exact = 1;
  365. break;
  366. }
  367. else if (pfound == NULL)
  368. {
  369. pfound = p;
  370. indfound = option_index;
  371. }
  372. else if (long_only || pfound->has_arg != p->has_arg || pfound->flag != p->flag || pfound->val != p->val)
  373. ambig = 1;
  374. }
  375. if (ambig && !exact)
  376. {
  377. if (print_errors)
  378. {
  379. fprintf(stderr, "%s: option '-W %s' is ambiguous\n",argv[0], d->optarg);
  380. }
  381. d->__nextchar += strlen(d->__nextchar);
  382. d->optind++;
  383. return '?';
  384. }
  385. if (pfound != NULL)
  386. {
  387. option_index = indfound;
  388. if (*nameend)
  389. {
  390. if (pfound->has_arg)
  391. d->optarg = nameend + 1;
  392. else
  393. {
  394. if (print_errors)
  395. {
  396. fprintf(stderr, "%s: option '-W %s' doesn't allow an argument\n",argv[0], pfound->name);
  397. }
  398. d->__nextchar += strlen(d->__nextchar);
  399. return '?';
  400. }
  401. }
  402. else if (pfound->has_arg == 1)
  403. {
  404. if (d->optind < argc)
  405. d->optarg = argv[d->optind++];
  406. else
  407. {
  408. if (print_errors)
  409. {
  410. fprintf(stderr, "%s: option '-W %s' requires an argument\n",argv[0], pfound->name);
  411. }
  412. d->__nextchar += strlen(d->__nextchar);
  413. return optstring[0] == ':' ? ':' : '?';
  414. }
  415. }
  416. else
  417. d->optarg = NULL;
  418. d->__nextchar += strlen(d->__nextchar);
  419. if (longind != NULL)
  420. *longind = option_index;
  421. if (pfound->flag)
  422. {
  423. *(pfound->flag) = pfound->val;
  424. return 0;
  425. }
  426. return pfound->val;
  427. }
  428. no_longs:
  429. d->__nextchar = NULL;
  430. return 'W';
  431. }
  432. if (temp[1] == ':')
  433. {
  434. if (temp[2] == ':')
  435. {
  436. if (*d->__nextchar != '\0')
  437. {
  438. d->optarg = d->__nextchar;
  439. d->optind++;
  440. }
  441. else
  442. d->optarg = NULL;
  443. d->__nextchar = NULL;
  444. }
  445. else
  446. {
  447. if (*d->__nextchar != '\0')
  448. {
  449. d->optarg = d->__nextchar;
  450. d->optind++;
  451. }
  452. else if (d->optind == argc)
  453. {
  454. if (print_errors)
  455. {
  456. fprintf(stderr,"%s: option requires an argument -- '%c'\n",argv[0], c);
  457. }
  458. d->optopt = c;
  459. if (optstring[0] == ':')
  460. c = ':';
  461. else
  462. c = '?';
  463. }
  464. else
  465. d->optarg = argv[d->optind++];
  466. d->__nextchar = NULL;
  467. }
  468. }
  469. return c;
  470. }
  471. }
  472. 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)
  473. {
  474. int result;
  475. getopt_data_a.optind = optind;
  476. getopt_data_a.opterr = opterr;
  477. result = _getopt_internal_r_a (argc, argv, optstring, longopts,longind, long_only, &getopt_data_a,posixly_correct);
  478. optind = getopt_data_a.optind;
  479. optarg_a = getopt_data_a.optarg;
  480. optopt = getopt_data_a.optopt;
  481. return result;
  482. }
  483. int getopt_a (int argc, char *const *argv, const char *optstring) _GETOPT_THROW
  484. {
  485. return _getopt_internal_a (argc, argv, optstring, (const struct option_a *) 0, (int *) 0, 0, 0);
  486. }
  487. int getopt_long_a (int argc, char *const *argv, const char *options, const struct option_a *long_options, int *opt_index) _GETOPT_THROW
  488. {
  489. return _getopt_internal_a (argc, argv, options, long_options, opt_index, 0, 0);
  490. }
  491. int getopt_long_only_a (int argc, char *const *argv, const char *options, const struct option_a *long_options, int *opt_index) _GETOPT_THROW
  492. {
  493. return _getopt_internal_a (argc, argv, options, long_options, opt_index, 1, 0);
  494. }
  495. 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)
  496. {
  497. return _getopt_internal_r_a (argc, argv, options, long_options, opt_index,0, d, 0);
  498. }
  499. 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)
  500. {
  501. return _getopt_internal_r_a (argc, argv, options, long_options, opt_index, 1, d, 0);
  502. }