From b8501210aefeff45b2eb75429c6e3ca2c1ecc983 Mon Sep 17 00:00:00 2001 From: BanceDev Date: Tue, 17 Sep 2024 20:44:04 -0400 Subject: changed tokenizer to handle all the chaining operators --- src/lush.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/lush.h') diff --git a/src/lush.h b/src/lush.h index 15d4d71..6836be2 100644 --- a/src/lush.h +++ b/src/lush.h @@ -35,7 +35,7 @@ int lush_num_builtins(); int lush_run(lua_State *L, char ***commands, int num_commands); char *lush_read_line(); -char **lush_split_pipes(char *line); +char **lush_split_commands(char *line); char ***lush_split_args(char **commands, int *status); void lush_execute_command(char **args, int input_fd, int output_fd); -- cgit v1.2.3-59-g8ed1b From b7718ede99f3a4817fdea01d32b3bbb4a713eb8e Mon Sep 17 00:00:00 2001 From: BanceDev Date: Wed, 18 Sep 2024 12:17:27 -0400 Subject: added basic && chaining --- src/lush.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ src/lush.h | 3 ++- 2 files changed, 73 insertions(+), 8 deletions(-) (limited to 'src/lush.h') diff --git a/src/lush.c b/src/lush.c index f1bb28b..2813330 100644 --- a/src/lush.c +++ b/src/lush.c @@ -44,6 +44,18 @@ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. #define BUFFER_SIZE 1024 +typedef enum { + OP_PIPE = 1, // | + OP_AND, // && + OP_OR, // || + OP_SEMICOLON, // ; + OP_BACKGROUND, // & + OP_REDIRECT_OUT, // > + OP_APPEND_OUT, // >> + OP_REDIRECT_IN, // < + OP_OTHER // All other operators like parentheses, braces, etc. +} OperatorType; + // initialize prompt format char *prompt_format = NULL; @@ -940,14 +952,31 @@ static int is_operator(const char *str) { const char *operators[] = {"||", "&&", "&", ";", ">>", ">", "<", "\\", "(", ")", "{", "}", "!", "|"}; int num_operators = sizeof(operators) / sizeof(operators[0]); - for (int i = 0; i < num_operators; i++) { - int len = strlen(operators[i]); - if (strncmp(str, operators[i], len) == 0) { - return len; + if (strncmp(str, operators[i], strlen(operators[i])) == 0) { + switch (i) { + case 0: + return OP_OR; + case 1: + return OP_AND; + case 2: + return OP_BACKGROUND; + case 3: + return OP_SEMICOLON; + case 4: + return OP_APPEND_OUT; + case 5: + return OP_REDIRECT_OUT; + case 6: + return OP_REDIRECT_IN; + case 13: + return OP_PIPE; + default: + return OP_OTHER; // Parentheses, braces, etc. + } } } - return 0; + return 0; // Not an operator } static char *trim_whitespace(char *str) { @@ -1098,6 +1127,33 @@ char ***lush_split_args(char **commands, int *status) { return command_args; } +int lush_execute_chain(char ***commands, int num_commands) { + if (commands[0][0][0] == '\0') { + return 1; + } + + int num_actions = (num_commands + 1) / 2; + + int last_result = 0; + for (int i = 0; i < num_actions; i++) { + // Determine the operator type between commands + if (i < num_actions - 1) { + int op_type = is_operator(commands[2 * i + 1][0]); + if (op_type == OP_AND && last_result != 0) { + continue; + } + } + + // Execute the current command if it's not an operator + if (!is_operator(commands[2 * i][0])) { + last_result = lush_execute_command(commands[2 * i], STDIN_FILENO, + STDOUT_FILENO); + } + } + + return 1; +} + int lush_execute_pipeline(char ***commands, int num_commands) { // no command given if (commands[0][0][0] == '\0') { @@ -1140,7 +1196,7 @@ int lush_execute_pipeline(char ***commands, int num_commands) { return 1; } -void lush_execute_command(char **args, int input_fd, int output_fd) { +int lush_execute_command(char **args, int input_fd, int output_fd) { // create child pid_t pid; int status; @@ -1152,6 +1208,8 @@ void lush_execute_command(char **args, int input_fd, int output_fd) { // restore default sigint for child sa.sa_handler = SIG_DFL; + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; sigaction(SIGINT, &sa, NULL); // redirect in and out fd's if needed @@ -1180,6 +1238,12 @@ void lush_execute_command(char **args, int input_fd, int output_fd) { waitpid(pid, &status, WUNTRACED); } while (!WIFEXITED(status) && !WIFSIGNALED(status)); } + + if (WIFEXITED(status)) { + return WEXITSTATUS(status); + } else { + return -1; + } } int lush_run(lua_State *L, char ***commands, int num_commands) { @@ -1203,7 +1267,7 @@ int lush_run(lua_State *L, char ***commands, int num_commands) { return ((*builtin_func[i])(L, commands)); } } - return lush_execute_pipeline(commands, num_commands); + return lush_execute_chain(commands, num_commands); } int main(int argc, char *argv[]) { diff --git a/src/lush.h b/src/lush.h index 6836be2..564dd4b 100644 --- a/src/lush.h +++ b/src/lush.h @@ -38,8 +38,9 @@ char *lush_read_line(); char **lush_split_commands(char *line); char ***lush_split_args(char **commands, int *status); -void lush_execute_command(char **args, int input_fd, int output_fd); +int lush_execute_command(char **args, int input_fd, int output_fd); int lush_execute_pipeline(char ***commands, int num_commands); +int lush_execute_chain(char ***commands, int num_commands); void lush_format_prompt(const char *prompt_format); -- cgit v1.2.3-59-g8ed1b From ecfbc1f47ff85b71365f2f25bf07291037a5dcc6 Mon Sep 17 00:00:00 2001 From: BanceDev Date: Wed, 18 Sep 2024 14:57:58 -0400 Subject: fixed && chaining for builtins --- src/lush.c | 52 ++++++++++++++++++++++++++++++---------------------- src/lush.h | 2 +- 2 files changed, 31 insertions(+), 23 deletions(-) (limited to 'src/lush.h') diff --git a/src/lush.c b/src/lush.c index 2813330..9b43324 100644 --- a/src/lush.c +++ b/src/lush.c @@ -139,7 +139,7 @@ int lush_help(lua_State *L, char ***args) { return 1; } -int lush_exit(lua_State *L, char ***args) { return 0; } +int lush_exit(lua_State *L, char ***args) { exit(0); } int lush_time(lua_State *L, char ***args) { // advance past time command @@ -1127,7 +1127,27 @@ char ***lush_split_args(char **commands, int *status) { return command_args; } -int lush_execute_chain(char ***commands, int num_commands) { +static int run_command(lua_State *L, char ***commands) { + // check if the command is a lua script + char *ext = strrchr(commands[0][0], '.'); + if (ext) { + ext++; + if (strcmp(ext, "lua") == 0) { + return ((*builtin_func[4])(L, commands)); + } + } + + // check shell builtins + for (int j = 0; j < lush_num_builtins(); j++) { + if (strcmp(commands[0][0], builtin_strs[j]) == 0) { + return ((*builtin_func[j])(L, commands)); + } + } + + return lush_execute_command(commands[0], STDIN_FILENO, STDOUT_FILENO); +} + +int lush_execute_chain(lua_State *L, char ***commands, int num_commands) { if (commands[0][0][0] == '\0') { return 1; } @@ -1138,19 +1158,22 @@ int lush_execute_chain(char ***commands, int num_commands) { for (int i = 0; i < num_actions; i++) { // Determine the operator type between commands if (i < num_actions - 1) { - int op_type = is_operator(commands[2 * i + 1][0]); + int op_type = is_operator(commands[1][0]); if (op_type == OP_AND && last_result != 0) { continue; } } // Execute the current command if it's not an operator - if (!is_operator(commands[2 * i][0])) { - last_result = lush_execute_command(commands[2 * i], STDIN_FILENO, - STDOUT_FILENO); + if (!is_operator(commands[0][0])) { + last_result = run_command(L, commands); + commands += 2; } } + commands -= num_actions * 2; + printf("sanity check, commands[0]: %s", commands[0][0]); + return 1; } @@ -1252,22 +1275,7 @@ int lush_run(lua_State *L, char ***commands, int num_commands) { return 1; } - // check if the command is a lua script - char *ext = strrchr(commands[0][0], '.'); - if (ext) { - ext++; - if (strcmp(ext, "lua") == 0) { - return ((*builtin_func[4])(L, commands)); - } - } - - // check shell builtins - for (int i = 0; i < lush_num_builtins(); i++) { - if (strcmp(commands[0][0], builtin_strs[i]) == 0) { - return ((*builtin_func[i])(L, commands)); - } - } - return lush_execute_chain(commands, num_commands); + return lush_execute_chain(L, commands, num_commands); } int main(int argc, char *argv[]) { diff --git a/src/lush.h b/src/lush.h index 564dd4b..291a8dd 100644 --- a/src/lush.h +++ b/src/lush.h @@ -40,7 +40,7 @@ char ***lush_split_args(char **commands, int *status); int lush_execute_command(char **args, int input_fd, int output_fd); int lush_execute_pipeline(char ***commands, int num_commands); -int lush_execute_chain(char ***commands, int num_commands); +int lush_execute_chain(lua_State *L, char ***commands, int num_commands); void lush_format_prompt(const char *prompt_format); -- cgit v1.2.3-59-g8ed1b