From cd554877c5f57cdebc919cd889c1812773446a1e Mon Sep 17 00:00:00 2001 From: BanceDev Date: Thu, 26 Sep 2024 23:08:46 -0400 Subject: fixed crash in redirect operator --- src/lush.c | 19 ++++++++-------- test/chaining_test.lua | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ test/pipes_test.lua | 25 -------------------- test/run_tests.lua | 4 ++-- 4 files changed, 73 insertions(+), 37 deletions(-) create mode 100644 test/chaining_test.lua delete mode 100644 test/pipes_test.lua diff --git a/src/lush.c b/src/lush.c index c3bd831..68447b0 100644 --- a/src/lush.c +++ b/src/lush.c @@ -1126,13 +1126,8 @@ int lush_execute_chain(lua_State *L, char ***commands, int num_commands) { int last_result = 0; for (int i = 0; i < num_actions; i++) { - // Handle ; operator - if (is_operator(commands[0][0]) == OP_SEMICOLON) { - commands++; - } - - // Handle && and || operators - if (i > 0) { + // Handle &&, ||, and ; operators + if (i > 0 && commands[0] != NULL) { commands--; if (last_result != 0) { if (is_operator(commands[0][0]) == OP_AND) { @@ -1146,6 +1141,10 @@ int lush_execute_chain(lua_State *L, char ***commands, int num_commands) { } } commands++; + + if (is_operator(commands[0][0]) == OP_SEMICOLON) { + commands++; + } } // Handle other operations @@ -1174,15 +1173,15 @@ int lush_execute_chain(lua_State *L, char ***commands, int num_commands) { commands += 2; continue; } else if (op_type == OP_BACKGROUND) { - run_command_background(L, commands); + last_result = run_command_background(L, commands); commands += 2; continue; } else if (op_type == OP_REDIRECT_OUT) { - run_command_redirect(L, commands, O_TRUNC); + last_result = run_command_redirect(L, commands, O_TRUNC); commands += 3; // to go past fd given continue; } else if (op_type == OP_APPEND_OUT) { - run_command_redirect(L, commands, O_APPEND); + last_result = run_command_redirect(L, commands, O_APPEND); commands += 3; continue; } diff --git a/test/chaining_test.lua b/test/chaining_test.lua new file mode 100644 index 0000000..83f3e76 --- /dev/null +++ b/test/chaining_test.lua @@ -0,0 +1,62 @@ +--[[ +Copyright (c) 2024, Lance Borden +All rights reserved. + +This software is licensed under the BSD 3-Clause License. +You may obtain a copy of the license at: +https://opensource.org/licenses/BSD-3-Clause + +Redistribution and use in source and binary forms, with or without +modification, are permitted under the conditions stated in the BSD 3-Clause +License. + +THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTIES, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +]] + +local cwd = lush.getcwd() +lush.cd("~/.lush/scripts") +if lush.exec('cat "example.lua" | grep "hello" | sort | uniq') then + print("piping test passed ✅\n") +else + print("piping test failed ❌\n") +end + +if lush.exec("echo hi && echo bye") then + print("and test passed ✅\n") +else + print("and test failed ❌\n") +end + +if lush.exec("cd lol || echo lol does not exist") then + print("or test passed ✅\n") +else + print("or test failed ❌\n") +end + +if lush.exec("sleep 2 &") then + print("background test passed ✅\n") +else + print("background test failed ❌\n") +end + +if lush.exec("echo hi; echo bye") then + print("semicolon test passed ✅\n") +else + print("semicolon test failed ❌\n") +end + +if lush.exec("echo hi > test.txt") then + print("redirect test passed ✅\n") +else + print("redirect test failed ❌\n") +end + +if lush.exec("echo hi >> test.txt") then + print("append test passed ✅\n") +else + print("append test failed ❌\n") +end + +lush.cd(cwd) diff --git a/test/pipes_test.lua b/test/pipes_test.lua deleted file mode 100644 index 46000e3..0000000 --- a/test/pipes_test.lua +++ /dev/null @@ -1,25 +0,0 @@ ---[[ -Copyright (c) 2024, Lance Borden -All rights reserved. - -This software is licensed under the BSD 3-Clause License. -You may obtain a copy of the license at: -https://opensource.org/licenses/BSD-3-Clause - -Redistribution and use in source and binary forms, with or without -modification, are permitted under the conditions stated in the BSD 3-Clause -License. - -THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTIES, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE -IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. -]] - -local cwd = lush.getcwd() -lush.cd("~/.lush/scripts") -if lush.exec('cat "example.lua" | grep "hello" | sort | uniq') then - print("piping test passed ✅\n") -else - print("piping test failed ❌\n") -end -lush.cd(cwd) diff --git a/test/run_tests.lua b/test/run_tests.lua index e861326..6c54157 100644 --- a/test/run_tests.lua +++ b/test/run_tests.lua @@ -22,8 +22,8 @@ lush.debug(true) print("Testing Args...") lush.exec("args_test.lua testarg1 testarg2 testarg3") -print("\nTesting Piping...") -lush.exec("pipes_test.lua") +print("\nTesting Chaining...") +lush.exec("chaining_test.lua") print("\nTesting File Checks...") lush.exec("filecheck_test.lua") -- cgit v1.2.3-59-g8ed1b