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