add struct command_invocation for COMMAND_HANDLER
[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(sizeof(char *) * (argc + 1));
106 words[0] = c->name;
107 for (i = 0; i < argc; i++)
108 {
109 int len;
110 const char *w = Jim_GetString(argv[i], &len);
111 if (*w=='#')
112 {
113 /* hit an end of line comment */
114 break;
115 }
116 words[i + 1] = strdup(w);
117 if (words[i + 1] == NULL)
118 {
119 int j;
120 for (j = 0; j < i; j++)
121 free(words[j + 1]);
122 free(words);
123 return JIM_ERR;
124 }
125 }
126 nwords = i;
127
128 /* grab the command context from the associated data */
129 context = Jim_GetAssocData(interp, "context");
130 if (context == NULL)
131 {
132 /* Tcl can invoke commands directly instead of via command_run_line(). This would
133 * happen when the Jim Tcl interpreter is provided by eCos.
134 */
135 context = global_cmd_ctx;
136 }
137
138 /* capture log output and return it */
139 Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
140 /* a garbage collect can happen, so we need a reference count to this object */
141 Jim_IncrRefCount(tclOutput);
142
143 log_add_callback(tcl_output, tclOutput);
144
145 // turn words[0] into CMD_ARGV[-1] with this cast
146 retval = run_command(context, c, (const char **)words + 1, nwords);
147
148 log_remove_callback(tcl_output, tclOutput);
149
150 /* We dump output into this local variable */
151 Jim_SetResult(interp, tclOutput);
152 Jim_DecrRefCount(interp, tclOutput);
153
154 for (i = 0; i < nwords; i++)
155 free(words[i + 1]);
156 free(words);
157
158 int *return_retval = Jim_GetAssocData(interp, "retval");
159 if (return_retval != NULL)
160 {
161 *return_retval = retval;
162 }
163
164 return (retval == ERROR_OK)?JIM_OK:JIM_ERR;
165 }
166
167 static Jim_Obj *command_name_list(struct command *c)
168 {
169 Jim_Obj *cmd_list = c->parent ?
170 command_name_list(c->parent) :
171 Jim_NewListObj(interp, NULL, 0);
172 Jim_ListAppendElement(interp, cmd_list,
173 Jim_NewStringObj(interp, c->name, -1));
174
175 return cmd_list;
176 }
177
178 static void command_helptext_add(Jim_Obj *cmd_list, const char *help)
179 {
180 Jim_Obj *cmd_entry = Jim_NewListObj(interp, NULL, 0);
181 Jim_ListAppendElement(interp, cmd_entry, cmd_list);
182 Jim_ListAppendElement(interp, cmd_entry,
183 Jim_NewStringObj(interp, help ? : "", -1));
184
185 /* accumulate help text in Tcl helptext list. */
186 Jim_Obj *helptext = Jim_GetGlobalVariableStr(interp,
187 "ocd_helptext", JIM_ERRMSG);
188 if (Jim_IsShared(helptext))
189 helptext = Jim_DuplicateObj(interp, helptext);
190 Jim_ListAppendElement(interp, helptext, cmd_entry);
191 }
192
193 /* nice short description of source file */
194 #define __THIS__FILE__ "command.c"
195
196 /**
197 * Find a command by name from a list of commands.
198 * @returns The named command if found, or NULL.
199 */
200 static struct command *command_find(struct command **head, const char *name)
201 {
202 assert(head);
203 for (struct command *cc = *head; cc; cc = cc->next)
204 {
205 if (strcmp(cc->name, name) == 0)
206 return cc;
207 }
208 return NULL;
209 }
210
211 /**
212 * Add the command to the end of linked list.
213 * @returns Returns false if the named command already exists in the list.
214 * Returns true otherwise.
215 */
216 static void command_add_child(struct command **head, struct command *c)
217 {
218 assert(head);
219 if (NULL == *head)
220 {
221 *head = c;
222 return;
223 }
224 struct command *cc = *head;
225 while (cc->next) cc = cc->next;
226 cc->next = c;
227 }
228
229 struct command* register_command(struct command_context *context,
230 struct command *parent, char *name, command_handler_t handler,
231 enum command_mode mode, char *help)
232 {
233 if (!context || !name)
234 return NULL;
235
236 struct command **head = parent ? &parent->children : &context->commands;
237 struct command *c = command_find(head, name);
238 if (NULL != c)
239 return c;
240
241 c = malloc(sizeof(struct command));
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(struct command_context *context)
278 {
279 struct command *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(struct command_context *context, char *name)
310 {
311 struct command *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(struct command_context *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(struct command_context *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(struct command_context *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 *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 *c, char delim)
435 {
436 return __command_name(c, delim, 0);
437 }
438
439 static int run_command(struct command_context *context,
440 struct command *c, const 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 struct command_invocation cmd = {
451 .ctx = context,
452 .argc = num_words - start_word - 1,
453 .argv = words + start_word + 1,
454 };
455 int retval = c->handler(&cmd);
456 if (retval == ERROR_COMMAND_SYNTAX_ERROR)
457 {
458 /* Print help for command */
459 char *full_name = command_name(c, ' ');
460 if (NULL != full_name) {
461 command_run_linef(context, "help %s", full_name);
462 free(full_name);
463 } else
464 retval = -ENOMEM;
465 }
466 else if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
467 {
468 /* just fall through for a shutdown request */
469 }
470 else if (retval != ERROR_OK)
471 {
472 /* we do not print out an error message because the command *should*
473 * have printed out an error
474 */
475 LOG_DEBUG("Command failed with error code %d", retval);
476 }
477
478 return retval;
479 }
480
481 int command_run_line(struct command_context *context, char *line)
482 {
483 /* all the parent commands have been registered with the interpreter
484 * so, can just evaluate the line as a script and check for
485 * results
486 */
487 /* run the line thru a script engine */
488 int retval = ERROR_FAIL;
489 int retcode;
490 /* Beware! This code needs to be reentrant. It is also possible
491 * for OpenOCD commands to be invoked directly from Tcl. This would
492 * happen when the Jim Tcl interpreter is provided by eCos for
493 * instance.
494 */
495 Jim_DeleteAssocData(interp, "context");
496 retcode = Jim_SetAssocData(interp, "context", NULL, context);
497 if (retcode == JIM_OK)
498 {
499 /* associated the return value */
500 Jim_DeleteAssocData(interp, "retval");
501 retcode = Jim_SetAssocData(interp, "retval", NULL, &retval);
502 if (retcode == JIM_OK)
503 {
504 retcode = Jim_Eval_Named(interp, line, __THIS__FILE__, __LINE__);
505
506 Jim_DeleteAssocData(interp, "retval");
507 }
508 Jim_DeleteAssocData(interp, "context");
509 }
510 if (retcode == JIM_ERR) {
511 if (retval != ERROR_COMMAND_CLOSE_CONNECTION)
512 {
513 /* We do not print the connection closed error message */
514 Jim_PrintErrorMessage(interp);
515 }
516 if (retval == ERROR_OK)
517 {
518 /* It wasn't a low level OpenOCD command that failed */
519 return ERROR_FAIL;
520 }
521 return retval;
522 } else if (retcode == JIM_EXIT) {
523 /* ignore. */
524 /* exit(Jim_GetExitCode(interp)); */
525 } else {
526 const char *result;
527 int reslen;
528
529 result = Jim_GetString(Jim_GetResult(interp), &reslen);
530 if (reslen > 0)
531 {
532 int i;
533 char buff[256 + 1];
534 for (i = 0; i < reslen; i += 256)
535 {
536 int chunk;
537 chunk = reslen - i;
538 if (chunk > 256)
539 chunk = 256;
540 strncpy(buff, result + i, chunk);
541 buff[chunk] = 0;
542 LOG_USER_N("%s", buff);
543 }
544 LOG_USER_N("%s", "\n");
545 }
546 retval = ERROR_OK;
547 }
548 return retval;
549 }
550
551 int command_run_linef(struct command_context *context, const char *format, ...)
552 {
553 int retval = ERROR_FAIL;
554 char *string;
555 va_list ap;
556 va_start(ap, format);
557 string = alloc_vprintf(format, ap);
558 if (string != NULL)
559 {
560 retval = command_run_line(context, string);
561 }
562 va_end(ap);
563 return retval;
564 }
565
566 void command_set_output_handler(struct command_context* context,
567 command_output_handler_t output_handler, void *priv)
568 {
569 context->output_handler = output_handler;
570 context->output_handler_priv = priv;
571 }
572
573 struct command_context* copy_command_context(struct command_context* context)
574 {
575 struct command_context* copy_context = malloc(sizeof(struct command_context));
576
577 *copy_context = *context;
578
579 return copy_context;
580 }
581
582 int command_done(struct command_context *context)
583 {
584 free(context);
585 context = NULL;
586
587 return ERROR_OK;
588 }
589
590 /* find full path to file */
591 static int jim_find(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
592 {
593 if (argc != 2)
594 return JIM_ERR;
595 const char *file = Jim_GetString(argv[1], NULL);
596 char *full_path = find_file(file);
597 if (full_path == NULL)
598 return JIM_ERR;
599 Jim_Obj *result = Jim_NewStringObj(interp, full_path, strlen(full_path));
600 free(full_path);
601
602 Jim_SetResult(interp, result);
603 return JIM_OK;
604 }
605
606 static int jim_echo(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
607 {
608 if (argc != 2)
609 return JIM_ERR;
610 const char *str = Jim_GetString(argv[1], NULL);
611 LOG_USER("%s", str);
612 return JIM_OK;
613 }
614
615 static size_t openocd_jim_fwrite(const void *_ptr, size_t size, size_t n, void *cookie)
616 {
617 size_t nbytes;
618 const char *ptr;
619 Jim_Interp *interp;
620
621 /* make it a char easier to read code */
622 ptr = _ptr;
623 interp = cookie;
624 nbytes = size * n;
625 if (ptr == NULL || interp == NULL || nbytes == 0) {
626 return 0;
627 }
628
629 /* do we have to chunk it? */
630 if (ptr[nbytes] == 0)
631 {
632 /* no it is a C style string */
633 LOG_USER_N("%s", ptr);
634 return strlen(ptr);
635 }
636 /* GRR we must chunk - not null terminated */
637 while (nbytes) {
638 char chunk[128 + 1];
639 int x;
640
641 x = nbytes;
642 if (x > 128) {
643 x = 128;
644 }
645 /* copy it */
646 memcpy(chunk, ptr, x);
647 /* terminate it */
648 chunk[n] = 0;
649 /* output it */
650 LOG_USER_N("%s", chunk);
651 ptr += x;
652 nbytes -= x;
653 }
654
655 return n;
656 }
657
658 static size_t openocd_jim_fread(void *ptr, size_t size, size_t n, void *cookie)
659 {
660 /* TCL wants to read... tell him no */
661 return 0;
662 }
663
664 static int openocd_jim_vfprintf(void *cookie, const char *fmt, va_list ap)
665 {
666 char *cp;
667 int n;
668 Jim_Interp *interp;
669
670 n = -1;
671 interp = cookie;
672 if (interp == NULL)
673 return n;
674
675 cp = alloc_vprintf(fmt, ap);
676 if (cp)
677 {
678 LOG_USER_N("%s", cp);
679 n = strlen(cp);
680 free(cp);
681 }
682 return n;
683 }
684
685 static int openocd_jim_fflush(void *cookie)
686 {
687 /* nothing to flush */
688 return 0;
689 }
690
691 static char* openocd_jim_fgets(char *s, int size, void *cookie)
692 {
693 /* not supported */
694 errno = ENOTSUP;
695 return NULL;
696 }
697
698 static int jim_capture(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
699 {
700 if (argc != 2)
701 return JIM_ERR;
702 int retcode;
703 const char *str = Jim_GetString(argv[1], NULL);
704
705 /* capture log output and return it */
706 Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
707 /* a garbage collect can happen, so we need a reference count to this object */
708 Jim_IncrRefCount(tclOutput);
709
710 log_add_callback(tcl_output, tclOutput);
711
712 retcode = Jim_Eval_Named(interp, str, __THIS__FILE__, __LINE__);
713
714 log_remove_callback(tcl_output, tclOutput);
715
716 /* We dump output into this local variable */
717 Jim_SetResult(interp, tclOutput);
718 Jim_DecrRefCount(interp, tclOutput);
719
720 return retcode;
721 }
722
723 /* sleep command sleeps for <n> miliseconds
724 * this is useful in target startup scripts
725 */
726 COMMAND_HANDLER(handle_sleep_command)
727 {
728 bool busy = false;
729 if (CMD_ARGC == 2)
730 {
731 if (strcmp(CMD_ARGV[1], "busy") == 0)
732 busy = true;
733 else
734 return ERROR_COMMAND_SYNTAX_ERROR;
735 }
736 else if (CMD_ARGC < 1 || CMD_ARGC > 2)
737 return ERROR_COMMAND_SYNTAX_ERROR;
738
739 unsigned long duration = 0;
740 int retval = parse_ulong(CMD_ARGV[0], &duration);
741 if (ERROR_OK != retval)
742 return retval;
743
744 if (!busy)
745 {
746 long long then = timeval_ms();
747 while (timeval_ms() - then < (long long)duration)
748 {
749 target_call_timer_callbacks_now();
750 usleep(1000);
751 }
752 }
753 else
754 busy_sleep(duration);
755
756 return ERROR_OK;
757 }
758
759 COMMAND_HANDLER(handle_fast_command)
760 {
761 if (CMD_ARGC != 1)
762 return ERROR_COMMAND_SYNTAX_ERROR;
763
764 fast_and_dangerous = strcmp("enable", CMD_ARGV[0]) == 0;
765
766 return ERROR_OK;
767 }
768
769
770 struct command_context* command_init()
771 {
772 struct command_context* context = malloc(sizeof(struct command_context));
773 extern const char startup_tcl[];
774 const char *HostOs;
775
776 context->mode = COMMAND_EXEC;
777 context->commands = NULL;
778 context->current_target = 0;
779 context->output_handler = NULL;
780 context->output_handler_priv = NULL;
781
782 #if !BUILD_ECOSBOARD
783 Jim_InitEmbedded();
784 /* Create an interpreter */
785 interp = Jim_CreateInterp();
786 /* Add all the Jim core commands */
787 Jim_RegisterCoreCommands(interp);
788 #endif
789
790 #if defined(_MSC_VER)
791 /* WinXX - is generic, the forward
792 * looking problem is this:
793 *
794 * "win32" or "win64"
795 *
796 * "winxx" is generic.
797 */
798 HostOs = "winxx";
799 #elif defined(__linux__)
800 HostOs = "linux";
801 #elif defined(__DARWIN__)
802 HostOs = "darwin";
803 #elif defined(__CYGWIN__)
804 HostOs = "cygwin";
805 #elif defined(__MINGW32__)
806 HostOs = "mingw32";
807 #elif defined(__ECOS)
808 HostOs = "ecos";
809 #else
810 #warn unrecognized host OS...
811 HostOs = "other";
812 #endif
813 Jim_SetGlobalVariableStr(interp, "ocd_HOSTOS",
814 Jim_NewStringObj(interp, HostOs , strlen(HostOs)));
815
816 Jim_CreateCommand(interp, "ocd_find", jim_find, NULL, NULL);
817 Jim_CreateCommand(interp, "echo", jim_echo, NULL, NULL);
818 Jim_CreateCommand(interp, "capture", jim_capture, NULL, NULL);
819
820 /* Set Jim's STDIO */
821 interp->cookie_stdin = interp;
822 interp->cookie_stdout = interp;
823 interp->cookie_stderr = interp;
824 interp->cb_fwrite = openocd_jim_fwrite;
825 interp->cb_fread = openocd_jim_fread ;
826 interp->cb_vfprintf = openocd_jim_vfprintf;
827 interp->cb_fflush = openocd_jim_fflush;
828 interp->cb_fgets = openocd_jim_fgets;
829
830 #if !BUILD_ECOSBOARD
831 Jim_EventLoopOnLoad(interp);
832 #endif
833 if (Jim_Eval_Named(interp, startup_tcl, "embedded:startup.tcl",1) == JIM_ERR)
834 {
835 LOG_ERROR("Failed to run startup.tcl (embedded into OpenOCD)");
836 Jim_PrintErrorMessage(interp);
837 exit(-1);
838 }
839
840 register_command(context, NULL, "sleep",
841 handle_sleep_command, COMMAND_ANY,
842 "<n> [busy] - sleep for n milliseconds. "
843 "\"busy\" means busy wait");
844 register_command(context, NULL, "fast",
845 handle_fast_command, COMMAND_ANY,
846 "fast <enable/disable> - place at beginning of "
847 "config files. Sets defaults to fast and dangerous.");
848
849 return context;
850 }
851
852 int command_context_mode(struct command_context *cmd_ctx, enum command_mode mode)
853 {
854 if (!cmd_ctx)
855 return ERROR_INVALID_ARGUMENTS;
856
857 cmd_ctx->mode = mode;
858 return ERROR_OK;
859 }
860
861 void process_jim_events(void)
862 {
863 #if !BUILD_ECOSBOARD
864 static int recursion = 0;
865
866 if (!recursion)
867 {
868 recursion++;
869 Jim_ProcessEvents (interp, JIM_ALL_EVENTS | JIM_DONT_WAIT);
870 recursion--;
871 }
872 #endif
873 }
874
875 void register_jim(struct command_context *cmd_ctx, const char *name,
876 Jim_CmdProc cmd, const char *help)
877 {
878 Jim_CreateCommand(interp, name, cmd, NULL, NULL);
879
880 Jim_Obj *cmd_list = Jim_NewListObj(interp, NULL, 0);
881 Jim_ListAppendElement(interp, cmd_list,
882 Jim_NewStringObj(interp, name, -1));
883
884 command_helptext_add(cmd_list, help);
885 }
886
887 /* return global variable long value or 0 upon failure */
888 long jim_global_long(const char *variable)
889 {
890 Jim_Obj *objPtr = Jim_GetGlobalVariableStr(interp, variable, JIM_ERRMSG);
891 long t;
892 if (Jim_GetLong(interp, objPtr, &t) == JIM_OK)
893 {
894 return t;
895 }
896 return 0;
897 }
898
899 #define DEFINE_PARSE_NUM_TYPE(name, type, func, min, max) \
900 int parse##name(const char *str, type *ul) \
901 { \
902 if (!*str) \
903 { \
904 LOG_ERROR("Invalid command argument"); \
905 return ERROR_COMMAND_ARGUMENT_INVALID; \
906 } \
907 char *end; \
908 *ul = func(str, &end, 0); \
909 if (*end) \
910 { \
911 LOG_ERROR("Invalid command argument"); \
912 return ERROR_COMMAND_ARGUMENT_INVALID; \
913 } \
914 if ((max == *ul) && (ERANGE == errno)) \
915 { \
916 LOG_ERROR("Argument overflow"); \
917 return ERROR_COMMAND_ARGUMENT_OVERFLOW; \
918 } \
919 if (min && (min == *ul) && (ERANGE == errno)) \
920 { \
921 LOG_ERROR("Argument underflow"); \
922 return ERROR_COMMAND_ARGUMENT_UNDERFLOW; \
923 } \
924 return ERROR_OK; \
925 }
926 DEFINE_PARSE_NUM_TYPE(_ulong, unsigned long , strtoul, 0, ULONG_MAX)
927 DEFINE_PARSE_NUM_TYPE(_ullong, unsigned long long, strtoull, 0, ULLONG_MAX)
928 DEFINE_PARSE_NUM_TYPE(_long, long , strtol, LONG_MIN, LONG_MAX)
929 DEFINE_PARSE_NUM_TYPE(_llong, long long, strtoll, LLONG_MIN, LLONG_MAX)
930
931 #define DEFINE_PARSE_WRAPPER(name, type, min, max, functype, funcname) \
932 int parse##name(const char *str, type *ul) \
933 { \
934 functype n; \
935 int retval = parse##funcname(str, &n); \
936 if (ERROR_OK != retval) \
937 return retval; \
938 if (n > max) \
939 return ERROR_COMMAND_ARGUMENT_OVERFLOW; \
940 if (min) \
941 return ERROR_COMMAND_ARGUMENT_UNDERFLOW; \
942 *ul = n; \
943 return ERROR_OK; \
944 }
945
946 #define DEFINE_PARSE_ULONG(name, type, min, max) \
947 DEFINE_PARSE_WRAPPER(name, type, min, max, unsigned long, _ulong)
948 DEFINE_PARSE_ULONG(_uint, unsigned, 0, UINT_MAX)
949 DEFINE_PARSE_ULONG(_u32, uint32_t, 0, UINT32_MAX)
950 DEFINE_PARSE_ULONG(_u16, uint16_t, 0, UINT16_MAX)
951 DEFINE_PARSE_ULONG(_u8, uint8_t, 0, UINT8_MAX)
952
953 #define DEFINE_PARSE_LONG(name, type, min, max) \
954 DEFINE_PARSE_WRAPPER(name, type, min, max, long, _long)
955 DEFINE_PARSE_LONG(_int, int, n < INT_MIN, INT_MAX)
956 DEFINE_PARSE_LONG(_s32, int32_t, n < INT32_MIN, INT32_MAX)
957 DEFINE_PARSE_LONG(_s16, int16_t, n < INT16_MIN, INT16_MAX)
958 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)