17c478bd9Sstevel@tonic-gate /* terminfo.c - read a terminfo entry from the command line */
27c478bd9Sstevel@tonic-gate /*
37c478bd9Sstevel@tonic-gate * GRUB -- GRand Unified Bootloader
47c478bd9Sstevel@tonic-gate * Copyright (C) 2002,2004 Free Software Foundation, Inc.
57c478bd9Sstevel@tonic-gate *
67c478bd9Sstevel@tonic-gate * This program is free software; you can redistribute it and/or modify
77c478bd9Sstevel@tonic-gate * it under the terms of the GNU General Public License as published by
87c478bd9Sstevel@tonic-gate * the Free Software Foundation; either version 2 of the License, or
97c478bd9Sstevel@tonic-gate * (at your option) any later version.
107c478bd9Sstevel@tonic-gate *
117c478bd9Sstevel@tonic-gate * This program is distributed in the hope that it will be useful,
127c478bd9Sstevel@tonic-gate * but WITHOUT ANY WARRANTY; without even the implied warranty of
137c478bd9Sstevel@tonic-gate * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
147c478bd9Sstevel@tonic-gate * GNU General Public License for more details.
157c478bd9Sstevel@tonic-gate *
167c478bd9Sstevel@tonic-gate * You should have received a copy of the GNU General Public License
177c478bd9Sstevel@tonic-gate * along with this program; if not, write to the Free Software
187c478bd9Sstevel@tonic-gate * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * ######################################################################
217c478bd9Sstevel@tonic-gate *
227c478bd9Sstevel@tonic-gate * This file contains various functions dealing with different
237c478bd9Sstevel@tonic-gate * terminal capabilities. It knows the difference between a vt52 and vt100
247c478bd9Sstevel@tonic-gate * terminal (and much more) and is mainly used the terminal emulation
257c478bd9Sstevel@tonic-gate * in the serial driver.
267c478bd9Sstevel@tonic-gate */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <shared.h>
297c478bd9Sstevel@tonic-gate #include "terminfo.h"
307c478bd9Sstevel@tonic-gate #include "tparm.h"
317c478bd9Sstevel@tonic-gate #include "serial.h"
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate /* Current terminal capabilities. Default is "vt100". */
347c478bd9Sstevel@tonic-gate struct terminfo term =
357c478bd9Sstevel@tonic-gate {
367c478bd9Sstevel@tonic-gate .name = "vt100",
377c478bd9Sstevel@tonic-gate .cursor_address = "\e[%i%p1%d;%p2%dH",
387c478bd9Sstevel@tonic-gate .clear_screen = "\e[H\e[J",
397c478bd9Sstevel@tonic-gate .enter_standout_mode = "\e[7m",
407c478bd9Sstevel@tonic-gate .exit_standout_mode = "\e[m"
417c478bd9Sstevel@tonic-gate };
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate /* A number of escape sequences are provided in the string valued
447c478bd9Sstevel@tonic-gate capabilities for easy encoding of characters there. Both \E and \e
457c478bd9Sstevel@tonic-gate map to an ESCAPE character, ^x maps to a control-x for any
467c478bd9Sstevel@tonic-gate appropriate x, and the sequences \n \l \r \t \b \f \s give a
477c478bd9Sstevel@tonic-gate newline, line-feed, return, tab, backspace, form-feed, and space.
487c478bd9Sstevel@tonic-gate Other escapes include \^ for ^, \\ for \, \, for comma, \: for :,
497c478bd9Sstevel@tonic-gate and \0 for null. (\0 will produce \200, which does not terminate a
507c478bd9Sstevel@tonic-gate string but behaves as a null character on most terminals, provid�
517c478bd9Sstevel@tonic-gate ing CS7 is specified. See stty(1).) Finally, characters may be
527c478bd9Sstevel@tonic-gate given as three octal digits after a \. */
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate char *
ti_unescape_memory(const char * in,const char * end)557c478bd9Sstevel@tonic-gate ti_unescape_memory (const char *in, const char *end)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate static char out_buffer[256];
587c478bd9Sstevel@tonic-gate char c;
597c478bd9Sstevel@tonic-gate char *out;
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate out = out_buffer;
627c478bd9Sstevel@tonic-gate do
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate c = *(in++);
657c478bd9Sstevel@tonic-gate switch (c)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate case '^':
687c478bd9Sstevel@tonic-gate if (*in >= 'A' && *in <= 'Z')
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate *out = (*in) - 'A';
717c478bd9Sstevel@tonic-gate in++;
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate else
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate *out = '^';
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate break;
787c478bd9Sstevel@tonic-gate case '\\':
797c478bd9Sstevel@tonic-gate c = *(in++);
807c478bd9Sstevel@tonic-gate if (c >= '0' && c <= '9')
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate // octal number
837c478bd9Sstevel@tonic-gate int n = 0;
847c478bd9Sstevel@tonic-gate do
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate n = (n << 4) | (c - '0');
877c478bd9Sstevel@tonic-gate c = *(in++);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate while (c >= '0' && c <= '9');
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate *out++ = (char)(n & 0xff);
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate // redo last character
947c478bd9Sstevel@tonic-gate in--;
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate break;
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate switch (c)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate case 'e':
1027c478bd9Sstevel@tonic-gate case 'E':
1037c478bd9Sstevel@tonic-gate *out++ = '\e';
1047c478bd9Sstevel@tonic-gate break;
1057c478bd9Sstevel@tonic-gate case 'n':
1067c478bd9Sstevel@tonic-gate *out++ = '\n';
1077c478bd9Sstevel@tonic-gate break;
1087c478bd9Sstevel@tonic-gate case 'r':
1097c478bd9Sstevel@tonic-gate *out++ = '\r';
1107c478bd9Sstevel@tonic-gate break;
1117c478bd9Sstevel@tonic-gate case 't':
1127c478bd9Sstevel@tonic-gate *out++ = '\t';
1137c478bd9Sstevel@tonic-gate break;
1147c478bd9Sstevel@tonic-gate case 'b':
1157c478bd9Sstevel@tonic-gate *out++ = '\b';
1167c478bd9Sstevel@tonic-gate break;
1177c478bd9Sstevel@tonic-gate case 'f':
1187c478bd9Sstevel@tonic-gate *out++ = '\f';
1197c478bd9Sstevel@tonic-gate break;
1207c478bd9Sstevel@tonic-gate case 's':
1217c478bd9Sstevel@tonic-gate *out++ = ' ';
1227c478bd9Sstevel@tonic-gate break;
1237c478bd9Sstevel@tonic-gate case '\\':
1247c478bd9Sstevel@tonic-gate *out++ = '\\';
1257c478bd9Sstevel@tonic-gate break;
1267c478bd9Sstevel@tonic-gate case '^':
1277c478bd9Sstevel@tonic-gate *out++ = '^';
1287c478bd9Sstevel@tonic-gate break;
1297c478bd9Sstevel@tonic-gate case ',':
1307c478bd9Sstevel@tonic-gate *out++ = ',';
1317c478bd9Sstevel@tonic-gate break;
1327c478bd9Sstevel@tonic-gate case ':':
1337c478bd9Sstevel@tonic-gate *out++ = ':';
1347c478bd9Sstevel@tonic-gate break;
1357c478bd9Sstevel@tonic-gate case '0':
1367c478bd9Sstevel@tonic-gate *out++ = '\200';
1377c478bd9Sstevel@tonic-gate break;
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate break;
1407c478bd9Sstevel@tonic-gate default:
1417c478bd9Sstevel@tonic-gate *out++ = c;
1427c478bd9Sstevel@tonic-gate break;
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate while (in <= end);
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate return out_buffer;
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate char *
ti_unescape_string(const char * in)1517c478bd9Sstevel@tonic-gate ti_unescape_string (const char *in)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate return ti_unescape_memory (in, in + grub_strlen (in));
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate /* convert a memory region containing binary character into an external
1577c478bd9Sstevel@tonic-gate * ascii representation. The binary characters will be replaced by an
1587c478bd9Sstevel@tonic-gate * "ecsape notation". E.g. "033" will become "\e". */
1597c478bd9Sstevel@tonic-gate char *
ti_escape_memory(const char * in,const char * end)1607c478bd9Sstevel@tonic-gate ti_escape_memory (const char *in, const char *end)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate static char out_buffer[256];
1637c478bd9Sstevel@tonic-gate char c;
1647c478bd9Sstevel@tonic-gate char *out;
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate out = out_buffer;
1677c478bd9Sstevel@tonic-gate do
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate c = *(in++);
1707c478bd9Sstevel@tonic-gate switch (c)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate case '\e':
1737c478bd9Sstevel@tonic-gate *out++ = '\\'; *out++ = 'e'; break;
1747c478bd9Sstevel@tonic-gate case ' ':
1757c478bd9Sstevel@tonic-gate *out++ = '\\'; *out++ = 's'; break;
1767c478bd9Sstevel@tonic-gate case '\\':
1777c478bd9Sstevel@tonic-gate *out++ = '\\'; *out++ = '\\'; break;
1787c478bd9Sstevel@tonic-gate case '0' ... '9':
1797c478bd9Sstevel@tonic-gate case 'a' ... 'z':
1807c478bd9Sstevel@tonic-gate case 'A' ... 'Z':
1817c478bd9Sstevel@tonic-gate case '%':
1827c478bd9Sstevel@tonic-gate case '+':
1837c478bd9Sstevel@tonic-gate case '-':
1847c478bd9Sstevel@tonic-gate case '*':
1857c478bd9Sstevel@tonic-gate case '/':
1867c478bd9Sstevel@tonic-gate case ';':
1877c478bd9Sstevel@tonic-gate case ':':
1887c478bd9Sstevel@tonic-gate case '{':
1897c478bd9Sstevel@tonic-gate case '}':
1907c478bd9Sstevel@tonic-gate case '[':
1917c478bd9Sstevel@tonic-gate case ']':
1927c478bd9Sstevel@tonic-gate *out++ = c; break;
1937c478bd9Sstevel@tonic-gate case 0 ... 25:
1947c478bd9Sstevel@tonic-gate *out++ = '^'; *out++ = 'A' + c; break;
1957c478bd9Sstevel@tonic-gate default:
1967c478bd9Sstevel@tonic-gate *out++ = '\\';
1977c478bd9Sstevel@tonic-gate *out++ = ((c >> 8) & 7) + '0';
1987c478bd9Sstevel@tonic-gate *out++ = ((c >> 4) & 7) + '0';
1997c478bd9Sstevel@tonic-gate *out++ = ((c >> 0) & 7) + '0';
2007c478bd9Sstevel@tonic-gate break;
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate while (in < end);
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate *out++ = 0;
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate return out_buffer;
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate /* convert a string containing binary character into an external ascii
2117c478bd9Sstevel@tonic-gate * representation. */
2127c478bd9Sstevel@tonic-gate char *
ti_escape_string(const char * in)2137c478bd9Sstevel@tonic-gate ti_escape_string (const char *in)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate return ti_escape_memory (in, in + grub_strlen (in));
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate /* move the cursor to the given position starting with "0". */
2197c478bd9Sstevel@tonic-gate void
ti_cursor_address(int x,int y)2207c478bd9Sstevel@tonic-gate ti_cursor_address (int x, int y)
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate grub_putstr (grub_tparm (term.cursor_address, y, x));
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate /* clear the screen. */
2267c478bd9Sstevel@tonic-gate void
ti_clear_screen(void)2277c478bd9Sstevel@tonic-gate ti_clear_screen (void)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate grub_putstr (grub_tparm (term.clear_screen));
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate /* enter reverse video */
2337c478bd9Sstevel@tonic-gate void
ti_enter_standout_mode(void)2347c478bd9Sstevel@tonic-gate ti_enter_standout_mode (void)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate grub_putstr (grub_tparm (term.enter_standout_mode));
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate /* exit reverse video */
2407c478bd9Sstevel@tonic-gate void
ti_exit_standout_mode(void)2417c478bd9Sstevel@tonic-gate ti_exit_standout_mode (void)
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate grub_putstr (grub_tparm (term.exit_standout_mode));
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate /* set the current terminal emulation to use */
2477c478bd9Sstevel@tonic-gate void
ti_set_term(const struct terminfo * new)2487c478bd9Sstevel@tonic-gate ti_set_term (const struct terminfo *new)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate grub_memmove (&term, new, sizeof (struct terminfo));
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate /* get the current terminal emulation */
2547c478bd9Sstevel@tonic-gate void
ti_get_term(struct terminfo * copy)2557c478bd9Sstevel@tonic-gate ti_get_term(struct terminfo *copy)
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate grub_memmove (copy, &term, sizeof (struct terminfo));
2587c478bd9Sstevel@tonic-gate }
259