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,2009 oharboe - Øyvind Harboe - oyvind.harboe@zylin.com
7 * Copyright 2008 Andrew Lunn <andrew@lunn.ch>
8 * Copyright 2008 Duane Ellis <openocd@duaneellis.com>
9 * Copyright 2008 Uwe Klein <uklein@klein-messgeraete.de>
10 * Copyright 2008 Steve Bennett <steveb@workware.net.au>
11 * Copyright 2009 Nico Coesel <ncoesel@dealogic.nl>
12 * Copyright 2009 Zachary T Welch zw@superlucidity.net
13 * Copyright 2009 David Brownell
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE JIM TCL PROJECT ``AS IS'' AND ANY
27 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
29 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
30 * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
31 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * The views and conclusions contained in the software and documentation
40 * are those of the authors and should not be interpreted as representing
41 * official policies, either expressed or implied, of the Jim Tcl Project.
43 #define JIM_OPTIMIZATION /* comment to avoid optimizations and reduce size */
45 #define _GNU_SOURCE /* Mostly just for environ */
61 #include "jimautoconf.h"
64 #ifdef HAVE_SYS_TIME_H
70 #ifdef HAVE_CRT_EXTERNS_H
71 #include <crt_externs.h>
74 /* For INFINITY, even if math functions are not enabled */
77 /* We may decide to switch to using $[...] after all, so leave it as an option */
78 /*#define EXPRSUGAR_BRACKET*/
80 /* For the no-autoconf case */
82 #define TCL_LIBRARY "."
84 #ifndef TCL_PLATFORM_OS
85 #define TCL_PLATFORM_OS "unknown"
87 #ifndef TCL_PLATFORM_PLATFORM
88 #define TCL_PLATFORM_PLATFORM "unknown"
90 #ifndef TCL_PLATFORM_PATH_SEPARATOR
91 #define TCL_PLATFORM_PATH_SEPARATOR ":"
94 /*#define DEBUG_SHOW_SCRIPT*/
95 /*#define DEBUG_SHOW_SCRIPT_TOKENS*/
96 /*#define DEBUG_SHOW_SUBST*/
97 /*#define DEBUG_SHOW_EXPR*/
98 /*#define DEBUG_SHOW_EXPR_TOKENS*/
99 /*#define JIM_DEBUG_GC*/
100 #ifdef JIM_MAINTAINER
101 #define JIM_DEBUG_COMMAND
102 #define JIM_DEBUG_PANIC
104 /* Enable this (in conjunction with valgrind) to help debug
105 * reference counting issues
107 /*#define JIM_DISABLE_OBJECT_POOL*/
109 /* Maximum size of an integer */
110 #define JIM_INTEGER_SPACE 24
112 const char *jim_tt_name(int type
);
114 #ifdef JIM_DEBUG_PANIC
115 static void JimPanicDump(int fail_condition
, const char *fmt
, ...);
116 #define JimPanic(X) JimPanicDump X
121 #ifdef JIM_OPTIMIZATION
122 #define JIM_IF_OPTIM(X) X
124 #define JIM_IF_OPTIM(X)
127 /* -----------------------------------------------------------------------------
129 * ---------------------------------------------------------------------------*/
131 /* A shared empty string for the objects string representation.
132 * Jim_InvalidateStringRep knows about it and doesn't try to free it. */
133 static char JimEmptyStringRep
[] = "";
135 /* -----------------------------------------------------------------------------
136 * Required prototypes of not exported functions
137 * ---------------------------------------------------------------------------*/
138 static void JimFreeCallFrame(Jim_Interp
*interp
, Jim_CallFrame
*cf
, int action
);
139 static int ListSetIndex(Jim_Interp
*interp
, Jim_Obj
*listPtr
, int listindex
, Jim_Obj
*newObjPtr
,
141 static int JimDeleteLocalProcs(Jim_Interp
*interp
, Jim_Stack
*localCommands
);
142 static Jim_Obj
*JimExpandDictSugar(Jim_Interp
*interp
, Jim_Obj
*objPtr
);
143 static void SetDictSubstFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
);
144 static Jim_Obj
**JimDictPairs(Jim_Obj
*dictPtr
, int *len
);
145 static void JimSetFailedEnumResult(Jim_Interp
*interp
, const char *arg
, const char *badtype
,
146 const char *prefix
, const char *const *tablePtr
, const char *name
);
147 static int JimCallProcedure(Jim_Interp
*interp
, Jim_Cmd
*cmd
, int argc
, Jim_Obj
*const *argv
);
148 static int JimGetWideNoErr(Jim_Interp
*interp
, Jim_Obj
*objPtr
, jim_wide
* widePtr
);
149 static int JimSign(jim_wide w
);
150 static int JimValidName(Jim_Interp
*interp
, const char *type
, Jim_Obj
*nameObjPtr
);
151 static void JimPrngSeed(Jim_Interp
*interp
, unsigned char *seed
, int seedLen
);
152 static void JimRandomBytes(Jim_Interp
*interp
, void *dest
, unsigned int len
);
155 /* Fast access to the int (wide) value of an object which is known to be of int type */
156 #define JimWideValue(objPtr) (objPtr)->internalRep.wideValue
158 #define JimObjTypeName(O) ((O)->typePtr ? (O)->typePtr->name : "none")
160 static int utf8_tounicode_case(const char *s
, int *uc
, int upper
)
162 int l
= utf8_tounicode(s
, uc
);
164 *uc
= utf8_upper(*uc
);
169 /* These can be used in addition to JIM_CASESENS/JIM_NOCASE */
170 #define JIM_CHARSET_SCAN 2
171 #define JIM_CHARSET_GLOB 0
174 * pattern points to a string like "[^a-z\ub5]"
176 * The pattern may contain trailing chars, which are ignored.
178 * The pattern is matched against unicode char 'c'.
180 * If (flags & JIM_NOCASE), case is ignored when matching.
181 * If (flags & JIM_CHARSET_SCAN), the considers ^ and ] special at the start
182 * of the charset, per scan, rather than glob/string match.
184 * If the unicode char 'c' matches that set, returns a pointer to the ']' character,
185 * or the null character if the ']' is missing.
187 * Returns NULL on no match.
189 static const char *JimCharsetMatch(const char *pattern
, int c
, int flags
)
196 if (flags
& JIM_NOCASE
) {
201 if (flags
& JIM_CHARSET_SCAN
) {
202 if (*pattern
== '^') {
207 /* Special case. If the first char is ']', it is part of the set */
208 if (*pattern
== ']') {
213 while (*pattern
&& *pattern
!= ']') {
215 if (pattern
[0] == '\\') {
217 pattern
+= utf8_tounicode_case(pattern
, &pchar
, nocase
);
220 /* Is this a range? a-z */
224 pattern
+= utf8_tounicode_case(pattern
, &start
, nocase
);
225 if (pattern
[0] == '-' && pattern
[1]) {
228 pattern
+= utf8_tounicode_case(pattern
, &end
, nocase
);
230 /* Handle reversed range too */
231 if ((c
>= start
&& c
<= end
) || (c
>= end
&& c
<= start
)) {
247 return match
? pattern
: NULL
;
250 /* Glob-style pattern matching. */
252 /* Note: string *must* be valid UTF-8 sequences
254 static int JimGlobMatch(const char *pattern
, const char *string
, int nocase
)
259 switch (pattern
[0]) {
261 while (pattern
[1] == '*') {
266 return 1; /* match */
269 /* Recursive call - Does the remaining pattern match anywhere? */
270 if (JimGlobMatch(pattern
, string
, nocase
))
271 return 1; /* match */
272 string
+= utf8_tounicode(string
, &c
);
274 return 0; /* no match */
277 string
+= utf8_tounicode(string
, &c
);
281 string
+= utf8_tounicode(string
, &c
);
282 pattern
= JimCharsetMatch(pattern
+ 1, c
, nocase
? JIM_NOCASE
: 0);
287 /* Ran out of pattern (no ']') */
298 string
+= utf8_tounicode_case(string
, &c
, nocase
);
299 utf8_tounicode_case(pattern
, &pchar
, nocase
);
305 pattern
+= utf8_tounicode_case(pattern
, &pchar
, nocase
);
307 while (*pattern
== '*') {
313 if (!*pattern
&& !*string
) {
320 * string comparison. Works on binary data.
324 * Note that the lengths are byte lengths, not char lengths.
326 static int JimStringCompare(const char *s1
, int l1
, const char *s2
, int l2
)
329 return memcmp(s1
, s2
, l1
) <= 0 ? -1 : 1;
332 return memcmp(s1
, s2
, l2
) >= 0 ? 1 : -1;
335 return JimSign(memcmp(s1
, s2
, l1
));
340 * Compare null terminated strings, up to a maximum of 'maxchars' characters,
341 * (or end of string if 'maxchars' is -1).
343 * Returns -1, 0, 1 for s1 < s2, s1 == s2, s1 > s2 respectively.
345 * Note: does not support embedded nulls.
347 static int JimStringCompareLen(const char *s1
, const char *s2
, int maxchars
, int nocase
)
349 while (*s1
&& *s2
&& maxchars
) {
351 s1
+= utf8_tounicode_case(s1
, &c1
, nocase
);
352 s2
+= utf8_tounicode_case(s2
, &c2
, nocase
);
354 return JimSign(c1
- c2
);
361 /* One string or both terminated */
371 /* Search for 's1' inside 's2', starting to search from char 'index' of 's2'.
372 * The index of the first occurrence of s1 in s2 is returned.
373 * If s1 is not found inside s2, -1 is returned.
375 * Note: Lengths and return value are in bytes, not chars.
377 static int JimStringFirst(const char *s1
, int l1
, const char *s2
, int l2
, int idx
)
382 if (!l1
|| !l2
|| l1
> l2
) {
387 s2
+= utf8_index(s2
, idx
);
389 l1bytelen
= utf8_index(s1
, l1
);
391 for (i
= idx
; i
<= l2
- l1
; i
++) {
393 if (memcmp(s2
, s1
, l1bytelen
) == 0) {
396 s2
+= utf8_tounicode(s2
, &c
);
401 /* Search for the last occurrence 's1' inside 's2', starting to search from char 'index' of 's2'.
402 * The index of the last occurrence of s1 in s2 is returned.
403 * If s1 is not found inside s2, -1 is returned.
405 * Note: Lengths and return value are in bytes, not chars.
407 static int JimStringLast(const char *s1
, int l1
, const char *s2
, int l2
)
411 if (!l1
|| !l2
|| l1
> l2
)
414 /* Now search for the needle */
415 for (p
= s2
+ l2
- 1; p
!= s2
- 1; p
--) {
416 if (*p
== *s1
&& memcmp(s1
, p
, l1
) == 0) {
425 * Per JimStringLast but lengths and return value are in chars, not bytes.
427 static int JimStringLastUtf8(const char *s1
, int l1
, const char *s2
, int l2
)
429 int n
= JimStringLast(s1
, utf8_index(s1
, l1
), s2
, utf8_index(s2
, l2
));
431 n
= utf8_strlen(s2
, n
);
438 * After an strtol()/strtod()-like conversion,
439 * check whether something was converted and that
440 * the only thing left is white space.
442 * Returns JIM_OK or JIM_ERR.
444 static int JimCheckConversion(const char *str
, const char *endptr
)
446 if (str
[0] == '\0' || str
== endptr
) {
450 if (endptr
[0] != '\0') {
452 if (!isspace(UCHAR(*endptr
))) {
461 /* Parses the front of a number to determine its sign and base.
462 * Returns the index to start parsing according to the given base
464 static int JimNumberBase(const char *str
, int *base
, int *sign
)
470 while (isspace(UCHAR(str
[i
]))) {
490 /* We have 0<x>, so see if we can convert it */
491 switch (str
[i
+ 1]) {
492 case 'x': case 'X': *base
= 16; break;
493 case 'o': case 'O': *base
= 8; break;
494 case 'b': case 'B': *base
= 2; break;
498 /* Ensure that (e.g.) 0x-5 fails to parse */
499 if (str
[i
] != '-' && str
[i
] != '+' && !isspace(UCHAR(str
[i
]))) {
500 /* Parse according to this base */
503 /* Parse as base 10 */
508 /* Converts a number as per strtol(..., 0) except leading zeros do *not*
509 * imply octal. Instead, decimal is assumed unless the number begins with 0x, 0o or 0b
511 static long jim_strtol(const char *str
, char **endptr
)
515 int i
= JimNumberBase(str
, &base
, &sign
);
518 long value
= strtol(str
+ i
, endptr
, base
);
519 if (endptr
== NULL
|| *endptr
!= str
+ i
) {
524 /* Can just do a regular base-10 conversion */
525 return strtol(str
, endptr
, 10);
529 /* Converts a number as per strtoull(..., 0) except leading zeros do *not*
530 * imply octal. Instead, decimal is assumed unless the number begins with 0x, 0o or 0b
532 static jim_wide
jim_strtoull(const char *str
, char **endptr
)
534 #ifdef HAVE_LONG_LONG
537 int i
= JimNumberBase(str
, &base
, &sign
);
540 jim_wide value
= strtoull(str
+ i
, endptr
, base
);
541 if (endptr
== NULL
|| *endptr
!= str
+ i
) {
546 /* Can just do a regular base-10 conversion */
547 return strtoull(str
, endptr
, 10);
549 return (unsigned long)jim_strtol(str
, endptr
);
553 int Jim_StringToWide(const char *str
, jim_wide
* widePtr
, int base
)
558 *widePtr
= strtoull(str
, &endptr
, base
);
561 *widePtr
= jim_strtoull(str
, &endptr
);
564 return JimCheckConversion(str
, endptr
);
567 int Jim_StringToDouble(const char *str
, double *doublePtr
)
571 /* Callers can check for underflow via ERANGE */
574 *doublePtr
= strtod(str
, &endptr
);
576 return JimCheckConversion(str
, endptr
);
579 static jim_wide
JimPowWide(jim_wide b
, jim_wide e
)
592 /* Only special case is -1 ^ -n
610 /* -----------------------------------------------------------------------------
612 * ---------------------------------------------------------------------------*/
613 #ifdef JIM_DEBUG_PANIC
614 static void JimPanicDump(int condition
, const char *fmt
, ...)
624 fprintf(stderr
, "\nJIM INTERPRETER PANIC: ");
625 vfprintf(stderr
, fmt
, ap
);
626 fprintf(stderr
, "\n\n");
629 #ifdef HAVE_BACKTRACE
635 size
= backtrace(array
, 40);
636 strings
= backtrace_symbols(array
, size
);
637 for (i
= 0; i
< size
; i
++)
638 fprintf(stderr
, "[backtrace] %s\n", strings
[i
]);
639 fprintf(stderr
, "[backtrace] Include the above lines and the output\n");
640 fprintf(stderr
, "[backtrace] of 'nm <executable>' in the bug report.\n");
648 /* -----------------------------------------------------------------------------
650 * ---------------------------------------------------------------------------*/
652 void *Jim_Alloc(int size
)
654 return size
? malloc(size
) : NULL
;
657 void Jim_Free(void *ptr
)
662 void *Jim_Realloc(void *ptr
, int size
)
664 return realloc(ptr
, size
);
667 char *Jim_StrDup(const char *s
)
672 char *Jim_StrDupLen(const char *s
, int l
)
674 char *copy
= Jim_Alloc(l
+ 1);
676 memcpy(copy
, s
, l
+ 1);
677 copy
[l
] = 0; /* Just to be sure, original could be substring */
681 /* -----------------------------------------------------------------------------
682 * Time related functions
683 * ---------------------------------------------------------------------------*/
685 /* Returns current time in microseconds */
686 static jim_wide
JimClock(void)
690 gettimeofday(&tv
, NULL
);
691 return (jim_wide
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
694 /* -----------------------------------------------------------------------------
696 * ---------------------------------------------------------------------------*/
698 /* -------------------------- private prototypes ---------------------------- */
699 static void JimExpandHashTableIfNeeded(Jim_HashTable
*ht
);
700 static unsigned int JimHashTableNextPower(unsigned int size
);
701 static Jim_HashEntry
*JimInsertHashEntry(Jim_HashTable
*ht
, const void *key
, int replace
);
703 /* -------------------------- hash functions -------------------------------- */
705 /* Thomas Wang's 32 bit Mix Function */
706 unsigned int Jim_IntHashFunction(unsigned int key
)
717 /* Generic hash function (we are using to multiply by 9 and add the byte
719 unsigned int Jim_GenHashFunction(const unsigned char *buf
, int len
)
724 h
+= (h
<< 3) + *buf
++;
728 /* ----------------------------- API implementation ------------------------- */
730 /* reset a hashtable already initialized */
731 static void JimResetHashTable(Jim_HashTable
*ht
)
738 #ifdef JIM_RANDOMISE_HASH
739 /* This is initialised to a random value to avoid a hash collision attack.
740 * See: n.runs-SA-2011.004
742 ht
->uniq
= (rand() ^ time(NULL
) ^ clock());
748 static void JimInitHashTableIterator(Jim_HashTable
*ht
, Jim_HashTableIterator
*iter
)
753 iter
->nextEntry
= NULL
;
756 /* Initialize the hash table */
757 int Jim_InitHashTable(Jim_HashTable
*ht
, const Jim_HashTableType
*type
, void *privDataPtr
)
759 JimResetHashTable(ht
);
761 ht
->privdata
= privDataPtr
;
765 /* Expand or create the hashtable */
766 void Jim_ExpandHashTable(Jim_HashTable
*ht
, unsigned int size
)
768 Jim_HashTable n
; /* the new hashtable */
769 unsigned int realsize
= JimHashTableNextPower(size
), i
;
771 /* the size is invalid if it is smaller than the number of
772 * elements already inside the hashtable */
773 if (size
<= ht
->used
)
776 Jim_InitHashTable(&n
, ht
->type
, ht
->privdata
);
778 n
.sizemask
= realsize
- 1;
779 n
.table
= Jim_Alloc(realsize
* sizeof(Jim_HashEntry
*));
780 /* Keep the same 'uniq' as the original */
783 /* Initialize all the pointers to NULL */
784 memset(n
.table
, 0, realsize
* sizeof(Jim_HashEntry
*));
786 /* Copy all the elements from the old to the new table:
787 * note that if the old hash table is empty ht->used is zero,
788 * so Jim_ExpandHashTable just creates an empty hash table. */
790 for (i
= 0; ht
->used
> 0; i
++) {
791 Jim_HashEntry
*he
, *nextHe
;
793 if (ht
->table
[i
] == NULL
)
796 /* For each hash entry on this slot... */
802 /* Get the new element index */
803 h
= Jim_HashKey(ht
, he
->key
) & n
.sizemask
;
804 he
->next
= n
.table
[h
];
807 /* Pass to the next element */
811 assert(ht
->used
== 0);
814 /* Remap the new hashtable in the old */
818 /* Add an element to the target hash table */
819 int Jim_AddHashEntry(Jim_HashTable
*ht
, const void *key
, void *val
)
821 Jim_HashEntry
*entry
;
823 /* Get the index of the new element, or -1 if
824 * the element already exists. */
825 entry
= JimInsertHashEntry(ht
, key
, 0);
829 /* Set the hash entry fields. */
830 Jim_SetHashKey(ht
, entry
, key
);
831 Jim_SetHashVal(ht
, entry
, val
);
835 /* Add an element, discarding the old if the key already exists */
836 int Jim_ReplaceHashEntry(Jim_HashTable
*ht
, const void *key
, void *val
)
839 Jim_HashEntry
*entry
;
841 /* Get the index of the new element, or -1 if
842 * the element already exists. */
843 entry
= JimInsertHashEntry(ht
, key
, 1);
845 /* It already exists, so only replace the value.
846 * Note if both a destructor and a duplicate function exist,
847 * need to dup before destroy. perhaps they are the same
848 * reference counted object
850 if (ht
->type
->valDestructor
&& ht
->type
->valDup
) {
851 void *newval
= ht
->type
->valDup(ht
->privdata
, val
);
852 ht
->type
->valDestructor(ht
->privdata
, entry
->u
.val
);
853 entry
->u
.val
= newval
;
856 Jim_FreeEntryVal(ht
, entry
);
857 Jim_SetHashVal(ht
, entry
, val
);
862 /* Doesn't exist, so set the key */
863 Jim_SetHashKey(ht
, entry
, key
);
864 Jim_SetHashVal(ht
, entry
, val
);
871 /* Search and remove an element */
872 int Jim_DeleteHashEntry(Jim_HashTable
*ht
, const void *key
)
875 Jim_HashEntry
*he
, *prevHe
;
879 h
= Jim_HashKey(ht
, key
) & ht
->sizemask
;
884 if (Jim_CompareHashKeys(ht
, key
, he
->key
)) {
885 /* Unlink the element from the list */
887 prevHe
->next
= he
->next
;
889 ht
->table
[h
] = he
->next
;
890 Jim_FreeEntryKey(ht
, he
);
891 Jim_FreeEntryVal(ht
, he
);
899 return JIM_ERR
; /* not found */
902 /* Destroy an entire hash table and leave it ready for reuse */
903 int Jim_FreeHashTable(Jim_HashTable
*ht
)
907 /* Free all the elements */
908 for (i
= 0; ht
->used
> 0; i
++) {
909 Jim_HashEntry
*he
, *nextHe
;
911 if ((he
= ht
->table
[i
]) == NULL
)
915 Jim_FreeEntryKey(ht
, he
);
916 Jim_FreeEntryVal(ht
, he
);
922 /* Free the table and the allocated cache structure */
924 /* Re-initialize the table */
925 JimResetHashTable(ht
);
926 return JIM_OK
; /* never fails */
929 Jim_HashEntry
*Jim_FindHashEntry(Jim_HashTable
*ht
, const void *key
)
936 h
= Jim_HashKey(ht
, key
) & ht
->sizemask
;
939 if (Jim_CompareHashKeys(ht
, key
, he
->key
))
946 Jim_HashTableIterator
*Jim_GetHashTableIterator(Jim_HashTable
*ht
)
948 Jim_HashTableIterator
*iter
= Jim_Alloc(sizeof(*iter
));
949 JimInitHashTableIterator(ht
, iter
);
953 Jim_HashEntry
*Jim_NextHashEntry(Jim_HashTableIterator
*iter
)
956 if (iter
->entry
== NULL
) {
958 if (iter
->index
>= (signed)iter
->ht
->size
)
960 iter
->entry
= iter
->ht
->table
[iter
->index
];
963 iter
->entry
= iter
->nextEntry
;
966 /* We need to save the 'next' here, the iterator user
967 * may delete the entry we are returning. */
968 iter
->nextEntry
= iter
->entry
->next
;
975 /* ------------------------- private functions ------------------------------ */
977 /* Expand the hash table if needed */
978 static void JimExpandHashTableIfNeeded(Jim_HashTable
*ht
)
980 /* If the hash table is empty expand it to the intial size,
981 * if the table is "full" double its size. */
983 Jim_ExpandHashTable(ht
, JIM_HT_INITIAL_SIZE
);
984 if (ht
->size
== ht
->used
)
985 Jim_ExpandHashTable(ht
, ht
->size
* 2);
988 /* Our hash table capability is a power of two */
989 static unsigned int JimHashTableNextPower(unsigned int size
)
991 unsigned int i
= JIM_HT_INITIAL_SIZE
;
993 if (size
>= 2147483648U)
1002 /* Returns the index of a free slot that can be populated with
1003 * a hash entry for the given 'key'.
1004 * If the key already exists, -1 is returned. */
1005 static Jim_HashEntry
*JimInsertHashEntry(Jim_HashTable
*ht
, const void *key
, int replace
)
1010 /* Expand the hashtable if needed */
1011 JimExpandHashTableIfNeeded(ht
);
1013 /* Compute the key hash value */
1014 h
= Jim_HashKey(ht
, key
) & ht
->sizemask
;
1015 /* Search if this slot does not already contain the given key */
1018 if (Jim_CompareHashKeys(ht
, key
, he
->key
))
1019 return replace
? he
: NULL
;
1023 /* Allocates the memory and stores key */
1024 he
= Jim_Alloc(sizeof(*he
));
1025 he
->next
= ht
->table
[h
];
1033 /* ----------------------- StringCopy Hash Table Type ------------------------*/
1035 static unsigned int JimStringCopyHTHashFunction(const void *key
)
1037 return Jim_GenHashFunction(key
, strlen(key
));
1040 static void *JimStringCopyHTDup(void *privdata
, const void *key
)
1042 return Jim_StrDup(key
);
1045 static int JimStringCopyHTKeyCompare(void *privdata
, const void *key1
, const void *key2
)
1047 return strcmp(key1
, key2
) == 0;
1050 static void JimStringCopyHTKeyDestructor(void *privdata
, void *key
)
1055 static const Jim_HashTableType JimPackageHashTableType
= {
1056 JimStringCopyHTHashFunction
, /* hash function */
1057 JimStringCopyHTDup
, /* key dup */
1059 JimStringCopyHTKeyCompare
, /* key compare */
1060 JimStringCopyHTKeyDestructor
, /* key destructor */
1061 NULL
/* val destructor */
1064 typedef struct AssocDataValue
1066 Jim_InterpDeleteProc
*delProc
;
1070 static void JimAssocDataHashTableValueDestructor(void *privdata
, void *data
)
1072 AssocDataValue
*assocPtr
= (AssocDataValue
*) data
;
1074 if (assocPtr
->delProc
!= NULL
)
1075 assocPtr
->delProc((Jim_Interp
*)privdata
, assocPtr
->data
);
1079 static const Jim_HashTableType JimAssocDataHashTableType
= {
1080 JimStringCopyHTHashFunction
, /* hash function */
1081 JimStringCopyHTDup
, /* key dup */
1083 JimStringCopyHTKeyCompare
, /* key compare */
1084 JimStringCopyHTKeyDestructor
, /* key destructor */
1085 JimAssocDataHashTableValueDestructor
/* val destructor */
1088 /* -----------------------------------------------------------------------------
1089 * Stack - This is a simple generic stack implementation. It is used for
1090 * example in the 'expr' expression compiler.
1091 * ---------------------------------------------------------------------------*/
1092 void Jim_InitStack(Jim_Stack
*stack
)
1096 stack
->vector
= NULL
;
1099 void Jim_FreeStack(Jim_Stack
*stack
)
1101 Jim_Free(stack
->vector
);
1104 int Jim_StackLen(Jim_Stack
*stack
)
1109 void Jim_StackPush(Jim_Stack
*stack
, void *element
)
1111 int neededLen
= stack
->len
+ 1;
1113 if (neededLen
> stack
->maxlen
) {
1114 stack
->maxlen
= neededLen
< 20 ? 20 : neededLen
* 2;
1115 stack
->vector
= Jim_Realloc(stack
->vector
, sizeof(void *) * stack
->maxlen
);
1117 stack
->vector
[stack
->len
] = element
;
1121 void *Jim_StackPop(Jim_Stack
*stack
)
1123 if (stack
->len
== 0)
1126 return stack
->vector
[stack
->len
];
1129 void *Jim_StackPeek(Jim_Stack
*stack
)
1131 if (stack
->len
== 0)
1133 return stack
->vector
[stack
->len
- 1];
1136 void Jim_FreeStackElements(Jim_Stack
*stack
, void (*freeFunc
) (void *ptr
))
1140 for (i
= 0; i
< stack
->len
; i
++)
1141 freeFunc(stack
->vector
[i
]);
1144 /* -----------------------------------------------------------------------------
1146 * ---------------------------------------------------------------------------*/
1149 #define JIM_TT_NONE 0 /* No token returned */
1150 #define JIM_TT_STR 1 /* simple string */
1151 #define JIM_TT_ESC 2 /* string that needs escape chars conversion */
1152 #define JIM_TT_VAR 3 /* var substitution */
1153 #define JIM_TT_DICTSUGAR 4 /* Syntax sugar for [dict get], $foo(bar) */
1154 #define JIM_TT_CMD 5 /* command substitution */
1155 /* Note: Keep these three together for TOKEN_IS_SEP() */
1156 #define JIM_TT_SEP 6 /* word separator (white space) */
1157 #define JIM_TT_EOL 7 /* line separator */
1158 #define JIM_TT_EOF 8 /* end of script */
1160 #define JIM_TT_LINE 9 /* special 'start-of-line' token. arg is # of arguments to the command. -ve if {*} */
1161 #define JIM_TT_WORD 10 /* special 'start-of-word' token. arg is # of tokens to combine. -ve if {*} */
1163 /* Additional token types needed for expressions */
1164 #define JIM_TT_SUBEXPR_START 11
1165 #define JIM_TT_SUBEXPR_END 12
1166 #define JIM_TT_SUBEXPR_COMMA 13
1167 #define JIM_TT_EXPR_INT 14
1168 #define JIM_TT_EXPR_DOUBLE 15
1169 #define JIM_TT_EXPR_BOOLEAN 16
1171 #define JIM_TT_EXPRSUGAR 17 /* $(expression) */
1173 /* Operator token types start here */
1174 #define JIM_TT_EXPR_OP 20
1176 #define TOKEN_IS_SEP(type) (type >= JIM_TT_SEP && type <= JIM_TT_EOF)
1177 /* Can this token start an expression? */
1178 #define TOKEN_IS_EXPR_START(type) (type == JIM_TT_NONE || type == JIM_TT_SUBEXPR_START || type == JIM_TT_SUBEXPR_COMMA)
1179 /* Is this token an expression operator? */
1180 #define TOKEN_IS_EXPR_OP(type) (type >= JIM_TT_EXPR_OP)
1183 * Results of missing quotes, braces, etc. from parsing.
1185 struct JimParseMissing
{
1186 int ch
; /* At end of parse, ' ' if complete or '{', '[', '"', '\\', '}' if incomplete */
1187 int line
; /* Line number starting the missing token */
1190 /* Parser context structure. The same context is used to parse
1191 * Tcl scripts, expressions and lists. */
1194 const char *p
; /* Pointer to the point of the program we are parsing */
1195 int len
; /* Remaining length */
1196 int linenr
; /* Current line number */
1198 const char *tend
; /* Returned token is at tstart-tend in 'prg'. */
1199 int tline
; /* Line number of the returned token */
1200 int tt
; /* Token type */
1201 int eof
; /* Non zero if EOF condition is true. */
1202 int inquote
; /* Parsing a quoted string */
1203 int comment
; /* Non zero if the next chars may be a comment. */
1204 struct JimParseMissing missing
; /* Details of any missing quotes, etc. */
1207 static int JimParseScript(struct JimParserCtx
*pc
);
1208 static int JimParseSep(struct JimParserCtx
*pc
);
1209 static int JimParseEol(struct JimParserCtx
*pc
);
1210 static int JimParseCmd(struct JimParserCtx
*pc
);
1211 static int JimParseQuote(struct JimParserCtx
*pc
);
1212 static int JimParseVar(struct JimParserCtx
*pc
);
1213 static int JimParseBrace(struct JimParserCtx
*pc
);
1214 static int JimParseStr(struct JimParserCtx
*pc
);
1215 static int JimParseComment(struct JimParserCtx
*pc
);
1216 static void JimParseSubCmd(struct JimParserCtx
*pc
);
1217 static int JimParseSubQuote(struct JimParserCtx
*pc
);
1218 static Jim_Obj
*JimParserGetTokenObj(Jim_Interp
*interp
, struct JimParserCtx
*pc
);
1220 /* Initialize a parser context.
1221 * 'prg' is a pointer to the program text, linenr is the line
1222 * number of the first line contained in the program. */
1223 static void JimParserInit(struct JimParserCtx
*pc
, const char *prg
, int len
, int linenr
)
1230 pc
->tt
= JIM_TT_NONE
;
1233 pc
->linenr
= linenr
;
1235 pc
->missing
.ch
= ' ';
1236 pc
->missing
.line
= linenr
;
1239 static int JimParseScript(struct JimParserCtx
*pc
)
1241 while (1) { /* the while is used to reiterate with continue if needed */
1244 pc
->tend
= pc
->p
- 1;
1245 pc
->tline
= pc
->linenr
;
1246 pc
->tt
= JIM_TT_EOL
;
1252 if (*(pc
->p
+ 1) == '\n' && !pc
->inquote
) {
1253 return JimParseSep(pc
);
1256 return JimParseStr(pc
);
1262 return JimParseSep(pc
);
1264 return JimParseStr(pc
);
1269 return JimParseEol(pc
);
1270 return JimParseStr(pc
);
1273 return JimParseCmd(pc
);
1276 if (JimParseVar(pc
) == JIM_ERR
) {
1277 /* An orphan $. Create as a separate token */
1278 pc
->tstart
= pc
->tend
= pc
->p
++;
1280 pc
->tt
= JIM_TT_ESC
;
1285 JimParseComment(pc
);
1288 return JimParseStr(pc
);
1291 return JimParseStr(pc
);
1297 static int JimParseSep(struct JimParserCtx
*pc
)
1300 pc
->tline
= pc
->linenr
;
1301 while (isspace(UCHAR(*pc
->p
)) || (*pc
->p
== '\\' && *(pc
->p
+ 1) == '\n')) {
1302 if (*pc
->p
== '\n') {
1305 if (*pc
->p
== '\\') {
1313 pc
->tend
= pc
->p
- 1;
1314 pc
->tt
= JIM_TT_SEP
;
1318 static int JimParseEol(struct JimParserCtx
*pc
)
1321 pc
->tline
= pc
->linenr
;
1322 while (isspace(UCHAR(*pc
->p
)) || *pc
->p
== ';') {
1328 pc
->tend
= pc
->p
- 1;
1329 pc
->tt
= JIM_TT_EOL
;
1334 ** Here are the rules for parsing:
1335 ** {braced expression}
1336 ** - Count open and closing braces
1337 ** - Backslash escapes meaning of braces but doesn't remove the backslash
1339 ** "quoted expression"
1340 ** - Unescaped double quote terminates the expression
1341 ** - Backslash escapes next char
1342 ** - [commands brackets] are counted/nested
1343 ** - command rules apply within [brackets], not quoting rules (i.e. brackets have their own rules)
1345 ** [command expression]
1346 ** - Count open and closing brackets
1347 ** - Backslash escapes next char
1348 ** - [commands brackets] are counted/nested
1349 ** - "quoted expressions" are parsed according to quoting rules
1350 ** - {braced expressions} are parsed according to brace rules
1352 ** For everything, backslash escapes the next char, newline increments current line
1356 * Parses a braced expression starting at pc->p.
1358 * Positions the parser at the end of the braced expression,
1359 * sets pc->tend and possibly pc->missing.
1361 static void JimParseSubBrace(struct JimParserCtx
*pc
)
1365 /* Skip the brace */
1372 if (*++pc
->p
== '\n') {
1385 pc
->tend
= pc
->p
- 1;
1399 pc
->missing
.ch
= '{';
1400 pc
->missing
.line
= pc
->tline
;
1401 pc
->tend
= pc
->p
- 1;
1405 * Parses a quoted expression starting at pc->p.
1407 * Positions the parser at the end of the quoted expression,
1408 * sets pc->tend and possibly pc->missing.
1410 * Returns the type of the token of the string,
1411 * either JIM_TT_ESC (if it contains values which need to be [subst]ed)
1414 static int JimParseSubQuote(struct JimParserCtx
*pc
)
1416 int tt
= JIM_TT_STR
;
1417 int line
= pc
->tline
;
1419 /* Skip the quote */
1426 if (*++pc
->p
== '\n') {
1435 pc
->tend
= pc
->p
- 1;
1456 pc
->missing
.ch
= '"';
1457 pc
->missing
.line
= line
;
1458 pc
->tend
= pc
->p
- 1;
1463 * Parses a [command] expression starting at pc->p.
1465 * Positions the parser at the end of the command expression,
1466 * sets pc->tend and possibly pc->missing.
1468 static void JimParseSubCmd(struct JimParserCtx
*pc
)
1471 int startofword
= 1;
1472 int line
= pc
->tline
;
1474 /* Skip the bracket */
1481 if (*++pc
->p
== '\n') {
1494 pc
->tend
= pc
->p
- 1;
1503 JimParseSubQuote(pc
);
1509 JimParseSubBrace(pc
);
1517 startofword
= isspace(UCHAR(*pc
->p
));
1521 pc
->missing
.ch
= '[';
1522 pc
->missing
.line
= line
;
1523 pc
->tend
= pc
->p
- 1;
1526 static int JimParseBrace(struct JimParserCtx
*pc
)
1528 pc
->tstart
= pc
->p
+ 1;
1529 pc
->tline
= pc
->linenr
;
1530 pc
->tt
= JIM_TT_STR
;
1531 JimParseSubBrace(pc
);
1535 static int JimParseCmd(struct JimParserCtx
*pc
)
1537 pc
->tstart
= pc
->p
+ 1;
1538 pc
->tline
= pc
->linenr
;
1539 pc
->tt
= JIM_TT_CMD
;
1544 static int JimParseQuote(struct JimParserCtx
*pc
)
1546 pc
->tstart
= pc
->p
+ 1;
1547 pc
->tline
= pc
->linenr
;
1548 pc
->tt
= JimParseSubQuote(pc
);
1552 static int JimParseVar(struct JimParserCtx
*pc
)
1558 #ifdef EXPRSUGAR_BRACKET
1559 if (*pc
->p
== '[') {
1560 /* Parse $[...] expr shorthand syntax */
1562 pc
->tt
= JIM_TT_EXPRSUGAR
;
1568 pc
->tt
= JIM_TT_VAR
;
1569 pc
->tline
= pc
->linenr
;
1571 if (*pc
->p
== '{') {
1572 pc
->tstart
= ++pc
->p
;
1575 while (pc
->len
&& *pc
->p
!= '}') {
1576 if (*pc
->p
== '\n') {
1582 pc
->tend
= pc
->p
- 1;
1590 /* Skip double colon, but not single colon! */
1591 if (pc
->p
[0] == ':' && pc
->p
[1] == ':') {
1592 while (*pc
->p
== ':') {
1598 /* Note that any char >= 0x80 must be part of a utf-8 char.
1599 * We consider all unicode points outside of ASCII as letters
1601 if (isalnum(UCHAR(*pc
->p
)) || *pc
->p
== '_' || UCHAR(*pc
->p
) >= 0x80) {
1608 /* Parse [dict get] syntax sugar. */
1609 if (*pc
->p
== '(') {
1611 const char *paren
= NULL
;
1613 pc
->tt
= JIM_TT_DICTSUGAR
;
1615 while (count
&& pc
->len
) {
1618 if (*pc
->p
== '\\' && pc
->len
>= 1) {
1622 else if (*pc
->p
== '(') {
1625 else if (*pc
->p
== ')') {
1635 /* Did not find a matching paren. Back up */
1637 pc
->len
+= (pc
->p
- paren
);
1640 #ifndef EXPRSUGAR_BRACKET
1641 if (*pc
->tstart
== '(') {
1642 pc
->tt
= JIM_TT_EXPRSUGAR
;
1646 pc
->tend
= pc
->p
- 1;
1648 /* Check if we parsed just the '$' character.
1649 * That's not a variable so an error is returned
1650 * to tell the state machine to consider this '$' just
1652 if (pc
->tstart
== pc
->p
) {
1660 static int JimParseStr(struct JimParserCtx
*pc
)
1662 if (pc
->tt
== JIM_TT_SEP
|| pc
->tt
== JIM_TT_EOL
||
1663 pc
->tt
== JIM_TT_NONE
|| pc
->tt
== JIM_TT_STR
) {
1664 /* Starting a new word */
1665 if (*pc
->p
== '{') {
1666 return JimParseBrace(pc
);
1668 if (*pc
->p
== '"') {
1672 /* In case the end quote is missing */
1673 pc
->missing
.line
= pc
->tline
;
1677 pc
->tline
= pc
->linenr
;
1681 pc
->missing
.ch
= '"';
1683 pc
->tend
= pc
->p
- 1;
1684 pc
->tt
= JIM_TT_ESC
;
1689 if (!pc
->inquote
&& *(pc
->p
+ 1) == '\n') {
1690 pc
->tend
= pc
->p
- 1;
1691 pc
->tt
= JIM_TT_ESC
;
1695 if (*(pc
->p
+ 1) == '\n') {
1701 else if (pc
->len
== 1) {
1702 /* End of script with trailing backslash */
1703 pc
->missing
.ch
= '\\';
1707 /* If the following token is not '$' just keep going */
1708 if (pc
->len
> 1 && pc
->p
[1] != '$') {
1713 /* Only need a separate ')' token if the previous was a var */
1714 if (*pc
->p
== '(' || pc
->tt
== JIM_TT_VAR
) {
1715 if (pc
->p
== pc
->tstart
) {
1716 /* At the start of the token, so just return this char */
1720 pc
->tend
= pc
->p
- 1;
1721 pc
->tt
= JIM_TT_ESC
;
1728 pc
->tend
= pc
->p
- 1;
1729 pc
->tt
= JIM_TT_ESC
;
1738 pc
->tend
= pc
->p
- 1;
1739 pc
->tt
= JIM_TT_ESC
;
1742 else if (*pc
->p
== '\n') {
1748 pc
->tend
= pc
->p
- 1;
1749 pc
->tt
= JIM_TT_ESC
;
1760 return JIM_OK
; /* unreached */
1763 static int JimParseComment(struct JimParserCtx
*pc
)
1766 if (*pc
->p
== '\\') {
1770 pc
->missing
.ch
= '\\';
1773 if (*pc
->p
== '\n') {
1777 else if (*pc
->p
== '\n') {
1789 /* xdigitval and odigitval are helper functions for JimEscape() */
1790 static int xdigitval(int c
)
1792 if (c
>= '0' && c
<= '9')
1794 if (c
>= 'a' && c
<= 'f')
1795 return c
- 'a' + 10;
1796 if (c
>= 'A' && c
<= 'F')
1797 return c
- 'A' + 10;
1801 static int odigitval(int c
)
1803 if (c
>= '0' && c
<= '7')
1808 /* Perform Tcl escape substitution of 's', storing the result
1809 * string into 'dest'. The escaped string is guaranteed to
1810 * be the same length or shorter than the source string.
1811 * slen is the length of the string at 's'.
1813 * The function returns the length of the resulting string. */
1814 static int JimEscape(char *dest
, const char *s
, int slen
)
1819 for (i
= 0; i
< slen
; i
++) {
1850 /* A unicode or hex sequence.
1851 * \x Expect 1-2 hex chars and convert to hex.
1852 * \u Expect 1-4 hex chars and convert to utf-8.
1853 * \U Expect 1-8 hex chars and convert to utf-8.
1854 * \u{NNN} supports 1-6 hex chars and convert to utf-8.
1855 * An invalid sequence means simply the escaped char.
1867 else if (s
[i
] == 'u') {
1868 if (s
[i
+ 1] == '{') {
1877 for (k
= 0; k
< maxchars
; k
++) {
1878 int c
= xdigitval(s
[i
+ k
+ 1]);
1882 val
= (val
<< 4) | c
;
1884 /* The \u{nnn} syntax supports up to 21 bit codepoints. */
1886 if (k
== 0 || val
> 0x1fffff || s
[i
+ k
+ 1] != '}') {
1892 /* Skip the closing brace */
1897 /* Got a valid sequence, so convert */
1902 p
+= utf8_fromunicode(p
, val
);
1907 /* Not a valid codepoint, just an escaped char */
1920 /* Replace all spaces and tabs after backslash newline with a single space*/
1924 } while (s
[i
+ 1] == ' ' || s
[i
+ 1] == '\t');
1937 int c
= odigitval(s
[i
+ 1]);
1940 c
= odigitval(s
[i
+ 2]);
1946 val
= (val
* 8) + c
;
1947 c
= odigitval(s
[i
+ 3]);
1953 val
= (val
* 8) + c
;
1974 /* Returns a dynamically allocated copy of the current token in the
1975 * parser context. The function performs conversion of escapes if
1976 * the token is of type JIM_TT_ESC.
1978 * Note that after the conversion, tokens that are grouped with
1979 * braces in the source code, are always recognizable from the
1980 * identical string obtained in a different way from the type.
1982 * For example the string:
1986 * will return as first token "*", of type JIM_TT_STR
1992 * will return as first token "*", of type JIM_TT_ESC
1994 static Jim_Obj
*JimParserGetTokenObj(Jim_Interp
*interp
, struct JimParserCtx
*pc
)
1996 const char *start
, *end
;
2002 len
= (end
- start
) + 1;
2006 token
= Jim_Alloc(len
+ 1);
2007 if (pc
->tt
!= JIM_TT_ESC
) {
2008 /* No escape conversion needed? Just copy it. */
2009 memcpy(token
, start
, len
);
2013 /* Else convert the escape chars. */
2014 len
= JimEscape(token
, start
, len
);
2017 return Jim_NewStringObjNoAlloc(interp
, token
, len
);
2020 /* -----------------------------------------------------------------------------
2022 * ---------------------------------------------------------------------------*/
2023 static int JimParseListSep(struct JimParserCtx
*pc
);
2024 static int JimParseListStr(struct JimParserCtx
*pc
);
2025 static int JimParseListQuote(struct JimParserCtx
*pc
);
2027 static int JimParseList(struct JimParserCtx
*pc
)
2029 if (isspace(UCHAR(*pc
->p
))) {
2030 return JimParseListSep(pc
);
2034 return JimParseListQuote(pc
);
2037 return JimParseBrace(pc
);
2041 return JimParseListStr(pc
);
2046 pc
->tstart
= pc
->tend
= pc
->p
;
2047 pc
->tline
= pc
->linenr
;
2048 pc
->tt
= JIM_TT_EOL
;
2053 static int JimParseListSep(struct JimParserCtx
*pc
)
2056 pc
->tline
= pc
->linenr
;
2057 while (isspace(UCHAR(*pc
->p
))) {
2058 if (*pc
->p
== '\n') {
2064 pc
->tend
= pc
->p
- 1;
2065 pc
->tt
= JIM_TT_SEP
;
2069 static int JimParseListQuote(struct JimParserCtx
*pc
)
2075 pc
->tline
= pc
->linenr
;
2076 pc
->tt
= JIM_TT_STR
;
2081 pc
->tt
= JIM_TT_ESC
;
2082 if (--pc
->len
== 0) {
2083 /* Trailing backslash */
2093 pc
->tend
= pc
->p
- 1;
2102 pc
->tend
= pc
->p
- 1;
2106 static int JimParseListStr(struct JimParserCtx
*pc
)
2109 pc
->tline
= pc
->linenr
;
2110 pc
->tt
= JIM_TT_STR
;
2113 if (isspace(UCHAR(*pc
->p
))) {
2114 pc
->tend
= pc
->p
- 1;
2117 if (*pc
->p
== '\\') {
2118 if (--pc
->len
== 0) {
2119 /* Trailing backslash */
2123 pc
->tt
= JIM_TT_ESC
;
2129 pc
->tend
= pc
->p
- 1;
2133 /* -----------------------------------------------------------------------------
2134 * Jim_Obj related functions
2135 * ---------------------------------------------------------------------------*/
2137 /* Return a new initialized object. */
2138 Jim_Obj
*Jim_NewObj(Jim_Interp
*interp
)
2142 /* -- Check if there are objects in the free list -- */
2143 if (interp
->freeList
!= NULL
) {
2144 /* -- Unlink the object from the free list -- */
2145 objPtr
= interp
->freeList
;
2146 interp
->freeList
= objPtr
->nextObjPtr
;
2149 /* -- No ready to use objects: allocate a new one -- */
2150 objPtr
= Jim_Alloc(sizeof(*objPtr
));
2153 /* Object is returned with refCount of 0. Every
2154 * kind of GC implemented should take care to avoid
2155 * scanning objects with refCount == 0. */
2156 objPtr
->refCount
= 0;
2157 /* All the other fields are left uninitialized to save time.
2158 * The caller will probably want to set them to the right
2161 /* -- Put the object into the live list -- */
2162 objPtr
->prevObjPtr
= NULL
;
2163 objPtr
->nextObjPtr
= interp
->liveList
;
2164 if (interp
->liveList
)
2165 interp
->liveList
->prevObjPtr
= objPtr
;
2166 interp
->liveList
= objPtr
;
2171 /* Free an object. Actually objects are never freed, but
2172 * just moved to the free objects list, where they will be
2173 * reused by Jim_NewObj(). */
2174 void Jim_FreeObj(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
2176 /* Check if the object was already freed, panic. */
2177 JimPanic((objPtr
->refCount
!= 0, "!!!Object %p freed with bad refcount %d, type=%s", objPtr
,
2178 objPtr
->refCount
, objPtr
->typePtr
? objPtr
->typePtr
->name
: "<none>"));
2180 /* Free the internal representation */
2181 Jim_FreeIntRep(interp
, objPtr
);
2182 /* Free the string representation */
2183 if (objPtr
->bytes
!= NULL
) {
2184 if (objPtr
->bytes
!= JimEmptyStringRep
)
2185 Jim_Free(objPtr
->bytes
);
2187 /* Unlink the object from the live objects list */
2188 if (objPtr
->prevObjPtr
)
2189 objPtr
->prevObjPtr
->nextObjPtr
= objPtr
->nextObjPtr
;
2190 if (objPtr
->nextObjPtr
)
2191 objPtr
->nextObjPtr
->prevObjPtr
= objPtr
->prevObjPtr
;
2192 if (interp
->liveList
== objPtr
)
2193 interp
->liveList
= objPtr
->nextObjPtr
;
2194 #ifdef JIM_DISABLE_OBJECT_POOL
2197 /* Link the object into the free objects list */
2198 objPtr
->prevObjPtr
= NULL
;
2199 objPtr
->nextObjPtr
= interp
->freeList
;
2200 if (interp
->freeList
)
2201 interp
->freeList
->prevObjPtr
= objPtr
;
2202 interp
->freeList
= objPtr
;
2203 objPtr
->refCount
= -1;
2207 /* Invalidate the string representation of an object. */
2208 void Jim_InvalidateStringRep(Jim_Obj
*objPtr
)
2210 if (objPtr
->bytes
!= NULL
) {
2211 if (objPtr
->bytes
!= JimEmptyStringRep
)
2212 Jim_Free(objPtr
->bytes
);
2214 objPtr
->bytes
= NULL
;
2217 /* Duplicate an object. The returned object has refcount = 0. */
2218 Jim_Obj
*Jim_DuplicateObj(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
2222 dupPtr
= Jim_NewObj(interp
);
2223 if (objPtr
->bytes
== NULL
) {
2224 /* Object does not have a valid string representation. */
2225 dupPtr
->bytes
= NULL
;
2227 else if (objPtr
->length
== 0) {
2228 /* Zero length, so don't even bother with the type-specific dup,
2229 * since all zero length objects look the same
2231 dupPtr
->bytes
= JimEmptyStringRep
;
2233 dupPtr
->typePtr
= NULL
;
2237 dupPtr
->bytes
= Jim_Alloc(objPtr
->length
+ 1);
2238 dupPtr
->length
= objPtr
->length
;
2239 /* Copy the null byte too */
2240 memcpy(dupPtr
->bytes
, objPtr
->bytes
, objPtr
->length
+ 1);
2243 /* By default, the new object has the same type as the old object */
2244 dupPtr
->typePtr
= objPtr
->typePtr
;
2245 if (objPtr
->typePtr
!= NULL
) {
2246 if (objPtr
->typePtr
->dupIntRepProc
== NULL
) {
2247 dupPtr
->internalRep
= objPtr
->internalRep
;
2250 /* The dup proc may set a different type, e.g. NULL */
2251 objPtr
->typePtr
->dupIntRepProc(interp
, objPtr
, dupPtr
);
2257 /* Return the string representation for objPtr. If the object's
2258 * string representation is invalid, calls the updateStringProc method to create
2259 * a new one from the internal representation of the object.
2261 const char *Jim_GetString(Jim_Obj
*objPtr
, int *lenPtr
)
2263 if (objPtr
->bytes
== NULL
) {
2264 /* Invalid string repr. Generate it. */
2265 JimPanic((objPtr
->typePtr
->updateStringProc
== NULL
, "UpdateStringProc called against '%s' type.", objPtr
->typePtr
->name
));
2266 objPtr
->typePtr
->updateStringProc(objPtr
);
2269 *lenPtr
= objPtr
->length
;
2270 return objPtr
->bytes
;
2273 /* Just returns the length (in bytes) of the object's string rep */
2274 int Jim_Length(Jim_Obj
*objPtr
)
2276 if (objPtr
->bytes
== NULL
) {
2277 /* Invalid string repr. Generate it. */
2278 Jim_GetString(objPtr
, NULL
);
2280 return objPtr
->length
;
2283 /* Just returns object's string rep */
2284 const char *Jim_String(Jim_Obj
*objPtr
)
2286 if (objPtr
->bytes
== NULL
) {
2287 /* Invalid string repr. Generate it. */
2288 Jim_GetString(objPtr
, NULL
);
2290 return objPtr
->bytes
;
2293 static void JimSetStringBytes(Jim_Obj
*objPtr
, const char *str
)
2295 objPtr
->bytes
= Jim_StrDup(str
);
2296 objPtr
->length
= strlen(str
);
2299 static void FreeDictSubstInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
);
2300 static void DupDictSubstInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
);
2302 static const Jim_ObjType dictSubstObjType
= {
2303 "dict-substitution",
2304 FreeDictSubstInternalRep
,
2305 DupDictSubstInternalRep
,
2310 static void FreeInterpolatedInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
);
2311 static void DupInterpolatedInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
);
2313 static const Jim_ObjType interpolatedObjType
= {
2315 FreeInterpolatedInternalRep
,
2316 DupInterpolatedInternalRep
,
2321 static void FreeInterpolatedInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
2323 Jim_DecrRefCount(interp
, objPtr
->internalRep
.dictSubstValue
.indexObjPtr
);
2326 static void DupInterpolatedInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
)
2328 /* Copy the interal rep */
2329 dupPtr
->internalRep
= srcPtr
->internalRep
;
2330 /* Need to increment the key ref count */
2331 Jim_IncrRefCount(dupPtr
->internalRep
.dictSubstValue
.indexObjPtr
);
2334 /* -----------------------------------------------------------------------------
2336 * ---------------------------------------------------------------------------*/
2337 static void DupStringInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
);
2338 static int SetStringFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
);
2340 static const Jim_ObjType stringObjType
= {
2343 DupStringInternalRep
,
2345 JIM_TYPE_REFERENCES
,
2348 static void DupStringInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
)
2350 JIM_NOTUSED(interp
);
2352 /* This is a bit subtle: the only caller of this function
2353 * should be Jim_DuplicateObj(), that will copy the
2354 * string representaion. After the copy, the duplicated
2355 * object will not have more room in the buffer than
2356 * srcPtr->length bytes. So we just set it to length. */
2357 dupPtr
->internalRep
.strValue
.maxLength
= srcPtr
->length
;
2358 dupPtr
->internalRep
.strValue
.charLength
= srcPtr
->internalRep
.strValue
.charLength
;
2361 static int SetStringFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
2363 if (objPtr
->typePtr
!= &stringObjType
) {
2364 /* Get a fresh string representation. */
2365 if (objPtr
->bytes
== NULL
) {
2366 /* Invalid string repr. Generate it. */
2367 JimPanic((objPtr
->typePtr
->updateStringProc
== NULL
, "UpdateStringProc called against '%s' type.", objPtr
->typePtr
->name
));
2368 objPtr
->typePtr
->updateStringProc(objPtr
);
2370 /* Free any other internal representation. */
2371 Jim_FreeIntRep(interp
, objPtr
);
2372 /* Set it as string, i.e. just set the maxLength field. */
2373 objPtr
->typePtr
= &stringObjType
;
2374 objPtr
->internalRep
.strValue
.maxLength
= objPtr
->length
;
2375 /* Don't know the utf-8 length yet */
2376 objPtr
->internalRep
.strValue
.charLength
= -1;
2382 * Returns the length of the object string in chars, not bytes.
2384 * These may be different for a utf-8 string.
2386 int Jim_Utf8Length(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
2389 SetStringFromAny(interp
, objPtr
);
2391 if (objPtr
->internalRep
.strValue
.charLength
< 0) {
2392 objPtr
->internalRep
.strValue
.charLength
= utf8_strlen(objPtr
->bytes
, objPtr
->length
);
2394 return objPtr
->internalRep
.strValue
.charLength
;
2396 return Jim_Length(objPtr
);
2400 /* len is in bytes -- see also Jim_NewStringObjUtf8() */
2401 Jim_Obj
*Jim_NewStringObj(Jim_Interp
*interp
, const char *s
, int len
)
2403 Jim_Obj
*objPtr
= Jim_NewObj(interp
);
2405 /* Need to find out how many bytes the string requires */
2408 /* Alloc/Set the string rep. */
2410 objPtr
->bytes
= JimEmptyStringRep
;
2413 objPtr
->bytes
= Jim_StrDupLen(s
, len
);
2415 objPtr
->length
= len
;
2417 /* No typePtr field for the vanilla string object. */
2418 objPtr
->typePtr
= NULL
;
2422 /* charlen is in characters -- see also Jim_NewStringObj() */
2423 Jim_Obj
*Jim_NewStringObjUtf8(Jim_Interp
*interp
, const char *s
, int charlen
)
2426 /* Need to find out how many bytes the string requires */
2427 int bytelen
= utf8_index(s
, charlen
);
2429 Jim_Obj
*objPtr
= Jim_NewStringObj(interp
, s
, bytelen
);
2431 /* Remember the utf8 length, so set the type */
2432 objPtr
->typePtr
= &stringObjType
;
2433 objPtr
->internalRep
.strValue
.maxLength
= bytelen
;
2434 objPtr
->internalRep
.strValue
.charLength
= charlen
;
2438 return Jim_NewStringObj(interp
, s
, charlen
);
2442 /* This version does not try to duplicate the 's' pointer, but
2443 * use it directly. */
2444 Jim_Obj
*Jim_NewStringObjNoAlloc(Jim_Interp
*interp
, char *s
, int len
)
2446 Jim_Obj
*objPtr
= Jim_NewObj(interp
);
2449 objPtr
->length
= (len
== -1) ? strlen(s
) : len
;
2450 objPtr
->typePtr
= NULL
;
2454 /* Low-level string append. Use it only against unshared objects
2455 * of type "string". */
2456 static void StringAppendString(Jim_Obj
*objPtr
, const char *str
, int len
)
2462 needlen
= objPtr
->length
+ len
;
2463 if (objPtr
->internalRep
.strValue
.maxLength
< needlen
||
2464 objPtr
->internalRep
.strValue
.maxLength
== 0) {
2466 /* Inefficient to malloc() for less than 8 bytes */
2470 if (objPtr
->bytes
== JimEmptyStringRep
) {
2471 objPtr
->bytes
= Jim_Alloc(needlen
+ 1);
2474 objPtr
->bytes
= Jim_Realloc(objPtr
->bytes
, needlen
+ 1);
2476 objPtr
->internalRep
.strValue
.maxLength
= needlen
;
2478 memcpy(objPtr
->bytes
+ objPtr
->length
, str
, len
);
2479 objPtr
->bytes
[objPtr
->length
+ len
] = '\0';
2481 if (objPtr
->internalRep
.strValue
.charLength
>= 0) {
2482 /* Update the utf-8 char length */
2483 objPtr
->internalRep
.strValue
.charLength
+= utf8_strlen(objPtr
->bytes
+ objPtr
->length
, len
);
2485 objPtr
->length
+= len
;
2488 /* Higher level API to append strings to objects.
2489 * Object must not be unshared for each of these.
2491 void Jim_AppendString(Jim_Interp
*interp
, Jim_Obj
*objPtr
, const char *str
, int len
)
2493 JimPanic((Jim_IsShared(objPtr
), "Jim_AppendString called with shared object"));
2494 SetStringFromAny(interp
, objPtr
);
2495 StringAppendString(objPtr
, str
, len
);
2498 void Jim_AppendObj(Jim_Interp
*interp
, Jim_Obj
*objPtr
, Jim_Obj
*appendObjPtr
)
2501 const char *str
= Jim_GetString(appendObjPtr
, &len
);
2502 Jim_AppendString(interp
, objPtr
, str
, len
);
2505 void Jim_AppendStrings(Jim_Interp
*interp
, Jim_Obj
*objPtr
, ...)
2509 SetStringFromAny(interp
, objPtr
);
2510 va_start(ap
, objPtr
);
2512 const char *s
= va_arg(ap
, const char *);
2516 Jim_AppendString(interp
, objPtr
, s
, -1);
2521 int Jim_StringEqObj(Jim_Obj
*aObjPtr
, Jim_Obj
*bObjPtr
)
2523 if (aObjPtr
== bObjPtr
) {
2528 const char *sA
= Jim_GetString(aObjPtr
, &Alen
);
2529 const char *sB
= Jim_GetString(bObjPtr
, &Blen
);
2531 return Alen
== Blen
&& memcmp(sA
, sB
, Alen
) == 0;
2536 * Note. Does not support embedded nulls in either the pattern or the object.
2538 int Jim_StringMatchObj(Jim_Interp
*interp
, Jim_Obj
*patternObjPtr
, Jim_Obj
*objPtr
, int nocase
)
2540 return JimGlobMatch(Jim_String(patternObjPtr
), Jim_String(objPtr
), nocase
);
2544 * Note: does not support embedded nulls for the nocase option.
2546 int Jim_StringCompareObj(Jim_Interp
*interp
, Jim_Obj
*firstObjPtr
, Jim_Obj
*secondObjPtr
, int nocase
)
2549 const char *s1
= Jim_GetString(firstObjPtr
, &l1
);
2550 const char *s2
= Jim_GetString(secondObjPtr
, &l2
);
2553 /* Do a character compare for nocase */
2554 return JimStringCompareLen(s1
, s2
, -1, nocase
);
2556 return JimStringCompare(s1
, l1
, s2
, l2
);
2560 * Like Jim_StringCompareObj() except compares to a maximum of the length of firstObjPtr.
2562 * Note: does not support embedded nulls
2564 int Jim_StringCompareLenObj(Jim_Interp
*interp
, Jim_Obj
*firstObjPtr
, Jim_Obj
*secondObjPtr
, int nocase
)
2566 const char *s1
= Jim_String(firstObjPtr
);
2567 const char *s2
= Jim_String(secondObjPtr
);
2569 return JimStringCompareLen(s1
, s2
, Jim_Utf8Length(interp
, firstObjPtr
), nocase
);
2572 /* Convert a range, as returned by Jim_GetRange(), into
2573 * an absolute index into an object of the specified length.
2574 * This function may return negative values, or values
2575 * greater than or equal to the length of the list if the index
2576 * is out of range. */
2577 static int JimRelToAbsIndex(int len
, int idx
)
2584 /* Convert a pair of indexes (*firstPtr, *lastPtr) as normalized by JimRelToAbsIndex(),
2585 * into a form suitable for implementation of commands like [string range] and [lrange].
2587 * The resulting range is guaranteed to address valid elements of
2590 static void JimRelToAbsRange(int len
, int *firstPtr
, int *lastPtr
, int *rangeLenPtr
)
2594 if (*firstPtr
> *lastPtr
) {
2598 rangeLen
= *lastPtr
- *firstPtr
+ 1;
2600 if (*firstPtr
< 0) {
2601 rangeLen
+= *firstPtr
;
2604 if (*lastPtr
>= len
) {
2605 rangeLen
-= (*lastPtr
- (len
- 1));
2613 *rangeLenPtr
= rangeLen
;
2616 static int JimStringGetRange(Jim_Interp
*interp
, Jim_Obj
*firstObjPtr
, Jim_Obj
*lastObjPtr
,
2617 int len
, int *first
, int *last
, int *range
)
2619 if (Jim_GetIndex(interp
, firstObjPtr
, first
) != JIM_OK
) {
2622 if (Jim_GetIndex(interp
, lastObjPtr
, last
) != JIM_OK
) {
2625 *first
= JimRelToAbsIndex(len
, *first
);
2626 *last
= JimRelToAbsIndex(len
, *last
);
2627 JimRelToAbsRange(len
, first
, last
, range
);
2631 Jim_Obj
*Jim_StringByteRangeObj(Jim_Interp
*interp
,
2632 Jim_Obj
*strObjPtr
, Jim_Obj
*firstObjPtr
, Jim_Obj
*lastObjPtr
)
2639 str
= Jim_GetString(strObjPtr
, &bytelen
);
2641 if (JimStringGetRange(interp
, firstObjPtr
, lastObjPtr
, bytelen
, &first
, &last
, &rangeLen
) != JIM_OK
) {
2645 if (first
== 0 && rangeLen
== bytelen
) {
2648 return Jim_NewStringObj(interp
, str
+ first
, rangeLen
);
2651 Jim_Obj
*Jim_StringRangeObj(Jim_Interp
*interp
,
2652 Jim_Obj
*strObjPtr
, Jim_Obj
*firstObjPtr
, Jim_Obj
*lastObjPtr
)
2660 str
= Jim_GetString(strObjPtr
, &bytelen
);
2661 len
= Jim_Utf8Length(interp
, strObjPtr
);
2663 if (JimStringGetRange(interp
, firstObjPtr
, lastObjPtr
, len
, &first
, &last
, &rangeLen
) != JIM_OK
) {
2667 if (first
== 0 && rangeLen
== len
) {
2670 if (len
== bytelen
) {
2671 /* ASCII optimisation */
2672 return Jim_NewStringObj(interp
, str
+ first
, rangeLen
);
2674 return Jim_NewStringObjUtf8(interp
, str
+ utf8_index(str
, first
), rangeLen
);
2676 return Jim_StringByteRangeObj(interp
, strObjPtr
, firstObjPtr
, lastObjPtr
);
2680 Jim_Obj
*JimStringReplaceObj(Jim_Interp
*interp
,
2681 Jim_Obj
*strObjPtr
, Jim_Obj
*firstObjPtr
, Jim_Obj
*lastObjPtr
, Jim_Obj
*newStrObj
)
2688 len
= Jim_Utf8Length(interp
, strObjPtr
);
2690 if (JimStringGetRange(interp
, firstObjPtr
, lastObjPtr
, len
, &first
, &last
, &rangeLen
) != JIM_OK
) {
2698 str
= Jim_String(strObjPtr
);
2701 objPtr
= Jim_NewStringObjUtf8(interp
, str
, first
);
2705 Jim_AppendObj(interp
, objPtr
, newStrObj
);
2709 Jim_AppendString(interp
, objPtr
, str
+ utf8_index(str
, last
+ 1), len
- last
- 1);
2715 * Note: does not support embedded nulls.
2717 static void JimStrCopyUpperLower(char *dest
, const char *str
, int uc
)
2721 str
+= utf8_tounicode(str
, &c
);
2722 dest
+= utf8_getchars(dest
, uc
? utf8_upper(c
) : utf8_lower(c
));
2728 * Note: does not support embedded nulls.
2730 static Jim_Obj
*JimStringToLower(Jim_Interp
*interp
, Jim_Obj
*strObjPtr
)
2736 str
= Jim_GetString(strObjPtr
, &len
);
2739 /* Case mapping can change the utf-8 length of the string.
2740 * But at worst it will be by one extra byte per char
2744 buf
= Jim_Alloc(len
+ 1);
2745 JimStrCopyUpperLower(buf
, str
, 0);
2746 return Jim_NewStringObjNoAlloc(interp
, buf
, -1);
2750 * Note: does not support embedded nulls.
2752 static Jim_Obj
*JimStringToUpper(Jim_Interp
*interp
, Jim_Obj
*strObjPtr
)
2758 str
= Jim_GetString(strObjPtr
, &len
);
2761 /* Case mapping can change the utf-8 length of the string.
2762 * But at worst it will be by one extra byte per char
2766 buf
= Jim_Alloc(len
+ 1);
2767 JimStrCopyUpperLower(buf
, str
, 1);
2768 return Jim_NewStringObjNoAlloc(interp
, buf
, -1);
2772 * Note: does not support embedded nulls.
2774 static Jim_Obj
*JimStringToTitle(Jim_Interp
*interp
, Jim_Obj
*strObjPtr
)
2781 str
= Jim_GetString(strObjPtr
, &len
);
2784 /* Case mapping can change the utf-8 length of the string.
2785 * But at worst it will be by one extra byte per char
2789 buf
= p
= Jim_Alloc(len
+ 1);
2791 str
+= utf8_tounicode(str
, &c
);
2792 p
+= utf8_getchars(p
, utf8_title(c
));
2794 JimStrCopyUpperLower(p
, str
, 0);
2796 return Jim_NewStringObjNoAlloc(interp
, buf
, -1);
2799 /* Similar to memchr() except searches a UTF-8 string 'str' of byte length 'len'
2800 * for unicode character 'c'.
2801 * Returns the position if found or NULL if not
2803 static const char *utf8_memchr(const char *str
, int len
, int c
)
2808 int n
= utf8_tounicode(str
, &sc
);
2817 return memchr(str
, c
, len
);
2822 * Searches for the first non-trim char in string (str, len)
2824 * If none is found, returns just past the last char.
2826 * Lengths are in bytes.
2828 static const char *JimFindTrimLeft(const char *str
, int len
, const char *trimchars
, int trimlen
)
2832 int n
= utf8_tounicode(str
, &c
);
2834 if (utf8_memchr(trimchars
, trimlen
, c
) == NULL
) {
2835 /* Not a trim char, so stop */
2845 * Searches backwards for a non-trim char in string (str, len).
2847 * Returns a pointer to just after the non-trim char, or NULL if not found.
2849 * Lengths are in bytes.
2851 static const char *JimFindTrimRight(const char *str
, int len
, const char *trimchars
, int trimlen
)
2857 int n
= utf8_prev_len(str
, len
);
2862 n
= utf8_tounicode(str
, &c
);
2864 if (utf8_memchr(trimchars
, trimlen
, c
) == NULL
) {
2872 static const char default_trim_chars
[] = " \t\n\r";
2873 /* sizeof() here includes the null byte */
2874 static int default_trim_chars_len
= sizeof(default_trim_chars
);
2876 static Jim_Obj
*JimStringTrimLeft(Jim_Interp
*interp
, Jim_Obj
*strObjPtr
, Jim_Obj
*trimcharsObjPtr
)
2879 const char *str
= Jim_GetString(strObjPtr
, &len
);
2880 const char *trimchars
= default_trim_chars
;
2881 int trimcharslen
= default_trim_chars_len
;
2884 if (trimcharsObjPtr
) {
2885 trimchars
= Jim_GetString(trimcharsObjPtr
, &trimcharslen
);
2888 newstr
= JimFindTrimLeft(str
, len
, trimchars
, trimcharslen
);
2889 if (newstr
== str
) {
2893 return Jim_NewStringObj(interp
, newstr
, len
- (newstr
- str
));
2896 static Jim_Obj
*JimStringTrimRight(Jim_Interp
*interp
, Jim_Obj
*strObjPtr
, Jim_Obj
*trimcharsObjPtr
)
2899 const char *trimchars
= default_trim_chars
;
2900 int trimcharslen
= default_trim_chars_len
;
2901 const char *nontrim
;
2903 if (trimcharsObjPtr
) {
2904 trimchars
= Jim_GetString(trimcharsObjPtr
, &trimcharslen
);
2907 SetStringFromAny(interp
, strObjPtr
);
2909 len
= Jim_Length(strObjPtr
);
2910 nontrim
= JimFindTrimRight(strObjPtr
->bytes
, len
, trimchars
, trimcharslen
);
2912 if (nontrim
== NULL
) {
2913 /* All trim, so return a zero-length string */
2914 return Jim_NewEmptyStringObj(interp
);
2916 if (nontrim
== strObjPtr
->bytes
+ len
) {
2917 /* All non-trim, so return the original object */
2921 if (Jim_IsShared(strObjPtr
)) {
2922 strObjPtr
= Jim_NewStringObj(interp
, strObjPtr
->bytes
, (nontrim
- strObjPtr
->bytes
));
2925 /* Can modify this string in place */
2926 strObjPtr
->bytes
[nontrim
- strObjPtr
->bytes
] = 0;
2927 strObjPtr
->length
= (nontrim
- strObjPtr
->bytes
);
2933 static Jim_Obj
*JimStringTrim(Jim_Interp
*interp
, Jim_Obj
*strObjPtr
, Jim_Obj
*trimcharsObjPtr
)
2935 /* First trim left. */
2936 Jim_Obj
*objPtr
= JimStringTrimLeft(interp
, strObjPtr
, trimcharsObjPtr
);
2938 /* Now trim right */
2939 strObjPtr
= JimStringTrimRight(interp
, objPtr
, trimcharsObjPtr
);
2941 /* Note: refCount check is needed since objPtr may be emptyObj */
2942 if (objPtr
!= strObjPtr
&& objPtr
->refCount
== 0) {
2943 /* We don't want this object to be leaked */
2944 Jim_FreeNewObj(interp
, objPtr
);
2950 /* Some platforms don't have isascii - need a non-macro version */
2952 #define jim_isascii isascii
2954 static int jim_isascii(int c
)
2956 return !(c
& ~0x7f);
2960 static int JimStringIs(Jim_Interp
*interp
, Jim_Obj
*strObjPtr
, Jim_Obj
*strClass
, int strict
)
2962 static const char * const strclassnames
[] = {
2963 "integer", "alpha", "alnum", "ascii", "digit",
2964 "double", "lower", "upper", "space", "xdigit",
2965 "control", "print", "graph", "punct", "boolean",
2969 STR_IS_INTEGER
, STR_IS_ALPHA
, STR_IS_ALNUM
, STR_IS_ASCII
, STR_IS_DIGIT
,
2970 STR_IS_DOUBLE
, STR_IS_LOWER
, STR_IS_UPPER
, STR_IS_SPACE
, STR_IS_XDIGIT
,
2971 STR_IS_CONTROL
, STR_IS_PRINT
, STR_IS_GRAPH
, STR_IS_PUNCT
, STR_IS_BOOLEAN
,
2977 int (*isclassfunc
)(int c
) = NULL
;
2979 if (Jim_GetEnum(interp
, strClass
, strclassnames
, &strclass
, "class", JIM_ERRMSG
| JIM_ENUM_ABBREV
) != JIM_OK
) {
2983 str
= Jim_GetString(strObjPtr
, &len
);
2985 Jim_SetResultBool(interp
, !strict
);
2990 case STR_IS_INTEGER
:
2993 Jim_SetResultBool(interp
, JimGetWideNoErr(interp
, strObjPtr
, &w
) == JIM_OK
);
3000 Jim_SetResultBool(interp
, Jim_GetDouble(interp
, strObjPtr
, &d
) == JIM_OK
&& errno
!= ERANGE
);
3004 case STR_IS_BOOLEAN
:
3007 Jim_SetResultBool(interp
, Jim_GetBoolean(interp
, strObjPtr
, &b
) == JIM_OK
);
3011 case STR_IS_ALPHA
: isclassfunc
= isalpha
; break;
3012 case STR_IS_ALNUM
: isclassfunc
= isalnum
; break;
3013 case STR_IS_ASCII
: isclassfunc
= jim_isascii
; break;
3014 case STR_IS_DIGIT
: isclassfunc
= isdigit
; break;
3015 case STR_IS_LOWER
: isclassfunc
= islower
; break;
3016 case STR_IS_UPPER
: isclassfunc
= isupper
; break;
3017 case STR_IS_SPACE
: isclassfunc
= isspace
; break;
3018 case STR_IS_XDIGIT
: isclassfunc
= isxdigit
; break;
3019 case STR_IS_CONTROL
: isclassfunc
= iscntrl
; break;
3020 case STR_IS_PRINT
: isclassfunc
= isprint
; break;
3021 case STR_IS_GRAPH
: isclassfunc
= isgraph
; break;
3022 case STR_IS_PUNCT
: isclassfunc
= ispunct
; break;
3027 for (i
= 0; i
< len
; i
++) {
3028 if (!isclassfunc(UCHAR(str
[i
]))) {
3029 Jim_SetResultBool(interp
, 0);
3033 Jim_SetResultBool(interp
, 1);
3037 /* -----------------------------------------------------------------------------
3038 * Compared String Object
3039 * ---------------------------------------------------------------------------*/
3041 /* This is strange object that allows comparison of a C literal string
3042 * with a Jim object in a very short time if the same comparison is done
3043 * multiple times. For example every time the [if] command is executed,
3044 * Jim has to check if a given argument is "else".
3045 * If the code has no errors, this comparison is true most of the time,
3046 * so we can cache the pointer of the string of the last matching
3047 * comparison inside the object. Because most C compilers perform literal sharing,
3048 * so that: char *x = "foo", char *y = "foo", will lead to x == y,
3049 * this works pretty well even if comparisons are at different places
3050 * inside the C code. */
3052 static const Jim_ObjType comparedStringObjType
= {
3057 JIM_TYPE_REFERENCES
,
3060 /* The only way this object is exposed to the API is via the following
3061 * function. Returns true if the string and the object string repr.
3062 * are the same, otherwise zero is returned.
3064 * Note: this isn't binary safe, but it hardly needs to be.*/
3065 int Jim_CompareStringImmediate(Jim_Interp
*interp
, Jim_Obj
*objPtr
, const char *str
)
3067 if (objPtr
->typePtr
== &comparedStringObjType
&& objPtr
->internalRep
.ptr
== str
) {
3071 if (strcmp(str
, Jim_String(objPtr
)) != 0)
3074 if (objPtr
->typePtr
!= &comparedStringObjType
) {
3075 Jim_FreeIntRep(interp
, objPtr
);
3076 objPtr
->typePtr
= &comparedStringObjType
;
3078 objPtr
->internalRep
.ptr
= (char *)str
; /*ATTENTION: const cast */
3083 static int qsortCompareStringPointers(const void *a
, const void *b
)
3085 char *const *sa
= (char *const *)a
;
3086 char *const *sb
= (char *const *)b
;
3088 return strcmp(*sa
, *sb
);
3092 /* -----------------------------------------------------------------------------
3095 * This object is just a string from the language point of view, but
3096 * the internal representation contains the filename and line number
3097 * where this token was read. This information is used by
3098 * Jim_EvalObj() if the object passed happens to be of type "source".
3100 * This allows propagation of the information about line numbers and file
3101 * names and gives error messages with absolute line numbers.
3103 * Note that this object uses the internal representation of the Jim_Object,
3104 * so there is almost no memory overhead. (One Jim_Obj for each filename).
3106 * Also the object will be converted to something else if the given
3107 * token it represents in the source file is not something to be
3108 * evaluated (not a script), and will be specialized in some other way,
3109 * so the time overhead is also almost zero.
3110 * ---------------------------------------------------------------------------*/
3112 static void FreeSourceInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
);
3113 static void DupSourceInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
);
3115 static const Jim_ObjType sourceObjType
= {
3117 FreeSourceInternalRep
,
3118 DupSourceInternalRep
,
3120 JIM_TYPE_REFERENCES
,
3123 void FreeSourceInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
3125 Jim_DecrRefCount(interp
, objPtr
->internalRep
.sourceValue
.fileNameObj
);
3128 void DupSourceInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
)
3130 dupPtr
->internalRep
.sourceValue
= srcPtr
->internalRep
.sourceValue
;
3131 Jim_IncrRefCount(dupPtr
->internalRep
.sourceValue
.fileNameObj
);
3134 static void JimSetSourceInfo(Jim_Interp
*interp
, Jim_Obj
*objPtr
,
3135 Jim_Obj
*fileNameObj
, int lineNumber
)
3137 JimPanic((Jim_IsShared(objPtr
), "JimSetSourceInfo called with shared object"));
3138 JimPanic((objPtr
->typePtr
!= NULL
, "JimSetSourceInfo called with typed object"));
3139 Jim_IncrRefCount(fileNameObj
);
3140 objPtr
->internalRep
.sourceValue
.fileNameObj
= fileNameObj
;
3141 objPtr
->internalRep
.sourceValue
.lineNumber
= lineNumber
;
3142 objPtr
->typePtr
= &sourceObjType
;
3145 /* -----------------------------------------------------------------------------
3148 * This object is used only in the Script internal represenation.
3149 * For each line of the script, it holds the number of tokens on the line
3150 * and the source line number.
3152 static const Jim_ObjType scriptLineObjType
= {
3160 static Jim_Obj
*JimNewScriptLineObj(Jim_Interp
*interp
, int argc
, int line
)
3164 #ifdef DEBUG_SHOW_SCRIPT
3166 snprintf(buf
, sizeof(buf
), "line=%d, argc=%d", line
, argc
);
3167 objPtr
= Jim_NewStringObj(interp
, buf
, -1);
3169 objPtr
= Jim_NewEmptyStringObj(interp
);
3171 objPtr
->typePtr
= &scriptLineObjType
;
3172 objPtr
->internalRep
.scriptLineValue
.argc
= argc
;
3173 objPtr
->internalRep
.scriptLineValue
.line
= line
;
3178 /* -----------------------------------------------------------------------------
3181 * This object holds the parsed internal representation of a script.
3182 * This representation is help within an allocated ScriptObj (see below)
3184 static void FreeScriptInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
);
3185 static void DupScriptInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
);
3187 static const Jim_ObjType scriptObjType
= {
3189 FreeScriptInternalRep
,
3190 DupScriptInternalRep
,
3192 JIM_TYPE_REFERENCES
,
3195 /* Each token of a script is represented by a ScriptToken.
3196 * The ScriptToken contains a type and a Jim_Obj. The Jim_Obj
3197 * can be specialized by commands operating on it.
3199 typedef struct ScriptToken
3205 /* This is the script object internal representation. An array of
3206 * ScriptToken structures, including a pre-computed representation of the
3207 * command length and arguments.
3209 * For example the script:
3212 * set $i $x$y [foo]BAR
3214 * will produce a ScriptObj with the following ScriptToken's:
3229 * "puts hello" has two args (LIN 2), composed of single tokens.
3230 * (Note that the WRD token is omitted for the common case of a single token.)
3232 * "set $i $x$y [foo]BAR" has four (LIN 4) args, the first word
3233 * has 1 token (ESC SET), and the last has two tokens (WRD 2 CMD foo ESC BAR)
3235 * The precomputation of the command structure makes Jim_Eval() faster,
3236 * and simpler because there aren't dynamic lengths / allocations.
3238 * -- {expand}/{*} handling --
3240 * Expand is handled in a special way.
3242 * If a "word" begins with {*}, the word token count is -ve.
3244 * For example the command:
3248 * Will produce the following cmdstruct array:
3255 * Note that the 'LIN' token also contains the source information for the
3256 * first word of the line for error reporting purposes
3258 * -- the substFlags field of the structure --
3260 * The scriptObj structure is used to represent both "script" objects
3261 * and "subst" objects. In the second case, there are no LIN and WRD
3262 * tokens. Instead SEP and EOL tokens are added as-is.
3263 * In addition, the field 'substFlags' is used to represent the flags used to turn
3264 * the string into the internal representation.
3265 * If these flags do not match what the application requires,
3266 * the scriptObj is created again. For example the script:
3268 * subst -nocommands $string
3269 * subst -novariables $string
3271 * Will (re)create the internal representation of the $string object
3274 typedef struct ScriptObj
3276 ScriptToken
*token
; /* Tokens array. */
3277 Jim_Obj
*fileNameObj
; /* Filename */
3278 int len
; /* Length of token[] */
3279 int substFlags
; /* flags used for the compilation of "subst" objects */
3280 int inUse
; /* Used to share a ScriptObj. Currently
3281 only used by Jim_EvalObj() as protection against
3282 shimmering of the currently evaluated object. */
3283 int firstline
; /* Line number of the first line */
3284 int linenr
; /* Error line number, if any */
3285 int missing
; /* Missing char if script failed to parse, (or space or backslash if OK) */
3288 static void JimSetScriptFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
);
3289 static int JimParseCheckMissing(Jim_Interp
*interp
, int ch
);
3290 static ScriptObj
*JimGetScript(Jim_Interp
*interp
, Jim_Obj
*objPtr
);
3292 void FreeScriptInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
3295 struct ScriptObj
*script
= (void *)objPtr
->internalRep
.ptr
;
3297 if (--script
->inUse
!= 0)
3299 for (i
= 0; i
< script
->len
; i
++) {
3300 Jim_DecrRefCount(interp
, script
->token
[i
].objPtr
);
3302 Jim_Free(script
->token
);
3303 Jim_DecrRefCount(interp
, script
->fileNameObj
);
3307 void DupScriptInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
)
3309 JIM_NOTUSED(interp
);
3310 JIM_NOTUSED(srcPtr
);
3312 /* Just return a simple string. We don't try to preserve the source info
3313 * since in practice scripts are never duplicated
3315 dupPtr
->typePtr
= NULL
;
3318 /* A simple parse token.
3319 * As the script is parsed, the created tokens point into the script string rep.
3323 const char *token
; /* Pointer to the start of the token */
3324 int len
; /* Length of this token */
3325 int type
; /* Token type */
3326 int line
; /* Line number */
3329 /* A list of parsed tokens representing a script.
3330 * Tokens are added to this list as the script is parsed.
3331 * It grows as needed.
3335 /* Start with a statically allocated list of tokens which will be expanded with realloc if needed */
3336 ParseToken
*list
; /* Array of tokens */
3337 int size
; /* Current size of the list */
3338 int count
; /* Number of entries used */
3339 ParseToken static_list
[20]; /* Small initial token space to avoid allocation */
3342 static void ScriptTokenListInit(ParseTokenList
*tokenlist
)
3344 tokenlist
->list
= tokenlist
->static_list
;
3345 tokenlist
->size
= sizeof(tokenlist
->static_list
) / sizeof(ParseToken
);
3346 tokenlist
->count
= 0;
3349 static void ScriptTokenListFree(ParseTokenList
*tokenlist
)
3351 if (tokenlist
->list
!= tokenlist
->static_list
) {
3352 Jim_Free(tokenlist
->list
);
3357 * Adds the new token to the tokenlist.
3358 * The token has the given length, type and line number.
3359 * The token list is resized as necessary.
3361 static void ScriptAddToken(ParseTokenList
*tokenlist
, const char *token
, int len
, int type
,
3366 if (tokenlist
->count
== tokenlist
->size
) {
3367 /* Resize the list */
3368 tokenlist
->size
*= 2;
3369 if (tokenlist
->list
!= tokenlist
->static_list
) {
3371 Jim_Realloc(tokenlist
->list
, tokenlist
->size
* sizeof(*tokenlist
->list
));
3374 /* The list needs to become allocated */
3375 tokenlist
->list
= Jim_Alloc(tokenlist
->size
* sizeof(*tokenlist
->list
));
3376 memcpy(tokenlist
->list
, tokenlist
->static_list
,
3377 tokenlist
->count
* sizeof(*tokenlist
->list
));
3380 t
= &tokenlist
->list
[tokenlist
->count
++];
3387 /* Counts the number of adjoining non-separator tokens.
3389 * Returns -ve if the first token is the expansion
3390 * operator (in which case the count doesn't include
3393 static int JimCountWordTokens(struct ScriptObj
*script
, ParseToken
*t
)
3398 /* Is the first word {*} or {expand}? */
3399 if (t
->type
== JIM_TT_STR
&& !TOKEN_IS_SEP(t
[1].type
)) {
3400 if ((t
->len
== 1 && *t
->token
== '*') || (t
->len
== 6 && strncmp(t
->token
, "expand", 6) == 0)) {
3401 /* Create an expand token */
3406 if (script
->missing
== ' ') {
3407 /* This is a "extra characters after close-brace" error. Report the first error */
3408 script
->missing
= '}';
3409 script
->linenr
= t
[1].line
;
3414 /* Now count non-separator words */
3415 while (!TOKEN_IS_SEP(t
->type
)) {
3420 return count
* expand
;
3424 * Create a script/subst object from the given token.
3426 static Jim_Obj
*JimMakeScriptObj(Jim_Interp
*interp
, const ParseToken
*t
)
3430 if (t
->type
== JIM_TT_ESC
&& memchr(t
->token
, '\\', t
->len
) != NULL
) {
3431 /* Convert backlash escapes. The result will never be longer than the original */
3433 char *str
= Jim_Alloc(len
+ 1);
3434 len
= JimEscape(str
, t
->token
, len
);
3435 objPtr
= Jim_NewStringObjNoAlloc(interp
, str
, len
);
3438 /* XXX: For strict Tcl compatibility, JIM_TT_STR should replace <backslash><newline><whitespace>
3439 * with a single space.
3441 objPtr
= Jim_NewStringObj(interp
, t
->token
, t
->len
);
3447 * Takes a tokenlist and creates the allocated list of script tokens
3448 * in script->token, of length script->len.
3450 * Unnecessary tokens are discarded, and LINE and WORD tokens are inserted
3453 * Also sets script->line to the line number of the first token
3455 static void ScriptObjAddTokens(Jim_Interp
*interp
, struct ScriptObj
*script
,
3456 ParseTokenList
*tokenlist
)
3459 struct ScriptToken
*token
;
3460 /* Number of tokens so far for the current command */
3462 /* This is the first token for the current command */
3463 ScriptToken
*linefirst
;
3467 #ifdef DEBUG_SHOW_SCRIPT_TOKENS
3468 printf("==== Tokens ====\n");
3469 for (i
= 0; i
< tokenlist
->count
; i
++) {
3470 printf("[%2d]@%d %s '%.*s'\n", i
, tokenlist
->list
[i
].line
, jim_tt_name(tokenlist
->list
[i
].type
),
3471 tokenlist
->list
[i
].len
, tokenlist
->list
[i
].token
);
3475 /* May need up to one extra script token for each EOL in the worst case */
3476 count
= tokenlist
->count
;
3477 for (i
= 0; i
< tokenlist
->count
; i
++) {
3478 if (tokenlist
->list
[i
].type
== JIM_TT_EOL
) {
3482 linenr
= script
->firstline
= tokenlist
->list
[0].line
;
3484 token
= script
->token
= Jim_Alloc(sizeof(ScriptToken
) * count
);
3486 /* This is the first token for the current command */
3487 linefirst
= token
++;
3489 for (i
= 0; i
< tokenlist
->count
; ) {
3490 /* Look ahead to find out how many tokens make up the next word */
3493 /* Skip any leading separators */
3494 while (tokenlist
->list
[i
].type
== JIM_TT_SEP
) {
3498 wordtokens
= JimCountWordTokens(script
, tokenlist
->list
+ i
);
3500 if (wordtokens
== 0) {
3501 /* None, so at end of line */
3503 linefirst
->type
= JIM_TT_LINE
;
3504 linefirst
->objPtr
= JimNewScriptLineObj(interp
, lineargs
, linenr
);
3505 Jim_IncrRefCount(linefirst
->objPtr
);
3507 /* Reset for new line */
3509 linefirst
= token
++;
3514 else if (wordtokens
!= 1) {
3515 /* More than 1, or {*}, so insert a WORD token */
3516 token
->type
= JIM_TT_WORD
;
3517 token
->objPtr
= Jim_NewIntObj(interp
, wordtokens
);
3518 Jim_IncrRefCount(token
->objPtr
);
3520 if (wordtokens
< 0) {
3521 /* Skip the expand token */
3523 wordtokens
= -wordtokens
- 1;
3528 if (lineargs
== 0) {
3529 /* First real token on the line, so record the line number */
3530 linenr
= tokenlist
->list
[i
].line
;
3534 /* Add each non-separator word token to the line */
3535 while (wordtokens
--) {
3536 const ParseToken
*t
= &tokenlist
->list
[i
++];
3538 token
->type
= t
->type
;
3539 token
->objPtr
= JimMakeScriptObj(interp
, t
);
3540 Jim_IncrRefCount(token
->objPtr
);
3542 /* Every object is initially a string of type 'source', but the
3543 * internal type may be specialized during execution of the
3545 JimSetSourceInfo(interp
, token
->objPtr
, script
->fileNameObj
, t
->line
);
3550 if (lineargs
== 0) {
3554 script
->len
= token
- script
->token
;
3556 JimPanic((script
->len
>= count
, "allocated script array is too short"));
3558 #ifdef DEBUG_SHOW_SCRIPT
3559 printf("==== Script (%s) ====\n", Jim_String(script
->fileNameObj
));
3560 for (i
= 0; i
< script
->len
; i
++) {
3561 const ScriptToken
*t
= &script
->token
[i
];
3562 printf("[%2d] %s %s\n", i
, jim_tt_name(t
->type
), Jim_String(t
->objPtr
));
3568 /* Parses the given string object to determine if it represents a complete script.
3570 * This is useful for interactive shells implementation, for [info complete].
3572 * If 'stateCharPtr' != NULL, the function stores ' ' on complete script,
3573 * '{' on scripts incomplete missing one or more '}' to be balanced.
3574 * '[' on scripts incomplete missing one or more ']' to be balanced.
3575 * '"' on scripts incomplete missing a '"' char.
3576 * '\\' on scripts with a trailing backslash.
3578 * If the script is complete, 1 is returned, otherwise 0.
3580 * If the script has extra characters after a close brace, this still returns 1,
3581 * but sets *stateCharPtr to '}'
3582 * Evaluating the script will give the error "extra characters after close-brace".
3584 int Jim_ScriptIsComplete(Jim_Interp
*interp
, Jim_Obj
*scriptObj
, char *stateCharPtr
)
3586 ScriptObj
*script
= JimGetScript(interp
, scriptObj
);
3588 *stateCharPtr
= script
->missing
;
3590 return script
->missing
== ' ' || script
->missing
== '}';
3594 * Sets an appropriate error message for a missing script/expression terminator.
3596 * Returns JIM_ERR if 'ch' represents an unmatched/missing character.
3598 * Note that a trailing backslash is not considered to be an error.
3600 static int JimParseCheckMissing(Jim_Interp
*interp
, int ch
)
3610 msg
= "unmatched \"[\"";
3613 msg
= "missing close-brace";
3616 msg
= "extra characters after close-brace";
3620 msg
= "missing quote";
3624 Jim_SetResultString(interp
, msg
, -1);
3629 * Similar to ScriptObjAddTokens(), but for subst objects.
3631 static void SubstObjAddTokens(Jim_Interp
*interp
, struct ScriptObj
*script
,
3632 ParseTokenList
*tokenlist
)
3635 struct ScriptToken
*token
;
3637 token
= script
->token
= Jim_Alloc(sizeof(ScriptToken
) * tokenlist
->count
);
3639 for (i
= 0; i
< tokenlist
->count
; i
++) {
3640 const ParseToken
*t
= &tokenlist
->list
[i
];
3642 /* Create a token for 't' */
3643 token
->type
= t
->type
;
3644 token
->objPtr
= JimMakeScriptObj(interp
, t
);
3645 Jim_IncrRefCount(token
->objPtr
);
3652 /* This method takes the string representation of an object
3653 * as a Tcl script, and generates the pre-parsed internal representation
3656 * On parse error, sets an error message and returns JIM_ERR
3657 * (Note: the object is still converted to a script, even if an error occurs)
3659 static void JimSetScriptFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
)
3662 const char *scriptText
= Jim_GetString(objPtr
, &scriptTextLen
);
3663 struct JimParserCtx parser
;
3664 struct ScriptObj
*script
;
3665 ParseTokenList tokenlist
;
3668 /* Try to get information about filename / line number */
3669 if (objPtr
->typePtr
== &sourceObjType
) {
3670 line
= objPtr
->internalRep
.sourceValue
.lineNumber
;
3673 /* Initially parse the script into tokens (in tokenlist) */
3674 ScriptTokenListInit(&tokenlist
);
3676 JimParserInit(&parser
, scriptText
, scriptTextLen
, line
);
3677 while (!parser
.eof
) {
3678 JimParseScript(&parser
);
3679 ScriptAddToken(&tokenlist
, parser
.tstart
, parser
.tend
- parser
.tstart
+ 1, parser
.tt
,
3683 /* Add a final EOF token */
3684 ScriptAddToken(&tokenlist
, scriptText
+ scriptTextLen
, 0, JIM_TT_EOF
, 0);
3686 /* Create the "real" script tokens from the parsed tokens */
3687 script
= Jim_Alloc(sizeof(*script
));
3688 memset(script
, 0, sizeof(*script
));
3690 if (objPtr
->typePtr
== &sourceObjType
) {
3691 script
->fileNameObj
= objPtr
->internalRep
.sourceValue
.fileNameObj
;
3694 script
->fileNameObj
= interp
->emptyObj
;
3696 Jim_IncrRefCount(script
->fileNameObj
);
3697 script
->missing
= parser
.missing
.ch
;
3698 script
->linenr
= parser
.missing
.line
;
3700 ScriptObjAddTokens(interp
, script
, &tokenlist
);
3702 /* No longer need the token list */
3703 ScriptTokenListFree(&tokenlist
);
3705 /* Free the old internal rep and set the new one. */
3706 Jim_FreeIntRep(interp
, objPtr
);
3707 Jim_SetIntRepPtr(objPtr
, script
);
3708 objPtr
->typePtr
= &scriptObjType
;
3711 static void JimAddErrorToStack(Jim_Interp
*interp
, ScriptObj
*script
);
3714 * Returns the parsed script.
3715 * Note that if there is any possibility that the script is not valid,
3716 * call JimScriptValid() to check
3718 static ScriptObj
*JimGetScript(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
3720 if (objPtr
== interp
->emptyObj
) {
3721 /* Avoid converting emptyObj to a script. use nullScriptObj instead. */
3722 objPtr
= interp
->nullScriptObj
;
3725 if (objPtr
->typePtr
!= &scriptObjType
|| ((struct ScriptObj
*)Jim_GetIntRepPtr(objPtr
))->substFlags
) {
3726 JimSetScriptFromAny(interp
, objPtr
);
3729 return (ScriptObj
*)Jim_GetIntRepPtr(objPtr
);
3733 * Returns 1 if the script is valid (parsed ok), otherwise returns 0
3734 * and leaves an error message in the interp result.
3737 static int JimScriptValid(Jim_Interp
*interp
, ScriptObj
*script
)
3739 if (JimParseCheckMissing(interp
, script
->missing
) == JIM_ERR
) {
3740 JimAddErrorToStack(interp
, script
);
3747 /* -----------------------------------------------------------------------------
3749 * ---------------------------------------------------------------------------*/
3750 static void JimIncrCmdRefCount(Jim_Cmd
*cmdPtr
)
3755 static void JimDecrCmdRefCount(Jim_Interp
*interp
, Jim_Cmd
*cmdPtr
)
3757 if (--cmdPtr
->inUse
== 0) {
3758 if (cmdPtr
->isproc
) {
3759 Jim_DecrRefCount(interp
, cmdPtr
->u
.proc
.argListObjPtr
);
3760 Jim_DecrRefCount(interp
, cmdPtr
->u
.proc
.bodyObjPtr
);
3761 Jim_DecrRefCount(interp
, cmdPtr
->u
.proc
.nsObj
);
3762 if (cmdPtr
->u
.proc
.staticVars
) {
3763 Jim_FreeHashTable(cmdPtr
->u
.proc
.staticVars
);
3764 Jim_Free(cmdPtr
->u
.proc
.staticVars
);
3769 if (cmdPtr
->u
.native
.delProc
) {
3770 cmdPtr
->u
.native
.delProc(interp
, cmdPtr
->u
.native
.privData
);
3773 if (cmdPtr
->prevCmd
) {
3774 /* Delete any pushed command too */
3775 JimDecrCmdRefCount(interp
, cmdPtr
->prevCmd
);
3781 /* Variables HashTable Type.
3783 * Keys are dynamically allocated strings, Values are Jim_Var structures.
3785 static void JimVariablesHTValDestructor(void *interp
, void *val
)
3787 Jim_DecrRefCount(interp
, ((Jim_Var
*)val
)->objPtr
);
3791 static const Jim_HashTableType JimVariablesHashTableType
= {
3792 JimStringCopyHTHashFunction
, /* hash function */
3793 JimStringCopyHTDup
, /* key dup */
3795 JimStringCopyHTKeyCompare
, /* key compare */
3796 JimStringCopyHTKeyDestructor
, /* key destructor */
3797 JimVariablesHTValDestructor
/* val destructor */
3800 /* Commands HashTable Type.
3802 * Keys are dynamic allocated strings, Values are Jim_Cmd structures.
3804 static void JimCommandsHT_ValDestructor(void *interp
, void *val
)
3806 JimDecrCmdRefCount(interp
, val
);
3809 static const Jim_HashTableType JimCommandsHashTableType
= {
3810 JimStringCopyHTHashFunction
, /* hash function */
3811 JimStringCopyHTDup
, /* key dup */
3813 JimStringCopyHTKeyCompare
, /* key compare */
3814 JimStringCopyHTKeyDestructor
, /* key destructor */
3815 JimCommandsHT_ValDestructor
/* val destructor */
3818 /* ------------------------- Commands related functions --------------------- */
3820 #ifdef jim_ext_namespace
3822 * Returns the "unscoped" version of the given namespace.
3823 * That is, the fully qualified name without the leading ::
3824 * The returned value is either nsObj, or an object with a zero ref count.
3826 static Jim_Obj
*JimQualifyNameObj(Jim_Interp
*interp
, Jim_Obj
*nsObj
)
3828 const char *name
= Jim_String(nsObj
);
3829 if (name
[0] == ':' && name
[1] == ':') {
3830 /* This command is being defined in the global namespace */
3831 while (*++name
== ':') {
3833 nsObj
= Jim_NewStringObj(interp
, name
, -1);
3835 else if (Jim_Length(interp
->framePtr
->nsObj
)) {
3836 /* This command is being defined in a non-global namespace */
3837 nsObj
= Jim_DuplicateObj(interp
, interp
->framePtr
->nsObj
);
3838 Jim_AppendStrings(interp
, nsObj
, "::", name
, NULL
);
3844 * If nameObjPtr starts with "::", returns it.
3845 * Otherwise returns a new object with nameObjPtr prefixed with "::".
3846 * In this case, decrements the ref count of nameObjPtr.
3848 Jim_Obj
*Jim_MakeGlobalNamespaceName(Jim_Interp
*interp
, Jim_Obj
*nameObjPtr
)
3852 const char *name
= Jim_String(nameObjPtr
);
3853 if (name
[0] == ':' && name
[1] == ':') {
3856 Jim_IncrRefCount(nameObjPtr
);
3857 resultObj
= Jim_NewStringObj(interp
, "::", -1);
3858 Jim_AppendObj(interp
, resultObj
, nameObjPtr
);
3859 Jim_DecrRefCount(interp
, nameObjPtr
);
3865 * An efficient version of JimQualifyNameObj() where the name is
3866 * available (and needed) as a 'const char *'.
3867 * Avoids creating an object if not necessary.
3868 * The object stored in *objPtrPtr should be disposed of with JimFreeQualifiedName() after use.
3870 static const char *JimQualifyName(Jim_Interp
*interp
, const char *name
, Jim_Obj
**objPtrPtr
)
3872 Jim_Obj
*objPtr
= interp
->emptyObj
;
3874 if (name
[0] == ':' && name
[1] == ':') {
3875 /* This command is being defined in the global namespace */
3876 while (*++name
== ':') {
3879 else if (Jim_Length(interp
->framePtr
->nsObj
)) {
3880 /* This command is being defined in a non-global namespace */
3881 objPtr
= Jim_DuplicateObj(interp
, interp
->framePtr
->nsObj
);
3882 Jim_AppendStrings(interp
, objPtr
, "::", name
, NULL
);
3883 name
= Jim_String(objPtr
);
3885 Jim_IncrRefCount(objPtr
);
3886 *objPtrPtr
= objPtr
;
3890 #define JimFreeQualifiedName(INTERP, OBJ) Jim_DecrRefCount((INTERP), (OBJ))
3893 /* We can be more efficient in the no-namespace case */
3894 #define JimQualifyName(INTERP, NAME, DUMMY) (((NAME)[0] == ':' && (NAME)[1] == ':') ? (NAME) + 2 : (NAME))
3895 #define JimFreeQualifiedName(INTERP, DUMMY) (void)(DUMMY)
3897 Jim_Obj
*Jim_MakeGlobalNamespaceName(Jim_Interp
*interp
, Jim_Obj
*nameObjPtr
)
3903 static int JimCreateCommand(Jim_Interp
*interp
, const char *name
, Jim_Cmd
*cmd
)
3905 /* It may already exist, so we try to delete the old one.
3906 * Note that reference count means that it won't be deleted yet if
3907 * it exists in the call stack.
3909 * BUT, if 'local' is in force, instead of deleting the existing
3910 * proc, we stash a reference to the old proc here.
3912 Jim_HashEntry
*he
= Jim_FindHashEntry(&interp
->commands
, name
);
3914 /* There was an old cmd with the same name,
3915 * so this requires a 'proc epoch' update. */
3917 /* If a procedure with the same name didn't exist there is no need
3918 * to increment the 'proc epoch' because creation of a new procedure
3919 * can never affect existing cached commands. We don't do
3920 * negative caching. */
3921 Jim_InterpIncrProcEpoch(interp
);
3924 if (he
&& interp
->local
) {
3925 /* Push this command over the top of the previous one */
3926 cmd
->prevCmd
= Jim_GetHashEntryVal(he
);
3927 Jim_SetHashVal(&interp
->commands
, he
, cmd
);
3931 /* Replace the existing command */
3932 Jim_DeleteHashEntry(&interp
->commands
, name
);
3935 Jim_AddHashEntry(&interp
->commands
, name
, cmd
);
3941 int Jim_CreateCommand(Jim_Interp
*interp
, const char *cmdNameStr
,
3942 Jim_CmdProc
*cmdProc
, void *privData
, Jim_DelCmdProc
*delProc
)
3944 Jim_Cmd
*cmdPtr
= Jim_Alloc(sizeof(*cmdPtr
));
3946 /* Store the new details for this command */
3947 memset(cmdPtr
, 0, sizeof(*cmdPtr
));
3949 cmdPtr
->u
.native
.delProc
= delProc
;
3950 cmdPtr
->u
.native
.cmdProc
= cmdProc
;
3951 cmdPtr
->u
.native
.privData
= privData
;
3953 JimCreateCommand(interp
, cmdNameStr
, cmdPtr
);
3958 static int JimCreateProcedureStatics(Jim_Interp
*interp
, Jim_Cmd
*cmdPtr
, Jim_Obj
*staticsListObjPtr
)
3962 len
= Jim_ListLength(interp
, staticsListObjPtr
);
3967 cmdPtr
->u
.proc
.staticVars
= Jim_Alloc(sizeof(Jim_HashTable
));
3968 Jim_InitHashTable(cmdPtr
->u
.proc
.staticVars
, &JimVariablesHashTableType
, interp
);
3969 for (i
= 0; i
< len
; i
++) {
3970 Jim_Obj
*objPtr
, *initObjPtr
, *nameObjPtr
;
3974 objPtr
= Jim_ListGetIndex(interp
, staticsListObjPtr
, i
);
3975 /* Check if it's composed of two elements. */
3976 subLen
= Jim_ListLength(interp
, objPtr
);
3977 if (subLen
== 1 || subLen
== 2) {
3978 /* Try to get the variable value from the current
3980 nameObjPtr
= Jim_ListGetIndex(interp
, objPtr
, 0);
3982 initObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_NONE
);
3983 if (initObjPtr
== NULL
) {
3984 Jim_SetResultFormatted(interp
,
3985 "variable for initialization of static \"%#s\" not found in the local context",
3991 initObjPtr
= Jim_ListGetIndex(interp
, objPtr
, 1);
3993 if (JimValidName(interp
, "static variable", nameObjPtr
) != JIM_OK
) {
3997 varPtr
= Jim_Alloc(sizeof(*varPtr
));
3998 varPtr
->objPtr
= initObjPtr
;
3999 Jim_IncrRefCount(initObjPtr
);
4000 varPtr
->linkFramePtr
= NULL
;
4001 if (Jim_AddHashEntry(cmdPtr
->u
.proc
.staticVars
,
4002 Jim_String(nameObjPtr
), varPtr
) != JIM_OK
) {
4003 Jim_SetResultFormatted(interp
,
4004 "static variable name \"%#s\" duplicated in statics list", nameObjPtr
);
4005 Jim_DecrRefCount(interp
, initObjPtr
);
4011 Jim_SetResultFormatted(interp
, "too many fields in static specifier \"%#s\"",
4020 * If the command is a proc, sets/updates the cached namespace (nsObj)
4021 * based on the command name.
4023 static void JimUpdateProcNamespace(Jim_Interp
*interp
, Jim_Cmd
*cmdPtr
, const char *cmdname
)
4025 #ifdef jim_ext_namespace
4026 if (cmdPtr
->isproc
) {
4027 /* XXX: Really need JimNamespaceSplit() */
4028 const char *pt
= strrchr(cmdname
, ':');
4029 if (pt
&& pt
!= cmdname
&& pt
[-1] == ':') {
4030 Jim_DecrRefCount(interp
, cmdPtr
->u
.proc
.nsObj
);
4031 cmdPtr
->u
.proc
.nsObj
= Jim_NewStringObj(interp
, cmdname
, pt
- cmdname
- 1);
4032 Jim_IncrRefCount(cmdPtr
->u
.proc
.nsObj
);
4034 if (Jim_FindHashEntry(&interp
->commands
, pt
+ 1)) {
4035 /* This command shadows a global command, so a proc epoch update is required */
4036 Jim_InterpIncrProcEpoch(interp
);
4043 static Jim_Cmd
*JimCreateProcedureCmd(Jim_Interp
*interp
, Jim_Obj
*argListObjPtr
,
4044 Jim_Obj
*staticsListObjPtr
, Jim_Obj
*bodyObjPtr
, Jim_Obj
*nsObj
)
4050 argListLen
= Jim_ListLength(interp
, argListObjPtr
);
4052 /* Allocate space for both the command pointer and the arg list */
4053 cmdPtr
= Jim_Alloc(sizeof(*cmdPtr
) + sizeof(struct Jim_ProcArg
) * argListLen
);
4054 memset(cmdPtr
, 0, sizeof(*cmdPtr
));
4057 cmdPtr
->u
.proc
.argListObjPtr
= argListObjPtr
;
4058 cmdPtr
->u
.proc
.argListLen
= argListLen
;
4059 cmdPtr
->u
.proc
.bodyObjPtr
= bodyObjPtr
;
4060 cmdPtr
->u
.proc
.argsPos
= -1;
4061 cmdPtr
->u
.proc
.arglist
= (struct Jim_ProcArg
*)(cmdPtr
+ 1);
4062 cmdPtr
->u
.proc
.nsObj
= nsObj
? nsObj
: interp
->emptyObj
;
4063 Jim_IncrRefCount(argListObjPtr
);
4064 Jim_IncrRefCount(bodyObjPtr
);
4065 Jim_IncrRefCount(cmdPtr
->u
.proc
.nsObj
);
4067 /* Create the statics hash table. */
4068 if (staticsListObjPtr
&& JimCreateProcedureStatics(interp
, cmdPtr
, staticsListObjPtr
) != JIM_OK
) {
4072 /* Parse the args out into arglist, validating as we go */
4073 /* Examine the argument list for default parameters and 'args' */
4074 for (i
= 0; i
< argListLen
; i
++) {
4076 Jim_Obj
*nameObjPtr
;
4077 Jim_Obj
*defaultObjPtr
;
4080 /* Examine a parameter */
4081 argPtr
= Jim_ListGetIndex(interp
, argListObjPtr
, i
);
4082 len
= Jim_ListLength(interp
, argPtr
);
4084 Jim_SetResultString(interp
, "argument with no name", -1);
4086 JimDecrCmdRefCount(interp
, cmdPtr
);
4090 Jim_SetResultFormatted(interp
, "too many fields in argument specifier \"%#s\"", argPtr
);
4095 /* Optional parameter */
4096 nameObjPtr
= Jim_ListGetIndex(interp
, argPtr
, 0);
4097 defaultObjPtr
= Jim_ListGetIndex(interp
, argPtr
, 1);
4100 /* Required parameter */
4101 nameObjPtr
= argPtr
;
4102 defaultObjPtr
= NULL
;
4106 if (Jim_CompareStringImmediate(interp
, nameObjPtr
, "args")) {
4107 if (cmdPtr
->u
.proc
.argsPos
>= 0) {
4108 Jim_SetResultString(interp
, "'args' specified more than once", -1);
4111 cmdPtr
->u
.proc
.argsPos
= i
;
4115 cmdPtr
->u
.proc
.optArity
++;
4118 cmdPtr
->u
.proc
.reqArity
++;
4122 cmdPtr
->u
.proc
.arglist
[i
].nameObjPtr
= nameObjPtr
;
4123 cmdPtr
->u
.proc
.arglist
[i
].defaultObjPtr
= defaultObjPtr
;
4129 int Jim_DeleteCommand(Jim_Interp
*interp
, const char *name
)
4132 Jim_Obj
*qualifiedNameObj
;
4133 const char *qualname
= JimQualifyName(interp
, name
, &qualifiedNameObj
);
4135 if (Jim_DeleteHashEntry(&interp
->commands
, qualname
) == JIM_ERR
) {
4136 Jim_SetResultFormatted(interp
, "can't delete \"%s\": command doesn't exist", name
);
4140 Jim_InterpIncrProcEpoch(interp
);
4143 JimFreeQualifiedName(interp
, qualifiedNameObj
);
4148 int Jim_RenameCommand(Jim_Interp
*interp
, const char *oldName
, const char *newName
)
4153 Jim_Obj
*qualifiedOldNameObj
;
4154 Jim_Obj
*qualifiedNewNameObj
;
4158 if (newName
[0] == 0) {
4159 return Jim_DeleteCommand(interp
, oldName
);
4162 fqold
= JimQualifyName(interp
, oldName
, &qualifiedOldNameObj
);
4163 fqnew
= JimQualifyName(interp
, newName
, &qualifiedNewNameObj
);
4165 /* Does it exist? */
4166 he
= Jim_FindHashEntry(&interp
->commands
, fqold
);
4168 Jim_SetResultFormatted(interp
, "can't rename \"%s\": command doesn't exist", oldName
);
4170 else if (Jim_FindHashEntry(&interp
->commands
, fqnew
)) {
4171 Jim_SetResultFormatted(interp
, "can't rename to \"%s\": command already exists", newName
);
4174 /* Add the new name first */
4175 cmdPtr
= Jim_GetHashEntryVal(he
);
4176 JimIncrCmdRefCount(cmdPtr
);
4177 JimUpdateProcNamespace(interp
, cmdPtr
, fqnew
);
4178 Jim_AddHashEntry(&interp
->commands
, fqnew
, cmdPtr
);
4180 /* Now remove the old name */
4181 Jim_DeleteHashEntry(&interp
->commands
, fqold
);
4183 /* Increment the epoch */
4184 Jim_InterpIncrProcEpoch(interp
);
4189 JimFreeQualifiedName(interp
, qualifiedOldNameObj
);
4190 JimFreeQualifiedName(interp
, qualifiedNewNameObj
);
4195 /* -----------------------------------------------------------------------------
4197 * ---------------------------------------------------------------------------*/
4199 static void FreeCommandInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
4201 Jim_DecrRefCount(interp
, objPtr
->internalRep
.cmdValue
.nsObj
);
4204 static void DupCommandInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
)
4206 dupPtr
->internalRep
.cmdValue
= srcPtr
->internalRep
.cmdValue
;
4207 dupPtr
->typePtr
= srcPtr
->typePtr
;
4208 Jim_IncrRefCount(dupPtr
->internalRep
.cmdValue
.nsObj
);
4211 static const Jim_ObjType commandObjType
= {
4213 FreeCommandInternalRep
,
4214 DupCommandInternalRep
,
4216 JIM_TYPE_REFERENCES
,
4219 /* This function returns the command structure for the command name
4220 * stored in objPtr. It specializes the objPtr to contain
4221 * cached info instead of performing the lookup into the hash table
4222 * every time. The information cached may not be up-to-date, in this
4223 * case the lookup is performed and the cache updated.
4225 * Respects the 'upcall' setting.
4227 Jim_Cmd
*Jim_GetCommand(Jim_Interp
*interp
, Jim_Obj
*objPtr
, int flags
)
4231 /* In order to be valid, the proc epoch must match and
4232 * the lookup must have occurred in the same namespace
4234 if (objPtr
->typePtr
!= &commandObjType
||
4235 objPtr
->internalRep
.cmdValue
.procEpoch
!= interp
->procEpoch
4236 #ifdef jim_ext_namespace
4237 || !Jim_StringEqObj(objPtr
->internalRep
.cmdValue
.nsObj
, interp
->framePtr
->nsObj
)
4240 /* Not cached or out of date, so lookup */
4242 /* Do we need to try the local namespace? */
4243 const char *name
= Jim_String(objPtr
);
4246 if (name
[0] == ':' && name
[1] == ':') {
4247 while (*++name
== ':') {
4250 #ifdef jim_ext_namespace
4251 else if (Jim_Length(interp
->framePtr
->nsObj
)) {
4252 /* This command is being defined in a non-global namespace */
4253 Jim_Obj
*nameObj
= Jim_DuplicateObj(interp
, interp
->framePtr
->nsObj
);
4254 Jim_AppendStrings(interp
, nameObj
, "::", name
, NULL
);
4255 he
= Jim_FindHashEntry(&interp
->commands
, Jim_String(nameObj
));
4256 Jim_FreeNewObj(interp
, nameObj
);
4263 /* Lookup in the global namespace */
4264 he
= Jim_FindHashEntry(&interp
->commands
, name
);
4266 if (flags
& JIM_ERRMSG
) {
4267 Jim_SetResultFormatted(interp
, "invalid command name \"%#s\"", objPtr
);
4271 #ifdef jim_ext_namespace
4274 cmd
= Jim_GetHashEntryVal(he
);
4276 /* Free the old internal rep and set the new one. */
4277 Jim_FreeIntRep(interp
, objPtr
);
4278 objPtr
->typePtr
= &commandObjType
;
4279 objPtr
->internalRep
.cmdValue
.procEpoch
= interp
->procEpoch
;
4280 objPtr
->internalRep
.cmdValue
.cmdPtr
= cmd
;
4281 objPtr
->internalRep
.cmdValue
.nsObj
= interp
->framePtr
->nsObj
;
4282 Jim_IncrRefCount(interp
->framePtr
->nsObj
);
4285 cmd
= objPtr
->internalRep
.cmdValue
.cmdPtr
;
4287 while (cmd
->u
.proc
.upcall
) {
4293 /* -----------------------------------------------------------------------------
4295 * ---------------------------------------------------------------------------*/
4297 /* -----------------------------------------------------------------------------
4299 * ---------------------------------------------------------------------------*/
4301 #define JIM_DICT_SUGAR 100 /* Only returned by SetVariableFromAny() */
4303 static int SetVariableFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
);
4305 static const Jim_ObjType variableObjType
= {
4310 JIM_TYPE_REFERENCES
,
4314 * Check that the name does not contain embedded nulls.
4316 * Variable and procedure names are manipulated as null terminated strings, so
4317 * don't allow names with embedded nulls.
4319 static int JimValidName(Jim_Interp
*interp
, const char *type
, Jim_Obj
*nameObjPtr
)
4321 /* Variable names and proc names can't contain embedded nulls */
4322 if (nameObjPtr
->typePtr
!= &variableObjType
) {
4324 const char *str
= Jim_GetString(nameObjPtr
, &len
);
4325 if (memchr(str
, '\0', len
)) {
4326 Jim_SetResultFormatted(interp
, "%s name contains embedded null", type
);
4333 /* This method should be called only by the variable API.
4334 * It returns JIM_OK on success (variable already exists),
4335 * JIM_ERR if it does not exist, JIM_DICT_SUGAR if it's not
4336 * a variable name, but syntax glue for [dict] i.e. the last
4337 * character is ')' */
4338 static int SetVariableFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
)
4340 const char *varName
;
4341 Jim_CallFrame
*framePtr
;
4346 /* Check if the object is already an uptodate variable */
4347 if (objPtr
->typePtr
== &variableObjType
) {
4348 framePtr
= objPtr
->internalRep
.varValue
.global
? interp
->topFramePtr
: interp
->framePtr
;
4349 if (objPtr
->internalRep
.varValue
.callFrameId
== framePtr
->id
) {
4353 /* Need to re-resolve the variable in the updated callframe */
4355 else if (objPtr
->typePtr
== &dictSubstObjType
) {
4356 return JIM_DICT_SUGAR
;
4358 else if (JimValidName(interp
, "variable", objPtr
) != JIM_OK
) {
4363 varName
= Jim_GetString(objPtr
, &len
);
4365 /* Make sure it's not syntax glue to get/set dict. */
4366 if (len
&& varName
[len
- 1] == ')' && strchr(varName
, '(') != NULL
) {
4367 return JIM_DICT_SUGAR
;
4370 if (varName
[0] == ':' && varName
[1] == ':') {
4371 while (*++varName
== ':') {
4374 framePtr
= interp
->topFramePtr
;
4378 framePtr
= interp
->framePtr
;
4381 /* Resolve this name in the variables hash table */
4382 he
= Jim_FindHashEntry(&framePtr
->vars
, varName
);
4384 if (!global
&& framePtr
->staticVars
) {
4385 /* Try with static vars. */
4386 he
= Jim_FindHashEntry(framePtr
->staticVars
, varName
);
4393 /* Free the old internal repr and set the new one. */
4394 Jim_FreeIntRep(interp
, objPtr
);
4395 objPtr
->typePtr
= &variableObjType
;
4396 objPtr
->internalRep
.varValue
.callFrameId
= framePtr
->id
;
4397 objPtr
->internalRep
.varValue
.varPtr
= Jim_GetHashEntryVal(he
);
4398 objPtr
->internalRep
.varValue
.global
= global
;
4402 /* -------------------- Variables related functions ------------------------- */
4403 static int JimDictSugarSet(Jim_Interp
*interp
, Jim_Obj
*ObjPtr
, Jim_Obj
*valObjPtr
);
4404 static Jim_Obj
*JimDictSugarGet(Jim_Interp
*interp
, Jim_Obj
*ObjPtr
, int flags
);
4406 static Jim_Var
*JimCreateVariable(Jim_Interp
*interp
, Jim_Obj
*nameObjPtr
, Jim_Obj
*valObjPtr
)
4409 Jim_CallFrame
*framePtr
;
4412 /* New variable to create */
4413 Jim_Var
*var
= Jim_Alloc(sizeof(*var
));
4415 var
->objPtr
= valObjPtr
;
4416 Jim_IncrRefCount(valObjPtr
);
4417 var
->linkFramePtr
= NULL
;
4419 name
= Jim_String(nameObjPtr
);
4420 if (name
[0] == ':' && name
[1] == ':') {
4421 while (*++name
== ':') {
4423 framePtr
= interp
->topFramePtr
;
4427 framePtr
= interp
->framePtr
;
4431 /* Insert the new variable */
4432 Jim_AddHashEntry(&framePtr
->vars
, name
, var
);
4434 /* Make the object int rep a variable */
4435 Jim_FreeIntRep(interp
, nameObjPtr
);
4436 nameObjPtr
->typePtr
= &variableObjType
;
4437 nameObjPtr
->internalRep
.varValue
.callFrameId
= framePtr
->id
;
4438 nameObjPtr
->internalRep
.varValue
.varPtr
= var
;
4439 nameObjPtr
->internalRep
.varValue
.global
= global
;
4444 /* For now that's dummy. Variables lookup should be optimized
4445 * in many ways, with caching of lookups, and possibly with
4446 * a table of pre-allocated vars in every CallFrame for local vars.
4447 * All the caching should also have an 'epoch' mechanism similar
4448 * to the one used by Tcl for procedures lookup caching. */
4451 * Set the variable nameObjPtr to value valObjptr.
4453 int Jim_SetVariable(Jim_Interp
*interp
, Jim_Obj
*nameObjPtr
, Jim_Obj
*valObjPtr
)
4458 switch (SetVariableFromAny(interp
, nameObjPtr
)) {
4459 case JIM_DICT_SUGAR
:
4460 return JimDictSugarSet(interp
, nameObjPtr
, valObjPtr
);
4463 if (JimValidName(interp
, "variable", nameObjPtr
) != JIM_OK
) {
4466 JimCreateVariable(interp
, nameObjPtr
, valObjPtr
);
4470 var
= nameObjPtr
->internalRep
.varValue
.varPtr
;
4471 if (var
->linkFramePtr
== NULL
) {
4472 Jim_IncrRefCount(valObjPtr
);
4473 Jim_DecrRefCount(interp
, var
->objPtr
);
4474 var
->objPtr
= valObjPtr
;
4476 else { /* Else handle the link */
4477 Jim_CallFrame
*savedCallFrame
;
4479 savedCallFrame
= interp
->framePtr
;
4480 interp
->framePtr
= var
->linkFramePtr
;
4481 err
= Jim_SetVariable(interp
, var
->objPtr
, valObjPtr
);
4482 interp
->framePtr
= savedCallFrame
;
4490 int Jim_SetVariableStr(Jim_Interp
*interp
, const char *name
, Jim_Obj
*objPtr
)
4492 Jim_Obj
*nameObjPtr
;
4495 nameObjPtr
= Jim_NewStringObj(interp
, name
, -1);
4496 Jim_IncrRefCount(nameObjPtr
);
4497 result
= Jim_SetVariable(interp
, nameObjPtr
, objPtr
);
4498 Jim_DecrRefCount(interp
, nameObjPtr
);
4502 int Jim_SetGlobalVariableStr(Jim_Interp
*interp
, const char *name
, Jim_Obj
*objPtr
)
4504 Jim_CallFrame
*savedFramePtr
;
4507 savedFramePtr
= interp
->framePtr
;
4508 interp
->framePtr
= interp
->topFramePtr
;
4509 result
= Jim_SetVariableStr(interp
, name
, objPtr
);
4510 interp
->framePtr
= savedFramePtr
;
4514 int Jim_SetVariableStrWithStr(Jim_Interp
*interp
, const char *name
, const char *val
)
4519 valObjPtr
= Jim_NewStringObj(interp
, val
, -1);
4520 Jim_IncrRefCount(valObjPtr
);
4521 result
= Jim_SetVariableStr(interp
, name
, valObjPtr
);
4522 Jim_DecrRefCount(interp
, valObjPtr
);
4526 int Jim_SetVariableLink(Jim_Interp
*interp
, Jim_Obj
*nameObjPtr
,
4527 Jim_Obj
*targetNameObjPtr
, Jim_CallFrame
*targetCallFrame
)
4529 const char *varName
;
4530 const char *targetName
;
4531 Jim_CallFrame
*framePtr
;
4534 /* Check for an existing variable or link */
4535 switch (SetVariableFromAny(interp
, nameObjPtr
)) {
4536 case JIM_DICT_SUGAR
:
4537 /* XXX: This message seem unnecessarily verbose, but it matches Tcl */
4538 Jim_SetResultFormatted(interp
, "bad variable name \"%#s\": upvar won't create a scalar variable that looks like an array element", nameObjPtr
);
4542 varPtr
= nameObjPtr
->internalRep
.varValue
.varPtr
;
4544 if (varPtr
->linkFramePtr
== NULL
) {
4545 Jim_SetResultFormatted(interp
, "variable \"%#s\" already exists", nameObjPtr
);
4549 /* It exists, but is a link, so first delete the link */
4550 varPtr
->linkFramePtr
= NULL
;
4554 /* Resolve the call frames for both variables */
4555 /* XXX: SetVariableFromAny() already did this! */
4556 varName
= Jim_String(nameObjPtr
);
4558 if (varName
[0] == ':' && varName
[1] == ':') {
4559 while (*++varName
== ':') {
4561 /* Linking a global var does nothing */
4562 framePtr
= interp
->topFramePtr
;
4565 framePtr
= interp
->framePtr
;
4568 targetName
= Jim_String(targetNameObjPtr
);
4569 if (targetName
[0] == ':' && targetName
[1] == ':') {
4570 while (*++targetName
== ':') {
4572 targetNameObjPtr
= Jim_NewStringObj(interp
, targetName
, -1);
4573 targetCallFrame
= interp
->topFramePtr
;
4575 Jim_IncrRefCount(targetNameObjPtr
);
4577 if (framePtr
->level
< targetCallFrame
->level
) {
4578 Jim_SetResultFormatted(interp
,
4579 "bad variable name \"%#s\": upvar won't create namespace variable that refers to procedure variable",
4581 Jim_DecrRefCount(interp
, targetNameObjPtr
);
4585 /* Check for cycles. */
4586 if (framePtr
== targetCallFrame
) {
4587 Jim_Obj
*objPtr
= targetNameObjPtr
;
4589 /* Cycles are only possible with 'uplevel 0' */
4591 if (strcmp(Jim_String(objPtr
), varName
) == 0) {
4592 Jim_SetResultString(interp
, "can't upvar from variable to itself", -1);
4593 Jim_DecrRefCount(interp
, targetNameObjPtr
);
4596 if (SetVariableFromAny(interp
, objPtr
) != JIM_OK
)
4598 varPtr
= objPtr
->internalRep
.varValue
.varPtr
;
4599 if (varPtr
->linkFramePtr
!= targetCallFrame
)
4601 objPtr
= varPtr
->objPtr
;
4605 /* Perform the binding */
4606 Jim_SetVariable(interp
, nameObjPtr
, targetNameObjPtr
);
4607 /* We are now sure 'nameObjPtr' type is variableObjType */
4608 nameObjPtr
->internalRep
.varValue
.varPtr
->linkFramePtr
= targetCallFrame
;
4609 Jim_DecrRefCount(interp
, targetNameObjPtr
);
4613 /* Return the Jim_Obj pointer associated with a variable name,
4614 * or NULL if the variable was not found in the current context.
4615 * The same optimization discussed in the comment to the
4616 * 'SetVariable' function should apply here.
4618 * If JIM_UNSHARED is set and the variable is an array element (dict sugar)
4619 * in a dictionary which is shared, the array variable value is duplicated first.
4620 * This allows the array element to be updated (e.g. append, lappend) without
4621 * affecting other references to the dictionary.
4623 Jim_Obj
*Jim_GetVariable(Jim_Interp
*interp
, Jim_Obj
*nameObjPtr
, int flags
)
4625 switch (SetVariableFromAny(interp
, nameObjPtr
)) {
4627 Jim_Var
*varPtr
= nameObjPtr
->internalRep
.varValue
.varPtr
;
4629 if (varPtr
->linkFramePtr
== NULL
) {
4630 return varPtr
->objPtr
;
4635 /* The variable is a link? Resolve it. */
4636 Jim_CallFrame
*savedCallFrame
= interp
->framePtr
;
4638 interp
->framePtr
= varPtr
->linkFramePtr
;
4639 objPtr
= Jim_GetVariable(interp
, varPtr
->objPtr
, flags
);
4640 interp
->framePtr
= savedCallFrame
;
4644 /* Error, so fall through to the error message */
4649 case JIM_DICT_SUGAR
:
4650 /* [dict] syntax sugar. */
4651 return JimDictSugarGet(interp
, nameObjPtr
, flags
);
4653 if (flags
& JIM_ERRMSG
) {
4654 Jim_SetResultFormatted(interp
, "can't read \"%#s\": no such variable", nameObjPtr
);
4659 Jim_Obj
*Jim_GetGlobalVariable(Jim_Interp
*interp
, Jim_Obj
*nameObjPtr
, int flags
)
4661 Jim_CallFrame
*savedFramePtr
;
4664 savedFramePtr
= interp
->framePtr
;
4665 interp
->framePtr
= interp
->topFramePtr
;
4666 objPtr
= Jim_GetVariable(interp
, nameObjPtr
, flags
);
4667 interp
->framePtr
= savedFramePtr
;
4672 Jim_Obj
*Jim_GetVariableStr(Jim_Interp
*interp
, const char *name
, int flags
)
4674 Jim_Obj
*nameObjPtr
, *varObjPtr
;
4676 nameObjPtr
= Jim_NewStringObj(interp
, name
, -1);
4677 Jim_IncrRefCount(nameObjPtr
);
4678 varObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, flags
);
4679 Jim_DecrRefCount(interp
, nameObjPtr
);
4683 Jim_Obj
*Jim_GetGlobalVariableStr(Jim_Interp
*interp
, const char *name
, int flags
)
4685 Jim_CallFrame
*savedFramePtr
;
4688 savedFramePtr
= interp
->framePtr
;
4689 interp
->framePtr
= interp
->topFramePtr
;
4690 objPtr
= Jim_GetVariableStr(interp
, name
, flags
);
4691 interp
->framePtr
= savedFramePtr
;
4696 /* Unset a variable.
4697 * Note: On success unset invalidates all the (cached) variable objects
4698 * by incrementing callFrameEpoch
4700 int Jim_UnsetVariable(Jim_Interp
*interp
, Jim_Obj
*nameObjPtr
, int flags
)
4704 Jim_CallFrame
*framePtr
;
4706 retval
= SetVariableFromAny(interp
, nameObjPtr
);
4707 if (retval
== JIM_DICT_SUGAR
) {
4708 /* [dict] syntax sugar. */
4709 return JimDictSugarSet(interp
, nameObjPtr
, NULL
);
4711 else if (retval
== JIM_OK
) {
4712 varPtr
= nameObjPtr
->internalRep
.varValue
.varPtr
;
4714 /* If it's a link call UnsetVariable recursively */
4715 if (varPtr
->linkFramePtr
) {
4716 framePtr
= interp
->framePtr
;
4717 interp
->framePtr
= varPtr
->linkFramePtr
;
4718 retval
= Jim_UnsetVariable(interp
, varPtr
->objPtr
, JIM_NONE
);
4719 interp
->framePtr
= framePtr
;
4722 const char *name
= Jim_String(nameObjPtr
);
4723 if (nameObjPtr
->internalRep
.varValue
.global
) {
4725 framePtr
= interp
->topFramePtr
;
4728 framePtr
= interp
->framePtr
;
4731 retval
= Jim_DeleteHashEntry(&framePtr
->vars
, name
);
4732 if (retval
== JIM_OK
) {
4733 /* Change the callframe id, invalidating var lookup caching */
4734 framePtr
->id
= interp
->callFrameEpoch
++;
4738 if (retval
!= JIM_OK
&& (flags
& JIM_ERRMSG
)) {
4739 Jim_SetResultFormatted(interp
, "can't unset \"%#s\": no such variable", nameObjPtr
);
4744 /* ---------- Dict syntax sugar (similar to array Tcl syntax) -------------- */
4746 /* Given a variable name for [dict] operation syntax sugar,
4747 * this function returns two objects, the first with the name
4748 * of the variable to set, and the second with the respective key.
4749 * For example "foo(bar)" will return objects with string repr. of
4752 * The returned objects have refcount = 1. The function can't fail. */
4753 static void JimDictSugarParseVarKey(Jim_Interp
*interp
, Jim_Obj
*objPtr
,
4754 Jim_Obj
**varPtrPtr
, Jim_Obj
**keyPtrPtr
)
4756 const char *str
, *p
;
4758 Jim_Obj
*varObjPtr
, *keyObjPtr
;
4760 str
= Jim_GetString(objPtr
, &len
);
4762 p
= strchr(str
, '(');
4763 JimPanic((p
== NULL
, "JimDictSugarParseVarKey() called for non-dict-sugar (%s)", str
));
4765 varObjPtr
= Jim_NewStringObj(interp
, str
, p
- str
);
4768 keyLen
= (str
+ len
) - p
;
4769 if (str
[len
- 1] == ')') {
4773 /* Create the objects with the variable name and key. */
4774 keyObjPtr
= Jim_NewStringObj(interp
, p
, keyLen
);
4776 Jim_IncrRefCount(varObjPtr
);
4777 Jim_IncrRefCount(keyObjPtr
);
4778 *varPtrPtr
= varObjPtr
;
4779 *keyPtrPtr
= keyObjPtr
;
4782 /* Helper of Jim_SetVariable() to deal with dict-syntax variable names.
4783 * Also used by Jim_UnsetVariable() with valObjPtr = NULL. */
4784 static int JimDictSugarSet(Jim_Interp
*interp
, Jim_Obj
*objPtr
, Jim_Obj
*valObjPtr
)
4788 SetDictSubstFromAny(interp
, objPtr
);
4790 err
= Jim_SetDictKeysVector(interp
, objPtr
->internalRep
.dictSubstValue
.varNameObjPtr
,
4791 &objPtr
->internalRep
.dictSubstValue
.indexObjPtr
, 1, valObjPtr
, JIM_MUSTEXIST
);
4793 if (err
== JIM_OK
) {
4794 /* Don't keep an extra ref to the result */
4795 Jim_SetEmptyResult(interp
);
4799 /* Better error message for unset a(2) where a exists but a(2) doesn't */
4800 if (Jim_GetVariable(interp
, objPtr
->internalRep
.dictSubstValue
.varNameObjPtr
, JIM_NONE
)) {
4801 Jim_SetResultFormatted(interp
, "can't unset \"%#s\": no such element in array",
4806 /* Make the error more informative and Tcl-compatible */
4807 Jim_SetResultFormatted(interp
, "can't %s \"%#s\": variable isn't array",
4808 (valObjPtr
? "set" : "unset"), objPtr
);
4814 * Expands the array variable (dict sugar) and returns the result, or NULL on error.
4816 * If JIM_UNSHARED is set and the dictionary is shared, it will be duplicated
4817 * and stored back to the variable before expansion.
4819 static Jim_Obj
*JimDictExpandArrayVariable(Jim_Interp
*interp
, Jim_Obj
*varObjPtr
,
4820 Jim_Obj
*keyObjPtr
, int flags
)
4822 Jim_Obj
*dictObjPtr
;
4823 Jim_Obj
*resObjPtr
= NULL
;
4826 dictObjPtr
= Jim_GetVariable(interp
, varObjPtr
, JIM_ERRMSG
);
4831 ret
= Jim_DictKey(interp
, dictObjPtr
, keyObjPtr
, &resObjPtr
, JIM_NONE
);
4832 if (ret
!= JIM_OK
) {
4833 Jim_SetResultFormatted(interp
,
4834 "can't read \"%#s(%#s)\": %s array", varObjPtr
, keyObjPtr
,
4835 ret
< 0 ? "variable isn't" : "no such element in");
4837 else if ((flags
& JIM_UNSHARED
) && Jim_IsShared(dictObjPtr
)) {
4838 /* Update the variable to have an unshared copy */
4839 Jim_SetVariable(interp
, varObjPtr
, Jim_DuplicateObj(interp
, dictObjPtr
));
4845 /* Helper of Jim_GetVariable() to deal with dict-syntax variable names */
4846 static Jim_Obj
*JimDictSugarGet(Jim_Interp
*interp
, Jim_Obj
*objPtr
, int flags
)
4848 SetDictSubstFromAny(interp
, objPtr
);
4850 return JimDictExpandArrayVariable(interp
,
4851 objPtr
->internalRep
.dictSubstValue
.varNameObjPtr
,
4852 objPtr
->internalRep
.dictSubstValue
.indexObjPtr
, flags
);
4855 /* --------- $var(INDEX) substitution, using a specialized object ----------- */
4857 void FreeDictSubstInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
4859 Jim_DecrRefCount(interp
, objPtr
->internalRep
.dictSubstValue
.varNameObjPtr
);
4860 Jim_DecrRefCount(interp
, objPtr
->internalRep
.dictSubstValue
.indexObjPtr
);
4863 static void DupDictSubstInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
)
4865 /* Copy the internal rep */
4866 dupPtr
->internalRep
= srcPtr
->internalRep
;
4867 /* Need to increment the ref counts */
4868 Jim_IncrRefCount(dupPtr
->internalRep
.dictSubstValue
.varNameObjPtr
);
4869 Jim_IncrRefCount(dupPtr
->internalRep
.dictSubstValue
.indexObjPtr
);
4872 /* Note: The object *must* be in dict-sugar format */
4873 static void SetDictSubstFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
4875 if (objPtr
->typePtr
!= &dictSubstObjType
) {
4876 Jim_Obj
*varObjPtr
, *keyObjPtr
;
4878 if (objPtr
->typePtr
== &interpolatedObjType
) {
4879 /* An interpolated object in dict-sugar form */
4881 varObjPtr
= objPtr
->internalRep
.dictSubstValue
.varNameObjPtr
;
4882 keyObjPtr
= objPtr
->internalRep
.dictSubstValue
.indexObjPtr
;
4884 Jim_IncrRefCount(varObjPtr
);
4885 Jim_IncrRefCount(keyObjPtr
);
4888 JimDictSugarParseVarKey(interp
, objPtr
, &varObjPtr
, &keyObjPtr
);
4891 Jim_FreeIntRep(interp
, objPtr
);
4892 objPtr
->typePtr
= &dictSubstObjType
;
4893 objPtr
->internalRep
.dictSubstValue
.varNameObjPtr
= varObjPtr
;
4894 objPtr
->internalRep
.dictSubstValue
.indexObjPtr
= keyObjPtr
;
4898 /* This function is used to expand [dict get] sugar in the form
4899 * of $var(INDEX). The function is mainly used by Jim_EvalObj()
4900 * to deal with tokens of type JIM_TT_DICTSUGAR. objPtr points to an
4901 * object that is *guaranteed* to be in the form VARNAME(INDEX).
4902 * The 'index' part is [subst]ituted, and is used to lookup a key inside
4903 * the [dict]ionary contained in variable VARNAME. */
4904 static Jim_Obj
*JimExpandDictSugar(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
4906 Jim_Obj
*resObjPtr
= NULL
;
4907 Jim_Obj
*substKeyObjPtr
= NULL
;
4909 SetDictSubstFromAny(interp
, objPtr
);
4911 if (Jim_SubstObj(interp
, objPtr
->internalRep
.dictSubstValue
.indexObjPtr
,
4912 &substKeyObjPtr
, JIM_NONE
)
4916 Jim_IncrRefCount(substKeyObjPtr
);
4918 JimDictExpandArrayVariable(interp
, objPtr
->internalRep
.dictSubstValue
.varNameObjPtr
,
4920 Jim_DecrRefCount(interp
, substKeyObjPtr
);
4925 /* -----------------------------------------------------------------------------
4927 * ---------------------------------------------------------------------------*/
4929 static Jim_CallFrame
*JimCreateCallFrame(Jim_Interp
*interp
, Jim_CallFrame
*parent
, Jim_Obj
*nsObj
)
4933 if (interp
->freeFramesList
) {
4934 cf
= interp
->freeFramesList
;
4935 interp
->freeFramesList
= cf
->next
;
4939 cf
->procArgsObjPtr
= NULL
;
4940 cf
->procBodyObjPtr
= NULL
;
4942 cf
->staticVars
= NULL
;
4943 cf
->localCommands
= NULL
;
4944 cf
->tailcallObj
= NULL
;
4945 cf
->tailcallCmd
= NULL
;
4948 cf
= Jim_Alloc(sizeof(*cf
));
4949 memset(cf
, 0, sizeof(*cf
));
4951 Jim_InitHashTable(&cf
->vars
, &JimVariablesHashTableType
, interp
);
4954 cf
->id
= interp
->callFrameEpoch
++;
4955 cf
->parent
= parent
;
4956 cf
->level
= parent
? parent
->level
+ 1 : 0;
4958 Jim_IncrRefCount(nsObj
);
4963 static int JimDeleteLocalProcs(Jim_Interp
*interp
, Jim_Stack
*localCommands
)
4965 /* Delete any local procs */
4966 if (localCommands
) {
4967 Jim_Obj
*cmdNameObj
;
4969 while ((cmdNameObj
= Jim_StackPop(localCommands
)) != NULL
) {
4972 Jim_HashTable
*ht
= &interp
->commands
;
4974 const char *fqname
= JimQualifyName(interp
, Jim_String(cmdNameObj
), &fqObjName
);
4976 he
= Jim_FindHashEntry(ht
, fqname
);
4979 Jim_Cmd
*cmd
= Jim_GetHashEntryVal(he
);
4981 Jim_Cmd
*prevCmd
= cmd
->prevCmd
;
4982 cmd
->prevCmd
= NULL
;
4984 /* Delete the old command */
4985 JimDecrCmdRefCount(interp
, cmd
);
4987 /* And restore the original */
4988 Jim_SetHashVal(ht
, he
, prevCmd
);
4991 Jim_DeleteHashEntry(ht
, fqname
);
4993 Jim_InterpIncrProcEpoch(interp
);
4995 Jim_DecrRefCount(interp
, cmdNameObj
);
4996 JimFreeQualifiedName(interp
, fqObjName
);
4998 Jim_FreeStack(localCommands
);
4999 Jim_Free(localCommands
);
5005 * Run any $jim::defer scripts for the current call frame.
5007 * retcode is the return code from the current proc.
5009 * Returns the new return code.
5011 static int JimInvokeDefer(Jim_Interp
*interp
, int retcode
)
5015 /* Fast check for the likely case that the variable doesn't exist */
5016 if (Jim_FindHashEntry(&interp
->framePtr
->vars
, "jim::defer") == NULL
) {
5020 objPtr
= Jim_GetVariableStr(interp
, "jim::defer", JIM_NONE
);
5025 int listLen
= Jim_ListLength(interp
, objPtr
);
5026 Jim_Obj
*resultObjPtr
;
5028 Jim_IncrRefCount(objPtr
);
5030 /* Need to save away the current interp result and
5031 * restore it if appropriate
5033 resultObjPtr
= Jim_GetResult(interp
);
5034 Jim_IncrRefCount(resultObjPtr
);
5035 Jim_SetEmptyResult(interp
);
5037 /* Invoke in reverse order */
5038 for (i
= listLen
; i
> 0; i
--) {
5039 /* If a defer script returns an error, don't evaluate remaining scripts */
5040 Jim_Obj
*scriptObjPtr
= Jim_ListGetIndex(interp
, objPtr
, i
- 1);
5041 ret
= Jim_EvalObj(interp
, scriptObjPtr
);
5042 if (ret
!= JIM_OK
) {
5047 if (ret
== JIM_OK
|| retcode
== JIM_ERR
) {
5048 /* defer script had no error, or proc had an error so restore proc result */
5049 Jim_SetResult(interp
, resultObjPtr
);
5055 Jim_DecrRefCount(interp
, resultObjPtr
);
5056 Jim_DecrRefCount(interp
, objPtr
);
5061 #define JIM_FCF_FULL 0 /* Always free the vars hash table */
5062 #define JIM_FCF_REUSE 1 /* Reuse the vars hash table if possible */
5063 static void JimFreeCallFrame(Jim_Interp
*interp
, Jim_CallFrame
*cf
, int action
)
5065 JimDeleteLocalProcs(interp
, cf
->localCommands
);
5067 if (cf
->procArgsObjPtr
)
5068 Jim_DecrRefCount(interp
, cf
->procArgsObjPtr
);
5069 if (cf
->procBodyObjPtr
)
5070 Jim_DecrRefCount(interp
, cf
->procBodyObjPtr
);
5071 Jim_DecrRefCount(interp
, cf
->nsObj
);
5072 if (action
== JIM_FCF_FULL
|| cf
->vars
.size
!= JIM_HT_INITIAL_SIZE
)
5073 Jim_FreeHashTable(&cf
->vars
);
5076 Jim_HashEntry
**table
= cf
->vars
.table
, *he
;
5078 for (i
= 0; i
< JIM_HT_INITIAL_SIZE
; i
++) {
5080 while (he
!= NULL
) {
5081 Jim_HashEntry
*nextEntry
= he
->next
;
5082 Jim_Var
*varPtr
= Jim_GetHashEntryVal(he
);
5084 Jim_DecrRefCount(interp
, varPtr
->objPtr
);
5085 Jim_Free(Jim_GetHashEntryKey(he
));
5094 cf
->next
= interp
->freeFramesList
;
5095 interp
->freeFramesList
= cf
;
5099 /* -----------------------------------------------------------------------------
5101 * ---------------------------------------------------------------------------*/
5102 #if defined(JIM_REFERENCES) && !defined(JIM_BOOTSTRAP)
5104 /* References HashTable Type.
5106 * Keys are unsigned long integers, dynamically allocated for now but in the
5107 * future it's worth to cache this 4 bytes objects. Values are pointers
5108 * to Jim_References. */
5109 static void JimReferencesHTValDestructor(void *interp
, void *val
)
5111 Jim_Reference
*refPtr
= (void *)val
;
5113 Jim_DecrRefCount(interp
, refPtr
->objPtr
);
5114 if (refPtr
->finalizerCmdNamePtr
!= NULL
) {
5115 Jim_DecrRefCount(interp
, refPtr
->finalizerCmdNamePtr
);
5120 static unsigned int JimReferencesHTHashFunction(const void *key
)
5122 /* Only the least significant bits are used. */
5123 const unsigned long *widePtr
= key
;
5124 unsigned int intValue
= (unsigned int)*widePtr
;
5126 return Jim_IntHashFunction(intValue
);
5129 static void *JimReferencesHTKeyDup(void *privdata
, const void *key
)
5131 void *copy
= Jim_Alloc(sizeof(unsigned long));
5133 JIM_NOTUSED(privdata
);
5135 memcpy(copy
, key
, sizeof(unsigned long));
5139 static int JimReferencesHTKeyCompare(void *privdata
, const void *key1
, const void *key2
)
5141 JIM_NOTUSED(privdata
);
5143 return memcmp(key1
, key2
, sizeof(unsigned long)) == 0;
5146 static void JimReferencesHTKeyDestructor(void *privdata
, void *key
)
5148 JIM_NOTUSED(privdata
);
5153 static const Jim_HashTableType JimReferencesHashTableType
= {
5154 JimReferencesHTHashFunction
, /* hash function */
5155 JimReferencesHTKeyDup
, /* key dup */
5157 JimReferencesHTKeyCompare
, /* key compare */
5158 JimReferencesHTKeyDestructor
, /* key destructor */
5159 JimReferencesHTValDestructor
/* val destructor */
5162 /* -----------------------------------------------------------------------------
5163 * Reference object type and References API
5164 * ---------------------------------------------------------------------------*/
5166 /* The string representation of references has two features in order
5167 * to make the GC faster. The first is that every reference starts
5168 * with a non common character '<', in order to make the string matching
5169 * faster. The second is that the reference string rep is 42 characters
5170 * in length, this means that it is not necessary to check any object with a string
5171 * repr < 42, and usually there aren't many of these objects. */
5173 #define JIM_REFERENCE_SPACE (35+JIM_REFERENCE_TAGLEN)
5175 static int JimFormatReference(char *buf
, Jim_Reference
*refPtr
, unsigned long id
)
5177 const char *fmt
= "<reference.<%s>.%020lu>";
5179 sprintf(buf
, fmt
, refPtr
->tag
, id
);
5180 return JIM_REFERENCE_SPACE
;
5183 static void UpdateStringOfReference(struct Jim_Obj
*objPtr
);
5185 static const Jim_ObjType referenceObjType
= {
5189 UpdateStringOfReference
,
5190 JIM_TYPE_REFERENCES
,
5193 static void UpdateStringOfReference(struct Jim_Obj
*objPtr
)
5195 char buf
[JIM_REFERENCE_SPACE
+ 1];
5197 JimFormatReference(buf
, objPtr
->internalRep
.refValue
.refPtr
, objPtr
->internalRep
.refValue
.id
);
5198 JimSetStringBytes(objPtr
, buf
);
5201 /* returns true if 'c' is a valid reference tag character.
5202 * i.e. inside the range [_a-zA-Z0-9] */
5203 static int isrefchar(int c
)
5205 return (c
== '_' || isalnum(c
));
5208 static int SetReferenceFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
5210 unsigned long value
;
5212 const char *str
, *start
, *end
;
5214 Jim_Reference
*refPtr
;
5218 /* Get the string representation */
5219 str
= Jim_GetString(objPtr
, &len
);
5220 /* Check if it looks like a reference */
5221 if (len
< JIM_REFERENCE_SPACE
)
5225 end
= str
+ len
- 1;
5226 while (*start
== ' ')
5228 while (*end
== ' ' && end
> start
)
5230 if (end
- start
+ 1 != JIM_REFERENCE_SPACE
)
5232 /* <reference.<1234567>.%020> */
5233 if (memcmp(start
, "<reference.<", 12) != 0)
5235 if (start
[12 + JIM_REFERENCE_TAGLEN
] != '>' || end
[0] != '>')
5237 /* The tag can't contain chars other than a-zA-Z0-9 + '_'. */
5238 for (i
= 0; i
< JIM_REFERENCE_TAGLEN
; i
++) {
5239 if (!isrefchar(start
[12 + i
]))
5242 /* Extract info from the reference. */
5243 memcpy(refId
, start
+ 14 + JIM_REFERENCE_TAGLEN
, 20);
5245 /* Try to convert the ID into an unsigned long */
5246 value
= strtoul(refId
, &endptr
, 10);
5247 if (JimCheckConversion(refId
, endptr
) != JIM_OK
)
5249 /* Check if the reference really exists! */
5250 he
= Jim_FindHashEntry(&interp
->references
, &value
);
5252 Jim_SetResultFormatted(interp
, "invalid reference id \"%#s\"", objPtr
);
5255 refPtr
= Jim_GetHashEntryVal(he
);
5256 /* Free the old internal repr and set the new one. */
5257 Jim_FreeIntRep(interp
, objPtr
);
5258 objPtr
->typePtr
= &referenceObjType
;
5259 objPtr
->internalRep
.refValue
.id
= value
;
5260 objPtr
->internalRep
.refValue
.refPtr
= refPtr
;
5264 Jim_SetResultFormatted(interp
, "expected reference but got \"%#s\"", objPtr
);
5268 /* Returns a new reference pointing to objPtr, having cmdNamePtr
5269 * as finalizer command (or NULL if there is no finalizer).
5270 * The returned reference object has refcount = 0. */
5271 Jim_Obj
*Jim_NewReference(Jim_Interp
*interp
, Jim_Obj
*objPtr
, Jim_Obj
*tagPtr
, Jim_Obj
*cmdNamePtr
)
5273 struct Jim_Reference
*refPtr
;
5279 /* Perform the Garbage Collection if needed. */
5280 Jim_CollectIfNeeded(interp
);
5282 refPtr
= Jim_Alloc(sizeof(*refPtr
));
5283 refPtr
->objPtr
= objPtr
;
5284 Jim_IncrRefCount(objPtr
);
5285 refPtr
->finalizerCmdNamePtr
= cmdNamePtr
;
5287 Jim_IncrRefCount(cmdNamePtr
);
5288 id
= interp
->referenceNextId
++;
5289 Jim_AddHashEntry(&interp
->references
, &id
, refPtr
);
5290 refObjPtr
= Jim_NewObj(interp
);
5291 refObjPtr
->typePtr
= &referenceObjType
;
5292 refObjPtr
->bytes
= NULL
;
5293 refObjPtr
->internalRep
.refValue
.id
= id
;
5294 refObjPtr
->internalRep
.refValue
.refPtr
= refPtr
;
5295 interp
->referenceNextId
++;
5296 /* Set the tag. Trimmed at JIM_REFERENCE_TAGLEN. Everything
5297 * that does not pass the 'isrefchar' test is replaced with '_' */
5298 tag
= Jim_GetString(tagPtr
, &tagLen
);
5299 if (tagLen
> JIM_REFERENCE_TAGLEN
)
5300 tagLen
= JIM_REFERENCE_TAGLEN
;
5301 for (i
= 0; i
< JIM_REFERENCE_TAGLEN
; i
++) {
5302 if (i
< tagLen
&& isrefchar(tag
[i
]))
5303 refPtr
->tag
[i
] = tag
[i
];
5305 refPtr
->tag
[i
] = '_';
5307 refPtr
->tag
[JIM_REFERENCE_TAGLEN
] = '\0';
5311 Jim_Reference
*Jim_GetReference(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
5313 if (objPtr
->typePtr
!= &referenceObjType
&& SetReferenceFromAny(interp
, objPtr
) == JIM_ERR
)
5315 return objPtr
->internalRep
.refValue
.refPtr
;
5318 int Jim_SetFinalizer(Jim_Interp
*interp
, Jim_Obj
*objPtr
, Jim_Obj
*cmdNamePtr
)
5320 Jim_Reference
*refPtr
;
5322 if ((refPtr
= Jim_GetReference(interp
, objPtr
)) == NULL
)
5324 Jim_IncrRefCount(cmdNamePtr
);
5325 if (refPtr
->finalizerCmdNamePtr
)
5326 Jim_DecrRefCount(interp
, refPtr
->finalizerCmdNamePtr
);
5327 refPtr
->finalizerCmdNamePtr
= cmdNamePtr
;
5331 int Jim_GetFinalizer(Jim_Interp
*interp
, Jim_Obj
*objPtr
, Jim_Obj
**cmdNamePtrPtr
)
5333 Jim_Reference
*refPtr
;
5335 if ((refPtr
= Jim_GetReference(interp
, objPtr
)) == NULL
)
5337 *cmdNamePtrPtr
= refPtr
->finalizerCmdNamePtr
;
5341 /* -----------------------------------------------------------------------------
5342 * References Garbage Collection
5343 * ---------------------------------------------------------------------------*/
5345 /* This the hash table type for the "MARK" phase of the GC */
5346 static const Jim_HashTableType JimRefMarkHashTableType
= {
5347 JimReferencesHTHashFunction
, /* hash function */
5348 JimReferencesHTKeyDup
, /* key dup */
5350 JimReferencesHTKeyCompare
, /* key compare */
5351 JimReferencesHTKeyDestructor
, /* key destructor */
5352 NULL
/* val destructor */
5355 /* Performs the garbage collection. */
5356 int Jim_Collect(Jim_Interp
*interp
)
5359 Jim_HashTable marks
;
5360 Jim_HashTableIterator htiter
;
5364 /* Avoid recursive calls */
5365 if (interp
->lastCollectId
== (unsigned long)~0) {
5366 /* Jim_Collect() already running. Return just now. */
5369 interp
->lastCollectId
= ~0;
5371 /* Mark all the references found into the 'mark' hash table.
5372 * The references are searched in every live object that
5373 * is of a type that can contain references. */
5374 Jim_InitHashTable(&marks
, &JimRefMarkHashTableType
, NULL
);
5375 objPtr
= interp
->liveList
;
5377 if (objPtr
->typePtr
== NULL
|| objPtr
->typePtr
->flags
& JIM_TYPE_REFERENCES
) {
5378 const char *str
, *p
;
5381 /* If the object is of type reference, to get the
5382 * Id is simple... */
5383 if (objPtr
->typePtr
== &referenceObjType
) {
5384 Jim_AddHashEntry(&marks
, &objPtr
->internalRep
.refValue
.id
, NULL
);
5386 printf("MARK (reference): %d refcount: %d\n",
5387 (int)objPtr
->internalRep
.refValue
.id
, objPtr
->refCount
);
5389 objPtr
= objPtr
->nextObjPtr
;
5392 /* Get the string repr of the object we want
5393 * to scan for references. */
5394 p
= str
= Jim_GetString(objPtr
, &len
);
5395 /* Skip objects too little to contain references. */
5396 if (len
< JIM_REFERENCE_SPACE
) {
5397 objPtr
= objPtr
->nextObjPtr
;
5400 /* Extract references from the object string repr. */
5405 if ((p
= strstr(p
, "<reference.<")) == NULL
)
5407 /* Check if it's a valid reference. */
5408 if (len
- (p
- str
) < JIM_REFERENCE_SPACE
)
5410 if (p
[41] != '>' || p
[19] != '>' || p
[20] != '.')
5412 for (i
= 21; i
<= 40; i
++)
5413 if (!isdigit(UCHAR(p
[i
])))
5416 id
= strtoul(p
+ 21, NULL
, 10);
5418 /* Ok, a reference for the given ID
5419 * was found. Mark it. */
5420 Jim_AddHashEntry(&marks
, &id
, NULL
);
5422 printf("MARK: %d\n", (int)id
);
5424 p
+= JIM_REFERENCE_SPACE
;
5427 objPtr
= objPtr
->nextObjPtr
;
5430 /* Run the references hash table to destroy every reference that
5431 * is not referenced outside (not present in the mark HT). */
5432 JimInitHashTableIterator(&interp
->references
, &htiter
);
5433 while ((he
= Jim_NextHashEntry(&htiter
)) != NULL
) {
5434 const unsigned long *refId
;
5435 Jim_Reference
*refPtr
;
5438 /* Check if in the mark phase we encountered
5439 * this reference. */
5440 if (Jim_FindHashEntry(&marks
, refId
) == NULL
) {
5442 printf("COLLECTING %d\n", (int)*refId
);
5445 /* Drop the reference, but call the
5446 * finalizer first if registered. */
5447 refPtr
= Jim_GetHashEntryVal(he
);
5448 if (refPtr
->finalizerCmdNamePtr
) {
5449 char *refstr
= Jim_Alloc(JIM_REFERENCE_SPACE
+ 1);
5450 Jim_Obj
*objv
[3], *oldResult
;
5452 JimFormatReference(refstr
, refPtr
, *refId
);
5454 objv
[0] = refPtr
->finalizerCmdNamePtr
;
5455 objv
[1] = Jim_NewStringObjNoAlloc(interp
, refstr
, JIM_REFERENCE_SPACE
);
5456 objv
[2] = refPtr
->objPtr
;
5458 /* Drop the reference itself */
5459 /* Avoid the finaliser being freed here */
5460 Jim_IncrRefCount(objv
[0]);
5461 /* Don't remove the reference from the hash table just yet
5462 * since that will free refPtr, and hence refPtr->objPtr
5465 /* Call the finalizer. Errors ignored. (should we use bgerror?) */
5466 oldResult
= interp
->result
;
5467 Jim_IncrRefCount(oldResult
);
5468 Jim_EvalObjVector(interp
, 3, objv
);
5469 Jim_SetResult(interp
, oldResult
);
5470 Jim_DecrRefCount(interp
, oldResult
);
5472 Jim_DecrRefCount(interp
, objv
[0]);
5474 Jim_DeleteHashEntry(&interp
->references
, refId
);
5477 Jim_FreeHashTable(&marks
);
5478 interp
->lastCollectId
= interp
->referenceNextId
;
5479 interp
->lastCollectTime
= time(NULL
);
5483 #define JIM_COLLECT_ID_PERIOD 5000
5484 #define JIM_COLLECT_TIME_PERIOD 300
5486 void Jim_CollectIfNeeded(Jim_Interp
*interp
)
5488 unsigned long elapsedId
;
5491 elapsedId
= interp
->referenceNextId
- interp
->lastCollectId
;
5492 elapsedTime
= time(NULL
) - interp
->lastCollectTime
;
5495 if (elapsedId
> JIM_COLLECT_ID_PERIOD
|| elapsedTime
> JIM_COLLECT_TIME_PERIOD
) {
5496 Jim_Collect(interp
);
5499 #endif /* JIM_REFERENCES && !JIM_BOOTSTRAP */
5501 int Jim_IsBigEndian(void)
5508 return uval
.c
[0] == 1;
5511 /* -----------------------------------------------------------------------------
5512 * Interpreter related functions
5513 * ---------------------------------------------------------------------------*/
5515 Jim_Interp
*Jim_CreateInterp(void)
5517 Jim_Interp
*i
= Jim_Alloc(sizeof(*i
));
5519 memset(i
, 0, sizeof(*i
));
5521 i
->maxCallFrameDepth
= JIM_MAX_CALLFRAME_DEPTH
;
5522 i
->maxEvalDepth
= JIM_MAX_EVAL_DEPTH
;
5523 i
->lastCollectTime
= time(NULL
);
5525 /* Note that we can create objects only after the
5526 * interpreter liveList and freeList pointers are
5527 * initialized to NULL. */
5528 Jim_InitHashTable(&i
->commands
, &JimCommandsHashTableType
, i
);
5529 #ifdef JIM_REFERENCES
5530 Jim_InitHashTable(&i
->references
, &JimReferencesHashTableType
, i
);
5532 Jim_InitHashTable(&i
->assocData
, &JimAssocDataHashTableType
, i
);
5533 Jim_InitHashTable(&i
->packages
, &JimPackageHashTableType
, NULL
);
5534 i
->emptyObj
= Jim_NewEmptyStringObj(i
);
5535 i
->trueObj
= Jim_NewIntObj(i
, 1);
5536 i
->falseObj
= Jim_NewIntObj(i
, 0);
5537 i
->framePtr
= i
->topFramePtr
= JimCreateCallFrame(i
, NULL
, i
->emptyObj
);
5538 i
->errorFileNameObj
= i
->emptyObj
;
5539 i
->result
= i
->emptyObj
;
5540 i
->stackTrace
= Jim_NewListObj(i
, NULL
, 0);
5541 i
->unknown
= Jim_NewStringObj(i
, "unknown", -1);
5542 i
->errorProc
= i
->emptyObj
;
5543 i
->currentScriptObj
= Jim_NewEmptyStringObj(i
);
5544 i
->nullScriptObj
= Jim_NewEmptyStringObj(i
);
5545 Jim_IncrRefCount(i
->emptyObj
);
5546 Jim_IncrRefCount(i
->errorFileNameObj
);
5547 Jim_IncrRefCount(i
->result
);
5548 Jim_IncrRefCount(i
->stackTrace
);
5549 Jim_IncrRefCount(i
->unknown
);
5550 Jim_IncrRefCount(i
->currentScriptObj
);
5551 Jim_IncrRefCount(i
->nullScriptObj
);
5552 Jim_IncrRefCount(i
->errorProc
);
5553 Jim_IncrRefCount(i
->trueObj
);
5554 Jim_IncrRefCount(i
->falseObj
);
5556 /* Initialize key variables every interpreter should contain */
5557 Jim_SetVariableStrWithStr(i
, JIM_LIBPATH
, TCL_LIBRARY
);
5558 Jim_SetVariableStrWithStr(i
, JIM_INTERACTIVE
, "0");
5560 Jim_SetVariableStrWithStr(i
, "tcl_platform(engine)", "Jim");
5561 Jim_SetVariableStrWithStr(i
, "tcl_platform(os)", TCL_PLATFORM_OS
);
5562 Jim_SetVariableStrWithStr(i
, "tcl_platform(platform)", TCL_PLATFORM_PLATFORM
);
5563 Jim_SetVariableStrWithStr(i
, "tcl_platform(pathSeparator)", TCL_PLATFORM_PATH_SEPARATOR
);
5564 Jim_SetVariableStrWithStr(i
, "tcl_platform(byteOrder)", Jim_IsBigEndian() ? "bigEndian" : "littleEndian");
5565 Jim_SetVariableStrWithStr(i
, "tcl_platform(threaded)", "0");
5566 Jim_SetVariableStr(i
, "tcl_platform(pointerSize)", Jim_NewIntObj(i
, sizeof(void *)));
5567 Jim_SetVariableStr(i
, "tcl_platform(wordSize)", Jim_NewIntObj(i
, sizeof(jim_wide
)));
5572 void Jim_FreeInterp(Jim_Interp
*i
)
5574 Jim_CallFrame
*cf
, *cfx
;
5576 Jim_Obj
*objPtr
, *nextObjPtr
;
5578 /* Free the active call frames list - must be done before i->commands is destroyed */
5579 for (cf
= i
->framePtr
; cf
; cf
= cfx
) {
5580 /* Note that we ignore any errors */
5581 JimInvokeDefer(i
, JIM_OK
);
5583 JimFreeCallFrame(i
, cf
, JIM_FCF_FULL
);
5586 Jim_DecrRefCount(i
, i
->emptyObj
);
5587 Jim_DecrRefCount(i
, i
->trueObj
);
5588 Jim_DecrRefCount(i
, i
->falseObj
);
5589 Jim_DecrRefCount(i
, i
->result
);
5590 Jim_DecrRefCount(i
, i
->stackTrace
);
5591 Jim_DecrRefCount(i
, i
->errorProc
);
5592 Jim_DecrRefCount(i
, i
->unknown
);
5593 Jim_DecrRefCount(i
, i
->errorFileNameObj
);
5594 Jim_DecrRefCount(i
, i
->currentScriptObj
);
5595 Jim_DecrRefCount(i
, i
->nullScriptObj
);
5596 Jim_FreeHashTable(&i
->commands
);
5597 #ifdef JIM_REFERENCES
5598 Jim_FreeHashTable(&i
->references
);
5600 Jim_FreeHashTable(&i
->packages
);
5601 Jim_Free(i
->prngState
);
5602 Jim_FreeHashTable(&i
->assocData
);
5604 /* Check that the live object list is empty, otherwise
5605 * there is a memory leak. */
5606 #ifdef JIM_MAINTAINER
5607 if (i
->liveList
!= NULL
) {
5608 objPtr
= i
->liveList
;
5610 printf("\n-------------------------------------\n");
5611 printf("Objects still in the free list:\n");
5613 const char *type
= objPtr
->typePtr
? objPtr
->typePtr
->name
: "string";
5616 if (objPtr
->bytes
&& strlen(objPtr
->bytes
) > 20) {
5617 printf("%p (%d) %-10s: '%.20s...'\n",
5618 (void *)objPtr
, objPtr
->refCount
, type
, objPtr
->bytes
);
5621 printf("%p (%d) %-10s: '%s'\n",
5622 (void *)objPtr
, objPtr
->refCount
, type
, objPtr
->bytes
? objPtr
->bytes
: "(null)");
5624 if (objPtr
->typePtr
== &sourceObjType
) {
5625 printf("FILE %s LINE %d\n",
5626 Jim_String(objPtr
->internalRep
.sourceValue
.fileNameObj
),
5627 objPtr
->internalRep
.sourceValue
.lineNumber
);
5629 objPtr
= objPtr
->nextObjPtr
;
5631 printf("-------------------------------------\n\n");
5632 JimPanic((1, "Live list non empty freeing the interpreter! Leak?"));
5636 /* Free all the freed objects. */
5637 objPtr
= i
->freeList
;
5639 nextObjPtr
= objPtr
->nextObjPtr
;
5641 objPtr
= nextObjPtr
;
5644 /* Free the free call frames list */
5645 for (cf
= i
->freeFramesList
; cf
; cf
= cfx
) {
5648 Jim_FreeHashTable(&cf
->vars
);
5652 /* Free the interpreter structure. */
5656 /* Returns the call frame relative to the level represented by
5657 * levelObjPtr. If levelObjPtr == NULL, the level is assumed to be '1'.
5659 * This function accepts the 'level' argument in the form
5660 * of the commands [uplevel] and [upvar].
5662 * Returns NULL on error.
5664 * Note: for a function accepting a relative integer as level suitable
5665 * for implementation of [info level ?level?], see JimGetCallFrameByInteger()
5667 Jim_CallFrame
*Jim_GetCallFrameByLevel(Jim_Interp
*interp
, Jim_Obj
*levelObjPtr
)
5671 Jim_CallFrame
*framePtr
;
5674 str
= Jim_String(levelObjPtr
);
5675 if (str
[0] == '#') {
5678 level
= jim_strtol(str
+ 1, &endptr
);
5679 if (str
[1] == '\0' || endptr
[0] != '\0') {
5684 if (Jim_GetLong(interp
, levelObjPtr
, &level
) != JIM_OK
|| level
< 0) {
5688 /* Convert from a relative to an absolute level */
5689 level
= interp
->framePtr
->level
- level
;
5694 str
= "1"; /* Needed to format the error message. */
5695 level
= interp
->framePtr
->level
- 1;
5699 return interp
->topFramePtr
;
5703 for (framePtr
= interp
->framePtr
; framePtr
; framePtr
= framePtr
->parent
) {
5704 if (framePtr
->level
== level
) {
5710 Jim_SetResultFormatted(interp
, "bad level \"%s\"", str
);
5714 /* Similar to Jim_GetCallFrameByLevel() but the level is specified
5715 * as a relative integer like in the [info level ?level?] command.
5717 static Jim_CallFrame
*JimGetCallFrameByInteger(Jim_Interp
*interp
, Jim_Obj
*levelObjPtr
)
5720 Jim_CallFrame
*framePtr
;
5722 if (Jim_GetLong(interp
, levelObjPtr
, &level
) == JIM_OK
) {
5724 /* Convert from a relative to an absolute level */
5725 level
= interp
->framePtr
->level
+ level
;
5729 return interp
->topFramePtr
;
5733 for (framePtr
= interp
->framePtr
; framePtr
; framePtr
= framePtr
->parent
) {
5734 if (framePtr
->level
== level
) {
5740 Jim_SetResultFormatted(interp
, "bad level \"%#s\"", levelObjPtr
);
5744 static void JimResetStackTrace(Jim_Interp
*interp
)
5746 Jim_DecrRefCount(interp
, interp
->stackTrace
);
5747 interp
->stackTrace
= Jim_NewListObj(interp
, NULL
, 0);
5748 Jim_IncrRefCount(interp
->stackTrace
);
5751 static void JimSetStackTrace(Jim_Interp
*interp
, Jim_Obj
*stackTraceObj
)
5755 /* Increment reference first in case these are the same object */
5756 Jim_IncrRefCount(stackTraceObj
);
5757 Jim_DecrRefCount(interp
, interp
->stackTrace
);
5758 interp
->stackTrace
= stackTraceObj
;
5759 interp
->errorFlag
= 1;
5761 /* This is a bit ugly.
5762 * If the filename of the last entry of the stack trace is empty,
5763 * the next stack level should be added.
5765 len
= Jim_ListLength(interp
, interp
->stackTrace
);
5767 if (Jim_Length(Jim_ListGetIndex(interp
, interp
->stackTrace
, len
- 2)) == 0) {
5768 interp
->addStackTrace
= 1;
5773 static void JimAppendStackTrace(Jim_Interp
*interp
, const char *procname
,
5774 Jim_Obj
*fileNameObj
, int linenr
)
5776 if (strcmp(procname
, "unknown") == 0) {
5779 if (!*procname
&& !Jim_Length(fileNameObj
)) {
5780 /* No useful info here */
5784 if (Jim_IsShared(interp
->stackTrace
)) {
5785 Jim_DecrRefCount(interp
, interp
->stackTrace
);
5786 interp
->stackTrace
= Jim_DuplicateObj(interp
, interp
->stackTrace
);
5787 Jim_IncrRefCount(interp
->stackTrace
);
5790 /* If we have no procname but the previous element did, merge with that frame */
5791 if (!*procname
&& Jim_Length(fileNameObj
)) {
5792 /* Just a filename. Check the previous entry */
5793 int len
= Jim_ListLength(interp
, interp
->stackTrace
);
5796 Jim_Obj
*objPtr
= Jim_ListGetIndex(interp
, interp
->stackTrace
, len
- 3);
5797 if (Jim_Length(objPtr
)) {
5798 /* Yes, the previous level had procname */
5799 objPtr
= Jim_ListGetIndex(interp
, interp
->stackTrace
, len
- 2);
5800 if (Jim_Length(objPtr
) == 0) {
5801 /* But no filename, so merge the new info with that frame */
5802 ListSetIndex(interp
, interp
->stackTrace
, len
- 2, fileNameObj
, 0);
5803 ListSetIndex(interp
, interp
->stackTrace
, len
- 1, Jim_NewIntObj(interp
, linenr
), 0);
5810 Jim_ListAppendElement(interp
, interp
->stackTrace
, Jim_NewStringObj(interp
, procname
, -1));
5811 Jim_ListAppendElement(interp
, interp
->stackTrace
, fileNameObj
);
5812 Jim_ListAppendElement(interp
, interp
->stackTrace
, Jim_NewIntObj(interp
, linenr
));
5815 int Jim_SetAssocData(Jim_Interp
*interp
, const char *key
, Jim_InterpDeleteProc
* delProc
,
5818 AssocDataValue
*assocEntryPtr
= (AssocDataValue
*) Jim_Alloc(sizeof(AssocDataValue
));
5820 assocEntryPtr
->delProc
= delProc
;
5821 assocEntryPtr
->data
= data
;
5822 return Jim_AddHashEntry(&interp
->assocData
, key
, assocEntryPtr
);
5825 void *Jim_GetAssocData(Jim_Interp
*interp
, const char *key
)
5827 Jim_HashEntry
*entryPtr
= Jim_FindHashEntry(&interp
->assocData
, key
);
5829 if (entryPtr
!= NULL
) {
5830 AssocDataValue
*assocEntryPtr
= Jim_GetHashEntryVal(entryPtr
);
5831 return assocEntryPtr
->data
;
5836 int Jim_DeleteAssocData(Jim_Interp
*interp
, const char *key
)
5838 return Jim_DeleteHashEntry(&interp
->assocData
, key
);
5841 int Jim_GetExitCode(Jim_Interp
*interp
)
5843 return interp
->exitCode
;
5846 /* -----------------------------------------------------------------------------
5848 * ---------------------------------------------------------------------------*/
5849 static void UpdateStringOfInt(struct Jim_Obj
*objPtr
);
5850 static int SetIntFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
, int flags
);
5852 static const Jim_ObjType intObjType
= {
5860 /* A coerced double is closer to an int than a double.
5861 * It is an int value temporarily masquerading as a double value.
5862 * i.e. it has the same string value as an int and Jim_GetWide()
5863 * succeeds, but also Jim_GetDouble() returns the value directly.
5865 static const Jim_ObjType coercedDoubleObjType
= {
5874 static void UpdateStringOfInt(struct Jim_Obj
*objPtr
)
5876 char buf
[JIM_INTEGER_SPACE
+ 1];
5877 jim_wide wideValue
= JimWideValue(objPtr
);
5880 if (wideValue
== 0) {
5884 char tmp
[JIM_INTEGER_SPACE
];
5888 if (wideValue
< 0) {
5891 /* C89 is implementation defined as to whether (-106 % 10) is -6 or 4,
5892 * whereas C99 is always -6
5893 * coverity[dead_error_line]
5895 tmp
[num
++] = (i
> 0) ? (10 - i
) : -i
;
5900 tmp
[num
++] = wideValue
% 10;
5904 for (i
= 0; i
< num
; i
++) {
5905 buf
[pos
++] = '0' + tmp
[num
- i
- 1];
5910 JimSetStringBytes(objPtr
, buf
);
5913 static int SetIntFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
, int flags
)
5918 if (objPtr
->typePtr
== &coercedDoubleObjType
) {
5920 objPtr
->typePtr
= &intObjType
;
5924 /* Get the string representation */
5925 str
= Jim_String(objPtr
);
5926 /* Try to convert into a jim_wide */
5927 if (Jim_StringToWide(str
, &wideValue
, 0) != JIM_OK
) {
5928 if (flags
& JIM_ERRMSG
) {
5929 Jim_SetResultFormatted(interp
, "expected integer but got \"%#s\"", objPtr
);
5933 if ((wideValue
== JIM_WIDE_MIN
|| wideValue
== JIM_WIDE_MAX
) && errno
== ERANGE
) {
5934 Jim_SetResultString(interp
, "Integer value too big to be represented", -1);
5937 /* Free the old internal repr and set the new one. */
5938 Jim_FreeIntRep(interp
, objPtr
);
5939 objPtr
->typePtr
= &intObjType
;
5940 objPtr
->internalRep
.wideValue
= wideValue
;
5944 #ifdef JIM_OPTIMIZATION
5945 static int JimIsWide(Jim_Obj
*objPtr
)
5947 return objPtr
->typePtr
== &intObjType
;
5951 int Jim_GetWide(Jim_Interp
*interp
, Jim_Obj
*objPtr
, jim_wide
* widePtr
)
5953 if (objPtr
->typePtr
!= &intObjType
&& SetIntFromAny(interp
, objPtr
, JIM_ERRMSG
) == JIM_ERR
)
5955 *widePtr
= JimWideValue(objPtr
);
5959 /* Get a wide but does not set an error if the format is bad. */
5960 static int JimGetWideNoErr(Jim_Interp
*interp
, Jim_Obj
*objPtr
, jim_wide
* widePtr
)
5962 if (objPtr
->typePtr
!= &intObjType
&& SetIntFromAny(interp
, objPtr
, JIM_NONE
) == JIM_ERR
)
5964 *widePtr
= JimWideValue(objPtr
);
5968 int Jim_GetLong(Jim_Interp
*interp
, Jim_Obj
*objPtr
, long *longPtr
)
5973 retval
= Jim_GetWide(interp
, objPtr
, &wideValue
);
5974 if (retval
== JIM_OK
) {
5975 *longPtr
= (long)wideValue
;
5981 Jim_Obj
*Jim_NewIntObj(Jim_Interp
*interp
, jim_wide wideValue
)
5985 objPtr
= Jim_NewObj(interp
);
5986 objPtr
->typePtr
= &intObjType
;
5987 objPtr
->bytes
= NULL
;
5988 objPtr
->internalRep
.wideValue
= wideValue
;
5992 /* -----------------------------------------------------------------------------
5994 * ---------------------------------------------------------------------------*/
5995 #define JIM_DOUBLE_SPACE 30
5997 static void UpdateStringOfDouble(struct Jim_Obj
*objPtr
);
5998 static int SetDoubleFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
);
6000 static const Jim_ObjType doubleObjType
= {
6004 UpdateStringOfDouble
,
6010 #define isnan(X) ((X) != (X))
6014 #define isinf(X) (1.0 / (X) == 0.0)
6017 static void UpdateStringOfDouble(struct Jim_Obj
*objPtr
)
6019 double value
= objPtr
->internalRep
.doubleValue
;
6022 JimSetStringBytes(objPtr
, "NaN");
6027 JimSetStringBytes(objPtr
, "-Inf");
6030 JimSetStringBytes(objPtr
, "Inf");
6035 char buf
[JIM_DOUBLE_SPACE
+ 1];
6037 int len
= sprintf(buf
, "%.12g", value
);
6039 /* Add a final ".0" if necessary */
6040 for (i
= 0; i
< len
; i
++) {
6041 if (buf
[i
] == '.' || buf
[i
] == 'e') {
6042 #if defined(JIM_SPRINTF_DOUBLE_NEEDS_FIX)
6043 /* If 'buf' ends in e-0nn or e+0nn, remove
6044 * the 0 after the + or - and reduce the length by 1
6046 char *e
= strchr(buf
, 'e');
6047 if (e
&& (e
[1] == '-' || e
[1] == '+') && e
[2] == '0') {
6050 memmove(e
, e
+ 1, len
- (e
- buf
));
6056 if (buf
[i
] == '\0') {
6061 JimSetStringBytes(objPtr
, buf
);
6065 static int SetDoubleFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
6071 #ifdef HAVE_LONG_LONG
6072 /* Assume a 53 bit mantissa */
6073 #define MIN_INT_IN_DOUBLE -(1LL << 53)
6074 #define MAX_INT_IN_DOUBLE -(MIN_INT_IN_DOUBLE + 1)
6076 if (objPtr
->typePtr
== &intObjType
6077 && JimWideValue(objPtr
) >= MIN_INT_IN_DOUBLE
6078 && JimWideValue(objPtr
) <= MAX_INT_IN_DOUBLE
) {
6080 /* Direct conversion to coerced double */
6081 objPtr
->typePtr
= &coercedDoubleObjType
;
6085 /* Preserve the string representation.
6086 * Needed so we can convert back to int without loss
6088 str
= Jim_String(objPtr
);
6090 if (Jim_StringToWide(str
, &wideValue
, 10) == JIM_OK
) {
6091 /* Managed to convert to an int, so we can use this as a cooerced double */
6092 Jim_FreeIntRep(interp
, objPtr
);
6093 objPtr
->typePtr
= &coercedDoubleObjType
;
6094 objPtr
->internalRep
.wideValue
= wideValue
;
6098 /* Try to convert into a double */
6099 if (Jim_StringToDouble(str
, &doubleValue
) != JIM_OK
) {
6100 Jim_SetResultFormatted(interp
, "expected floating-point number but got \"%#s\"", objPtr
);
6103 /* Free the old internal repr and set the new one. */
6104 Jim_FreeIntRep(interp
, objPtr
);
6106 objPtr
->typePtr
= &doubleObjType
;
6107 objPtr
->internalRep
.doubleValue
= doubleValue
;
6111 int Jim_GetDouble(Jim_Interp
*interp
, Jim_Obj
*objPtr
, double *doublePtr
)
6113 if (objPtr
->typePtr
== &coercedDoubleObjType
) {
6114 *doublePtr
= JimWideValue(objPtr
);
6117 if (objPtr
->typePtr
!= &doubleObjType
&& SetDoubleFromAny(interp
, objPtr
) == JIM_ERR
)
6120 if (objPtr
->typePtr
== &coercedDoubleObjType
) {
6121 *doublePtr
= JimWideValue(objPtr
);
6124 *doublePtr
= objPtr
->internalRep
.doubleValue
;
6129 Jim_Obj
*Jim_NewDoubleObj(Jim_Interp
*interp
, double doubleValue
)
6133 objPtr
= Jim_NewObj(interp
);
6134 objPtr
->typePtr
= &doubleObjType
;
6135 objPtr
->bytes
= NULL
;
6136 objPtr
->internalRep
.doubleValue
= doubleValue
;
6140 /* -----------------------------------------------------------------------------
6141 * Boolean conversion
6142 * ---------------------------------------------------------------------------*/
6143 static int SetBooleanFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
, int flags
);
6145 int Jim_GetBoolean(Jim_Interp
*interp
, Jim_Obj
*objPtr
, int * booleanPtr
)
6147 if (objPtr
->typePtr
!= &intObjType
&& SetBooleanFromAny(interp
, objPtr
, JIM_ERRMSG
) == JIM_ERR
)
6149 *booleanPtr
= (int) JimWideValue(objPtr
);
6153 static int SetBooleanFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
, int flags
)
6155 static const char * const falses
[] = {
6156 "0", "false", "no", "off", NULL
6158 static const char * const trues
[] = {
6159 "1", "true", "yes", "on", NULL
6165 if (Jim_GetEnum(interp
, objPtr
, falses
, &index
, NULL
, 0) == JIM_OK
) {
6167 } else if (Jim_GetEnum(interp
, objPtr
, trues
, &index
, NULL
, 0) == JIM_OK
) {
6170 if (flags
& JIM_ERRMSG
) {
6171 Jim_SetResultFormatted(interp
, "expected boolean but got \"%#s\"", objPtr
);
6176 /* Free the old internal repr and set the new one. */
6177 Jim_FreeIntRep(interp
, objPtr
);
6178 objPtr
->typePtr
= &intObjType
;
6179 objPtr
->internalRep
.wideValue
= boolean
;
6183 /* -----------------------------------------------------------------------------
6185 * ---------------------------------------------------------------------------*/
6186 static void ListInsertElements(Jim_Obj
*listPtr
, int idx
, int elemc
, Jim_Obj
*const *elemVec
);
6187 static void ListAppendElement(Jim_Obj
*listPtr
, Jim_Obj
*objPtr
);
6188 static void FreeListInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
);
6189 static void DupListInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
);
6190 static void UpdateStringOfList(struct Jim_Obj
*objPtr
);
6191 static int SetListFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
);
6193 /* Note that while the elements of the list may contain references,
6194 * the list object itself can't. This basically means that the
6195 * list object string representation as a whole can't contain references
6196 * that are not presents in the single elements. */
6197 static const Jim_ObjType listObjType
= {
6199 FreeListInternalRep
,
6205 void FreeListInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
6209 for (i
= 0; i
< objPtr
->internalRep
.listValue
.len
; i
++) {
6210 Jim_DecrRefCount(interp
, objPtr
->internalRep
.listValue
.ele
[i
]);
6212 Jim_Free(objPtr
->internalRep
.listValue
.ele
);
6215 void DupListInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
)
6219 JIM_NOTUSED(interp
);
6221 dupPtr
->internalRep
.listValue
.len
= srcPtr
->internalRep
.listValue
.len
;
6222 dupPtr
->internalRep
.listValue
.maxLen
= srcPtr
->internalRep
.listValue
.maxLen
;
6223 dupPtr
->internalRep
.listValue
.ele
=
6224 Jim_Alloc(sizeof(Jim_Obj
*) * srcPtr
->internalRep
.listValue
.maxLen
);
6225 memcpy(dupPtr
->internalRep
.listValue
.ele
, srcPtr
->internalRep
.listValue
.ele
,
6226 sizeof(Jim_Obj
*) * srcPtr
->internalRep
.listValue
.len
);
6227 for (i
= 0; i
< dupPtr
->internalRep
.listValue
.len
; i
++) {
6228 Jim_IncrRefCount(dupPtr
->internalRep
.listValue
.ele
[i
]);
6230 dupPtr
->typePtr
= &listObjType
;
6233 /* The following function checks if a given string can be encoded
6234 * into a list element without any kind of quoting, surrounded by braces,
6235 * or using escapes to quote. */
6236 #define JIM_ELESTR_SIMPLE 0
6237 #define JIM_ELESTR_BRACE 1
6238 #define JIM_ELESTR_QUOTE 2
6239 static unsigned char ListElementQuotingType(const char *s
, int len
)
6241 int i
, level
, blevel
, trySimple
= 1;
6243 /* Try with the SIMPLE case */
6245 return JIM_ELESTR_BRACE
;
6246 if (s
[0] == '"' || s
[0] == '{') {
6250 for (i
= 0; i
< len
; i
++) {
6271 return JIM_ELESTR_SIMPLE
;
6274 /* Test if it's possible to do with braces */
6275 if (s
[len
- 1] == '\\')
6276 return JIM_ELESTR_QUOTE
;
6279 for (i
= 0; i
< len
; i
++) {
6287 return JIM_ELESTR_QUOTE
;
6296 if (s
[i
+ 1] == '\n')
6297 return JIM_ELESTR_QUOTE
;
6298 else if (s
[i
+ 1] != '\0')
6304 return JIM_ELESTR_QUOTE
;
6309 return JIM_ELESTR_BRACE
;
6310 for (i
= 0; i
< len
; i
++) {
6324 return JIM_ELESTR_BRACE
;
6328 return JIM_ELESTR_SIMPLE
;
6330 return JIM_ELESTR_QUOTE
;
6333 /* Backslashes-escapes the null-terminated string 's' into the buffer at 'q'
6334 * The buffer must be at least strlen(s) * 2 + 1 bytes long for the worst-case
6336 * Returns the length of the result.
6338 static int BackslashQuoteString(const char *s
, int len
, char *q
)
6391 static void JimMakeListStringRep(Jim_Obj
*objPtr
, Jim_Obj
**objv
, int objc
)
6393 #define STATIC_QUOTING_LEN 32
6394 int i
, bufLen
, realLength
;
6397 unsigned char *quotingType
, staticQuoting
[STATIC_QUOTING_LEN
];
6399 /* Estimate the space needed. */
6400 if (objc
> STATIC_QUOTING_LEN
) {
6401 quotingType
= Jim_Alloc(objc
);
6404 quotingType
= staticQuoting
;
6407 for (i
= 0; i
< objc
; i
++) {
6410 strRep
= Jim_GetString(objv
[i
], &len
);
6411 quotingType
[i
] = ListElementQuotingType(strRep
, len
);
6412 switch (quotingType
[i
]) {
6413 case JIM_ELESTR_SIMPLE
:
6414 if (i
!= 0 || strRep
[0] != '#') {
6418 /* Special case '#' on first element needs braces */
6419 quotingType
[i
] = JIM_ELESTR_BRACE
;
6421 case JIM_ELESTR_BRACE
:
6424 case JIM_ELESTR_QUOTE
:
6428 bufLen
++; /* elements separator. */
6432 /* Generate the string rep. */
6433 p
= objPtr
->bytes
= Jim_Alloc(bufLen
+ 1);
6435 for (i
= 0; i
< objc
; i
++) {
6438 strRep
= Jim_GetString(objv
[i
], &len
);
6440 switch (quotingType
[i
]) {
6441 case JIM_ELESTR_SIMPLE
:
6442 memcpy(p
, strRep
, len
);
6446 case JIM_ELESTR_BRACE
:
6448 memcpy(p
, strRep
, len
);
6451 realLength
+= len
+ 2;
6453 case JIM_ELESTR_QUOTE
:
6454 if (i
== 0 && strRep
[0] == '#') {
6458 qlen
= BackslashQuoteString(strRep
, len
, p
);
6463 /* Add a separating space */
6464 if (i
+ 1 != objc
) {
6469 *p
= '\0'; /* nul term. */
6470 objPtr
->length
= realLength
;
6472 if (quotingType
!= staticQuoting
) {
6473 Jim_Free(quotingType
);
6477 static void UpdateStringOfList(struct Jim_Obj
*objPtr
)
6479 JimMakeListStringRep(objPtr
, objPtr
->internalRep
.listValue
.ele
, objPtr
->internalRep
.listValue
.len
);
6482 static int SetListFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
)
6484 struct JimParserCtx parser
;
6487 Jim_Obj
*fileNameObj
;
6490 if (objPtr
->typePtr
== &listObjType
) {
6494 /* Optimise dict -> list for object with no string rep. Note that this may only save a little time, but
6495 * it also preserves any source location of the dict elements
6496 * which can be very useful
6498 if (Jim_IsDict(objPtr
) && objPtr
->bytes
== NULL
) {
6499 Jim_Obj
**listObjPtrPtr
;
6503 listObjPtrPtr
= JimDictPairs(objPtr
, &len
);
6504 for (i
= 0; i
< len
; i
++) {
6505 Jim_IncrRefCount(listObjPtrPtr
[i
]);
6508 /* Now just switch the internal rep */
6509 Jim_FreeIntRep(interp
, objPtr
);
6510 objPtr
->typePtr
= &listObjType
;
6511 objPtr
->internalRep
.listValue
.len
= len
;
6512 objPtr
->internalRep
.listValue
.maxLen
= len
;
6513 objPtr
->internalRep
.listValue
.ele
= listObjPtrPtr
;
6518 /* Try to preserve information about filename / line number */
6519 if (objPtr
->typePtr
== &sourceObjType
) {
6520 fileNameObj
= objPtr
->internalRep
.sourceValue
.fileNameObj
;
6521 linenr
= objPtr
->internalRep
.sourceValue
.lineNumber
;
6524 fileNameObj
= interp
->emptyObj
;
6527 Jim_IncrRefCount(fileNameObj
);
6529 /* Get the string representation */
6530 str
= Jim_GetString(objPtr
, &strLen
);
6532 /* Free the old internal repr just now and initialize the
6533 * new one just now. The string->list conversion can't fail. */
6534 Jim_FreeIntRep(interp
, objPtr
);
6535 objPtr
->typePtr
= &listObjType
;
6536 objPtr
->internalRep
.listValue
.len
= 0;
6537 objPtr
->internalRep
.listValue
.maxLen
= 0;
6538 objPtr
->internalRep
.listValue
.ele
= NULL
;
6540 /* Convert into a list */
6542 JimParserInit(&parser
, str
, strLen
, linenr
);
6543 while (!parser
.eof
) {
6544 Jim_Obj
*elementPtr
;
6546 JimParseList(&parser
);
6547 if (parser
.tt
!= JIM_TT_STR
&& parser
.tt
!= JIM_TT_ESC
)
6549 elementPtr
= JimParserGetTokenObj(interp
, &parser
);
6550 JimSetSourceInfo(interp
, elementPtr
, fileNameObj
, parser
.tline
);
6551 ListAppendElement(objPtr
, elementPtr
);
6554 Jim_DecrRefCount(interp
, fileNameObj
);
6558 Jim_Obj
*Jim_NewListObj(Jim_Interp
*interp
, Jim_Obj
*const *elements
, int len
)
6562 objPtr
= Jim_NewObj(interp
);
6563 objPtr
->typePtr
= &listObjType
;
6564 objPtr
->bytes
= NULL
;
6565 objPtr
->internalRep
.listValue
.ele
= NULL
;
6566 objPtr
->internalRep
.listValue
.len
= 0;
6567 objPtr
->internalRep
.listValue
.maxLen
= 0;
6570 ListInsertElements(objPtr
, 0, len
, elements
);
6576 /* Return a vector of Jim_Obj with the elements of a Jim list, and the
6577 * length of the vector. Note that the user of this function should make
6578 * sure that the list object can't shimmer while the vector returned
6579 * is in use, this vector is the one stored inside the internal representation
6580 * of the list object. This function is not exported, extensions should
6581 * always access to the List object elements using Jim_ListIndex(). */
6582 static void JimListGetElements(Jim_Interp
*interp
, Jim_Obj
*listObj
, int *listLen
,
6585 *listLen
= Jim_ListLength(interp
, listObj
);
6586 *listVec
= listObj
->internalRep
.listValue
.ele
;
6589 /* Sorting uses ints, but commands may return wide */
6590 static int JimSign(jim_wide w
)
6601 /* ListSortElements type values */
6617 int (*subfn
)(Jim_Obj
**, Jim_Obj
**);
6620 static struct lsort_info
*sort_info
;
6622 static int ListSortIndexHelper(Jim_Obj
**lhsObj
, Jim_Obj
**rhsObj
)
6624 Jim_Obj
*lObj
, *rObj
;
6626 if (Jim_ListIndex(sort_info
->interp
, *lhsObj
, sort_info
->index
, &lObj
, JIM_ERRMSG
) != JIM_OK
||
6627 Jim_ListIndex(sort_info
->interp
, *rhsObj
, sort_info
->index
, &rObj
, JIM_ERRMSG
) != JIM_OK
) {
6628 longjmp(sort_info
->jmpbuf
, JIM_ERR
);
6630 return sort_info
->subfn(&lObj
, &rObj
);
6633 /* Sort the internal rep of a list. */
6634 static int ListSortString(Jim_Obj
**lhsObj
, Jim_Obj
**rhsObj
)
6636 return Jim_StringCompareObj(sort_info
->interp
, *lhsObj
, *rhsObj
, 0) * sort_info
->order
;
6639 static int ListSortStringNoCase(Jim_Obj
**lhsObj
, Jim_Obj
**rhsObj
)
6641 return Jim_StringCompareObj(sort_info
->interp
, *lhsObj
, *rhsObj
, 1) * sort_info
->order
;
6644 static int ListSortInteger(Jim_Obj
**lhsObj
, Jim_Obj
**rhsObj
)
6646 jim_wide lhs
= 0, rhs
= 0;
6648 if (Jim_GetWide(sort_info
->interp
, *lhsObj
, &lhs
) != JIM_OK
||
6649 Jim_GetWide(sort_info
->interp
, *rhsObj
, &rhs
) != JIM_OK
) {
6650 longjmp(sort_info
->jmpbuf
, JIM_ERR
);
6653 return JimSign(lhs
- rhs
) * sort_info
->order
;
6656 static int ListSortReal(Jim_Obj
**lhsObj
, Jim_Obj
**rhsObj
)
6658 double lhs
= 0, rhs
= 0;
6660 if (Jim_GetDouble(sort_info
->interp
, *lhsObj
, &lhs
) != JIM_OK
||
6661 Jim_GetDouble(sort_info
->interp
, *rhsObj
, &rhs
) != JIM_OK
) {
6662 longjmp(sort_info
->jmpbuf
, JIM_ERR
);
6668 return sort_info
->order
;
6670 return -sort_info
->order
;
6673 static int ListSortCommand(Jim_Obj
**lhsObj
, Jim_Obj
**rhsObj
)
6675 Jim_Obj
*compare_script
;
6680 /* This must be a valid list */
6681 compare_script
= Jim_DuplicateObj(sort_info
->interp
, sort_info
->command
);
6682 Jim_ListAppendElement(sort_info
->interp
, compare_script
, *lhsObj
);
6683 Jim_ListAppendElement(sort_info
->interp
, compare_script
, *rhsObj
);
6685 rc
= Jim_EvalObj(sort_info
->interp
, compare_script
);
6687 if (rc
!= JIM_OK
|| Jim_GetWide(sort_info
->interp
, Jim_GetResult(sort_info
->interp
), &ret
) != JIM_OK
) {
6688 longjmp(sort_info
->jmpbuf
, rc
);
6691 return JimSign(ret
) * sort_info
->order
;
6694 /* Remove duplicate elements from the (sorted) list in-place, according to the
6695 * comparison function, comp.
6697 * Note that the last unique value is kept, not the first
6699 static void ListRemoveDuplicates(Jim_Obj
*listObjPtr
, int (*comp
)(Jim_Obj
**lhs
, Jim_Obj
**rhs
))
6703 Jim_Obj
**ele
= listObjPtr
->internalRep
.listValue
.ele
;
6705 for (src
= 1; src
< listObjPtr
->internalRep
.listValue
.len
; src
++) {
6706 if (comp(&ele
[dst
], &ele
[src
]) == 0) {
6707 /* Match, so replace the dest with the current source */
6708 Jim_DecrRefCount(sort_info
->interp
, ele
[dst
]);
6711 /* No match, so keep the current source and move to the next destination */
6714 ele
[dst
] = ele
[src
];
6717 /* At end of list, keep the final element unless all elements were kept */
6719 if (dst
< listObjPtr
->internalRep
.listValue
.len
) {
6720 ele
[dst
] = ele
[src
];
6723 /* Set the new length */
6724 listObjPtr
->internalRep
.listValue
.len
= dst
;
6727 /* Sort a list *in place*. MUST be called with a non-shared list. */
6728 static int ListSortElements(Jim_Interp
*interp
, Jim_Obj
*listObjPtr
, struct lsort_info
*info
)
6730 struct lsort_info
*prev_info
;
6732 typedef int (qsort_comparator
) (const void *, const void *);
6733 int (*fn
) (Jim_Obj
**, Jim_Obj
**);
6738 JimPanic((Jim_IsShared(listObjPtr
), "ListSortElements called with shared object"));
6739 SetListFromAny(interp
, listObjPtr
);
6741 /* Allow lsort to be called reentrantly */
6742 prev_info
= sort_info
;
6745 vector
= listObjPtr
->internalRep
.listValue
.ele
;
6746 len
= listObjPtr
->internalRep
.listValue
.len
;
6747 switch (info
->type
) {
6748 case JIM_LSORT_ASCII
:
6749 fn
= ListSortString
;
6751 case JIM_LSORT_NOCASE
:
6752 fn
= ListSortStringNoCase
;
6754 case JIM_LSORT_INTEGER
:
6755 fn
= ListSortInteger
;
6757 case JIM_LSORT_REAL
:
6760 case JIM_LSORT_COMMAND
:
6761 fn
= ListSortCommand
;
6764 fn
= NULL
; /* avoid warning */
6765 JimPanic((1, "ListSort called with invalid sort type"));
6766 return -1; /* Should not be run but keeps static analysers happy */
6769 if (info
->indexed
) {
6770 /* Need to interpose a "list index" function */
6772 fn
= ListSortIndexHelper
;
6775 if ((rc
= setjmp(info
->jmpbuf
)) == 0) {
6776 qsort(vector
, len
, sizeof(Jim_Obj
*), (qsort_comparator
*) fn
);
6778 if (info
->unique
&& len
> 1) {
6779 ListRemoveDuplicates(listObjPtr
, fn
);
6782 Jim_InvalidateStringRep(listObjPtr
);
6784 sort_info
= prev_info
;
6789 /* This is the low-level function to insert elements into a list.
6790 * The higher-level Jim_ListInsertElements() performs shared object
6791 * check and invalidates the string repr. This version is used
6792 * in the internals of the List Object and is not exported.
6794 * NOTE: this function can be called only against objects
6795 * with internal type of List.
6797 * An insertion point (idx) of -1 means end-of-list.
6799 static void ListInsertElements(Jim_Obj
*listPtr
, int idx
, int elemc
, Jim_Obj
*const *elemVec
)
6801 int currentLen
= listPtr
->internalRep
.listValue
.len
;
6802 int requiredLen
= currentLen
+ elemc
;
6806 if (requiredLen
> listPtr
->internalRep
.listValue
.maxLen
) {
6807 if (requiredLen
< 2) {
6808 /* Don't do allocations of under 4 pointers. */
6815 listPtr
->internalRep
.listValue
.ele
= Jim_Realloc(listPtr
->internalRep
.listValue
.ele
,
6816 sizeof(Jim_Obj
*) * requiredLen
);
6818 listPtr
->internalRep
.listValue
.maxLen
= requiredLen
;
6823 point
= listPtr
->internalRep
.listValue
.ele
+ idx
;
6824 memmove(point
+ elemc
, point
, (currentLen
- idx
) * sizeof(Jim_Obj
*));
6825 for (i
= 0; i
< elemc
; ++i
) {
6826 point
[i
] = elemVec
[i
];
6827 Jim_IncrRefCount(point
[i
]);
6829 listPtr
->internalRep
.listValue
.len
+= elemc
;
6832 /* Convenience call to ListInsertElements() to append a single element.
6834 static void ListAppendElement(Jim_Obj
*listPtr
, Jim_Obj
*objPtr
)
6836 ListInsertElements(listPtr
, -1, 1, &objPtr
);
6839 /* Appends every element of appendListPtr into listPtr.
6840 * Both have to be of the list type.
6841 * Convenience call to ListInsertElements()
6843 static void ListAppendList(Jim_Obj
*listPtr
, Jim_Obj
*appendListPtr
)
6845 ListInsertElements(listPtr
, -1,
6846 appendListPtr
->internalRep
.listValue
.len
, appendListPtr
->internalRep
.listValue
.ele
);
6849 void Jim_ListAppendElement(Jim_Interp
*interp
, Jim_Obj
*listPtr
, Jim_Obj
*objPtr
)
6851 JimPanic((Jim_IsShared(listPtr
), "Jim_ListAppendElement called with shared object"));
6852 SetListFromAny(interp
, listPtr
);
6853 Jim_InvalidateStringRep(listPtr
);
6854 ListAppendElement(listPtr
, objPtr
);
6857 void Jim_ListAppendList(Jim_Interp
*interp
, Jim_Obj
*listPtr
, Jim_Obj
*appendListPtr
)
6859 JimPanic((Jim_IsShared(listPtr
), "Jim_ListAppendList called with shared object"));
6860 SetListFromAny(interp
, listPtr
);
6861 SetListFromAny(interp
, appendListPtr
);
6862 Jim_InvalidateStringRep(listPtr
);
6863 ListAppendList(listPtr
, appendListPtr
);
6866 int Jim_ListLength(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
6868 SetListFromAny(interp
, objPtr
);
6869 return objPtr
->internalRep
.listValue
.len
;
6872 void Jim_ListInsertElements(Jim_Interp
*interp
, Jim_Obj
*listPtr
, int idx
,
6873 int objc
, Jim_Obj
*const *objVec
)
6875 JimPanic((Jim_IsShared(listPtr
), "Jim_ListInsertElement called with shared object"));
6876 SetListFromAny(interp
, listPtr
);
6877 if (idx
>= 0 && idx
> listPtr
->internalRep
.listValue
.len
)
6878 idx
= listPtr
->internalRep
.listValue
.len
;
6881 Jim_InvalidateStringRep(listPtr
);
6882 ListInsertElements(listPtr
, idx
, objc
, objVec
);
6885 Jim_Obj
*Jim_ListGetIndex(Jim_Interp
*interp
, Jim_Obj
*listPtr
, int idx
)
6887 SetListFromAny(interp
, listPtr
);
6888 if ((idx
>= 0 && idx
>= listPtr
->internalRep
.listValue
.len
) ||
6889 (idx
< 0 && (-idx
- 1) >= listPtr
->internalRep
.listValue
.len
)) {
6893 idx
= listPtr
->internalRep
.listValue
.len
+ idx
;
6894 return listPtr
->internalRep
.listValue
.ele
[idx
];
6897 int Jim_ListIndex(Jim_Interp
*interp
, Jim_Obj
*listPtr
, int idx
, Jim_Obj
**objPtrPtr
, int flags
)
6899 *objPtrPtr
= Jim_ListGetIndex(interp
, listPtr
, idx
);
6900 if (*objPtrPtr
== NULL
) {
6901 if (flags
& JIM_ERRMSG
) {
6902 Jim_SetResultString(interp
, "list index out of range", -1);
6909 static int ListSetIndex(Jim_Interp
*interp
, Jim_Obj
*listPtr
, int idx
,
6910 Jim_Obj
*newObjPtr
, int flags
)
6912 SetListFromAny(interp
, listPtr
);
6913 if ((idx
>= 0 && idx
>= listPtr
->internalRep
.listValue
.len
) ||
6914 (idx
< 0 && (-idx
- 1) >= listPtr
->internalRep
.listValue
.len
)) {
6915 if (flags
& JIM_ERRMSG
) {
6916 Jim_SetResultString(interp
, "list index out of range", -1);
6921 idx
= listPtr
->internalRep
.listValue
.len
+ idx
;
6922 Jim_DecrRefCount(interp
, listPtr
->internalRep
.listValue
.ele
[idx
]);
6923 listPtr
->internalRep
.listValue
.ele
[idx
] = newObjPtr
;
6924 Jim_IncrRefCount(newObjPtr
);
6928 /* Modify the list stored in the variable named 'varNamePtr'
6929 * setting the element specified by the 'indexc' indexes objects in 'indexv',
6930 * with the new element 'newObjptr'. (implements the [lset] command) */
6931 int Jim_ListSetIndex(Jim_Interp
*interp
, Jim_Obj
*varNamePtr
,
6932 Jim_Obj
*const *indexv
, int indexc
, Jim_Obj
*newObjPtr
)
6934 Jim_Obj
*varObjPtr
, *objPtr
, *listObjPtr
;
6937 varObjPtr
= objPtr
= Jim_GetVariable(interp
, varNamePtr
, JIM_ERRMSG
| JIM_UNSHARED
);
6940 if ((shared
= Jim_IsShared(objPtr
)))
6941 varObjPtr
= objPtr
= Jim_DuplicateObj(interp
, objPtr
);
6942 for (i
= 0; i
< indexc
- 1; i
++) {
6943 listObjPtr
= objPtr
;
6944 if (Jim_GetIndex(interp
, indexv
[i
], &idx
) != JIM_OK
)
6946 if (Jim_ListIndex(interp
, listObjPtr
, idx
, &objPtr
, JIM_ERRMSG
) != JIM_OK
) {
6949 if (Jim_IsShared(objPtr
)) {
6950 objPtr
= Jim_DuplicateObj(interp
, objPtr
);
6951 ListSetIndex(interp
, listObjPtr
, idx
, objPtr
, JIM_NONE
);
6953 Jim_InvalidateStringRep(listObjPtr
);
6955 if (Jim_GetIndex(interp
, indexv
[indexc
- 1], &idx
) != JIM_OK
)
6957 if (ListSetIndex(interp
, objPtr
, idx
, newObjPtr
, JIM_ERRMSG
) == JIM_ERR
)
6959 Jim_InvalidateStringRep(objPtr
);
6960 Jim_InvalidateStringRep(varObjPtr
);
6961 if (Jim_SetVariable(interp
, varNamePtr
, varObjPtr
) != JIM_OK
)
6963 Jim_SetResult(interp
, varObjPtr
);
6967 Jim_FreeNewObj(interp
, varObjPtr
);
6972 Jim_Obj
*Jim_ListJoin(Jim_Interp
*interp
, Jim_Obj
*listObjPtr
, const char *joinStr
, int joinStrLen
)
6975 int listLen
= Jim_ListLength(interp
, listObjPtr
);
6976 Jim_Obj
*resObjPtr
= Jim_NewEmptyStringObj(interp
);
6978 for (i
= 0; i
< listLen
; ) {
6979 Jim_AppendObj(interp
, resObjPtr
, Jim_ListGetIndex(interp
, listObjPtr
, i
));
6980 if (++i
!= listLen
) {
6981 Jim_AppendString(interp
, resObjPtr
, joinStr
, joinStrLen
);
6987 Jim_Obj
*Jim_ConcatObj(Jim_Interp
*interp
, int objc
, Jim_Obj
*const *objv
)
6991 /* If all the objects in objv are lists,
6992 * it's possible to return a list as result, that's the
6993 * concatenation of all the lists. */
6994 for (i
= 0; i
< objc
; i
++) {
6995 if (!Jim_IsList(objv
[i
]))
6999 Jim_Obj
*objPtr
= Jim_NewListObj(interp
, NULL
, 0);
7001 for (i
= 0; i
< objc
; i
++)
7002 ListAppendList(objPtr
, objv
[i
]);
7006 /* Else... we have to glue strings together */
7007 int len
= 0, objLen
;
7010 /* Compute the length */
7011 for (i
= 0; i
< objc
; i
++) {
7012 len
+= Jim_Length(objv
[i
]);
7016 /* Create the string rep, and a string object holding it. */
7017 p
= bytes
= Jim_Alloc(len
+ 1);
7018 for (i
= 0; i
< objc
; i
++) {
7019 const char *s
= Jim_GetString(objv
[i
], &objLen
);
7021 /* Remove leading space */
7022 while (objLen
&& isspace(UCHAR(*s
))) {
7027 /* And trailing space */
7028 while (objLen
&& isspace(UCHAR(s
[objLen
- 1]))) {
7029 /* Handle trailing backslash-space case */
7030 if (objLen
> 1 && s
[objLen
- 2] == '\\') {
7036 memcpy(p
, s
, objLen
);
7038 if (i
+ 1 != objc
) {
7042 /* Drop the space calculated for this
7043 * element that is instead null. */
7049 return Jim_NewStringObjNoAlloc(interp
, bytes
, len
);
7053 /* Returns a list composed of the elements in the specified range.
7054 * first and start are directly accepted as Jim_Objects and
7055 * processed for the end?-index? case. */
7056 Jim_Obj
*Jim_ListRange(Jim_Interp
*interp
, Jim_Obj
*listObjPtr
, Jim_Obj
*firstObjPtr
,
7057 Jim_Obj
*lastObjPtr
)
7062 if (Jim_GetIndex(interp
, firstObjPtr
, &first
) != JIM_OK
||
7063 Jim_GetIndex(interp
, lastObjPtr
, &last
) != JIM_OK
)
7065 len
= Jim_ListLength(interp
, listObjPtr
); /* will convert into list */
7066 first
= JimRelToAbsIndex(len
, first
);
7067 last
= JimRelToAbsIndex(len
, last
);
7068 JimRelToAbsRange(len
, &first
, &last
, &rangeLen
);
7069 if (first
== 0 && last
== len
) {
7072 return Jim_NewListObj(interp
, listObjPtr
->internalRep
.listValue
.ele
+ first
, rangeLen
);
7075 /* -----------------------------------------------------------------------------
7077 * ---------------------------------------------------------------------------*/
7078 static void FreeDictInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
);
7079 static void DupDictInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
);
7080 static void UpdateStringOfDict(struct Jim_Obj
*objPtr
);
7081 static int SetDictFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
);
7083 /* Dict HashTable Type.
7085 * Keys and Values are Jim objects. */
7087 static unsigned int JimObjectHTHashFunction(const void *key
)
7090 const char *str
= Jim_GetString((Jim_Obj
*)key
, &len
);
7091 return Jim_GenHashFunction((const unsigned char *)str
, len
);
7094 static int JimObjectHTKeyCompare(void *privdata
, const void *key1
, const void *key2
)
7096 return Jim_StringEqObj((Jim_Obj
*)key1
, (Jim_Obj
*)key2
);
7099 static void *JimObjectHTKeyValDup(void *privdata
, const void *val
)
7101 Jim_IncrRefCount((Jim_Obj
*)val
);
7105 static void JimObjectHTKeyValDestructor(void *interp
, void *val
)
7107 Jim_DecrRefCount(interp
, (Jim_Obj
*)val
);
7110 static const Jim_HashTableType JimDictHashTableType
= {
7111 JimObjectHTHashFunction
, /* hash function */
7112 JimObjectHTKeyValDup
, /* key dup */
7113 JimObjectHTKeyValDup
, /* val dup */
7114 JimObjectHTKeyCompare
, /* key compare */
7115 JimObjectHTKeyValDestructor
, /* key destructor */
7116 JimObjectHTKeyValDestructor
/* val destructor */
7119 /* Note that while the elements of the dict may contain references,
7120 * the list object itself can't. This basically means that the
7121 * dict object string representation as a whole can't contain references
7122 * that are not presents in the single elements. */
7123 static const Jim_ObjType dictObjType
= {
7125 FreeDictInternalRep
,
7131 void FreeDictInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
7133 JIM_NOTUSED(interp
);
7135 Jim_FreeHashTable(objPtr
->internalRep
.ptr
);
7136 Jim_Free(objPtr
->internalRep
.ptr
);
7139 void DupDictInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
)
7141 Jim_HashTable
*ht
, *dupHt
;
7142 Jim_HashTableIterator htiter
;
7145 /* Create a new hash table */
7146 ht
= srcPtr
->internalRep
.ptr
;
7147 dupHt
= Jim_Alloc(sizeof(*dupHt
));
7148 Jim_InitHashTable(dupHt
, &JimDictHashTableType
, interp
);
7150 Jim_ExpandHashTable(dupHt
, ht
->size
);
7151 /* Copy every element from the source to the dup hash table */
7152 JimInitHashTableIterator(ht
, &htiter
);
7153 while ((he
= Jim_NextHashEntry(&htiter
)) != NULL
) {
7154 Jim_AddHashEntry(dupHt
, he
->key
, he
->u
.val
);
7157 dupPtr
->internalRep
.ptr
= dupHt
;
7158 dupPtr
->typePtr
= &dictObjType
;
7161 static Jim_Obj
**JimDictPairs(Jim_Obj
*dictPtr
, int *len
)
7164 Jim_HashTableIterator htiter
;
7169 ht
= dictPtr
->internalRep
.ptr
;
7171 /* Turn the hash table into a flat vector of Jim_Objects. */
7172 objv
= Jim_Alloc((ht
->used
* 2) * sizeof(Jim_Obj
*));
7173 JimInitHashTableIterator(ht
, &htiter
);
7175 while ((he
= Jim_NextHashEntry(&htiter
)) != NULL
) {
7176 objv
[i
++] = Jim_GetHashEntryKey(he
);
7177 objv
[i
++] = Jim_GetHashEntryVal(he
);
7183 static void UpdateStringOfDict(struct Jim_Obj
*objPtr
)
7185 /* Turn the hash table into a flat vector of Jim_Objects. */
7187 Jim_Obj
**objv
= JimDictPairs(objPtr
, &len
);
7189 /* And now generate the string rep as a list */
7190 JimMakeListStringRep(objPtr
, objv
, len
);
7195 static int SetDictFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
)
7199 if (objPtr
->typePtr
== &dictObjType
) {
7203 if (Jim_IsList(objPtr
) && Jim_IsShared(objPtr
)) {
7204 /* A shared list, so get the string representation now to avoid
7205 * changing the order in case of fast conversion to dict.
7210 /* For simplicity, convert a non-list object to a list and then to a dict */
7211 listlen
= Jim_ListLength(interp
, objPtr
);
7213 Jim_SetResultString(interp
, "missing value to go with key", -1);
7217 /* Converting from a list to a dict can't fail */
7221 ht
= Jim_Alloc(sizeof(*ht
));
7222 Jim_InitHashTable(ht
, &JimDictHashTableType
, interp
);
7224 for (i
= 0; i
< listlen
; i
+= 2) {
7225 Jim_Obj
*keyObjPtr
= Jim_ListGetIndex(interp
, objPtr
, i
);
7226 Jim_Obj
*valObjPtr
= Jim_ListGetIndex(interp
, objPtr
, i
+ 1);
7228 Jim_ReplaceHashEntry(ht
, keyObjPtr
, valObjPtr
);
7231 Jim_FreeIntRep(interp
, objPtr
);
7232 objPtr
->typePtr
= &dictObjType
;
7233 objPtr
->internalRep
.ptr
= ht
;
7239 /* Dict object API */
7241 /* Add an element to a dict. objPtr must be of the "dict" type.
7242 * The higher-level exported function is Jim_DictAddElement().
7243 * If an element with the specified key already exists, the value
7244 * associated is replaced with the new one.
7246 * if valueObjPtr == NULL, the key is instead removed if it exists. */
7247 static int DictAddElement(Jim_Interp
*interp
, Jim_Obj
*objPtr
,
7248 Jim_Obj
*keyObjPtr
, Jim_Obj
*valueObjPtr
)
7250 Jim_HashTable
*ht
= objPtr
->internalRep
.ptr
;
7252 if (valueObjPtr
== NULL
) { /* unset */
7253 return Jim_DeleteHashEntry(ht
, keyObjPtr
);
7255 Jim_ReplaceHashEntry(ht
, keyObjPtr
, valueObjPtr
);
7259 /* Add an element, higher-level interface for DictAddElement().
7260 * If valueObjPtr == NULL, the key is removed if it exists. */
7261 int Jim_DictAddElement(Jim_Interp
*interp
, Jim_Obj
*objPtr
,
7262 Jim_Obj
*keyObjPtr
, Jim_Obj
*valueObjPtr
)
7264 JimPanic((Jim_IsShared(objPtr
), "Jim_DictAddElement called with shared object"));
7265 if (SetDictFromAny(interp
, objPtr
) != JIM_OK
) {
7268 Jim_InvalidateStringRep(objPtr
);
7269 return DictAddElement(interp
, objPtr
, keyObjPtr
, valueObjPtr
);
7272 Jim_Obj
*Jim_NewDictObj(Jim_Interp
*interp
, Jim_Obj
*const *elements
, int len
)
7277 JimPanic((len
% 2, "Jim_NewDictObj() 'len' argument must be even"));
7279 objPtr
= Jim_NewObj(interp
);
7280 objPtr
->typePtr
= &dictObjType
;
7281 objPtr
->bytes
= NULL
;
7282 objPtr
->internalRep
.ptr
= Jim_Alloc(sizeof(Jim_HashTable
));
7283 Jim_InitHashTable(objPtr
->internalRep
.ptr
, &JimDictHashTableType
, interp
);
7284 for (i
= 0; i
< len
; i
+= 2)
7285 DictAddElement(interp
, objPtr
, elements
[i
], elements
[i
+ 1]);
7289 /* Return the value associated to the specified dict key
7290 * Returns JIM_OK if OK, JIM_ERR if entry not found or -1 if can't create dict value
7292 * Sets *objPtrPtr to non-NULL only upon success.
7294 int Jim_DictKey(Jim_Interp
*interp
, Jim_Obj
*dictPtr
, Jim_Obj
*keyPtr
,
7295 Jim_Obj
**objPtrPtr
, int flags
)
7300 if (SetDictFromAny(interp
, dictPtr
) != JIM_OK
) {
7303 ht
= dictPtr
->internalRep
.ptr
;
7304 if ((he
= Jim_FindHashEntry(ht
, keyPtr
)) == NULL
) {
7305 if (flags
& JIM_ERRMSG
) {
7306 Jim_SetResultFormatted(interp
, "key \"%#s\" not known in dictionary", keyPtr
);
7311 *objPtrPtr
= Jim_GetHashEntryVal(he
);
7316 /* Return an allocated array of key/value pairs for the dictionary. Stores the length in *len */
7317 int Jim_DictPairs(Jim_Interp
*interp
, Jim_Obj
*dictPtr
, Jim_Obj
***objPtrPtr
, int *len
)
7319 if (SetDictFromAny(interp
, dictPtr
) != JIM_OK
) {
7322 *objPtrPtr
= JimDictPairs(dictPtr
, len
);
7328 /* Return the value associated to the specified dict keys */
7329 int Jim_DictKeysVector(Jim_Interp
*interp
, Jim_Obj
*dictPtr
,
7330 Jim_Obj
*const *keyv
, int keyc
, Jim_Obj
**objPtrPtr
, int flags
)
7335 *objPtrPtr
= dictPtr
;
7339 for (i
= 0; i
< keyc
; i
++) {
7342 int rc
= Jim_DictKey(interp
, dictPtr
, keyv
[i
], &objPtr
, flags
);
7348 *objPtrPtr
= dictPtr
;
7352 /* Modify the dict stored into the variable named 'varNamePtr'
7353 * setting the element specified by the 'keyc' keys objects in 'keyv',
7354 * with the new value of the element 'newObjPtr'.
7356 * If newObjPtr == NULL the operation is to remove the given key
7357 * from the dictionary.
7359 * If flags & JIM_ERRMSG, then failure to remove the key is considered an error
7360 * and JIM_ERR is returned. Otherwise it is ignored and JIM_OK is returned.
7362 int Jim_SetDictKeysVector(Jim_Interp
*interp
, Jim_Obj
*varNamePtr
,
7363 Jim_Obj
*const *keyv
, int keyc
, Jim_Obj
*newObjPtr
, int flags
)
7365 Jim_Obj
*varObjPtr
, *objPtr
, *dictObjPtr
;
7368 varObjPtr
= objPtr
= Jim_GetVariable(interp
, varNamePtr
, flags
);
7369 if (objPtr
== NULL
) {
7370 if (newObjPtr
== NULL
&& (flags
& JIM_MUSTEXIST
)) {
7371 /* Cannot remove a key from non existing var */
7374 varObjPtr
= objPtr
= Jim_NewDictObj(interp
, NULL
, 0);
7375 if (Jim_SetVariable(interp
, varNamePtr
, objPtr
) != JIM_OK
) {
7376 Jim_FreeNewObj(interp
, varObjPtr
);
7380 if ((shared
= Jim_IsShared(objPtr
)))
7381 varObjPtr
= objPtr
= Jim_DuplicateObj(interp
, objPtr
);
7382 for (i
= 0; i
< keyc
; i
++) {
7383 dictObjPtr
= objPtr
;
7385 /* Check if it's a valid dictionary */
7386 if (SetDictFromAny(interp
, dictObjPtr
) != JIM_OK
) {
7390 if (i
== keyc
- 1) {
7391 /* Last key: Note that error on unset with missing last key is OK */
7392 if (Jim_DictAddElement(interp
, objPtr
, keyv
[keyc
- 1], newObjPtr
) != JIM_OK
) {
7393 if (newObjPtr
|| (flags
& JIM_MUSTEXIST
)) {
7400 /* Check if the given key exists. */
7401 Jim_InvalidateStringRep(dictObjPtr
);
7402 if (Jim_DictKey(interp
, dictObjPtr
, keyv
[i
], &objPtr
,
7403 newObjPtr
? JIM_NONE
: JIM_ERRMSG
) == JIM_OK
) {
7404 /* This key exists at the current level.
7405 * Make sure it's not shared!. */
7406 if (Jim_IsShared(objPtr
)) {
7407 objPtr
= Jim_DuplicateObj(interp
, objPtr
);
7408 DictAddElement(interp
, dictObjPtr
, keyv
[i
], objPtr
);
7412 /* Key not found. If it's an [unset] operation
7413 * this is an error. Only the last key may not
7415 if (newObjPtr
== NULL
) {
7418 /* Otherwise set an empty dictionary
7419 * as key's value. */
7420 objPtr
= Jim_NewDictObj(interp
, NULL
, 0);
7421 DictAddElement(interp
, dictObjPtr
, keyv
[i
], objPtr
);
7424 /* XXX: Is this necessary? */
7425 Jim_InvalidateStringRep(objPtr
);
7426 Jim_InvalidateStringRep(varObjPtr
);
7427 if (Jim_SetVariable(interp
, varNamePtr
, varObjPtr
) != JIM_OK
) {
7430 Jim_SetResult(interp
, varObjPtr
);
7434 Jim_FreeNewObj(interp
, varObjPtr
);
7439 /* -----------------------------------------------------------------------------
7441 * ---------------------------------------------------------------------------*/
7442 static void UpdateStringOfIndex(struct Jim_Obj
*objPtr
);
7443 static int SetIndexFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
);
7445 static const Jim_ObjType indexObjType
= {
7449 UpdateStringOfIndex
,
7453 static void UpdateStringOfIndex(struct Jim_Obj
*objPtr
)
7455 if (objPtr
->internalRep
.intValue
== -1) {
7456 JimSetStringBytes(objPtr
, "end");
7459 char buf
[JIM_INTEGER_SPACE
+ 1];
7460 if (objPtr
->internalRep
.intValue
>= 0) {
7461 sprintf(buf
, "%d", objPtr
->internalRep
.intValue
);
7465 sprintf(buf
, "end%d", objPtr
->internalRep
.intValue
+ 1);
7467 JimSetStringBytes(objPtr
, buf
);
7471 static int SetIndexFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
7477 /* Get the string representation */
7478 str
= Jim_String(objPtr
);
7480 /* Try to convert into an index */
7481 if (strncmp(str
, "end", 3) == 0) {
7487 idx
= jim_strtol(str
, &endptr
);
7489 if (endptr
== str
) {
7495 /* Now str may include or +<num> or -<num> */
7496 if (*str
== '+' || *str
== '-') {
7497 int sign
= (*str
== '+' ? 1 : -1);
7499 idx
+= sign
* jim_strtol(++str
, &endptr
);
7500 if (str
== endptr
|| *endptr
) {
7505 /* The only thing left should be spaces */
7506 while (isspace(UCHAR(*str
))) {
7517 /* end-1 is repesented as -2 */
7525 /* Free the old internal repr and set the new one. */
7526 Jim_FreeIntRep(interp
, objPtr
);
7527 objPtr
->typePtr
= &indexObjType
;
7528 objPtr
->internalRep
.intValue
= idx
;
7532 Jim_SetResultFormatted(interp
,
7533 "bad index \"%#s\": must be integer?[+-]integer? or end?[+-]integer?", objPtr
);
7537 int Jim_GetIndex(Jim_Interp
*interp
, Jim_Obj
*objPtr
, int *indexPtr
)
7539 /* Avoid shimmering if the object is an integer. */
7540 if (objPtr
->typePtr
== &intObjType
) {
7541 jim_wide val
= JimWideValue(objPtr
);
7544 *indexPtr
= -INT_MAX
;
7545 else if (val
> INT_MAX
)
7546 *indexPtr
= INT_MAX
;
7548 *indexPtr
= (int)val
;
7551 if (objPtr
->typePtr
!= &indexObjType
&& SetIndexFromAny(interp
, objPtr
) == JIM_ERR
)
7553 *indexPtr
= objPtr
->internalRep
.intValue
;
7557 /* -----------------------------------------------------------------------------
7558 * Return Code Object.
7559 * ---------------------------------------------------------------------------*/
7561 /* NOTE: These must be kept in the same order as JIM_OK, JIM_ERR, ... */
7562 static const char * const jimReturnCodes
[] = {
7574 #define jimReturnCodesSize (sizeof(jimReturnCodes)/sizeof(*jimReturnCodes) - 1)
7576 static const Jim_ObjType returnCodeObjType
= {
7584 /* Converts a (standard) return code to a string. Returns "?" for
7585 * non-standard return codes.
7587 const char *Jim_ReturnCode(int code
)
7589 if (code
< 0 || code
>= (int)jimReturnCodesSize
) {
7593 return jimReturnCodes
[code
];
7597 static int SetReturnCodeFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
7602 /* Try to convert into an integer */
7603 if (JimGetWideNoErr(interp
, objPtr
, &wideValue
) != JIM_ERR
)
7604 returnCode
= (int)wideValue
;
7605 else if (Jim_GetEnum(interp
, objPtr
, jimReturnCodes
, &returnCode
, NULL
, JIM_NONE
) != JIM_OK
) {
7606 Jim_SetResultFormatted(interp
, "expected return code but got \"%#s\"", objPtr
);
7609 /* Free the old internal repr and set the new one. */
7610 Jim_FreeIntRep(interp
, objPtr
);
7611 objPtr
->typePtr
= &returnCodeObjType
;
7612 objPtr
->internalRep
.intValue
= returnCode
;
7616 int Jim_GetReturnCode(Jim_Interp
*interp
, Jim_Obj
*objPtr
, int *intPtr
)
7618 if (objPtr
->typePtr
!= &returnCodeObjType
&& SetReturnCodeFromAny(interp
, objPtr
) == JIM_ERR
)
7620 *intPtr
= objPtr
->internalRep
.intValue
;
7624 /* -----------------------------------------------------------------------------
7625 * Expression Parsing
7626 * ---------------------------------------------------------------------------*/
7627 static int JimParseExprOperator(struct JimParserCtx
*pc
);
7628 static int JimParseExprNumber(struct JimParserCtx
*pc
);
7629 static int JimParseExprIrrational(struct JimParserCtx
*pc
);
7630 static int JimParseExprBoolean(struct JimParserCtx
*pc
);
7632 /* expr operator opcodes. */
7635 /* Continues on from the JIM_TT_ space */
7637 /* Binary operators (numbers) */
7638 JIM_EXPROP_MUL
= JIM_TT_EXPR_OP
, /* 20 */
7653 JIM_EXPROP_BITAND
, /* 35 */
7656 JIM_EXPROP_LOGICAND
, /* 38 */
7657 JIM_EXPROP_LOGICOR
, /* 39 */
7658 JIM_EXPROP_TERNARY
, /* 40 */
7659 JIM_EXPROP_COLON
, /* 41 */
7660 JIM_EXPROP_POW
, /* 42 */
7662 /* Binary operators (strings) */
7663 JIM_EXPROP_STREQ
, /* 43 */
7668 /* Unary operators (numbers) */
7669 JIM_EXPROP_NOT
, /* 47 */
7671 JIM_EXPROP_UNARYMINUS
,
7672 JIM_EXPROP_UNARYPLUS
,
7675 JIM_EXPROP_FUNC_INT
, /* 51 */
7676 JIM_EXPROP_FUNC_WIDE
,
7677 JIM_EXPROP_FUNC_ABS
,
7678 JIM_EXPROP_FUNC_DOUBLE
,
7679 JIM_EXPROP_FUNC_ROUND
,
7680 JIM_EXPROP_FUNC_RAND
,
7681 JIM_EXPROP_FUNC_SRAND
,
7683 /* math functions from libm */
7684 JIM_EXPROP_FUNC_SIN
, /* 65 */
7685 JIM_EXPROP_FUNC_COS
,
7686 JIM_EXPROP_FUNC_TAN
,
7687 JIM_EXPROP_FUNC_ASIN
,
7688 JIM_EXPROP_FUNC_ACOS
,
7689 JIM_EXPROP_FUNC_ATAN
,
7690 JIM_EXPROP_FUNC_ATAN2
,
7691 JIM_EXPROP_FUNC_SINH
,
7692 JIM_EXPROP_FUNC_COSH
,
7693 JIM_EXPROP_FUNC_TANH
,
7694 JIM_EXPROP_FUNC_CEIL
,
7695 JIM_EXPROP_FUNC_FLOOR
,
7696 JIM_EXPROP_FUNC_EXP
,
7697 JIM_EXPROP_FUNC_LOG
,
7698 JIM_EXPROP_FUNC_LOG10
,
7699 JIM_EXPROP_FUNC_SQRT
,
7700 JIM_EXPROP_FUNC_POW
,
7701 JIM_EXPROP_FUNC_HYPOT
,
7702 JIM_EXPROP_FUNC_FMOD
,
7705 /* A expression node is either a term or an operator
7706 * If a node is an operator, 'op' points to the details of the operator and it's terms.
7708 struct JimExprNode
{
7709 int type
; /* JIM_TT_xxx */
7710 struct Jim_Obj
*objPtr
; /* The object for a term, or NULL for an operator */
7712 struct JimExprNode
*left
; /* For all operators */
7713 struct JimExprNode
*right
; /* For binary operators */
7714 struct JimExprNode
*ternary
; /* For ternary operator only */
7717 /* Operators table */
7718 typedef struct Jim_ExprOperator
7721 int (*funcop
) (Jim_Interp
*interp
, struct JimExprNode
*opnode
);
7722 unsigned char precedence
;
7723 unsigned char arity
;
7725 unsigned char namelen
;
7728 static int JimExprGetTerm(Jim_Interp
*interp
, struct JimExprNode
*node
, Jim_Obj
**objPtrPtr
);
7729 static int JimExprGetTermBoolean(Jim_Interp
*interp
, struct JimExprNode
*node
);
7730 static int JimExprEvalTermNode(Jim_Interp
*interp
, struct JimExprNode
*node
);
7732 static int JimExprOpNumUnary(Jim_Interp
*interp
, struct JimExprNode
*node
)
7737 jim_wide wA
, wC
= 0;
7740 if ((rc
= JimExprGetTerm(interp
, node
->left
, &A
)) != JIM_OK
) {
7744 if ((A
->typePtr
!= &doubleObjType
|| A
->bytes
) && JimGetWideNoErr(interp
, A
, &wA
) == JIM_OK
) {
7745 switch (node
->type
) {
7746 case JIM_EXPROP_FUNC_INT
:
7747 case JIM_EXPROP_FUNC_WIDE
:
7748 case JIM_EXPROP_FUNC_ROUND
:
7749 case JIM_EXPROP_UNARYPLUS
:
7752 case JIM_EXPROP_FUNC_DOUBLE
:
7756 case JIM_EXPROP_FUNC_ABS
:
7757 wC
= wA
>= 0 ? wA
: -wA
;
7759 case JIM_EXPROP_UNARYMINUS
:
7762 case JIM_EXPROP_NOT
:
7769 else if ((rc
= Jim_GetDouble(interp
, A
, &dA
)) == JIM_OK
) {
7770 switch (node
->type
) {
7771 case JIM_EXPROP_FUNC_INT
:
7772 case JIM_EXPROP_FUNC_WIDE
:
7775 case JIM_EXPROP_FUNC_ROUND
:
7776 wC
= dA
< 0 ? (dA
- 0.5) : (dA
+ 0.5);
7778 case JIM_EXPROP_FUNC_DOUBLE
:
7779 case JIM_EXPROP_UNARYPLUS
:
7783 case JIM_EXPROP_FUNC_ABS
:
7784 #ifdef JIM_MATH_FUNCTIONS
7787 dC
= dA
>= 0 ? dA
: -dA
;
7791 case JIM_EXPROP_UNARYMINUS
:
7795 case JIM_EXPROP_NOT
:
7805 Jim_SetResultInt(interp
, wC
);
7808 Jim_SetResult(interp
, Jim_NewDoubleObj(interp
, dC
));
7812 Jim_DecrRefCount(interp
, A
);
7817 static double JimRandDouble(Jim_Interp
*interp
)
7820 JimRandomBytes(interp
, &x
, sizeof(x
));
7822 return (double)x
/ (unsigned long)~0;
7825 static int JimExprOpIntUnary(Jim_Interp
*interp
, struct JimExprNode
*node
)
7831 if ((rc
= JimExprGetTerm(interp
, node
->left
, &A
)) != JIM_OK
) {
7835 rc
= Jim_GetWide(interp
, A
, &wA
);
7837 switch (node
->type
) {
7838 case JIM_EXPROP_BITNOT
:
7839 Jim_SetResultInt(interp
, ~wA
);
7841 case JIM_EXPROP_FUNC_SRAND
:
7842 JimPrngSeed(interp
, (unsigned char *)&wA
, sizeof(wA
));
7843 Jim_SetResult(interp
, Jim_NewDoubleObj(interp
, JimRandDouble(interp
)));
7850 Jim_DecrRefCount(interp
, A
);
7855 static int JimExprOpNone(Jim_Interp
*interp
, struct JimExprNode
*node
)
7857 JimPanic((node
->type
!= JIM_EXPROP_FUNC_RAND
, "JimExprOpNone only support rand()"));
7859 Jim_SetResult(interp
, Jim_NewDoubleObj(interp
, JimRandDouble(interp
)));
7864 #ifdef JIM_MATH_FUNCTIONS
7865 static int JimExprOpDoubleUnary(Jim_Interp
*interp
, struct JimExprNode
*node
)
7871 if ((rc
= JimExprGetTerm(interp
, node
->left
, &A
)) != JIM_OK
) {
7875 rc
= Jim_GetDouble(interp
, A
, &dA
);
7877 switch (node
->type
) {
7878 case JIM_EXPROP_FUNC_SIN
:
7881 case JIM_EXPROP_FUNC_COS
:
7884 case JIM_EXPROP_FUNC_TAN
:
7887 case JIM_EXPROP_FUNC_ASIN
:
7890 case JIM_EXPROP_FUNC_ACOS
:
7893 case JIM_EXPROP_FUNC_ATAN
:
7896 case JIM_EXPROP_FUNC_SINH
:
7899 case JIM_EXPROP_FUNC_COSH
:
7902 case JIM_EXPROP_FUNC_TANH
:
7905 case JIM_EXPROP_FUNC_CEIL
:
7908 case JIM_EXPROP_FUNC_FLOOR
:
7911 case JIM_EXPROP_FUNC_EXP
:
7914 case JIM_EXPROP_FUNC_LOG
:
7917 case JIM_EXPROP_FUNC_LOG10
:
7920 case JIM_EXPROP_FUNC_SQRT
:
7926 Jim_SetResult(interp
, Jim_NewDoubleObj(interp
, dC
));
7929 Jim_DecrRefCount(interp
, A
);
7935 /* A binary operation on two ints */
7936 static int JimExprOpIntBin(Jim_Interp
*interp
, struct JimExprNode
*node
)
7942 if ((rc
= JimExprGetTerm(interp
, node
->left
, &A
)) != JIM_OK
) {
7945 if ((rc
= JimExprGetTerm(interp
, node
->right
, &B
)) != JIM_OK
) {
7946 Jim_DecrRefCount(interp
, A
);
7952 if (Jim_GetWide(interp
, A
, &wA
) == JIM_OK
&& Jim_GetWide(interp
, B
, &wB
) == JIM_OK
) {
7957 switch (node
->type
) {
7958 case JIM_EXPROP_LSHIFT
:
7961 case JIM_EXPROP_RSHIFT
:
7964 case JIM_EXPROP_BITAND
:
7967 case JIM_EXPROP_BITXOR
:
7970 case JIM_EXPROP_BITOR
:
7973 case JIM_EXPROP_MOD
:
7976 Jim_SetResultString(interp
, "Division by zero", -1);
7983 * This code is tricky: C doesn't guarantee much
7984 * about the quotient or remainder, but Tcl does.
7985 * The remainder always has the same sign as the
7986 * divisor and a smaller absolute value.
8004 case JIM_EXPROP_ROTL
:
8005 case JIM_EXPROP_ROTR
:{
8006 /* uint32_t would be better. But not everyone has inttypes.h? */
8007 unsigned long uA
= (unsigned long)wA
;
8008 unsigned long uB
= (unsigned long)wB
;
8009 const unsigned int S
= sizeof(unsigned long) * 8;
8011 /* Shift left by the word size or more is undefined. */
8014 if (node
->type
== JIM_EXPROP_ROTR
) {
8017 wC
= (unsigned long)(uA
<< uB
) | (uA
>> (S
- uB
));
8023 Jim_SetResultInt(interp
, wC
);
8026 Jim_DecrRefCount(interp
, A
);
8027 Jim_DecrRefCount(interp
, B
);
8033 /* A binary operation on two ints or two doubles (or two strings for some ops) */
8034 static int JimExprOpBin(Jim_Interp
*interp
, struct JimExprNode
*node
)
8037 double dA
, dB
, dC
= 0;
8038 jim_wide wA
, wB
, wC
= 0;
8041 if ((rc
= JimExprGetTerm(interp
, node
->left
, &A
)) != JIM_OK
) {
8044 if ((rc
= JimExprGetTerm(interp
, node
->right
, &B
)) != JIM_OK
) {
8045 Jim_DecrRefCount(interp
, A
);
8049 if ((A
->typePtr
!= &doubleObjType
|| A
->bytes
) &&
8050 (B
->typePtr
!= &doubleObjType
|| B
->bytes
) &&
8051 JimGetWideNoErr(interp
, A
, &wA
) == JIM_OK
&& JimGetWideNoErr(interp
, B
, &wB
) == JIM_OK
) {
8055 switch (node
->type
) {
8056 case JIM_EXPROP_POW
:
8057 case JIM_EXPROP_FUNC_POW
:
8058 if (wA
== 0 && wB
< 0) {
8059 Jim_SetResultString(interp
, "exponentiation of zero by negative power", -1);
8063 wC
= JimPowWide(wA
, wB
);
8065 case JIM_EXPROP_ADD
:
8068 case JIM_EXPROP_SUB
:
8071 case JIM_EXPROP_MUL
:
8074 case JIM_EXPROP_DIV
:
8076 Jim_SetResultString(interp
, "Division by zero", -1);
8084 * This code is tricky: C doesn't guarantee much
8085 * about the quotient or remainder, but Tcl does.
8086 * The remainder always has the same sign as the
8087 * divisor and a smaller absolute value.
8105 case JIM_EXPROP_LTE
:
8108 case JIM_EXPROP_GTE
:
8111 case JIM_EXPROP_NUMEQ
:
8114 case JIM_EXPROP_NUMNE
:
8119 if (Jim_GetDouble(interp
, A
, &dA
) == JIM_OK
&& Jim_GetDouble(interp
, B
, &dB
) == JIM_OK
) {
8120 switch (node
->type
) {
8121 #ifndef JIM_MATH_FUNCTIONS
8122 case JIM_EXPROP_POW
:
8123 case JIM_EXPROP_FUNC_POW
:
8124 case JIM_EXPROP_FUNC_ATAN2
:
8125 case JIM_EXPROP_FUNC_HYPOT
:
8126 case JIM_EXPROP_FUNC_FMOD
:
8127 Jim_SetResultString(interp
, "unsupported", -1);
8131 case JIM_EXPROP_POW
:
8132 case JIM_EXPROP_FUNC_POW
:
8135 case JIM_EXPROP_FUNC_ATAN2
:
8138 case JIM_EXPROP_FUNC_HYPOT
:
8141 case JIM_EXPROP_FUNC_FMOD
:
8145 case JIM_EXPROP_ADD
:
8148 case JIM_EXPROP_SUB
:
8151 case JIM_EXPROP_MUL
:
8154 case JIM_EXPROP_DIV
:
8157 dC
= dA
< 0 ? -INFINITY
: INFINITY
;
8159 dC
= (dA
< 0 ? -1.0 : 1.0) * strtod("Inf", NULL
);
8172 case JIM_EXPROP_LTE
:
8175 case JIM_EXPROP_GTE
:
8178 case JIM_EXPROP_NUMEQ
:
8181 case JIM_EXPROP_NUMNE
:
8187 /* Handle the string case */
8189 /* XXX: Could optimise the eq/ne case by checking lengths */
8190 int i
= Jim_StringCompareObj(interp
, A
, B
, 0);
8192 switch (node
->type
) {
8199 case JIM_EXPROP_LTE
:
8202 case JIM_EXPROP_GTE
:
8205 case JIM_EXPROP_NUMEQ
:
8208 case JIM_EXPROP_NUMNE
:
8213 /* If we get here, it is an error */
8216 Jim_DecrRefCount(interp
, A
);
8217 Jim_DecrRefCount(interp
, B
);
8220 Jim_SetResultInt(interp
, wC
);
8223 Jim_SetResult(interp
, Jim_NewDoubleObj(interp
, dC
));
8227 static int JimSearchList(Jim_Interp
*interp
, Jim_Obj
*listObjPtr
, Jim_Obj
*valObj
)
8232 listlen
= Jim_ListLength(interp
, listObjPtr
);
8233 for (i
= 0; i
< listlen
; i
++) {
8234 if (Jim_StringEqObj(Jim_ListGetIndex(interp
, listObjPtr
, i
), valObj
)) {
8243 static int JimExprOpStrBin(Jim_Interp
*interp
, struct JimExprNode
*node
)
8249 if ((rc
= JimExprGetTerm(interp
, node
->left
, &A
)) != JIM_OK
) {
8252 if ((rc
= JimExprGetTerm(interp
, node
->right
, &B
)) != JIM_OK
) {
8253 Jim_DecrRefCount(interp
, A
);
8257 switch (node
->type
) {
8258 case JIM_EXPROP_STREQ
:
8259 case JIM_EXPROP_STRNE
:
8260 wC
= Jim_StringEqObj(A
, B
);
8261 if (node
->type
== JIM_EXPROP_STRNE
) {
8265 case JIM_EXPROP_STRIN
:
8266 wC
= JimSearchList(interp
, B
, A
);
8268 case JIM_EXPROP_STRNI
:
8269 wC
= !JimSearchList(interp
, B
, A
);
8274 Jim_SetResultInt(interp
, wC
);
8276 Jim_DecrRefCount(interp
, A
);
8277 Jim_DecrRefCount(interp
, B
);
8282 static int ExprBool(Jim_Interp
*interp
, Jim_Obj
*obj
)
8289 /* In case the object is interp->result with refcount 1*/
8290 Jim_IncrRefCount(obj
);
8292 if (Jim_GetLong(interp
, obj
, &l
) == JIM_OK
) {
8295 else if (Jim_GetDouble(interp
, obj
, &d
) == JIM_OK
) {
8298 else if (Jim_GetBoolean(interp
, obj
, &b
) == JIM_OK
) {
8302 Jim_DecrRefCount(interp
, obj
);
8306 static int JimExprOpAnd(Jim_Interp
*interp
, struct JimExprNode
*node
)
8309 int result
= JimExprGetTermBoolean(interp
, node
->left
);
8312 /* true so evaluate right */
8313 result
= JimExprGetTermBoolean(interp
, node
->right
);
8318 Jim_SetResultInt(interp
, result
);
8322 static int JimExprOpOr(Jim_Interp
*interp
, struct JimExprNode
*node
)
8325 int result
= JimExprGetTermBoolean(interp
, node
->left
);
8328 /* false so evaluate right */
8329 result
= JimExprGetTermBoolean(interp
, node
->right
);
8334 Jim_SetResultInt(interp
, result
);
8338 static int JimExprOpTernary(Jim_Interp
*interp
, struct JimExprNode
*node
)
8341 int result
= JimExprGetTermBoolean(interp
, node
->left
);
8344 /* true so select right */
8345 return JimExprEvalTermNode(interp
, node
->right
);
8347 else if (result
== 0) {
8348 /* false so select ternary */
8349 return JimExprEvalTermNode(interp
, node
->ternary
);
8357 OP_FUNC
= 0x0001, /* function syntax */
8358 OP_RIGHT_ASSOC
= 0x0002, /* right associative */
8361 /* name - precedence - arity - opcode
8363 * This array *must* be kept in sync with the JIM_EXPROP enum.
8365 * The following macros pre-compute the string length at compile time.
8367 #define OPRINIT_ATTR(N, P, ARITY, F, ATTR) {N, F, P, ARITY, ATTR, sizeof(N) - 1}
8368 #define OPRINIT(N, P, ARITY, F) OPRINIT_ATTR(N, P, ARITY, F, 0)
8370 static const struct Jim_ExprOperator Jim_ExprOperators
[] = {
8371 OPRINIT("*", 110, 2, JimExprOpBin
),
8372 OPRINIT("/", 110, 2, JimExprOpBin
),
8373 OPRINIT("%", 110, 2, JimExprOpIntBin
),
8375 OPRINIT("-", 100, 2, JimExprOpBin
),
8376 OPRINIT("+", 100, 2, JimExprOpBin
),
8378 OPRINIT("<<", 90, 2, JimExprOpIntBin
),
8379 OPRINIT(">>", 90, 2, JimExprOpIntBin
),
8381 OPRINIT("<<<", 90, 2, JimExprOpIntBin
),
8382 OPRINIT(">>>", 90, 2, JimExprOpIntBin
),
8384 OPRINIT("<", 80, 2, JimExprOpBin
),
8385 OPRINIT(">", 80, 2, JimExprOpBin
),
8386 OPRINIT("<=", 80, 2, JimExprOpBin
),
8387 OPRINIT(">=", 80, 2, JimExprOpBin
),
8389 OPRINIT("==", 70, 2, JimExprOpBin
),
8390 OPRINIT("!=", 70, 2, JimExprOpBin
),
8392 OPRINIT("&", 50, 2, JimExprOpIntBin
),
8393 OPRINIT("^", 49, 2, JimExprOpIntBin
),
8394 OPRINIT("|", 48, 2, JimExprOpIntBin
),
8396 OPRINIT("&&", 10, 2, JimExprOpAnd
),
8397 OPRINIT("||", 9, 2, JimExprOpOr
),
8398 OPRINIT_ATTR("?", 5, 3, JimExprOpTernary
, OP_RIGHT_ASSOC
),
8399 OPRINIT_ATTR(":", 5, 3, NULL
, OP_RIGHT_ASSOC
),
8401 /* Precedence is higher than * and / but lower than ! and ~, and right-associative */
8402 OPRINIT_ATTR("**", 120, 2, JimExprOpBin
, OP_RIGHT_ASSOC
),
8404 OPRINIT("eq", 60, 2, JimExprOpStrBin
),
8405 OPRINIT("ne", 60, 2, JimExprOpStrBin
),
8407 OPRINIT("in", 55, 2, JimExprOpStrBin
),
8408 OPRINIT("ni", 55, 2, JimExprOpStrBin
),
8410 OPRINIT_ATTR("!", 150, 1, JimExprOpNumUnary
, OP_RIGHT_ASSOC
),
8411 OPRINIT_ATTR("~", 150, 1, JimExprOpIntUnary
, OP_RIGHT_ASSOC
),
8412 OPRINIT_ATTR(" -", 150, 1, JimExprOpNumUnary
, OP_RIGHT_ASSOC
),
8413 OPRINIT_ATTR(" +", 150, 1, JimExprOpNumUnary
, OP_RIGHT_ASSOC
),
8417 OPRINIT_ATTR("int", 200, 1, JimExprOpNumUnary
, OP_FUNC
),
8418 OPRINIT_ATTR("wide", 200, 1, JimExprOpNumUnary
, OP_FUNC
),
8419 OPRINIT_ATTR("abs", 200, 1, JimExprOpNumUnary
, OP_FUNC
),
8420 OPRINIT_ATTR("double", 200, 1, JimExprOpNumUnary
, OP_FUNC
),
8421 OPRINIT_ATTR("round", 200, 1, JimExprOpNumUnary
, OP_FUNC
),
8422 OPRINIT_ATTR("rand", 200, 0, JimExprOpNone
, OP_FUNC
),
8423 OPRINIT_ATTR("srand", 200, 1, JimExprOpIntUnary
, OP_FUNC
),
8425 #ifdef JIM_MATH_FUNCTIONS
8426 OPRINIT_ATTR("sin", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8427 OPRINIT_ATTR("cos", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8428 OPRINIT_ATTR("tan", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8429 OPRINIT_ATTR("asin", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8430 OPRINIT_ATTR("acos", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8431 OPRINIT_ATTR("atan", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8432 OPRINIT_ATTR("atan2", 200, 2, JimExprOpBin
, OP_FUNC
),
8433 OPRINIT_ATTR("sinh", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8434 OPRINIT_ATTR("cosh", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8435 OPRINIT_ATTR("tanh", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8436 OPRINIT_ATTR("ceil", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8437 OPRINIT_ATTR("floor", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8438 OPRINIT_ATTR("exp", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8439 OPRINIT_ATTR("log", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8440 OPRINIT_ATTR("log10", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8441 OPRINIT_ATTR("sqrt", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8442 OPRINIT_ATTR("pow", 200, 2, JimExprOpBin
, OP_FUNC
),
8443 OPRINIT_ATTR("hypot", 200, 2, JimExprOpBin
, OP_FUNC
),
8444 OPRINIT_ATTR("fmod", 200, 2, JimExprOpBin
, OP_FUNC
),
8450 #define JIM_EXPR_OPERATORS_NUM \
8451 (sizeof(Jim_ExprOperators)/sizeof(struct Jim_ExprOperator))
8453 static int JimParseExpression(struct JimParserCtx
*pc
)
8455 /* Discard spaces and quoted newline */
8456 while (isspace(UCHAR(*pc
->p
)) || (*(pc
->p
) == '\\' && *(pc
->p
+ 1) == '\n')) {
8457 if (*pc
->p
== '\n') {
8465 pc
->tline
= pc
->linenr
;
8470 pc
->tt
= JIM_TT_EOL
;
8476 pc
->tt
= JIM_TT_SUBEXPR_START
;
8479 pc
->tt
= JIM_TT_SUBEXPR_END
;
8482 pc
->tt
= JIM_TT_SUBEXPR_COMMA
;
8489 return JimParseCmd(pc
);
8491 if (JimParseVar(pc
) == JIM_ERR
)
8492 return JimParseExprOperator(pc
);
8494 /* Don't allow expr sugar in expressions */
8495 if (pc
->tt
== JIM_TT_EXPRSUGAR
) {
8512 return JimParseExprNumber(pc
);
8514 return JimParseQuote(pc
);
8516 return JimParseBrace(pc
);
8522 if (JimParseExprIrrational(pc
) == JIM_ERR
)
8523 if (JimParseExprBoolean(pc
) == JIM_ERR
)
8524 return JimParseExprOperator(pc
);
8530 if (JimParseExprBoolean(pc
) == JIM_ERR
)
8531 return JimParseExprOperator(pc
);
8534 return JimParseExprOperator(pc
);
8540 static int JimParseExprNumber(struct JimParserCtx
*pc
)
8544 /* Assume an integer for now */
8545 pc
->tt
= JIM_TT_EXPR_INT
;
8547 jim_strtoull(pc
->p
, (char **)&pc
->p
);
8548 /* Tried as an integer, but perhaps it parses as a double */
8549 if (strchr("eENnIi.", *pc
->p
) || pc
->p
== pc
->tstart
) {
8550 /* Some stupid compilers insist they are cleverer that
8551 * we are. Even a (void) cast doesn't prevent this warning!
8553 if (strtod(pc
->tstart
, &end
)) { /* nothing */ }
8554 if (end
== pc
->tstart
)
8557 /* Yes, double captured more chars */
8558 pc
->tt
= JIM_TT_EXPR_DOUBLE
;
8562 pc
->tend
= pc
->p
- 1;
8563 pc
->len
-= (pc
->p
- pc
->tstart
);
8567 static int JimParseExprIrrational(struct JimParserCtx
*pc
)
8569 const char *irrationals
[] = { "NaN", "nan", "NAN", "Inf", "inf", "INF", NULL
};
8572 for (i
= 0; irrationals
[i
]; i
++) {
8573 const char *irr
= irrationals
[i
];
8575 if (strncmp(irr
, pc
->p
, 3) == 0) {
8578 pc
->tend
= pc
->p
- 1;
8579 pc
->tt
= JIM_TT_EXPR_DOUBLE
;
8586 static int JimParseExprBoolean(struct JimParserCtx
*pc
)
8588 const char *booleans
[] = { "false", "no", "off", "true", "yes", "on", NULL
};
8589 const int lengths
[] = { 5, 2, 3, 4, 3, 2, 0 };
8592 for (i
= 0; booleans
[i
]; i
++) {
8593 const char *boolean
= booleans
[i
];
8594 int length
= lengths
[i
];
8596 if (strncmp(boolean
, pc
->p
, length
) == 0) {
8599 pc
->tend
= pc
->p
- 1;
8600 pc
->tt
= JIM_TT_EXPR_BOOLEAN
;
8607 static const struct Jim_ExprOperator
*JimExprOperatorInfoByOpcode(int opcode
)
8609 static Jim_ExprOperator dummy_op
;
8610 if (opcode
< JIM_TT_EXPR_OP
) {
8613 return &Jim_ExprOperators
[opcode
- JIM_TT_EXPR_OP
];
8616 static int JimParseExprOperator(struct JimParserCtx
*pc
)
8619 const struct Jim_ExprOperator
*bestOp
= NULL
;
8622 /* Try to get the longest match. */
8623 for (i
= 0; i
< (signed)JIM_EXPR_OPERATORS_NUM
; i
++) {
8624 const struct Jim_ExprOperator
*op
= &Jim_ExprOperators
[i
];
8626 if (op
->name
[0] != pc
->p
[0]) {
8630 if (op
->namelen
> bestLen
&& strncmp(op
->name
, pc
->p
, op
->namelen
) == 0) {
8632 bestLen
= op
->namelen
;
8635 if (bestOp
== NULL
) {
8639 /* Validate paretheses around function arguments */
8640 if (bestOp
->attr
& OP_FUNC
) {
8641 const char *p
= pc
->p
+ bestLen
;
8642 int len
= pc
->len
- bestLen
;
8644 while (len
&& isspace(UCHAR(*p
))) {
8652 pc
->tend
= pc
->p
+ bestLen
- 1;
8656 pc
->tt
= (bestOp
- Jim_ExprOperators
) + JIM_TT_EXPR_OP
;
8660 const char *jim_tt_name(int type
)
8662 static const char * const tt_names
[JIM_TT_EXPR_OP
] =
8663 { "NIL", "STR", "ESC", "VAR", "ARY", "CMD", "SEP", "EOL", "EOF", "LIN", "WRD", "(((", ")))", ",,,", "INT",
8664 "DBL", "BOO", "$()" };
8665 if (type
< JIM_TT_EXPR_OP
) {
8666 return tt_names
[type
];
8668 else if (type
== JIM_EXPROP_UNARYMINUS
) {
8671 else if (type
== JIM_EXPROP_UNARYPLUS
) {
8675 const struct Jim_ExprOperator
*op
= JimExprOperatorInfoByOpcode(type
);
8676 static char buf
[20];
8681 sprintf(buf
, "(%d)", type
);
8686 /* -----------------------------------------------------------------------------
8688 * ---------------------------------------------------------------------------*/
8689 static void FreeExprInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
);
8690 static void DupExprInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
);
8691 static int SetExprFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
);
8693 static const Jim_ObjType exprObjType
= {
8695 FreeExprInternalRep
,
8698 JIM_TYPE_REFERENCES
,
8701 /* expr tree structure */
8704 struct JimExprNode
*expr
; /* The first operator or term */
8705 struct JimExprNode
*nodes
; /* Storage of all nodes in the tree */
8706 int len
; /* Number of nodes in use */
8707 int inUse
; /* Used for sharing. */
8710 static void ExprTreeFreeNodes(Jim_Interp
*interp
, struct JimExprNode
*nodes
, int num
)
8713 for (i
= 0; i
< num
; i
++) {
8714 if (nodes
[i
].objPtr
) {
8715 Jim_DecrRefCount(interp
, nodes
[i
].objPtr
);
8721 static void ExprTreeFree(Jim_Interp
*interp
, struct ExprTree
*expr
)
8723 ExprTreeFreeNodes(interp
, expr
->nodes
, expr
->len
);
8727 static void FreeExprInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
8729 struct ExprTree
*expr
= (void *)objPtr
->internalRep
.ptr
;
8732 if (--expr
->inUse
!= 0) {
8736 ExprTreeFree(interp
, expr
);
8740 static void DupExprInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
)
8742 JIM_NOTUSED(interp
);
8743 JIM_NOTUSED(srcPtr
);
8745 /* Just returns an simple string. */
8746 dupPtr
->typePtr
= NULL
;
8749 struct ExprBuilder
{
8750 int parencount
; /* count of outstanding parentheses */
8751 int level
; /* recursion depth */
8752 ParseToken
*token
; /* The current token */
8753 ParseToken
*first_token
; /* The first token */
8754 Jim_Stack stack
; /* stack of pending terms */
8755 Jim_Obj
*exprObjPtr
; /* the original expression */
8756 Jim_Obj
*fileNameObj
; /* filename of the original expression */
8757 struct JimExprNode
*nodes
; /* storage for all nodes */
8758 struct JimExprNode
*next
; /* storage for the next node */
8761 #ifdef DEBUG_SHOW_EXPR
8762 static void JimShowExprNode(struct JimExprNode
*node
, int level
)
8765 for (i
= 0; i
< level
; i
++) {
8768 if (TOKEN_IS_EXPR_OP(node
->type
)) {
8769 printf("%s\n", jim_tt_name(node
->type
));
8771 JimShowExprNode(node
->left
, level
+ 1);
8774 JimShowExprNode(node
->right
, level
+ 1);
8776 if (node
->ternary
) {
8777 JimShowExprNode(node
->ternary
, level
+ 1);
8781 printf("[%s] %s\n", jim_tt_name(node
->type
), Jim_String(node
->objPtr
));
8786 #define EXPR_UNTIL_CLOSE 0x0001
8787 #define EXPR_FUNC_ARGS 0x0002
8788 #define EXPR_TERNARY 0x0004
8791 * Parse the subexpression at builder->token and return with the node on the stack.
8792 * builder->token is advanced to the next unconsumed token.
8793 * Returns JIM_OK if OK or JIM_ERR on error and leaves a message in the interpreter result.
8795 * 'precedence' is the precedence of the current operator. Tokens are consumed until an operator
8796 * with an equal or lower precedence is reached (or strictly lower if right associative).
8798 * If EXPR_UNTIL_CLOSE is set, the subexpression extends up to and including the next close parenthesis.
8799 * If EXPR_FUNC_ARGS is set, multiple subexpressions (terms) are expected separated by comma
8800 * If EXPR_TERNARY is set, two subexpressions (terms) are expected separated by colon
8802 * 'exp_numterms' indicates how many terms are expected. Normally this is 1, but may be more for EXPR_FUNC_ARGS and EXPR_TERNARY.
8804 static int ExprTreeBuildTree(Jim_Interp
*interp
, struct ExprBuilder
*builder
, int precedence
, int flags
, int exp_numterms
)
8807 struct JimExprNode
*node
;
8808 /* Calculate the stack length expected after pushing the number of expected terms */
8809 int exp_stacklen
= builder
->stack
.len
+ exp_numterms
;
8811 if (builder
->level
++ > 200) {
8812 Jim_SetResultString(interp
, "Expression too complex", -1);
8816 while (builder
->token
->type
!= JIM_TT_EOL
) {
8817 ParseToken
*t
= builder
->token
++;
8820 if (t
== builder
->first_token
) {
8821 prevtt
= JIM_TT_NONE
;
8824 prevtt
= t
[-1].type
;
8827 if (t
->type
== JIM_TT_SUBEXPR_START
) {
8828 if (builder
->stack
.len
== exp_stacklen
) {
8829 Jim_SetResultFormatted(interp
, "unexpected open parenthesis in expression: \"%#s\"", builder
->exprObjPtr
);
8832 builder
->parencount
++;
8833 rc
= ExprTreeBuildTree(interp
, builder
, 0, EXPR_UNTIL_CLOSE
, 1);
8837 /* A complete subexpression is on the stack */
8839 else if (t
->type
== JIM_TT_SUBEXPR_END
) {
8840 if (!(flags
& EXPR_UNTIL_CLOSE
)) {
8841 if (builder
->stack
.len
== exp_stacklen
&& builder
->level
> 1) {
8846 Jim_SetResultFormatted(interp
, "unexpected closing parenthesis in expression: \"%#s\"", builder
->exprObjPtr
);
8849 builder
->parencount
--;
8850 if (builder
->stack
.len
== exp_stacklen
) {
8851 /* Return with the expected number of subexpressions on the stack */
8855 else if (t
->type
== JIM_TT_SUBEXPR_COMMA
) {
8856 if (!(flags
& EXPR_FUNC_ARGS
)) {
8857 if (builder
->stack
.len
== exp_stacklen
) {
8858 /* handle the comma back at the parent level */
8863 Jim_SetResultFormatted(interp
, "unexpected comma in expression: \"%#s\"", builder
->exprObjPtr
);
8867 /* If we see more terms than expected, it is an error */
8868 if (builder
->stack
.len
> exp_stacklen
) {
8869 Jim_SetResultFormatted(interp
, "too many arguments to math function");
8873 /* just go onto the next arg */
8875 else if (t
->type
== JIM_EXPROP_COLON
) {
8876 if (!(flags
& EXPR_TERNARY
)) {
8877 if (builder
->level
!= 1) {
8878 /* handle the comma back at the parent level */
8883 Jim_SetResultFormatted(interp
, ": without ? in expression: \"%#s\"", builder
->exprObjPtr
);
8886 if (builder
->stack
.len
== exp_stacklen
) {
8887 /* handle the comma back at the parent level */
8892 /* just go onto the next term */
8894 else if (TOKEN_IS_EXPR_OP(t
->type
)) {
8895 const struct Jim_ExprOperator
*op
;
8897 /* Convert -/+ to unary minus or unary plus if necessary */
8898 if (TOKEN_IS_EXPR_OP(prevtt
) || TOKEN_IS_EXPR_START(prevtt
)) {
8899 if (t
->type
== JIM_EXPROP_SUB
) {
8900 t
->type
= JIM_EXPROP_UNARYMINUS
;
8902 else if (t
->type
== JIM_EXPROP_ADD
) {
8903 t
->type
= JIM_EXPROP_UNARYPLUS
;
8907 op
= JimExprOperatorInfoByOpcode(t
->type
);
8909 if (op
->precedence
< precedence
|| (!(op
->attr
& OP_RIGHT_ASSOC
) && op
->precedence
== precedence
)) {
8910 /* next op is lower precedence, or equal and left associative, so done here */
8915 if (op
->attr
& OP_FUNC
) {
8916 if (builder
->token
->type
!= JIM_TT_SUBEXPR_START
) {
8917 Jim_SetResultString(interp
, "missing arguments for math function", -1);
8921 if (op
->arity
== 0) {
8922 if (builder
->token
->type
!= JIM_TT_SUBEXPR_END
) {
8923 Jim_SetResultString(interp
, "too many arguments for math function", -1);
8929 builder
->parencount
++;
8931 /* This will push left and return right */
8932 rc
= ExprTreeBuildTree(interp
, builder
, 0, EXPR_FUNC_ARGS
| EXPR_UNTIL_CLOSE
, op
->arity
);
8934 else if (t
->type
== JIM_EXPROP_TERNARY
) {
8935 /* Collect the two arguments to the ternary operator */
8936 rc
= ExprTreeBuildTree(interp
, builder
, op
->precedence
, EXPR_TERNARY
, 2);
8939 /* Recursively handle everything on the right until we see a precendence <= op->precedence or == and right associative
8940 * and push that on the term stack
8942 rc
= ExprTreeBuildTree(interp
, builder
, op
->precedence
, 0, 1);
8950 node
= builder
->next
++;
8951 node
->type
= t
->type
;
8953 if (op
->arity
>= 3) {
8954 node
->ternary
= Jim_StackPop(&builder
->stack
);
8955 if (node
->ternary
== NULL
) {
8956 goto missingoperand
;
8959 if (op
->arity
>= 2) {
8960 node
->right
= Jim_StackPop(&builder
->stack
);
8961 if (node
->right
== NULL
) {
8962 goto missingoperand
;
8965 if (op
->arity
>= 1) {
8966 node
->left
= Jim_StackPop(&builder
->stack
);
8967 if (node
->left
== NULL
) {
8969 Jim_SetResultFormatted(interp
, "missing operand to %s in expression: \"%#s\"", op
->name
, builder
->exprObjPtr
);
8976 /* Now push the node */
8977 Jim_StackPush(&builder
->stack
, node
);
8980 Jim_Obj
*objPtr
= NULL
;
8982 /* This is a simple non-operator term, so create and push the appropriate object */
8984 /* Two consecutive terms without an operator is invalid */
8985 if (!TOKEN_IS_EXPR_START(prevtt
) && !TOKEN_IS_EXPR_OP(prevtt
)) {
8986 Jim_SetResultFormatted(interp
, "missing operator in expression: \"%#s\"", builder
->exprObjPtr
);
8990 /* Immediately create a double or int object? */
8991 if (t
->type
== JIM_TT_EXPR_INT
|| t
->type
== JIM_TT_EXPR_DOUBLE
) {
8993 if (t
->type
== JIM_TT_EXPR_INT
) {
8994 objPtr
= Jim_NewIntObj(interp
, jim_strtoull(t
->token
, &endptr
));
8997 objPtr
= Jim_NewDoubleObj(interp
, strtod(t
->token
, &endptr
));
8999 if (endptr
!= t
->token
+ t
->len
) {
9000 /* Conversion failed, so just store it as a string */
9001 Jim_FreeNewObj(interp
, objPtr
);
9007 /* Everything else is stored a simple string term */
9008 objPtr
= Jim_NewStringObj(interp
, t
->token
, t
->len
);
9009 if (t
->type
== JIM_TT_CMD
) {
9010 /* Only commands need source info */
9011 JimSetSourceInfo(interp
, objPtr
, builder
->fileNameObj
, t
->line
);
9015 /* Now push a term node */
9016 node
= builder
->next
++;
9017 node
->objPtr
= objPtr
;
9018 Jim_IncrRefCount(node
->objPtr
);
9019 node
->type
= t
->type
;
9020 Jim_StackPush(&builder
->stack
, node
);
9024 if (builder
->stack
.len
== exp_stacklen
) {
9029 if ((flags
& EXPR_FUNC_ARGS
)) {
9030 Jim_SetResultFormatted(interp
, "too %s arguments for math function", (builder
->stack
.len
< exp_stacklen
) ? "few" : "many");
9033 if (builder
->stack
.len
< exp_stacklen
) {
9034 if (builder
->level
== 0) {
9035 Jim_SetResultFormatted(interp
, "empty expression");
9038 Jim_SetResultFormatted(interp
, "syntax error in expression \"%#s\": premature end of expression", builder
->exprObjPtr
);
9042 Jim_SetResultFormatted(interp
, "extra terms after expression");
9049 static struct ExprTree
*ExprTreeCreateTree(Jim_Interp
*interp
, const ParseTokenList
*tokenlist
, Jim_Obj
*exprObjPtr
, Jim_Obj
*fileNameObj
)
9051 struct ExprTree
*expr
;
9052 struct ExprBuilder builder
;
9054 struct JimExprNode
*top
= NULL
;
9056 builder
.parencount
= 0;
9058 builder
.token
= builder
.first_token
= tokenlist
->list
;
9059 builder
.exprObjPtr
= exprObjPtr
;
9060 builder
.fileNameObj
= fileNameObj
;
9061 /* The bytecode will never produce more nodes than there are tokens - 1 (for EOL)*/
9062 builder
.nodes
= malloc(sizeof(struct JimExprNode
) * (tokenlist
->count
- 1));
9063 memset(builder
.nodes
, 0, sizeof(struct JimExprNode
) * (tokenlist
->count
- 1));
9064 builder
.next
= builder
.nodes
;
9065 Jim_InitStack(&builder
.stack
);
9067 rc
= ExprTreeBuildTree(interp
, &builder
, 0, 0, 1);
9070 top
= Jim_StackPop(&builder
.stack
);
9072 if (builder
.parencount
) {
9073 Jim_SetResultString(interp
, "missing close parenthesis", -1);
9078 /* Free the stack used for the compilation. */
9079 Jim_FreeStack(&builder
.stack
);
9082 ExprTreeFreeNodes(interp
, builder
.nodes
, builder
.next
- builder
.nodes
);
9086 expr
= Jim_Alloc(sizeof(*expr
));
9089 expr
->nodes
= builder
.nodes
;
9090 expr
->len
= builder
.next
- builder
.nodes
;
9092 assert(expr
->len
<= tokenlist
->count
- 1);
9097 /* This method takes the string representation of an expression
9098 * and generates a program for the expr engine */
9099 static int SetExprFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
)
9102 const char *exprText
;
9103 struct JimParserCtx parser
;
9104 struct ExprTree
*expr
;
9105 ParseTokenList tokenlist
;
9107 Jim_Obj
*fileNameObj
;
9110 /* Try to get information about filename / line number */
9111 if (objPtr
->typePtr
== &sourceObjType
) {
9112 fileNameObj
= objPtr
->internalRep
.sourceValue
.fileNameObj
;
9113 line
= objPtr
->internalRep
.sourceValue
.lineNumber
;
9116 fileNameObj
= interp
->emptyObj
;
9119 Jim_IncrRefCount(fileNameObj
);
9121 exprText
= Jim_GetString(objPtr
, &exprTextLen
);
9123 /* Initially tokenise the expression into tokenlist */
9124 ScriptTokenListInit(&tokenlist
);
9126 JimParserInit(&parser
, exprText
, exprTextLen
, line
);
9127 while (!parser
.eof
) {
9128 if (JimParseExpression(&parser
) != JIM_OK
) {
9129 ScriptTokenListFree(&tokenlist
);
9130 Jim_SetResultFormatted(interp
, "syntax error in expression: \"%#s\"", objPtr
);
9135 ScriptAddToken(&tokenlist
, parser
.tstart
, parser
.tend
- parser
.tstart
+ 1, parser
.tt
,
9139 #ifdef DEBUG_SHOW_EXPR_TOKENS
9142 printf("==== Expr Tokens (%s) ====\n", Jim_String(fileNameObj
));
9143 for (i
= 0; i
< tokenlist
.count
; i
++) {
9144 printf("[%2d]@%d %s '%.*s'\n", i
, tokenlist
.list
[i
].line
, jim_tt_name(tokenlist
.list
[i
].type
),
9145 tokenlist
.list
[i
].len
, tokenlist
.list
[i
].token
);
9150 if (JimParseCheckMissing(interp
, parser
.missing
.ch
) == JIM_ERR
) {
9151 ScriptTokenListFree(&tokenlist
);
9152 Jim_DecrRefCount(interp
, fileNameObj
);
9156 /* Now create the expression bytecode from the tokenlist */
9157 expr
= ExprTreeCreateTree(interp
, &tokenlist
, objPtr
, fileNameObj
);
9159 /* No longer need the token list */
9160 ScriptTokenListFree(&tokenlist
);
9166 #ifdef DEBUG_SHOW_EXPR
9167 printf("==== Expr ====\n");
9168 JimShowExprNode(expr
->expr
, 0);
9174 /* Free the old internal rep and set the new one. */
9175 Jim_DecrRefCount(interp
, fileNameObj
);
9176 Jim_FreeIntRep(interp
, objPtr
);
9177 Jim_SetIntRepPtr(objPtr
, expr
);
9178 objPtr
->typePtr
= &exprObjType
;
9182 static struct ExprTree
*JimGetExpression(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
9184 if (objPtr
->typePtr
!= &exprObjType
) {
9185 if (SetExprFromAny(interp
, objPtr
) != JIM_OK
) {
9189 return (struct ExprTree
*) Jim_GetIntRepPtr(objPtr
);
9192 #ifdef JIM_OPTIMIZATION
9193 static Jim_Obj
*JimExprIntValOrVar(Jim_Interp
*interp
, struct JimExprNode
*node
)
9195 if (node
->type
== JIM_TT_EXPR_INT
)
9196 return node
->objPtr
;
9197 else if (node
->type
== JIM_TT_VAR
)
9198 return Jim_GetVariable(interp
, node
->objPtr
, JIM_NONE
);
9199 else if (node
->type
== JIM_TT_DICTSUGAR
)
9200 return JimExpandDictSugar(interp
, node
->objPtr
);
9206 /* -----------------------------------------------------------------------------
9207 * Expressions evaluation.
9208 * Jim uses a recursive evaluation engine for expressions,
9209 * that takes advantage of the fact that expr's operators
9210 * can't be redefined.
9212 * Jim_EvalExpression() uses the expression tree compiled by
9213 * SetExprFromAny() method of the "expression" object.
9215 * On success a Tcl Object containing the result of the evaluation
9216 * is stored into expResultPtrPtr (having refcount of 1), and JIM_OK is
9218 * On error the function returns a retcode != to JIM_OK and set a suitable
9219 * error on the interp.
9220 * ---------------------------------------------------------------------------*/
9222 static int JimExprEvalTermNode(Jim_Interp
*interp
, struct JimExprNode
*node
)
9224 if (TOKEN_IS_EXPR_OP(node
->type
)) {
9225 const struct Jim_ExprOperator
*op
= JimExprOperatorInfoByOpcode(node
->type
);
9226 return op
->funcop(interp
, node
);
9232 switch (node
->type
) {
9233 case JIM_TT_EXPR_INT
:
9234 case JIM_TT_EXPR_DOUBLE
:
9235 case JIM_TT_EXPR_BOOLEAN
:
9237 Jim_SetResult(interp
, node
->objPtr
);
9241 objPtr
= Jim_GetVariable(interp
, node
->objPtr
, JIM_ERRMSG
);
9243 Jim_SetResult(interp
, objPtr
);
9248 case JIM_TT_DICTSUGAR
:
9249 objPtr
= JimExpandDictSugar(interp
, node
->objPtr
);
9251 Jim_SetResult(interp
, objPtr
);
9257 if (Jim_SubstObj(interp
, node
->objPtr
, &objPtr
, JIM_NONE
) == JIM_OK
) {
9258 Jim_SetResult(interp
, objPtr
);
9264 return Jim_EvalObj(interp
, node
->objPtr
);
9267 /* Should never get here */
9273 static int JimExprGetTerm(Jim_Interp
*interp
, struct JimExprNode
*node
, Jim_Obj
**objPtrPtr
)
9275 int rc
= JimExprEvalTermNode(interp
, node
);
9277 *objPtrPtr
= Jim_GetResult(interp
);
9278 Jim_IncrRefCount(*objPtrPtr
);
9283 static int JimExprGetTermBoolean(Jim_Interp
*interp
, struct JimExprNode
*node
)
9285 if (JimExprEvalTermNode(interp
, node
) == JIM_OK
) {
9286 return ExprBool(interp
, Jim_GetResult(interp
));
9291 int Jim_EvalExpression(Jim_Interp
*interp
, Jim_Obj
*exprObjPtr
)
9293 struct ExprTree
*expr
;
9294 int retcode
= JIM_OK
;
9296 expr
= JimGetExpression(interp
, exprObjPtr
);
9298 return JIM_ERR
; /* error in expression. */
9301 #ifdef JIM_OPTIMIZATION
9302 /* Check for one of the following common expressions used by while/for
9307 * $a < CONST, $a < $b
9308 * $a <= CONST, $a <= $b
9309 * $a > CONST, $a > $b
9310 * $a >= CONST, $a >= $b
9311 * $a != CONST, $a != $b
9312 * $a == CONST, $a == $b
9317 /* STEP 1 -- Check if there are the conditions to run the specialized
9318 * version of while */
9320 switch (expr
->len
) {
9322 objPtr
= JimExprIntValOrVar(interp
, expr
->expr
);
9324 Jim_SetResult(interp
, objPtr
);
9330 if (expr
->expr
->type
== JIM_EXPROP_NOT
) {
9331 objPtr
= JimExprIntValOrVar(interp
, expr
->expr
->left
);
9333 if (objPtr
&& JimIsWide(objPtr
)) {
9334 Jim_SetResult(interp
, JimWideValue(objPtr
) ? interp
->falseObj
: interp
->trueObj
);
9341 objPtr
= JimExprIntValOrVar(interp
, expr
->expr
->left
);
9342 if (objPtr
&& JimIsWide(objPtr
)) {
9343 Jim_Obj
*objPtr2
= JimExprIntValOrVar(interp
, expr
->expr
->right
);
9344 if (objPtr2
&& JimIsWide(objPtr2
)) {
9345 jim_wide wideValueA
= JimWideValue(objPtr
);
9346 jim_wide wideValueB
= JimWideValue(objPtr2
);
9348 switch (expr
->expr
->type
) {
9350 cmpRes
= wideValueA
< wideValueB
;
9352 case JIM_EXPROP_LTE
:
9353 cmpRes
= wideValueA
<= wideValueB
;
9356 cmpRes
= wideValueA
> wideValueB
;
9358 case JIM_EXPROP_GTE
:
9359 cmpRes
= wideValueA
>= wideValueB
;
9361 case JIM_EXPROP_NUMEQ
:
9362 cmpRes
= wideValueA
== wideValueB
;
9364 case JIM_EXPROP_NUMNE
:
9365 cmpRes
= wideValueA
!= wideValueB
;
9370 Jim_SetResult(interp
, cmpRes
? interp
->trueObj
: interp
->falseObj
);
9380 /* In order to avoid the internal repr being freed due to
9381 * shimmering of the exprObjPtr's object, we make the internal rep
9385 /* Evaluate with the recursive expr engine */
9386 retcode
= JimExprEvalTermNode(interp
, expr
->expr
);
9393 int Jim_GetBoolFromExpr(Jim_Interp
*interp
, Jim_Obj
*exprObjPtr
, int *boolPtr
)
9395 int retcode
= Jim_EvalExpression(interp
, exprObjPtr
);
9397 if (retcode
== JIM_OK
) {
9398 switch (ExprBool(interp
, Jim_GetResult(interp
))) {
9415 /* -----------------------------------------------------------------------------
9416 * ScanFormat String Object
9417 * ---------------------------------------------------------------------------*/
9419 /* This Jim_Obj will held a parsed representation of a format string passed to
9420 * the Jim_ScanString command. For error diagnostics, the scanformat string has
9421 * to be parsed in its entirely first and then, if correct, can be used for
9422 * scanning. To avoid endless re-parsing, the parsed representation will be
9423 * stored in an internal representation and re-used for performance reason. */
9425 /* A ScanFmtPartDescr will held the information of /one/ part of the whole
9426 * scanformat string. This part will later be used to extract information
9427 * out from the string to be parsed by Jim_ScanString */
9429 typedef struct ScanFmtPartDescr
9431 const char *arg
; /* Specification of a CHARSET conversion */
9432 const char *prefix
; /* Prefix to be scanned literally before conversion */
9433 size_t width
; /* Maximal width of input to be converted */
9434 int pos
; /* -1 - no assign, 0 - natural pos, >0 - XPG3 pos */
9435 char type
; /* Type of conversion (e.g. c, d, f) */
9436 char modifier
; /* Modify type (e.g. l - long, h - short */
9439 /* The ScanFmtStringObj will hold the internal representation of a scanformat
9440 * string parsed and separated in part descriptions. Furthermore it contains
9441 * the original string representation of the scanformat string to allow for
9442 * fast update of the Jim_Obj's string representation part.
9444 * As an add-on the internal object representation adds some scratch pad area
9445 * for usage by Jim_ScanString to avoid endless allocating and freeing of
9446 * memory for purpose of string scanning.
9448 * The error member points to a static allocated string in case of a mal-
9449 * formed scanformat string or it contains '0' (NULL) in case of a valid
9450 * parse representation.
9452 * The whole memory of the internal representation is allocated as a single
9453 * area of memory that will be internally separated. So freeing and duplicating
9454 * of such an object is cheap */
9456 typedef struct ScanFmtStringObj
9458 jim_wide size
; /* Size of internal repr in bytes */
9459 char *stringRep
; /* Original string representation */
9460 size_t count
; /* Number of ScanFmtPartDescr contained */
9461 size_t convCount
; /* Number of conversions that will assign */
9462 size_t maxPos
; /* Max position index if XPG3 is used */
9463 const char *error
; /* Ptr to error text (NULL if no error */
9464 char *scratch
; /* Some scratch pad used by Jim_ScanString */
9465 ScanFmtPartDescr descr
[1]; /* The vector of partial descriptions */
9469 static void FreeScanFmtInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
);
9470 static void DupScanFmtInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
);
9471 static void UpdateStringOfScanFmt(Jim_Obj
*objPtr
);
9473 static const Jim_ObjType scanFmtStringObjType
= {
9475 FreeScanFmtInternalRep
,
9476 DupScanFmtInternalRep
,
9477 UpdateStringOfScanFmt
,
9481 void FreeScanFmtInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
9483 JIM_NOTUSED(interp
);
9484 Jim_Free((char *)objPtr
->internalRep
.ptr
);
9485 objPtr
->internalRep
.ptr
= 0;
9488 void DupScanFmtInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
)
9490 size_t size
= (size_t) ((ScanFmtStringObj
*) srcPtr
->internalRep
.ptr
)->size
;
9491 ScanFmtStringObj
*newVec
= (ScanFmtStringObj
*) Jim_Alloc(size
);
9493 JIM_NOTUSED(interp
);
9494 memcpy(newVec
, srcPtr
->internalRep
.ptr
, size
);
9495 dupPtr
->internalRep
.ptr
= newVec
;
9496 dupPtr
->typePtr
= &scanFmtStringObjType
;
9499 static void UpdateStringOfScanFmt(Jim_Obj
*objPtr
)
9501 JimSetStringBytes(objPtr
, ((ScanFmtStringObj
*) objPtr
->internalRep
.ptr
)->stringRep
);
9504 /* SetScanFmtFromAny will parse a given string and create the internal
9505 * representation of the format specification. In case of an error
9506 * the error data member of the internal representation will be set
9507 * to an descriptive error text and the function will be left with
9508 * JIM_ERR to indicate unsucessful parsing (aka. malformed scanformat
9511 static int SetScanFmtFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
9513 ScanFmtStringObj
*fmtObj
;
9515 int maxCount
, i
, approxSize
, lastPos
= -1;
9516 const char *fmt
= Jim_String(objPtr
);
9517 int maxFmtLen
= Jim_Length(objPtr
);
9518 const char *fmtEnd
= fmt
+ maxFmtLen
;
9521 Jim_FreeIntRep(interp
, objPtr
);
9522 /* Count how many conversions could take place maximally */
9523 for (i
= 0, maxCount
= 0; i
< maxFmtLen
; ++i
)
9526 /* Calculate an approximation of the memory necessary */
9527 approxSize
= sizeof(ScanFmtStringObj
) /* Size of the container */
9528 +(maxCount
+ 1) * sizeof(ScanFmtPartDescr
) /* Size of all partials */
9529 +maxFmtLen
* sizeof(char) + 3 + 1 /* Scratch + "%n" + '\0' */
9530 + maxFmtLen
* sizeof(char) + 1 /* Original stringrep */
9531 + maxFmtLen
* sizeof(char) /* Arg for CHARSETs */
9532 +(maxCount
+ 1) * sizeof(char) /* '\0' for every partial */
9533 +1; /* safety byte */
9534 fmtObj
= (ScanFmtStringObj
*) Jim_Alloc(approxSize
);
9535 memset(fmtObj
, 0, approxSize
);
9536 fmtObj
->size
= approxSize
;
9538 fmtObj
->scratch
= (char *)&fmtObj
->descr
[maxCount
+ 1];
9539 fmtObj
->stringRep
= fmtObj
->scratch
+ maxFmtLen
+ 3 + 1;
9540 memcpy(fmtObj
->stringRep
, fmt
, maxFmtLen
);
9541 buffer
= fmtObj
->stringRep
+ maxFmtLen
+ 1;
9542 objPtr
->internalRep
.ptr
= fmtObj
;
9543 objPtr
->typePtr
= &scanFmtStringObjType
;
9544 for (i
= 0, curr
= 0; fmt
< fmtEnd
; ++fmt
) {
9545 int width
= 0, skip
;
9546 ScanFmtPartDescr
*descr
= &fmtObj
->descr
[curr
];
9549 descr
->width
= 0; /* Assume width unspecified */
9550 /* Overread and store any "literal" prefix */
9551 if (*fmt
!= '%' || fmt
[1] == '%') {
9553 descr
->prefix
= &buffer
[i
];
9554 for (; fmt
< fmtEnd
; ++fmt
) {
9564 /* Skip the conversion introducing '%' sign */
9566 /* End reached due to non-conversion literal only? */
9569 descr
->pos
= 0; /* Assume "natural" positioning */
9571 descr
->pos
= -1; /* Okay, conversion will not be assigned */
9575 fmtObj
->convCount
++; /* Otherwise count as assign-conversion */
9576 /* Check if next token is a number (could be width or pos */
9577 if (sscanf(fmt
, "%d%n", &width
, &skip
) == 1) {
9579 /* Was the number a XPG3 position specifier? */
9580 if (descr
->pos
!= -1 && *fmt
== '$') {
9586 /* Look if "natural" postioning and XPG3 one was mixed */
9587 if ((lastPos
== 0 && descr
->pos
> 0)
9588 || (lastPos
> 0 && descr
->pos
== 0)) {
9589 fmtObj
->error
= "cannot mix \"%\" and \"%n$\" conversion specifiers";
9592 /* Look if this position was already used */
9593 for (prev
= 0; prev
< curr
; ++prev
) {
9594 if (fmtObj
->descr
[prev
].pos
== -1)
9596 if (fmtObj
->descr
[prev
].pos
== descr
->pos
) {
9598 "variable is assigned by multiple \"%n$\" conversion specifiers";
9602 if (descr
->pos
< 0) {
9604 "\"%n$\" conversion specifier is negative";
9607 /* Try to find a width after the XPG3 specifier */
9608 if (sscanf(fmt
, "%d%n", &width
, &skip
) == 1) {
9609 descr
->width
= width
;
9612 if (descr
->pos
> 0 && (size_t) descr
->pos
> fmtObj
->maxPos
)
9613 fmtObj
->maxPos
= descr
->pos
;
9616 /* Number was not a XPG3, so it has to be a width */
9617 descr
->width
= width
;
9620 /* If positioning mode was undetermined yet, fix this */
9622 lastPos
= descr
->pos
;
9623 /* Handle CHARSET conversion type ... */
9625 int swapped
= 1, beg
= i
, end
, j
;
9628 descr
->arg
= &buffer
[i
];
9631 buffer
[i
++] = *fmt
++;
9633 buffer
[i
++] = *fmt
++;
9634 while (*fmt
&& *fmt
!= ']')
9635 buffer
[i
++] = *fmt
++;
9637 fmtObj
->error
= "unmatched [ in format string";
9642 /* In case a range fence was given "backwards", swap it */
9645 for (j
= beg
+ 1; j
< end
- 1; ++j
) {
9646 if (buffer
[j
] == '-' && buffer
[j
- 1] > buffer
[j
+ 1]) {
9647 char tmp
= buffer
[j
- 1];
9649 buffer
[j
- 1] = buffer
[j
+ 1];
9650 buffer
[j
+ 1] = tmp
;
9657 /* Remember any valid modifier if given */
9658 if (fmt
< fmtEnd
&& strchr("hlL", *fmt
))
9659 descr
->modifier
= tolower((int)*fmt
++);
9661 if (fmt
>= fmtEnd
) {
9662 fmtObj
->error
= "missing scan conversion character";
9667 if (strchr("efgcsndoxui", *fmt
) == 0) {
9668 fmtObj
->error
= "bad scan conversion character";
9671 else if (*fmt
== 'c' && descr
->width
!= 0) {
9672 fmtObj
->error
= "field width may not be specified in %c " "conversion";
9675 else if (*fmt
== 'u' && descr
->modifier
== 'l') {
9676 fmtObj
->error
= "unsigned wide not supported";
9686 /* Some accessor macros to allow lowlevel access to fields of internal repr */
9688 #define FormatGetCnvCount(_fo_) \
9689 ((ScanFmtStringObj*)((_fo_)->internalRep.ptr))->convCount
9690 #define FormatGetMaxPos(_fo_) \
9691 ((ScanFmtStringObj*)((_fo_)->internalRep.ptr))->maxPos
9692 #define FormatGetError(_fo_) \
9693 ((ScanFmtStringObj*)((_fo_)->internalRep.ptr))->error
9695 /* JimScanAString is used to scan an unspecified string that ends with
9696 * next WS, or a string that is specified via a charset.
9699 static Jim_Obj
*JimScanAString(Jim_Interp
*interp
, const char *sdescr
, const char *str
)
9701 char *buffer
= Jim_StrDup(str
);
9708 if (!sdescr
&& isspace(UCHAR(*str
)))
9709 break; /* EOS via WS if unspecified */
9711 n
= utf8_tounicode(str
, &c
);
9712 if (sdescr
&& !JimCharsetMatch(sdescr
, c
, JIM_CHARSET_SCAN
))
9718 return Jim_NewStringObjNoAlloc(interp
, buffer
, p
- buffer
);
9721 /* ScanOneEntry will scan one entry out of the string passed as argument.
9722 * It use the sscanf() function for this task. After extracting and
9723 * converting of the value, the count of scanned characters will be
9724 * returned of -1 in case of no conversion tool place and string was
9725 * already scanned thru */
9727 static int ScanOneEntry(Jim_Interp
*interp
, const char *str
, int pos
, int strLen
,
9728 ScanFmtStringObj
* fmtObj
, long idx
, Jim_Obj
**valObjPtr
)
9731 const ScanFmtPartDescr
*descr
= &fmtObj
->descr
[idx
];
9733 size_t anchor
= pos
;
9735 Jim_Obj
*tmpObj
= NULL
;
9737 /* First pessimistically assume, we will not scan anything :-) */
9739 if (descr
->prefix
) {
9740 /* There was a prefix given before the conversion, skip it and adjust
9741 * the string-to-be-parsed accordingly */
9742 for (i
= 0; pos
< strLen
&& descr
->prefix
[i
]; ++i
) {
9743 /* If prefix require, skip WS */
9744 if (isspace(UCHAR(descr
->prefix
[i
])))
9745 while (pos
< strLen
&& isspace(UCHAR(str
[pos
])))
9747 else if (descr
->prefix
[i
] != str
[pos
])
9748 break; /* Prefix do not match here, leave the loop */
9750 ++pos
; /* Prefix matched so far, next round */
9752 if (pos
>= strLen
) {
9753 return -1; /* All of str consumed: EOF condition */
9755 else if (descr
->prefix
[i
] != 0)
9756 return 0; /* Not whole prefix consumed, no conversion possible */
9758 /* For all but following conversion, skip leading WS */
9759 if (descr
->type
!= 'c' && descr
->type
!= '[' && descr
->type
!= 'n')
9760 while (isspace(UCHAR(str
[pos
])))
9762 /* Determine how much skipped/scanned so far */
9763 scanned
= pos
- anchor
;
9765 /* %c is a special, simple case. no width */
9766 if (descr
->type
== 'n') {
9767 /* Return pseudo conversion means: how much scanned so far? */
9768 *valObjPtr
= Jim_NewIntObj(interp
, anchor
+ scanned
);
9770 else if (pos
>= strLen
) {
9771 /* Cannot scan anything, as str is totally consumed */
9774 else if (descr
->type
== 'c') {
9776 scanned
+= utf8_tounicode(&str
[pos
], &c
);
9777 *valObjPtr
= Jim_NewIntObj(interp
, c
);
9781 /* Processing of conversions follows ... */
9782 if (descr
->width
> 0) {
9783 /* Do not try to scan as fas as possible but only the given width.
9784 * To ensure this, we copy the part that should be scanned. */
9785 size_t sLen
= utf8_strlen(&str
[pos
], strLen
- pos
);
9786 size_t tLen
= descr
->width
> sLen
? sLen
: descr
->width
;
9788 tmpObj
= Jim_NewStringObjUtf8(interp
, str
+ pos
, tLen
);
9789 tok
= tmpObj
->bytes
;
9792 /* As no width was given, simply refer to the original string */
9795 switch (descr
->type
) {
9801 char *endp
; /* Position where the number finished */
9804 int base
= descr
->type
== 'o' ? 8
9805 : descr
->type
== 'x' ? 16 : descr
->type
== 'i' ? 0 : 10;
9807 /* Try to scan a number with the given base */
9809 w
= jim_strtoull(tok
, &endp
);
9812 w
= strtoull(tok
, &endp
, base
);
9816 /* There was some number sucessfully scanned! */
9817 *valObjPtr
= Jim_NewIntObj(interp
, w
);
9819 /* Adjust the number-of-chars scanned so far */
9820 scanned
+= endp
- tok
;
9823 /* Nothing was scanned. We have to determine if this
9824 * happened due to e.g. prefix mismatch or input str
9826 scanned
= *tok
? 0 : -1;
9832 *valObjPtr
= JimScanAString(interp
, descr
->arg
, tok
);
9833 scanned
+= Jim_Length(*valObjPtr
);
9840 double value
= strtod(tok
, &endp
);
9843 /* There was some number sucessfully scanned! */
9844 *valObjPtr
= Jim_NewDoubleObj(interp
, value
);
9845 /* Adjust the number-of-chars scanned so far */
9846 scanned
+= endp
- tok
;
9849 /* Nothing was scanned. We have to determine if this
9850 * happened due to e.g. prefix mismatch or input str
9852 scanned
= *tok
? 0 : -1;
9857 /* If a substring was allocated (due to pre-defined width) do not
9858 * forget to free it */
9860 Jim_FreeNewObj(interp
, tmpObj
);
9866 /* Jim_ScanString is the workhorse of string scanning. It will scan a given
9867 * string and returns all converted (and not ignored) values in a list back
9868 * to the caller. If an error occured, a NULL pointer will be returned */
9870 Jim_Obj
*Jim_ScanString(Jim_Interp
*interp
, Jim_Obj
*strObjPtr
, Jim_Obj
*fmtObjPtr
, int flags
)
9874 const char *str
= Jim_String(strObjPtr
);
9875 int strLen
= Jim_Utf8Length(interp
, strObjPtr
);
9876 Jim_Obj
*resultList
= 0;
9877 Jim_Obj
**resultVec
= 0;
9879 Jim_Obj
*emptyStr
= 0;
9880 ScanFmtStringObj
*fmtObj
;
9882 /* This should never happen. The format object should already be of the correct type */
9883 JimPanic((fmtObjPtr
->typePtr
!= &scanFmtStringObjType
, "Jim_ScanString() for non-scan format"));
9885 fmtObj
= (ScanFmtStringObj
*) fmtObjPtr
->internalRep
.ptr
;
9886 /* Check if format specification was valid */
9887 if (fmtObj
->error
!= 0) {
9888 if (flags
& JIM_ERRMSG
)
9889 Jim_SetResultString(interp
, fmtObj
->error
, -1);
9892 /* Allocate a new "shared" empty string for all unassigned conversions */
9893 emptyStr
= Jim_NewEmptyStringObj(interp
);
9894 Jim_IncrRefCount(emptyStr
);
9895 /* Create a list and fill it with empty strings up to max specified XPG3 */
9896 resultList
= Jim_NewListObj(interp
, NULL
, 0);
9897 if (fmtObj
->maxPos
> 0) {
9898 for (i
= 0; i
< fmtObj
->maxPos
; ++i
)
9899 Jim_ListAppendElement(interp
, resultList
, emptyStr
);
9900 JimListGetElements(interp
, resultList
, &resultc
, &resultVec
);
9902 /* Now handle every partial format description */
9903 for (i
= 0, pos
= 0; i
< fmtObj
->count
; ++i
) {
9904 ScanFmtPartDescr
*descr
= &(fmtObj
->descr
[i
]);
9907 /* Only last type may be "literal" w/o conversion - skip it! */
9908 if (descr
->type
== 0)
9910 /* As long as any conversion could be done, we will proceed */
9912 scanned
= ScanOneEntry(interp
, str
, pos
, strLen
, fmtObj
, i
, &value
);
9913 /* In case our first try results in EOF, we will leave */
9914 if (scanned
== -1 && i
== 0)
9916 /* Advance next pos-to-be-scanned for the amount scanned already */
9919 /* value == 0 means no conversion took place so take empty string */
9921 value
= Jim_NewEmptyStringObj(interp
);
9922 /* If value is a non-assignable one, skip it */
9923 if (descr
->pos
== -1) {
9924 Jim_FreeNewObj(interp
, value
);
9926 else if (descr
->pos
== 0)
9927 /* Otherwise append it to the result list if no XPG3 was given */
9928 Jim_ListAppendElement(interp
, resultList
, value
);
9929 else if (resultVec
[descr
->pos
- 1] == emptyStr
) {
9930 /* But due to given XPG3, put the value into the corr. slot */
9931 Jim_DecrRefCount(interp
, resultVec
[descr
->pos
- 1]);
9932 Jim_IncrRefCount(value
);
9933 resultVec
[descr
->pos
- 1] = value
;
9936 /* Otherwise, the slot was already used - free obj and ERROR */
9937 Jim_FreeNewObj(interp
, value
);
9941 Jim_DecrRefCount(interp
, emptyStr
);
9944 Jim_DecrRefCount(interp
, emptyStr
);
9945 Jim_FreeNewObj(interp
, resultList
);
9946 return (Jim_Obj
*)EOF
;
9948 Jim_DecrRefCount(interp
, emptyStr
);
9949 Jim_FreeNewObj(interp
, resultList
);
9953 /* -----------------------------------------------------------------------------
9954 * Pseudo Random Number Generation
9955 * ---------------------------------------------------------------------------*/
9956 /* Initialize the sbox with the numbers from 0 to 255 */
9957 static void JimPrngInit(Jim_Interp
*interp
)
9959 #define PRNG_SEED_SIZE 256
9962 time_t t
= time(NULL
);
9964 interp
->prngState
= Jim_Alloc(sizeof(Jim_PrngState
));
9966 seed
= Jim_Alloc(PRNG_SEED_SIZE
* sizeof(*seed
));
9967 for (i
= 0; i
< PRNG_SEED_SIZE
; i
++) {
9968 seed
[i
] = (rand() ^ t
^ clock());
9970 JimPrngSeed(interp
, (unsigned char *)seed
, PRNG_SEED_SIZE
* sizeof(*seed
));
9974 /* Generates N bytes of random data */
9975 static void JimRandomBytes(Jim_Interp
*interp
, void *dest
, unsigned int len
)
9977 Jim_PrngState
*prng
;
9978 unsigned char *destByte
= (unsigned char *)dest
;
9979 unsigned int si
, sj
, x
;
9981 /* initialization, only needed the first time */
9982 if (interp
->prngState
== NULL
)
9983 JimPrngInit(interp
);
9984 prng
= interp
->prngState
;
9985 /* generates 'len' bytes of pseudo-random numbers */
9986 for (x
= 0; x
< len
; x
++) {
9987 prng
->i
= (prng
->i
+ 1) & 0xff;
9988 si
= prng
->sbox
[prng
->i
];
9989 prng
->j
= (prng
->j
+ si
) & 0xff;
9990 sj
= prng
->sbox
[prng
->j
];
9991 prng
->sbox
[prng
->i
] = sj
;
9992 prng
->sbox
[prng
->j
] = si
;
9993 *destByte
++ = prng
->sbox
[(si
+ sj
) & 0xff];
9997 /* Re-seed the generator with user-provided bytes */
9998 static void JimPrngSeed(Jim_Interp
*interp
, unsigned char *seed
, int seedLen
)
10001 Jim_PrngState
*prng
;
10003 /* initialization, only needed the first time */
10004 if (interp
->prngState
== NULL
)
10005 JimPrngInit(interp
);
10006 prng
= interp
->prngState
;
10008 /* Set the sbox[i] with i */
10009 for (i
= 0; i
< 256; i
++)
10011 /* Now use the seed to perform a random permutation of the sbox */
10012 for (i
= 0; i
< seedLen
; i
++) {
10015 t
= prng
->sbox
[i
& 0xFF];
10016 prng
->sbox
[i
& 0xFF] = prng
->sbox
[seed
[i
]];
10017 prng
->sbox
[seed
[i
]] = t
;
10019 prng
->i
= prng
->j
= 0;
10021 /* discard at least the first 256 bytes of stream.
10022 * borrow the seed buffer for this
10024 for (i
= 0; i
< 256; i
+= seedLen
) {
10025 JimRandomBytes(interp
, seed
, seedLen
);
10030 static int Jim_IncrCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
10032 jim_wide wideValue
, increment
= 1;
10033 Jim_Obj
*intObjPtr
;
10035 if (argc
!= 2 && argc
!= 3) {
10036 Jim_WrongNumArgs(interp
, 1, argv
, "varName ?increment?");
10040 if (Jim_GetWide(interp
, argv
[2], &increment
) != JIM_OK
)
10043 intObjPtr
= Jim_GetVariable(interp
, argv
[1], JIM_UNSHARED
);
10045 /* Set missing variable to 0 */
10048 else if (Jim_GetWide(interp
, intObjPtr
, &wideValue
) != JIM_OK
) {
10051 if (!intObjPtr
|| Jim_IsShared(intObjPtr
)) {
10052 intObjPtr
= Jim_NewIntObj(interp
, wideValue
+ increment
);
10053 if (Jim_SetVariable(interp
, argv
[1], intObjPtr
) != JIM_OK
) {
10054 Jim_FreeNewObj(interp
, intObjPtr
);
10059 /* Can do it the quick way */
10060 Jim_InvalidateStringRep(intObjPtr
);
10061 JimWideValue(intObjPtr
) = wideValue
+ increment
;
10063 /* The following step is required in order to invalidate the
10064 * string repr of "FOO" if the var name is on the form of "FOO(IDX)" */
10065 if (argv
[1]->typePtr
!= &variableObjType
) {
10066 /* Note that this can't fail since GetVariable already succeeded */
10067 Jim_SetVariable(interp
, argv
[1], intObjPtr
);
10070 Jim_SetResult(interp
, intObjPtr
);
10075 /* -----------------------------------------------------------------------------
10077 * ---------------------------------------------------------------------------*/
10078 #define JIM_EVAL_SARGV_LEN 8 /* static arguments vector length */
10079 #define JIM_EVAL_SINTV_LEN 8 /* static interpolation vector length */
10081 /* Handle calls to the [unknown] command */
10082 static int JimUnknown(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
10086 /* If JimUnknown() is recursively called too many times...
10089 if (interp
->unknown_called
> 50) {
10093 /* The object interp->unknown just contains
10094 * the "unknown" string, it is used in order to
10095 * avoid to lookup the unknown command every time
10096 * but instead to cache the result. */
10098 /* If the [unknown] command does not exist ... */
10099 if (Jim_GetCommand(interp
, interp
->unknown
, JIM_NONE
) == NULL
)
10102 interp
->unknown_called
++;
10103 /* XXX: Are we losing fileNameObj and linenr? */
10104 retcode
= Jim_EvalObjPrefix(interp
, interp
->unknown
, argc
, argv
);
10105 interp
->unknown_called
--;
10110 static int JimInvokeCommand(Jim_Interp
*interp
, int objc
, Jim_Obj
*const *objv
)
10114 void *prevPrivData
;
10119 for (j
= 0; j
< objc
; j
++) {
10120 printf(" '%s'", Jim_String(objv
[j
]));
10125 if (interp
->framePtr
->tailcallCmd
) {
10126 /* Special tailcall command was pre-resolved */
10127 cmdPtr
= interp
->framePtr
->tailcallCmd
;
10128 interp
->framePtr
->tailcallCmd
= NULL
;
10131 cmdPtr
= Jim_GetCommand(interp
, objv
[0], JIM_ERRMSG
);
10132 if (cmdPtr
== NULL
) {
10133 return JimUnknown(interp
, objc
, objv
);
10135 JimIncrCmdRefCount(cmdPtr
);
10138 if (interp
->evalDepth
== interp
->maxEvalDepth
) {
10139 Jim_SetResultString(interp
, "Infinite eval recursion", -1);
10143 interp
->evalDepth
++;
10144 prevPrivData
= interp
->cmdPrivData
;
10146 /* Call it -- Make sure result is an empty object. */
10147 Jim_SetEmptyResult(interp
);
10148 if (cmdPtr
->isproc
) {
10149 retcode
= JimCallProcedure(interp
, cmdPtr
, objc
, objv
);
10152 interp
->cmdPrivData
= cmdPtr
->u
.native
.privData
;
10153 retcode
= cmdPtr
->u
.native
.cmdProc(interp
, objc
, objv
);
10155 interp
->cmdPrivData
= prevPrivData
;
10156 interp
->evalDepth
--;
10159 JimDecrCmdRefCount(interp
, cmdPtr
);
10164 /* Eval the object vector 'objv' composed of 'objc' elements.
10165 * Every element is used as single argument.
10166 * Jim_EvalObj() will call this function every time its object
10167 * argument is of "list" type, with no string representation.
10169 * This is possible because the string representation of a
10170 * list object generated by the UpdateStringOfList is made
10171 * in a way that ensures that every list element is a different
10172 * command argument. */
10173 int Jim_EvalObjVector(Jim_Interp
*interp
, int objc
, Jim_Obj
*const *objv
)
10177 /* Incr refcount of arguments. */
10178 for (i
= 0; i
< objc
; i
++)
10179 Jim_IncrRefCount(objv
[i
]);
10181 retcode
= JimInvokeCommand(interp
, objc
, objv
);
10183 /* Decr refcount of arguments and return the retcode */
10184 for (i
= 0; i
< objc
; i
++)
10185 Jim_DecrRefCount(interp
, objv
[i
]);
10191 * Invokes 'prefix' as a command with the objv array as arguments.
10193 int Jim_EvalObjPrefix(Jim_Interp
*interp
, Jim_Obj
*prefix
, int objc
, Jim_Obj
*const *objv
)
10196 Jim_Obj
**nargv
= Jim_Alloc((objc
+ 1) * sizeof(*nargv
));
10199 memcpy(&nargv
[1], &objv
[0], sizeof(nargv
[0]) * objc
);
10200 ret
= Jim_EvalObjVector(interp
, objc
+ 1, nargv
);
10205 static void JimAddErrorToStack(Jim_Interp
*interp
, ScriptObj
*script
)
10207 if (!interp
->errorFlag
) {
10208 /* This is the first error, so save the file/line information and reset the stack */
10209 interp
->errorFlag
= 1;
10210 Jim_IncrRefCount(script
->fileNameObj
);
10211 Jim_DecrRefCount(interp
, interp
->errorFileNameObj
);
10212 interp
->errorFileNameObj
= script
->fileNameObj
;
10213 interp
->errorLine
= script
->linenr
;
10215 JimResetStackTrace(interp
);
10216 /* Always add a level where the error first occurs */
10217 interp
->addStackTrace
++;
10220 /* Now if this is an "interesting" level, add it to the stack trace */
10221 if (interp
->addStackTrace
> 0) {
10222 /* Add the stack info for the current level */
10224 JimAppendStackTrace(interp
, Jim_String(interp
->errorProc
), script
->fileNameObj
, script
->linenr
);
10226 /* Note: if we didn't have a filename for this level,
10227 * don't clear the addStackTrace flag
10228 * so we can pick it up at the next level
10230 if (Jim_Length(script
->fileNameObj
)) {
10231 interp
->addStackTrace
= 0;
10234 Jim_DecrRefCount(interp
, interp
->errorProc
);
10235 interp
->errorProc
= interp
->emptyObj
;
10236 Jim_IncrRefCount(interp
->errorProc
);
10240 static int JimSubstOneToken(Jim_Interp
*interp
, const ScriptToken
*token
, Jim_Obj
**objPtrPtr
)
10245 switch (token
->type
) {
10248 objPtr
= token
->objPtr
;
10251 objPtr
= Jim_GetVariable(interp
, token
->objPtr
, JIM_ERRMSG
);
10253 case JIM_TT_DICTSUGAR
:
10254 objPtr
= JimExpandDictSugar(interp
, token
->objPtr
);
10256 case JIM_TT_EXPRSUGAR
:
10257 ret
= Jim_EvalExpression(interp
, token
->objPtr
);
10258 if (ret
== JIM_OK
) {
10259 objPtr
= Jim_GetResult(interp
);
10266 ret
= Jim_EvalObj(interp
, token
->objPtr
);
10267 if (ret
== JIM_OK
|| ret
== JIM_RETURN
) {
10268 objPtr
= interp
->result
;
10270 /* includes JIM_BREAK, JIM_CONTINUE */
10276 "default token type (%d) reached " "in Jim_SubstObj().", token
->type
));
10281 *objPtrPtr
= objPtr
;
10287 /* Interpolate the given tokens into a unique Jim_Obj returned by reference
10288 * via *objPtrPtr. This function is only called by Jim_EvalObj() and Jim_SubstObj()
10289 * The returned object has refcount = 0.
10291 static Jim_Obj
*JimInterpolateTokens(Jim_Interp
*interp
, const ScriptToken
* token
, int tokens
, int flags
)
10295 Jim_Obj
*sintv
[JIM_EVAL_SINTV_LEN
];
10299 if (tokens
<= JIM_EVAL_SINTV_LEN
)
10302 intv
= Jim_Alloc(sizeof(Jim_Obj
*) * tokens
);
10304 /* Compute every token forming the argument
10305 * in the intv objects vector. */
10306 for (i
= 0; i
< tokens
; i
++) {
10307 switch (JimSubstOneToken(interp
, &token
[i
], &intv
[i
])) {
10312 if (flags
& JIM_SUBST_FLAG
) {
10317 /* XXX: Should probably set an error about break outside loop */
10318 /* fall through to error */
10320 if (flags
& JIM_SUBST_FLAG
) {
10324 /* XXX: Ditto continue outside loop */
10325 /* fall through to error */
10328 Jim_DecrRefCount(interp
, intv
[i
]);
10330 if (intv
!= sintv
) {
10335 Jim_IncrRefCount(intv
[i
]);
10336 Jim_String(intv
[i
]);
10337 totlen
+= intv
[i
]->length
;
10340 /* Fast path return for a single token */
10341 if (tokens
== 1 && intv
[0] && intv
== sintv
) {
10342 /* Reverse the Jim_IncrRefCount() above, but don't free the object */
10343 intv
[0]->refCount
--;
10347 /* Concatenate every token in an unique
10349 objPtr
= Jim_NewStringObjNoAlloc(interp
, NULL
, 0);
10351 if (tokens
== 4 && token
[0].type
== JIM_TT_ESC
&& token
[1].type
== JIM_TT_ESC
10352 && token
[2].type
== JIM_TT_VAR
) {
10353 /* May be able to do fast interpolated object -> dictSubst */
10354 objPtr
->typePtr
= &interpolatedObjType
;
10355 objPtr
->internalRep
.dictSubstValue
.varNameObjPtr
= token
[0].objPtr
;
10356 objPtr
->internalRep
.dictSubstValue
.indexObjPtr
= intv
[2];
10357 Jim_IncrRefCount(intv
[2]);
10359 else if (tokens
&& intv
[0] && intv
[0]->typePtr
== &sourceObjType
) {
10360 /* The first interpolated token is source, so preserve the source info */
10361 JimSetSourceInfo(interp
, objPtr
, intv
[0]->internalRep
.sourceValue
.fileNameObj
, intv
[0]->internalRep
.sourceValue
.lineNumber
);
10365 s
= objPtr
->bytes
= Jim_Alloc(totlen
+ 1);
10366 objPtr
->length
= totlen
;
10367 for (i
= 0; i
< tokens
; i
++) {
10369 memcpy(s
, intv
[i
]->bytes
, intv
[i
]->length
);
10370 s
+= intv
[i
]->length
;
10371 Jim_DecrRefCount(interp
, intv
[i
]);
10374 objPtr
->bytes
[totlen
] = '\0';
10375 /* Free the intv vector if not static. */
10376 if (intv
!= sintv
) {
10384 /* listPtr *must* be a list.
10385 * The contents of the list is evaluated with the first element as the command and
10386 * the remaining elements as the arguments.
10388 static int JimEvalObjList(Jim_Interp
*interp
, Jim_Obj
*listPtr
)
10390 int retcode
= JIM_OK
;
10392 JimPanic((Jim_IsList(listPtr
) == 0, "JimEvalObjList() invoked on non-list."));
10394 if (listPtr
->internalRep
.listValue
.len
) {
10395 Jim_IncrRefCount(listPtr
);
10396 retcode
= JimInvokeCommand(interp
,
10397 listPtr
->internalRep
.listValue
.len
,
10398 listPtr
->internalRep
.listValue
.ele
);
10399 Jim_DecrRefCount(interp
, listPtr
);
10404 int Jim_EvalObjList(Jim_Interp
*interp
, Jim_Obj
*listPtr
)
10406 SetListFromAny(interp
, listPtr
);
10407 return JimEvalObjList(interp
, listPtr
);
10410 int Jim_EvalObj(Jim_Interp
*interp
, Jim_Obj
*scriptObjPtr
)
10414 ScriptToken
*token
;
10415 int retcode
= JIM_OK
;
10416 Jim_Obj
*sargv
[JIM_EVAL_SARGV_LEN
], **argv
= NULL
;
10417 Jim_Obj
*prevScriptObj
;
10419 /* If the object is of type "list", with no string rep we can call
10420 * a specialized version of Jim_EvalObj() */
10421 if (Jim_IsList(scriptObjPtr
) && scriptObjPtr
->bytes
== NULL
) {
10422 return JimEvalObjList(interp
, scriptObjPtr
);
10425 Jim_IncrRefCount(scriptObjPtr
); /* Make sure it's shared. */
10426 script
= JimGetScript(interp
, scriptObjPtr
);
10427 if (!JimScriptValid(interp
, script
)) {
10428 Jim_DecrRefCount(interp
, scriptObjPtr
);
10432 /* Reset the interpreter result. This is useful to
10433 * return the empty result in the case of empty program. */
10434 Jim_SetEmptyResult(interp
);
10436 token
= script
->token
;
10438 #ifdef JIM_OPTIMIZATION
10439 /* Check for one of the following common scripts used by for, while
10444 if (script
->len
== 0) {
10445 Jim_DecrRefCount(interp
, scriptObjPtr
);
10448 if (script
->len
== 3
10449 && token
[1].objPtr
->typePtr
== &commandObjType
10450 && token
[1].objPtr
->internalRep
.cmdValue
.cmdPtr
->isproc
== 0
10451 && token
[1].objPtr
->internalRep
.cmdValue
.cmdPtr
->u
.native
.cmdProc
== Jim_IncrCoreCommand
10452 && token
[2].objPtr
->typePtr
== &variableObjType
) {
10454 Jim_Obj
*objPtr
= Jim_GetVariable(interp
, token
[2].objPtr
, JIM_NONE
);
10456 if (objPtr
&& !Jim_IsShared(objPtr
) && objPtr
->typePtr
== &intObjType
) {
10457 JimWideValue(objPtr
)++;
10458 Jim_InvalidateStringRep(objPtr
);
10459 Jim_DecrRefCount(interp
, scriptObjPtr
);
10460 Jim_SetResult(interp
, objPtr
);
10466 /* Now we have to make sure the internal repr will not be
10467 * freed on shimmering.
10469 * Think for example to this:
10471 * set x {llength $x; ... some more code ...}; eval $x
10473 * In order to preserve the internal rep, we increment the
10474 * inUse field of the script internal rep structure. */
10477 /* Stash the current script */
10478 prevScriptObj
= interp
->currentScriptObj
;
10479 interp
->currentScriptObj
= scriptObjPtr
;
10481 interp
->errorFlag
= 0;
10484 /* Execute every command sequentially until the end of the script
10485 * or an error occurs.
10487 for (i
= 0; i
< script
->len
&& retcode
== JIM_OK
; ) {
10491 /* First token of the line is always JIM_TT_LINE */
10492 argc
= token
[i
].objPtr
->internalRep
.scriptLineValue
.argc
;
10493 script
->linenr
= token
[i
].objPtr
->internalRep
.scriptLineValue
.line
;
10495 /* Allocate the arguments vector if required */
10496 if (argc
> JIM_EVAL_SARGV_LEN
)
10497 argv
= Jim_Alloc(sizeof(Jim_Obj
*) * argc
);
10499 /* Skip the JIM_TT_LINE token */
10502 /* Populate the arguments objects.
10503 * If an error occurs, retcode will be set and
10504 * 'j' will be set to the number of args expanded
10506 for (j
= 0; j
< argc
; j
++) {
10507 long wordtokens
= 1;
10509 Jim_Obj
*wordObjPtr
= NULL
;
10511 if (token
[i
].type
== JIM_TT_WORD
) {
10512 wordtokens
= JimWideValue(token
[i
++].objPtr
);
10513 if (wordtokens
< 0) {
10515 wordtokens
= -wordtokens
;
10519 if (wordtokens
== 1) {
10520 /* Fast path if the token does not
10521 * need interpolation */
10523 switch (token
[i
].type
) {
10526 wordObjPtr
= token
[i
].objPtr
;
10529 wordObjPtr
= Jim_GetVariable(interp
, token
[i
].objPtr
, JIM_ERRMSG
);
10531 case JIM_TT_EXPRSUGAR
:
10532 retcode
= Jim_EvalExpression(interp
, token
[i
].objPtr
);
10533 if (retcode
== JIM_OK
) {
10534 wordObjPtr
= Jim_GetResult(interp
);
10540 case JIM_TT_DICTSUGAR
:
10541 wordObjPtr
= JimExpandDictSugar(interp
, token
[i
].objPtr
);
10544 retcode
= Jim_EvalObj(interp
, token
[i
].objPtr
);
10545 if (retcode
== JIM_OK
) {
10546 wordObjPtr
= Jim_GetResult(interp
);
10550 JimPanic((1, "default token type reached " "in Jim_EvalObj()."));
10554 /* For interpolation we call a helper
10555 * function to do the work for us. */
10556 wordObjPtr
= JimInterpolateTokens(interp
, token
+ i
, wordtokens
, JIM_NONE
);
10560 if (retcode
== JIM_OK
) {
10566 Jim_IncrRefCount(wordObjPtr
);
10570 argv
[j
] = wordObjPtr
;
10573 /* Need to expand wordObjPtr into multiple args from argv[j] ... */
10574 int len
= Jim_ListLength(interp
, wordObjPtr
);
10575 int newargc
= argc
+ len
- 1;
10579 if (argv
== sargv
) {
10580 if (newargc
> JIM_EVAL_SARGV_LEN
) {
10581 argv
= Jim_Alloc(sizeof(*argv
) * newargc
);
10582 memcpy(argv
, sargv
, sizeof(*argv
) * j
);
10586 /* Need to realloc to make room for (len - 1) more entries */
10587 argv
= Jim_Realloc(argv
, sizeof(*argv
) * newargc
);
10591 /* Now copy in the expanded version */
10592 for (k
= 0; k
< len
; k
++) {
10593 argv
[j
++] = wordObjPtr
->internalRep
.listValue
.ele
[k
];
10594 Jim_IncrRefCount(wordObjPtr
->internalRep
.listValue
.ele
[k
]);
10597 /* The original object reference is no longer needed,
10598 * after the expansion it is no longer present on
10599 * the argument vector, but the single elements are
10601 Jim_DecrRefCount(interp
, wordObjPtr
);
10603 /* And update the indexes */
10609 if (retcode
== JIM_OK
&& argc
) {
10610 /* Invoke the command */
10611 retcode
= JimInvokeCommand(interp
, argc
, argv
);
10612 /* Check for a signal after each command */
10613 if (Jim_CheckSignal(interp
)) {
10614 retcode
= JIM_SIGNAL
;
10618 /* Finished with the command, so decrement ref counts of each argument */
10620 Jim_DecrRefCount(interp
, argv
[j
]);
10623 if (argv
!= sargv
) {
10629 /* Possibly add to the error stack trace */
10630 if (retcode
== JIM_ERR
) {
10631 JimAddErrorToStack(interp
, script
);
10633 /* Propagate the addStackTrace value through 'return -code error' */
10634 else if (retcode
!= JIM_RETURN
|| interp
->returnCode
!= JIM_ERR
) {
10635 /* No need to add stack trace */
10636 interp
->addStackTrace
= 0;
10639 /* Restore the current script */
10640 interp
->currentScriptObj
= prevScriptObj
;
10642 /* Note that we don't have to decrement inUse, because the
10643 * following code transfers our use of the reference again to
10644 * the script object. */
10645 Jim_FreeIntRep(interp
, scriptObjPtr
);
10646 scriptObjPtr
->typePtr
= &scriptObjType
;
10647 Jim_SetIntRepPtr(scriptObjPtr
, script
);
10648 Jim_DecrRefCount(interp
, scriptObjPtr
);
10653 static int JimSetProcArg(Jim_Interp
*interp
, Jim_Obj
*argNameObj
, Jim_Obj
*argValObj
)
10656 /* If argObjPtr begins with '&', do an automatic upvar */
10657 const char *varname
= Jim_String(argNameObj
);
10658 if (*varname
== '&') {
10659 /* First check that the target variable exists */
10661 Jim_CallFrame
*savedCallFrame
= interp
->framePtr
;
10663 interp
->framePtr
= interp
->framePtr
->parent
;
10664 objPtr
= Jim_GetVariable(interp
, argValObj
, JIM_ERRMSG
);
10665 interp
->framePtr
= savedCallFrame
;
10670 /* It exists, so perform the binding. */
10671 objPtr
= Jim_NewStringObj(interp
, varname
+ 1, -1);
10672 Jim_IncrRefCount(objPtr
);
10673 retcode
= Jim_SetVariableLink(interp
, objPtr
, argValObj
, interp
->framePtr
->parent
);
10674 Jim_DecrRefCount(interp
, objPtr
);
10677 retcode
= Jim_SetVariable(interp
, argNameObj
, argValObj
);
10683 * Sets the interp result to be an error message indicating the required proc args.
10685 static void JimSetProcWrongArgs(Jim_Interp
*interp
, Jim_Obj
*procNameObj
, Jim_Cmd
*cmd
)
10687 /* Create a nice error message, consistent with Tcl 8.5 */
10688 Jim_Obj
*argmsg
= Jim_NewStringObj(interp
, "", 0);
10691 for (i
= 0; i
< cmd
->u
.proc
.argListLen
; i
++) {
10692 Jim_AppendString(interp
, argmsg
, " ", 1);
10694 if (i
== cmd
->u
.proc
.argsPos
) {
10695 if (cmd
->u
.proc
.arglist
[i
].defaultObjPtr
) {
10697 Jim_AppendString(interp
, argmsg
, "?", 1);
10698 Jim_AppendObj(interp
, argmsg
, cmd
->u
.proc
.arglist
[i
].defaultObjPtr
);
10699 Jim_AppendString(interp
, argmsg
, " ...?", -1);
10702 /* We have plain args */
10703 Jim_AppendString(interp
, argmsg
, "?arg...?", -1);
10707 if (cmd
->u
.proc
.arglist
[i
].defaultObjPtr
) {
10708 Jim_AppendString(interp
, argmsg
, "?", 1);
10709 Jim_AppendObj(interp
, argmsg
, cmd
->u
.proc
.arglist
[i
].nameObjPtr
);
10710 Jim_AppendString(interp
, argmsg
, "?", 1);
10713 const char *arg
= Jim_String(cmd
->u
.proc
.arglist
[i
].nameObjPtr
);
10717 Jim_AppendString(interp
, argmsg
, arg
, -1);
10721 Jim_SetResultFormatted(interp
, "wrong # args: should be \"%#s%#s\"", procNameObj
, argmsg
);
10724 #ifdef jim_ext_namespace
10728 int Jim_EvalNamespace(Jim_Interp
*interp
, Jim_Obj
*scriptObj
, Jim_Obj
*nsObj
)
10730 Jim_CallFrame
*callFramePtr
;
10733 /* Create a new callframe */
10734 callFramePtr
= JimCreateCallFrame(interp
, interp
->framePtr
, nsObj
);
10735 callFramePtr
->argv
= &interp
->emptyObj
;
10736 callFramePtr
->argc
= 0;
10737 callFramePtr
->procArgsObjPtr
= NULL
;
10738 callFramePtr
->procBodyObjPtr
= scriptObj
;
10739 callFramePtr
->staticVars
= NULL
;
10740 callFramePtr
->fileNameObj
= interp
->emptyObj
;
10741 callFramePtr
->line
= 0;
10742 Jim_IncrRefCount(scriptObj
);
10743 interp
->framePtr
= callFramePtr
;
10745 /* Check if there are too nested calls */
10746 if (interp
->framePtr
->level
== interp
->maxCallFrameDepth
) {
10747 Jim_SetResultString(interp
, "Too many nested calls. Infinite recursion?", -1);
10751 /* Eval the body */
10752 retcode
= Jim_EvalObj(interp
, scriptObj
);
10755 /* Destroy the callframe */
10756 interp
->framePtr
= interp
->framePtr
->parent
;
10757 JimFreeCallFrame(interp
, callFramePtr
, JIM_FCF_REUSE
);
10763 /* Call a procedure implemented in Tcl.
10764 * It's possible to speed-up a lot this function, currently
10765 * the callframes are not cached, but allocated and
10766 * destroied every time. What is expecially costly is
10767 * to create/destroy the local vars hash table every time.
10769 * This can be fixed just implementing callframes caching
10770 * in JimCreateCallFrame() and JimFreeCallFrame(). */
10771 static int JimCallProcedure(Jim_Interp
*interp
, Jim_Cmd
*cmd
, int argc
, Jim_Obj
*const *argv
)
10773 Jim_CallFrame
*callFramePtr
;
10774 int i
, d
, retcode
, optargs
;
10778 if (argc
- 1 < cmd
->u
.proc
.reqArity
||
10779 (cmd
->u
.proc
.argsPos
< 0 && argc
- 1 > cmd
->u
.proc
.reqArity
+ cmd
->u
.proc
.optArity
)) {
10780 JimSetProcWrongArgs(interp
, argv
[0], cmd
);
10784 if (Jim_Length(cmd
->u
.proc
.bodyObjPtr
) == 0) {
10785 /* Optimise for procedure with no body - useful for optional debugging */
10789 /* Check if there are too nested calls */
10790 if (interp
->framePtr
->level
== interp
->maxCallFrameDepth
) {
10791 Jim_SetResultString(interp
, "Too many nested calls. Infinite recursion?", -1);
10795 /* Create a new callframe */
10796 callFramePtr
= JimCreateCallFrame(interp
, interp
->framePtr
, cmd
->u
.proc
.nsObj
);
10797 callFramePtr
->argv
= argv
;
10798 callFramePtr
->argc
= argc
;
10799 callFramePtr
->procArgsObjPtr
= cmd
->u
.proc
.argListObjPtr
;
10800 callFramePtr
->procBodyObjPtr
= cmd
->u
.proc
.bodyObjPtr
;
10801 callFramePtr
->staticVars
= cmd
->u
.proc
.staticVars
;
10803 /* Remember where we were called from. */
10804 script
= JimGetScript(interp
, interp
->currentScriptObj
);
10805 callFramePtr
->fileNameObj
= script
->fileNameObj
;
10806 callFramePtr
->line
= script
->linenr
;
10808 Jim_IncrRefCount(cmd
->u
.proc
.argListObjPtr
);
10809 Jim_IncrRefCount(cmd
->u
.proc
.bodyObjPtr
);
10810 interp
->framePtr
= callFramePtr
;
10812 /* How many optional args are available */
10813 optargs
= (argc
- 1 - cmd
->u
.proc
.reqArity
);
10815 /* Step 'i' along the actual args, and step 'd' along the formal args */
10817 for (d
= 0; d
< cmd
->u
.proc
.argListLen
; d
++) {
10818 Jim_Obj
*nameObjPtr
= cmd
->u
.proc
.arglist
[d
].nameObjPtr
;
10819 if (d
== cmd
->u
.proc
.argsPos
) {
10821 Jim_Obj
*listObjPtr
;
10823 if (cmd
->u
.proc
.reqArity
+ cmd
->u
.proc
.optArity
< argc
- 1) {
10824 argsLen
= argc
- 1 - (cmd
->u
.proc
.reqArity
+ cmd
->u
.proc
.optArity
);
10826 listObjPtr
= Jim_NewListObj(interp
, &argv
[i
], argsLen
);
10828 /* It is possible to rename args. */
10829 if (cmd
->u
.proc
.arglist
[d
].defaultObjPtr
) {
10830 nameObjPtr
=cmd
->u
.proc
.arglist
[d
].defaultObjPtr
;
10832 retcode
= Jim_SetVariable(interp
, nameObjPtr
, listObjPtr
);
10833 if (retcode
!= JIM_OK
) {
10841 /* Optional or required? */
10842 if (cmd
->u
.proc
.arglist
[d
].defaultObjPtr
== NULL
|| optargs
-- > 0) {
10843 retcode
= JimSetProcArg(interp
, nameObjPtr
, argv
[i
++]);
10846 /* Ran out, so use the default */
10847 retcode
= Jim_SetVariable(interp
, nameObjPtr
, cmd
->u
.proc
.arglist
[d
].defaultObjPtr
);
10849 if (retcode
!= JIM_OK
) {
10854 /* Eval the body */
10855 retcode
= Jim_EvalObj(interp
, cmd
->u
.proc
.bodyObjPtr
);
10859 /* Invoke $jim::defer then destroy the callframe */
10860 retcode
= JimInvokeDefer(interp
, retcode
);
10861 interp
->framePtr
= interp
->framePtr
->parent
;
10862 JimFreeCallFrame(interp
, callFramePtr
, JIM_FCF_REUSE
);
10864 /* Now chain any tailcalls in the parent frame */
10865 if (interp
->framePtr
->tailcallObj
) {
10867 Jim_Obj
*tailcallObj
= interp
->framePtr
->tailcallObj
;
10869 interp
->framePtr
->tailcallObj
= NULL
;
10871 if (retcode
== JIM_EVAL
) {
10872 retcode
= Jim_EvalObjList(interp
, tailcallObj
);
10873 if (retcode
== JIM_RETURN
) {
10874 /* If the result of the tailcall is 'return', push
10875 * it up to the caller
10877 interp
->returnLevel
++;
10880 Jim_DecrRefCount(interp
, tailcallObj
);
10881 } while (interp
->framePtr
->tailcallObj
);
10883 /* If the tailcall chain finished early, may need to manually discard the command */
10884 if (interp
->framePtr
->tailcallCmd
) {
10885 JimDecrCmdRefCount(interp
, interp
->framePtr
->tailcallCmd
);
10886 interp
->framePtr
->tailcallCmd
= NULL
;
10890 /* Handle the JIM_RETURN return code */
10891 if (retcode
== JIM_RETURN
) {
10892 if (--interp
->returnLevel
<= 0) {
10893 retcode
= interp
->returnCode
;
10894 interp
->returnCode
= JIM_OK
;
10895 interp
->returnLevel
= 0;
10898 else if (retcode
== JIM_ERR
) {
10899 interp
->addStackTrace
++;
10900 Jim_DecrRefCount(interp
, interp
->errorProc
);
10901 interp
->errorProc
= argv
[0];
10902 Jim_IncrRefCount(interp
->errorProc
);
10908 int Jim_EvalSource(Jim_Interp
*interp
, const char *filename
, int lineno
, const char *script
)
10911 Jim_Obj
*scriptObjPtr
;
10913 scriptObjPtr
= Jim_NewStringObj(interp
, script
, -1);
10914 Jim_IncrRefCount(scriptObjPtr
);
10917 Jim_Obj
*prevScriptObj
;
10919 JimSetSourceInfo(interp
, scriptObjPtr
, Jim_NewStringObj(interp
, filename
, -1), lineno
);
10921 prevScriptObj
= interp
->currentScriptObj
;
10922 interp
->currentScriptObj
= scriptObjPtr
;
10924 retval
= Jim_EvalObj(interp
, scriptObjPtr
);
10926 interp
->currentScriptObj
= prevScriptObj
;
10929 retval
= Jim_EvalObj(interp
, scriptObjPtr
);
10931 Jim_DecrRefCount(interp
, scriptObjPtr
);
10935 int Jim_Eval(Jim_Interp
*interp
, const char *script
)
10937 return Jim_EvalObj(interp
, Jim_NewStringObj(interp
, script
, -1));
10940 /* Execute script in the scope of the global level */
10941 int Jim_EvalGlobal(Jim_Interp
*interp
, const char *script
)
10944 Jim_CallFrame
*savedFramePtr
= interp
->framePtr
;
10946 interp
->framePtr
= interp
->topFramePtr
;
10947 retval
= Jim_Eval(interp
, script
);
10948 interp
->framePtr
= savedFramePtr
;
10953 int Jim_EvalFileGlobal(Jim_Interp
*interp
, const char *filename
)
10956 Jim_CallFrame
*savedFramePtr
= interp
->framePtr
;
10958 interp
->framePtr
= interp
->topFramePtr
;
10959 retval
= Jim_EvalFile(interp
, filename
);
10960 interp
->framePtr
= savedFramePtr
;
10965 #include <sys/stat.h>
10967 int Jim_EvalFile(Jim_Interp
*interp
, const char *filename
)
10971 Jim_Obj
*scriptObjPtr
;
10972 Jim_Obj
*prevScriptObj
;
10977 if (stat(filename
, &sb
) != 0 || (fp
= fopen(filename
, "rt")) == NULL
) {
10978 Jim_SetResultFormatted(interp
, "couldn't read file \"%s\": %s", filename
, strerror(errno
));
10981 if (sb
.st_size
== 0) {
10986 buf
= Jim_Alloc(sb
.st_size
+ 1);
10987 readlen
= fread(buf
, 1, sb
.st_size
, fp
);
10991 Jim_SetResultFormatted(interp
, "failed to load file \"%s\": %s", filename
, strerror(errno
));
10997 scriptObjPtr
= Jim_NewStringObjNoAlloc(interp
, buf
, readlen
);
10998 JimSetSourceInfo(interp
, scriptObjPtr
, Jim_NewStringObj(interp
, filename
, -1), 1);
10999 Jim_IncrRefCount(scriptObjPtr
);
11001 prevScriptObj
= interp
->currentScriptObj
;
11002 interp
->currentScriptObj
= scriptObjPtr
;
11004 retcode
= Jim_EvalObj(interp
, scriptObjPtr
);
11006 /* Handle the JIM_RETURN return code */
11007 if (retcode
== JIM_RETURN
) {
11008 if (--interp
->returnLevel
<= 0) {
11009 retcode
= interp
->returnCode
;
11010 interp
->returnCode
= JIM_OK
;
11011 interp
->returnLevel
= 0;
11014 if (retcode
== JIM_ERR
) {
11015 /* EvalFile changes context, so add a stack frame here */
11016 interp
->addStackTrace
++;
11019 interp
->currentScriptObj
= prevScriptObj
;
11021 Jim_DecrRefCount(interp
, scriptObjPtr
);
11026 /* -----------------------------------------------------------------------------
11028 * ---------------------------------------------------------------------------*/
11029 static void JimParseSubst(struct JimParserCtx
*pc
, int flags
)
11031 pc
->tstart
= pc
->p
;
11032 pc
->tline
= pc
->linenr
;
11034 if (pc
->len
== 0) {
11036 pc
->tt
= JIM_TT_EOL
;
11040 if (*pc
->p
== '[' && !(flags
& JIM_SUBST_NOCMD
)) {
11044 if (*pc
->p
== '$' && !(flags
& JIM_SUBST_NOVAR
)) {
11045 if (JimParseVar(pc
) == JIM_OK
) {
11048 /* Not a var, so treat as a string */
11049 pc
->tstart
= pc
->p
;
11050 flags
|= JIM_SUBST_NOVAR
;
11053 if (*pc
->p
== '$' && !(flags
& JIM_SUBST_NOVAR
)) {
11056 if (*pc
->p
== '[' && !(flags
& JIM_SUBST_NOCMD
)) {
11059 if (*pc
->p
== '\\' && pc
->len
> 1) {
11066 pc
->tend
= pc
->p
- 1;
11067 pc
->tt
= (flags
& JIM_SUBST_NOESC
) ? JIM_TT_STR
: JIM_TT_ESC
;
11070 /* The subst object type reuses most of the data structures and functions
11071 * of the script object. Script's data structures are a bit more complex
11072 * for what is needed for [subst]itution tasks, but the reuse helps to
11073 * deal with a single data structure at the cost of some more memory
11074 * usage for substitutions. */
11076 /* This method takes the string representation of an object
11077 * as a Tcl string where to perform [subst]itution, and generates
11078 * the pre-parsed internal representation. */
11079 static int SetSubstFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
, int flags
)
11082 const char *scriptText
= Jim_GetString(objPtr
, &scriptTextLen
);
11083 struct JimParserCtx parser
;
11084 struct ScriptObj
*script
= Jim_Alloc(sizeof(*script
));
11085 ParseTokenList tokenlist
;
11087 /* Initially parse the subst into tokens (in tokenlist) */
11088 ScriptTokenListInit(&tokenlist
);
11090 JimParserInit(&parser
, scriptText
, scriptTextLen
, 1);
11092 JimParseSubst(&parser
, flags
);
11094 /* Note that subst doesn't need the EOL token */
11097 ScriptAddToken(&tokenlist
, parser
.tstart
, parser
.tend
- parser
.tstart
+ 1, parser
.tt
,
11101 /* Create the "real" subst/script tokens from the initial token list */
11103 script
->substFlags
= flags
;
11104 script
->fileNameObj
= interp
->emptyObj
;
11105 Jim_IncrRefCount(script
->fileNameObj
);
11106 SubstObjAddTokens(interp
, script
, &tokenlist
);
11108 /* No longer need the token list */
11109 ScriptTokenListFree(&tokenlist
);
11111 #ifdef DEBUG_SHOW_SUBST
11115 printf("==== Subst ====\n");
11116 for (i
= 0; i
< script
->len
; i
++) {
11117 printf("[%2d] %s '%s'\n", i
, jim_tt_name(script
->token
[i
].type
),
11118 Jim_String(script
->token
[i
].objPtr
));
11123 /* Free the old internal rep and set the new one. */
11124 Jim_FreeIntRep(interp
, objPtr
);
11125 Jim_SetIntRepPtr(objPtr
, script
);
11126 objPtr
->typePtr
= &scriptObjType
;
11130 static ScriptObj
*Jim_GetSubst(Jim_Interp
*interp
, Jim_Obj
*objPtr
, int flags
)
11132 if (objPtr
->typePtr
!= &scriptObjType
|| ((ScriptObj
*)Jim_GetIntRepPtr(objPtr
))->substFlags
!= flags
)
11133 SetSubstFromAny(interp
, objPtr
, flags
);
11134 return (ScriptObj
*) Jim_GetIntRepPtr(objPtr
);
11137 /* Performs commands,variables,blackslashes substitution,
11138 * storing the result object (with refcount 0) into
11140 int Jim_SubstObj(Jim_Interp
*interp
, Jim_Obj
*substObjPtr
, Jim_Obj
**resObjPtrPtr
, int flags
)
11144 JimPanic((substObjPtr
->refCount
== 0, "Jim_SubstObj() called with zero refcount object"));
11146 script
= Jim_GetSubst(interp
, substObjPtr
, flags
);
11148 Jim_IncrRefCount(substObjPtr
); /* Make sure it's shared. */
11149 /* In order to preserve the internal rep, we increment the
11150 * inUse field of the script internal rep structure. */
11153 *resObjPtrPtr
= JimInterpolateTokens(interp
, script
->token
, script
->len
, flags
);
11156 Jim_DecrRefCount(interp
, substObjPtr
);
11157 if (*resObjPtrPtr
== NULL
) {
11163 /* -----------------------------------------------------------------------------
11164 * Core commands utility functions
11165 * ---------------------------------------------------------------------------*/
11166 void Jim_WrongNumArgs(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
, const char *msg
)
11169 Jim_Obj
*listObjPtr
;
11171 JimPanic((argc
== 0, "Jim_WrongNumArgs() called with argc=0"));
11173 listObjPtr
= Jim_NewListObj(interp
, argv
, argc
);
11176 Jim_ListAppendElement(interp
, listObjPtr
, Jim_NewStringObj(interp
, msg
, -1));
11178 Jim_IncrRefCount(listObjPtr
);
11179 objPtr
= Jim_ListJoin(interp
, listObjPtr
, " ", 1);
11180 Jim_DecrRefCount(interp
, listObjPtr
);
11182 Jim_SetResultFormatted(interp
, "wrong # args: should be \"%#s\"", objPtr
);
11186 * May add the key and/or value to the list.
11188 typedef void JimHashtableIteratorCallbackType(Jim_Interp
*interp
, Jim_Obj
*listObjPtr
,
11189 Jim_HashEntry
*he
, int type
);
11191 #define JimTrivialMatch(pattern) (strpbrk((pattern), "*[?\\") == NULL)
11194 * For each key of the hash table 'ht' (with string keys) which matches the glob pattern (all if NULL),
11195 * invoke the callback to add entries to a list.
11196 * Returns the list.
11198 static Jim_Obj
*JimHashtablePatternMatch(Jim_Interp
*interp
, Jim_HashTable
*ht
, Jim_Obj
*patternObjPtr
,
11199 JimHashtableIteratorCallbackType
*callback
, int type
)
11202 Jim_Obj
*listObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
11204 /* Check for the non-pattern case. We can do this much more efficiently. */
11205 if (patternObjPtr
&& JimTrivialMatch(Jim_String(patternObjPtr
))) {
11206 he
= Jim_FindHashEntry(ht
, Jim_String(patternObjPtr
));
11208 callback(interp
, listObjPtr
, he
, type
);
11212 Jim_HashTableIterator htiter
;
11213 JimInitHashTableIterator(ht
, &htiter
);
11214 while ((he
= Jim_NextHashEntry(&htiter
)) != NULL
) {
11215 if (patternObjPtr
== NULL
|| JimGlobMatch(Jim_String(patternObjPtr
), he
->key
, 0)) {
11216 callback(interp
, listObjPtr
, he
, type
);
11223 /* Keep these in order */
11224 #define JIM_CMDLIST_COMMANDS 0
11225 #define JIM_CMDLIST_PROCS 1
11226 #define JIM_CMDLIST_CHANNELS 2
11229 * Adds matching command names (procs, channels) to the list.
11231 static void JimCommandMatch(Jim_Interp
*interp
, Jim_Obj
*listObjPtr
,
11232 Jim_HashEntry
*he
, int type
)
11234 Jim_Cmd
*cmdPtr
= Jim_GetHashEntryVal(he
);
11237 if (type
== JIM_CMDLIST_PROCS
&& !cmdPtr
->isproc
) {
11242 objPtr
= Jim_NewStringObj(interp
, he
->key
, -1);
11243 Jim_IncrRefCount(objPtr
);
11245 if (type
!= JIM_CMDLIST_CHANNELS
|| Jim_AioFilehandle(interp
, objPtr
)) {
11246 Jim_ListAppendElement(interp
, listObjPtr
, objPtr
);
11248 Jim_DecrRefCount(interp
, objPtr
);
11251 /* type is JIM_CMDLIST_xxx */
11252 static Jim_Obj
*JimCommandsList(Jim_Interp
*interp
, Jim_Obj
*patternObjPtr
, int type
)
11254 return JimHashtablePatternMatch(interp
, &interp
->commands
, patternObjPtr
, JimCommandMatch
, type
);
11257 /* Keep these in order */
11258 #define JIM_VARLIST_GLOBALS 0
11259 #define JIM_VARLIST_LOCALS 1
11260 #define JIM_VARLIST_VARS 2
11262 #define JIM_VARLIST_VALUES 0x1000
11265 * Adds matching variable names to the list.
11267 static void JimVariablesMatch(Jim_Interp
*interp
, Jim_Obj
*listObjPtr
,
11268 Jim_HashEntry
*he
, int type
)
11270 Jim_Var
*varPtr
= Jim_GetHashEntryVal(he
);
11272 if (type
!= JIM_VARLIST_LOCALS
|| varPtr
->linkFramePtr
== NULL
) {
11273 Jim_ListAppendElement(interp
, listObjPtr
, Jim_NewStringObj(interp
, he
->key
, -1));
11274 if (type
& JIM_VARLIST_VALUES
) {
11275 Jim_ListAppendElement(interp
, listObjPtr
, varPtr
->objPtr
);
11280 /* mode is JIM_VARLIST_xxx */
11281 static Jim_Obj
*JimVariablesList(Jim_Interp
*interp
, Jim_Obj
*patternObjPtr
, int mode
)
11283 if (mode
== JIM_VARLIST_LOCALS
&& interp
->framePtr
== interp
->topFramePtr
) {
11284 /* For [info locals], if we are at top level an emtpy list
11285 * is returned. I don't agree, but we aim at compatibility (SS) */
11286 return interp
->emptyObj
;
11289 Jim_CallFrame
*framePtr
= (mode
== JIM_VARLIST_GLOBALS
) ? interp
->topFramePtr
: interp
->framePtr
;
11290 return JimHashtablePatternMatch(interp
, &framePtr
->vars
, patternObjPtr
, JimVariablesMatch
, mode
);
11294 static int JimInfoLevel(Jim_Interp
*interp
, Jim_Obj
*levelObjPtr
,
11295 Jim_Obj
**objPtrPtr
, int info_level_cmd
)
11297 Jim_CallFrame
*targetCallFrame
;
11299 targetCallFrame
= JimGetCallFrameByInteger(interp
, levelObjPtr
);
11300 if (targetCallFrame
== NULL
) {
11303 /* No proc call at toplevel callframe */
11304 if (targetCallFrame
== interp
->topFramePtr
) {
11305 Jim_SetResultFormatted(interp
, "bad level \"%#s\"", levelObjPtr
);
11308 if (info_level_cmd
) {
11309 *objPtrPtr
= Jim_NewListObj(interp
, targetCallFrame
->argv
, targetCallFrame
->argc
);
11312 Jim_Obj
*listObj
= Jim_NewListObj(interp
, NULL
, 0);
11314 Jim_ListAppendElement(interp
, listObj
, targetCallFrame
->argv
[0]);
11315 Jim_ListAppendElement(interp
, listObj
, targetCallFrame
->fileNameObj
);
11316 Jim_ListAppendElement(interp
, listObj
, Jim_NewIntObj(interp
, targetCallFrame
->line
));
11317 *objPtrPtr
= listObj
;
11322 /* -----------------------------------------------------------------------------
11324 * ---------------------------------------------------------------------------*/
11326 /* fake [puts] -- not the real puts, just for debugging. */
11327 static int Jim_PutsCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11329 if (argc
!= 2 && argc
!= 3) {
11330 Jim_WrongNumArgs(interp
, 1, argv
, "?-nonewline? string");
11334 if (!Jim_CompareStringImmediate(interp
, argv
[1], "-nonewline")) {
11335 Jim_SetResultString(interp
, "The second argument must " "be -nonewline", -1);
11339 fputs(Jim_String(argv
[2]), stdout
);
11343 puts(Jim_String(argv
[1]));
11348 /* Helper for [+] and [*] */
11349 static int JimAddMulHelper(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
, int op
)
11351 jim_wide wideValue
, res
;
11352 double doubleValue
, doubleRes
;
11355 res
= (op
== JIM_EXPROP_ADD
) ? 0 : 1;
11357 for (i
= 1; i
< argc
; i
++) {
11358 if (Jim_GetWide(interp
, argv
[i
], &wideValue
) != JIM_OK
)
11360 if (op
== JIM_EXPROP_ADD
)
11365 Jim_SetResultInt(interp
, res
);
11368 doubleRes
= (double)res
;
11369 for (; i
< argc
; i
++) {
11370 if (Jim_GetDouble(interp
, argv
[i
], &doubleValue
) != JIM_OK
)
11372 if (op
== JIM_EXPROP_ADD
)
11373 doubleRes
+= doubleValue
;
11375 doubleRes
*= doubleValue
;
11377 Jim_SetResult(interp
, Jim_NewDoubleObj(interp
, doubleRes
));
11381 /* Helper for [-] and [/] */
11382 static int JimSubDivHelper(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
, int op
)
11384 jim_wide wideValue
, res
= 0;
11385 double doubleValue
, doubleRes
= 0;
11389 Jim_WrongNumArgs(interp
, 1, argv
, "number ?number ... number?");
11392 else if (argc
== 2) {
11393 /* The arity = 2 case is different. For [- x] returns -x,
11394 * while [/ x] returns 1/x. */
11395 if (Jim_GetWide(interp
, argv
[1], &wideValue
) != JIM_OK
) {
11396 if (Jim_GetDouble(interp
, argv
[1], &doubleValue
) != JIM_OK
) {
11400 if (op
== JIM_EXPROP_SUB
)
11401 doubleRes
= -doubleValue
;
11403 doubleRes
= 1.0 / doubleValue
;
11404 Jim_SetResult(interp
, Jim_NewDoubleObj(interp
, doubleRes
));
11408 if (op
== JIM_EXPROP_SUB
) {
11410 Jim_SetResultInt(interp
, res
);
11413 doubleRes
= 1.0 / wideValue
;
11414 Jim_SetResult(interp
, Jim_NewDoubleObj(interp
, doubleRes
));
11419 if (Jim_GetWide(interp
, argv
[1], &res
) != JIM_OK
) {
11420 if (Jim_GetDouble(interp
, argv
[1], &doubleRes
)
11429 for (i
= 2; i
< argc
; i
++) {
11430 if (Jim_GetWide(interp
, argv
[i
], &wideValue
) != JIM_OK
) {
11431 doubleRes
= (double)res
;
11434 if (op
== JIM_EXPROP_SUB
)
11437 if (wideValue
== 0) {
11438 Jim_SetResultString(interp
, "Division by zero", -1);
11444 Jim_SetResultInt(interp
, res
);
11447 for (; i
< argc
; i
++) {
11448 if (Jim_GetDouble(interp
, argv
[i
], &doubleValue
) != JIM_OK
)
11450 if (op
== JIM_EXPROP_SUB
)
11451 doubleRes
-= doubleValue
;
11453 doubleRes
/= doubleValue
;
11455 Jim_SetResult(interp
, Jim_NewDoubleObj(interp
, doubleRes
));
11461 static int Jim_AddCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11463 return JimAddMulHelper(interp
, argc
, argv
, JIM_EXPROP_ADD
);
11467 static int Jim_MulCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11469 return JimAddMulHelper(interp
, argc
, argv
, JIM_EXPROP_MUL
);
11473 static int Jim_SubCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11475 return JimSubDivHelper(interp
, argc
, argv
, JIM_EXPROP_SUB
);
11479 static int Jim_DivCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11481 return JimSubDivHelper(interp
, argc
, argv
, JIM_EXPROP_DIV
);
11485 static int Jim_SetCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11487 if (argc
!= 2 && argc
!= 3) {
11488 Jim_WrongNumArgs(interp
, 1, argv
, "varName ?newValue?");
11494 objPtr
= Jim_GetVariable(interp
, argv
[1], JIM_ERRMSG
);
11497 Jim_SetResult(interp
, objPtr
);
11500 /* argc == 3 case. */
11501 if (Jim_SetVariable(interp
, argv
[1], argv
[2]) != JIM_OK
)
11503 Jim_SetResult(interp
, argv
[2]);
11509 * unset ?-nocomplain? ?--? ?varName ...?
11511 static int Jim_UnsetCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11517 if (Jim_CompareStringImmediate(interp
, argv
[i
], "--")) {
11521 if (Jim_CompareStringImmediate(interp
, argv
[i
], "-nocomplain")) {
11530 if (Jim_UnsetVariable(interp
, argv
[i
], complain
? JIM_ERRMSG
: JIM_NONE
) != JIM_OK
11540 static int Jim_WhileCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11543 Jim_WrongNumArgs(interp
, 1, argv
, "condition body");
11547 /* The general purpose implementation of while starts here */
11549 int boolean
, retval
;
11551 if ((retval
= Jim_GetBoolFromExpr(interp
, argv
[1], &boolean
)) != JIM_OK
)
11556 if ((retval
= Jim_EvalObj(interp
, argv
[2])) != JIM_OK
) {
11570 Jim_SetEmptyResult(interp
);
11575 static int Jim_ForCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11579 Jim_Obj
*varNamePtr
= NULL
;
11580 Jim_Obj
*stopVarNamePtr
= NULL
;
11583 Jim_WrongNumArgs(interp
, 1, argv
, "start test next body");
11587 /* Do the initialisation */
11588 if ((retval
= Jim_EvalObj(interp
, argv
[1])) != JIM_OK
) {
11592 /* And do the first test now. Better for optimisation
11593 * if we can do next/test at the bottom of the loop
11595 retval
= Jim_GetBoolFromExpr(interp
, argv
[2], &boolean
);
11597 /* Ready to do the body as follows:
11599 * body // check retcode
11600 * next // check retcode
11601 * test // check retcode/test bool
11605 #ifdef JIM_OPTIMIZATION
11606 /* Check if the for is on the form:
11607 * for ... {$i < CONST} {incr i}
11608 * for ... {$i < $j} {incr i}
11610 if (retval
== JIM_OK
&& boolean
) {
11611 ScriptObj
*incrScript
;
11612 struct ExprTree
*expr
;
11613 jim_wide stop
, currentVal
;
11617 /* Do it only if there aren't shared arguments */
11618 expr
= JimGetExpression(interp
, argv
[2]);
11619 incrScript
= JimGetScript(interp
, argv
[3]);
11621 /* Ensure proper lengths to start */
11622 if (incrScript
== NULL
|| incrScript
->len
!= 3 || !expr
|| expr
->len
!= 3) {
11625 /* Ensure proper token types. */
11626 if (incrScript
->token
[1].type
!= JIM_TT_ESC
) {
11630 if (expr
->expr
->type
== JIM_EXPROP_LT
) {
11633 else if (expr
->expr
->type
== JIM_EXPROP_LTE
) {
11640 if (expr
->expr
->left
->type
!= JIM_TT_VAR
) {
11644 if (expr
->expr
->right
->type
!= JIM_TT_VAR
&& expr
->expr
->right
->type
!= JIM_TT_EXPR_INT
) {
11648 /* Update command must be incr */
11649 if (!Jim_CompareStringImmediate(interp
, incrScript
->token
[1].objPtr
, "incr")) {
11653 /* incr, expression must be about the same variable */
11654 if (!Jim_StringEqObj(incrScript
->token
[2].objPtr
, expr
->expr
->left
->objPtr
)) {
11658 /* Get the stop condition (must be a variable or integer) */
11659 if (expr
->expr
->right
->type
== JIM_TT_EXPR_INT
) {
11660 if (Jim_GetWide(interp
, expr
->expr
->right
->objPtr
, &stop
) == JIM_ERR
) {
11665 stopVarNamePtr
= expr
->expr
->right
->objPtr
;
11666 Jim_IncrRefCount(stopVarNamePtr
);
11667 /* Keep the compiler happy */
11671 /* Initialization */
11672 varNamePtr
= expr
->expr
->left
->objPtr
;
11673 Jim_IncrRefCount(varNamePtr
);
11675 objPtr
= Jim_GetVariable(interp
, varNamePtr
, JIM_NONE
);
11676 if (objPtr
== NULL
|| Jim_GetWide(interp
, objPtr
, ¤tVal
) != JIM_OK
) {
11680 /* --- OPTIMIZED FOR --- */
11681 while (retval
== JIM_OK
) {
11682 /* === Check condition === */
11683 /* Note that currentVal is already set here */
11685 /* Immediate or Variable? get the 'stop' value if the latter. */
11686 if (stopVarNamePtr
) {
11687 objPtr
= Jim_GetVariable(interp
, stopVarNamePtr
, JIM_NONE
);
11688 if (objPtr
== NULL
|| Jim_GetWide(interp
, objPtr
, &stop
) != JIM_OK
) {
11693 if (currentVal
>= stop
+ cmpOffset
) {
11698 retval
= Jim_EvalObj(interp
, argv
[4]);
11699 if (retval
== JIM_OK
|| retval
== JIM_CONTINUE
) {
11702 objPtr
= Jim_GetVariable(interp
, varNamePtr
, JIM_ERRMSG
);
11705 if (objPtr
== NULL
) {
11709 if (!Jim_IsShared(objPtr
) && objPtr
->typePtr
== &intObjType
) {
11710 currentVal
= ++JimWideValue(objPtr
);
11711 Jim_InvalidateStringRep(objPtr
);
11714 if (Jim_GetWide(interp
, objPtr
, ¤tVal
) != JIM_OK
||
11715 Jim_SetVariable(interp
, varNamePtr
, Jim_NewIntObj(interp
,
11716 ++currentVal
)) != JIM_OK
) {
11727 while (boolean
&& (retval
== JIM_OK
|| retval
== JIM_CONTINUE
)) {
11729 retval
= Jim_EvalObj(interp
, argv
[4]);
11731 if (retval
== JIM_OK
|| retval
== JIM_CONTINUE
) {
11733 JIM_IF_OPTIM(evalnext
:)
11734 retval
= Jim_EvalObj(interp
, argv
[3]);
11735 if (retval
== JIM_OK
|| retval
== JIM_CONTINUE
) {
11737 JIM_IF_OPTIM(testcond
:)
11738 retval
= Jim_GetBoolFromExpr(interp
, argv
[2], &boolean
);
11743 if (stopVarNamePtr
) {
11744 Jim_DecrRefCount(interp
, stopVarNamePtr
);
11747 Jim_DecrRefCount(interp
, varNamePtr
);
11750 if (retval
== JIM_CONTINUE
|| retval
== JIM_BREAK
|| retval
== JIM_OK
) {
11751 Jim_SetEmptyResult(interp
);
11759 static int Jim_LoopCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11765 Jim_Obj
*bodyObjPtr
;
11767 if (argc
!= 5 && argc
!= 6) {
11768 Jim_WrongNumArgs(interp
, 1, argv
, "var first limit ?incr? body");
11772 if (Jim_GetWide(interp
, argv
[2], &i
) != JIM_OK
||
11773 Jim_GetWide(interp
, argv
[3], &limit
) != JIM_OK
||
11774 (argc
== 6 && Jim_GetWide(interp
, argv
[4], &incr
) != JIM_OK
)) {
11777 bodyObjPtr
= (argc
== 5) ? argv
[4] : argv
[5];
11779 retval
= Jim_SetVariable(interp
, argv
[1], argv
[2]);
11781 while (((i
< limit
&& incr
> 0) || (i
> limit
&& incr
< 0)) && retval
== JIM_OK
) {
11782 retval
= Jim_EvalObj(interp
, bodyObjPtr
);
11783 if (retval
== JIM_OK
|| retval
== JIM_CONTINUE
) {
11784 Jim_Obj
*objPtr
= Jim_GetVariable(interp
, argv
[1], JIM_ERRMSG
);
11791 if (objPtr
&& !Jim_IsShared(objPtr
) && objPtr
->typePtr
== &intObjType
) {
11792 if (argv
[1]->typePtr
!= &variableObjType
) {
11793 if (Jim_SetVariable(interp
, argv
[1], objPtr
) != JIM_OK
) {
11797 JimWideValue(objPtr
) = i
;
11798 Jim_InvalidateStringRep(objPtr
);
11800 /* The following step is required in order to invalidate the
11801 * string repr of "FOO" if the var name is of the form of "FOO(IDX)" */
11802 if (argv
[1]->typePtr
!= &variableObjType
) {
11803 if (Jim_SetVariable(interp
, argv
[1], objPtr
) != JIM_OK
) {
11810 objPtr
= Jim_NewIntObj(interp
, i
);
11811 retval
= Jim_SetVariable(interp
, argv
[1], objPtr
);
11812 if (retval
!= JIM_OK
) {
11813 Jim_FreeNewObj(interp
, objPtr
);
11819 if (retval
== JIM_OK
|| retval
== JIM_CONTINUE
|| retval
== JIM_BREAK
) {
11820 Jim_SetEmptyResult(interp
);
11826 /* List iterators make it easy to iterate over a list.
11827 * At some point iterators will be expanded to support generators.
11835 * Initialise the iterator at the start of the list.
11837 static void JimListIterInit(Jim_ListIter
*iter
, Jim_Obj
*objPtr
)
11839 iter
->objPtr
= objPtr
;
11844 * Returns the next object from the list, or NULL on end-of-list.
11846 static Jim_Obj
*JimListIterNext(Jim_Interp
*interp
, Jim_ListIter
*iter
)
11848 if (iter
->idx
>= Jim_ListLength(interp
, iter
->objPtr
)) {
11851 return iter
->objPtr
->internalRep
.listValue
.ele
[iter
->idx
++];
11855 * Returns 1 if end-of-list has been reached.
11857 static int JimListIterDone(Jim_Interp
*interp
, Jim_ListIter
*iter
)
11859 return iter
->idx
>= Jim_ListLength(interp
, iter
->objPtr
);
11862 /* foreach + lmap implementation. */
11863 static int JimForeachMapHelper(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
, int doMap
)
11865 int result
= JIM_OK
;
11867 Jim_ListIter twoiters
[2]; /* Avoid allocation for a single list */
11868 Jim_ListIter
*iters
;
11870 Jim_Obj
*resultObj
;
11872 if (argc
< 4 || argc
% 2 != 0) {
11873 Jim_WrongNumArgs(interp
, 1, argv
, "varList list ?varList list ...? script");
11876 script
= argv
[argc
- 1]; /* Last argument is a script */
11877 numargs
= (argc
- 1 - 1); /* argc - 'foreach' - script */
11879 if (numargs
== 2) {
11883 iters
= Jim_Alloc(numargs
* sizeof(*iters
));
11885 for (i
= 0; i
< numargs
; i
++) {
11886 JimListIterInit(&iters
[i
], argv
[i
+ 1]);
11887 if (i
% 2 == 0 && JimListIterDone(interp
, &iters
[i
])) {
11891 if (result
!= JIM_OK
) {
11892 Jim_SetResultString(interp
, "foreach varlist is empty", -1);
11893 goto empty_varlist
;
11897 resultObj
= Jim_NewListObj(interp
, NULL
, 0);
11900 resultObj
= interp
->emptyObj
;
11902 Jim_IncrRefCount(resultObj
);
11905 /* Have we expired all lists? */
11906 for (i
= 0; i
< numargs
; i
+= 2) {
11907 if (!JimListIterDone(interp
, &iters
[i
+ 1])) {
11911 if (i
== numargs
) {
11916 /* For each list */
11917 for (i
= 0; i
< numargs
; i
+= 2) {
11921 JimListIterInit(&iters
[i
], argv
[i
+ 1]);
11922 while ((varName
= JimListIterNext(interp
, &iters
[i
])) != NULL
) {
11923 Jim_Obj
*valObj
= JimListIterNext(interp
, &iters
[i
+ 1]);
11925 /* Ran out, so store the empty string */
11926 valObj
= interp
->emptyObj
;
11928 /* Avoid shimmering */
11929 Jim_IncrRefCount(valObj
);
11930 result
= Jim_SetVariable(interp
, varName
, valObj
);
11931 Jim_DecrRefCount(interp
, valObj
);
11932 if (result
!= JIM_OK
) {
11937 switch (result
= Jim_EvalObj(interp
, script
)) {
11940 Jim_ListAppendElement(interp
, resultObj
, interp
->result
);
11953 Jim_SetResult(interp
, resultObj
);
11955 Jim_DecrRefCount(interp
, resultObj
);
11964 static int Jim_ForeachCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11966 return JimForeachMapHelper(interp
, argc
, argv
, 0);
11970 static int Jim_LmapCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11972 return JimForeachMapHelper(interp
, argc
, argv
, 1);
11976 static int Jim_LassignCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11978 int result
= JIM_ERR
;
11981 Jim_Obj
*resultObj
;
11984 Jim_WrongNumArgs(interp
, 1, argv
, "varList list ?varName ...?");
11988 JimListIterInit(&iter
, argv
[1]);
11990 for (i
= 2; i
< argc
; i
++) {
11991 Jim_Obj
*valObj
= JimListIterNext(interp
, &iter
);
11992 result
= Jim_SetVariable(interp
, argv
[i
], valObj
? valObj
: interp
->emptyObj
);
11993 if (result
!= JIM_OK
) {
11998 resultObj
= Jim_NewListObj(interp
, NULL
, 0);
11999 while (!JimListIterDone(interp
, &iter
)) {
12000 Jim_ListAppendElement(interp
, resultObj
, JimListIterNext(interp
, &iter
));
12003 Jim_SetResult(interp
, resultObj
);
12009 static int Jim_IfCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12011 int boolean
, retval
, current
= 1, falsebody
= 0;
12015 /* Far not enough arguments given! */
12016 if (current
>= argc
)
12018 if ((retval
= Jim_GetBoolFromExpr(interp
, argv
[current
++], &boolean
))
12021 /* There lacks something, isn't it? */
12022 if (current
>= argc
)
12024 if (Jim_CompareStringImmediate(interp
, argv
[current
], "then"))
12026 /* Tsk tsk, no then-clause? */
12027 if (current
>= argc
)
12030 return Jim_EvalObj(interp
, argv
[current
]);
12031 /* Ok: no else-clause follows */
12032 if (++current
>= argc
) {
12033 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
12036 falsebody
= current
++;
12037 if (Jim_CompareStringImmediate(interp
, argv
[falsebody
], "else")) {
12038 /* IIICKS - else-clause isn't last cmd? */
12039 if (current
!= argc
- 1)
12041 return Jim_EvalObj(interp
, argv
[current
]);
12043 else if (Jim_CompareStringImmediate(interp
, argv
[falsebody
], "elseif"))
12044 /* Ok: elseif follows meaning all the stuff
12045 * again (how boring...) */
12047 /* OOPS - else-clause is not last cmd? */
12048 else if (falsebody
!= argc
- 1)
12050 return Jim_EvalObj(interp
, argv
[falsebody
]);
12055 Jim_WrongNumArgs(interp
, 1, argv
, "condition ?then? trueBody ?elseif ...? ?else? falseBody");
12060 /* Returns 1 if match, 0 if no match or -<error> on error (e.g. -JIM_ERR, -JIM_BREAK)*/
12061 int Jim_CommandMatchObj(Jim_Interp
*interp
, Jim_Obj
*commandObj
, Jim_Obj
*patternObj
,
12062 Jim_Obj
*stringObj
, int nocase
)
12069 parms
[argc
++] = commandObj
;
12071 parms
[argc
++] = Jim_NewStringObj(interp
, "-nocase", -1);
12073 parms
[argc
++] = patternObj
;
12074 parms
[argc
++] = stringObj
;
12076 rc
= Jim_EvalObjVector(interp
, argc
, parms
);
12078 if (rc
!= JIM_OK
|| Jim_GetLong(interp
, Jim_GetResult(interp
), &eq
) != JIM_OK
) {
12086 static int Jim_SwitchCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12088 enum { SWITCH_EXACT
, SWITCH_GLOB
, SWITCH_RE
, SWITCH_CMD
};
12089 int matchOpt
= SWITCH_EXACT
, opt
= 1, patCount
, i
;
12090 Jim_Obj
*command
= NULL
, *scriptObj
= NULL
, *strObj
;
12091 Jim_Obj
**caseList
;
12095 Jim_WrongNumArgs(interp
, 1, argv
, "?options? string "
12096 "pattern body ... ?default body? or " "{pattern body ?pattern body ...?}");
12099 for (opt
= 1; opt
< argc
; ++opt
) {
12100 const char *option
= Jim_String(argv
[opt
]);
12102 if (*option
!= '-')
12104 else if (strncmp(option
, "--", 2) == 0) {
12108 else if (strncmp(option
, "-exact", 2) == 0)
12109 matchOpt
= SWITCH_EXACT
;
12110 else if (strncmp(option
, "-glob", 2) == 0)
12111 matchOpt
= SWITCH_GLOB
;
12112 else if (strncmp(option
, "-regexp", 2) == 0)
12113 matchOpt
= SWITCH_RE
;
12114 else if (strncmp(option
, "-command", 2) == 0) {
12115 matchOpt
= SWITCH_CMD
;
12116 if ((argc
- opt
) < 2)
12118 command
= argv
[++opt
];
12121 Jim_SetResultFormatted(interp
,
12122 "bad option \"%#s\": must be -exact, -glob, -regexp, -command procname or --",
12126 if ((argc
- opt
) < 2)
12129 strObj
= argv
[opt
++];
12130 patCount
= argc
- opt
;
12131 if (patCount
== 1) {
12132 JimListGetElements(interp
, argv
[opt
], &patCount
, &caseList
);
12135 caseList
= (Jim_Obj
**)&argv
[opt
];
12136 if (patCount
== 0 || patCount
% 2 != 0)
12138 for (i
= 0; scriptObj
== NULL
&& i
< patCount
; i
+= 2) {
12139 Jim_Obj
*patObj
= caseList
[i
];
12141 if (!Jim_CompareStringImmediate(interp
, patObj
, "default")
12142 || i
< (patCount
- 2)) {
12143 switch (matchOpt
) {
12145 if (Jim_StringEqObj(strObj
, patObj
))
12146 scriptObj
= caseList
[i
+ 1];
12149 if (Jim_StringMatchObj(interp
, patObj
, strObj
, 0))
12150 scriptObj
= caseList
[i
+ 1];
12153 command
= Jim_NewStringObj(interp
, "regexp", -1);
12154 /* Fall thru intentionally */
12156 int rc
= Jim_CommandMatchObj(interp
, command
, patObj
, strObj
, 0);
12158 /* After the execution of a command we need to
12159 * make sure to reconvert the object into a list
12160 * again. Only for the single-list style [switch]. */
12161 if (argc
- opt
== 1) {
12162 JimListGetElements(interp
, argv
[opt
], &patCount
, &caseList
);
12164 /* command is here already decref'd */
12169 scriptObj
= caseList
[i
+ 1];
12175 scriptObj
= caseList
[i
+ 1];
12178 for (; i
< patCount
&& Jim_CompareStringImmediate(interp
, scriptObj
, "-"); i
+= 2)
12179 scriptObj
= caseList
[i
+ 1];
12180 if (scriptObj
&& Jim_CompareStringImmediate(interp
, scriptObj
, "-")) {
12181 Jim_SetResultFormatted(interp
, "no body specified for pattern \"%#s\"", caseList
[i
- 2]);
12184 Jim_SetEmptyResult(interp
);
12186 return Jim_EvalObj(interp
, scriptObj
);
12192 static int Jim_ListCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12194 Jim_Obj
*listObjPtr
;
12196 listObjPtr
= Jim_NewListObj(interp
, argv
+ 1, argc
- 1);
12197 Jim_SetResult(interp
, listObjPtr
);
12202 static int Jim_LindexCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12204 Jim_Obj
*objPtr
, *listObjPtr
;
12209 Jim_WrongNumArgs(interp
, 1, argv
, "list ?index ...?");
12213 Jim_IncrRefCount(objPtr
);
12214 for (i
= 2; i
< argc
; i
++) {
12215 listObjPtr
= objPtr
;
12216 if (Jim_GetIndex(interp
, argv
[i
], &idx
) != JIM_OK
) {
12217 Jim_DecrRefCount(interp
, listObjPtr
);
12220 if (Jim_ListIndex(interp
, listObjPtr
, idx
, &objPtr
, JIM_NONE
) != JIM_OK
) {
12221 /* Returns an empty object if the index
12222 * is out of range. */
12223 Jim_DecrRefCount(interp
, listObjPtr
);
12224 Jim_SetEmptyResult(interp
);
12227 Jim_IncrRefCount(objPtr
);
12228 Jim_DecrRefCount(interp
, listObjPtr
);
12230 Jim_SetResult(interp
, objPtr
);
12231 Jim_DecrRefCount(interp
, objPtr
);
12236 static int Jim_LlengthCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12239 Jim_WrongNumArgs(interp
, 1, argv
, "list");
12242 Jim_SetResultInt(interp
, Jim_ListLength(interp
, argv
[1]));
12247 static int Jim_LsearchCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12249 static const char * const options
[] = {
12250 "-bool", "-not", "-nocase", "-exact", "-glob", "-regexp", "-all", "-inline", "-command",
12254 { OPT_BOOL
, OPT_NOT
, OPT_NOCASE
, OPT_EXACT
, OPT_GLOB
, OPT_REGEXP
, OPT_ALL
, OPT_INLINE
,
12259 int opt_nocase
= 0;
12261 int opt_inline
= 0;
12262 int opt_match
= OPT_EXACT
;
12265 Jim_Obj
*listObjPtr
= NULL
;
12266 Jim_Obj
*commandObj
= NULL
;
12270 Jim_WrongNumArgs(interp
, 1, argv
,
12271 "?-exact|-glob|-regexp|-command 'command'? ?-bool|-inline? ?-not? ?-nocase? ?-all? list value");
12275 for (i
= 1; i
< argc
- 2; i
++) {
12278 if (Jim_GetEnum(interp
, argv
[i
], options
, &option
, NULL
, JIM_ERRMSG
) != JIM_OK
) {
12300 if (i
>= argc
- 2) {
12303 commandObj
= argv
[++i
];
12308 opt_match
= option
;
12316 listObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
12318 if (opt_match
== OPT_REGEXP
) {
12319 commandObj
= Jim_NewStringObj(interp
, "regexp", -1);
12322 Jim_IncrRefCount(commandObj
);
12325 listlen
= Jim_ListLength(interp
, argv
[0]);
12326 for (i
= 0; i
< listlen
; i
++) {
12328 Jim_Obj
*objPtr
= Jim_ListGetIndex(interp
, argv
[0], i
);
12330 switch (opt_match
) {
12332 eq
= Jim_StringCompareObj(interp
, argv
[1], objPtr
, opt_nocase
) == 0;
12336 eq
= Jim_StringMatchObj(interp
, argv
[1], objPtr
, opt_nocase
);
12341 eq
= Jim_CommandMatchObj(interp
, commandObj
, argv
[1], objPtr
, opt_nocase
);
12344 Jim_FreeNewObj(interp
, listObjPtr
);
12352 /* If we have a non-match with opt_bool, opt_not, !opt_all, can't exit early */
12353 if (!eq
&& opt_bool
&& opt_not
&& !opt_all
) {
12357 if ((!opt_bool
&& eq
== !opt_not
) || (opt_bool
&& (eq
|| opt_all
))) {
12358 /* Got a match (or non-match for opt_not), or (opt_bool && opt_all) */
12359 Jim_Obj
*resultObj
;
12362 resultObj
= Jim_NewIntObj(interp
, eq
^ opt_not
);
12364 else if (!opt_inline
) {
12365 resultObj
= Jim_NewIntObj(interp
, i
);
12368 resultObj
= objPtr
;
12372 Jim_ListAppendElement(interp
, listObjPtr
, resultObj
);
12375 Jim_SetResult(interp
, resultObj
);
12382 Jim_SetResult(interp
, listObjPtr
);
12387 Jim_SetResultBool(interp
, opt_not
);
12389 else if (!opt_inline
) {
12390 Jim_SetResultInt(interp
, -1);
12396 Jim_DecrRefCount(interp
, commandObj
);
12402 static int Jim_LappendCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12404 Jim_Obj
*listObjPtr
;
12409 Jim_WrongNumArgs(interp
, 1, argv
, "varName ?value value ...?");
12412 listObjPtr
= Jim_GetVariable(interp
, argv
[1], JIM_UNSHARED
);
12414 /* Create the list if it does not exist */
12415 listObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
12418 else if (Jim_IsShared(listObjPtr
)) {
12419 listObjPtr
= Jim_DuplicateObj(interp
, listObjPtr
);
12422 for (i
= 2; i
< argc
; i
++)
12423 Jim_ListAppendElement(interp
, listObjPtr
, argv
[i
]);
12424 if (Jim_SetVariable(interp
, argv
[1], listObjPtr
) != JIM_OK
) {
12426 Jim_FreeNewObj(interp
, listObjPtr
);
12429 Jim_SetResult(interp
, listObjPtr
);
12434 static int Jim_LinsertCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12440 Jim_WrongNumArgs(interp
, 1, argv
, "list index ?element ...?");
12444 if (Jim_IsShared(listPtr
))
12445 listPtr
= Jim_DuplicateObj(interp
, listPtr
);
12446 if (Jim_GetIndex(interp
, argv
[2], &idx
) != JIM_OK
)
12448 len
= Jim_ListLength(interp
, listPtr
);
12452 idx
= len
+ idx
+ 1;
12453 Jim_ListInsertElements(interp
, listPtr
, idx
, argc
- 3, &argv
[3]);
12454 Jim_SetResult(interp
, listPtr
);
12457 if (listPtr
!= argv
[1]) {
12458 Jim_FreeNewObj(interp
, listPtr
);
12464 static int Jim_LreplaceCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12466 int first
, last
, len
, rangeLen
;
12468 Jim_Obj
*newListObj
;
12471 Jim_WrongNumArgs(interp
, 1, argv
, "list first last ?element ...?");
12474 if (Jim_GetIndex(interp
, argv
[2], &first
) != JIM_OK
||
12475 Jim_GetIndex(interp
, argv
[3], &last
) != JIM_OK
) {
12480 len
= Jim_ListLength(interp
, listObj
);
12482 first
= JimRelToAbsIndex(len
, first
);
12483 last
= JimRelToAbsIndex(len
, last
);
12484 JimRelToAbsRange(len
, &first
, &last
, &rangeLen
);
12486 /* Now construct a new list which consists of:
12487 * <elements before first> <supplied elements> <elements after last>
12490 /* Trying to replace past the end of the list means end of list
12497 /* Add the first set of elements */
12498 newListObj
= Jim_NewListObj(interp
, listObj
->internalRep
.listValue
.ele
, first
);
12500 /* Add supplied elements */
12501 ListInsertElements(newListObj
, -1, argc
- 4, argv
+ 4);
12503 /* Add the remaining elements */
12504 ListInsertElements(newListObj
, -1, len
- first
- rangeLen
, listObj
->internalRep
.listValue
.ele
+ first
+ rangeLen
);
12506 Jim_SetResult(interp
, newListObj
);
12511 static int Jim_LsetCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12514 Jim_WrongNumArgs(interp
, 1, argv
, "listVar ?index...? newVal");
12517 else if (argc
== 3) {
12518 /* With no indexes, simply implements [set] */
12519 if (Jim_SetVariable(interp
, argv
[1], argv
[2]) != JIM_OK
)
12521 Jim_SetResult(interp
, argv
[2]);
12524 return Jim_ListSetIndex(interp
, argv
[1], argv
+ 2, argc
- 3, argv
[argc
- 1]);
12528 static int Jim_LsortCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const argv
[])
12530 static const char * const options
[] = {
12531 "-ascii", "-nocase", "-increasing", "-decreasing", "-command", "-integer", "-real", "-index", "-unique", NULL
12534 { OPT_ASCII
, OPT_NOCASE
, OPT_INCREASING
, OPT_DECREASING
, OPT_COMMAND
, OPT_INTEGER
, OPT_REAL
, OPT_INDEX
, OPT_UNIQUE
};
12540 struct lsort_info info
;
12543 Jim_WrongNumArgs(interp
, 1, argv
, "?options? list");
12547 info
.type
= JIM_LSORT_ASCII
;
12551 info
.command
= NULL
;
12552 info
.interp
= interp
;
12554 for (i
= 1; i
< (argc
- 1); i
++) {
12557 if (Jim_GetEnum(interp
, argv
[i
], options
, &option
, NULL
, JIM_ENUM_ABBREV
| JIM_ERRMSG
)
12562 info
.type
= JIM_LSORT_ASCII
;
12565 info
.type
= JIM_LSORT_NOCASE
;
12568 info
.type
= JIM_LSORT_INTEGER
;
12571 info
.type
= JIM_LSORT_REAL
;
12573 case OPT_INCREASING
:
12576 case OPT_DECREASING
:
12583 if (i
>= (argc
- 2)) {
12584 Jim_SetResultString(interp
, "\"-command\" option must be followed by comparison command", -1);
12587 info
.type
= JIM_LSORT_COMMAND
;
12588 info
.command
= argv
[i
+ 1];
12592 if (i
>= (argc
- 2)) {
12593 Jim_SetResultString(interp
, "\"-index\" option must be followed by list index", -1);
12596 if (Jim_GetIndex(interp
, argv
[i
+ 1], &info
.index
) != JIM_OK
) {
12604 resObj
= argv
[argc
- 1];
12605 if ((shared
= Jim_IsShared(resObj
)))
12606 resObj
= Jim_DuplicateObj(interp
, resObj
);
12607 retCode
= ListSortElements(interp
, resObj
, &info
);
12608 if (retCode
== JIM_OK
) {
12609 Jim_SetResult(interp
, resObj
);
12612 Jim_FreeNewObj(interp
, resObj
);
12618 static int Jim_AppendCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12620 Jim_Obj
*stringObjPtr
;
12624 Jim_WrongNumArgs(interp
, 1, argv
, "varName ?value ...?");
12628 stringObjPtr
= Jim_GetVariable(interp
, argv
[1], JIM_ERRMSG
);
12634 stringObjPtr
= Jim_GetVariable(interp
, argv
[1], JIM_UNSHARED
);
12635 if (!stringObjPtr
) {
12636 /* Create the string if it doesn't exist */
12637 stringObjPtr
= Jim_NewEmptyStringObj(interp
);
12640 else if (Jim_IsShared(stringObjPtr
)) {
12642 stringObjPtr
= Jim_DuplicateObj(interp
, stringObjPtr
);
12644 for (i
= 2; i
< argc
; i
++) {
12645 Jim_AppendObj(interp
, stringObjPtr
, argv
[i
]);
12647 if (Jim_SetVariable(interp
, argv
[1], stringObjPtr
) != JIM_OK
) {
12649 Jim_FreeNewObj(interp
, stringObjPtr
);
12654 Jim_SetResult(interp
, stringObjPtr
);
12658 #if defined(JIM_DEBUG_COMMAND) && !defined(JIM_BOOTSTRAP)
12660 * Returns a zero-refcount list describing the expression at 'node'
12662 static Jim_Obj
*JimGetExprAsList(Jim_Interp
*interp
, struct JimExprNode
*node
)
12664 Jim_Obj
*listObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
12666 Jim_ListAppendElement(interp
, listObjPtr
, Jim_NewStringObj(interp
, jim_tt_name(node
->type
), -1));
12667 if (TOKEN_IS_EXPR_OP(node
->type
)) {
12669 Jim_ListAppendElement(interp
, listObjPtr
, JimGetExprAsList(interp
, node
->left
));
12672 Jim_ListAppendElement(interp
, listObjPtr
, JimGetExprAsList(interp
, node
->right
));
12674 if (node
->ternary
) {
12675 Jim_ListAppendElement(interp
, listObjPtr
, JimGetExprAsList(interp
, node
->ternary
));
12679 Jim_ListAppendElement(interp
, listObjPtr
, node
->objPtr
);
12683 #endif /* JIM_DEBUG_COMMAND && !JIM_BOOTSTRAP */
12686 static int Jim_DebugCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12688 #if defined(JIM_DEBUG_COMMAND) && !defined(JIM_BOOTSTRAP)
12689 static const char * const options
[] = {
12690 "refcount", "objcount", "objects", "invstr", "scriptlen", "exprlen",
12696 OPT_REFCOUNT
, OPT_OBJCOUNT
, OPT_OBJECTS
, OPT_INVSTR
, OPT_SCRIPTLEN
,
12697 OPT_EXPRLEN
, OPT_EXPRBC
, OPT_SHOW
,
12702 Jim_WrongNumArgs(interp
, 1, argv
, "subcommand ?...?");
12705 if (Jim_GetEnum(interp
, argv
[1], options
, &option
, "subcommand", JIM_ERRMSG
) != JIM_OK
)
12706 return Jim_CheckShowCommands(interp
, argv
[1], options
);
12707 if (option
== OPT_REFCOUNT
) {
12709 Jim_WrongNumArgs(interp
, 2, argv
, "object");
12712 Jim_SetResultInt(interp
, argv
[2]->refCount
);
12715 else if (option
== OPT_OBJCOUNT
) {
12716 int freeobj
= 0, liveobj
= 0;
12721 Jim_WrongNumArgs(interp
, 2, argv
, "");
12724 /* Count the number of free objects. */
12725 objPtr
= interp
->freeList
;
12728 objPtr
= objPtr
->nextObjPtr
;
12730 /* Count the number of live objects. */
12731 objPtr
= interp
->liveList
;
12734 objPtr
= objPtr
->nextObjPtr
;
12736 /* Set the result string and return. */
12737 sprintf(buf
, "free %d used %d", freeobj
, liveobj
);
12738 Jim_SetResultString(interp
, buf
, -1);
12741 else if (option
== OPT_OBJECTS
) {
12742 Jim_Obj
*objPtr
, *listObjPtr
, *subListObjPtr
;
12744 /* Count the number of live objects. */
12745 objPtr
= interp
->liveList
;
12746 listObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
12749 const char *type
= objPtr
->typePtr
? objPtr
->typePtr
->name
: "";
12751 subListObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
12752 sprintf(buf
, "%p", objPtr
);
12753 Jim_ListAppendElement(interp
, subListObjPtr
, Jim_NewStringObj(interp
, buf
, -1));
12754 Jim_ListAppendElement(interp
, subListObjPtr
, Jim_NewStringObj(interp
, type
, -1));
12755 Jim_ListAppendElement(interp
, subListObjPtr
, Jim_NewIntObj(interp
, objPtr
->refCount
));
12756 Jim_ListAppendElement(interp
, subListObjPtr
, objPtr
);
12757 Jim_ListAppendElement(interp
, listObjPtr
, subListObjPtr
);
12758 objPtr
= objPtr
->nextObjPtr
;
12760 Jim_SetResult(interp
, listObjPtr
);
12763 else if (option
== OPT_INVSTR
) {
12767 Jim_WrongNumArgs(interp
, 2, argv
, "object");
12771 if (objPtr
->typePtr
!= NULL
)
12772 Jim_InvalidateStringRep(objPtr
);
12773 Jim_SetEmptyResult(interp
);
12776 else if (option
== OPT_SHOW
) {
12781 Jim_WrongNumArgs(interp
, 2, argv
, "object");
12784 s
= Jim_GetString(argv
[2], &len
);
12786 charlen
= utf8_strlen(s
, len
);
12790 printf("refcount: %d, type: %s\n", argv
[2]->refCount
, JimObjTypeName(argv
[2]));
12791 printf("chars (%d): <<%s>>\n", charlen
, s
);
12792 printf("bytes (%d):", len
);
12794 printf(" %02x", (unsigned char)*s
++);
12799 else if (option
== OPT_SCRIPTLEN
) {
12803 Jim_WrongNumArgs(interp
, 2, argv
, "script");
12806 script
= JimGetScript(interp
, argv
[2]);
12807 if (script
== NULL
)
12809 Jim_SetResultInt(interp
, script
->len
);
12812 else if (option
== OPT_EXPRLEN
) {
12813 struct ExprTree
*expr
;
12816 Jim_WrongNumArgs(interp
, 2, argv
, "expression");
12819 expr
= JimGetExpression(interp
, argv
[2]);
12822 Jim_SetResultInt(interp
, expr
->len
);
12825 else if (option
== OPT_EXPRBC
) {
12826 struct ExprTree
*expr
;
12829 Jim_WrongNumArgs(interp
, 2, argv
, "expression");
12832 expr
= JimGetExpression(interp
, argv
[2]);
12835 Jim_SetResult(interp
, JimGetExprAsList(interp
, expr
->expr
));
12839 Jim_SetResultString(interp
,
12840 "bad option. Valid options are refcount, " "objcount, objects, invstr", -1);
12844 #endif /* JIM_DEBUG_COMMAND && !JIM_BOOTSTRAP */
12845 #if !defined(JIM_DEBUG_COMMAND)
12846 Jim_SetResultString(interp
, "unsupported", -1);
12852 static int Jim_EvalCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12857 Jim_WrongNumArgs(interp
, 1, argv
, "arg ?arg ...?");
12862 rc
= Jim_EvalObj(interp
, argv
[1]);
12865 rc
= Jim_EvalObj(interp
, Jim_ConcatObj(interp
, argc
- 1, argv
+ 1));
12868 if (rc
== JIM_ERR
) {
12869 /* eval is "interesting", so add a stack frame here */
12870 interp
->addStackTrace
++;
12876 static int Jim_UplevelCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12880 Jim_CallFrame
*savedCallFrame
, *targetCallFrame
;
12883 /* Save the old callframe pointer */
12884 savedCallFrame
= interp
->framePtr
;
12886 /* Lookup the target frame pointer */
12887 str
= Jim_String(argv
[1]);
12888 if ((str
[0] >= '0' && str
[0] <= '9') || str
[0] == '#') {
12889 targetCallFrame
= Jim_GetCallFrameByLevel(interp
, argv
[1]);
12894 targetCallFrame
= Jim_GetCallFrameByLevel(interp
, NULL
);
12896 if (targetCallFrame
== NULL
) {
12900 Jim_WrongNumArgs(interp
, 1, argv
- 1, "?level? command ?arg ...?");
12903 /* Eval the code in the target callframe. */
12904 interp
->framePtr
= targetCallFrame
;
12906 retcode
= Jim_EvalObj(interp
, argv
[1]);
12909 retcode
= Jim_EvalObj(interp
, Jim_ConcatObj(interp
, argc
- 1, argv
+ 1));
12911 interp
->framePtr
= savedCallFrame
;
12915 Jim_WrongNumArgs(interp
, 1, argv
, "?level? command ?arg ...?");
12921 static int Jim_ExprCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12926 retcode
= Jim_EvalExpression(interp
, argv
[1]);
12928 else if (argc
> 2) {
12931 objPtr
= Jim_ConcatObj(interp
, argc
- 1, argv
+ 1);
12932 Jim_IncrRefCount(objPtr
);
12933 retcode
= Jim_EvalExpression(interp
, objPtr
);
12934 Jim_DecrRefCount(interp
, objPtr
);
12937 Jim_WrongNumArgs(interp
, 1, argv
, "expression ?...?");
12940 if (retcode
!= JIM_OK
)
12946 static int Jim_BreakCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12949 Jim_WrongNumArgs(interp
, 1, argv
, "");
12956 static int Jim_ContinueCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12959 Jim_WrongNumArgs(interp
, 1, argv
, "");
12962 return JIM_CONTINUE
;
12966 static int Jim_ReturnCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12969 Jim_Obj
*stackTraceObj
= NULL
;
12970 Jim_Obj
*errorCodeObj
= NULL
;
12971 int returnCode
= JIM_OK
;
12974 for (i
= 1; i
< argc
- 1; i
+= 2) {
12975 if (Jim_CompareStringImmediate(interp
, argv
[i
], "-code")) {
12976 if (Jim_GetReturnCode(interp
, argv
[i
+ 1], &returnCode
) == JIM_ERR
) {
12980 else if (Jim_CompareStringImmediate(interp
, argv
[i
], "-errorinfo")) {
12981 stackTraceObj
= argv
[i
+ 1];
12983 else if (Jim_CompareStringImmediate(interp
, argv
[i
], "-errorcode")) {
12984 errorCodeObj
= argv
[i
+ 1];
12986 else if (Jim_CompareStringImmediate(interp
, argv
[i
], "-level")) {
12987 if (Jim_GetLong(interp
, argv
[i
+ 1], &level
) != JIM_OK
|| level
< 0) {
12988 Jim_SetResultFormatted(interp
, "bad level \"%#s\"", argv
[i
+ 1]);
12997 if (i
!= argc
- 1 && i
!= argc
) {
12998 Jim_WrongNumArgs(interp
, 1, argv
,
12999 "?-code code? ?-errorinfo stacktrace? ?-level level? ?result?");
13002 /* If a stack trace is supplied and code is error, set the stack trace */
13003 if (stackTraceObj
&& returnCode
== JIM_ERR
) {
13004 JimSetStackTrace(interp
, stackTraceObj
);
13006 /* If an error code list is supplied, set the global $errorCode */
13007 if (errorCodeObj
&& returnCode
== JIM_ERR
) {
13008 Jim_SetGlobalVariableStr(interp
, "errorCode", errorCodeObj
);
13010 interp
->returnCode
= returnCode
;
13011 interp
->returnLevel
= level
;
13013 if (i
== argc
- 1) {
13014 Jim_SetResult(interp
, argv
[i
]);
13020 static int Jim_TailcallCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13022 if (interp
->framePtr
->level
== 0) {
13023 Jim_SetResultString(interp
, "tailcall can only be called from a proc or lambda", -1);
13026 else if (argc
>= 2) {
13027 /* Need to resolve the tailcall command in the current context */
13028 Jim_CallFrame
*cf
= interp
->framePtr
->parent
;
13030 Jim_Cmd
*cmdPtr
= Jim_GetCommand(interp
, argv
[1], JIM_ERRMSG
);
13031 if (cmdPtr
== NULL
) {
13035 JimPanic((cf
->tailcallCmd
!= NULL
, "Already have a tailcallCmd"));
13037 /* And stash this pre-resolved command */
13038 JimIncrCmdRefCount(cmdPtr
);
13039 cf
->tailcallCmd
= cmdPtr
;
13041 /* And stash the command list */
13042 JimPanic((cf
->tailcallObj
!= NULL
, "Already have a tailcallobj"));
13044 cf
->tailcallObj
= Jim_NewListObj(interp
, argv
+ 1, argc
- 1);
13045 Jim_IncrRefCount(cf
->tailcallObj
);
13047 /* When the stack unwinds to the previous proc, the stashed command will be evaluated */
13053 static int JimAliasCmd(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13056 Jim_Obj
*prefixListObj
= Jim_CmdPrivData(interp
);
13058 /* prefixListObj is a list to which the args need to be appended */
13059 cmdList
= Jim_DuplicateObj(interp
, prefixListObj
);
13060 Jim_ListInsertElements(interp
, cmdList
, Jim_ListLength(interp
, cmdList
), argc
- 1, argv
+ 1);
13062 return JimEvalObjList(interp
, cmdList
);
13065 static void JimAliasCmdDelete(Jim_Interp
*interp
, void *privData
)
13067 Jim_Obj
*prefixListObj
= privData
;
13068 Jim_DecrRefCount(interp
, prefixListObj
);
13071 static int Jim_AliasCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13073 Jim_Obj
*prefixListObj
;
13074 const char *newname
;
13077 Jim_WrongNumArgs(interp
, 1, argv
, "newname command ?args ...?");
13081 prefixListObj
= Jim_NewListObj(interp
, argv
+ 2, argc
- 2);
13082 Jim_IncrRefCount(prefixListObj
);
13083 newname
= Jim_String(argv
[1]);
13084 if (newname
[0] == ':' && newname
[1] == ':') {
13085 while (*++newname
== ':') {
13089 Jim_SetResult(interp
, argv
[1]);
13091 return Jim_CreateCommand(interp
, newname
, JimAliasCmd
, prefixListObj
, JimAliasCmdDelete
);
13095 static int Jim_ProcCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13099 if (argc
!= 4 && argc
!= 5) {
13100 Jim_WrongNumArgs(interp
, 1, argv
, "name arglist ?statics? body");
13104 if (JimValidName(interp
, "procedure", argv
[1]) != JIM_OK
) {
13109 cmd
= JimCreateProcedureCmd(interp
, argv
[2], NULL
, argv
[3], NULL
);
13112 cmd
= JimCreateProcedureCmd(interp
, argv
[2], argv
[3], argv
[4], NULL
);
13116 /* Add the new command */
13117 Jim_Obj
*qualifiedCmdNameObj
;
13118 const char *cmdname
= JimQualifyName(interp
, Jim_String(argv
[1]), &qualifiedCmdNameObj
);
13120 JimCreateCommand(interp
, cmdname
, cmd
);
13122 /* Calculate and set the namespace for this proc */
13123 JimUpdateProcNamespace(interp
, cmd
, cmdname
);
13125 JimFreeQualifiedName(interp
, qualifiedCmdNameObj
);
13127 /* Unlike Tcl, set the name of the proc as the result */
13128 Jim_SetResult(interp
, argv
[1]);
13135 static int Jim_LocalCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13140 Jim_WrongNumArgs(interp
, 1, argv
, "cmd ?args ...?");
13144 /* Evaluate the arguments with 'local' in force */
13146 retcode
= Jim_EvalObjVector(interp
, argc
- 1, argv
+ 1);
13150 /* If OK, and the result is a proc, add it to the list of local procs */
13151 if (retcode
== 0) {
13152 Jim_Obj
*cmdNameObj
= Jim_GetResult(interp
);
13154 if (Jim_GetCommand(interp
, cmdNameObj
, JIM_ERRMSG
) == NULL
) {
13157 if (interp
->framePtr
->localCommands
== NULL
) {
13158 interp
->framePtr
->localCommands
= Jim_Alloc(sizeof(*interp
->framePtr
->localCommands
));
13159 Jim_InitStack(interp
->framePtr
->localCommands
);
13161 Jim_IncrRefCount(cmdNameObj
);
13162 Jim_StackPush(interp
->framePtr
->localCommands
, cmdNameObj
);
13169 static int Jim_UpcallCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13172 Jim_WrongNumArgs(interp
, 1, argv
, "cmd ?args ...?");
13178 Jim_Cmd
*cmdPtr
= Jim_GetCommand(interp
, argv
[1], JIM_ERRMSG
);
13179 if (cmdPtr
== NULL
|| !cmdPtr
->isproc
|| !cmdPtr
->prevCmd
) {
13180 Jim_SetResultFormatted(interp
, "no previous command: \"%#s\"", argv
[1]);
13183 /* OK. Mark this command as being in an upcall */
13184 cmdPtr
->u
.proc
.upcall
++;
13185 JimIncrCmdRefCount(cmdPtr
);
13187 /* Invoke the command as normal */
13188 retcode
= Jim_EvalObjVector(interp
, argc
- 1, argv
+ 1);
13190 /* No longer in an upcall */
13191 cmdPtr
->u
.proc
.upcall
--;
13192 JimDecrCmdRefCount(interp
, cmdPtr
);
13199 static int Jim_ApplyCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13202 Jim_WrongNumArgs(interp
, 1, argv
, "lambdaExpr ?arg ...?");
13208 Jim_Obj
*argListObjPtr
;
13209 Jim_Obj
*bodyObjPtr
;
13210 Jim_Obj
*nsObj
= NULL
;
13213 int len
= Jim_ListLength(interp
, argv
[1]);
13214 if (len
!= 2 && len
!= 3) {
13215 Jim_SetResultFormatted(interp
, "can't interpret \"%#s\" as a lambda expression", argv
[1]);
13220 #ifdef jim_ext_namespace
13221 /* Need to canonicalise the given namespace. */
13222 nsObj
= JimQualifyNameObj(interp
, Jim_ListGetIndex(interp
, argv
[1], 2));
13224 Jim_SetResultString(interp
, "namespaces not enabled", -1);
13228 argListObjPtr
= Jim_ListGetIndex(interp
, argv
[1], 0);
13229 bodyObjPtr
= Jim_ListGetIndex(interp
, argv
[1], 1);
13231 cmd
= JimCreateProcedureCmd(interp
, argListObjPtr
, NULL
, bodyObjPtr
, nsObj
);
13234 /* Create a new argv array with a dummy argv[0], for error messages */
13235 nargv
= Jim_Alloc((argc
- 2 + 1) * sizeof(*nargv
));
13236 nargv
[0] = Jim_NewStringObj(interp
, "apply lambdaExpr", -1);
13237 Jim_IncrRefCount(nargv
[0]);
13238 memcpy(&nargv
[1], argv
+ 2, (argc
- 2) * sizeof(*nargv
));
13239 ret
= JimCallProcedure(interp
, cmd
, argc
- 2 + 1, nargv
);
13240 Jim_DecrRefCount(interp
, nargv
[0]);
13243 JimDecrCmdRefCount(interp
, cmd
);
13252 static int Jim_ConcatCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13254 Jim_SetResult(interp
, Jim_ConcatObj(interp
, argc
- 1, argv
+ 1));
13259 static int Jim_UpvarCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13262 Jim_CallFrame
*targetCallFrame
;
13264 /* Lookup the target frame pointer */
13265 if (argc
> 3 && (argc
% 2 == 0)) {
13266 targetCallFrame
= Jim_GetCallFrameByLevel(interp
, argv
[1]);
13271 targetCallFrame
= Jim_GetCallFrameByLevel(interp
, NULL
);
13273 if (targetCallFrame
== NULL
) {
13277 /* Check for arity */
13279 Jim_WrongNumArgs(interp
, 1, argv
, "?level? otherVar localVar ?otherVar localVar ...?");
13283 /* Now... for every other/local couple: */
13284 for (i
= 1; i
< argc
; i
+= 2) {
13285 if (Jim_SetVariableLink(interp
, argv
[i
+ 1], argv
[i
], targetCallFrame
) != JIM_OK
)
13292 static int Jim_GlobalCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13297 Jim_WrongNumArgs(interp
, 1, argv
, "varName ?varName ...?");
13300 /* Link every var to the toplevel having the same name */
13301 if (interp
->framePtr
->level
== 0)
13302 return JIM_OK
; /* global at toplevel... */
13303 for (i
= 1; i
< argc
; i
++) {
13304 /* global ::blah does nothing */
13305 const char *name
= Jim_String(argv
[i
]);
13306 if (name
[0] != ':' || name
[1] != ':') {
13307 if (Jim_SetVariableLink(interp
, argv
[i
], argv
[i
], interp
->topFramePtr
) != JIM_OK
)
13314 /* does the [string map] operation. On error NULL is returned,
13315 * otherwise a new string object with the result, having refcount = 0,
13317 static Jim_Obj
*JimStringMap(Jim_Interp
*interp
, Jim_Obj
*mapListObjPtr
,
13318 Jim_Obj
*objPtr
, int nocase
)
13321 const char *str
, *noMatchStart
= NULL
;
13323 Jim_Obj
*resultObjPtr
;
13325 numMaps
= Jim_ListLength(interp
, mapListObjPtr
);
13327 Jim_SetResultString(interp
, "list must contain an even number of elements", -1);
13331 str
= Jim_String(objPtr
);
13332 strLen
= Jim_Utf8Length(interp
, objPtr
);
13335 resultObjPtr
= Jim_NewStringObj(interp
, "", 0);
13337 for (i
= 0; i
< numMaps
; i
+= 2) {
13338 Jim_Obj
*eachObjPtr
;
13342 eachObjPtr
= Jim_ListGetIndex(interp
, mapListObjPtr
, i
);
13343 k
= Jim_String(eachObjPtr
);
13344 kl
= Jim_Utf8Length(interp
, eachObjPtr
);
13346 if (strLen
>= kl
&& kl
) {
13348 rc
= JimStringCompareLen(str
, k
, kl
, nocase
);
13350 if (noMatchStart
) {
13351 Jim_AppendString(interp
, resultObjPtr
, noMatchStart
, str
- noMatchStart
);
13352 noMatchStart
= NULL
;
13354 Jim_AppendObj(interp
, resultObjPtr
, Jim_ListGetIndex(interp
, mapListObjPtr
, i
+ 1));
13355 str
+= utf8_index(str
, kl
);
13361 if (i
== numMaps
) { /* no match */
13363 if (noMatchStart
== NULL
)
13364 noMatchStart
= str
;
13365 str
+= utf8_tounicode(str
, &c
);
13369 if (noMatchStart
) {
13370 Jim_AppendString(interp
, resultObjPtr
, noMatchStart
, str
- noMatchStart
);
13372 return resultObjPtr
;
13376 static int Jim_StringCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13381 static const char * const options
[] = {
13382 "bytelength", "length", "compare", "match", "equal", "is", "byterange", "range", "replace",
13383 "map", "repeat", "reverse", "index", "first", "last", "cat",
13384 "trim", "trimleft", "trimright", "tolower", "toupper", "totitle", NULL
13388 OPT_BYTELENGTH
, OPT_LENGTH
, OPT_COMPARE
, OPT_MATCH
, OPT_EQUAL
, OPT_IS
, OPT_BYTERANGE
, OPT_RANGE
, OPT_REPLACE
,
13389 OPT_MAP
, OPT_REPEAT
, OPT_REVERSE
, OPT_INDEX
, OPT_FIRST
, OPT_LAST
, OPT_CAT
,
13390 OPT_TRIM
, OPT_TRIMLEFT
, OPT_TRIMRIGHT
, OPT_TOLOWER
, OPT_TOUPPER
, OPT_TOTITLE
13392 static const char * const nocase_options
[] = {
13395 static const char * const nocase_length_options
[] = {
13396 "-nocase", "-length", NULL
13400 Jim_WrongNumArgs(interp
, 1, argv
, "option ?arguments ...?");
13403 if (Jim_GetEnum(interp
, argv
[1], options
, &option
, NULL
,
13404 JIM_ERRMSG
| JIM_ENUM_ABBREV
) != JIM_OK
)
13405 return Jim_CheckShowCommands(interp
, argv
[1], options
);
13409 case OPT_BYTELENGTH
:
13411 Jim_WrongNumArgs(interp
, 2, argv
, "string");
13414 if (option
== OPT_LENGTH
) {
13415 len
= Jim_Utf8Length(interp
, argv
[2]);
13418 len
= Jim_Length(argv
[2]);
13420 Jim_SetResultInt(interp
, len
);
13426 /* optimise the one-arg case */
13432 objPtr
= Jim_NewStringObj(interp
, "", 0);
13434 for (i
= 2; i
< argc
; i
++) {
13435 Jim_AppendObj(interp
, objPtr
, argv
[i
]);
13438 Jim_SetResult(interp
, objPtr
);
13445 /* n is the number of remaining option args */
13446 long opt_length
= -1;
13451 if (Jim_GetEnum(interp
, argv
[i
++], nocase_length_options
, &subopt
, NULL
,
13452 JIM_ENUM_ABBREV
) != JIM_OK
) {
13454 Jim_WrongNumArgs(interp
, 2, argv
, "?-nocase? ?-length int? string1 string2");
13465 goto badcompareargs
;
13467 if (Jim_GetLong(interp
, argv
[i
++], &opt_length
) != JIM_OK
) {
13474 goto badcompareargs
;
13477 if (opt_length
< 0 && option
!= OPT_COMPARE
&& opt_case
) {
13478 /* Fast version - [string equal], case sensitive, no length */
13479 Jim_SetResultBool(interp
, Jim_StringEqObj(argv
[0], argv
[1]));
13482 if (opt_length
>= 0) {
13483 n
= JimStringCompareLen(Jim_String(argv
[0]), Jim_String(argv
[1]), opt_length
, !opt_case
);
13486 n
= Jim_StringCompareObj(interp
, argv
[0], argv
[1], !opt_case
);
13488 Jim_SetResultInt(interp
, option
== OPT_COMPARE
? n
: n
== 0);
13496 Jim_GetEnum(interp
, argv
[2], nocase_options
, &opt_case
, NULL
,
13497 JIM_ENUM_ABBREV
) != JIM_OK
)) {
13498 Jim_WrongNumArgs(interp
, 2, argv
, "?-nocase? pattern string");
13501 if (opt_case
== 0) {
13504 Jim_SetResultBool(interp
, Jim_StringMatchObj(interp
, argv
[2], argv
[3], !opt_case
));
13512 Jim_GetEnum(interp
, argv
[2], nocase_options
, &opt_case
, NULL
,
13513 JIM_ENUM_ABBREV
) != JIM_OK
)) {
13514 Jim_WrongNumArgs(interp
, 2, argv
, "?-nocase? mapList string");
13518 if (opt_case
== 0) {
13521 objPtr
= JimStringMap(interp
, argv
[2], argv
[3], !opt_case
);
13522 if (objPtr
== NULL
) {
13525 Jim_SetResult(interp
, objPtr
);
13530 case OPT_BYTERANGE
:{
13534 Jim_WrongNumArgs(interp
, 2, argv
, "string first last");
13537 if (option
== OPT_RANGE
) {
13538 objPtr
= Jim_StringRangeObj(interp
, argv
[2], argv
[3], argv
[4]);
13542 objPtr
= Jim_StringByteRangeObj(interp
, argv
[2], argv
[3], argv
[4]);
13545 if (objPtr
== NULL
) {
13548 Jim_SetResult(interp
, objPtr
);
13555 if (argc
!= 5 && argc
!= 6) {
13556 Jim_WrongNumArgs(interp
, 2, argv
, "string first last ?string?");
13559 objPtr
= JimStringReplaceObj(interp
, argv
[2], argv
[3], argv
[4], argc
== 6 ? argv
[5] : NULL
);
13560 if (objPtr
== NULL
) {
13563 Jim_SetResult(interp
, objPtr
);
13573 Jim_WrongNumArgs(interp
, 2, argv
, "string count");
13576 if (Jim_GetWide(interp
, argv
[3], &count
) != JIM_OK
) {
13579 objPtr
= Jim_NewStringObj(interp
, "", 0);
13582 Jim_AppendObj(interp
, objPtr
, argv
[2]);
13585 Jim_SetResult(interp
, objPtr
);
13595 Jim_WrongNumArgs(interp
, 2, argv
, "string");
13599 str
= Jim_GetString(argv
[2], &len
);
13600 buf
= Jim_Alloc(len
+ 1);
13603 for (i
= 0; i
< len
; ) {
13605 int l
= utf8_tounicode(str
, &c
);
13606 memcpy(p
- l
, str
, l
);
13611 Jim_SetResult(interp
, Jim_NewStringObjNoAlloc(interp
, buf
, len
));
13620 Jim_WrongNumArgs(interp
, 2, argv
, "string index");
13623 if (Jim_GetIndex(interp
, argv
[3], &idx
) != JIM_OK
) {
13626 str
= Jim_String(argv
[2]);
13627 len
= Jim_Utf8Length(interp
, argv
[2]);
13628 if (idx
!= INT_MIN
&& idx
!= INT_MAX
) {
13629 idx
= JimRelToAbsIndex(len
, idx
);
13631 if (idx
< 0 || idx
>= len
|| str
== NULL
) {
13632 Jim_SetResultString(interp
, "", 0);
13634 else if (len
== Jim_Length(argv
[2])) {
13635 /* ASCII optimisation */
13636 Jim_SetResultString(interp
, str
+ idx
, 1);
13640 int i
= utf8_index(str
, idx
);
13641 Jim_SetResultString(interp
, str
+ i
, utf8_tounicode(str
+ i
, &c
));
13648 int idx
= 0, l1
, l2
;
13649 const char *s1
, *s2
;
13651 if (argc
!= 4 && argc
!= 5) {
13652 Jim_WrongNumArgs(interp
, 2, argv
, "subString string ?index?");
13655 s1
= Jim_String(argv
[2]);
13656 s2
= Jim_String(argv
[3]);
13657 l1
= Jim_Utf8Length(interp
, argv
[2]);
13658 l2
= Jim_Utf8Length(interp
, argv
[3]);
13660 if (Jim_GetIndex(interp
, argv
[4], &idx
) != JIM_OK
) {
13663 idx
= JimRelToAbsIndex(l2
, idx
);
13665 else if (option
== OPT_LAST
) {
13668 if (option
== OPT_FIRST
) {
13669 Jim_SetResultInt(interp
, JimStringFirst(s1
, l1
, s2
, l2
, idx
));
13673 Jim_SetResultInt(interp
, JimStringLastUtf8(s1
, l1
, s2
, idx
));
13675 Jim_SetResultInt(interp
, JimStringLast(s1
, l1
, s2
, idx
));
13683 case OPT_TRIMRIGHT
:{
13684 Jim_Obj
*trimchars
;
13686 if (argc
!= 3 && argc
!= 4) {
13687 Jim_WrongNumArgs(interp
, 2, argv
, "string ?trimchars?");
13690 trimchars
= (argc
== 4 ? argv
[3] : NULL
);
13691 if (option
== OPT_TRIM
) {
13692 Jim_SetResult(interp
, JimStringTrim(interp
, argv
[2], trimchars
));
13694 else if (option
== OPT_TRIMLEFT
) {
13695 Jim_SetResult(interp
, JimStringTrimLeft(interp
, argv
[2], trimchars
));
13697 else if (option
== OPT_TRIMRIGHT
) {
13698 Jim_SetResult(interp
, JimStringTrimRight(interp
, argv
[2], trimchars
));
13707 Jim_WrongNumArgs(interp
, 2, argv
, "string");
13710 if (option
== OPT_TOLOWER
) {
13711 Jim_SetResult(interp
, JimStringToLower(interp
, argv
[2]));
13713 else if (option
== OPT_TOUPPER
) {
13714 Jim_SetResult(interp
, JimStringToUpper(interp
, argv
[2]));
13717 Jim_SetResult(interp
, JimStringToTitle(interp
, argv
[2]));
13722 if (argc
== 4 || (argc
== 5 && Jim_CompareStringImmediate(interp
, argv
[3], "-strict"))) {
13723 return JimStringIs(interp
, argv
[argc
- 1], argv
[2], argc
== 5);
13725 Jim_WrongNumArgs(interp
, 2, argv
, "class ?-strict? str");
13732 static int Jim_TimeCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13735 jim_wide start
, elapsed
;
13737 const char *fmt
= "%" JIM_WIDE_MODIFIER
" microseconds per iteration";
13740 Jim_WrongNumArgs(interp
, 1, argv
, "script ?count?");
13744 if (Jim_GetLong(interp
, argv
[2], &count
) != JIM_OK
)
13750 start
= JimClock();
13754 retval
= Jim_EvalObj(interp
, argv
[1]);
13755 if (retval
!= JIM_OK
) {
13759 elapsed
= JimClock() - start
;
13760 sprintf(buf
, fmt
, count
== 0 ? 0 : elapsed
/ count
);
13761 Jim_SetResultString(interp
, buf
, -1);
13766 static int Jim_ExitCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13771 Jim_WrongNumArgs(interp
, 1, argv
, "?exitCode?");
13775 if (Jim_GetLong(interp
, argv
[1], &exitCode
) != JIM_OK
)
13778 interp
->exitCode
= exitCode
;
13783 static int Jim_CatchCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13789 /* Which return codes are ignored (passed through)? By default, only exit, eval and signal */
13790 jim_wide ignore_mask
= (1 << JIM_EXIT
) | (1 << JIM_EVAL
) | (1 << JIM_SIGNAL
);
13791 static const int max_ignore_code
= sizeof(ignore_mask
) * 8;
13793 /* Reset the error code before catch.
13794 * Note that this is not strictly correct.
13796 Jim_SetGlobalVariableStr(interp
, "errorCode", Jim_NewStringObj(interp
, "NONE", -1));
13798 for (i
= 1; i
< argc
- 1; i
++) {
13799 const char *arg
= Jim_String(argv
[i
]);
13803 /* It's a pity we can't use Jim_GetEnum here :-( */
13804 if (strcmp(arg
, "--") == 0) {
13812 if (strncmp(arg
, "-no", 3) == 0) {
13821 if (Jim_StringToWide(arg
, &option
, 10) != JIM_OK
) {
13825 option
= Jim_FindByName(arg
, jimReturnCodes
, jimReturnCodesSize
);
13832 ignore_mask
|= ((jim_wide
)1 << option
);
13835 ignore_mask
&= (~((jim_wide
)1 << option
));
13840 if (argc
< 1 || argc
> 3) {
13842 Jim_WrongNumArgs(interp
, 1, argv
,
13843 "?-?no?code ... --? script ?resultVarName? ?optionVarName?");
13848 if ((ignore_mask
& (1 << JIM_SIGNAL
)) == 0) {
13852 interp
->signal_level
+= sig
;
13853 if (Jim_CheckSignal(interp
)) {
13854 /* If a signal is set, don't even try to execute the body */
13855 exitCode
= JIM_SIGNAL
;
13858 exitCode
= Jim_EvalObj(interp
, argv
[0]);
13859 /* Don't want any caught error included in a later stack trace */
13860 interp
->errorFlag
= 0;
13862 interp
->signal_level
-= sig
;
13864 /* Catch or pass through? Only the first 32/64 codes can be passed through */
13865 if (exitCode
>= 0 && exitCode
< max_ignore_code
&& (((unsigned jim_wide
)1 << exitCode
) & ignore_mask
)) {
13866 /* Not caught, pass it up */
13870 if (sig
&& exitCode
== JIM_SIGNAL
) {
13871 /* Catch the signal at this level */
13872 if (interp
->signal_set_result
) {
13873 interp
->signal_set_result(interp
, interp
->sigmask
);
13876 Jim_SetResultInt(interp
, interp
->sigmask
);
13878 interp
->sigmask
= 0;
13882 if (Jim_SetVariable(interp
, argv
[1], Jim_GetResult(interp
)) != JIM_OK
) {
13886 Jim_Obj
*optListObj
= Jim_NewListObj(interp
, NULL
, 0);
13888 Jim_ListAppendElement(interp
, optListObj
, Jim_NewStringObj(interp
, "-code", -1));
13889 Jim_ListAppendElement(interp
, optListObj
,
13890 Jim_NewIntObj(interp
, exitCode
== JIM_RETURN
? interp
->returnCode
: exitCode
));
13891 Jim_ListAppendElement(interp
, optListObj
, Jim_NewStringObj(interp
, "-level", -1));
13892 Jim_ListAppendElement(interp
, optListObj
, Jim_NewIntObj(interp
, interp
->returnLevel
));
13893 if (exitCode
== JIM_ERR
) {
13894 Jim_Obj
*errorCode
;
13895 Jim_ListAppendElement(interp
, optListObj
, Jim_NewStringObj(interp
, "-errorinfo",
13897 Jim_ListAppendElement(interp
, optListObj
, interp
->stackTrace
);
13899 errorCode
= Jim_GetGlobalVariableStr(interp
, "errorCode", JIM_NONE
);
13901 Jim_ListAppendElement(interp
, optListObj
, Jim_NewStringObj(interp
, "-errorcode", -1));
13902 Jim_ListAppendElement(interp
, optListObj
, errorCode
);
13905 if (Jim_SetVariable(interp
, argv
[2], optListObj
) != JIM_OK
) {
13910 Jim_SetResultInt(interp
, exitCode
);
13914 #if defined(JIM_REFERENCES) && !defined(JIM_BOOTSTRAP)
13917 static int Jim_RefCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13919 if (argc
!= 3 && argc
!= 4) {
13920 Jim_WrongNumArgs(interp
, 1, argv
, "string tag ?finalizer?");
13924 Jim_SetResult(interp
, Jim_NewReference(interp
, argv
[1], argv
[2], NULL
));
13927 Jim_SetResult(interp
, Jim_NewReference(interp
, argv
[1], argv
[2], argv
[3]));
13933 static int Jim_GetrefCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13935 Jim_Reference
*refPtr
;
13938 Jim_WrongNumArgs(interp
, 1, argv
, "reference");
13941 if ((refPtr
= Jim_GetReference(interp
, argv
[1])) == NULL
)
13943 Jim_SetResult(interp
, refPtr
->objPtr
);
13948 static int Jim_SetrefCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13950 Jim_Reference
*refPtr
;
13953 Jim_WrongNumArgs(interp
, 1, argv
, "reference newValue");
13956 if ((refPtr
= Jim_GetReference(interp
, argv
[1])) == NULL
)
13958 Jim_IncrRefCount(argv
[2]);
13959 Jim_DecrRefCount(interp
, refPtr
->objPtr
);
13960 refPtr
->objPtr
= argv
[2];
13961 Jim_SetResult(interp
, argv
[2]);
13966 static int Jim_CollectCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13969 Jim_WrongNumArgs(interp
, 1, argv
, "");
13972 Jim_SetResultInt(interp
, Jim_Collect(interp
));
13974 /* Free all the freed objects. */
13975 while (interp
->freeList
) {
13976 Jim_Obj
*nextObjPtr
= interp
->freeList
->nextObjPtr
;
13977 Jim_Free(interp
->freeList
);
13978 interp
->freeList
= nextObjPtr
;
13984 /* [finalize] reference ?newValue? */
13985 static int Jim_FinalizeCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13987 if (argc
!= 2 && argc
!= 3) {
13988 Jim_WrongNumArgs(interp
, 1, argv
, "reference ?finalizerProc?");
13992 Jim_Obj
*cmdNamePtr
;
13994 if (Jim_GetFinalizer(interp
, argv
[1], &cmdNamePtr
) != JIM_OK
)
13996 if (cmdNamePtr
!= NULL
) /* otherwise the null string is returned. */
13997 Jim_SetResult(interp
, cmdNamePtr
);
14000 if (Jim_SetFinalizer(interp
, argv
[1], argv
[2]) != JIM_OK
)
14002 Jim_SetResult(interp
, argv
[2]);
14007 /* [info references] */
14008 static int JimInfoReferences(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14010 Jim_Obj
*listObjPtr
;
14011 Jim_HashTableIterator htiter
;
14014 listObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
14016 JimInitHashTableIterator(&interp
->references
, &htiter
);
14017 while ((he
= Jim_NextHashEntry(&htiter
)) != NULL
) {
14018 char buf
[JIM_REFERENCE_SPACE
+ 1];
14019 Jim_Reference
*refPtr
= Jim_GetHashEntryVal(he
);
14020 const unsigned long *refId
= he
->key
;
14022 JimFormatReference(buf
, refPtr
, *refId
);
14023 Jim_ListAppendElement(interp
, listObjPtr
, Jim_NewStringObj(interp
, buf
, -1));
14025 Jim_SetResult(interp
, listObjPtr
);
14028 #endif /* JIM_REFERENCES && !JIM_BOOTSTRAP */
14031 static int Jim_RenameCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14034 Jim_WrongNumArgs(interp
, 1, argv
, "oldName newName");
14038 if (JimValidName(interp
, "new procedure", argv
[2])) {
14042 return Jim_RenameCommand(interp
, Jim_String(argv
[1]), Jim_String(argv
[2]));
14045 #define JIM_DICTMATCH_KEYS 0x0001
14046 #define JIM_DICTMATCH_VALUES 0x002
14049 * match_type must be one of JIM_DICTMATCH_KEYS or JIM_DICTMATCH_VALUES
14050 * return_types should be either or both
14052 int Jim_DictMatchTypes(Jim_Interp
*interp
, Jim_Obj
*objPtr
, Jim_Obj
*patternObj
, int match_type
, int return_types
)
14055 Jim_Obj
*listObjPtr
;
14056 Jim_HashTableIterator htiter
;
14058 if (SetDictFromAny(interp
, objPtr
) != JIM_OK
) {
14062 listObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
14064 JimInitHashTableIterator(objPtr
->internalRep
.ptr
, &htiter
);
14065 while ((he
= Jim_NextHashEntry(&htiter
)) != NULL
) {
14067 Jim_Obj
*matchObj
= (match_type
== JIM_DICTMATCH_KEYS
) ? (Jim_Obj
*)he
->key
: Jim_GetHashEntryVal(he
);
14068 if (!JimGlobMatch(Jim_String(patternObj
), Jim_String(matchObj
), 0)) {
14073 if (return_types
& JIM_DICTMATCH_KEYS
) {
14074 Jim_ListAppendElement(interp
, listObjPtr
, (Jim_Obj
*)he
->key
);
14076 if (return_types
& JIM_DICTMATCH_VALUES
) {
14077 Jim_ListAppendElement(interp
, listObjPtr
, Jim_GetHashEntryVal(he
));
14081 Jim_SetResult(interp
, listObjPtr
);
14085 int Jim_DictSize(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
14087 if (SetDictFromAny(interp
, objPtr
) != JIM_OK
) {
14090 return ((Jim_HashTable
*)objPtr
->internalRep
.ptr
)->used
;
14094 * Must be called with at least one object.
14095 * Returns the new dictionary, or NULL on error.
14097 Jim_Obj
*Jim_DictMerge(Jim_Interp
*interp
, int objc
, Jim_Obj
*const *objv
)
14099 Jim_Obj
*objPtr
= Jim_NewDictObj(interp
, NULL
, 0);
14102 JimPanic((objc
== 0, "Jim_DictMerge called with objc=0"));
14104 /* Note that we don't optimise the trivial case of a single argument */
14106 for (i
= 0; i
< objc
; i
++) {
14108 Jim_HashTableIterator htiter
;
14111 if (SetDictFromAny(interp
, objv
[i
]) != JIM_OK
) {
14112 Jim_FreeNewObj(interp
, objPtr
);
14115 ht
= objv
[i
]->internalRep
.ptr
;
14116 JimInitHashTableIterator(ht
, &htiter
);
14117 while ((he
= Jim_NextHashEntry(&htiter
)) != NULL
) {
14118 Jim_ReplaceHashEntry(objPtr
->internalRep
.ptr
, Jim_GetHashEntryKey(he
), Jim_GetHashEntryVal(he
));
14124 int Jim_DictInfo(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
14130 int nonzero_count
= 0;
14132 int bucket_counts
[11] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
14134 if (SetDictFromAny(interp
, objPtr
) != JIM_OK
) {
14138 ht
= (Jim_HashTable
*)objPtr
->internalRep
.ptr
;
14140 /* Note that this uses internal knowledge of the hash table */
14141 snprintf(buffer
, sizeof(buffer
), "%d entries in table, %d buckets\n", ht
->used
, ht
->size
);
14142 output
= Jim_NewStringObj(interp
, buffer
, -1);
14144 for (i
= 0; i
< ht
->size
; i
++) {
14145 Jim_HashEntry
*he
= ht
->table
[i
];
14152 bucket_counts
[10]++;
14155 bucket_counts
[entries
]++;
14162 for (i
= 0; i
< 10; i
++) {
14163 snprintf(buffer
, sizeof(buffer
), "number of buckets with %d entries: %d\n", i
, bucket_counts
[i
]);
14164 Jim_AppendString(interp
, output
, buffer
, -1);
14166 snprintf(buffer
, sizeof(buffer
), "number of buckets with 10 or more entries: %d\n", bucket_counts
[10]);
14167 Jim_AppendString(interp
, output
, buffer
, -1);
14168 snprintf(buffer
, sizeof(buffer
), "average search distance for entry: %.1f", nonzero_count
? (double)sum
/ nonzero_count
: 0.0);
14169 Jim_AppendString(interp
, output
, buffer
, -1);
14170 Jim_SetResult(interp
, output
);
14174 static int Jim_EvalEnsemble(Jim_Interp
*interp
, const char *basecmd
, const char *subcmd
, int argc
, Jim_Obj
*const *argv
)
14176 Jim_Obj
*prefixObj
= Jim_NewStringObj(interp
, basecmd
, -1);
14178 Jim_AppendString(interp
, prefixObj
, " ", 1);
14179 Jim_AppendString(interp
, prefixObj
, subcmd
, -1);
14181 return Jim_EvalObjPrefix(interp
, prefixObj
, argc
, argv
);
14185 * Implements the [dict with] command
14187 static int JimDictWith(Jim_Interp
*interp
, Jim_Obj
*dictVarName
, Jim_Obj
*const *keyv
, int keyc
, Jim_Obj
*scriptObj
)
14192 Jim_Obj
**dictValues
;
14196 /* Open up the appropriate level of the dictionary */
14197 dictObj
= Jim_GetVariable(interp
, dictVarName
, JIM_ERRMSG
);
14198 if (dictObj
== NULL
|| Jim_DictKeysVector(interp
, dictObj
, keyv
, keyc
, &objPtr
, JIM_ERRMSG
) != JIM_OK
) {
14201 /* Set the local variables */
14202 if (Jim_DictPairs(interp
, objPtr
, &dictValues
, &len
) == JIM_ERR
) {
14205 for (i
= 0; i
< len
; i
+= 2) {
14206 if (Jim_SetVariable(interp
, dictValues
[i
], dictValues
[i
+ 1]) == JIM_ERR
) {
14207 Jim_Free(dictValues
);
14212 /* As an optimisation, if the script is empty, no need to evaluate it or update the dict */
14213 if (Jim_Length(scriptObj
)) {
14214 ret
= Jim_EvalObj(interp
, scriptObj
);
14216 /* Now if the dictionary still exists, update it based on the local variables */
14217 if (ret
== JIM_OK
&& Jim_GetVariable(interp
, dictVarName
, 0) != NULL
) {
14218 /* We need a copy of keyv with one extra element at the end for Jim_SetDictKeysVector() */
14219 Jim_Obj
**newkeyv
= Jim_Alloc(sizeof(*newkeyv
) * (keyc
+ 1));
14220 for (i
= 0; i
< keyc
; i
++) {
14221 newkeyv
[i
] = keyv
[i
];
14224 for (i
= 0; i
< len
; i
+= 2) {
14225 /* This will be NULL if the variable no longer exists, thus deleting the variable */
14226 objPtr
= Jim_GetVariable(interp
, dictValues
[i
], 0);
14227 newkeyv
[keyc
] = dictValues
[i
];
14228 Jim_SetDictKeysVector(interp
, dictVarName
, newkeyv
, keyc
+ 1, objPtr
, 0);
14234 Jim_Free(dictValues
);
14240 static int Jim_DictCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14243 int types
= JIM_DICTMATCH_KEYS
;
14245 static const char * const options
[] = {
14246 "create", "get", "set", "unset", "exists", "keys", "size", "info",
14247 "merge", "with", "append", "lappend", "incr", "remove", "values", "for",
14248 "replace", "update", NULL
14252 OPT_CREATE
, OPT_GET
, OPT_SET
, OPT_UNSET
, OPT_EXISTS
, OPT_KEYS
, OPT_SIZE
, OPT_INFO
,
14253 OPT_MERGE
, OPT_WITH
, OPT_APPEND
, OPT_LAPPEND
, OPT_INCR
, OPT_REMOVE
, OPT_VALUES
, OPT_FOR
,
14254 OPT_REPLACE
, OPT_UPDATE
,
14258 Jim_WrongNumArgs(interp
, 1, argv
, "subcommand ?arguments ...?");
14262 if (Jim_GetEnum(interp
, argv
[1], options
, &option
, "subcommand", JIM_ERRMSG
) != JIM_OK
) {
14263 return Jim_CheckShowCommands(interp
, argv
[1], options
);
14269 Jim_WrongNumArgs(interp
, 2, argv
, "dictionary ?key ...?");
14272 if (Jim_DictKeysVector(interp
, argv
[2], argv
+ 3, argc
- 3, &objPtr
,
14273 JIM_ERRMSG
) != JIM_OK
) {
14276 Jim_SetResult(interp
, objPtr
);
14281 Jim_WrongNumArgs(interp
, 2, argv
, "varName key ?key ...? value");
14284 return Jim_SetDictKeysVector(interp
, argv
[2], argv
+ 3, argc
- 4, argv
[argc
- 1], JIM_ERRMSG
);
14288 Jim_WrongNumArgs(interp
, 2, argv
, "dictionary key ?key ...?");
14292 int rc
= Jim_DictKeysVector(interp
, argv
[2], argv
+ 3, argc
- 3, &objPtr
, JIM_ERRMSG
);
14296 Jim_SetResultBool(interp
, rc
== JIM_OK
);
14302 Jim_WrongNumArgs(interp
, 2, argv
, "varName key ?key ...?");
14305 if (Jim_SetDictKeysVector(interp
, argv
[2], argv
+ 3, argc
- 3, NULL
, 0) != JIM_OK
) {
14311 types
= JIM_DICTMATCH_VALUES
;
14314 if (argc
!= 3 && argc
!= 4) {
14315 Jim_WrongNumArgs(interp
, 2, argv
, "dictionary ?pattern?");
14318 return Jim_DictMatchTypes(interp
, argv
[2], argc
== 4 ? argv
[3] : NULL
, types
, types
);
14322 Jim_WrongNumArgs(interp
, 2, argv
, "dictionary");
14325 else if (Jim_DictSize(interp
, argv
[2]) < 0) {
14328 Jim_SetResultInt(interp
, Jim_DictSize(interp
, argv
[2]));
14335 objPtr
= Jim_DictMerge(interp
, argc
- 2, argv
+ 2);
14336 if (objPtr
== NULL
) {
14339 Jim_SetResult(interp
, objPtr
);
14343 if (argc
< 6 || argc
% 2) {
14344 /* Better error message */
14351 Jim_WrongNumArgs(interp
, 2, argv
, "?key value ...?");
14354 objPtr
= Jim_NewDictObj(interp
, argv
+ 2, argc
- 2);
14355 Jim_SetResult(interp
, objPtr
);
14360 Jim_WrongNumArgs(interp
, 2, argv
, "dictionary");
14363 return Jim_DictInfo(interp
, argv
[2]);
14367 Jim_WrongNumArgs(interp
, 2, argv
, "dictVar ?key ...? script");
14370 return JimDictWith(interp
, argv
[2], argv
+ 3, argc
- 4, argv
[argc
- 1]);
14372 /* Handle command as an ensemble */
14373 return Jim_EvalEnsemble(interp
, "dict", options
[option
], argc
- 2, argv
+ 2);
14377 static int Jim_SubstCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14379 static const char * const options
[] = {
14380 "-nobackslashes", "-nocommands", "-novariables", NULL
14383 { OPT_NOBACKSLASHES
, OPT_NOCOMMANDS
, OPT_NOVARIABLES
};
14385 int flags
= JIM_SUBST_FLAG
;
14389 Jim_WrongNumArgs(interp
, 1, argv
, "?options? string");
14392 for (i
= 1; i
< (argc
- 1); i
++) {
14395 if (Jim_GetEnum(interp
, argv
[i
], options
, &option
, NULL
,
14396 JIM_ERRMSG
| JIM_ENUM_ABBREV
) != JIM_OK
) {
14400 case OPT_NOBACKSLASHES
:
14401 flags
|= JIM_SUBST_NOESC
;
14403 case OPT_NOCOMMANDS
:
14404 flags
|= JIM_SUBST_NOCMD
;
14406 case OPT_NOVARIABLES
:
14407 flags
|= JIM_SUBST_NOVAR
;
14411 if (Jim_SubstObj(interp
, argv
[argc
- 1], &objPtr
, flags
) != JIM_OK
) {
14414 Jim_SetResult(interp
, objPtr
);
14419 static int Jim_InfoCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14425 static const char * const commands
[] = {
14426 "body", "statics", "commands", "procs", "channels", "exists", "globals", "level", "frame", "locals",
14427 "vars", "version", "patchlevel", "complete", "args", "hostname",
14428 "script", "source", "stacktrace", "nameofexecutable", "returncodes",
14429 "references", "alias", NULL
14432 { INFO_BODY
, INFO_STATICS
, INFO_COMMANDS
, INFO_PROCS
, INFO_CHANNELS
, INFO_EXISTS
, INFO_GLOBALS
, INFO_LEVEL
,
14433 INFO_FRAME
, INFO_LOCALS
, INFO_VARS
, INFO_VERSION
, INFO_PATCHLEVEL
, INFO_COMPLETE
, INFO_ARGS
,
14434 INFO_HOSTNAME
, INFO_SCRIPT
, INFO_SOURCE
, INFO_STACKTRACE
, INFO_NAMEOFEXECUTABLE
,
14435 INFO_RETURNCODES
, INFO_REFERENCES
, INFO_ALIAS
,
14438 #ifdef jim_ext_namespace
14441 if (argc
> 2 && Jim_CompareStringImmediate(interp
, argv
[1], "-nons")) {
14442 /* This is for internal use only */
14450 Jim_WrongNumArgs(interp
, 1, argv
, "subcommand ?args ...?");
14453 if (Jim_GetEnum(interp
, argv
[1], commands
, &cmd
, "subcommand", JIM_ERRMSG
| JIM_ENUM_ABBREV
) != JIM_OK
) {
14454 return Jim_CheckShowCommands(interp
, argv
[1], commands
);
14457 /* Test for the most common commands first, just in case it makes a difference */
14461 Jim_WrongNumArgs(interp
, 2, argv
, "varName");
14464 Jim_SetResultBool(interp
, Jim_GetVariable(interp
, argv
[2], 0) != NULL
);
14471 Jim_WrongNumArgs(interp
, 2, argv
, "command");
14474 if ((cmdPtr
= Jim_GetCommand(interp
, argv
[2], JIM_ERRMSG
)) == NULL
) {
14477 if (cmdPtr
->isproc
|| cmdPtr
->u
.native
.cmdProc
!= JimAliasCmd
) {
14478 Jim_SetResultFormatted(interp
, "command \"%#s\" is not an alias", argv
[2]);
14481 Jim_SetResult(interp
, (Jim_Obj
*)cmdPtr
->u
.native
.privData
);
14485 case INFO_CHANNELS
:
14486 mode
++; /* JIM_CMDLIST_CHANNELS */
14487 #ifndef jim_ext_aio
14488 Jim_SetResultString(interp
, "aio not enabled", -1);
14493 mode
++; /* JIM_CMDLIST_PROCS */
14495 case INFO_COMMANDS
:
14496 /* mode 0 => JIM_CMDLIST_COMMANDS */
14497 if (argc
!= 2 && argc
!= 3) {
14498 Jim_WrongNumArgs(interp
, 2, argv
, "?pattern?");
14501 #ifdef jim_ext_namespace
14503 if (Jim_Length(interp
->framePtr
->nsObj
) || (argc
== 3 && JimGlobMatch("::*", Jim_String(argv
[2]), 0))) {
14504 return Jim_EvalPrefix(interp
, "namespace info", argc
- 1, argv
+ 1);
14508 Jim_SetResult(interp
, JimCommandsList(interp
, (argc
== 3) ? argv
[2] : NULL
, mode
));
14512 mode
++; /* JIM_VARLIST_VARS */
14515 mode
++; /* JIM_VARLIST_LOCALS */
14518 /* mode 0 => JIM_VARLIST_GLOBALS */
14519 if (argc
!= 2 && argc
!= 3) {
14520 Jim_WrongNumArgs(interp
, 2, argv
, "?pattern?");
14523 #ifdef jim_ext_namespace
14525 if (Jim_Length(interp
->framePtr
->nsObj
) || (argc
== 3 && JimGlobMatch("::*", Jim_String(argv
[2]), 0))) {
14526 return Jim_EvalPrefix(interp
, "namespace info", argc
- 1, argv
+ 1);
14530 Jim_SetResult(interp
, JimVariablesList(interp
, argc
== 3 ? argv
[2] : NULL
, mode
));
14535 Jim_WrongNumArgs(interp
, 2, argv
, "");
14538 Jim_SetResult(interp
, JimGetScript(interp
, interp
->currentScriptObj
)->fileNameObj
);
14543 Jim_Obj
*resObjPtr
;
14544 Jim_Obj
*fileNameObj
;
14546 if (argc
!= 3 && argc
!= 5) {
14547 Jim_WrongNumArgs(interp
, 2, argv
, "source ?filename line?");
14551 if (Jim_GetWide(interp
, argv
[4], &line
) != JIM_OK
) {
14554 resObjPtr
= Jim_NewStringObj(interp
, Jim_String(argv
[2]), Jim_Length(argv
[2]));
14555 JimSetSourceInfo(interp
, resObjPtr
, argv
[3], line
);
14558 if (argv
[2]->typePtr
== &sourceObjType
) {
14559 fileNameObj
= argv
[2]->internalRep
.sourceValue
.fileNameObj
;
14560 line
= argv
[2]->internalRep
.sourceValue
.lineNumber
;
14562 else if (argv
[2]->typePtr
== &scriptObjType
) {
14563 ScriptObj
*script
= JimGetScript(interp
, argv
[2]);
14564 fileNameObj
= script
->fileNameObj
;
14565 line
= script
->firstline
;
14568 fileNameObj
= interp
->emptyObj
;
14571 resObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
14572 Jim_ListAppendElement(interp
, resObjPtr
, fileNameObj
);
14573 Jim_ListAppendElement(interp
, resObjPtr
, Jim_NewIntObj(interp
, line
));
14575 Jim_SetResult(interp
, resObjPtr
);
14579 case INFO_STACKTRACE
:
14580 Jim_SetResult(interp
, interp
->stackTrace
);
14587 Jim_SetResultInt(interp
, interp
->framePtr
->level
);
14591 if (JimInfoLevel(interp
, argv
[2], &objPtr
, cmd
== INFO_LEVEL
) != JIM_OK
) {
14594 Jim_SetResult(interp
, objPtr
);
14598 Jim_WrongNumArgs(interp
, 2, argv
, "?levelNum?");
14609 Jim_WrongNumArgs(interp
, 2, argv
, "procname");
14612 if ((cmdPtr
= Jim_GetCommand(interp
, argv
[2], JIM_ERRMSG
)) == NULL
) {
14615 if (!cmdPtr
->isproc
) {
14616 Jim_SetResultFormatted(interp
, "command \"%#s\" is not a procedure", argv
[2]);
14621 Jim_SetResult(interp
, cmdPtr
->u
.proc
.bodyObjPtr
);
14624 Jim_SetResult(interp
, cmdPtr
->u
.proc
.argListObjPtr
);
14627 if (cmdPtr
->u
.proc
.staticVars
) {
14628 Jim_SetResult(interp
, JimHashtablePatternMatch(interp
, cmdPtr
->u
.proc
.staticVars
,
14629 NULL
, JimVariablesMatch
, JIM_VARLIST_LOCALS
| JIM_VARLIST_VALUES
));
14637 case INFO_PATCHLEVEL
:{
14638 char buf
[(JIM_INTEGER_SPACE
* 2) + 1];
14640 sprintf(buf
, "%d.%d", JIM_VERSION
/ 100, JIM_VERSION
% 100);
14641 Jim_SetResultString(interp
, buf
, -1);
14645 case INFO_COMPLETE
:
14646 if (argc
!= 3 && argc
!= 4) {
14647 Jim_WrongNumArgs(interp
, 2, argv
, "script ?missing?");
14653 Jim_SetResultBool(interp
, Jim_ScriptIsComplete(interp
, argv
[2], &missing
));
14654 if (missing
!= ' ' && argc
== 4) {
14655 Jim_SetVariable(interp
, argv
[3], Jim_NewStringObj(interp
, &missing
, 1));
14660 case INFO_HOSTNAME
:
14661 /* Redirect to os.gethostname if it exists */
14662 return Jim_Eval(interp
, "os.gethostname");
14664 case INFO_NAMEOFEXECUTABLE
:
14665 /* Redirect to Tcl proc */
14666 return Jim_Eval(interp
, "{info nameofexecutable}");
14668 case INFO_RETURNCODES
:
14671 Jim_Obj
*listObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
14673 for (i
= 0; jimReturnCodes
[i
]; i
++) {
14674 Jim_ListAppendElement(interp
, listObjPtr
, Jim_NewIntObj(interp
, i
));
14675 Jim_ListAppendElement(interp
, listObjPtr
, Jim_NewStringObj(interp
,
14676 jimReturnCodes
[i
], -1));
14679 Jim_SetResult(interp
, listObjPtr
);
14681 else if (argc
== 3) {
14685 if (Jim_GetLong(interp
, argv
[2], &code
) != JIM_OK
) {
14688 name
= Jim_ReturnCode(code
);
14689 if (*name
== '?') {
14690 Jim_SetResultInt(interp
, code
);
14693 Jim_SetResultString(interp
, name
, -1);
14697 Jim_WrongNumArgs(interp
, 2, argv
, "?code?");
14701 case INFO_REFERENCES
:
14702 #ifdef JIM_REFERENCES
14703 return JimInfoReferences(interp
, argc
, argv
);
14705 Jim_SetResultString(interp
, "not supported", -1);
14713 static int Jim_ExistsCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14718 static const char * const options
[] = {
14719 "-command", "-proc", "-alias", "-var", NULL
14723 OPT_COMMAND
, OPT_PROC
, OPT_ALIAS
, OPT_VAR
14731 else if (argc
== 3) {
14732 if (Jim_GetEnum(interp
, argv
[1], options
, &option
, NULL
, JIM_ERRMSG
| JIM_ENUM_ABBREV
) != JIM_OK
) {
14738 Jim_WrongNumArgs(interp
, 1, argv
, "?option? name");
14742 if (option
== OPT_VAR
) {
14743 result
= Jim_GetVariable(interp
, objPtr
, 0) != NULL
;
14746 /* Now different kinds of commands */
14747 Jim_Cmd
*cmd
= Jim_GetCommand(interp
, objPtr
, JIM_NONE
);
14756 result
= cmd
->isproc
== 0 && cmd
->u
.native
.cmdProc
== JimAliasCmd
;
14760 result
= cmd
->isproc
;
14765 Jim_SetResultBool(interp
, result
);
14770 static int Jim_SplitCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14772 const char *str
, *splitChars
, *noMatchStart
;
14773 int splitLen
, strLen
;
14774 Jim_Obj
*resObjPtr
;
14778 if (argc
!= 2 && argc
!= 3) {
14779 Jim_WrongNumArgs(interp
, 1, argv
, "string ?splitChars?");
14783 str
= Jim_GetString(argv
[1], &len
);
14787 strLen
= Jim_Utf8Length(interp
, argv
[1]);
14791 splitChars
= " \n\t\r";
14795 splitChars
= Jim_String(argv
[2]);
14796 splitLen
= Jim_Utf8Length(interp
, argv
[2]);
14799 noMatchStart
= str
;
14800 resObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
14806 const char *sc
= splitChars
;
14807 int scLen
= splitLen
;
14808 int sl
= utf8_tounicode(str
, &c
);
14811 sc
+= utf8_tounicode(sc
, &pc
);
14813 objPtr
= Jim_NewStringObj(interp
, noMatchStart
, (str
- noMatchStart
));
14814 Jim_ListAppendElement(interp
, resObjPtr
, objPtr
);
14815 noMatchStart
= str
+ sl
;
14821 objPtr
= Jim_NewStringObj(interp
, noMatchStart
, (str
- noMatchStart
));
14822 Jim_ListAppendElement(interp
, resObjPtr
, objPtr
);
14825 /* This handles the special case of splitchars eq {}
14826 * Optimise by sharing common (ASCII) characters
14828 Jim_Obj
**commonObj
= NULL
;
14829 #define NUM_COMMON (128 - 9)
14831 int n
= utf8_tounicode(str
, &c
);
14832 #ifdef JIM_OPTIMIZATION
14833 if (c
>= 9 && c
< 128) {
14834 /* Common ASCII char. Note that 9 is the tab character */
14837 commonObj
= Jim_Alloc(sizeof(*commonObj
) * NUM_COMMON
);
14838 memset(commonObj
, 0, sizeof(*commonObj
) * NUM_COMMON
);
14840 if (!commonObj
[c
]) {
14841 commonObj
[c
] = Jim_NewStringObj(interp
, str
, 1);
14843 Jim_ListAppendElement(interp
, resObjPtr
, commonObj
[c
]);
14848 Jim_ListAppendElement(interp
, resObjPtr
, Jim_NewStringObjUtf8(interp
, str
, 1));
14851 Jim_Free(commonObj
);
14854 Jim_SetResult(interp
, resObjPtr
);
14859 static int Jim_JoinCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14861 const char *joinStr
;
14864 if (argc
!= 2 && argc
!= 3) {
14865 Jim_WrongNumArgs(interp
, 1, argv
, "list ?joinString?");
14874 joinStr
= Jim_GetString(argv
[2], &joinStrLen
);
14876 Jim_SetResult(interp
, Jim_ListJoin(interp
, argv
[1], joinStr
, joinStrLen
));
14881 static int Jim_FormatCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14886 Jim_WrongNumArgs(interp
, 1, argv
, "formatString ?arg arg ...?");
14889 objPtr
= Jim_FormatString(interp
, argv
[1], argc
- 2, argv
+ 2);
14890 if (objPtr
== NULL
)
14892 Jim_SetResult(interp
, objPtr
);
14897 static int Jim_ScanCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14899 Jim_Obj
*listPtr
, **outVec
;
14903 Jim_WrongNumArgs(interp
, 1, argv
, "string format ?varName varName ...?");
14906 if (argv
[2]->typePtr
!= &scanFmtStringObjType
)
14907 SetScanFmtFromAny(interp
, argv
[2]);
14908 if (FormatGetError(argv
[2]) != 0) {
14909 Jim_SetResultString(interp
, FormatGetError(argv
[2]), -1);
14913 int maxPos
= FormatGetMaxPos(argv
[2]);
14914 int count
= FormatGetCnvCount(argv
[2]);
14916 if (maxPos
> argc
- 3) {
14917 Jim_SetResultString(interp
, "\"%n$\" argument index out of range", -1);
14920 else if (count
> argc
- 3) {
14921 Jim_SetResultString(interp
, "different numbers of variable names and "
14922 "field specifiers", -1);
14925 else if (count
< argc
- 3) {
14926 Jim_SetResultString(interp
, "variable is not assigned by any "
14927 "conversion specifiers", -1);
14931 listPtr
= Jim_ScanString(interp
, argv
[1], argv
[2], JIM_ERRMSG
);
14938 if (listPtr
!= 0 && listPtr
!= (Jim_Obj
*)EOF
) {
14939 int len
= Jim_ListLength(interp
, listPtr
);
14942 JimListGetElements(interp
, listPtr
, &outc
, &outVec
);
14943 for (i
= 0; i
< outc
; ++i
) {
14944 if (Jim_Length(outVec
[i
]) > 0) {
14946 if (Jim_SetVariable(interp
, argv
[3 + i
], outVec
[i
]) != JIM_OK
) {
14952 Jim_FreeNewObj(interp
, listPtr
);
14957 if (rc
== JIM_OK
) {
14958 Jim_SetResultInt(interp
, count
);
14963 if (listPtr
== (Jim_Obj
*)EOF
) {
14964 Jim_SetResult(interp
, Jim_NewListObj(interp
, 0, 0));
14967 Jim_SetResult(interp
, listPtr
);
14973 static int Jim_ErrorCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14975 if (argc
!= 2 && argc
!= 3) {
14976 Jim_WrongNumArgs(interp
, 1, argv
, "message ?stacktrace?");
14979 Jim_SetResult(interp
, argv
[1]);
14981 JimSetStackTrace(interp
, argv
[2]);
14984 interp
->addStackTrace
++;
14989 static int Jim_LrangeCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14994 Jim_WrongNumArgs(interp
, 1, argv
, "list first last");
14997 if ((objPtr
= Jim_ListRange(interp
, argv
[1], argv
[2], argv
[3])) == NULL
)
14999 Jim_SetResult(interp
, objPtr
);
15004 static int Jim_LrepeatCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
15009 if (argc
< 2 || Jim_GetLong(interp
, argv
[1], &count
) != JIM_OK
|| count
< 0) {
15010 Jim_WrongNumArgs(interp
, 1, argv
, "count ?value ...?");
15014 if (count
== 0 || argc
== 2) {
15021 objPtr
= Jim_NewListObj(interp
, argv
, argc
);
15023 ListInsertElements(objPtr
, -1, argc
, argv
);
15026 Jim_SetResult(interp
, objPtr
);
15030 char **Jim_GetEnviron(void)
15032 #if defined(HAVE__NSGETENVIRON)
15033 return *_NSGetEnviron();
15035 #if !defined(NO_ENVIRON_EXTERN)
15036 extern char **environ
;
15043 void Jim_SetEnviron(char **env
)
15045 #if defined(HAVE__NSGETENVIRON)
15046 *_NSGetEnviron() = env
;
15048 #if !defined(NO_ENVIRON_EXTERN)
15049 extern char **environ
;
15057 static int Jim_EnvCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
15063 char **e
= Jim_GetEnviron();
15066 Jim_Obj
*listObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
15068 for (i
= 0; e
[i
]; i
++) {
15069 const char *equals
= strchr(e
[i
], '=');
15072 Jim_ListAppendElement(interp
, listObjPtr
, Jim_NewStringObj(interp
, e
[i
],
15074 Jim_ListAppendElement(interp
, listObjPtr
, Jim_NewStringObj(interp
, equals
+ 1, -1));
15078 Jim_SetResult(interp
, listObjPtr
);
15083 Jim_WrongNumArgs(interp
, 1, argv
, "varName ?default?");
15086 key
= Jim_String(argv
[1]);
15090 Jim_SetResultFormatted(interp
, "environment variable \"%#s\" does not exist", argv
[1]);
15093 val
= Jim_String(argv
[2]);
15095 Jim_SetResult(interp
, Jim_NewStringObj(interp
, val
, -1));
15100 static int Jim_SourceCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
15105 Jim_WrongNumArgs(interp
, 1, argv
, "fileName");
15108 retval
= Jim_EvalFile(interp
, Jim_String(argv
[1]));
15109 if (retval
== JIM_RETURN
)
15115 static int Jim_LreverseCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
15117 Jim_Obj
*revObjPtr
, **ele
;
15121 Jim_WrongNumArgs(interp
, 1, argv
, "list");
15124 JimListGetElements(interp
, argv
[1], &len
, &ele
);
15126 revObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
15128 ListAppendElement(revObjPtr
, ele
[len
--]);
15129 Jim_SetResult(interp
, revObjPtr
);
15133 static int JimRangeLen(jim_wide start
, jim_wide end
, jim_wide step
)
15141 else if (step
> 0 && start
> end
)
15143 else if (step
< 0 && end
> start
)
15147 len
= -len
; /* abs(len) */
15149 step
= -step
; /* abs(step) */
15150 len
= 1 + ((len
- 1) / step
);
15151 /* We can truncate safely to INT_MAX, the range command
15152 * will always return an error for a such long range
15153 * because Tcl lists can't be so long. */
15156 return (int)((len
< 0) ? -1 : len
);
15160 static int Jim_RangeCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
15162 jim_wide start
= 0, end
, step
= 1;
15166 if (argc
< 2 || argc
> 4) {
15167 Jim_WrongNumArgs(interp
, 1, argv
, "?start? end ?step?");
15171 if (Jim_GetWide(interp
, argv
[1], &end
) != JIM_OK
)
15175 if (Jim_GetWide(interp
, argv
[1], &start
) != JIM_OK
||
15176 Jim_GetWide(interp
, argv
[2], &end
) != JIM_OK
)
15178 if (argc
== 4 && Jim_GetWide(interp
, argv
[3], &step
) != JIM_OK
)
15181 if ((len
= JimRangeLen(start
, end
, step
)) == -1) {
15182 Jim_SetResultString(interp
, "Invalid (infinite?) range specified", -1);
15185 objPtr
= Jim_NewListObj(interp
, NULL
, 0);
15186 for (i
= 0; i
< len
; i
++)
15187 ListAppendElement(objPtr
, Jim_NewIntObj(interp
, start
+ i
* step
));
15188 Jim_SetResult(interp
, objPtr
);
15193 static int Jim_RandCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
15195 jim_wide min
= 0, max
= 0, len
, maxMul
;
15197 if (argc
< 1 || argc
> 3) {
15198 Jim_WrongNumArgs(interp
, 1, argv
, "?min? max");
15202 max
= JIM_WIDE_MAX
;
15203 } else if (argc
== 2) {
15204 if (Jim_GetWide(interp
, argv
[1], &max
) != JIM_OK
)
15206 } else if (argc
== 3) {
15207 if (Jim_GetWide(interp
, argv
[1], &min
) != JIM_OK
||
15208 Jim_GetWide(interp
, argv
[2], &max
) != JIM_OK
)
15213 Jim_SetResultString(interp
, "Invalid arguments (max < min)", -1);
15216 maxMul
= JIM_WIDE_MAX
- (len
? (JIM_WIDE_MAX
%len
) : 0);
15220 JimRandomBytes(interp
, &r
, sizeof(jim_wide
));
15221 if (r
< 0 || r
>= maxMul
) continue;
15222 r
= (len
== 0) ? 0 : r
%len
;
15223 Jim_SetResultInt(interp
, min
+r
);
15228 static const struct {
15230 Jim_CmdProc
*cmdProc
;
15231 } Jim_CoreCommandsTable
[] = {
15232 {"alias", Jim_AliasCoreCommand
},
15233 {"set", Jim_SetCoreCommand
},
15234 {"unset", Jim_UnsetCoreCommand
},
15235 {"puts", Jim_PutsCoreCommand
},
15236 {"+", Jim_AddCoreCommand
},
15237 {"*", Jim_MulCoreCommand
},
15238 {"-", Jim_SubCoreCommand
},
15239 {"/", Jim_DivCoreCommand
},
15240 {"incr", Jim_IncrCoreCommand
},
15241 {"while", Jim_WhileCoreCommand
},
15242 {"loop", Jim_LoopCoreCommand
},
15243 {"for", Jim_ForCoreCommand
},
15244 {"foreach", Jim_ForeachCoreCommand
},
15245 {"lmap", Jim_LmapCoreCommand
},
15246 {"lassign", Jim_LassignCoreCommand
},
15247 {"if", Jim_IfCoreCommand
},
15248 {"switch", Jim_SwitchCoreCommand
},
15249 {"list", Jim_ListCoreCommand
},
15250 {"lindex", Jim_LindexCoreCommand
},
15251 {"lset", Jim_LsetCoreCommand
},
15252 {"lsearch", Jim_LsearchCoreCommand
},
15253 {"llength", Jim_LlengthCoreCommand
},
15254 {"lappend", Jim_LappendCoreCommand
},
15255 {"linsert", Jim_LinsertCoreCommand
},
15256 {"lreplace", Jim_LreplaceCoreCommand
},
15257 {"lsort", Jim_LsortCoreCommand
},
15258 {"append", Jim_AppendCoreCommand
},
15259 {"debug", Jim_DebugCoreCommand
},
15260 {"eval", Jim_EvalCoreCommand
},
15261 {"uplevel", Jim_UplevelCoreCommand
},
15262 {"expr", Jim_ExprCoreCommand
},
15263 {"break", Jim_BreakCoreCommand
},
15264 {"continue", Jim_ContinueCoreCommand
},
15265 {"proc", Jim_ProcCoreCommand
},
15266 {"concat", Jim_ConcatCoreCommand
},
15267 {"return", Jim_ReturnCoreCommand
},
15268 {"upvar", Jim_UpvarCoreCommand
},
15269 {"global", Jim_GlobalCoreCommand
},
15270 {"string", Jim_StringCoreCommand
},
15271 {"time", Jim_TimeCoreCommand
},
15272 {"exit", Jim_ExitCoreCommand
},
15273 {"catch", Jim_CatchCoreCommand
},
15274 #ifdef JIM_REFERENCES
15275 {"ref", Jim_RefCoreCommand
},
15276 {"getref", Jim_GetrefCoreCommand
},
15277 {"setref", Jim_SetrefCoreCommand
},
15278 {"finalize", Jim_FinalizeCoreCommand
},
15279 {"collect", Jim_CollectCoreCommand
},
15281 {"rename", Jim_RenameCoreCommand
},
15282 {"dict", Jim_DictCoreCommand
},
15283 {"subst", Jim_SubstCoreCommand
},
15284 {"info", Jim_InfoCoreCommand
},
15285 {"exists", Jim_ExistsCoreCommand
},
15286 {"split", Jim_SplitCoreCommand
},
15287 {"join", Jim_JoinCoreCommand
},
15288 {"format", Jim_FormatCoreCommand
},
15289 {"scan", Jim_ScanCoreCommand
},
15290 {"error", Jim_ErrorCoreCommand
},
15291 {"lrange", Jim_LrangeCoreCommand
},
15292 {"lrepeat", Jim_LrepeatCoreCommand
},
15293 {"env", Jim_EnvCoreCommand
},
15294 {"source", Jim_SourceCoreCommand
},
15295 {"lreverse", Jim_LreverseCoreCommand
},
15296 {"range", Jim_RangeCoreCommand
},
15297 {"rand", Jim_RandCoreCommand
},
15298 {"tailcall", Jim_TailcallCoreCommand
},
15299 {"local", Jim_LocalCoreCommand
},
15300 {"upcall", Jim_UpcallCoreCommand
},
15301 {"apply", Jim_ApplyCoreCommand
},
15305 void Jim_RegisterCoreCommands(Jim_Interp
*interp
)
15309 while (Jim_CoreCommandsTable
[i
].name
!= NULL
) {
15310 Jim_CreateCommand(interp
,
15311 Jim_CoreCommandsTable
[i
].name
, Jim_CoreCommandsTable
[i
].cmdProc
, NULL
, NULL
);
15316 /* -----------------------------------------------------------------------------
15317 * Interactive prompt
15318 * ---------------------------------------------------------------------------*/
15319 void Jim_MakeErrorMessage(Jim_Interp
*interp
)
15323 argv
[0] = Jim_NewStringObj(interp
, "errorInfo", -1);
15324 argv
[1] = interp
->result
;
15326 Jim_EvalObjVector(interp
, 2, argv
);
15330 * Given a null terminated array of strings, returns an allocated, sorted
15331 * copy of the array.
15333 static char **JimSortStringTable(const char *const *tablePtr
)
15336 char **tablePtrSorted
;
15338 /* Find the size of the table */
15339 for (count
= 0; tablePtr
[count
]; count
++) {
15342 /* Allocate one extra for the terminating NULL pointer */
15343 tablePtrSorted
= Jim_Alloc(sizeof(char *) * (count
+ 1));
15344 memcpy(tablePtrSorted
, tablePtr
, sizeof(char *) * count
);
15345 qsort(tablePtrSorted
, count
, sizeof(char *), qsortCompareStringPointers
);
15346 tablePtrSorted
[count
] = NULL
;
15348 return tablePtrSorted
;
15351 static void JimSetFailedEnumResult(Jim_Interp
*interp
, const char *arg
, const char *badtype
,
15352 const char *prefix
, const char *const *tablePtr
, const char *name
)
15354 char **tablePtrSorted
;
15357 if (name
== NULL
) {
15361 Jim_SetResultFormatted(interp
, "%s%s \"%s\": must be ", badtype
, name
, arg
);
15362 tablePtrSorted
= JimSortStringTable(tablePtr
);
15363 for (i
= 0; tablePtrSorted
[i
]; i
++) {
15364 if (tablePtrSorted
[i
+ 1] == NULL
&& i
> 0) {
15365 Jim_AppendString(interp
, Jim_GetResult(interp
), "or ", -1);
15367 Jim_AppendStrings(interp
, Jim_GetResult(interp
), prefix
, tablePtrSorted
[i
], NULL
);
15368 if (tablePtrSorted
[i
+ 1]) {
15369 Jim_AppendString(interp
, Jim_GetResult(interp
), ", ", -1);
15372 Jim_Free(tablePtrSorted
);
15377 * If objPtr is "-commands" sets the Jim result as a sorted list of options in the table
15378 * and returns JIM_OK.
15380 * Otherwise returns JIM_ERR.
15382 int Jim_CheckShowCommands(Jim_Interp
*interp
, Jim_Obj
*objPtr
, const char *const *tablePtr
)
15384 if (Jim_CompareStringImmediate(interp
, objPtr
, "-commands")) {
15386 char **tablePtrSorted
= JimSortStringTable(tablePtr
);
15387 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
15388 for (i
= 0; tablePtrSorted
[i
]; i
++) {
15389 Jim_ListAppendElement(interp
, Jim_GetResult(interp
), Jim_NewStringObj(interp
, tablePtrSorted
[i
], -1));
15391 Jim_Free(tablePtrSorted
);
15397 /* internal rep is stored in ptrIntvalue
15402 static const Jim_ObjType getEnumObjType
= {
15407 JIM_TYPE_REFERENCES
15410 int Jim_GetEnum(Jim_Interp
*interp
, Jim_Obj
*objPtr
,
15411 const char *const *tablePtr
, int *indexPtr
, const char *name
, int flags
)
15413 const char *bad
= "bad ";
15414 const char *const *entryPtr
= NULL
;
15420 if (objPtr
->typePtr
== &getEnumObjType
) {
15421 if (objPtr
->internalRep
.ptrIntValue
.ptr
== tablePtr
&& objPtr
->internalRep
.ptrIntValue
.int1
== flags
) {
15422 *indexPtr
= objPtr
->internalRep
.ptrIntValue
.int2
;
15427 arg
= Jim_GetString(objPtr
, &arglen
);
15431 for (entryPtr
= tablePtr
, i
= 0; *entryPtr
!= NULL
; entryPtr
++, i
++) {
15432 if (Jim_CompareStringImmediate(interp
, objPtr
, *entryPtr
)) {
15433 /* Found an exact match */
15437 if (flags
& JIM_ENUM_ABBREV
) {
15438 /* Accept an unambiguous abbreviation.
15439 * Note that '-' doesnt' consitute a valid abbreviation
15441 if (strncmp(arg
, *entryPtr
, arglen
) == 0) {
15442 if (*arg
== '-' && arglen
== 1) {
15446 bad
= "ambiguous ";
15454 /* If we had an unambiguous partial match */
15457 /* Record the match in the object */
15458 Jim_FreeIntRep(interp
, objPtr
);
15459 objPtr
->typePtr
= &getEnumObjType
;
15460 objPtr
->internalRep
.ptrIntValue
.ptr
= (void *)tablePtr
;
15461 objPtr
->internalRep
.ptrIntValue
.int1
= flags
;
15462 objPtr
->internalRep
.ptrIntValue
.int2
= match
;
15463 /* Return the result */
15469 if (flags
& JIM_ERRMSG
) {
15470 JimSetFailedEnumResult(interp
, arg
, bad
, "", tablePtr
, name
);
15475 int Jim_FindByName(const char *name
, const char * const array
[], size_t len
)
15479 for (i
= 0; i
< (int)len
; i
++) {
15480 if (array
[i
] && strcmp(array
[i
], name
) == 0) {
15487 int Jim_IsDict(Jim_Obj
*objPtr
)
15489 return objPtr
->typePtr
== &dictObjType
;
15492 int Jim_IsList(Jim_Obj
*objPtr
)
15494 return objPtr
->typePtr
== &listObjType
;
15498 * Very simple printf-like formatting, designed for error messages.
15500 * The format may contain up to 5 '%s' or '%#s', corresponding to variable arguments.
15501 * The resulting string is created and set as the result.
15503 * Each '%s' should correspond to a regular string parameter.
15504 * Each '%#s' should correspond to a (Jim_Obj *) parameter.
15505 * Any other printf specifier is not allowed (but %% is allowed for the % character).
15507 * e.g. Jim_SetResultFormatted(interp, "Bad option \"%#s\" in proc \"%#s\"", optionObjPtr, procNamePtr);
15509 * Note: We take advantage of the fact that printf has the same behaviour for both %s and %#s
15511 * Note that any Jim_Obj parameters with zero ref count will be freed as a result of this call.
15513 void Jim_SetResultFormatted(Jim_Interp
*interp
, const char *format
, ...)
15515 /* Initial space needed */
15516 int len
= strlen(format
);
15519 const char *params
[5];
15521 Jim_Obj
*objparam
[5];
15526 va_start(args
, format
);
15528 for (i
= 0; i
< len
&& n
< 5; i
++) {
15531 if (strncmp(format
+ i
, "%s", 2) == 0) {
15532 params
[n
] = va_arg(args
, char *);
15534 l
= strlen(params
[n
]);
15536 else if (strncmp(format
+ i
, "%#s", 3) == 0) {
15537 Jim_Obj
*objPtr
= va_arg(args
, Jim_Obj
*);
15539 params
[n
] = Jim_GetString(objPtr
, &l
);
15540 objparam
[nobjparam
++] = objPtr
;
15541 Jim_IncrRefCount(objPtr
);
15544 if (format
[i
] == '%') {
15554 buf
= Jim_Alloc(len
+ 1);
15555 len
= snprintf(buf
, len
+ 1, format
, params
[0], params
[1], params
[2], params
[3], params
[4]);
15559 Jim_SetResult(interp
, Jim_NewStringObjNoAlloc(interp
, buf
, len
));
15561 for (i
= 0; i
< nobjparam
; i
++) {
15562 Jim_DecrRefCount(interp
, objparam
[i
]);
15567 #ifndef jim_ext_package
15568 int Jim_PackageProvide(Jim_Interp
*interp
, const char *name
, const char *ver
, int flags
)
15573 #ifndef jim_ext_aio
15574 FILE *Jim_AioFilehandle(Jim_Interp
*interp
, Jim_Obj
*fhObj
)
15576 Jim_SetResultString(interp
, "aio not enabled", -1);
15583 * Local Variables: ***
15584 * c-basic-offset: 4 ***