From 17aadc35445c08572edcc9df826ade5d74cb4e17 Mon Sep 17 00:00:00 2001 From: BanceDev Date: Thu, 13 Feb 2025 11:56:52 -0500 Subject: added ability to configure alternate shell --- src/lua_api.c | 16 ++++++++++++++++ src/lush.c | 28 ++++++++++++++++++++++++++-- src/lush.h | 2 +- 3 files changed, 43 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/lua_api.c b/src/lua_api.c index ae5decf..a35a308 100644 --- a/src/lua_api.c +++ b/src/lua_api.c @@ -33,6 +33,7 @@ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. // globals static bool debug_mode = false; bool suggestion_enable = true; +char *alt_shell; // -- script execution -- int lua_load_script(lua_State *L, const char *script, char **args) { @@ -422,6 +423,19 @@ static int l_exit(lua_State *L) { return 0; } +static int l_alt_shell(lua_State *L) { + const char *alt = luaL_checkstring(L, 1); + + free(alt_shell); + alt_shell = strdup(alt); + + if (!alt_shell) { + return luaL_error(L, "Memory allocation failed"); + } + + return 1; +} + // -- register Lua functions -- void lua_register_api(lua_State *L) { @@ -470,6 +484,8 @@ void lua_register_api(lua_State *L) { lua_setfield(L, -2, "glob"); lua_pushcfunction(L, l_exit); lua_setfield(L, -2, "exit"); + lua_pushcfunction(L, l_alt_shell); + lua_setfield(L, -2, "altShell"); // set the table as global lua_setglobal(L, "lush"); } diff --git a/src/lush.c b/src/lush.c index f815fa5..1c42139 100644 --- a/src/lush.c +++ b/src/lush.c @@ -1390,6 +1390,21 @@ int lush_execute_pipeline(char ***commands, int num_commands) { return 0; } +static void build_alt_command(char *buffer, char **args) { + size_t offset = 0; + + for (int i = 0; args[i]; i++) { + if (offset + strlen(args[i]) + 1 < BUFFER_SIZE) { + strcat(buffer, args[i]); + strcat(buffer, " "); + offset = strlen(buffer); + } else { + fprintf(stderr, "command too long\n"); + exit(EXIT_FAILURE); + } + } +} + int lush_execute_command(char **args, int input_fd, int output_fd) { // create child pid_t pid; @@ -1419,8 +1434,15 @@ int lush_execute_command(char **args, int input_fd, int output_fd) { // execute the command if (execvp(args[0], args) == -1) { - perror("execvp"); - exit(EXIT_FAILURE); + if (alt_shell) { + char command[BUFFER_SIZE] = {0}; + build_alt_command(command, args); + execlp(alt_shell, alt_shell, "-c", command, (char *)NULL); + perror("alt shell"); + } else { + perror("lush"); + exit(EXIT_FAILURE); + } } } else if (pid < 0) { // forking failed @@ -1592,5 +1614,7 @@ int main(int argc, char *argv[]) { free(prompt_format); if (aliases != NULL) free(aliases); + if (alt_shell != NULL) + free(alt_shell); return 0; } diff --git a/src/lush.h b/src/lush.h index 9ca9722..139f638 100644 --- a/src/lush.h +++ b/src/lush.h @@ -55,9 +55,9 @@ int lush_execute_chain(lua_State *L, char ***commands, int num_commands); void lush_format_prompt(const char *prompt_format); -// enable for autocomplete text set in init.lua // initialized in the lua_api extern bool suggestion_enable; +extern char *alt_shell; // format spec for the prompt extern char *prompt_format; -- cgit v1.2.3-59-g8ed1b