static grub_err_t grub_cmd_lua (grub_command_t cmd __attribute__ ((unused)), int argc, char **args) { if (argc == 1) { if (luaL_loadfile (state, args[0])) { handle_lua_error ("Lua"); } else if (lua_pcall (state, 0, 0, 0)) { handle_lua_error ("Lua"); } } else if (argc == 0) { return interactive (); } else { return grub_error (GRUB_ERR_BAD_ARGUMENT, "1 or 0 arguments expected"); } return grub_errno; }
static void sqlite_lua(sqlite3_context *ctx, int nargs, sqlite3_value **args) { lua_State *L = (lua_State *) sqlite3_user_data(ctx); const char *lua_source; int status; if(nargs < 1) { const char error[] = "No argument passed to lua()"; sqlite3_result_error(ctx, error, sizeof(error) - 1); return; } lua_source = (const char *) sqlite3_value_text(args[0]); /* we prefix "return " first to enable automatic returning of values */ lua_pushliteral(L, "return "); lua_pushstring(L, lua_source); lua_concat(L, 2); status = luaL_loadstring(L, lua_tostring(L, -1)); lua_remove(L, -2); if(status == LUA_ERRSYNTAX) { lua_pop(L, 1); status = luaL_loadstring(L, lua_source); } if(status) { handle_lua_error(L, ctx); return; } convert_sqlite_values_to_lua(L, nargs - 1, args + 1); insert_args_into_globals(L, nargs - 1); status = lua_pcall(L, nargs - 1, 1, 0); if(status) { handle_lua_error(L, ctx); return; } convert_lua_value_to_sqlite(L, ctx); }
static grub_err_t interactive (void) { const char *ps1 = "lua> "; const char *ps2 = "lua>> "; const char *prompt = ps1; char *line; char *chunk = NULL; size_t len; size_t oldlen = 0; int r; grub_printf ("%s", N_ ("Welcome to lua, press the escape key to exit.")); while ((line = grub_cmdline_get (prompt)) != NULL) { /* len = lenght of chunk + line + newline character */ len = oldlen + grub_strlen (line) + 1; chunk = grub_realloc (chunk, len + 1); grub_strcpy (chunk + oldlen , line); chunk[len - 1] = '\n'; chunk[len] = '\0'; grub_free (line); r = luaL_loadbuffer (state, chunk, len, "stdin"); if (!r) { /* No error: Execute this chunk and prepare to read another */ r = lua_pcall (state, 0, 0, 0); if (r) { handle_lua_error ("Lua"); grub_print_error (); } grub_free (chunk); chunk = NULL; len = 0; prompt = ps1; } else if (incomplete (state, r)) { /* Chunk is incomplete, try reading another line */ prompt = ps2; } else if (r == LUA_ERRSYNTAX) { handle_lua_error ("Lua"); grub_print_error (); /* This chunk is garbage, try starting another one */ grub_free (chunk); chunk = NULL; len = 0; prompt = ps1; } else { /* Handle errors other than syntax errors (out of memory, etc.) */ grub_free (chunk); handle_lua_error ("Lua parser failed"); return grub_errno; } oldlen = len; } grub_free (chunk); lua_gc (state, LUA_GCCOLLECT, 0); return grub_errno; }