auto.def: tclprefix should not be enabled by default
[jimtcl.git] / jim.h
blob65bf7cc2cdb347fc64568a0365bf8ca9563d0d14
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 benefit 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 #define JIM_OK 0
129 #define JIM_ERR 1
130 #define JIM_RETURN 2
131 #define JIM_BREAK 3
132 #define JIM_CONTINUE 4
133 #define JIM_SIGNAL 5
134 #define JIM_EXIT 6
135 /* The following are internal codes and should never been seen/used */
136 #define JIM_EVAL 7
138 #define JIM_MAX_CALLFRAME_DEPTH 1000 /* default max nesting depth for procs */
139 #define JIM_MAX_EVAL_DEPTH 2000 /* default max nesting depth for eval */
141 /* Some function get an integer argument with flags to change
142 * the behaviour. */
144 /* Starting from 1 << 20 flags are reserved for private uses of
145 * different calls. This way the same 'flags' argument may be used
146 * to pass both global flags and private flags. */
147 #define JIM_PRIV_FLAG_SHIFT 20
149 #define JIM_NONE 0 /* no flags set */
150 #define JIM_ERRMSG 1 /* set an error message in the interpreter. */
151 #define JIM_ENUM_ABBREV 2 /* Jim_GetEnum() - Allow unambiguous abbreviation */
152 #define JIM_UNSHARED 4 /* Jim_GetVariable() - return unshared object */
153 #define JIM_MUSTEXIST 8 /* Jim_SetDictKeysVector() - fail if non-existent */
155 /* Flags for Jim_SubstObj() */
156 #define JIM_SUBST_NOVAR 1 /* don't perform variables substitutions */
157 #define JIM_SUBST_NOCMD 2 /* don't perform command substitutions */
158 #define JIM_SUBST_NOESC 4 /* don't perform escapes substitutions */
159 #define JIM_SUBST_FLAG 128 /* flag to indicate that this is a real substitution object */
161 /* Flags used by API calls getting a 'nocase' argument. */
162 #define JIM_CASESENS 0 /* case sensitive */
163 #define JIM_NOCASE 1 /* no case */
165 /* Filesystem related */
166 #define JIM_PATH_LEN 1024
168 /* Unused arguments generate annoying warnings... */
169 #define JIM_NOTUSED(V) ((void) V)
171 #define JIM_LIBPATH "auto_path"
172 #define JIM_INTERACTIVE "tcl_interactive"
174 /* -----------------------------------------------------------------------------
175 * Stack
176 * ---------------------------------------------------------------------------*/
178 typedef struct Jim_Stack {
179 int len;
180 int maxlen;
181 void **vector;
182 } Jim_Stack;
184 /* -----------------------------------------------------------------------------
185 * Hash table
186 * ---------------------------------------------------------------------------*/
188 typedef struct Jim_HashEntry {
189 void *key;
190 union {
191 void *val;
192 int intval;
193 } u;
194 struct Jim_HashEntry *next;
195 } Jim_HashEntry;
197 typedef struct Jim_HashTableType {
198 unsigned int (*hashFunction)(const void *key);
199 void *(*keyDup)(void *privdata, const void *key);
200 void *(*valDup)(void *privdata, const void *obj);
201 int (*keyCompare)(void *privdata, const void *key1, const void *key2);
202 void (*keyDestructor)(void *privdata, void *key);
203 void (*valDestructor)(void *privdata, void *obj);
204 } Jim_HashTableType;
206 typedef struct Jim_HashTable {
207 Jim_HashEntry **table;
208 const Jim_HashTableType *type;
209 void *privdata;
210 unsigned int size;
211 unsigned int sizemask;
212 unsigned int used;
213 unsigned int collisions;
214 unsigned int uniq;
215 } Jim_HashTable;
217 typedef struct Jim_HashTableIterator {
218 Jim_HashTable *ht;
219 Jim_HashEntry *entry, *nextEntry;
220 int index;
221 } Jim_HashTableIterator;
223 /* This is the initial size of every hash table */
224 #define JIM_HT_INITIAL_SIZE 16
226 /* ------------------------------- Macros ------------------------------------*/
227 #define Jim_FreeEntryVal(ht, entry) \
228 if ((ht)->type->valDestructor) \
229 (ht)->type->valDestructor((ht)->privdata, (entry)->u.val)
231 #define Jim_SetHashVal(ht, entry, _val_) do { \
232 if ((ht)->type->valDup) \
233 (entry)->u.val = (ht)->type->valDup((ht)->privdata, (_val_)); \
234 else \
235 (entry)->u.val = (_val_); \
236 } while(0)
238 #define Jim_FreeEntryKey(ht, entry) \
239 if ((ht)->type->keyDestructor) \
240 (ht)->type->keyDestructor((ht)->privdata, (entry)->key)
242 #define Jim_SetHashKey(ht, entry, _key_) do { \
243 if ((ht)->type->keyDup) \
244 (entry)->key = (ht)->type->keyDup((ht)->privdata, (_key_)); \
245 else \
246 (entry)->key = (void *)(_key_); \
247 } while(0)
249 #define Jim_CompareHashKeys(ht, key1, key2) \
250 (((ht)->type->keyCompare) ? \
251 (ht)->type->keyCompare((ht)->privdata, (key1), (key2)) : \
252 (key1) == (key2))
254 #define Jim_HashKey(ht, key) ((ht)->type->hashFunction(key) + (ht)->uniq)
256 #define Jim_GetHashEntryKey(he) ((he)->key)
257 #define Jim_GetHashEntryVal(he) ((he)->u.val)
258 #define Jim_GetHashTableCollisions(ht) ((ht)->collisions)
259 #define Jim_GetHashTableSize(ht) ((ht)->size)
260 #define Jim_GetHashTableUsed(ht) ((ht)->used)
262 /* -----------------------------------------------------------------------------
263 * Jim_Obj structure
264 * ---------------------------------------------------------------------------*/
266 /* -----------------------------------------------------------------------------
267 * Jim object. This is mostly the same as Tcl_Obj itself,
268 * with the addition of the 'prev' and 'next' pointers.
269 * In Jim all the objects are stored into a linked list for GC purposes,
270 * so that it's possible to access every object living in a given interpreter
271 * sequentially. When an object is freed, it's moved into a different
272 * linked list, used as object pool.
274 * The refcount of a freed object is always -1.
275 * ---------------------------------------------------------------------------*/
276 typedef struct Jim_Obj {
277 char *bytes; /* string representation buffer. NULL = no string repr. */
278 const struct Jim_ObjType *typePtr; /* object type. */
279 int refCount; /* reference count */
280 int length; /* number of bytes in 'bytes', not including the null term. */
281 /* Internal representation union */
282 union {
283 /* integer number type */
284 jim_wide wideValue;
285 /* generic integer value (e.g. index, return code) */
286 int intValue;
287 /* double number type */
288 double doubleValue;
289 /* Generic pointer */
290 void *ptr;
291 /* Generic two pointers value */
292 struct {
293 void *ptr1;
294 void *ptr2;
295 } twoPtrValue;
296 /* Generic pointer, int, int value */
297 struct {
298 void *ptr;
299 int int1;
300 int int2;
301 } ptrIntValue;
302 /* Variable object */
303 struct {
304 struct Jim_Var *varPtr;
305 unsigned long callFrameId; /* for caching */
306 int global; /* If the variable name is globally scoped with :: */
307 } varValue;
308 /* Command object */
309 struct {
310 struct Jim_Obj *nsObj;
311 struct Jim_Cmd *cmdPtr;
312 unsigned long procEpoch; /* for caching */
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 struct {
341 int line;
342 int argc;
343 } scriptLineValue;
344 } internalRep;
345 /* These fields add 8 or 16 bytes more for every object
346 * but this is required for efficient garbage collection
347 * of Jim references. */
348 struct Jim_Obj *prevObjPtr; /* pointer to the prev object. */
349 struct Jim_Obj *nextObjPtr; /* pointer to the next object. */
350 } Jim_Obj;
352 /* Jim_Obj related macros */
353 #define Jim_IncrRefCount(objPtr) \
354 ++(objPtr)->refCount
355 #define Jim_DecrRefCount(interp, objPtr) \
356 if (--(objPtr)->refCount <= 0) Jim_FreeObj(interp, objPtr)
357 #define Jim_IsShared(objPtr) \
358 ((objPtr)->refCount > 1)
360 /* This macro is used when we allocate a new object using
361 * Jim_New...Obj(), but for some error we need to destroy it.
362 * Instead to use Jim_IncrRefCount() + Jim_DecrRefCount() we
363 * can just call Jim_FreeNewObj. To call Jim_Free directly
364 * seems too raw, the object handling may change and we want
365 * that Jim_FreeNewObj() can be called only against objects
366 * that are believed to have refcount == 0. */
367 #define Jim_FreeNewObj Jim_FreeObj
369 /* Free the internal representation of the object. */
370 #define Jim_FreeIntRep(i,o) \
371 if ((o)->typePtr && (o)->typePtr->freeIntRepProc) \
372 (o)->typePtr->freeIntRepProc(i, o)
374 /* Get the internal representation pointer */
375 #define Jim_GetIntRepPtr(o) (o)->internalRep.ptr
377 /* Set the internal representation pointer */
378 #define Jim_SetIntRepPtr(o, p) \
379 (o)->internalRep.ptr = (p)
381 /* The object type structure.
382 * There are three methods.
384 * - freeIntRepProc is used to free the internal representation of the object.
385 * Can be NULL if there is nothing to free.
387 * - dupIntRepProc is used to duplicate the internal representation of the object.
388 * If NULL, when an object is duplicated, the internalRep union is
389 * directly copied from an object to another.
390 * Note that it's up to the caller to free the old internal repr of the
391 * object before to call the Dup method.
393 * - updateStringProc is used to create the string from the internal repr.
396 struct Jim_Interp;
398 typedef void (Jim_FreeInternalRepProc)(struct Jim_Interp *interp,
399 struct Jim_Obj *objPtr);
400 typedef void (Jim_DupInternalRepProc)(struct Jim_Interp *interp,
401 struct Jim_Obj *srcPtr, Jim_Obj *dupPtr);
402 typedef void (Jim_UpdateStringProc)(struct Jim_Obj *objPtr);
404 typedef struct Jim_ObjType {
405 const char *name; /* The name of the type. */
406 Jim_FreeInternalRepProc *freeIntRepProc;
407 Jim_DupInternalRepProc *dupIntRepProc;
408 Jim_UpdateStringProc *updateStringProc;
409 int flags;
410 } Jim_ObjType;
412 /* Jim_ObjType flags */
413 #define JIM_TYPE_NONE 0 /* No flags */
414 #define JIM_TYPE_REFERENCES 1 /* The object may contain references. */
416 /* -----------------------------------------------------------------------------
417 * Call frame, vars, commands structures
418 * ---------------------------------------------------------------------------*/
420 /* Call frame */
421 typedef struct Jim_CallFrame {
422 unsigned long id; /* Call Frame ID. Used for caching. */
423 int level; /* Level of this call frame. 0 = global */
424 struct Jim_HashTable vars; /* Where local vars are stored */
425 struct Jim_HashTable *staticVars; /* pointer to procedure static vars */
426 struct Jim_CallFrame *parent; /* The parent callframe */
427 Jim_Obj *const *argv; /* object vector of the current procedure call. */
428 int argc; /* number of args of the current procedure call. */
429 Jim_Obj *procArgsObjPtr; /* arglist object of the running procedure */
430 Jim_Obj *procBodyObjPtr; /* body object of the running procedure */
431 struct Jim_CallFrame *next; /* Callframes are in a linked list */
432 Jim_Obj *nsObj; /* Namespace for this proc call frame */
433 Jim_Obj *fileNameObj; /* file and line of caller of this proc (if available) */
434 int line;
435 Jim_Stack *localCommands; /* commands to be destroyed when the call frame is destroyed */
436 struct Jim_Obj *tailcallObj; /* Pending tailcall invocation */
437 struct Jim_Cmd *tailcallCmd; /* Resolved command for pending tailcall invocation */
438 } Jim_CallFrame;
440 /* The var structure. It just holds the pointer of the referenced
441 * object. If linkFramePtr is not NULL the variable is a link
442 * to a variable of name stored in objPtr living in the given callframe
443 * (this happens when the [global] or [upvar] command is used).
444 * The interp in order to always know how to free the Jim_Obj associated
445 * with a given variable because in Jim objects memory management is
446 * bound to interpreters. */
447 typedef struct Jim_Var {
448 Jim_Obj *objPtr;
449 struct Jim_CallFrame *linkFramePtr;
450 } Jim_Var;
452 /* The cmd structure. */
453 typedef int Jim_CmdProc(struct Jim_Interp *interp, int argc,
454 Jim_Obj *const *argv);
455 typedef void Jim_DelCmdProc(struct Jim_Interp *interp, void *privData);
459 /* A command is implemented in C if isproc is 0, otherwise
460 * it is a Tcl procedure with the arglist and body represented by the
461 * two objects referenced by arglistObjPtr and bodyObjPtr. */
462 typedef struct Jim_Cmd {
463 int inUse; /* Reference count */
464 int isproc; /* Is this a procedure? */
465 struct Jim_Cmd *prevCmd; /* Previous command defn if cmd created 'local' */
466 union {
467 struct {
468 /* native (C) command */
469 Jim_CmdProc *cmdProc; /* The command implementation */
470 Jim_DelCmdProc *delProc; /* Called when the command is deleted if != NULL */
471 void *privData; /* command-private data available via Jim_CmdPrivData() */
472 } native;
473 struct {
474 /* Tcl procedure */
475 Jim_Obj *argListObjPtr;
476 Jim_Obj *bodyObjPtr;
477 Jim_HashTable *staticVars; /* Static vars hash table. NULL if no statics. */
478 int argListLen; /* Length of argListObjPtr */
479 int reqArity; /* Number of required parameters */
480 int optArity; /* Number of optional parameters */
481 int argsPos; /* Position of 'args', if specified, or -1 */
482 int upcall; /* True if proc is currently in upcall */
483 struct Jim_ProcArg {
484 Jim_Obj *nameObjPtr; /* Name of this arg */
485 Jim_Obj *defaultObjPtr; /* Default value, (or rename for $args) */
486 } *arglist;
487 Jim_Obj *nsObj; /* Namespace for this proc */
488 } proc;
489 } u;
490 } Jim_Cmd;
492 /* Pseudo Random Number Generator State structure */
493 typedef struct Jim_PrngState {
494 unsigned char sbox[256];
495 unsigned int i, j;
496 } Jim_PrngState;
498 /* -----------------------------------------------------------------------------
499 * Jim interpreter structure.
500 * Fields similar to the real Tcl interpreter structure have the same names.
501 * ---------------------------------------------------------------------------*/
502 typedef struct Jim_Interp {
503 Jim_Obj *result; /* object returned by the last command called. */
504 int errorLine; /* Error line where an error occurred. */
505 Jim_Obj *errorFileNameObj; /* Error file where an error occurred. */
506 int addStackTrace; /* > 0 if a level should be added to the stack trace */
507 int maxCallFrameDepth; /* Used for infinite loop detection. */
508 int maxEvalDepth; /* Used for infinite loop detection. */
509 int evalDepth; /* Current eval depth */
510 int returnCode; /* Completion code to return on JIM_RETURN. */
511 int returnLevel; /* Current level of 'return -level' */
512 int exitCode; /* Code to return to the OS on JIM_EXIT. */
513 long id; /* Hold unique id for various purposes */
514 int signal_level; /* A nesting level of catch -signal */
515 jim_wide sigmask; /* Bit mask of caught signals, or 0 if none */
516 int (*signal_set_result)(struct Jim_Interp *interp, jim_wide sigmask); /* Set a result for the sigmask */
517 Jim_CallFrame *framePtr; /* Pointer to the current call frame */
518 Jim_CallFrame *topFramePtr; /* toplevel/global frame pointer. */
519 struct Jim_HashTable commands; /* Commands hash table */
520 unsigned long procEpoch; /* Incremented every time the result
521 of procedures names lookup caching
522 may no longer be valid. */
523 unsigned long callFrameEpoch; /* Incremented every time a new
524 callframe is created. This id is used for the
525 'ID' field contained in the Jim_CallFrame
526 structure. */
527 int local; /* If 'local' is in effect, newly defined procs keep a reference to the old defn */
528 Jim_Obj *liveList; /* Linked list of all the live objects. */
529 Jim_Obj *freeList; /* Linked list of all the unused objects. */
530 Jim_Obj *currentScriptObj; /* Script currently in execution. */
531 Jim_Obj *nullScriptObj; /* script representation of an empty string */
532 Jim_Obj *emptyObj; /* Shared empty string object. */
533 Jim_Obj *trueObj; /* Shared true int object. */
534 Jim_Obj *falseObj; /* Shared false int object. */
535 unsigned long referenceNextId; /* Next id for reference. */
536 struct Jim_HashTable references; /* References hash table. */
537 unsigned long lastCollectId; /* reference max Id of the last GC
538 execution. It's set to -1 while the collection
539 is running as sentinel to avoid to recursive
540 calls via the [collect] command inside
541 finalizers. */
542 time_t lastCollectTime; /* unix time of the last GC execution */
543 Jim_Obj *stackTrace; /* Stack trace object. */
544 Jim_Obj *errorProc; /* Name of last procedure which returned an error */
545 Jim_Obj *unknown; /* Unknown command cache */
546 int unknown_called; /* The unknown command has been invoked */
547 int errorFlag; /* Set if an error occurred during execution. */
548 void *cmdPrivData; /* Used to pass the private data pointer to
549 a command. It is set to what the user specified
550 via Jim_CreateCommand(). */
552 struct Jim_CallFrame *freeFramesList; /* list of CallFrame structures. */
553 struct Jim_HashTable assocData; /* per-interp storage for use by packages */
554 Jim_PrngState *prngState; /* per interpreter Random Number Gen. state. */
555 struct Jim_HashTable packages; /* Provided packages hash table */
556 Jim_Stack *loadHandles; /* handles of loaded modules [load] */
557 } Jim_Interp;
559 /* Currently provided as macro that performs the increment.
560 * At some point may be a real function doing more work.
561 * The proc epoch is used in order to know when a command lookup
562 * cached can no longer considered valid. */
563 #define Jim_InterpIncrProcEpoch(i) (i)->procEpoch++
564 #define Jim_SetResultString(i,s,l) Jim_SetResult(i, Jim_NewStringObj(i,s,l))
565 #define Jim_SetResultInt(i,intval) Jim_SetResult(i, Jim_NewIntObj(i,intval))
566 /* Note: Using trueObj and falseObj here makes some things slower...*/
567 #define Jim_SetResultBool(i,b) Jim_SetResultInt(i, b)
568 #define Jim_SetEmptyResult(i) Jim_SetResult(i, (i)->emptyObj)
569 #define Jim_GetResult(i) ((i)->result)
570 #define Jim_CmdPrivData(i) ((i)->cmdPrivData)
572 /* Note that 'o' is expanded only one time inside this macro,
573 * so it's safe to use side effects. */
574 #define Jim_SetResult(i,o) do { \
575 Jim_Obj *_resultObjPtr_ = (o); \
576 Jim_IncrRefCount(_resultObjPtr_); \
577 Jim_DecrRefCount(i,(i)->result); \
578 (i)->result = _resultObjPtr_; \
579 } while(0)
581 /* Use this for filehandles, etc. which need a unique id */
582 #define Jim_GetId(i) (++(i)->id)
584 /* Reference structure. The interpreter pointer is held within privdata member in HashTable */
585 #define JIM_REFERENCE_TAGLEN 7 /* The tag is fixed-length, because the reference
586 string representation must be fixed length. */
587 typedef struct Jim_Reference {
588 Jim_Obj *objPtr;
589 Jim_Obj *finalizerCmdNamePtr;
590 char tag[JIM_REFERENCE_TAGLEN+1];
591 } Jim_Reference;
593 /* -----------------------------------------------------------------------------
594 * Exported API prototypes.
595 * ---------------------------------------------------------------------------*/
597 #define Jim_NewEmptyStringObj(i) Jim_NewStringObj(i, "", 0)
598 #define Jim_FreeHashTableIterator(iter) Jim_Free(iter)
600 #define JIM_EXPORT
602 /* Memory allocation */
603 JIM_EXPORT void *Jim_Alloc (int size);
604 JIM_EXPORT void *Jim_Realloc(void *ptr, int size);
605 JIM_EXPORT void Jim_Free (void *ptr);
606 JIM_EXPORT char * Jim_StrDup (const char *s);
607 JIM_EXPORT char *Jim_StrDupLen(const char *s, int l);
609 /* environment */
610 JIM_EXPORT char **Jim_GetEnviron(void);
611 JIM_EXPORT void Jim_SetEnviron(char **env);
612 JIM_EXPORT int Jim_MakeTempFile(Jim_Interp *interp, const char *filename_template);
614 /* evaluation */
615 JIM_EXPORT int Jim_Eval(Jim_Interp *interp, const char *script);
616 /* in C code, you can do this and get better error messages */
617 /* Jim_EvalSource( interp, __FILE__, __LINE__ , "some tcl commands"); */
618 JIM_EXPORT int Jim_EvalSource(Jim_Interp *interp, const char *filename, int lineno, const char *script);
619 /* Backwards compatibility */
620 #define Jim_Eval_Named(I, S, F, L) Jim_EvalSource((I), (F), (L), (S))
622 JIM_EXPORT int Jim_EvalGlobal(Jim_Interp *interp, const char *script);
623 JIM_EXPORT int Jim_EvalFile(Jim_Interp *interp, const char *filename);
624 JIM_EXPORT int Jim_EvalFileGlobal(Jim_Interp *interp, const char *filename);
625 JIM_EXPORT int Jim_EvalObj (Jim_Interp *interp, Jim_Obj *scriptObjPtr);
626 JIM_EXPORT int Jim_EvalObjVector (Jim_Interp *interp, int objc,
627 Jim_Obj *const *objv);
628 JIM_EXPORT int Jim_EvalObjList(Jim_Interp *interp, Jim_Obj *listObj);
629 JIM_EXPORT int Jim_EvalObjPrefix(Jim_Interp *interp, Jim_Obj *prefix,
630 int objc, Jim_Obj *const *objv);
631 #define Jim_EvalPrefix(i, p, oc, ov) Jim_EvalObjPrefix((i), Jim_NewStringObj((i), (p), -1), (oc), (ov))
632 JIM_EXPORT int Jim_EvalNamespace(Jim_Interp *interp, Jim_Obj *scriptObj, Jim_Obj *nsObj);
633 JIM_EXPORT int Jim_SubstObj (Jim_Interp *interp, Jim_Obj *substObjPtr,
634 Jim_Obj **resObjPtrPtr, int flags);
636 /* stack */
637 JIM_EXPORT void Jim_InitStack(Jim_Stack *stack);
638 JIM_EXPORT void Jim_FreeStack(Jim_Stack *stack);
639 JIM_EXPORT int Jim_StackLen(Jim_Stack *stack);
640 JIM_EXPORT void Jim_StackPush(Jim_Stack *stack, void *element);
641 JIM_EXPORT void * Jim_StackPop(Jim_Stack *stack);
642 JIM_EXPORT void * Jim_StackPeek(Jim_Stack *stack);
643 JIM_EXPORT void Jim_FreeStackElements(Jim_Stack *stack, void (*freeFunc)(void *ptr));
645 /* hash table */
646 JIM_EXPORT int Jim_InitHashTable (Jim_HashTable *ht,
647 const Jim_HashTableType *type, void *privdata);
648 JIM_EXPORT void Jim_ExpandHashTable (Jim_HashTable *ht,
649 unsigned int size);
650 JIM_EXPORT int Jim_AddHashEntry (Jim_HashTable *ht, const void *key,
651 void *val);
652 JIM_EXPORT int Jim_ReplaceHashEntry (Jim_HashTable *ht,
653 const void *key, void *val);
654 JIM_EXPORT int Jim_DeleteHashEntry (Jim_HashTable *ht,
655 const void *key);
656 JIM_EXPORT int Jim_FreeHashTable (Jim_HashTable *ht);
657 JIM_EXPORT Jim_HashEntry * Jim_FindHashEntry (Jim_HashTable *ht,
658 const void *key);
659 JIM_EXPORT void Jim_ResizeHashTable (Jim_HashTable *ht);
660 JIM_EXPORT Jim_HashTableIterator *Jim_GetHashTableIterator
661 (Jim_HashTable *ht);
662 JIM_EXPORT Jim_HashEntry * Jim_NextHashEntry
663 (Jim_HashTableIterator *iter);
665 /* objects */
666 JIM_EXPORT Jim_Obj * Jim_NewObj (Jim_Interp *interp);
667 JIM_EXPORT void Jim_FreeObj (Jim_Interp *interp, Jim_Obj *objPtr);
668 JIM_EXPORT void Jim_InvalidateStringRep (Jim_Obj *objPtr);
669 JIM_EXPORT Jim_Obj * Jim_DuplicateObj (Jim_Interp *interp,
670 Jim_Obj *objPtr);
671 JIM_EXPORT const char * Jim_GetString(Jim_Obj *objPtr,
672 int *lenPtr);
673 JIM_EXPORT const char *Jim_String(Jim_Obj *objPtr);
674 JIM_EXPORT int Jim_Length(Jim_Obj *objPtr);
676 /* string object */
677 JIM_EXPORT Jim_Obj * Jim_NewStringObj (Jim_Interp *interp,
678 const char *s, int len);
679 JIM_EXPORT Jim_Obj *Jim_NewStringObjUtf8(Jim_Interp *interp,
680 const char *s, int charlen);
681 JIM_EXPORT Jim_Obj * Jim_NewStringObjNoAlloc (Jim_Interp *interp,
682 char *s, int len);
683 JIM_EXPORT void Jim_AppendString (Jim_Interp *interp, Jim_Obj *objPtr,
684 const char *str, int len);
685 JIM_EXPORT void Jim_AppendObj (Jim_Interp *interp, Jim_Obj *objPtr,
686 Jim_Obj *appendObjPtr);
687 JIM_EXPORT void Jim_AppendStrings (Jim_Interp *interp,
688 Jim_Obj *objPtr, ...);
689 JIM_EXPORT int Jim_StringEqObj(Jim_Obj *aObjPtr, Jim_Obj *bObjPtr);
690 JIM_EXPORT int Jim_StringMatchObj (Jim_Interp *interp, Jim_Obj *patternObjPtr,
691 Jim_Obj *objPtr, int nocase);
692 JIM_EXPORT Jim_Obj * Jim_StringRangeObj (Jim_Interp *interp,
693 Jim_Obj *strObjPtr, Jim_Obj *firstObjPtr,
694 Jim_Obj *lastObjPtr);
695 JIM_EXPORT Jim_Obj * Jim_FormatString (Jim_Interp *interp,
696 Jim_Obj *fmtObjPtr, int objc, Jim_Obj *const *objv);
697 JIM_EXPORT Jim_Obj * Jim_ScanString (Jim_Interp *interp, Jim_Obj *strObjPtr,
698 Jim_Obj *fmtObjPtr, int flags);
699 JIM_EXPORT int Jim_CompareStringImmediate (Jim_Interp *interp,
700 Jim_Obj *objPtr, const char *str);
701 JIM_EXPORT int Jim_StringCompareObj(Jim_Interp *interp, Jim_Obj *firstObjPtr,
702 Jim_Obj *secondObjPtr, int nocase);
703 JIM_EXPORT int Jim_StringCompareLenObj(Jim_Interp *interp, Jim_Obj *firstObjPtr,
704 Jim_Obj *secondObjPtr, int nocase);
705 JIM_EXPORT int Jim_Utf8Length(Jim_Interp *interp, Jim_Obj *objPtr);
707 /* reference object */
708 JIM_EXPORT Jim_Obj * Jim_NewReference (Jim_Interp *interp,
709 Jim_Obj *objPtr, Jim_Obj *tagPtr, Jim_Obj *cmdNamePtr);
710 JIM_EXPORT Jim_Reference * Jim_GetReference (Jim_Interp *interp,
711 Jim_Obj *objPtr);
712 JIM_EXPORT int Jim_SetFinalizer (Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *cmdNamePtr);
713 JIM_EXPORT int Jim_GetFinalizer (Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj **cmdNamePtrPtr);
715 /* interpreter */
716 JIM_EXPORT Jim_Interp * Jim_CreateInterp (void);
717 JIM_EXPORT void Jim_FreeInterp (Jim_Interp *i);
718 JIM_EXPORT int Jim_GetExitCode (Jim_Interp *interp);
719 JIM_EXPORT const char *Jim_ReturnCode(int code);
720 JIM_EXPORT void Jim_SetResultFormatted(Jim_Interp *interp, const char *format, ...);
722 /* commands */
723 JIM_EXPORT void Jim_RegisterCoreCommands (Jim_Interp *interp);
724 JIM_EXPORT int Jim_CreateCommand (Jim_Interp *interp,
725 const char *cmdName, Jim_CmdProc *cmdProc, void *privData,
726 Jim_DelCmdProc *delProc);
727 JIM_EXPORT int Jim_DeleteCommand (Jim_Interp *interp,
728 const char *cmdName);
729 JIM_EXPORT int Jim_RenameCommand (Jim_Interp *interp,
730 const char *oldName, const char *newName);
731 JIM_EXPORT Jim_Cmd * Jim_GetCommand (Jim_Interp *interp,
732 Jim_Obj *objPtr, int flags);
733 JIM_EXPORT int Jim_SetVariable (Jim_Interp *interp,
734 Jim_Obj *nameObjPtr, Jim_Obj *valObjPtr);
735 JIM_EXPORT int Jim_SetVariableStr (Jim_Interp *interp,
736 const char *name, Jim_Obj *objPtr);
737 JIM_EXPORT int Jim_SetGlobalVariableStr (Jim_Interp *interp,
738 const char *name, Jim_Obj *objPtr);
739 JIM_EXPORT int Jim_SetVariableStrWithStr (Jim_Interp *interp,
740 const char *name, const char *val);
741 JIM_EXPORT int Jim_SetVariableLink (Jim_Interp *interp,
742 Jim_Obj *nameObjPtr, Jim_Obj *targetNameObjPtr,
743 Jim_CallFrame *targetCallFrame);
744 JIM_EXPORT Jim_Obj * Jim_MakeGlobalNamespaceName(Jim_Interp *interp,
745 Jim_Obj *nameObjPtr);
746 JIM_EXPORT Jim_Obj * Jim_GetVariable (Jim_Interp *interp,
747 Jim_Obj *nameObjPtr, int flags);
748 JIM_EXPORT Jim_Obj * Jim_GetGlobalVariable (Jim_Interp *interp,
749 Jim_Obj *nameObjPtr, int flags);
750 JIM_EXPORT Jim_Obj * Jim_GetVariableStr (Jim_Interp *interp,
751 const char *name, int flags);
752 JIM_EXPORT Jim_Obj * Jim_GetGlobalVariableStr (Jim_Interp *interp,
753 const char *name, int flags);
754 JIM_EXPORT int Jim_UnsetVariable (Jim_Interp *interp,
755 Jim_Obj *nameObjPtr, int flags);
757 /* call frame */
758 JIM_EXPORT Jim_CallFrame *Jim_GetCallFrameByLevel(Jim_Interp *interp,
759 Jim_Obj *levelObjPtr);
761 /* garbage collection */
762 JIM_EXPORT int Jim_Collect (Jim_Interp *interp);
763 JIM_EXPORT void Jim_CollectIfNeeded (Jim_Interp *interp);
765 /* index object */
766 JIM_EXPORT int Jim_GetIndex (Jim_Interp *interp, Jim_Obj *objPtr,
767 int *indexPtr);
769 /* list object */
770 JIM_EXPORT Jim_Obj * Jim_NewListObj (Jim_Interp *interp,
771 Jim_Obj *const *elements, int len);
772 JIM_EXPORT void Jim_ListInsertElements (Jim_Interp *interp,
773 Jim_Obj *listPtr, int listindex, int objc, Jim_Obj *const *objVec);
774 JIM_EXPORT void Jim_ListAppendElement (Jim_Interp *interp,
775 Jim_Obj *listPtr, Jim_Obj *objPtr);
776 JIM_EXPORT void Jim_ListAppendList (Jim_Interp *interp,
777 Jim_Obj *listPtr, Jim_Obj *appendListPtr);
778 JIM_EXPORT int Jim_ListLength (Jim_Interp *interp, Jim_Obj *objPtr);
779 JIM_EXPORT int Jim_ListIndex (Jim_Interp *interp, Jim_Obj *listPrt,
780 int listindex, Jim_Obj **objPtrPtr, int seterr);
781 JIM_EXPORT Jim_Obj *Jim_ListGetIndex(Jim_Interp *interp, Jim_Obj *listPtr, int idx);
782 JIM_EXPORT int Jim_SetListIndex (Jim_Interp *interp,
783 Jim_Obj *varNamePtr, Jim_Obj *const *indexv, int indexc,
784 Jim_Obj *newObjPtr);
785 JIM_EXPORT Jim_Obj * Jim_ConcatObj (Jim_Interp *interp, int objc,
786 Jim_Obj *const *objv);
787 JIM_EXPORT Jim_Obj *Jim_ListJoin(Jim_Interp *interp,
788 Jim_Obj *listObjPtr, const char *joinStr, int joinStrLen);
790 /* dict object */
791 JIM_EXPORT Jim_Obj * Jim_NewDictObj (Jim_Interp *interp,
792 Jim_Obj *const *elements, int len);
793 JIM_EXPORT int Jim_DictKey (Jim_Interp *interp, Jim_Obj *dictPtr,
794 Jim_Obj *keyPtr, Jim_Obj **objPtrPtr, int flags);
795 JIM_EXPORT int Jim_DictKeysVector (Jim_Interp *interp,
796 Jim_Obj *dictPtr, Jim_Obj *const *keyv, int keyc,
797 Jim_Obj **objPtrPtr, int flags);
798 JIM_EXPORT int Jim_SetDictKeysVector (Jim_Interp *interp,
799 Jim_Obj *varNamePtr, Jim_Obj *const *keyv, int keyc,
800 Jim_Obj *newObjPtr, int flags);
801 JIM_EXPORT int Jim_DictPairs(Jim_Interp *interp,
802 Jim_Obj *dictPtr, Jim_Obj ***objPtrPtr, int *len);
803 JIM_EXPORT int Jim_DictAddElement(Jim_Interp *interp, Jim_Obj *objPtr,
804 Jim_Obj *keyObjPtr, Jim_Obj *valueObjPtr);
806 #define JIM_DICTMATCH_KEYS 0x0001
807 #define JIM_DICTMATCH_VALUES 0x002
809 JIM_EXPORT int Jim_DictMatchTypes(Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *patternObj, int match_type, int return_types);
810 JIM_EXPORT int Jim_DictSize(Jim_Interp *interp, Jim_Obj *objPtr);
811 JIM_EXPORT int Jim_DictInfo(Jim_Interp *interp, Jim_Obj *objPtr);
812 JIM_EXPORT Jim_Obj *Jim_DictMerge(Jim_Interp *interp, int objc, Jim_Obj *const *objv);
814 /* return code object */
815 JIM_EXPORT int Jim_GetReturnCode (Jim_Interp *interp, Jim_Obj *objPtr,
816 int *intPtr);
818 /* expression object */
819 JIM_EXPORT int Jim_EvalExpression (Jim_Interp *interp,
820 Jim_Obj *exprObjPtr);
821 JIM_EXPORT int Jim_GetBoolFromExpr (Jim_Interp *interp,
822 Jim_Obj *exprObjPtr, int *boolPtr);
824 /* boolean object */
825 JIM_EXPORT int Jim_GetBoolean(Jim_Interp *interp, Jim_Obj *objPtr,
826 int *booleanPtr);
828 /* integer object */
829 JIM_EXPORT int Jim_GetWide (Jim_Interp *interp, Jim_Obj *objPtr,
830 jim_wide *widePtr);
831 JIM_EXPORT int Jim_GetLong (Jim_Interp *interp, Jim_Obj *objPtr,
832 long *longPtr);
833 #define Jim_NewWideObj Jim_NewIntObj
834 JIM_EXPORT Jim_Obj * Jim_NewIntObj (Jim_Interp *interp,
835 jim_wide wideValue);
837 /* double object */
838 JIM_EXPORT int Jim_GetDouble(Jim_Interp *interp, Jim_Obj *objPtr,
839 double *doublePtr);
840 JIM_EXPORT void Jim_SetDouble(Jim_Interp *interp, Jim_Obj *objPtr,
841 double doubleValue);
842 JIM_EXPORT Jim_Obj * Jim_NewDoubleObj(Jim_Interp *interp, double doubleValue);
844 /* commands utilities */
845 JIM_EXPORT void Jim_WrongNumArgs (Jim_Interp *interp, int argc,
846 Jim_Obj *const *argv, const char *msg);
847 JIM_EXPORT int Jim_GetEnum (Jim_Interp *interp, Jim_Obj *objPtr,
848 const char * const *tablePtr, int *indexPtr, const char *name, int flags);
849 JIM_EXPORT int Jim_CheckShowCommands(Jim_Interp *interp, Jim_Obj *objPtr,
850 const char *const *tablePtr);
851 JIM_EXPORT int Jim_ScriptIsComplete(Jim_Interp *interp,
852 Jim_Obj *scriptObj, char *stateCharPtr);
855 * Find a matching name in the array of the given length.
857 * NULL entries are ignored.
859 * Returns the matching index if found, or -1 if not.
861 JIM_EXPORT int Jim_FindByName(const char *name, const char * const array[], size_t len);
863 /* package utilities */
864 typedef void (Jim_InterpDeleteProc)(Jim_Interp *interp, void *data);
865 JIM_EXPORT void * Jim_GetAssocData(Jim_Interp *interp, const char *key);
866 JIM_EXPORT int Jim_SetAssocData(Jim_Interp *interp, const char *key,
867 Jim_InterpDeleteProc *delProc, void *data);
868 JIM_EXPORT int Jim_DeleteAssocData(Jim_Interp *interp, const char *key);
870 /* Packages C API */
871 /* jim-package.c */
872 JIM_EXPORT int Jim_PackageProvide (Jim_Interp *interp,
873 const char *name, const char *ver, int flags);
874 JIM_EXPORT int Jim_PackageRequire (Jim_Interp *interp,
875 const char *name, int flags);
877 /* error messages */
878 JIM_EXPORT void Jim_MakeErrorMessage (Jim_Interp *interp);
880 /* interactive mode */
881 JIM_EXPORT int Jim_InteractivePrompt (Jim_Interp *interp);
882 JIM_EXPORT void Jim_HistoryLoad(const char *filename);
883 JIM_EXPORT void Jim_HistorySave(const char *filename);
884 JIM_EXPORT char *Jim_HistoryGetline(Jim_Interp *interp, const char *prompt);
885 JIM_EXPORT void Jim_HistorySetCompletion(Jim_Interp *interp, Jim_Obj *commandObj);
886 JIM_EXPORT void Jim_HistoryAdd(const char *line);
887 JIM_EXPORT void Jim_HistoryShow(void);
889 /* Misc */
890 JIM_EXPORT int Jim_InitStaticExtensions(Jim_Interp *interp);
891 JIM_EXPORT int Jim_StringToWide(const char *str, jim_wide *widePtr, int base);
892 JIM_EXPORT int Jim_IsBigEndian(void);
895 * Returns 1 if a signal has been received while
896 * in a catch -signal {} clause.
898 #define Jim_CheckSignal(i) ((i)->signal_level && (i)->sigmask)
900 /* jim-load.c */
901 JIM_EXPORT int Jim_LoadLibrary(Jim_Interp *interp, const char *pathName);
902 JIM_EXPORT void Jim_FreeLoadHandles(Jim_Interp *interp);
904 /* jim-aio.c */
905 JIM_EXPORT FILE *Jim_AioFilehandle(Jim_Interp *interp, Jim_Obj *command);
907 /* type inspection - avoid where possible */
908 JIM_EXPORT int Jim_IsDict(Jim_Obj *objPtr);
909 JIM_EXPORT int Jim_IsList(Jim_Obj *objPtr);
911 #ifdef __cplusplus
913 #endif
915 #endif /* __JIM__H */
918 * Local Variables: ***
919 * c-basic-offset: 4 ***
920 * tab-width: 4 ***
921 * End: ***