From f66f0b34c8aabe8883a85b46d42e5893d633c81c Mon Sep 17 00:00:00 2001 From: rr- Date: Tue, 20 Sep 2016 20:32:16 +0200 Subject: lua: expose subprocess_detached --- DOCS/man/lua.rst | 13 +++++++++++++ player/lua.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/DOCS/man/lua.rst b/DOCS/man/lua.rst index d8252a2dd5..cb32baabc1 100644 --- a/DOCS/man/lua.rst +++ b/DOCS/man/lua.rst @@ -635,6 +635,19 @@ strictly part of the guaranteed API. In all cases, ``mp.resume_all()`` is implicitly called. +``utils.subprocess_detached(t)`` + Runs an external process and detaches it from mpv's control. + + The parameter ``t`` is a table. The function reads the following entries: + + ``args`` + Array of strings of the same semantics as the ``args`` used in the + ``subprocess`` function. + + The function returns ``nil``. + + In all cases, ``mp.resume_all()`` is implicitly called. + ``utils.parse_json(str [, trail])`` Parses the given string argument as JSON, and returns it as a Lua table. On error, returns ``nil, error``. (Currently, ``error`` is just a string diff --git a/player/lua.c b/player/lua.c index 1c3d526349..8bf8f2b590 100644 --- a/player/lua.c +++ b/player/lua.c @@ -1185,6 +1185,37 @@ static int script_subprocess(lua_State *L) return 1; } +static int script_subprocess_detached(lua_State *L) +{ + struct script_ctx *ctx = get_ctx(L); + luaL_checktype(L, 1, LUA_TTABLE); + void *tmp = mp_lua_PITA(L); + + mp_resume_all(ctx->client); + + lua_getfield(L, 1, "args"); // args + int num_args = mp_lua_len(L, -1); + char *args[256]; + if (num_args > MP_ARRAY_SIZE(args) - 1) // last needs to be NULL + luaL_error(L, "too many arguments"); + if (num_args < 1) + luaL_error(L, "program name missing"); + for (int n = 0; n < num_args; n++) { + lua_pushinteger(L, n + 1); // args n + lua_gettable(L, -2); // args arg + args[n] = talloc_strdup(tmp, lua_tostring(L, -1)); + if (!args[n]) + luaL_error(L, "program arguments must be strings"); + lua_pop(L, 1); // args + } + args[num_args] = NULL; + lua_pop(L, 1); // - + + mp_subprocess_detached(ctx->log, args); + lua_pushnil(L); + return 1; +} + static int script_parse_json(lua_State *L) { mp_lua_optarg(L, 2); @@ -1269,6 +1300,7 @@ static const struct fn_entry utils_fns[] = { FN_ENTRY(split_path), FN_ENTRY(join_path), FN_ENTRY(subprocess), + FN_ENTRY(subprocess_detached), FN_ENTRY(parse_json), FN_ENTRY(format_json), {0} -- cgit v1.2.3