isl_arg.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  1. /*
  2. * Copyright 2008-2009 Katholieke Universiteit Leuven
  3. *
  4. * Use of this software is governed by the MIT license
  5. *
  6. * Written by Sven Verdoolaege, K.U.Leuven, Departement
  7. * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <isl/arg.h>
  13. #include <isl/ctx.h>
  14. #include <isl_config.h>
  15. static struct isl_arg help_arg[] = {
  16. ISL_ARG_PHANTOM_BOOL('h', "help", NULL, "print this help, then exit")
  17. { isl_arg_end }
  18. };
  19. static void set_default_choice(struct isl_arg *arg, void *opt)
  20. {
  21. if (arg->offset == ISL_ARG_OFFSET_NONE)
  22. return;
  23. *(unsigned *)(((char *)opt) + arg->offset) = arg->u.choice.default_value;
  24. }
  25. static void set_default_flags(struct isl_arg *arg, void *opt)
  26. {
  27. *(unsigned *)(((char *)opt) + arg->offset) = arg->u.flags.default_value;
  28. }
  29. static void set_default_bool(struct isl_arg *arg, void *opt)
  30. {
  31. if (arg->offset == ISL_ARG_OFFSET_NONE)
  32. return;
  33. *(unsigned *)(((char *)opt) + arg->offset) = arg->u.b.default_value;
  34. }
  35. static void set_default_child(struct isl_arg *arg, void *opt)
  36. {
  37. void *child;
  38. if (arg->offset == ISL_ARG_OFFSET_NONE)
  39. child = opt;
  40. else {
  41. child = calloc(1, arg->u.child.child->options_size);
  42. *(void **)(((char *)opt) + arg->offset) = child;
  43. }
  44. if (child)
  45. isl_args_set_defaults(arg->u.child.child, child);
  46. }
  47. static void set_default_user(struct isl_arg *arg, void *opt)
  48. {
  49. arg->u.user.init(((char *)opt) + arg->offset);
  50. }
  51. static void set_default_int(struct isl_arg *arg, void *opt)
  52. {
  53. *(int *)(((char *)opt) + arg->offset) = arg->u.i.default_value;
  54. }
  55. static void set_default_long(struct isl_arg *arg, void *opt)
  56. {
  57. *(long *)(((char *)opt) + arg->offset) = arg->u.l.default_value;
  58. }
  59. static void set_default_ulong(struct isl_arg *arg, void *opt)
  60. {
  61. *(unsigned long *)(((char *)opt) + arg->offset) = arg->u.ul.default_value;
  62. }
  63. static void set_default_str(struct isl_arg *arg, void *opt)
  64. {
  65. const char *str = NULL;
  66. if (arg->u.str.default_value)
  67. str = strdup(arg->u.str.default_value);
  68. *(const char **)(((char *)opt) + arg->offset) = str;
  69. }
  70. static void set_default_str_list(struct isl_arg *arg, void *opt)
  71. {
  72. *(const char ***)(((char *) opt) + arg->offset) = NULL;
  73. *(int *)(((char *) opt) + arg->u.str_list.offset_n) = 0;
  74. }
  75. void isl_args_set_defaults(struct isl_args *args, void *opt)
  76. {
  77. int i;
  78. for (i = 0; args->args[i].type != isl_arg_end; ++i) {
  79. switch (args->args[i].type) {
  80. case isl_arg_choice:
  81. set_default_choice(&args->args[i], opt);
  82. break;
  83. case isl_arg_flags:
  84. set_default_flags(&args->args[i], opt);
  85. break;
  86. case isl_arg_bool:
  87. set_default_bool(&args->args[i], opt);
  88. break;
  89. case isl_arg_child:
  90. set_default_child(&args->args[i], opt);
  91. break;
  92. case isl_arg_user:
  93. set_default_user(&args->args[i], opt);
  94. break;
  95. case isl_arg_int:
  96. set_default_int(&args->args[i], opt);
  97. break;
  98. case isl_arg_long:
  99. set_default_long(&args->args[i], opt);
  100. break;
  101. case isl_arg_ulong:
  102. set_default_ulong(&args->args[i], opt);
  103. break;
  104. case isl_arg_arg:
  105. case isl_arg_str:
  106. set_default_str(&args->args[i], opt);
  107. break;
  108. case isl_arg_str_list:
  109. set_default_str_list(&args->args[i], opt);
  110. break;
  111. case isl_arg_alias:
  112. case isl_arg_footer:
  113. case isl_arg_version:
  114. case isl_arg_end:
  115. break;
  116. }
  117. }
  118. }
  119. static void free_args(struct isl_arg *arg, void *opt);
  120. static void free_child(struct isl_arg *arg, void *opt)
  121. {
  122. if (arg->offset == ISL_ARG_OFFSET_NONE)
  123. free_args(arg->u.child.child->args, opt);
  124. else
  125. isl_args_free(arg->u.child.child,
  126. *(void **)(((char *)opt) + arg->offset));
  127. }
  128. static void free_str_list(struct isl_arg *arg, void *opt)
  129. {
  130. int i;
  131. int n = *(int *)(((char *) opt) + arg->u.str_list.offset_n);
  132. char **list = *(char ***)(((char *) opt) + arg->offset);
  133. for (i = 0; i < n; ++i)
  134. free(list[i]);
  135. free(list);
  136. }
  137. static void free_user(struct isl_arg *arg, void *opt)
  138. {
  139. if (arg->u.user.clear)
  140. arg->u.user.clear(((char *)opt) + arg->offset);
  141. }
  142. static void free_args(struct isl_arg *arg, void *opt)
  143. {
  144. int i;
  145. for (i = 0; arg[i].type != isl_arg_end; ++i) {
  146. switch (arg[i].type) {
  147. case isl_arg_child:
  148. free_child(&arg[i], opt);
  149. break;
  150. case isl_arg_arg:
  151. case isl_arg_str:
  152. free(*(char **)(((char *)opt) + arg[i].offset));
  153. break;
  154. case isl_arg_str_list:
  155. free_str_list(&arg[i], opt);
  156. break;
  157. case isl_arg_user:
  158. free_user(&arg[i], opt);
  159. break;
  160. case isl_arg_alias:
  161. case isl_arg_bool:
  162. case isl_arg_choice:
  163. case isl_arg_flags:
  164. case isl_arg_int:
  165. case isl_arg_long:
  166. case isl_arg_ulong:
  167. case isl_arg_version:
  168. case isl_arg_footer:
  169. case isl_arg_end:
  170. break;
  171. }
  172. }
  173. }
  174. void isl_args_free(struct isl_args *args, void *opt)
  175. {
  176. if (!opt)
  177. return;
  178. free_args(args->args, opt);
  179. free(opt);
  180. }
  181. /* Data structure for collecting the prefixes of ancestor nodes.
  182. *
  183. * n is the number of prefixes.
  184. * prefix[i] for i < n is a prefix of an ancestor.
  185. * len[i] for i < n is the length of prefix[i].
  186. */
  187. struct isl_prefixes {
  188. int n;
  189. const char *prefix[10];
  190. size_t len[10];
  191. };
  192. /* Add "prefix" to the list of prefixes and return the updated
  193. * number of prefixes.
  194. */
  195. static int add_prefix(struct isl_prefixes *prefixes, const char *prefix)
  196. {
  197. int n = prefixes->n;
  198. if (!prefix)
  199. return n;
  200. if (prefixes->n >= 10) {
  201. fprintf(stderr, "too many prefixes\n");
  202. exit(EXIT_FAILURE);
  203. }
  204. prefixes->len[prefixes->n] = strlen(prefix);
  205. prefixes->prefix[prefixes->n] = prefix;
  206. prefixes->n++;
  207. return n;
  208. }
  209. /* Drop all prefixes starting at "first".
  210. */
  211. static void drop_prefix(struct isl_prefixes *prefixes, int first)
  212. {
  213. prefixes->n = first;
  214. }
  215. /* Print the prefixes in "prefixes".
  216. */
  217. static int print_prefixes(struct isl_prefixes *prefixes)
  218. {
  219. int i;
  220. int len = 0;
  221. if (!prefixes)
  222. return 0;
  223. for (i = 0; i < prefixes->n; ++i) {
  224. printf("%s-", prefixes->prefix[i]);
  225. len += strlen(prefixes->prefix[i]) + 1;
  226. }
  227. return len;
  228. }
  229. /* Check if "name" starts with one or more of the prefixes in "prefixes",
  230. * starting at *first. If so, advance the pointer beyond the prefixes
  231. * and return the updated pointer. Additionally, update *first to
  232. * the index after the last prefix found.
  233. */
  234. static const char *skip_prefixes(const char *name,
  235. struct isl_prefixes *prefixes, int *first)
  236. {
  237. int i;
  238. for (i = first ? *first : 0; i < prefixes->n; ++i) {
  239. size_t len = prefixes->len[i];
  240. const char *prefix = prefixes->prefix[i];
  241. if (strncmp(name, prefix, len) == 0 && name[len] == '-') {
  242. name += len + 1;
  243. if (first)
  244. *first = i + 1;
  245. }
  246. }
  247. return name;
  248. }
  249. static int print_arg_help(struct isl_arg *decl, struct isl_prefixes *prefixes,
  250. int no)
  251. {
  252. int len = 0;
  253. if (!decl->long_name) {
  254. printf(" -%c", decl->short_name);
  255. return 4;
  256. }
  257. if (decl->short_name) {
  258. printf(" -%c, --", decl->short_name);
  259. len += 8;
  260. } else if (decl->flags & ISL_ARG_SINGLE_DASH) {
  261. printf(" -");
  262. len += 3;
  263. } else {
  264. printf(" --");
  265. len += 8;
  266. }
  267. if (no) {
  268. printf("no-");
  269. len += 3;
  270. }
  271. len += print_prefixes(prefixes);
  272. printf("%s", decl->long_name);
  273. len += strlen(decl->long_name);
  274. while ((++decl)->type == isl_arg_alias) {
  275. printf(", --");
  276. len += 4;
  277. if (no) {
  278. printf("no-");
  279. len += 3;
  280. }
  281. printf("%s", decl->long_name);
  282. len += strlen(decl->long_name);
  283. }
  284. return len;
  285. }
  286. const void *isl_memrchr(const void *s, int c, size_t n)
  287. {
  288. const char *p = s;
  289. while (n-- > 0)
  290. if (p[n] == c)
  291. return p + n;
  292. return NULL;
  293. }
  294. static int wrap_msg(const char *s, int indent, int pos)
  295. {
  296. int len;
  297. int wrap_len = 75 - indent;
  298. if (pos + 1 >= indent)
  299. printf("\n%*s", indent, "");
  300. else
  301. printf("%*s", indent - pos, "");
  302. len = strlen(s);
  303. while (len > wrap_len) {
  304. const char *space = isl_memrchr(s, ' ', wrap_len);
  305. int l;
  306. if (!space)
  307. space = strchr(s + wrap_len, ' ');
  308. if (!space)
  309. break;
  310. l = space - s;
  311. printf("%.*s", l, s);
  312. s = space + 1;
  313. len -= l + 1;
  314. printf("\n%*s", indent, "");
  315. }
  316. printf("%s", s);
  317. return len;
  318. }
  319. static int print_help_msg(struct isl_arg *decl, int pos)
  320. {
  321. if (!decl->help_msg)
  322. return pos;
  323. return wrap_msg(decl->help_msg, 30, pos);
  324. }
  325. static void print_default(struct isl_arg *decl, const char *def, int pos)
  326. {
  327. const char *default_prefix = "[default: ";
  328. const char *default_suffix = "]";
  329. int len;
  330. len = strlen(default_prefix) + strlen(def) + strlen(default_suffix);
  331. if (!decl->help_msg) {
  332. if (pos >= 29)
  333. printf("\n%30s", "");
  334. else
  335. printf("%*s", 30 - pos, "");
  336. } else {
  337. if (pos + len >= 48)
  338. printf("\n%30s", "");
  339. else
  340. printf(" ");
  341. }
  342. printf("%s%s%s", default_prefix, def, default_suffix);
  343. }
  344. static void print_default_choice(struct isl_arg *decl, void *opt, int pos)
  345. {
  346. int i;
  347. const char *s = "none";
  348. unsigned *p;
  349. p = (unsigned *)(((char *) opt) + decl->offset);
  350. for (i = 0; decl->u.choice.choice[i].name; ++i)
  351. if (decl->u.choice.choice[i].value == *p) {
  352. s = decl->u.choice.choice[i].name;
  353. break;
  354. }
  355. print_default(decl, s, pos);
  356. }
  357. static void print_choice_help(struct isl_arg *decl,
  358. struct isl_prefixes *prefixes, void *opt)
  359. {
  360. int i;
  361. int pos;
  362. pos = print_arg_help(decl, prefixes, 0);
  363. printf("=");
  364. pos++;
  365. for (i = 0; decl->u.choice.choice[i].name; ++i) {
  366. if (i) {
  367. printf("|");
  368. pos++;
  369. }
  370. printf("%s", decl->u.choice.choice[i].name);
  371. pos += strlen(decl->u.choice.choice[i].name);
  372. }
  373. pos = print_help_msg(decl, pos);
  374. print_default_choice(decl, opt, pos);
  375. printf("\n");
  376. }
  377. static void print_default_flags(struct isl_arg *decl, void *opt, int pos)
  378. {
  379. int i, first;
  380. const char *default_prefix = "[default: ";
  381. const char *default_suffix = "]";
  382. int len = strlen(default_prefix) + strlen(default_suffix);
  383. unsigned *p;
  384. p = (unsigned *)(((char *) opt) + decl->offset);
  385. for (i = 0; decl->u.flags.flags[i].name; ++i)
  386. if ((*p & decl->u.flags.flags[i].mask) ==
  387. decl->u.flags.flags[i].value)
  388. len += strlen(decl->u.flags.flags[i].name);
  389. if (!decl->help_msg) {
  390. if (pos >= 29)
  391. printf("\n%30s", "");
  392. else
  393. printf("%*s", 30 - pos, "");
  394. } else {
  395. if (pos + len >= 48)
  396. printf("\n%30s", "");
  397. else
  398. printf(" ");
  399. }
  400. printf("%s", default_prefix);
  401. for (first = 1, i = 0; decl->u.flags.flags[i].name; ++i)
  402. if ((*p & decl->u.flags.flags[i].mask) ==
  403. decl->u.flags.flags[i].value) {
  404. if (!first)
  405. printf(",");
  406. printf("%s", decl->u.flags.flags[i].name);
  407. first = 0;
  408. }
  409. printf("%s", default_suffix);
  410. }
  411. static void print_flags_help(struct isl_arg *decl,
  412. struct isl_prefixes *prefixes, void *opt)
  413. {
  414. int i, j;
  415. int pos;
  416. pos = print_arg_help(decl, prefixes, 0);
  417. printf("=");
  418. pos++;
  419. for (i = 0; decl->u.flags.flags[i].name; ++i) {
  420. if (i) {
  421. printf(",");
  422. pos++;
  423. }
  424. for (j = i;
  425. decl->u.flags.flags[j].mask == decl->u.flags.flags[i].mask;
  426. ++j) {
  427. if (j != i) {
  428. printf("|");
  429. pos++;
  430. }
  431. printf("%s", decl->u.flags.flags[j].name);
  432. pos += strlen(decl->u.flags.flags[j].name);
  433. }
  434. i = j - 1;
  435. }
  436. pos = print_help_msg(decl, pos);
  437. print_default_flags(decl, opt, pos);
  438. printf("\n");
  439. }
  440. static void print_bool_help(struct isl_arg *decl,
  441. struct isl_prefixes *prefixes, void *opt)
  442. {
  443. int pos;
  444. unsigned *p = opt ? (unsigned *)(((char *) opt) + decl->offset) : NULL;
  445. int no = p ? *p == 1 : 0;
  446. pos = print_arg_help(decl, prefixes, no);
  447. pos = print_help_msg(decl, pos);
  448. if (decl->offset != ISL_ARG_OFFSET_NONE)
  449. print_default(decl, no ? "yes" : "no", pos);
  450. printf("\n");
  451. }
  452. static int print_argument_name(struct isl_arg *decl, const char *name, int pos)
  453. {
  454. printf("%c<%s>", decl->long_name ? '=' : ' ', name);
  455. return pos + 3 + strlen(name);
  456. }
  457. static void print_int_help(struct isl_arg *decl,
  458. struct isl_prefixes *prefixes, void *opt)
  459. {
  460. int pos;
  461. char val[20];
  462. int *p = (int *)(((char *) opt) + decl->offset);
  463. pos = print_arg_help(decl, prefixes, 0);
  464. pos = print_argument_name(decl, decl->argument_name, pos);
  465. pos = print_help_msg(decl, pos);
  466. snprintf(val, sizeof(val), "%d", *p);
  467. print_default(decl, val, pos);
  468. printf("\n");
  469. }
  470. static void print_long_help(struct isl_arg *decl,
  471. struct isl_prefixes *prefixes, void *opt)
  472. {
  473. int pos;
  474. long *p = (long *)(((char *) opt) + decl->offset);
  475. pos = print_arg_help(decl, prefixes, 0);
  476. if (*p != decl->u.l.default_selected) {
  477. printf("[");
  478. pos++;
  479. }
  480. printf("=long");
  481. pos += 5;
  482. if (*p != decl->u.l.default_selected) {
  483. printf("]");
  484. pos++;
  485. }
  486. print_help_msg(decl, pos);
  487. printf("\n");
  488. }
  489. static void print_ulong_help(struct isl_arg *decl,
  490. struct isl_prefixes *prefixes)
  491. {
  492. int pos;
  493. pos = print_arg_help(decl, prefixes, 0);
  494. printf("=ulong");
  495. pos += 6;
  496. print_help_msg(decl, pos);
  497. printf("\n");
  498. }
  499. static void print_str_help(struct isl_arg *decl,
  500. struct isl_prefixes *prefixes, void *opt)
  501. {
  502. int pos;
  503. const char *a = decl->argument_name ? decl->argument_name : "string";
  504. const char **p = (const char **)(((char *) opt) + decl->offset);
  505. pos = print_arg_help(decl, prefixes, 0);
  506. pos = print_argument_name(decl, a, pos);
  507. pos = print_help_msg(decl, pos);
  508. if (*p)
  509. print_default(decl, *p, pos);
  510. printf("\n");
  511. }
  512. static void print_str_list_help(struct isl_arg *decl,
  513. struct isl_prefixes *prefixes)
  514. {
  515. int pos;
  516. const char *a = decl->argument_name ? decl->argument_name : "string";
  517. pos = print_arg_help(decl, prefixes, 0);
  518. pos = print_argument_name(decl, a, pos);
  519. pos = print_help_msg(decl, pos);
  520. printf("\n");
  521. }
  522. static void print_help(struct isl_arg *arg,
  523. struct isl_prefixes *prefixes, void *opt)
  524. {
  525. int i;
  526. int any = 0;
  527. for (i = 0; arg[i].type != isl_arg_end; ++i) {
  528. if (arg[i].flags & ISL_ARG_HIDDEN)
  529. continue;
  530. switch (arg[i].type) {
  531. case isl_arg_flags:
  532. print_flags_help(&arg[i], prefixes, opt);
  533. any = 1;
  534. break;
  535. case isl_arg_choice:
  536. print_choice_help(&arg[i], prefixes, opt);
  537. any = 1;
  538. break;
  539. case isl_arg_bool:
  540. print_bool_help(&arg[i], prefixes, opt);
  541. any = 1;
  542. break;
  543. case isl_arg_int:
  544. print_int_help(&arg[i], prefixes, opt);
  545. any = 1;
  546. break;
  547. case isl_arg_long:
  548. print_long_help(&arg[i], prefixes, opt);
  549. any = 1;
  550. break;
  551. case isl_arg_ulong:
  552. print_ulong_help(&arg[i], prefixes);
  553. any = 1;
  554. break;
  555. case isl_arg_str:
  556. print_str_help(&arg[i], prefixes, opt);
  557. any = 1;
  558. break;
  559. case isl_arg_str_list:
  560. print_str_list_help(&arg[i], prefixes);
  561. any = 1;
  562. break;
  563. case isl_arg_alias:
  564. case isl_arg_version:
  565. case isl_arg_arg:
  566. case isl_arg_footer:
  567. case isl_arg_child:
  568. case isl_arg_user:
  569. case isl_arg_end:
  570. break;
  571. }
  572. }
  573. for (i = 0; arg[i].type != isl_arg_end; ++i) {
  574. void *child;
  575. int first;
  576. if (arg[i].type != isl_arg_child)
  577. continue;
  578. if (arg[i].flags & ISL_ARG_HIDDEN)
  579. continue;
  580. if (any)
  581. printf("\n");
  582. if (arg[i].help_msg)
  583. printf(" %s\n", arg[i].help_msg);
  584. if (arg[i].offset == ISL_ARG_OFFSET_NONE)
  585. child = opt;
  586. else
  587. child = *(void **)(((char *) opt) + arg[i].offset);
  588. first = add_prefix(prefixes, arg[i].long_name);
  589. print_help(arg[i].u.child.child->args, prefixes, child);
  590. drop_prefix(prefixes, first);
  591. any = 1;
  592. }
  593. }
  594. static const char *prog_name(const char *prog)
  595. {
  596. const char *slash;
  597. slash = strrchr(prog, '/');
  598. if (slash)
  599. prog = slash + 1;
  600. if (strncmp(prog, "lt-", 3) == 0)
  601. prog += 3;
  602. return prog;
  603. }
  604. static int any_version(struct isl_arg *decl)
  605. {
  606. int i;
  607. for (i = 0; decl[i].type != isl_arg_end; ++i) {
  608. switch (decl[i].type) {
  609. case isl_arg_version:
  610. return 1;
  611. case isl_arg_child:
  612. if (any_version(decl[i].u.child.child->args))
  613. return 1;
  614. break;
  615. default:
  616. break;
  617. }
  618. }
  619. return 0;
  620. }
  621. static void print_help_and_exit(struct isl_arg *arg, const char *prog,
  622. void *opt)
  623. {
  624. int i;
  625. struct isl_prefixes prefixes = { 0 };
  626. printf("Usage: %s [OPTION...]", prog_name(prog));
  627. for (i = 0; arg[i].type != isl_arg_end; ++i)
  628. if (arg[i].type == isl_arg_arg)
  629. printf(" %s", arg[i].argument_name);
  630. printf("\n\n");
  631. print_help(arg, &prefixes, opt);
  632. printf("\n");
  633. if (any_version(arg))
  634. printf(" -V, --version\n");
  635. print_bool_help(help_arg, NULL, NULL);
  636. for (i = 0; arg[i].type != isl_arg_end; ++i) {
  637. if (arg[i].type != isl_arg_footer)
  638. continue;
  639. wrap_msg(arg[i].help_msg, 0, 0);
  640. printf("\n");
  641. }
  642. exit(0);
  643. }
  644. static int match_long_name(struct isl_arg *decl,
  645. const char *start, const char *end)
  646. {
  647. do {
  648. if (end - start == strlen(decl->long_name) &&
  649. !strncmp(start, decl->long_name, end - start))
  650. return 1;
  651. } while ((++decl)->type == isl_arg_alias);
  652. return 0;
  653. }
  654. static const char *skip_dash_dash(struct isl_arg *decl, const char *arg)
  655. {
  656. if (!strncmp(arg, "--", 2))
  657. return arg + 2;
  658. if ((decl->flags & ISL_ARG_SINGLE_DASH) && arg[0] == '-')
  659. return arg + 1;
  660. return NULL;
  661. }
  662. static const char *skip_name(struct isl_arg *decl, const char *arg,
  663. struct isl_prefixes *prefixes, int need_argument, int *has_argument)
  664. {
  665. const char *equal;
  666. const char *name;
  667. const char *end;
  668. if (arg[0] == '-' && arg[1] && arg[1] == decl->short_name) {
  669. if (need_argument && !arg[2])
  670. return NULL;
  671. if (has_argument)
  672. *has_argument = arg[2] != '\0';
  673. return arg + 2;
  674. }
  675. if (!decl->long_name)
  676. return NULL;
  677. name = skip_dash_dash(decl, arg);
  678. if (!name)
  679. return NULL;
  680. equal = strchr(name, '=');
  681. if (need_argument && !equal)
  682. return NULL;
  683. if (has_argument)
  684. *has_argument = !!equal;
  685. end = equal ? equal : name + strlen(name);
  686. name = skip_prefixes(name, prefixes, NULL);
  687. if (!match_long_name(decl, name, end))
  688. return NULL;
  689. return equal ? equal + 1 : end;
  690. }
  691. static int parse_choice_option(struct isl_arg *decl, char **arg,
  692. struct isl_prefixes *prefixes, void *opt)
  693. {
  694. int i;
  695. int has_argument;
  696. const char *choice;
  697. choice = skip_name(decl, arg[0], prefixes, 0, &has_argument);
  698. if (!choice)
  699. return 0;
  700. if (!has_argument && (!arg[1] || arg[1][0] == '-')) {
  701. unsigned u = decl->u.choice.default_selected;
  702. if (decl->offset != ISL_ARG_OFFSET_NONE)
  703. *(unsigned *)(((char *)opt) + decl->offset) = u;
  704. if (decl->u.choice.set)
  705. decl->u.choice.set(opt, u);
  706. return 1;
  707. }
  708. if (!has_argument)
  709. choice = arg[1];
  710. for (i = 0; decl->u.choice.choice[i].name; ++i) {
  711. unsigned u;
  712. if (strcmp(choice, decl->u.choice.choice[i].name))
  713. continue;
  714. u = decl->u.choice.choice[i].value;
  715. if (decl->offset != ISL_ARG_OFFSET_NONE)
  716. *(unsigned *)(((char *)opt) + decl->offset) = u;
  717. if (decl->u.choice.set)
  718. decl->u.choice.set(opt, u);
  719. return has_argument ? 1 : 2;
  720. }
  721. return 0;
  722. }
  723. static int set_flag(struct isl_arg *decl, unsigned *val, const char *flag,
  724. size_t len)
  725. {
  726. int i;
  727. for (i = 0; decl->u.flags.flags[i].name; ++i) {
  728. if (strncmp(flag, decl->u.flags.flags[i].name, len))
  729. continue;
  730. *val &= ~decl->u.flags.flags[i].mask;
  731. *val |= decl->u.flags.flags[i].value;
  732. return 1;
  733. }
  734. return 0;
  735. }
  736. static int parse_flags_option(struct isl_arg *decl, char **arg,
  737. struct isl_prefixes *prefixes, void *opt)
  738. {
  739. int has_argument;
  740. const char *flags;
  741. const char *comma;
  742. unsigned val;
  743. flags = skip_name(decl, arg[0], prefixes, 0, &has_argument);
  744. if (!flags)
  745. return 0;
  746. if (!has_argument && !arg[1])
  747. return 0;
  748. if (!has_argument)
  749. flags = arg[1];
  750. val = 0;
  751. while ((comma = strchr(flags, ',')) != NULL) {
  752. if (!set_flag(decl, &val, flags, comma - flags))
  753. return 0;
  754. flags = comma + 1;
  755. }
  756. if (!set_flag(decl, &val, flags, strlen(flags)))
  757. return 0;
  758. *(unsigned *)(((char *)opt) + decl->offset) = val;
  759. return has_argument ? 1 : 2;
  760. }
  761. static int parse_bool_option(struct isl_arg *decl, char **arg,
  762. struct isl_prefixes *prefixes, void *opt)
  763. {
  764. const char *name;
  765. unsigned *p = (unsigned *)(((char *)opt) + decl->offset);
  766. int next_prefix;
  767. if (skip_name(decl, arg[0], prefixes, 0, NULL)) {
  768. if ((decl->flags & ISL_ARG_BOOL_ARG) && arg[1]) {
  769. char *endptr;
  770. int val = strtol(arg[1], &endptr, 0);
  771. if (*endptr == '\0' && (val == 0 || val == 1)) {
  772. if (decl->offset != ISL_ARG_OFFSET_NONE)
  773. *p = val;
  774. if (decl->u.b.set)
  775. decl->u.b.set(opt, val);
  776. return 2;
  777. }
  778. }
  779. if (decl->offset != ISL_ARG_OFFSET_NONE)
  780. *p = 1;
  781. if (decl->u.b.set)
  782. decl->u.b.set(opt, 1);
  783. return 1;
  784. }
  785. if (!decl->long_name)
  786. return 0;
  787. name = skip_dash_dash(decl, arg[0]);
  788. if (!name)
  789. return 0;
  790. next_prefix = 0;
  791. name = skip_prefixes(name, prefixes, &next_prefix);
  792. if (strncmp(name, "no-", 3))
  793. return 0;
  794. name += 3;
  795. name = skip_prefixes(name, prefixes, &next_prefix);
  796. if (match_long_name(decl, name, name + strlen(name))) {
  797. if (decl->offset != ISL_ARG_OFFSET_NONE)
  798. *p = 0;
  799. if (decl->u.b.set)
  800. decl->u.b.set(opt, 0);
  801. return 1;
  802. }
  803. return 0;
  804. }
  805. static int parse_str_option(struct isl_arg *decl, char **arg,
  806. struct isl_prefixes *prefixes, void *opt)
  807. {
  808. int has_argument;
  809. const char *s;
  810. char **p = (char **)(((char *)opt) + decl->offset);
  811. s = skip_name(decl, arg[0], prefixes, 0, &has_argument);
  812. if (!s)
  813. return 0;
  814. if (has_argument) {
  815. free(*p);
  816. *p = strdup(s);
  817. return 1;
  818. }
  819. if (arg[1]) {
  820. free(*p);
  821. *p = strdup(arg[1]);
  822. return 2;
  823. }
  824. return 0;
  825. }
  826. static int isl_arg_str_list_append(struct isl_arg *decl, void *opt,
  827. const char *s)
  828. {
  829. int *n = (int *)(((char *) opt) + decl->u.str_list.offset_n);
  830. char **list = *(char ***)(((char *) opt) + decl->offset);
  831. list = realloc(list, (*n + 1) * sizeof(char *));
  832. if (!list)
  833. return -1;
  834. *(char ***)(((char *) opt) + decl->offset) = list;
  835. list[*n] = strdup(s);
  836. (*n)++;
  837. return 0;
  838. }
  839. static int parse_str_list_option(struct isl_arg *decl, char **arg,
  840. struct isl_prefixes *prefixes, void *opt)
  841. {
  842. int has_argument;
  843. const char *s;
  844. s = skip_name(decl, arg[0], prefixes, 0, &has_argument);
  845. if (!s)
  846. return 0;
  847. if (has_argument) {
  848. isl_arg_str_list_append(decl, opt, s);
  849. return 1;
  850. }
  851. if (arg[1]) {
  852. isl_arg_str_list_append(decl, opt, arg[1]);
  853. return 2;
  854. }
  855. return 0;
  856. }
  857. static int parse_int_option(struct isl_arg *decl, char **arg,
  858. struct isl_prefixes *prefixes, void *opt)
  859. {
  860. int has_argument;
  861. const char *val;
  862. char *endptr;
  863. int *p = (int *)(((char *)opt) + decl->offset);
  864. val = skip_name(decl, arg[0], prefixes, 0, &has_argument);
  865. if (!val)
  866. return 0;
  867. if (has_argument) {
  868. *p = atoi(val);
  869. return 1;
  870. }
  871. if (arg[1]) {
  872. int i = strtol(arg[1], &endptr, 0);
  873. if (*endptr == '\0') {
  874. *p = i;
  875. return 2;
  876. }
  877. }
  878. return 0;
  879. }
  880. static int parse_long_option(struct isl_arg *decl, char **arg,
  881. struct isl_prefixes *prefixes, void *opt)
  882. {
  883. int has_argument;
  884. const char *val;
  885. char *endptr;
  886. long *p = (long *)(((char *)opt) + decl->offset);
  887. val = skip_name(decl, arg[0], prefixes, 0, &has_argument);
  888. if (!val)
  889. return 0;
  890. if (has_argument) {
  891. long l = strtol(val, NULL, 0);
  892. *p = l;
  893. if (decl->u.l.set)
  894. decl->u.l.set(opt, l);
  895. return 1;
  896. }
  897. if (arg[1]) {
  898. long l = strtol(arg[1], &endptr, 0);
  899. if (*endptr == '\0') {
  900. *p = l;
  901. if (decl->u.l.set)
  902. decl->u.l.set(opt, l);
  903. return 2;
  904. }
  905. }
  906. if (decl->u.l.default_value != decl->u.l.default_selected) {
  907. *p = decl->u.l.default_selected;
  908. if (decl->u.l.set)
  909. decl->u.l.set(opt, decl->u.l.default_selected);
  910. return 1;
  911. }
  912. return 0;
  913. }
  914. static int parse_ulong_option(struct isl_arg *decl, char **arg,
  915. struct isl_prefixes *prefixes, void *opt)
  916. {
  917. int has_argument;
  918. const char *val;
  919. char *endptr;
  920. unsigned long *p = (unsigned long *)(((char *)opt) + decl->offset);
  921. val = skip_name(decl, arg[0], prefixes, 0, &has_argument);
  922. if (!val)
  923. return 0;
  924. if (has_argument) {
  925. *p = strtoul(val, NULL, 0);
  926. return 1;
  927. }
  928. if (arg[1]) {
  929. unsigned long ul = strtoul(arg[1], &endptr, 0);
  930. if (*endptr == '\0') {
  931. *p = ul;
  932. return 2;
  933. }
  934. }
  935. return 0;
  936. }
  937. static int parse_option(struct isl_arg *decl, char **arg,
  938. struct isl_prefixes *prefixes, void *opt);
  939. static int parse_child_option(struct isl_arg *decl, char **arg,
  940. struct isl_prefixes *prefixes, void *opt)
  941. {
  942. void *child;
  943. int first, parsed;
  944. if (decl->offset == ISL_ARG_OFFSET_NONE)
  945. child = opt;
  946. else
  947. child = *(void **)(((char *)opt) + decl->offset);
  948. first = add_prefix(prefixes, decl->long_name);
  949. parsed = parse_option(decl->u.child.child->args, arg, prefixes, child);
  950. drop_prefix(prefixes, first);
  951. return parsed;
  952. }
  953. static int parse_option(struct isl_arg *decl, char **arg,
  954. struct isl_prefixes *prefixes, void *opt)
  955. {
  956. int i;
  957. for (i = 0; decl[i].type != isl_arg_end; ++i) {
  958. int parsed = 0;
  959. switch (decl[i].type) {
  960. case isl_arg_choice:
  961. parsed = parse_choice_option(&decl[i], arg,
  962. prefixes, opt);
  963. break;
  964. case isl_arg_flags:
  965. parsed = parse_flags_option(&decl[i], arg,
  966. prefixes, opt);
  967. break;
  968. case isl_arg_int:
  969. parsed = parse_int_option(&decl[i], arg, prefixes, opt);
  970. break;
  971. case isl_arg_long:
  972. parsed = parse_long_option(&decl[i], arg,
  973. prefixes, opt);
  974. break;
  975. case isl_arg_ulong:
  976. parsed = parse_ulong_option(&decl[i], arg,
  977. prefixes, opt);
  978. break;
  979. case isl_arg_bool:
  980. parsed = parse_bool_option(&decl[i], arg,
  981. prefixes, opt);
  982. break;
  983. case isl_arg_str:
  984. parsed = parse_str_option(&decl[i], arg, prefixes, opt);
  985. break;
  986. case isl_arg_str_list:
  987. parsed = parse_str_list_option(&decl[i], arg, prefixes,
  988. opt);
  989. break;
  990. case isl_arg_child:
  991. parsed = parse_child_option(&decl[i], arg,
  992. prefixes, opt);
  993. break;
  994. case isl_arg_alias:
  995. case isl_arg_arg:
  996. case isl_arg_footer:
  997. case isl_arg_user:
  998. case isl_arg_version:
  999. case isl_arg_end:
  1000. break;
  1001. }
  1002. if (parsed)
  1003. return parsed;
  1004. }
  1005. return 0;
  1006. }
  1007. static void print_version(struct isl_arg *decl)
  1008. {
  1009. int i;
  1010. for (i = 0; decl[i].type != isl_arg_end; ++i) {
  1011. switch (decl[i].type) {
  1012. case isl_arg_version:
  1013. decl[i].u.version.print_version();
  1014. break;
  1015. case isl_arg_child:
  1016. print_version(decl[i].u.child.child->args);
  1017. break;
  1018. default:
  1019. break;
  1020. }
  1021. }
  1022. }
  1023. static void print_version_and_exit(struct isl_arg *decl)
  1024. {
  1025. print_version(decl);
  1026. exit(0);
  1027. }
  1028. static int drop_argument(int argc, char **argv, int drop, int n)
  1029. {
  1030. for (; drop + n < argc; ++drop)
  1031. argv[drop] = argv[drop + n];
  1032. return argc - n;
  1033. }
  1034. static int n_arg(struct isl_arg *arg)
  1035. {
  1036. int i;
  1037. int n_arg = 0;
  1038. for (i = 0; arg[i].type != isl_arg_end; ++i)
  1039. if (arg[i].type == isl_arg_arg)
  1040. n_arg++;
  1041. return n_arg;
  1042. }
  1043. static int next_arg(struct isl_arg *arg, int a)
  1044. {
  1045. for (++a; arg[a].type != isl_arg_end; ++a)
  1046. if (arg[a].type == isl_arg_arg)
  1047. return a;
  1048. return -1;
  1049. }
  1050. /* Unless ISL_ARG_SKIP_HELP is set, check if "arg" is
  1051. * equal to "--help" or "-h" and if so call print_help_and_exit.
  1052. */
  1053. static void check_help(struct isl_args *args, char *arg, char *prog, void *opt,
  1054. unsigned flags)
  1055. {
  1056. if (ISL_FL_ISSET(flags, ISL_ARG_SKIP_HELP))
  1057. return;
  1058. if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0)
  1059. print_help_and_exit(args->args, prog, opt);
  1060. }
  1061. int isl_args_parse(struct isl_args *args, int argc, char **argv, void *opt,
  1062. unsigned flags)
  1063. {
  1064. int a = -1;
  1065. int skip = 0;
  1066. int i;
  1067. int n;
  1068. struct isl_prefixes prefixes = { 0 };
  1069. n = n_arg(args->args);
  1070. for (i = 1; i < argc; ++i) {
  1071. if ((strcmp(argv[i], "--version") == 0 ||
  1072. strcmp(argv[i], "-V") == 0) && any_version(args->args))
  1073. print_version_and_exit(args->args);
  1074. }
  1075. while (argc > 1 + skip) {
  1076. int parsed;
  1077. if (argv[1 + skip][0] != '-') {
  1078. a = next_arg(args->args, a);
  1079. if (a >= 0) {
  1080. char **p;
  1081. p = (char **)(((char *)opt)+args->args[a].offset);
  1082. free(*p);
  1083. *p = strdup(argv[1 + skip]);
  1084. argc = drop_argument(argc, argv, 1 + skip, 1);
  1085. --n;
  1086. } else if (ISL_FL_ISSET(flags, ISL_ARG_ALL)) {
  1087. fprintf(stderr, "%s: extra argument: %s\n",
  1088. prog_name(argv[0]), argv[1 + skip]);
  1089. exit(-1);
  1090. } else
  1091. ++skip;
  1092. continue;
  1093. }
  1094. check_help(args, argv[1 + skip], argv[0], opt, flags);
  1095. parsed = parse_option(args->args, &argv[1 + skip],
  1096. &prefixes, opt);
  1097. if (parsed)
  1098. argc = drop_argument(argc, argv, 1 + skip, parsed);
  1099. else if (ISL_FL_ISSET(flags, ISL_ARG_ALL)) {
  1100. fprintf(stderr, "%s: unrecognized option: %s\n",
  1101. prog_name(argv[0]), argv[1 + skip]);
  1102. exit(-1);
  1103. } else
  1104. ++skip;
  1105. }
  1106. if (n > 0) {
  1107. fprintf(stderr, "%s: expecting %d more argument(s)\n",
  1108. prog_name(argv[0]), n);
  1109. exit(-1);
  1110. }
  1111. return argc;
  1112. }