-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.cpp
More file actions
115 lines (91 loc) · 2.96 KB
/
shell.cpp
File metadata and controls
115 lines (91 loc) · 2.96 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
#include "shell.hpp"
#include "parser.hpp"
#include <iostream>
#include <unistd.h> // chdir, fork, execvp
#include <sys/wait.h> // waitpid
#include <cerrno>
#include <cstring> // strerror
void MiniShell::run() {
std::string line;
while (true) {
std::cout << "mini-shell> " << std::flush;
if (!std::getline(std::cin, line)) {
// EOF (Ctrl+D)
std::cout << "\n";
break;
}
line = trim(line);
if (line.empty()) continue;
std::vector<std::string> args = Parser::tokenize(line);
if (args.empty()) continue;
if (handleBuiltin(args)) continue;
runExternal(args);
}
}
bool MiniShell::handleBuiltin(const std::vector<std::string>& args) {
const std::string& cmd = args[0];
if (cmd == "exit") {
std::exit(0);
}
if (cmd == "help") {
std::cout
<< "Built-ins:\n"
<< " help Show this help\n"
<< " cd <dir> Change directory\n"
<< " exit Exit the shell\n"
<< "External commands are run via PATH (e.g., ls, pwd, echo).\n";
return true;
}
if (cmd == "cd") {
const char* target = nullptr;
if (args.size() < 2) {
// No dir provided -> go HOME
target = std::getenv("HOME");
if (!target) {
std::cerr << "cd: HOME not set\n";
return true;
}
} else {
target = args[1].c_str();
}
if (chdir(target) != 0) {
std::cerr << "cd: " << std::strerror(errno) << "\n";
}
return true;
}
return false;
}
void MiniShell::runExternal(const std::vector<std::string>& args) {
pid_t pid = fork();
if (pid < 0) {
std::cerr << "fork failed: " << std::strerror(errno) << "\n";
return;
}
if (pid == 0) {
// Child: build argv for execvp (needs char* array ending with nullptr)
std::vector<char*> argv;
argv.reserve(args.size() + 1);
for (const auto& s : args) {
// execvp wants mutable char*, but it will not modify the strings.
argv.push_back(const_cast<char*>(s.c_str()));
}
argv.push_back(nullptr);
execvp(argv[0], argv.data());
// If execvp returns, it's an error
std::cerr << argv[0] << ": " << std::strerror(errno) << "\n";
_exit(127);
} else {
// Parent: wait for child to finish
int status = 0;
if (waitpid(pid, &status, 0) < 0) {
std::cerr << "waitpid failed: " << std::strerror(errno) << "\n";
}
}
}
std::string MiniShell::trim(const std::string& s) {
size_t start = 0;
while (start < s.size() && std::isspace(static_cast<unsigned char>(s[start]))) start++;
size_t end = s.size();
while (end > start && std::isspace(static_cast<unsigned char>(s[end - 1]))) end--;
return s.substr(start, end - start);
}