env.c 5.2 KB

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