arg.c 9.5 KB

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