1
/* env.c - Environment variables */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2003,2005 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 2 of the License, or
9
* (at your option) any later version.
11
* This program 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, write to the Free Software
18
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
#include <grub/misc.h>
25
/* The size of the hash table. */
28
/* A hashtable for quick lookup of variables. */
29
static struct grub_env_var *grub_env[HASHSZ];
31
/* The variables in a sorted list. */
32
static struct grub_env_var *grub_env_sorted;
34
/* Return the hash representation of the string S. */
35
static unsigned int grub_env_hashval (const char *s)
39
/* XXX: This can be done much more effecient. */
46
static struct grub_env_var *
47
grub_env_find (const char *name)
49
struct grub_env_var *var;
50
int idx = grub_env_hashval (name);
52
for (var = grub_env[idx]; var; var = var->next)
53
if (! grub_strcmp (var->name, name))
59
grub_env_set (const char *var, const char *val)
61
int idx = grub_env_hashval (var);
62
struct grub_env_var *env;
63
struct grub_env_var *sort;
64
struct grub_env_var **sortp;
66
/* If the variable does already exist, just update the variable. */
67
env = grub_env_find (var);
70
char *old = env->value;
73
env->value = env->write_hook (env, val);
75
env->value = grub_strdup (val);
87
/* The variable does not exist, create it. */
88
env = grub_malloc (sizeof (struct grub_env_var));
92
grub_memset (env, 0, sizeof (struct grub_env_var));
94
env->name = grub_strdup (var);
98
env->value = grub_strdup (val);
102
/* Insert it in the hashtable. */
103
env->prevp = &grub_env[idx];
104
env->next = grub_env[idx];
106
grub_env[idx]->prevp = &env->next;
109
/* Insert it in the sorted list. */
110
sortp = &grub_env_sorted;
111
sort = grub_env_sorted;
114
if (grub_strcmp (sort->name, var) > 0)
117
sortp = &sort->sort_next;
118
sort = sort->sort_next;
120
env->sort_prevp = sortp;
121
env->sort_next = sort;
123
sort->sort_prevp = &env->sort_next;
129
grub_free (env->name);
130
grub_free (env->value);
137
grub_env_get (const char *name)
139
struct grub_env_var *env;
140
env = grub_env_find (name);
145
return env->read_hook (env, env->value);
151
grub_env_unset (const char *name)
153
struct grub_env_var *env;
154
env = grub_env_find (name);
158
/* XXX: It is not possible to unset variables with a read or write
160
if (env->read_hook || env->write_hook)
163
*env->prevp = env->next;
165
env->next->prevp = env->prevp;
167
*env->sort_prevp = env->sort_next;
169
env->sort_next->sort_prevp = env->sort_prevp;
171
grub_free (env->name);
172
grub_free (env->value);
178
grub_env_iterate (int (* func) (struct grub_env_var *var))
180
struct grub_env_var *env;
182
for (env = grub_env_sorted; env; env = env->sort_next)
188
grub_register_variable_hook (const char *var,
189
grub_env_read_hook_t read_hook,
190
grub_env_write_hook_t write_hook)
192
struct grub_env_var *env = grub_env_find (var);
196
char *val = grub_strdup ("");
201
if (grub_env_set (var, val) != GRUB_ERR_NONE)
205
env = grub_env_find (var);
206
/* XXX Insert an assertion? */
208
env->read_hook = read_hook;
209
env->write_hook = write_hook;
211
return GRUB_ERR_NONE;