-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsys_cmd.cpp
More file actions
434 lines (385 loc) · 11.9 KB
/
Copy pathsys_cmd.cpp
File metadata and controls
434 lines (385 loc) · 11.9 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
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
// **************************************************************************
// File [ sys_cmd.cpp ]
// Author [ littleshamoo ]
// Synopsis [ some system and misc commands ]
// Date [ Ver. 2.0 started 2010/07/01 ]
// **************************************************************************
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <unistd.h>
#include "sys_cmd.h"
using namespace std;
using namespace CommonNs;
//{{{ class SysBashCmd method
SysBashCmd::SysBashCmd(const char *const name) : Cmd(name) {
optMgr_.setShortDes("opens a new bash shell environment");
optMgr_.setDes("opens a new bash shell environment");
Opt *opt = new Opt(Opt::BOOL, "print usage", "");
opt->addFlag("h");
opt->addFlag("help");
optMgr_.regOpt(opt);
}
SysBashCmd::~SysBashCmd() {}
bool SysBashCmd::exec(int argc, char **argv) {
optMgr_.parse(argc, argv);
if (optMgr_.getParsedOpt("h")) {
optMgr_.usage();
return true;
}
int res = system("bash --login");
if (res != 0)
return false;
return true;
}
//}}}
//{{{ class SysListCmd method
SysListCmd::SysListCmd(const char *const name) : Cmd(name) {
optMgr_.setShortDes("list diectory contents");
optMgr_.setDes("lists contents in DIRECTORY. If not specified, list current directory content.");
optMgr_.regArg(new Arg(Arg::OPT, "target directories", "DIRECTORY"));
Opt *opt = new Opt(Opt::BOOL, "print usage", "");
opt->addFlag("h");
opt->addFlag("help");
optMgr_.regOpt(opt);
}
SysListCmd::~SysListCmd() {}
bool SysListCmd::exec(int argc, char **argv) {
optMgr_.parse(argc, argv);
if (optMgr_.getParsedOpt("h")) {
optMgr_.usage();
return true;
}
char cmd[256];
cmd[0] = '\0';
for (int i = 0; i < argc; ++i) {
strcat(cmd, argv[i]);
strcat(cmd, " ");
}
strcat(cmd, " --color=always -F 2> /dev/null");
if (system(cmd) != 0) {
fprintf(stderr, "**ERROR SysListCmd::exec(): list failed\n");
return false;
}
return true;
}
//}}}
//{{{ class SysCatCmd method
SysCatCmd::SysCatCmd(const char *const name) : Cmd(name) {
optMgr_.setShortDes("concatenate files and print on the standard output");
optMgr_.setDes("Concatenate FILE(s), or standard input, to standard output");
optMgr_.regArg(new Arg(Arg::REQ_INF, "files to be printed", "FILE"));
Opt *opt = new Opt(Opt::BOOL, "print usage", "");
opt->addFlag("h");
opt->addFlag("help");
optMgr_.regOpt(opt);
}
SysCatCmd::~SysCatCmd() {}
bool SysCatCmd::exec(int argc, char **argv) {
optMgr_.parse(argc, argv);
if (optMgr_.getParsedOpt("h")) {
optMgr_.usage();
return true;
}
if (argc < 2) {
fprintf(stderr, "**ERROR SysCatCmd::exec(): file needed\n");
return false;
}
char cmd[256];
cmd[0] = '\0';
for (int i = 0; i < argc; ++i) {
strcat(cmd, argv[i]);
strcat(cmd, " ");
}
strcat(cmd, " 2> /dev/null");
if (system(cmd) != 0) {
fprintf(stderr, "**ERROR SysCatCmd::exec(): cat files failed\n");
return false;
}
return true;
}
//}}}
//{{{class SysCdCmd method
SysCdCmd::SysCdCmd(const char *const name) : Cmd(name) {
optMgr_.setShortDes("change directory");
optMgr_.setDes("changes working directory to DIRECTORY. If not specified, changes to home directory.");
optMgr_.regArg(new Arg(Arg::OPT, "target directories", "DIRECTORY"));
Opt *opt = new Opt(Opt::BOOL, "print usage", "");
opt->addFlag("h");
opt->addFlag("help");
optMgr_.regOpt(opt);
}
SysCdCmd::~SysCdCmd() {}
bool SysCdCmd::exec(int argc, char **argv) {
optMgr_.parse(argc, argv);
if (optMgr_.getParsedOpt("h")) {
optMgr_.usage();
return true;
}
if (argc == 1) {
char *home = getenv("HOME");
if (chdir(home) != 0) {
fprintf(stderr, "**ERROR SysCdCmd::exec(): ");
fprintf(stderr, "cannot change to target directory\n");
return false;
}
return true;
}
if (argc > 1) {
if (chdir(argv[1]) != 0) {
fprintf(stderr, "**ERROR SysCdCmd::exec(): ");
fprintf(stderr, "cannot change to target directory\n");
return false;
}
}
return true;
}
//}}}
//{{{ class SysPwdCmd method
SysPwdCmd::SysPwdCmd(const char *const name) : Cmd(name) {
optMgr_.setShortDes("print name of current directory");
optMgr_.setDes("prints the full filename of the current working directory");
Opt *opt = new Opt(Opt::BOOL, "print usage", "");
opt->addFlag("h");
opt->addFlag("help");
optMgr_.regOpt(opt);
}
SysPwdCmd::~SysPwdCmd() {}
bool SysPwdCmd::exec(int argc, char **argv) {
optMgr_.parse(argc, argv);
if (optMgr_.getParsedOpt("h")) {
optMgr_.usage();
return true;
}
int res = system("pwd");
if (res != 0)
return false;
return true;
}
//}}}
//{{{ class SysSetCmd method
SysSetCmd::SysSetCmd(const char *const name, CmdMgr *cmdMgr) : Cmd(name) {
cmdMgr_ = cmdMgr;
optMgr_.setShortDes("set variables");
optMgr_.setDes("set VAR to VALUE");
optMgr_.regArg(new Arg(Arg::OPT, "variable name", "VAR"));
optMgr_.regArg(new Arg(Arg::OPT, "value of the variable", "VALUE"));
Opt *opt = new Opt(Opt::BOOL, "print usage", "");
opt->addFlag("h");
opt->addFlag("help");
optMgr_.regOpt(opt);
}
SysSetCmd::~SysSetCmd() {}
bool SysSetCmd::exec(int argc, char **argv) {
optMgr_.parse(argc, argv);
if (optMgr_.getParsedOpt("h")) {
optMgr_.usage();
return true;
}
if (argc == 1) {
vector<const char *> vars;
cmdMgr_->getVars(vars);
printf("%s set variables\n", cmdMgr_->getComment());
for (size_t i = 0; i < vars.size(); ++i) {
printf("%s %s = %s\n", cmdMgr_->getComment(), vars[i],
cmdMgr_->getValue(vars[i]));
}
return true;
}
if (argc < 3) {
fprintf(stderr, "**ERROR SysSetCmd::exec(): ");
fprintf(stderr, "variable and value needed\n");
return false;
}
if (!cmdMgr_->addVar(argv[1], argv[2])) {
fprintf(stderr, "**ERROR SysSetCmd::exec(): set failed\n");
return false;
}
return true;
}
//}}}
//{{{ class SysExitCmd method
SysExitCmd::SysExitCmd(const char *const name, CmdMgr *cmdMgr) : Cmd(name) {
cmdMgr_ = cmdMgr;
optMgr_.setShortDes("exit the program");
optMgr_.setDes("exits the program");
Opt *opt = new Opt(Opt::BOOL, "print usage", "");
opt->addFlag("h");
opt->addFlag("help");
optMgr_.regOpt(opt);
}
SysExitCmd::~SysExitCmd() {}
bool SysExitCmd::exec(int argc, char **argv) {
optMgr_.parse(argc, argv);
if (optMgr_.getParsedOpt("h")) {
optMgr_.usage();
return true;
}
cmdMgr_->setExit(true);
return true;
}
//}}}
//{{{ class SysSourceCmd method
SysSourceCmd::SysSourceCmd(const char *const name, CmdMgr *cmdMgr) : Cmd(name) {
cmdMgr_ = cmdMgr;
optMgr_.setShortDes("run commands from startup file");
optMgr_.setDes("runs commands from FILE");
optMgr_.regArg(new Arg(Arg::REQ, "target file with commands", "FILE"));
Opt *opt = new Opt(Opt::BOOL, "print usage", "");
opt->addFlag("h");
opt->addFlag("help");
optMgr_.regOpt(opt);
}
SysSourceCmd::~SysSourceCmd() {}
bool SysSourceCmd::exec(int argc, char **argv) {
optMgr_.parse(argc, argv);
if (optMgr_.getParsedOpt("h")) {
optMgr_.usage();
return true;
}
if (argc < 2) {
fprintf(stderr, "**ERROR SysSourceCmd::exec(): ");
fprintf(stderr, "please specify source file\n");
return false;
}
FILE *fin = fopen(argv[1], "r");
if (!fin) {
fprintf(stderr, "**ERROR SysSourceCmd::exec(): ");
fprintf(stderr, "file cannot be opened\n");
return false;
}
char expr[4096];
char *pos = strrchr(argv[1], '/') ? strrchr(argv[1], '/') : argv[1];
int count = 0;
CmdMgr::Result res = CmdMgr::NOP;
while (fgets(expr, 4096, fin) && res != CmdMgr::EXIT) {
count++;
printf("%s %d> %s\n", pos, count, expr);
res = cmdMgr_->exec(expr);
if (res == CmdMgr::NOT_EXIST)
fprintf(stderr, "**ERROR command does not exist\n");
}
fclose(fin);
return true;
}
//}}}
//{{{ class SysHelpCmd method
SysHelpCmd::SysHelpCmd(const char *const name, CmdMgr *cmdMgr) : Cmd(name) {
cmdMgr_ = cmdMgr;
optMgr_.setShortDes("print help messages");
optMgr_.setDes("prints help for COMMAND. If not specified, prints the usage of the command manager.");
optMgr_.regArg(new Arg(Arg::OPT, "target command", "COMMAND"));
Opt *opt = new Opt(Opt::BOOL, "print usage", "");
opt->addFlag("h");
opt->addFlag("help");
optMgr_.regOpt(opt);
}
SysHelpCmd::~SysHelpCmd() {}
bool SysHelpCmd::exec(int argc, char **argv) {
optMgr_.parse(argc, argv);
if (optMgr_.getParsedOpt("h")) {
optMgr_.usage();
cout << endl;
return true;
}
if (argc == 1) {
cmdMgr_->usage();
cout << endl;
return true;
}
if (argc > 1) {
Cmd *cmd = cmdMgr_->getCmd(argv[1]);
if (!cmd) {
fprintf(stderr, "**ERROR SysHelpCmd::exec(): ");
fprintf(stderr, "command does not exist\n");
return false;
}
else
cmd->optMgr_.usage();
}
return true;
}
//}}}
//{{{ class SysDotCmd method
SysDotCmd::SysDotCmd(const char * const name) : Cmd(name) {
optMgr_.setShortDes("call system dot filter");
optMgr_.setDes("convert a graph in dot language to a picture");
optMgr_.regArg(new Arg(Arg::REQ, "input dot file", "INPUT"));
Opt *opt = new Opt(Opt::BOOL, "print usage", "");
opt->addFlag("h");
opt->addFlag("help");
optMgr_.regOpt(opt);
opt = new Opt(Opt::STR_REQ,
"output format. Default is jpg",
"bmp|jpg|ps|png ...");
opt->addFlag("T");
optMgr_.regOpt(opt);
opt = new Opt(Opt::STR_REQ,
"output file. Default is <input>.<format>",
"OUTPUT");
opt->addFlag("o");
optMgr_.regOpt(opt);
}
SysDotCmd::~SysDotCmd() {}
bool SysDotCmd::exec(int argc, char **argv) {
optMgr_.parse(argc, argv);
if (optMgr_.getParsedOpt("h")) {
optMgr_.usage();
return true;
}
if (optMgr_.getNParsedArg() < 1) {
fprintf(stderr, "**ERROR SysDotCmd::exec(): need input file\n");
return false;
}
string format = "jpg";
if (optMgr_.getParsedOpt("T"))
format = optMgr_.getParsedValue("T");
string fname = string(optMgr_.getParsedArg(0)) + "." + format;
if (optMgr_.getParsedOpt("o"))
fname = optMgr_.getParsedValue("o");
int res = 0;
string cmd = "dot ";
cmd += "-T" + format + " ";
cmd += "-o" + fname + " ";
cmd += optMgr_.getParsedArg(0);
res = system(cmd.c_str());
if (res != 0) {
fprintf(stderr, "**ERROR SysDotCmd::exec(): display failed\n");
return false;
}
return true;
}
//}}}
//{{{ class SysDisplayCmd method
SysDisplayCmd::SysDisplayCmd(const char *const name) : Cmd(name) {
optMgr_.setShortDes("disply picture in X-window"); // short
optMgr_.setDes("call the system function display."); // long
optMgr_.regArg(new Arg(Arg::OPT, "file name of the picture. can be in jpg, png, gif format ", "FILENAME"));
Opt *opt = new Opt(Opt::BOOL, "print usage", "");
opt->addFlag("h");
opt->addFlag("help");
optMgr_.regOpt(opt);
}
SysDisplayCmd::~SysDisplayCmd() {}
bool SysDisplayCmd::exec(int argc, char **argv) {
optMgr_.parse(argc, argv);
if (optMgr_.getParsedOpt("h")) {
optMgr_.usage();
return true;
}
char cmd[256];
cmd[0] = '\0';
for (int i = 0; i < argc; ++i) {
strcat(cmd, argv[i]);
strcat(cmd, " ");
}
strcat(cmd, " &"); // display in background
// debug
cout << cmd <<endl;
if (system(cmd) != 0) {
fprintf(stderr, "**ERROR SysListCmd::exec(): list failed\n");
return false;
}
return true;
}
//}}}