From 5a88a8d4a476d071550b98eec2219e0311962bbf Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Thu, 26 Mar 2020 17:41:19 +0100 Subject: Allow Lua-session to read and interpret scripts --- tools/luadevel/luasession.cc | 103 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 95 insertions(+), 8 deletions(-) (limited to 'tools') diff --git a/tools/luadevel/luasession.cc b/tools/luadevel/luasession.cc index e345fb750c..14bd9bd56a 100644 --- a/tools/luadevel/luasession.cc +++ b/tools/luadevel/luasession.cc @@ -1,5 +1,6 @@ -#include #include +#include +#include #include #include @@ -7,6 +8,13 @@ #include #include +#ifdef PLATFORM_WINDOWS +# include +# include +#else +# include +#endif + #include #include "pbd/debug.h" @@ -403,11 +411,9 @@ incomplete (lua_State* L, int status) { return 0; } -int main (int argc, char **argv) +static void +interactive_interpreter () { - init (); - setup_lua (); - using_history (); std::string histfile = Glib::build_filename (user_config_directory(), "/luahist"); @@ -459,6 +465,90 @@ int main (int argc, char **argv) } free (line); printf ("\n"); + write_history (histfile.c_str()); +} + +static bool +is_tty () +{ +#ifdef PLATFORM_WINDOWS + return _isatty (_fileno (stdin)); +#else + return isatty (0); +#endif +} + +static void usage () +{ + printf ("ardour-lua - interactive Ardour Lua interpreter.\n\n"); + printf ("Usage: ardour-lua [ OPTIONS ] [ file ]\n\n"); + printf ("Options:\n\ + -h, --help display this help and exit\n\ + -i, --interactive enter interactive mode after executing 'script'\n\ + -V, --version print version information and exit\n\ +\n"); + printf ("\n\ +Ardour at your finger tips...\n\ +\n"); + printf ("Report bugs to \n" + "Website: \n"); + ::exit (EXIT_SUCCESS); +} + +int main (int argc, char **argv) +{ + + const char *optstring = "hiV"; + + const struct option longopts[] = { + { "help", 0, 0, 'h' }, + { "interactive", 0, 0, 'i' }, + { "version", 0, 0, 'V' }, + }; + + bool interactive = false; + + int c = 0; + while (EOF != (c = getopt_long (argc, argv, + optstring, longopts, (int *) 0))) { + switch (c) { + case 'h': + usage (); + break; + + case 'i': + interactive = true; + break; + + case 'V': + printf ("ardour-lua version %s\n\n", VERSIONSTRING); + printf ("Copyright (C) GPL 2015-2020 Robin Gareus \n"); + exit (EXIT_SUCCESS); + break; + + default: + cerr << "Error: unrecognized option. See --help for usage information.\n"; + ::exit (EXIT_FAILURE); + break; + } + } + + init (); + setup_lua (); + + if (argc > optind) { + lua->do_file (argv[optind]); + } else { + interactive = true; + } + + if (!interactive || !keep_running) { + /* continue to exit */ + } else if (is_tty ()) { + interactive_interpreter (); + } else { + luaL_dofile (lua->getState(), NULL); + } if (session) { close_session (); @@ -469,12 +559,9 @@ int main (int argc, char **argv) delete lua; lua = NULL; - write_history (histfile.c_str()); - AudioEngine::instance ()->stop (); AudioEngine::destroy (); - // cleanup ARDOUR::cleanup (); delete event_loop; pthread_cancel_all (); -- cgit v1.2.3