-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
103 lines (83 loc) · 2.47 KB
/
Copy pathmain.cpp
File metadata and controls
103 lines (83 loc) · 2.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
#include <iostream>
#include <sstream>
#include <string>
#include <csignal>
#include <vector>
#include <readline/readline.h>
#include <readline/history.h>
#include "signal_handler.hpp"
#include "builtins.hpp"
#include "tokenizer.hpp"
#include "helper.hpp"
#include "parser.hpp"
int main() {
std::string line;
std::vector<std::string> tokens;
setup_signals();
// Load history from file on startup
History::load_from_file();
// Set history size limit to 1000 commands
stifle_history(1000);
// Load existing history into readline
const auto& hist = History::get_all();
for (const auto& cmd : hist) {
add_history(cmd.c_str());
}
while (true) {
char cwd[1024];
std::string prompt_str;
if (getcwd(cwd, sizeof(cwd)) != nullptr) {
std::string path(cwd);
std::string dir_name;
if (path == "/") {
dir_name = "/";
} else {
size_t last_slash = path.find_last_of('/');
if (last_slash != std::string::npos) {
dir_name = path.substr(last_slash + 1);
} else {
dir_name = path;
}
}
// Green color: \033[1;32m, Reset: \033[0m
// \001 and \002 are used to tell readline these are non-printing characters
prompt_str = "\001\033[1;32m\002" + dir_name + " > \001\033[0m\002";
} else {
prompt_str = "\001\033[1;32m\002> \001\033[0m\002";
}
// Use readline for input with arrow key support
char* input = readline(prompt_str.c_str());
// Handle Ctrl+D (EOF)
if (!input) {
std::cout << "\n";
break;
}
line = input;
free(input);
if(line.empty()) continue;
// Add to both readline history and our History namespace
add_history(line.c_str());
History::add(line);
tokens = tokenize(line);
if(tokens.empty()) continue;
// Execute the AST
try {
auto ast = parse(tokens);
if (ast) {
ast->execute();
}
} catch (const std::exception& e) {
std::cerr << "Parse/Exec Error: " << e.what() << "\n";
}
//std::cout << "Not a built-in command: " << tokens[0] << "\n";
// std::cout << "Tokens: [";
// for (size_t i = 0; i < tokens.size(); ++i) {
// std::cout << tokens[i];
// if (i < tokens.size() - 1) std::cout << ",";
// }
// std::cout << "]\n";
}
// Save history to file before exiting
History::save_to_file();
return 0;
}