-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
102 lines (84 loc) · 2.08 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "shell.hpp"
#include<string>
#include<signal.h>
#include<memory>
#include"color.hpp"
int startShell();
std::vector<std::string> parseCommand(std::string);
int findPipe(std::vector<std::string>);
int main(){
startShell();
return EXIT_SUCCESS;
}
extern "C" void myHandler(int SignalID);
void myHandler(int SignalID){
std::cout << "You can't escape that easily!" << std::endl;
char workingDirectory[1024];
getcwd(workingDirectory, sizeof(workingDirectory));
std::cout << FRED("[ ");
std::cout << workingDirectory;
std::cout << FRED(" ]: ");
std::cout.flush();
}
int startShell(){
shell myshell;
signal(SIGINT, myHandler);
while(true){
std::cout << FRED("[ ");
myshell.pwd();
std::cout << FRED(" ]: ");
std::cout.flush();
std::string cmd = "";
std::getline(std::cin, cmd);
auto parsedCommand = parseCommand(cmd);
myshell.appendHistory(parsedCommand);
int p = findPipe(parsedCommand);
//myshell.showHistory();
//going to have to check for pipes here, then for each pipe run execute command
if(p == -1){myshell.executeCommand(parsedCommand);}
else{
std::vector<std::string> cmd1;
std::vector<std::string> cmd2;
std::cout << p << std::endl;
for(int i = 0; i < parsedCommand.size() - 1; i++){
if(i < p){cmd1.push_back(parsedCommand.at(i));}
else if(i > p){cmd2.push_back(parsedCommand.at(i));}
}
myshell.handlePipe(cmd1, cmd2);
}
}
return 0;
}
int findPipe(std::vector<std::string> fullcmd){
int i = 0;
for(i; i < fullcmd.size(); i++){
if(fullcmd.at(i) == "|"){
return i;
}
}
return -1;
}
std::vector<std::string> parseCommand(std::string s){
std::vector<std::string> argvVector;
std::istringstream iss(s);
do{
std::string cx;
iss >> cx;
argvVector.push_back(cx);
} while (iss);
return argvVector;
}
/* int i = argvVector.size();
char** argv = new char*[i];
argv[i] = NULL;
i--;
for(i;i>=0;i--){
argv[i] = new char[5];
std::string cmd = argvVector.at(i);
strcpy(argv[i], cmd.c_str());
std::cout << "argv["<<i<<"]"<<argv[i]<<std::endl;
}
std::cout << "made it here" << std::endl;
return argv;
}
*/