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

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)