-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathkidshell.cpp
144 lines (128 loc) · 3.56 KB
/
kidshell.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
#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include <sstream>
#include <vector>
#include <unistd.h>
#include <wait.h>
#include <cstring>
void error(std::string const& cause) {
std::cerr << "Error! Cause: " << cause << " - " << strerror(errno)
<< std::endl;
}
// ---- enviroment variables sector
static std::map<std::string, std::string> ENV;
std::vector<std::string> get_env_vars() {
std::vector<std::string> result;
std::transform(ENV.begin(), ENV.end(), std::back_inserter(result),
[](std::pair<std::string, std::string> const& p) {
return p.first + "=" + p.second;
});
return result;
}
void print_vars() {
for (std::string const& x : get_env_vars()) {
std::cout << x << std::endl;
}
}
void set_var(std::string const& settlement) {
for (size_t i = 0; i < settlement.size(); i++) {
if (settlement[i] == '=') {
std::string value = settlement.substr(i + 1);
if ((value.front() == '\'' || value.front() == '\"') &&
value.back() == value.front())
value = value.substr(1, value.length() - 2);
ENV[settlement.substr(0, i)] = value;
return;
}
}
std::cerr << "No \'=\' in variable set: " << settlement << std::endl;
}
void unset_var(std::string const& var_name) {
auto var_it = ENV.find(var_name);
if (var_it != ENV.end()) {
ENV.erase(var_it);
}
}
// ---- arguments and executing sector
std::vector<std::string> parse_args(std::string const& command) {
std::istringstream command_stream(command);
return std::vector<std::string>{
std::istream_iterator<std::string>(command_stream),
std::istream_iterator<std::string>()};
}
char* converter(std::string const& s) { return const_cast<char*>(s.data()); }
std::vector<char*> get_ptrs(std::vector<std::string>& args) {
std::vector<char*> result;
std::transform(args.begin(), args.end(), std::back_inserter(result),
converter);
return result;
}
void execute(char* argv[], char* envp[]) {
switch (auto pid = fork()) {
case -1:
error("Can not fork");
break;
case 0:
if (execve(argv[0], argv, envp) == -1) {
error("Execution failed");
exit(-1);
}
exit(0);
default:
int status;
if (waitpid(pid, &status, 0) == -1) {
error("Error in execution");
} else {
std::cout << "Executed. Return code: " << WEXITSTATUS(status)
<< std::endl;
}
}
}
void pre_execute(std::vector<std::string>& args) {
auto vars = get_env_vars();
auto prepared = get_ptrs(args);
auto enviroment = get_ptrs(vars);
prepared.push_back(nullptr);
enviroment.push_back(nullptr);
execute(prepared.data(), enviroment.data());
}
void run(std::string const& command) {
auto args = parse_args(command);
if (args.size() > 0) {
if (args[0] == "export") {
if (args.size() == 1) {
print_vars();
} else {
for (size_t i = 1; i < args.size(); i++) {
set_var(args[i]);
}
}
} else if (args[0] == "unset") {
if (args.size() < 2) {
std::cerr << "unset command usage : unset VARNAMES..." << std::endl;
} else {
for (size_t i = 1; i < args.size(); i++) {
unset_var(args[i]);
}
}
} else {
pre_execute(args);
}
} else {
pre_execute(args);
}
}
int main() {
while (true) {
std::cout << ">>> ";
std::cout.flush();
std::string command;
getline(std::cin, command);
if (std::cin.eof() || command == "exit") {
break;
}
run(command);
}
}