From bbd34d9c87517994462904d965f4ae70cd5f8dab Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Wed, 9 Apr 2014 09:22:04 +1000 Subject: Pass C command arguments to skang. --- ClientHamr/GuiLua/GuiLua.c | 21 +++++++++++++++++- ClientHamr/GuiLua/skang.lua | 54 +++++++++++++++++++++++++++++++-------------- 2 files changed, 57 insertions(+), 18 deletions(-) diff --git a/ClientHamr/GuiLua/GuiLua.c b/ClientHamr/GuiLua/GuiLua.c index e3af5ee..25625aa 100644 --- a/ClientHamr/GuiLua/GuiLua.c +++ b/ClientHamr/GuiLua/GuiLua.c @@ -416,6 +416,8 @@ int luaopen_widget(lua_State *L) lua_pushstring(L, "skang"); lua_call(L, 1, 1); skang = lua_gettop(L); + lua_setfield(L, LUA_REGISTRYINDEX, "skang"); + lua_getfield(L, LUA_REGISTRYINDEX, "skang"); // local _M = skang.moduleBegin('widget', nil, 'Copyright 2014 David Seikel', '0.1', '2014-04-08 00:42:00', nil, false) lua_getfield(L, skang, "moduleBegin"); @@ -468,6 +470,7 @@ int luaopen_widget(lua_State *L) int main(int argc, char **argv) { int result = EXIT_FAILURE; + lua_Number i; memset(&ourGlobals, 0, sizeof(globals)); @@ -482,7 +485,23 @@ int main(int argc, char **argv) // luaopen_widget() expects a string argument, and returns a table. // Though in this case, both get ignored anyway. lua_pushstring(ourGlobals.L, "widget"); - lua_pop(ourGlobals.L, luaopen_widget(ourGlobals.L)); + lua_pop(ourGlobals.L, luaopen_widget(ourGlobals.L) + 1); + + // Pass all our command line arguments to skang. + i = 1; + lua_getfield(ourGlobals.L, LUA_REGISTRYINDEX, "skang"); + lua_getfield(ourGlobals.L, 1, "scanArguments"); + lua_newtable(ourGlobals.L); + while (--argc > 0 && *++argv != '\0') + { + lua_pushnumber(ourGlobals.L, i++); + lua_pushstring(ourGlobals.L, *argv); + lua_settable(ourGlobals.L, -3); + } + lua_call(ourGlobals.L, 1, 0); + lua_getfield(ourGlobals.L, 1, "pullArguments"); + lua_getfield(ourGlobals.L, LUA_REGISTRYINDEX, "skang"); + lua_call(ourGlobals.L, 1, 0); // Run the main loop via a Lua call. lua_pop(ourGlobals.L, loopWindow(ourGlobals.L)); diff --git a/ClientHamr/GuiLua/skang.lua b/ClientHamr/GuiLua/skang.lua index 4f221a0..2957fcc 100644 --- a/ClientHamr/GuiLua/skang.lua +++ b/ClientHamr/GuiLua/skang.lua @@ -200,12 +200,21 @@ TODO - Finish supporting all of the above. arg1=value1&arg2=value2 For URLs. arg1=value1|arg2=value2 Can't remember why, probably the old skang multivalue syntax. Test it all. + Skang command line should have standardish stuff, like --version, --help, --help module.thing. + Lua does these already, might be no need to do them ourselves - + -e 'some code'. + -i go interactive after running the script. + -v version. + - read from stdin non interactively. + LuaJIT also has this - + -- stop processing options. ]] ARGS = {} lua = '' command = '' + -- Do an initial scan and tokenise of the command line arguments. scanArguments = function (args) if args then @@ -258,6 +267,21 @@ parseType = function (module, thingy, v, value) end end + if 'function' == thingy.types[1] then + local args = {} + -- TODO - Should allow more than one argument, but would need to pass in ARGS and i. + if 2 == #thingy.types then + if value then + -- TODO - Should check the type of the arguments. + args[#args + 1] = value[2] + module[v[2] ](args[1]) + value[2] = nil -- Mark it as used. + else + print('ERROR - Expected an argument for ' .. thingy.names[1]) + end + else + module[v[2] ]() + end if 'boolean' == thingy.types[1] then if value then -- Only parse the next value as a boolean if it doesn't have an introducer. @@ -273,15 +297,7 @@ parseType = function (module, thingy, v, value) end end --- Restore the environment, and grab paramateres from standard places. -moduleEnd = function (module) - -- See if there is a properties file, and run it in the modules environment. - local properties = loadfile(module._NAME .. '.properties') - if properties then - setfenv(properties, getfenv(2)) - properties() - end - +pullArguments = function (module) -- Look for our command line arguments. local metaMum = getmetatable(module) if metaMum and metaMum.__self then @@ -318,7 +334,18 @@ moduleEnd = function (module) end end end +end +-- Restore the environment, and grab paramateres from standard places. +moduleEnd = function (module) + -- See if there is a properties file, and run it in the modules environment. + local properties = loadfile(module._NAME .. '.properties') + if properties then + setfenv(properties, getfenv(2)) + properties() + end + + pullArguments(module) if module.isLua then setfenv(2, module.savedEnvironment) end end @@ -529,14 +556,6 @@ TODO - Maybe try looking in the skang table for Things that are not found? Maybe put Things in the skang table that are unique from modules? I think this is what matrix-RAD Collisions was all about. - Skang command line should have standardish stuff, like --version, --help, --help module.thing. - Lua does these already, might be no need to do them ourselves - - -e 'some code'. - -i go interactive after running the script. - -v version. - - read from stdin non interactively. - LuaJIT has this - - -- stop processing options. ]] -- There is no ThingSpace, or Stuff, now it's all just in this meta table. @@ -949,6 +968,7 @@ end module = function (name) end + skang = function (name) end quit = function () -- cgit v1.1