diff options
| author | 2024-09-02 23:10:48 -0400 | |
|---|---|---|
| committer | 2024-09-02 23:10:48 -0400 | |
| commit | 4375a8eea7b7fdd8405c4c9a749e03b858e9ea2d (patch) | |
| tree | 72b0e89a5551c47724ff28005ae5336dda454cd7 /src | |
| parent | added basic lua scripting (diff) | |
searches .lush/scripts for lua files
Diffstat (limited to '')
| -rw-r--r-- | src/lua_api.c | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/src/lua_api.c b/src/lua_api.c index becd2b7..5a92b58 100644 --- a/src/lua_api.c +++ b/src/lua_api.c @@ -22,12 +22,37 @@ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. #include <lualib.h> #include <stdio.h> #include <stdlib.h> +#include <unistd.h> // -- script execution -- void lua_load_script(lua_State *L, const char *script) { - if (luaL_dofile(L, script) != LUA_OK) { + char script_path[512]; + // check if script is in the current directory + if (access(script, F_OK) == 0) { + snprintf(script_path, sizeof(script_path), "%s", script); + } else { + const char *home_dir = getenv("HOME"); + if (home_dir != NULL) { + snprintf(script_path, sizeof(script_path), "%s/.lush/scripts/%s", + home_dir, script); + + if (access(script_path, F_OK) != 0) { + // script not in either location + fprintf(stderr, "[C] Script not found: %s\n", script); + return; + } + } else { + // HOME not set + fprintf(stderr, "[C] HOME directory is not set.\n"); + return; + } + } + // if we got here the file exists + if (luaL_dofile(L, script_path) != LUA_OK) { printf("[C] Error reading script\n"); luaL_error(L, "Error: %s\n", lua_tostring(L, -1)); + // remove error from stack + lua_pop(L, 1); } } |
