help.c 3.8 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/normal.h>
  26. #include <grub/charset.h>
  27. GRUB_MOD_LICENSE ("GPLv3+");
  28. static grub_err_t
  29. grub_cmd_help (grub_extcmd_context_t ctxt __attribute__ ((unused)), int argc,
  30. char **args)
  31. {
  32. int cnt = 0;
  33. char *currarg;
  34. if (argc == 0)
  35. {
  36. grub_command_t cmd;
  37. FOR_COMMANDS(cmd)
  38. {
  39. if ((cmd->prio & GRUB_COMMAND_FLAG_ACTIVE))
  40. {
  41. struct grub_term_output *term;
  42. const char *summary_translated = _(cmd->summary);
  43. char *command_help;
  44. grub_uint32_t *unicode_command_help;
  45. grub_uint32_t *unicode_last_position;
  46. command_help = grub_xasprintf ("%s %s", cmd->name, summary_translated);
  47. if (!command_help)
  48. break;
  49. grub_utf8_to_ucs4_alloc (command_help, &unicode_command_help,
  50. &unicode_last_position);
  51. FOR_ACTIVE_TERM_OUTPUTS(term)
  52. {
  53. unsigned stringwidth;
  54. grub_uint32_t *unicode_last_screen_position;
  55. unicode_last_screen_position = unicode_command_help;
  56. stringwidth = 0;
  57. while (unicode_last_screen_position < unicode_last_position &&
  58. stringwidth < ((grub_term_width (term) / 2) - 2))
  59. {
  60. struct grub_unicode_glyph glyph;
  61. unicode_last_screen_position
  62. += grub_unicode_aglomerate_comb (unicode_last_screen_position,
  63. unicode_last_position
  64. - unicode_last_screen_position,
  65. &glyph);
  66. stringwidth
  67. += grub_term_getcharwidth (term, &glyph);
  68. }
  69. grub_print_ucs4 (unicode_command_help,
  70. unicode_last_screen_position, 0, 0, term);
  71. if (!(cnt % 2))
  72. grub_print_spaces (term, grub_term_width (term) / 2
  73. - stringwidth);
  74. }
  75. if (cnt % 2)
  76. grub_printf ("\n");
  77. cnt++;
  78. grub_free (command_help);
  79. grub_free (unicode_command_help);
  80. }
  81. }
  82. if (!(cnt % 2))
  83. grub_printf ("\n");
  84. }
  85. else
  86. {
  87. int i;
  88. grub_command_t cmd_iter, cmd, cmd_next;
  89. for (i = 0; i < argc; i++)
  90. {
  91. currarg = args[i];
  92. FOR_COMMANDS_SAFE (cmd_iter, cmd_next)
  93. {
  94. if (!(cmd_iter->prio & GRUB_COMMAND_FLAG_ACTIVE))
  95. continue;
  96. if (grub_strncmp (cmd_iter->name, currarg,
  97. grub_strlen (currarg)) != 0)
  98. continue;
  99. if (cmd_iter->flags & GRUB_COMMAND_FLAG_DYNCMD)
  100. cmd = grub_dyncmd_get_cmd (cmd_iter);
  101. else
  102. cmd = cmd_iter;
  103. if (!cmd)
  104. {
  105. grub_print_error ();
  106. continue;
  107. }
  108. if (cnt++ > 0)
  109. grub_printf ("\n\n");
  110. if ((cmd->flags & GRUB_COMMAND_FLAG_EXTCMD) &&
  111. ! (cmd->flags & GRUB_COMMAND_FLAG_DYNCMD))
  112. grub_arg_show_help ((grub_extcmd_t) cmd->data);
  113. else
  114. grub_printf ("%s %s %s\n%s\n", _("Usage:"), cmd->name,
  115. _(cmd->summary), _(cmd->description));
  116. }
  117. }
  118. }
  119. return 0;
  120. }
  121. static grub_extcmd_t cmd;
  122. GRUB_MOD_INIT(help)
  123. {
  124. cmd = grub_register_extcmd ("help", grub_cmd_help, 0,
  125. N_("[PATTERN ...]"),
  126. N_("Show a help message."), 0);
  127. }
  128. GRUB_MOD_FINI(help)
  129. {
  130. grub_unregister_extcmd (cmd);
  131. }