1
2
3
10
11
12
13
14
21
24
25
26
27
28
29
30
39
45
46
47
48
49
50
51
52
53
54
55
56
57
58
64
65
66
67
68
69
70
71
76
85
86
89
90
91
95
96
97
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
120
121
127
129
130
135
136
137
141
142
146
147
151
152
156
157
161
162
166
167
171
172
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
212
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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
321
322
328
329
330
331
337
338
339
340
341
342
343
344
345
348
349
354
355
362
363
364
365
366
367
368
369
370
371
372
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
408
409
410
411
412
413
414
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
451
452
454
455
456
457
458
459
460
461
462
463
464
465
466
467
476
477
478
479
480
481
482
483
484
485
486
487
488
489
496
497
503
516
517
518
519
520
527
528
529
530
532
533
535
536
/* ... */
#ifndef OPENOCD_HELPER_COMMAND_H
#define OPENOCD_HELPER_COMMAND_H
#include <stdint.h>
#include <stdbool.h>
#include <helper/jim-nvp.h>
#include <helper/list.h>
#include <helper/types.h>
5 includes
/* ... */
#if (defined(IS_MINGW) && (((__GNUC__ << 16) + __GNUC_MINOR__) >= 0x00040004)) && !defined(__clang__)
#define PRINTF_ATTRIBUTE_FORMAT gnu_printf
#else
#define PRINTF_ATTRIBUTE_FORMAT printf
#endif
/* ... */
enum command_mode {
COMMAND_EXEC,
COMMAND_CONFIG,
COMMAND_ANY,
COMMAND_UNKNOWN = -1,
...};
struct command_context;
typedef int (*command_output_handler_t)(struct command_context *context,
const char *line);
struct command_context {
Jim_Interp *interp;
enum command_mode mode;
struct target *current_target;
struct target *current_target_override;
/* ... */
command_output_handler_t output_handler;
void *output_handler_priv;
struct list_head *help_list;
...};
struct command;
/* ... */
struct command_invocation {
struct command_context *ctx;
struct command *current;
const char *name;
unsigned argc;
const char **argv;
Jim_Obj * const *jimtcl_argv;
Jim_Obj *output;
...};
/* ... */
bool jimcmd_is_oocd_command(Jim_Cmd *cmd);
/* ... */
void *jimcmd_privdata(Jim_Cmd *cmd);
/* ... */
#define __COMMAND_HANDLER(name, extra ...) \
int name(struct command_invocation *cmd, ## extra)...
/* ... */
#define CALL_COMMAND_HANDLER(name, extra ...) \
name(cmd, ## extra)...
/* ... */
#define COMMAND_HANDLER(name) \
static __COMMAND_HANDLER(name)...
/* ... */
#define COMMAND_HELPER(name, extra ...) __COMMAND_HANDLER(name, extra)
/* ... */
#define CMD (cmd)
/* ... */
#define CMD_CTX (cmd->ctx)
/* ... */
#define CMD_ARGC (cmd->argc)
/* ... */
#define CMD_ARGV (cmd->argv)
/* ... */
#define CMD_JIMTCL_ARGV (cmd->jimtcl_argv)
/* ... */
#define CMD_NAME (cmd->name)
/* ... */
#define CMD_CURRENT (cmd->current)
/* ... */
#define CMD_DATA (CMD_CURRENT->jim_handler_data)
12 defines
/* ... */
typedef __COMMAND_HANDLER((*command_handler_t));
struct command {
char *name;
command_handler_t handler;
Jim_CmdProc *jim_handler;
void *jim_handler_data;
struct target *jim_override_target;
enum command_mode mode;
...};
/* ... */
static inline struct command *jim_to_command(Jim_Interp *interp)
{
return Jim_CmdPrivData(interp);
}{ ... }
/* ... */
struct command_registration {
const char *name;
command_handler_t handler;
Jim_CmdProc *jim_handler;
enum command_mode mode;
const char *help;
const char *usage;
/* ... */
const struct command_registration *chain;
...};
#define COMMAND_REGISTRATION_DONE { .name = NULL, .chain = NULL }
int __register_commands(struct command_context *cmd_ctx, const char *cmd_prefix,
const struct command_registration *cmds, void *data,
struct target *override_target);
/* ... */
static inline int register_commands(struct command_context *cmd_ctx, const char *cmd_prefix,
const struct command_registration *cmds)
{
return __register_commands(cmd_ctx, cmd_prefix, cmds, NULL, NULL);
}{ ... }
/* ... */
static inline int register_commands_override_target(struct command_context *cmd_ctx,
const char *cmd_prefix, const struct command_registration *cmds,
struct target *target)
{
return __register_commands(cmd_ctx, cmd_prefix, cmds, NULL, target);
}{ ... }
/* ... */
static inline int register_commands_with_data(struct command_context *cmd_ctx,
const char *cmd_prefix, const struct command_registration *cmds,
void *data)
{
return __register_commands(cmd_ctx, cmd_prefix, cmds, data, NULL);
}{ ... }
/* ... */
int unregister_all_commands(struct command_context *cmd_ctx,
const char *cmd_prefix);
/* ... */
int help_del_all_commands(struct command_context *cmd_ctx);
void command_set_output_handler(struct command_context *context,
command_output_handler_t output_handler, void *priv);
int command_context_mode(struct command_context *context, enum command_mode mode);
/* ... */
struct command_context *current_command_context(Jim_Interp *interp);
/* ... */
struct command_context *command_init(const char *startup_tcl, Jim_Interp *interp);
/* ... */
void command_exit(struct command_context *context);
/* ... */
struct command_context *copy_command_context(struct command_context *cmd_ctx);
/* ... */
void command_done(struct command_context *context);
/* ... */
void command_print(struct command_invocation *cmd, const char *format, ...)
__attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 2, 3)));
void command_print_sameline(struct command_invocation *cmd, const char *format, ...)
__attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 2, 3)));
int command_run_line(struct command_context *context, char *line);
int command_run_linef(struct command_context *context, const char *format, ...)
__attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 2, 3)));
void command_output_text(struct command_context *context, const char *data);
void process_jim_events(struct command_context *cmd_ctx);
#define ERROR_COMMAND_CLOSE_CONNECTION (-600)
#define ERROR_COMMAND_SYNTAX_ERROR (-601)
#define ERROR_COMMAND_NOTFOUND (-602)
#define ERROR_COMMAND_ARGUMENT_INVALID (-603)
#define ERROR_COMMAND_ARGUMENT_OVERFLOW (-604)
#define ERROR_COMMAND_ARGUMENT_UNDERFLOW (-605)
6 defines
int parse_ulong(const char *str, unsigned long *ul);
int parse_ullong(const char *str, unsigned long long *ul);
int parse_long(const char *str, long *ul);
int parse_llong(const char *str, long long *ul);
#define DECLARE_PARSE_WRAPPER(name, type) \
int parse ## name(const char *str, type * ul)...
DECLARE_PARSE_WRAPPER(_uint, unsigned);
DECLARE_PARSE_WRAPPER(_u64, uint64_t);
DECLARE_PARSE_WRAPPER(_u32, uint32_t);
DECLARE_PARSE_WRAPPER(_u16, uint16_t);
DECLARE_PARSE_WRAPPER(_u8, uint8_t);
DECLARE_PARSE_WRAPPER(_int, int);
DECLARE_PARSE_WRAPPER(_s64, int64_t);
DECLARE_PARSE_WRAPPER(_s32, int32_t);
DECLARE_PARSE_WRAPPER(_s16, int16_t);
DECLARE_PARSE_WRAPPER(_s8, int8_t);
DECLARE_PARSE_WRAPPER(_target_addr, target_addr_t);
/* ... */
#define COMMAND_PARSE_NUMBER(type, in, out) \
do { \
int retval_macro_tmp = parse_ ## type(in, &(out)); \
if (retval_macro_tmp != ERROR_OK) { \
command_print(CMD, stringify(out) \
" option value ('%s') is not valid", in); \
return retval_macro_tmp; \
}if (retval_macro_tmp != ERROR_OK) { ... } \
...} while (0)...
#define COMMAND_PARSE_ADDRESS(in, out) \
COMMAND_PARSE_NUMBER(target_addr, in, out)...
/* ... */
#define COMMAND_PARSE_ADDITIONAL_NUMBER(type, argn, out, name_str) \
do { \
if (argn+1 >= CMD_ARGC || CMD_ARGV[argn+1][0] == '-') { \
command_print(CMD, "no " name_str " given"); \
return ERROR_FAIL; \
}if (argn+1 >= CMD_ARGC || CMD_ARGV[argn+1][0] == '-') { ... } \
++argn; \
COMMAND_PARSE_NUMBER(type, CMD_ARGV[argn], out); \
...} while (0)...
/* ... */
#define COMMAND_PARSE_OPTIONAL_NUMBER(type, argn, out) \
do { \
if (argn+1 < CMD_ARGC && CMD_ARGV[argn+1][0] != '-') { \
++argn; \
COMMAND_PARSE_NUMBER(type, CMD_ARGV[argn], out); \
}if (argn+1 < CMD_ARGC && CMD_ARGV[argn+1][0] != '-') { ... } \
...} while (0)...
/* ... */
#define COMMAND_PARSE_BOOL(in, out, on, off) \
do { \
bool value; \
int retval_macro_tmp = command_parse_bool_arg(in, &value); \
if (retval_macro_tmp != ERROR_OK) { \
command_print(CMD, stringify(out) \
" option value ('%s') is not valid", in); \
command_print(CMD, " choices are '%s' or '%s'", \
on, off); \
return retval_macro_tmp; \
}if (retval_macro_tmp != ERROR_OK) { ... } \
out = value; \
...} while (0)...
5 defines
int command_parse_bool_arg(const char *in, bool *out);
COMMAND_HELPER(handle_command_parse_bool, bool *out, const char *label);
/* ... */
COMMAND_HELPER(command_parse_str_to_buf, const char *str, void *buf, unsigned int buf_len);
#define COMMAND_PARSE_ON_OFF(in, out) \
COMMAND_PARSE_BOOL(in, out, "on", "off")...
#define COMMAND_PARSE_ENABLE(in, out) \
COMMAND_PARSE_BOOL(in, out, "enable", "disable")...
/* ... */
#endif