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

« back to all changes in this revision

Viewing changes to .pc/fix_powerpc_emu.patch/grub-core/term/terminfo.c

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2014-01-16 15:18:04 UTC
  • mfrom: (17.6.38 experimental)
  • Revision ID: package-import@ubuntu.com-20140116151804-3foouk7fpqcq3sxx
Tags: 2.02~beta2-2
* Convert patch handling to git-dpm.
* Add bi-endian support to ELF parser (Tomohiro B Berry).
* Adjust restore_mkdevicemap.patch to mark get_kfreebsd_version as static,
  to appease "gcc -Werror=missing-prototypes".
* Cherry-pick from upstream:
  - Change grub-macbless' manual page section to 8.
* Install grub-glue-efi, grub-macbless, grub-render-label, and
  grub-syslinux2cfg.
* grub-shell: Pass -no-pad to xorriso when building floppy images.

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