-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltins.cpp
More file actions
175 lines (152 loc) · 4.25 KB
/
Copy pathbuiltins.cpp
File metadata and controls
175 lines (152 loc) · 4.25 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "builtins.hpp"
#include <cstdlib>
#include <cstring>
#include <string>
#include <unistd.h>
#include <unordered_map>
#include <cctype>
#include <vector>
#include <iostream>
#include <stdlib.h>
#include "helper.hpp"
// Internal environment map
static std::unordered_map<std::string, std::string> shell_vars;
static std::unordered_map<std::string, std::string> alias_map;
// Dispatcher
BuiltinFunc handle_builtin(const std::vector<std::string> &input) {
static std::unordered_map<std::string, BuiltinFunc> builtins ={
{"exit", builtin_exit},
{"cd", builtin_cd},
{"echo", builtin_echo},
{"pwd", builtin_pwd},
{"export", builtin_export},
{"alias", builtin_alias},
{"unalias", builtin_unalias},
{"history", builtin_history},
};
if (input.empty())
return nullptr;
auto it = builtins.find(input[0]);
if (it != builtins.end()) {
it->second(input);
return it->second;
}
return nullptr;
}
bool builtin_exit(const std::vector<std::string> &input) {
if(input.empty() || input[0] != "exit") return false;
History::save_to_file();
exit(0);
}
bool builtin_cd(const std::vector<std::string> &input) {
if(input.empty() || input[0] != "cd") return false;
if (input.size() < 2) {
const char* home = getenv("HOME");
if(home != nullptr){
chdir(home);
}else{
std::cerr << "cd: Couldn't find home directory\n";
}
return true;
}
std::string path = input[1];
// Expand tilde to home directory
if (path[0] == '~') {
const char* home = getenv("HOME");
if (home != nullptr) {
if (path.length() == 1) {
path = home;
} else if (path[1] == '/') {
path = std::string(home) + path.substr(1);
}
} else {
std::cerr << "cd: HOME not set\n";
return true;
}
}
if (chdir(path.c_str()) != 0) {
std::cerr << "cd: " << strerror(errno) << "\n";
}
return true;
}
bool builtin_pwd(const std::vector<std::string> &input){
if(input[0] != "pwd") return false;
char curr_dir[1024];
if (getcwd(curr_dir, sizeof(curr_dir)) != nullptr) {
std::cout << curr_dir << "\n";
return true;
} else {
return false;
}
}
bool builtin_echo(const std::vector<std::string> &input) {
if(input.size() < 1) return false;
for (size_t i = 1; i < input.size(); i++) {
std::cout << input[i] << " ";
}
std::cout << "\n";
return true;
}
bool builtin_export(const std::vector<std::string> &input){
if (input.size() < 2) {
return false;
}
std::vector<std::string> key_val = extract_key_value(input[1]);
if (key_val.size() != 2) {
std::cerr << "can't find key value pair.\n";
return false;
}
std::string key = key_val[0];
std::string value = key_val[1];
shell_vars[key] = value;
if (setenv(key.c_str(), value.c_str(), 1) != 0) {
std::cerr << "setenv failed.\n ";
return false;
}
return true;
}
bool builtin_alias(const std::vector<std::string> &input) {
if (input.size() < 2) {
std::cerr << "alias: missing destination\n";
return false;
}
std::vector<std::string> key_val = extract_key_value(input[1]);
if (key_val.size() != 2) {
std::cerr << "can't find key value pair.\n";
return false;
}
std::string key = key_val[0];
std::string value = key_val[1];
alias_map[key] = value;
return true;
}
bool builtin_unalias(const std::vector<std::string> &input) {
if (input.size() < 2) {
std::cerr << "unalias: missing destination\n";
return false;
}
std::string key = input[1];
alias_map.erase(key);
return true;
}
bool builtin_history(const std::vector<std::string> &input){
if(input.empty()) return false;
if(input.size() == 1){
History::print();
return true;
}
bool is_numeric = true;
for(char c : input[1]) {
if(!isdigit(c)) {
is_numeric = false;
break;
}
}
if(is_numeric){
int num = std::stoi(input[1]);
History::print(num);
} else {
std::cerr << "history: numeric argument required\n";
}
return true;
}