aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ClientHamr/GuiLua
diff options
context:
space:
mode:
authorDavid Walter Seikel2014-04-09 09:22:04 +1000
committerDavid Walter Seikel2014-04-09 09:22:04 +1000
commitbbd34d9c87517994462904d965f4ae70cd5f8dab (patch)
tree91a46459547bd351bb32905d86e66714bf12b98f /ClientHamr/GuiLua
parentMake GuiLua a bit more real, with real Lua functions and stuff. (diff)
downloadSledjHamr-bbd34d9c87517994462904d965f4ae70cd5f8dab.zip
SledjHamr-bbd34d9c87517994462904d965f4ae70cd5f8dab.tar.gz
SledjHamr-bbd34d9c87517994462904d965f4ae70cd5f8dab.tar.bz2
SledjHamr-bbd34d9c87517994462904d965f4ae70cd5f8dab.tar.xz
Pass C command arguments to skang.
Diffstat (limited to 'ClientHamr/GuiLua')
-rw-r--r--ClientHamr/GuiLua/GuiLua.c21
-rw-r--r--ClientHamr/GuiLua/skang.lua54
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)
416 lua_pushstring(L, "skang"); 416 lua_pushstring(L, "skang");
417 lua_call(L, 1, 1); 417 lua_call(L, 1, 1);
418 skang = lua_gettop(L); 418 skang = lua_gettop(L);
419 lua_setfield(L, LUA_REGISTRYINDEX, "skang");
420 lua_getfield(L, LUA_REGISTRYINDEX, "skang");
419 421
420// local _M = skang.moduleBegin('widget', nil, 'Copyright 2014 David Seikel', '0.1', '2014-04-08 00:42:00', nil, false) 422// local _M = skang.moduleBegin('widget', nil, 'Copyright 2014 David Seikel', '0.1', '2014-04-08 00:42:00', nil, false)
421 lua_getfield(L, skang, "moduleBegin"); 423 lua_getfield(L, skang, "moduleBegin");
@@ -468,6 +470,7 @@ int luaopen_widget(lua_State *L)
468int main(int argc, char **argv) 470int main(int argc, char **argv)
469{ 471{
470 int result = EXIT_FAILURE; 472 int result = EXIT_FAILURE;
473 lua_Number i;
471 474
472 memset(&ourGlobals, 0, sizeof(globals)); 475 memset(&ourGlobals, 0, sizeof(globals));
473 476
@@ -482,7 +485,23 @@ int main(int argc, char **argv)
482 // luaopen_widget() expects a string argument, and returns a table. 485 // luaopen_widget() expects a string argument, and returns a table.
483 // Though in this case, both get ignored anyway. 486 // Though in this case, both get ignored anyway.
484 lua_pushstring(ourGlobals.L, "widget"); 487 lua_pushstring(ourGlobals.L, "widget");
485 lua_pop(ourGlobals.L, luaopen_widget(ourGlobals.L)); 488 lua_pop(ourGlobals.L, luaopen_widget(ourGlobals.L) + 1);
489
490 // Pass all our command line arguments to skang.
491 i = 1;
492 lua_getfield(ourGlobals.L, LUA_REGISTRYINDEX, "skang");
493 lua_getfield(ourGlobals.L, 1, "scanArguments");
494 lua_newtable(ourGlobals.L);
495 while (--argc > 0 && *++argv != '\0')
496 {
497 lua_pushnumber(ourGlobals.L, i++);
498 lua_pushstring(ourGlobals.L, *argv);
499 lua_settable(ourGlobals.L, -3);
500 }
501 lua_call(ourGlobals.L, 1, 0);
502 lua_getfield(ourGlobals.L, 1, "pullArguments");
503 lua_getfield(ourGlobals.L, LUA_REGISTRYINDEX, "skang");
504 lua_call(ourGlobals.L, 1, 0);
486 505
487 // Run the main loop via a Lua call. 506 // Run the main loop via a Lua call.
488 lua_pop(ourGlobals.L, loopWindow(ourGlobals.L)); 507 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.
200 arg1=value1&arg2=value2 For URLs. 200 arg1=value1&arg2=value2 For URLs.
201 arg1=value1|arg2=value2 Can't remember why, probably the old skang multivalue syntax. 201 arg1=value1|arg2=value2 Can't remember why, probably the old skang multivalue syntax.
202 Test it all. 202 Test it all.
203 Skang command line should have standardish stuff, like --version, --help, --help module.thing.
204 Lua does these already, might be no need to do them ourselves -
205 -e 'some code'.
206 -i go interactive after running the script.
207 -v version.
208 - read from stdin non interactively.
209 LuaJIT also has this -
210 -- stop processing options.
203]] 211]]
204 212
205ARGS = {} 213ARGS = {}
206lua = '' 214lua = ''
207command = '' 215command = ''
208 216
217
209-- Do an initial scan and tokenise of the command line arguments. 218-- Do an initial scan and tokenise of the command line arguments.
210scanArguments = function (args) 219scanArguments = function (args)
211 if args then 220 if args then
@@ -258,6 +267,21 @@ parseType = function (module, thingy, v, value)
258 end 267 end
259 end 268 end
260 269
270 if 'function' == thingy.types[1] then
271 local args = {}
272 -- TODO - Should allow more than one argument, but would need to pass in ARGS and i.
273 if 2 == #thingy.types then
274 if value then
275 -- TODO - Should check the type of the arguments.
276 args[#args + 1] = value[2]
277 module[v[2] ](args[1])
278 value[2] = nil -- Mark it as used.
279 else
280 print('ERROR - Expected an argument for ' .. thingy.names[1])
281 end
282 else
283 module[v[2] ]()
284 end
261 if 'boolean' == thingy.types[1] then 285 if 'boolean' == thingy.types[1] then
262 if value then 286 if value then
263 -- Only parse the next value as a boolean if it doesn't have an introducer. 287 -- 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)
273 end 297 end
274end 298end
275 299
276-- Restore the environment, and grab paramateres from standard places. 300pullArguments = function (module)
277moduleEnd = function (module)
278 -- See if there is a properties file, and run it in the modules environment.
279 local properties = loadfile(module._NAME .. '.properties')
280 if properties then
281 setfenv(properties, getfenv(2))
282 properties()
283 end
284
285 -- Look for our command line arguments. 301 -- Look for our command line arguments.
286 local metaMum = getmetatable(module) 302 local metaMum = getmetatable(module)
287 if metaMum and metaMum.__self then 303 if metaMum and metaMum.__self then
@@ -318,7 +334,18 @@ moduleEnd = function (module)
318 end 334 end
319 end 335 end
320 end 336 end
337end
321 338
339-- Restore the environment, and grab paramateres from standard places.
340moduleEnd = function (module)
341 -- See if there is a properties file, and run it in the modules environment.
342 local properties = loadfile(module._NAME .. '.properties')
343 if properties then
344 setfenv(properties, getfenv(2))
345 properties()
346 end
347
348 pullArguments(module)
322 if module.isLua then setfenv(2, module.savedEnvironment) end 349 if module.isLua then setfenv(2, module.savedEnvironment) end
323end 350end
324 351
@@ -529,14 +556,6 @@ TODO -
529 Maybe try looking in the skang table for Things that are not found? 556 Maybe try looking in the skang table for Things that are not found?
530 Maybe put Things in the skang table that are unique from modules? 557 Maybe put Things in the skang table that are unique from modules?
531 I think this is what matrix-RAD Collisions was all about. 558 I think this is what matrix-RAD Collisions was all about.
532 Skang command line should have standardish stuff, like --version, --help, --help module.thing.
533 Lua does these already, might be no need to do them ourselves -
534 -e 'some code'.
535 -i go interactive after running the script.
536 -v version.
537 - read from stdin non interactively.
538 LuaJIT has this -
539 -- stop processing options.
540]] 559]]
541 560
542-- There is no ThingSpace, or Stuff, now it's all just in this meta table. 561-- There is no ThingSpace, or Stuff, now it's all just in this meta table.
@@ -949,6 +968,7 @@ end
949 968
950module = function (name) 969module = function (name)
951end 970end
971
952skang = function (name) 972skang = function (name)
953end 973end
954quit = function () 974quit = function ()