-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
81 lines (65 loc) · 2.04 KB
/
main.cpp
File metadata and controls
81 lines (65 loc) · 2.04 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
#include "parsetree/ExecNode.cpp"
#include "parsetree/BackgroundNode.cpp"
#include "parsetree/SeparatorNode.cpp"
#include "parsetree/PipeNode.cpp"
#include "util/parser.cpp"
#include "config.h"
#include "util/Logger.h"
#include<string>
#include<sstream>
#include<vector>
#include<stdlib.h>
#include<iostream>
#include<unistd.h>
#include <sys/types.h>
#include<sys/wait.h>
#include<fstream>
#include <thread>
#include <chrono>
using namespace std;
int main() {
Config* config = Config::get_instance();
Logger *logger = Logger::get_instance();
config->debug_mode = true;
config->debug_color_enabled = false;
ofstream log_file_stream("log.txt");
logger->set_output_stream(&log_file_stream);
logger->log("Shell Process started!");
string input;
int pid;
while (true) {
//* print shell prompt
cout << config->prompt_color_code
<< "[myshell:"
<< config->prompt_cwd_color_code
<< SystemCallWrapper::getcwd_wrapper()
<< config->prompt_color_code
<< "]$ "
<< Config::PROMPT_COLOR_CODE[Config::PROMPT_COLOR::DEFAULT];
cout.flush();
getline(cin, input);
if (input == "exit")
exit(0);
try {
//* parse command
ParseTree parse_tree = Parser::parse(input);
if (config->debug_mode == true) {
cout << config->debug_color << "[DEBUG]: parse tree: ";
parse_tree.print();
cout << Config::PROMPT_COLOR_CODE[Config::PROMPT_COLOR::DEFAULT] << endl;
}
pid = fork();
if (pid == 0) {
parse_tree.run();
}
// //* prevent memory leak
// delete command;
int status;
waitpid(pid, &status, 0);
logger->log("All child process finished");
} catch (const runtime_error &error) {
cout << "[Syntax Error]: " << error.what() << endl;
}
}
}
// ls -l|grep out >> test_output.txt