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

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)