From 9f56ed01ff81c014c6379990a5879ba0072ad355 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vin=C3=ADcius=20R=2E=20Miguel?= Date: Wed, 3 Jun 2020 18:33:59 -0300 Subject: [PATCH] quit command and verbosity --- Headers/opsys.h | 2 +- Sources/input.cpp | 18 ++++++++++++++---- Sources/main.cpp | 5 ++--- Sources/opsys.cpp | 39 +++++++++++++++++---------------------- 4 files changed, 34 insertions(+), 30 deletions(-) diff --git a/Headers/opsys.h b/Headers/opsys.h index b4654e9..157743e 100644 --- a/Headers/opsys.h +++ b/Headers/opsys.h @@ -2,7 +2,7 @@ #define OPSYS_H #include "input.h" -extern bool sigint; +extern bool exit_program; /*! * \class SignalHandler diff --git a/Sources/input.cpp b/Sources/input.cpp index 635b7cf..8e01fd4 100644 --- a/Sources/input.cpp +++ b/Sources/input.cpp @@ -55,9 +55,13 @@ void Prompt::parse(string input) istringstream iss (input); copy(istream_iterator(iss), istream_iterator(), back_inserter(tokens)); - for(unsigned short int i = 0; i < tokens.size(); i++) - cout << tokens[i] << ' '; - cout << '\n'; + if(is_verbose) + { + cout << "Parsed: ["; + for(unsigned short int i = 0; i < tokens.size()-1; i++) + cout << "\"" << tokens[i] << "\", "; + cout << "\"" << tokens.back() << "\"]\n"; + } } short int Prompt::run() @@ -74,7 +78,13 @@ short int Prompt::run() { cout << "Insert help text here.\n"; return 1; - } else + } else if (!tokens[0].compare("quit")) + { + cout << "Exiting" << std::endl; + exit_program = true; + return 1; + } + else { OS.simple_command(tokens); return 1; diff --git a/Sources/main.cpp b/Sources/main.cpp index 5ec914f..3424c3a 100644 --- a/Sources/main.cpp +++ b/Sources/main.cpp @@ -8,11 +8,11 @@ int main(int argc, char ** argv) SignalHandler sighandler; for(;;) { + if (exit_program) return 0; // Tests for SIGINT/SIGHUP string line; prompt.print(); std::getline(cin, line); - if (sigint) - return 0; + if (exit_program) return 0; // Tests for "quit" if (cin.eof()) { cout << "\nEOF found. Exiting.\n"; @@ -20,7 +20,6 @@ int main(int argc, char ** argv) } if(line.empty()) continue; - prompt.parse(line); prompt.run(); } diff --git a/Sources/opsys.cpp b/Sources/opsys.cpp index d52f8c5..8c80c7d 100644 --- a/Sources/opsys.cpp +++ b/Sources/opsys.cpp @@ -1,20 +1,20 @@ #include "Headers/opsys.h" OpSys OS; // Global OpSys variable. Should be accessible to all modules. -bool sigint = false; +bool exit_program = false; void signal_handler(int s) { if (s==SIGINT) { cout << "\nSIGINT (Ctrl+C) received. Code " << s << ". Exiting.\n"; - sigint = true; // We'll do this instead of an exit(0) in order to allow for cleanup. + exit_program = true; // We'll do this instead of an exit(0) in order to allow for cleanup. } if (s==SIGHUP) { cout << "\nSIGHUP received. Code " << s << ". Exiting.\n"; - sigint = true; // We'll do this instead of an exit(0) in order to allow for cleanup. + exit_program = true; // We'll do this instead of an exit(0) in order to allow for cleanup. } } @@ -49,24 +49,19 @@ void OpSys::change_dir(vector command) } } -std::vector make_argv( std::vectorconst& in ) +vector make_argv(vectorconst& in) { - std::vector out; - out.reserve( in.size() + 1 ); - - for (const auto& s : in) { - out.push_back( s.data() ); - } - out.push_back(NULL); - out.shrink_to_fit(); - - return out; + vector out; + out.reserve(in.size() + 1); + for (const auto& s : in) + out.push_back(s.data()); + out.push_back(nullptr); + out.shrink_to_fit(); + return out; } -short int OpSys::simple_command(vector tokens) +short OpSys::simple_command(vector tokens) { - pid_t pid; - int status; if(is_verbose) { @@ -78,21 +73,21 @@ short int OpSys::simple_command(vector tokens) } - pid = fork(); + pid_t pid = fork(); if (pid == 0) { //printf("execlp(%s, %s, NULL);", bincmd.c_str(), command.c_str()); execvp(tokens[0].c_str(), const_cast(make_argv(tokens).data())); - cerr << tokens[0] << ": comando não encontrado\n"; - exit(0); // TODO: exit with cleanup + cerr << tokens[0] << ": command not found.\n"; + return 0; // TODO: exit with cleanup } else if (pid < 0) { - printf("Erro ao produzir fork."); + printf("Couldn't fork a process."); return 0; } waitpid(-1, &status, WUNTRACED); - return 1; // sucesso + return 1; } OpSys::OpSys() -- 2.11.4.GIT