Merge mainline into keylayouts

This commit is contained in:
Vladimir 'phcoder' Serbinenko 2010-08-18 22:28:47 +02:00
commit 2cccf4b0c4
342 changed files with 46791 additions and 19202 deletions

View file

@ -133,25 +133,24 @@ static int
is_authenticated (const char *userlist)
{
const char *superusers;
auto int hook (grub_list_t item);
int hook (grub_list_t item)
{
const char *name;
if (!((struct grub_auth_user *) item)->authenticated)
return 0;
name = ((struct grub_auth_user *) item)->name;
return (userlist && grub_strword (userlist, name))
|| grub_strword (superusers, name);
}
struct grub_auth_user *user;
superusers = grub_env_get ("superusers");
if (!superusers)
return 1;
return grub_list_iterate (GRUB_AS_LIST (users), hook);
FOR_LIST_ELEMENTS (user, users)
{
if (!(user->authenticated))
continue;
if ((userlist && grub_strword (userlist, user->name))
|| grub_strword (superusers, user->name))
return 1;
}
return 0;
}
static int
@ -185,13 +184,13 @@ grub_username_get (char buf[], unsigned buf_size)
if (cur_len + 2 < buf_size)
{
buf[cur_len++] = key;
grub_putchar (key);
grub_printf ("%c", key);
}
}
grub_memset (buf + cur_len, 0, buf_size - cur_len);
grub_putchar ('\n');
grub_xputs ("\n");
grub_refresh ();
return (key != '\e');
@ -205,22 +204,7 @@ grub_auth_check_authentication (const char *userlist)
grub_err_t err;
static unsigned long punishment_delay = 1;
char entered[GRUB_AUTH_MAX_PASSLEN];
auto int hook (grub_list_t item);
int hook (grub_list_t item)
{
if (grub_strcmp (login, ((struct grub_auth_user *) item)->name) == 0)
cur = (struct grub_auth_user *) item;
return 0;
}
auto int hook_any (grub_list_t item);
int hook_any (grub_list_t item)
{
if (((struct grub_auth_user *) item)->callback)
cur = (struct grub_auth_user *) item;
return 0;
}
struct grub_auth_user *user;
grub_memset (login, 0, sizeof (login));
@ -240,7 +224,11 @@ grub_auth_check_authentication (const char *userlist)
if (!grub_password_get (entered, GRUB_AUTH_MAX_PASSLEN))
goto access_denied;
grub_list_iterate (GRUB_AS_LIST (users), hook);
FOR_LIST_ELEMENTS (user, users)
{
if (grub_strcmp (login, user->name) == 0)
cur = user;
}
if (!cur || ! cur->callback)
goto access_denied;

1294
normal/charset.c Normal file

File diff suppressed because it is too large Load diff

View file

@ -362,6 +362,7 @@ grub_cmdline_get (const char *prompt)
grub_putcode ('\n', term);
}
grub_printf ("%s ", prompt_translated);
grub_normal_reset_more ();
{
struct cmdline_term *cl_term_cur;
@ -445,6 +446,8 @@ grub_cmdline_get (const char *prompt)
print_completion);
grub_free (bufu8);
grub_normal_reset_more ();
if (restore)
{
/* Restore the prompt. */
@ -623,7 +626,7 @@ grub_cmdline_get (const char *prompt)
grub_refresh ();
}
grub_putchar ('\n');
grub_xputs ("\n");
grub_refresh ();
/* Remove leading spaces. */

View file

