1
/* terminfo.c - simple terminfo module */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2003,2004,2005,2007 Free Software Foundation, Inc.
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.
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.
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/>.
21
* This file contains various functions dealing with different
22
* terminal capabilities. For example, vt52 and vt100.
25
#include <grub/types.h>
26
#include <grub/misc.h>
30
#include <grub/term.h>
31
#include <grub/terminfo.h>
32
#include <grub/tparm.h>
33
#include <grub/command.h>
34
#include <grub/i18n.h>
35
#include <grub/time.h>
37
static struct grub_term_output *terminfo_outputs;
39
/* Get current terminfo name. */
41
grub_terminfo_get_current (struct grub_term_output *term)
43
struct grub_terminfo_output_state *data
44
= (struct grub_terminfo_output_state *) term->data;
48
/* Free *PTR and set *PTR to NULL, to prevent double-free. */
50
grub_terminfo_free (char **ptr)
57
grub_terminfo_all_free (struct grub_term_output *term)
59
struct grub_terminfo_output_state *data
60
= (struct grub_terminfo_output_state *) term->data;
62
/* Free previously allocated memory. */
63
grub_terminfo_free (&data->name);
64
grub_terminfo_free (&data->gotoxy);
65
grub_terminfo_free (&data->cls);
66
grub_terminfo_free (&data->reverse_video_on);
67
grub_terminfo_free (&data->reverse_video_off);
68
grub_terminfo_free (&data->cursor_on);
69
grub_terminfo_free (&data->cursor_off);
72
/* Set current terminfo type. */
74
grub_terminfo_set_current (struct grub_term_output *term,
77
struct grub_terminfo_output_state *data
78
= (struct grub_terminfo_output_state *) term->data;
80
* Lookup user specified terminfo type. If found, set term variables
81
* as appropriate. Otherwise return an error.
83
* How should this be done?
84
* a. A static table included in this module.
85
* - I do not like this idea.
86
* b. A table stored in the configuration directory.
87
* - Users must convert their terminfo settings if we have not already.
88
* c. Look for terminfo files in the configuration directory.
89
* - /usr/share/terminfo is 6.3M on my system.
90
* - /usr/share/terminfo is not on most users boot partition.
91
* + Copying the terminfo files you want to use to the grub
92
* configuration directory is easier then (b).
96
grub_terminfo_all_free (term);
98
if (grub_strcmp ("vt100", str) == 0)
100
data->name = grub_strdup ("vt100");
101
data->gotoxy = grub_strdup ("\e[%i%p1%d;%p2%dH");
102
data->cls = grub_strdup ("\e[H\e[J");
103
data->reverse_video_on = grub_strdup ("\e[7m");
104
data->reverse_video_off = grub_strdup ("\e[m");
105
data->cursor_on = grub_strdup ("\e[?25h");
106
data->cursor_off = grub_strdup ("\e[?25l");
107
data->setcolor = NULL;
111
if (grub_strcmp ("vt100-color", str) == 0)
113
data->name = grub_strdup ("vt100-color");
114
data->gotoxy = grub_strdup ("\e[%i%p1%d;%p2%dH");
115
data->cls = grub_strdup ("\e[H\e[J");
116
data->reverse_video_on = grub_strdup ("\e[7m");
117
data->reverse_video_off = grub_strdup ("\e[m");
118
data->cursor_on = grub_strdup ("\e[?25h");
119
data->cursor_off = grub_strdup ("\e[?25l");
120
data->setcolor = grub_strdup ("\e[3%p1%dm\e[4%p2%dm");
124
if (grub_strcmp ("ieee1275", str) == 0)
126
data->name = grub_strdup ("ieee1275");
127
data->gotoxy = grub_strdup ("\e[%i%p1%d;%p2%dH");
128
/* Clear the screen. Using serial console, screen(1) only recognizes the
129
* ANSI escape sequence. Using video console, Apple Open Firmware
130
* (version 3.1.1) only recognizes the literal ^L. So use both. */
131
data->cls = grub_strdup ("\e[2J");
132
data->reverse_video_on = grub_strdup ("\e[7m");
133
data->reverse_video_off = grub_strdup ("\e[m");
134
data->cursor_on = grub_strdup ("\e[?25h");
135
data->cursor_off = grub_strdup ("\e[?25l");
136
data->setcolor = grub_strdup ("\e[3%p1%dm\e[4%p2%dm");
140
if (grub_strcmp ("dumb", str) == 0)
142
data->name = grub_strdup ("dumb");
145
data->reverse_video_on = NULL;
146
data->reverse_video_off = NULL;
147
data->cursor_on = NULL;
148
data->cursor_off = NULL;
149
data->setcolor = NULL;
153
return grub_error (GRUB_ERR_BAD_ARGUMENT, "unknown terminfo type");
157
grub_terminfo_output_register (struct grub_term_output *term,
161
struct grub_terminfo_output_state *data;
163
err = grub_terminfo_set_current (term, type);
168
data = (struct grub_terminfo_output_state *) term->data;
169
data->next = terminfo_outputs;
170
terminfo_outputs = term;
172
return GRUB_ERR_NONE;
176
grub_terminfo_output_unregister (struct grub_term_output *term)
178
struct grub_term_output **ptr;
180
for (ptr = &terminfo_outputs; *ptr;
181
ptr = &((struct grub_terminfo_output_state *) (*ptr)->data)->next)
184
grub_terminfo_all_free (term);
185
*ptr = ((struct grub_terminfo_output_state *) (*ptr)->data)->next;
186
return GRUB_ERR_NONE;
188
return grub_error (GRUB_ERR_BAD_ARGUMENT, "terminal not found");
191
/* Wrapper for grub_putchar to write strings. */
193
putstr (struct grub_term_output *term, const char *str)
195
struct grub_terminfo_output_state *data
196
= (struct grub_terminfo_output_state *) term->data;
202
grub_terminfo_getxy (struct grub_term_output *term)
204
struct grub_terminfo_output_state *data
205
= (struct grub_terminfo_output_state *) term->data;
207
return ((data->xpos << 8) | data->ypos);
211
grub_terminfo_gotoxy (struct grub_term_output *term,
212
grub_uint8_t x, grub_uint8_t y)
214
struct grub_terminfo_output_state *data
215
= (struct grub_terminfo_output_state *) term->data;
217
if (x > grub_term_width (term) || y > grub_term_height (term))
219
grub_error (GRUB_ERR_OUT_OF_RANGE, "invalid point (%u,%u)", x, y);
224
putstr (term, grub_terminfo_tparm (data->gotoxy, y, x));
227
if ((y == data->ypos) && (x == data->xpos - 1))
235
/* Clear the screen. */
237
grub_terminfo_cls (struct grub_term_output *term)
239
struct grub_terminfo_output_state *data
240
= (struct grub_terminfo_output_state *) term->data;
242
putstr (term, grub_terminfo_tparm (data->cls));
244
data->xpos = data->ypos = 0;
248
grub_terminfo_setcolorstate (struct grub_term_output *term,
249
const grub_term_color_state state)
251
struct grub_terminfo_output_state *data
252
= (struct grub_terminfo_output_state *) term->data;
258
/* Map from VGA to terminal colors. */
259
const int colormap[8]
272
case GRUB_TERM_COLOR_STANDARD:
273
case GRUB_TERM_COLOR_NORMAL:
274
fg = term->normal_color & 0x0f;
275
bg = term->normal_color >> 4;
277
case GRUB_TERM_COLOR_HIGHLIGHT:
278
fg = term->highlight_color & 0x0f;
279
bg = term->highlight_color >> 4;
285
putstr (term, grub_terminfo_tparm (data->setcolor, colormap[fg & 7],
292
case GRUB_TERM_COLOR_STANDARD:
293
case GRUB_TERM_COLOR_NORMAL:
294
putstr (term, grub_terminfo_tparm (data->reverse_video_off));
296
case GRUB_TERM_COLOR_HIGHLIGHT:
297
putstr (term, grub_terminfo_tparm (data->reverse_video_on));
305
grub_terminfo_setcursor (struct grub_term_output *term, const int on)
307
struct grub_terminfo_output_state *data
308
= (struct grub_terminfo_output_state *) term->data;
311
putstr (term, grub_terminfo_tparm (data->cursor_on));
313
putstr (term, grub_terminfo_tparm (data->cursor_off));
316
/* The terminfo version of putchar. */
318
grub_terminfo_putchar (struct grub_term_output *term,
319
const struct grub_unicode_glyph *c)
321
struct grub_terminfo_output_state *data
322
= (struct grub_terminfo_output_state *) term->data;
324
/* Keep track of the cursor. */
337
if (data->ypos < grub_term_height (term) - 1)
346
if (data->xpos + c->estimated_width >= grub_term_width (term) + 1)
349
if (data->ypos < grub_term_height (term) - 1)
354
data->xpos += c->estimated_width;
364
grub_terminfo_readkey (int *keys, int *len, int (*readkey) (void))
368
#define CONTINUE_READ \
370
grub_uint64_t start; \
371
/* On 9600 we have to wait up to 12 milliseconds. */ \
372
start = grub_get_time_ms (); \
375
while (c == -1 && grub_get_time_ms () - start < 12); \
391
if (c != ANSI_C0 && c != '\e')
393
/* Backspace: Ctrl-h. */
411
{'B', GRUB_TERM_DOWN},
412
{'C', GRUB_TERM_RIGHT},
413
{'D', GRUB_TERM_LEFT},
414
{'F', GRUB_TERM_END},
415
{'H', GRUB_TERM_HOME},
416
{'K', GRUB_TERM_END},
418
{'?', GRUB_TERM_PPAGE},
419
{'/', GRUB_TERM_NPAGE}
429
{'1', GRUB_TERM_HOME},
431
{'5', GRUB_TERM_PPAGE},
432
{'6', GRUB_TERM_NPAGE}
446
for (i = 0; i < ARRAY_SIZE (three_code_table); i++)
447
if (three_code_table[i].key == c)
449
keys[0] = three_code_table[i].ascii;
454
for (i = 0; i < ARRAY_SIZE (four_code_table); i++)
455
if (four_code_table[i].key == c)
460
keys[0] = three_code_table[i].ascii;
469
/* The terminfo version of checkkey. */
471
grub_terminfo_checkkey (struct grub_term_input *termi)
473
struct grub_terminfo_input_state *data
474
= (struct grub_terminfo_input_state *) (termi->data);
476
return data->input_buf[0];
478
grub_terminfo_readkey (data->input_buf, &data->npending, data->readkey);
481
return data->input_buf[0];
486
/* The terminfo version of getkey. */
488
grub_terminfo_getkey (struct grub_term_input *termi)
490
struct grub_terminfo_input_state *data
491
= (struct grub_terminfo_input_state *) (termi->data);
493
while (! data->npending)
494
grub_terminfo_readkey (data->input_buf, &data->npending, data->readkey);
496
ret = data->input_buf[0];
498
grub_memmove (data->input_buf, data->input_buf + 1, data->npending);
503
grub_terminfo_input_init (struct grub_term_input *termi)
505
struct grub_terminfo_input_state *data
506
= (struct grub_terminfo_input_state *) (termi->data);
509
return GRUB_ERR_NONE;
515
print_terminfo (void)
517
const char *encoding_names[(GRUB_TERM_CODE_TYPE_MASK
518
>> GRUB_TERM_CODE_TYPE_SHIFT) + 1]
520
/* VGA and glyph descriptor types are just for completeness,
521
they are not used on terminfo terminals.
523
[GRUB_TERM_CODE_TYPE_ASCII >> GRUB_TERM_CODE_TYPE_SHIFT] = _("ASCII"),
524
[GRUB_TERM_CODE_TYPE_CP437 >> GRUB_TERM_CODE_TYPE_SHIFT] = "CP-437",
525
[GRUB_TERM_CODE_TYPE_UTF8_LOGICAL >> GRUB_TERM_CODE_TYPE_SHIFT]
527
[GRUB_TERM_CODE_TYPE_UTF8_VISUAL >> GRUB_TERM_CODE_TYPE_SHIFT]
529
[GRUB_TERM_CODE_TYPE_VISUAL_GLYPHS >> GRUB_TERM_CODE_TYPE_SHIFT]
530
= "Glyph descriptors",
531
_("Unknown"), _("Unknown"), _("Unknown")
533
struct grub_term_output *cur;
535
grub_printf ("Current terminfo types: \n");
536
for (cur = terminfo_outputs; cur;
537
cur = ((struct grub_terminfo_output_state *) cur->data)->next)
538
grub_printf ("%s: %s\t%s\n", cur->name,
539
grub_terminfo_get_current(cur),
540
encoding_names[(cur->flags & GRUB_TERM_CODE_TYPE_MASK)
541
>> GRUB_TERM_CODE_TYPE_SHIFT]);
543
return GRUB_ERR_NONE;
547
grub_cmd_terminfo (grub_command_t cmd __attribute__ ((unused)),
548
int argc, char **args)
550
struct grub_term_output *cur;
551
int encoding = GRUB_TERM_CODE_TYPE_ASCII;
553
char *name = NULL, *type = NULL;
556
return print_terminfo ();
558
for (i = 0; i < argc; i++)
560
if (grub_strcmp (args[i], "-a") == 0
561
|| grub_strcmp (args[i], "--ascii") == 0)
563
encoding = GRUB_TERM_CODE_TYPE_ASCII;
566
if (grub_strcmp (args[i], "-u") == 0
567
|| grub_strcmp (args[i], "--utf8") == 0)
569
encoding = GRUB_TERM_CODE_TYPE_UTF8_LOGICAL;
572
if (grub_strcmp (args[i], "-v") == 0
573
|| grub_strcmp (args[i], "--visual-utf8") == 0)
575
encoding = GRUB_TERM_CODE_TYPE_UTF8_VISUAL;
589
return grub_error (GRUB_ERR_BAD_ARGUMENT, "too many parameters");
593
return grub_error (GRUB_ERR_BAD_ARGUMENT, "too few parameters");
595
for (cur = terminfo_outputs; cur;
596
cur = ((struct grub_terminfo_output_state *) cur->data)->next)
597
if (grub_strcmp (name, cur->name) == 0)
599
cur->flags = (cur->flags & ~GRUB_TERM_CODE_TYPE_MASK) | encoding;
601
return GRUB_ERR_NONE;
603
return grub_terminfo_set_current (cur, type);
605
return grub_error (GRUB_ERR_BAD_ARGUMENT,
606
"no terminal %s found or it's not handled by terminfo",
610
static grub_command_t cmd;
612
GRUB_MOD_INIT(terminfo)
614
cmd = grub_register_command ("terminfo", grub_cmd_terminfo,
615
N_("[[-a|-u|-v] TERM [TYPE]]"),
616
N_("Set terminfo type of TERM to TYPE.\n"
617
"-a, --ascii Terminal is ASCII-only [default].\n"
618
"-u, --utf8 Terminal is logical-ordered UTF-8.\n"
619
"-v, --visual-utf8 Terminal is visually-ordered UTF-8.")
624
GRUB_MOD_FINI(terminfo)
626
grub_unregister_command (cmd);