-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
73 lines (63 loc) · 1.85 KB
/
main.cpp
File metadata and controls
73 lines (63 loc) · 1.85 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
#include <string>
#include <iostream>
#include <unistd.h>
#include <fstream>
#include <cstring>
#include "Global.h"
#include "executor.h"
#include "repl.h"
#include "job_control.h"
static void initialize_variables() {
// "In a subshell, '$' shall expand to the same value as that of the current shell."
g.variables["$"] = std::to_string(getpid());
}
static void load_kishrc() {
std::string home;
if(const char *c_home = getenv("HOME")) {
home = c_home;
} else {
return;
}
// TODO: make this efficiant
// TODO: don't save the whole file at all
std::string lines;
std::ifstream f(home + "/.kishrc");
std::string line;
while(std::getline(f, line)) {
lines.append(line);
lines.append("\n");
}
executor::run_from_string(lines);
}
static void usage(const char *ownName) {
std::cerr << "Usage: " << ownName << "\n"
<< " or: " << ownName << " -c <command>\n"
<< " or: " << ownName << " <scriptfile>\n";
}
int main(int argc, char *argv[]) {
// The replxx library uses C stdio - do this to not get confused
std::ios::sync_with_stdio();
initialize_variables();
job_control::init_interactive_shell();
if(argc == 1) {
load_kishrc();
repl::run();
} else if(argc == 2 && argv[1][0] != '-') {
// TODO: make this efficiant
// TODO: don't save the whole file at all
std::string lines;
std::ifstream f(argv[1]);
std::string line;
while(std::getline(f, line)) {
lines.append(line);
lines.append("\n");
}
executor::run_from_string(lines);
} else if(argc == 3 && strcmp(argv[1], "-c") == 0) {
executor::run_from_string(argv[2]);
} else {
usage(argc > 0 ? argv[0] : "kish");
return 1;
}
return g.last_return_value;
}