-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyshell.cpp
More file actions
128 lines (107 loc) · 2.81 KB
/
myshell.cpp
File metadata and controls
128 lines (107 loc) · 2.81 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
#include "command.h"
#include "helper.h"
#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
using namespace std;
const char PIPE_DELIMITER = '|';
void waitForChildren()
{
while (true)
{
// Wait for the child process to exit
int status;
pid_t pid = wait(&status);
if (int(pid) <= 0)
{
// No children left. We have waited for all children to exit
return;
}
// Get and print exit status of child process
if (WIFEXITED(status))
{
int code = WEXITSTATUS(status);
cout << "process " << pid << " exits with " << code << endl;
}
// Handle edge case where child process was terminated via signal
else if (WIFSIGNALED(status))
{
int signal = WTERMSIG(status);
// The process was terminated by a signal.
cout << "process " << pid << " was terminated with " << signal << endl;
}
else
{
cout << "process " << pid << " exits with "
<< "unknown code" << endl;
}
}
}
void runCommands(vector<string> rawCommands)
{
// Handle single command (no pipes)
if (int(rawCommands.size()) == 1)
{
// Run the single command
Command command = Command(rawCommands.front());
command.run();
// Wait for the single child process to exit
waitForChildren();
return;
}
// Everything from here on is for multi-commands (involving pipes)
// Set up array to keep track of pipes
int inPipe[2];
int outPipe[2];
// Set up pipe for first command
if (pipe(outPipe) == -1)
{
cout << "Error: pipe failed" << endl;
};
// Run the first command with only an output pipe
Command leadingCommand = Command(rawCommands[0], NULL, outPipe);
leadingCommand.run();
// Reroute the pipes for the next command
inPipe[0] = outPipe[0];
inPipe[1] = outPipe[1];
// Start with the 2nd command and end with the 2nd to last command since the
// first and last commands use default STDIN/STDOUT
for (size_t i = 1; i < rawCommands.size() - 1; i++)
{
// Set up the pipes
if (pipe(outPipe) == -1)
{
cout << "Error: pipe failed" << endl;
};
// Run the command
Command command = Command(rawCommands[i], inPipe, outPipe);
command.run();
// Reroute the pipes for the next command
inPipe[0] = outPipe[0];
inPipe[1] = outPipe[1];
}
// Run the last command
Command trailingCommand = Command(rawCommands[rawCommands.size() - 1], inPipe, NULL);
trailingCommand.run();
// Wait for children AFTER forking and executing all commands
waitForChildren();
}
int main(int argc, char *argv[])
{
// Display prompt and wait for input
printf("myshell$");
// Get multi-command from standard input
string rawMultiCommand;
getline(cin, rawMultiCommand);
// Parse the multi-command into a vector of raw commands (strings)
vector<string> rawCommands = Helper::lex(rawMultiCommand, PIPE_DELIMITER);
try
{
runCommands(rawCommands);
}
catch (...)
{
cout << "An error has occured";
}
return 0;
}