-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
83 lines (73 loc) · 2.26 KB
/
main.cpp
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
#include "crash.h"
#include "command.h"
#include <iostream>
#include <string>
#include <readline/readline.h>
#include <readline/history.h>
#include <cstdio>
#include <signal.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sstream>
#include <fstream>
#include <map>
using namespace std;
#define DEFAULT_SHELL ".myshell"
extern char ** environ;
bool fileExists (const string& name) {
return ( access( name.c_str(), F_OK ) != -1 );
}
// Adapted from stackoverflow:
// https://stackoverflow.com/questions/7868936/read-file-line-by-line
void addCmdsFromFile(string filename, string prompt, map<string, string> * aliases) {
ifstream infile(filename);
string line;
vector<string> commandArgs;
while (getline(infile, line)) {
commandArgs = command::split(line, ' ');
crash * cr = new crash(line, prompt, environ, aliases);
cr->parseArgs(commandArgs);
}
}
void run(struct sigaction sigIntHandler) {
char * buf;
string prompt = "Enter a command: > ";
map<string, string> * myAliasMap = new map<string, string>;
if (fileExists(DEFAULT_SHELL)) {
addCmdsFromFile(DEFAULT_SHELL, prompt, myAliasMap);
}
while((buf = readline(prompt.c_str())) != NULL) {
sigaction(SIGINT, &sigIntHandler, NULL);
string s(buf);
crash * cr = new crash(buf, prompt, environ, myAliasMap);
if (buf[0] != 0) {
add_history(buf);
}
vector<string> splitArgs = command::split(buf, ' ');
//We are executing commands from a file
int executableFilePosition = splitArgs[0].find("./");
if (executableFilePosition != -1) {
addCmdsFromFile(splitArgs[0].substr(executableFilePosition), prompt, myAliasMap);
}
else {
cr->parseArgs(splitArgs);
}
myAliasMap = cr->aliasMap;
prompt = cr->prompt;
}
}
void handleSignal(int signal) {
cout << "\nHEY! You interrupted me with this signal: " << signal << endl;
}
// Signal handling adapted from stackoverflow:
// https://stackoverflow.com/questions/1641182/how-can-i-catch-a-ctrl-c-event-c
// https://stackoverflow.com/questions/17766550/ctrl-c-interrupt-event-handling-in-linux
int main (int argc, char * argv[]){
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = handleSignal;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
run(sigIntHandler);
}