arg.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /* arg.c - argument parser */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2003,2004,2005,2007,2008 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/misc.h>
  20. #include <grub/mm.h>
  21. #include <grub/err.h>
  22. #include <grub/term.h>
  23. #include <grub/extcmd.h>
  24. #include <grub/i18n.h>
  25. #include <grub/safemath.h>
  26. /* Built-in parser for default options. */
  27. static const struct grub_arg_option help_options[] =
  28. {
  29. {"help", 0, 0,
  30. N_("Display this help and exit."), 0, ARG_TYPE_NONE},
  31. {"usage", 0, 0,
  32. N_("Display the usage of this command and exit."), 0, ARG_TYPE_NONE},
  33. {0, 0, 0, 0, 0, 0}
  34. };
  35. /* Helper for find_short. */
  36. static const struct grub_arg_option *
  37. fnd_short (const struct grub_arg_option *opt, char c)
  38. {
  39. while (opt->doc)
  40. {
  41. if (opt->shortarg == c)
  42. return opt;
  43. opt++;
  44. }
  45. return 0;
  46. }
  47. static const struct grub_arg_option *
  48. find_short (const struct grub_arg_option *options, char c)
  49. {
  50. const struct grub_arg_option *found = 0;
  51. if (options)
  52. found = fnd_short (options, c);
  53. if (! found)
  54. {
  55. switch (c)
  56. {
  57. case 'h':
  58. found = help_options;
  59. break;
  60. case 'u':
  61. found = (help_options + 1);
  62. break;
  63. default:
  64. break;
  65. }
  66. }
  67. return found;
  68. }
  69. /* Helper for find_long. */
  70. static const struct grub_arg_option *
  71. fnd_long (const struct grub_arg_option *opt, const char *s, int len)
  72. {
  73. while (opt->doc)
  74. {
  75. if (opt->longarg && ! grub_strncmp (opt->longarg, s, len) &&
  76. opt->longarg[len] == '\0')
  77. return opt;
  78. opt++;
  79. }
  80. return 0;
  81. }
  82. static const struct grub_arg_option *
  83. find_long (const struct grub_arg_option *options, const char *s, int len)
  84. {
  85. const struct grub_arg_option *found = 0;
  86. if (options)
  87. found = fnd_long (options, s, len);
  88. if (! found)
  89. found = fnd_long (help_options, s, len);
  90. return found;
  91. }
  92. static void
  93. show_usage (grub_extcmd_t cmd)
  94. {
  95. grub_printf ("%s %s %s\n", _("Usage:"), cmd->cmd->name, _(cmd->cmd->summary));
  96. }
  97. static void
  98. showargs (const struct grub_arg_option *opt,
  99. int h_is_used, int u_is_used)
  100. {
  101. for (; opt->doc; opt++)
  102. {
  103. int spacing = 20;
  104. if (opt->shortarg && grub_isgraph (opt->shortarg))
  105. grub_printf ("-%c%c ", opt->shortarg, opt->longarg ? ',':' ');
  106. else if (opt == help_options && ! h_is_used)
  107. grub_printf ("-h, ");
  108. else if (opt == help_options + 1 && ! u_is_used)
  109. grub_printf ("-u, ");
  110. else
  111. grub_printf (" ");
  112. if (opt->longarg)
  113. {
  114. grub_printf ("--%s", opt->longarg);
  115. spacing -= grub_strlen (opt->longarg) + 2;
  116. if (opt->arg)
  117. {
  118. grub_printf ("=%s", opt->arg);
  119. spacing -= grub_strlen (opt->arg) + 1;
  120. }
  121. }
  122. if (spacing <= 0)
  123. spacing = 3;
  124. while (spacing--)
  125. grub_xputs (" ");
  126. grub_printf ("%s\n", _(opt->doc));
  127. }
  128. }
  129. void
  130. grub_arg_show_help (grub_extcmd_t cmd)
  131. {
  132. int h_is_used = 0;
  133. int u_is_used = 0;
  134. const struct grub_arg_option *opt;
  135. show_usage (cmd);
  136. grub_printf ("%s\n\n", _(cmd->cmd->description));
  137. for (opt = cmd->options; opt && opt->doc; opt++)
  138. switch (opt->shortarg)
  139. {
  140. case 'h':
  141. h_is_used = 1;
  142. break;
  143. case 'u':
  144. u_is_used = 1;
  145. break;
  146. }
  147. if (cmd->options)
  148. showargs (cmd->options, h_is_used, u_is_used);
  149. showargs (help_options, h_is_used, u_is_used);
  150. #if 0
  151. grub_printf ("\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT);
  152. #endif
  153. }
  154. static int
  155. parse_option (grub_extcmd_t cmd, const struct grub_arg_option *opt,
  156. char *arg, struct grub_arg_list *usr)
  157. {
  158. if (opt == help_options)
  159. {
  160. grub_arg_show_help (cmd);
  161. return -1;
  162. }
  163. if (opt == help_options + 1)
  164. {
  165. show_usage (cmd);
  166. return -1;
  167. }
  168. {
  169. int found = opt - cmd->options;
  170. if (opt->flags & GRUB_ARG_OPTION_REPEATABLE)
  171. {
  172. usr[found].args[usr[found].set++] = arg;
  173. usr[found].args[usr[found].set] = NULL;
  174. }
  175. else
  176. {
  177. usr[found].set = 1;
  178. usr[found].arg = arg;
  179. }
  180. }
  181. return 0;
  182. }
  183. static inline grub_err_t
  184. add_arg (char ***argl, int *num, char *s)
  185. {
  186. char **p = *argl;
  187. grub_size_t sz;
  188. if (grub_add (++(*num), 1, &sz) ||
  189. grub_mul (sz, sizeof (char *), &sz))
  190. return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
  191. *argl = grub_realloc (*argl, sz);
  192. if (! *argl)
  193. {
  194. grub_free (p);
  195. return grub_errno;
  196. }
  197. (*argl)[(*num) - 1] = s;
  198. (*argl)[(*num)] = NULL;
  199. return 0;
  200. }
  201. int
  202. grub_arg_parse (grub_extcmd_t cmd, int argc, char **argv,
  203. struct grub_arg_list *usr, char ***args, int *argnum)
  204. {
  205. int curarg;
  206. int arglen;
  207. char **argl = 0;
  208. int num = 0;
  209. for (curarg = 0; curarg < argc; curarg++)
  210. {
  211. char *arg = argv[curarg];
  212. const struct grub_arg_option *opt;
  213. char *option = 0;
  214. /* No option is used. */
  215. if ((num && (cmd->cmd->flags & GRUB_COMMAND_OPTIONS_AT_START))
  216. || arg[0] != '-' || grub_strlen (arg) == 1)
  217. {
  218. if (add_arg (&argl, &num, arg) != 0)
  219. goto fail;
  220. continue;
  221. }
  222. /* One or more short options. */
  223. if (arg[1] != '-')
  224. {
  225. char *curshort;
  226. if (cmd->cmd->flags & GRUB_COMMAND_ACCEPT_DASH)
  227. {
  228. for (curshort = arg + 1; *curshort; curshort++)
  229. if (!find_short (cmd->options, *curshort))
  230. break;
  231. if (*curshort)
  232. {
  233. if (add_arg (&argl, &num, arg) != 0)
  234. goto fail;
  235. continue;
  236. }
  237. }
  238. curshort = arg + 1;
  239. while (1)
  240. {
  241. opt = find_short (cmd->options, *curshort);
  242. if (! opt)
  243. {
  244. char tmp[3] = { '-', *curshort, 0 };
  245. grub_error (GRUB_ERR_BAD_ARGUMENT,
  246. N_("unknown argument `%s'"), tmp);
  247. goto fail;
  248. }
  249. curshort++;
  250. /* Parse all arguments here except the last one because
  251. it can have an argument value. */
  252. if (*curshort)
  253. {
  254. /*
  255. * Only permit further short opts if this one doesn't
  256. * require a value.
  257. */
  258. if (opt->type != ARG_TYPE_NONE &&
  259. !(opt->flags & GRUB_ARG_OPTION_OPTIONAL))
  260. {
  261. grub_error (GRUB_ERR_BAD_ARGUMENT,
  262. N_("missing mandatory option for `%s'"),
  263. opt->longarg);
  264. goto fail;
  265. }
  266. if (parse_option (cmd, opt, 0, usr) || grub_errno)
  267. goto fail;
  268. }
  269. else
  270. {
  271. if (opt->type != ARG_TYPE_NONE)
  272. {
  273. if (curarg + 1 < argc)
  274. {
  275. char *nextarg = argv[curarg + 1];
  276. if (!(opt->flags & GRUB_ARG_OPTION_OPTIONAL)
  277. || (grub_strlen (nextarg) < 2 || nextarg[0] != '-'))
  278. option = argv[++curarg];
  279. }
  280. }
  281. break;
  282. }
  283. }
  284. }
  285. else /* The argument starts with "--". */
  286. {
  287. /* If the argument "--" is used just pass the other
  288. arguments. */
  289. if (grub_strlen (arg) == 2)
  290. {
  291. for (curarg++; curarg < argc; curarg++)
  292. if (add_arg (&argl, &num, argv[curarg]) != 0)
  293. goto fail;
  294. break;
  295. }
  296. option = grub_strchr (arg, '=');
  297. if (option)
  298. {
  299. arglen = option - arg - 2;
  300. option++;
  301. }
  302. else
  303. arglen = grub_strlen (arg) - 2;
  304. opt = find_long (cmd->options, arg + 2, arglen);
  305. if (!option && argv[curarg + 1] && argv[curarg + 1][0] != '-'
  306. && opt && opt->type != ARG_TYPE_NONE)
  307. option = argv[++curarg];
  308. if (!opt && (cmd->cmd->flags & GRUB_COMMAND_ACCEPT_DASH))
  309. {
  310. if (add_arg (&argl, &num, arg) != 0)
  311. goto fail;
  312. continue;
  313. }
  314. if (! opt)
  315. {
  316. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("unknown argument `%s'"), arg);
  317. goto fail;
  318. }
  319. }
  320. if (! (opt->type == ARG_TYPE_NONE
  321. || (! option && (opt->flags & GRUB_ARG_OPTION_OPTIONAL))))
  322. {
  323. if (! option)
  324. {
  325. grub_error (GRUB_ERR_BAD_ARGUMENT,
  326. N_("missing mandatory option for `%s'"), opt->longarg);
  327. goto fail;
  328. }
  329. switch (opt->type)
  330. {
  331. case ARG_TYPE_NONE:
  332. /* This will never happen. */
  333. break;
  334. case ARG_TYPE_STRING:
  335. /* No need to do anything. */
  336. break;
  337. case ARG_TYPE_INT:
  338. {
  339. const char * tail;
  340. grub_strtoull (option, &tail, 0);
  341. if (tail == 0 || tail == option || *tail != '\0' || grub_errno)
  342. {
  343. grub_error (GRUB_ERR_BAD_ARGUMENT,
  344. N_("the argument `%s' requires an integer"),
  345. arg);
  346. goto fail;
  347. }
  348. break;
  349. }
  350. case ARG_TYPE_DEVICE:
  351. case ARG_TYPE_DIR:
  352. case ARG_TYPE_FILE:
  353. case ARG_TYPE_PATHNAME:
  354. /* XXX: Not implemented. */
  355. break;
  356. }
  357. if (parse_option (cmd, opt, option, usr) || grub_errno)
  358. goto fail;
  359. }
  360. else
  361. {
  362. if (option)
  363. {
  364. grub_error (GRUB_ERR_BAD_ARGUMENT,
  365. N_("a value was assigned to the argument `%s' while it "
  366. "doesn't require an argument"), arg);
  367. goto fail;
  368. }
  369. if (parse_option (cmd, opt, 0, usr) || grub_errno)
  370. goto fail;
  371. }
  372. }
  373. *args = argl;
  374. *argnum = num;
  375. return 1;
  376. fail:
  377. return 0;
  378. }
  379. struct grub_arg_list*
  380. grub_arg_list_alloc(grub_extcmd_t extcmd, int argc,
  381. char **argv __attribute__((unused)))
  382. {
  383. int i;
  384. char **args;
  385. grub_size_t argcnt;
  386. struct grub_arg_list *list;
  387. const struct grub_arg_option *options;
  388. grub_size_t sz0, sz1;
  389. options = extcmd->options;
  390. if (! options)
  391. return 0;
  392. argcnt = 0;
  393. for (i = 0; options[i].doc; i++)
  394. {
  395. if (options[i].flags & GRUB_ARG_OPTION_REPEATABLE)
  396. argcnt += ((grub_size_t) argc + 1) / 2 + 1; /* max possible for any option */
  397. }
  398. if (grub_mul (sizeof (*list), i, &sz0) ||
  399. grub_mul (sizeof (char *), argcnt, &sz1) ||
  400. grub_add (sz0, sz1, &sz0))
  401. {
  402. grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
  403. return 0;
  404. }
  405. list = grub_zalloc (sz0);
  406. if (! list)
  407. return 0;
  408. args = (char**) (list + i);
  409. for (i = 0; options[i].doc; i++)
  410. {
  411. list[i].set = 0;
  412. list[i].arg = 0;
  413. if (options[i].flags & GRUB_ARG_OPTION_REPEATABLE)
  414. {
  415. list[i].args = args;
  416. args += (grub_size_t) argc / 2 + 1;
  417. }
  418. }
  419. return list;
  420. }