env.c 4.88 KB
Newer Older
1 2
/* env.c - Environment variables */
/*
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 10
 *  (at your option) any later version.
 *
11
 *  GRUB is distributed in the hope that it will be useful,
12 13 14 15 16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  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 23
#include <grub/misc.h>
#include <grub/mm.h>
24

25 26
/* The initial context.  */
static struct grub_env_context initial_context;
27

28
/* The current context.  */
29
struct grub_env_context *grub_current_context = &initial_context;
30 31

/* Return the hash representation of the string S.  */
32 33
static unsigned int
grub_env_hashval (const char *s)
34 35 36
{
  unsigned int i = 0;

37
  /* XXX: This can be done much more efficiently.  */
38 39 40 41 42 43
  while (*s)
    i += 5 * *(s++);

  return i % HASHSZ;
}

44
static struct grub_env_var *
45
grub_env_find (const char *name)
46
{
47 48
  struct grub_env_var *var;
  int idx = grub_env_hashval (name);
49

50
  /* Look for the variable in the current context.  */
51
  for (var = grub_current_context->vars[idx]; var; var = var->next)
52
    if (grub_strcmp (var->name, name) == 0)
53 54
      return var;

55 56 57
  return 0;
}

58 59
static void
grub_env_insert (struct grub_env_context *context,
60
		 struct grub_env_var *var)
61
{
62 63 64
  int idx = grub_env_hashval (var->name);

  /* Insert the variable into the hashtable.  */
65
  var->prevp = &context->vars[idx];
66 67
  var->next = context->vars[idx];
  if (var->next)
68
    var->next->prevp = &(var->next);
69
  context->vars[idx] = var;
70 71 72
}

static void
73
grub_env_remove (struct grub_env_var *var)
74 75
{
  /* Remove the entry from the variable table.  */
76 77 78
  *var->prevp = var->next;
  if (var->next)
    var->next->prevp = var->prevp;
79 80 81
}

grub_err_t
82
grub_env_set (const char *name, const char *val)
83
{
84
  struct grub_env_var *var;
85

86
  /* If the variable does already exist, just update the variable.  */
87 88
  var = grub_env_find (name);
  if (var)
89
    {
90
      char *old = var->value;
91

92 93
      if (var->write_hook)
	var->value = var->write_hook (var, val);
94
      else
95
	var->value = grub_strdup (val);
96

97
      if (! var->value)
98
	{
99
	  var->value = old;
100
	  return grub_errno;
101 102
	}

103
      grub_free (old);
104
      return GRUB_ERR_NONE;
105 106
    }

107
  /* The variable does not exist, so create a new one.  */
108
  var = grub_zalloc (sizeof (*var));
109
  if (! var)
110
    return grub_errno;
111

112 113
  var->name = grub_strdup (name);
  if (! var->name)
114
    goto fail;
115

116 117
  var->value = grub_strdup (val);
  if (! var->value)
118
    goto fail;
119

120
  grub_env_insert (grub_current_context, var);
121

122
  return GRUB_ERR_NONE;
123 124

 fail:
125 126 127
  grub_free (var->name);
  grub_free (var->value);
  grub_free (var);
128 129

  return grub_errno;
130 131
}

132
const char *
133
grub_env_get (const char *name)
134
{
135
  struct grub_env_var *var;
136

137 138
  var = grub_env_find (name);
  if (! var)
139 140
    return 0;

141 142
  if (var->read_hook)
    return var->read_hook (var, var->value);
143

144
  return var->value;
145 146 147
}

void
148
grub_env_unset (const char *name)
149
{
150
  struct grub_env_var *var;
151

152 153
  var = grub_env_find (name);
  if (! var)
154 155
    return;

156
  if (var->read_hook || var->write_hook)
157 158 159 160
    {
      grub_env_set (name, "");
      return;
    }
161

162
  grub_env_remove (var);
163

164
  grub_free (var->name);
165
  grub_free (var->value);
166
  grub_free (var);
167 168
}

169 170
struct grub_env_var *
grub_env_update_get_sorted (void)
171
{
172
  struct grub_env_var *sorted_list = 0;
173
  int i;
174

175 176
  /* Add variables associated with this context into a sorted list.  */
  for (i = 0; i < HASHSZ; i++)
177
    {
178
      struct grub_env_var *var;
179

180
      for (var = grub_current_context->vars[i]; var; var = var->next)
181
	{
182
	  struct grub_env_var *p, **q;
183

184
	  for (q = &sorted_list, p = *q; p; q = &((*q)->sorted_next), p = *q)
185
	    {
186
	      if (grub_strcmp (p->name, var->name) > 0)
187 188
		break;
	    }
189

190 191
	  var->sorted_next = *q;
	  *q = var;
192 193 194
	}
    }

195
  return sorted_list;
196 197
}

198
grub_err_t
199
grub_register_variable_hook (const char *name,
200 201
			     grub_env_read_hook_t read_hook,
			     grub_env_write_hook_t write_hook)
202
{
203
  struct grub_env_var *var = grub_env_find (name);
204

205
  if (! var)
206
    {
207
      if (grub_env_set (name, "") != GRUB_ERR_NONE)
208
	return grub_errno;
209

210 211
      var = grub_env_find (name);
      /* XXX Insert an assertion?  */
212
    }
213

214 215 216 217 218
  var->read_hook = read_hook;
  var->write_hook = write_hook;

  return GRUB_ERR_NONE;
}
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

grub_err_t
grub_env_export (const char *name)
{
  struct grub_env_var *var;

  var = grub_env_find (name);
  if (! var)
    {
      grub_err_t err;
      
      err = grub_env_set (name, "");
      if (err)
	return err;
      var = grub_env_find (name);
    }    
  var->global = 1;

  return GRUB_ERR_NONE;
}