diff options
author | Robin Gareus <robin@gareus.org> | 2017-08-10 02:17:41 +0200 |
---|---|---|
committer | Robin Gareus <robin@gareus.org> | 2017-08-10 02:26:11 +0200 |
commit | 418570b04129eed250e3047cc83bc4d70113f5ab (patch) | |
tree | 22f1b7858c6883dc0a563782663100468203f2cc /gtk2_ardour | |
parent | 80e0094ef486370d696e873fc8ba141acf6f43a5 (diff) |
Update Lua os.exec, SystemExec API
We can't use Lua to construct a PBD::SystemExec Obejct. Lifetime of the
object is bound to the Lua interpreter or local function scope.
Destroying the C++ object terminates the process.
Additionally to adding a dedicated method, we also override the existing
os.execute Lua libary method with a rt-save (vfork, close filedescriptors)
wrapper.
Diffstat (limited to 'gtk2_ardour')
-rw-r--r-- | gtk2_ardour/luainstance.cc | 66 |
1 files changed, 60 insertions, 6 deletions
diff --git a/gtk2_ardour/luainstance.cc b/gtk2_ardour/luainstance.cc index 9db09e38dc..cfa380e700 100644 --- a/gtk2_ardour/luainstance.cc +++ b/gtk2_ardour/luainstance.cc @@ -368,6 +368,60 @@ namespace LuaMixer { } }; +//////////////////////////////////////////////////////////////////////////////// + +static PBD::ScopedConnectionList _luaexecs; + +static void reaper (ARDOUR::SystemExec* x) +{ + delete x; +} + +static int +lua_forkexec (lua_State *L) +{ + int argc = lua_gettop (L); + if (argc == 0) { + return luaL_argerror (L, 1, "invalid number of arguments, forkexec (command, ...)"); + } + // args are free()ed in ~SystemExec + char** args = (char**) malloc ((argc + 1) * sizeof(char*)); + for (int i = 0; i < argc; ++i) { + args[i] = strdup (luaL_checkstring (L, i + 1)); + } + args[argc] = 0; + + ARDOUR::SystemExec* x = new ARDOUR::SystemExec (args[0], args); + x->Terminated.connect (_luaexecs, MISSING_INVALIDATOR, boost::bind (&reaper, x), gui_context()); + + if (x->start()) { + reaper (x); + luabridge::Stack<bool>::push (L, false); + return -1; + } else { + luabridge::Stack<bool>::push (L, false); + } + return 1; +} + +#ifndef PLATFORM_WINDOWS +static int +lua_exec (std::string cmd) +{ + // args are free()ed in ~SystemExec + char** args = (char**) malloc (4 * sizeof(char*)); + args[0] = strdup ("/bin/sh"); + args[1] = strdup ("-c"); + args[2] = strdup (cmd.c_str()); + args[3] = 0; + ARDOUR::SystemExec x ("/bin/sh", args); + if (x.start()) { + return -1; + } + x.wait (); + return 0; +} +#endif //////////////////////////////////////////////////////////////////////////////// @@ -878,12 +932,12 @@ LuaInstance::register_classes (lua_State* L) .endNamespace () // end ArdourUI - .beginNamespace ("ARDOUR") - .beginClass <ARDOUR::SystemExec> ("SystemExec") - .addConstructor <void (*) (std::string, std::string)> () - .addFunction ("start", &ARDOUR::SystemExec::start) - .endClass () - .endNamespace (); // end ARDOUR + .beginNamespace ("os") +#ifndef PLATFORM_WINDOWS + .addFunction ("execute", &lua_exec) +#endif + .addCFunction ("forkexec", &lua_forkexec) + .endNamespace (); // Editing Symbols |