-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.cpp
More file actions
302 lines (251 loc) · 6.22 KB
/
shell.cpp
File metadata and controls
302 lines (251 loc) · 6.22 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <vector>
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <fcntl.h>
#include <string>
#include <sstream>
#include <sys/wait.h>
#include <cstring>
#define REDIRECT_NO -1
#define REDIRECT_IN 0
#define REDIRECT_OUT 1
#define REDIRECT_APP 2
#define STD_IN 0
#define STD_OUT 1
#define STD_ERR 2
using namespace std;
// a clown fiesta of declarations
istream& getinput(string &line);
bool builtins(vector< const char * > argv);
vector<const char *> parse_to_vector(string line);
int start_full_command (vector< const char * > args);
void printargs(vector<const char *> a);
void child_sighand(int sig);
void parent_sighand(int sig);
vector < vector<const char *> > split_on_pipes(vector<const char *> args);
int spawn_single_command(int i_ID, int o_ID, vector<const char *> argv);
int fork_commands(vector < vector < const char * > > commands, int redirect, const char* redirectLocation);
int execvec(vector<const char *> argv);
int main(int argc, char ** argv)
{
// flush the outputs
cout << flush;
cerr << flush;
string line;
int status;
while (getinput(line))
{
vector<const char *> argv = parse_to_vector(line);
//printargs(argv);
if (argv[0] != 0)
if (! builtins(argv))
status = start_full_command(argv);
}
return 0;
}
// parses the line and fills the given vector with its contents
vector<const char *> parse_to_vector(string line)
{
stringstream ss(line);
string t;
vector<const char *> args;
args.clear();
int i = 0;
while (ss >> t)
{
char * push = new char[t.size() + 1];
strcpy(push, t.c_str());
args.push_back(push);
++i;
}
return args;
}
// checks if it's a builtin and does it
// does exit and cd
bool builtins(vector< const char * > argv)
{
// handle "exit"
if (strcmp(argv[0], "exit") == 0)
{
exit(EXIT_SUCCESS);
return true;
}
// handle "cd"
else if (strcmp(argv[0], "cd") == 0)
{
if (argv.size() != 2)
cerr << "CD must have exactly one argument";
else
chdir(argv[1]);
return true;
}
return false;
}
// execvp's the given vector
int execvec(vector<const char *> argv)
{
// please don't ask why this is necessary
// all i know is that if failed if there
// were an even, nonzero number of arguments past the command
if (argv.size() > 2 || argv.size() % 2 == 1)
argv.push_back((const char*)0);
// does the actual execution
if (execvp(argv[0], (char**) argv.data()) == -1)
{
cerr << "Error: execvp failed.\n";
exit(EXIT_FAILURE);
return -1;
}
return 0;
}
// called by fork commands to do the single command
int spawn_single_command(int i_ID, int o_ID, vector<const char *> argv)
{
pid_t pid = fork();
// updates the inputs and outputs to their appropriate values
if (pid == 0)
{
if (i_ID != 0)
{
dup2(i_ID, 0);
close(i_ID);
}
if (o_ID != 1)
{
dup2(o_ID, 1);
close(o_ID);
}
return execvec(argv);
}
return pid;
}
// forks off the individual commands as necessary
int fork_commands(vector < vector < const char * > > commands, int redirect, const char* redirectLocation)
{
pid_t pid;
int fd[2];
// if there's input redirection
if (redirect == REDIRECT_IN)
{
int i_ID = open(redirectLocation, O_RDONLY);
dup2(i_ID, STD_IN);
close(i_ID);
}
int i_ID;
int i = 0;
// for all but the last command
for ( ; i < commands.size() - 1; ++i)
{
pipe(fd);
//cout << fd[0] << "-" << fd[1] << endl;
spawn_single_command(i_ID, fd[1], commands[i]);
close(fd[1]);
i_ID = fd[0];
}
if (i_ID) // if the output of the previous is not standard input
{
dup2 (i_ID, STD_IN);
}
// if there is file redirection
if (redirect == REDIRECT_APP || redirect == REDIRECT_OUT)
{
int o_ID;
if (redirect == REDIRECT_OUT)
o_ID = open(redirectLocation, O_WRONLY | O_TRUNC | O_CREAT, 0666);
else
o_ID = open(redirectLocation, O_WRONLY | O_APPEND | O_CREAT, 0666);
dup2(o_ID, STD_OUT);
close(o_ID);
}
return execvec(commands[i]);
}
// starts the full command execution
int start_full_command (vector< const char * > args)
{
pid_t pid;
int status;
signal(SIGUSR1, parent_sighand);
pid = fork();
if (pid == 0)
{
// child process
int redirect = REDIRECT_NO;
const char * redirectLocation = (const char*)0;
if (args.size() >= 3)
{
int i = args.size() - 2;
if (strcmp(args[i], "<") == 0)
redirect = REDIRECT_IN;
else if (strcmp(args[i], ">>") == 0)
redirect = REDIRECT_APP;
else if (strcmp(args[i], ">") == 0)
redirect = REDIRECT_OUT;
//cout << "RED" << redirect << endl;
if (redirect != REDIRECT_NO)
{
redirectLocation = args.back();
args.pop_back();
args.pop_back();
}
}
// split to commands
vector < vector < const char * > > commands = split_on_pipes(args);
//for (unsigned int i = 0; i < commands.size(); ++i) printargs(commands[i]);
signal(SIGUSR1, child_sighand);
fork_commands(commands, redirect, redirectLocation);
exit(EXIT_FAILURE);
}
else if (pid < 0)
{
// fork failed
cerr << "Error: Fork failed.\n";
}
// wait for the kiddo to finish
if ((pid =waitpid(pid, &status, 0)) < 0)
cerr << "Error: failure while waiting for child to complete.\n";
signal(SIGINT, SIG_DFL);
return 1;
}
// splits the token vector to a vector of vectors
// where each vector is what is between the pipes
vector < vector<const char *> > split_on_pipes(vector<const char *> args)
{
vector< vector <const char * > > commands;
commands.push_back(*(new vector<const char *>));
for (int i = 0; i < args.size(); ++i)
{
if (strcmp(args[i], "|") == 0) // when it's a pipe
commands.push_back(*(new vector<const char *>)); // start the next set of commands
else
commands.back().push_back(args[i]); // if it's not a pipe, just stick it in the last set of commands
}
return commands;
}
// prints the prompt and gets the line of input
istream& getinput(string &l)
{
//char temp[WD_MAXLEN];
//getcwd(temp, WD_MAXLEN);
//cout << temp << "$ ";
cout << "$$ ";
return getline(cin, l);
}
// prints out the arguments of the command vector passed in
void printargs(vector<const char *> argv)
{
cout << "ARGS: ";
for (int i = 0; i < argv.size() ; ++i)
cout << argv[i] << " ";
cout << endl;
}
// unneeded
void parent_sighand(int sig)
{
}
void child_sighand(int sig)
{
exit(EXIT_FAILURE);
}