Move serial encoding handling to terminfo.c

This commit is contained in:
Vladimir 'phcoder' Serbinenko 2010-05-08 00:17:23 +02:00
parent 710d05aa8c
commit d8e9099811
3 changed files with 93 additions and 35 deletions

View file

@ -70,18 +70,19 @@ grub_term_color_state;
/* Set when the terminal cannot do fancy things. */
#define GRUB_TERM_DUMB (1 << 2)
/* Which encoding does terminal expect stream to be. */
#define GRUB_TERM_CODE_TYPE_MASK ((1 << 5) | (1 << 4) | (1 << 3))
#define GRUB_TERM_CODE_TYPE_SHIFT 3
#define GRUB_TERM_CODE_TYPE_MASK (7 << GRUB_TERM_CODE_TYPE_SHIFT)
/* Only ASCII characters accepted. */
#define GRUB_TERM_CODE_TYPE_ASCII 0
#define GRUB_TERM_CODE_TYPE_ASCII (0 << GRUB_TERM_CODE_TYPE_SHIFT)
/* Expects VGA characters (ASCII + pseudographics). */
#define GRUB_TERM_CODE_TYPE_VGA (1 << 3)
#define GRUB_TERM_CODE_TYPE_VGA (1 << GRUB_TERM_CODE_TYPE_SHIFT)
/* UTF-8 stream in logical order. Usually used for terminals
which just forward the stream to another computer. */
#define GRUB_TERM_CODE_TYPE_UTF8_LOGICAL (1 << 4)
#define GRUB_TERM_CODE_TYPE_UTF8_LOGICAL (2 << GRUB_TERM_CODE_TYPE_SHIFT)
/* UTF-8 in visual order. Like UTF-8 logical but for buggy endpoints. */
#define GRUB_TERM_CODE_TYPE_UTF8_VISUAL ((1 << 4) | (1 << 3))
#define GRUB_TERM_CODE_TYPE_UTF8_VISUAL (3 << GRUB_TERM_CODE_TYPE_SHIFT)
/* Glyph description in visual order. */
#define GRUB_TERM_CODE_TYPE_VISUAL_GLYPHS (1 << 5)
#define GRUB_TERM_CODE_TYPE_VISUAL_GLYPHS (4 << GRUB_TERM_CODE_TYPE_SHIFT)
/* Bitmasks for modifier keys returned by grub_getkeystatus. */

View file

@ -38,9 +38,6 @@ static const struct grub_arg_option options[] =
{"word", 'w', 0, N_("Set the serial port word length."), 0, ARG_TYPE_INT},
{"parity", 'r', 0, N_("Set the serial port parity."), 0, ARG_TYPE_STRING},
{"stop", 't', 0, N_("Set the serial port stop bits."), 0, ARG_TYPE_INT},
{"ascii", 'a', 0, N_("Terminal is ASCII-only."), 0, ARG_TYPE_NONE},
{"utf8", 'l', 0, N_("Terminal is logical-ordered UTF-8."), 0, ARG_TYPE_NONE},
{"visual-utf8", 'v', 0, N_("Terminal is visually-ordered UTF-8."), 0, ARG_TYPE_NONE},
{0, 0, 0, 0, 0, 0}
};
@ -201,9 +198,7 @@ struct grub_terminfo_input_state grub_serial_terminfo_input =
struct grub_terminfo_output_state grub_serial_terminfo_output =
{
.put = serial_hw_put,
.normal_color = 0x7,
.highlight_color = 0x70
.put = serial_hw_put
};
static struct grub_term_input grub_serial_term_input =
@ -313,15 +308,6 @@ grub_cmd_serial (grub_extcmd_t cmd,
}
}
grub_serial_term_output.flags &= ~GRUB_TERM_CODE_TYPE_MASK;
if (state[7].set)
grub_serial_term_output.flags |= GRUB_TERM_CODE_TYPE_UTF8_LOGICAL;
else if (state[8].set)
grub_serial_term_output.flags |= GRUB_TERM_CODE_TYPE_UTF8_VISUAL;
else
grub_serial_term_output.flags |= GRUB_TERM_CODE_TYPE_ASCII;
/* Initialize with new settings. */
hwiniterr = serial_hw_init ();

View file

