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>
30 struct menu_pointer
*prev
;
33 struct menu_pointer initial_menu
;
34 struct menu_pointer
*current_menu
= &initial_menu
;
37 grub_env_unset_menu (void)
39 current_menu
->menu
= NULL
;
43 grub_env_get_menu (void)
45 return current_menu
->menu
;
49 grub_env_set_menu (grub_menu_t nmenu
)
51 current_menu
->menu
= nmenu
;
55 grub_env_context_open (int export
)
57 struct grub_env_context
*context
;
59 struct menu_pointer
*menu
;
61 context
= grub_zalloc (sizeof (*context
));
64 menu
= grub_zalloc (sizeof (*menu
));
68 context
->prev
= grub_current_context
;
69 grub_current_context
= context
;
71 menu
->prev
= current_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 ();
88 grub_env_export (var
->name
);
89 grub_register_variable_hook (var
->name
, var
->read_hook
, var
->write_hook
);
98 grub_env_context_close (void)
100 struct grub_env_context
*context
;
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
)
117 grub_free (p
->value
);
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
);
131 return GRUB_ERR_NONE
;
135 grub_env_export (const char *name
)
137 struct grub_env_var
*var
;
139 var
= grub_env_find (name
);
144 err
= grub_env_set (name
, "");
147 var
= grub_env_find (name
);
151 return GRUB_ERR_NONE
;
154 static grub_command_t export_cmd
;
157 grub_cmd_export (struct grub_command
*cmd
__attribute__ ((unused
)),
158 int argc
, char **args
)
161 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
162 "no environment variable specified");
164 grub_env_export (args
[0]);
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.");
179 grub_context_fini (void)
181 grub_unregister_command (export_cmd
);