Repository navigation
Expand file tree
/
Copy pathcommand_script.h
More file actions
75 lines (75 loc) · 3.45 KB
/
Copy pathcommand_script.h
File metadata and controls
75 lines (75 loc) · 3.45 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
#ifndef DLUA_COMMAND_SCRIPT_H
#define DLUA_COMMAND_SCRIPT_H
#include "common.h"
#include <fstream>
// Command files contain debugger commands, never implicitly evaluated Lua.
inline std::string script_trim(const std::string &s) {
size_t a = s.find_first_not_of(" \t\r\n");
if (a == std::string::npos) return "";
return s.substr(a, s.find_last_not_of(" \t\r\n") - a + 1);
}
inline bool expand_script_command(const std::string &raw, std::vector<std::string> &out,
std::string &error, const std::string &base = ".", int depth = 0) {
std::string line = script_trim(raw);
if (line.empty() || line[0] == '#') return true;
if (out.size() >= 10000 || line.size() >= QUEUED_MESSAGE_MSG_LEN) {
error = "script limit exceeded (10000 commands, 1022 bytes per command)"; return false;
}
std::istringstream words(line);
std::string verb; words >> verb;
if (verb != "source") { out.push_back(line); return true; }
if (depth >= 8) { error = "source nesting limit exceeded (8)"; return false; }
std::string path = script_trim(line.substr(verb.size()));
if (path.empty()) { error = "source requires a filename"; return false; }
if (path[0] != '/') path = base + "/" + path;
std::ifstream file(path);
if (!file) { error = "cannot open command file: " + path; return false; }
while (std::getline(file, line)) {
if (!expand_script_command(line, out, error, dirname_of_path(path), depth + 1)) return false;
}
if (file.bad()) { error = "cannot read command file: " + path; return false; }
return true;
}
struct ScriptOptions {
int pid = 0;
bool batch = false;
bool silent = false;
bool help = false;
bool doctor = false;
bool version = false;
std::vector<std::string> commands;
};
inline bool parse_script_options(int argc, char **argv, ScriptOptions &o, std::string &error) {
for (int i = 1; i < argc; ++i) {
std::string a = argv[i], value;
if (a == "--doctor") { o.doctor = true; continue; }
if (a == "--version") { o.version = true; continue; }
if (a == "--help" || a == "-h") { o.help = true; continue; }
if (a == "--batch" || a == "-batch") { o.batch = true; continue; }
if (a == "--batch-silent") { o.batch = o.silent = true; continue; }
size_t eq = a.find('=');
bool has_value = eq != std::string::npos;
if (has_value) { value = a.substr(eq + 1); a = a.substr(0, eq); }
if (a == "-p" || a == "--pid" || a == "-x" || a == "--command" ||
a == "-ex" || a == "--eval-command") {
if (!has_value) {
if (++i >= argc) { error = "missing value for " + a; return false; }
value = argv[i];
}
if (a == "-p" || a == "--pid") a = value;
else {
if (a == "-x" || a == "--command") value = "source " + value;
if (!expand_script_command(value, o.commands, error)) return false;
continue;
}
} else if (has_value) { error = "unknown option: " + a; return false; }
if (o.pid != 0 || a.empty() || a.size() > 9 ||
a.find_first_not_of("0123456789") != std::string::npos || atoi(a.c_str()) <= 0) {
error = "invalid option or pid: " + a; return false;
}
o.pid = atoi(a.c_str());
}
if (!o.help && !o.doctor && !o.version && !o.pid) { error = "a positive target PID is required"; return false; }
return true;
}
#endif