merge with mainline

This commit is contained in:
BVK Chaitanya 2010-07-13 00:43:28 +05:30
commit 297f0c2b6e
218 changed files with 35637 additions and 4957 deletions

View file

@ -23,57 +23,87 @@
#include <grub/dl.h>
#include <grub/env.h>
#include <grub/normal.h>
#include <grub/charset.h>
/* The amount of lines counted by the pager. */
static unsigned grub_more_lines;
struct term_state
{
struct term_state *next;
const struct grub_unicode_glyph *backlog_glyphs;
const grub_uint32_t *backlog_ucs4;
grub_size_t backlog_len;
void *free;
int num_lines;
char *term_name;
};
static struct term_state *term_states = NULL;
/* If the more pager is active. */
static int grub_more;
static int grub_normal_line_counter = 0;
static int grub_normal_char_counter = 0;
int
grub_normal_get_line_counter (void)
grub_normal_get_char_counter (void)
{
return grub_normal_line_counter;
return grub_normal_char_counter;
}
void
grub_normal_reset_more (void)
{
static struct term_state *state;
for (state = term_states; state; state = state->next)
state->num_lines = 0;
}
static void
process_newline (void)
print_more (void)
{
struct grub_term_output *cur;
unsigned height = -1;
char key;
grub_uint16_t *pos;
grub_term_output_t term;
grub_uint32_t *unicode_str, *unicode_last_position;
FOR_ACTIVE_TERM_OUTPUTS(cur)
if (grub_term_height (cur) < height)
height = grub_term_height (cur);
grub_more_lines++;
pos = grub_term_save_pos ();
grub_normal_line_counter++;
grub_utf8_to_ucs4_alloc ("--MORE--", &unicode_str,
&unicode_last_position);
if (grub_more && grub_more_lines >= height - 1)
if (!unicode_str)
{
char key;
grub_uint16_t *pos;
grub_errno = GRUB_ERR_NONE;
return;
}
pos = grub_term_save_pos ();
grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
grub_printf ("--MORE--");
grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
FOR_ACTIVE_TERM_OUTPUTS(term)
{
grub_print_ucs4 (unicode_str, unicode_last_position, 0, 0, term);
}
grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
key = grub_getkey ();
grub_free (unicode_str);
key = grub_getkey ();
/* Remove the message. */
grub_term_restore_pos (pos);
grub_printf (" ");
grub_term_restore_pos (pos);
/* Remove the message. */
grub_term_restore_pos (pos);
FOR_ACTIVE_TERM_OUTPUTS(term)
grub_print_spaces (term, 8);
grub_term_restore_pos (pos);
/* Scroll one lines or an entire page, depending on the key. */
if (key == '\r' || key =='\n')
grub_more_lines = height - 2;
else
grub_more_lines = 0;
/* Scroll one lines or an entire page, depending on the key. */
if (key == '\r' || key =='\n')
grub_normal_reset_more ();
else
{
static struct term_state *state;
for (state = term_states; state; state = state->next)
state->num_lines -= 2;
}
}
@ -84,30 +114,99 @@ grub_set_more (int onoff)
grub_more++;
else
grub_more--;
grub_more_lines = 0;
grub_normal_reset_more ();
}
void
grub_install_newline_hook (void)
enum
{
GRUB_CP437_UPARROW = 0x18,
GRUB_CP437_DOWNARROW = 0x19,
GRUB_CP437_RIGHTARROW = 0x1a,
GRUB_CP437_LEFTARROW = 0x1b,
GRUB_CP437_VLINE = 0xb3,
GRUB_CP437_CORNER_UR = 0xbf,
GRUB_CP437_CORNER_LL = 0xc0,
GRUB_CP437_HLINE = 0xc4,
GRUB_CP437_CORNER_LR = 0xd9,
GRUB_CP437_CORNER_UL = 0xda,
};
static grub_uint32_t
map_code (grub_uint32_t in, struct grub_term_output *term)
{
grub_newline_hook = process_newline;
if (in <= 0x7f)
return in;
switch (term->flags & GRUB_TERM_CODE_TYPE_MASK)
{
case GRUB_TERM_CODE_TYPE_CP437:
switch (in)
{
case GRUB_UNICODE_LEFTARROW:
return GRUB_CP437_LEFTARROW;
case GRUB_UNICODE_UPARROW:
return GRUB_CP437_UPARROW;
case GRUB_UNICODE_RIGHTARROW:
return GRUB_CP437_RIGHTARROW;
case GRUB_UNICODE_DOWNARROW:
return GRUB_CP437_DOWNARROW;
case GRUB_UNICODE_HLINE:
return GRUB_CP437_HLINE;
case GRUB_UNICODE_VLINE:
return GRUB_CP437_VLINE;
case GRUB_UNICODE_CORNER_UL:
return GRUB_CP437_CORNER_UL;
case GRUB_UNICODE_CORNER_UR:
return GRUB_CP437_CORNER_UR;
case GRUB_UNICODE_CORNER_LL:
return GRUB_CP437_CORNER_LL;
case GRUB_UNICODE_CORNER_LR:
return GRUB_CP437_CORNER_LR;
}
return '?';
case GRUB_TERM_CODE_TYPE_ASCII:
/* Better than nothing. */
switch (in)
{
case GRUB_UNICODE_LEFTARROW:
return '<';
case GRUB_UNICODE_UPARROW:
return '^';
case GRUB_UNICODE_RIGHTARROW:
return '>';
case GRUB_UNICODE_DOWNARROW:
return 'v';
case GRUB_UNICODE_HLINE:
return '-';
case GRUB_UNICODE_VLINE:
return '|';
case GRUB_UNICODE_CORNER_UL:
case GRUB_UNICODE_CORNER_UR:
case GRUB_UNICODE_CORNER_LL:
case GRUB_UNICODE_CORNER_LR:
return '+';
}
return '?';
}
return in;
}
void
grub_puts_terminal (const char *str, struct grub_term_output *term)
{
grub_uint32_t code;
grub_ssize_t ret;
const grub_uint8_t *ptr = (const grub_uint8_t *) str;
const grub_uint8_t *end;
end = (const grub_uint8_t *) (str + grub_strlen (str));
grub_uint32_t *unicode_str, *unicode_last_position;
grub_utf8_to_ucs4_alloc (str, &unicode_str,
&unicode_last_position);
while (*ptr)
{
ret = grub_utf8_to_ucs4 (&code, 1, ptr, end - ptr, &ptr);
grub_putcode (code, term);
}
grub_print_ucs4 (unicode_str, unicode_last_position, 0, 0, term);
grub_free (unicode_str);
}
grub_uint16_t *
@ -262,3 +361,435 @@ read_terminal_list (const char *prefix)
grub_errno = GRUB_ERR_NONE;
}
static void
putglyph (const struct grub_unicode_glyph *c, struct grub_term_output *term)
{
struct grub_unicode_glyph c2 =
{
.variant = 0,
.attributes = 0,
.ncomb = 0,
.combining = 0,
.estimated_width = 1
};
grub_normal_char_counter++;
if (c->base == '\t' && term->getxy)
{
int n;
n = 8 - ((term->getxy (term) >> 8) & 7);
c2.base = ' ';
while (n--)
(term->putchar) (term, &c2);
return;
}
if ((term->flags & GRUB_TERM_CODE_TYPE_MASK)
== GRUB_TERM_CODE_TYPE_UTF8_LOGICAL
|| (term->flags & GRUB_TERM_CODE_TYPE_MASK)
== GRUB_TERM_CODE_TYPE_UTF8_VISUAL)
{
int i;
c2.estimated_width = grub_term_getcharwidth (term, c);
for (i = -1; i < (int) c->ncomb; i++)
{
grub_uint8_t u8[20], *ptr;
grub_uint32_t code;
if (i == -1)
{
code = c->base;
if ((term->flags & GRUB_TERM_CODE_TYPE_MASK)
== GRUB_TERM_CODE_TYPE_UTF8_VISUAL)
{
if ((c->attributes & GRUB_UNICODE_GLYPH_ATTRIBUTE_MIRROR))
code = grub_unicode_mirror_code (code);
code = grub_unicode_shape_code (code, c->attributes);
}
}
else
code = c->combining[i].code;
grub_ucs4_to_utf8 (&code, 1, u8, sizeof (u8));
for (ptr = u8; *ptr; ptr++)
{
c2.base = *ptr;
(term->putchar) (term, &c2);
c2.estimated_width = 0;
}
}
c2.estimated_width = 1;
}
else
(term->putchar) (term, c);
if (c->base == '\n')
{
c2.base = '\r';
(term->putchar) (term, &c2);
}
}
static void
putcode_real (grub_uint32_t code, struct grub_term_output *term)
{
struct grub_unicode_glyph c =
{
.variant = 0,
.attributes = 0,
.ncomb = 0,
.combining = 0,
.estimated_width = 1
};
c.base = map_code (code, term);
putglyph (&c, term);
}
/* Put a Unicode character. */
void
grub_putcode (grub_uint32_t code, struct grub_term_output *term)
{
/* Combining character by itself? */
if (grub_unicode_get_comb_type (code) != GRUB_UNICODE_COMB_NONE)
return;
putcode_real (code, term);
}
static grub_ssize_t
get_maxwidth (struct grub_term_output *term,
int margin_left, int margin_right)
{
struct grub_unicode_glyph space_glyph = {
.base = ' ',
.variant = 0,
.attributes = 0,
.ncomb = 0,
.combining = 0
};
return (grub_term_width (term)
- grub_term_getcharwidth (term, &space_glyph)
* (margin_left + margin_right) - 1);
}
static grub_ssize_t
get_startwidth (struct grub_term_output *term,
int margin_left)
{
return ((term->getxy (term) >> 8) & 0xff) - margin_left;
}
static int
print_ucs4_terminal (const grub_uint32_t * str,
const grub_uint32_t * last_position,
int margin_left, int margin_right,
struct grub_term_output *term,
struct term_state *state)
{
const grub_uint32_t *ptr;
grub_ssize_t startwidth = get_startwidth (term, margin_left);
grub_ssize_t line_width = startwidth;
grub_ssize_t lastspacewidth = 0;
grub_ssize_t max_width = get_maxwidth (term, margin_left, margin_right);
const grub_uint32_t *line_start = str, *last_space = str - 1;
for (ptr = str; ptr < last_position; ptr++)
{
grub_ssize_t last_width = 0;
if (grub_unicode_get_comb_type (*ptr) == GRUB_UNICODE_COMB_NONE)
{
struct grub_unicode_glyph c = {
.variant = 0,
.attributes = 0,
.ncomb = 0,
.combining = 0
};
c.base = *ptr;
line_width += last_width = grub_term_getcharwidth (term, &c);
}
if (*ptr == ' ')
{
lastspacewidth = line_width;
last_space = ptr;
}
if (line_width > max_width || *ptr == '\n')
{
const grub_uint32_t *ptr2;
if (line_width > max_width && last_space > line_start)
ptr = last_space;
else if (line_width > max_width
&& line_start == str && startwidth != 0)
{
ptr = str;
lastspacewidth = startwidth;
}
else
lastspacewidth = line_width - last_width;
for (ptr2 = line_start; ptr2 < ptr; ptr2++)
{
/* Skip combining characters on non-UTF8 terminals. */
if ((term->flags & GRUB_TERM_CODE_TYPE_MASK)
!= GRUB_TERM_CODE_TYPE_UTF8_LOGICAL
&& grub_unicode_get_comb_type (*ptr2)
!= GRUB_UNICODE_COMB_NONE)
continue;
putcode_real (*ptr2, term);
}
grub_print_spaces (term, margin_right);
grub_putcode ('\n', term);
if (state && ++state->num_lines
>= (grub_ssize_t) grub_term_height (term) - 2)
{
state->backlog_ucs4 = (ptr == last_space || *ptr == '\n')
? ptr + 1 : ptr;
state->backlog_len = last_position - state->backlog_ucs4;
return 1;
}
line_width -= lastspacewidth;
grub_print_spaces (term, margin_left);
if (ptr == last_space || *ptr == '\n')
ptr++;
line_start = ptr;
}
}
{
const grub_uint32_t *ptr2;
for (ptr2 = line_start; ptr2 < last_position; ptr2++)
{
/* Skip combining characters on non-UTF8 terminals. */
if ((term->flags & GRUB_TERM_CODE_TYPE_MASK)
!= GRUB_TERM_CODE_TYPE_UTF8_LOGICAL
&& grub_unicode_get_comb_type (*ptr2)
!= GRUB_UNICODE_COMB_NONE)
continue;
putcode_real (*ptr2, term);
}
}
return 0;
}
static struct term_state *
find_term_state (struct grub_term_output *term)
{
struct term_state *state;
for (state = term_states; state; state = state->next)
if (grub_strcmp (state->term_name, term->name) == 0)
return state;
state = grub_zalloc (sizeof (*state));
if (!state)
{
grub_errno = GRUB_ERR_NONE;
return NULL;
}
state->term_name = grub_strdup (term->name);
state->next = term_states;
term_states = state;
return state;
}
static int
put_glyphs_terminal (const struct grub_unicode_glyph *visual,
grub_ssize_t visual_len,
int margin_left, int margin_right,
struct grub_term_output *term,
struct term_state *state)
{
const struct grub_unicode_glyph *visual_ptr;
for (visual_ptr = visual; visual_ptr < visual + visual_len; visual_ptr++)
{
if (visual_ptr->base == '\n')
grub_print_spaces (term, margin_right);
putglyph (visual_ptr, term);
if (visual_ptr->base == '\n')
{
if (state && ++state->num_lines
>= (grub_ssize_t) grub_term_height (term) - 2)
{
state->backlog_glyphs = visual_ptr + 1;
state->backlog_len = visual_len - (visual - visual_ptr) - 1;
return 1;
}
grub_print_spaces (term, margin_left);
}
grub_free (visual_ptr->combining);
}
return 0;
}
static int
print_backlog (struct grub_term_output *term,
int margin_left, int margin_right)
{
struct term_state *state = find_term_state (term);
if (!state)
return 0;
if (state->backlog_ucs4)
{
int ret;
ret = print_ucs4_terminal (state->backlog_ucs4,
state->backlog_ucs4 + state->backlog_len,
margin_left, margin_right, term, state);
if (!ret)
{
grub_free (state->free);
state->free = NULL;
state->backlog_len = 0;
}
return ret;
}
if (state->backlog_glyphs)
{
int ret;
ret = put_glyphs_terminal (state->backlog_glyphs,
state->backlog_len,
margin_left, margin_right, term, state);
if (!ret)
{
grub_free (state->free);
state->free = NULL;
state->backlog_len = 0;
}
return ret;
}
return 0;
}
static int
print_ucs4_real (const grub_uint32_t * str,
const grub_uint32_t * last_position,
int margin_left, int margin_right,
struct grub_term_output *term, int backlog)
{
struct term_state *state = NULL;
if (backlog)
state = find_term_state (term);
if (((term->getxy (term) >> 8) & 0xff) < margin_left)
grub_print_spaces (term, margin_left - ((term->getxy (term) >> 8) & 0xff));
if ((term->flags & GRUB_TERM_CODE_TYPE_MASK)
== GRUB_TERM_CODE_TYPE_VISUAL_GLYPHS
|| (term->flags & GRUB_TERM_CODE_TYPE_MASK)
== GRUB_TERM_CODE_TYPE_UTF8_VISUAL)
{
grub_ssize_t visual_len;
struct grub_unicode_glyph *visual;
int ret;
auto grub_ssize_t getcharwidth (const struct grub_unicode_glyph *c);
grub_ssize_t getcharwidth (const struct grub_unicode_glyph *c)
{
return grub_term_getcharwidth (term, c);
}
visual_len = grub_bidi_logical_to_visual (str, last_position - str,
&visual, getcharwidth,
get_maxwidth (term,
margin_left,
margin_right),
get_startwidth (term,
margin_left));
if (visual_len < 0)
{
grub_print_error ();
return 0;
}
ret = put_glyphs_terminal (visual, visual_len, margin_left, margin_right,
term, state);
if (!ret)
grub_free (visual);
else
state->free = visual;
return ret;
}
return print_ucs4_terminal (str, last_position, margin_left, margin_right,
term, state);
}
void
grub_print_ucs4 (const grub_uint32_t * str,
const grub_uint32_t * last_position,
int margin_left, int margin_right,
struct grub_term_output *term)
{
print_ucs4_real (str, last_position, margin_left, margin_right,
term, 0);
}
void
grub_xputs_normal (const char *str)
{
grub_term_output_t term;
grub_uint32_t *unicode_str, *unicode_last_position;
int backlog = 0;
grub_utf8_to_ucs4_alloc (str, &unicode_str,
&unicode_last_position);
if (!unicode_str)
{
grub_errno = GRUB_ERR_NONE;
return;
}
FOR_ACTIVE_TERM_OUTPUTS(term)
{
int cur;
cur = print_ucs4_real (unicode_str, unicode_last_position, 0, 0,
term, grub_more);
if (cur)
backlog = 1;
}
while (backlog)
{
print_more ();
backlog = 0;
FOR_ACTIVE_TERM_OUTPUTS(term)
{
int cur;
cur = print_backlog (term, 0, 0);
if (cur)
backlog = 1;
}
}
grub_free (unicode_str);
}
void
grub_cls (void)
{
struct grub_term_output *term;
FOR_ACTIVE_TERM_OUTPUTS(term)
{
if ((term->flags & GRUB_TERM_DUMB) || (grub_env_get ("debug")))
{
grub_putcode ('\n', term);
grub_term_refresh (term);
}
else
(term->cls) (term);
}
}