# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-# Test that the shell and ! commands work.
+# Test that the "shell", "!", "|" and "pipe" commands work.
gdb_exit
gdb_start
gdb_test "! echo foo" "foo"
gdb_test "!echo foo" "foo"
+
+# Convenience variables with shell command.
+gdb_test_no_output "! exit 0"
+gdb_test "p \$_shell_exitcode" " = 0" "shell success exitcode"
+gdb_test "p \$_shell_exitsignal" " = void" "shell success exitsignal"
+
+gdb_test_no_output "! exit 1"
+gdb_test "p \$_shell_exitcode" " = 1" "shell fail exitcode"
+gdb_test "p \$_shell_exitsignal" " = void" "shell fail exitsignal"
+
+gdb_test_no_output "! kill -2 $$"
+gdb_test "p \$_shell_exitcode" " = void" "shell interrupt exitcode"
+gdb_test "p \$_shell_exitsignal" " = 2" "shell interrupt exitsignal"
+
+# Define the user command "foo", used to test "pipe" command.
+gdb_test_multiple "define foo" "define foo" {
+ -re "End with" {
+ pass "define foo"
+ }
+}
+gdb_test \
+ [multi_line_input \
+ { echo coucou\n }\
+ { echo truc\n }\
+ { echo machin\n }\
+ { if $argc > 0 }\
+ { echo $arg0\n}\
+ {end}\
+ {end}] \
+ "" \
+ "enter commands"
+
+
+gdb_test "pipe foo | wc -l" "3" "simple pipe"
+gdb_test "pipe foo brol| wc -l" "4" "simple pipe with arg"
+gdb_test "pipe foo truc2 | grep truc | wc -l" "2" "double pipe"
+
+gdb_test "| foo truc2 | grep truc | wc -l" "2" "double pipe, pipe char"
+gdb_test "|foo|grep truc|wc -l" "1" "no space around pipe char"
+
+gdb_test "echo coucou\\n" "coucou" "echo coucou"
+gdb_test "||wc -l" "1" "repeat previous command"
+
+gdb_test "| -d ! echo this contains a | character\\n ! sed -e 's/|/PIPE/'" \
+ "this contains a PIPE character" "alternate 1char delim"
+
+gdb_test "|-d ! echo this contains a | character\\n!sed -e 's/|/PIPE/'" \
+ "this contains a PIPE character" "alternate 1char delim, no space"
+
+gdb_test "| -d !!! echo this contains a | character\\n !!! sed -e 's/|/PIPE/'" \
+ "this contains a PIPE character" "alternate 3char delim"
+
+gdb_test "|-d !!! echo this contains a | character\\n!!!sed -e 's/|/PIPE/'" \
+ "this contains a PIPE character" "alternate 3char delim, no space"
+
+# Convenience variables with pipe command.
+gdb_test "|p 123| exit 0" ""
+gdb_test "p \$_shell_exitcode" " = 0" "pipe success exitcode"
+gdb_test "p \$_shell_exitsignal" " = void" "pipe success exitsignal"
+
+gdb_test "|p 123| exit 1" ""
+gdb_test "p \$_shell_exitcode" " = 1" "pipe fail exitcode"
+gdb_test "p \$_shell_exitsignal" " = void" "pipe fail exitsignal"
+
+gdb_test "|p 123| kill -2 $$" ""
+gdb_test "p \$_shell_exitcode" " = void" "pipe interrupt exitcode"
+gdb_test "p \$_shell_exitsignal" " = 2" "pipe interrupt exitsignal"
+
+# Error handling verifications.
+gdb_test "|" "Missing COMMAND" "all missing"
+gdb_test "|-d" "Missing delimiter DELIM after -d" "-d value missing"
+gdb_test "|-d " "Missing delimiter DELIM after -d" "-d spaces value missing"
+gdb_test "| echo coucou" \
+ "Missing delimiter before SHELL_COMMAND" \
+ "| delimiter missing"
+gdb_test "|-d DELIM echo coucou" \
+ "Missing delimiter before SHELL_COMMAND" \
+ "DELIM delimiter missing"
+gdb_test "|echo coucou|" \
+ "Missing SHELL_COMMAND" \
+ "SHELL_COMMAND missing"
+gdb_test "|-d ! echo coucou !" \
+ "Missing SHELL_COMMAND" \
+ "SHELL_COMMAND missing with delimiter"
+gdb_test "|-d! echo coucou ! wc" \
+ "Missing delimiter before SHELL_COMMAND" \
+ "delimiter missing due to missing space"
+