function.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. static grub_script_function_t grub_script_function_list;
  23. grub_script_function_t
  24. grub_script_function_create (struct grub_script_arg *functionname_arg,
  25. struct grub_script *cmd)
  26. {
  27. grub_script_function_t func;
  28. grub_script_function_t *p;
  29. func = (grub_script_function_t) grub_malloc (sizeof (*func));
  30. if (! func)
  31. return 0;
  32. func->name = grub_strdup (functionname_arg->str);
  33. if (! func->name)
  34. {
  35. grub_free (func);
  36. return 0;
  37. }
  38. func->func = cmd;
  39. /* Keep the list sorted for simplicity. */
  40. p = &grub_script_function_list;
  41. while (*p)
  42. {
  43. if (grub_strcmp ((*p)->name, func->name) >= 0)
  44. break;
  45. p = &((*p)->next);
  46. }
  47. /* If the function already exists, overwrite the old function. */
  48. if (*p && grub_strcmp ((*p)->name, func->name) == 0)
  49. {
  50. grub_script_function_t q;
  51. q = *p;
  52. grub_script_free (q->func);
  53. q->func = cmd;
  54. grub_free (func);
  55. func = q;
  56. }
  57. else
  58. {
  59. func->next = *p;
  60. *p = func;
  61. }
  62. return func;
  63. }
  64. void
  65. grub_script_function_remove (const char *name)
  66. {
  67. grub_script_function_t *p, q;
  68. for (p = &grub_script_function_list, q = *p; q; p = &(q->next), q = q->next)
  69. if (grub_strcmp (name, q->name) == 0)
  70. {
  71. *p = q->next;
  72. grub_free (q->name);
  73. grub_script_free (q->func);
  74. grub_free (q);
  75. break;
  76. }
  77. }
  78. grub_script_function_t
  79. grub_script_function_find (char *functionname)
  80. {
  81. grub_script_function_t func;
  82. for (func = grub_script_function_list; func; func = func->next)
  83. if (grub_strcmp (functionname, func->name) == 0)
  84. break;
  85. if (! func)
  86. grub_error (GRUB_ERR_UNKNOWN_COMMAND, "unknown command `%.20s'", functionname);
  87. return func;
  88. }
  89. int
  90. grub_script_function_iterate (int (*iterate) (grub_script_function_t))
  91. {
  92. grub_script_function_t func;
  93. for (func = grub_script_function_list; func; func = func->next)
  94. if (iterate (func))
  95. return 1;
  96. return 0;
  97. }
  98. int
  99. grub_script_function_call (grub_script_function_t func,
  100. int argc __attribute__((unused)),
  101. char **args __attribute__((unused)))
  102. {
  103. /* XXX: Arguments are not supported yet. */
  104. return grub_script_execute (func->func);
  105. }