malloc->zalloc
[grub2/phcoder.git] / commands / help.c
blobc18ec6b83ca1d68997ac278f23db49a62c9c7c7d
1 /* help.c - command to show a help text. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2007,2008 Free Software Foundation, Inc.
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.
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.
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/>.
20 #include <grub/dl.h>
21 #include <grub/misc.h>
22 #include <grub/term.h>
23 #include <grub/extcmd.h>
25 static grub_err_t
26 grub_cmd_help (grub_extcmd_t ext __attribute__ ((unused)), int argc,
27 char **args)
29 int cnt = 0;
30 char *currarg;
32 auto int print_command_info (grub_command_t cmd);
33 auto int print_command_help (grub_command_t cmd);
35 int print_command_info (grub_command_t cmd)
37 if ((cmd->prio & GRUB_PRIO_LIST_FLAG_ACTIVE) &&
38 (cmd->flags & GRUB_COMMAND_FLAG_CMDLINE))
40 char description[GRUB_TERM_WIDTH / 2];
41 int desclen = grub_strlen (cmd->summary);
43 /* Make a string with a length of GRUB_TERM_WIDTH / 2 - 1 filled
44 with the description followed by spaces. */
45 grub_memset (description, ' ', GRUB_TERM_WIDTH / 2 - 1);
46 description[GRUB_TERM_WIDTH / 2 - 1] = '\0';
47 grub_memcpy (description, cmd->summary,
48 (desclen < GRUB_TERM_WIDTH / 2 - 1
49 ? desclen : GRUB_TERM_WIDTH / 2 - 1));
51 grub_printf ("%s%s", description, (cnt++) % 2 ? "\n" : " ");
53 return 0;
56 int print_command_help (grub_command_t cmd)
58 if (cmd->prio & GRUB_PRIO_LIST_FLAG_ACTIVE)
60 if (! grub_strncmp (cmd->name, currarg, grub_strlen (currarg)))
62 if (cnt++ > 0)
63 grub_printf ("\n\n");
65 if (cmd->flags & GRUB_COMMAND_FLAG_EXTCMD)
66 grub_arg_show_help ((grub_extcmd_t) cmd->data);
67 else
68 grub_printf ("Usage: %s\n%s\b", cmd->summary,
69 cmd->description);
72 return 0;
75 if (argc == 0)
76 grub_command_iterate (print_command_info);
77 else
79 int i;
81 for (i = 0; i < argc; i++)
83 currarg = args[i];
84 grub_command_iterate (print_command_help);
88 return 0;
91 static grub_extcmd_t cmd;
93 GRUB_MOD_INIT(help)
95 cmd = grub_register_extcmd ("help", grub_cmd_help,
96 GRUB_COMMAND_FLAG_CMDLINE,
97 "help [PATTERN ...]",
98 "Show a help message.", 0);
101 GRUB_MOD_FINI(help)
103 grub_unregister_extcmd (cmd);