arg.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. GRUB_EXPORT(grub_arg_show_help);
  26. /* Built-in parser for default options. */
  27. #define SHORT_ARG_HELP -100
  28. #define SHORT_ARG_USAGE -101
  29. static const struct grub_arg_option help_options[] =
  30. {
  31. {"help", SHORT_ARG_HELP, 0,
  32. N_("Display this help and exit."), 0, ARG_TYPE_NONE},
  33. {"usage", SHORT_ARG_USAGE, 0,
  34. N_("Display the usage of this command and exit."), 0, ARG_TYPE_NONE},
  35. {0, 0, 0, 0, 0, 0}
  36. };
  37. static struct grub_arg_option
  38. *fnd_short (const struct grub_arg_option *opt, char c)
  39. {
  40. while (opt->doc)
  41. {
  42. if (opt->shortarg == c)
  43. return (struct grub_arg_option *) opt;
  44. opt++;
  45. }
  46. return 0;
  47. }
  48. static struct grub_arg_option *
  49. find_short (const struct grub_arg_option *options, char c)
  50. {
  51. struct grub_arg_option *found = 0;
  52. if (options)
  53. found = fnd_short (options, c);
  54. if (! found)
  55. {
  56. switch (c)
  57. {
  58. case 'h':
  59. found = (struct grub_arg_option *) help_options;
  60. break;
  61. case 'u':
  62. found = (struct grub_arg_option *) (help_options + 1);
  63. break;
  64. default:
  65. break;
  66. }
  67. }
  68. return found;
  69. }
  70. static 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 (struct grub_arg_option *) opt;
  78. opt++;
  79. }
  80. return 0;
  81. }
  82. static struct grub_arg_option *
  83. find_long (const struct grub_arg_option *options, const char *s, int len)
  84. {
  85. 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, 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->shortarg == SHORT_ARG_HELP && ! *h_is_used)
  106. grub_printf ("-h, ");
  107. else if (opt->shortarg == SHORT_ARG_USAGE && ! *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. const char *doc = _(opt->doc);
  122. for (;;)
  123. {
  124. while (spacing-- > 0)
  125. grub_putchar (' ');
  126. while (*doc && *doc != '\n')
  127. grub_putchar (*doc++);
  128. grub_putchar ('\n');
  129. if (! *doc)
  130. break;
  131. doc++;
  132. spacing = 4 + 20;
  133. }
  134. switch (opt->shortarg)
  135. {
  136. case 'h':
  137. *h_is_used = 1;
  138. break;
  139. case 'u':
  140. *u_is_used = 1;
  141. break;
  142. default:
  143. break;
  144. }
  145. }
  146. }
  147. void
  148. grub_arg_show_help (grub_extcmd_t cmd)
  149. {
  150. int h_is_used = 0;
  151. int u_is_used = 0;
  152. show_usage (cmd);
  153. grub_printf ("%s\n\n", _(cmd->cmd->description));
  154. if (cmd->options)
  155. showargs (cmd->options, &h_is_used, &u_is_used);
  156. showargs (help_options, &h_is_used, &u_is_used);
  157. #if 0
  158. grub_printf ("\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT);
  159. #endif
  160. }
  161. static int
  162. parse_option (grub_extcmd_t cmd, int key, char *arg, struct grub_arg_list *usr)
  163. {
  164. switch (key)
  165. {
  166. case SHORT_ARG_HELP:
  167. grub_arg_show_help (cmd);
  168. return -1;
  169. case SHORT_ARG_USAGE:
  170. show_usage (cmd);
  171. return -1;
  172. default:
  173. {
  174. int found = -1;
  175. int i = 0;
  176. const struct grub_arg_option *opt = cmd->options;
  177. while (opt->doc)
  178. {
  179. if (opt->shortarg && key == opt->shortarg)
  180. {
  181. found = i;
  182. break;
  183. }
  184. opt++;
  185. i++;
  186. }
  187. if (found == -1)
  188. return -1;
  189. usr[found].set = 1;
  190. usr[found].arg = arg;
  191. }
  192. }
  193. return 0;
  194. }
  195. static grub_err_t
  196. add_arg (char *s, char ***argl, int *num)
  197. {
  198. *argl = grub_realloc (*argl, (++(*num)) * sizeof (char *));
  199. if (! *argl)
  200. return grub_errno;
  201. (*argl)[*num - 1] = s;
  202. return 0;
  203. }
  204. int
  205. grub_arg_parse (grub_extcmd_t cmd, int argc, char **argv,
  206. struct grub_arg_list *usr, char ***args, int *argnum)
  207. {
  208. int curarg;
  209. int arglen;
  210. int complete = 0;
  211. char **argl = 0;
  212. int num = 0;
  213. for (curarg = 0; curarg < argc; curarg++)
  214. {
  215. char *arg = argv[curarg];
  216. struct grub_arg_option *opt;
  217. char *option = 0;
  218. /* No option is used. */
  219. if (arg[0] != '-' || grub_strlen (arg) == 1)
  220. {
  221. if (add_arg (arg, &argl, &num) != 0)
  222. goto fail;
  223. continue;
  224. }
  225. /* One or more short options. */
  226. if (arg[1] != '-')
  227. {
  228. char *curshort = arg + 1;
  229. while (1)
  230. {
  231. opt = find_short (cmd->options, *curshort);
  232. if (! opt)
  233. {
  234. grub_error (GRUB_ERR_BAD_ARGUMENT,
  235. "unknown argument `-%c'", *curshort);
  236. goto fail;
  237. }
  238. curshort++;
  239. /* Parse all arguments here except the last one because
  240. it can have an argument value. */
  241. if (*curshort)
  242. {
  243. if (parse_option (cmd, opt->shortarg, 0, usr) || grub_errno)
  244. goto fail;
  245. }
  246. else
  247. {
  248. if (opt->type != ARG_TYPE_NONE)
  249. {
  250. if (curarg + 1 < argc)
  251. {
  252. char *nextarg = argv[curarg + 1];
  253. if (!(opt->flags & GRUB_ARG_OPTION_OPTIONAL)
  254. || (grub_strlen (nextarg) < 2 || nextarg[0] != '-'))
  255. option = argv[++curarg];
  256. }
  257. }
  258. break;
  259. }
  260. }
  261. }
  262. else /* The argument starts with "--". */
  263. {
  264. /* If the argument "--" is used just pass the other
  265. arguments. */
  266. if (grub_strlen (arg) == 2)
  267. {
  268. for (curarg++; curarg < argc; curarg++)
  269. if (add_arg (argv[curarg], &argl, &num) != 0)
  270. goto fail;
  271. break;
  272. }
  273. option = grub_strchr (arg, '=');
  274. if (option) {
  275. arglen = option - arg - 2;
  276. option++;
  277. } else
  278. arglen = grub_strlen (arg) - 2;
  279. opt = find_long (cmd->options, arg + 2, arglen);
  280. if (! opt)
  281. {
  282. grub_error (GRUB_ERR_BAD_ARGUMENT, "unknown argument `%s'", arg);
  283. goto fail;
  284. }
  285. }
  286. if (! (opt->type == ARG_TYPE_NONE
  287. || (! option && (opt->flags & GRUB_ARG_OPTION_OPTIONAL))))
  288. {
  289. if (! option)
  290. {
  291. grub_error (GRUB_ERR_BAD_ARGUMENT,
  292. "missing mandatory option for `%s'", opt->longarg);
  293. goto fail;
  294. }
  295. switch (opt->type)
  296. {
  297. case ARG_TYPE_NONE:
  298. /* This will never happen. */
  299. break;
  300. case ARG_TYPE_STRING:
  301. /* No need to do anything. */
  302. break;
  303. case ARG_TYPE_INT:
  304. {
  305. char *tail;
  306. grub_strtoull (option, &tail, 0);
  307. if (tail == 0 || tail == option || *tail != '\0' || grub_errno)
  308. {
  309. grub_error (GRUB_ERR_BAD_ARGUMENT,
  310. "the argument `%s' requires an integer",
  311. arg);
  312. goto fail;
  313. }
  314. break;
  315. }
  316. case ARG_TYPE_DEVICE:
  317. case ARG_TYPE_DIR:
  318. case ARG_TYPE_FILE:
  319. case ARG_TYPE_PATHNAME:
  320. /* XXX: Not implemented. */
  321. break;
  322. }
  323. if (parse_option (cmd, opt->shortarg, option, usr) || grub_errno)
  324. goto fail;
  325. }
  326. else
  327. {
  328. if (option)
  329. {
  330. grub_error (GRUB_ERR_BAD_ARGUMENT,
  331. "a value was assigned to the argument `%s' while it "
  332. "doesn't require an argument", arg);
  333. goto fail;
  334. }
  335. if (parse_option (cmd, opt->shortarg, 0, usr) || grub_errno)
  336. goto fail;
  337. }
  338. }
  339. complete = 1;
  340. *args = argl;
  341. *argnum = num;
  342. fail:
  343. return complete;
  344. }