summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorRobin Gareus <robin@gareus.org>2018-11-12 23:14:48 +0100
committerRobin Gareus <robin@gareus.org>2018-11-15 23:05:22 +0100
commitf83e87cf3b75a0eaf8a306b8a242ad79730b3879 (patch)
treef74b6635f204c2d52006671e0c8d17b7006ab6d9 /tools
parent9b2612f68615d1b3f80fce732e4a13f5e50ee87d (diff)
LuaSession: allow multi-line commands and functions
Diffstat (limited to 'tools')
-rw-r--r--tools/luadevel/luasession.cc38
1 files changed, 37 insertions, 1 deletions
diff --git a/tools/luadevel/luasession.cc b/tools/luadevel/luasession.cc
index c6977529c0..13c4460c9d 100644
--- a/tools/luadevel/luasession.cc
+++ b/tools/luadevel/luasession.cc
@@ -398,6 +398,19 @@ static void setup_lua ()
AudioEngine::instance ()->stop ();
}
+static int
+incomplete (lua_State* L, int status) {
+ if (status == LUA_ERRSYNTAX) {
+ size_t lmsg;
+ const char *msg = lua_tolstring (L, -1, &lmsg);
+ if (lmsg >= 5 && strcmp(msg + lmsg - 5, "<eof>") == 0) {
+ lua_pop(L, 1);
+ return 1;
+ }
+ }
+ return 0;
+}
+
int main (int argc, char **argv)
{
init ();
@@ -406,6 +419,7 @@ int main (int argc, char **argv)
using_history ();
std::string histfile = Glib::build_filename (user_config_directory(), "/luahist");
+ rl_bind_key ('\t', rl_insert); // disable completion
read_history (histfile.c_str());
char *line = NULL;
@@ -421,8 +435,30 @@ int main (int argc, char **argv)
continue;
}
+ do {
+ LuaState lt;
+ lua_State* L = lt.getState ();
+ int status = luaL_loadbuffer (L, line, strlen(line), "=stdin");
+ if (!incomplete (L, status)) {
+ break;
+ }
+ char *l2 = readline (">> ");
+ if (!l2) {
+ break;
+ }
+ if (strlen (l2) == 0) {
+ continue;
+ }
+ line = (char*) realloc ((void*)line, (strlen(line) + strlen (l2) + 2) * sizeof(char));
+ strcat (line, "\n");
+ strcat (line, l2);
+ free (l2);
+ } while (1);
+
if (lua->do_command (line)) {
- // error
+ /* error */
+ free (line); line = NULL;
+ continue;
}
add_history (line);