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
;
15 using std::istream_iterator
;
16 using std::ostream_iterator
;
20 CLIInputs::CLIInputs(int argc
, char **argv
)
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");
33 if (!s_argv
.compare("-v") || !s_argv
.compare("--verbose"))
35 printf("Executing in verbose mode.\n");
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
;
62 cout
<< BLUE_ANSI
<< OS
.username
<< "@" << OS
.hostname
<< ":" << RED_ANSI
<< last_dir
<< BLUE_ANSI
<< "$ " << RESET_ANSI
;
65 void Prompt::parse(string input
)
69 istringstream
iss (input
);
70 copy(istream_iterator
<string
>(iss
), istream_iterator
<string
>(), back_inserter(tokens
));
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
);
87 } else if (!tokens
[0].compare("pwd"))
89 cout
<< OS
.cwd
<< '\n';
91 } else if (!tokens
[0].compare("help"))
93 cout
<< "Insert help text here.\n";
95 } else if (!tokens
[0].compare("quit"))
97 cout
<< "Exiting" << std::endl
;
103 OS
.simple_command(tokens
);