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

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)