diff --git a/Makefile.in b/Makefile.in index b28b660da..413c6a59d 100644 --- a/Makefile.in +++ b/Makefile.in @@ -169,7 +169,7 @@ endif ### General targets. CLEANFILES += $(pkglib_DATA) $(pkgdata_DATA) po/*.mo -pkglib_DATA += moddep.lst command.lst fs.lst partmap.lst parttool.lst handler.lst video.lst crypto.lst +pkglib_DATA += moddep.lst command.lst fs.lst partmap.lst parttool.lst handler.lst video.lst crypto.lst terminal.lst moddep.lst: $(DEFSYMFILES) $(UNDSYMFILES) genmoddep.awk cat $(DEFSYMFILES) /dev/null \ | $(AWK) -f $(srcdir)/genmoddep.awk $(UNDSYMFILES) > $@ \ @@ -187,6 +187,9 @@ partmap.lst: $(PARTMAPFILES) handler.lst: $(HANDLERFILES) cat $^ /dev/null | sort > $@ +terminal.lst: $(TERMINALFILES) + cat $^ /dev/null | sort > $@ + parttool.lst: $(PARTTOOLFILES) cat $^ /dev/null | sort | uniq > $@ diff --git a/commands/terminal.c b/commands/terminal.c index a667825f4..5befecd31 100644 --- a/commands/terminal.c +++ b/commands/terminal.c @@ -23,12 +23,16 @@ #include #include +struct grub_term_autoload *grub_term_input_autoload = NULL; +struct grub_term_autoload *grub_term_output_autoload = NULL; + grub_err_t grub_cmd_terminal_input (grub_command_t cmd __attribute__ ((unused)), int argc, char **args) { int i; grub_term_input_t term; + struct grub_term_autoload *aut; if (argc == 0) { @@ -39,6 +43,20 @@ grub_cmd_terminal_input (grub_command_t cmd __attribute__ ((unused)), grub_puts_ (N_ ("Available input terminals:")); FOR_DISABLED_TERM_INPUTS(term) grub_printf ("%s ", term->name); + /* This is quadratic but we don't expect mode than 30 terminal + modules ever. */ + for (aut = grub_term_input_autoload; aut; aut = aut->next) + { + FOR_DISABLED_TERM_INPUTS(term) + if (grub_strcmp (term->name, aut->name) == 0) + break; + if (!term) + FOR_ACTIVE_TERM_INPUTS(term) + if (grub_strcmp (term->name, aut->name) == 0) + break; + if (!term) + grub_printf ("%s ", aut->name); + } grub_printf ("\n"); return GRUB_ERR_NONE; } @@ -53,16 +71,36 @@ grub_cmd_terminal_input (grub_command_t cmd __attribute__ ((unused)), for (; i < argc; i++) { - FOR_DISABLED_TERM_INPUTS(term) - if (grub_strcmp (args[i], term->name) == 0) - break; - if (term == 0) - FOR_ACTIVE_TERM_INPUTS(term) - if (grub_strcmp (args[i], term->name) == 0) + int again = 0; + while (1) + { + FOR_DISABLED_TERM_INPUTS(term) + if (grub_strcmp (args[i], term->name) == 0) + break; + if (term == 0) + FOR_ACTIVE_TERM_INPUTS(term) + if (grub_strcmp (args[i], term->name) == 0) + break; + if (term) break; - if (term == 0) - return grub_error (GRUB_ERR_BAD_ARGUMENT, "unknown terminal '%s'\n", - args[i]); + if (again) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "unknown terminal '%s'\n", + args[i]); + for (aut = grub_term_input_autoload; aut; aut = aut->next) + if (grub_strcmp (args[i], aut->name) == 0) + { + grub_dl_t mod; + mod = grub_dl_load (aut->modname); + if (mod) + grub_dl_ref (mod); + grub_errno = GRUB_ERR_NONE; + break; + } + if (!aut) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "unknown terminal '%s'\n", + args[i]); + again = 1; + } } if (grub_strcmp (args[0], "--append") == 0) @@ -151,6 +189,7 @@ grub_cmd_terminal_output (grub_command_t cmd __attribute__ ((unused)), { int i; grub_term_output_t term; + struct grub_term_autoload *aut; if (argc == 0) { @@ -161,6 +200,20 @@ grub_cmd_terminal_output (grub_command_t cmd __attribute__ ((unused)), grub_puts_ (N_ ("Available output terminals:")); FOR_DISABLED_TERM_OUTPUTS(term) grub_printf ("%s ", term->name); + /* This is quadratic but we don't expect mode than 30 terminal + modules ever. */ + for (aut = grub_term_output_autoload; aut; aut = aut->next) + { + FOR_DISABLED_TERM_OUTPUTS(term) + if (grub_strcmp (term->name, aut->name) == 0) + break; + if (!term) + FOR_ACTIVE_TERM_OUTPUTS(term) + if (grub_strcmp (term->name, aut->name) == 0) + break; + if (!term) + grub_printf ("%s ", aut->name); + } grub_printf ("\n"); return GRUB_ERR_NONE; } @@ -175,16 +228,36 @@ grub_cmd_terminal_output (grub_command_t cmd __attribute__ ((unused)), for (; i < argc; i++) { - FOR_DISABLED_TERM_OUTPUTS(term) - if (grub_strcmp (args[i], term->name) == 0) - break; - if (term == 0) - FOR_ACTIVE_TERM_OUTPUTS(term) - if (grub_strcmp (args[i], term->name) == 0) + int again = 0; + while (1) + { + FOR_DISABLED_TERM_OUTPUTS(term) + if (grub_strcmp (args[i], term->name) == 0) + break; + if (term == 0) + FOR_ACTIVE_TERM_OUTPUTS(term) + if (grub_strcmp (args[i], term->name) == 0) + break; + if (term) break; - if (term == 0) - return grub_error (GRUB_ERR_BAD_ARGUMENT, "unknown terminal '%s'\n", - args[i]); + if (again) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "unknown terminal '%s'\n", + args[i]); + for (aut = grub_term_output_autoload; aut; aut = aut->next) + if (grub_strcmp (args[i], aut->name) == 0) + { + grub_dl_t mod; + mod = grub_dl_load (aut->modname); + if (mod) + grub_dl_ref (mod); + grub_errno = GRUB_ERR_NONE; + break; + } + if (!aut) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "unknown terminal '%s'\n", + args[i]); + again = 1; + } } if (grub_strcmp (args[0], "--append") == 0) diff --git a/genmk.rb b/genmk.rb index b3dbe8678..43eaef150 100644 --- a/genmk.rb +++ b/genmk.rb @@ -192,6 +192,7 @@ endif fs = 'fs-' + obj.suffix('lst') partmap = 'partmap-' + obj.suffix('lst') handler = 'handler-' + obj.suffix('lst') + terminal = 'terminal-' + obj.suffix('lst') parttool = 'parttool-' + obj.suffix('lst') video = 'video-' + obj.suffix('lst') dep = deps[i] @@ -213,6 +214,7 @@ FSFILES += #{fs} PARTTOOLFILES += #{parttool} PARTMAPFILES += #{partmap} HANDLERFILES += #{handler} +TERMINALFILES += #{terminal} VIDEOFILES += #{video} #{command}: #{src} $(#{src}_DEPENDENCIES) gencmdlist.sh @@ -240,6 +242,11 @@ VIDEOFILES += #{video} $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -E $< \ | sh $(srcdir)/genhandlerlist.sh #{symbolic_name} > $@ || (rm -f $@; exit 1) +#{terminal}: #{src} $(#{src}_DEPENDENCIES) genterminlist.sh + set -e; \ + $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -E $< \ + | sh $(srcdir)/genterminallist.sh #{symbolic_name} > $@ || (rm -f $@; exit 1) + #{video}: #{src} $(#{src}_DEPENDENCIES) genvideolist.sh set -e; \ $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -E $< \ diff --git a/genterminallist.sh b/genterminallist.sh new file mode 100644 index 000000000..eb0de0c32 --- /dev/null +++ b/genterminallist.sh @@ -0,0 +1,22 @@ +#! /bin/sh +# +# Copyright (C) 2009 Free Software Foundation, Inc. +# +# This script is free software; the author +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + +# Read source code from stdin and detect command names. + +module=$1 + +grep -v "^#" | sed -n \ + -e "/grub_term_register_input *( *\"/{s/.*( *\"\([^\"]*\)\".*/i\1: $module/;p;}" \ + -e "/grub_term_register_input_active *( *\"/{s/.*( *\"\([^\"]*\)\".*/i\1: $module/;p;}" \ + -e "/grub_term_register_output *( *\"/{s/.*( *\"\([^\"]*\)\".*/o\1: $module/;p;}" \ + -e "/grub_term_register_output_active *( *\"/{s/.*( *\"\([^\"]*\)\".*/o\1: $module/;p;}" diff --git a/include/grub/normal.h b/include/grub/normal.h index 91457fc09..af8e00a71 100644 --- a/include/grub/normal.h +++ b/include/grub/normal.h @@ -103,6 +103,8 @@ void read_fs_list (void); void read_crypto_list (void); +void read_terminal_list (void); + void grub_set_more (int onoff); #ifdef GRUB_UTIL diff --git a/include/grub/term.h b/include/grub/term.h index e8cf727be..922ccb2d2 100644 --- a/include/grub/term.h +++ b/include/grub/term.h @@ -395,6 +395,16 @@ grub_term_getcolor (struct grub_term_output *term, extern void (*EXPORT_VAR (grub_newline_hook)) (void); +struct grub_term_autoload +{ + struct grub_term_autoload *next; + char *name; + char *modname; +}; + +extern struct grub_term_autoload *grub_term_input_autoload; +extern struct grub_term_autoload *grub_term_output_autoload; + /* For convenience. */ #define GRUB_TERM_ASCII_CHAR(c) ((c) & 0xff) diff --git a/normal/main.c b/normal/main.c index b4af09be9..3ee4d295d 100644 --- a/normal/main.c +++ b/normal/main.c @@ -443,6 +443,7 @@ grub_normal_execute (const char *config, int nested, int batch) read_fs_list (); read_handler_list (); read_crypto_list (); + read_terminal_list (); grub_command_execute ("parser.grub", 0, 0); reader_nested = nested; diff --git a/normal/term.c b/normal/term.c index 5a8e9cffc..b099ae84c 100644 --- a/normal/term.c +++ b/normal/term.c @@ -19,6 +19,10 @@ #include #include #include +#include +#include +#include +#include /* The amount of lines counted by the pager. */ static unsigned grub_more_lines; @@ -127,3 +131,122 @@ grub_term_restore_pos (grub_uint16_t *pos) ptr++; } } + +static void +grub_terminal_autoload_free (void) +{ + struct grub_term_autoload *cur, *next; + unsigned i; + for (i = 0; i < 2; i++) + for (cur = i ? grub_term_input_autoload : grub_term_output_autoload; + cur; cur = next) + { + next = cur->next; + grub_free (cur->name); + grub_free (cur->modname); + grub_free (cur); + } + grub_term_input_autoload = NULL; + grub_term_output_autoload = NULL; +} + + +/* Read the file terminal.lst for auto-loading. */ +void +read_terminal_list (void) +{ + const char *prefix; + char *filename; + grub_file_t file; + char *buf = NULL; + + prefix = grub_env_get ("prefix"); + if (!prefix) + { + grub_errno = GRUB_ERR_NONE; + return; + } + + filename = grub_malloc (grub_strlen (prefix) + sizeof ("/crypto.lst")); + if (!filename) + { + grub_errno = GRUB_ERR_NONE; + return; + } + + grub_sprintf (filename, "%s/terminal.lst", prefix); + file = grub_file_open (filename); + if (!file) + { + grub_errno = GRUB_ERR_NONE; + return; + } + + /* Override previous terminal.lst. */ + grub_terminal_autoload_free (); + + for (;; grub_free (buf)) + { + char *p, *name; + struct grub_term_autoload *cur; + struct grub_term_autoload **target = NULL; + + buf = grub_file_getline (file); + + if (! buf) + break; + + switch (buf[0]) + { + case 'i': + target = &grub_term_input_autoload; + break; + + case 'o': + target = &grub_term_output_autoload; + break; + } + if (!target) + continue; + + name = buf + 1; + + p = grub_strchr (name, ':'); + if (! p) + continue; + + *p = '\0'; + while (*++p == ' ') + ; + + cur = grub_malloc (sizeof (*cur)); + if (!cur) + { + grub_errno = GRUB_ERR_NONE; + continue; + } + + cur->name = grub_strdup (name); + if (! name) + { + grub_errno = GRUB_ERR_NONE; + grub_free (cur); + continue; + } + + cur->modname = grub_strdup (p); + if (! cur->modname) + { + grub_errno = GRUB_ERR_NONE; + grub_free (cur); + grub_free (cur->name); + continue; + } + cur->next = *target; + *target = cur; + } + + grub_file_close (file); + + grub_errno = GRUB_ERR_NONE; +}