Fixes (more or less) random SEGFAULT upon invoking script_command().
[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 #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 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, __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         for (c = context->commands; c; c = c->next)
290         {
291                 if (strcmp(name, c->name) == 0)
292                 {
293                         /* unlink command */
294                         if (p)
295                         {
296                                 p->next = c->next;
297                         }
298                         else
299                         {
300                                 context->commands = c->next;
301                         }
302
303                         /* unregister children */
304                         if (c->children)
305                         {
306                                 for (c2 = c->children; c2; c2 = c2->next)
307                                 {
308                                         free(c2->name);
309                                         free(c2);
310                                 }
311                         }
312
313                         /* delete command */
314                         free(c->name);
315                         free(c);
316                 }
317
318                 /* remember the last command for unlinking */
319                 p = c;
320         }
321
322         return ERROR_OK;
323 }
324
325 void command_output_text(command_context_t *context, const char *data)
326 {
327         if( context && context->output_handler && data  ){
328                 context->output_handler( context, data );
329         }
330 }
331
332 void command_print_n(command_context_t *context, char *format, ...)
333 {
334         char *string;
335
336         va_list ap;
337         va_start(ap, format);
338
339         string = alloc_vprintf(format, ap);
340         if (string != NULL)
341         {
342                 /* we want this collected in the log + we also want to pick it up as a tcl return
343                  * value.
344                  *
345                  * The latter bit isn't precisely neat, but will do for now.
346                  */
347                 LOG_USER_N("%s", string);
348                 // We already printed it above
349                 //command_output_text(context, string);
350                 free(string);
351         }
352
353         va_end(ap);
354 }
355
356 void command_print(command_context_t *context, char *format, ...)
357 {
358         char *string;
359
360         va_list ap;
361         va_start(ap, format);
362
363         string = alloc_vprintf(format, ap);
364         if (string != NULL)
365         {
366                 strcat(string, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one char longer */
367                 /* we want this collected in the log + we also want to pick it up as a tcl return
368                  * value.
369                  *
370                  * The latter bit isn't precisely neat, but will do for now.
371                  */
372                 LOG_USER_N("%s", string);
373                 // We already printed it above
374                 //command_output_text(context, string);
375                 free(string);
376         }
377
378         va_end(ap);
379 }
380
381 int run_command(command_context_t *context, command_t *c, char *words[], int num_words)
382 {
383         int start_word=0;
384         if (!((context->mode == COMMAND_CONFIG) || (c->mode == COMMAND_ANY) || (c->mode == context->mode) ))
385         {
386                 /* Config commands can not run after the config stage */
387                 LOG_ERROR("Illegal mode for command");
388                 return ERROR_FAIL;
389         }
390
391         int retval = c->handler(context, c->name, words + start_word + 1, num_words - start_word - 1);
392         if (retval == ERROR_COMMAND_SYNTAX_ERROR)
393         {
394                 /* Print help for command */
395                 const char *t1="";
396                 const char *t2="";
397                 const char *t3="";
398                 /* maximum of two levels :-) */
399                 if (c->parent!=NULL)
400                 {
401                         t1=c->parent->name;
402                         t2=" ";
403                 }
404                 t3=c->name;
405                 command_run_linef(context, "help {%s%s%s}", t1, t2, t3);
406         }
407         else if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
408         {
409                 /* just fall through for a shutdown request */
410         }
411         else if (retval != ERROR_OK)
412         {
413                 /* we do not print out an error message because the command *should*
414                  * have printed out an error
415                  */
416                 LOG_DEBUG("Command failed with error code %d", retval);
417         }
418
419         return retval;
420 }
421
422 int command_run_line(command_context_t *context, char *line)
423 {
424         /* all the parent commands have been registered with the interpreter
425          * so, can just evaluate the line as a script and check for
426          * results
427          */
428         /* run the line thru a script engine */
429         int retval=ERROR_FAIL;
430         int retcode;
431         /* Beware! This code needs to be reentrant. It is also possible
432          * for OpenOCD commands to be invoked directly from Tcl. This would
433          * happen when the Jim Tcl interpreter is provided by eCos for
434          * instance.
435          */
436         Jim_DeleteAssocData(interp, "context");
437         retcode = Jim_SetAssocData(interp, "context", NULL, context);
438         if (retcode == JIM_OK)
439         {
440                 /* associated the return value */
441                 Jim_DeleteAssocData(interp, "retval");
442                 retcode = Jim_SetAssocData(interp, "retval", NULL, &retval);
443                 if (retcode == JIM_OK)
444                 {
445                         retcode = Jim_Eval_Named(interp, line, __FILE__, __LINE__ );
446
447                         Jim_DeleteAssocData(interp, "retval");
448                 }
449                 Jim_DeleteAssocData(interp, "context");
450         }
451         if (retcode == JIM_ERR) {
452                 if (retval!=ERROR_COMMAND_CLOSE_CONNECTION)
453                 {
454                         /* We do not print the connection closed error message */
455                         Jim_PrintErrorMessage(interp);
456                 }
457                 if (retval==ERROR_OK)
458                 {
459                         /* It wasn't a low level OpenOCD command that failed */
460                         return ERROR_FAIL;
461                 }
462                 return retval;
463         } else if (retcode == JIM_EXIT) {
464                 /* ignore. */
465                 /* exit(Jim_GetExitCode(interp)); */
466         } else {
467                 const char *result;
468                 int reslen;
469
470                 result = Jim_GetString(Jim_GetResult(interp), &reslen);
471                 if (reslen) {
472                         int i;
473                         char buff[256+1];
474                         for (i = 0; i < reslen; i += 256)
475                         {
476                                 int chunk;
477                                 chunk = reslen - i;
478                                 if (chunk > 256)
479                                         chunk = 256;
480                                 strncpy(buff, result+i, chunk);
481                                 buff[chunk] = 0;
482                                 LOG_USER_N("%s", buff);
483                         }
484                         LOG_USER_N("%s", "\n");
485                 }
486         }
487         return retval;
488 }
489
490 int command_run_linef(command_context_t *context, char *format, ...)
491 {
492         int retval=ERROR_FAIL;
493         char *string;
494         va_list ap;
495         va_start(ap, format);
496         string = alloc_vprintf(format, ap);
497         if (string!=NULL)
498         {
499                 retval=command_run_line(context, string);
500         }
501         va_end(ap);
502         return retval;
503 }
504
505 void command_set_output_handler(command_context_t* context, int (*output_handler)(struct command_context_s *context, const char* line), void *priv)
506 {
507         context->output_handler = output_handler;
508         context->output_handler_priv = priv;
509 }
510
511 command_context_t* copy_command_context(command_context_t* context)
512 {
513         command_context_t* copy_context = malloc(sizeof(command_context_t));
514
515         *copy_context = *context;
516
517         return copy_context;
518 }
519
520 int command_done(command_context_t *context)
521 {
522         free(context);
523         context = NULL;
524
525         return ERROR_OK;
526 }
527
528 /* find full path to file */
529 static int jim_find(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
530 {
531         if (argc != 2)
532                 return JIM_ERR;
533         const char *file = Jim_GetString(argv[1], NULL);
534         char *full_path = find_file(file);
535         if (full_path == NULL)
536                 return JIM_ERR;
537         Jim_Obj *result = Jim_NewStringObj(interp, full_path, strlen(full_path));
538         free(full_path);
539
540         Jim_SetResult(interp, result);
541         return JIM_OK;
542 }
543
544 static int jim_echo(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
545 {
546         if (argc != 2)
547                 return JIM_ERR;
548         const char *str = Jim_GetString(argv[1], NULL);
549         LOG_USER("%s", str);
550         return JIM_OK;
551 }
552
553 static size_t openocd_jim_fwrite(const void *_ptr, size_t size, size_t n, void *cookie)
554 {
555         size_t nbytes;
556         const char *ptr;
557         Jim_Interp *interp;
558
559         /* make it a char easier to read code */
560         ptr = _ptr;
561         interp = cookie;
562         nbytes = size * n;
563         if (ptr == NULL || interp == NULL || nbytes == 0) {
564                 return 0;
565         }
566
567         /* do we have to chunk it? */
568         if (ptr[nbytes] == 0)
569         {
570                 /* no it is a C style string */
571                 LOG_USER_N("%s", ptr);
572                 return strlen(ptr);
573         }
574         /* GRR we must chunk - not null terminated */
575         while (nbytes) {
576                 char chunk[128+1];
577                 int x;
578
579                 x = nbytes;
580                 if (x > 128) {
581                         x = 128;
582                 }
583                 /* copy it */
584                 memcpy(chunk, ptr, x);
585                 /* terminate it */
586                 chunk[n] = 0;
587                 /* output it */
588                 LOG_USER_N("%s", chunk);
589                 ptr += x;
590                 nbytes -= x;
591         }
592
593         return n;
594 }
595
596 static size_t openocd_jim_fread(void *ptr, size_t size, size_t n, void *cookie)
597 {
598         /* TCL wants to read... tell him no */
599         return 0;
600 }
601
602 static int openocd_jim_vfprintf(void *cookie, const char *fmt, va_list ap)
603 {
604         char *cp;
605         int n;
606         Jim_Interp *interp;
607
608         n = -1;
609         interp = cookie;
610         if (interp == NULL)
611                 return n;
612
613         cp = alloc_vprintf(fmt, ap);
614         if (cp)
615         {
616                 LOG_USER_N("%s", cp);
617                 n = strlen(cp);
618                 free(cp);
619         }
620         return n;
621 }
622
623 static int openocd_jim_fflush(void *cookie)
624 {
625         /* nothing to flush */
626         return 0;
627 }
628
629 static char* openocd_jim_fgets(char *s, int size, void *cookie)
630 {
631         /* not supported */
632         errno = ENOTSUP;
633         return NULL;
634 }
635
636
637 static int jim_capture(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
638 {
639         if (argc != 2)
640                 return JIM_ERR;
641         int retcode;
642         const char *str = Jim_GetString(argv[1], NULL);
643
644         /* capture log output and return it */
645         Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
646         /* a garbage collect can happen, so we need a reference count to this object */
647         Jim_IncrRefCount(tclOutput);
648
649         log_add_callback(tcl_output, tclOutput);
650
651         retcode = Jim_Eval_Named(interp, str, __FILE__, __LINE__ );
652
653         log_remove_callback(tcl_output, tclOutput);
654
655         /* We dump output into this local variable */
656         Jim_SetResult(interp, tclOutput);
657         Jim_DecrRefCount(interp, tclOutput);
658
659         return retcode;
660 }
661
662 command_context_t* command_init()
663 {
664         command_context_t* context = malloc(sizeof(command_context_t));
665         extern const char startup_tcl[];
666
667         context->mode = COMMAND_EXEC;
668         context->commands = NULL;
669         context->current_target = 0;
670         context->output_handler = NULL;
671         context->output_handler_priv = NULL;
672
673 #ifdef JIM_EMBEDDED
674         Jim_InitEmbedded();
675         /* Create an interpreter */
676         interp = Jim_CreateInterp();
677         /* Add all the Jim core commands */
678         Jim_RegisterCoreCommands(interp);
679 #endif
680
681         Jim_CreateCommand(interp, "ocd_find", jim_find, NULL, NULL);
682         Jim_CreateCommand(interp, "echo", jim_echo, NULL, NULL);
683         Jim_CreateCommand(interp, "capture", jim_capture, NULL, NULL);
684
685         /* Set Jim's STDIO */
686         interp->cookie_stdin = interp;
687         interp->cookie_stdout = interp;
688         interp->cookie_stderr = interp;
689         interp->cb_fwrite = openocd_jim_fwrite;
690         interp->cb_fread = openocd_jim_fread ;
691         interp->cb_vfprintf = openocd_jim_vfprintf;
692         interp->cb_fflush = openocd_jim_fflush;
693         interp->cb_fgets = openocd_jim_fgets;
694
695         add_default_dirs();
696
697 #ifdef JIM_EMBEDDED
698         Jim_EventLoopOnLoad(interp);
699 #endif
700         if (Jim_Eval_Named(interp, startup_tcl, "embedded:startup.tcl",1)==JIM_ERR)
701         {
702                 LOG_ERROR("Failed to run startup.tcl (embedded into OpenOCD compile time)");
703                 Jim_PrintErrorMessage(interp);
704                 exit(-1);
705         }
706
707         register_command(context, NULL, "sleep", handle_sleep_command,
708                                          COMMAND_ANY, "<n> [busy] - sleep for n milliseconds. \"busy\" means busy wait");
709
710         register_command(context, NULL, "fast", handle_fast_command,
711                                          COMMAND_ANY, "fast <enable/disable> - place at beginning of config files. Sets defaults to fast and dangerous.");
712
713         return context;
714 }
715
716 int command_context_mode(command_context_t *cmd_ctx, enum command_mode mode)
717 {
718         if (!cmd_ctx)
719                 return ERROR_INVALID_ARGUMENTS;
720
721         cmd_ctx->mode = mode;
722         return ERROR_OK;
723 }
724
725 /* sleep command sleeps for <n> miliseconds
726  * this is useful in target startup scripts
727  */
728 int handle_sleep_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
729 {
730         unsigned long duration = 0;
731         int busy = 0;
732
733         if (argc==1)
734         {
735
736         } else if (argc==2)
737         {
738                 if (strcmp(args[1], "busy")!=0)
739                         return ERROR_COMMAND_SYNTAX_ERROR;
740                 busy = 1;
741         } else
742         {
743                 return ERROR_COMMAND_SYNTAX_ERROR;
744         }
745
746         duration = strtoul(args[0], NULL, 0);
747
748         if (busy)
749         {
750                 busy_sleep(duration);
751         } else
752         {
753                 long long then=timeval_ms();
754                 while ((timeval_ms()-then)<duration)
755                 {
756                         target_call_timer_callbacks_now();
757                         usleep(1000);
758                 }
759         }
760
761         return ERROR_OK;
762 }
763
764 int handle_fast_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
765 {
766         if (argc!=1)
767                 return ERROR_COMMAND_SYNTAX_ERROR;
768
769         fast_and_dangerous = strcmp("enable", args[0])==0;
770
771         return ERROR_OK;
772 }
773
774 void process_jim_events(void)
775 {
776 #ifdef JIM_EMBEDDED
777         static int recursion = 0;
778
779         if (!recursion)
780         {
781                 recursion++;
782                 Jim_ProcessEvents (interp, JIM_ALL_EVENTS|JIM_DONT_WAIT);
783                 recursion--;
784         }
785 #endif
786 }
787
788 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)
789 {
790         Jim_CreateCommand(interp, name, cmd, NULL, NULL);
791
792         /* FIX!!! it would be prettier to invoke add_help_text...
793            accumulate help text in Tcl helptext list.  */
794         Jim_Obj *helptext=Jim_GetGlobalVariableStr(interp, "ocd_helptext", JIM_ERRMSG);
795         if (Jim_IsShared(helptext))
796                 helptext = Jim_DuplicateObj(interp, helptext);
797
798         Jim_Obj *cmd_entry=Jim_NewListObj(interp, NULL, 0);
799
800         Jim_Obj *cmd_list=Jim_NewListObj(interp, NULL, 0);
801         Jim_ListAppendElement(interp, cmd_list, Jim_NewStringObj(interp, name, -1));
802
803         Jim_ListAppendElement(interp, cmd_entry, cmd_list);
804         Jim_ListAppendElement(interp, cmd_entry, Jim_NewStringObj(interp, help, -1));
805         Jim_ListAppendElement(interp, helptext, cmd_entry);
806 }
807
808
809 /* return global variable long value or 0 upon failure */
810 long jim_global_long(const char *variable)
811 {
812         Jim_Obj *objPtr=Jim_GetGlobalVariableStr(interp, variable, JIM_ERRMSG);
813         long t;
814         if (Jim_GetLong(interp, objPtr, &t)==JIM_OK)
815         {
816                 return t;
817         }
818         return 0;
819 }