This repository has been archived by the owner on May 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.cpp
204 lines (169 loc) · 4.06 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
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
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "pipe.h"
#include "process.h"
using namespace std;
inline
bool prompt(string &line) {
cout << "% ";
getline(cin, line);
return cin;
}
bool special_cmd(istream &cmd);
void parse_cmd(istream &cmd);
int main() {
signal(SIGCHLD, SIG_IGN);
setenv("PATH", "bin:.", 1);
cout
<< "****************************************\n"
<< "** Welcome to the information server. **\n"
<< "****************************************" << endl;
string input_line;
while (prompt(input_line)) {
istringstream iss(input_line);
if (!special_cmd(iss))
parse_cmd(iss);
}
}
bool special_cmd(istream &cmd) {
string arg;
cmd >> arg;
if (arg == "exit") {
exit(0);
} else if (arg == "printenv") {
cmd >> arg;
const char *val = getenv(arg.c_str());
if (val)
cout << arg << "=" << val << endl;
} else if (arg == "setenv") {
string arg2;
cmd >> arg >> arg2;
setenv(arg.c_str(), arg2.c_str(), 1);
} else {
cmd.seekg(0);
return false;
}
return true;
}
inline
vector<string> get_argv(istream &cmd) {
string arg;
vector<string> argv;
while ((cmd >> ws) && !strchr("|!><", cmd.peek()) && (cmd >> arg)) {
argv.push_back(arg);
}
return argv;
}
void parse_cmd(istream &cmd) {
int running_ps = 0;
bool terminate = false;
Pipe *inPipe = NULL, *outPipe = NULL;
while (cmd && !terminate) {
/* Processing command's arguments. */
vector<string> argv = get_argv(cmd);
/* Processing command's I/O redirection. */
int newps_stdin;
int newps_stdout = STDOUT_FILENO;
int newps_stderr = STDERR_FILENO;
inPipe = outPipe;
outPipe = NULL;
if (!running_ps) {
if ((newps_stdin = Pipe::front()) == -1)
newps_stdin = STDIN_FILENO;
} else {
newps_stdin = inPipe->read_fd();
}
int stdout_at = -1;
int stderr_at = -1;
bool file_redirection = false;
bool file_in_redirection = false;
while ((cmd >> ws) && strchr("|!><", cmd.peek())) {
string filename;
switch (cmd.get()) {
case '|':
if (isdigit(cmd.peek())) {
// Number pipe
int pipe_at;
cmd >> pipe_at;
if (newps_stdout == STDOUT_FILENO) {
stdout_at = pipe_at;
newps_stdout = Pipe::append_at(pipe_at);
}
} else if (newps_stdout == STDOUT_FILENO) {
outPipe = new Pipe();
newps_stdout = outPipe->write_fd();
}
break;
case '!':
if (isdigit(cmd.peek())) {
int pipe_at;
cmd >> pipe_at;
if (newps_stderr == STDERR_FILENO) {
stderr_at = pipe_at;
newps_stderr = Pipe::append_at(pipe_at);
}
}
break;
case '>':
cmd >> filename; // file name
if (newps_stdout != STDOUT_FILENO)
break;
newps_stdout = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 000777);
if (newps_stdout == -1) {
cout << "Cannot open file: " << filename << endl;
delete inPipe;
return;
}
file_redirection = true;
break;
case '<':
cmd >> filename; // file name
if (newps_stdin != STDIN_FILENO)
break;
newps_stdin = open(filename.c_str(), O_RDONLY | O_CLOEXEC);
if (newps_stdin == -1) {
cout << "Cannot open file: " << filename << endl;
delete inPipe;
return;
}
file_in_redirection = true;
break;
}
}
/* Launching the command. */
if (create_process(newps_stdin, newps_stdout, newps_stderr, argv)) {
running_ps++;
} else {
if (stdout_at != -1)
Pipe::cancel_at(stdout_at);
if (stderr_at != -1)
Pipe::cancel_at(stderr_at);
cout << "Unknown command: [" << argv[0] << "]." << endl;
terminate = true;
}
delete inPipe;
if (file_redirection) {
close(newps_stdout);
}
if (file_in_redirection) {
close(newps_stdin);
}
terminate |= file_redirection || stdout_at != -1 || stderr_at != -1;
}
delete outPipe;
if (running_ps)
Pipe::pop();
int exit_status;
while (running_ps--)
wait(&exit_status);
}