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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
/*:*
*: File: ./src/cli/cli.cpp
*:
*: yChat; Homepage: ychat.buetow.org; Version 0.9.0-CURRENT
*:
*: Copyright (C) 2003 Paul C. Buetow, Volker Richter
*: Copyright (C) 2004 Paul C. Buetow
*: Copyright (C) 2005 EXA Digital Solutions GbR
*: Copyright (C) 2006, 2007 Paul C. Buetow
*:
*: 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 2
*: 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, write to the Free Software
*: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*:*/
#ifndef CLI_CPP
#define CLI_CPP
#include "cli.h"
#ifdef CLI
#ifdef READLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif
using namespace std;
cli::cli( )
{}
cli::~cli()
{}
int
cli::parse_input( string s_input )
{
string s_param = "";
unsigned long ul_pos;
// Check for empty string or ignore leading whitespaces
while (1)
{
if (s_input.empty())
{
#ifndef READLINE
cout << CLIPRMI;
#endif
return 1;
}
ul_pos = s_input.find_first_of(" ");
if (ul_pos != 0)
break;
s_input.erase(s_input.begin());
}
if ( ul_pos != (unsigned long) string::npos )
{
s_param = s_input.substr(ul_pos+1);
s_input = s_input.substr(0, ul_pos);
}
if ( s_input.compare("help") == 0 || s_input.compare("h") == 0)
{
cout << CLIPRMO << "COMMAND LINE INTERFACE HELP MENU" << endl
<< CLIPRMO << " !command - Uses system to run a command" << endl;
#ifdef DEBUG
cout << CLIPRMO << " (d)ebug - Starts debug routine (cli.cpp)" << endl;
#endif
cout << CLIPRMO << " (du)mp [part] - Prints out a dump of the data structure" << endl;
cout << CLIPRMO << " (Run without part to list all possibilities)" << endl;
cout << CLIPRMO << " (e)cho VAR - Prints out configuration value of VAR" << endl;
cout << CLIPRMO << " Wildcards can be used too, example: echo http*" << endl;
cout << CLIPRMO << " (h)elp - Prints out this help!" << endl;
//<<*
cout << CLIPRMO << " (m)ysql - Runs MySQL client on yChat DB" << endl
<< CLIPRMO << " (rel)oad - Reloads all modules" << endl;
//*>>
#ifdef EXPERIM
cout << CLIPRMO << " (re)conf - Reloads configuration (EXPERIMENTAL)" << endl;
#endif
cout << CLIPRMO << " (r)usage - Shows current resource usage" << endl
<< CLIPRMO << " (ru)sageh - Shows resource usage history (yChat needs to run > 1 day)" << endl
<< CLIPRMO << " (set) VAR VAL - Sets configuration value VAR to VAL" << endl
<< CLIPRMO << " (sh)ell - Runs a system shell" << endl
<< CLIPRMO << " (shut)down - Shuts down the whole server" << endl
<< CLIPRMO << " (t)ime - Prints out time and uptime" << endl;
cout << CLIPRMO << " (unl)oad - Unloads all loaded modules" << endl;//<<
cout << CLIPRMO << " (u)nset VAR - Deletes configuration value VAR" << endl
<< CLIPRMO << " (v)ersion - Prints out version" << endl;
#ifndef READLINE
cout << CLIPRMI;
#endif
}
else if ( s_input.at(0) == '!' )
{
system( s_input.substr(1).c_str() );
#ifndef READLINE
cout << CLIPRMI;
#endif
}
#ifdef DEBUG
else if ( s_input.compare("d") == 0 || s_input.compare("debug") == 0 )
{
debug_routine();
#ifndef READLINE
cout << CLIPRMI;
#endif
}
#endif
else if ( s_input.compare("du") == 0 || s_input.compare("dump") == 0 )
{
dump d(vectorize(s_param));
#ifndef READLINE
cout << CLIPRMI;
#endif
}
else if ( s_input.compare("echo") == 0 || s_input.compare("e") == 0 )
{
string s_val;
// Check wildcards
unsigned long ul_pos = s_param.find("*");
if ( ul_pos != (unsigned long) string::npos )
{
s_param = s_param.substr( 0, ul_pos );
vector<string>* p_vec = wrap::CONF->get_key_vector();
sort(p_vec->begin(), p_vec->end());
vector<string>::iterator iter;
for ( iter = p_vec->begin(); iter != p_vec->end(); iter++ )
if ( iter->find(s_param) == 0 )
s_val.append( *iter + " := " + wrap::CONF->get_elem(*iter) + "\n" + CLIPRMO );
delete p_vec;
}
else
{
s_val = wrap::CONF->get_elem(s_param);
}
if ( s_val.empty() )
s_val = "Value not set";
cout << CLIPRMO << s_val << endl;
#ifndef READLINE
cout << CLIPRMI;
#endif
}
//<<*
else if ( s_input.compare("mysql") == 0 || s_input.compare("m") == 0 )
{
cout << CLIPRMO << CLIMSQL << endl;
system((wrap::CONF->get_elem("chat.system.mysqlclient") + " -p -h " +
wrap::CONF->get_elem("chat.database.serverhost") + " -u " +
wrap::CONF->get_elem("chat.database.user") ).c_str());
cout << CLIPRMO << CLIWELC << endl;
#ifndef READLINE
cout << CLIPRMI;
#endif
}
//*>>
//<<*
else if ( s_input.compare("reload") == 0 || s_input.compare("rel") == 0 )
{
cout << CLIPRMO;
wrap::MODL->reload_modules();
cout << MODRELO << endl;
#ifndef READLINE
cout << CLIPRMI;
#endif
}
//*>>
#ifdef EXPERIM
else if ( s_input.compare("reconf") == 0 || s_input.compare("re") == 0 )
{
wrap::CHAT->reconf();
#ifndef READLINE
cout << CLIPRMI;
#endif
}
#endif
else if ( s_input.compare("rusage") == 0 || s_input.compare("r") == 0 )
{
print_rusage();
#ifndef READLINE
cout << CLIPRMI;
#endif
}
else if ( s_input.compare("ru") == 0 || s_input.compare("rusageh") == 0 )
{
cout << wrap::STAT->get_rusage_history( "ru_maxrss", string(CLIPRMO) + " " );
#ifndef READLINE
cout << CLIPRMI;
#endif
}
else if ( s_input.compare("set") == 0 )
{
string s_varname = "";
ul_pos = s_param.find_first_of(" ");
if ( ul_pos != string::npos )
{
s_varname = s_param.substr(0, ul_pos);
if ( s_param.length() > ul_pos+1 )
s_param = s_param.substr(ul_pos+1);
else
s_param = "";
}
string s_old_val = wrap::CONF->get_elem(s_varname);
if ( !s_old_val.empty() )
{
cout << CLIPRMO << "Old value: " << s_old_val << endl;
wrap::CONF->del_elem(s_varname);
}
wrap::CONF->add_elem(s_param, s_varname);
cout << CLIPRMO << "New value: " << s_param << endl;
#ifndef READLINE
cout << CLIPRMI;
#endif
}
else if ( s_input.compare("shell") == 0 || s_input.compare("sh") == 0 )
{
cout << CLIPRMO << CLISHEL << endl;
system(wrap::CONF->get_elem("httpd.system.shell").c_str());
cout << CLIPRMO << CLIWELC << endl;
#ifndef READLINE
cout << CLIPRMI;
#endif
}
else if ( s_input.compare("shutdown") == 0 || s_input.compare("shut") == 0 )
{
exit(0);
}
else if ( s_input.compare("t") == 0 || s_input.compare("time") == 0 )
{
cout << CLIPRMO << "Time: " << wrap::TIMR->get_time() << endl
<< CLIPRMO << "Uptime: " << wrap::TIMR->get_uptime() << endl;
#ifndef READLINE
cout << CLIPRMI;
#endif
}
//<<*
else if ( s_input.compare("unl") == 0 || s_input.compare("unload") == 0 )
{
cout << CLIPRMO;
wrap::MODL->unload_modules();
cout << MODUNLO << endl;
#ifndef READLINE
cout << CLIPRMI;
#endif
}
//*>>
else if ( s_input.compare("u") == 0 || s_input.compare("unset") == 0 )
{
if (!s_param.empty())
{
string s_old_val = wrap::CONF->get_elem(s_param);
if ( !s_old_val.empty() )
{
cout << CLIPRMO << "Deleted variable " << s_param << " with value: " << s_old_val << endl;
wrap::CONF->del_elem(s_param);
}
else
{
cout << CLIPRMO << "Variable " << s_param << " was not set" << endl;
}
#ifndef READLINE
cout << CLIPRMI;
#endif
}
}
else if ( s_input.compare("v") == 0 || s_input.compare("version") == 0 )
{
cout << CLIPRMO << tool::ychat_version() << " " << UNAME << endl;
#ifndef READLINE
cout << CLIPRMI;
#endif
}
else
{
cout << CLIPRMO << CLIHELP << "\"" << s_input << "\"" << endl;
#ifndef READLINE
cout << CLIPRMI;
#endif
}
return 1;
}
void
cli::start()
{
start(NULL);
}
void
cli::start(void* p_void)
{
cout << CLIPRMO << CLIWELC << endl;
string s_input;
#ifndef READLINE
cout << CLIPRMI;
#endif
while (1)
{
#ifndef READLINE
getline(cin, s_input);
#else
char *c_line = readline(CLIPRMI);
s_input = string(c_line);
free(c_line);
#endif
if (!parse_input(s_input))
break;
}
}
void
cli::print_rusage()
{
rusage* p_rusage = new rusage;
getrusage( RUSAGE_SELF, p_rusage );
cout << CLIPRMO << "ru_maxrss: " << p_rusage->ru_maxrss << "\t(max resident set size)" << endl
<< CLIPRMO << "ru_ixrss: " << p_rusage->ru_ixrss << "\t(integral shared text memory size)" << endl
<< CLIPRMO << "ru_idrss: " << p_rusage->ru_idrss << "\t(integral unshared data size)" << endl
<< CLIPRMO << "ru_isrss: " << p_rusage->ru_isrss << "\t(integral unshared stack size)" << endl
<< CLIPRMO << "ru_minflt: " << p_rusage->ru_minflt << "\t(page reclaims)" << endl
<< CLIPRMO << "ru_majflt: " << p_rusage->ru_majflt << "\t(page faults)" << endl
<< CLIPRMO << "ru_nswap: " << p_rusage->ru_nswap << "\t(swaps)" << endl
<< CLIPRMO << "ru_inblock: " << p_rusage->ru_inblock << "\t(block input operations)" << endl
<< CLIPRMO << "ru_oublock: " << p_rusage->ru_oublock << "\t(block output operations)" << endl
<< CLIPRMO << "ru_msgsnd: " << p_rusage->ru_msgsnd << "\t(messages sent)" << endl
<< CLIPRMO << "ru_msgrcv: " << p_rusage->ru_msgrcv << "\t(messages received)" << endl
<< CLIPRMO << "ru_nsignals: " << p_rusage->ru_nsignals << "\t(signals received)" << endl
<< CLIPRMO << "ru_nvcsw: " << p_rusage->ru_nvcsw << "\t(voluntary context switches)" << endl
<< CLIPRMO << "ru_nivcsw: " << p_rusage->ru_nivcsw << "\t(involuntary context switches)" << endl;
delete p_rusage;
}
vector<string>
cli::vectorize(string s_param)
{
vector<string> vec_ret;
unsigned long ul_pos;
for (ul_pos = s_param.find(" ");
ul_pos != (unsigned long) string::npos;
ul_pos = s_param.find(" "))
{
vec_ret.push_back(s_param.substr(0, ul_pos));
s_param = s_param.substr(ul_pos+1);
}
if (!s_param.empty())
vec_ret.push_back(s_param);
return vec_ret;
}
#ifdef DEBUG
void
cli::debug_routine()
{
rusage* p_rusage = new rusage;
for (;;)
{
/*
ossl *p_tmp = new ossl;
getrusage( RUSAGE_SELF, p_rusage );
cout << CLIPRMO << "ru_maxrss: " << p_rusage->ru_maxrss << "\t(max resident set size)" << endl;
delete p_tmp;
*/
}
delete p_rusage;
}
#endif
#endif
#endif
|