From 1e20276520eb70051fc4bf29f0bbd196b66cca53 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vin=C3=ADcius=20R=2E=20Miguel?= Date: Thu, 4 Jun 2020 01:17:10 -0300 Subject: [PATCH] Piped commands (almost) working --- Headers/input.h | 4 +++ Headers/opsys.h | 6 +++- Sources/input.cpp | 20 ++++++++++--- Sources/main.cpp | 1 + Sources/opsys.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 109 insertions(+), 7 deletions(-) diff --git a/Headers/input.h b/Headers/input.h index 2f7c5e8..1d117e1 100644 --- a/Headers/input.h +++ b/Headers/input.h @@ -10,6 +10,10 @@ using std::vector; #define RED_ANSI "\x1b[31m" // ANSI escape code for red #define BLUE_ANSI "\x1b[34m" // ANSI escape code for blue #define RESET_ANSI "\x1b[0m" // ANSI escape code to reset stdout's color +#define KEY_UP 72 // ASCII code for arrow key up +#define KEY_LEFT 75 // ASCII code for arrow key left +#define KEY_RIGHT 77 // ASCII code for arrow key right +#define KEY_DOWN 80 // ASCII code for arrow key down extern bool is_verbose; extern bool cwd_changed; diff --git a/Headers/opsys.h b/Headers/opsys.h index 24ecfd8..94c49ff 100644 --- a/Headers/opsys.h +++ b/Headers/opsys.h @@ -6,6 +6,9 @@ #include // std::cin, std::cout, printf, fprintf extern bool exit_program; +#define WRITE_END 1 +#define READ_END 0 + /*! * \class SignalHandler * \brief Handles SIGHUP and SIGINT signals. @@ -29,7 +32,8 @@ public: string cwd; // Current working directory OpSys(); void change_dir(vector); - short int simple_command(vector); + short simple_command(vector); + short piped_command (vector, int); }; extern OpSys OS; diff --git a/Sources/input.cpp b/Sources/input.cpp index 9ede9b6..17a91c6 100644 --- a/Sources/input.cpp +++ b/Sources/input.cpp @@ -1,5 +1,6 @@ #include "Headers/opsys.h" #include // std::regex and std::regex_replace +#include bool is_verbose = false; bool cwd_changed = false; @@ -49,7 +50,12 @@ void Prompt::get_last_dir() Prompt::Prompt() { - get_last_dir(); +// struct termios t; +// tcgetattr(STDIN_FILENO, &t); +// t.c_lflag &= ~ICANON; +// tcsetattr(STDIN_FILENO, TCSANOW, &t); // WIP for arrow keys support + + get_last_dir(); // Process cwd for exibition on prompt } void Prompt::print() @@ -80,6 +86,13 @@ void Prompt::parse(string input) short int Prompt::run() { + string pipe = "|"; + int pipe_count = std::count(tokens.begin(), tokens.end(), pipe); + + if(pipe_count) + { // Checks if there's any pipe in the given command + OS.piped_command(tokens, pipe_count); + } if(!tokens[0].compare("cd")) { OS.change_dir(tokens); @@ -94,13 +107,12 @@ short int Prompt::run() return 1; } else if (!tokens[0].compare("quit")) { - cout << "Exiting" << std::endl; + std::cerr << "Exiting.\n"; exit_program = true; return 1; } else { - OS.simple_command(tokens); - return 1; + return OS.simple_command(tokens); } } diff --git a/Sources/main.cpp b/Sources/main.cpp index a11c06d..ff920cb 100644 --- a/Sources/main.cpp +++ b/Sources/main.cpp @@ -15,6 +15,7 @@ int main(int argc, char ** argv) string line; prompt.print(); std::getline(cin, line); + if (exit_program) return 0; // Tests for "quit" if (cin.eof()) { diff --git a/Sources/opsys.cpp b/Sources/opsys.cpp index 71651b9..5ef2f43 100644 --- a/Sources/opsys.cpp +++ b/Sources/opsys.cpp @@ -80,6 +80,88 @@ vector make_argv(vectorconst& in) return out; } +short OpSys::piped_command(vector tokens, int pipe_count) +{ + cerr << "I'm in piped_command\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) + { + cerr << "More pipes than supported.\n"; + return -1; + } + vector aux_cmd; + for(;j(make_argv(aux_cmd).data())); + cerr << aux_cmd[0] << ": Comando não encontrado.\n"; + return -1; + } + else + { + if(i!=0) + { + close(file_descriptor_array[i - 1][READ_END]); + close(file_descriptor_array[i - 1][WRITE_END]); + } + } + } + for(i=0; i tokens) { int status; @@ -92,13 +174,12 @@ short OpSys::simple_command(vector tokens) cerr << "\");\n"; } - pid_t pid = fork(); if (pid == 0) { execvp(tokens[0].c_str(), const_cast(make_argv(tokens).data())); cerr << tokens[0] << ": command not found.\n"; - return 0; // TODO: exit with cleanup + return -1; // TODO: exit with cleanup } else if (pid < 0) { -- 2.11.4.GIT