Skip to contentSkip to conversation

The menu and scripting

PATCH Marco Gerards 2 participants 3 messages Jan 5, 2006

lore
Share & tools
lore
#0 lore raw

Hi,

Here is a patch that makes it possible to script in the configfile and make it possible to add menu entries from scripts. The keyword menuentry' (or short @') was added for this. Currently the implementation only adds basic functionality. You can do things like:

set a=1

if [ ${a}= 1 ]; then
menuitem foo {
foo1
foo2
}
else
menuitem bar {
bar1
bar2
}
fi

In that case only foo will be shown.

There are a lot of things that can be improved. Here is a list of things I will work on:

- Making the parser reentrant.
- Relaxing the syntax a bit.
- Adding support for labels.
- Making it possible to generate menu entries using arguments or so.
- Better error handing.

Please tell me if there are any issues with this patch. I will apply it next Sunday if I don't hear anything.

Thanks,
Marco

2006-01-05 Marco Gerards <[email protected]>

* include/grub/normal.h: Include <grub/script.h>. (grub_command_list): Removed struct. (grub_command_list_t): Removed type. (grub_menu_entry): Remove members num' and command_list'. Add members commands' and sourcecode'.
* include/grub/script.h: Add inclusion guards. (grub_script_cmd_menuentry): New struct. (grub_script_execute_menuentry): New prototype. (grub_script_lexer_record_start): Likewise. (grub_script_lexer_record_stop): Likewise.
* normal/execute.c (grub_script_execute_menuentry): New function.
* normal/lexer.c (record, recording, recordpos, recordlen): New variables.
(grub_script_lexer_record_start): New function. (grub_script_lexer_record_stop): Likewise. (recordchar): Likewise.
(nextchar): Likewise.
(grub_script_yylex): Use nextchar' to fetch new characters. Use 2048 as the buffer size. Add the tokens menuentry' and `@'.
* normal/main.c: Include <grub/parser.h> and <grub/script.h> (current_menu): New variable.
(free_menu): Mainly rewritten. (grub_normal_menu_addentry): New function. (read_config_file): Rewritten.
* normal/menu.c (run_menu_entry): Mainly rewritten.
* normal/menu_entry.c (make_screen): Rewritten te code to insert the menu entry.
(run): Mainly rewritten.
* normal/parser.y (menu_entry): New variable. (GRUB_PARSER_TOKEN_MENUENTRY): New token. (menuentry): New rule.
(command): Add `menuentry'.
(if_statement): Allow additional returns before `fi'.
* normal/script.c (grub_script_create_cmdmenu): New function.

Index: include/grub/normal.h
===================================================================
RCS file: /cvsroot/grub/grub2/include/grub/normal.h,v retrieving revision 1.23

grub/normal.h +8 -15
diff -u -p -u -p -r1.23 normal.h
@@ -25,6 +25,7 @@
 #include <grub/symbol.h>
 #include <grub/err.h>
 #include <grub/arg.h>
+#include <grub/script.h>
 
 /* The maximum size of a command-line.  */
 #define GRUB_MAX_CMDLINE	1600
@@ -84,28 +85,17 @@ struct grub_command
 };
 typedef struct grub_command *grub_command_t;
 
-/* The command list.  */
-struct grub_command_list
-{
-  /* The string of a command.  */
-  char *command;
-
-  /* The next element.  */
-  struct grub_command_list *next;
-};
-typedef struct grub_command_list *grub_command_list_t;
-
 /* The menu entry.  */
 struct grub_menu_entry
 {
   /* The title name.  */
   const char *title;
 
-  /* The number of commands.  */
-  int num;
+    /* The commands associated with this menu entry.  */
+  struct grub_script *commands;
 
-  /* The list of commands.  */
-  grub_command_list_t command_list;
+  /* The sourcecode of the menu entry, used by the editor.  */
+  const char *sourcecode;
 
   /* The next element.  */
   struct grub_menu_entry *next;
@@ -192,6 +182,9 @@ void grub_context_pop_menu (void);
 char *grub_normal_do_completion (char *buf, int *restore,
 				 void (*hook) (const char *item, grub_completion_type_t type, int count));
 grub_err_t grub_normal_print_device_info (const char *name);
+grub_err_t grub_normal_menu_addentry (const char *title,
+				      struct grub_script *script,
+				      const char *sourcecode);
 
 #ifdef GRUB_UTIL
 void grub_normal_init (void);

Index: include/grub/script.h
===================================================================
RCS file: /cvsroot/grub/grub2/include/grub/script.h,v retrieving revision 1.1

grub/script.h +30 -0
diff -u -p -u -p -r1.1 script.h
@@ -17,6 +17,10 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
+
+#ifndef GRUB_SCRIPT_HEADER
+#define GRUB_SCRIPT_HEADER	1
+
 #include <grub/types.h>
 #include <grub/err.h>
 
@@ -104,6 +108,21 @@ struct grub_script_cmdif
   struct grub_script_cmd *false;
 };
 
+/* A menu entry generate statement.  */
+struct grub_script_cmd_menuentry
+{
+  struct grub_script_cmd cmd;
+
+  /* The title of the menu entry.  */
+  struct grub_script_arg *title;
+
+  /* The sourcecode the entry will be generated from.  */
+  const char *sourcecode;
+
+  /* Options.  XXX: Not used yet.  */
+  int options;
+};
+
 struct grub_script_arglist *
 grub_script_create_arglist (void);
 
@@ -120,6 +139,12 @@ struct grub_script_cmd *
 grub_script_create_cmdif (struct grub_script_cmd *bool,
 			  struct grub_script_cmd *true,
 			  struct grub_script_cmd *false);
+
+struct grub_script_cmd *
+grub_script_create_cmdmenu (struct grub_script_arg *title,
+			    char *sourcecode,
+			    int options);
+
 struct grub_script_cmd *
 grub_script_add_cmd (struct grub_script_cmdblock *cmdblock,
 		     struct grub_script_cmd *cmd);
@@ -136,6 +161,8 @@ struct grub_script *grub_script_create (
 void grub_script_lexer_init (char *s, grub_err_t (*getline) (char **));
 void grub_script_lexer_ref (void);
 void grub_script_lexer_deref (void);
+void grub_script_lexer_record_start (void);
+char *grub_script_lexer_record_stop (void);
 
 /* Functions to track allocated memory.  */
 void *grub_script_malloc (grub_size_t size);
@@ -151,6 +178,7 @@ void grub_script_yyerror (char const *er
 grub_err_t grub_script_execute_cmdline (struct grub_script_cmd *cmd);
 grub_err_t grub_script_execute_cmdblock (struct grub_script_cmd *cmd);
 grub_err_t grub_script_execute_cmdif (struct grub_script_cmd *cmd);
+grub_err_t grub_script_execute_menuentry (struct grub_script_cmd *cmd);
 
 /* Execute any GRUB pre-parsed command or script.  */
 grub_err_t grub_script_execute (struct grub_script *script);
@@ -187,3 +215,5 @@ grub_script_function_t grub_script_funct
 int grub_script_function_iterate (int (*iterate) (grub_script_function_t));
 int grub_script_function_call (grub_script_function_t func,
 			       int argc, char **args);
+
+#endif /* ! GRUB_SCRIPT_HEADER */

Index: normal/execute.c
===================================================================
RCS file: /cvsroot/grub/grub2/normal/execute.c,v
retrieving revision 1.1

execute.c +30 -0
diff -u -p -u -p -r1.1 execute.c
@@ -191,6 +191,36 @@ grub_script_execute_cmdif (struct grub_s
     return grub_script_execute_cmd (cmdif->false);
 }
 
+/* Execute the menu entry generate statement.  */
+grub_err_t
+grub_script_execute_menuentry (struct grub_script_cmd *cmd)
+{
+  struct grub_script_cmd_menuentry *cmd_menuentry;
+  char *title;
+  struct grub_script *script;
+
+  cmd_menuentry = (struct grub_script_cmd_menuentry *) cmd;
+
+  /* The title can contain variables, parse them and generate a string
+     from it.  */
+  title = grub_script_execute_argument_to_string (cmd_menuentry->title);
+  if (! title)
+    return grub_errno;
+
+  /* Parse the menu entry *again*.  */
+  script = grub_script_parse ((char *) cmd_menuentry->sourcecode, 0);
+
+  if (! script)
+    {
+      grub_free (title);
+      return grub_errno;
+    }
+
+  /* XXX: When this fails, the memory should be free'ed?  */
+  return grub_normal_menu_addentry (title, script,
+				    cmd_menuentry->sourcecode);
+}
+
 
 
 /* Execute any GRUB pre-parsed command or script.  */

Index: normal/lexer.c
===================================================================
RCS file: /cvsroot/grub/grub2/normal/lexer.c,v
retrieving revision 1.1

lexer.c +83 -8

155 lines not on this page · raw message

Index: normal/main.c
===================================================================
RCS file: /cvsroot/grub/grub2/normal/main.c,v
retrieving revision 1.13

main.c +87 -152

286 lines not on this page · raw message

Index: normal/menu.c
===================================================================
RCS file: /cvsroot/grub/grub2/normal/menu.c,v
retrieving revision 1.14

menu.c +1 -11
diff -u -p -u -p -r1.14 menu.c
@@ -347,17 +347,7 @@ run_menu (grub_menu_t menu, int nested)
 static void
 run_menu_entry (grub_menu_entry_t entry)
 {
-  grub_command_list_t cl;
-
-  for (cl = entry->command_list; cl != 0; cl = cl->next)
-    {
-      if (cl->command[0] == '\0')
-	/* Ignore an empty command line.  */
-	continue;
-      
-      if (grub_command_execute (cl->command, 0) != 0)
-	break;
-    }
+  grub_script_execute (entry->commands);
   
   if (grub_errno == GRUB_ERR_NONE && grub_loader_is_loaded ())
     /* Implicit execution of boot, only if something is loaded.  */

Index: normal/menu_entry.c
===================================================================
RCS file: /cvsroot/grub/grub2/normal/menu_entry.c,v retrieving revision 1.4

menu_entry.c +44 -30

112 lines not on this page · raw message

Index: normal/parser.y
===================================================================
RCS file: /cvsroot/grub/grub2/normal/parser.y,v
retrieving revision 1.1

parser.y +25 -3
diff -u -p -u -p -r1.1 parser.y
@@ -28,6 +28,8 @@
 /* Keep track of the memory allocated for this specific function.  */
 static struct grub_script_mem *func_mem = 0;
 
+static char *menu_entry = 0;
+
 %}
 
 %union {
@@ -40,12 +42,13 @@ static struct grub_script_mem *func_mem 
 %token GRUB_PARSER_TOKEN_IF		"if"
 %token GRUB_PARSER_TOKEN_WHILE		"while"
 %token GRUB_PARSER_TOKEN_FUNCTION	"function"
+%token GRUB_PARSER_TOKEN_MENUENTRY	"menuentry"
 %token GRUB_PARSER_TOKEN_ELSE		"else"
 %token GRUB_PARSER_TOKEN_THEN		"then"
 %token GRUB_PARSER_TOKEN_FI		"fi"
 %token GRUB_PARSER_TOKEN_NAME
 %token GRUB_PARSER_TOKEN_VAR
-%type <cmd> script grubcmd command commands if
+%type <cmd> script grubcmd command commands menuentry if
 %type <arglist> arguments;
 %type <arg> argument;
 %type <string> "if" "while" "function" "else" "then" "fi"
@@ -53,7 +56,7 @@ static struct grub_script_mem *func_mem 
 
 %%
 /* It should be possible to do this in a clean way...  */
-script:		commands '\n'
+script:		commands returns
 		  {
 		    grub_script_parsed = $1;
 		  }
@@ -127,6 +130,7 @@ grubcmd:	ws GRUB_PARSER_TOKEN_NAME ' ' a
 command:	grubcmd 	{ $$ = $1; }
 		| if 		{ $$ = $1; }
 		| function	{ $$ = 0;  }
+		| menuentry	{ $$ = $1;  }
 ;
 
 /* A block of commands.  */
@@ -172,6 +176,24 @@ function:	"function" ' ' GRUB_PARSER_TOK
 		  }
 ;
 
+/* A menu entry.  Carefully save the memory that is allocated.  */
+menuentry:	"menuentry" ' ' argument
+		  { 
+		    grub_script_lexer_ref ();
+		  } ws '{' returns
+		  { 
+		    /* Record sourcecode of the menu entry.  It can be
+		       parsed multiple times if it is part of a
+		       loop.  */
+		    grub_script_lexer_record_start ();
+		  } commands returns '}'
+		  {
+		    menu_entry = grub_script_lexer_record_stop ();
+		    $$ = grub_script_create_cmdmenu ($3, menu_entry, 0);
+		    grub_script_lexer_deref ();
+		  }
+;
+
 /* The first part of the if statement.  It's used to switch the lexer
    to a state in which it demands more tokens.  */
 if_statement:	"if" { grub_script_lexer_ref (); }
@@ -183,7 +205,7 @@ if:		 if_statement grubcmd ';' ws "then"
 		    $$ = grub_script_create_cmdif ($2, $7, 0);
 		    grub_script_lexer_deref ();
 		  }
-		 | if_statement grubcmd ';' ws "then" returns commands returns "else" returns commands "fi"
+		 | if_statement grubcmd ';' ws "then" returns commands returns "else" returns commands returns  "fi"
 		  {
 		    $$ = grub_script_create_cmdif ($2, $7, $11);
 		    grub_script_lexer_deref ();

Index: normal/script.c
===================================================================
RCS file: /cvsroot/grub/grub2/normal/script.c,v
retrieving revision 1.1

script.c +31 -0
diff -u -p -u -p -r1.1 script.c
@@ -201,6 +201,37 @@ grub_script_create_cmdif (struct grub_sc
   return (struct grub_script_cmd *) cmd;
 }
 
+/* Create a command that adds a menu entry to the menu.  Title is an
+   argument that is parsed to generate a string that can be used as
+   the title.  The sourcecode for this entry is passed in SOURCECODE.
+   The options for this entry are passed in OPTIONS.  */
+struct grub_script_cmd *
+grub_script_create_cmdmenu (struct grub_script_arg *title,
+			    char *sourcecode,
+			    int options)
+{
+  struct grub_script_cmd_menuentry *cmd;
+  int i;
+
+  /* Having trailing returns can some some annoying conflicts, remove
+     them.  XXX: Can the parser be improved to handle this?  */
+  for (i = grub_strlen (sourcecode) - 1; i > 0; i--)
+    {
+      if (sourcecode[i] != '\n')
+	break;
+      sourcecode[i] = '\0';
+    }
+
+  cmd = grub_script_malloc (sizeof (*cmd));
+  cmd->cmd.exec = grub_script_execute_menuentry;
+  cmd->cmd.next = 0;
+  cmd->sourcecode = sourcecode;
+  cmd->title = title;
+  cmd->options = options;
+ 
+  return (struct grub_script_cmd *) cmd;
+}
+
 /* Create a block of commands.  CMD contains the command that should
    be added at the end of CMDBLOCK's list.  If CMDBLOCK is zero, a new
    cmdblock will be created.  */
#1 lore raw

Feature request: MBR backup

Hello everyone,

I'd like to propose a backup-mechanism for GRUB2.
On installation the old MBR could easily be stored in the partition hosting the stage2.
That would allow for a clean self-destruction of GRUB2 as it could simply overwrite its own MBR-data if required.

I guess many people install grub to play a little with linux/fbsd/etc and afterwards have a messed-up MBR and they want their Windows back. Such a feature would prevent a lot of support-requests I believe.

Cheers

Jan

#2 lore raw

Re: Feature request: MBR backup

[email protected] writes:

Hi,

Quoted 9 lines

I'd like to propose a backup-mechanism for GRUB2.
On installation the old MBR could easily be stored in the partition hosting the stage2.
That would allow for a clean self-destruction of GRUB2 as it could simply overwrite its own MBR-data if required.

I guess many people install grub to play a little with linux/fbsd/etc and afterwards have a messed-up MBR and they want their Windows back. Such a feature would prevent a lot of support-requests I believe.

You are right, it might be a nice feature to add. I think it would be quite easy to add an uninstall feature that does just this. I'll add it to the todo list on the wiki.

Thanks,
Marco