@ -56,22 +56,23 @@ parse_color_name (grub_uint8_t *ret, char *name)
return -1;
}
void
grub_parse_color_name_pair (grub_uint8_t *ret, const char *name)
int
grub_parse_color_name_pair (grub_uint8_t *color, const char *name)
{
int result = 1;
grub_uint8_t fg, bg;
char *fg_name, *bg_name;
/* nothing specified by user */
if (name == NULL)
return;
return result;
fg_name = grub_strdup (name);
if (fg_name == NULL)
{
/* "out of memory" message was printed by grub_strdup() */
grub_wait_after_message ();
return;
return result;
}
bg_name = grub_strchr (fg_name, '/');
@ -97,10 +98,12 @@ grub_parse_color_name_pair (grub_uint8_t *ret, const char *name)
goto free_and_return;
}
*ret = (bg << 4) | fg;
*color = (bg << 4) | fg;
result = 0;
free_and_return:
grub_free (fg_name);
return result;
}
static grub_uint8_t color_normal, color_highlight;
@ -122,10 +125,10 @@ set_colors (void)
/* Replace default `normal' colors with the ones specified by user (if any). */
char *
grub_env_write_color_normal (struct grub_env_var *var __attribute__ ((unused)),
const char *val)
grub_env_write_color_normal (struct grub_env_var *var, const char *val)
{
grub_parse_color_name_pair (&color_normal, val);
if (grub_parse_color_name_pair (&color_normal, val))
return 0;
set_colors ();
@ -134,10 +137,10 @@ grub_env_write_color_normal (struct grub_env_var *var __attribute__ ((unused)),
/* Replace default `highlight' colors with the ones specified by user (if any). */
char *
grub_env_write_color_highlight (struct grub_env_var *var __attribute__ ((unused)),
const char *val)
grub_env_write_color_highlight (struct grub_env_var *var, const char *val)
{
grub_parse_color_name_pair (&color_highlight, val);
if (grub_parse_color_name_pair (&color_highlight, val))
return 0;
set_colors ();

View file

@ -176,21 +176,6 @@ iterate_dev (const char *devname)
return 0;
}
static int
iterate_command (grub_command_t cmd)
{
if (cmd->prio & GRUB_PRIO_LIST_FLAG_ACTIVE)
{
if (cmd->flags & GRUB_COMMAND_FLAG_CMDLINE)
{
if (add_completion (cmd->name, " ", GRUB_COMPLETION_TYPE_COMMAND))
return 1;
}
}
return 0;
}
/* Complete a device. */
static int
complete_device (void)
@ -414,6 +399,14 @@ grub_normal_do_completion (char *buf, int *restore,
else
current_word = argv[argc - 1];
if (argc > 1 && ! grub_strcmp (argv[0], "set"))
{
char *equals = grub_strchr (current_word, '=');
if (equals)
/* Complete the value of the variable. */
current_word = equals + 1;
}
/* Determine the state the command line is in, depending on the
state, it can be determined how to complete. */
cmdline_state = get_state (buf);
@ -421,8 +414,18 @@ grub_normal_do_completion (char *buf, int *restore,
if (argc == 1 || argc == 0)
{
/* Complete a command. */
if (grub_command_iterate (iterate_command))
goto fail;
grub_command_t cmd;
FOR_COMMANDS(cmd)
{
if (cmd->prio & GRUB_PRIO_LIST_FLAG_ACTIVE)
{
if (cmd->flags & GRUB_COMMAND_FLAG_CMDLINE)
{
if (add_completion (cmd->name, " ", GRUB_COMPLETION_TYPE_COMMAND))
goto fail;
}
}
}
}
else if (*current_word == '-')
{

View file

@ -1,231 +0,0 @@
/* handler.c - support handler loading */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2009 Free Software Foundation, Inc.
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/dl.h>
#include <grub/mm.h>
#include <grub/err.h>
#include <grub/env.h>
#include <grub/misc.h>
#include <grub/command.h>
#include <grub/handler.h>
#include <grub/normal.h>
struct grub_handler_list
{
struct grub_handler_list *next;
char *name;
grub_command_t cmd;
};
static grub_list_t handler_list;
static grub_err_t
grub_handler_cmd (struct grub_command *cmd,
int argc __attribute__ ((unused)),
char **args __attribute__ ((unused)))
{
char *p;
grub_handler_class_t class;
grub_handler_t handler;
p = grub_strchr (cmd->name, '.');
if (! p)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid command name");
if (cmd->data)
{
if (! grub_dl_get (cmd->data))
{
grub_dl_t mod;
mod = grub_dl_load (cmd->data);
if (mod)
grub_dl_ref (mod);
else
return grub_errno;
}
grub_free (cmd->data);
cmd->data = 0;
}
*p = 0;
class = grub_named_list_find (GRUB_AS_NAMED_LIST (grub_handler_class_list),
cmd->name);
*p = '.';
if (! class)
return grub_error (GRUB_ERR_FILE_NOT_FOUND, "class not found");
handler = grub_named_list_find (GRUB_AS_NAMED_LIST (class->handler_list),
p + 1);
if (! handler)
return grub_error (GRUB_ERR_FILE_NOT_FOUND, "handler not found");
grub_handler_set_current (class, handler);
return 0;
}
static void
insert_handler (char *name, char *module)
{
struct grub_handler_list *item;
char *data;
if (grub_command_find (name))
return;
item = grub_malloc (sizeof (*item));
if (! item)
return;
item->name = grub_strdup (name);
if (! item->name)
{
grub_free (item);
return;
}
if (module)
{
data = grub_strdup (module);
if (! data)
{
grub_free (item->name);
grub_free (item);
return;
}
}
else
data = 0;
item->cmd = grub_register_command (item->name, grub_handler_cmd, 0,
"Set active handler.");
if (! item->cmd)
{
grub_free (data);
grub_free (item->name);
grub_free (item);
return;
}
item->cmd->data = data;
grub_list_push (&handler_list, GRUB_AS_LIST (item));
}
/* Read the file handler.lst for auto-loading. */
void
read_handler_list (void)
{
const char *prefix;
static int first_time = 1;
const char *class_name;
auto int iterate_handler (grub_handler_t handler);
int iterate_handler (grub_handler_t handler)
{
char name[grub_strlen (class_name) + grub_strlen (handler->name) + 2];
grub_strcpy (name, class_name);
grub_strcat (name, ".");
grub_strcat (name, handler->name);
insert_handler (name, 0);
return 0;
}
auto int iterate_class (grub_handler_class_t class);
int iterate_class (grub_handler_class_t class)
{
class_name = class->name;
grub_list_iterate (GRUB_AS_LIST (class->handler_list),
(grub_list_hook_t) iterate_handler);
return 0;
}
/* Make sure that this function does not get executed twice. */
if (! first_time)
return;
first_time = 0;
prefix = grub_env_get ("prefix");
if (prefix)
{
char *filename;
filename = grub_xasprintf ("%s/handler.lst", prefix);
if (filename)
{
grub_file_t file;
file = grub_file_open (filename);
if (file)
{
char *buf = NULL;
for (;; grub_free (buf))
{
char *p;
buf = grub_file_getline (file);
if (! buf)
break;
if (! grub_isgraph (buf[0]))
continue;
p = grub_strchr (buf, ':');
if (! p)
continue;
*p = '\0';
while (*++p == ' ')
;
insert_handler (buf, p);
}
grub_file_close (file);
}
grub_free (filename);
}
}
grub_list_iterate (GRUB_AS_LIST (grub_handler_class_list),
(grub_list_hook_t) iterate_class);
/* Ignore errors. */
grub_errno = GRUB_ERR_NONE;
}
void
free_handler_list (void)
{
struct grub_handler_list *item;
while ((item = grub_list_pop (&handler_list)) != 0)
{
grub_free (item->cmd->data);
grub_unregister_command (item->cmd);
grub_free (item->name);
grub_free (item);
}
}

View file

@ -31,6 +31,7 @@
#include <grub/auth.h>
#include <grub/i18n.h>
#include <grub/charset.h>
#include <grub/script_sh.h>
#define GRUB_DEFAULT_HISTORY_SIZE 50
@ -204,7 +205,7 @@ grub_normal_add_menu_entry (int argc, const char **args,
for (i = 0; i < argc; i++)
{
/* Capture arguments. */
if (grub_strncmp ("--", args[i], 2) == 0)
if (grub_strncmp ("--", args[i], 2) == 0 && i + 1 < argc)
{
const char *arg = &args[i][2];
@ -347,7 +348,6 @@ static grub_menu_t
read_config_file (const char *config)
{
grub_file_t file;
grub_parser_t old_parser = 0;
auto grub_err_t getline (char **line, int cont);
grub_err_t getline (char **line, int cont __attribute__ ((unused)))
@ -361,36 +361,7 @@ read_config_file (const char *config)
return grub_errno;
if (buf[0] == '#')
{
if (buf[1] == '!')
{
grub_parser_t parser;
grub_named_list_t list;
buf += 2;
while (grub_isspace (*buf))
buf++;
if (! old_parser)
old_parser = grub_parser_get_current ();
list = GRUB_AS_NAMED_LIST (grub_parser_class.handler_list);
parser = grub_named_list_find (list, buf);
if (parser)
grub_parser_set_current (parser);
else
{
char cmd_name[8 + grub_strlen (buf)];
/* Perhaps it's not loaded yet, try the autoload
command. */
grub_strcpy (cmd_name, "parser.");
grub_strcat (cmd_name, buf);
grub_command_execute (cmd_name, 0, 0);
}
}
grub_free (*line);
}
grub_free (*line);
else
break;
}
@ -426,15 +397,12 @@ read_config_file (const char *config)
if ((getline (&line, 0)) || (! line))
break;
grub_parser_get_current ()->parse_line (line, getline);
grub_normal_parse_line (line, getline);
grub_free (line);
}
grub_file_close (file);
if (old_parser)
grub_parser_set_current (old_parser);
return newmenu;
}
@ -468,8 +436,9 @@ grub_normal_init_page (struct grub_term_output *term)
posx = (grub_term_width (term) - posx) / 2;
grub_term_gotoxy (term, posx, 1);
grub_print_ucs4 (unicode_msg, last_position, term);
grub_printf("\n\n");
grub_print_ucs4 (unicode_msg, last_position, 0, 0, term);
grub_putcode ('\n', term);
grub_putcode ('\n', term);
grub_free (unicode_msg);
}
@ -496,12 +465,15 @@ void
grub_normal_execute (const char *config, int nested, int batch)
{
grub_menu_t menu = 0;
const char *prefix = grub_env_get ("prefix");
const char *prefix;
read_lists (prefix);
read_handler_list ();
grub_register_variable_hook ("prefix", NULL, read_lists_hook);
grub_command_execute ("parser.grub", 0, 0);
if (! nested)
{
prefix = grub_env_get ("prefix");
read_lists (prefix);
grub_register_variable_hook ("prefix", NULL, read_lists_hook);
grub_command_execute ("parser.grub", 0, 0);
}
if (config)
{
@ -596,26 +568,25 @@ grub_normal_reader_init (int nested)
{
grub_normal_init_page (term);
grub_term_setcursor (term, 1);
grub_print_message_indented (msg_formatted, 3, STANDARD_MARGIN, term);
grub_puts ("\n");
grub_putcode ('\n', term);
grub_putcode ('\n', term);
}
grub_free (msg_formatted);
return 0;
}
static grub_err_t
grub_normal_read_line_real (char **line, int cont, int nested)
{
grub_parser_t parser = grub_parser_get_current ();
char *prompt;
const char *prompt;
if (cont)
prompt = grub_xasprintf (">");
prompt = ">";
else
prompt = grub_xasprintf ("%s>", parser->name);
prompt = "grub>";
if (!prompt)
return grub_errno;
@ -629,14 +600,11 @@ grub_normal_read_line_real (char **line, int cont, int nested)
if (cont || nested)
{
grub_free (*line);
grub_free (prompt);
*line = 0;
return grub_errno;
}
}
grub_free (prompt);
return 0;
}
@ -677,7 +645,7 @@ grub_cmdline_run (int nested)
if (! line)
break;
grub_parser_get_current ()->parse_line (line, grub_normal_read_line);
grub_normal_parse_line (line, grub_normal_read_line);
grub_free (line);
}
}
@ -690,17 +658,38 @@ grub_env_write_pager (struct grub_env_var *var __attribute__ ((unused)),
return grub_strdup (val);
}
/* clear */
static grub_err_t
grub_mini_cmd_clear (struct grub_command *cmd __attribute__ ((unused)),
int argc __attribute__ ((unused)),
char *argv[] __attribute__ ((unused)))
{
grub_cls ();
return 0;
}
static grub_command_t cmd_clear;
static void (*grub_xputs_saved) (const char *str);
GRUB_MOD_INIT(normal)
{
grub_context_init ();
grub_script_init ();
grub_xputs_saved = grub_xputs;
grub_xputs = grub_xputs_normal;
/* Normal mode shouldn't be unloaded. */
if (mod)
grub_dl_ref (mod);
cmd_clear =
grub_register_command ("clear", grub_mini_cmd_clear,
0, N_("Clear the screen."));
grub_set_history (GRUB_DEFAULT_HISTORY_SIZE);
grub_install_newline_hook ();
grub_register_variable_hook ("pager", 0, grub_env_write_pager);
/* Register a command "normal" for the rescue mode. */
@ -716,14 +705,21 @@ GRUB_MOD_INIT(normal)
/* Preserve hooks after context changes. */
grub_env_export ("color_normal");
grub_env_export ("color_highlight");
/* Set default color names. */
grub_env_set ("color_normal", "white/black");
grub_env_set ("color_highlight", "black/white");
}
GRUB_MOD_FINI(normal)
{
grub_context_fini ();
grub_script_fini ();
grub_xputs = grub_xputs_saved;
grub_set_history (0);
grub_register_variable_hook ("pager", 0, 0);
grub_fs_autoload_hook = 0;
free_handler_list ();
grub_unregister_command (cmd_clear);
}

View file

@ -29,6 +29,7 @@
#include <grub/auth.h>
#include <grub/i18n.h>
#include <grub/term.h>
#include <grub/script_sh.h>
/* Time to delay after displaying an error message about a default/fallback
entry failing to boot. */
@ -42,10 +43,10 @@ grub_err_t (*grub_gfxmenu_try_hook) (int entry, grub_menu_t menu,
void
grub_wait_after_message (void)
{
grub_putchar ('\n');
grub_xputs ("\n");
grub_printf_ (N_("Press any key to continue..."));
(void) grub_getkey ();
grub_putchar ('\n');
grub_xputs ("\n");
}
/* Get a menu entry by its index in the entry list. */
@ -141,6 +142,44 @@ get_and_remove_first_entry_number (const char *name)
return entry;
}
static void
grub_menu_execute_entry_real (grub_menu_entry_t entry)
{
const char *source;
auto grub_err_t getline (char **line, int cont);
grub_err_t getline (char **line, int cont __attribute__ ((unused)))
{
const char *p;
if (!source)
{
*line = 0;
return 0;
}
p = grub_strchr (source, '\n');
if (p)
*line = grub_strndup (source, p - source);
else
*line = grub_strdup (source);
source = p ? p + 1 : 0;
return 0;
}
source = entry->sourcecode;
while (source)
{
char *line;
getline (&line, 0);
grub_normal_parse_line (line, getline);
grub_free (line);
}
}
/* Run a menu entry. */
void
grub_menu_execute_entry(grub_menu_entry_t entry)
@ -159,7 +198,7 @@ grub_menu_execute_entry(grub_menu_entry_t entry)
grub_env_set ("chosen", entry->title);
grub_parser_execute ((char *) entry->sourcecode);
grub_menu_execute_entry_real (entry);
if (grub_errno == GRUB_ERR_NONE && grub_loader_is_loaded ())
/* Implicit execution of boot, only if something is loaded. */
@ -246,7 +285,6 @@ menu_init (int entry, grub_menu_t menu, int nested)
err = grub_gfxmenu_try_hook (entry, menu, nested);
if(!err)
continue;
grub_print_error ();
grub_errno = GRUB_ERR_NONE;
}
@ -578,13 +616,13 @@ show_menu (grub_menu_t menu, int nested)
}
else
{
int lines_before = grub_normal_get_line_counter ();
int chars_before = grub_normal_get_char_counter ();
grub_errno = GRUB_ERR_NONE;
grub_menu_execute_entry (e);
grub_print_error ();
grub_errno = GRUB_ERR_NONE;
if (lines_before != grub_normal_get_line_counter ())
if (chars_before != grub_normal_get_char_counter ())
grub_wait_after_message ();
}
}

