- Replace 'while(' with 'while ('.
[openocd.git] / src / helper / jim.h
1 /* Jim - A small embeddable Tcl interpreter
2 *
3 * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
4 * Copyright 2005 Clemens Hintze <c.hintze@gmx.net>
5 * Copyright 2005 patthoyts - Pat Thoyts <patthoyts@users.sf.net>
6 * Copyright 2008 oharboe - √ėyvind Harboe - oyvind.harboe@zylin.com
7 * Copyright 2008 Andrew Lunn <andrew@lunn.ch>
8 * Copyright 2008 Duane Ellis <openocd@duaneellis.com>
9 * Copyright 2008 Uwe Klein <uklein@klein-messgeraete.de>
10 *
11 * The FreeBSD license
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE JIM TCL PROJECT ``AS IS'' AND ANY
25 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * The views and conclusions contained in the software and documentation
38 * are those of the authors and should not be interpreted as representing
39 * official policies, either expressed or implied, of the Jim Tcl Project.
40 *
41 *--- Inline Header File Documentation ---
42 * [By Duane Ellis, openocd@duaneellis.com, 8/18/8]
43 *
44 * Belief is "Jim" would greatly benifit if Jim Internals where
45 * documented in some way - form whatever, and perhaps - the package:
46 * 'doxygen' is the correct approach to do that.
47 *
48 * Details, see: http://www.stack.nl/~dimitri/doxygen/
49 *
50 * To that end please follow these guide lines:
51 *
52 * (A) Document the PUBLIC api in the .H file.
53 *
54 * (B) Document JIM Internals, in the .C file.
55 *
56 * (C) Remember JIM is embedded in other packages, to that end do
57 * not assume that your way of documenting is the right way, Jim's
58 * public documentation should be agnostic, such that it is some
59 * what agreeable with the "package" that is embedding JIM inside
60 * of it's own doxygen documentation.
61 *
62 * (D) Use minimal Doxygen tags.
63 *
64 * This will be an "ongoing work in progress" for some time.
65 **/
66
67 #ifndef __JIM__H
68 #define __JIM__H
69
70 #ifdef __cplusplus
71 extern "C" {
72 #endif
73
74 #include <time.h>
75 #include <limits.h>
76 #include <stdio.h> /* for the FILE typedef definition */
77 #include <stdlib.h> /* In order to export the Jim_Free() macro */
78 #include <stdarg.h> /* In order to get type va_list */
79
80 /* -----------------------------------------------------------------------------
81 * Compiler specific fixes.
82 * ---------------------------------------------------------------------------*/
83
84 /* MSC has _stricmp instead of strcasecmp */
85 #ifdef _MSC_VER
86 # define strcasecmp _stricmp
87 #endif /* _MSC_VER */
88
89 /* Long Long type and related issues */
90 #ifdef HAVE_LONG_LONG_INT
91 # ifdef _MSC_VER /* MSC compiler */
92 # define jim_wide _int64
93 # ifndef LLONG_MAX
94 # define LLONG_MAX 9223372036854775807I64
95 # endif
96 # ifndef LLONG_MIN
97 # define LLONG_MIN (-LLONG_MAX - 1I64)
98 # endif
99 # define JIM_WIDE_MIN LLONG_MIN
100 # define JIM_WIDE_MAX LLONG_MAX
101 # else /* Other compilers (mainly GCC) */
102 # define jim_wide long long
103 # ifndef LLONG_MAX
104 # define LLONG_MAX 9223372036854775807LL
105 # endif
106 # ifndef LLONG_MIN
107 # define LLONG_MIN (-LLONG_MAX - 1LL)
108 # endif
109 # define JIM_WIDE_MIN LLONG_MIN
110 # define JIM_WIDE_MAX LLONG_MAX
111 # endif
112 #else
113 # define jim_wide long
114 # define JIM_WIDE_MIN LONG_MIN
115 # define JIM_WIDE_MAX LONG_MAX
116 #endif
117
118 /* -----------------------------------------------------------------------------
119 * LIBC specific fixes
120 * ---------------------------------------------------------------------------*/
121
122 #ifdef HAVE_LONG_LONG_INT
123 # if defined(_MSC_VER) || defined(__MSVCRT__)
124 # define JIM_WIDE_MODIFIER "I64d"
125 # else
126 # define JIM_WIDE_MODIFIER "lld"
127 # endif
128 #else
129 # define JIM_WIDE_MODIFIER "ld"
130 #endif
131
132 /* -----------------------------------------------------------------------------
133 * Exported defines
134 * ---------------------------------------------------------------------------*/
135
136 /* Jim version numbering: every version of jim is marked with a
137 * successive integer number. This is version 0. The first
138 * stable version will be 1, then 2, 3, and so on. */
139 #define JIM_VERSION 51
140
141 #define JIM_OK 0
142 #define JIM_ERR 1
143 #define JIM_RETURN 2
144 #define JIM_BREAK 3
145 #define JIM_CONTINUE 4
146 #define JIM_EVAL 5
147 #define JIM_EXIT 6
148 #define JIM_ERR_ADDSTACK 7
149 #define JIM_MAX_NESTING_DEPTH 10000 /* default max nesting depth */
150
151 /* Some function get an integer argument with flags to change
152 * the behaviour. */
153 #define JIM_NONE 0 /* no flags set */
154 #define JIM_ERRMSG 1 /* set an error message in the interpreter. */
155
156 /* Flags for Jim_SubstObj() */
157 #define JIM_SUBST_NOVAR 1 /* don't perform variables substitutions */
158 #define JIM_SUBST_NOCMD 2 /* don't perform command substitutions */
159 #define JIM_SUBST_NOESC 4 /* don't perform escapes substitutions */
160
161 /* Unused arguments generate annoying warnings... */
162 #define JIM_NOTUSED(V) ((void) V)
163
164 /* Flags used by API calls getting a 'nocase' argument. */
165 #define JIM_CASESENS 0 /* case sensitive */
166 #define JIM_NOCASE 1 /* no case */
167
168 /* Filesystem related */
169 #define JIM_PATH_LEN 1024
170
171 /* Newline, some embedded system may need -DJIM_CRLF */
172 #ifdef JIM_CRLF
173 #define JIM_NL "\r\n"
174 #else
175 #define JIM_NL "\n"
176 #endif
177
178 #if defined(__WIN32__) || defined(_WIN32)
179 #define DLLEXPORT __declspec(dllexport)
180 #define DLLIMPORT __declspec(dllimport)
181 #else
182 #define DLLEXPORT
183 #define DLLIMPORT
184 #endif
185
186 /* -----------------------------------------------------------------------------
187 * Stack
188 * ---------------------------------------------------------------------------*/
189
190 typedef struct Jim_Stack {
191 int len;
192 int maxlen;
193 void **vector;
194 } Jim_Stack;
195
196 /* -----------------------------------------------------------------------------
197 * Hash table
198 * ---------------------------------------------------------------------------*/
199
200 typedef struct Jim_HashEntry {
201 const void *key;
202 void *val;
203 struct Jim_HashEntry *next;
204 } Jim_HashEntry;
205
206 typedef struct Jim_HashTableType {
207 unsigned int (*hashFunction)(const void *key);
208 const void *(*keyDup)(void *privdata, const void *key);
209 void *(*valDup)(void *privdata, const void *obj);
210 int (*keyCompare)(void *privdata, const void *key1, const void *key2);
211 void (*keyDestructor)(void *privdata, const void *key);
212 void (*valDestructor)(void *privdata, void *obj);
213 } Jim_HashTableType;
214
215 typedef struct Jim_HashTable {
216 Jim_HashEntry **table;
217 Jim_HashTableType *type;
218 unsigned int size;
219 unsigned int sizemask;
220 unsigned int used;
221 unsigned int collisions;
222 void *privdata;
223 } Jim_HashTable;
224
225 typedef struct Jim_HashTableIterator {
226 Jim_HashTable *ht;
227 int index;
228 Jim_HashEntry *entry, *nextEntry;
229 } Jim_HashTableIterator;
230
231 /* This is the initial size of every hash table */
232 #define JIM_HT_INITIAL_SIZE 16
233
234 /* ------------------------------- Macros ------------------------------------*/
235 #define Jim_FreeEntryVal(ht, entry) \
236 if ((ht)->type->valDestructor) \
237 (ht)->type->valDestructor((ht)->privdata, (entry)->val)
238
239 #define Jim_SetHashVal(ht, entry, _val_) do { \
240 if ((ht)->type->valDup) \
241 entry->val = (ht)->type->valDup((ht)->privdata, _val_); \
242 else \
243 entry->val = (_val_); \
244 } while (0)
245
246 #define Jim_FreeEntryKey(ht, entry) \
247 if ((ht)->type->keyDestructor) \
248 (ht)->type->keyDestructor((ht)->privdata, (entry)->key)
249
250 #define Jim_SetHashKey(ht, entry, _key_) do { \
251 if ((ht)->type->keyDup) \
252 entry->key = (ht)->type->keyDup((ht)->privdata, _key_); \
253 else \
254 entry->key = (_key_); \
255 } while (0)
256
257 #define Jim_CompareHashKeys(ht, key1, key2) \
258 (((ht)->type->keyCompare) ? \
259 (ht)->type->keyCompare((ht)->privdata, key1, key2) : \
260 (key1) == (key2))
261
262 #define Jim_HashKey(ht, key) (ht)->type->hashFunction(key)
263
264 #define Jim_GetHashEntryKey(he) ((he)->key)
265 #define Jim_GetHashEntryVal(he) ((he)->val)
266 #define Jim_GetHashTableCollisions(ht) ((ht)->collisions)
267 #define Jim_GetHashTableSize(ht) ((ht)->size)
268 #define Jim_GetHashTableUsed(ht) ((ht)->used)
269
270 /* -----------------------------------------------------------------------------
271 * Jim_Obj structure
272 * ---------------------------------------------------------------------------*/
273
274 /* -----------------------------------------------------------------------------
275 * Jim object. This is mostly the same as Tcl_Obj itself,
276 * with the addition of the 'prev' and 'next' pointers.
277 * In Jim all the objects are stored into a linked list for GC purposes,
278 * so that it's possible to access every object living in a given interpreter
279 * sequentially. When an object is freed, it's moved into a different
280 * linked list, used as object pool.
281 *
282 * The refcount of a freed object is always -1.
283 * ---------------------------------------------------------------------------*/
284 typedef struct Jim_Obj {
285 int refCount; /* reference count */
286 char *bytes; /* string representation buffer. NULL = no string repr. */
287 int length; /* number of bytes in 'bytes', not including the numterm. */
288 struct Jim_ObjType *typePtr; /* object type. */
289 /* Internal representation union */
290 union {
291 /* integer number type */
292 jim_wide wideValue;
293 /* hashed object type value */
294 int hashValue;
295 /* index type */
296 int indexValue;
297 /* return code type */
298 int returnCode;
299 /* double number type */
300 double doubleValue;
301 /* Generic pointer */
302 void *ptr;
303 /* Generic two pointers value */
304 struct {
305 void *ptr1;
306 void *ptr2;
307 } twoPtrValue;
308 /* Variable object */
309 struct {
310 unsigned jim_wide callFrameId;
311 struct Jim_Var *varPtr;
312 } varValue;
313 /* Command object */
314 struct {
315 unsigned jim_wide procEpoch;
316 struct Jim_Cmd *cmdPtr;
317 } cmdValue;
318 /* List object */
319 struct {
320 struct Jim_Obj **ele; /* Elements vector */
321 int len; /* Length */
322 int maxLen; /* Allocated 'ele' length */
323 } listValue;
324 /* String type */
325 struct {
326 int maxLength;
327 } strValue;
328 /* Reference type */
329 struct {
330 jim_wide id;
331 struct Jim_Reference *refPtr;
332 } refValue;
333 /* Source type */
334 struct {
335 const char *fileName;
336 int lineNumber;
337 } sourceValue;
338 /* Dict substitution type */
339 struct {
340 struct Jim_Obj *varNameObjPtr;
341 struct Jim_Obj *indexObjPtr;
342 } dictSubstValue;
343 /* tagged binary type */
344 struct {
345 unsigned char *data;
346 size_t len;
347 } binaryValue;
348 } internalRep;
349 /* This are 8 or 16 bytes more for every object
350 * but this is required for efficient garbage collection
351 * of Jim references. */
352 struct Jim_Obj *prevObjPtr; /* pointer to the prev object. */
353 struct Jim_Obj *nextObjPtr; /* pointer to the next object. */
354 } Jim_Obj;
355
356 /* Jim_Obj related macros */
357 #define Jim_IncrRefCount(objPtr) \
358 ++(objPtr)->refCount
359 #define Jim_DecrRefCount(interp, objPtr) \
360 if (--(objPtr)->refCount <= 0) Jim_FreeObj(interp, objPtr)
361 #define Jim_IsShared(objPtr) \
362 ((objPtr)->refCount > 1)
363
364 /* This macro is used when we allocate a new object using
365 * Jim_New...Obj(), but for some error we need to destroy it.
366 * Instead to use Jim_IncrRefCount() + Jim_DecrRefCount() we
367 * can just call Jim_FreeNewObj. To call Jim_Free directly
368 * seems too raw, the object handling may change and we want
369 * that Jim_FreeNewObj() can be called only against objects
370 * that are belived to have refcount == 0. */
371 #define Jim_FreeNewObj Jim_FreeObj
372
373 /* Free the internal representation of the object. */
374 #define Jim_FreeIntRep(i,o) \
375 if ((o)->typePtr && (o)->typePtr->freeIntRepProc) \
376 (o)->typePtr->freeIntRepProc(i, o)
377
378 /* Get the internal representation pointer */
379 #define Jim_GetIntRepPtr(o) (o)->internalRep.ptr
380
381 /* Set the internal representation pointer */
382 #define Jim_SetIntRepPtr(o, p) \
383 (o)->internalRep.ptr = (p)
384
385 /* The object type structure.
386 * There are four methods.
387 *
388 * - FreeIntRep is used to free the internal representation of the object.
389 * Can be NULL if there is nothing to free.
390 * - DupIntRep is used to duplicate the internal representation of the object.
391 * If NULL, when an object is duplicated, the internalRep union is
392 * directly copied from an object to another.
393 * Note that it's up to the caller to free the old internal repr of the
394 * object before to call the Dup method.
395 * - UpdateString is used to create the string from the internal repr.
396 * - setFromAny is used to convert the current object into one of this type.
397 */
398
399 struct Jim_Interp;
400
401 typedef void (Jim_FreeInternalRepProc)(struct Jim_Interp *interp,
402 struct Jim_Obj *objPtr);
403 typedef void (Jim_DupInternalRepProc)(struct Jim_Interp *interp,
404 struct Jim_Obj *srcPtr, Jim_Obj *dupPtr);
405 typedef void (Jim_UpdateStringProc)(struct Jim_Obj *objPtr);
406
407 typedef struct Jim_ObjType {
408 const char *name; /* The name of the type. */
409 Jim_FreeInternalRepProc *freeIntRepProc;
410 Jim_DupInternalRepProc *dupIntRepProc;
411 Jim_UpdateStringProc *updateStringProc;
412 int flags;
413 } Jim_ObjType;
414
415 /* Jim_ObjType flags */
416 #define JIM_TYPE_NONE 0 /* No flags */
417 #define JIM_TYPE_REFERENCES 1 /* The object may contain referneces. */
418
419 /* Starting from 1 << 20 flags are reserved for private uses of
420 * different calls. This way the same 'flags' argument may be used
421 * to pass both global flags and private flags. */
422 #define JIM_PRIV_FLAG_SHIFT 20
423
424 /* -----------------------------------------------------------------------------
425 * Call frame, vars, commands structures
426 * ---------------------------------------------------------------------------*/
427
428 /* Call frame */
429 typedef struct Jim_CallFrame {
430 unsigned jim_wide id; /* Call Frame ID. Used for caching. */
431 struct Jim_HashTable vars; /* Where local vars are stored */
432 struct Jim_HashTable *staticVars; /* pointer to procedure static vars */
433 struct Jim_CallFrame *parentCallFrame;
434 Jim_Obj *const *argv; /* object vector of the current procedure call. */
435 int argc; /* number of args of the current procedure call. */
436 Jim_Obj *procArgsObjPtr; /* arglist object of the running procedure */
437 Jim_Obj *procBodyObjPtr; /* body object of the running procedure */
438 struct Jim_CallFrame *nextFramePtr;
439 } Jim_CallFrame;
440
441 /* The var structure. It just holds the pointer of the referenced
442 * object. If linkFramePtr is not NULL the variable is a link
443 * to a variable of name store on objPtr living on the given callframe
444 * (this happens when the [global] or [upvar] command is used).
445 * The interp in order to always know how to free the Jim_Obj associated
446 * with a given variable because In Jim objects memory managment is
447 * bound to interpreters. */
448 typedef struct Jim_Var {
449 Jim_Obj *objPtr;
450 struct Jim_CallFrame *linkFramePtr;
451 } Jim_Var;
452
453 /* The cmd structure. */
454 typedef int (*Jim_CmdProc)(struct Jim_Interp *interp, int argc,
455 Jim_Obj *const *argv);
456 typedef void (*Jim_DelCmdProc)(struct Jim_Interp *interp, void *privData);
457
458 /* A command is implemented in C if funcPtr is != NULL, otherwise
459 * it's a Tcl procedure with the arglist and body represented by the
460 * two objects referenced by arglistObjPtr and bodyoObjPtr. */
461 typedef struct Jim_Cmd {
462 Jim_CmdProc cmdProc; /* Not-NULL for a C command. */
463 void *privData; /* Only used for C commands. */
464 Jim_DelCmdProc delProc; /* Called when the command is deleted if != NULL */
465 Jim_Obj *argListObjPtr;
466 Jim_Obj *bodyObjPtr;
467 Jim_HashTable *staticVars; /* Static vars hash table. NULL if no statics. */
468 int arityMin; /* Min number of arguments. */
469 int arityMax; /* Max number of arguments. */
470 } Jim_Cmd;
471
472 /* Pseudo Random Number Generator State structure */
473 typedef struct Jim_PrngState {
474 unsigned char sbox[256];
475 unsigned int i, j;
476 } Jim_PrngState;
477
478 /* -----------------------------------------------------------------------------
479 * Jim interpreter structure.
480 * Fields similar to the real Tcl interpreter structure have the same names.
481 * ---------------------------------------------------------------------------*/
482 typedef struct Jim_Interp {
483 Jim_Obj *result; /* object returned by the last command called. */
484 int errorLine; /* Error line where an error occurred. */
485 const char *errorFileName; /* Error file where an error occurred. */
486 int numLevels; /* Number of current nested calls. */
487 int maxNestingDepth; /* Used for infinite loop detection. */
488 int returnCode; /* Completion code to return on JIM_RETURN. */
489 int exitCode; /* Code to return to the OS on JIM_EXIT. */
490 Jim_CallFrame *framePtr; /* Pointer to the current call frame */
491 Jim_CallFrame *topFramePtr; /* toplevel/global frame pointer. */
492 struct Jim_HashTable commands; /* Commands hash table */
493 unsigned jim_wide procEpoch; /* Incremented every time the result
494 of procedures names lookup caching
495 may no longer be valid. */
496 unsigned jim_wide callFrameEpoch; /* Incremented every time a new
497 callframe is created. This id is used for the
498 'ID' field contained in the Jim_CallFrame
499 structure. */
500 Jim_Obj *liveList; /* Linked list of all the live objects. */
501 Jim_Obj *freeList; /* Linked list of all the unused objects. */
502 const char *scriptFileName; /* File name of the script currently in execution. */
503 Jim_Obj *emptyObj; /* Shared empty string object. */
504 unsigned jim_wide referenceNextId; /* Next id for reference. */
505 struct Jim_HashTable references; /* References hash table. */
506 jim_wide lastCollectId; /* reference max Id of the last GC
507 execution. It's set to -1 while the collection
508 is running as sentinel to avoid to recursive
509 calls via the [collect] command inside
510 finalizers. */
511 time_t lastCollectTime; /* unix time of the last GC execution */
512 struct Jim_HashTable sharedStrings; /* Shared Strings hash table */
513 Jim_Obj *stackTrace; /* Stack trace object. */
514 Jim_Obj *unknown; /* Unknown command cache */
515 int unknown_called; /* The unknown command has been invoked */
516 int errorFlag; /* Set if an error occurred during execution. */
517 int evalRetcodeLevel; /* Level where the last return with code JIM_EVAL
518 happened. */
519 void *cmdPrivData; /* Used to pass the private data pointer to
520 a command. It is set to what the user specified
521 via Jim_CreateCommand(). */
522
523 struct Jim_HashTable stub; /* Stub hash table to export API */
524 /* Jim_GetApi() function pointer, used to bootstrap the STUB table */
525 int (*getApiFuncPtr)(struct Jim_Interp *, const char *, void *);
526 struct Jim_CallFrame *freeFramesList; /* list of CallFrame structures. */
527 struct Jim_HashTable assocData; /* per-interp storage for use by packages */
528 Jim_PrngState *prngState; /* per interpreter Random Number Gen. state. */
529 struct Jim_HashTable packages; /* Provided packages hash table */
530 void *cookie_stdin; /* input file pointer, 'stdin' by default */
531 void *cookie_stdout; /* output file pointer, 'stdout' by default */
532 void *cookie_stderr; /* errors file pointer, 'stderr' by default */
533 size_t (*cb_fwrite )( const void *ptr, size_t size, size_t n, void *cookie );
534 size_t (*cb_fread )( void *ptr, size_t size, size_t n, void *cookie );
535 int (*cb_vfprintf)( void *cookie, const char *fmt, va_list ap );
536 int (*cb_fflush )( void *cookie );
537 char *(*cb_fgets )( char *s, int size, void *cookie );
538 } Jim_Interp;
539
540 /* Currently provided as macro that performs the increment.
541 * At some point may be a real function doing more work.
542 * The proc epoch is used in order to know when a command lookup
543 * cached can no longer considered valid. */
544 #define Jim_InterpIncrProcEpoch(i) (i)->procEpoch++
545 #define Jim_SetResultString(i,s,l) Jim_SetResult(i, Jim_NewStringObj(i,s,l))
546 #define Jim_SetResultInt(i,intval) Jim_SetResult(i, Jim_NewIntObj(i,intval))
547 #define Jim_SetEmptyResult(i) Jim_SetResult(i, (i)->emptyObj)
548 #define Jim_GetResult(i) ((i)->result)
549 #define Jim_CmdPrivData(i) ((i)->cmdPrivData)
550
551 /* Note that 'o' is expanded only one time inside this macro,
552 * so it's safe to use side effects. */
553 #define Jim_SetResult(i,o) do { \
554 Jim_Obj *_resultObjPtr_ = (o); \
555 Jim_IncrRefCount(_resultObjPtr_); \
556 Jim_DecrRefCount(i,(i)->result); \
557 (i)->result = _resultObjPtr_; \
558 } while (0)
559
560 /* Reference structure. The interpreter pointer is held within privdata member in HashTable */
561 #define JIM_REFERENCE_TAGLEN 7 /* The tag is fixed-length, because the reference
562 string representation must be fixed length. */
563 typedef struct Jim_Reference {
564 Jim_Obj *objPtr;
565 Jim_Obj *finalizerCmdNamePtr;
566 char tag[JIM_REFERENCE_TAGLEN+1];
567 } Jim_Reference;
568
569 /** Name Value Pairs, aka: NVP
570 * - Given a string - return the associated int.
571 * - Given a number - return the associated string.
572 * .
573 *
574 * Very useful when the number is not a simple index into an array of
575 * known string, or there may be multiple strings (aliases) that mean then same
576 * thing.
577 *
578 * An NVP Table is terminated with ".name=NULL".
579 *
580 * During the 'name2value' operation, if no matching string is found
581 * the pointer to the terminal element (with p->name==NULL) is returned.
582 *
583 * Example:
584 * \code
585 * const Jim_Nvp yn[] = {
586 * { "yes", 1 },
587 * { "no" , 0 },
588 * { "yep", 1 },
589 * { "nope", 0 },
590 * { NULL, -1 },
591 * };
592 *
593 * Jim_Nvp *result
594 * e = Jim_Nvp_name2value( interp, yn, "y", &result );
595 * returns &yn[0];
596 * e = Jim_Nvp_name2value( interp, yn, "n", &result );
597 * returns &yn[1];
598 * e = Jim_Nvp_name2value( interp, yn, "Blah", &result );
599 * returns &yn[4];
600 * \endcode
601 *
602 * During the number2name operation, the first matching value is returned.
603 */
604 typedef struct {
605 const char *name;
606 int value;
607 } Jim_Nvp;
608
609
610 /* -----------------------------------------------------------------------------
611 * Exported API prototypes.
612 * ---------------------------------------------------------------------------*/
613
614 /* Macros that are common for extensions and core. */
615 #define Jim_NewEmptyStringObj(i) Jim_NewStringObj(i, "", 0)
616
617 /* The core includes real prototypes, extensions instead
618 * include a global function pointer for every function exported.
619 * Once the extension calls Jim_InitExtension(), the global
620 * functon pointers are set to the value of the STUB table
621 * contained in the Jim_Interp structure.
622 *
623 * This makes Jim able to load extensions even if it is statically
624 * linked itself, and to load extensions compiled with different
625 * versions of Jim (as long as the API is still compatible.) */
626
627 /* Macros are common for core and extensions */
628 #define Jim_FreeHashTableIterator(iter) Jim_Free(iter)
629
630 #ifdef DOXYGEN
631 #define JIM_STATIC
632 #define JIM_API( X ) X
633 #else
634 #ifndef __JIM_CORE__
635 # if defined JIM_EMBEDDED
636 # define JIM_API(x) (*x)
637 # define JIM_STATIC
638 # else
639 # define JIM_API(x) (*x)
640 # define JIM_STATIC extern
641 # endif
642 #else
643 # define JIM_API(x) x
644 # if defined(BUILD_Jim)
645 # define JIM_STATIC DLLEXPORT
646 # else
647 # define JIM_STATIC static
648 # endif
649 #endif /* __JIM_CORE__ */
650 #endif /* DOXYGEN */
651
652 /** Set the result - printf() style */
653 JIM_STATIC int JIM_API( Jim_SetResult_sprintf )( Jim_Interp *p, const char *fmt, ... );
654
655 /* Memory allocation */
656 JIM_STATIC void * JIM_API(Jim_Alloc) (int size);
657 JIM_STATIC void JIM_API(Jim_Free) (void *ptr);
658 JIM_STATIC char * JIM_API(Jim_StrDup) (const char *s);
659
660 /* evaluation */
661 JIM_STATIC int JIM_API(Jim_Eval)(Jim_Interp *interp, const char *script);
662 /* in C code, you can do this and get better error messages */
663 /* Jim_Eval_Named( interp, "some tcl commands", __FILE__, __LINE__ ); */
664 JIM_STATIC int JIM_API(Jim_Eval_Named)(Jim_Interp *interp, const char *script,const char *filename, int lineno);
665 JIM_STATIC int JIM_API(Jim_EvalGlobal)(Jim_Interp *interp, const char *script);
666 JIM_STATIC int JIM_API(Jim_EvalFile)(Jim_Interp *interp, const char *filename);
667 JIM_STATIC int JIM_API(Jim_EvalObj) (Jim_Interp *interp, Jim_Obj *scriptObjPtr);
668 JIM_STATIC int JIM_API(Jim_EvalObjBackground) (Jim_Interp *interp,
669 Jim_Obj *scriptObjPtr);
670 JIM_STATIC int JIM_API(Jim_EvalObjVector) (Jim_Interp *interp, int objc,
671 Jim_Obj *const *objv);
672 JIM_STATIC int JIM_API(Jim_SubstObj) (Jim_Interp *interp, Jim_Obj *substObjPtr,
673 Jim_Obj **resObjPtrPtr, int flags);
674
675 /* stack */
676 JIM_STATIC void JIM_API(Jim_InitStack)(Jim_Stack *stack);
677 JIM_STATIC void JIM_API(Jim_FreeStack)(Jim_Stack *stack);
678 JIM_STATIC int JIM_API(Jim_StackLen)(Jim_Stack *stack);
679 JIM_STATIC void JIM_API(Jim_StackPush)(Jim_Stack *stack, void *element);
680 JIM_STATIC void * JIM_API(Jim_StackPop)(Jim_Stack *stack);
681 JIM_STATIC void * JIM_API(Jim_StackPeek)(Jim_Stack *stack);
682 JIM_STATIC void JIM_API(Jim_FreeStackElements)(Jim_Stack *stack, void (*freeFunc)(void *ptr));
683
684 /* hash table */
685 JIM_STATIC int JIM_API(Jim_InitHashTable) (Jim_HashTable *ht,
686 Jim_HashTableType *type, void *privdata);
687 JIM_STATIC int JIM_API(Jim_ExpandHashTable) (Jim_HashTable *ht,
688 unsigned int size);
689 JIM_STATIC int JIM_API(Jim_AddHashEntry) (Jim_HashTable *ht, const void *key,
690 void *val);
691 JIM_STATIC int JIM_API(Jim_ReplaceHashEntry) (Jim_HashTable *ht,
692 const void *key, void *val);
693 JIM_STATIC int JIM_API(Jim_DeleteHashEntry) (Jim_HashTable *ht,
694 const void *key);
695 JIM_STATIC int JIM_API(Jim_FreeHashTable) (Jim_HashTable *ht);
696 JIM_STATIC Jim_HashEntry * JIM_API(Jim_FindHashEntry) (Jim_HashTable *ht,
697 const void *key);
698 JIM_STATIC int JIM_API(Jim_ResizeHashTable) (Jim_HashTable *ht);
699 JIM_STATIC Jim_HashTableIterator *JIM_API(Jim_GetHashTableIterator)
700 (Jim_HashTable *ht);
701 JIM_STATIC Jim_HashEntry * JIM_API(Jim_NextHashEntry)
702 (Jim_HashTableIterator *iter);
703
704 /* objects */
705 JIM_STATIC Jim_Obj * JIM_API(Jim_NewObj) (Jim_Interp *interp);
706 JIM_STATIC void JIM_API(Jim_FreeObj) (Jim_Interp *interp, Jim_Obj *objPtr);
707 JIM_STATIC void JIM_API(Jim_InvalidateStringRep) (Jim_Obj *objPtr);
708 JIM_STATIC void JIM_API(Jim_InitStringRep) (Jim_Obj *objPtr, const char *bytes,
709 int length);
710 JIM_STATIC Jim_Obj * JIM_API(Jim_DuplicateObj) (Jim_Interp *interp,
711 Jim_Obj *objPtr);
712 JIM_STATIC const char * JIM_API(Jim_GetString)(Jim_Obj *objPtr,
713 int *lenPtr);
714 JIM_STATIC int JIM_API(Jim_Length)(Jim_Obj *objPtr);
715
716 /* string object */
717 JIM_STATIC Jim_Obj * JIM_API(Jim_NewStringObj) (Jim_Interp *interp,
718 const char *s, int len);
719 JIM_STATIC Jim_Obj * JIM_API(Jim_NewStringObjNoAlloc) (Jim_Interp *interp,
720 char *s, int len);
721 JIM_STATIC void JIM_API(Jim_AppendString) (Jim_Interp *interp, Jim_Obj *objPtr,
722 const char *str, int len);
723 JIM_STATIC void JIM_API(Jim_AppendString_sprintf) (Jim_Interp *interp, Jim_Obj *objPtr,
724 const char *fmt, ... );
725 JIM_STATIC void JIM_API(Jim_AppendObj) (Jim_Interp *interp, Jim_Obj *objPtr,
726 Jim_Obj *appendObjPtr);
727 JIM_STATIC void JIM_API(Jim_AppendStrings) (Jim_Interp *interp,
728 Jim_Obj *objPtr, ...);
729 JIM_STATIC int JIM_API(Jim_StringEqObj) (Jim_Obj *aObjPtr,
730 Jim_Obj *bObjPtr, int nocase);
731 JIM_STATIC int JIM_API(Jim_StringMatchObj) (Jim_Obj *patternObjPtr,
732 Jim_Obj *objPtr, int nocase);
733 JIM_STATIC Jim_Obj * JIM_API(Jim_StringRangeObj) (Jim_Interp *interp,
734 Jim_Obj *strObjPtr, Jim_Obj *firstObjPtr,
735 Jim_Obj *lastObjPtr);
736 JIM_STATIC Jim_Obj * JIM_API(Jim_FormatString) (Jim_Interp *interp,
737 Jim_Obj *fmtObjPtr, int objc, Jim_Obj *const *objv);
738 JIM_STATIC Jim_Obj * JIM_API(Jim_ScanString) (Jim_Interp *interp, Jim_Obj *strObjPtr,
739 Jim_Obj *fmtObjPtr, int flags);
740 JIM_STATIC int JIM_API(Jim_CompareStringImmediate) (Jim_Interp *interp,
741 Jim_Obj *objPtr, const char *str);
742
743 /* reference object */
744 JIM_STATIC Jim_Obj * JIM_API(Jim_NewReference) (Jim_Interp *interp,
745 Jim_Obj *objPtr, Jim_Obj *tagPtr, Jim_Obj *cmdNamePtr);
746 JIM_STATIC Jim_Reference * JIM_API(Jim_GetReference) (Jim_Interp *interp,
747 Jim_Obj *objPtr);
748 JIM_STATIC int JIM_API(Jim_SetFinalizer) (Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *cmdNamePtr);
749 JIM_STATIC int JIM_API(Jim_GetFinalizer) (Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj **cmdNamePtrPtr);
750
751 /* interpreter */
752 JIM_STATIC Jim_Interp * JIM_API(Jim_CreateInterp) (void);
753 JIM_STATIC void JIM_API(Jim_FreeInterp) (Jim_Interp *i);
754 JIM_STATIC int JIM_API(Jim_GetExitCode) (Jim_Interp *interp);
755 JIM_STATIC void * JIM_API(Jim_SetStdin) (Jim_Interp *interp, void *fp);
756 JIM_STATIC void * JIM_API(Jim_SetStdout) (Jim_Interp *interp, void *fp);
757 JIM_STATIC void * JIM_API(Jim_SetStderr) (Jim_Interp *interp, void *fp);
758
759 /* commands */
760 JIM_STATIC void JIM_API(Jim_RegisterCoreCommands) (Jim_Interp *interp);
761 JIM_STATIC int JIM_API(Jim_CreateCommand) (Jim_Interp *interp,
762 const char *cmdName, Jim_CmdProc cmdProc, void *privData,
763 Jim_DelCmdProc delProc);
764 JIM_STATIC int JIM_API(Jim_CreateProcedure) (Jim_Interp *interp,
765 const char *cmdName, Jim_Obj *argListObjPtr, Jim_Obj *staticsListObjPtr,
766 Jim_Obj *bodyObjPtr, int arityMin, int arityMax);
767 JIM_STATIC int JIM_API(Jim_DeleteCommand) (Jim_Interp *interp,
768 const char *cmdName);
769 JIM_STATIC int JIM_API(Jim_RenameCommand) (Jim_Interp *interp,
770 const char *oldName, const char *newName);
771 JIM_STATIC Jim_Cmd * JIM_API(Jim_GetCommand) (Jim_Interp *interp,
772 Jim_Obj *objPtr, int flags);
773 JIM_STATIC int JIM_API(Jim_SetVariable) (Jim_Interp *interp,
774 Jim_Obj *nameObjPtr, Jim_Obj *valObjPtr);
775 JIM_STATIC int JIM_API(Jim_SetVariableStr) (Jim_Interp *interp,
776 const char *name, Jim_Obj *objPtr);
777 JIM_STATIC int JIM_API(Jim_SetGlobalVariableStr) (Jim_Interp *interp,
778 const char *name, Jim_Obj *objPtr);
779 JIM_STATIC int JIM_API(Jim_SetVariableStrWithStr) (Jim_Interp *interp,
780 const char *name, const char *val);
781 JIM_STATIC int JIM_API(Jim_SetVariableLink) (Jim_Interp *interp,
782 Jim_Obj *nameObjPtr, Jim_Obj *targetNameObjPtr,
783 Jim_CallFrame *targetCallFrame);
784 JIM_STATIC Jim_Obj * JIM_API(Jim_GetVariable) (Jim_Interp *interp,
785 Jim_Obj *nameObjPtr, int flags);
786 JIM_STATIC Jim_Obj * JIM_API(Jim_GetGlobalVariable) (Jim_Interp *interp,
787 Jim_Obj *nameObjPtr, int flags);
788 JIM_STATIC Jim_Obj * JIM_API(Jim_GetVariableStr) (Jim_Interp *interp,
789 const char *name, int flags);
790 JIM_STATIC Jim_Obj * JIM_API(Jim_GetGlobalVariableStr) (Jim_Interp *interp,
791 const char *name, int flags);
792 JIM_STATIC int JIM_API(Jim_UnsetVariable) (Jim_Interp *interp,
793 Jim_Obj *nameObjPtr, int flags);
794
795 /* call frame */
796 JIM_STATIC int JIM_API(Jim_GetCallFrameByLevel) (Jim_Interp *interp,
797 Jim_Obj *levelObjPtr, Jim_CallFrame **framePtrPtr,
798 int *newLevelPtr);
799
800 /* garbage collection */
801 JIM_STATIC int JIM_API(Jim_Collect) (Jim_Interp *interp);
802 JIM_STATIC void JIM_API(Jim_CollectIfNeeded) (Jim_Interp *interp);
803
804 /* index object */
805 JIM_STATIC int JIM_API(Jim_GetIndex) (Jim_Interp *interp, Jim_Obj *objPtr,
806 int *indexPtr);
807
808 /* list object */
809 JIM_STATIC Jim_Obj * JIM_API(Jim_NewListObj) (Jim_Interp *interp,
810 Jim_Obj *const *elements, int len);
811 JIM_STATIC void JIM_API(Jim_ListInsertElements) (Jim_Interp *interp,
812 Jim_Obj *listPtr, int index, int objc, Jim_Obj *const *objVec);
813 JIM_STATIC void JIM_API(Jim_ListAppendElement) (Jim_Interp *interp,
814 Jim_Obj *listPtr, Jim_Obj *objPtr);
815 JIM_STATIC void JIM_API(Jim_ListAppendList) (Jim_Interp *interp,
816 Jim_Obj *listPtr, Jim_Obj *appendListPtr);
817 JIM_STATIC void JIM_API(Jim_ListLength) (Jim_Interp *interp, Jim_Obj *listPtr,
818 int *intPtr);
819 JIM_STATIC int JIM_API(Jim_ListIndex) (Jim_Interp *interp, Jim_Obj *listPrt,
820 int index, Jim_Obj **objPtrPtr, int seterr);
821 JIM_STATIC int JIM_API(Jim_SetListIndex) (Jim_Interp *interp,
822 Jim_Obj *varNamePtr, Jim_Obj *const *indexv, int indexc,
823 Jim_Obj *newObjPtr);
824 JIM_STATIC Jim_Obj * JIM_API(Jim_ConcatObj) (Jim_Interp *interp, int objc,
825 Jim_Obj *const *objv);
826
827 /* dict object */
828 JIM_STATIC Jim_Obj * JIM_API(Jim_NewDictObj) (Jim_Interp *interp,
829 Jim_Obj *const *elements, int len);
830 JIM_STATIC int JIM_API(Jim_DictKey) (Jim_Interp *interp, Jim_Obj *dictPtr,
831 Jim_Obj *keyPtr, Jim_Obj **objPtrPtr, int flags);
832 JIM_STATIC int JIM_API(Jim_DictKeysVector) (Jim_Interp *interp,
833 Jim_Obj *dictPtr, Jim_Obj *const *keyv, int keyc,
834 Jim_Obj **objPtrPtr, int flags);
835 JIM_STATIC int JIM_API(Jim_SetDictKeysVector) (Jim_Interp *interp,
836 Jim_Obj *varNamePtr, Jim_Obj *const *keyv, int keyc,
837 Jim_Obj *newObjPtr);
838
839 /* return code object */
840 JIM_STATIC int JIM_API(Jim_GetReturnCode) (Jim_Interp *interp, Jim_Obj *objPtr,
841 int *intPtr);
842
843 /* expression object */
844 JIM_STATIC int JIM_API(Jim_EvalExpression) (Jim_Interp *interp,
845 Jim_Obj *exprObjPtr, Jim_Obj **exprResultPtrPtr);
846 JIM_STATIC int JIM_API(Jim_GetBoolFromExpr) (Jim_Interp *interp,
847 Jim_Obj *exprObjPtr, int *boolPtr);
848
849 /* integer object */
850 JIM_STATIC int JIM_API(Jim_GetWide) (Jim_Interp *interp, Jim_Obj *objPtr,
851 jim_wide *widePtr);
852 JIM_STATIC int JIM_API(Jim_GetLong) (Jim_Interp *interp, Jim_Obj *objPtr,
853 long *longPtr);
854 JIM_STATIC void JIM_API(Jim_SetWide) (Jim_Interp *interp, Jim_Obj *objPtr,
855 jim_wide wideValue);
856 #define Jim_NewWideObj Jim_NewIntObj
857 JIM_STATIC Jim_Obj * JIM_API(Jim_NewIntObj) (Jim_Interp *interp,
858 jim_wide wideValue);
859
860 /* double object */
861 JIM_STATIC int JIM_API(Jim_GetDouble)(Jim_Interp *interp, Jim_Obj *objPtr,
862 double *doublePtr);
863 JIM_STATIC void JIM_API(Jim_SetDouble)(Jim_Interp *interp, Jim_Obj *objPtr,
864 double doubleValue);
865 JIM_STATIC Jim_Obj * JIM_API(Jim_NewDoubleObj)(Jim_Interp *interp, double doubleValue);
866
867 /* shared strings */
868 JIM_STATIC const char * JIM_API(Jim_GetSharedString) (Jim_Interp *interp,
869 const char *str);
870 JIM_STATIC void JIM_API(Jim_ReleaseSharedString) (Jim_Interp *interp,
871 const char *str);
872
873 /* commands utilities */
874 JIM_STATIC void JIM_API(Jim_WrongNumArgs) (Jim_Interp *interp, int argc,
875 Jim_Obj *const *argv, const char *msg);
876 JIM_STATIC int JIM_API(Jim_GetEnum) (Jim_Interp *interp, Jim_Obj *objPtr,
877 const char * const *tablePtr, int *indexPtr, const char *name, int flags);
878 JIM_STATIC int JIM_API(Jim_GetNvp) (Jim_Interp *interp,
879 Jim_Obj *objPtr,
880 const Jim_Nvp *nvp_table,
881 const Jim_Nvp **result);
882 JIM_STATIC int JIM_API(Jim_ScriptIsComplete) (const char *s, int len,
883 char *stateCharPtr);
884
885 /* package utilities */
886 typedef void (Jim_InterpDeleteProc)(Jim_Interp *interp, void *data);
887 JIM_STATIC void * JIM_API(Jim_GetAssocData)(Jim_Interp *interp, const char *key);
888 JIM_STATIC int JIM_API(Jim_SetAssocData)(Jim_Interp *interp, const char *key,
889 Jim_InterpDeleteProc *delProc, void *data);
890 JIM_STATIC int JIM_API(Jim_DeleteAssocData)(Jim_Interp *interp, const char *key);
891
892 /* API import/export functions */
893 JIM_STATIC int JIM_API(Jim_GetApi) (Jim_Interp *interp, const char *funcname,
894 void *targetPtrPtr);
895 JIM_STATIC int JIM_API(Jim_RegisterApi) (Jim_Interp *interp,
896 const char *funcname, void *funcptr);
897
898 /* Packages C API */
899 JIM_STATIC int JIM_API(Jim_PackageProvide) (Jim_Interp *interp,
900 const char *name, const char *ver, int flags);
901 JIM_STATIC const char * JIM_API(Jim_PackageRequire) (Jim_Interp *interp,
902 const char *name, const char *ver, int flags);
903
904 /* error messages */
905 JIM_STATIC void JIM_API(Jim_PrintErrorMessage) (Jim_Interp *interp);
906
907 /* interactive mode */
908 JIM_STATIC int JIM_API(Jim_InteractivePrompt) (Jim_Interp *interp);
909
910 /* Misc */
911 JIM_STATIC void JIM_API(Jim_Panic) (Jim_Interp *interp, const char *fmt, ...);
912
913 /* Jim's STDIO */
914 JIM_STATIC int JIM_API( Jim_fprintf )( Jim_Interp *interp, void *cookie, const char *fmt, ... );
915 JIM_STATIC int JIM_API( Jim_vfprintf )( Jim_Interp *interp, void *cookie, const char *fmt, va_list ap );
916 JIM_STATIC size_t JIM_API( Jim_fwrite )( Jim_Interp *interp, const void *ptr, size_t size, size_t nmeb, void *cookie );
917 JIM_STATIC size_t JIM_API( Jim_fread )( Jim_Interp *interp, void *ptr, size_t size, size_t nmeb, void *cookie );
918 JIM_STATIC int JIM_API( Jim_fflush )( Jim_Interp *interp, void *cookie );
919 JIM_STATIC char * JIM_API( Jim_fgets )( Jim_Interp *interp, char *s, int size, void *cookie );
920
921 /* Name Value Pairs Operations */
922 JIM_STATIC Jim_Nvp *JIM_API(Jim_Nvp_name2value_simple)( const Jim_Nvp *nvp_table, const char *name );
923 JIM_STATIC Jim_Nvp *JIM_API(Jim_Nvp_name2value_nocase_simple)( const Jim_Nvp *nvp_table, const char *name );
924 JIM_STATIC Jim_Nvp *JIM_API(Jim_Nvp_value2name_simple)( const Jim_Nvp *nvp_table, int v );
925
926 JIM_STATIC int JIM_API(Jim_Nvp_name2value)( Jim_Interp *interp, const Jim_Nvp *nvp_table, const char *name, Jim_Nvp **result );
927 JIM_STATIC int JIM_API(Jim_Nvp_name2value_nocase)( Jim_Interp *interp, const Jim_Nvp *nvp_table, const char *name, Jim_Nvp **result);
928 JIM_STATIC int JIM_API(Jim_Nvp_value2name)( Jim_Interp *interp, const Jim_Nvp *nvp_table, int value, Jim_Nvp **result );
929
930 JIM_STATIC int JIM_API(Jim_Nvp_name2value_obj)( Jim_Interp *interp, const Jim_Nvp *nvp_table, Jim_Obj *name_obj, Jim_Nvp **result );
931 JIM_STATIC int JIM_API(Jim_Nvp_name2value_obj_nocase)( Jim_Interp *interp, const Jim_Nvp *nvp_table, Jim_Obj *name_obj, Jim_Nvp **result );
932 JIM_STATIC int JIM_API(Jim_Nvp_value2name_obj)( Jim_Interp *interp, const Jim_Nvp *nvp_table, Jim_Obj *value_obj, Jim_Nvp **result );
933
934 /** prints a nice 'unknown' parameter error message to the 'result' */
935 JIM_STATIC void JIM_API(Jim_SetResult_NvpUnknown)( Jim_Interp *interp,
936 Jim_Obj *param_name,
937 Jim_Obj *param_value,
938 const Jim_Nvp *nvp_table );
939
940
941 /** Debug: convert argc/argv into a printable string for printf() debug
942 *
943 * \param interp - the interpeter
944 * \param argc - arg count
945 * \param argv - the objects
946 *
947 * \returns string pointer holding the text.
948 *
949 * Note, next call to this function will free the old (last) string.
950 *
951 * For example might want do this:
952 * \code
953 * fp = fopen("some.file.log", "a" );
954 * fprintf( fp, "PARAMS are: %s\n", Jim_DebugArgvString( interp, argc, argv ) );
955 * fclose(fp);
956 * \endcode
957 */
958 JIM_STATIC const char *JIM_API( Jim_Debug_ArgvString )( Jim_Interp *interp, int argc, Jim_Obj *const *argv );
959
960
961 /** A TCL -ish GetOpt like code.
962 *
963 * Some TCL objects have various "configuration" values.
964 * For example - in Tcl/Tk the "buttons" have many options.
965 *
966 * Usefull when dealing with command options.
967 * that may come in any order...
968 *
969 * Does not support "-foo=123" type options.
970 * Only supports tcl type options, like "-foo 123"
971 */
972
973 typedef struct jim_getopt {
974 Jim_Interp *interp;
975 int argc;
976 Jim_Obj * const * argv;
977 int isconfigure; /* non-zero if configure */
978 } Jim_GetOptInfo;
979
980 /** GetOpt - how to.
981 *
982 * Example (short and incomplete):
983 * \code
984 * Jim_GetOptInfo goi;
985 *
986 * Jim_GetOpt_Setup( &goi, interp, argc, argv );
987 *
988 * while ( goi.argc ){
989 * e = Jim_GetOpt_Nvp( &goi, nvp_options, &n );
990 * if ( e != JIM_OK ){
991 * Jim_GetOpt_NvpUnknown( &goi, nvp_options, 0 );
992 * return e;
993 * }
994 *
995 * switch( n->value ){
996 * case ALIVE:
997 * printf("Option ALIVE specified\n");
998 * break;
999 * case FIRST:
1000 * if ( goi.argc < 1 ){
1001 * .. not enough args error ..
1002 * }
1003 * Jim_GetOpt_String( &goi, &cp, NULL );
1004 * printf("FIRSTNAME: %s\n", cp );
1005 * case AGE:
1006 * Jim_GetOpt_Wide( &goi, &w );
1007 * printf("AGE: %d\n", (int)(w) );
1008 * break;
1009 * case POLITICS:
1010 * e = Jim_GetOpt_Nvp( &goi, nvp_politics, &n );
1011 * if ( e != JIM_OK ){
1012 * Jim_GetOpt_NvpUnknown( &goi, nvp_politics, 1 );
1013 * return e;
1014 * }
1015 * }
1016 * }
1017 *
1018 * \endcode
1019 *
1020 */
1021
1022 /** Setup GETOPT
1023 *
1024 * \param goi - get opt info to be initialized
1025 * \param interp - jim interp
1026 * \param argc - argc count.
1027 * \param argv - argv (will be copied)
1028 *
1029 * \code
1030 * Jim_GetOptInfo goi;
1031 *
1032 * Jim_GetOptSetup( &goi, interp, argc, argv );
1033 * \endcode
1034 */
1035
1036 JIM_STATIC int JIM_API( Jim_GetOpt_Setup )( Jim_GetOptInfo *goi,
1037 Jim_Interp *interp,
1038 int argc,
1039 Jim_Obj * const * argv );
1040
1041
1042 /** Debug - Dump parameters to stderr
1043 * \param goi - current parameters
1044 */
1045 JIM_STATIC void JIM_API( Jim_GetOpt_Debug )( Jim_GetOptInfo *goi);
1046
1047
1048
1049 /** Remove argv[0] from the list.
1050 *
1051 * \param goi - get opt info
1052 * \param puthere - where param is put
1053 *
1054 */
1055 JIM_STATIC int JIM_API( Jim_GetOpt_Obj)( Jim_GetOptInfo *goi, Jim_Obj **puthere );
1056
1057 /** Remove argv[0] as string.
1058 *
1059 * \param goi - get opt info
1060 * \param puthere - where param is put
1061 * \param len - return its length
1062 */
1063 JIM_STATIC int JIM_API( Jim_GetOpt_String )( Jim_GetOptInfo *goi, char **puthere, int *len );
1064
1065 /** Remove argv[0] as double.
1066 *
1067 * \param goi - get opt info
1068 * \param puthere - where param is put.
1069 *
1070 */
1071 JIM_STATIC int JIM_API( Jim_GetOpt_Double )( Jim_GetOptInfo *goi, double *puthere );
1072
1073 /** Remove argv[0] as wide.
1074 *
1075 * \param goi - get opt info
1076 * \param puthere - where param is put.
1077 */
1078 JIM_STATIC int JIM_API( Jim_GetOpt_Wide )( Jim_GetOptInfo *goi, jim_wide *puthere );
1079
1080 /** Remove argv[0] as NVP.
1081 *
1082 * \param goi - get opt info
1083 * \param lookup - nvp lookup table
1084 * \param puthere - where param is put.
1085 *
1086 */
1087 JIM_STATIC int JIM_API( Jim_GetOpt_Nvp)( Jim_GetOptInfo *goi, const Jim_Nvp *lookup, Jim_Nvp **puthere );
1088
1089 /** Create an appropriate error message for an NVP.
1090 *
1091 * \param goi - options info
1092 * \param lookup - the NVP table that was used.
1093 * \param hadprefix - 0 or 1 if the option had a prefix.
1094 *
1095 * This function will set the "interp->result" to a human readable
1096 * error message listing the available options.
1097 *
1098 * This function assumes the previous option argv[-1] is the unknown string.
1099 *
1100 * If this option had some prefix, then pass "hadprefix=1" else pass "hadprefix=0"
1101 *
1102 * Example:
1103 * \code
1104 *
1105 * while ( goi.argc ){
1106 * // Get the next option
1107 * e = Jim_GetOpt_Nvp( &goi, cmd_options, &n );
1108 * if ( e != JIM_OK ){
1109 * // option was not recognized
1110 * // pass 'hadprefix=0' because there is no prefix
1111 * Jim_GetOpt_NvpUnknown( &goi, cmd_options, 0 );
1112 * return e;
1113 * }
1114 *
1115 * switch( n->value ){
1116 * case OPT_SEX:
1117 * // handle: --sex male|female|lots|needmore
1118 * e = Jim_GetOpt_Nvp( &goi, &nvp_sex, &n );
1119 * if ( e != JIM_OK ){
1120 * Jim_GetOpt_NvpUnknown( &ogi, nvp_sex, 1 );
1121 * return e;
1122 * }
1123 * printf("Code: (%d) is %s\n", n->value, n->name );
1124 * break;
1125 * case ...:
1126 * [snip]
1127 * }
1128 * }
1129 * \endcode
1130 *
1131 */
1132 JIM_STATIC void JIM_API( Jim_GetOpt_NvpUnknown)( Jim_GetOptInfo *goi, const Jim_Nvp *lookup, int hadprefix );
1133
1134
1135 /** Remove argv[0] as Enum
1136 *
1137 * \param goi - get opt info
1138 * \param lookup - lookup table.
1139 * \param puthere - where param is put.
1140 *
1141 */
1142 JIM_STATIC int JIM_API( Jim_GetOpt_Enum)( Jim_GetOptInfo *goi, const char * const * lookup, int *puthere );
1143
1144
1145 #undef JIM_STATIC
1146 #undef JIM_API
1147
1148 #ifndef __JIM_CORE__
1149
1150 #define JIM_GET_API(name) \
1151 Jim_GetApi(interp, "Jim_" #name, ((void *)&Jim_ ## name))
1152
1153 #if defined JIM_EXTENSION || defined JIM_EMBEDDED
1154 /* This must be included "inline" inside the extension */
1155 static void Jim_InitExtension(Jim_Interp *interp)
1156 {
1157 Jim_GetApi = interp->getApiFuncPtr;
1158
1159 JIM_GET_API(Alloc);
1160 JIM_GET_API(Free);
1161 JIM_GET_API(Eval);
1162 JIM_GET_API(Eval_Named);
1163 JIM_GET_API(EvalGlobal);
1164 JIM_GET_API(EvalFile);
1165 JIM_GET_API(EvalObj);
1166 JIM_GET_API(EvalObjBackground);
1167 JIM_GET_API(EvalObjVector);
1168 JIM_GET_API(InitHashTable);
1169 JIM_GET_API(ExpandHashTable);
1170 JIM_GET_API(AddHashEntry);
1171 JIM_GET_API(ReplaceHashEntry);
1172 JIM_GET_API(DeleteHashEntry);
1173 JIM_GET_API(FreeHashTable);
1174 JIM_GET_API(FindHashEntry);
1175 JIM_GET_API(ResizeHashTable);
1176 JIM_GET_API(GetHashTableIterator);
1177 JIM_GET_API(NextHashEntry);
1178 JIM_GET_API(NewObj);
1179 JIM_GET_API(FreeObj);
1180 JIM_GET_API(InvalidateStringRep);
1181 JIM_GET_API(InitStringRep);
1182 JIM_GET_API(DuplicateObj);
1183 JIM_GET_API(GetString);
1184 JIM_GET_API(Length);
1185 JIM_GET_API(InvalidateStringRep);
1186 JIM_GET_API(NewStringObj);
1187 JIM_GET_API(NewStringObjNoAlloc);
1188 JIM_GET_API(AppendString);
1189 JIM_GET_API(AppendString_sprintf);
1190 JIM_GET_API(AppendObj);
1191 JIM_GET_API(AppendStrings);
1192 JIM_GET_API(StringEqObj);
1193 JIM_GET_API(StringMatchObj);
1194 JIM_GET_API(StringRangeObj);
1195 JIM_GET_API(FormatString);
1196 JIM_GET_API(ScanString);
1197 JIM_GET_API(CompareStringImmediate);
1198 JIM_GET_API(NewReference);
1199 JIM_GET_API(GetReference);
1200 JIM_GET_API(SetFinalizer);
1201 JIM_GET_API(GetFinalizer);
1202 JIM_GET_API(CreateInterp);
1203 JIM_GET_API(FreeInterp);
1204 JIM_GET_API(GetExitCode);
1205 JIM_GET_API(SetStdin);
1206 JIM_GET_API(SetStdout);
1207 JIM_GET_API(SetStderr);
1208 JIM_GET_API(CreateCommand);
1209 JIM_GET_API(CreateProcedure);
1210 JIM_GET_API(DeleteCommand);
1211 JIM_GET_API(RenameCommand);
1212 JIM_GET_API(GetCommand);
1213 JIM_GET_API(SetVariable);
1214 JIM_GET_API(SetVariableStr);
1215 JIM_GET_API(SetGlobalVariableStr);
1216 JIM_GET_API(SetVariableStrWithStr);
1217 JIM_GET_API(SetVariableLink);
1218 JIM_GET_API(GetVariable);
1219 JIM_GET_API(GetCallFrameByLevel);
1220 JIM_GET_API(Collect);
1221 JIM_GET_API(CollectIfNeeded);
1222 JIM_GET_API(GetIndex);
1223 JIM_GET_API(NewListObj);
1224 JIM_GET_API(ListInsertElements);
1225 JIM_GET_API(ListAppendElement);
1226 JIM_GET_API(ListAppendList);
1227 JIM_GET_API(ListLength);
1228 JIM_GET_API(ListIndex);
1229 JIM_GET_API(SetListIndex);
1230 JIM_GET_API(ConcatObj);
1231 JIM_GET_API(NewDictObj);
1232 JIM_GET_API(DictKey);
1233 JIM_GET_API(DictKeysVector);
1234 JIM_GET_API(GetIndex);
1235 JIM_GET_API(GetReturnCode);
1236 JIM_GET_API(EvalExpression);
1237 JIM_GET_API(GetBoolFromExpr);
1238 JIM_GET_API(GetWide);
1239 JIM_GET_API(GetLong);
1240 JIM_GET_API(SetWide);
1241 JIM_GET_API(NewIntObj);
1242 JIM_GET_API(GetDouble);
1243 JIM_GET_API(SetDouble);
1244 JIM_GET_API(NewDoubleObj);
1245 JIM_GET_API(WrongNumArgs);
1246 JIM_GET_API(SetDictKeysVector);
1247 JIM_GET_API(SubstObj);
1248 JIM_GET_API(RegisterApi);
1249 JIM_GET_API(PrintErrorMessage);
1250 JIM_GET_API(InteractivePrompt);
1251 JIM_GET_API(RegisterCoreCommands);
1252 JIM_GET_API(GetSharedString);
1253 JIM_GET_API(ReleaseSharedString);
1254 JIM_GET_API(Panic);
1255 JIM_GET_API(StrDup);
1256 JIM_GET_API(UnsetVariable);
1257 JIM_GET_API(GetVariableStr);
1258 JIM_GET_API(GetGlobalVariable);
1259 JIM_GET_API(GetGlobalVariableStr);
1260 JIM_GET_API(GetAssocData);
1261 JIM_GET_API(SetAssocData);
1262 JIM_GET_API(DeleteAssocData);
1263 JIM_GET_API(GetEnum);
1264 JIM_GET_API(GetNvp);
1265 JIM_GET_API(ScriptIsComplete);
1266 JIM_GET_API(PackageProvide);
1267 JIM_GET_API(PackageRequire);
1268 JIM_GET_API(InitStack);
1269 JIM_GET_API(FreeStack);
1270 JIM_GET_API(StackLen);
1271 JIM_GET_API(StackPush);
1272 JIM_GET_API(StackPop);
1273 JIM_GET_API(StackPeek);
1274 JIM_GET_API(FreeStackElements);
1275 JIM_GET_API(fprintf );
1276 JIM_GET_API(vfprintf );
1277 JIM_GET_API(fwrite );
1278 JIM_GET_API(fread );
1279 JIM_GET_API(fflush );
1280 JIM_GET_API(fgets );
1281 JIM_GET_API(Nvp_name2value);
1282 JIM_GET_API(Nvp_name2value_nocase);
1283 JIM_GET_API(Nvp_name2value_simple);
1284
1285 JIM_GET_API(Nvp_value2name);
1286 JIM_GET_API(Nvp_value2name_simple);
1287
1288
1289 JIM_GET_API(Nvp_name2value_obj);
1290 JIM_GET_API(Nvp_value2name_obj);
1291 JIM_GET_API(Nvp_name2value_obj_nocase);
1292
1293 JIM_GET_API(GetOpt_Setup);
1294 JIM_GET_API(GetOpt_Obj);
1295 JIM_GET_API(GetOpt_String);
1296 JIM_GET_API(GetOpt_Double);
1297 JIM_GET_API(GetOpt_Wide);
1298 JIM_GET_API(GetOpt_Nvp);
1299 JIM_GET_API(GetOpt_NvpUnknown);
1300 JIM_GET_API(GetOpt_Enum);
1301 JIM_GET_API(GetOpt_Debug);
1302 JIM_GET_API(SetResult_sprintf);
1303 JIM_GET_API(SetResult_NvpUnknown);
1304 JIM_GET_API(Debug_ArgvString);
1305 }
1306 #endif /* defined JIM_EXTENSION || defined JIM_EMBEDDED */
1307
1308 #undef JIM_GET_API
1309
1310 #ifdef JIM_EMBEDDED
1311 Jim_Interp *ExportedJimCreateInterp(void);
1312 static __inline__ void Jim_InitEmbedded(void) {
1313 Jim_Interp *i = ExportedJimCreateInterp();
1314 Jim_InitExtension(i);
1315 Jim_FreeInterp(i);
1316 }
1317 #endif /* JIM_EMBEDDED */
1318 #endif /* __JIM_CORE__ */
1319
1320 #ifdef __cplusplus
1321 }
1322 #endif
1323
1324 #endif /* __JIM__H */