forked from hazzus/os-kidshell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.cpp
128 lines (110 loc) · 3.35 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
//
// Created by Павел Пономарев on 2019-03-31.
//
#include "ShellHelper.h"
#include "ParseUtils.h"
#include <iostream>
#include <sstream>
#include <unistd.h>
using command = std::vector<std::string>;
static std::map<std::string, std::string> environment;
void printErrorMessage(std::string const& message) {
std::cerr << "Error occurred: " << message << std::endl;
}
void printEnvironmentalVars() {
for (auto const& x : environment) {
std::cout << x.first
<< '='
<< x.second
<< std::endl;
}
}
void printShell() {
std::cout << "Shell$ " << std::flush;
}
void setEnvironmentalVars(command& cmd) {
for (size_t i = 1; i < cmd.size(); ++i) {
auto varInfo = ParseUtils::parseEnvironmentalVar(cmd[i]);
if (varInfo.first.empty()) {
printErrorMessage(cmd[i] + " not a valid identifier");
break;
}
environment[varInfo.first] = varInfo.second;
}
}
void setDefaultEnvironmentalVars(char** envp) {
std::vector<std::string> cur;
for (int i = 0; *envp != nullptr; ++i) {
std::string str(*(envp++));
auto varInfo = ParseUtils::parseEnvironmentalVar(str);
environment[varInfo.first] = varInfo.second;
}
}
void unsetEnvironmentalVars(command& cmd) {
for (size_t i = 1; i < cmd.size(); ++i) {
environment.erase(cmd[i]);
}
}
void execute(command& cmd) {
int status;
pid_t pid;
pid = fork();
if (pid == -1) {
printErrorMessage("fork failed");
}
if (pid == 0) {
// We are in a child
std::vector<std::string> paths = ParseUtils::parsePath(environment["PATH"]);
std::string execCommand = ShellHelper::getCommand(paths, cmd[0]);
std::vector<char*> arguments = ShellHelper::getCharVector(cmd);
std::vector<std::string> tempEnv = ShellHelper::getEnvironmentVector(environment);
std::vector<char*> env = ShellHelper::getCharVector(tempEnv);
if (execve(execCommand.c_str(), arguments.data(), env.data()) == -1) {
printErrorMessage("execution failed");
exit(-1);
}
} else {
// We are in a parent
if (waitpid(pid, &status, 0) == -1) {
printErrorMessage("error while executing");
}
}
}
void process() {
std::string str;
printShell();
while (std::getline(std::cin, str)) {
command command = ParseUtils::splitString(str);
if (command.empty()) {
printShell();
continue;
}
if (command[0] == "exit") {
break;
}
if (command[0] == "export") {
if (command.size() == 1 || (command.size() >= 2 && command[1] == "-p")) {
printEnvironmentalVars();
} else {
setEnvironmentalVars(command);
}
printShell();
continue;
}
if (command[0] == "unset") {
if (command.size() < 2) {
printErrorMessage("illegal number of arguments. Use unset <var_name> ...");
} else {
unsetEnvironmentalVars(command);
}
printShell();
continue;
}
execute(command);
printShell();
}
}
int main(int argc, char* argv[], char* envp[]) {
setDefaultEnvironmentalVars(envp);
process();
}