2
* GRUB -- GRand Unified Bootloader
3
* Copyright (C) 2009 Free Software Foundation, Inc.
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.
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.
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/>.
25
#include <grub/command.h>
26
#include <grub/i18n.h>
27
#include <grub/misc.h>
28
#include <grub/normal.h>
31
scan_str (const char *s1, const char *s2)
51
strcspn (const char *s1, const char *s2)
55
r = scan_str (s1, s2);
60
strpbrk (const char *s1, const char *s2)
64
r = scan_str (s1, s2);
65
return (*r) ? (char *) r : 0;
69
memchr (const void *s, int c, size_t n)
71
const unsigned char *p = s;
85
static lua_State *state;
87
/* Call `grub_error' to report a Lua error. The error message string must be
88
on the top of the Lua stack (at index -1). The error message is popped off
89
the Lua stack before this function returns. */
91
handle_lua_error (const char *error_type)
93
const char *error_msg;
94
error_msg = lua_tostring (state, -1);
95
if (error_msg == NULL)
96
error_msg = "(error message not a string)";
97
grub_error (GRUB_ERR_BAD_ARGUMENT, "%s: %s", error_type, error_msg);
98
/* Pop the error message. */
102
/* Taken from lua.c */
104
incomplete (lua_State * L, int status)
106
if (status == LUA_ERRSYNTAX)
109
const char *msg = lua_tolstring (L, -1, &lmsg);
110
const char *tp = msg + lmsg - (sizeof (LUA_QL ("<eof>")) - 1);
111
if (strstr (msg, LUA_QL ("<eof>")) == tp)
117
return 0; /* else... */
124
char *ps2 = "lua>> ";
132
grub_printf ("%s", N_ ("Welcome to lua, press the escape key to exit."));
134
while ((line = grub_cmdline_get (prompt)) != NULL)
136
/* len = lenght of chunk + line + newline character */
137
len = oldlen + grub_strlen (line) + 1;
138
chunk = grub_realloc (chunk, len + 1);
139
grub_strcpy (chunk + oldlen , line);
140
chunk[len - 1] = '\n';
144
r = luaL_loadbuffer (state, chunk, len, "stdin");
147
/* No error: Execute this chunk and prepare to read another */
148
r = lua_pcall (state, 0, 0, 0);
151
handle_lua_error ("Lua");
160
else if (incomplete (state, r))
162
/* Chunk is incomplete, try reading another line */
165
else if (r == LUA_ERRSYNTAX)
167
handle_lua_error ("Lua");
170
/* This chunk is garbage, try starting another one */
178
/* Handle errors other than syntax errors (out of memory, etc.) */
180
handle_lua_error ("Lua parser failed");
188
lua_gc (state, LUA_GCCOLLECT, 0);
194
grub_cmd_lua (grub_command_t cmd __attribute__ ((unused)),
195
int argc, char **args)
199
if (luaL_loadfile (state, args[0]))
201
handle_lua_error ("Lua");
203
else if (lua_pcall (state, 0, 0, 0))
205
handle_lua_error ("Lua");
210
return interactive ();
214
return grub_error (GRUB_ERR_BAD_ARGUMENT, "1 or 0 arguments expected");
220
static grub_command_t cmd;
229
lua_gc (state, LUA_GCSTOP, 0);
230
luaL_openlibs (state);
231
luaL_register (state, "grub", grub_lua_lib);
232
lua_gc (state, LUA_GCRESTART, 0);
233
cmd = grub_register_command ("lua", grub_cmd_lua, N_("[FILE]"),
234
N_ ("Run lua script FILE or start interactive lua shell"));
243
grub_unregister_command (cmd);