1
2
3
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
44
45
46
47
48
53
54
55
56
57
58
80
81
86
87
88
89
90
91
92
93
94
95
96
97
98
102
103
104
105
106
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
139
140
141
142
143
144
145
146
147
148
149
150
151
157
162
163
164
165
168
169
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
255
256
257
258
259
260
261
262
263
267
268
269
270
271
272
273
274
275
276
277
278
286
287
293
297
298
306
307
308
309
310
311
312
313
314
315
319
320
321
322
323
330
337
338
339
340
350
351
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/* ... */
#include <helper/log.h>
#include <helper/replacements.h>
#include <transport/transport.h>
extern struct command_context *global_cmd_ctx;
/* ... */
static struct transport *transport_list;
/* ... */
static const char * const *allowed_transports;
static struct transport *session;
static int transport_select(struct command_context *ctx, const char *name)
{
/* ... */
for (struct transport *t = transport_list; t; t = t->next) {
if (strcmp(t->name, name) == 0) {
int retval = t->select(ctx);
/* ... */
if (retval == ERROR_OK)
session = t;
else
LOG_ERROR("Error selecting '%s' as transport", t->name);
return retval;
}if (strcmp(t->name, name) == 0) { ... }
}for (struct transport *t = transport_list; t; t = t->next) { ... }
LOG_ERROR("No transport named '%s' is available.", name);
return ERROR_FAIL;
}{ ... }
/* ... */
int allow_transports(struct command_context *ctx, const char * const *vector)
{
/* ... */
if (allowed_transports || session) {
LOG_ERROR("Can't modify the set of allowed transports.");
return ERROR_FAIL;
}if (allowed_transports || session) { ... }
allowed_transports = vector;
if (!vector[1]) {
LOG_INFO("only one transport option; autoselecting '%s'", vector[0]);
return transport_select(ctx, vector[0]);
}if (!vector[1]) { ... }
return ERROR_OK;
}{ ... }
/* ... */
int transport_register(struct transport *new_transport)
{
struct transport *t;
for (t = transport_list; t; t = t->next) {
if (strcmp(t->name, new_transport->name) == 0) {
LOG_ERROR("transport name already used");
return ERROR_FAIL;
}if (strcmp(t->name, new_transport->name) == 0) { ... }
}for (t = transport_list; t; t = t->next) { ... }
if (!new_transport->select || !new_transport->init)
LOG_ERROR("invalid transport %s", new_transport->name);
new_transport->next = transport_list;
transport_list = new_transport;
LOG_DEBUG("register '%s'", new_transport->name);
return ERROR_OK;
}{ ... }
/* ... */
struct transport *get_current_transport(void)
{
return session;
}{ ... }
/* ... */
/* ... */
COMMAND_HELPER(transport_list_parse, char ***vector)
{
char **argv;
unsigned n = CMD_ARGC;
unsigned j = 0;
*vector = NULL;
if (n < 1)
return ERROR_COMMAND_SYNTAX_ERROR;
argv = calloc(n + 1, sizeof(char *));
if (!argv)
return ERROR_FAIL;
for (unsigned i = 0; i < n; i++) {
struct transport *t;
for (t = transport_list; t; t = t->next) {
if (strcmp(t->name, CMD_ARGV[i]) != 0)
continue;
argv[j++] = strdup(CMD_ARGV[i]);
break;
}for (t = transport_list; t; t = t->next) { ... }
if (!t) {
LOG_ERROR("no such transport '%s'", CMD_ARGV[i]);
goto fail;
}if (!t) { ... }
}for (unsigned i = 0; i < n; i++) { ... }
*vector = argv;
return ERROR_OK;
fail:
for (unsigned i = 0; i < n; i++)
free(argv[i]);
free(argv);
return ERROR_FAIL;
}{ ... }
COMMAND_HANDLER(handle_transport_init)
{
LOG_DEBUG("%s", __func__);
if (!session) {
LOG_ERROR("session transport was not selected. Use 'transport select <transport>'");
LOG_ERROR("Transports available:");
const char * const *vector = allowed_transports;
while (*vector) {
LOG_ERROR("%s", *vector);
vector++;
}while (*vector) { ... }
return ERROR_FAIL;
}if (!session) { ... }
return session->init(CMD_CTX);
}{ ... }
COMMAND_HANDLER(handle_transport_list)
{
if (CMD_ARGC != 0)
return ERROR_COMMAND_SYNTAX_ERROR;
command_print(CMD, "The following transports are available:");
for (struct transport *t = transport_list; t; t = t->next)
command_print(CMD, "\t%s", t->name);
return ERROR_OK;
}{ ... }
/* ... */
COMMAND_HANDLER(handle_transport_select)
{
if (CMD_ARGC > 1)
return ERROR_COMMAND_SYNTAX_ERROR;
if (CMD_ARGC == 0) {
if (!session) {
if (!allowed_transports) {
command_print(CMD, "Debug adapter does not support any transports? Check config file order.");
return ERROR_FAIL;
}if (!allowed_transports) { ... }
LOG_INFO("auto-selecting first available session transport \"%s\". "
"To override use 'transport select <transport>'.", allowed_transports[0]);
int retval = transport_select(CMD_CTX, allowed_transports[0]);
if (retval != ERROR_OK)
return retval;
}if (!session) { ... }
command_print(CMD, "%s", session->name);
return ERROR_OK;
}if (CMD_ARGC == 0) { ... }
if (session) {
if (!strcmp(session->name, CMD_ARGV[0])) {
LOG_WARNING("Transport \"%s\" was already selected", session->name);
return ERROR_OK;
}if (!strcmp(session->name, CMD_ARGV[0])) { ... }
command_print(CMD, "Can't change session's transport after the initial selection was made");
return ERROR_FAIL;
}if (session) { ... }
/* ... */
if (!allowed_transports) {
command_print(CMD, "Debug adapter doesn't support any transports?");
return ERROR_FAIL;
}if (!allowed_transports) { ... }
for (unsigned int i = 0; allowed_transports[i]; i++) {
if (!strcmp(allowed_transports[i], CMD_ARGV[0])) {
int retval = transport_select(CMD_CTX, CMD_ARGV[0]);
if (retval != ERROR_OK)
return retval;
return ERROR_OK;
}if (!strcmp(allowed_transports[i], CMD_ARGV[0])) { ... }
}for (unsigned int i = 0; allowed_transports[i]; i++) { ... }
command_print(CMD, "Debug adapter doesn't support '%s' transport", CMD_ARGV[0]);
return ERROR_FAIL;
}{ ... }
static const struct command_registration transport_commands[] = {
{
.name = "init",
.handler = handle_transport_init,
/* ... */
.mode = COMMAND_ANY,
.help = "Initialize this session's transport",
.usage = ""
...},
{
.name = "list",
.handler = handle_transport_list,
.mode = COMMAND_ANY,
.help = "list all built-in transports",
.usage = ""
...},
{
.name = "select",
.handler = handle_transport_select,
.mode = COMMAND_ANY,
.help = "Select this session's transport",
.usage = "[transport_name]",
...},
COMMAND_REGISTRATION_DONE
...};
static const struct command_registration transport_group[] = {
{
.name = "transport",
.mode = COMMAND_ANY,
.help = "Transport command group",
.chain = transport_commands,
.usage = ""
...},
COMMAND_REGISTRATION_DONE
...};
int transport_register_commands(struct command_context *ctx)
{
return register_commands(ctx, NULL, transport_group);
}{ ... }