-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter.pike
executable file
·270 lines (216 loc) · 6.41 KB
/
adapter.pike
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
#define DEBUG(X, Y ...) do { if (debug) werror(X, Y); } while (0)
#define DEFAULT_PORT 4711
import .Protocol;
bool debug;
bool im_a_server;
Stdio.File client;
Stdio.Buffer obuf = Stdio.Buffer();
void write_event(ProtocolMessage msg) {
string msg_str = encode_message(msg);
DEBUG("\nSending event: \n%s\n", msg_str);
if (im_a_server) obuf->add(msg_str);
else obuf->write(msg_str);
}
void write_response(ProtocolMessage msg) {
string msg_str = encode_message(msg);
DEBUG("\nSending response: \n%s\n", msg_str);
if (im_a_server) client->write(msg_str);
else obuf->write(msg_str);
}
void handle_initialize_request(mixed msg) {
InitializeRequest req = InitializeRequest(msg);
InitializeResponse res = InitializeResponse();
res->body->supports_configuration_done_request = 1;
res->request_seq = req->seq;
res->success = 1;
write_response(res);
InitializedEvent evt = InitializedEvent();
write_event(evt);
}
void handle_attach_request(mixed msg) {
AttachRequest req = AttachRequest(msg);
AttachResponse res = AttachResponse();
res->request_seq = req->seq;
res->success = 1;
write_response(res);
}
void handle_launch_request(mixed msg) {
LaunchRequest req = LaunchRequest(msg);
LaunchResponse res = LaunchResponse();
res->request_seq = req->seq;
res->success = 1;
write_response(res);
}
void handle_evaluate_request(mixed msg) {
EvaluateRequest req = EvaluateRequest(msg);
Response res = Response();
res->request_seq = req->seq;
res->success = 1;
res->body = ([
"result" : "EvaluateResponse echo: " + req?->arguments?->expression,
"variablesReference" : 0
]);
write_response(res);
}
void handle_threads_request(mixed msg) {
Request req = Request(msg);
ThreadsResponse res = ThreadsResponse();
DAPThread dt = DAPThread();
dt->id = 1;
dt->name = "dummy";
res->body = (["threads" : ({ dt })]);
res->request_seq = req->seq;
res->success = 1;
write_response(res);
}
void handle_configuration_done_request(mixed msg) {
Request req = Request(msg);
Response res = Response();
res->request_seq = req->seq;
res->success = 1;
res->command = req->command;
write_response(res);
StoppedEvent evt = StoppedEvent();
evt->body = (["reason" : "entry", "threadId" : 1]);
write_event(evt);
}
void handle_stack_trace_request(mixed msg) {
Request req = Request(msg);
Response res = Response();
res->request_seq = req->seq;
res->success = 1;
res->command = req->command;
res->body = ([
"stackFrames" : ({
([
"id" : 0,
"line" : 0,
"column" : 0,
"name" : "Stack frame 1"
])
})
]);
write_response(res);
}
void handle_scopes_request(mixed msg) {
Request req = Request(msg);
Response res = Response();
res->request_seq = req->seq;
res->success = 1;
res->command = req->command;
res->body = ([
"scopes" : ({
([
"name" : "Current scope",
"variablesReference" : 0,
"expensive" : true
])
})
]);
write_response(res);
}
void handle_action_request(mixed msg) {
handle_request_generic(msg);
StoppedEvent evt = StoppedEvent();
evt->body = (["reason" : "pause", "threadId" : 1]);
sleep(0.5);
write_event(evt);
}
void handle_request_generic(mixed msg) {
Request req = Request(msg);
Response res = Response();
res->request_seq = req->seq;
res->success = 1;
res->command = req->command;
write_response(res);
}
private void close_cb(mixed data)
{
DEBUG("\nClose callback. Shutting down.\n");
exit(0);
}
private void write_cb(mixed id)
{
if (im_a_server) {
string m = obuf->read();
if (sizeof(m)) client->write(m);
}
}
private void read_cb(mixed id, string data)
{
mixed msg = Standards.JSON.decode(parse_request(data));
DEBUG("\nReceived request: \n%s\n", data);
switch (msg[?"command"]) {
case "initialize":
handle_initialize_request(msg);
break;
case "attach":
handle_attach_request(msg);
break;
case "launch":
handle_launch_request(msg);
break;
case "evaluate":
handle_evaluate_request(msg);
break;
case "threads":
handle_threads_request(msg);
break;
case "configurationDone":
handle_configuration_done_request(msg);
break;
case "stackTrace":
handle_stack_trace_request(msg);
break;
case "scopes":
handle_scopes_request(msg);
break;
case "continue":
case "next":
case "stepIn":
case "stepOut":
handle_action_request(msg);
break;
default:
handle_request_generic(msg);
}
}
int main(int argc, array(string) argv) {
int port_no;
foreach( Getopt.find_all_options( argv, ({
({"debug", Getopt.NO_ARG, ({"-d", "--debug"})}),
({"help", Getopt.NO_ARG, ({"-h", "--help"})}),
({"port", Getopt.MAY_HAVE_ARG, ({"-p", "--port"}), 0, DEFAULT_PORT}),
})), [string name, mixed value]) {
switch (name) {
case "port":
port_no = (int) value;
im_a_server = true;
break;
case "debug":
debug = true;
break;
case "help":
write("\t-d --debug \t enable debug werrors.\n");
write("\t-p --port \t communicate with debug client through\n");
write("\t\t\t sockets. Default port is %d.\n", DEFAULT_PORT);
write("\t\t\t If not specified, communicate through stdio.\n");
write("\t-h --help \t print this message and exits.\n");
exit(0);
break;
}
}
if (im_a_server) {
DEBUG("Starting Test Debug Adapter on port %d.\n", port_no);
Stdio.Port port = Stdio.Port(port_no);
client = port->accept();
} else {
DEBUG("Starting Test Debug Adapter on stdio.\n");
client = Stdio.File("stdin");
obuf = Stdio.File("stdout");
}
client->set_nonblocking(read_cb, write_cb, close_cb);
while (true)
Pike.DefaultBackend(3600.0);
return 0;
}