1
/* env.c - Environment variables */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2003,2005,2006,2007,2008,2009 Free Software Foundation, Inc.
6
* GRUB is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* GRUB is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
21
#include <grub/env_private.h>
22
#include <grub/misc.h>
24
#include <grub/command.h>
25
#include <grub/normal.h>
26
#include <grub/i18n.h>
31
struct menu_pointer *prev;
34
struct menu_pointer initial_menu;
35
struct menu_pointer *current_menu = &initial_menu;
38
grub_env_unset_menu (void)
40
current_menu->menu = NULL;
44
grub_env_get_menu (void)
46
return current_menu->menu;
50
grub_env_set_menu (grub_menu_t nmenu)
52
current_menu->menu = nmenu;
56
grub_env_context_open (int export)
58
struct grub_env_context *context;
60
struct menu_pointer *menu;
62
context = grub_zalloc (sizeof (*context));
65
menu = grub_zalloc (sizeof (*menu));
69
context->prev = grub_current_context;
70
grub_current_context = context;
72
menu->prev = current_menu;
75
/* Copy exported variables. */
76
for (i = 0; i < HASHSZ; i++)
78
struct grub_env_var *var;
80
for (var = context->prev->vars[i]; var; var = var->next)
82
if (export && var->global)
84
if (grub_env_set (var->name, var->value) != GRUB_ERR_NONE)
86
grub_env_context_close ();
89
grub_env_export (var->name);
90
grub_register_variable_hook (var->name, var->read_hook, var->write_hook);
99
grub_env_context_close (void)
101
struct grub_env_context *context;
103
struct menu_pointer *menu;
105
if (! grub_current_context->prev)
106
return grub_error (GRUB_ERR_BAD_ARGUMENT,
107
"cannot close the initial context");
109
/* Free the variables associated with this context. */
110
for (i = 0; i < HASHSZ; i++)
112
struct grub_env_var *p, *q;
114
for (p = grub_current_context->vars[i]; p; p = q)
118
grub_free (p->value);
123
/* Restore the previous context. */
124
context = grub_current_context->prev;
125
grub_free (grub_current_context);
126
grub_current_context = context;
128
menu = current_menu->prev;
129
grub_free (current_menu);
132
return GRUB_ERR_NONE;
136
grub_env_export (const char *name)
138
struct grub_env_var *var;
140
var = grub_env_find (name);
145
err = grub_env_set (name, "");
148
var = grub_env_find (name);
152
return GRUB_ERR_NONE;
155
static grub_command_t export_cmd;
158
grub_cmd_export (struct grub_command *cmd __attribute__ ((unused)),
159
int argc, char **args)
162
return grub_error (GRUB_ERR_BAD_ARGUMENT,
163
"no environment variable specified");
165
grub_env_export (args[0]);
170
grub_context_init (void)
172
grub_env_export ("root");
173
grub_env_export ("prefix");
175
export_cmd = grub_register_command ("export", grub_cmd_export,
176
N_("ENVVAR"), N_("Export a variable."));
180
grub_context_fini (void)
182
grub_unregister_command (export_cmd);