help.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* help.c - command to show a help text. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2005,2007,2008,2009 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/dl.h>
  20. #include <grub/misc.h>
  21. #include <grub/term.h>
  22. #include <grub/extcmd.h>
  23. #include <grub/i18n.h>
  24. #include <grub/mm.h>
  25. #include <grub/lib.h>
  26. #include <grub/charset.h>
  27. struct grub_cmd_help_closure
  28. {
  29. int cnt;
  30. char *currarg;
  31. };
  32. static int
  33. print_command_info (grub_command_t cmd, void *closure)
  34. {
  35. struct grub_cmd_help_closure *c = closure;
  36. if ((cmd->prio & GRUB_PRIO_LIST_FLAG_ACTIVE) &&
  37. (cmd->flags & GRUB_COMMAND_FLAG_CMDLINE))
  38. {
  39. struct grub_term_output *term;
  40. const char *summary_translated = _(cmd->summary);
  41. char *command_help;
  42. grub_uint32_t *unicode_command_help;
  43. grub_uint32_t *unicode_last_position;
  44. command_help = grub_xasprintf ("%s %s", cmd->name, summary_translated);
  45. if (! command_help)
  46. return 1;
  47. grub_utf8_to_ucs4_alloc (command_help, &unicode_command_help,
  48. &unicode_last_position);
  49. FOR_ACTIVE_TERM_OUTPUTS(term)
  50. {
  51. unsigned stringwidth;
  52. grub_uint32_t *unicode_last_screen_position;
  53. unicode_last_screen_position = unicode_command_help;
  54. stringwidth = 0;
  55. while (unicode_last_screen_position < unicode_last_position &&
  56. stringwidth < ((grub_term_width (term) / 2) - 2))
  57. {
  58. stringwidth
  59. += grub_term_getcharwidth (term,
  60. *unicode_last_screen_position);
  61. unicode_last_screen_position++;
  62. }
  63. grub_print_ucs4 (unicode_command_help,
  64. unicode_last_screen_position, term);
  65. if (!(c->cnt % 2))
  66. grub_print_spaces (term, grub_term_width (term) / 2
  67. - stringwidth);
  68. }
  69. if (c->cnt % 2)
  70. grub_printf ("\n");
  71. c->cnt++;
  72. grub_free (command_help);
  73. grub_free (unicode_command_help);
  74. }
  75. return 0;
  76. }
  77. static int
  78. print_command_help (grub_command_t cmd, void *closure)
  79. {
  80. struct grub_cmd_help_closure *c = closure;
  81. if (cmd->prio & GRUB_PRIO_LIST_FLAG_ACTIVE)
  82. {
  83. if (! grub_strncmp (cmd->name, c->currarg, grub_strlen (c->currarg)))
  84. {
  85. if (c->cnt++ > 0)
  86. grub_printf ("\n\n");
  87. if (cmd->flags & GRUB_COMMAND_FLAG_EXTCMD)
  88. grub_arg_show_help ((grub_extcmd_t) cmd->data);
  89. else
  90. grub_printf ("%s %s %s\n%s\n", _("Usage:"), cmd->name, _(cmd->summary),
  91. _(cmd->description));
  92. }
  93. }
  94. return 0;
  95. }
  96. static grub_err_t
  97. grub_cmd_help (grub_extcmd_t ext __attribute__ ((unused)), int argc,
  98. char **args)
  99. {
  100. struct grub_cmd_help_closure c;
  101. c.cnt = 0;
  102. if (argc == 0)
  103. {
  104. grub_command_iterate (print_command_info, &c);
  105. if (!(c.cnt % 2))
  106. grub_printf ("\n");
  107. }
  108. else
  109. {
  110. int i;
  111. for (i = 0; i < argc; i++)
  112. {
  113. c.currarg = args[i];
  114. grub_command_iterate (print_command_help, &c);
  115. }
  116. }
  117. return 0;
  118. }
  119. static grub_extcmd_t cmd;
  120. GRUB_MOD_INIT(help)
  121. {
  122. cmd = grub_register_extcmd ("help", grub_cmd_help,
  123. GRUB_COMMAND_FLAG_CMDLINE,
  124. N_("[PATTERN ...]"),
  125. N_("Show a help message."), 0);
  126. }
  127. GRUB_MOD_FINI(help)
  128. {
  129. grub_unregister_extcmd (cmd);
  130. }