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