-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmdconsole.cpp
219 lines (194 loc) · 7.37 KB
/
cmdconsole.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
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
/****************************************************************************
* Copyright (c) 2012, voidnull@github
* License : BSD Clause 2 : http://bit.ly/bsd-clause-2
****************************************************************************/
#include <string.h>
#include <iostream>
#include "cmdconsole.h"
#include <algorithm>
using namespace std;
CmdConsole::CmdConsole() {
registerCommand("help", &CmdConsole::cmdHelp);
setPrompt("console:> ");
}
CmdConsole* CmdConsole::CompletionCB::console = NULL;
void CmdConsole::setPrompt(const std::string& prompt) {
this->prompt = prompt;
}
void CmdConsole::setHistoryFile(const std::string& filename) {
historyFile = filename;
}
void CmdConsole::setHistoryLength(size_t length) {
linenoiseHistorySetMaxLen(length);
}
void CmdConsole::clearScreen() {
linenoiseClearScreen();
}
void CmdConsole::complete(const std::string& line, Completions& completions) {
std::map <std::string, CmdCallBack>::const_iterator cmdIter;
for ( cmdIter = mapCallBacks.begin() ; cmdIter != mapCallBacks.end() ; ++ cmdIter) {
if (0 == strncmp(line.c_str(),cmdIter->first.c_str(), line.length())){
completions.add(cmdIter->first);
}
}
}
void CmdConsole::cmdUnknown(const std::string& line) {
cout << " " << Color::Red << "unable to process line: " << Color::End << line << "\n";
}
void CmdConsole::cmdHelp(std::vector<std::string>& args) {
cout << Color::BoldMagenta << "Available Commands: "<< Color::End << "\n";
std::map <std::string, CmdCallBack>::const_iterator cmdIter;
for ( cmdIter = mapCallBacks.begin() ; cmdIter != mapCallBacks.end() ; ++cmdIter) {
cout << " " << cmdIter->first << "\n";
}
if (!args.empty()) {
cout << "\n";
cout << Color::BoldWhite << "Arguments:" << Color::End << "\n";
for (uint i=0 ; i< args.size(); i++) {
cout << " " << args[i] << "\n";
}
}
}
void CmdConsole::registerCommand(const std::string& cmd, CmdCallBack cb) {
std::string lowercmd=cmd;
std::transform(lowercmd.begin(), lowercmd.end(), lowercmd.begin(), ::tolower);
mapCallBacks[lowercmd] = cb;
}
void CmdConsole::run() {
char *line,*next;
CompletionCB::console = this;
linenoiseSetCompletionCallback(CompletionCB::completion);
if (!historyFile.empty()) {
linenoiseHistoryLoad(historyFile.c_str());
}
std::string cmd;
std::map <std::string, CmdCallBack>::const_iterator cmdIter;
std::vector<std::string> args;
if (!historyFile.empty()) {
linenoiseHistoryLoad(historyFile.c_str());
}
while((line = linenoise(prompt.c_str())) != NULL) {
if (line[0] != '\0') {
next = line;
while(next[0]!='\0' && !isspace(next[0])) next++;
cmd = std::string(line,next-line);
// cout << line << ":" << cmd << "\n";
args.clear();
// find first non white space
while(next[0]!='\0' && isspace(next[0])) next++;
// cout << "next=" << next << "\n";
std::transform(cmd.begin(), cmd.end(), cmd.begin(), ::tolower);
cmdIter = mapCallBacks.find(cmd);
if (cmdIter != mapCallBacks.end()) {
// cout << "known command: " << cmdIter->first << "\n";
if (next[0]!='\0') {
if (!splitArgs(next,args)) {
cout << Color::Yellow << "[WARN] error parsing line .. " << Color::End <<"\n";
}
}
(this->*(cmdIter->second))(args);
} else {
cmdUnknown(line);
}
linenoiseHistoryAdd(line);
if (!historyFile.empty()) {
if (-1 == linenoiseHistorySave(historyFile.c_str())) {
cout <<"error saving history\n";
}
}
}
free(line);
}
}
/**
* adapted from redis.sds.sdssplitargs
* https://github.com/antirez/redis/blob/unstable/src/sds.c
*/
bool CmdConsole::splitArgs(const std::string& line, std::vector<std::string>& args) {
const char *p = line.c_str();
std::string current;
while(true) {
while(*p && isspace(*p)) p++;
if (*p) {
int inq=0; /* set to 1 if we are in "quotes" */
int insq=0; /* set to 1 if we are in 'single quotes' */
int done=0;
while(!done) {
if (inq) {
if (*p == '\\' && *(p+1) == '"') {
current.append(1,'"');
p++;
} else if (*p == '"') {
/* closing quote must be followed by a space or
* nothing at all. */
if (*(p+1) && !isspace(*(p+1))) return false;
done = 1;
} else if (!*p) {
/* unterminated quotes */
return false;
} else {
current = current.append(1,p[0]);
}
} else if (insq) {
if (*p == '\\' && *(p+1) == '\'') {
p++;
current.append(1,'\'');
} else if (*p == '\'') {
/* closing quote must be followed by a space or
* nothing at all. */
if (*(p+1) && !isspace(*(p+1))) return false;
done=1;
} else if (!*p) {
/* unterminated quotes */
return false;
} else {
current = current.append(1,p[0]);
}
} else {
switch(*p) {
case ' ':
case '\n':
case '\r':
case '\t':
case '\0':
done=1;
break;
case '"':
inq=1;
break;
case '\'':
insq=1;
break;
default:
current.append(1,p[0]);
// cout << "append:" << p[0] << ":" << current <<"\n";
break;
}
}
if (*p) p++;
} // while (!done)
args.push_back(current);
current.clear();
} else { // if p
return true;
}
} // while true
return false;
}
std::string Color::End = "\x1b[0m";
std::string Color::Black = "\x1b[30m";
std::string Color::Red= "\x1b[31m";
std::string Color::Green = "\x1b[32m";
std::string Color::Yellow = "\x1b[33m";
std::string Color::Blue = "\x1b[34m";
std::string Color::Magenta = "\x1b[35m";
std::string Color::Cyan = "\x1b[36m";
std::string Color::White = "\x1b[37m";
std::string Color::BoldBlack = "\x1b[1;30m";
std::string Color::BoldRed= "\x1b[1;31m";
std::string Color::BoldGreen = "\x1b[1;32m";
std::string Color::BoldYellow = "\x1b[1;33m";
std::string Color::BoldBlue = "\x1b[1;34m";
std::string Color::BoldMagenta = "\x1b[1;35m";
std::string Color::BoldCyan = "\x1b[1;36m";
std::string Color::BoldWhite = "\x1b[1;37m";