diff options
| author | 2024-09-03 10:51:42 -0400 | |
|---|---|---|
| committer | 2024-09-03 10:51:42 -0400 | |
| commit | d5d3f94aa5eacab4a8d6a1a2beb244798330ec6a (patch) | |
| tree | cab0007065c7b3494ba9052d49ced85b1986dd26 | |
| parent | fixed crash in cd when no path found (diff) | |
update lua api to table and added getcwd to api
Diffstat (limited to '')
| -rw-r--r-- | .lush/scripts/example.lua | 21 | ||||
| -rw-r--r-- | src/lua_api.c | 27 | ||||
| -rw-r--r-- | src/lush.c | 6 |
3 files changed, 46 insertions, 8 deletions
diff --git a/.lush/scripts/example.lua b/.lush/scripts/example.lua index 0357276..279ad5e 100644 --- a/.lush/scripts/example.lua +++ b/.lush/scripts/example.lua @@ -15,10 +15,27 @@ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ]] +-- This file is a demo of all of the commands Lunar Shell has in its Lua API -- the Lua file itself is the executable unit so all code is run line by line --- the print function can be used to write to the console +-- All Lua specific tools still work here too. Functions, loops, conditionals, +-- tables, Lua's standard library, etc are all available to integrate with Lunar Shell + print("Welcome to Lunar Shell scripting") -- exec can be used to run any command line prompt -exec('echo "hello world"\n') +-- 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 +if lush.exec('echo "hello world"\n') then + print("echo worked properly") +end + +-- getcwd returns the current working directory +local cwd = lush.getcwd() +print(cwd) + +-- exec also supports piping +-- this comment will get grepped since it says hello +lush.exec("cd " .. os.getenv("HOME") .. "/.lush/scripts") +lush.exec('cat "example.lua" | grep "hello" | sort | uniq') +lush.exec("cd " .. cwd) diff --git a/src/lua_api.c b/src/lua_api.c index 5a92b58..554c0c9 100644 --- a/src/lua_api.c +++ b/src/lua_api.c @@ -20,6 +20,7 @@ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. #include <lauxlib.h> #include <lua.h> #include <lualib.h> +#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> @@ -57,7 +58,7 @@ void lua_load_script(lua_State *L, const char *script) { } // -- C funtions -- -static void execute_command(lua_State *L, const char *line) { +static int execute_command(lua_State *L, const char *line) { int status = 0; char **commands = lush_split_pipes((char *)line); char ***args = lush_split_args(commands, &status); @@ -72,17 +73,35 @@ static void execute_command(lua_State *L, const char *line) { } free(args); free(commands); + return status; } // -- Lua wrappers -- static int l_execute_command(lua_State *L) { const char *command = luaL_checkstring(L, 1); - execute_command(L, command); - return 0; + int status = execute_command(L, command); + bool rc = status == 0 ? true : false; + lua_pushboolean(L, rc); + return 1; +} + +static int l_get_cwd(lua_State *L) { + char *cwd = getcwd(NULL, 0); + lua_pushstring(L, cwd); + free(cwd); + return 1; } // -- register Lua functions -- void lua_register_api(lua_State *L) { - lua_register(L, "exec", l_execute_command); + // global table for api functions + lua_newtable(L); + + lua_pushcfunction(L, l_execute_command); + lua_setfield(L, -2, "exec"); + lua_pushcfunction(L, l_get_cwd); + lua_setfield(L, -2, "getcwd"); + // set the table as global + lua_setglobal(L, "lush"); } @@ -123,9 +123,11 @@ int lush_time(lua_State *L, char ***args) { } int lush_lush(lua_State *L, char ***args) { - // run the lua file given + // move past lush command args[0]++; - lua_load_script(L, *args[0]); + + // run the lua file given + lua_load_script(L, args[0][0]); // return pointer back for free() args[0]--; |