@ -538,35 +538,100 @@ grub_terminfo_input_init (struct grub_term_input *termi)
/* GRUB Command. */
static grub_err_t
print_terminfo (void)
{
const char *encoding_names[(GRUB_TERM_CODE_TYPE_MASK
>> GRUB_TERM_CODE_TYPE_SHIFT) + 1]
= {
/* VGA and glyph descriptor types are just for completeness,
they are not used on terminfo terminals.
*/
[GRUB_TERM_CODE_TYPE_ASCII >> GRUB_TERM_CODE_TYPE_SHIFT] = _("ASCII"),
[GRUB_TERM_CODE_TYPE_VGA >> GRUB_TERM_CODE_TYPE_SHIFT] = "VGA",
[GRUB_TERM_CODE_TYPE_UTF8_LOGICAL >> GRUB_TERM_CODE_TYPE_SHIFT]
= _("UTF-8"),
[GRUB_TERM_CODE_TYPE_UTF8_VISUAL >> GRUB_TERM_CODE_TYPE_SHIFT]
= _("UTF-8 visual"),
[GRUB_TERM_CODE_TYPE_VISUAL_GLYPHS >> GRUB_TERM_CODE_TYPE_SHIFT]
= "Glyph descriptors",
_("Unknown"), _("Unknown"), _("Unknown")
};
struct grub_term_output *cur;
grub_printf ("Current terminfo types: \n");
for (cur = terminfo_outputs; cur;
cur = ((struct grub_terminfo_output_state *) cur->data)->next)
grub_printf ("%s: %s\t%s\n", cur->name,
grub_terminfo_get_current(cur),
encoding_names[(cur->flags & GRUB_TERM_CODE_TYPE_MASK)
>> GRUB_TERM_CODE_TYPE_SHIFT]);
return GRUB_ERR_NONE;
}
static grub_err_t
grub_cmd_terminfo (grub_command_t cmd __attribute__ ((unused)),
int argc, char **args)
{
struct grub_term_output *cur;
int encoding = GRUB_TERM_CODE_TYPE_ASCII;
int i;
char *name = NULL, *type = NULL;
if (argc == 0)
{
grub_printf ("Current terminfo types: \n");
for (cur = terminfo_outputs; cur;
cur = ((struct grub_terminfo_output_state *) cur->data)->next)
grub_printf ("%s: %s\n", cur->name,
grub_terminfo_get_current(cur));
return print_terminfo ();
return GRUB_ERR_NONE;
for (i = 0; i < argc; i++)
{
if (grub_strcmp (args[i], "-a") == 0
|| grub_strcmp (args[i], "--ascii") == 0)
{
encoding = GRUB_TERM_CODE_TYPE_ASCII;
continue;
}
if (grub_strcmp (args[i], "-u") == 0
|| grub_strcmp (args[i], "--utf8") == 0)
{
encoding = GRUB_TERM_CODE_TYPE_UTF8_LOGICAL;
continue;
}
if (grub_strcmp (args[i], "-v") == 0
|| grub_strcmp (args[i], "--visual-utf8") == 0)
{
encoding = GRUB_TERM_CODE_TYPE_UTF8_VISUAL;
continue;
}
if (!name)
{
name = args[i];
continue;
}
if (!type)
{
type = args[i];
continue;
}
return grub_error (GRUB_ERR_BAD_ARGUMENT, "too many parameters");
}
if (argc == 1)
if (name == NULL)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "too few parameters");
if (argc != 2)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "too many parameters");
for (cur = terminfo_outputs; cur;
cur = ((struct grub_terminfo_output_state *) cur->data)->next)
if (grub_strcmp (args[0], cur->name) == 0)
return grub_terminfo_set_current (cur, args[1]);
if (grub_strcmp (name, cur->name) == 0)
{
cur->flags = (cur->flags & ~GRUB_TERM_CODE_TYPE_MASK) | encoding;
if (!type)
return GRUB_ERR_NONE;
return grub_terminfo_set_current (cur, type);
}
return grub_error (GRUB_ERR_BAD_ARGUMENT,
"no terminal %s found or it's not handled by terminfo",
args[0]);
name);
}
static grub_command_t cmd;
@ -574,7 +639,13 @@ static grub_command_t cmd;
GRUB_MOD_INIT(terminfo)
{
cmd = grub_register_command ("terminfo", grub_cmd_terminfo,
N_("[TERM TYPE]"), N_("Set terminfo type of TERM to TYPE."));
N_("[[-a|-u|-v] TERM [TYPE]]"),
N_("Set terminfo type of TERM to TYPE.\n"
"-a, --ascii Terminal is ASCII-only [default].\n"
"-u, --utf8 Terminal is logical-ordered UTF-8.\n"
"-v, --visual-utf8 Terminal is visually-ordered UTF-8.")
);
}
GRUB_MOD_FINI(terminfo)