change all bool parsers to accept any value
[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, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #if !BUILD_ECOSBOARD
34 /* see Embedder-HOWTO.txt in Jim Tcl project hosted on BerliOS*/
35 #define JIM_EMBEDDED
36 #endif
37
38 // @todo the inclusion of target.h here is a layering violation
39 #include "target.h"
40 #include "command.h"
41 #include "configuration.h"
42 #include "log.h"
43 #include "time_support.h"
44 #include "jim-eventloop.h"
45
46
47 int fast_and_dangerous = 0;
48 Jim_Interp *interp = NULL;
49
50 static int run_command(struct command_context *context,
51 struct command *c, const char *words[], unsigned num_words);
52
53 static void tcl_output(void *privData, const char *file, unsigned line,
54 const char *function, const char *string)
55 {
56 Jim_Obj *tclOutput = (Jim_Obj *)privData;
57 Jim_AppendString(interp, tclOutput, string, strlen(string));
58 }
59
60 extern struct command_context *global_cmd_ctx;
61
62 void script_debug(Jim_Interp *interp, const char *name,
63 unsigned argc, Jim_Obj *const *argv)
64 {
65 LOG_DEBUG("command - %s", name);
66 for (unsigned i = 0; i < argc; i++)
67 {
68 int len;
69 const char *w = Jim_GetString(argv[i], &len);
70
71 /* end of line comment? */
72 if (*w == '#')
73 break;
74
75 LOG_DEBUG("%s - argv[%d]=%s", name, i, w);
76 }
77 }
78
79 static int script_command(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
80 {
81 /* the private data is stashed in the interp structure */
82 struct command *c;
83 struct command_context *context;
84 int retval;
85 int i;
86 int nwords;
87 char **words;
88
89 /* DANGER!!!! be careful what we invoke here, since interp->cmdPrivData might
90 * get overwritten by running other Jim commands! Treat it as an
91 * emphemeral global variable that is used in lieu of an argument
92 * to the fn and fish it out manually.
93 */
94 c = interp->cmdPrivData;
95 if (c == NULL)
96 {
97 LOG_ERROR("BUG: interp->cmdPrivData == NULL");
98 return JIM_ERR;
99 }
100 target_call_timer_callbacks_now();
101 LOG_USER_N("%s", ""); /* Keep GDB connection alive*/
102
103 script_debug(interp, c->name, argc, argv);
104
105 words = malloc(argc * sizeof(char *));
106 for (i = 0; i < argc; i++)
107 {
108 int len;
109 const char *w = Jim_GetString(argv[i], &len);
110 if (*w=='#')
111 {
112 /* hit an end of line comment */
113 break;
114 }
115 words[i] = strdup(w);
116 if (words[i] == NULL)
117 {
118 int j;
119 for (j = 0; j < i; j++)
120 free(words[j]);
121 free(words);
122 return JIM_ERR;
123 }
124 }
125 nwords = i;
126
127 /* grab the command context from the associated data */
128 context = Jim_GetAssocData(interp, "context");
129 if (context == NULL)
130 {
131 /* Tcl can invoke commands directly instead of via command_run_line(). This would
132 * happen when the Jim Tcl interpreter is provided by eCos.
133 */
134 context = global_cmd_ctx;
135 }
136
137 /* capture log output and return it */
138 Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
139 /* a garbage collect can happen, so we need a reference count to this object */
140 Jim_IncrRefCount(tclOutput);
141
142 log_add_callback(tcl_output, tclOutput);
143
144 // turn words[0] into CMD_ARGV[-1] with this cast
145 retval = run_command(context, c, (const char **)words, nwords);
146
147 log_remove_callback(tcl_output, tclOutput);
148
149 /* We dump output into this local variable */
150 Jim_SetResult(interp, tclOutput);
151 Jim_DecrRefCount(interp, tclOutput);
152
153 for (i = 0; i < nwords; i++)
154 free(words[i]);
155 free(words);
156
157 int *return_retval = Jim_GetAssocData(interp, "retval");
158 if (return_retval != NULL)
159 {
160 *return_retval = retval;
161 }
162
163 return (retval == ERROR_OK)?JIM_OK:JIM_ERR;
164 }
165
166 static Jim_Obj *command_name_list(struct command *c)
167 {
168 Jim_Obj *cmd_list = c->parent ?
169 command_name_list(c->parent) :
170 Jim_NewListObj(interp, NULL, 0);
171 Jim_ListAppendElement(interp, cmd_list,
172 Jim_NewStringObj(interp, c->name, -1));
173
174 return cmd_list;
175 }
176
177 static void command_helptext_add(Jim_Obj *cmd_list, const char *help)
178 {
179 Jim_Obj *cmd_entry = Jim_NewListObj(interp, NULL, 0);
180 Jim_ListAppendElement(interp, cmd_entry, cmd_list);
181 Jim_ListAppendElement(interp, cmd_entry,
182 Jim_NewStringObj(interp, help ? : "", -1));
183
184 /* accumulate help text in Tcl helptext list. */
185 Jim_Obj *helptext = Jim_GetGlobalVariableStr(interp,
186 "ocd_helptext", JIM_ERRMSG);
187 if (Jim_IsShared(helptext))
188 helptext = Jim_DuplicateObj(interp, helptext);
189 Jim_ListAppendElement(interp, helptext, cmd_entry);
190 }
191
192 /* nice short description of source file */
193 #define __THIS__FILE__ "command.c"
194
195 /**
196 * Find a command by name from a list of commands.
197 * @returns The named command if found, or NULL.
198 */
199 static struct command *command_find(struct command **head, const char *name)
200 {
201 assert(head);
202 for (struct command *cc = *head; cc; cc = cc->next)
203 {
204 if (strcmp(cc->name, name) == 0)
205 return cc;
206 }
207 return NULL;
208 }
209
210 /**
211 * Add the command to the end of linked list.
212 * @returns Returns false if the named command already exists in the list.
213 * Returns true otherwise.
214 */
215 static void command_add_child(struct command **head, struct command *c)
216 {
217 assert(head);
218 if (NULL == *head)
219 {
220 *head = c;
221 return;
222 }
223 struct command *cc = *head;
224 while (cc->next) cc = cc->next;
225 cc->next = c;
226 }
227
228 struct command* register_command(struct command_context *context,
229 struct command *parent, char *name, command_handler_t handler,
230 enum command_mode mode, char *help)
231 {
232 if (!context || !name)
233 return NULL;
234
235 struct command **head = parent ? &parent->children : &context->commands;
236 struct command *c = command_find(head, name);
237 if (NULL != c)
238 return c;
239
240 c = malloc(sizeof(struct command));
241
242 c->name = strdup(name);
243 c->parent = parent;
244 c->children = NULL;
245 c->handler = handler;
246 c->mode = mode;
247 c->next = NULL;
248
249 command_add_child(head, c);
250
251 command_helptext_add(command_name_list(c), help);
252
253 /* just a placeholder, no handler */
254 if (c->handler == NULL)
255 return c;
256
257 const char *full_name = command_name(c, '_');
258
259 const char *ocd_name = alloc_printf("ocd_%s", full_name);
260 Jim_CreateCommand(interp, ocd_name, script_command, c, NULL);
261 free((void *)ocd_name);
262
263 /* we now need to add an overrideable proc */
264 const char *override_name = alloc_printf("proc %s {args} {"
265 "if {[catch {eval ocd_%s $args}] == 0} "
266 "{return \"\"} else {return -code error}}",
267 full_name, full_name);
268 Jim_Eval_Named(interp, override_name, __THIS__FILE__, __LINE__);
269 free((void *)override_name);
270
271 free((void *)full_name);
272
273 return c;
274 }
275
276 int unregister_all_commands(struct command_context *context)
277 {
278 struct command *c, *c2;
279
280 if (context == NULL)
281 return ERROR_OK;
282
283 while (NULL != context->commands)
284 {
285 c = context->commands;
286
287 while (NULL != c->children)
288 {
289 c2 = c->children;
290 c->children = c->children->next;
291 free(c2->name);
292 c2->name = NULL;
293 free(c2);
294 c2 = NULL;
295 }
296
297 context->commands = context->commands->next;
298
299 free(c->name);
300 c->name = NULL;
301 free(c);
302 c = NULL;
303 }
304
305 return ERROR_OK;
306 }
307
308 int unregister_command(struct command_context *context, char *name)
309 {
310 struct command *c, *p = NULL, *c2;
311
312 if ((!context) || (!name))
313 return ERROR_INVALID_ARGUMENTS;
314
315 /* find command */
316 c = context->commands;
317
318 while (NULL != c)
319 {
320 if (strcmp(name, c->name) == 0)
321 {
322 /* unlink command */
323 if (p)
324 {
325 p->next = c->next;
326 }
327 else
328 {
329 /* first element in command list */
330 context->commands = c->next;
331 }
332
333 /* unregister children */
334 while (NULL != c->children)
335 {
336 c2 = c->children;
337 c->children = c->children->next;
338 free(c2->name);
339 c2->name = NULL;
340 free(c2);
341 c2 = NULL;
342 }
343
344 /* delete command */
345 free(c->name);
346 c->name = NULL;
347 free(c);
348 c = NULL;
349 return ERROR_OK;
350 }
351
352 /* remember the last command for unlinking */
353 p = c;
354 c = c->next;
355 }
356
357 return ERROR_OK;
358 }
359
360 void command_output_text(struct command_context *context, const char *data)
361 {
362 if (context && context->output_handler && data) {
363 context->output_handler(context, data);
364 }
365 }
366
367 void command_print_sameline(struct command_context *context, const char *format, ...)
368 {
369 char *string;
370
371 va_list ap;
372 va_start(ap, format);
373
374 string = alloc_vprintf(format, ap);
375 if (string != NULL)
376 {
377 /* we want this collected in the log + we also want to pick it up as a tcl return
378 * value.
379 *
380 * The latter bit isn't precisely neat, but will do for now.
381 */
382 LOG_USER_N("%s", string);
383 /* We already printed it above */
384 /* command_output_text(context, string); */
385 free(string);
386 }
387
388 va_end(ap);
389 }
390
391 void command_print(struct command_context *context, const char *format, ...)
392 {
393 char *string;
394
395 va_list ap;
396 va_start(ap, format);
397
398 string = alloc_vprintf(format, ap);
399 if (string != NULL)
400 {
401 strcat(string, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one char longer */
402 /* we want this collected in the log + we also want to pick it up as a tcl return
403 * value.
404 *
405 * The latter bit isn't precisely neat, but will do for now.
406 */
407 LOG_USER_N("%s", string);
408 /* We already printed it above */
409 /* command_output_text(context, string); */
410 free(string);
411 }
412
413 va_end(ap);
414 }
415
416 static char *__command_name(struct command *c, char delim, unsigned extra)
417 {
418 char *name;
419 unsigned len = strlen(c->name);
420 if (NULL == c->parent) {
421 // allocate enough for the name, child names, and '\0'
422 name = malloc(len + extra + 1);
423 strcpy(name, c->name);
424 } else {
425 // parent's extra must include both the space and name
426 name = __command_name(c->parent, delim, 1 + len + extra);
427 char dstr[2] = { delim, 0 };
428 strcat(name, dstr);
429 strcat(name, c->name);
430 }
431 return name;
432 }
433 char *command_name(struct command *c, char delim)
434 {
435 return __command_name(c, delim, 0);
436 }
437
438 static int run_command(struct command_context *context,
439 struct command *c, const char *words[], unsigned num_words)
440 {
441 if (!((context->mode == COMMAND_CONFIG) || (c->mode == COMMAND_ANY) || (c->mode == context->mode)))
442 {
443 /* Config commands can not run after the config stage */
444 LOG_ERROR("Command '%s' only runs during configuration stage", c->name);
445 return ERROR_FAIL;
446 }
447
448 struct command_invocation cmd = {
449 .ctx = context,
450 .name = c->name,
451 .argc = num_words - 1,
452 .argv = words + 1,
453 };
454 int retval = c->handler(&cmd);
455 if (retval == ERROR_COMMAND_SYNTAX_ERROR)
456 {
457 /* Print help for command */
458 char *full_name = command_name(c, ' ');
459 if (NULL != full_name) {
460 command_run_linef(context, "help %s", full_name);
461 free(full_name);
462 } else
463 retval = -ENOMEM;
464 }
465 else if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
466 {
467 /* just fall through for a shutdown request */
468 }
469 else if (retval != ERROR_OK)
470 {
471 /* we do not print out an error message because the command *should*
472 * have printed out an error
473 */
474 LOG_DEBUG("Command failed with error code %d", retval);
475 }
476
477 return retval;
478 }
479
480 int command_run_line(struct command_context *context, char *line)
481 {
482 /* all the parent commands have been registered with the interpreter
483 * so, can just evaluate the line as a script and check for
484 * results
485 */
486 /* run the line thru a script engine */
487 int retval = ERROR_FAIL;
488 int retcode;
489 /* Beware! This code needs to be reentrant. It is also possible
490 * for OpenOCD commands to be invoked directly from Tcl. This would
491 * happen when the Jim Tcl interpreter is provided by eCos for
492 * instance.
493 */
494 Jim_DeleteAssocData(interp, "context");
495 retcode = Jim_SetAssocData(interp, "context", NULL, context);
496 if (retcode == JIM_OK)
497 {
498 /* associated the return value */
499 Jim_DeleteAssocData(interp, "retval");
500 retcode = Jim_SetAssocData(interp, "retval", NULL, &retval);
501 if (retcode == JIM_OK)
502 {
503 retcode = Jim_Eval_Named(interp, line, __THIS__FILE__, __LINE__);
504
505 Jim_DeleteAssocData(interp, "retval");
506 }
507 Jim_DeleteAssocData(interp, "context");
508 }
509 if (retcode == JIM_ERR) {
510 if (retval != ERROR_COMMAND_CLOSE_CONNECTION)
511 {
512 /* We do not print the connection closed error message */
513 Jim_PrintErrorMessage(interp);
514 }
515 if (retval == ERROR_OK)
516 {
517 /* It wasn't a low level OpenOCD command that failed */
518 return ERROR_FAIL;
519 }
520 return retval;
521 } else if (retcode == JIM_EXIT) {
522 /* ignore. */
523 /* exit(Jim_GetExitCode(interp)); */
524 } else {
525 const char *result;
526 int reslen;
527
528 result = Jim_GetString(Jim_GetResult(interp), &reslen);
529 if (reslen > 0)
530 {
531 int i;
532 char buff[256 + 1];
533 for (i = 0; i < reslen; i += 256)
534 {
535 int chunk;
536 chunk = reslen - i;
537 if (chunk > 256)
538 chunk = 256;
539 strncpy(buff, result + i, chunk);
540 buff[chunk] = 0;
541 LOG_USER_N("%s", buff);
542 }
543 LOG_USER_N("%s", "\n");
544 }
545 retval = ERROR_OK;
546 }
547 return retval;
548 }
549
550 int command_run_linef(struct command_context *context, const char *format, ...)
551 {
552 int retval = ERROR_FAIL;
553 char *string;
554 va_list ap;
555 va_start(ap, format);
556 string = alloc_vprintf(format, ap);
557 if (string != NULL)
558 {
559 retval = command_run_line(context, string);
560 }
561 va_end(ap);
562 return retval;
563 }
564
565 void command_set_output_handler(struct command_context* context,
566 command_output_handler_t output_handler, void *priv)
567 {
568 context->output_handler = output_handler;
569 context->output_handler_priv = priv;
570 }
571
572 struct command_context* copy_command_context(struct command_context* context)
573 {
574 struct command_context* copy_context = malloc(sizeof(struct command_context));
575
576 *copy_context = *context;
577
578 return copy_context;
579 }
580
581 int command_done(struct command_context *context)
582 {
583 free(context);
584 context = NULL;
585
586 return ERROR_OK;
587 }
588
589 /* find full path to file */
590 static int jim_find(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
591 {
592 if (argc != 2)
593 return JIM_ERR;
594 const char *file = Jim_GetString(argv[1], NULL);
595 char *full_path = find_file(file);
596 if (full_path == NULL)
597 return JIM_ERR;
598 Jim_Obj *result = Jim_NewStringObj(interp, full_path, strlen(full_path));
599 free(full_path);
600
601 Jim_SetResult(interp, result);
602 return JIM_OK;
603 }
604
605 static int jim_echo(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
606 {
607 if (argc != 2)
608 return JIM_ERR;
609 const char *str = Jim_GetString(argv[1], NULL);
610 LOG_USER("%s", str);
611 return JIM_OK;
612 }
613
614 static size_t openocd_jim_fwrite(const void *_ptr, size_t size, size_t n, void *cookie)
615 {
616 size_t nbytes;
617 const char *ptr;
618 Jim_Interp *interp;
619
620 /* make it a char easier to read code */
621 ptr = _ptr;
622 interp = cookie;
623 nbytes = size * n;
624 if (ptr == NULL || interp == NULL || nbytes == 0) {
625 return 0;
626 }
627
628 /* do we have to chunk it? */
629 if (ptr[nbytes] == 0)
630 {
631 /* no it is a C style string */
632 LOG_USER_N("%s", ptr);
633 return strlen(ptr);
634 }
635 /* GRR we must chunk - not null terminated */
636 while (nbytes) {
637 char chunk[128 + 1];
638 int x;
639
640 x = nbytes;
641 if (x > 128) {
642 x = 128;
643 }
644 /* copy it */
645 memcpy(chunk, ptr, x);
646 /* terminate it */
647 chunk[n] = 0;
648 /* output it */
649 LOG_USER_N("%s", chunk);
650 ptr += x;
651 nbytes -= x;
652 }
653
654 return n;
655 }
656
657 static size_t openocd_jim_fread(void *ptr, size_t size, size_t n, void *cookie)
658 {
659 /* TCL wants to read... tell him no */
660 return 0;
661 }
662
663 static int openocd_jim_vfprintf(void *cookie, const char *fmt, va_list ap)
664 {
665 char *cp;
666 int n;
667 Jim_Interp *interp;
668
669 n = -1;
670 interp = cookie;
671 if (interp == NULL)
672 return n;
673
674 cp = alloc_vprintf(fmt, ap);
675 if (cp)
676 {
677 LOG_USER_N("%s", cp);
678 n = strlen(cp);
679 free(cp);
680 }
681 return n;
682 }
683
684 static int openocd_jim_fflush(void *cookie)
685 {
686 /* nothing to flush */
687 return 0;
688 }
689
690 static char* openocd_jim_fgets(char *s, int size, void *cookie)
691 {
692 /* not supported */
693 errno = ENOTSUP;
694 return NULL;
695 }
696
697 static int jim_capture(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
698 {
699 if (argc != 2)
700 return JIM_ERR;
701 int retcode;
702 const char *str = Jim_GetString(argv[1], NULL);
703
704 /* capture log output and return it */
705 Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
706 /* a garbage collect can happen, so we need a reference count to this object */
707 Jim_IncrRefCount(tclOutput);
708
709 log_add_callback(tcl_output, tclOutput);
710
711 retcode = Jim_Eval_Named(interp, str, __THIS__FILE__, __LINE__);
712
713 log_remove_callback(tcl_output, tclOutput);
714
715 /* We dump output into this local variable */
716 Jim_SetResult(interp, tclOutput);
717 Jim_DecrRefCount(interp, tclOutput);
718
719 return retcode;
720 }
721
722 /* sleep command sleeps for <n> miliseconds
723 * this is useful in target startup scripts
724 */
725 COMMAND_HANDLER(handle_sleep_command)
726 {
727 bool busy = false;
728 if (CMD_ARGC == 2)
729 {
730 if (strcmp(CMD_ARGV[1], "busy") == 0)
731 busy = true;
732 else
733 return ERROR_COMMAND_SYNTAX_ERROR;
734 }
735 else if (CMD_ARGC < 1 || CMD_ARGC > 2)
736 return ERROR_COMMAND_SYNTAX_ERROR;
737
738 unsigned long duration = 0;
739 int retval = parse_ulong(CMD_ARGV[0], &duration);
740 if (ERROR_OK != retval)
741 return retval;
742
743 if (!busy)
744 {
745 long long then = timeval_ms();
746 while (timeval_ms() - then < (long long)duration)
747 {
748 target_call_timer_callbacks_now();
749 usleep(1000);
750 }
751 }
752 else
753 busy_sleep(duration);
754
755 return ERROR_OK;
756 }
757
758 COMMAND_HANDLER(handle_fast_command)
759 {
760 if (CMD_ARGC != 1)
761 return ERROR_COMMAND_SYNTAX_ERROR;
762
763 fast_and_dangerous = strcmp("enable", CMD_ARGV[0]) == 0;
764
765 return ERROR_OK;
766 }
767
768
769 struct command_context* command_init(const char *startup_tcl)
770 {
771 struct command_context* context = malloc(sizeof(struct command_context));
772 const char *HostOs;
773
774 context->mode = COMMAND_EXEC;
775 context->commands = NULL;
776 context->current_target = 0;
777 context->output_handler = NULL;
778 context->output_handler_priv = NULL;
779
780 #if !BUILD_ECOSBOARD
781 Jim_InitEmbedded();
782 /* Create an interpreter */
783 interp = Jim_CreateInterp();
784 /* Add all the Jim core commands */
785 Jim_RegisterCoreCommands(interp);
786 #endif
787
788 #if defined(_MSC_VER)
789 /* WinXX - is generic, the forward
790 * looking problem is this:
791 *
792 * "win32" or "win64"
793 *
794 * "winxx" is generic.
795 */
796 HostOs = "winxx";
797 #elif defined(__linux__)
798 HostOs = "linux";
799 #elif defined(__DARWIN__)
800 HostOs = "darwin";
801 #elif defined(__CYGWIN__)
802 HostOs = "cygwin";
803 #elif defined(__MINGW32__)
804 HostOs = "mingw32";
805 #elif defined(__ECOS)
806 HostOs = "ecos";
807 #else
808 #warn unrecognized host OS...
809 HostOs = "other";
810 #endif
811 Jim_SetGlobalVariableStr(interp, "ocd_HOSTOS",
812 Jim_NewStringObj(interp, HostOs , strlen(HostOs)));
813
814 Jim_CreateCommand(interp, "ocd_find", jim_find, NULL, NULL);
815 Jim_CreateCommand(interp, "echo", jim_echo, NULL, NULL);
816 Jim_CreateCommand(interp, "capture", jim_capture, NULL, NULL);
817
818 /* Set Jim's STDIO */
819 interp->cookie_stdin = interp;
820 interp->cookie_stdout = interp;
821 interp->cookie_stderr = interp;
822 interp->cb_fwrite = openocd_jim_fwrite;
823 interp->cb_fread = openocd_jim_fread ;
824 interp->cb_vfprintf = openocd_jim_vfprintf;
825 interp->cb_fflush = openocd_jim_fflush;
826 interp->cb_fgets = openocd_jim_fgets;
827
828 #if !BUILD_ECOSBOARD
829 Jim_EventLoopOnLoad(interp);
830 #endif
831 if (Jim_Eval_Named(interp, startup_tcl, "embedded:startup.tcl",1) == JIM_ERR)
832 {
833 LOG_ERROR("Failed to run startup.tcl (embedded into OpenOCD)");
834 Jim_PrintErrorMessage(interp);
835 exit(-1);
836 }
837
838 register_command(context, NULL, "sleep",
839 handle_sleep_command, COMMAND_ANY,
840 "<n> [busy] - sleep for n milliseconds. "
841 "\"busy\" means busy wait");
842 register_command(context, NULL, "fast",
843 handle_fast_command, COMMAND_ANY,
844 "fast <enable/disable> - place at beginning of "
845 "config files. Sets defaults to fast and dangerous.");
846
847 return context;
848 }
849
850 int command_context_mode(struct command_context *cmd_ctx, enum command_mode mode)
851 {
852 if (!cmd_ctx)
853 return ERROR_INVALID_ARGUMENTS;
854
855 cmd_ctx->mode = mode;
856 return ERROR_OK;
857 }
858
859 void process_jim_events(void)
860 {
861 #if !BUILD_ECOSBOARD
862 static int recursion = 0;
863
864 if (!recursion)
865 {
866 recursion++;
867 Jim_ProcessEvents (interp, JIM_ALL_EVENTS | JIM_DONT_WAIT);
868 recursion--;
869 }
870 #endif
871 }
872
873 void register_jim(struct command_context *cmd_ctx, const char *name,
874 Jim_CmdProc cmd, const char *help)
875 {
876 Jim_CreateCommand(interp, name, cmd, NULL, NULL);
877
878 Jim_Obj *cmd_list = Jim_NewListObj(interp, NULL, 0);
879 Jim_ListAppendElement(interp, cmd_list,
880 Jim_NewStringObj(interp, name, -1));
881
882 command_helptext_add(cmd_list, help);
883 }
884
885 /* return global variable long value or 0 upon failure */
886 long jim_global_long(const char *variable)
887 {
888 Jim_Obj *objPtr = Jim_GetGlobalVariableStr(interp, variable, JIM_ERRMSG);
889 long t;
890 if (Jim_GetLong(interp, objPtr, &t) == JIM_OK)
891 {
892 return t;
893 }
894 return 0;
895 }
896
897 #define DEFINE_PARSE_NUM_TYPE(name, type, func, min, max) \
898 int parse##name(const char *str, type *ul) \
899 { \
900 if (!*str) \
901 { \
902 LOG_ERROR("Invalid command argument"); \
903 return ERROR_COMMAND_ARGUMENT_INVALID; \
904 } \
905 char *end; \
906 *ul = func(str, &end, 0); \
907 if (*end) \
908 { \
909 LOG_ERROR("Invalid command argument"); \
910 return ERROR_COMMAND_ARGUMENT_INVALID; \
911 } \
912 if ((max == *ul) && (ERANGE == errno)) \
913 { \
914 LOG_ERROR("Argument overflow"); \
915 return ERROR_COMMAND_ARGUMENT_OVERFLOW; \
916 } \
917 if (min && (min == *ul) && (ERANGE == errno)) \
918 { \
919 LOG_ERROR("Argument underflow"); \
920 return ERROR_COMMAND_ARGUMENT_UNDERFLOW; \
921 } \
922 return ERROR_OK; \
923 }
924 DEFINE_PARSE_NUM_TYPE(_ulong, unsigned long , strtoul, 0, ULONG_MAX)
925 DEFINE_PARSE_NUM_TYPE(_ullong, unsigned long long, strtoull, 0, ULLONG_MAX)
926 DEFINE_PARSE_NUM_TYPE(_long, long , strtol, LONG_MIN, LONG_MAX)
927 DEFINE_PARSE_NUM_TYPE(_llong, long long, strtoll, LLONG_MIN, LLONG_MAX)
928
929 #define DEFINE_PARSE_WRAPPER(name, type, min, max, functype, funcname) \
930 int parse##name(const char *str, type *ul) \
931 { \
932 functype n; \
933 int retval = parse##funcname(str, &n); \
934 if (ERROR_OK != retval) \
935 return retval; \
936 if (n > max) \
937 return ERROR_COMMAND_ARGUMENT_OVERFLOW; \
938 if (min) \
939 return ERROR_COMMAND_ARGUMENT_UNDERFLOW; \
940 *ul = n; \
941 return ERROR_OK; \
942 }
943
944 #define DEFINE_PARSE_ULONG(name, type, min, max) \
945 DEFINE_PARSE_WRAPPER(name, type, min, max, unsigned long, _ulong)
946 DEFINE_PARSE_ULONG(_uint, unsigned, 0, UINT_MAX)
947 DEFINE_PARSE_ULONG(_u32, uint32_t, 0, UINT32_MAX)
948 DEFINE_PARSE_ULONG(_u16, uint16_t, 0, UINT16_MAX)
949 DEFINE_PARSE_ULONG(_u8, uint8_t, 0, UINT8_MAX)
950
951 #define DEFINE_PARSE_LONG(name, type, min, max) \
952 DEFINE_PARSE_WRAPPER(name, type, min, max, long, _long)
953 DEFINE_PARSE_LONG(_int, int, n < INT_MIN, INT_MAX)
954 DEFINE_PARSE_LONG(_s32, int32_t, n < INT32_MIN, INT32_MAX)
955 DEFINE_PARSE_LONG(_s16, int16_t, n < INT16_MIN, INT16_MAX)
956 DEFINE_PARSE_LONG(_s8, int8_t, n < INT8_MIN, INT8_MAX)
957
958 static int command_parse_bool(const char *in, bool *out,
959 const char *on, const char *off)
960 {
961 if (strcasecmp(in, on) == 0)
962 *out = true;
963 else if (strcasecmp(in, off) == 0)
964 *out = false;
965 else
966 return ERROR_COMMAND_SYNTAX_ERROR;
967 return ERROR_OK;
968 }
969
970 int command_parse_bool_arg(const char *in, bool *out)
971 {
972 if (command_parse_bool(in, out, "on", "off") == ERROR_OK)
973 return ERROR_OK;
974 if (command_parse_bool(in, out, "enable", "disable") == ERROR_OK)
975 return ERROR_OK;
976 if (command_parse_bool(in, out, "true", "false") == ERROR_OK)
977 return ERROR_OK;
978 if (command_parse_bool(in, out, "yes", "no") == ERROR_OK)
979 return ERROR_OK;
980 if (command_parse_bool(in, out, "1", "0") == ERROR_OK)
981 return ERROR_OK;
982 return ERROR_INVALID_ARGUMENTS;
983 }
984
985 COMMAND_HELPER(handle_command_parse_bool, bool *out, const char *label)
986 {
987 switch (CMD_ARGC) {
988 case 1: {
989 const char *in = CMD_ARGV[0];
990 if (command_parse_bool_arg(in, out) != ERROR_OK)
991 {
992 LOG_ERROR("%s: argument '%s' is not valid", CMD_NAME, in);
993 return ERROR_INVALID_ARGUMENTS;
994 }
995 // fall through
996 }
997 case 0:
998 LOG_INFO("%s is %s", label, *out ? "enabled" : "disabled");
999 break;
1000 default:
1001 return ERROR_INVALID_ARGUMENTS;
1002 }
1003 return ERROR_OK;
1004 }
1005
1006

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)