lua add _fullpath and argument parsing

WIP for passing arguments to lua scripts

args are recognized by adding ?arg to the end of the script path
The easiest way to do this currently is to use the function rb.restart_lua

local sfile = rawget(_G, "_fullpath") or ""
local sArgs = rawget(_G, "_arguments")

if not sArgs then
    rb.restart_lua(sfile .. "?my arguments")
else
    rb.splash(1000, sfile .. "?" .. sArgs)
end

I'd eventually like to figure out a sensible way to do this from a
shortcut / quick list

Change-Id: I2b60fe3b8f1d04b57361fe532510bd6afee59fbf
This commit is contained in:
William Wilgus 2020-05-29 10:44:24 -05:00
parent fba4df5d6d
commit e4ee5980c6

View file

@ -146,12 +146,28 @@ static int docall (lua_State *L) {
}
static void lua_atexit(void);
static int lua_split_arguments(lua_State *L, const char *filename);
static int loadfile_newstate(lua_State **L, const char *filename)
{
const char *file;
int ret;
*L = luaL_newstate();
rb_atexit(lua_atexit);
lua_gc(*L, LUA_GCSTOP, 0); /* stop collector during initialization */
rocklua_openlibs(*L);
return luaL_loadfile(*L, filename);
lua_split_arguments(*L, filename);
lua_setglobal (*L, "_arguments");
file = lua_tostring (*L, -1);
lua_setglobal (*L, "_fullpath");
/* lua manual -> no guarantee pointer valid after value is removed from stack */
ret = luaL_loadfile(*L, file);
lua_gc(*L, LUA_GCRESTART, 0);
return ret;
}
static void lua_atexit(void)
@ -162,10 +178,14 @@ static void lua_atexit(void)
{
if (Ls == lua_touserdata(Ls, -1)) /* signal from restart_lua */
{
filename = (char *) malloc(MAX_PATH);
filename = (char *) malloc((MAX_PATH * 2) + 1);
if (filename) /* out of memory? */
rb->strlcpy(filename, lua_tostring(Ls, -2), MAX_PATH);
if (filename) {/* out of memory? */
filename[MAX_PATH * 2] = '\0';
rb->strlcpy(filename, lua_tostring(Ls, -2), MAX_PATH * 2);
} else {
goto ERR_RUN;
}
lua_close(Ls); /* close old state */
lu_status = loadfile_newstate(&Ls, filename);
@ -175,6 +195,7 @@ static void lua_atexit(void)
}
else if (lua_tointeger(Ls, -1) != 0) /* os.exit */
{
ERR_RUN:
lu_status = LUA_ERRRUN;
lua_pop(Ls, 1); /* put exit string on top of stack */
plugin_start(NULL);
@ -185,6 +206,25 @@ static void lua_atexit(void)
_exit(0); /* don't call exit handler */
}
/* split filename at argchar
* remainder of filename pushed on stack (-1)
* argument string pushed on stack or nil if doesn't exist (-2)
*/
static int lua_split_arguments(lua_State *L, const char *filename)
{
const char argchar = '?';
const char* arguments = strchr(filename, argchar);
if(arguments) {
lua_pushstring(L, (arguments + 1));
} else {
arguments = strlen(filename) + filename;
lua_pushnil(L);
}
lua_pushlstring(L, filename, arguments - filename);
lua_insert(L, -2); /* swap filename and argument */
return 2;
}
/***************** Plugin Entry Point *****************/
enum plugin_status plugin_start(const void* parameter)
{