2009-06-08 Felix Zielcke <fzielcke@z-51.de>
[grub2/phcoder.git] / term / terminfo.c
blobf18be9ef02efaab191cf3adf3d4840797630becc
1 /* terminfo.c - simple terminfo module */
2 /*
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>
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/command.h>
35 struct terminfo
37 char *name;
39 char *gotoxy;
40 char *cls;
41 char *reverse_video_on;
42 char *reverse_video_off;
43 char *cursor_on;
44 char *cursor_off;
47 static struct terminfo term;
49 /* Get current terminfo name. */
50 char *
51 grub_terminfo_get_current (void)
53 return term.name;
56 /* Free *PTR and set *PTR to NULL, to prevent double-free. */
57 static void
58 grub_terminfo_free (char **ptr)
60 grub_free (*ptr);
61 *ptr = 0;
64 /* Set current terminfo type. */
65 grub_err_t
66 grub_terminfo_set_current (const char *str)
68 /* TODO
69 * Lookup user specified terminfo type. If found, set term variables
70 * as appropriate. Otherwise return an error.
72 * How should this be done?
73 * a. A static table included in this module.
74 * - I do not like this idea.
75 * b. A table stored in the configuration directory.
76 * - Users must convert their terminfo settings if we have not already.
77 * c. Look for terminfo files in the configuration directory.
78 * - /usr/share/terminfo is 6.3M on my system.
79 * - /usr/share/terminfo is not on most users boot partition.
80 * + Copying the terminfo files you want to use to the grub
81 * configuration directory is easier then (b).
82 * d. Your idea here.
85 /* Free previously allocated memory. */
86 grub_terminfo_free (&term.name);
87 grub_terminfo_free (&term.gotoxy);
88 grub_terminfo_free (&term.cls);
89 grub_terminfo_free (&term.reverse_video_on);
90 grub_terminfo_free (&term.reverse_video_off);
91 grub_terminfo_free (&term.cursor_on);
92 grub_terminfo_free (&term.cursor_off);
94 if (grub_strcmp ("vt100", str) == 0)
96 term.name = grub_strdup ("vt100");
97 term.gotoxy = grub_strdup ("\e[%i%p1%d;%p2%dH");
98 term.cls = grub_strdup ("\e[H\e[J");
99 term.reverse_video_on = grub_strdup ("\e[7m");
100 term.reverse_video_off = grub_strdup ("\e[m");
101 term.cursor_on = grub_strdup ("\e[?25h");
102 term.cursor_off = grub_strdup ("\e[?25l");
103 return grub_errno;
106 return grub_error (GRUB_ERR_BAD_ARGUMENT, "unknown terminfo type.");
109 /* Wrapper for grub_putchar to write strings. */
110 static void
111 putstr (const char *str)
113 while (*str)
114 grub_putchar (*str++);
117 /* Move the cursor to the given position starting with "0". */
118 void
119 grub_terminfo_gotoxy (grub_uint8_t x, grub_uint8_t y)
121 putstr (grub_terminfo_tparm (term.gotoxy, y, x));
124 /* Clear the screen. */
125 void
126 grub_terminfo_cls (void)
128 putstr (grub_terminfo_tparm (term.cls));
131 /* Set reverse video mode on. */
132 void
133 grub_terminfo_reverse_video_on (void)
135 putstr (grub_terminfo_tparm (term.reverse_video_on));
138 /* Set reverse video mode off. */
139 void
140 grub_terminfo_reverse_video_off (void)
142 putstr (grub_terminfo_tparm (term.reverse_video_off));
145 /* Show cursor. */
146 void
147 grub_terminfo_cursor_on (void)
149 putstr (grub_terminfo_tparm (term.cursor_on));
152 /* Hide cursor. */
153 void
154 grub_terminfo_cursor_off (void)
156 putstr (grub_terminfo_tparm (term.cursor_off));
159 /* GRUB Command. */
161 static grub_err_t
162 grub_cmd_terminfo (grub_command_t cmd __attribute__ ((unused)),
163 int argc, char **args)
165 if (argc == 0)
167 grub_printf ("Current terminfo type: %s\n", grub_terminfo_get_current());
168 return GRUB_ERR_NONE;
170 else if (argc != 1)
171 return grub_error (GRUB_ERR_BAD_ARGUMENT, "too many parameters.");
172 else
173 return grub_terminfo_set_current (args[0]);
176 static grub_command_t cmd;
178 GRUB_MOD_INIT(terminfo)
180 cmd = grub_register_command ("terminfo", grub_cmd_terminfo,
181 "terminfo [TERM]", "Set terminfo type.");
182 grub_terminfo_set_current ("vt100");
185 GRUB_MOD_FINI(terminfo)
187 grub_unregister_command (cmd);