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