From cf3d0b5ecabc4071ed28ad93109e63038da8f73d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vin=C3=ADcius=20R=2E=20Miguel?= Date: Thu, 4 Jun 2020 19:56:34 -0300 Subject: [PATCH] Small changes Fixes an issue appointed by GCC's -Wall and one by lgtm.com, as well as a change to simple_command --- Headers/input.h | 2 +- Headers/opsys.h | 1 + Sources/input.cpp | 34 +++----------------------- Sources/main.cpp | 2 +- Sources/opsys.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++------------ 5 files changed, 65 insertions(+), 47 deletions(-) diff --git a/Headers/input.h b/Headers/input.h index b32c9d8..2183134 100644 --- a/Headers/input.h +++ b/Headers/input.h @@ -51,7 +51,7 @@ public: * \param argc Command-line argument count. * \param argv Array of strings that representing the command-line argument given. */ - CLIInputs(int argc, char ** argv); + CLIInputs(unsigned short argc, char ** argv); }; class Prompt diff --git a/Headers/opsys.h b/Headers/opsys.h index 845fb3a..30f2a2b 100644 --- a/Headers/opsys.h +++ b/Headers/opsys.h @@ -56,6 +56,7 @@ public: string username; // Username string hostname; // User's hostname string cwd; // Current working directory + string home_dir; // User's $HOME directory OpSys(); void change_dir(vector); void show_history(); diff --git a/Sources/input.cpp b/Sources/input.cpp index 055395f..c9e47b1 100644 --- a/Sources/input.cpp +++ b/Sources/input.cpp @@ -43,11 +43,11 @@ using std::ostream_iterator; using std::cin; using std::cout; -CLIInputs::CLIInputs(int argc, char **argv) +CLIInputs::CLIInputs(unsigned short argc, char **argv) { if(argc > 1) { - for(short unsigned int i = 1; i < argc; i++) + for(unsigned short i = 1; i < argc; i++) { string s_argv = argv[i]; if (!s_argv.compare("-h") || !s_argv.compare("--help")) @@ -68,8 +68,7 @@ CLIInputs::CLIInputs(int argc, char **argv) void Prompt::get_last_dir() { string last_dir = OS.cwd; - string home_dir = getenv("HOME"); - last_dir = std::regex_replace(last_dir, std::regex(home_dir), "~"); // Change HOME to ~ on the prompt line. + last_dir = std::regex_replace(last_dir, std::regex(OS.home_dir), "~"); // Change HOME to ~ on the prompt line. this->last_dir = last_dir; } @@ -111,10 +110,8 @@ 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); + int pipe_count = std::count(tokens.begin(), tokens.end(), "|"); OS.history.push_back(tokens); // Save the current command on OS.history @@ -122,29 +119,6 @@ short int Prompt::run() { // If there were pipes on the command, run OS.piped_command(); return OS.piped_command(tokens, pipe_count); } - if(!tokens[0].compare("cd")) - { - OS.change_dir(tokens); - return cwd_changed; - } else if (!tokens[0].compare("pwd")) - { - cout << OS.cwd << '\n'; - return 1; - } else if (!tokens[0].compare("help")) - { - cout << "Insert help text here.\n"; - return 1; - } else if (!tokens[0].compare("history")) - { - OS.show_history(); - return 1; - } - else if (!tokens[0].compare("quit")) - { - std::cerr << "Exiting.\n"; - exit_program = true; - return 1; - } else { return OS.simple_command(tokens); diff --git a/Sources/main.cpp b/Sources/main.cpp index 5490ff1..01397d0 100644 --- a/Sources/main.cpp +++ b/Sources/main.cpp @@ -31,7 +31,7 @@ using std::cout; int main(int argc, char ** argv) { cout << "miniSHELL 2 -- github.com/vrmiguel/minishell2\n"; - CLIInputs(argc, argv); // Process command-line arguments + CLIInputs((unsigned short) argc, argv); // Process command-line arguments Prompt prompt; SignalHandler sighandler; for(;;) diff --git a/Sources/opsys.cpp b/Sources/opsys.cpp index 27eca15..e68ace2 100644 --- a/Sources/opsys.cpp +++ b/Sources/opsys.cpp @@ -70,7 +70,11 @@ void OpSys::change_dir(vector command) { if (command.size() == 1 || !command[1].compare("~") || !command[1].compare("$HOME")) { // User wants to cd into $HOME/~. - chdir(getenv("HOME")); + if(chdir(home_dir.c_str())) + { + cerr << "minish2: cd: " << command[1] << ": Arquivo ou diretório não encontrado.\n"; + return; + } this->cwd = get_cwd(); cwd_changed = true; return; @@ -124,7 +128,8 @@ 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. + { /* This is an arbitrary decision. You can allow for more pipes by + altering this `if` and the size of file_descriptor_array. */ cerr << "More pipes than supported.\n"; return -1; } @@ -154,8 +159,8 @@ short OpSys::piped_command(vector tokens, int pipe_count) } // Forking pid_t pid = fork(); - if(pid==0) // Primeiro processo filho - { + if(pid==0) + { // Child process if(i!=n_commands-1) { dup2(file_descriptor_array[i][WRITE_END],STDOUT_FILENO); @@ -175,20 +180,20 @@ short OpSys::piped_command(vector tokens, int pipe_count) 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"; + execvp(aux_cmd[0].c_str(), const_cast(make_argv(aux_cmd).data())); // TODO: use OS.simple_command here + cerr << aux_cmd[0] << ": Command not found.\n"; return -1; } else { if(i!=0) - { + { // Close the file descriptors previously used close(file_descriptor_array[i - 1][READ_END]); close(file_descriptor_array[i - 1][WRITE_END]); } } } for(i=0; i tokens) { + + /* Checking for internal minishell functions */ + if(!tokens[0].compare("cd")) + { + OS.change_dir(tokens); + return cwd_changed; + } else if (!tokens[0].compare("pwd")) + { + cout << OS.cwd << '\n'; + return 1; + } else if (!tokens[0].compare("help")) + { + cout << "Insert help text here.\n"; + return 1; + } else if (!tokens[0].compare("history")) + { + OS.show_history(); + return 1; + } + else if (!tokens[0].compare("quit")) + { + std::cerr << "Exiting.\n"; + exit_program = true; + return 1; + } + + + /* Running an external command */ int status; if(is_verbose) { @@ -223,15 +256,19 @@ short OpSys::simple_command(vector tokens) pid_t pid = fork(); if (pid == 0) { + // Child process is going to execvp the given command execvp(tokens[0].c_str(), const_cast(make_argv(tokens).data())); + // The following lines will only run if execvp doesn't work. cerr << tokens[0] << ": command not found.\n"; - return -1; // TODO: exit with cleanup + return -1; } else if (pid < 0) { - printf("Couldn't fork a process."); - return 0; + cerr << "Couldn't fork a process."; + return -1; } + + // Block the parent process until the child process finishes. waitpid(-1, &status, WUNTRACED); return 1; } @@ -251,7 +288,13 @@ OpSys::OpSys() getpwuid_r(uid, &pwent, buffer, sizeof buffer, &pwent_ptr); username = pwent.pw_name; // Saves username - char hostname[64]; - gethostname(hostname, 64); - this->hostname = hostname; + string home = getenv("HOME"); + if(home.empty()) + cerr << "Couldn't obtain $HOME.\n"; + + this->home_dir = home; + + char hostname_buffer[64]; + gethostname(hostname_buffer, 64); + this->hostname = hostname_buffer; } -- 2.11.4.GIT