function.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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->name = grub_strdup (functionname_arg->str);
  34. if (! func->name)
  35. {
  36. grub_free (func);
  37. return 0;
  38. }
  39. func->func = cmd;
  40. /* Keep the list sorted for simplicity. */
  41. p = &grub_script_function_list;
  42. while (*p)
  43. {
  44. if (grub_strcmp ((*p)->name, func->name) >= 0)
  45. break;
  46. p = &((*p)->next);
  47. }
  48. /* If the function already exists, overwrite the old function. */
  49. if (*p && grub_strcmp ((*p)->name, func->name) == 0)
  50. {
  51. grub_script_function_t q;
  52. q = *p;
  53. grub_script_free (q->func);
  54. q->func = cmd;
  55. grub_free (func);
  56. func = q;
  57. }
  58. else
  59. {
  60. func->next = *p;
  61. *p = func;
  62. }
  63. return func;
  64. }
  65. void
  66. grub_script_function_remove (const char *name)
  67. {
  68. grub_script_function_t *p, q;
  69. for (p = &grub_script_function_list, q = *p; q; p = &(q->next), q = q->next)
  70. if (grub_strcmp (name, q->name) == 0)
  71. {
  72. *p = q->next;
  73. grub_free (q->name);
  74. grub_script_free (q->func);
  75. grub_free (q);
  76. break;
  77. }
  78. }
  79. grub_script_function_t
  80. grub_script_function_find (char *functionname)
  81. {
  82. grub_script_function_t func;
  83. for (func = grub_script_function_list; func; func = func->next)
  84. if (grub_strcmp (functionname, func->name) == 0)
  85. break;
  86. if (! func)
  87. {
  88. char tmp[21];
  89. grub_strncpy (tmp, functionname, 20);
  90. tmp[20] = 0;
  91. /* Avoid truncating inside UTF-8 character. */
  92. tmp[grub_getend (tmp, tmp + grub_strlen (tmp))] = 0;
  93. grub_error (GRUB_ERR_UNKNOWN_COMMAND, N_("can't find command `%s'"), tmp);
  94. }
  95. return func;
  96. }