]> git.sur5r.net Git - openocd/blob - src/helper/command.c
retire jtag_set_check_value
[openocd] / 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 #include "replacements.h"
34 #include "target.h"
35 #include "command.h"
36 #include "configuration.h"
37
38 #include "log.h"
39 #include "time_support.h"
40 #include "jim-eventloop.h"
41
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <stdarg.h>
46 #include <stdio.h>
47 #include <unistd.h>
48 #include <errno.h>
49
50 int fast_and_dangerous = 0;
51 Jim_Interp *interp = NULL;
52
53 int handle_sleep_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
54 int handle_fast_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
55
56 int run_command(command_context_t *context, command_t *c, char *words[], int num_words);
57
58 static void tcl_output(void *privData, const char *file, int line, const char *function, const char *string)
59 {
60         Jim_Obj *tclOutput=(Jim_Obj *)privData;
61
62         Jim_AppendString(interp, tclOutput, string, strlen(string));
63 }
64
65 extern command_context_t *global_cmd_ctx;
66
67 static int script_command(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
68 {
69         /* the private data is stashed in the interp structure */
70         command_t *c;
71         command_context_t *context;
72         int retval;
73         int i;
74         int nwords;
75         char **words;
76
77         /* DANGER!!!! be careful what we invoke here, since interp->cmdPrivData might
78          * get overwritten by running other Jim commands! Treat it as an
79          * emphemeral global variable that is used in lieu of an argument
80          * to the fn and fish it out manually.
81          */
82         c = interp->cmdPrivData;
83         if (c==NULL)
84         {
85                 LOG_ERROR("BUG: interp->cmdPrivData==NULL");
86                 return JIM_ERR;
87         }
88         target_call_timer_callbacks_now();
89         LOG_USER_N("%s", ""); /* Keep GDB connection alive*/
90
91         LOG_DEBUG("script_command - %s", c->name);
92
93         words = malloc(sizeof(char *) * argc);
94         for (i = 0; i < argc; i++)
95         {
96                 int len;
97                 const char *w=Jim_GetString(argv[i], &len);
98                 if (*w=='#')
99                 {
100                         /* hit an end of line comment */
101                         break;
102                 }
103                 words[i] = strdup(w);
104                 if (words[i] == NULL)
105                 {
106                         return JIM_ERR;
107                 }
108                 LOG_DEBUG("script_command - %s, argv[%u]=%s", c->name, i, words[i]);
109         }
110         nwords = i;
111
112         /* grab the command context from the associated data */
113         context = Jim_GetAssocData(interp, "context");
114         if (context == NULL)
115         {
116                 /* Tcl can invoke commands directly instead of via command_run_line(). This would
117                  * happen when the Jim Tcl interpreter is provided by eCos.
118                  */
119                 context = global_cmd_ctx;
120         }
121
122         /* capture log output and return it */
123         Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
124         /* a garbage collect can happen, so we need a reference count to this object */
125         Jim_IncrRefCount(tclOutput);
126
127         log_add_callback(tcl_output, tclOutput);
128
129         retval = run_command(context, c, words, nwords);
130
131         log_remove_callback(tcl_output, tclOutput);
132
133         /* We dump output into this local variable */
134         Jim_SetResult(interp, tclOutput);
135         Jim_DecrRefCount(interp, tclOutput);
136
137         for (i = 0; i < nwords; i++)
138                 free(words[i]);
139         free(words);
140
141         int *return_retval = Jim_GetAssocData(interp, "retval");
142         if (return_retval != NULL)
143         {
144                 *return_retval = retval;
145         }
146
147         return (retval==ERROR_OK)?JIM_OK:JIM_ERR;
148 }
149
150 /* nice short description of source file */
151 #define __THIS__FILE__ "command.c"
152
153 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)
154 {
155         command_t *c, *p;
156
157         if (!context || !name)
158                 return NULL;
159
160         c = malloc(sizeof(command_t));
161
162         c->name = strdup(name);
163         c->parent = parent;
164         c->children = NULL;
165         c->handler = handler;
166         c->mode = mode;
167         if (!help)
168                 help="";
169         c->next = NULL;
170
171         /* place command in tree */
172         if (parent)
173         {
174                 if (parent->children)
175                 {
176                         /* find last child */
177                         for (p = parent->children; p && p->next; p = p->next);
178                         if (p)
179                                 p->next = c;
180                 }
181                 else
182                 {
183                         parent->children = c;
184                 }
185         }
186         else
187         {
188                 if (context->commands)
189                 {
190                         /* find last command */
191                         for (p = context->commands; p && p->next; p = p->next);
192                         if (p)
193                                 p->next = c;
194                 }
195                 else
196                 {
197                         context->commands = c;
198                 }
199         }
200
201         /* just a placeholder, no handler */
202         if (c->handler==NULL)
203                 return c;
204
205         /* If this is a two level command, e.g. "flash banks", then the
206          * "unknown" proc in startup.tcl must redirect to  this command.
207          *
208          * "flash banks" is translated by "unknown" to "flash_banks"
209          * if such a proc exists
210          */
211         /* Print help for command */
212         const char *t1="";
213         const char *t2="";
214         const char *t3="";
215         /* maximum of two levels :-) */
216         if (c->parent!=NULL)
217         {
218                 t1=c->parent->name;
219                 t2="_";
220         }
221         t3=c->name;
222         const char *full_name=alloc_printf("ocd_%s%s%s", t1, t2, t3);
223         Jim_CreateCommand(interp, full_name, script_command, c, NULL);
224         free((void *)full_name);
225
226         /* we now need to add an overrideable proc */
227         const char *override_name=alloc_printf("proc %s%s%s {args} {if {[catch {eval \"ocd_%s%s%s $args\"}]==0} {return \"\"} else { return -code error }", t1, t2, t3, t1, t2, t3);
228         Jim_Eval_Named(interp, override_name, __THIS__FILE__, __LINE__ );
229         free((void *)override_name);
230
231         /* accumulate help text in Tcl helptext list.  */
232         Jim_Obj *helptext=Jim_GetGlobalVariableStr(interp, "ocd_helptext", JIM_ERRMSG);
233         if (Jim_IsShared(helptext))
234                 helptext = Jim_DuplicateObj(interp, helptext);
235         Jim_Obj *cmd_entry=Jim_NewListObj(interp, NULL, 0);
236
237         Jim_Obj *cmd_list=Jim_NewListObj(interp, NULL, 0);
238
239         /* maximum of two levels :-) */
240         if (c->parent!=NULL)
241         {
242                 Jim_ListAppendElement(interp, cmd_list, Jim_NewStringObj(interp, c->parent->name, -1));
243         }
244         Jim_ListAppendElement(interp, cmd_list, Jim_NewStringObj(interp, c->name, -1));
245
246         Jim_ListAppendElement(interp, cmd_entry, cmd_list);
247         Jim_ListAppendElement(interp, cmd_entry, Jim_NewStringObj(interp, help, -1));
248         Jim_ListAppendElement(interp, helptext, cmd_entry);
249         return c;
250 }
251
252 int unregister_all_commands(command_context_t *context)
253 {
254         command_t *c, *c2;
255
256         if (context == NULL)
257                 return ERROR_OK;
258
259         while(NULL != context->commands)
260         {
261                 c = context->commands;
262
263                 while(NULL != c->children)
264                 {
265                         c2 = c->children;
266                         c->children = c->children->next;
267                         free(c2->name);
268                         c2->name = NULL;
269                         free(c2);
270                         c2 = NULL;
271                 }
272
273                 context->commands = context->commands->next;
274
275                 free(c->name);
276                 c->name = NULL;
277                 free(c);
278                 c = NULL;
279         }
280
281         return ERROR_OK;
282 }
283
284 int unregister_command(command_context_t *context, char *name)
285 {
286         command_t *c, *p = NULL, *c2;
287
288         if ((!context) || (!name))
289                 return ERROR_INVALID_ARGUMENTS;
290
291         /* find command */
292         c = context->commands;
293
294         while(NULL != c)
295         {
296                 if (strcmp(name, c->name) == 0)
297                 {
298                         /* unlink command */
299                         if (p)
300                         {
301                                 p->next = c->next;
302                         }
303                         else
304                         {
305                                 /* first element in command list */
306                                 context->commands = c->next;
307                         }
308
309                         /* unregister children */
310                         while(NULL != c->children)
311                         {
312                                 c2 = c->children;
313                                 c->children = c->children->next;
314                                 free(c2->name);
315                                 c2->name = NULL;
316                                 free(c2);
317                                 c2 = NULL;
318                         }
319
320                         /* delete command */
321                         free(c->name);
322                         c->name = NULL;
323                         free(c);
324                         c = NULL;
325                         return ERROR_OK;
326                 }
327
328                 /* remember the last command for unlinking */
329                 p = c;
330                 c = c->next;
331         }
332
333         return ERROR_OK;
334 }
335
336 void command_output_text(command_context_t *context, const char *data)
337 {
338         if( context && context->output_handler && data  ){
339                 context->output_handler( context, data );
340         }
341 }
342
343 void command_print_n(command_context_t *context, char *format, ...)
344 {
345         char *string;
346
347         va_list ap;
348         va_start(ap, format);
349
350         string = alloc_vprintf(format, ap);
351         if (string != NULL)
352         {
353                 /* we want this collected in the log + we also want to pick it up as a tcl return
354                  * value.
355                  *
356                  * The latter bit isn't precisely neat, but will do for now.
357                  */
358                 LOG_USER_N("%s", string);
359                 /* We already printed it above */
360                 /* command_output_text(context, string); */
361                 free(string);
362         }
363
364         va_end(ap);
365 }
366
367 void command_print(command_context_t *context, 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                 strcat(string, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one char longer */
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 int run_command(command_context_t *context, command_t *c, char *words[], int num_words)
393 {
394         int start_word=0;
395         if (!((context->mode == COMMAND_CONFIG) || (c->mode == COMMAND_ANY) || (c->mode == context->mode) ))
396         {
397                 /* Config commands can not run after the config stage */
398                 LOG_ERROR("Illegal mode for command");
399                 return ERROR_FAIL;
400         }
401
402         int retval = c->handler(context, c->name, words + start_word + 1, num_words - start_word - 1);
403         if (retval == ERROR_COMMAND_SYNTAX_ERROR)
404         {
405                 /* Print help for command */
406                 const char *t1="";
407                 const char *t2="";
408                 const char *t3="";
409                 /* maximum of two levels :-) */
410                 if (c->parent!=NULL)
411                 {
412                         t1=c->parent->name;
413                         t2=" ";
414                 }
415                 t3=c->name;
416                 command_run_linef(context, "help {%s%s%s}", t1, t2, t3);
417         }
418         else if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
419         {
420                 /* just fall through for a shutdown request */
421         }
422         else if (retval != ERROR_OK)
423         {
424                 /* we do not print out an error message because the command *should*
425                  * have printed out an error
426                  */
427                 LOG_DEBUG("Command failed with error code %d", retval);
428         }
429
430         return retval;
431 }
432
433 int command_run_line(command_context_t *context, char *line)
434 {
435         /* all the parent commands have been registered with the interpreter
436          * so, can just evaluate the line as a script and check for
437          * results
438          */
439         /* run the line thru a script engine */
440         int retval=ERROR_FAIL;
441         int retcode;
442         /* Beware! This code needs to be reentrant. It is also possible
443          * for OpenOCD commands to be invoked directly from Tcl. This would
444          * happen when the Jim Tcl interpreter is provided by eCos for
445          * instance.
446          */
447         Jim_DeleteAssocData(interp, "context");
448         retcode = Jim_SetAssocData(interp, "context", NULL, context);
449         if (retcode == JIM_OK)
450         {
451                 /* associated the return value */
452                 Jim_DeleteAssocData(interp, "retval");
453                 retcode = Jim_SetAssocData(interp, "retval", NULL, &retval);
454                 if (retcode == JIM_OK)
455                 {
456                         retcode = Jim_Eval_Named(interp, line, __THIS__FILE__, __LINE__ );
457
458                         Jim_DeleteAssocData(interp, "retval");
459                 }
460                 Jim_DeleteAssocData(interp, "context");
461         }
462         if (retcode == JIM_ERR) {
463                 if (retval!=ERROR_COMMAND_CLOSE_CONNECTION)
464                 {
465                         /* We do not print the connection closed error message */
466                         Jim_PrintErrorMessage(interp);
467                 }
468                 if (retval==ERROR_OK)
469                 {
470                         /* It wasn't a low level OpenOCD command that failed */
471                         return ERROR_FAIL;
472                 }
473                 return retval;
474         } else if (retcode == JIM_EXIT) {
475                 /* ignore. */
476                 /* exit(Jim_GetExitCode(interp)); */
477         } else {
478                 const char *result;
479                 int reslen;
480
481                 result = Jim_GetString(Jim_GetResult(interp), &reslen);
482                 if (reslen>0)
483                 {
484                         int i;
485                         char buff[256+1];
486                         for (i = 0; i < reslen; i += 256)
487                         {
488                                 int chunk;
489                                 chunk = reslen - i;
490                                 if (chunk > 256)
491                                         chunk = 256;
492                                 strncpy(buff, result+i, chunk);
493                                 buff[chunk] = 0;
494                                 LOG_USER_N("%s", buff);
495                         }
496                         LOG_USER_N("%s", "\n");
497                 }
498                 retval=ERROR_OK;
499         }
500         return retval;
501 }
502
503 int command_run_linef(command_context_t *context, char *format, ...)
504 {
505         int retval=ERROR_FAIL;
506         char *string;
507         va_list ap;
508         va_start(ap, format);
509         string = alloc_vprintf(format, ap);
510         if (string!=NULL)
511         {
512                 retval=command_run_line(context, string);
513         }
514         va_end(ap);
515         return retval;
516 }
517
518 void command_set_output_handler(command_context_t* context, int (*output_handler)(struct command_context_s *context, const char* line), void *priv)
519 {
520         context->output_handler = output_handler;
521         context->output_handler_priv = priv;
522 }
523
524 command_context_t* copy_command_context(command_context_t* context)
525 {
526         command_context_t* copy_context = malloc(sizeof(command_context_t));
527
528         *copy_context = *context;
529
530         return copy_context;
531 }
532
533 int command_done(command_context_t *context)
534 {
535         free(context);
536         context = NULL;
537
538         return ERROR_OK;
539 }
540
541 /* find full path to file */
542 static int jim_find(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
543 {
544         if (argc != 2)
545                 return JIM_ERR;
546         const char *file = Jim_GetString(argv[1], NULL);
547         char *full_path = find_file(file);
548         if (full_path == NULL)
549                 return JIM_ERR;
550         Jim_Obj *result = Jim_NewStringObj(interp, full_path, strlen(full_path));
551         free(full_path);
552
553         Jim_SetResult(interp, result);
554         return JIM_OK;
555 }
556
557 static int jim_echo(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
558 {
559         if (argc != 2)
560                 return JIM_ERR;
561         const char *str = Jim_GetString(argv[1], NULL);
562         LOG_USER("%s", str);
563         return JIM_OK;
564 }
565
566 static size_t openocd_jim_fwrite(const void *_ptr, size_t size, size_t n, void *cookie)
567 {
568         size_t nbytes;
569         const char *ptr;
570         Jim_Interp *interp;
571
572         /* make it a char easier to read code */
573         ptr = _ptr;
574         interp = cookie;
575         nbytes = size * n;
576         if (ptr == NULL || interp == NULL || nbytes == 0) {
577                 return 0;
578         }
579
580         /* do we have to chunk it? */
581         if (ptr[nbytes] == 0)
582         {
583                 /* no it is a C style string */
584                 LOG_USER_N("%s", ptr);
585                 return strlen(ptr);
586         }
587         /* GRR we must chunk - not null terminated */
588         while (nbytes) {
589                 char chunk[128+1];
590                 int x;
591
592                 x = nbytes;
593                 if (x > 128) {
594                         x = 128;
595                 }
596                 /* copy it */
597                 memcpy(chunk, ptr, x);
598                 /* terminate it */
599                 chunk[n] = 0;
600                 /* output it */
601                 LOG_USER_N("%s", chunk);
602                 ptr += x;
603                 nbytes -= x;
604         }
605
606         return n;
607 }
608
609 static size_t openocd_jim_fread(void *ptr, size_t size, size_t n, void *cookie)
610 {
611         /* TCL wants to read... tell him no */
612         return 0;
613 }
614
615 static int openocd_jim_vfprintf(void *cookie, const char *fmt, va_list ap)
616 {
617         char *cp;
618         int n;
619         Jim_Interp *interp;
620
621         n = -1;
622         interp = cookie;
623         if (interp == NULL)
624                 return n;
625
626         cp = alloc_vprintf(fmt, ap);
627         if (cp)
628         {
629                 LOG_USER_N("%s", cp);
630                 n = strlen(cp);
631                 free(cp);
632         }
633         return n;
634 }
635
636 static int openocd_jim_fflush(void *cookie)
637 {
638         /* nothing to flush */
639         return 0;
640 }
641
642 static char* openocd_jim_fgets(char *s, int size, void *cookie)
643 {
644         /* not supported */
645         errno = ENOTSUP;
646         return NULL;
647 }
648
649 static int jim_capture(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
650 {
651         if (argc != 2)
652                 return JIM_ERR;
653         int retcode;
654         const char *str = Jim_GetString(argv[1], NULL);
655
656         /* capture log output and return it */
657         Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
658         /* a garbage collect can happen, so we need a reference count to this object */
659         Jim_IncrRefCount(tclOutput);
660
661         log_add_callback(tcl_output, tclOutput);
662
663         retcode = Jim_Eval_Named(interp, str, __THIS__FILE__, __LINE__ );
664
665         log_remove_callback(tcl_output, tclOutput);
666
667         /* We dump output into this local variable */
668         Jim_SetResult(interp, tclOutput);
669         Jim_DecrRefCount(interp, tclOutput);
670
671         return retcode;
672 }
673
674 command_context_t* command_init()
675 {
676         command_context_t* context = malloc(sizeof(command_context_t));
677         extern const char startup_tcl[];
678         const char *HostOs;
679
680         context->mode = COMMAND_EXEC;
681         context->commands = NULL;
682         context->current_target = 0;
683         context->output_handler = NULL;
684         context->output_handler_priv = NULL;
685
686 #ifdef JIM_EMBEDDED
687         Jim_InitEmbedded();
688         /* Create an interpreter */
689         interp = Jim_CreateInterp();
690         /* Add all the Jim core commands */
691         Jim_RegisterCoreCommands(interp);
692 #endif
693
694 #if defined( _MSC_VER )
695         /* WinXX - is generic, the forward
696          * looking problem is this:
697          *
698          *   "win32" or "win64"
699          *
700          * "winxx" is generic.
701          */
702         HostOs = "winxx";
703 #elif defined( __LINUX__)
704         HostOs = "linux";
705 #elif defined( __DARWIN__ )
706         HostOs = "darwin";
707 #elif defined( __CYGWIN__ )
708         HostOs = "cygwin";
709 #elif defined( __MINGW32__ )
710         HostOs = "mingw32";
711 #else
712         HostOs = "other";
713 #endif
714         Jim_SetGlobalVariableStr( interp, "ocd_HOSTOS", Jim_NewStringObj( interp, HostOs , strlen(HostOs)) );
715
716         Jim_CreateCommand(interp, "ocd_find", jim_find, NULL, NULL);
717         Jim_CreateCommand(interp, "echo", jim_echo, NULL, NULL);
718         Jim_CreateCommand(interp, "capture", jim_capture, NULL, NULL);
719
720         /* Set Jim's STDIO */
721         interp->cookie_stdin = interp;
722         interp->cookie_stdout = interp;
723         interp->cookie_stderr = interp;
724         interp->cb_fwrite = openocd_jim_fwrite;
725         interp->cb_fread = openocd_jim_fread ;
726         interp->cb_vfprintf = openocd_jim_vfprintf;
727         interp->cb_fflush = openocd_jim_fflush;
728         interp->cb_fgets = openocd_jim_fgets;
729
730         add_default_dirs();
731
732 #ifdef JIM_EMBEDDED
733         Jim_EventLoopOnLoad(interp);
734 #endif
735         if (Jim_Eval_Named(interp, startup_tcl, "embedded:startup.tcl",1)==JIM_ERR)
736         {
737                 LOG_ERROR("Failed to run startup.tcl (embedded into OpenOCD compile time)");
738                 Jim_PrintErrorMessage(interp);
739                 exit(-1);
740         }
741
742         register_command(context, NULL, "sleep", handle_sleep_command,
743                                          COMMAND_ANY, "<n> [busy] - sleep for n milliseconds. \"busy\" means busy wait");
744
745         register_command(context, NULL, "fast", handle_fast_command,
746                                          COMMAND_ANY, "fast <enable/disable> - place at beginning of config files. Sets defaults to fast and dangerous.");
747
748         return context;
749 }
750
751 int command_context_mode(command_context_t *cmd_ctx, enum command_mode mode)
752 {
753         if (!cmd_ctx)
754                 return ERROR_INVALID_ARGUMENTS;
755
756         cmd_ctx->mode = mode;
757         return ERROR_OK;
758 }
759
760 /* sleep command sleeps for <n> miliseconds
761  * this is useful in target startup scripts
762  */
763 int handle_sleep_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
764 {
765         unsigned long duration = 0;
766         int busy = 0;
767
768         if (argc==1)
769         {
770
771         } else if (argc==2)
772         {
773                 if (strcmp(args[1], "busy")!=0)
774                         return ERROR_COMMAND_SYNTAX_ERROR;
775                 busy = 1;
776         } else
777         {
778                 return ERROR_COMMAND_SYNTAX_ERROR;
779         }
780
781         duration = strtoul(args[0], NULL, 0);
782
783         if (busy)
784         {
785                 busy_sleep(duration);
786         } else
787         {
788                 long long then=timeval_ms();
789                 while ((timeval_ms()-then)<(long long)duration)
790                 {
791                         target_call_timer_callbacks_now();
792                         usleep(1000);
793                 }
794         }
795
796         return ERROR_OK;
797 }
798
799 int handle_fast_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
800 {
801         if (argc!=1)
802                 return ERROR_COMMAND_SYNTAX_ERROR;
803
804         fast_and_dangerous = strcmp("enable", args[0])==0;
805
806         return ERROR_OK;
807 }
808
809 void process_jim_events(void)
810 {
811 #ifdef JIM_EMBEDDED
812         static int recursion = 0;
813
814         if (!recursion)
815         {
816                 recursion++;
817                 Jim_ProcessEvents (interp, JIM_ALL_EVENTS|JIM_DONT_WAIT);
818                 recursion--;
819         }
820 #endif
821 }
822
823 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)
824 {
825         Jim_CreateCommand(interp, name, cmd, NULL, NULL);
826
827         /* FIX!!! it would be prettier to invoke add_help_text...
828          * accumulate help text in Tcl helptext list.  */
829         Jim_Obj *helptext=Jim_GetGlobalVariableStr(interp, "ocd_helptext", JIM_ERRMSG);
830         if (Jim_IsShared(helptext))
831                 helptext = Jim_DuplicateObj(interp, helptext);
832
833         Jim_Obj *cmd_entry=Jim_NewListObj(interp, NULL, 0);
834
835         Jim_Obj *cmd_list=Jim_NewListObj(interp, NULL, 0);
836         Jim_ListAppendElement(interp, cmd_list, Jim_NewStringObj(interp, name, -1));
837
838         Jim_ListAppendElement(interp, cmd_entry, cmd_list);
839         Jim_ListAppendElement(interp, cmd_entry, Jim_NewStringObj(interp, help, -1));
840         Jim_ListAppendElement(interp, helptext, cmd_entry);
841 }
842
843 /* return global variable long value or 0 upon failure */
844 long jim_global_long(const char *variable)
845 {
846         Jim_Obj *objPtr=Jim_GetGlobalVariableStr(interp, variable, JIM_ERRMSG);
847         long t;
848         if (Jim_GetLong(interp, objPtr, &t)==JIM_OK)
849         {
850                 return t;
851         }
852         return 0;
853 }