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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
/*
* CommandParser.cpp
*
* Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "CommandParser.h"
#include "InformationManager.h"
#include "Requests/DaemonFSInfoRequest.h"
#include "Requests/DaemonCommandRequest.h"
#include "Requests/DaemonStatusRequest.h"
#include <Common/Exception.h>
#include <Common/Logger.h>
#include <Common/RequestManager.h>
#include <Common/Requests/FSInfoRequest.h>
#include <Common/Requests/DisconnectRequest.h>
#include <Common/Requests/StatusRequest.h>
#include <Common/Tokenizer.h>
#include <Net/Packets/HostListPacket.h>
#include <Net/Packets/HostStatusPacket.h>
#include <iostream>
#include <cstdio>
#include <sigc++/bind.h>
namespace Mad {
namespace Client {
const CommandParser::Command CommandParser::commands[] = {
{{"fsinfo", 0}, "fsinfo [host]", "Displays file system usage information", "Displays file system usage information of a host or the server (if no host is given).", &CommandParser::fsinfoCommand},
{{"help", "?", 0}, "help [command]", "Displays usage information about commands", "Displays usage information about a command. If no command is given, a list of all available commands is displayed.", &CommandParser::helpCommand},
{{"list_hosts", "hosts", 0}, "list_hosts [-a]", "Lists the currently active hosts", "Lists the currently active hosts.\n\n -a\tAlso list inactive hosts", &CommandParser::listHostsCommand},
{{"reboot", 0}, "reboot *|host...", "Reboots host", "Reboots hosts. * will reboot all hosts.", &CommandParser::rebootCommand},
{{"shutdown", "halt", 0}, "shutdown *|host...", "Shuts hosts down", "Shuts hosts down. * will shut down all hosts.", &CommandParser::shutdownCommand},
{{"status", "st", 0}, "status [host]", "Displays status information", "Displays host status information. If no host is given, server status information is displayed.", &CommandParser::statusCommand},
{{"exit", "quit", 0}, "exit", "Closes the connection and quits the client", "Closes the connection and quits the client.", &CommandParser::exitCommand},
{{0}, 0, 0, 0, 0}
};
CommandParser CommandParser::commandParser;
const CommandParser::Command* CommandParser::findCommand(const std::string& command) {
for(int i = 0; commands[i].commands[0] != 0; ++i) {
for(int j = 0; commands[i].commands[j] != 0; ++j) {
if(command == commands[i].commands[j]) {
return &commands[i];
}
}
}
return 0;
}
void CommandParser::printUsage(const std::string& command) {
const CommandParser::Command *cmd = findCommand(command);
if(cmd)
Common::Logger::logf("Usage: %s\n", cmd->cmdline);
}
std::map<std::string, Common::HostInfo> CommandParser::parseHostList(const std::vector<std::string> &args, bool mustBeActive) {
const std::map<std::string, Common::HostInfo>& hosts = InformationManager::get()->getDaemons();
std::map<std::string, Common::HostInfo> ret;
for(std::vector<std::string>::const_iterator arg = args.begin(); arg != args.end(); ++arg) {
if(*arg == "*") {
if(mustBeActive) {
for(std::map<std::string, Common::HostInfo>::const_iterator host = hosts.begin(); host != hosts.end(); ++host) {
if(host->second.getState() != Common::HostInfo::INACTIVE)
ret.insert(*host);
}
if(ret.empty())
Common::Logger::log("No hosts active.");
}
else {
ret = hosts;
}
}
else {
std::map<std::string, Common::HostInfo>::const_iterator host = hosts.find(*arg);
if(host == hosts.end())
Common::Logger::logf(Common::Logger::ERROR, "Host '%s' doesn't exist.", arg->c_str());
else if(mustBeActive && host->second.getState() == Common::HostInfo::INACTIVE)
Common::Logger::logf(Common::Logger::WARNING, "Host '%s' is inactive.", arg->c_str());
else
ret.insert(*host);
}
}
return ret;
}
void CommandParser::fsinfoCommand(const std::vector<std::string> &args) {
if(args.size() == 1)
Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Common::Requests::FSInfoRequest(sigc::mem_fun(CommandManager::get(), &CommandManager::fsInfoRequestFinished))));
else if(args.size() == 2)
Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Requests::DaemonFSInfoRequest(args[1], sigc::mem_fun(CommandManager::get(), &CommandManager::daemonFSInfoRequestFinished))));
else {
Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str());
printUsage("fsinfo");
return;
}
++CommandManager::get()->activeRequests;
}
void CommandParser::helpCommand(const std::vector<std::string> &args) {
if(args.size() == 1) {
std::cout << "Available commands:" << std::endl << std::endl;
for(int i = 0; commands[i].commands[0] != 0; ++i) {
std::cout << commands[i].commands[0];
for(int j = 1; commands[i].commands[j] != 0; ++j)
std::cout << ", " << commands[i].commands[j];
std::cout << std::endl << "\t" << commands[i].desc << std::endl;
}
}
else if(args.size() == 2) {
const Command* command = findCommand(args[1]);
if(command) {
std::cout << "Usage: " << command->cmdline << std::endl << std::endl;
std::cout << command->longdesc << std::endl << std::endl;
}
else
Common::Logger::logf(Common::Logger::WARNING, "%s: Command '%s' doesn't exist.", args[0].c_str(), args[1].c_str());
}
else {
Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str());
printUsage("help");
}
}
void CommandParser::listHostsCommand(const std::vector<std::string> &args) {
const std::map<std::string, Common::HostInfo>& hosts = InformationManager::get()->getDaemons();
if(args.size() == 1) {
if(hosts.empty()) {
std::cout << "The host list is empty." << std::endl << std::endl;
return;
}
bool output = false;
for(std::map<std::string, Common::HostInfo>::const_iterator host = hosts.begin(); host != hosts.end(); ++host) {
if(host->second.getState() == Common::HostInfo::INACTIVE)
continue;
if(!output) {
std::cout << "Active hosts:" << std::endl;
output = true;
}
std::cout << " " << host->first << std::endl;
}
if(!output)
std::cout << "No active hosts." << std::endl;
std::cout << std::endl;
}
else if(args.size() > 2) {
Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str());
printUsage("list_hosts");
}
else if(args[1] == "-a") {
if(hosts.empty()) {
std::cout << "The host list is empty." << std::endl << std::endl;
return;
}
std::cout << "Host list:" << std::endl;
for(std::map<std::string, Common::HostInfo>::const_iterator host = hosts.begin(); host != hosts.end(); ++host) {
std::cout << " " << host->first << " (" << (host->second.getState() == Common::HostInfo::RUNNING ? "running" : "inactive") << ")" << std::endl;
}
std::cout << std::endl;
}
else {
Common::Logger::logf(Common::Logger::ERROR, "%s: Don't understand argument '%s'.", args[0].c_str(), args[1].c_str());
printUsage("list_hosts");
}
}
void CommandParser::rebootCommand(const std::vector<std::string> &args) {
if(args.size() < 2) {
Common::Logger::logf(Common::Logger::ERROR, "%s: No host given.", args[0].c_str());
printUsage("reboot");
return;
}
std::map<std::string, Common::HostInfo> hosts = parseHostList(std::vector<std::string>(args.begin()+1, args.end()), true);
for(std::map<std::string, Common::HostInfo>::iterator host = hosts.begin(); host != hosts.end(); ++host) {
Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(
new Requests::DaemonCommandRequest(host->first, true, sigc::mem_fun(CommandManager::get(), &CommandManager::daemonCommandRequestFinished))
));
++CommandManager::get()->activeRequests;
}
}
void CommandParser::shutdownCommand(const std::vector<std::string> &args) {
if(args.size() < 2) {
Common::Logger::logf(Common::Logger::ERROR, "%s: No host given.", args[0].c_str());
printUsage("shutdown");
return;
}
std::map<std::string, Common::HostInfo> hosts = parseHostList(std::vector<std::string>(args.begin()+1, args.end()), true);
for(std::map<std::string, Common::HostInfo>::iterator host = hosts.begin(); host != hosts.end(); ++host) {
Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(
new Requests::DaemonCommandRequest(host->first, false, sigc::mem_fun(CommandManager::get(), &CommandManager::daemonCommandRequestFinished))
));
++CommandManager::get()->activeRequests;
}
}
void CommandParser::statusCommand(const std::vector<std::string> &args) {
if(args.size() == 1)
Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Common::Requests::StatusRequest(sigc::mem_fun(CommandManager::get(), &CommandManager::statusRequestFinished))));
else if(args.size() == 2)
Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Requests::DaemonStatusRequest(args[1], sigc::mem_fun(CommandManager::get(), &CommandManager::daemonStatusRequestFinished))));
else {
Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str());
printUsage("status");
return;
}
++CommandManager::get()->activeRequests;
}
void CommandParser::exitCommand(const std::vector<std::string>&) {
++CommandManager::get()->activeRequests;
Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Common::Requests::DisconnectRequest(sigc::mem_fun(CommandManager::get(), &CommandManager::disconnectRequestFinished))));
}
bool CommandParser::split(const std::string &str, std::vector<std::string> &ret) {
std::string temp;
bool quoteSingle = false, quoteDouble = false, escape = false;
size_t beg = 0;
for(size_t cur = 0; cur < str.length(); ++cur) {
if(!escape) {
if(str[cur] == ' ' && !quoteSingle && !quoteDouble) {
if(cur == beg && temp.empty()) {
++beg;
}
else {
temp += str.substr(beg, cur-beg);
ret.push_back(temp);
temp.clear();
beg = cur+1;
}
continue;
}
if(str[cur] == '"' && !quoteSingle) {
temp += str.substr(beg, cur-beg);
beg = cur+1;
quoteDouble = !quoteDouble;
continue;
}
if(str[cur] == '\'' && !quoteDouble) {
temp += str.substr(beg, cur-beg);
beg = cur+1;
quoteSingle = !quoteSingle;
continue;
}
if(str[cur] == '\\') {
escape = true;
continue;
}
}
if(escape && ((!quoteSingle && !quoteDouble) || (quoteSingle && str[cur] == '\'') || (quoteDouble && (str[cur] == '"' || str[cur] == '\\')))) {
temp += str.substr(beg, cur-beg-1);
beg = cur;
}
escape = false;
}
temp += str.substr(beg, std::string::npos);
ret.push_back(temp);
return true;
}
bool CommandParser::parse(const std::string &cmd) {
std::vector<std::string> splitCmd;
Common::Tokenizer::tokenize(cmd, splitCmd);
if(splitCmd.empty())
return true;
const Command* command = findCommand(splitCmd[0]);
if(command)
(this->*command->funcPtr)(splitCmd);
else
Common::Logger::logf(Common::Logger::ERROR, "Unknown command '%s'.", splitCmd[0].c_str());
return true;
}
}
}
|