context.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #include <grub/i18n.h>
  26. struct menu_pointer
  27. {
  28. grub_menu_t menu;
  29. struct menu_pointer *prev;
  30. };
  31. static struct menu_pointer initial_menu;
  32. static struct menu_pointer *current_menu = &initial_menu;
  33. void
  34. grub_env_unset_menu (void)
  35. {
  36. current_menu->menu = NULL;
  37. }
  38. grub_menu_t
  39. grub_env_get_menu (void)
  40. {
  41. return current_menu->menu;
  42. }
  43. void
  44. grub_env_set_menu (grub_menu_t nmenu)
  45. {
  46. current_menu->menu = nmenu;
  47. }
  48. static grub_err_t
  49. grub_env_new_context (int export_all)
  50. {
  51. struct grub_env_context *context;
  52. int i;
  53. struct menu_pointer *menu;
  54. context = grub_zalloc (sizeof (*context));
  55. if (! context)
  56. return grub_errno;
  57. menu = grub_zalloc (sizeof (*menu));
  58. if (! menu)
  59. {
  60. grub_free (context);
  61. return grub_errno;
  62. }
  63. context->prev = grub_current_context;
  64. grub_current_context = context;
  65. menu->prev = current_menu;
  66. current_menu = menu;
  67. /* Copy exported variables. */
  68. for (i = 0; i < HASHSZ; i++)
  69. {
  70. struct grub_env_var *var;
  71. for (var = context->prev->vars[i]; var; var = var->next)
  72. if (var->global || export_all)
  73. {
  74. if (grub_env_set (var->name, var->value) != GRUB_ERR_NONE)
  75. {
  76. grub_env_context_close ();
  77. return grub_errno;
  78. }
  79. grub_env_export (var->name);
  80. grub_register_variable_hook (var->name, var->read_hook, var->write_hook);
  81. }
  82. }
  83. return GRUB_ERR_NONE;
  84. }
  85. grub_err_t
  86. grub_env_context_open (void)
  87. {
  88. return grub_env_new_context (0);
  89. }
  90. int grub_extractor_level = 0;
  91. grub_err_t
  92. grub_env_extractor_open (int source)
  93. {
  94. grub_extractor_level++;
  95. return grub_env_new_context (source);
  96. }
  97. grub_err_t
  98. grub_env_context_close (void)
  99. {
  100. struct grub_env_context *context;
  101. int i;
  102. struct menu_pointer *menu;
  103. if (! grub_current_context->prev)
  104. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  105. "cannot close the initial context");
  106. /* Free the variables associated with this context. */
  107. for (i = 0; i < HASHSZ; i++)
  108. {
  109. struct grub_env_var *p, *q;
  110. for (p = grub_current_context->vars[i]; p; p = q)
  111. {
  112. q = p->next;
  113. grub_free (p->name);
  114. grub_free (p->value);
  115. grub_free (p);
  116. }
  117. }
  118. /* Restore the previous context. */
  119. context = grub_current_context->prev;
  120. grub_free (grub_current_context);
  121. grub_current_context = context;
  122. menu = current_menu->prev;
  123. if (current_menu->menu)
  124. grub_normal_free_menu (current_menu->menu);
  125. grub_free (current_menu);
  126. current_menu = menu;
  127. return GRUB_ERR_NONE;
  128. }
  129. grub_err_t
  130. grub_env_extractor_close (int source)
  131. {
  132. grub_menu_t menu = NULL;
  133. grub_menu_entry_t *last;
  134. grub_err_t err;
  135. if (source)
  136. {
  137. menu = grub_env_get_menu ();
  138. grub_env_unset_menu ();
  139. }
  140. err = grub_env_context_close ();
  141. if (source && menu)
  142. {
  143. grub_menu_t menu2;
  144. menu2 = grub_env_get_menu ();
  145. last = &menu2->entry_list;
  146. while (*last)
  147. last = &(*last)->next;
  148. *last = menu->entry_list;
  149. menu2->size += menu->size;
  150. }
  151. grub_extractor_level--;
  152. return err;
  153. }
  154. static grub_command_t export_cmd;
  155. static grub_err_t
  156. grub_cmd_export (struct grub_command *cmd __attribute__ ((unused)),
  157. int argc, char **args)
  158. {
  159. int i;
  160. if (argc < 1)
  161. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  162. N_("one argument expected"));
  163. for (i = 0; i < argc; i++)
  164. grub_env_export (args[i]);
  165. return 0;
  166. }
  167. void
  168. grub_context_init (void)
  169. {
  170. export_cmd = grub_register_command ("export", grub_cmd_export,
  171. N_("ENVVAR [ENVVAR] ..."),
  172. N_("Export variables."));
  173. }
  174. void
  175. grub_context_fini (void)
  176. {
  177. grub_unregister_command (export_cmd);
  178. }