-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.h
221 lines (188 loc) · 4.41 KB
/
shell.h
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "banner.h"
#include "builtins.h"
#include "constants.h"
#include "myutils.h"
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <list>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;
int loop_ctr = 0; //
class Shell
{
protected:
string line;
string command;
vector<string> args;
string current_working_dir;
string hostname;
Builtins builtins;
//vector<pid_t> bg_jobs;
const vector<string> path_variable = {
"/usr/local/sbin",
"/usr/local/bin",
"/usr/sbin",
"/usr/bin",
"/sbin",
"/bin"
};
public:
Shell() : builtins(*this) {}
void main_loop();
protected:
virtual void show_prompt();
virtual void read_line();
virtual void parse_line();
virtual void find_and_execute_command();
virtual void execute_external_command();
string get_current_working_dir() {return current_working_dir;}
// (cd and pwd need to be able to modify/view shell's private vars)
friend int Builtins:: pwd(const vector<string>&);
friend int Builtins:: cd(const vector<string>&);
};
/*------------------------------------------------*/
// Defined here where both classes are not "improperly defined" anymore.
// (cd and pwd need to be able to modify/view shell's private vars)
// Can't be bothered to create a shell.h, shell.cc and builtins.cc right now.
int Builtins:: cd(const vector<string>& args)
{
int status = chdir(string_to_charptr(args[0]));
if (status) cerr << "Directory not found.\n";
else
{
char cwd[PATH_MAX]; // constant defined in climits
if (getcwd(cwd, sizeof(cwd)) != NULL) // this func is from unistd
{
shell.current_working_dir = cwd;
}
else
{
cerr << "getcwd() error.\n";
status = 1;
}
}
return status;
}
int Builtins:: pwd(const vector<string>& args)
{
cout << shell.current_working_dir << '\n';
return 0;
}
/*------------------------------------------------*/
void Shell:: show_prompt()
{
cout << "\033[33m"
<< getenv("USERNAME")
<< " "
<< current_working_dir
<<"\033[00m"
<< '\n'
// << loop_ctr ///
<< "% ";
}
void Shell:: read_line()
{
getline(cin, line);
}
void Shell:: parse_line()
{
vector<string> tokens;
string token;
istringstream line_stream(line);
while (line_stream >> token)
{
tokens.push_back(token);
}
// the command is the first token and the args are the rest
// `at` used because line may be empty.
command = tokens.at(0);
args = tokens;
args.erase(args.begin());
}
void Shell:: execute_external_command()
{
// BUG
// Ctrl+C not only stops command execution, it also kills the entire shell
// let child process execute command
if (fork() == 0)
{
myexecv(command, args);
}
else
{
int status;
wait(&status);
}
}
void Shell:: find_and_execute_command()
{
bool found = true;
// Checking if command matches builtins
// Did not use hashmap due to some difficulty with function pointers
if (command == "cd")
{
builtins.cd(args);
}
else if (command == "help")
{
builtins.help(args);
}
else if (command == "pwd")
{
builtins.pwd(args);
}
else if (command == "echo")
{
builtins.echo(args);
}
else if (command == "exit" or command == "q")
{}
// Checking if command matches one of the external commands in bin, sbin, etc.
else
{
string modified_command;
if (IS_CYGWIN and not endswith(command, ".exe")) // Doubt: Will this work for all commands?
modified_command = command + ".exe";
modified_command = find_path_of_file(modified_command, path_variable, found);
if (found)
{
command = modified_command;
try {execute_external_command();}
catch (runtime_error rerr){cerr << rerr.what() << endl;} // should never happen if found is true; it's just there as a sanity check
}
}
if (not found) // the command is called by the user by its absolute or relative path
{
try{
ifstream fin(command);
if (!fin) throw runtime_error("Relative path not found.");
execute_external_command();
}
catch (runtime_error rerr) {cerr << rerr.what() << endl;} // may happen
}
// TODO
// Check if commands match stuff in $PATH variable
}
void Shell:: main_loop()
{
chdir(getenv("HOME"));
current_working_dir = getenv("HOME");
display_banner();
do
{
show_prompt();
read_line();
parse_line();
find_and_execute_command();
loop_ctr++;//
} while(command != "exit" and command != "q");
}