Add exists -alias
[jimtcl.git] / jim.h
blob5a8b7b2b9ce27a26bffab5895179a929fe777cef
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
13 * are met:
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.
63 **/
65 #ifndef __JIM__H
66 #define __JIM__H
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
72 #include <time.h>
73 #include <limits.h>
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>
86 #endif
88 /* -----------------------------------------------------------------------------
89 * Compiler specific fixes.
90 * ---------------------------------------------------------------------------*/
92 /* Long Long type and related issues */
93 #ifndef jim_wide
94 # ifdef HAVE_LONG_LONG
95 # define jim_wide long long
96 # ifndef LLONG_MAX
97 # define LLONG_MAX 9223372036854775807LL
98 # endif
99 # ifndef LLONG_MIN
100 # define LLONG_MIN (-LLONG_MAX - 1LL)
101 # endif
102 # define JIM_WIDE_MIN LLONG_MIN
103 # define JIM_WIDE_MAX LLONG_MAX
104 # else
105 # define jim_wide long
106 # define JIM_WIDE_MIN LONG_MIN
107 # define JIM_WIDE_MAX LONG_MAX
108 # endif
110 /* -----------------------------------------------------------------------------
111 * LIBC specific fixes
112 * ---------------------------------------------------------------------------*/
114 # ifdef HAVE_LONG_LONG
115 # define JIM_WIDE_MODIFIER "lld"
116 # else
117 # define JIM_WIDE_MODIFIER "ld"
118 # define strtoull strtoul
119 # endif
120 #endif
122 #define UCHAR(c) ((unsigned char)(c))
124 /* -----------------------------------------------------------------------------
125 * Exported defines
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 73
133 #define JIM_OK 0
134 #define JIM_ERR 1
135 #define JIM_RETURN 2
136 #define JIM_BREAK 3
137 #define JIM_CONTINUE 4
138 #define JIM_SIGNAL 5
139 #define JIM_EXIT 6
140 /* The following are internal codes and should never been seen/used */
141 #define JIM_EVAL 7
143 #define JIM_MAX_CALLFRAME_DEPTH 1000 /* default max nesting depth for procs */
144 #define JIM_MAX_EVAL_DEPTH 2000 /* default max nesting depth for eval */
146 /* Some function get an integer argument with flags to change
147 * the behaviour. */
148 #define JIM_NONE 0 /* no flags set */
149 #define JIM_ERRMSG 1 /* set an error message in the interpreter. */
151 #define JIM_UNSHARED 4 /* Flag to Jim_GetVariable() */
153 /* Flags for Jim_SubstObj() */
154 #define JIM_SUBST_NOVAR 1 /* don't perform variables substitutions */
155 #define JIM_SUBST_NOCMD 2 /* don't perform command substitutions */
156 #define JIM_SUBST_NOESC 4 /* don't perform escapes substitutions */
157 #define JIM_SUBST_FLAG 128 /* flag to indicate that this is a real substition object */
159 /* Unused arguments generate annoying warnings... */
160 #define JIM_NOTUSED(V) ((void) V)
162 /* Flags for Jim_GetEnum() */
163 #define JIM_ENUM_ABBREV 2 /* Allow unambiguous abbreviation */
165 /* Flags used by API calls getting a 'nocase' argument. */
166 #define JIM_CASESENS 0 /* case sensitive */
167 #define JIM_NOCASE 1 /* no case */
169 /* Filesystem related */
170 #define JIM_PATH_LEN 1024
172 /* Newline, some embedded system may need -DJIM_CRLF */
173 #ifdef JIM_CRLF
174 #define JIM_NL "\r\n"
175 #else
176 #define JIM_NL "\n"
177 #endif
179 #define JIM_LIBPATH "auto_path"
180 #define JIM_INTERACTIVE "tcl_interactive"
182 /* -----------------------------------------------------------------------------
183 * Stack
184 * ---------------------------------------------------------------------------*/
186 typedef struct Jim_Stack {
187 int len;
188 int maxlen;
189 void **vector;
190 } Jim_Stack;
192 /* -----------------------------------------------------------------------------
193 * Hash table
194 * ---------------------------------------------------------------------------*/
196 typedef struct Jim_HashEntry {
197 void *key;
198 union {
199 void *val;
200 int intval;
201 } u;
202 struct Jim_HashEntry *next;
203 } Jim_HashEntry;
205 typedef struct Jim_HashTableType {
206 unsigned int (*hashFunction)(const void *key);
207 void *(*keyDup)(void *privdata, const void *key);
208 void *(*valDup)(void *privdata, const void *obj);
209 int (*keyCompare)(void *privdata, const void *key1, const void *key2);
210 void (*keyDestructor)(void *privdata, void *key);
211 void (*valDestructor)(void *privdata, void *obj);
212 } Jim_HashTableType;
214 typedef struct Jim_HashTable {
215 Jim_HashEntry **table;
216 const Jim_HashTableType *type;
217 unsigned int size;
218 unsigned int sizemask;
219 unsigned int used;
220 unsigned int collisions;
221 void *privdata;
222 } Jim_HashTable;
224 typedef struct Jim_HashTableIterator {
225 Jim_HashTable *ht;
226 int index;
227 Jim_HashEntry *entry, *nextEntry;
228 } Jim_HashTableIterator;
230 /* This is the initial size of every hash table */
231 #define JIM_HT_INITIAL_SIZE 16
233 /* ------------------------------- Macros ------------------------------------*/
234 #define Jim_FreeEntryVal(ht, entry) \
235 if ((ht)->type->valDestructor) \
236 (ht)->type->valDestructor((ht)->privdata, (entry)->u.val)
238 #define Jim_SetHashVal(ht, entry, _val_) do { \
239 if ((ht)->type->valDup) \
240 entry->u.val = (ht)->type->valDup((ht)->privdata, _val_); \
241 else \
242 entry->u.val = (_val_); \
243 } while(0)
245 #define Jim_FreeEntryKey(ht, entry) \
246 if ((ht)->type->keyDestructor) \
247 (ht)->type->keyDestructor((ht)->privdata, (entry)->key)
249 #define Jim_SetHashKey(ht, entry, _key_) do { \
250 if ((ht)->type->keyDup) \
251 entry->key = (ht)->type->keyDup((ht)->privdata, _key_); \
252 else \
253 entry->key = (void *)(_key_); \
254 } while(0)
256 #define Jim_CompareHashKeys(ht, key1, key2) \
257 (((ht)->type->keyCompare) ? \
258 (ht)->type->keyCompare((ht)->privdata, key1, key2) : \
259 (key1) == (key2))
261 #define Jim_HashKey(ht, key) (ht)->type->hashFunction(key)
263 #define Jim_GetHashEntryKey(he) ((he)->key)
264 #define Jim_GetHashEntryVal(he) ((he)->val)
265 #define Jim_GetHashTableCollisions(ht) ((ht)->collisions)
266 #define Jim_GetHashTableSize(ht) ((ht)->size)
267 #define Jim_GetHashTableUsed(ht) ((ht)->used)
269 /* -----------------------------------------------------------------------------
270 * Jim_Obj structure
271 * ---------------------------------------------------------------------------*/
273 /* -----------------------------------------------------------------------------
274 * Jim object. This is mostly the same as Tcl_Obj itself,
275 * with the addition of the 'prev' and 'next' pointers.
276 * In Jim all the objects are stored into a linked list for GC purposes,
277 * so that it's possible to access every object living in a given interpreter
278 * sequentially. When an object is freed, it's moved into a different
279 * linked list, used as object pool.
281 * The refcount of a freed object is always -1.
282 * ---------------------------------------------------------------------------*/
283 typedef struct Jim_Obj {
284 int refCount; /* reference count */
285 char *bytes; /* string representation buffer. NULL = no string repr. */
286 int length; /* number of bytes in 'bytes', not including the null term. */
287 const struct Jim_ObjType *typePtr; /* object type. */
288 /* Internal representation union */
289 union {
290 /* integer number type */
291 jim_wide wideValue;
292 /* generic integer value (e.g. index, return code) */
293 int intValue;
294 /* double number type */
295 double doubleValue;
296 /* Generic pointer */
297 void *ptr;
298 /* Generic two pointers value */
299 struct {
300 void *ptr1;
301 void *ptr2;
302 } twoPtrValue;
303 /* Variable object */
304 struct {
305 unsigned long callFrameId; /* for caching */
306 struct Jim_Var *varPtr;
307 int global; /* If the variable name is globally scoped with :: */
308 } varValue;
309 /* Command object */
310 struct {
311 unsigned long procEpoch; /* for caching */
312 struct Jim_Cmd *cmdPtr;
313 } cmdValue;
314 /* List object */
315 struct {
316 struct Jim_Obj **ele; /* Elements vector */
317 int len; /* Length */
318 int maxLen; /* Allocated 'ele' length */
319 } listValue;
320 /* String type */
321 struct {
322 int maxLength;
323 int charLength; /* utf-8 char length. -1 if unknown */
324 } strValue;
325 /* Reference type */
326 struct {
327 unsigned long id;
328 struct Jim_Reference *refPtr;
329 } refValue;
330 /* Source type */
331 struct {
332 struct Jim_Obj *fileNameObj;
333 int lineNumber;
334 } sourceValue;
335 /* Dict substitution type */
336 struct {
337 struct Jim_Obj *varNameObjPtr;
338 struct Jim_Obj *indexObjPtr;
339 } dictSubstValue;
340 /* Regular expression pattern */
341 struct {
342 unsigned flags;
343 void *compre; /* really an allocated (regex_t *) */
344 } regexpValue;
345 struct {
346 int line;
347 int argc;
348 } scriptLineValue;
349 } internalRep;
350 /* This are 8 or 16 bytes more for every object
351 * but this is required for efficient garbage collection
352 * of Jim references. */
353 struct Jim_Obj *prevObjPtr; /* pointer to the prev object. */
354 struct Jim_Obj *nextObjPtr; /* pointer to the next object. */
355 } Jim_Obj;
357 /* Jim_Obj related macros */
358 #define Jim_IncrRefCount(objPtr) \
359 ++(objPtr)->refCount
360 #define Jim_DecrRefCount(interp, objPtr) \
361 if (--(objPtr)->refCount <= 0) Jim_FreeObj(interp, objPtr)
362 #define Jim_IsShared(objPtr) \
363 ((objPtr)->refCount > 1)
365 /* This macro is used when we allocate a new object using
366 * Jim_New...Obj(), but for some error we need to destroy it.
367 * Instead to use Jim_IncrRefCount() + Jim_DecrRefCount() we
368 * can just call Jim_FreeNewObj. To call Jim_Free directly
369 * seems too raw, the object handling may change and we want
370 * that Jim_FreeNewObj() can be called only against objects
371 * that are belived to have refcount == 0. */
372 #define Jim_FreeNewObj Jim_FreeObj
374 /* Free the internal representation of the object. */
375 #define Jim_FreeIntRep(i,o) \
376 if ((o)->typePtr && (o)->typePtr->freeIntRepProc) \
377 (o)->typePtr->freeIntRepProc(i, o)
379 /* Get the internal representation pointer */
380 #define Jim_GetIntRepPtr(o) (o)->internalRep.ptr
382 /* Set the internal representation pointer */
383 #define Jim_SetIntRepPtr(o, p) \
384 (o)->internalRep.ptr = (p)
386 /* The object type structure.
387 * There are four methods.
389 * - FreeIntRep is used to free the internal representation of the object.
390 * Can be NULL if there is nothing to free.
391 * - DupIntRep is used to duplicate the internal representation of the object.
392 * If NULL, when an object is duplicated, the internalRep union is
393 * directly copied from an object to another.
394 * Note that it's up to the caller to free the old internal repr of the
395 * object before to call the Dup method.
396 * - UpdateString is used to create the string from the internal repr.
397 * - setFromAny is used to convert the current object into one of this type.
400 struct Jim_Interp;
402 typedef void (Jim_FreeInternalRepProc)(struct Jim_Interp *interp,
403 struct Jim_Obj *objPtr);
404 typedef void (Jim_DupInternalRepProc)(struct Jim_Interp *interp,
405 struct Jim_Obj *srcPtr, Jim_Obj *dupPtr);
406 typedef void (Jim_UpdateStringProc)(struct Jim_Obj *objPtr);
408 typedef struct Jim_ObjType {
409 const char *name; /* The name of the type. */
410 Jim_FreeInternalRepProc *freeIntRepProc;
411 Jim_DupInternalRepProc *dupIntRepProc;
412 Jim_UpdateStringProc *updateStringProc;
413 int flags;
414 } Jim_ObjType;
416 /* Jim_ObjType flags */
417 #define JIM_TYPE_NONE 0 /* No flags */
418 #define JIM_TYPE_REFERENCES 1 /* The object may contain referneces. */
420 /* Starting from 1 << 20 flags are reserved for private uses of
421 * different calls. This way the same 'flags' argument may be used
422 * to pass both global flags and private flags. */
423 #define JIM_PRIV_FLAG_SHIFT 20
425 /* -----------------------------------------------------------------------------
426 * Call frame, vars, commands structures
427 * ---------------------------------------------------------------------------*/
429 /* Call frame */
430 typedef struct Jim_CallFrame {
431 unsigned long id; /* Call Frame ID. Used for caching. */
432 int level; /* Level of this call frame. 0 = global */
433 struct Jim_HashTable vars; /* Where local vars are stored */
434 struct Jim_HashTable *staticVars; /* pointer to procedure static vars */
435 struct Jim_CallFrame *parent; /* The parent callframe */
436 Jim_Obj *const *argv; /* object vector of the current procedure call. */
437 int argc; /* number of args of the current procedure call. */
438 Jim_Obj *procArgsObjPtr; /* arglist object of the running procedure */
439 Jim_Obj *procBodyObjPtr; /* body object of the running procedure */
440 struct Jim_CallFrame *next; /* Callframes are in a linked list */
441 Jim_Obj *fileNameObj; /* file and line of caller of this proc (if available) */
442 int line;
443 Jim_Stack *localCommands; /* commands to be destroyed when the call frame is destroyed */
444 } Jim_CallFrame;
446 /* The var structure. It just holds the pointer of the referenced
447 * object. If linkFramePtr is not NULL the variable is a link
448 * to a variable of name store on objPtr living on the given callframe
449 * (this happens when the [global] or [upvar] command is used).
450 * The interp in order to always know how to free the Jim_Obj associated
451 * with a given variable because In Jim objects memory managment is
452 * bound to interpreters. */
453 typedef struct Jim_Var {
454 Jim_Obj *objPtr;
455 struct Jim_CallFrame *linkFramePtr;
456 } Jim_Var;
458 /* The cmd structure. */
459 typedef int (*Jim_CmdProc)(struct Jim_Interp *interp, int argc,
460 Jim_Obj *const *argv);
461 typedef void (*Jim_DelCmdProc)(struct Jim_Interp *interp, void *privData);
465 /* A command is implemented in C if funcPtr is != NULL, otherwise
466 * it's a Tcl procedure with the arglist and body represented by the
467 * two objects referenced by arglistObjPtr and bodyoObjPtr. */
468 typedef struct Jim_Cmd {
469 int inUse; /* Reference count */
470 int isproc; /* Is this a procedure? */
471 struct Jim_Cmd *prevCmd; /* Previous command defn if cmd created 'local' */
472 union {
473 struct {
474 /* native (C) command */
475 Jim_CmdProc cmdProc; /* The command implementation */
476 Jim_DelCmdProc delProc; /* Called when the command is deleted if != NULL */
477 void *privData; /* command-private data available via Jim_CmdPrivData() */
478 } native;
479 struct {
480 /* Tcl procedure */
481 Jim_Obj *argListObjPtr;
482 Jim_Obj *bodyObjPtr;
483 Jim_HashTable *staticVars; /* Static vars hash table. NULL if no statics. */
484 int argListLen; /* Length of argListObjPtr */
485 int reqArity; /* Number of required parameters */
486 int optArity; /* Number of optional parameters */
487 int argsPos; /* Position of 'args', if specified, or -1 */
488 int upcall; /* True if proc is currently in upcall */
489 struct Jim_ProcArg {
490 Jim_Obj *nameObjPtr; /* Name of this arg */
491 Jim_Obj *defaultObjPtr; /* Default value, (or rename for $args) */
492 } *arglist;
493 } proc;
494 } u;
495 } Jim_Cmd;
497 /* Pseudo Random Number Generator State structure */
498 typedef struct Jim_PrngState {
499 unsigned char sbox[256];
500 unsigned int i, j;
501 } Jim_PrngState;
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 Jim_Obj *errorFileNameObj; /* Error file where an error occurred. */
511 int addStackTrace; /* > 0 If a level should be added to the stack trace */
512 int maxCallFrameDepth; /* Used for infinite loop detection. */
513 int maxEvalDepth; /* Used for infinite loop detection. */
514 int evalDepth; /* Current eval depth */
515 int returnCode; /* Completion code to return on JIM_RETURN. */
516 int returnLevel; /* Current level of 'return -level' */
517 int exitCode; /* Code to return to the OS on JIM_EXIT. */
518 long id; /* Hold unique id for various purposes */
519 int signal_level; /* A nesting level of catch -signal */
520 jim_wide sigmask; /* Bit mask of caught signals, or 0 if none */
521 int (*signal_set_result)(struct Jim_Interp *interp, jim_wide sigmask); /* Set a result for the sigmask */
522 Jim_CallFrame *framePtr; /* Pointer to the current call frame */
523 Jim_CallFrame *topFramePtr; /* toplevel/global frame pointer. */
524 struct Jim_HashTable commands; /* Commands hash table */
525 unsigned long procEpoch; /* Incremented every time the result
526 of procedures names lookup caching
527 may no longer be valid. */
528 unsigned long callFrameEpoch; /* Incremented every time a new
529 callframe is created. This id is used for the
530 'ID' field contained in the Jim_CallFrame
531 structure. */
532 int local; /* If 'local' is in effect, newly defined procs keep a reference to the old defn */
533 Jim_Obj *liveList; /* Linked list of all the live objects. */
534 Jim_Obj *freeList; /* Linked list of all the unused objects. */
535 Jim_Obj *currentScriptObj; /* Script currently in execution. */
536 Jim_Obj *emptyObj; /* Shared empty string object. */
537 Jim_Obj *trueObj; /* Shared true int object. */
538 Jim_Obj *falseObj; /* Shared false int object. */
539 unsigned long referenceNextId; /* Next id for reference. */
540 struct Jim_HashTable references; /* References hash table. */
541 unsigned long lastCollectId; /* reference max Id of the last GC
542 execution. It's set to -1 while the collection
543 is running as sentinel to avoid to recursive
544 calls via the [collect] command inside
545 finalizers. */
546 time_t lastCollectTime; /* unix time of the last GC execution */
547 Jim_Obj *stackTrace; /* Stack trace object. */
548 Jim_Obj *errorProc; /* Name of last procedure which returned an error */
549 Jim_Obj *unknown; /* Unknown command cache */
550 int unknown_called; /* The unknown command has been invoked */
551 int errorFlag; /* Set if an error occurred during execution. */
552 void *cmdPrivData; /* Used to pass the private data pointer to
553 a command. It is set to what the user specified
554 via Jim_CreateCommand(). */
556 struct Jim_CallFrame *freeFramesList; /* list of CallFrame structures. */
557 struct Jim_HashTable assocData; /* per-interp storage for use by packages */
558 Jim_PrngState *prngState; /* per interpreter Random Number Gen. state. */
559 struct Jim_HashTable packages; /* Provided packages hash table */
560 Jim_Stack *loadHandles; /* handles of loaded modules [load] */
561 } Jim_Interp;
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)
576 /* Note that 'o' is expanded only one time inside this macro,
577 * so it's safe to use side effects. */
578 #define Jim_SetResult(i,o) do { \
579 Jim_Obj *_resultObjPtr_ = (o); \
580 Jim_IncrRefCount(_resultObjPtr_); \
581 Jim_DecrRefCount(i,(i)->result); \
582 (i)->result = _resultObjPtr_; \
583 } while(0)
585 /* Use this for filehandles, etc. which need a unique id */
586 #define Jim_GetId(i) (++(i)->id)
588 /* Reference structure. The interpreter pointer is held within privdata member in HashTable */
589 #define JIM_REFERENCE_TAGLEN 7 /* The tag is fixed-length, because the reference
590 string representation must be fixed length. */
591 typedef struct Jim_Reference {
592 Jim_Obj *objPtr;
593 Jim_Obj *finalizerCmdNamePtr;
594 char tag[JIM_REFERENCE_TAGLEN+1];
595 } Jim_Reference;
597 /* -----------------------------------------------------------------------------
598 * Exported API prototypes.
599 * ---------------------------------------------------------------------------*/
601 /* Macros that are common for extensions and core. */
602 #define Jim_NewEmptyStringObj(i) Jim_NewStringObj(i, "", 0)
604 /* The core includes real prototypes, extensions instead
605 * include a global function pointer for every function exported.
606 * Once the extension calls Jim_InitExtension(), the global
607 * functon pointers are set to the value of the STUB table
608 * contained in the Jim_Interp structure.
610 * This makes Jim able to load extensions even if it is statically
611 * linked itself, and to load extensions compiled with different
612 * versions of Jim (as long as the API is still compatible.) */
614 /* Macros are common for core and extensions */
615 #define Jim_FreeHashTableIterator(iter) Jim_Free(iter)
617 #define JIM_EXPORT
619 /* Memory allocation */
620 JIM_EXPORT void *Jim_Alloc (int size);
621 JIM_EXPORT void *Jim_Realloc(void *ptr, int size);
622 JIM_EXPORT void Jim_Free (void *ptr);
623 JIM_EXPORT char * Jim_StrDup (const char *s);
624 JIM_EXPORT char *Jim_StrDupLen(const char *s, int l);
626 /* environment */
627 JIM_EXPORT char **Jim_GetEnviron(void);
628 JIM_EXPORT void Jim_SetEnviron(char **env);
630 /* evaluation */
631 JIM_EXPORT int Jim_Eval(Jim_Interp *interp, const char *script);
632 /* in C code, you can do this and get better error messages */
633 /* Jim_EvalSource( interp, __FILE__, __LINE__ , "some tcl commands"); */
634 JIM_EXPORT int Jim_EvalSource(Jim_Interp *interp, const char *filename, int lineno, const char *script);
635 /* Backwards compatibility */
636 #define Jim_Eval_Named(I, S, F, L) Jim_EvalSource((I), (F), (L), (S))
638 JIM_EXPORT int Jim_EvalGlobal(Jim_Interp *interp, const char *script);
639 JIM_EXPORT int Jim_EvalFile(Jim_Interp *interp, const char *filename);
640 JIM_EXPORT int Jim_EvalFileGlobal(Jim_Interp *interp, const char *filename);
641 JIM_EXPORT int Jim_EvalObj (Jim_Interp *interp, Jim_Obj *scriptObjPtr);
642 JIM_EXPORT int Jim_EvalObjVector (Jim_Interp *interp, int objc,
643 Jim_Obj *const *objv);
644 JIM_EXPORT int Jim_EvalObjList(Jim_Interp *interp, Jim_Obj *listObj);
645 JIM_EXPORT int Jim_EvalObjPrefix(Jim_Interp *interp, Jim_Obj *prefix,
646 int objc, Jim_Obj *const *objv);
647 #define Jim_EvalPrefix(i, p, oc, ov) Jim_EvalObjPrefix((i), Jim_NewStringObj((i), (p), -1), (oc), (ov))
648 JIM_EXPORT int Jim_SubstObj (Jim_Interp *interp, Jim_Obj *substObjPtr,
649 Jim_Obj **resObjPtrPtr, int flags);
651 /* stack */
652 JIM_EXPORT void Jim_InitStack(Jim_Stack *stack);
653 JIM_EXPORT void Jim_FreeStack(Jim_Stack *stack);
654 JIM_EXPORT int Jim_StackLen(Jim_Stack *stack);
655 JIM_EXPORT void Jim_StackPush(Jim_Stack *stack, void *element);
656 JIM_EXPORT void * Jim_StackPop(Jim_Stack *stack);
657 JIM_EXPORT void * Jim_StackPeek(Jim_Stack *stack);
658 JIM_EXPORT void Jim_FreeStackElements(Jim_Stack *stack, void (*freeFunc)(void *ptr));
660 /* hash table */
661 JIM_EXPORT int Jim_InitHashTable (Jim_HashTable *ht,
662 const Jim_HashTableType *type, void *privdata);
663 JIM_EXPORT void Jim_ExpandHashTable (Jim_HashTable *ht,
664 unsigned int size);
665 JIM_EXPORT int Jim_AddHashEntry (Jim_HashTable *ht, const void *key,
666 void *val);
667 JIM_EXPORT int Jim_ReplaceHashEntry (Jim_HashTable *ht,
668 const void *key, void *val);
669 JIM_EXPORT int Jim_DeleteHashEntry (Jim_HashTable *ht,
670 const void *key);
671 JIM_EXPORT int Jim_FreeHashTable (Jim_HashTable *ht);
672 JIM_EXPORT Jim_HashEntry * Jim_FindHashEntry (Jim_HashTable *ht,
673 const void *key);
674 JIM_EXPORT void Jim_ResizeHashTable (Jim_HashTable *ht);
675 JIM_EXPORT Jim_HashTableIterator *Jim_GetHashTableIterator
676 (Jim_HashTable *ht);
677 JIM_EXPORT Jim_HashEntry * Jim_NextHashEntry
678 (Jim_HashTableIterator *iter);
680 /* objects */
681 JIM_EXPORT Jim_Obj * Jim_NewObj (Jim_Interp *interp);
682 JIM_EXPORT void Jim_FreeObj (Jim_Interp *interp, Jim_Obj *objPtr);
683 JIM_EXPORT void Jim_InvalidateStringRep (Jim_Obj *objPtr);
684 JIM_EXPORT void Jim_InitStringRep (Jim_Obj *objPtr, const char *bytes,
685 int length);
686 JIM_EXPORT Jim_Obj * Jim_DuplicateObj (Jim_Interp *interp,
687 Jim_Obj *objPtr);
688 JIM_EXPORT const char * Jim_GetString(Jim_Obj *objPtr,
689 int *lenPtr);
690 JIM_EXPORT const char *Jim_String(Jim_Obj *objPtr);
691 JIM_EXPORT int Jim_Length(Jim_Obj *objPtr);
693 /* string object */
694 JIM_EXPORT Jim_Obj * Jim_NewStringObj (Jim_Interp *interp,
695 const char *s, int len);
696 JIM_EXPORT Jim_Obj *Jim_NewStringObjUtf8(Jim_Interp *interp,
697 const char *s, int charlen);
698 JIM_EXPORT Jim_Obj * Jim_NewStringObjNoAlloc (Jim_Interp *interp,
699 char *s, int len);
700 JIM_EXPORT void Jim_AppendString (Jim_Interp *interp, Jim_Obj *objPtr,
701 const char *str, int len);
702 JIM_EXPORT void Jim_AppendObj (Jim_Interp *interp, Jim_Obj *objPtr,
703 Jim_Obj *appendObjPtr);
704 JIM_EXPORT void Jim_AppendStrings (Jim_Interp *interp,
705 Jim_Obj *objPtr, ...);
706 JIM_EXPORT int Jim_StringEqObj(Jim_Obj *aObjPtr, Jim_Obj *bObjPtr);
707 JIM_EXPORT int Jim_StringMatchObj (Jim_Interp *interp, Jim_Obj *patternObjPtr,
708 Jim_Obj *objPtr, int nocase);
709 JIM_EXPORT Jim_Obj * Jim_StringRangeObj (Jim_Interp *interp,
710 Jim_Obj *strObjPtr, Jim_Obj *firstObjPtr,
711 Jim_Obj *lastObjPtr);
712 JIM_EXPORT Jim_Obj * Jim_FormatString (Jim_Interp *interp,
713 Jim_Obj *fmtObjPtr, int objc, Jim_Obj *const *objv);
714 JIM_EXPORT Jim_Obj * Jim_ScanString (Jim_Interp *interp, Jim_Obj *strObjPtr,
715 Jim_Obj *fmtObjPtr, int flags);
716 JIM_EXPORT int Jim_CompareStringImmediate (Jim_Interp *interp,
717 Jim_Obj *objPtr, const char *str);
718 JIM_EXPORT int Jim_StringCompareObj(Jim_Interp *interp, Jim_Obj *firstObjPtr,
719 Jim_Obj *secondObjPtr, int nocase);
720 JIM_EXPORT int Jim_StringCompareLenObj(Jim_Interp *interp, Jim_Obj *firstObjPtr,
721 Jim_Obj *secondObjPtr, int nocase);
722 JIM_EXPORT int Jim_Utf8Length(Jim_Interp *interp, Jim_Obj *objPtr);
724 /* reference object */
725 JIM_EXPORT Jim_Obj * Jim_NewReference (Jim_Interp *interp,
726 Jim_Obj *objPtr, Jim_Obj *tagPtr, Jim_Obj *cmdNamePtr);
727 JIM_EXPORT Jim_Reference * Jim_GetReference (Jim_Interp *interp,
728 Jim_Obj *objPtr);
729 JIM_EXPORT int Jim_SetFinalizer (Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *cmdNamePtr);
730 JIM_EXPORT int Jim_GetFinalizer (Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj **cmdNamePtrPtr);
732 /* interpreter */
733 JIM_EXPORT Jim_Interp * Jim_CreateInterp (void);
734 JIM_EXPORT void Jim_FreeInterp (Jim_Interp *i);
735 JIM_EXPORT int Jim_GetExitCode (Jim_Interp *interp);
736 JIM_EXPORT const char *Jim_ReturnCode(int code);
737 JIM_EXPORT void Jim_SetResultFormatted(Jim_Interp *interp, const char *format, ...);
739 /* commands */
740 JIM_EXPORT void Jim_RegisterCoreCommands (Jim_Interp *interp);
741 JIM_EXPORT int Jim_CreateCommand (Jim_Interp *interp,
742 const char *cmdName, Jim_CmdProc cmdProc, void *privData,
743 Jim_DelCmdProc delProc);
744 JIM_EXPORT int Jim_DeleteCommand (Jim_Interp *interp,
745 const char *cmdName);
746 JIM_EXPORT int Jim_RenameCommand (Jim_Interp *interp,
747 const char *oldName, const char *newName);
748 JIM_EXPORT Jim_Cmd * Jim_GetCommand (Jim_Interp *interp,
749 Jim_Obj *objPtr, int flags);
750 JIM_EXPORT int Jim_SetVariable (Jim_Interp *interp,
751 Jim_Obj *nameObjPtr, Jim_Obj *valObjPtr);
752 JIM_EXPORT int Jim_SetVariableStr (Jim_Interp *interp,
753 const char *name, Jim_Obj *objPtr);
754 JIM_EXPORT int Jim_SetGlobalVariableStr (Jim_Interp *interp,
755 const char *name, Jim_Obj *objPtr);
756 JIM_EXPORT int Jim_SetVariableStrWithStr (Jim_Interp *interp,
757 const char *name, const char *val);
758 JIM_EXPORT int Jim_SetVariableLink (Jim_Interp *interp,
759 Jim_Obj *nameObjPtr, Jim_Obj *targetNameObjPtr,
760 Jim_CallFrame *targetCallFrame);
761 JIM_EXPORT Jim_Obj * Jim_GetVariable (Jim_Interp *interp,
762 Jim_Obj *nameObjPtr, int flags);
763 JIM_EXPORT Jim_Obj * Jim_GetGlobalVariable (Jim_Interp *interp,
764 Jim_Obj *nameObjPtr, int flags);
765 JIM_EXPORT Jim_Obj * Jim_GetVariableStr (Jim_Interp *interp,
766 const char *name, int flags);
767 JIM_EXPORT Jim_Obj * Jim_GetGlobalVariableStr (Jim_Interp *interp,
768 const char *name, int flags);
769 JIM_EXPORT int Jim_UnsetVariable (Jim_Interp *interp,
770 Jim_Obj *nameObjPtr, int flags);
772 /* call frame */
773 JIM_EXPORT Jim_CallFrame *Jim_GetCallFrameByLevel(Jim_Interp *interp,
774 Jim_Obj *levelObjPtr);
776 /* garbage collection */
777 JIM_EXPORT int Jim_Collect (Jim_Interp *interp);
778 JIM_EXPORT void Jim_CollectIfNeeded (Jim_Interp *interp);
780 /* index object */
781 JIM_EXPORT int Jim_GetIndex (Jim_Interp *interp, Jim_Obj *objPtr,
782 int *indexPtr);
784 /* list object */
785 JIM_EXPORT Jim_Obj * Jim_NewListObj (Jim_Interp *interp,
786 Jim_Obj *const *elements, int len);
787 JIM_EXPORT void Jim_ListInsertElements (Jim_Interp *interp,
788 Jim_Obj *listPtr, int listindex, int objc, Jim_Obj *const *objVec);
789 JIM_EXPORT void Jim_ListAppendElement (Jim_Interp *interp,
790 Jim_Obj *listPtr, Jim_Obj *objPtr);
791 JIM_EXPORT void Jim_ListAppendList (Jim_Interp *interp,
792 Jim_Obj *listPtr, Jim_Obj *appendListPtr);
793 JIM_EXPORT int Jim_ListLength (Jim_Interp *interp, Jim_Obj *objPtr);
794 JIM_EXPORT int Jim_ListIndex (Jim_Interp *interp, Jim_Obj *listPrt,
795 int listindex, Jim_Obj **objPtrPtr, int seterr);
796 JIM_EXPORT Jim_Obj *Jim_ListGetIndex(Jim_Interp *interp, Jim_Obj *listPtr, int idx);
797 JIM_EXPORT int Jim_SetListIndex (Jim_Interp *interp,
798 Jim_Obj *varNamePtr, Jim_Obj *const *indexv, int indexc,
799 Jim_Obj *newObjPtr);
800 JIM_EXPORT Jim_Obj * Jim_ConcatObj (Jim_Interp *interp, int objc,
801 Jim_Obj *const *objv);
802 JIM_EXPORT Jim_Obj *Jim_ListJoin(Jim_Interp *interp,
803 Jim_Obj *listObjPtr, const char *joinStr, int joinStrLen);
805 /* dict object */
806 JIM_EXPORT Jim_Obj * Jim_NewDictObj (Jim_Interp *interp,
807 Jim_Obj *const *elements, int len);
808 JIM_EXPORT int Jim_DictKey (Jim_Interp *interp, Jim_Obj *dictPtr,
809 Jim_Obj *keyPtr, Jim_Obj **objPtrPtr, int flags);
810 JIM_EXPORT int Jim_DictKeysVector (Jim_Interp *interp,
811 Jim_Obj *dictPtr, Jim_Obj *const *keyv, int keyc,
812 Jim_Obj **objPtrPtr, int flags);
813 JIM_EXPORT int Jim_SetDictKeysVector (Jim_Interp *interp,
814 Jim_Obj *varNamePtr, Jim_Obj *const *keyv, int keyc,
815 Jim_Obj *newObjPtr, int flags);
816 JIM_EXPORT int Jim_DictPairs(Jim_Interp *interp,
817 Jim_Obj *dictPtr, Jim_Obj ***objPtrPtr, int *len);
818 JIM_EXPORT int Jim_DictAddElement(Jim_Interp *interp, Jim_Obj *objPtr,
819 Jim_Obj *keyObjPtr, Jim_Obj *valueObjPtr);
820 JIM_EXPORT int Jim_DictKeys(Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *patternObj);
821 JIM_EXPORT int Jim_DictValues(Jim_Interp *interp, Jim_Obj *dictObjPtr, Jim_Obj *patternObjPtr);
822 JIM_EXPORT int Jim_DictSize(Jim_Interp *interp, Jim_Obj *objPtr);
824 /* return code object */
825 JIM_EXPORT int Jim_GetReturnCode (Jim_Interp *interp, Jim_Obj *objPtr,
826 int *intPtr);
828 /* expression object */
829 JIM_EXPORT int Jim_EvalExpression (Jim_Interp *interp,
830 Jim_Obj *exprObjPtr, Jim_Obj **exprResultPtrPtr);
831 JIM_EXPORT int Jim_GetBoolFromExpr (Jim_Interp *interp,
832 Jim_Obj *exprObjPtr, int *boolPtr);
834 /* integer object */
835 JIM_EXPORT int Jim_GetWide (Jim_Interp *interp, Jim_Obj *objPtr,
836 jim_wide *widePtr);
837 JIM_EXPORT int Jim_GetLong (Jim_Interp *interp, Jim_Obj *objPtr,
838 long *longPtr);
839 #define Jim_NewWideObj Jim_NewIntObj
840 JIM_EXPORT Jim_Obj * Jim_NewIntObj (Jim_Interp *interp,
841 jim_wide wideValue);
843 /* double object */
844 JIM_EXPORT int Jim_GetDouble(Jim_Interp *interp, Jim_Obj *objPtr,
845 double *doublePtr);
846 JIM_EXPORT void Jim_SetDouble(Jim_Interp *interp, Jim_Obj *objPtr,
847 double doubleValue);
848 JIM_EXPORT Jim_Obj * Jim_NewDoubleObj(Jim_Interp *interp, double doubleValue);
850 /* shared strings */
851 JIM_EXPORT const char * Jim_GetSharedString (Jim_Interp *interp,
852 const char *str);
853 JIM_EXPORT void Jim_ReleaseSharedString (Jim_Interp *interp,
854 const char *str);
856 /* commands utilities */
857 JIM_EXPORT void Jim_WrongNumArgs (Jim_Interp *interp, int argc,
858 Jim_Obj *const *argv, const char *msg);
859 JIM_EXPORT int Jim_GetEnum (Jim_Interp *interp, Jim_Obj *objPtr,
860 const char * const *tablePtr, int *indexPtr, const char *name, int flags);
861 JIM_EXPORT int Jim_ScriptIsComplete (const char *s, int len,
862 char *stateCharPtr);
864 * Find a matching name in the array of the given length.
866 * NULL entries are ignored.
868 * Returns the matching index if found, or -1 if not.
870 JIM_EXPORT int Jim_FindByName(const char *name, const char * const array[], size_t len);
872 /* package utilities */
873 typedef void (Jim_InterpDeleteProc)(Jim_Interp *interp, void *data);
874 JIM_EXPORT void * Jim_GetAssocData(Jim_Interp *interp, const char *key);
875 JIM_EXPORT int Jim_SetAssocData(Jim_Interp *interp, const char *key,
876 Jim_InterpDeleteProc *delProc, void *data);
877 JIM_EXPORT int Jim_DeleteAssocData(Jim_Interp *interp, const char *key);
879 /* Packages C API */
880 /* jim-package.c */
881 JIM_EXPORT int Jim_PackageProvide (Jim_Interp *interp,
882 const char *name, const char *ver, int flags);
883 JIM_EXPORT int Jim_PackageRequire (Jim_Interp *interp,
884 const char *name, int flags);
886 /* error messages */
887 JIM_EXPORT void Jim_MakeErrorMessage (Jim_Interp *interp);
889 /* interactive mode */
890 JIM_EXPORT int Jim_InteractivePrompt (Jim_Interp *interp);
891 JIM_EXPORT void Jim_HistoryLoad(const char *filename);
892 JIM_EXPORT void Jim_HistorySave(const char *filename);
893 JIM_EXPORT char *Jim_HistoryGetline(const char *prompt);
894 JIM_EXPORT void Jim_HistoryAdd(const char *line);
895 JIM_EXPORT void Jim_HistoryShow(void);
897 /* Misc */
898 JIM_EXPORT int Jim_InitStaticExtensions(Jim_Interp *interp);
899 JIM_EXPORT int Jim_StringToWide(const char *str, jim_wide *widePtr, int base);
901 /* jim-load.c */
902 JIM_EXPORT int Jim_LoadLibrary(Jim_Interp *interp, const char *pathName);
903 JIM_EXPORT void Jim_FreeLoadHandles(Jim_Interp *interp);
905 /* jim-aio.c */
906 JIM_EXPORT FILE *Jim_AioFilehandle(Jim_Interp *interp, Jim_Obj *command);
909 /* type inspection - avoid where possible */
910 JIM_EXPORT int Jim_IsDict(Jim_Obj *objPtr);
911 JIM_EXPORT int Jim_IsList(Jim_Obj *objPtr);
913 #ifdef __cplusplus
915 #endif
917 #endif /* __JIM__H */
920 * Local Variables: ***
921 * c-basic-offset: 4 ***
922 * tab-width: 4 ***
923 * End: ***