Variable context
Hi,
Here is a patch for making variables dependant on the context. Now when the user uses configfile, another context is created. Like in bash you can make variables global by using `export'.
--
Marco
2006-04-29 Marco Gerards <[email protected]>
* commands/configfile.c (grub_cmd_configfile): Execute the configfile within its own context.
* include/grub/env.h (grub_env_context_open): New prototype. (grub_env_context_close): Likewise. (grub_env_init): Likewise.
* kern/env.c (grub_env): Removed.
(grub_env_sorted): Likewise.
(grub_env_context): New variable. (grub_env_var_context): Likewise.
(grub_env_find): Search both the active context and the global context.
(grub_env_context_open): New function. (grub_env_context_close): Likewise.
(grub_env_init): Likewise.
(grub_env_insert): Likewise.
(grub_env_remove): Likewise.
(grub_env_export): Likewise.
(grub_env_set): Changed to use helper functions to avoid code duplication.
(grub_env_iterate): Rewritten so both the current context and the global context are being used.
* kern/i386/pc/init.c (grub_machine_init): Call `grub_env_init'.
* kern/powerpc/ieee1275/init.c (grub_machine_init): Likewise.
* kern/sparc64/ieee1275/init.c (grub_machine_init): Likewise.
* util/grub-emu.c (grub_machine_init): Likewise.
* kern/efi/init.c (grub_efi_init): Likewise.
* normal/command.c (export_command): New function. (grub_command_init): Register the `export' function.
Index: commands/configfile.c
===================================================================
RCS file: /sources/grub/grub2/commands/configfile.c,v retrieving revision 1.3
configfile.c +3 -1
@@ -1,7 +1,7 @@/* configfile.c - command to manually load config file */ /* * GRUB -- GRand Unified Bootloader -* Copyright (C) 2005 Free Software Foundation, Inc. +* Copyright (C) 2005,2006 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
@@ -33,7 +33,9 @@ grub_cmd_configfile (struct grub_arg_lisreturn grub_error (GRUB_ERR_BAD_ARGUMENT ,"file name required" );grub_cls (); +grub_env_context_open ();grub_normal_execute (args[0 ],1 ); +grub_env_context_close ();return 0 ; }
Index: include/grub/env.h
===================================================================
RCS file: /sources/grub/grub2/include/grub/env.h,v retrieving revision 1.3
grub/env.h +4 -1
@@ -1,6 +1,6 @@/* * GRUB -- GRand Unified Bootloader -* Copyright (C) 2003,2005 Free Software Foundation, Inc. +* Copyright (C) 2003,2005,2006 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
@@ -50,5 +50,8 @@ void EXPORT_FUNC(grub_env_iterate) (intgrub_err_t EXPORT_FUNC (grub_register_variable_hook ) (const char *var,grub_env_read_hook_t read_hook,grub_env_write_hook_t write_hook); +grub_err_t EXPORT_FUNC (grub_env_context_open ) (void ); +grub_err_t EXPORT_FUNC (grub_env_context_close ) (void ); +grub_err_t EXPORT_FUNC (grub_env_init ) (void );#endif /* ! GRUB_ENV_HEADER */
Index: kern/env.c
===================================================================
RCS file: /sources/grub/grub2/kern/env.c,v
retrieving revision 1.5
env.c +182 -43
273 lines not on this page · raw message
Index: kern/efi/init.c
===================================================================
RCS file: /sources/grub/grub2/kern/efi/init.c,v
retrieving revision 1.3
efi/init.c +2 -0
@@ -38,6 +38,8 @@ grub_efi_init (void)/* Initialize the memory management system. */ grub_efi_mm_init (); +grub_env_init (); +grub_efidisk_init (); }
Index: kern/i386/pc/init.c
===================================================================
RCS file: /sources/grub/grub2/kern/i386/pc/init.c,v retrieving revision 1.12
i386/pc/init.c +3 -1
@@ -1,6 +1,6 @@/* * GRUB -- GRand Unified Bootloader -* Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. +* Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by
@@ -226,6 +226,8 @@ grub_machine_init (void)/* The memory system was initialized, thus register built-in devices. */ grub_biosdisk_init (); + +grub_env_init (); }void
Index: kern/powerpc/ieee1275/init.c
===================================================================
RCS file: /sources/grub/grub2/kern/powerpc/ieee1275/init.c,v retrieving revision 1.25
powerpc/ieee1275/init.c +3 -1
@@ -1,7 +1,7 @@/* init.c -- Initialize GRUB on the newworld mac (PPC). */ /* * GRUB -- GRand Unified Bootloader -* Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc. +* Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by
@@ -134,6 +134,8 @@ grub_machine_init (void) }grub_mm_init_region ((void *) grub_heap_start, grub_heap_len); +grub_env_init (); +grub_ofdisk_init ();/* Process commandline. */
Index: kern/sparc64/ieee1275/init.c
===================================================================
RCS file: /sources/grub/grub2/kern/sparc64/ieee1275/init.c,v retrieving revision 1.2
sparc64/ieee1275/init.c +3 -1
@@ -1,7 +1,7 @@/* init.c -- Initialize GRUB on the Ultra Sprac (sparc64). */ /* * GRUB -- GRand Unified Bootloader -* Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc. +* Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by
@@ -156,6 +156,8 @@ grub_machine_init (void) grub_heap_len);grub_mm_init_region ((void *) grub_heap_start, grub_heap_len); +grub_env_init (); +grub_ofdisk_init ();/* Process commandline. */
Index: normal/command.c
===================================================================
RCS file: /sources/grub/grub2/normal/command.c,v
retrieving revision 1.16
command.c +16 -1
@@ -1,6 +1,6 @@/* * GRUB -- GRand Unified Bootloader -* Copyright (C) 2003,2005 Free Software Foundation, Inc. +* Copyright (C) 2003,2005,2006 Free Software Foundation, Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by
@@ -284,6 +284,18 @@ unset_command (struct grub_arg_list *sta }static grub_err_t +export_command (struct grub_arg_list *state __attribute__ ((unused)), +int argc,char **args) +{ +if (argc <1 ) +return grub_error (GRUB_ERR_BAD_ARGUMENT , +"no environment variable specified" ); + +grub_env_export (args[0 ]); +return 0 ; +} + +static grub_err_t insmod_command (struct grub_arg_list *state __attribute__ ((unused)),int argc,char **args) {
@@ -367,6 +379,9 @@ grub_command_init (void)grub_register_command ("unset" , unset_command,GRUB_COMMAND_FLAG_BOTH ,"unset ENVVAR" ,"Remove an environment variable." ,0 ); +grub_register_command ("export" , export_command,GRUB_COMMAND_FLAG_BOTH , +"export ENVVAR" ,"Export a variable." ,0 ); + grub_register_command ("insmod", insmod_command, GRUB_COMMAND_FLAG_BOTH, "insmod MODULE", "Insert a module. The argument can be a file or a module name.",
Index: util/grub-emu.c
===================================================================
RCS file: /sources/grub/grub2/util/grub-emu.c,v
retrieving revision 1.29
grub-emu.c +1 -0
@@ -83,6 +83,7 @@ voidgrub_machine_init (void) {signal (SIGINT ,SIG_IGN ); +grub_env_init ();grub_console_init (); }
Marco Gerards <[email protected]> writes:
Hi,
Quoted 3 lines
Here is a patch for making variables dependant on the context. Now when the user uses configfile, another context is created. Like in bash you can make variables global by using `export'.
Committed. And some other patches were committed as well. Now there is a new command source', with the alias .' you can use to include files and stuff. Please tell me if you find some problem.
--
Marco