Less idiotic includes
[minishell-2.git] / Sources / input.cpp
blob1a0c6b7a5b49322eecc33a3d35d5311df2adebe9
1 #include "Headers/opsys.h"
2 #include <regex> // std::regex and std::regex_replace
4 bool is_verbose = false;
5 bool cwd_changed = false;
7 #include <unistd.h> // fork, execvp
8 #include <pwd.h> // struct password, getpwuid_r
9 #include <sstream> // isstreams, osstreams
10 #include <algorithm> // copy
11 #include <iterator> // istream_iterator, iterator, back_inserter
13 using std::istringstream;
14 using std::copy;
15 using std::istream_iterator;
16 using std::ostream_iterator;
17 using std::cin;
18 using std::cout;
20 CLIInputs::CLIInputs(int argc, char **argv)
22 if(argc > 1)
24 for(short unsigned int i = 1; i < argc; i++)
26 string s_argv = argv[i];
27 if (!s_argv.compare("-h") || !s_argv.compare("--help"))
29 printf("%-20s\tObtain help.\n", "-h, --help");
30 printf("%-20s\tShows additional execution data.\n", "-v, --verbose");
31 exit(0);
33 if (!s_argv.compare("-v") || !s_argv.compare("--verbose"))
35 printf("Executing in verbose mode.\n");
36 is_verbose = true;
42 void Prompt::get_last_dir()
44 string last_dir = OS.cwd;
45 string home_dir = getenv("HOME");
46 last_dir = std::regex_replace(last_dir, std::regex(home_dir), "~"); // Change HOME to ~ on the prompt line.
47 this->last_dir = last_dir;
50 Prompt::Prompt()
52 get_last_dir();
55 void Prompt::print()
57 if (cwd_changed)
59 get_last_dir();
60 cwd_changed = false;
62 cout << BLUE_ANSI << OS.username << "@" << OS.hostname << ":" << RED_ANSI << last_dir << BLUE_ANSI << "$ " << RESET_ANSI;
65 void Prompt::parse(string input)
67 if(!tokens.empty())
68 tokens.clear();
69 istringstream iss (input);
70 copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter(tokens));
72 if(is_verbose)
74 cout << "Parsed: [";
75 for(unsigned short int i = 0; i < tokens.size()-1; i++)
76 cout << "\"" << tokens[i] << "\", ";
77 cout << "\"" << tokens.back() << "\"]\n";
81 short int Prompt::run()
83 if(!tokens[0].compare("cd"))
85 OS.change_dir(tokens);
86 return cwd_changed;
87 } else if (!tokens[0].compare("pwd"))
89 cout << OS.cwd << '\n';
90 return 1;
91 } else if (!tokens[0].compare("help"))
93 cout << "Insert help text here.\n";
94 return 1;
95 } else if (!tokens[0].compare("quit"))
97 cout << "Exiting" << std::endl;
98 exit_program = true;
99 return 1;
101 else
103 OS.simple_command(tokens);
104 return 1;