Refactor spawn.c

This moves some code of parse_command() out into a new helper function.
No functional changes intended.

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2017-08-12 16:32:27 +02:00 committed by Daniel Hahler
parent cc0871498c
commit aac519b946
1 changed files with 43 additions and 26 deletions

45
spawn.c
View File

@ -369,26 +369,20 @@ spawn_callback(gpointer user_data)
unsetenv("DESKTOP_STARTUP_ID");
}
/** Parse a command line.
/** Convert a Lua table of string to a char** array.
* \param L The Lua VM state.
* \param idx The index of the argument that we should parse
* \return The argv array for the new process.
* \param idx The index of the table that we should parse
* \return The argv array.
*/
static gchar **
parse_command(lua_State *L, int idx, GError **error)
parse_table_array(lua_State *L, int idx, GError **error)
{
gchar **argv = NULL;
idx = luaA_absindex(L, idx);
size_t i, len;
if (lua_isstring(L, idx))
{
const char *cmd = luaL_checkstring(L, idx);
if(!g_shell_parse_argv(cmd, NULL, &argv, error))
return NULL;
}
else if (lua_istable(L, idx))
{
size_t i, len = luaA_rawlen(L, idx);
luaL_checktype(L, idx, LUA_TTABLE);
idx = luaA_absindex(L, idx);
len = luaA_rawlen(L, idx);
/* First verify that the table is sane: All integer keys must contain
* strings. Do this by pushing them all onto the stack.
@ -413,6 +407,29 @@ parse_command(lua_State *L, int idx, GError **error)
argv[len - i - 1] = g_strdup(lua_tostring(L, -1));
lua_pop(L, 1);
}
return argv;
}
/** Parse a command line.
* \param L The Lua VM state.
* \param idx The index of the argument that we should parse
* \return The argv array for the new process.
*/
static gchar **
parse_command(lua_State *L, int idx, GError **error)
{
gchar **argv = NULL;
if (lua_isstring(L, idx))
{
const char *cmd = luaL_checkstring(L, idx);
if(!g_shell_parse_argv(cmd, NULL, &argv, error))
return NULL;
}
else if (lua_istable(L, idx))
{
argv = parse_table_array(L, idx, error);
}
else
{