terminal.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2009,2010 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/mm.h>
  19. #include <grub/dl.h>
  20. #include <grub/command.h>
  21. #include <grub/term.h>
  22. #include <grub/i18n.h>
  23. #include <grub/misc.h>
  24. GRUB_MOD_LICENSE ("GPLv3+");
  25. struct grub_term_autoload *grub_term_input_autoload = NULL;
  26. struct grub_term_autoload *grub_term_output_autoload = NULL;
  27. struct abstract_terminal
  28. {
  29. struct abstract_terminal *next;
  30. struct abstract_terminal *prev;
  31. const char *name;
  32. grub_err_t (*init) (struct abstract_terminal *term);
  33. grub_err_t (*fini) (struct abstract_terminal *term);
  34. };
  35. static grub_err_t
  36. handle_command (int argc, char **args, struct abstract_terminal **enabled,
  37. struct abstract_terminal **disabled,
  38. struct grub_term_autoload *autoloads,
  39. const char *active_str,
  40. const char *available_str)
  41. {
  42. int i;
  43. struct abstract_terminal *term;
  44. struct grub_term_autoload *aut;
  45. if (argc == 0)
  46. {
  47. grub_puts_ (active_str);
  48. for (term = *enabled; term; term = term->next)
  49. grub_printf ("%s ", term->name);
  50. grub_printf ("\n");
  51. grub_puts_ (available_str);
  52. for (term = *disabled; term; term = term->next)
  53. grub_printf ("%s ", term->name);
  54. /* This is quadratic but we don't expect mode than 30 terminal
  55. modules ever. */
  56. for (aut = autoloads; aut; aut = aut->next)
  57. {
  58. for (term = *disabled; term; term = term->next)
  59. if (grub_strcmp (term->name, aut->name) == 0
  60. || (aut->name[0] && aut->name[grub_strlen (aut->name) - 1] == '*'
  61. && grub_memcmp (term->name, aut->name,
  62. grub_strlen (aut->name) - 1) == 0))
  63. break;
  64. if (!term)
  65. for (term = *enabled; term; term = term->next)
  66. if (grub_strcmp (term->name, aut->name) == 0
  67. || (aut->name[0] && aut->name[grub_strlen (aut->name) - 1] == '*'
  68. && grub_memcmp (term->name, aut->name,
  69. grub_strlen (aut->name) - 1) == 0))
  70. break;
  71. if (!term)
  72. grub_printf ("%s ", aut->name);
  73. }
  74. grub_printf ("\n");
  75. return GRUB_ERR_NONE;
  76. }
  77. i = 0;
  78. if (grub_strcmp (args[0], "--append") == 0
  79. || grub_strcmp (args[0], "--remove") == 0)
  80. i++;
  81. if (i == argc)
  82. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("no terminal specified"));
  83. for (; i < argc; i++)
  84. {
  85. int again = 0;
  86. while (1)
  87. {
  88. for (term = *disabled; term; term = term->next)
  89. if (grub_strcmp (args[i], term->name) == 0
  90. || (grub_strcmp (args[i], "ofconsole") == 0
  91. && grub_strcmp ("console", term->name) == 0))
  92. break;
  93. if (term == 0)
  94. for (term = *enabled; term; term = term->next)
  95. if (grub_strcmp (args[i], term->name) == 0
  96. || (grub_strcmp (args[i], "ofconsole") == 0
  97. && grub_strcmp ("console", term->name) == 0))
  98. break;
  99. if (term)
  100. break;
  101. if (again)
  102. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  103. N_("terminal `%s' isn't found"),
  104. args[i]);
  105. for (aut = autoloads; aut; aut = aut->next)
  106. if (grub_strcmp (args[i], aut->name) == 0
  107. || (grub_strcmp (args[i], "ofconsole") == 0
  108. && grub_strcmp ("console", aut->name) == 0)
  109. || (aut->name[0] && aut->name[grub_strlen (aut->name) - 1] == '*'
  110. && grub_memcmp (args[i], aut->name,
  111. grub_strlen (aut->name) - 1) == 0))
  112. {
  113. grub_dl_t mod;
  114. mod = grub_dl_load (aut->modname);
  115. if (mod)
  116. grub_dl_ref (mod);
  117. grub_errno = GRUB_ERR_NONE;
  118. break;
  119. }
  120. if (grub_memcmp (args[i], "serial_usb",
  121. sizeof ("serial_usb") - 1) == 0
  122. && grub_term_poll_usb)
  123. {
  124. grub_term_poll_usb (1);
  125. again = 1;
  126. continue;
  127. }
  128. if (!aut)
  129. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  130. N_("terminal `%s' isn't found"),
  131. args[i]);
  132. again = 1;
  133. }
  134. }
  135. if (grub_strcmp (args[0], "--append") == 0)
  136. {
  137. for (i = 1; i < argc; i++)
  138. {
  139. for (term = *disabled; term; term = term->next)
  140. if (grub_strcmp (args[i], term->name) == 0
  141. || (grub_strcmp (args[i], "ofconsole") == 0
  142. && grub_strcmp ("console", term->name) == 0))
  143. break;
  144. if (term)
  145. {
  146. if (term->init && term->init (term) != GRUB_ERR_NONE)
  147. return grub_errno;
  148. grub_list_remove (GRUB_AS_LIST (term));
  149. grub_list_push (GRUB_AS_LIST_P (enabled), GRUB_AS_LIST (term));
  150. }
  151. }
  152. return GRUB_ERR_NONE;
  153. }
  154. if (grub_strcmp (args[0], "--remove") == 0)
  155. {
  156. for (i = 1; i < argc; i++)
  157. {
  158. for (term = *enabled; term; term = term->next)
  159. if (grub_strcmp (args[i], term->name) == 0
  160. || (grub_strcmp (args[i], "ofconsole") == 0
  161. && grub_strcmp ("console", term->name) == 0))
  162. break;
  163. if (term)
  164. {
  165. if (!term->next && term == *enabled)
  166. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  167. "can't remove the last terminal");
  168. grub_list_remove (GRUB_AS_LIST (term));
  169. if (term->fini)
  170. term->fini (term);
  171. grub_list_push (GRUB_AS_LIST_P (disabled), GRUB_AS_LIST (term));
  172. }
  173. }
  174. return GRUB_ERR_NONE;
  175. }
  176. for (i = 0; i < argc; i++)
  177. {
  178. for (term = *disabled; term; term = term->next)
  179. if (grub_strcmp (args[i], term->name) == 0
  180. || (grub_strcmp (args[i], "ofconsole") == 0
  181. && grub_strcmp ("console", term->name) == 0))
  182. break;
  183. if (term)
  184. {
  185. if (term->init && term->init (term) != GRUB_ERR_NONE)
  186. return grub_errno;
  187. grub_list_remove (GRUB_AS_LIST (term));
  188. grub_list_push (GRUB_AS_LIST_P (enabled), GRUB_AS_LIST (term));
  189. }
  190. }
  191. {
  192. struct abstract_terminal *next;
  193. for (term = *enabled; term; term = next)
  194. {
  195. next = term->next;
  196. for (i = 0; i < argc; i++)
  197. if (grub_strcmp (args[i], term->name) == 0
  198. || (grub_strcmp (args[i], "ofconsole") == 0
  199. && grub_strcmp ("console", term->name) == 0))
  200. break;
  201. if (i == argc)
  202. {
  203. if (!term->next && term == *enabled)
  204. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  205. "can't remove the last terminal");
  206. grub_list_remove (GRUB_AS_LIST (term));
  207. if (term->fini)
  208. term->fini (term);
  209. grub_list_push (GRUB_AS_LIST_P (disabled), GRUB_AS_LIST (term));
  210. }
  211. }
  212. }
  213. return GRUB_ERR_NONE;
  214. }
  215. static grub_err_t
  216. grub_cmd_terminal_input (grub_command_t cmd __attribute__ ((unused)),
  217. int argc, char **args)
  218. {
  219. (void) GRUB_FIELD_MATCH (grub_term_inputs, struct abstract_terminal *, next);
  220. (void) GRUB_FIELD_MATCH (grub_term_inputs, struct abstract_terminal *, prev);
  221. (void) GRUB_FIELD_MATCH (grub_term_inputs, struct abstract_terminal *, name);
  222. (void) GRUB_FIELD_MATCH (grub_term_inputs, struct abstract_terminal *, init);
  223. (void) GRUB_FIELD_MATCH (grub_term_inputs, struct abstract_terminal *, fini);
  224. return handle_command (argc, args,
  225. (struct abstract_terminal **) (void *) &grub_term_inputs,
  226. (struct abstract_terminal **) (void *) &grub_term_inputs_disabled,
  227. grub_term_input_autoload,
  228. N_("Active input terminals:"),
  229. N_("Available input terminals:"));
  230. }
  231. static grub_err_t
  232. grub_cmd_terminal_output (grub_command_t cmd __attribute__ ((unused)),
  233. int argc, char **args)
  234. {
  235. (void) GRUB_FIELD_MATCH (grub_term_outputs, struct abstract_terminal *, next);
  236. (void) GRUB_FIELD_MATCH (grub_term_outputs, struct abstract_terminal *, prev);
  237. (void) GRUB_FIELD_MATCH (grub_term_outputs, struct abstract_terminal *, name);
  238. (void) GRUB_FIELD_MATCH (grub_term_outputs, struct abstract_terminal *, init);
  239. (void) GRUB_FIELD_MATCH (grub_term_outputs, struct abstract_terminal *, fini);
  240. return handle_command (argc, args,
  241. (struct abstract_terminal **) (void *) &grub_term_outputs,
  242. (struct abstract_terminal **) (void *) &grub_term_outputs_disabled,
  243. grub_term_output_autoload,
  244. N_("Active output terminals:"),
  245. N_("Available output terminals:"));
  246. }
  247. static grub_command_t cmd_terminal_input, cmd_terminal_output;
  248. GRUB_MOD_INIT(terminal)
  249. {
  250. cmd_terminal_input =
  251. grub_register_command ("terminal_input", grub_cmd_terminal_input,
  252. N_("[--append|--remove] "
  253. "[TERMINAL1] [TERMINAL2] ..."),
  254. N_("List or select an input terminal."));
  255. cmd_terminal_output =
  256. grub_register_command ("terminal_output", grub_cmd_terminal_output,
  257. N_("[--append|--remove] "
  258. "[TERMINAL1] [TERMINAL2] ..."),
  259. N_("List or select an output terminal."));
  260. }
  261. GRUB_MOD_FINI(terminal)
  262. {
  263. grub_unregister_command (cmd_terminal_input);
  264. grub_unregister_command (cmd_terminal_output);
  265. }