-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.cpp
155 lines (138 loc) · 4.17 KB
/
shell.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Name: shell.cpp
* Authors: Dean M. Sands, III of Team DDD
* Contains shell static class (should just call it a namespace)
*/
#include "shell.h"
char *getLastToken(char *buffer, char delimiter){
char *returnPointer,*walkingPointer;
returnPointer=buffer;
walkingPointer=buffer;
while(*walkingPointer!=0){
if(walkingPointer[0]==delimiter&&walkingPointer[1]!=0)
returnPointer=walkingPointer;
walkingPointer++;
}
if(returnPointer[1]!=0)
returnPointer++;
return returnPointer;
}
shell::shell() {
}
shell::shell(const shell& that) {
}
shell::~shell() {
}
char *shell::recombineArguments(char **arguments){
char*complete;
std::string temp;
for(int i=0;arguments[i]!=NULL;i++){
if(i!=0){
temp+=" ";
}
temp+=arguments[i];
}
complete=new char[temp.size()+1];
std::strcpy(complete, temp.c_str());
temp.clear();
return complete;
}
char *shell::createArgumentFromEnvironment(char *argument){
char *temp, *temp2;
char buffer[MAXBUFFER];
if(argument[0]=='$'){
temp=getenv(argument+1);
if(temp!=NULL){
temp2=new char[strlen(temp)+1];
strcpy(temp2, temp);
}else{
temp2=new char[2];
temp2[0]=' ';
temp2[1]=0;
}
return temp2;
}
else
return argument;
}
void shell::printPrompt(){
char *hostName=new char[24];
char *currentWorkingDirectory=new char[2048];
gethostname(hostName, 24);
getcwd(currentWorkingDirectory, 2048);
std::cout
<<ANSIRESET<<"[" //Opening [ in default color
<<ANSI_BOLD //Bold Text
<<ANSIBLUFG<<getenv("USER") //Print User in Blue
<<ANSICYNFG<<"@" //Print @ in Cyan
<<ANSIGRNFG<<hostName //Print Hostname in Green
<<ANSIYLWFG<<":" //Print : in Yellow
<<ANSIMAGFG<<getLastToken(currentWorkingDirectory,'/') //Print current Directory in Magenta
<<ANSIRESET<<"]"; //Print closing ] in default color
if(strcmp(getenv("USER"), "root")==0)
std::cout<<ANSIREDFG<<"#"; //Print hash mark for root in dark red (very awesome)
else
std::cout<<ANSI_BOLD<<ANSIWHTFG<<"$"; //Print dollar sign for non-root in bright White
std::cout<<ANSIRESET<<" "; //Print a friend space
delete currentWorkingDirectory;
delete hostName;
}
bool shell::fileExists (const char * fileName){
return (access ( fileName, F_OK )==0); //If access returns 0, we're good!
}
bool shell::isDone() {
return done;
}
void shell::init(){
std::cout<<version(NULL);
setupLocatorThread();
setupExecuteThread();
numberOfChildrenProcesses=(int*)SharedMem::getSharedMemorySegment(sizeof(int), DDDShellKey);
running=true;
done=false;
}
void shell::shutDown(){
running=false;
stopLocatorThread();
stopExecuteThread();
}
void shell::run(){
char inputBuffer[MAXBUFFER];
int length;
pid_t childpid;
while(!shell::isDone()){
shell::printPrompt();
std::cin.getline(inputBuffer,MAXBUFFER,'\n');
length=strlen(inputBuffer);
if((childpid=fork())==-1){
perror("Failed to fork child");
}
else if(childpid==0){
shell::executeCommand(inputBuffer);
exit(1);
}
else{
wait(NULL); //Still doesn't work right.
for(int i=1;i<numberOfChildrenProcesses[0];i++){
wait(NULL);
}
}
}
}
void shell::setDDDShellKey(char* DDDShellKeyPath) {
DDDShellKey = new char[std::strlen(DDDShellKeyPath)+1];
std::strcpy(DDDShellKey, DDDShellKeyPath);
}
void shell::go(){
shell::init();
shell::initReservedWords();
shell::run();
shell::shutDown();
}
std::vector <std::string> shell::path;
std::vector <std::string> shell::directories;
bool shell::running;
bool shell::done;
char* shell::DDDShellKey;
std::map<std::string, ReservedWords*> shell::reserved_words;
int *shell::numberOfChildrenProcesses;