From da3eaa121de15aefab169cc9b0be5f9012f0e220 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vin=C3=ADcius=20R=2E=20Miguel?= Date: Thu, 4 Jun 2020 01:31:05 -0300 Subject: [PATCH] Piped commands work now --- Sources/input.cpp | 2 +- Sources/opsys.cpp | 30 +++++++++++++++++------------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/Sources/input.cpp b/Sources/input.cpp index 17a91c6..dfff7c6 100644 --- a/Sources/input.cpp +++ b/Sources/input.cpp @@ -91,7 +91,7 @@ short int Prompt::run() if(pipe_count) { // Checks if there's any pipe in the given command - OS.piped_command(tokens, pipe_count); + return OS.piped_command(tokens, pipe_count); } if(!tokens[0].compare("cd")) { diff --git a/Sources/opsys.cpp b/Sources/opsys.cpp index 5ef2f43..37f9ed2 100644 --- a/Sources/opsys.cpp +++ b/Sources/opsys.cpp @@ -82,17 +82,20 @@ vector make_argv(vectorconst& in) short OpSys::piped_command(vector tokens, int pipe_count) { - cerr << "I'm in piped_command\n"; + if(is_verbose) + { + cerr << "Running piped_command with "; + cerr << "tokens: ["; + for(unsigned short int k = 0; k < tokens.size()-1; k++) + cerr << "\"" << tokens[k] << "\", "; + cerr << "\"" << tokens.back() << "\"]\n"; + } + int n_commands = pipe_count + 1; int file_descriptor_array[10][2]; unsigned short i, j=0; string pipe_str = "|"; - cerr << "tokens: ["; - for(unsigned short int k = 0; k < tokens.size()-1; k++) - cerr << "\"" << tokens[k] << "\", "; - cerr << "\"" << tokens.back() << "\"]\n"; - for(i=0; i < n_commands; i++) { if (n_commands > 10) @@ -106,16 +109,11 @@ short OpSys::piped_command(vector tokens, int pipe_count) if (!tokens[j].compare(pipe_str)) { j++; - cerr << "BREAKING\n"; break; } else { aux_cmd.push_back(tokens[j++]); - cerr << "aux_cmd: ["; - for(unsigned short int k = 0; k < aux_cmd.size()-1; k++) - cerr << "\"" << aux_cmd[k] << "\", "; - cerr << "\"" << aux_cmd.back() << "\"]\n"; } } @@ -144,12 +142,18 @@ short OpSys::piped_command(vector tokens, int pipe_count) close(file_descriptor_array[i-1][WRITE_END]); close(file_descriptor_array[i-1][READ_END]); } + if(is_verbose) + { + cerr << "piped_command is going to execvp aux_cmd: ["; + for(unsigned short int k = 0; k < aux_cmd.size()-1; k++) + cerr << "\"" << aux_cmd[k] << "\", "; + cerr << "\"" << aux_cmd.back() << "\"]\n"; + } execvp(aux_cmd[0].c_str(), const_cast(make_argv(aux_cmd).data())); cerr << aux_cmd[0] << ": Comando não encontrado.\n"; return -1; } - else - { + else { if(i!=0) { close(file_descriptor_array[i - 1][READ_END]); -- 2.11.4.GIT