2 * (c) 2010 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
3 * Alexander Warg <warg@os.inf.tu-dresden.de>
4 * economic rights: Technische Universität Dresden (Germany)
6 * This file is part of TUD:OS and distributed under the terms of the
7 * GNU General Public License 2.
8 * Please see the COPYING-GPL-2 file for details.
11 #include <l4/sys/err.h>
23 #include <readline/history.h>
24 #include <readline/readline.h>
30 static Lua::Lib *_lua_init;
32 extern char const _binary_ned_lua_start[];
33 extern char const _binary_ned_lua_end[];
35 Lua::Lib::Lib(Prio prio) : _prio(prio), _next(0)
38 while (*f && (*f)->prio() < prio)
46 Lua::Lib::run_init(lua_State *l)
48 for (Lib *c = _lua_init; c; c = c->_next)
53 static const luaL_Reg libs[] =
56 // { LUA_IOLIBNAME, luaopen_io },
57 { LUA_STRLIBNAME, luaopen_string },
58 {LUA_LOADLIBNAME, luaopen_package},
59 {LUA_DBLIBNAME, luaopen_debug},
63 static char const *const options = "+i";
64 static struct option const loptions[] =
65 {{"interactive", 0, NULL, 'i' },
68 int lua(int argc, char const *const *argv)
71 printf("Ned says: Hi World!\n");
73 bool interactive = false;
79 while ((opt = getopt_long(argc, const_cast<char *const*>(argv), options, loptions, NULL)) != -1)
83 case 'i': interactive = true; break;
99 for (int i = 0; libs[i].func; ++i)
101 lua_pushcfunction(L, libs[i].func);
102 lua_pushstring(L,libs[i].name);
105 Lua::init_lua_cap(L);
106 Lua::Lib::run_init(L);
108 if (luaL_loadbuffer(L, _binary_ned_lua_start, _binary_ned_lua_end - _binary_ned_lua_start, "@ned.lua"))
110 printf("Ned: script: ---\n%.*s\n---", (int)(_binary_ned_lua_end - _binary_ned_lua_start), _binary_ned_lua_start);
111 fprintf(stderr, "lua error: %s.\n", lua_tostring(L, -1));
112 lua_pop(L, lua_gettop(L));
116 if (lua_pcall(L, 0, 1, 0))
118 fprintf(stderr, "lua error: %s.\n", lua_tostring(L, -1));
119 lua_pop(L, lua_gettop(L));
123 lua_pop(L, lua_gettop(L));
125 for (int c = optind; c < argc; ++c)
127 printf("Ned: loading file: '%s'\n", argv[c]);
128 int e = luaL_dofile(L, argv[c]);
131 char const *error = lua_tostring(L, -1);
132 printf("Ned: ERROR: %s\n", error);
134 lua_pop(L, lua_gettop(L));
137 lua_gc(L, LUA_GCCOLLECT, 0);
143 fprintf(stderr, "Ned: Interactive mode not compiled in.\n");
145 printf("Ned: Interactive mode.\n");
148 cmd = readline((char*)"Ned: ");
150 //printf("INPUT: %s\n", cmd);
155 if (luaL_loadbuffer(L, cmd, strlen(cmd), "argument"))
157 fprintf(stderr, "lua couldn't parse '%s': %s.\n",
158 cmd, lua_tostring(L, -1));
163 if (lua_pcall(L, 0, 1, 0))
165 fprintf(stderr, "lua couldn't execute '%s': %s.\n",
166 cmd, lua_tostring(L, -1));
170 lua_pop(L, lua_gettop(L));