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