terminfo.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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/extcmd.h>
  32. #include <grub/i18n.h>
  33. #include <grub/time.h>
  34. #if defined(__powerpc__) && defined(GRUB_MACHINE_IEEE1275)
  35. #include <grub/ieee1275/ieee1275.h>
  36. #endif
  37. GRUB_MOD_LICENSE ("GPLv3+");
  38. #define ANSI_CSI 0x9b
  39. #define ANSI_CSI_STR "\x9b"
  40. static struct grub_term_output *terminfo_outputs;
  41. /* Get current terminfo name. */
  42. char *
  43. grub_terminfo_get_current (struct grub_term_output *term)
  44. {
  45. struct grub_terminfo_output_state *data
  46. = (struct grub_terminfo_output_state *) term->data;
  47. return data->name;
  48. }
  49. /* Free *PTR and set *PTR to NULL, to prevent double-free. */
  50. static void
  51. grub_terminfo_free (char **ptr)
  52. {
  53. grub_free (*ptr);
  54. *ptr = 0;
  55. }
  56. static void
  57. grub_terminfo_all_free (struct grub_term_output *term)
  58. {
  59. struct grub_terminfo_output_state *data
  60. = (struct grub_terminfo_output_state *) term->data;
  61. /* Free previously allocated memory. */
  62. grub_terminfo_free (&data->name);
  63. grub_terminfo_free (&data->gotoxy);
  64. grub_terminfo_free (&data->cls);
  65. grub_terminfo_free (&data->reverse_video_on);
  66. grub_terminfo_free (&data->reverse_video_off);
  67. grub_terminfo_free (&data->cursor_on);
  68. grub_terminfo_free (&data->cursor_off);
  69. }
  70. /* Set current terminfo type. */
  71. grub_err_t
  72. grub_terminfo_set_current (struct grub_term_output *term,
  73. const char *str)
  74. {
  75. struct grub_terminfo_output_state *data
  76. = (struct grub_terminfo_output_state *) term->data;
  77. /* TODO
  78. * Lookup user specified terminfo type. If found, set term variables
  79. * as appropriate. Otherwise return an error.
  80. *
  81. * How should this be done?
  82. * a. A static table included in this module.
  83. * - I do not like this idea.
  84. * b. A table stored in the configuration directory.
  85. * - Users must convert their terminfo settings if we have not already.
  86. * c. Look for terminfo files in the configuration directory.
  87. * - /usr/share/terminfo is 6.3M on my system.
  88. * - /usr/share/terminfo is not on most users boot partition.
  89. * + Copying the terminfo files you want to use to the grub
  90. * configuration directory is easier then (b).
  91. * d. Your idea here.
  92. */
  93. grub_terminfo_all_free (term);
  94. if (grub_strcmp ("vt100", str) == 0)
  95. {
  96. data->name = grub_strdup ("vt100");
  97. data->gotoxy = grub_strdup ("\e[%i%p1%d;%p2%dH");
  98. data->cls = grub_strdup ("\e[H\e[J");
  99. data->reverse_video_on = grub_strdup ("\e[7m");
  100. data->reverse_video_off = grub_strdup ("\e[m");
  101. data->cursor_on = grub_strdup ("\e[?25h");
  102. data->cursor_off = grub_strdup ("\e[?25l");
  103. data->setcolor = NULL;
  104. return grub_errno;
  105. }
  106. if (grub_strcmp ("vt100-color", str) == 0)
  107. {
  108. data->name = grub_strdup ("vt100-color");
  109. data->gotoxy = grub_strdup ("\e[%i%p1%d;%p2%dH");
  110. data->cls = grub_strdup ("\e[H\e[J");
  111. data->reverse_video_on = grub_strdup ("\e[7m");
  112. data->reverse_video_off = grub_strdup ("\e[m");
  113. data->cursor_on = grub_strdup ("\e[?25h");
  114. data->cursor_off = grub_strdup ("\e[?25l");
  115. data->setcolor = grub_strdup ("\e[3%p1%dm\e[4%p2%dm");
  116. return grub_errno;
  117. }
  118. if (grub_strcmp ("arc", str) == 0)
  119. {
  120. data->name = grub_strdup ("arc");
  121. data->gotoxy = grub_strdup (ANSI_CSI_STR "%i%p1%d;%p2%dH");
  122. data->cls = grub_strdup (ANSI_CSI_STR "2J");
  123. data->reverse_video_on = grub_strdup (ANSI_CSI_STR "7m");
  124. data->reverse_video_off = grub_strdup (ANSI_CSI_STR "0m");
  125. data->cursor_on = 0;
  126. data->cursor_off = 0;
  127. data->setcolor = grub_strdup (ANSI_CSI_STR "3%p1%dm"
  128. ANSI_CSI_STR "4%p2%dm");
  129. return grub_errno;
  130. }
  131. if (grub_strcmp ("ieee1275", str) == 0
  132. || grub_strcmp ("ieee1275-nocursor", str) == 0)
  133. {
  134. data->name = grub_strdup ("ieee1275");
  135. data->gotoxy = grub_strdup ("\e[%i%p1%d;%p2%dH");
  136. /* Clear the screen. Using serial console, screen(1) only recognizes the
  137. * ANSI escape sequence. Using video console, Apple Open Firmware
  138. * (version 3.1.1) only recognizes the literal ^L. So use both. */
  139. data->cls = grub_strdup (" \e[2J");
  140. data->reverse_video_on = grub_strdup ("\e[7m");
  141. data->reverse_video_off = grub_strdup ("\e[m");
  142. if (grub_strcmp ("ieee1275", str) == 0)
  143. {
  144. data->cursor_on = grub_strdup ("\e[?25h");
  145. data->cursor_off = grub_strdup ("\e[?25l");
  146. }
  147. else
  148. {
  149. data->cursor_on = 0;
  150. data->cursor_off = 0;
  151. }
  152. data->setcolor = grub_strdup ("\e[3%p1%dm\e[4%p2%dm");
  153. return grub_errno;
  154. }
  155. if (grub_strcmp ("dumb", str) == 0)
  156. {
  157. data->name = grub_strdup ("dumb");
  158. data->gotoxy = NULL;
  159. data->cls = NULL;
  160. data->reverse_video_on = NULL;
  161. data->reverse_video_off = NULL;
  162. data->cursor_on = NULL;
  163. data->cursor_off = NULL;
  164. data->setcolor = NULL;
  165. return grub_errno;
  166. }
  167. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("unknown terminfo type `%s'"),
  168. str);
  169. }
  170. grub_err_t
  171. grub_terminfo_output_register (struct grub_term_output *term,
  172. const char *type)
  173. {
  174. grub_err_t err;
  175. struct grub_terminfo_output_state *data;
  176. err = grub_terminfo_set_current (term, type);
  177. if (err)
  178. return err;
  179. data = (struct grub_terminfo_output_state *) term->data;
  180. data->next = terminfo_outputs;
  181. terminfo_outputs = term;
  182. return GRUB_ERR_NONE;
  183. }
  184. grub_err_t
  185. grub_terminfo_output_unregister (struct grub_term_output *term)
  186. {
  187. struct grub_term_output **ptr;
  188. for (ptr = &terminfo_outputs; *ptr;
  189. ptr = &((struct grub_terminfo_output_state *) (*ptr)->data)->next)
  190. if (*ptr == term)
  191. {
  192. grub_terminfo_all_free (term);
  193. *ptr = ((struct grub_terminfo_output_state *) (*ptr)->data)->next;
  194. return GRUB_ERR_NONE;
  195. }
  196. return grub_error (GRUB_ERR_BUG, "terminal not found");
  197. }
  198. /* Wrapper for grub_putchar to write strings. */
  199. static void
  200. putstr (struct grub_term_output *term, const char *str)
  201. {
  202. struct grub_terminfo_output_state *data
  203. = (struct grub_terminfo_output_state *) term->data;
  204. while (*str)
  205. data->put (term, *str++);
  206. }
  207. struct grub_term_coordinate
  208. grub_terminfo_getxy (struct grub_term_output *term)
  209. {
  210. struct grub_terminfo_output_state *data
  211. = (struct grub_terminfo_output_state *) term->data;
  212. return data->pos;
  213. }
  214. void
  215. grub_terminfo_gotoxy (struct grub_term_output *term,
  216. struct grub_term_coordinate pos)
  217. {
  218. struct grub_terminfo_output_state *data
  219. = (struct grub_terminfo_output_state *) term->data;
  220. if (pos.x > grub_term_width (term) || pos.y > grub_term_height (term))
  221. {
  222. grub_error (GRUB_ERR_BUG, "invalid point (%u,%u)", pos.x, pos.y);
  223. return;
  224. }
  225. if (data->gotoxy)
  226. putstr (term, grub_terminfo_tparm (data->gotoxy, pos.y, pos.x));
  227. else
  228. {
  229. if ((pos.y == data->pos.y) && (pos.x == data->pos.x - 1))
  230. data->put (term, '\b');
  231. }
  232. data->pos = pos;
  233. }
  234. /* Clear the screen. */
  235. void
  236. grub_terminfo_cls (struct grub_term_output *term)
  237. {
  238. struct grub_terminfo_output_state *data
  239. = (struct grub_terminfo_output_state *) term->data;
  240. putstr (term, grub_terminfo_tparm (data->cls));
  241. grub_terminfo_gotoxy (term, (struct grub_term_coordinate) { 0, 0 });
  242. }
  243. void
  244. grub_terminfo_setcolorstate (struct grub_term_output *term,
  245. const grub_term_color_state state)
  246. {
  247. struct grub_terminfo_output_state *data
  248. = (struct grub_terminfo_output_state *) term->data;
  249. if (data->setcolor)
  250. {
  251. int fg;
  252. int bg;
  253. /* Map from VGA to terminal colors. */
  254. const int colormap[8]
  255. = { 0, /* Black. */
  256. 4, /* Blue. */
  257. 2, /* Green. */
  258. 6, /* Cyan. */
  259. 1, /* Red. */
  260. 5, /* Magenta. */
  261. 3, /* Yellow. */
  262. 7, /* White. */
  263. };
  264. switch (state)
  265. {
  266. case GRUB_TERM_COLOR_STANDARD:
  267. case GRUB_TERM_COLOR_NORMAL:
  268. fg = grub_term_normal_color & 0x0f;
  269. bg = grub_term_normal_color >> 4;
  270. break;
  271. case GRUB_TERM_COLOR_HIGHLIGHT:
  272. fg = grub_term_highlight_color & 0x0f;
  273. bg = grub_term_highlight_color >> 4;
  274. break;
  275. default:
  276. return;
  277. }
  278. putstr (term, grub_terminfo_tparm (data->setcolor, colormap[fg & 7],
  279. colormap[bg & 7]));
  280. return;
  281. }
  282. switch (state)
  283. {
  284. case GRUB_TERM_COLOR_STANDARD:
  285. case GRUB_TERM_COLOR_NORMAL:
  286. putstr (term, grub_terminfo_tparm (data->reverse_video_off));
  287. break;
  288. case GRUB_TERM_COLOR_HIGHLIGHT:
  289. putstr (term, grub_terminfo_tparm (data->reverse_video_on));
  290. break;
  291. default:
  292. break;
  293. }
  294. }
  295. void
  296. grub_terminfo_setcursor (struct grub_term_output *term, const int on)
  297. {
  298. struct grub_terminfo_output_state *data
  299. = (struct grub_terminfo_output_state *) term->data;
  300. if (on)
  301. putstr (term, grub_terminfo_tparm (data->cursor_on));
  302. else
  303. putstr (term, grub_terminfo_tparm (data->cursor_off));
  304. }
  305. /* The terminfo version of putchar. */
  306. void
  307. grub_terminfo_putchar (struct grub_term_output *term,
  308. const struct grub_unicode_glyph *c)
  309. {
  310. struct grub_terminfo_output_state *data
  311. = (struct grub_terminfo_output_state *) term->data;
  312. /* Keep track of the cursor. */
  313. switch (c->base)
  314. {
  315. case '\a':
  316. break;
  317. case '\b':
  318. case 127:
  319. if (data->pos.x > 0)
  320. data->pos.x--;
  321. break;
  322. case '\n':
  323. if (data->pos.y < grub_term_height (term) - 1)
  324. data->pos.y++;
  325. break;
  326. case '\r':
  327. data->pos.x = 0;
  328. break;
  329. default:
  330. if ((int) data->pos.x + c->estimated_width >= (int) grub_term_width (term) + 1)
  331. {
  332. data->pos.x = 0;
  333. if (data->pos.y < grub_term_height (term) - 1)
  334. data->pos.y++;
  335. data->put (term, '\r');
  336. data->put (term, '\n');
  337. }
  338. data->pos.x += c->estimated_width;
  339. break;
  340. }
  341. data->put (term, c->base);
  342. }
  343. struct grub_term_coordinate
  344. grub_terminfo_getwh (struct grub_term_output *term)
  345. {
  346. struct grub_terminfo_output_state *data
  347. = (struct grub_terminfo_output_state *) term->data;
  348. return data->size;
  349. }
  350. static void
  351. grub_terminfo_readkey (struct grub_term_input *term, int *keys, int *len, int max_len,
  352. int (*readkey) (struct grub_term_input *term))
  353. {
  354. int c;
  355. #define CONTINUE_READ \
  356. { \
  357. grub_uint64_t start; \
  358. /* On 9600 we have to wait up to 12 milliseconds. */ \
  359. start = grub_get_time_ms (); \
  360. do \
  361. c = readkey (term); \
  362. while (c == -1 && grub_get_time_ms () - start < 100); \
  363. if (c == -1) \
  364. return; \
  365. \
  366. if (*len >= max_len) \
  367. return; \
  368. \
  369. keys[*len] = c; \
  370. (*len)++; \
  371. }
  372. c = readkey (term);
  373. if (c < 0)
  374. {
  375. *len = 0;
  376. return;
  377. }
  378. *len = 1;
  379. keys[0] = c;
  380. if (c != ANSI_CSI && c != GRUB_TERM_ESC)
  381. {
  382. /* Backspace: Ctrl-h. */
  383. if (c == 0x7f)
  384. c = GRUB_TERM_BACKSPACE;
  385. if (c < 0x20 && c != GRUB_TERM_TAB && c!= GRUB_TERM_BACKSPACE && c != '\n' && c != '\r')
  386. c = GRUB_TERM_CTRL | (c - 1 + 'a');
  387. *len = 1;
  388. keys[0] = c;
  389. return;
  390. }
  391. {
  392. static struct
  393. {
  394. char key;
  395. unsigned ascii;
  396. }
  397. three_code_table[] =
  398. {
  399. {'4', GRUB_TERM_KEY_DC},
  400. {'A', GRUB_TERM_KEY_UP},
  401. {'B', GRUB_TERM_KEY_DOWN},
  402. {'C', GRUB_TERM_KEY_RIGHT},
  403. {'D', GRUB_TERM_KEY_LEFT},
  404. {'F', GRUB_TERM_KEY_END},
  405. {'H', GRUB_TERM_KEY_HOME},
  406. {'K', GRUB_TERM_KEY_END},
  407. {'P', GRUB_TERM_KEY_DC},
  408. {'?', GRUB_TERM_KEY_PPAGE},
  409. {'/', GRUB_TERM_KEY_NPAGE},
  410. {'@', GRUB_TERM_KEY_INSERT},
  411. };
  412. static unsigned four_code_table[] =
  413. {
  414. [1] = GRUB_TERM_KEY_HOME,
  415. [3] = GRUB_TERM_KEY_DC,
  416. [5] = GRUB_TERM_KEY_PPAGE,
  417. [6] = GRUB_TERM_KEY_NPAGE,
  418. [7] = GRUB_TERM_KEY_HOME,
  419. [8] = GRUB_TERM_KEY_END,
  420. [17] = GRUB_TERM_KEY_F6,
  421. [18] = GRUB_TERM_KEY_F7,
  422. [19] = GRUB_TERM_KEY_F8,
  423. [20] = GRUB_TERM_KEY_F9,
  424. [21] = GRUB_TERM_KEY_F10,
  425. [23] = GRUB_TERM_KEY_F11,
  426. [24] = GRUB_TERM_KEY_F12,
  427. };
  428. char fx_key[] =
  429. { 'P', 'Q', 'w', 'x', 't', 'u',
  430. 'q', 'r', 'p', 'M', 'A', 'B', 'H', 'F' };
  431. unsigned fx_code[] =
  432. { GRUB_TERM_KEY_F1, GRUB_TERM_KEY_F2, GRUB_TERM_KEY_F3,
  433. GRUB_TERM_KEY_F4, GRUB_TERM_KEY_F5, GRUB_TERM_KEY_F6,
  434. GRUB_TERM_KEY_F7, GRUB_TERM_KEY_F8, GRUB_TERM_KEY_F9,
  435. GRUB_TERM_KEY_F10, GRUB_TERM_KEY_F11, GRUB_TERM_KEY_F12,
  436. GRUB_TERM_KEY_HOME, GRUB_TERM_KEY_END };
  437. unsigned i;
  438. if (c == GRUB_TERM_ESC)
  439. {
  440. CONTINUE_READ;
  441. if (c == 'O')
  442. {
  443. CONTINUE_READ;
  444. for (i = 0; i < ARRAY_SIZE (fx_key); i++)
  445. if (fx_key[i] == c)
  446. {
  447. keys[0] = fx_code[i];
  448. *len = 1;
  449. return;
  450. }
  451. }
  452. if (c != '[')
  453. return;
  454. }
  455. CONTINUE_READ;
  456. for (i = 0; i < ARRAY_SIZE (three_code_table); i++)
  457. if (three_code_table[i].key == c)
  458. {
  459. keys[0] = three_code_table[i].ascii;
  460. *len = 1;
  461. return;
  462. }
  463. switch (c)
  464. {
  465. case '[':
  466. CONTINUE_READ;
  467. if (c >= 'A' && c <= 'E')
  468. {
  469. keys[0] = GRUB_TERM_KEY_F1 + c - 'A';
  470. *len = 1;
  471. return;
  472. }
  473. return;
  474. case 'O':
  475. CONTINUE_READ;
  476. for (i = 0; i < ARRAY_SIZE (fx_key); i++)
  477. if (fx_key[i] == c)
  478. {
  479. keys[0] = fx_code[i];
  480. *len = 1;
  481. return;
  482. }
  483. return;
  484. case '0':
  485. {
  486. int num = 0;
  487. CONTINUE_READ;
  488. if (c != '0' && c != '1')
  489. return;
  490. num = (c - '0') * 10;
  491. CONTINUE_READ;
  492. if (c < '0' || c > '9')
  493. return;
  494. num += (c - '0');
  495. if (num == 0 || num > 12)
  496. return;
  497. CONTINUE_READ;
  498. if (c != 'q')
  499. return;
  500. keys[0] = fx_code[num - 1];
  501. *len = 1;
  502. return;
  503. }
  504. case '1' ... '9':
  505. {
  506. unsigned val = c - '0';
  507. CONTINUE_READ;
  508. if (c >= '0' && c <= '9')
  509. {
  510. val = val * 10 + (c - '0');
  511. CONTINUE_READ;
  512. }
  513. if (c != '~')
  514. return;
  515. if (val >= ARRAY_SIZE (four_code_table)
  516. || four_code_table[val] == 0)
  517. return;
  518. keys[0] = four_code_table[val];
  519. *len = 1;
  520. return;
  521. }
  522. default:
  523. return;
  524. }
  525. }
  526. #undef CONTINUE_READ
  527. }
  528. /* The terminfo version of getkey. */
  529. int
  530. grub_terminfo_getkey (struct grub_term_input *termi)
  531. {
  532. struct grub_terminfo_input_state *data
  533. = (struct grub_terminfo_input_state *) (termi->data);
  534. if (data->npending)
  535. {
  536. int ret;
  537. data->npending--;
  538. ret = data->input_buf[0];
  539. grub_memmove (data->input_buf, data->input_buf + 1, data->npending
  540. * sizeof (data->input_buf[0]));
  541. return ret;
  542. }
  543. grub_terminfo_readkey (termi, data->input_buf, &data->npending,
  544. GRUB_TERMINFO_READKEY_MAX_LEN, data->readkey);
  545. #if defined(__powerpc__) && defined(GRUB_MACHINE_IEEE1275)
  546. if (data->npending == 1 && data->input_buf[0] == GRUB_TERM_ESC
  547. && grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_BROKEN_REPEAT)
  548. && grub_get_time_ms () - data->last_key_time < 1000
  549. && (data->last_key & GRUB_TERM_EXTENDED))
  550. {
  551. data->npending = 0;
  552. data->last_key_time = grub_get_time_ms ();
  553. return data->last_key;
  554. }
  555. #endif
  556. if (data->npending)
  557. {
  558. int ret;
  559. data->npending--;
  560. ret = data->input_buf[0];
  561. #if defined(__powerpc__) && defined(GRUB_MACHINE_IEEE1275)
  562. if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_BROKEN_REPEAT))
  563. {
  564. data->last_key = ret;
  565. data->last_key_time = grub_get_time_ms ();
  566. }
  567. #endif
  568. grub_memmove (data->input_buf, data->input_buf + 1, data->npending
  569. * sizeof (data->input_buf[0]));
  570. return ret;
  571. }
  572. return GRUB_TERM_NO_KEY;
  573. }
  574. grub_err_t
  575. grub_terminfo_input_init (struct grub_term_input *termi)
  576. {
  577. struct grub_terminfo_input_state *data
  578. = (struct grub_terminfo_input_state *) (termi->data);
  579. data->npending = 0;
  580. return GRUB_ERR_NONE;
  581. }
  582. grub_err_t
  583. grub_terminfo_output_init (struct grub_term_output *term)
  584. {
  585. grub_terminfo_cls (term);
  586. return GRUB_ERR_NONE;
  587. }
  588. /* GRUB Command. */
  589. static grub_err_t
  590. print_terminfo (void)
  591. {
  592. const char *encoding_names[(GRUB_TERM_CODE_TYPE_MASK
  593. >> GRUB_TERM_CODE_TYPE_SHIFT) + 1]
  594. = {
  595. /* VGA and glyph descriptor types are just for completeness,
  596. they are not used on terminfo terminals.
  597. */
  598. [GRUB_TERM_CODE_TYPE_ASCII >> GRUB_TERM_CODE_TYPE_SHIFT] = _("ASCII"),
  599. [GRUB_TERM_CODE_TYPE_CP437 >> GRUB_TERM_CODE_TYPE_SHIFT] = "CP-437",
  600. [GRUB_TERM_CODE_TYPE_UTF8_LOGICAL >> GRUB_TERM_CODE_TYPE_SHIFT]
  601. = _("UTF-8"),
  602. [GRUB_TERM_CODE_TYPE_UTF8_VISUAL >> GRUB_TERM_CODE_TYPE_SHIFT]
  603. /* TRANSLATORS: visually ordered UTF-8 is a non-compliant encoding
  604. based on UTF-8 with right-to-left languages written in reverse.
  605. Used on some terminals. Normal UTF-8 is refered as
  606. "logically-ordered UTF-8" by opposition. */
  607. = _("visually-ordered UTF-8"),
  608. [GRUB_TERM_CODE_TYPE_VISUAL_GLYPHS >> GRUB_TERM_CODE_TYPE_SHIFT]
  609. = "Glyph descriptors",
  610. _("Unknown encoding"), _("Unknown encoding"), _("Unknown encoding")
  611. };
  612. struct grub_term_output *cur;
  613. grub_puts_ (N_("Current terminfo types:"));
  614. for (cur = terminfo_outputs; cur;
  615. cur = ((struct grub_terminfo_output_state *) cur->data)->next)
  616. grub_printf ("%s: %s\t%s\t%dx%d\n", cur->name,
  617. grub_terminfo_get_current(cur),
  618. encoding_names[(cur->flags & GRUB_TERM_CODE_TYPE_MASK)
  619. >> GRUB_TERM_CODE_TYPE_SHIFT],
  620. ((struct grub_terminfo_output_state *) cur->data)->pos.x,
  621. ((struct grub_terminfo_output_state *) cur->data)->pos.y);
  622. return GRUB_ERR_NONE;
  623. }
  624. static const struct grub_arg_option options[] =
  625. {
  626. {"ascii", 'a', 0, N_("Terminal is ASCII-only [default]."), 0, ARG_TYPE_NONE},
  627. {"utf8", 'u', 0, N_("Terminal is logical-ordered UTF-8."), 0, ARG_TYPE_NONE},
  628. {"visual-utf8", 'v', 0, N_("Terminal is visually-ordered UTF-8."), 0,
  629. ARG_TYPE_NONE},
  630. {"geometry", 'g', 0, N_("Terminal has specified geometry."),
  631. /* TRANSLATORS: "x" has to be entered in, like an identifier, so please don't
  632. use better Unicode codepoints. */
  633. N_("WIDTHxHEIGHT."), ARG_TYPE_STRING},
  634. {0, 0, 0, 0, 0, 0}
  635. };
  636. enum
  637. {
  638. OPTION_ASCII,
  639. OPTION_UTF8,
  640. OPTION_VISUAL_UTF8,
  641. OPTION_GEOMETRY
  642. };
  643. static grub_err_t
  644. grub_cmd_terminfo (grub_extcmd_context_t ctxt, int argc, char **args)
  645. {
  646. struct grub_term_output *cur;
  647. int encoding = GRUB_TERM_CODE_TYPE_ASCII;
  648. struct grub_arg_list *state = ctxt->state;
  649. int w = 0, h = 0;
  650. if (argc == 0)
  651. return print_terminfo ();
  652. if (state[OPTION_ASCII].set)
  653. encoding = GRUB_TERM_CODE_TYPE_ASCII;
  654. if (state[OPTION_UTF8].set)
  655. encoding = GRUB_TERM_CODE_TYPE_UTF8_LOGICAL;
  656. if (state[OPTION_VISUAL_UTF8].set)
  657. encoding = GRUB_TERM_CODE_TYPE_UTF8_VISUAL;
  658. if (state[OPTION_GEOMETRY].set)
  659. {
  660. const char *ptr = state[OPTION_GEOMETRY].arg;
  661. w = grub_strtoul (ptr, &ptr, 0);
  662. if (grub_errno)
  663. return grub_errno;
  664. if (*ptr != 'x')
  665. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  666. N_("incorrect terminal dimensions specification"));
  667. ptr++;
  668. h = grub_strtoul (ptr, &ptr, 0);
  669. if (grub_errno)
  670. return grub_errno;
  671. }
  672. for (cur = terminfo_outputs; cur;
  673. cur = ((struct grub_terminfo_output_state *) cur->data)->next)
  674. if (grub_strcmp (args[0], cur->name) == 0
  675. || (grub_strcmp (args[0], "ofconsole") == 0
  676. && grub_strcmp ("console", cur->name) == 0))
  677. {
  678. cur->flags = (cur->flags & ~GRUB_TERM_CODE_TYPE_MASK) | encoding;
  679. if (w && h)
  680. {
  681. struct grub_terminfo_output_state *data
  682. = (struct grub_terminfo_output_state *) cur->data;
  683. data->size.x = w;
  684. data->size.y = h;
  685. }
  686. if (argc == 1)
  687. return GRUB_ERR_NONE;
  688. return grub_terminfo_set_current (cur, args[1]);
  689. }
  690. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  691. N_("terminal %s isn't found or it's not handled by terminfo"),
  692. args[0]);
  693. }
  694. static grub_extcmd_t cmd;
  695. GRUB_MOD_INIT(terminfo)
  696. {
  697. cmd = grub_register_extcmd ("terminfo", grub_cmd_terminfo, 0,
  698. N_("[[-a|-u|-v] [-g WxH] [TERM] [TYPE]]"),
  699. N_("Set terminfo type of TERM to TYPE.\n"),
  700. options);
  701. }
  702. GRUB_MOD_FINI(terminfo)
  703. {
  704. grub_unregister_extcmd (cmd);
  705. }