term.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2002,2003,2005,2007,2008,2009 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/term.h>
  19. #include <grub/misc.h>
  20. #include <grub/mm.h>
  21. #include <grub/file.h>
  22. #include <grub/dl.h>
  23. #include <grub/env.h>
  24. #include <grub/normal.h>
  25. #include <grub/lib.h>
  26. GRUB_EXPORT(grub_term_input_autoload);
  27. GRUB_EXPORT(grub_term_output_autoload);
  28. GRUB_EXPORT(grub_term_save_pos);
  29. GRUB_EXPORT(grub_term_restore_pos);
  30. GRUB_EXPORT(grub_set_more);
  31. GRUB_EXPORT(grub_puts_terminal);
  32. GRUB_EXPORT(grub_normal_get_line_counter);
  33. struct grub_term_autoload *grub_term_input_autoload = NULL;
  34. struct grub_term_autoload *grub_term_output_autoload = NULL;
  35. /* The amount of lines counted by the pager. */
  36. static unsigned grub_more_lines;
  37. /* If the more pager is active. */
  38. static int grub_more;
  39. static int grub_normal_line_counter = 0;
  40. int
  41. grub_normal_get_line_counter (void)
  42. {
  43. return grub_normal_line_counter;
  44. }
  45. static void
  46. process_newline (void)
  47. {
  48. struct grub_term_output *cur;
  49. unsigned height = -1;
  50. FOR_ACTIVE_TERM_OUTPUTS(cur)
  51. if (grub_term_height (cur) < height)
  52. height = grub_term_height (cur);
  53. grub_more_lines++;
  54. grub_normal_line_counter++;
  55. if (grub_more && grub_more_lines >= height - 1)
  56. {
  57. char key;
  58. grub_uint16_t *pos;
  59. pos = grub_term_save_pos ();
  60. grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
  61. grub_printf ("--MORE--");
  62. grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
  63. key = grub_getkey ();
  64. /* Remove the message. */
  65. grub_term_restore_pos (pos);
  66. grub_printf (" ");
  67. grub_term_restore_pos (pos);
  68. /* Scroll one lines or an entire page, depending on the key. */
  69. if (key == '\r' || key =='\n')
  70. grub_more_lines = height - 2;
  71. else
  72. grub_more_lines = 0;
  73. }
  74. }
  75. void
  76. grub_set_more (int onoff)
  77. {
  78. if (onoff == 1)
  79. grub_more++;
  80. else
  81. grub_more--;
  82. grub_more_lines = 0;
  83. }
  84. void
  85. grub_install_newline_hook (void)
  86. {
  87. grub_newline_hook = process_newline;
  88. }
  89. void
  90. grub_puts_terminal (const char *str, struct grub_term_output *term)
  91. {
  92. grub_uint32_t code;
  93. grub_ssize_t ret;
  94. const grub_uint8_t *ptr = (const grub_uint8_t *) str;
  95. const grub_uint8_t *end;
  96. end = (const grub_uint8_t *) (str + grub_strlen (str));
  97. while (*ptr)
  98. {
  99. ret = grub_utf8_to_ucs4 (&code, 1, ptr, end - ptr, &ptr);
  100. grub_putcode (code, term);
  101. }
  102. }
  103. grub_uint16_t *
  104. grub_term_save_pos (void)
  105. {
  106. struct grub_term_output *cur;
  107. unsigned cnt = 0;
  108. grub_uint16_t *ret, *ptr;
  109. FOR_ACTIVE_TERM_OUTPUTS(cur)
  110. cnt++;
  111. ret = grub_malloc (cnt * sizeof (ret[0]));
  112. if (!ret)
  113. return NULL;
  114. ptr = ret;
  115. FOR_ACTIVE_TERM_OUTPUTS(cur)
  116. *ptr++ = grub_term_getxy (cur);
  117. return ret;
  118. }
  119. void
  120. grub_term_restore_pos (grub_uint16_t *pos)
  121. {
  122. struct grub_term_output *cur;
  123. grub_uint16_t *ptr = pos;
  124. if (!pos)
  125. return;
  126. FOR_ACTIVE_TERM_OUTPUTS(cur)
  127. {
  128. grub_term_gotoxy (cur, (*ptr & 0xff00) >> 8, *ptr & 0xff);
  129. ptr++;
  130. }
  131. }
  132. static void
  133. grub_terminal_autoload_free (void)
  134. {
  135. struct grub_term_autoload *cur, *next;
  136. unsigned i;
  137. for (i = 0; i < 2; i++)
  138. for (cur = i ? grub_term_input_autoload : grub_term_output_autoload;
  139. cur; cur = next)
  140. {
  141. next = cur->next;
  142. grub_free (cur->name);
  143. grub_free (cur->modname);
  144. grub_free (cur);
  145. }
  146. grub_term_input_autoload = NULL;
  147. grub_term_output_autoload = NULL;
  148. }
  149. /* Read the file terminal.lst for auto-loading. */
  150. void
  151. read_terminal_list (const char *prefix)
  152. {
  153. char *filename;
  154. grub_file_t file;
  155. char *buf = NULL;
  156. if (!prefix)
  157. {
  158. grub_errno = GRUB_ERR_NONE;
  159. return;
  160. }
  161. filename = grub_xasprintf ("%s/terminal.lst", prefix);
  162. if (!filename)
  163. {
  164. grub_errno = GRUB_ERR_NONE;
  165. return;
  166. }
  167. file = grub_file_open (filename);
  168. grub_free (filename);
  169. if (!file)
  170. {
  171. grub_errno = GRUB_ERR_NONE;
  172. return;
  173. }
  174. /* Override previous terminal.lst. */
  175. grub_terminal_autoload_free ();
  176. for (;; grub_free (buf))
  177. {
  178. char *p, *name;
  179. struct grub_term_autoload *cur;
  180. struct grub_term_autoload **target = NULL;
  181. buf = grub_getline (file);
  182. if (! buf)
  183. break;
  184. switch (buf[0])
  185. {
  186. case 'i':
  187. target = &grub_term_input_autoload;
  188. break;
  189. case 'o':
  190. target = &grub_term_output_autoload;
  191. break;
  192. }
  193. if (!target)
  194. continue;
  195. name = buf + 1;
  196. p = grub_strchr (name, ':');
  197. if (! p)
  198. continue;
  199. *p = '\0';
  200. while (*++p == ' ')
  201. ;
  202. cur = grub_malloc (sizeof (*cur));
  203. if (!cur)
  204. {
  205. grub_errno = GRUB_ERR_NONE;
  206. continue;
  207. }
  208. cur->name = grub_strdup (name);
  209. if (! name)
  210. {
  211. grub_errno = GRUB_ERR_NONE;
  212. grub_free (cur);
  213. continue;
  214. }
  215. cur->modname = grub_strdup (p);
  216. if (! cur->modname)
  217. {
  218. grub_errno = GRUB_ERR_NONE;
  219. grub_free (cur);
  220. grub_free (cur->name);
  221. continue;
  222. }
  223. cur->next = *target;
  224. *target = cur;
  225. }
  226. grub_file_close (file);
  227. grub_errno = GRUB_ERR_NONE;
  228. }