grub_main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * BURG - Brand-new Universal loadeR from GRUB
  3. * Copyright 2009 Bean Lee - All Rights Reserved
  4. *
  5. * BURG 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. * BURG 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 BURG. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "lua.h"
  19. #include "lauxlib.h"
  20. #include "lualib.h"
  21. #include "grub_lib.h"
  22. #include <grub/dl.h>
  23. #include <grub/parser.h>
  24. static const char *
  25. scan_str (const char *s1, const char *s2)
  26. {
  27. while (*s1)
  28. {
  29. const char *p = s2;
  30. while (*p)
  31. {
  32. if (*s1 == *p)
  33. return s1;
  34. p++;
  35. }
  36. s1++;
  37. }
  38. return s1;
  39. }
  40. int
  41. strcspn (const char *s1, const char *s2)
  42. {
  43. const char *r;
  44. r = scan_str (s1, s2);
  45. return r - s1;
  46. }
  47. char *
  48. strpbrk (const char *s1, const char *s2)
  49. {
  50. const char *r;
  51. r = scan_str (s1, s2);
  52. return (*r) ? (char *) r : 0;
  53. }
  54. void *
  55. memchr (const void *s, int c, size_t n)
  56. {
  57. const unsigned char *p = s;
  58. while (n)
  59. {
  60. if (*p == c)
  61. return (void *) p;
  62. n--;
  63. p++;
  64. }
  65. return 0;
  66. }
  67. static lua_State *state;
  68. /* Call `grub_error' to report a Lua error. The error message string must be
  69. on the top of the Lua stack (at index -1). The error message is popped off
  70. the Lua stack before this function returns. */
  71. static void
  72. handle_lua_error (const char *error_type)
  73. {
  74. const char *error_msg;
  75. error_msg = lua_tostring (state, -1);
  76. if (error_msg == NULL)
  77. error_msg = "(error message not a string)";
  78. grub_error (GRUB_ERR_BAD_ARGUMENT, "%s: %s", error_type, error_msg);
  79. /* Pop the error message. */
  80. lua_pop (state, 1);
  81. }
  82. static grub_err_t
  83. grub_lua_parse_line (char *line, grub_reader_getline_t getline, void *closure)
  84. {
  85. int r;
  86. char *old_line = 0;
  87. lua_settop(state, 0);
  88. while (1)
  89. {
  90. r = luaL_loadbuffer (state, line, grub_strlen (line), "=grub");
  91. if (! r)
  92. {
  93. /* No error: Execute the statement. */
  94. r = lua_pcall (state, 0, 0, 0);
  95. if (r)
  96. {
  97. handle_lua_error ("Lua");
  98. break;
  99. }
  100. else
  101. {
  102. grub_free (old_line);
  103. return grub_errno;
  104. }
  105. }
  106. if (r == LUA_ERRSYNTAX)
  107. {
  108. /* Check whether the syntax error is a result of an incomplete
  109. statement. If it is, then try to complete the statement by
  110. reading more lines. */
  111. size_t lmsg;
  112. const char *msg = lua_tolstring (state, -1, &lmsg);
  113. const char *tp = msg + lmsg - (sizeof (LUA_QL ("<eof>")) - 1);
  114. if (grub_strstr (msg, LUA_QL ("<eof>")) == tp)
  115. {
  116. char *n, *t;
  117. int len;
  118. /* Discard the error message. */
  119. lua_pop (state, 1);
  120. /* Try to read another line to complete the statement. */
  121. if ((getline (&n, 1, closure)) || (! n))
  122. {
  123. grub_error (GRUB_ERR_BAD_ARGUMENT, "incomplete command");
  124. break;
  125. }
  126. /* More input was available: Add it to the current statement
  127. contents. */
  128. len = grub_strlen (line);
  129. t = grub_malloc (len + grub_strlen (n) + 2);
  130. if (! t)
  131. break;
  132. grub_strcpy (t, line);
  133. t[len] = '\n';
  134. grub_strcpy (t + len + 1, n);
  135. grub_free (old_line);
  136. line = old_line = t;
  137. /* Try again to execute the statement now that more input has
  138. been appended. */
  139. continue;
  140. }
  141. /* The syntax error was not the result of an incomplete line. */
  142. handle_lua_error ("Lua syntax error");
  143. }
  144. else
  145. {
  146. /* Handle errors other than syntax errors (out of memory, etc.). */
  147. handle_lua_error ("Lua parser failed");
  148. }
  149. break;
  150. }
  151. grub_free (old_line);
  152. lua_gc (state, LUA_GCCOLLECT, 0);
  153. return grub_errno;
  154. }
  155. static struct grub_parser grub_lua_parser =
  156. {
  157. .name = "lua",
  158. .parse_line = grub_lua_parse_line
  159. };
  160. GRUB_MOD_INIT(lua)
  161. {
  162. (void) mod;
  163. state = lua_open ();
  164. if (state)
  165. {
  166. lua_gc (state, LUA_GCSTOP, 0);
  167. luaL_openlibs (state);
  168. luaL_register (state, "grub", grub_lua_lib);
  169. lua_gc (state, LUA_GCRESTART, 0);
  170. grub_parser_register ("lua", &grub_lua_parser);
  171. }
  172. }
  173. GRUB_MOD_FINI(lua)
  174. {
  175. if (state)
  176. {
  177. grub_parser_unregister (&grub_lua_parser);
  178. lua_close (state);
  179. }
  180. }