-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShell.h
More file actions
70 lines (53 loc) · 1.36 KB
/
Shell.h
File metadata and controls
70 lines (53 loc) · 1.36 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
#ifndef MYSHELL_SHELL_H
#define MYSHELL_SHELL_H
#include <map>
#include <string>
#include <stack>
#include <vector>
#include "Token.h"
#include <curses.h>
#include <algorithm>
#include <signal.h>
#include <iostream>
const std::string HISTORY_FILE = ".history";
class Shell {
public:
Shell();
explicit Shell(bool mode) : is_ncurses(mode) {};
~Shell();
void start();
void execute(const std::string &line);
template<typename... Args>
void print(const char *str, Args... args) {
if (is_ncurses) {
printw(str, args...);
} else {
printf(str, args...);
}
}
void print(const char *str) {
if (is_ncurses)
printw(str);
else
printf("%s", str);
}
static void kill_children() {
std::for_each(Shell::pids.begin(), Shell::pids.end(), [](int pid) {
kill(pid, SIGKILL);
});
}
static std::vector<int> pids;
private:
friend class Command;
void update_history();
std::map<std::string, std::string> local_variables;
std::stack<std::string> history;
std::string pwd;
ssize_t error_code = 0;
SCREEN *scr;
bool is_ncurses = true;
void get_env_vars(char **environ);
void replace_vars(std::vector<Token> &cmd);
};
std::vector<Token> parse(const std::string &line);
#endif //MYSHELL_SHELL_H