Repository navigation
Expand file tree
/
Copy pathcmd_extended.cpp
More file actions
202 lines (177 loc) · 6.05 KB
/
Copy pathcmd_extended.cpp
File metadata and controls
202 lines (177 loc) · 6.05 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
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/debugger/cmd/cmd_extended.h"
#include <memory>
#include <vector>
#include "hphp/runtime/debugger/cmd/all.h"
#include "hphp/util/logger.h"
namespace HPHP { namespace Eval {
///////////////////////////////////////////////////////////////////////////////
TRACE_SET_MOD(debugger);
void CmdExtended::list(DebuggerClient &client) {
if (client.argCount() == 0) {
const ExtendedCommandMap &cmds = getCommandMap();
for (auto& pair : cmds) {
client.addCompletion(pair.first.c_str());
}
} else {
auto matches = match(client, 1);
if (matches.size() == 1) {
invokeList(client, matches.begin()->second);
}
}
}
static std::string format_unique_prefix(const std::string &cmd,
const std::string &prev,
const std::string &next) {
for (size_t i = 1; i < cmd.size(); i++) {
if (strncasecmp(cmd.c_str(), prev.c_str(), i) &&
strncasecmp(cmd.c_str(), next.c_str(), i)) {
return "[" + cmd.substr(0, i) + "]" + cmd.substr(i);
}
}
return cmd + " (ambiguous command)";
}
void CmdExtended::helpImpl(DebuggerClient &client, const char *name) {
const char *cmd = "{cmd} {arg1} {arg2} ...";
const char *help = "invoke specified command";
client.helpCmds((std::string(name) + " " + cmd).c_str(), help,
(std::string(name) + cmd).c_str(), help,
nullptr);
auto const& cmds = getCommandMap();
if (cmds.empty()) return;
client.help("%s", "");
client.help("where {cmd} can be:");
client.help("%s", "");
std::vector<std::string> vcmds;
for (auto& pair : cmds) {
vcmds.push_back(pair.first);
}
for (size_t i = 0; i < vcmds.size(); i++) {
client.help("\t%s", format_unique_prefix
(vcmds[i], i ? vcmds[i-1] : "",
i < vcmds.size() - 1 ? vcmds[i+1] : "").c_str());
}
client.help("%s", "");
client.help("Type '%s [h]elp|? {cmd} to read their usages.", name);
}
ExtendedCommandMap CmdExtended::match(DebuggerClient &client, int argIndex) {
ExtendedCommandMap matches;
auto const& cmds = getCommandMap();
for (auto& pair : cmds) {
if (client.arg(argIndex, pair.first.c_str())) {
matches[pair.first] = pair.second;
}
}
if (matches.empty()) {
client.error("Cannot find the specified user command: %s",
client.argValue(argIndex).c_str());
}
return matches;
}
void CmdExtended::helpCommands(DebuggerClient& client,
const ExtendedCommandMap& matches) {
for (auto& pair : matches) {
invokeHelp(client, pair.second);
}
}
void CmdExtended::onClient(DebuggerClient &client) {
if (client.arg(1, "help") || client.arg(1, "?")) {
if (client.argCount() == 1) {
help(client);
return;
}
auto matches = match(client, 2);
if (matches.empty()) {
help(client);
} else {
helpCommands(client, matches);
}
return;
}
auto matches = match(client, 1);
if (matches.empty()) {
help(client);
} else if (matches.size() > 1) {
client.error("Need more letters to tell which one of these:");
for (auto& pair : matches) {
client.error("\t%s", pair.first.c_str());
}
} else if (!invokeClient(client, matches.begin()->second)) {
if (client.arg(2, "help") || client.arg(2, "?")) {
helpCommands(client, matches);
}
}
}
///////////////////////////////////////////////////////////////////////////////
void CmdExtended::help(DebuggerClient &client) {
client.helpTitle("Extended Command");
helpImpl(client, "x");
}
const ExtendedCommandMap &CmdExtended::getCommandMap() {
return GetExtendedCommandMap();
}
void CmdExtended::invokeList(DebuggerClient &client, const std::string &cls) {
auto cmd = CreateExtendedCommand(cls);
if (cmd) {
cmd->list(client);
}
}
bool CmdExtended::invokeHelp(DebuggerClient &client, const std::string &cls) {
auto cmd = CreateExtendedCommand(cls);
if (cmd) {
cmd->help(client);
return true;
}
return false;
}
bool CmdExtended::invokeClient(DebuggerClient &client, const std::string &cls) {
client.usageLogCommand("extended", cls);
auto cmd = CreateExtendedCommand(cls);
if (cmd) {
cmd->onClient(client);
return true;
}
return false;
}
bool CmdExtended::onServer(DebuggerProxy &proxy) {
assert(false);
return false;
}
///////////////////////////////////////////////////////////////////////////////
const ExtendedCommandMap &CmdExtended::GetExtendedCommandMap() {
static ExtendedCommandMap s_command_map = {
{ "ample" , "CmdExample" },
{ "tension" , "CmdExtension" },
};
return s_command_map;
}
DebuggerCommandPtr CmdExtended::CreateExtendedCommand(const std::string &cls) {
std::shared_ptr<CmdExtended> ret;
if (cls == "CmdExample") {
ret = std::make_shared<CmdExample>();
} else if (cls == "CmdExtension") {
ret = std::make_shared<CmdExtension>();
}
if (ret) {
ret->m_class = cls;
} else {
Logger::Error("Unable to create %s extended command", cls.c_str());
}
return ret;
}
///////////////////////////////////////////////////////////////////////////////
}}