c3d2e95665b583056b28e847702ff57308bd413c
[openocd.git] / src / helper / command.c
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
10 * *
11 * part of this file is taken from libcli (libcli.sourceforge.net) *
12 * Copyright (C) David Parrish (david@dparrish.com) *
13 * *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
18 * *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
26 ***************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 /* see Embedded-HOWTO.txt in Jim Tcl project hosted on BerliOS*/
33 #define JIM_EMBEDDED
34
35 /* @todo the inclusion of target.h here is a layering violation */
36 #include <jtag/jtag.h>
37 #include <target/target.h>
38 #include "command.h"
39 #include "configuration.h"
40 #include "log.h"
41 #include "time_support.h"
42 #include "jim-eventloop.h"
43
44 /* nice short description of source file */
45 #define __THIS__FILE__ "command.c"
46
47 struct log_capture_state {
48 Jim_Interp *interp;
49 Jim_Obj *output;
50 };
51
52 static int unregister_command(struct command_context *context,
53 const char *cmd_prefix, const char *name);
54 static int jim_command_dispatch(Jim_Interp *interp, int argc, Jim_Obj * const *argv);
55 static int help_add_command(struct command_context *cmd_ctx,
56 const char *cmd_name, const char *help_text, const char *usage_text);
57 static int help_del_command(struct command_context *cmd_ctx, const char *cmd_name);
58
59 /* set of functions to wrap jimtcl internal data */
60 static inline bool jimcmd_is_proc(Jim_Cmd *cmd)
61 {
62 return cmd->isproc;
63 }
64
65 static inline bool jimcmd_is_ocd_command(Jim_Cmd *cmd)
66 {
67 return !cmd->isproc && cmd->u.native.cmdProc == jim_command_dispatch;
68 }
69
70 static inline void *jimcmd_privdata(Jim_Cmd *cmd)
71 {
72 return cmd->isproc ? NULL : cmd->u.native.privData;
73 }
74
75 static void tcl_output(void *privData, const char *file, unsigned line,
76 const char *function, const char *string)
77 {
78 struct log_capture_state *state = privData;
79 Jim_AppendString(state->interp, state->output, string, strlen(string));
80 }
81
82 static struct log_capture_state *command_log_capture_start(Jim_Interp *interp)
83 {
84 /* capture log output and return it. A garbage collect can
85 * happen, so we need a reference count to this object */
86 Jim_Obj *jim_output = Jim_NewStringObj(interp, "", 0);
87 if (!jim_output)
88 return NULL;
89
90 Jim_IncrRefCount(jim_output);
91
92 struct log_capture_state *state = malloc(sizeof(*state));
93 if (!state) {
94 LOG_ERROR("Out of memory");
95 Jim_DecrRefCount(interp, jim_output);
96 return NULL;
97 }
98
99 state->interp = interp;
100 state->output = jim_output;
101
102 log_add_callback(tcl_output, state);
103
104 return state;
105 }
106
107 /* Classic openocd commands provide progress output which we
108 * will capture and return as a Tcl return value.
109 *
110 * However, if a non-openocd command has been invoked, then it
111 * makes sense to return the tcl return value from that command.
112 *
113 * The tcl return value is empty for openocd commands that provide
114 * progress output.
115 *
116 * Therefore we set the tcl return value only if we actually
117 * captured output.
118 */
119 static void command_log_capture_finish(struct log_capture_state *state)
120 {
121 if (NULL == state)
122 return;
123
124 log_remove_callback(tcl_output, state);
125
126 int length;
127 Jim_GetString(state->output, &length);
128
129 if (length > 0)
130 Jim_SetResult(state->interp, state->output);
131 else {
132 /* No output captured, use tcl return value (which could
133 * be empty too). */
134 }
135 Jim_DecrRefCount(state->interp, state->output);
136
137 free(state);
138 }
139
140 /*
141 * FIXME: workaround for memory leak in jimtcl 0.80
142 * Jim API Jim_CreateCommand() converts the command name in a Jim object and
143 * does not free the object. Fixed for jimtcl 0.81 by e4416cf86f0b
144 * Use the internal jimtcl API Jim_CreateCommandObj, not exported by jim.h,
145 * and override the bugged API through preprocessor's macro.
146 * This workaround works only when jimtcl is compiled as OpenOCD submodule.
147 * It's broken on macOS, so it's currently restricted on Linux only.
148 * If jimtcl is linked-in from a precompiled library, either static or dynamic,
149 * the symbol Jim_CreateCommandObj is not exported and the build will use the
150 * bugged API.
151 * To be removed when OpenOCD will switch to jimtcl 0.81
152 */
153 #if JIM_VERSION == 80 && defined __linux__
154 static int workaround_createcommand(Jim_Interp *interp, const char *cmdName,
155 Jim_CmdProc *cmdProc, void *privData, Jim_DelCmdProc *delProc);
156 int Jim_CreateCommandObj(Jim_Interp *interp, Jim_Obj *cmdNameObj,
157 Jim_CmdProc *cmdProc, void *privData, Jim_DelCmdProc *delProc)
158 __attribute__((weak, alias("workaround_createcommand")));
159 static int workaround_createcommand(Jim_Interp *interp, const char *cmdName,
160 Jim_CmdProc *cmdProc, void *privData, Jim_DelCmdProc *delProc)
161 {
162 if ((void *)Jim_CreateCommandObj == (void *)workaround_createcommand)
163 return Jim_CreateCommand(interp, cmdName, cmdProc, privData, delProc);
164
165 Jim_Obj *cmd_name = Jim_NewStringObj(interp, cmdName, -1);
166 Jim_IncrRefCount(cmd_name);
167 int retval = Jim_CreateCommandObj(interp, cmd_name, cmdProc, privData, delProc);
168 Jim_DecrRefCount(interp, cmd_name);
169 return retval;
170 }
171 #define Jim_CreateCommand workaround_createcommand
172 #endif /* JIM_VERSION == 80 && defined __linux__*/
173 /* FIXME: end of workaround for memory leak in jimtcl 0.80 */
174
175 static int command_retval_set(Jim_Interp *interp, int retval)
176 {
177 int *return_retval = Jim_GetAssocData(interp, "retval");
178 if (return_retval != NULL)
179 *return_retval = retval;
180
181 return (retval == ERROR_OK) ? JIM_OK : retval;
182 }
183
184 extern struct command_context *global_cmd_ctx;
185
186 /* dump a single line to the log for the command.
187 * Do nothing in case we are not at debug level 3 */
188 static void script_debug(Jim_Interp *interp, unsigned int argc, Jim_Obj * const *argv)
189 {
190 if (debug_level < LOG_LVL_DEBUG)
191 return;
192
193 char *dbg = alloc_printf("command -");
194 for (unsigned i = 0; i < argc; i++) {
195 int len;
196 const char *w = Jim_GetString(argv[i], &len);
197 char *t = alloc_printf("%s %s", dbg, w);
198 free(dbg);
199 dbg = t;
200 }
201 LOG_DEBUG("%s", dbg);
202 free(dbg);
203 }
204
205 static void script_command_args_free(char **words, unsigned nwords)
206 {
207 for (unsigned i = 0; i < nwords; i++)
208 free(words[i]);
209 free(words);
210 }
211
212 static char **script_command_args_alloc(
213 unsigned argc, Jim_Obj * const *argv, unsigned *nwords)
214 {
215 char **words = malloc(argc * sizeof(char *));
216 if (NULL == words)
217 return NULL;
218
219 unsigned i;
220 for (i = 0; i < argc; i++) {
221 int len;
222 const char *w = Jim_GetString(argv[i], &len);
223 words[i] = strdup(w);
224 if (words[i] == NULL) {
225 script_command_args_free(words, i);
226 return NULL;
227 }
228 }
229 *nwords = i;
230 return words;
231 }
232
233 struct command_context *current_command_context(Jim_Interp *interp)
234 {
235 /* grab the command context from the associated data */
236 struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
237 if (NULL == cmd_ctx) {
238 /* Tcl can invoke commands directly instead of via command_run_line(). This would
239 * happen when the Jim Tcl interpreter is provided by eCos or if we are running
240 * commands in a startup script.
241 *
242 * A telnet or gdb server would provide a non-default command context to
243 * handle piping of error output, have a separate current target, etc.
244 */
245 cmd_ctx = global_cmd_ctx;
246 }
247 return cmd_ctx;
248 }
249
250 /**
251 * Find a openocd command from fullname.
252 * @returns Returns the named command if it is registred in interp.
253 * Returns NULL otherwise.
254 */
255 static struct command *command_find_from_name(Jim_Interp *interp, const char *name)
256 {
257 if (!name)
258 return NULL;
259
260 Jim_Obj *jim_name = Jim_NewStringObj(interp, name, -1);
261 Jim_IncrRefCount(jim_name);
262 Jim_Cmd *cmd = Jim_GetCommand(interp, jim_name, JIM_NONE);
263 Jim_DecrRefCount(interp, jim_name);
264 if (!cmd || jimcmd_is_proc(cmd) || !jimcmd_is_ocd_command(cmd))
265 return NULL;
266
267 return jimcmd_privdata(cmd);
268 }
269
270 static struct command *command_new(struct command_context *cmd_ctx,
271 const char *full_name, const struct command_registration *cr)
272 {
273 assert(cr->name);
274
275 /*
276 * If it is a non-jim command with no .usage specified,
277 * log an error.
278 *
279 * strlen(.usage) == 0 means that the command takes no
280 * arguments.
281 */
282 if (!cr->jim_handler && !cr->usage)
283 LOG_ERROR("BUG: command '%s' does not have the "
284 "'.usage' field filled out",
285 full_name);
286
287 struct command *c = calloc(1, sizeof(struct command));
288 if (NULL == c)
289 return NULL;
290
291 c->name = strdup(cr->name);
292 if (!c->name) {
293 free(c);
294 return NULL;
295 }
296
297 c->handler = cr->handler;
298 c->jim_handler = cr->jim_handler;
299 c->mode = cr->mode;
300
301 if (cr->help || cr->usage)
302 help_add_command(cmd_ctx, full_name, cr->help, cr->usage);
303
304 return c;
305 }
306
307 static void command_free(struct Jim_Interp *interp, void *priv)
308 {
309 struct command *c = priv;
310
311 free(c->name);
312 free(c);
313 }
314
315 static struct command *register_command(struct command_context *context,
316 const char *cmd_prefix, const struct command_registration *cr)
317 {
318 char *full_name;
319
320 if (!context || !cr->name)
321 return NULL;
322
323 if (cmd_prefix)
324 full_name = alloc_printf("%s %s", cmd_prefix, cr->name);
325 else
326 full_name = strdup(cr->name);
327 if (!full_name)
328 return NULL;
329
330 struct command *c = command_find_from_name(context->interp, full_name);
331 if (c) {
332 /* TODO: originally we treated attempting to register a cmd twice as an error
333 * Sometimes we need this behaviour, such as with flash banks.
334 * http://www.mail-archive.com/openocd-development@lists.berlios.de/msg11152.html */
335 LOG_DEBUG("command '%s' is already registered", full_name);
336 free(full_name);
337 return c;
338 }
339
340 c = command_new(context, full_name, cr);
341 if (!c) {
342 free(full_name);
343 return NULL;
344 }
345
346 if (false) /* too noisy with debug_level 3 */
347 LOG_DEBUG("registering '%s'...", full_name);
348 int retval = Jim_CreateCommand(context->interp, full_name,
349 jim_command_dispatch, c, command_free);
350 if (retval != JIM_OK) {
351 command_run_linef(context, "del_help_text {%s}", full_name);
352 command_run_linef(context, "del_usage_text {%s}", full_name);
353 free(c);
354 free(full_name);
355 return NULL;
356 }
357
358 free(full_name);
359 return c;
360 }
361
362 int __register_commands(struct command_context *cmd_ctx, const char *cmd_prefix,
363 const struct command_registration *cmds, void *data,
364 struct target *override_target)
365 {
366 int retval = ERROR_OK;
367 unsigned i;
368 for (i = 0; cmds[i].name || cmds[i].chain; i++) {
369 const struct command_registration *cr = cmds + i;
370
371 struct command *c = NULL;
372 if (NULL != cr->name) {
373 c = register_command(cmd_ctx, cmd_prefix, cr);
374 if (NULL == c) {
375 retval = ERROR_FAIL;
376 break;
377 }
378 c->jim_handler_data = data;
379 c->jim_override_target = override_target;
380 }
381 if (NULL != cr->chain) {
382 if (cr->name) {
383 if (cmd_prefix) {
384 char *new_prefix = alloc_printf("%s %s", cmd_prefix, cr->name);
385 if (!new_prefix) {
386 retval = ERROR_FAIL;
387 break;
388 }
389 retval = __register_commands(cmd_ctx, new_prefix, cr->chain, data, override_target);
390 free(new_prefix);
391 } else {
392 retval = __register_commands(cmd_ctx, cr->name, cr->chain, data, override_target);
393 }
394 } else {
395 retval = __register_commands(cmd_ctx, cmd_prefix, cr->chain, data, override_target);
396 }
397 if (ERROR_OK != retval)
398 break;
399 }
400 }
401 if (ERROR_OK != retval) {
402 for (unsigned j = 0; j < i; j++)
403 unregister_command(cmd_ctx, cmd_prefix, cmds[j].name);
404 }
405 return retval;
406 }
407
408 static __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 2, 3)))
409 int unregister_commands_match(struct command_context *cmd_ctx, const char *format, ...)
410 {
411 Jim_Interp *interp = cmd_ctx->interp;
412 va_list ap;
413
414 va_start(ap, format);
415 char *query = alloc_vprintf(format, ap);
416 va_end(ap);
417 if (!query)
418 return ERROR_FAIL;
419
420 char *query_cmd = alloc_printf("info commands {%s}", query);
421 free(query);
422 if (!query_cmd)
423 return ERROR_FAIL;
424
425 int retval = Jim_EvalSource(interp, __THIS__FILE__, __LINE__, query_cmd);
426 free(query_cmd);
427 if (retval != JIM_OK)
428 return ERROR_FAIL;
429
430 Jim_Obj *list = Jim_GetResult(interp);
431 Jim_IncrRefCount(list);
432
433 int len = Jim_ListLength(interp, list);
434 for (int i = 0; i < len; i++) {
435 Jim_Obj *elem = Jim_ListGetIndex(interp, list, i);
436 Jim_IncrRefCount(elem);
437
438 const char *name = Jim_GetString(elem, NULL);
439 struct command *c = command_find_from_name(interp, name);
440 if (!c) {
441 /* not openocd command */
442 Jim_DecrRefCount(interp, elem);
443 continue;
444 }
445 if (false) /* too noisy with debug_level 3 */
446 LOG_DEBUG("delete command \"%s\"", name);
447 #if JIM_VERSION >= 80
448 Jim_DeleteCommand(interp, elem);
449 #else
450 Jim_DeleteCommand(interp, name);
451 #endif
452
453 help_del_command(cmd_ctx, name);
454
455 Jim_DecrRefCount(interp, elem);
456 }
457
458 Jim_DecrRefCount(interp, list);
459 return ERROR_OK;
460 }
461
462 int unregister_all_commands(struct command_context *context,
463 const char *cmd_prefix)
464 {
465 if (!context)
466 return ERROR_OK;
467
468 if (!cmd_prefix || !*cmd_prefix)
469 return unregister_commands_match(context, "*");
470
471 int retval = unregister_commands_match(context, "%s *", cmd_prefix);
472 if (retval != ERROR_OK)
473 return retval;
474
475 return unregister_commands_match(context, "%s", cmd_prefix);
476 }
477
478 static int unregister_command(struct command_context *context,
479 const char *cmd_prefix, const char *name)
480 {
481 if (!context || !name)
482 return ERROR_COMMAND_SYNTAX_ERROR;
483
484 if (!cmd_prefix || !*cmd_prefix)
485 return unregister_commands_match(context, "%s", name);
486
487 return unregister_commands_match(context, "%s %s", cmd_prefix, name);
488 }
489
490 void command_output_text(struct command_context *context, const char *data)
491 {
492 if (context && context->output_handler && data)
493 context->output_handler(context, data);
494 }
495
496 void command_print_sameline(struct command_invocation *cmd, const char *format, ...)
497 {
498 char *string;
499
500 va_list ap;
501 va_start(ap, format);
502
503 string = alloc_vprintf(format, ap);
504 if (string != NULL && cmd) {
505 /* we want this collected in the log + we also want to pick it up as a tcl return
506 * value.
507 *
508 * The latter bit isn't precisely neat, but will do for now.
509 */
510 Jim_AppendString(cmd->ctx->interp, cmd->output, string, -1);
511 /* We already printed it above
512 * command_output_text(context, string); */
513 free(string);
514 }
515
516 va_end(ap);
517 }
518
519 void command_print(struct command_invocation *cmd, const char *format, ...)
520 {
521 char *string;
522
523 va_list ap;
524 va_start(ap, format);
525
526 string = alloc_vprintf(format, ap);
527 if (string != NULL && cmd) {
528 strcat(string, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one
529 *char longer */
530 /* we want this collected in the log + we also want to pick it up as a tcl return
531 * value.
532 *
533 * The latter bit isn't precisely neat, but will do for now.
534 */
535 Jim_AppendString(cmd->ctx->interp, cmd->output, string, -1);
536 /* We already printed it above
537 * command_output_text(context, string); */
538 free(string);
539 }
540
541 va_end(ap);
542 }
543
544 static bool command_can_run(struct command_context *cmd_ctx, struct command *c, const char *full_name)
545 {
546 if (c->mode == COMMAND_ANY || c->mode == cmd_ctx->mode)
547 return true;
548
549 /* Many commands may be run only before/after 'init' */
550 const char *when;
551 switch (c->mode) {
552 case COMMAND_CONFIG:
553 when = "before";
554 break;
555 case COMMAND_EXEC:
556 when = "after";
557 break;
558 /* handle the impossible with humor; it guarantees a bug report! */
559 default:
560 when = "if Cthulhu is summoned by";
561 break;
562 }
563 LOG_ERROR("The '%s' command must be used %s 'init'.",
564 full_name ? full_name : c->name, when);
565 return false;
566 }
567
568 static int run_command(struct command_context *context,
569 struct command *c, const char **words, unsigned num_words)
570 {
571 struct command_invocation cmd = {
572 .ctx = context,
573 .current = c,
574 .name = c->name,
575 .argc = num_words - 1,
576 .argv = words + 1,
577 };
578
579 cmd.output = Jim_NewEmptyStringObj(context->interp);
580 Jim_IncrRefCount(cmd.output);
581
582 int retval = c->handler(&cmd);
583 if (retval == ERROR_COMMAND_SYNTAX_ERROR) {
584 /* Print help for command */
585 command_run_linef(context, "usage %s", words[0]);
586 } else if (retval == ERROR_COMMAND_CLOSE_CONNECTION) {
587 /* just fall through for a shutdown request */
588 } else {
589 if (retval != ERROR_OK)
590 LOG_DEBUG("Command '%s' failed with error code %d",
591 words[0], retval);
592 /* Use the command output as the Tcl result */
593 Jim_SetResult(context->interp, cmd.output);
594 }
595 Jim_DecrRefCount(context->interp, cmd.output);
596
597 return retval;
598 }
599
600 int command_run_line(struct command_context *context, char *line)
601 {
602 /* all the parent commands have been registered with the interpreter
603 * so, can just evaluate the line as a script and check for
604 * results
605 */
606 /* run the line thru a script engine */
607 int retval = ERROR_FAIL;
608 int retcode;
609 /* Beware! This code needs to be reentrant. It is also possible
610 * for OpenOCD commands to be invoked directly from Tcl. This would
611 * happen when the Jim Tcl interpreter is provided by eCos for
612 * instance.
613 */
614 struct target *saved_target_override = context->current_target_override;
615 context->current_target_override = NULL;
616
617 Jim_Interp *interp = context->interp;
618 struct command_context *old_context = Jim_GetAssocData(interp, "context");
619 Jim_DeleteAssocData(interp, "context");
620 retcode = Jim_SetAssocData(interp, "context", NULL, context);
621 if (retcode == JIM_OK) {
622 /* associated the return value */
623 Jim_DeleteAssocData(interp, "retval");
624 retcode = Jim_SetAssocData(interp, "retval", NULL, &retval);
625 if (retcode == JIM_OK) {
626 retcode = Jim_Eval_Named(interp, line, 0, 0);
627
628 Jim_DeleteAssocData(interp, "retval");
629 }
630 Jim_DeleteAssocData(interp, "context");
631 int inner_retcode = Jim_SetAssocData(interp, "context", NULL, old_context);
632 if (retcode == JIM_OK)
633 retcode = inner_retcode;
634 }
635 context->current_target_override = saved_target_override;
636 if (retcode == JIM_OK) {
637 const char *result;
638 int reslen;
639
640 result = Jim_GetString(Jim_GetResult(interp), &reslen);
641 if (reslen > 0) {
642 command_output_text(context, result);
643 command_output_text(context, "\n");
644 }
645 retval = ERROR_OK;
646 } else if (retcode == JIM_EXIT) {
647 /* ignore.
648 * exit(Jim_GetExitCode(interp)); */
649 } else if (retcode == ERROR_COMMAND_CLOSE_CONNECTION) {
650 return retcode;
651 } else {
652 Jim_MakeErrorMessage(interp);
653 /* error is broadcast */
654 LOG_USER("%s", Jim_GetString(Jim_GetResult(interp), NULL));
655
656 if (retval == ERROR_OK) {
657 /* It wasn't a low level OpenOCD command that failed */
658 return ERROR_FAIL;
659 }
660 return retval;
661 }
662
663 return retval;
664 }
665
666 int command_run_linef(struct command_context *context, const char *format, ...)
667 {
668 int retval = ERROR_FAIL;
669 char *string;
670 va_list ap;
671 va_start(ap, format);
672 string = alloc_vprintf(format, ap);
673 if (string != NULL) {
674 retval = command_run_line(context, string);
675 free(string);
676 }
677 va_end(ap);
678 return retval;
679 }
680
681 void command_set_output_handler(struct command_context *context,
682 command_output_handler_t output_handler, void *priv)
683 {
684 context->output_handler = output_handler;
685 context->output_handler_priv = priv;
686 }
687
688 struct command_context *copy_command_context(struct command_context *context)
689 {
690 struct command_context *copy_context = malloc(sizeof(struct command_context));
691
692 *copy_context = *context;
693
694 return copy_context;
695 }
696
697 void command_done(struct command_context *cmd_ctx)
698 {
699 if (NULL == cmd_ctx)
700 return;
701
702 free(cmd_ctx);
703 }
704
705 /* find full path to file */
706 static int jim_find(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
707 {
708 if (argc != 2)
709 return JIM_ERR;
710 const char *file = Jim_GetString(argv[1], NULL);
711 char *full_path = find_file(file);
712 if (full_path == NULL)
713 return JIM_ERR;
714 Jim_Obj *result = Jim_NewStringObj(interp, full_path, strlen(full_path));
715 free(full_path);
716
717 Jim_SetResult(interp, result);
718 return JIM_OK;
719 }
720
721 COMMAND_HANDLER(jim_echo)
722 {
723 if (CMD_ARGC == 2 && !strcmp(CMD_ARGV[0], "-n")) {
724 LOG_USER_N("%s", CMD_ARGV[1]);
725 return JIM_OK;
726 }
727 if (CMD_ARGC != 1)
728 return JIM_ERR;
729 LOG_USER("%s", CMD_ARGV[0]);
730 return JIM_OK;
731 }
732
733 /* Capture progress output and return as tcl return value. If the
734 * progress output was empty, return tcl return value.
735 */
736 static int jim_capture(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
737 {
738 if (argc != 2)
739 return JIM_ERR;
740
741 struct log_capture_state *state = command_log_capture_start(interp);
742
743 /* disable polling during capture. This avoids capturing output
744 * from polling.
745 *
746 * This is necessary in order to avoid accidentally getting a non-empty
747 * string for tcl fn's.
748 */
749 bool save_poll = jtag_poll_get_enabled();
750
751 jtag_poll_set_enabled(false);
752
753 const char *str = Jim_GetString(argv[1], NULL);
754 int retcode = Jim_Eval_Named(interp, str, __THIS__FILE__, __LINE__);
755
756 jtag_poll_set_enabled(save_poll);
757
758 command_log_capture_finish(state);
759
760 return retcode;
761 }
762
763 struct help_entry {
764 struct list_head lh;
765 char *cmd_name;
766 char *help;
767 char *usage;
768 };
769
770 static COMMAND_HELPER(command_help_show, struct help_entry *c,
771 bool show_help, const char *cmd_match);
772
773 static COMMAND_HELPER(command_help_show_list, bool show_help, const char *cmd_match)
774 {
775 struct help_entry *entry;
776
777 list_for_each_entry(entry, CMD_CTX->help_list, lh)
778 CALL_COMMAND_HANDLER(command_help_show, entry, show_help, cmd_match);
779 return ERROR_OK;
780 }
781
782 #define HELP_LINE_WIDTH(_n) (int)(76 - (2 * _n))
783
784 static void command_help_show_indent(unsigned n)
785 {
786 for (unsigned i = 0; i < n; i++)
787 LOG_USER_N(" ");
788 }
789 static void command_help_show_wrap(const char *str, unsigned n, unsigned n2)
790 {
791 const char *cp = str, *last = str;
792 while (*cp) {
793 const char *next = last;
794 do {
795 cp = next;
796 do {
797 next++;
798 } while (*next != ' ' && *next != '\t' && *next != '\0');
799 } while ((next - last < HELP_LINE_WIDTH(n)) && *next != '\0');
800 if (next - last < HELP_LINE_WIDTH(n))
801 cp = next;
802 command_help_show_indent(n);
803 LOG_USER("%.*s", (int)(cp - last), last);
804 last = cp + 1;
805 n = n2;
806 }
807 }
808
809 static COMMAND_HELPER(command_help_show, struct help_entry *c,
810 bool show_help, const char *cmd_match)
811 {
812 unsigned int n = 0;
813 for (const char *s = strchr(c->cmd_name, ' '); s; s = strchr(s + 1, ' '))
814 n++;
815
816 /* If the match string occurs anywhere, we print out
817 * stuff for this command. */
818 bool is_match = (strstr(c->cmd_name, cmd_match) != NULL) ||
819 ((c->usage != NULL) && (strstr(c->usage, cmd_match) != NULL)) ||
820 ((c->help != NULL) && (strstr(c->help, cmd_match) != NULL));
821
822 if (is_match) {
823 command_help_show_indent(n);
824 LOG_USER_N("%s", c->cmd_name);
825
826 if (c->usage && strlen(c->usage) > 0) {
827 LOG_USER_N(" ");
828 command_help_show_wrap(c->usage, 0, n + 5);
829 } else
830 LOG_USER_N("\n");
831 }
832
833 if (is_match && show_help) {
834 char *msg;
835
836 /* TODO: factorize jim_command_mode() to avoid running jim command here */
837 char *request = alloc_printf("command mode %s", c->cmd_name);
838 if (!request) {
839 LOG_ERROR("Out of memory");
840 return ERROR_FAIL;
841 }
842 int retval = Jim_Eval(CMD_CTX->interp, request);
843 free(request);
844 enum command_mode mode = COMMAND_UNKNOWN;
845 if (retval != JIM_ERR) {
846 const char *result = Jim_GetString(Jim_GetResult(CMD_CTX->interp), NULL);
847 if (!strcmp(result, "any"))
848 mode = COMMAND_ANY;
849 else if (!strcmp(result, "config"))
850 mode = COMMAND_CONFIG;
851 else if (!strcmp(result, "exec"))
852 mode = COMMAND_EXEC;
853 }
854
855 /* Normal commands are runtime-only; highlight exceptions */
856 if (mode != COMMAND_EXEC) {
857 const char *stage_msg = "";
858
859 switch (mode) {
860 case COMMAND_CONFIG:
861 stage_msg = " (configuration command)";
862 break;
863 case COMMAND_ANY:
864 stage_msg = " (command valid any time)";
865 break;
866 default:
867 stage_msg = " (?mode error?)";
868 break;
869 }
870 msg = alloc_printf("%s%s", c->help ? : "", stage_msg);
871 } else
872 msg = alloc_printf("%s", c->help ? : "");
873
874 if (NULL != msg) {
875 command_help_show_wrap(msg, n + 3, n + 3);
876 free(msg);
877 } else
878 return -ENOMEM;
879 }
880
881 return ERROR_OK;
882 }
883
884 COMMAND_HANDLER(handle_help_command)
885 {
886 bool full = strcmp(CMD_NAME, "help") == 0;
887 int retval;
888 char *cmd_match;
889
890 if (CMD_ARGC <= 0)
891 cmd_match = strdup("");
892
893 else {
894 cmd_match = strdup(CMD_ARGV[0]);
895
896 for (unsigned int i = 1; i < CMD_ARGC && cmd_match; ++i) {
897 char *prev = cmd_match;
898 cmd_match = alloc_printf("%s %s", prev, CMD_ARGV[i]);
899 free(prev);
900 }
901 }
902
903 if (cmd_match == NULL) {
904 LOG_ERROR("unable to build search string");
905 return -ENOMEM;
906 }
907 retval = CALL_COMMAND_HANDLER(command_help_show_list, full, cmd_match);
908
909 free(cmd_match);
910 return retval;
911 }
912
913 static char *alloc_concatenate_strings(int argc, Jim_Obj * const *argv)
914 {
915 char *prev, *all;
916 int i;
917
918 assert(argc >= 1);
919
920 all = strdup(Jim_GetString(argv[0], NULL));
921 if (!all) {
922 LOG_ERROR("Out of memory");
923 return NULL;
924 }
925
926 for (i = 1; i < argc; ++i) {
927 prev = all;
928 all = alloc_printf("%s %s", all, Jim_GetString(argv[i], NULL));
929 free(prev);
930 if (!all) {
931 LOG_ERROR("Out of memory");
932 return NULL;
933 }
934 }
935
936 return all;
937 }
938
939 static int exec_command(Jim_Interp *interp, struct command_context *cmd_ctx,
940 struct command *c, int argc, Jim_Obj * const *argv)
941 {
942 if (c->jim_handler)
943 return c->jim_handler(interp, argc, argv);
944
945 /* use c->handler */
946 unsigned int nwords;
947 char **words = script_command_args_alloc(argc, argv, &nwords);
948 if (!words)
949 return JIM_ERR;
950
951 int retval = run_command(cmd_ctx, c, (const char **)words, nwords);
952 script_command_args_free(words, nwords);
953 return command_retval_set(interp, retval);
954 }
955
956 static int jim_command_dispatch(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
957 {
958 script_debug(interp, argc, argv);
959
960 /* check subcommands */
961 if (argc > 1) {
962 char *s = alloc_printf("%s %s", Jim_GetString(argv[0], NULL), Jim_GetString(argv[1], NULL));
963 Jim_Obj *js = Jim_NewStringObj(interp, s, -1);
964 Jim_IncrRefCount(js);
965 free(s);
966 Jim_Cmd *cmd = Jim_GetCommand(interp, js, JIM_NONE);
967 if (cmd) {
968 int retval = Jim_EvalObjPrefix(interp, js, argc - 2, argv + 2);
969 Jim_DecrRefCount(interp, js);
970 return retval;
971 }
972 Jim_DecrRefCount(interp, js);
973 }
974
975 struct command *c = jim_to_command(interp);
976 if (!c->jim_handler && !c->handler) {
977 Jim_EvalObjPrefix(interp, Jim_NewStringObj(interp, "usage", -1), 1, argv);
978 return JIM_ERR;
979 }
980
981 struct command_context *cmd_ctx = current_command_context(interp);
982
983 if (!command_can_run(cmd_ctx, c, Jim_GetString(argv[0], NULL)))
984 return JIM_ERR;
985
986 target_call_timer_callbacks_now();
987
988 /*
989 * Black magic of overridden current target:
990 * If the command we are going to handle has a target prefix,
991 * override the current target temporarily for the time
992 * of processing the command.
993 * current_target_override is used also for event handlers
994 * therefore we prevent touching it if command has no prefix.
995 * Previous override is saved and restored back to ensure
996 * correct work when jim_command_dispatch() is re-entered.
997 */
998 struct target *saved_target_override = cmd_ctx->current_target_override;
999 if (c->jim_override_target)
1000 cmd_ctx->current_target_override = c->jim_override_target;
1001
1002 int retval = exec_command(interp, cmd_ctx, c, argc, argv);
1003
1004 if (c->jim_override_target)
1005 cmd_ctx->current_target_override = saved_target_override;
1006
1007 return retval;
1008 }
1009
1010 static int jim_command_mode(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
1011 {
1012 struct command_context *cmd_ctx = current_command_context(interp);
1013 enum command_mode mode;
1014
1015 if (argc > 1) {
1016 char *full_name = alloc_concatenate_strings(argc - 1, argv + 1);
1017 if (!full_name)
1018 return JIM_ERR;
1019 Jim_Obj *s = Jim_NewStringObj(interp, full_name, -1);
1020 Jim_IncrRefCount(s);
1021 Jim_Cmd *cmd = Jim_GetCommand(interp, s, JIM_NONE);
1022 Jim_DecrRefCount(interp, s);
1023 free(full_name);
1024 if (!cmd || !(jimcmd_is_proc(cmd) || jimcmd_is_ocd_command(cmd))) {
1025 Jim_SetResultString(interp, "unknown", -1);
1026 return JIM_OK;
1027 }
1028
1029 if (jimcmd_is_proc(cmd)) {
1030 /* tcl proc */
1031 mode = COMMAND_ANY;
1032 } else {
1033 struct command *c = jimcmd_privdata(cmd);
1034
1035 mode = c->mode;
1036 }
1037 } else
1038 mode = cmd_ctx->mode;
1039
1040 const char *mode_str;
1041 switch (mode) {
1042 case COMMAND_ANY:
1043 mode_str = "any";
1044 break;
1045 case COMMAND_CONFIG:
1046 mode_str = "config";
1047 break;
1048 case COMMAND_EXEC:
1049 mode_str = "exec";
1050 break;
1051 default:
1052 mode_str = "unknown";
1053 break;
1054 }
1055 Jim_SetResultString(interp, mode_str, -1);
1056 return JIM_OK;
1057 }
1058
1059 int help_del_all_commands(struct command_context *cmd_ctx)
1060 {
1061 struct help_entry *curr, *n;
1062
1063 list_for_each_entry_safe(curr, n, cmd_ctx->help_list, lh) {
1064 list_del(&curr->lh);
1065 free(curr->cmd_name);
1066 free(curr->help);
1067 free(curr->usage);
1068 free(curr);
1069 }
1070 return ERROR_OK;
1071 }
1072
1073 static int help_del_command(struct command_context *cmd_ctx, const char *cmd_name)
1074 {
1075 struct help_entry *curr;
1076
1077 list_for_each_entry(curr, cmd_ctx->help_list, lh) {
1078 if (!strcmp(cmd_name, curr->cmd_name)) {
1079 list_del(&curr->lh);
1080 free(curr->cmd_name);
1081 free(curr->help);
1082 free(curr->usage);
1083 free(curr);
1084 break;
1085 }
1086 }
1087
1088 return ERROR_OK;
1089 }
1090
1091 static int help_add_command(struct command_context *cmd_ctx,
1092 const char *cmd_name, const char *help_text, const char *usage_text)
1093 {
1094 int cmp = -1; /* add after curr */
1095 struct help_entry *curr;
1096
1097 list_for_each_entry_reverse(curr, cmd_ctx->help_list, lh) {
1098 cmp = strcmp(cmd_name, curr->cmd_name);
1099 if (cmp >= 0)
1100 break;
1101 }
1102
1103 struct help_entry *entry;
1104 if (cmp) {
1105 entry = calloc(1, sizeof(*entry));
1106 if (!entry) {
1107 LOG_ERROR("Out of memory");
1108 return ERROR_FAIL;
1109 }
1110 entry->cmd_name = strdup(cmd_name);
1111 if (!entry->cmd_name) {
1112 LOG_ERROR("Out of memory");
1113 free(entry);
1114 return ERROR_FAIL;
1115 }
1116 list_add(&entry->lh, &curr->lh);
1117 } else {
1118 entry = curr;
1119 }
1120
1121 if (help_text) {
1122 char *text = strdup(help_text);
1123 if (!text) {
1124 LOG_ERROR("Out of memory");
1125 return ERROR_FAIL;
1126 }
1127 free(entry->help);
1128 entry->help = text;
1129 }
1130
1131 if (usage_text) {
1132 char *text = strdup(usage_text);
1133 if (!text) {
1134 LOG_ERROR("Out of memory");
1135 return ERROR_FAIL;
1136 }
1137 free(entry->usage);
1138 entry->usage = text;
1139 }
1140
1141 return ERROR_OK;
1142 }
1143
1144 COMMAND_HANDLER(handle_help_add_command)
1145 {
1146 if (CMD_ARGC != 2)
1147 return ERROR_COMMAND_SYNTAX_ERROR;
1148
1149 const char *help = !strcmp(CMD_NAME, "add_help_text") ? CMD_ARGV[1] : NULL;
1150 const char *usage = !strcmp(CMD_NAME, "add_usage_text") ? CMD_ARGV[1] : NULL;
1151 if (!help && !usage) {
1152 LOG_ERROR("command name '%s' is unknown", CMD_NAME);
1153 return ERROR_COMMAND_SYNTAX_ERROR;
1154 }
1155 const char *cmd_name = CMD_ARGV[0];
1156 return help_add_command(CMD_CTX, cmd_name, help, usage);
1157 }
1158
1159 /* sleep command sleeps for <n> milliseconds
1160 * this is useful in target startup scripts
1161 */
1162 COMMAND_HANDLER(handle_sleep_command)
1163 {
1164 bool busy = false;
1165 if (CMD_ARGC == 2) {
1166 if (strcmp(CMD_ARGV[1], "busy") == 0)
1167 busy = true;
1168 else
1169 return ERROR_COMMAND_SYNTAX_ERROR;
1170 } else if (CMD_ARGC < 1 || CMD_ARGC > 2)
1171 return ERROR_COMMAND_SYNTAX_ERROR;
1172
1173 unsigned long duration = 0;
1174 int retval = parse_ulong(CMD_ARGV[0], &duration);
1175 if (ERROR_OK != retval)
1176 return retval;
1177
1178 if (!busy) {
1179 int64_t then = timeval_ms();
1180 while (timeval_ms() - then < (int64_t)duration) {
1181 target_call_timer_callbacks_now();
1182 usleep(1000);
1183 }
1184 } else
1185 busy_sleep(duration);
1186
1187 return ERROR_OK;
1188 }
1189
1190 static const struct command_registration command_subcommand_handlers[] = {
1191 {
1192 .name = "mode",
1193 .mode = COMMAND_ANY,
1194 .jim_handler = jim_command_mode,
1195 .usage = "[command_name ...]",
1196 .help = "Returns the command modes allowed by a command: "
1197 "'any', 'config', or 'exec'. If no command is "
1198 "specified, returns the current command mode. "
1199 "Returns 'unknown' if an unknown command is given. "
1200 "Command can be multiple tokens.",
1201 },
1202 COMMAND_REGISTRATION_DONE
1203 };
1204
1205 static const struct command_registration command_builtin_handlers[] = {
1206 {
1207 .name = "ocd_find",
1208 .mode = COMMAND_ANY,
1209 .jim_handler = jim_find,
1210 .help = "find full path to file",
1211 .usage = "file",
1212 },
1213 {
1214 .name = "capture",
1215 .mode = COMMAND_ANY,
1216 .jim_handler = jim_capture,
1217 .help = "Capture progress output and return as tcl return value. If the "
1218 "progress output was empty, return tcl return value.",
1219 .usage = "command",
1220 },
1221 {
1222 .name = "echo",
1223 .handler = jim_echo,
1224 .mode = COMMAND_ANY,
1225 .help = "Logs a message at \"user\" priority. "
1226 "Output message to stdout. "
1227 "Option \"-n\" suppresses trailing newline",
1228 .usage = "[-n] string",
1229 },
1230 {
1231 .name = "add_help_text",
1232 .handler = handle_help_add_command,
1233 .mode = COMMAND_ANY,
1234 .help = "Add new command help text; "
1235 "Command can be multiple tokens.",
1236 .usage = "command_name helptext_string",
1237 },
1238 {
1239 .name = "add_usage_text",
1240 .handler = handle_help_add_command,
1241 .mode = COMMAND_ANY,
1242 .help = "Add new command usage text; "
1243 "command can be multiple tokens.",
1244 .usage = "command_name usage_string",
1245 },
1246 {
1247 .name = "sleep",
1248 .handler = handle_sleep_command,
1249 .mode = COMMAND_ANY,
1250 .help = "Sleep for specified number of milliseconds. "
1251 "\"busy\" will busy wait instead (avoid this).",
1252 .usage = "milliseconds ['busy']",
1253 },
1254 {
1255 .name = "help",
1256 .handler = handle_help_command,
1257 .mode = COMMAND_ANY,
1258 .help = "Show full command help; "
1259 "command can be multiple tokens.",
1260 .usage = "[command_name]",
1261 },
1262 {
1263 .name = "usage",
1264 .handler = handle_help_command,
1265 .mode = COMMAND_ANY,
1266 .help = "Show basic command usage; "
1267 "command can be multiple tokens.",
1268 .usage = "[command_name]",
1269 },
1270 {
1271 .name = "command",
1272 .mode = COMMAND_ANY,
1273 .help = "core command group (introspection)",
1274 .chain = command_subcommand_handlers,
1275 .usage = "",
1276 },
1277 COMMAND_REGISTRATION_DONE
1278 };
1279
1280 struct command_context *command_init(const char *startup_tcl, Jim_Interp *interp)
1281 {
1282 struct command_context *context = calloc(1, sizeof(struct command_context));
1283
1284 context->mode = COMMAND_EXEC;
1285
1286 /* context can be duplicated. Put list head on separate mem-chunk to keep list consistent */
1287 context->help_list = malloc(sizeof(*context->help_list));
1288 INIT_LIST_HEAD(context->help_list);
1289
1290 /* Create a jim interpreter if we were not handed one */
1291 if (interp == NULL) {
1292 /* Create an interpreter */
1293 interp = Jim_CreateInterp();
1294 /* Add all the Jim core commands */
1295 Jim_RegisterCoreCommands(interp);
1296 Jim_InitStaticExtensions(interp);
1297 }
1298
1299 context->interp = interp;
1300
1301 register_commands(context, NULL, command_builtin_handlers);
1302
1303 Jim_SetAssocData(interp, "context", NULL, context);
1304 if (Jim_Eval_Named(interp, startup_tcl, "embedded:startup.tcl", 1) == JIM_ERR) {
1305 LOG_ERROR("Failed to run startup.tcl (embedded into OpenOCD)");
1306 Jim_MakeErrorMessage(interp);
1307 LOG_USER_N("%s", Jim_GetString(Jim_GetResult(interp), NULL));
1308 exit(-1);
1309 }
1310 Jim_DeleteAssocData(interp, "context");
1311
1312 return context;
1313 }
1314
1315 void command_exit(struct command_context *context)
1316 {
1317 if (!context)
1318 return;
1319
1320 Jim_FreeInterp(context->interp);
1321 free(context->help_list);
1322 command_done(context);
1323 }
1324
1325 int command_context_mode(struct command_context *cmd_ctx, enum command_mode mode)
1326 {
1327 if (!cmd_ctx)
1328 return ERROR_COMMAND_SYNTAX_ERROR;
1329
1330 cmd_ctx->mode = mode;
1331 return ERROR_OK;
1332 }
1333
1334 void process_jim_events(struct command_context *cmd_ctx)
1335 {
1336 static int recursion;
1337 if (recursion)
1338 return;
1339
1340 recursion++;
1341 Jim_ProcessEvents(cmd_ctx->interp, JIM_ALL_EVENTS | JIM_DONT_WAIT);
1342 recursion--;
1343 }
1344
1345 #define DEFINE_PARSE_NUM_TYPE(name, type, func, min, max) \
1346 int parse ## name(const char *str, type * ul) \
1347 { \
1348 if (!*str) { \
1349 LOG_ERROR("Invalid command argument"); \
1350 return ERROR_COMMAND_ARGUMENT_INVALID; \
1351 } \
1352 char *end; \
1353 errno = 0; \
1354 *ul = func(str, &end, 0); \
1355 if (*end) { \
1356 LOG_ERROR("Invalid command argument"); \
1357 return ERROR_COMMAND_ARGUMENT_INVALID; \
1358 } \
1359 if ((max == *ul) && (ERANGE == errno)) { \
1360 LOG_ERROR("Argument overflow"); \
1361 return ERROR_COMMAND_ARGUMENT_OVERFLOW; \
1362 } \
1363 if (min && (min == *ul) && (ERANGE == errno)) { \
1364 LOG_ERROR("Argument underflow"); \
1365 return ERROR_COMMAND_ARGUMENT_UNDERFLOW; \
1366 } \
1367 return ERROR_OK; \
1368 }
1369 DEFINE_PARSE_NUM_TYPE(_ulong, unsigned long, strtoul, 0, ULONG_MAX)
1370 DEFINE_PARSE_NUM_TYPE(_ullong, unsigned long long, strtoull, 0, ULLONG_MAX)
1371 DEFINE_PARSE_NUM_TYPE(_long, long, strtol, LONG_MIN, LONG_MAX)
1372 DEFINE_PARSE_NUM_TYPE(_llong, long long, strtoll, LLONG_MIN, LLONG_MAX)
1373
1374 #define DEFINE_PARSE_WRAPPER(name, type, min, max, functype, funcname) \
1375 int parse ## name(const char *str, type * ul) \
1376 { \
1377 functype n; \
1378 int retval = parse ## funcname(str, &n); \
1379 if (ERROR_OK != retval) \
1380 return retval; \
1381 if (n > max) \
1382 return ERROR_COMMAND_ARGUMENT_OVERFLOW; \
1383 if (min) \
1384 return ERROR_COMMAND_ARGUMENT_UNDERFLOW; \
1385 *ul = n; \
1386 return ERROR_OK; \
1387 }
1388
1389 #define DEFINE_PARSE_ULONGLONG(name, type, min, max) \
1390 DEFINE_PARSE_WRAPPER(name, type, min, max, unsigned long long, _ullong)
1391 DEFINE_PARSE_ULONGLONG(_uint, unsigned, 0, UINT_MAX)
1392 DEFINE_PARSE_ULONGLONG(_u64, uint64_t, 0, UINT64_MAX)
1393 DEFINE_PARSE_ULONGLONG(_u32, uint32_t, 0, UINT32_MAX)
1394 DEFINE_PARSE_ULONGLONG(_u16, uint16_t, 0, UINT16_MAX)
1395 DEFINE_PARSE_ULONGLONG(_u8, uint8_t, 0, UINT8_MAX)
1396
1397 DEFINE_PARSE_ULONGLONG(_target_addr, target_addr_t, 0, TARGET_ADDR_MAX)
1398
1399 #define DEFINE_PARSE_LONGLONG(name, type, min, max) \
1400 DEFINE_PARSE_WRAPPER(name, type, min, max, long long, _llong)
1401 DEFINE_PARSE_LONGLONG(_int, int, n < INT_MIN, INT_MAX)
1402 DEFINE_PARSE_LONGLONG(_s64, int64_t, n < INT64_MIN, INT64_MAX)
1403 DEFINE_PARSE_LONGLONG(_s32, int32_t, n < INT32_MIN, INT32_MAX)
1404 DEFINE_PARSE_LONGLONG(_s16, int16_t, n < INT16_MIN, INT16_MAX)
1405 DEFINE_PARSE_LONGLONG(_s8, int8_t, n < INT8_MIN, INT8_MAX)
1406
1407 static int command_parse_bool(const char *in, bool *out,
1408 const char *on, const char *off)
1409 {
1410 if (strcasecmp(in, on) == 0)
1411 *out = true;
1412 else if (strcasecmp(in, off) == 0)
1413 *out = false;
1414 else
1415 return ERROR_COMMAND_SYNTAX_ERROR;
1416 return ERROR_OK;
1417 }
1418
1419 int command_parse_bool_arg(const char *in, bool *out)
1420 {
1421 if (command_parse_bool(in, out, "on", "off") == ERROR_OK)
1422 return ERROR_OK;
1423 if (command_parse_bool(in, out, "enable", "disable") == ERROR_OK)
1424 return ERROR_OK;
1425 if (command_parse_bool(in, out, "true", "false") == ERROR_OK)
1426 return ERROR_OK;
1427 if (command_parse_bool(in, out, "yes", "no") == ERROR_OK)
1428 return ERROR_OK;
1429 if (command_parse_bool(in, out, "1", "0") == ERROR_OK)
1430 return ERROR_OK;
1431 return ERROR_COMMAND_SYNTAX_ERROR;
1432 }
1433
1434 COMMAND_HELPER(handle_command_parse_bool, bool *out, const char *label)
1435 {
1436 switch (CMD_ARGC) {
1437 case 1: {
1438 const char *in = CMD_ARGV[0];
1439 if (command_parse_bool_arg(in, out) != ERROR_OK) {
1440 LOG_ERROR("%s: argument '%s' is not valid", CMD_NAME, in);
1441 return ERROR_COMMAND_SYNTAX_ERROR;
1442 }
1443 }
1444 /* fallthrough */
1445 case 0:
1446 LOG_INFO("%s is %s", label, *out ? "enabled" : "disabled");
1447 break;
1448 default:
1449 return ERROR_COMMAND_SYNTAX_ERROR;
1450 }
1451 return ERROR_OK;
1452 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)