GRUB-1.98 changes
[grub2/jjazz.git] / normal / context.c
blob08a841699d6a7cc9189ec7faeba89fddad256413
1 /* env.c - Environment variables */
2 /*
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/>.
20 #include <grub/env.h>
21 #include <grub/env_private.h>
22 #include <grub/misc.h>
23 #include <grub/mm.h>
24 #include <grub/command.h>
25 #include <grub/normal.h>
27 struct menu_pointer
29 grub_menu_t menu;
30 struct menu_pointer *prev;
33 struct menu_pointer initial_menu;
34 struct menu_pointer *current_menu = &initial_menu;
36 void
37 grub_env_unset_menu (void)
39 current_menu->menu = NULL;
42 grub_menu_t
43 grub_env_get_menu (void)
45 return current_menu->menu;
48 void
49 grub_env_set_menu (grub_menu_t nmenu)
51 current_menu->menu = nmenu;
54 grub_err_t
55 grub_env_context_open (int export)
57 struct grub_env_context *context;
58 int i;
59 struct menu_pointer *menu;
61 context = grub_zalloc (sizeof (*context));
62 if (! context)
63 return grub_errno;
64 menu = grub_zalloc (sizeof (*menu));
65 if (! menu)
66 return grub_errno;
68 context->prev = grub_current_context;
69 grub_current_context = context;
71 menu->prev = current_menu;
72 current_menu = menu;
74 /* Copy exported variables. */
75 for (i = 0; i < HASHSZ; i++)
77 struct grub_env_var *var;
79 for (var = context->prev->vars[i]; var; var = var->next)
81 if (export && var->global)
83 if (grub_env_set (var->name, var->value) != GRUB_ERR_NONE)
85 grub_env_context_close ();
86 return grub_errno;
88 grub_env_export (var->name);
89 grub_register_variable_hook (var->name, var->read_hook, var->write_hook);
94 return GRUB_ERR_NONE;
97 grub_err_t
98 grub_env_context_close (void)
100 struct grub_env_context *context;
101 int i;
102 struct menu_pointer *menu;
104 if (! grub_current_context->prev)
105 return grub_error (GRUB_ERR_BAD_ARGUMENT,
106 "cannot close the initial context");
108 /* Free the variables associated with this context. */
109 for (i = 0; i < HASHSZ; i++)
111 struct grub_env_var *p, *q;
113 for (p = grub_current_context->vars[i]; p; p = q)
115 q = p->next;
116 grub_free (p->name);
117 grub_free (p->value);
118 grub_free (p);
122 /* Restore the previous context. */
123 context = grub_current_context->prev;
124 grub_free (grub_current_context);
125 grub_current_context = context;
127 menu = current_menu->prev;
128 grub_free (current_menu);
129 current_menu = menu;
131 return GRUB_ERR_NONE;
134 grub_err_t
135 grub_env_export (const char *name)
137 struct grub_env_var *var;
139 var = grub_env_find (name);
140 if (! var)
142 grub_err_t err;
144 err = grub_env_set (name, "");
145 if (err)
146 return err;
147 var = grub_env_find (name);
149 var->global = 1;
151 return GRUB_ERR_NONE;
154 static grub_command_t export_cmd;
156 static grub_err_t
157 grub_cmd_export (struct grub_command *cmd __attribute__ ((unused)),
158 int argc, char **args)
160 if (argc < 1)
161 return grub_error (GRUB_ERR_BAD_ARGUMENT,
162 "no environment variable specified");
164 grub_env_export (args[0]);
165 return 0;
168 void
169 grub_context_init (void)
171 grub_env_export ("root");
172 grub_env_export ("prefix");
174 export_cmd = grub_register_command ("export", grub_cmd_export,
175 "export ENVVAR", "Export a variable.");
178 void
179 grub_context_fini (void)
181 grub_unregister_command (export_cmd);