terminfo.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* terminfo.c - simple terminfo module */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2003,2004,2005,2007 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. /*
  20. * This file contains various functions dealing with different
  21. * terminal capabilities. For example, vt52 and vt100.
  22. */
  23. #include <grub/types.h>
  24. #include <grub/misc.h>
  25. #include <grub/mm.h>
  26. #include <grub/err.h>
  27. #include <grub/dl.h>
  28. #include <grub/term.h>
  29. #include <grub/terminfo.h>
  30. #include <grub/tparm.h>
  31. #include <grub/command.h>
  32. #include <grub/i18n.h>
  33. GRUB_EXPORT(grub_terminfo_cls);
  34. GRUB_EXPORT(grub_terminfo_cursor_on);
  35. GRUB_EXPORT(grub_terminfo_cursor_off);
  36. GRUB_EXPORT(grub_terminfo_gotoxy);
  37. GRUB_EXPORT(grub_terminfo_reverse_video_on);
  38. GRUB_EXPORT(grub_terminfo_reverse_video_off);
  39. struct terminfo
  40. {
  41. char *name;
  42. char *gotoxy;
  43. char *cls;
  44. char *reverse_video_on;
  45. char *reverse_video_off;
  46. char *cursor_on;
  47. char *cursor_off;
  48. };
  49. static struct terminfo term;
  50. /* Get current terminfo name. */
  51. char *
  52. grub_terminfo_get_current (void)
  53. {
  54. return term.name;
  55. }
  56. /* Free *PTR and set *PTR to NULL, to prevent double-free. */
  57. static void
  58. grub_terminfo_free (char **ptr)
  59. {
  60. grub_free (*ptr);
  61. *ptr = 0;
  62. }
  63. /* Set current terminfo type. */
  64. grub_err_t
  65. grub_terminfo_set_current (const char *str)
  66. {
  67. /* TODO
  68. * Lookup user specified terminfo type. If found, set term variables
  69. * as appropriate. Otherwise return an error.
  70. *
  71. * How should this be done?
  72. * a. A static table included in this module.
  73. * - I do not like this idea.
  74. * b. A table stored in the configuration directory.
  75. * - Users must convert their terminfo settings if we have not already.
  76. * c. Look for terminfo files in the configuration directory.
  77. * - /usr/share/terminfo is 6.3M on my system.
  78. * - /usr/share/terminfo is not on most users boot partition.
  79. * + Copying the terminfo files you want to use to the grub
  80. * configuration directory is easier then (b).
  81. * d. Your idea here.
  82. */
  83. /* Free previously allocated memory. */
  84. grub_terminfo_free (&term.name);
  85. grub_terminfo_free (&term.gotoxy);
  86. grub_terminfo_free (&term.cls);
  87. grub_terminfo_free (&term.reverse_video_on);
  88. grub_terminfo_free (&term.reverse_video_off);
  89. grub_terminfo_free (&term.cursor_on);
  90. grub_terminfo_free (&term.cursor_off);
  91. if (grub_strcmp ("vt100", str) == 0)
  92. {
  93. term.name = grub_strdup ("vt100");
  94. term.gotoxy = grub_strdup ("\e[%i%p1%d;%p2%dH");
  95. term.cls = grub_strdup ("\e[H\e[J");
  96. term.reverse_video_on = grub_strdup ("\e[7m");
  97. term.reverse_video_off = grub_strdup ("\e[m");
  98. term.cursor_on = grub_strdup ("\e[?25h");
  99. term.cursor_off = grub_strdup ("\e[?25l");
  100. return grub_errno;
  101. }
  102. return grub_error (GRUB_ERR_BAD_ARGUMENT, "unknown terminfo type");
  103. }
  104. /* Wrapper for grub_putchar to write strings. */
  105. static void
  106. putstr (const char *str, grub_term_output_t oterm)
  107. {
  108. while (*str)
  109. grub_putcode (*str++, oterm);
  110. }
  111. /* Move the cursor to the given position starting with "0". */
  112. void
  113. grub_terminfo_gotoxy (grub_uint8_t x, grub_uint8_t y, grub_term_output_t oterm)
  114. {
  115. putstr (grub_terminfo_tparm (term.gotoxy, y, x), oterm);
  116. }
  117. /* Clear the screen. */
  118. void
  119. grub_terminfo_cls (grub_term_output_t oterm)
  120. {
  121. putstr (grub_terminfo_tparm (term.cls), oterm);
  122. }
  123. /* Set reverse video mode on. */
  124. void
  125. grub_terminfo_reverse_video_on (grub_term_output_t oterm)
  126. {
  127. putstr (grub_terminfo_tparm (term.reverse_video_on), oterm);
  128. }
  129. /* Set reverse video mode off. */
  130. void
  131. grub_terminfo_reverse_video_off (grub_term_output_t oterm)
  132. {
  133. putstr (grub_terminfo_tparm (term.reverse_video_off), oterm);
  134. }
  135. /* Show cursor. */
  136. void
  137. grub_terminfo_cursor_on (grub_term_output_t oterm)
  138. {
  139. putstr (grub_terminfo_tparm (term.cursor_on), oterm);
  140. }
  141. /* Hide cursor. */
  142. void
  143. grub_terminfo_cursor_off (grub_term_output_t oterm)
  144. {
  145. putstr (grub_terminfo_tparm (term.cursor_off), oterm);
  146. }
  147. /* GRUB Command. */
  148. static grub_err_t
  149. grub_cmd_terminfo (grub_command_t cmd __attribute__ ((unused)),
  150. int argc, char **args)
  151. {
  152. if (argc == 0)
  153. {
  154. grub_printf ("Current terminfo type: %s\n", grub_terminfo_get_current());
  155. return GRUB_ERR_NONE;
  156. }
  157. else if (argc != 1)
  158. return grub_error (GRUB_ERR_BAD_ARGUMENT, "too many parameters");
  159. else
  160. return grub_terminfo_set_current (args[0]);
  161. }
  162. static grub_command_t cmd;
  163. GRUB_MOD_INIT(terminfo)
  164. {
  165. cmd = grub_register_command ("terminfo", grub_cmd_terminfo,
  166. N_("[TERM]"), N_("Set terminfo type."));
  167. grub_terminfo_set_current ("vt100");
  168. }
  169. GRUB_MOD_FINI(terminfo)
  170. {
  171. grub_unregister_command (cmd);
  172. }