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
26 #include "Headers/opsys.h"
27 #include <regex> // std::regex and std::regex_replace
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
;
41 using std::istream_iterator
;
42 using std::ostream_iterator
;
46 CLIInputs::CLIInputs(int argc
, char **argv
)
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");
59 if (!s_argv
.compare("-v") || !s_argv
.compare("--verbose"))
61 printf("Executing in verbose mode.\n");
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
;
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
93 cout
<< BLUE_ANSI
<< OS
.username
<< "@" << OS
.hostname
<< ":" << RED_ANSI
<< last_dir
<< RESET_ANSI
<< "$ ";
96 void Prompt::parse(string input
)
100 istringstream
iss (input
);
101 copy(istream_iterator
<string
>(iss
), istream_iterator
<string
>(), back_inserter(tokens
));
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()
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
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
);
129 } else if (!tokens
[0].compare("pwd"))
131 cout
<< OS
.cwd
<< '\n';
133 } else if (!tokens
[0].compare("help"))
135 cout
<< "Insert help text here.\n";
137 } else if (!tokens
[0].compare("history"))
142 else if (!tokens
[0].compare("quit"))
144 std::cerr
<< "Exiting.\n";
150 return OS
.simple_command(tokens
);