function.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2005,2007,2009,2010 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/misc.h>
  19. #include <grub/script_sh.h>
  20. #include <grub/parser.h>
  21. #include <grub/mm.h>
  22. #include <grub/charset.h>
  23. grub_script_function_t grub_script_function_list;
  24. grub_script_function_t
  25. grub_script_function_create (struct grub_script_arg *functionname_arg,
  26. struct grub_script *cmd)
  27. {
  28. grub_script_function_t func;
  29. grub_script_function_t *p;
  30. func = (grub_script_function_t) grub_malloc (sizeof (*func));
  31. if (! func)
  32. return 0;
  33. func->executing = 0;
  34. func->name = grub_strdup (functionname_arg->str);
  35. if (! func->name)
  36. {
  37. grub_free (func);
  38. return 0;
  39. }
  40. func->func = cmd;
  41. /* Keep the list sorted for simplicity. */
  42. p = &grub_script_function_list;
  43. while (*p)
  44. {
  45. if (grub_strcmp ((*p)->name, func->name) >= 0)
  46. break;
  47. p = &((*p)->next);
  48. }
  49. /* If the function already exists, overwrite the old function. */
  50. if (*p && grub_strcmp ((*p)->name, func->name) == 0)
  51. {
  52. grub_script_function_t q;
  53. q = *p;
  54. grub_free (func);
  55. if (q->executing > 0)
  56. {
  57. grub_error (GRUB_ERR_BAD_ARGUMENT,
  58. N_("attempt to redefine a function being executed"));
  59. func = NULL;
  60. }
  61. else
  62. {
  63. grub_script_free (q->func);
  64. q->func = cmd;
  65. func = q;
  66. }
  67. }
  68. else
  69. {
  70. func->next = *p;
  71. *p = func;
  72. }
  73. return func;
  74. }
  75. void
  76. grub_script_function_remove (const char *name)
  77. {
  78. grub_script_function_t *p, q;
  79. for (p = &grub_script_function_list, q = *p; q; p = &(q->next), q = q->next)
  80. if (grub_strcmp (name, q->name) == 0)
  81. {
  82. *p = q->next;
  83. grub_free (q->name);
  84. grub_script_free (q->func);
  85. grub_free (q);
  86. break;
  87. }
  88. }
  89. grub_script_function_t
  90. grub_script_function_find (char *functionname)
  91. {
  92. grub_script_function_t func;
  93. for (func = grub_script_function_list; func; func = func->next)
  94. if (grub_strcmp (functionname, func->name) == 0)
  95. break;
  96. if (! func)
  97. {
  98. char tmp[21];
  99. grub_strncpy (tmp, functionname, 20);
  100. tmp[20] = 0;
  101. /* Avoid truncating inside UTF-8 character. */
  102. tmp[grub_getend (tmp, tmp + grub_strlen (tmp))] = 0;
  103. grub_error (GRUB_ERR_UNKNOWN_COMMAND, N_("can't find command `%s'"), tmp);
  104. }
  105. return func;
  106. }