From 4375a8eea7b7fdd8405c4c9a749e03b858e9ea2d Mon Sep 17 00:00:00 2001 From: BanceDev Date: Mon, 2 Sep 2024 23:10:48 -0400 Subject: searches .lush/scripts for lua files --- src/lua_api.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'src') 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 #include #include +#include // -- 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); } } -- cgit v1.2.3-59-g8ed1b