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/parser.h>
26
#include <grub/command.h>
27
#include <grub/normal.h>
28
#include <grub/file.h>
29
#include <grub/device.h>
32
save_errno (lua_State *state)
36
saved_errno = grub_errno;
39
lua_pushinteger (state, saved_errno);
40
lua_setfield (state, LUA_GLOBALSINDEX, "grub_errno");
43
lua_pushstring (state, grub_errmsg);
47
lua_setfield (state, LUA_GLOBALSINDEX, "grub_errmsg");
53
push_result (lua_State *state)
55
lua_pushinteger (state, save_errno (state));
60
grub_lua_run (lua_State *state)
66
s = luaL_checkstring (state, 1);
67
if ((! grub_parser_split_cmdline (s, 0, &n, &args))
72
cmd = grub_command_find (args[0]);
74
(cmd->func) (cmd, n, &args[1]);
76
grub_error (GRUB_ERR_FILE_NOT_FOUND, "command not found");
82
return push_result (state);
86
grub_lua_getenv (lua_State *state)
90
n = lua_gettop (state);
91
for (i = 1; i <= n; i++)
93
const char *name, *value;
95
name = luaL_checkstring (state, i);
96
value = grub_env_get (name);
98
lua_pushstring (state, value);
107
grub_lua_setenv (lua_State *state)
109
const char *name, *value;
111
name = luaL_checkstring (state, 1);
112
value = luaL_checkstring (state, 2);
115
grub_env_set (name, value);
121
grub_lua_enum_device (lua_State *state)
123
auto int enum_device (const char *name);
124
int enum_device (const char *name)
130
dev = grub_device_open (name);
135
fs = grub_fs_probe (dev);
138
lua_pushvalue (state, 1);
139
lua_pushstring (state, name);
140
lua_pushstring (state, fs->name);
148
err = fs->uuid (dev, &uuid);
156
lua_pushstring (state, uuid);
161
lua_call (state, 3, 1);
162
result = lua_tointeger (state, -1);
167
grub_device_close (dev);
175
luaL_checktype (state, 1, LUA_TFUNCTION);
176
grub_device_iterate (enum_device);
177
return push_result (state);
181
grub_lua_enum_file (lua_State *state)
187
auto int enum_file (const char *name, const struct grub_dirhook_info *info);
188
int enum_file (const char *name, const struct grub_dirhook_info *info)
192
lua_pushvalue (state, 1);
193
lua_pushstring (state, name);
194
lua_pushinteger (state, info->dir != 0);
195
lua_call (state, 2, 1);
196
result = lua_tointeger (state, -1);
202
luaL_checktype (state, 1, LUA_TFUNCTION);
203
arg = luaL_checkstring (state, 2);
204
device_name = grub_file_get_device_name (arg);
205
dev = grub_device_open (device_name);
211
fs = grub_fs_probe (dev);
212
path = grub_strchr (arg, ')');
220
(fs->dir) (dev, path, enum_file);
223
grub_device_close (dev);
226
grub_free (device_name);
228
return push_result (state);
232
grub_lua_file_open (lua_State *state)
237
name = luaL_checkstring (state, 1);
238
file = grub_file_open (name);
244
lua_pushlightuserdata (state, file);
249
grub_lua_file_close (lua_State *state)
253
luaL_checktype (state, 1, LUA_TLIGHTUSERDATA);
254
file = lua_touserdata (state, 1);
255
grub_file_close (file);
257
return push_result (state);
261
grub_lua_file_seek (lua_State *state)
266
luaL_checktype (state, 1, LUA_TLIGHTUSERDATA);
267
file = lua_touserdata (state, 1);
268
offset = luaL_checkinteger (state, 2);
270
offset = grub_file_seek (file, offset);
273
lua_pushinteger (state, offset);
278
grub_lua_file_read (lua_State *state)
284
luaL_checktype (state, 1, LUA_TLIGHTUSERDATA);
285
file = lua_touserdata (state, 1);
286
n = luaL_checkinteger (state, 2);
288
luaL_buffinit (state, &b);
294
nr = (n > LUAL_BUFFERSIZE) ? LUAL_BUFFERSIZE : n;
295
p = luaL_prepbuffer (&b);
297
nr = grub_file_read (file, p, nr);
301
luaL_addsize (&b, nr);
306
luaL_pushresult (&b);
311
grub_lua_file_getline (lua_State *state)
316
luaL_checktype (state, 1, LUA_TLIGHTUSERDATA);
317
file = lua_touserdata (state, 1);
319
line = grub_file_getline (file);
325
lua_pushstring (state, line);
331
grub_lua_file_getsize (lua_State *state)
335
luaL_checktype (state, 1, LUA_TLIGHTUSERDATA);
336
file = lua_touserdata (state, 1);
338
lua_pushinteger (state, file->size);
343
grub_lua_file_getpos (lua_State *state)
347
luaL_checktype (state, 1, LUA_TLIGHTUSERDATA);
348
file = lua_touserdata (state, 1);
350
lua_pushinteger (state, file->offset);
355
grub_lua_file_eof (lua_State *state)
359
luaL_checktype (state, 1, LUA_TLIGHTUSERDATA);
360
file = lua_touserdata (state, 1);
362
lua_pushboolean (state, file->offset >= file->size);
367
grub_lua_file_exist (lua_State *state)
374
name = luaL_checkstring (state, 1);
375
file = grub_file_open (name);
379
grub_file_close (file);
384
lua_pushboolean (state, result);
389
grub_lua_add_menu (lua_State *state)
394
source = luaL_checklstring (state, 1, 0);
395
n = lua_gettop (state) - 1;
398
const char *args[sizeof (char *) * n];
402
for (i = 0; i < n; i++)
403
args[i] = luaL_checkstring (state, 2 + i);
405
p = grub_strdup (source);
407
return push_result (state);
409
grub_normal_add_menu_entry (n, args, p);
413
lua_pushstring (state, "not enough parameter");
417
return push_result (state);
420
luaL_Reg grub_lua_lib[] =
422
{"run", grub_lua_run},
423
{"getenv", grub_lua_getenv},
424
{"setenv", grub_lua_setenv},
425
{"enum_device", grub_lua_enum_device},
426
{"enum_file", grub_lua_enum_file},
427
{"file_open", grub_lua_file_open},
428
{"file_close", grub_lua_file_close},
429
{"file_seek", grub_lua_file_seek},
430
{"file_read", grub_lua_file_read},
431
{"file_getline", grub_lua_file_getline},
432
{"file_getsize", grub_lua_file_getsize},
433
{"file_getpos", grub_lua_file_getpos},
434
{"file_eof", grub_lua_file_eof},
435
{"file_exist", grub_lua_file_exist},
436
{"add_menu", grub_lua_add_menu},