-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
160 lines (148 loc) · 4.73 KB
/
Copy pathmain.cpp
File metadata and controls
160 lines (148 loc) · 4.73 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "engine/CuffEngine.h"
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <filesystem>
#include <cstdlib>
#include <cerrno>
namespace
{
void printUsage(const char *prog)
{
std::cerr << "Usage: " << prog << " [options] [script.cuff]\n"
<< " (no file) read the script from stdin\n"
<< " --ast print tokens + AST instead of running the script\n"
<< " --root <dir> allow 'use ... from' to load modules anywhere under <dir>\n"
<< " (default: the script's own directory)\n"
<< " --max-steps <n> stop after <n> loop iterations + function calls\n"
<< " --timeout <ms> stop after <ms> milliseconds of run time\n"
<< " --no-network disable 'use DLC:network' entirely\n"
<< " --allow-private-network\n"
<< " let DLC:network reach loopback/private/link-local addresses\n"
<< " --no-filesystem disable 'use DLC:filesystem' entirely\n";
}
// Reads at most limit+1 bytes so an oversized input is detected (and
// rejected by the engine) without ever being held in memory in full.
std::string readLimited(std::istream &in)
{
const size_t cap = cuff::limits::kMaxSourceBytes + 1;
std::string data;
char buf[1 << 16];
while (data.size() < cap && in.good())
{
in.read(buf, static_cast<std::streamsize>(std::min(sizeof buf, cap - data.size())));
data.append(buf, static_cast<size_t>(in.gcount()));
}
return data;
}
bool parseCount(const char *text, uint64_t &out)
{
if (!text || !*text)
return false;
char *end = nullptr;
errno = 0;
unsigned long long v = std::strtoull(text, &end, 10);
if (errno != 0 || *end != '\0' || text[0] == '-')
return false;
out = v;
return true;
}
}
int main(int argc, char *argv[])
{
std::ios::sync_with_stdio(false);
bool debugMode = false;
std::string path;
cuff::CuffEngine::Options options;
for (int i = 1; i < argc; ++i)
{
std::string arg = argv[i];
auto needValue = [&](const char *flag) -> const char *
{
if (i + 1 >= argc)
{
std::cerr << "Error: " << flag << " requires a value\n";
return nullptr;
}
return argv[++i];
};
if (arg == "--ast" || arg == "--tokens" || arg == "--debug")
{
debugMode = true;
}
else if (arg == "-h" || arg == "--help")
{
printUsage(argv[0]);
return 0;
}
else if (arg == "--root")
{
const char *v = needValue("--root");
if (!v)
return 2;
options.rootDir = v;
}
else if (arg == "--no-network")
{
options.networkEnabled = false;
}
else if (arg == "--allow-private-network")
{
options.allowPrivateNetworkTargets = true;
}
else if (arg == "--no-filesystem")
{
options.filesystemEnabled = false;
}
else if (arg == "--max-steps" || arg == "--timeout")
{
const char *v = needValue(arg.c_str());
uint64_t n = 0;
if (!v || !parseCount(v, n))
{
std::cerr << "Error: " << arg << " expects a non-negative integer\n";
return 2;
}
if (arg == "--max-steps")
options.maxSteps = n;
else
options.timeoutMs = static_cast<uint32_t>(std::min<uint64_t>(n, UINT32_MAX));
}
else
{
path = arg;
}
}
std::string source;
std::string scriptDir = ".";
if (!path.empty())
{
std::ifstream file(path);
if (!file)
{
std::cerr << "Error: Cannot open file '" << path << "'\n";
return 1;
}
source = readLimited(file);
std::filesystem::path p(path);
scriptDir = p.has_parent_path() ? p.parent_path().string() : ".";
}
else
{
source = readLimited(std::cin);
}
if (debugMode)
{
cuff::CuffEngine::Result result = cuff::CuffEngine::run(source);
cuff::CuffEngine::debugDump(result);
return result.success ? 0 : 1;
}
cuff::CuffEngine::Result result = cuff::CuffEngine::execute(source, scriptDir, options);
if (!result.success)
{
std::cerr << "ERROR: " << result.error << "\n";
return 1;
}
return 0;
}