From a8e6427210ea07eb1ed67f23152bb4713377832a Mon Sep 17 00:00:00 2001 From: MikeCAT Date: Wed, 3 Aug 2022 22:35:43 +0000 Subject: [PATCH] terminal: improve how to deal with special characters * invalidate first_arg if it is beyond redir_char or pipe_char * improve behavior of "cat> z.txt", for example * improve behavior when both of redir_char and pipe_char is used * don't open output file (redirect) if the output is pipe * throw error if pipe is after redirect --- kernel/terminal.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/kernel/terminal.cpp b/kernel/terminal.cpp index 600d4a1f2..dfddd015f 100644 --- a/kernel/terminal.cpp +++ b/kernel/terminal.cpp @@ -366,6 +366,18 @@ void Terminal::ExecuteLine() { char* first_arg = strchr(&linebuf_[0], ' '); char* redir_char = strchr(&linebuf_[0], '>'); char* pipe_char = strchr(&linebuf_[0], '|'); + if (first_arg) { + if (redir_char && redir_char < first_arg) first_arg = 0; + else if (pipe_char && pipe_char < first_arg) first_arg = 0; + } + if (redir_char && pipe_char) { + if (pipe_char < redir_char) { + redir_char = 0; + } else { + PrintToFD(*files_[2], "pipe after redirect is not supported\n"); + return; + } + } if (first_arg) { *first_arg = 0; do {