context.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #include <grub/command.h>
  24. #include <grub/normal.h>
  25. GRUB_EXPORT(grub_env_context_open);
  26. GRUB_EXPORT(grub_env_context_close);
  27. GRUB_EXPORT(grub_env_export);
  28. grub_err_t
  29. grub_env_context_open (int export)
  30. {
  31. struct grub_env_context *context;
  32. int i;
  33. struct menu_pointer *menu;
  34. context = grub_zalloc (sizeof (*context));
  35. if (! context)
  36. return grub_errno;
  37. menu = grub_zalloc (sizeof (*menu));
  38. if (! menu)
  39. return grub_errno;
  40. context->prev = grub_current_context;
  41. grub_current_context = context;
  42. menu->prev = grub_current_menu;
  43. grub_current_menu = menu;
  44. /* Copy exported variables. */
  45. for (i = 0; i < HASHSZ; i++)
  46. {
  47. struct grub_env_var *var;
  48. for (var = context->prev->vars[i]; var; var = var->next)
  49. {
  50. if (export && var->global)
  51. {
  52. if (grub_env_set (var->name, var->value) != GRUB_ERR_NONE)
  53. {
  54. grub_env_context_close ();
  55. return grub_errno;
  56. }
  57. grub_env_export (var->name);
  58. grub_register_variable_hook (var->name, var->read_hook, var->write_hook);
  59. }
  60. }
  61. }
  62. return GRUB_ERR_NONE;
  63. }
  64. grub_err_t
  65. grub_env_context_close (void)
  66. {
  67. struct grub_env_context *context;
  68. int i;
  69. struct menu_pointer *menu;
  70. if (! grub_current_context->prev)
  71. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  72. "cannot close the initial context");
  73. /* Free the variables associated with this context. */
  74. for (i = 0; i < HASHSZ; i++)
  75. {
  76. struct grub_env_var *p, *q;
  77. for (p = grub_current_context->vars[i]; p; p = q)
  78. {
  79. q = p->next;
  80. grub_free (p->name);
  81. grub_free (p->value);
  82. grub_free (p);
  83. }
  84. }
  85. /* Restore the previous context. */
  86. context = grub_current_context->prev;
  87. grub_free (grub_current_context);
  88. grub_current_context = context;
  89. menu = grub_current_menu->prev;
  90. grub_free (grub_current_menu);
  91. grub_current_menu = menu;
  92. return GRUB_ERR_NONE;
  93. }
  94. grub_err_t
  95. grub_env_export (const char *name)
  96. {
  97. struct grub_env_var *var;
  98. var = grub_env_find (name);
  99. if (! var)
  100. {
  101. grub_err_t err;
  102. err = grub_env_set (name, "");
  103. if (err)
  104. return err;
  105. var = grub_env_find (name);
  106. }
  107. var->global = 1;
  108. return GRUB_ERR_NONE;
  109. }