1 /* env.c - Environment variables */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2003,2005,2006,2007,2008,2009 Free Software Foundation, Inc.
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20 #include <grub/env.h>
21 #include <grub/env_private.h>
22 #include <grub/misc.h>
23 #include <grub/mm.h>
24
25 GRUB_EXPORT(grub_env_set);
26 GRUB_EXPORT(grub_env_get);
27 GRUB_EXPORT(grub_env_unset);
28 GRUB_EXPORT(grub_env_iterate);
29 GRUB_EXPORT(grub_env_find);
30 GRUB_EXPORT(grub_register_variable_hook);
31 GRUB_EXPORT(grub_current_menu);
32 GRUB_EXPORT(grub_current_context);
33
34 static struct menu_pointer initial_menu;
35 struct menu_pointer *grub_current_menu = &initial_menu;
36
37 /* The initial context. */
38 static struct grub_env_context initial_context;
39
40 /* The current context. */
41 struct grub_env_context *grub_current_context = &initial_context;
42
43 /* Return the hash representation of the string S. */
44 static unsigned int
grub_env_hashval(const char * s)45 grub_env_hashval (const char *s)
46 {
47 unsigned int i = 0;
48
49 /* XXX: This can be done much more efficiently. */
50 while (*s)
51 i += 5 * *(s++);
52
53 return i % HASHSZ;
54 }
55
56 struct grub_env_var *
grub_env_find(const char * name)57 grub_env_find (const char *name)
58 {
59 struct grub_env_var *var;
60 int idx = grub_env_hashval (name);
61
62 /* Look for the variable in the current context. */
63 for (var = grub_current_context->vars[idx]; var; var = var->next)
64 if (grub_strcmp (var->name, name) == 0)
65 return var;
66
67 return 0;
68 }
69
70 static void
grub_env_insert(struct grub_env_context * context,struct grub_env_var * var)71 grub_env_insert (struct grub_env_context *context,
72 struct grub_env_var *var)
73 {
74 int idx = grub_env_hashval (var->name);
75
76 /* Insert the variable into the hashtable. */
77 var->prevp = &context->vars[idx];
78 var->next = context->vars[idx];
79 if (var->next)
80 var->next->prevp = &(var->next);
81 context->vars[idx] = var;
82 }
83
84 static void
grub_env_remove(struct grub_env_var * var)85 grub_env_remove (struct grub_env_var *var)
86 {
87 /* Remove the entry from the variable table. */
88 *var->prevp = var->next;
89 if (var->next)
90 var->next->prevp = var->prevp;
91 }
92
93 grub_err_t
grub_env_set(const char * name,const char * val)94 grub_env_set (const char *name, const char *val)
95 {
96 struct grub_env_var *var;
97
98 /* If the variable does already exist, just update the variable. */
99 var = grub_env_find (name);
100 if (var)
101 {
102 char *old = var->value;
103
104 if (var->write_hook)
105 var->value = var->write_hook (var, val);
106 else
107 var->value = grub_strdup (val);
108
109 if (! var->value)
110 {
111 var->value = old;
112 return grub_errno;
113 }
114
115 grub_free (old);
116 return GRUB_ERR_NONE;
117 }
118
119 /* The variable does not exist, so create a new one. */
120 var = grub_zalloc (sizeof (*var));
121 if (! var)
122 return grub_errno;
123
124 /* This is not necessary. But leave this for readability. */
125 var->global = 0;
126
127 var->name = grub_strdup (name);
128 if (! var->name)
129 goto fail;
130
131 var->value = grub_strdup (val);
132 if (! var->value)
133 goto fail;
134
135 grub_env_insert (grub_current_context, var);
136
137 return GRUB_ERR_NONE;
138
139 fail:
140 grub_free (var->name);
141 grub_free (var->value);
142 grub_free (var);
143
144 return grub_errno;
145 }
146
147 char *
grub_env_get(const char * name)148 grub_env_get (const char *name)
149 {
150 struct grub_env_var *var;
151
152 var = grub_env_find (name);
153 if (! var)
154 return 0;
155
156 if (var->read_hook)
157 return var->read_hook (var, var->value);
158
159 return var->value;
160 }
161
162 void
grub_env_unset(const char * name)163 grub_env_unset (const char *name)
164 {
165 struct grub_env_var *var;
166
167 var = grub_env_find (name);
168 if (! var)
169 return;
170
171 if (var->read_hook || var->write_hook)
172 {
173 grub_env_set (name, "");
174 return;
175 }
176
177 grub_env_remove (var);
178
179 grub_free (var->name);
180 grub_free (var->value);
181 grub_free (var);
182 }
183
184 void
grub_env_iterate(int (* func)(struct grub_env_var * var,void * closure),void * closure)185 grub_env_iterate (int (*func) (struct grub_env_var *var, void *closure),
186 void *closure)
187 {
188 struct grub_env_sorted_var *sorted_list = 0;
189 struct grub_env_sorted_var *sorted_var;
190 int i;
191
192 /* Add variables associated with this context into a sorted list. */
193 for (i = 0; i < HASHSZ; i++)
194 {
195 struct grub_env_var *var;
196
197 for (var = grub_current_context->vars[i]; var; var = var->next)
198 {
199 struct grub_env_sorted_var *p, **q;
200
201 sorted_var = grub_malloc (sizeof (*sorted_var));
202 if (! sorted_var)
203 goto fail;
204
205 sorted_var->var = var;
206
207 for (q = &sorted_list, p = *q; p; q = &((*q)->next), p = *q)
208 {
209 if (grub_strcmp (p->var->name, var->name) > 0)
210 break;
211 }
212
213 sorted_var->next = *q;
214 *q = sorted_var;
215 }
216 }
217
218 /* Iterate FUNC on the sorted list. */
219 for (sorted_var = sorted_list; sorted_var; sorted_var = sorted_var->next)
220 if (func (sorted_var->var, closure))
221 break;
222
223 fail:
224
225 /* Free the sorted list. */
226 for (sorted_var = sorted_list; sorted_var; )
227 {
228 struct grub_env_sorted_var *tmp = sorted_var->next;
229
230 grub_free (sorted_var);
231 sorted_var = tmp;
232 }
233 }
234
235 grub_err_t
grub_register_variable_hook(const char * name,grub_env_read_hook_t read_hook,grub_env_write_hook_t write_hook)236 grub_register_variable_hook (const char *name,
237 grub_env_read_hook_t read_hook,
238 grub_env_write_hook_t write_hook)
239 {
240 struct grub_env_var *var = grub_env_find (name);
241
242 if (! var)
243 {
244 if (grub_env_set (name, "") != GRUB_ERR_NONE)
245 return grub_errno;
246
247 var = grub_env_find (name);
248 /* XXX Insert an assertion? */
249 }
250
251 if (var) {
252 var->read_hook = read_hook;
253 var->write_hook = write_hook;
254 }
255
256 return GRUB_ERR_NONE;
257 }
258