-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmini-shell.c
More file actions
145 lines (110 loc) · 3.26 KB
/
mini-shell.c
File metadata and controls
145 lines (110 loc) · 3.26 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h> /* pid_t */
#include <sys/wait.h> /* waitpid */
#include <unistd.h> /* exit, fork */
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
std::vector<std::string> explode(std::string const & s, char delim)
{
std::vector<std::string> result;
std::istringstream iss(s);
for ( std::string token; std::getline(iss, token, delim); )
result.push_back(std::move(token));
return result;
}
void error_handler(char *e)
{
perror(e);
exit(EXIT_FAILURE);
}
std::vector<int> find_pipes( std::vector<std::string> ins )
{
std::vector<int> pipe_pos;
pipe_pos.push_back(-1);
for (uint i = 0; i < ins.size(); ++i)
if (ins.at(i) == "|") pipe_pos.push_back(i);
pipe_pos.push_back(ins.size());
return pipe_pos;
}
int run(std::string in)
{
std::vector<std::string> ins = explode(in,' ');
int pipes = std::count(ins.begin(), ins.end(), "|");
std::vector<int> pipe_pos = find_pipes(ins);
char* args[ins.size() + 1];
// REFERENCE ARRAY[i] TO EACH STRING
for (uint i = 0; i < ins.size(); ++i)
args[i] = &(ins[i][0]);
args[ins.size()] = '\0';
int pipeline[pipes+2][2]; // PIPELINE[0] NOT CURRENTLY IN USE :P
for (int i = 0; i < pipes+2; ++i)
{
if ( pipe(pipeline[i]) == -1 )
error_handler("at pipe()");
}
for (int i = 1; i < pipes+2; ++i)
{
pid_t pid = fork();
if ( pid == -1 )
error_handler("at fork()");
if ( pid == 0 )
{
// CLOSE PIPES THAT DON'T BELONG
for (int j = 0; j < pipes+2; ++j)
{
if (i!=j)
{
if ( close(pipeline[j][0]) == -1 )
error_handler("at close()");
if ( close(pipeline[(j+1)%(pipes+2)][1]) )
error_handler("at close()");
}
}
dup2(pipeline[i][0],0); // REFERENCE STDIN TO READ END OF PIPE
if (i!=pipes+1)
dup2(pipeline[(i+1)][1],1); // REFERENCE STDOUT TO WRITE END OF NEXT PROCESS'S PIPE
char* buf[pipe_pos[i]-pipe_pos[i-1]]; // ARRAY OF THE SIZE OF COMMAND + ARGS + \0
memcpy(buf , &args[pipe_pos[i-1]+1] , 8*(pipe_pos[i]-pipe_pos[i-1]+1)); // MAGIA
buf[pipe_pos[i]-pipe_pos[i-1]-1] = '\0';
if ( execvp(buf[0], buf) == -1 ){
std::cout << "\n";
error_handler(*buf);
}
}
}
// CLOSE PARENT'S PIPES
for (int j = 0; j < pipes+2; ++j)
{
if ( close(pipeline[j][0]) == -1 )
error_handler("at close()");
if ( close(pipeline[(j+1)%(pipes+2)][1]) )
error_handler("at close()");
}
// WAIT FOR ALL MY CHILDREN
for (int i = 1; i < pipes+2; ++i)
wait(NULL); // TODO: waitpid
return 0;
}
std::string prompt()
{
std::string in("");
while (1)
{
while ( in == "\0" )
{
std::cout << "[Mini-shell]$ ";
std::getline(std::cin,in);
}
run(in);
in = "";
}
}
int main(int argc, char* argv[])
{
return run(prompt());
}