aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.lush/scripts/example.lua9
-rw-r--r--src/lua_api.c12
-rw-r--r--src/lua_api.h2
-rw-r--r--src/lush.c8
4 files changed, 28 insertions, 3 deletions
diff --git a/.lush/scripts/example.lua b/.lush/scripts/example.lua
index 9cf3c96..ffd04c1 100644
--- a/.lush/scripts/example.lua
+++ b/.lush/scripts/example.lua
@@ -23,6 +23,15 @@ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
print("Welcome to Lunar Shell scripting")
+-- command line args can be passed to lua programs
+-- done like so: example.lua arg1 arg2 ...
+-- args can be read using the global args tables
+if args ~= nil then
+ for i = 1, #args do
+ print(args[i])
+ end
+end
+
-- exec can be used to run any command line prompt
-- this method is much more native than using os.execute and is the recommended function
-- it also returns a boolean on if the command executed successfully
diff --git a/src/lua_api.c b/src/lua_api.c
index ef5bf9a..8314e2a 100644
--- a/src/lua_api.c
+++ b/src/lua_api.c
@@ -33,7 +33,7 @@ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
static bool debug_mode = false;
// -- script execution --
-void lua_load_script(lua_State *L, const char *script) {
+void lua_load_script(lua_State *L, const char *script, char **args) {
char script_path[512];
// check if script is in the current directory
if (access(script, F_OK) == 0) {
@@ -55,6 +55,16 @@ void lua_load_script(lua_State *L, const char *script) {
return;
}
}
+ // add args global if args were passed
+ if (args[0] != NULL) {
+ lua_newtable(L);
+ for (int i = 0; args[i]; i++) {
+ lua_pushstring(L, args[i]);
+ // i + 1 since Lua is 1 based indexed
+ lua_rawseti(L, -2, i + 1);
+ }
+ lua_setglobal(L, "args");
+ }
// if we got here the file exists
if (luaL_dofile(L, script_path) != LUA_OK) {
printf("[C] Error reading script\n");
diff --git a/src/lua_api.h b/src/lua_api.h
index 915b4ab..c9f61ab 100644
--- a/src/lua_api.h
+++ b/src/lua_api.h
@@ -20,7 +20,7 @@ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#include <lua.h>
-void lua_load_script(lua_State *L, const char *script);
+void lua_load_script(lua_State *L, const char *script, char **args);
void lua_register_api(lua_State *L);
#endif
diff --git a/src/lush.c b/src/lush.c
index 69aa055..a3fbb93 100644
--- a/src/lush.c
+++ b/src/lush.c
@@ -127,8 +127,14 @@ int lush_time(lua_State *L, char ***args) {
int lush_lua(lua_State *L, char ***args) {
// run the lua file given
- lua_load_script(L, args[0][0]);
+ const char *script = args[0][0];
+ // move args forward to any command line args
+ args[0]++;
+
+ lua_load_script(L, script, args[0]);
+ // return pointer back to lua file
+ args[0]--;
return 1;
}