aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar BanceDev 2024-09-03 11:11:17 -0400
committerGravatar BanceDev 2024-09-03 11:11:17 -0400
commit3577398f4a319f0419f2c129ba09b8f14f3f0210 (patch)
tree239f79201aa536ee1d1373c4a0dbd7e24aff8020
parentupdate lua api to table and added getcwd to api (diff)
added debug mode for scripting
-rw-r--r--.lush/scripts/example.lua5
-rw-r--r--src/lua_api.c22
2 files changed, 26 insertions, 1 deletions
diff --git a/.lush/scripts/example.lua b/.lush/scripts/example.lua
index 279ad5e..b2f2f2b 100644
--- a/.lush/scripts/example.lua
+++ b/.lush/scripts/example.lua
@@ -30,6 +30,11 @@ if lush.exec('echo "hello world"\n') then
print("echo worked properly")
end
+-- debug mode can be used to log execution of commands
+lush.debug(true) -- enters debug
+lush.exec('echo "echo in debug mode"')
+lush.debug(false) -- exits debug
+
-- getcwd returns the current working directory
local cwd = lush.getcwd()
print(cwd)
diff --git a/src/lua_api.c b/src/lua_api.c
index 554c0c9..4b5de86 100644
--- a/src/lua_api.c
+++ b/src/lua_api.c
@@ -25,6 +25,9 @@ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#include <stdlib.h>
#include <unistd.h>
+// global for checking if debug_mode is toggled
+static bool debug_mode = false;
+
// -- script execution --
void lua_load_script(lua_State *L, const char *script) {
char script_path[512];
@@ -80,7 +83,15 @@ static int execute_command(lua_State *L, const char *line) {
static int l_execute_command(lua_State *L) {
const char *command = luaL_checkstring(L, 1);
int status = execute_command(L, command);
- bool rc = status == 0 ? true : false;
+ bool rc = status != -1 ? true : false;
+
+ if (debug_mode) {
+ if (rc)
+ printf("Executed: %s, success\n", command);
+ else
+ printf("Executed: %s, failed\n", command);
+ }
+
lua_pushboolean(L, rc);
return 1;
}
@@ -92,6 +103,13 @@ static int l_get_cwd(lua_State *L) {
return 1;
}
+static int l_debug(lua_State *L) {
+ if (lua_isboolean(L, 1)) {
+ debug_mode = lua_toboolean(L, 1);
+ }
+ return 0;
+}
+
// -- register Lua functions --
void lua_register_api(lua_State *L) {
@@ -102,6 +120,8 @@ void lua_register_api(lua_State *L) {
lua_setfield(L, -2, "exec");
lua_pushcfunction(L, l_get_cwd);
lua_setfield(L, -2, "getcwd");
+ lua_pushcfunction(L, l_debug);
+ lua_setfield(L, -2, "debug");
// set the table as global
lua_setglobal(L, "lush");
}