env.c 5.6 KB

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