-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
111 lines (90 loc) · 3.04 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
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
#include "include/built_in_commands.hpp"
#include "include/commands_manager.hpp"
#include "include/network.hpp"
#include "include/scripts_manager.hpp"
#include <algorithm>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <linux/limits.h>
#include <sstream>
static void trim(std::string &s) {
s.erase(s.begin(), std::find_if_not(s.begin(), s.end(),
[](char c) { return std::isspace(c); }));
s.erase(std::find_if_not(s.rbegin(), s.rend(),
[](char c) { return std::isspace(c); })
.base(),
s.end());
}
int getPort(int defaultPort) {
char *port = std::getenv("PC_REMOTE_PORT");
if (port)
return std::atoi(port);
else
return defaultPort;
}
std::string getScriptsPath(const char *defaultScriptsPath) {
char *path = nullptr, fullpath[PATH_MAX];
path = std::getenv("PC_REMOTE_SCRIPTS_PATH");
if (path != nullptr)
return std::string(path);
path = std::getenv("XDG_CONFIG_HOME");
if (path != nullptr)
goto find_realpath;
path = std::getenv("HOME");
if (path != nullptr)
goto find_realpath;
throw std::runtime_error("Please set PC_REMOTE_SCRIPTS_PATH or "
"XDG_CONFIG_HOME or HOME");
find_realpath:
realpath(path, fullpath);
std::string ret(fullpath);
return ret + "/remote_scripts/";
}
int main() {
Server server(getPort(5050));
ScriptsManager sm(getScriptsPath("~/.config/remote_scripts"));
CommandsManager cm;
std::cout << "loading scripts: " << sm.get_scripts_path() << std::endl;
sm.load_scripts();
cm.defineCommand("ls", "list all loaded scripts",
[&sm](std::ostringstream &output) {
BuiltInCommands::ls_command_action(output, sm);
});
cm.defineCommand("reload", "reload all scripts",
[&sm](std::ostringstream &output) {
BuiltInCommands::reload_command_action(output, sm);
});
cm.defineCommand("help", "list all commands",
[&cm](std::ostringstream &output) {
BuiltInCommands::help_command_action(output, cm);
});
std::cout << "server running on port: " << server.getPort() << std::endl;
int client_fd = server.accept();
Client &client = server.getClient(client_fd);
while (true) {
client.send("> ");
std::string script_name = client.recv();
trim(script_name);
if (script_name == "")
continue;
if (script_name == "q")
break;
if (cm.hasCommand(script_name)) {
std::ostringstream stream = cm.exec(script_name);
client.send(stream.str());
continue;
}
if (sm.has_script(script_name)) {
std::cout << "!> script: " << script_name << std::endl;
client.send("!> Executing script: " + script_name + '\n');
sm.run_script(script_name);
continue;
}
std::cout << "!> script not found: " << script_name << std::endl;
client.send("!> Script not found: " + script_name + '\n');
}
server.shutdown();
return 0;
}