aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar BanceDev 2024-09-18 14:57:58 -0400
committerGravatar BanceDev 2024-09-18 14:57:58 -0400
commitecfbc1f47ff85b71365f2f25bf07291037a5dcc6 (patch)
treec869ebe7ef64c1cb23028a1804ed13d75ad2bb8e
parentadded basic && chaining (diff)
fixed && chaining for builtins
-rw-r--r--src/lush.c52
-rw-r--r--src/lush.h2
2 files changed, 31 insertions, 23 deletions
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);