From dd43119332c64c7a7f63c1a33b61d4dcd9f9fb4d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vin=C3=ADcius=20R=2E=20Miguel?= Date: Thu, 4 Jun 2020 01:58:47 -0300 Subject: [PATCH] Add history --- Headers/opsys.h | 5 ++++- Sources/input.cpp | 13 +++++++++++-- Sources/opsys.cpp | 25 +++++++++++++++++++++---- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/Headers/opsys.h b/Headers/opsys.h index 94c49ff..0464b39 100644 --- a/Headers/opsys.h +++ b/Headers/opsys.h @@ -26,14 +26,17 @@ public: class OpSys { string get_cwd(); + vector> history; public: string username; // Username string hostname; // User's hostname string cwd; // Current working directory OpSys(); - void change_dir(vector); + void change_dir(vector); + void show_history(); short simple_command(vector); short piped_command (vector, int); + friend class Prompt; }; extern OpSys OS; diff --git a/Sources/input.cpp b/Sources/input.cpp index dfff7c6..f182baa 100644 --- a/Sources/input.cpp +++ b/Sources/input.cpp @@ -87,10 +87,14 @@ void Prompt::parse(string input) short int Prompt::run() { string pipe = "|"; + + // Counts occurrences of a pipe character in the given command int pipe_count = std::count(tokens.begin(), tokens.end(), pipe); + OS.history.push_back(tokens); // Save the current command on OS.history + if(pipe_count) - { // Checks if there's any pipe in the given command + { // If there were pipes on the command, run OS.piped_command(); return OS.piped_command(tokens, pipe_count); } if(!tokens[0].compare("cd")) @@ -105,7 +109,12 @@ short int Prompt::run() { cout << "Insert help text here.\n"; return 1; - } else if (!tokens[0].compare("quit")) + } else if (!tokens[0].compare("history")) + { + OS.show_history(); + return 1; + } + else if (!tokens[0].compare("quit")) { std::cerr << "Exiting.\n"; exit_program = true; diff --git a/Sources/opsys.cpp b/Sources/opsys.cpp index 37f9ed2..b2319b6 100644 --- a/Sources/opsys.cpp +++ b/Sources/opsys.cpp @@ -99,7 +99,7 @@ short OpSys::piped_command(vector tokens, int pipe_count) for(i=0; i < n_commands; i++) { if (n_commands > 10) - { + { // This is an arbitrary decision. Allowing for more pipes is easily possible. cerr << "More pipes than supported.\n"; return -1; } @@ -107,12 +107,13 @@ short OpSys::piped_command(vector tokens, int pipe_count) for(;j tokens, int pipe_count) return 1; } +void OpSys::show_history() // TODO: save (and read) history on a file? use Bourne Again's history?? +{ + unsigned short i = 0, j; + for(vector command : history) + { + string command_str = ""; + for(j=0; j tokens) { int status; -- 2.11.4.GIT