-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
231 lines (159 loc) · 6.05 KB
/
Copy pathmain.cpp
File metadata and controls
231 lines (159 loc) · 6.05 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
/*
* Copyright (c) 2018, Yutaka Tsutano
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/*
NAME: HUNG CHU
NUID: 95060612
*/
#include <iostream>
#include <string>
#include <vector>
#include "command.hpp"
#include "parser.hpp"
#include <sys/wait.h>
#include <wait.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int exec(const std::string& cmd, const std::vector<std::string>& args)
{
// Make an ugly C-style args array.
std::vector<char*> c_args = {const_cast<char*>(cmd.c_str())};
for (const auto& a : args) {
c_args.push_back(const_cast<char*>(a.c_str()));
}
c_args.push_back(nullptr);
return execvp(cmd.c_str(), c_args.data());
}
ostream_mode prevPipe;
int main(int argc, char *argv[])
{
std::string input_line;
int childId, out, out1, in;
int status = 0;
int pipeID = 0;
int pipeID1 = 0;
int checkStatus;
std::string com;
const char *fileIn;
const char *fileOut;
istream_mode inputMode;
ostream_mode outputMode;
std::vector<std::string> argument;
next_command_mode nextMode, prevMode;
bool disablePrint = false;
int pipefd[2];
for (;;) {
// check for "-t"
if ( argv[1] != NULL ){
disablePrint = true;
}
// Print the prompt.
if ( disablePrint == false){
std::cout<< "osh> " << std::flush;
}
// Read a single line.
if (!std::getline(std::cin, input_line) || input_line == "exit") {
break;
}
try {
// Parse the input line.
std::vector<shell_command> shell_commands
= parse_command_string(input_line);
prevPipe = ostream_mode::term;
checkStatus = 0;
prevMode = next_command_mode::always;
// std::cout << "-------------------------\n";
// for loop to iterate through each commands
for (const auto & cmd : shell_commands) {
// std::cout << cmd;
// std::cout << "-------------------------\n";
com = cmd.cmd;
argument = cmd.args;
inputMode = cmd.cin_mode;
outputMode = cmd.cout_mode;
fileIn = cmd.cin_file.c_str();
fileOut = cmd.cout_file.c_str();
nextMode = cmd.next_mode;
// check for the exec in phase 3
if ( ( checkStatus != 0 && prevMode == next_command_mode::on_success ) || ( checkStatus == 0 && prevMode == next_command_mode::on_fail ) ){
status = checkStatus;
break;
}
// create pipefd
if ( pipe(pipefd) == -1 ) {
perror(" fail to create pipe(pipefd)\n ");
}
// create child process to exec
childId = fork();
if ( childId == 0){
// > redirect In
if ( outputMode == ostream_mode::file ){
out = open(fileOut, O_WRONLY | O_CREAT , 0700);
dup2(out, 1);
close(out);
}
// >>
if ( outputMode == ostream_mode::append ){
out1 = open(fileOut,O_APPEND | O_CREAT| O_RDWR,0700);
dup2(out1, 1);
close(out1);
}
// <
if ( inputMode == istream_mode::file ){
in = open(fileIn,O_RDONLY , 0700);
dup2(in, 0);
close(in);
}
// check inputMode for pipe
if ( inputMode == istream_mode::pipe){
dup2(pipeID, 0);
close(pipefd[0]);
}
// check outputMode for pipe
if ( outputMode == ostream_mode::pipe){
prevPipe = ostream_mode::pipe;
dup2(pipefd[1], 1);
close(pipefd[1]);
}
exec(com, argument);
}
else{
wait(&status);
pipeID = pipefd[0];
pipeID1 = pipefd[1];
// turn the output of command back to terminal
if ( outputMode != ostream_mode::term ){
dup2(1, pipeID1);
}
// check for status of command in phase 3
if ( nextMode == next_command_mode::on_success || nextMode == next_command_mode::on_fail ){
prevMode = nextMode;
checkStatus = status;
}
}
}// for each command
}// try loop
catch (const std::runtime_error& e) {
//std::cout << "osh: " << e.what() << "\n";
std::cout << e.what() << "\n";
}
}// end for
if ( disablePrint == false){
std::cout << std::endl;
}
}