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