2009-05-16 12:53:43 +00:00
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
|
|
|
* Copyright (C) 2009 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lua.h"
|
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "lualib.h"
|
|
|
|
#include "grub_lib.h"
|
|
|
|
|
|
|
|
#include <grub/dl.h>
|
|
|
|
#include <grub/parser.h>
|
|
|
|
|
2009-07-05 09:59:27 +00:00
|
|
|
static const char *
|
|
|
|
scan_str (const char *s1, const char *s2)
|
|
|
|
{
|
|
|
|
while (*s1)
|
|
|
|
{
|
|
|
|
const char *p = s2;
|
|
|
|
|
|
|
|
while (*p)
|
|
|
|
{
|
|
|
|
if (*s1 == *p)
|
|
|
|
return s1;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
s1++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
strcspn (const char *s1, const char *s2)
|
|
|
|
{
|
|
|
|
const char *r;
|
|
|
|
|
|
|
|
r = scan_str (s1, s2);
|
|
|
|
return r - s1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
strpbrk (const char *s1, const char *s2)
|
|
|
|
{
|
|
|
|
const char *r;
|
|
|
|
|
|
|
|
r = scan_str (s1, s2);
|
|
|
|
return (*r) ? (char *) r : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
memchr (const void *s, int c, size_t n)
|
|
|
|
{
|
|
|
|
const unsigned char *p = s;
|
|
|
|
|
|
|
|
while (n)
|
|
|
|
{
|
|
|
|
if (*p == c)
|
|
|
|
return (void *) p;
|
|
|
|
|
|
|
|
n--;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-16 12:53:43 +00:00
|
|
|
static lua_State *state;
|
|
|
|
|
2009-05-18 21:53:09 +00:00
|
|
|
/* Call `grub_error' to report a Lua error. The error message string must be
|
|
|
|
on the top of the Lua stack (at index -1). The error message is popped off
|
|
|
|
the Lua stack before this function returns. */
|
|
|
|
static void
|
|
|
|
handle_lua_error (const char *error_type)
|
|
|
|
{
|
|
|
|
const char *error_msg;
|
|
|
|
error_msg = lua_tostring (state, -1);
|
|
|
|
if (error_msg == NULL)
|
|
|
|
error_msg = "(error message not a string)";
|
|
|
|
grub_error (GRUB_ERR_BAD_ARGUMENT, "%s: %s", error_type, error_msg);
|
|
|
|
/* Pop the error message. */
|
|
|
|
lua_pop (state, 1);
|
|
|
|
}
|
|
|
|
|
2009-05-16 12:53:43 +00:00
|
|
|
static grub_err_t
|
|
|
|
grub_lua_parse_line (char *line, grub_reader_getline_t getline)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
char *old_line = 0;
|
|
|
|
|
|
|
|
lua_settop(state, 0);
|
|
|
|
while (1)
|
|
|
|
{
|
2009-05-18 21:53:09 +00:00
|
|
|
r = luaL_loadbuffer (state, line, grub_strlen (line), "=grub");
|
2009-05-16 12:53:43 +00:00
|
|
|
if (! r)
|
|
|
|
{
|
2009-05-18 21:53:09 +00:00
|
|
|
/* No error: Execute the statement. */
|
2009-05-16 12:53:43 +00:00
|
|
|
r = lua_pcall (state, 0, 0, 0);
|
|
|
|
if (r)
|
2009-05-18 21:53:09 +00:00
|
|
|
{
|
|
|
|
handle_lua_error ("Lua");
|
|
|
|
break;
|
|
|
|
}
|
2009-05-16 12:53:43 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
grub_free (old_line);
|
|
|
|
return grub_errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r == LUA_ERRSYNTAX)
|
|
|
|
{
|
2009-05-18 21:53:09 +00:00
|
|
|
/* Check whether the syntax error is a result of an incomplete
|
|
|
|
statement. If it is, then try to complete the statement by
|
|
|
|
reading more lines. */
|
2009-05-17 00:37:10 +00:00
|
|
|
size_t lmsg;
|
2009-05-18 21:53:09 +00:00
|
|
|
const char *msg = lua_tolstring (state, -1, &lmsg);
|
|
|
|
const char *tp = msg + lmsg - (sizeof (LUA_QL ("<eof>")) - 1);
|
|
|
|
if (grub_strstr (msg, LUA_QL ("<eof>")) == tp)
|
2009-05-16 12:53:43 +00:00
|
|
|
{
|
|
|
|
char *n, *t;
|
|
|
|
int len;
|
|
|
|
|
2009-05-18 21:53:09 +00:00
|
|
|
/* Discard the error message. */
|
|
|
|
lua_pop (state, 1);
|
|
|
|
/* Try to read another line to complete the statement. */
|
2009-05-16 12:53:43 +00:00
|
|
|
if ((getline (&n, 1)) || (! n))
|
|
|
|
{
|
|
|
|
grub_error (GRUB_ERR_BAD_ARGUMENT, "incomplete command");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-05-18 21:53:09 +00:00
|
|
|
/* More input was available: Add it to the current statement
|
|
|
|
contents. */
|
2009-05-16 12:53:43 +00:00
|
|
|
len = grub_strlen (line);
|
|
|
|
t = grub_malloc (len + grub_strlen (n) + 2);
|
|
|
|
if (! t)
|
|
|
|
break;
|
|
|
|
|
|
|
|
grub_strcpy (t, line);
|
|
|
|
t[len] = '\n';
|
|
|
|
grub_strcpy (t + len + 1, n);
|
|
|
|
grub_free (old_line);
|
|
|
|
line = old_line = t;
|
2009-05-18 21:53:09 +00:00
|
|
|
/* Try again to execute the statement now that more input has
|
|
|
|
been appended. */
|
2009-05-16 12:53:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
2009-05-18 21:53:09 +00:00
|
|
|
/* The syntax error was not the result of an incomplete line. */
|
|
|
|
handle_lua_error ("Lua syntax error");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Handle errors other than syntax errors (out of memory, etc.). */
|
|
|
|
handle_lua_error ("Lua parser failed");
|
2009-05-16 12:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
grub_free (old_line);
|
|
|
|
lua_gc (state, LUA_GCCOLLECT, 0);
|
|
|
|
|
|
|
|
return grub_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct grub_parser grub_lua_parser =
|
|
|
|
{
|
|
|
|
.name = "lua",
|
|
|
|
.parse_line = grub_lua_parse_line
|
|
|
|
};
|
|
|
|
|
|
|
|
GRUB_MOD_INIT(lua)
|
|
|
|
{
|
|
|
|
(void) mod;
|
|
|
|
|
|
|
|
state = lua_open ();
|
|
|
|
if (state)
|
|
|
|
{
|
2009-05-18 21:53:09 +00:00
|
|
|
lua_gc (state, LUA_GCSTOP, 0);
|
|
|
|
luaL_openlibs (state);
|
|
|
|
luaL_register (state, "grub", grub_lua_lib);
|
|
|
|
lua_gc (state, LUA_GCRESTART, 0);
|
2009-05-16 12:53:43 +00:00
|
|
|
grub_parser_register ("lua", &grub_lua_parser);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GRUB_MOD_FINI(lua)
|
|
|
|
{
|
|
|
|
if (state)
|
|
|
|
{
|
|
|
|
grub_parser_unregister (&grub_lua_parser);
|
2009-05-18 21:53:09 +00:00
|
|
|
lua_close (state);
|
2009-05-16 12:53:43 +00:00
|
|
|
}
|
|
|
|
}
|