1 /* Jim - A small embeddable Tcl interpreter
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>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials
20 * provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE JIM TCL PROJECT ``AS IS'' AND ANY
23 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * The views and conclusions contained in the software and documentation
36 * are those of the authors and should not be interpreted as representing
37 * official policies, either expressed or implied, of the Jim Tcl Project.
39 *--- Inline Header File Documentation ---
40 * [By Duane Ellis, openocd@duaneellis.com, 8/18/8]
42 * Belief is "Jim" would greatly benifit if Jim Internals where
43 * documented in some way - form whatever, and perhaps - the package:
44 * 'doxygen' is the correct approach to do that.
46 * Details, see: http://www.stack.nl/~dimitri/doxygen/
48 * To that end please follow these guide lines:
50 * (A) Document the PUBLIC api in the .H file.
52 * (B) Document JIM Internals, in the .C file.
54 * (C) Remember JIM is embedded in other packages, to that end do
55 * not assume that your way of documenting is the right way, Jim's
56 * public documentation should be agnostic, such that it is some
57 * what agreeable with the "package" that is embedding JIM inside
58 * of it's own doxygen documentation.
60 * (D) Use minimal Doxygen tags.
62 * This will be an "ongoing work in progress" for some time.
74 #include <stdio.h> /* for the FILE typedef definition */
75 #include <stdlib.h> /* In order to export the Jim_Free() macro */
76 #include <stdarg.h> /* In order to get type va_list */
78 /* -----------------------------------------------------------------------------
79 * System configuration
80 * autoconf (configure) will set these
81 * ---------------------------------------------------------------------------*/
82 #include <jim-win32compat.h>
84 #ifndef HAVE_NO_AUTOCONF
85 #include <jim-config.h>
88 /* -----------------------------------------------------------------------------
89 * Compiler specific fixes.
90 * ---------------------------------------------------------------------------*/
92 /* Long Long type and related issues */
94 # ifdef HAVE_LONG_LONG
95 # define jim_wide long long
97 # define LLONG_MAX 9223372036854775807LL
100 # define LLONG_MIN (-LLONG_MAX - 1LL)
102 # define JIM_WIDE_MIN LLONG_MIN
103 # define JIM_WIDE_MAX LLONG_MAX
105 # define jim_wide long
106 # define JIM_WIDE_MIN LONG_MIN
107 # define JIM_WIDE_MAX LONG_MAX
110 /* -----------------------------------------------------------------------------
111 * LIBC specific fixes
112 * ---------------------------------------------------------------------------*/
114 # ifdef HAVE_LONG_LONG
115 # define JIM_WIDE_MODIFIER "lld"
117 # define JIM_WIDE_MODIFIER "ld"
118 # define strtoull strtoul
122 #define UCHAR(c) ((unsigned char)(c))
124 /* -----------------------------------------------------------------------------
126 * ---------------------------------------------------------------------------*/
128 /* Jim version numbering: every version of jim is marked with a
129 * successive integer number. This is version 0. The first
130 * stable version will be 1, then 2, 3, and so on. */
131 #define JIM_VERSION 71
137 #define JIM_CONTINUE 4
140 /* The following are internal codes and should never been seen/used */
143 #define JIM_MAX_NESTING_DEPTH 1000 /* default max nesting depth */
145 /* Some function get an integer argument with flags to change
147 #define JIM_NONE 0 /* no flags set */
148 #define JIM_ERRMSG 1 /* set an error message in the interpreter. */
150 #define JIM_UNSHARED 4 /* Flag to Jim_GetVariable() */
152 /* Flags for Jim_SubstObj() */
153 #define JIM_SUBST_NOVAR 1 /* don't perform variables substitutions */
154 #define JIM_SUBST_NOCMD 2 /* don't perform command substitutions */
155 #define JIM_SUBST_NOESC 4 /* don't perform escapes substitutions */
156 #define JIM_SUBST_FLAG 128 /* flag to indicate that this is a real substition object */
158 /* Unused arguments generate annoying warnings... */
159 #define JIM_NOTUSED(V) ((void) V)
161 /* Flags for Jim_GetEnum() */
162 #define JIM_ENUM_ABBREV 2 /* Allow unambiguous abbreviation */
164 /* Flags used by API calls getting a 'nocase' argument. */
165 #define JIM_CASESENS 0 /* case sensitive */
166 #define JIM_NOCASE 1 /* no case */
168 /* Filesystem related */
169 #define JIM_PATH_LEN 1024
171 /* Newline, some embedded system may need -DJIM_CRLF */
173 #define JIM_NL "\r\n"
178 #define JIM_LIBPATH "auto_path"
179 #define JIM_INTERACTIVE "tcl_interactive"
181 /* -----------------------------------------------------------------------------
183 * ---------------------------------------------------------------------------*/
185 typedef struct Jim_Stack
{
191 /* -----------------------------------------------------------------------------
193 * ---------------------------------------------------------------------------*/
195 typedef struct Jim_HashEntry
{
201 struct Jim_HashEntry
*next
;
204 typedef struct Jim_HashTableType
{
205 unsigned int (*hashFunction
)(const void *key
);
206 const void *(*keyDup
)(void *privdata
, const void *key
);
207 void *(*valDup
)(void *privdata
, const void *obj
);
208 int (*keyCompare
)(void *privdata
, const void *key1
, const void *key2
);
209 void (*keyDestructor
)(void *privdata
, const void *key
);
210 void (*valDestructor
)(void *privdata
, void *obj
);
213 typedef struct Jim_HashTable
{
214 Jim_HashEntry
**table
;
215 const Jim_HashTableType
*type
;
217 unsigned int sizemask
;
219 unsigned int collisions
;
223 typedef struct Jim_HashTableIterator
{
226 Jim_HashEntry
*entry
, *nextEntry
;
227 } Jim_HashTableIterator
;
229 /* This is the initial size of every hash table */
230 #define JIM_HT_INITIAL_SIZE 16
232 /* ------------------------------- Macros ------------------------------------*/
233 #define Jim_FreeEntryVal(ht, entry) \
234 if ((ht)->type->valDestructor) \
235 (ht)->type->valDestructor((ht)->privdata, (entry)->u.val)
237 #define Jim_SetHashVal(ht, entry, _val_) do { \
238 if ((ht)->type->valDup) \
239 entry->u.val = (ht)->type->valDup((ht)->privdata, _val_); \
241 entry->u.val = (_val_); \
244 #define Jim_FreeEntryKey(ht, entry) \
245 if ((ht)->type->keyDestructor) \
246 (ht)->type->keyDestructor((ht)->privdata, (entry)->key)
248 #define Jim_SetHashKey(ht, entry, _key_) do { \
249 if ((ht)->type->keyDup) \
250 entry->key = (ht)->type->keyDup((ht)->privdata, _key_); \
252 entry->key = (_key_); \
255 #define Jim_CompareHashKeys(ht, key1, key2) \
256 (((ht)->type->keyCompare) ? \
257 (ht)->type->keyCompare((ht)->privdata, key1, key2) : \
260 #define Jim_HashKey(ht, key) (ht)->type->hashFunction(key)
262 #define Jim_GetHashEntryKey(he) ((he)->key)
263 #define Jim_GetHashEntryVal(he) ((he)->val)
264 #define Jim_GetHashTableCollisions(ht) ((ht)->collisions)
265 #define Jim_GetHashTableSize(ht) ((ht)->size)
266 #define Jim_GetHashTableUsed(ht) ((ht)->used)
268 /* -----------------------------------------------------------------------------
270 * ---------------------------------------------------------------------------*/
272 /* -----------------------------------------------------------------------------
273 * Jim object. This is mostly the same as Tcl_Obj itself,
274 * with the addition of the 'prev' and 'next' pointers.
275 * In Jim all the objects are stored into a linked list for GC purposes,
276 * so that it's possible to access every object living in a given interpreter
277 * sequentially. When an object is freed, it's moved into a different
278 * linked list, used as object pool.
280 * The refcount of a freed object is always -1.
281 * ---------------------------------------------------------------------------*/
282 typedef struct Jim_Obj
{
283 int refCount
; /* reference count */
284 char *bytes
; /* string representation buffer. NULL = no string repr. */
285 int length
; /* number of bytes in 'bytes', not including the numterm. */
286 const struct Jim_ObjType
*typePtr
; /* object type. */
287 /* Internal representation union */
289 /* integer number type */
291 /* hashed object type value */
295 /* return code type */
297 /* double number type */
299 /* Generic pointer */
301 /* Generic two pointers value */
306 /* Variable object */
308 unsigned jim_wide callFrameId
;
309 struct Jim_Var
*varPtr
;
313 unsigned jim_wide procEpoch
;
314 struct Jim_Cmd
*cmdPtr
;
318 struct Jim_Obj
**ele
; /* Elements vector */
319 int len
; /* Length */
320 int maxLen
; /* Allocated 'ele' length */
325 int charLength
; /* utf-8 char length. -1 if unknown */
330 struct Jim_Reference
*refPtr
;
334 const char *fileName
;
337 /* Dict substitution type */
339 struct Jim_Obj
*varNameObjPtr
;
340 struct Jim_Obj
*indexObjPtr
;
342 /* tagged binary type */
347 /* Regular expression pattern */
350 void *compre
; /* really an allocated (regex_t *) */
357 /* This are 8 or 16 bytes more for every object
358 * but this is required for efficient garbage collection
359 * of Jim references. */
360 struct Jim_Obj
*prevObjPtr
; /* pointer to the prev object. */
361 struct Jim_Obj
*nextObjPtr
; /* pointer to the next object. */
364 /* Jim_Obj related macros */
365 #define Jim_IncrRefCount(objPtr) \
367 #define Jim_DecrRefCount(interp, objPtr) \
368 if (--(objPtr)->refCount <= 0) Jim_FreeObj(interp, objPtr)
369 #define Jim_IsShared(objPtr) \
370 ((objPtr)->refCount > 1)
372 /* This macro is used when we allocate a new object using
373 * Jim_New...Obj(), but for some error we need to destroy it.
374 * Instead to use Jim_IncrRefCount() + Jim_DecrRefCount() we
375 * can just call Jim_FreeNewObj. To call Jim_Free directly
376 * seems too raw, the object handling may change and we want
377 * that Jim_FreeNewObj() can be called only against objects
378 * that are belived to have refcount == 0. */
379 #define Jim_FreeNewObj Jim_FreeObj
381 /* Free the internal representation of the object. */
382 #define Jim_FreeIntRep(i,o) \
383 if ((o)->typePtr && (o)->typePtr->freeIntRepProc) \
384 (o)->typePtr->freeIntRepProc(i, o)
386 /* Get the internal representation pointer */
387 #define Jim_GetIntRepPtr(o) (o)->internalRep.ptr
389 /* Set the internal representation pointer */
390 #define Jim_SetIntRepPtr(o, p) \
391 (o)->internalRep.ptr = (p)
393 /* The object type structure.
394 * There are four methods.
396 * - FreeIntRep is used to free the internal representation of the object.
397 * Can be NULL if there is nothing to free.
398 * - DupIntRep is used to duplicate the internal representation of the object.
399 * If NULL, when an object is duplicated, the internalRep union is
400 * directly copied from an object to another.
401 * Note that it's up to the caller to free the old internal repr of the
402 * object before to call the Dup method.
403 * - UpdateString is used to create the string from the internal repr.
404 * - setFromAny is used to convert the current object into one of this type.
409 typedef void (Jim_FreeInternalRepProc
)(struct Jim_Interp
*interp
,
410 struct Jim_Obj
*objPtr
);
411 typedef void (Jim_DupInternalRepProc
)(struct Jim_Interp
*interp
,
412 struct Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
);
413 typedef void (Jim_UpdateStringProc
)(struct Jim_Obj
*objPtr
);
415 typedef struct Jim_ObjType
{
416 const char *name
; /* The name of the type. */
417 Jim_FreeInternalRepProc
*freeIntRepProc
;
418 Jim_DupInternalRepProc
*dupIntRepProc
;
419 Jim_UpdateStringProc
*updateStringProc
;
423 /* Jim_ObjType flags */
424 #define JIM_TYPE_NONE 0 /* No flags */
425 #define JIM_TYPE_REFERENCES 1 /* The object may contain referneces. */
427 /* Starting from 1 << 20 flags are reserved for private uses of
428 * different calls. This way the same 'flags' argument may be used
429 * to pass both global flags and private flags. */
430 #define JIM_PRIV_FLAG_SHIFT 20
432 /* -----------------------------------------------------------------------------
433 * Call frame, vars, commands structures
434 * ---------------------------------------------------------------------------*/
437 typedef struct Jim_CallFrame
{
438 unsigned jim_wide id
; /* Call Frame ID. Used for caching. */
439 int level
; /* Level of this call frame. 0 = global */
440 struct Jim_HashTable vars
; /* Where local vars are stored */
441 struct Jim_HashTable
*staticVars
; /* pointer to procedure static vars */
442 struct Jim_CallFrame
*parentCallFrame
;
443 Jim_Obj
*const *argv
; /* object vector of the current procedure call. */
444 int argc
; /* number of args of the current procedure call. */
445 Jim_Obj
*procArgsObjPtr
; /* arglist object of the running procedure */
446 Jim_Obj
*procBodyObjPtr
; /* body object of the running procedure */
447 struct Jim_CallFrame
*nextFramePtr
;
448 const char *filename
; /* file and line of caller of this proc (if available) */
452 /* The var structure. It just holds the pointer of the referenced
453 * object. If linkFramePtr is not NULL the variable is a link
454 * to a variable of name store on objPtr living on the given callframe
455 * (this happens when the [global] or [upvar] command is used).
456 * The interp in order to always know how to free the Jim_Obj associated
457 * with a given variable because In Jim objects memory managment is
458 * bound to interpreters. */
459 typedef struct Jim_Var
{
461 struct Jim_CallFrame
*linkFramePtr
;
464 /* The cmd structure. */
465 typedef int (*Jim_CmdProc
)(struct Jim_Interp
*interp
, int argc
,
466 Jim_Obj
*const *argv
);
467 typedef void (*Jim_DelCmdProc
)(struct Jim_Interp
*interp
, void *privData
);
469 /* A command is implemented in C if funcPtr is != NULL, otherwise
470 * it's a Tcl procedure with the arglist and body represented by the
471 * two objects referenced by arglistObjPtr and bodyoObjPtr. */
472 typedef struct Jim_Cmd
{
473 int inUse
; /* Reference count */
474 int isproc
; /* Is this a procedure? */
477 /* native (C) command */
478 Jim_CmdProc cmdProc
; /* The command implementation */
479 Jim_DelCmdProc delProc
; /* Called when the command is deleted if != NULL */
480 void *privData
; /* command-private data available via Jim_CmdPrivData() */
484 Jim_Obj
*argListObjPtr
;
486 Jim_HashTable
*staticVars
; /* Static vars hash table. NULL if no statics. */
487 int leftArity
; /* Required args assigned from the left */
488 int optionalArgs
; /* Number of optional args (default values) */
489 int rightArity
; /* Required args assigned from the right */
490 int args
; /* True if 'args' specified */
491 struct Jim_Cmd
*prevCmd
; /* Previous command defn if proc created 'local' */
492 int upcall
; /* True if proc is currently in upcall */
497 /* Pseudo Random Number Generator State structure */
498 typedef struct Jim_PrngState
{
499 unsigned char sbox
[256];
503 /* -----------------------------------------------------------------------------
504 * Jim interpreter structure.
505 * Fields similar to the real Tcl interpreter structure have the same names.
506 * ---------------------------------------------------------------------------*/
507 typedef struct Jim_Interp
{
508 Jim_Obj
*result
; /* object returned by the last command called. */
509 int errorLine
; /* Error line where an error occurred. */
510 char *errorFileName
; /* Error file where an error occurred. */
511 int addStackTrace
; /* > 0 If a level should be added to the stack trace */
512 int maxNestingDepth
; /* Used for infinite loop detection. */
513 int returnCode
; /* Completion code to return on JIM_RETURN. */
514 int returnLevel
; /* Current level of 'return -level' */
515 int exitCode
; /* Code to return to the OS on JIM_EXIT. */
516 long id
; /* Hold unique id for various purposes */
517 int signal_level
; /* A nesting level of catch -signal */
518 jim_wide sigmask
; /* Bit mask of caught signals, or 0 if none */
519 int (*signal_set_result
)(struct Jim_Interp
*interp
, jim_wide sigmask
); /* Set a result for the sigmask */
520 Jim_CallFrame
*framePtr
; /* Pointer to the current call frame */
521 Jim_CallFrame
*topFramePtr
; /* toplevel/global frame pointer. */
522 struct Jim_HashTable commands
; /* Commands hash table */
523 unsigned jim_wide procEpoch
; /* Incremented every time the result
524 of procedures names lookup caching
525 may no longer be valid. */
526 unsigned jim_wide callFrameEpoch
; /* Incremented every time a new
527 callframe is created. This id is used for the
528 'ID' field contained in the Jim_CallFrame
530 int local
; /* If 'local' is in effect, newly defined procs keep a reference to the old defn */
531 Jim_Obj
*liveList
; /* Linked list of all the live objects. */
532 Jim_Obj
*freeList
; /* Linked list of all the unused objects. */
533 Jim_Obj
*currentScriptObj
; /* Script currently in execution. */
534 Jim_Obj
*emptyObj
; /* Shared empty string object. */
535 Jim_Obj
*trueObj
; /* Shared true int object. */
536 Jim_Obj
*falseObj
; /* Shared false int object. */
537 unsigned jim_wide referenceNextId
; /* Next id for reference. */
538 struct Jim_HashTable references
; /* References hash table. */
539 jim_wide lastCollectId
; /* reference max Id of the last GC
540 execution. It's set to -1 while the collection
541 is running as sentinel to avoid to recursive
542 calls via the [collect] command inside
544 time_t lastCollectTime
; /* unix time of the last GC execution */
545 struct Jim_HashTable sharedStrings
; /* Shared Strings hash table */
546 Jim_Obj
*stackTrace
; /* Stack trace object. */
547 Jim_Obj
*errorProc
; /* Name of last procedure which returned an error */
548 Jim_Obj
*unknown
; /* Unknown command cache */
549 int unknown_called
; /* The unknown command has been invoked */
550 int errorFlag
; /* Set if an error occurred during execution. */
551 void *cmdPrivData
; /* Used to pass the private data pointer to
552 a command. It is set to what the user specified
553 via Jim_CreateCommand(). */
555 struct Jim_CallFrame
*freeFramesList
; /* list of CallFrame structures. */
556 struct Jim_HashTable assocData
; /* per-interp storage for use by packages */
557 Jim_PrngState
*prngState
; /* per interpreter Random Number Gen. state. */
558 struct Jim_HashTable packages
; /* Provided packages hash table */
559 Jim_Stack
*localProcs
; /* procs to be destroyed on end of evaluation */
560 Jim_Stack
*loadHandles
; /* handles of loaded modules [load] */
563 /* Currently provided as macro that performs the increment.
564 * At some point may be a real function doing more work.
565 * The proc epoch is used in order to know when a command lookup
566 * cached can no longer considered valid. */
567 #define Jim_InterpIncrProcEpoch(i) (i)->procEpoch++
568 #define Jim_SetResultString(i,s,l) Jim_SetResult(i, Jim_NewStringObj(i,s,l))
569 #define Jim_SetResultInt(i,intval) Jim_SetResult(i, Jim_NewIntObj(i,intval))
570 /* Note: Using trueObj and falseObj here makes some things slower...*/
571 #define Jim_SetResultBool(i,b) Jim_SetResultInt(i, b)
572 #define Jim_SetEmptyResult(i) Jim_SetResult(i, (i)->emptyObj)
573 #define Jim_GetResult(i) ((i)->result)
574 #define Jim_CmdPrivData(i) ((i)->cmdPrivData)
575 #define Jim_String(o) Jim_GetString((o), NULL)
577 /* Note that 'o' is expanded only one time inside this macro,
578 * so it's safe to use side effects. */
579 #define Jim_SetResult(i,o) do { \
580 Jim_Obj *_resultObjPtr_ = (o); \
581 Jim_IncrRefCount(_resultObjPtr_); \
582 Jim_DecrRefCount(i,(i)->result); \
583 (i)->result = _resultObjPtr_; \
586 /* Use this for filehandles, etc. which need a unique id */
587 #define Jim_GetId(i) (++(i)->id)
589 /* Reference structure. The interpreter pointer is held within privdata member in HashTable */
590 #define JIM_REFERENCE_TAGLEN 7 /* The tag is fixed-length, because the reference
591 string representation must be fixed length. */
592 typedef struct Jim_Reference
{
594 Jim_Obj
*finalizerCmdNamePtr
;
595 char tag
[JIM_REFERENCE_TAGLEN
+1];
598 /* -----------------------------------------------------------------------------
599 * Exported API prototypes.
600 * ---------------------------------------------------------------------------*/
602 /* Macros that are common for extensions and core. */
603 #define Jim_NewEmptyStringObj(i) Jim_NewStringObj(i, "", 0)
605 /* The core includes real prototypes, extensions instead
606 * include a global function pointer for every function exported.
607 * Once the extension calls Jim_InitExtension(), the global
608 * functon pointers are set to the value of the STUB table
609 * contained in the Jim_Interp structure.
611 * This makes Jim able to load extensions even if it is statically
612 * linked itself, and to load extensions compiled with different
613 * versions of Jim (as long as the API is still compatible.) */
615 /* Macros are common for core and extensions */
616 #define Jim_FreeHashTableIterator(iter) Jim_Free(iter)
620 /* Memory allocation */
621 JIM_EXPORT
void *Jim_Alloc (int size
);
622 JIM_EXPORT
void *Jim_Realloc(void *ptr
, int size
);
623 JIM_EXPORT
void Jim_Free (void *ptr
);
624 JIM_EXPORT
char * Jim_StrDup (const char *s
);
625 JIM_EXPORT
char *Jim_StrDupLen(const char *s
, int l
);
628 JIM_EXPORT
char **Jim_GetEnviron(void);
629 JIM_EXPORT
void Jim_SetEnviron(char **env
);
632 JIM_EXPORT
int Jim_Eval(Jim_Interp
*interp
, const char *script
);
633 /* in C code, you can do this and get better error messages */
634 /* Jim_Eval_Named( interp, "some tcl commands", __FILE__, __LINE__ ); */
635 JIM_EXPORT
int Jim_Eval_Named(Jim_Interp
*interp
, const char *script
,const char *filename
, int lineno
);
636 JIM_EXPORT
int Jim_EvalGlobal(Jim_Interp
*interp
, const char *script
);
637 JIM_EXPORT
int Jim_EvalFile(Jim_Interp
*interp
, const char *filename
);
638 JIM_EXPORT
int Jim_EvalFileGlobal(Jim_Interp
*interp
, const char *filename
);
639 JIM_EXPORT
int Jim_EvalObj (Jim_Interp
*interp
, Jim_Obj
*scriptObjPtr
);
640 JIM_EXPORT
int Jim_EvalObjVector (Jim_Interp
*interp
, int objc
,
641 Jim_Obj
*const *objv
);
642 JIM_EXPORT
int Jim_EvalObjPrefix(Jim_Interp
*interp
, const char *prefix
,
643 int objc
, Jim_Obj
*const *objv
);
644 JIM_EXPORT
int Jim_SubstObj (Jim_Interp
*interp
, Jim_Obj
*substObjPtr
,
645 Jim_Obj
**resObjPtrPtr
, int flags
);
648 JIM_EXPORT
void Jim_InitStack(Jim_Stack
*stack
);
649 JIM_EXPORT
void Jim_FreeStack(Jim_Stack
*stack
);
650 JIM_EXPORT
int Jim_StackLen(Jim_Stack
*stack
);
651 JIM_EXPORT
void Jim_StackPush(Jim_Stack
*stack
, void *element
);
652 JIM_EXPORT
void * Jim_StackPop(Jim_Stack
*stack
);
653 JIM_EXPORT
void * Jim_StackPeek(Jim_Stack
*stack
);
654 JIM_EXPORT
void Jim_FreeStackElements(Jim_Stack
*stack
, void (*freeFunc
)(void *ptr
));
657 JIM_EXPORT
int Jim_InitHashTable (Jim_HashTable
*ht
,
658 const Jim_HashTableType
*type
, void *privdata
);
659 JIM_EXPORT
int Jim_ExpandHashTable (Jim_HashTable
*ht
,
661 JIM_EXPORT
int Jim_AddHashEntry (Jim_HashTable
*ht
, const void *key
,
663 JIM_EXPORT
int Jim_ReplaceHashEntry (Jim_HashTable
*ht
,
664 const void *key
, void *val
);
665 JIM_EXPORT
int Jim_DeleteHashEntry (Jim_HashTable
*ht
,
667 JIM_EXPORT
int Jim_FreeHashTable (Jim_HashTable
*ht
);
668 JIM_EXPORT Jim_HashEntry
* Jim_FindHashEntry (Jim_HashTable
*ht
,
670 JIM_EXPORT
int Jim_ResizeHashTable (Jim_HashTable
*ht
);
671 JIM_EXPORT Jim_HashTableIterator
*Jim_GetHashTableIterator
673 JIM_EXPORT Jim_HashEntry
* Jim_NextHashEntry
674 (Jim_HashTableIterator
*iter
);
677 JIM_EXPORT Jim_Obj
* Jim_NewObj (Jim_Interp
*interp
);
678 JIM_EXPORT
void Jim_FreeObj (Jim_Interp
*interp
, Jim_Obj
*objPtr
);
679 JIM_EXPORT
void Jim_InvalidateStringRep (Jim_Obj
*objPtr
);
680 JIM_EXPORT
void Jim_InitStringRep (Jim_Obj
*objPtr
, const char *bytes
,
682 JIM_EXPORT Jim_Obj
* Jim_DuplicateObj (Jim_Interp
*interp
,
684 JIM_EXPORT
const char * Jim_GetString(Jim_Obj
*objPtr
,
686 JIM_EXPORT
int Jim_Length(Jim_Obj
*objPtr
);
689 JIM_EXPORT Jim_Obj
* Jim_NewStringObj (Jim_Interp
*interp
,
690 const char *s
, int len
);
691 JIM_EXPORT Jim_Obj
*Jim_NewStringObjUtf8(Jim_Interp
*interp
,
692 const char *s
, int charlen
);
693 JIM_EXPORT Jim_Obj
* Jim_NewStringObjNoAlloc (Jim_Interp
*interp
,
695 JIM_EXPORT
void Jim_AppendString (Jim_Interp
*interp
, Jim_Obj
*objPtr
,
696 const char *str
, int len
);
697 JIM_EXPORT
void Jim_AppendObj (Jim_Interp
*interp
, Jim_Obj
*objPtr
,
698 Jim_Obj
*appendObjPtr
);
699 JIM_EXPORT
void Jim_AppendStrings (Jim_Interp
*interp
,
700 Jim_Obj
*objPtr
, ...);
701 JIM_EXPORT
int Jim_StringEqObj(Jim_Obj
*aObjPtr
, Jim_Obj
*bObjPtr
);
702 JIM_EXPORT
int Jim_StringMatchObj (Jim_Interp
*interp
, Jim_Obj
*patternObjPtr
,
703 Jim_Obj
*objPtr
, int nocase
);
704 JIM_EXPORT Jim_Obj
* Jim_StringRangeObj (Jim_Interp
*interp
,
705 Jim_Obj
*strObjPtr
, Jim_Obj
*firstObjPtr
,
706 Jim_Obj
*lastObjPtr
);
707 JIM_EXPORT Jim_Obj
* Jim_FormatString (Jim_Interp
*interp
,
708 Jim_Obj
*fmtObjPtr
, int objc
, Jim_Obj
*const *objv
);
709 JIM_EXPORT Jim_Obj
* Jim_ScanString (Jim_Interp
*interp
, Jim_Obj
*strObjPtr
,
710 Jim_Obj
*fmtObjPtr
, int flags
);
711 JIM_EXPORT
int Jim_CompareStringImmediate (Jim_Interp
*interp
,
712 Jim_Obj
*objPtr
, const char *str
);
713 JIM_EXPORT
int Jim_StringCompareObj(Jim_Interp
*interp
, Jim_Obj
*firstObjPtr
,
714 Jim_Obj
*secondObjPtr
, int nocase
);
715 JIM_EXPORT
int Jim_Utf8Length(Jim_Interp
*interp
, Jim_Obj
*objPtr
);
717 /* reference object */
718 JIM_EXPORT Jim_Obj
* Jim_NewReference (Jim_Interp
*interp
,
719 Jim_Obj
*objPtr
, Jim_Obj
*tagPtr
, Jim_Obj
*cmdNamePtr
);
720 JIM_EXPORT Jim_Reference
* Jim_GetReference (Jim_Interp
*interp
,
722 JIM_EXPORT
int Jim_SetFinalizer (Jim_Interp
*interp
, Jim_Obj
*objPtr
, Jim_Obj
*cmdNamePtr
);
723 JIM_EXPORT
int Jim_GetFinalizer (Jim_Interp
*interp
, Jim_Obj
*objPtr
, Jim_Obj
**cmdNamePtrPtr
);
726 JIM_EXPORT Jim_Interp
* Jim_CreateInterp (void);
727 JIM_EXPORT
void Jim_FreeInterp (Jim_Interp
*i
);
728 JIM_EXPORT
int Jim_GetExitCode (Jim_Interp
*interp
);
729 JIM_EXPORT
const char *Jim_ReturnCode(int code
);
730 JIM_EXPORT
void Jim_SetResultFormatted(Jim_Interp
*interp
, const char *format
, ...);
733 JIM_EXPORT
void Jim_RegisterCoreCommands (Jim_Interp
*interp
);
734 JIM_EXPORT
int Jim_CreateCommand (Jim_Interp
*interp
,
735 const char *cmdName
, Jim_CmdProc cmdProc
, void *privData
,
736 Jim_DelCmdProc delProc
);
737 JIM_EXPORT
int Jim_DeleteCommand (Jim_Interp
*interp
,
738 const char *cmdName
);
739 JIM_EXPORT
int Jim_RenameCommand (Jim_Interp
*interp
,
740 const char *oldName
, const char *newName
);
741 JIM_EXPORT Jim_Cmd
* Jim_GetCommand (Jim_Interp
*interp
,
742 Jim_Obj
*objPtr
, int flags
);
743 JIM_EXPORT
int Jim_SetVariable (Jim_Interp
*interp
,
744 Jim_Obj
*nameObjPtr
, Jim_Obj
*valObjPtr
);
745 JIM_EXPORT
int Jim_SetVariableStr (Jim_Interp
*interp
,
746 const char *name
, Jim_Obj
*objPtr
);
747 JIM_EXPORT
int Jim_SetGlobalVariableStr (Jim_Interp
*interp
,
748 const char *name
, Jim_Obj
*objPtr
);
749 JIM_EXPORT
int Jim_SetVariableStrWithStr (Jim_Interp
*interp
,
750 const char *name
, const char *val
);
751 JIM_EXPORT
int Jim_SetVariableLink (Jim_Interp
*interp
,
752 Jim_Obj
*nameObjPtr
, Jim_Obj
*targetNameObjPtr
,
753 Jim_CallFrame
*targetCallFrame
);
754 JIM_EXPORT Jim_Obj
* Jim_GetVariable (Jim_Interp
*interp
,
755 Jim_Obj
*nameObjPtr
, int flags
);
756 JIM_EXPORT Jim_Obj
* Jim_GetGlobalVariable (Jim_Interp
*interp
,
757 Jim_Obj
*nameObjPtr
, int flags
);
758 JIM_EXPORT Jim_Obj
* Jim_GetVariableStr (Jim_Interp
*interp
,
759 const char *name
, int flags
);
760 JIM_EXPORT Jim_Obj
* Jim_GetGlobalVariableStr (Jim_Interp
*interp
,
761 const char *name
, int flags
);
762 JIM_EXPORT
int Jim_UnsetVariable (Jim_Interp
*interp
,
763 Jim_Obj
*nameObjPtr
, int flags
);
766 JIM_EXPORT Jim_CallFrame
*Jim_GetCallFrameByLevel(Jim_Interp
*interp
,
767 Jim_Obj
*levelObjPtr
);
769 /* garbage collection */
770 JIM_EXPORT
int Jim_Collect (Jim_Interp
*interp
);
771 JIM_EXPORT
void Jim_CollectIfNeeded (Jim_Interp
*interp
);
774 JIM_EXPORT
int Jim_GetIndex (Jim_Interp
*interp
, Jim_Obj
*objPtr
,
778 JIM_EXPORT Jim_Obj
* Jim_NewListObj (Jim_Interp
*interp
,
779 Jim_Obj
*const *elements
, int len
);
780 JIM_EXPORT
void Jim_ListInsertElements (Jim_Interp
*interp
,
781 Jim_Obj
*listPtr
, int listindex
, int objc
, Jim_Obj
*const *objVec
);
782 JIM_EXPORT
void Jim_ListAppendElement (Jim_Interp
*interp
,
783 Jim_Obj
*listPtr
, Jim_Obj
*objPtr
);
784 JIM_EXPORT
void Jim_ListAppendList (Jim_Interp
*interp
,
785 Jim_Obj
*listPtr
, Jim_Obj
*appendListPtr
);
786 JIM_EXPORT
int Jim_ListLength (Jim_Interp
*interp
, Jim_Obj
*objPtr
);
787 JIM_EXPORT
int Jim_ListIndex (Jim_Interp
*interp
, Jim_Obj
*listPrt
,
788 int listindex
, Jim_Obj
**objPtrPtr
, int seterr
);
789 JIM_EXPORT
int Jim_SetListIndex (Jim_Interp
*interp
,
790 Jim_Obj
*varNamePtr
, Jim_Obj
*const *indexv
, int indexc
,
792 JIM_EXPORT Jim_Obj
* Jim_ConcatObj (Jim_Interp
*interp
, int objc
,
793 Jim_Obj
*const *objv
);
796 JIM_EXPORT Jim_Obj
* Jim_NewDictObj (Jim_Interp
*interp
,
797 Jim_Obj
*const *elements
, int len
);
798 JIM_EXPORT
int Jim_DictKey (Jim_Interp
*interp
, Jim_Obj
*dictPtr
,
799 Jim_Obj
*keyPtr
, Jim_Obj
**objPtrPtr
, int flags
);
800 JIM_EXPORT
int Jim_DictKeysVector (Jim_Interp
*interp
,
801 Jim_Obj
*dictPtr
, Jim_Obj
*const *keyv
, int keyc
,
802 Jim_Obj
**objPtrPtr
, int flags
);
803 JIM_EXPORT
int Jim_SetDictKeysVector (Jim_Interp
*interp
,
804 Jim_Obj
*varNamePtr
, Jim_Obj
*const *keyv
, int keyc
,
806 JIM_EXPORT
int Jim_DictPairs(Jim_Interp
*interp
,
807 Jim_Obj
*dictPtr
, Jim_Obj
***objPtrPtr
, int *len
);
808 JIM_EXPORT
int Jim_DictAddElement(Jim_Interp
*interp
, Jim_Obj
*objPtr
,
809 Jim_Obj
*keyObjPtr
, Jim_Obj
*valueObjPtr
);
810 JIM_EXPORT
int Jim_DictKeys(Jim_Interp
*interp
, Jim_Obj
*objPtr
, Jim_Obj
*patternObj
);
811 JIM_EXPORT
int Jim_DictSize(Jim_Interp
*interp
, Jim_Obj
*objPtr
);
813 /* return code object */
814 JIM_EXPORT
int Jim_GetReturnCode (Jim_Interp
*interp
, Jim_Obj
*objPtr
,
817 /* expression object */
818 JIM_EXPORT
int Jim_EvalExpression (Jim_Interp
*interp
,
819 Jim_Obj
*exprObjPtr
, Jim_Obj
**exprResultPtrPtr
);
820 JIM_EXPORT
int Jim_GetBoolFromExpr (Jim_Interp
*interp
,
821 Jim_Obj
*exprObjPtr
, int *boolPtr
);
824 JIM_EXPORT
int Jim_GetWide (Jim_Interp
*interp
, Jim_Obj
*objPtr
,
826 JIM_EXPORT
int Jim_GetLong (Jim_Interp
*interp
, Jim_Obj
*objPtr
,
828 #define Jim_NewWideObj Jim_NewIntObj
829 JIM_EXPORT Jim_Obj
* Jim_NewIntObj (Jim_Interp
*interp
,
833 JIM_EXPORT
int Jim_GetDouble(Jim_Interp
*interp
, Jim_Obj
*objPtr
,
835 JIM_EXPORT
void Jim_SetDouble(Jim_Interp
*interp
, Jim_Obj
*objPtr
,
837 JIM_EXPORT Jim_Obj
* Jim_NewDoubleObj(Jim_Interp
*interp
, double doubleValue
);
840 JIM_EXPORT
const char * Jim_GetSharedString (Jim_Interp
*interp
,
842 JIM_EXPORT
void Jim_ReleaseSharedString (Jim_Interp
*interp
,
845 /* commands utilities */
846 JIM_EXPORT
void Jim_WrongNumArgs (Jim_Interp
*interp
, int argc
,
847 Jim_Obj
*const *argv
, const char *msg
);
848 JIM_EXPORT
int Jim_GetEnum (Jim_Interp
*interp
, Jim_Obj
*objPtr
,
849 const char * const *tablePtr
, int *indexPtr
, const char *name
, int flags
);
850 JIM_EXPORT
int Jim_ScriptIsComplete (const char *s
, int len
,
853 * Find a matching name in the array of the given length.
855 * NULL entries are ignored.
857 * Returns the matching index if found, or -1 if not.
859 JIM_EXPORT
int Jim_FindByName(const char *name
, const char * const array
[], size_t len
);
861 /* package utilities */
862 typedef void (Jim_InterpDeleteProc
)(Jim_Interp
*interp
, void *data
);
863 JIM_EXPORT
void * Jim_GetAssocData(Jim_Interp
*interp
, const char *key
);
864 JIM_EXPORT
int Jim_SetAssocData(Jim_Interp
*interp
, const char *key
,
865 Jim_InterpDeleteProc
*delProc
, void *data
);
866 JIM_EXPORT
int Jim_DeleteAssocData(Jim_Interp
*interp
, const char *key
);
870 JIM_EXPORT
int Jim_PackageProvide (Jim_Interp
*interp
,
871 const char *name
, const char *ver
, int flags
);
872 JIM_EXPORT
int Jim_PackageRequire (Jim_Interp
*interp
,
873 const char *name
, int flags
);
876 JIM_EXPORT
void Jim_MakeErrorMessage (Jim_Interp
*interp
);
878 /* interactive mode */
879 JIM_EXPORT
int Jim_InteractivePrompt (Jim_Interp
*interp
);
882 JIM_EXPORT
int Jim_InitStaticExtensions(Jim_Interp
*interp
);
883 JIM_EXPORT
int Jim_StringToWide(const char *str
, jim_wide
*widePtr
, int base
);
886 JIM_EXPORT
int Jim_LoadLibrary(Jim_Interp
*interp
, const char *pathName
);
887 JIM_EXPORT
void Jim_FreeLoadHandles(Jim_Interp
*interp
);
890 JIM_EXPORT
FILE *Jim_AioFilehandle(Jim_Interp
*interp
, Jim_Obj
*command
);
893 /* type inspection - avoid where possible */
894 JIM_EXPORT
int Jim_IsDict(Jim_Obj
*objPtr
);
895 JIM_EXPORT
int Jim_IsList(Jim_Obj
*objPtr
);
901 #endif /* __JIM__H */
904 * Local Variables: ***
905 * c-basic-offset: 4 ***