Add MIT license notice
[minishell-2.git] / Sources / input.cpp
blob055395fb25644473a045433cf5a26fa14b242f58
1 /*
2 * miniSHELL-2
3 * https://github.com/vrmiguel/minishell-2
5 * Copyright (c) 2020 Vinícius R. Miguel <vinicius.miguel at unifesp.br>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
26 #include "Headers/opsys.h"
27 #include <regex> // std::regex and std::regex_replace
28 #include <termios.h>
30 bool is_verbose = false;
31 bool cwd_changed = false;
33 #include <unistd.h> // fork, execvp
34 #include <pwd.h> // struct password, getpwuid_r
35 #include <sstream> // isstreams, osstreams
36 #include <algorithm> // copy
37 #include <iterator> // istream_iterator, iterator, back_inserter
39 using std::istringstream;
40 using std::copy;
41 using std::istream_iterator;
42 using std::ostream_iterator;
43 using std::cin;
44 using std::cout;
46 CLIInputs::CLIInputs(int argc, char **argv)
48 if(argc > 1)
50 for(short unsigned int i = 1; i < argc; i++)
52 string s_argv = argv[i];
53 if (!s_argv.compare("-h") || !s_argv.compare("--help"))
55 printf("%-20s\tObtain help.\n", "-h, --help");
56 printf("%-20s\tShows additional execution data.\n", "-v, --verbose");
57 exit(0);
59 if (!s_argv.compare("-v") || !s_argv.compare("--verbose"))
61 printf("Executing in verbose mode.\n");
62 is_verbose = true;
68 void Prompt::get_last_dir()
70 string last_dir = OS.cwd;
71 string home_dir = getenv("HOME");
72 last_dir = std::regex_replace(last_dir, std::regex(home_dir), "~"); // Change HOME to ~ on the prompt line.
73 this->last_dir = last_dir;
76 Prompt::Prompt()
78 // struct termios t;
79 // tcgetattr(STDIN_FILENO, &t);
80 // t.c_lflag &= ~ICANON;
81 // tcsetattr(STDIN_FILENO, TCSANOW, &t); // WIP for arrow keys support
83 get_last_dir(); // Process cwd for exibition on prompt
86 void Prompt::print()
88 if (cwd_changed)
90 get_last_dir();
91 cwd_changed = false;
93 cout << BLUE_ANSI << OS.username << "@" << OS.hostname << ":" << RED_ANSI << last_dir << RESET_ANSI << "$ ";
96 void Prompt::parse(string input)
98 if(!tokens.empty())
99 tokens.clear();
100 istringstream iss (input);
101 copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter(tokens));
103 if(is_verbose)
105 cout << "Parsed: [";
106 for(unsigned short int i = 0; i < tokens.size()-1; i++)
107 cout << "\"" << tokens[i] << "\", ";
108 cout << "\"" << tokens.back() << "\"]\n";
112 short int Prompt::run()
114 string pipe = "|";
116 // Counts occurrences of a pipe character in the given command
117 int pipe_count = std::count(tokens.begin(), tokens.end(), pipe);
119 OS.history.push_back(tokens); // Save the current command on OS.history
121 if(pipe_count)
122 { // If there were pipes on the command, run OS.piped_command();
123 return OS.piped_command(tokens, pipe_count);
125 if(!tokens[0].compare("cd"))
127 OS.change_dir(tokens);
128 return cwd_changed;
129 } else if (!tokens[0].compare("pwd"))
131 cout << OS.cwd << '\n';
132 return 1;
133 } else if (!tokens[0].compare("help"))
135 cout << "Insert help text here.\n";
136 return 1;
137 } else if (!tokens[0].compare("history"))
139 OS.show_history();
140 return 1;
142 else if (!tokens[0].compare("quit"))
144 std::cerr << "Exiting.\n";
145 exit_program = true;
146 return 1;
148 else
150 return OS.simple_command(tokens);