main.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* menu_text.c - Basic text menu implementation. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2003,2004,2005,2006,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/term.h>
  21. #include <grub/misc.h>
  22. #include <grub/loader.h>
  23. #include <grub/mm.h>
  24. #include <grub/time.h>
  25. #include <grub/env.h>
  26. #include <grub/lib.h>
  27. #include <grub/auth.h>
  28. #include <grub/reader.h>
  29. #include <grub/parser.h>
  30. #include <grub/normal_menu.h>
  31. #include <grub/menu_viewer.h>
  32. #include <grub/i18n.h>
  33. #include <grub/charset.h>
  34. #include <grub/controller.h>
  35. #include <grub/normal.h>
  36. /* Initialize the screen. */
  37. void
  38. grub_normal_init_page (struct grub_term_output *term)
  39. {
  40. int msg_len;
  41. int posx;
  42. const char *msg = _("%s version %s");
  43. char *msg_formatted;
  44. grub_uint32_t *unicode_msg;
  45. grub_uint32_t *last_position;
  46. grub_term_cls (term);
  47. msg_formatted = grub_xasprintf (msg, PACKAGE_NAME, PACKAGE_VERSION);
  48. if (!msg_formatted)
  49. return;
  50. msg_len = grub_utf8_to_ucs4_alloc (msg_formatted,
  51. &unicode_msg, &last_position);
  52. grub_free (msg_formatted);
  53. if (msg_len < 0)
  54. {
  55. return;
  56. }
  57. posx = grub_getstringwidth (unicode_msg, last_position, term);
  58. posx = (grub_term_width (term) - posx) / 2;
  59. grub_term_gotoxy (term, posx, 1);
  60. grub_print_ucs4 (unicode_msg, last_position, term);
  61. grub_printf("\n\n");
  62. grub_free (unicode_msg);
  63. }
  64. static int
  65. grub_username_get (char buf[], unsigned buf_size)
  66. {
  67. unsigned cur_len = 0;
  68. int key;
  69. while (1)
  70. {
  71. key = GRUB_TERM_ASCII_CHAR (grub_getkey ());
  72. if (key == '\n' || key == '\r')
  73. break;
  74. if (key == '\e')
  75. {
  76. cur_len = 0;
  77. break;
  78. }
  79. if (key == '\b')
  80. {
  81. cur_len--;
  82. grub_printf ("\b");
  83. continue;
  84. }
  85. if (!grub_isprint (key))
  86. continue;
  87. if (cur_len + 2 < buf_size)
  88. {
  89. buf[cur_len++] = key;
  90. grub_putchar (key);
  91. }
  92. }
  93. grub_memset (buf + cur_len, 0, buf_size - cur_len);
  94. grub_putchar ('\n');
  95. grub_refresh ();
  96. return (key != '\e');
  97. }
  98. grub_err_t
  99. grub_normal_check_authentication (const char *userlist)
  100. {
  101. char login[1024];
  102. char entered[GRUB_AUTH_MAX_PASSLEN];
  103. grub_memset (login, 0, sizeof (login));
  104. if (grub_auth_check_password (userlist, 0, 0))
  105. return GRUB_ERR_NONE;
  106. grub_puts_ (N_("Enter username: "));
  107. if (!grub_username_get (login, sizeof (login) - 1))
  108. return GRUB_ACCESS_DENIED;
  109. grub_puts_ (N_("Enter password: "));
  110. if (!grub_password_get (entered, GRUB_AUTH_MAX_PASSLEN))
  111. return GRUB_ACCESS_DENIED;
  112. return (grub_auth_check_password (userlist, login, entered)) ?
  113. GRUB_ERR_NONE : GRUB_ACCESS_DENIED;
  114. }
  115. static grub_err_t
  116. grub_normal_reader_init (int nested)
  117. {
  118. struct grub_term_output *term;
  119. const char *msg = _("Minimal BASH-like line editing is supported. For "
  120. "the first word, TAB lists possible command completions. Anywhere "
  121. "else TAB lists possible device or file completions. %s");
  122. const char *msg_esc = _("ESC at any time exits.");
  123. char *msg_formatted;
  124. msg_formatted = grub_xasprintf (msg, nested ? msg_esc : "");
  125. if (!msg_formatted)
  126. return grub_errno;
  127. FOR_ACTIVE_TERM_OUTPUTS(term)
  128. {
  129. grub_normal_init_page (term);
  130. grub_term_setcursor (term, 1);
  131. grub_print_message_indented (msg_formatted, 3, STANDARD_MARGIN, term);
  132. grub_puts ("\n");
  133. }
  134. grub_free (msg_formatted);
  135. return 0;
  136. }
  137. static grub_err_t
  138. grub_normal_read_line_real (char **line, int cont, int nested)
  139. {
  140. grub_parser_t parser = grub_parser_get_current ();
  141. char *prompt;
  142. if (cont)
  143. prompt = grub_xasprintf (">");
  144. else
  145. prompt = grub_xasprintf ("%s>", parser->name);
  146. if (!prompt)
  147. return grub_errno;
  148. while (1)
  149. {
  150. *line = grub_cmdline_get (prompt);
  151. if (*line)
  152. break;
  153. if (cont || nested)
  154. {
  155. grub_free (*line);
  156. grub_free (prompt);
  157. *line = 0;
  158. return grub_errno;
  159. }
  160. }
  161. grub_free (prompt);
  162. return 0;
  163. }
  164. static grub_err_t
  165. grub_normal_read_line (char **line, int cont,
  166. void *closure __attribute__((unused)))
  167. {
  168. return grub_normal_read_line_real (line, cont, 0);
  169. }
  170. void
  171. grub_normal_cmdline_run (int nested)
  172. {
  173. grub_err_t err = GRUB_ERR_NONE;
  174. err = grub_normal_check_authentication (NULL);
  175. if (err)
  176. {
  177. grub_print_error ();
  178. grub_errno = GRUB_ERR_NONE;
  179. return;
  180. }
  181. grub_normal_reader_init (nested);
  182. while (1)
  183. {
  184. char *line;
  185. if (grub_normal_exit_level)
  186. break;
  187. /* Print an error, if any. */
  188. grub_print_error ();
  189. grub_errno = GRUB_ERR_NONE;
  190. grub_normal_read_line_real (&line, 0, nested);
  191. if (! line)
  192. break;
  193. grub_parser_get_current ()->parse_line (line, grub_normal_read_line, 0);
  194. grub_free (line);
  195. }
  196. }
  197. static char *
  198. grub_env_write_pager (struct grub_env_var *var __attribute__ ((unused)),
  199. const char *val)
  200. {
  201. grub_set_more ((*val == '1'));
  202. return grub_strdup (val);
  203. }
  204. static grub_err_t
  205. show_menu (grub_menu_t menu, int nested)
  206. {
  207. if (menu)
  208. grub_normal_show_menu (menu, nested);
  209. else if (! nested)
  210. grub_normal_cmdline_run (0);
  211. return 0;
  212. }
  213. static struct grub_controller grub_normal_controller =
  214. {
  215. .name = "normal",
  216. .show_menu = show_menu,
  217. };
  218. GRUB_MOD_INIT(nmenu)
  219. {
  220. grub_controller_register ("normal", &grub_normal_controller);
  221. grub_register_variable_hook ("pager", 0, grub_env_write_pager);
  222. /* Reload terminal colors when these variables are written to. */
  223. grub_register_variable_hook ("color_normal", NULL,
  224. grub_env_write_color_normal);
  225. grub_register_variable_hook ("color_highlight", NULL,
  226. grub_env_write_color_highlight);
  227. /* Preserve hooks after context changes. */
  228. grub_env_export ("color_normal");
  229. grub_env_export ("color_highlight");
  230. }
  231. GRUB_MOD_FINI(nmenu)
  232. {
  233. grub_controller_unregister (&grub_normal_controller);
  234. grub_register_variable_hook ("pager", 0, 0);
  235. grub_register_variable_hook ("color_normal", 0, 0);
  236. grub_register_variable_hook ("color_highlight", 0, 0);
  237. }