View file

@ -23,8 +23,10 @@
#include <grub/loader.h>
#include <grub/command.h>
#include <grub/parser.h>
#include <grub/script_sh.h>
#include <grub/auth.h>
#include <grub/i18n.h>
#include <grub/charset.h>
enum update_mode
{
@ -172,7 +174,7 @@ print_up (int flag, struct per_term_screen *term_screen)
GRUB_TERM_FIRST_ENTRY_Y);
if (flag)
grub_putcode (GRUB_TERM_DISP_UP, term_screen->term);
grub_putcode (GRUB_UNICODE_UPARROW, term_screen->term);
else
grub_putcode (' ', term_screen->term);
}
@ -187,7 +189,7 @@ print_down (int flag, struct per_term_screen *term_screen)
+ grub_term_num_entries (term_screen->term));
if (flag)
grub_putcode (GRUB_TERM_DISP_DOWN, term_screen->term);
grub_putcode (GRUB_UNICODE_DOWNARROW, term_screen->term);
else
grub_putcode (' ', term_screen->term);
}
@ -242,6 +244,9 @@ update_screen (struct screen *screen, struct per_term_screen *term_screen,
{
int column;
if (linep >= screen->lines + screen->num_lines)
break;
for (column = 0;
column <= linep->len
&& y < grub_term_num_entries (term_screen->term);
@ -336,7 +341,7 @@ insert_string (struct screen *screen, char *s, int update)
screen->num_lines++;
screen->lines = grub_realloc (screen->lines,
screen->num_lines
* sizeof (struct line));
* sizeof (screen->lines[0]));
if (! screen->lines)
return 0;
@ -1019,87 +1024,92 @@ complete (struct screen *screen, int continuous, int update)
insert = grub_normal_do_completion (linep->buf, &restore, store_completion);
linep->buf[screen->column] = saved_char;
buflen = grub_strlen (completion_buffer.buf);
ucs4 = grub_malloc (sizeof (grub_uint32_t) * (buflen + 1));
if (!ucs4)
if (completion_buffer.buf)
{
grub_print_error ();
grub_errno = GRUB_ERR_NONE;
return 1;
}
buflen = grub_strlen (completion_buffer.buf);
ucs4 = grub_malloc (sizeof (grub_uint32_t) * (buflen + 1));
if (!ucs4)
{
grub_print_error ();
grub_errno = GRUB_ERR_NONE;
return 1;
}
ucs4len = grub_utf8_to_ucs4 (ucs4, buflen,
(grub_uint8_t *) completion_buffer.buf,
buflen, 0);
ucs4[ucs4len] = 0;
ucs4len = grub_utf8_to_ucs4 (ucs4, buflen,
(grub_uint8_t *) completion_buffer.buf,
buflen, 0);
ucs4[ucs4len] = 0;
if (restore)
for (i = 0; i < screen->nterms; i++)
{
int num_sections = ((completion_buffer.len
+ grub_term_width (screen->terms[i].term) - 8 - 1)
/ (grub_term_width (screen->terms[i].term) - 8));
grub_uint32_t *endp;
grub_uint16_t pos;
grub_uint32_t *p = ucs4;
pos = grub_term_getxy (screen->terms[i].term);
grub_term_gotoxy (screen->terms[i].term, 0,
grub_term_height (screen->terms[i].term) - 3);
screen->completion_shown = 1;
grub_term_gotoxy (screen->terms[i].term, 0,
grub_term_height (screen->terms[i].term) - 3);
grub_puts_terminal (" ", screen->terms[i].term);
switch (completion_type)
if (restore)
for (i = 0; i < screen->nterms; i++)
{
case GRUB_COMPLETION_TYPE_COMMAND:
grub_puts_terminal (_("Possible commands are:"),
screen->terms[i].term);
break;
case GRUB_COMPLETION_TYPE_DEVICE:
grub_puts_terminal (_("Possible devices are:"),
screen->terms[i].term);
break;
case GRUB_COMPLETION_TYPE_FILE:
grub_puts_terminal (_("Possible files are:"),
screen->terms[i].term);
break;
case GRUB_COMPLETION_TYPE_PARTITION:
grub_puts_terminal (_("Possible partitions are:"),
screen->terms[i].term);
break;
case GRUB_COMPLETION_TYPE_ARGUMENT:
grub_puts_terminal (_("Possible arguments are:"),
screen->terms[i].term);
break;
default:
grub_puts_terminal (_("Possible things are:"),
screen->terms[i].term);
break;
int num_sections = ((completion_buffer.len
+ grub_term_width (screen->terms[i].term)
- 8 - 1)
/ (grub_term_width (screen->terms[i].term)
- 8));
grub_uint32_t *endp;
grub_uint16_t pos;
grub_uint32_t *p = ucs4;
pos = grub_term_getxy (screen->terms[i].term);
grub_term_gotoxy (screen->terms[i].term, 0,
grub_term_height (screen->terms[i].term) - 3);
screen->completion_shown = 1;
grub_term_gotoxy (screen->terms[i].term, 0,
grub_term_height (screen->terms[i].term) - 3);
grub_puts_terminal (" ", screen->terms[i].term);
switch (completion_type)
{
case GRUB_COMPLETION_TYPE_COMMAND:
grub_puts_terminal (_("Possible commands are:"),
screen->terms[i].term);
break;
case GRUB_COMPLETION_TYPE_DEVICE:
grub_puts_terminal (_("Possible devices are:"),
screen->terms[i].term);
break;
case GRUB_COMPLETION_TYPE_FILE:
grub_puts_terminal (_("Possible files are:"),
screen->terms[i].term);
break;
case GRUB_COMPLETION_TYPE_PARTITION:
grub_puts_terminal (_("Possible partitions are:"),
screen->terms[i].term);
break;
case GRUB_COMPLETION_TYPE_ARGUMENT:
grub_puts_terminal (_("Possible arguments are:"),
screen->terms[i].term);
break;
default:
grub_puts_terminal (_("Possible things are:"),
screen->terms[i].term);
break;
}
grub_puts_terminal ("\n ", screen->terms[i].term);
p += (count % num_sections)
* (grub_term_width (screen->terms[i].term) - 8);
endp = p + (grub_term_width (screen->terms[i].term) - 8);
if (p != ucs4)
grub_putcode (GRUB_UNICODE_LEFTARROW, screen->terms[i].term);
else
grub_putcode (' ', screen->terms[i].term);
grub_print_ucs4 (p, ucs4 + ucs4len < endp ? ucs4 + ucs4len : endp,
0, 0, screen->terms[i].term);
if (ucs4 + ucs4len > endp)
grub_putcode (GRUB_UNICODE_RIGHTARROW, screen->terms[i].term);
grub_term_gotoxy (screen->terms[i].term, pos >> 8, pos & 0xFF);
}
grub_puts_terminal ("\n ", screen->terms[i].term);
p += (count % num_sections)
* (grub_term_width (screen->terms[i].term) - 8);
endp = p + (grub_term_width (screen->terms[i].term) - 8);
if (p != ucs4)
grub_putcode (GRUB_TERM_DISP_LEFT, screen->terms[i].term);
else
grub_putcode (' ', screen->terms[i].term);
while (*p && p < endp)
grub_putcode (*p++, screen->terms[i].term);
if (*p)
grub_putcode (GRUB_TERM_DISP_RIGHT, screen->terms[i].term);
grub_term_gotoxy (screen->terms[i].term, pos >> 8, pos & 0xFF);
}
}
if (insert)
{
@ -1189,7 +1199,7 @@ run (struct screen *screen)
while (currline < screen->num_lines)
{
editor_getline (&nextline, 0);
if (grub_parser_get_current ()->parse_line (nextline, editor_getline))
if (grub_normal_parse_line (nextline, editor_getline))
break;
}
@ -1369,8 +1379,13 @@ grub_menu_entry_run (grub_menu_entry_t entry)
goto refresh;
case GRUB_TERM_CTRL | 'x':
if (! run (screen))
goto fail;
{
int chars_before = grub_normal_get_char_counter ();
run (screen);
if (chars_before != grub_normal_get_char_counter ())
grub_wait_after_message ();
}
goto refresh;
case GRUB_TERM_CTRL | 'r':
@ -1401,7 +1416,7 @@ grub_menu_entry_run (grub_menu_entry_t entry)
grub_cls ();
grub_print_error ();
grub_errno = GRUB_ERR_NONE;
grub_putchar ('\n');
grub_xputs ("\n");
grub_printf_ (N_("Press any key to continue..."));
(void) grub_getkey ();
}

View file

@ -38,26 +38,6 @@ struct menu_viewer_data
struct grub_term_output *term;
};
static void
print_spaces (int number_spaces, struct grub_term_output *term)
{
int i;
for (i = 0; i < number_spaces; i++)
grub_putcode (' ', term);
}
void
grub_print_ucs4 (const grub_uint32_t * str,
const grub_uint32_t * last_position,
struct grub_term_output *term)
{
while (str < last_position)
{
grub_putcode (*str, term);
str++;
}
}
grub_ssize_t
grub_getstringwidth (grub_uint32_t * str, const grub_uint32_t * last_position,
struct grub_term_output *term)
@ -66,8 +46,9 @@ grub_getstringwidth (grub_uint32_t * str, const grub_uint32_t * last_position,
while (str < last_position)
{
width += grub_term_getcharwidth (term, *str);
str++;
struct grub_unicode_glyph glyph;
str += grub_unicode_aglomerate_comb (str, last_position - str, &glyph);
width += grub_term_getcharwidth (term, &glyph);
}
return width;
}
@ -76,16 +57,11 @@ void
grub_print_message_indented (const char *msg, int margin_left, int margin_right,
struct grub_term_output *term)
{
int line_len;
grub_uint32_t *unicode_msg;
grub_uint32_t *last_position;
int msg_len;
line_len = grub_term_width (term) - grub_term_getcharwidth (term, 'm') *
(margin_left + margin_right);
msg_len = grub_utf8_to_ucs4_alloc (msg, &unicode_msg, &last_position);
if (msg_len < 0)
@ -93,40 +69,8 @@ grub_print_message_indented (const char *msg, int margin_left, int margin_right,
return;
}
grub_uint32_t *current_position = unicode_msg;
grub_print_ucs4 (unicode_msg, last_position, margin_left, margin_right, term);
grub_uint32_t *next_new_line = unicode_msg;
int first_loop = 1;
while (current_position < last_position)
{
if (! first_loop)
grub_putcode ('\n', term);
next_new_line = (grub_uint32_t *) last_position;
while (grub_getstringwidth (current_position, next_new_line,term)
> line_len
|| (next_new_line != last_position && *next_new_line != ' '
&& next_new_line > current_position))
{
next_new_line--;
}
if (next_new_line == current_position)
{
next_new_line = (next_new_line + line_len > last_position) ?
(grub_uint32_t *) last_position : next_new_line + line_len;
}
print_spaces (margin_left, term);
grub_print_ucs4 (current_position, next_new_line, term);
next_new_line++;
current_position = next_new_line;
first_loop = 0;
}
grub_free (unicode_msg);
}
@ -139,27 +83,27 @@ draw_border (struct grub_term_output *term)
grub_term_setcolorstate (term, GRUB_TERM_COLOR_NORMAL);
grub_term_gotoxy (term, GRUB_TERM_MARGIN, GRUB_TERM_TOP_BORDER_Y);
grub_putcode (GRUB_TERM_DISP_UL, term);
grub_putcode (GRUB_UNICODE_CORNER_UL, term);
for (i = 0; i < (unsigned) grub_term_border_width (term) - 2; i++)
grub_putcode (GRUB_TERM_DISP_HLINE, term);
grub_putcode (GRUB_TERM_DISP_UR, term);
grub_putcode (GRUB_UNICODE_HLINE, term);
grub_putcode (GRUB_UNICODE_CORNER_UR, term);
for (i = 0; i < (unsigned) grub_term_num_entries (term); i++)
{
grub_term_gotoxy (term, GRUB_TERM_MARGIN, GRUB_TERM_TOP_BORDER_Y + i + 1);
grub_putcode (GRUB_TERM_DISP_VLINE, term);
grub_putcode (GRUB_UNICODE_VLINE, term);
grub_term_gotoxy (term, GRUB_TERM_MARGIN + grub_term_border_width (term)
- 1,
GRUB_TERM_TOP_BORDER_Y + i + 1);
grub_putcode (GRUB_TERM_DISP_VLINE, term);
grub_putcode (GRUB_UNICODE_VLINE, term);
}
grub_term_gotoxy (term, GRUB_TERM_MARGIN,
GRUB_TERM_TOP_BORDER_Y + grub_term_num_entries (term) + 1);
grub_putcode (GRUB_TERM_DISP_LL, term);
grub_putcode (GRUB_UNICODE_CORNER_LL, term);
for (i = 0; i < (unsigned) grub_term_border_width (term) - 2; i++)
grub_putcode (GRUB_TERM_DISP_HLINE, term);
grub_putcode (GRUB_TERM_DISP_LR, term);
grub_putcode (GRUB_UNICODE_HLINE, term);
grub_putcode (GRUB_UNICODE_CORNER_LR, term);
grub_term_setcolorstate (term, GRUB_TERM_COLOR_NORMAL);
@ -194,11 +138,11 @@ command-line or ESC to discard edits and return to the GRUB menu."),
"entry is highlighted.\n");
char *msg_translated;
msg_translated = grub_xasprintf (msg, (grub_uint32_t) GRUB_TERM_DISP_UP,
(grub_uint32_t) GRUB_TERM_DISP_DOWN);
msg_translated = grub_xasprintf (msg, GRUB_UNICODE_UPARROW,
GRUB_UNICODE_DOWNARROW);
if (!msg_translated)
return;
grub_putchar ('\n');
grub_putcode ('\n', term);
grub_print_message_indented (msg_translated, STANDARD_MARGIN,
STANDARD_MARGIN, term);
@ -259,34 +203,55 @@ print_entry (int y, int highlight, grub_menu_entry_t entry,
grub_term_gotoxy (term, GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_MARGIN, y);
int last_printed = 0;
for (x = GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_MARGIN + 1, i = 0;
x < (int) (GRUB_TERM_LEFT_BORDER_X + grub_term_border_width (term)
- GRUB_TERM_MARGIN);
i++)
- GRUB_TERM_MARGIN);)
{
if (i < len
&& x <= (int) (GRUB_TERM_LEFT_BORDER_X + grub_term_border_width (term)
- GRUB_TERM_MARGIN - 1))
{
grub_ssize_t width;
struct grub_unicode_glyph glyph;
width = grub_term_getcharwidth (term, unicode_title[i]);
i += grub_unicode_aglomerate_comb (unicode_title + i,
len - i, &glyph);
if (x + width > (int) (GRUB_TERM_LEFT_BORDER_X
width = grub_term_getcharwidth (term, &glyph);
grub_free (glyph.combining);
if (x + width <= (int) (GRUB_TERM_LEFT_BORDER_X
+ grub_term_border_width (term)
- GRUB_TERM_MARGIN - 1))
grub_putcode (GRUB_TERM_DISP_RIGHT, term);
else
grub_putcode (unicode_title[i], term);
last_printed = i;
x += width;
}
else
{
grub_putcode (' ', term);
x++;
}
break;
}
grub_print_ucs4 (unicode_title,
unicode_title + last_printed, 0, 0, term);
if (last_printed != len)
{
grub_putcode (GRUB_UNICODE_RIGHTARROW, term);
struct grub_unicode_glyph pseudo_glyph = {
.base = GRUB_UNICODE_RIGHTARROW,
.variant = 0,
.attributes = 0,
.ncomb = 0,
.combining = 0,
.estimated_width = 1
};
x += grub_term_getcharwidth (term, &pseudo_glyph);
}
for (; x < (int) (GRUB_TERM_LEFT_BORDER_X + grub_term_border_width (term)
- GRUB_TERM_MARGIN); x++)
grub_putcode (' ', term);
grub_term_setcolorstate (term, GRUB_TERM_COLOR_NORMAL);
grub_putcode (' ', term);
@ -309,7 +274,7 @@ print_entries (grub_menu_t menu, int first, int offset,
GRUB_TERM_FIRST_ENTRY_Y);
if (first)
grub_putcode (GRUB_TERM_DISP_UP, term);
grub_putcode (GRUB_UNICODE_UPARROW, term);
else
grub_putcode (' ', term);
@ -327,7 +292,7 @@ print_entries (grub_menu_t menu, int first, int offset,
GRUB_TERM_TOP_BORDER_Y + grub_term_num_entries (term));
if (e)
grub_putcode (GRUB_TERM_DISP_DOWN, term);
grub_putcode (GRUB_UNICODE_DOWNARROW, term);
else
grub_putcode (' ', term);
@ -385,7 +350,7 @@ menu_text_print_timeout (int timeout, void *dataptr)
grub_print_message_indented (msg_translated, 3, 0, data->term);
posx = grub_term_getxy (data->term) >> 8;
print_spaces (grub_term_width (data->term) - posx - 1, data->term);
grub_print_spaces (data->term, grub_term_width (data->term) - posx - 1);
grub_term_gotoxy (data->term,
grub_term_cursor_x (data->term),
@ -443,7 +408,7 @@ menu_text_clear_timeout (void *dataptr)
struct menu_viewer_data *data = dataptr;
grub_term_gotoxy (data->term, 0, grub_term_height (data->term) - 3);
print_spaces (grub_term_width (data->term) - 1, data->term);
grub_print_spaces (data->term, grub_term_width (data->term) - 1);
grub_term_gotoxy (data->term, grub_term_cursor_x (data->term),
GRUB_TERM_FIRST_ENTRY_Y + data->offset);
grub_term_refresh (data->term);

View file

@ -37,14 +37,14 @@ grub_normal_print_device_info (const char *name)
p = grub_strchr (name, ',');
if (p)
{
grub_putchar ('\t');
grub_xputs ("\t");
grub_printf_ (N_("Partition %s:"), name);
grub_putchar (' ');
grub_xputs (" ");
}
else
{
grub_printf_ (N_("Device %s:"), name);
grub_putchar (' ');
grub_xputs (" ");
}
dev = grub_device_open (name);
@ -69,7 +69,7 @@ grub_normal_print_device_info (const char *name)
{
if (label && grub_strlen (label))
{
grub_putchar (' ');
grub_xputs (" ");
grub_printf_ (N_("- Label \"%s\""), label);
}
grub_free (label);
@ -84,7 +84,7 @@ grub_normal_print_device_info (const char *name)
if (grub_errno == GRUB_ERR_NONE)
{
grub_unixtime2datetime (tm, &datetime);
grub_putchar (' ');
grub_xputs (" ");
grub_printf_ (N_("- Last modification time %d-%02d-%02d "
"%02d:%02d:%02d %s"),
datetime.year, datetime.month, datetime.day,
@ -115,6 +115,6 @@ grub_normal_print_device_info (const char *name)
grub_device_close (dev);
}
grub_putchar ('\n');
grub_xputs ("\n");
return grub_errno;
}

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);
}
}