~ubuntu-branches/ubuntu/trusty/grub2/trusty-updates

« back to all changes in this revision

Viewing changes to term/terminfo.c

Tags: upstream-1.99~20101122
ImportĀ upstreamĀ versionĀ 1.99~20101122

Show diffs side-by-side

added added

removed removed

Lines of Context:
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/command.h>
34
 
 
35
 
struct terminfo
36
 
{
37
 
  char *name;
38
 
 
39
 
  char *gotoxy;
40
 
  char *cls;
41
 
  char *reverse_video_on;
42
 
  char *reverse_video_off;
43
 
  char *cursor_on;
44
 
  char *cursor_off;
45
 
};
46
 
 
47
 
static struct terminfo term;
48
 
 
49
 
/* Get current terminfo name.  */
50
 
char *
51
 
grub_terminfo_get_current (void)
52
 
{
53
 
  return term.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
 
/* Set current terminfo type.  */
65
 
grub_err_t
66
 
grub_terminfo_set_current (const char *str)
67
 
{
68
 
  /* TODO
69
 
   * Lookup user specified terminfo type. If found, set term variables
70
 
   * as appropriate. Otherwise return an error.
71
 
   *
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.
83
 
   */
84
 
 
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);
93
 
 
94
 
  if (grub_strcmp ("vt100", str) == 0)
95
 
    {
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;
104
 
    }
105
 
 
106
 
  return grub_error (GRUB_ERR_BAD_ARGUMENT, "unknown terminfo type");
107
 
}
108
 
 
109
 
/* Wrapper for grub_putchar to write strings.  */
110
 
static void
111
 
putstr (const char *str, grub_term_output_t oterm)
112
 
{
113
 
  while (*str)
114
 
    grub_putcode (*str++, oterm);
115
 
}
116
 
 
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, grub_term_output_t oterm)
120
 
{
121
 
  putstr (grub_terminfo_tparm (term.gotoxy, y, x), oterm);
122
 
}
123
 
 
124
 
/* Clear the screen.  */
125
 
void
126
 
grub_terminfo_cls (grub_term_output_t oterm)
127
 
{
128
 
  putstr (grub_terminfo_tparm (term.cls), oterm);
129
 
}
130
 
 
131
 
/* Set reverse video mode on.  */
132
 
void
133
 
grub_terminfo_reverse_video_on (grub_term_output_t oterm)
134
 
{
135
 
  putstr (grub_terminfo_tparm (term.reverse_video_on), oterm);
136
 
}
137
 
 
138
 
/* Set reverse video mode off.  */
139
 
void
140
 
grub_terminfo_reverse_video_off (grub_term_output_t oterm)
141
 
{
142
 
  putstr (grub_terminfo_tparm (term.reverse_video_off), oterm);
143
 
}
144
 
 
145
 
/* Show cursor.  */
146
 
void
147
 
grub_terminfo_cursor_on (grub_term_output_t oterm)
148
 
{
149
 
  putstr (grub_terminfo_tparm (term.cursor_on), oterm);
150
 
}
151
 
 
152
 
/* Hide cursor.  */
153
 
void
154
 
grub_terminfo_cursor_off (grub_term_output_t oterm)
155
 
{
156
 
  putstr (grub_terminfo_tparm (term.cursor_off), oterm);
157
 
}
158
 
 
159
 
/* GRUB Command.  */
160
 
 
161
 
static grub_err_t
162
 
grub_cmd_terminfo (grub_command_t cmd __attribute__ ((unused)),
163
 
                   int argc, char **args)
164
 
{
165
 
  if (argc == 0)
166
 
  {
167
 
    grub_printf ("Current terminfo type: %s\n", grub_terminfo_get_current());
168
 
    return GRUB_ERR_NONE;
169
 
  }
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]);
174
 
}
175
 
 
176
 
static grub_command_t cmd;
177
 
 
178
 
GRUB_MOD_INIT(terminfo)
179
 
{
180
 
  cmd = grub_register_command ("terminfo", grub_cmd_terminfo,
181
 
                               "[TERM]", "Set terminfo type.");
182
 
  grub_terminfo_set_current ("vt100");
183
 
}
184
 
 
185
 
GRUB_MOD_FINI(terminfo)
186
 
{
187
 
  grub_unregister_command (cmd);
188
 
}