diff options
| author | 2024-09-02 23:10:48 -0400 | |
|---|---|---|
| committer | 2024-09-02 23:10:48 -0400 | |
| commit | 4375a8eea7b7fdd8405c4c9a749e03b858e9ea2d (patch) | |
| tree | 72b0e89a5551c47724ff28005ae5336dda454cd7 | |
| parent | added basic lua scripting (diff) | |
searches .lush/scripts for lua files
| -rw-r--r-- | README.md | 6 | ||||
| -rw-r--r-- | src/lua_api.c | 27 |
2 files changed, 27 insertions, 6 deletions
@@ -8,7 +8,7 @@ Lunar Shell (lush) is an open source unix shell with a single goal in mind. That goal is to offer the ability to write shell scripts for your operating system entirely in Lua. The Lua scripting language has many powerful features that allow for more control in the hands of the user to automate tasks on their machine. -## Compiling +## Compiling/Installation Clone the repo and run the install script. @@ -20,10 +20,6 @@ sh install.sh For future compiles just run ```make``` from the root directory. If you change the premake5.lua file rebuild the makefiles with ```premake5 gmake```. -## Releases - -Binary builds are available from [releases](https://github.com/BanceDev/lush/releases). - ## Contributing - For bug reports and feature suggestions please use [issues](https://github.com/BanceDev/lush/issues). 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); } } |
