-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
279 lines (233 loc) · 7.47 KB
/
utils.cpp
File metadata and controls
279 lines (233 loc) · 7.47 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "utils.h"
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <stack>
#include <stdio.h>
#include <windows.h>
#ifdef _WIN32
char file_sep = ';';
#include <direct.h>
#else
char file_sep = ':';
#include <sys/wait.h>
#include <unistd.h>
#endif
namespace fs = std::filesystem;
int MAX_HISTORY_LIMIT = 20;
Token::Token(std::string arg, int quote_type) {
// arg stores the current argument
this->value = arg;
// has_space is used to check if the current argument is followed by a space
this->has_space = false;
// quote_type is used to check if the current argument is quoted or not
this->quote_type = quote_type;
}
void Token::set_has_space() { this->has_space = true; }
Tokenizer::Tokenizer(std::string input) {
this->input = input;
this->count = 0;
this->generic_tokens = tokenize();
}
std::string Token::get_without_quotes() {
if (this->quote_type == 0)
return this->value;
return this->value.substr(1, this->value.size() - 2);
}
void Tokenizer::add_token(std::string &curr_arg, std::vector<Token> &tokens,
int quote_type) {
if (curr_arg.empty())
return;
Token token = Token(curr_arg, quote_type);
tokens.push_back(token);
curr_arg.clear();
}
std::vector<Token> Tokenizer::tokenize(bool escape_backslashes) {
int n = this->input.size();
std::string curr_arg = "";
bool in_double_q = false, in_single_q = false;
std::vector<Token> tokens;
int i = 0;
while (i < n && input[i] == ' ')
i++;
for (i; i < n; i++) {
if (!in_single_q && input[i] == '\\' && i == n - 1 &&
escape_backslashes)
throw new std::exception(
"Error: Expected escape character after \\");
if (in_single_q) {
curr_arg += input[i];
if (input[i] == '\'') {
add_token(curr_arg, tokens, 1);
in_single_q = false;
}
} else if (in_double_q) {
if (input[i] == '\\' && escape_backslashes) {
curr_arg += input[++i];
continue;
}
curr_arg += input[i];
if (input[i] == '"') {
add_token(curr_arg, tokens, 2);
in_double_q = false;
}
} else {
if (input[i] == ' ') {
add_token(curr_arg, tokens);
tokens.back().set_has_space();
} else if (input[i] == '\\') {
curr_arg += input[++i];
} else {
if (input[i] == '\'') {
in_single_q = true;
add_token(curr_arg, tokens);
} else if (input[i] == '"') {
in_double_q = true;
add_token(curr_arg, tokens);
}
curr_arg += input[i];
}
}
}
if (curr_arg != "")
add_token(curr_arg, tokens);
this->command = tokens[0];
tokens.erase(tokens.begin());
return tokens;
}
std::vector<Token> Tokenizer::get_tokens() { return generic_tokens; }
std::string Tokenizer::concat_args(bool add_space = false) {
std::string res = "";
for (auto tk : this->generic_tokens) {
res += tk.value;
if (add_space)
res += (tk.has_space ? " " : "");
}
return res;
}
std::vector<Token> Tokenizer::get_cat_args() { return tokenize(false); }
std::vector<std::string> split_args(std::string str, char delimiter) {
std::stringstream ss(str);
std::string item = "";
std::vector<std::string> tokens;
while (getline(ss, item, delimiter)) {
tokens.push_back(item);
}
return tokens;
}
bool is_shell_builtin(std::string str) {
std::string builtin_commands[] = {"pwd", "cd", "type", "exit", "echo"};
for (std::string command : builtin_commands) {
if (command == str)
return true;
}
return false;
}
std::string get_file_path(char *directory_paths, std::string filename) {
try {
std::vector<std::string> extensions = {".exe", ".sh", ".bat", ".cmd"};
std::vector<std::string> paths =
split_args(std::string(directory_paths), file_sep);
for (auto &path : paths) {
if (!fs::exists(path))
continue;
fs::path prog_path = fs::path(path) / filename;
for (auto &ext : extensions) {
if (fs::exists(prog_path.string() + ext)) {
return prog_path.lexically_normal().string() + ext;
}
}
}
} catch (const std::exception &ex) {
std::cerr << "Exception in get_file_path: " << ex.what() << std::endl;
}
return "";
}
std::string join(std::vector<Token> args, std::string sep) {
std::string res = "";
int n = args.size();
if (n == 0)
return res;
for (int i = 0; i < n - 1; i++) {
res += args[i].value + sep;
}
res += args[n - 1].value;
return res;
}
void print_cmd_type(std::string command, char *directory_paths) {
try {
bool is_builtin = is_shell_builtin(command);
if (is_builtin) {
std::cout << command << " is a shell builtin" << std::endl;
return;
}
std::string file_path = get_file_path(directory_paths, command);
if (file_path == "")
std::cout << command << ": not found" << std::endl;
else
std::cout << command << " is " << file_path << std::endl;
} catch (const std::exception &e) {
std::cerr << e.what() << std::endl;
}
}
/**
* Custom cat command that handles quoted and unquoted file paths
* Created this because cat command only works on linux/unix based systems
*/
void custom_cat_cmd(std::vector<Token> args) {
for (auto file_path : args) {
try {
if (file_path.value == " " || file_path.value == "")
continue;
std::string path = file_path.get_without_quotes();
std::ifstream file;
file.open(path);
if (!file.is_open()) {
std::cerr << "Error opening file : " << path << std::endl;
continue;
}
std::cout << file.rdbuf();
file.close();
} catch (const std::exception &e) {
std::cerr << "Exception in custom_cat_cmd: " << e.what()
<< std::endl;
}
}
}
std::string process_exec_input(std::string cmd, std::vector<Token> arguments) {
std::string p_input = cmd;
if (cmd.find(' ') != std::string::npos) {
if (cmd.find('"') != std::string::npos)
p_input = "'" + cmd + "'";
else
p_input = "\"" + cmd + "\"";
}
if (arguments.size() > 0)
p_input += " " + join(arguments, " ");
return p_input;
}
char *get_home_directory() {
#ifdef _WIN32
return getenv("USERPROFILE");
#else
return getenv("HOME");
#endif
}
void add_to_history(std::string command, std::vector<std::string> &history) {
if (history.size() >= MAX_HISTORY_LIMIT) {
history.erase(history.begin());
}
history.push_back(command);
}
void print_history(std::vector<std::string> history) {
for (int i = 0; i < history.size(); i++) {
std::cout << " " << i + 1 << " " << history[i] << std::endl;
}
}
void clear_screen() {
std::cout << "\033[2J"; // Clear entire screen
std::cout << "\033[3J"; // Clear scrollback buffer
std::cout << "\033[H"; // Move cursor to home position
std::cout << std::flush;
}