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 * utf-8 string comparison. case-insensitive if nocase is set.
324 * Note that the lengths are character lengths, not byte lengths.
326 static int JimStringCompareUtf8(const char *s1
, int l1
, const char *s2
, int l2
, int nocase
)
334 s1
+= utf8_tounicode_case(s1
, &c1
, nocase
);
335 s2
+= utf8_tounicode_case(s2
, &c2
, nocase
);
337 return JimSign(c1
- c2
);
341 /* Equal to this point, so the shorter string is less */
351 /* Search for 's1' inside 's2', starting to search from char 'index' of 's2'.
352 * The index of the first occurrence of s1 in s2 is returned.
353 * If s1 is not found inside s2, -1 is returned.
355 * Note: Lengths and return value are in bytes, not chars.
357 static int JimStringFirst(const char *s1
, int l1
, const char *s2
, int l2
, int idx
)
362 if (!l1
|| !l2
|| l1
> l2
) {
367 s2
+= utf8_index(s2
, idx
);
369 l1bytelen
= utf8_index(s1
, l1
);
371 for (i
= idx
; i
<= l2
- l1
; i
++) {
373 if (memcmp(s2
, s1
, l1bytelen
) == 0) {
376 s2
+= utf8_tounicode(s2
, &c
);
381 /* Search for the last occurrence 's1' inside 's2', starting to search from char 'index' of 's2'.
382 * The index of the last occurrence of s1 in s2 is returned.
383 * If s1 is not found inside s2, -1 is returned.
385 * Note: Lengths and return value are in bytes, not chars.
387 static int JimStringLast(const char *s1
, int l1
, const char *s2
, int l2
)
391 if (!l1
|| !l2
|| l1
> l2
)
394 /* Now search for the needle */
395 for (p
= s2
+ l2
- 1; p
!= s2
- 1; p
--) {
396 if (*p
== *s1
&& memcmp(s1
, p
, l1
) == 0) {
405 * Per JimStringLast but lengths and return value are in chars, not bytes.
407 static int JimStringLastUtf8(const char *s1
, int l1
, const char *s2
, int l2
)
409 int n
= JimStringLast(s1
, utf8_index(s1
, l1
), s2
, utf8_index(s2
, l2
));
411 n
= utf8_strlen(s2
, n
);
418 * After an strtol()/strtod()-like conversion,
419 * check whether something was converted and that
420 * the only thing left is white space.
422 * Returns JIM_OK or JIM_ERR.
424 static int JimCheckConversion(const char *str
, const char *endptr
)
426 if (str
[0] == '\0' || str
== endptr
) {
430 if (endptr
[0] != '\0') {
432 if (!isspace(UCHAR(*endptr
))) {
441 /* Parses the front of a number to determine its sign and base.
442 * Returns the index to start parsing according to the given base
444 static int JimNumberBase(const char *str
, int *base
, int *sign
)
450 while (isspace(UCHAR(str
[i
]))) {
470 /* We have 0<x>, so see if we can convert it */
471 switch (str
[i
+ 1]) {
472 case 'x': case 'X': *base
= 16; break;
473 case 'o': case 'O': *base
= 8; break;
474 case 'b': case 'B': *base
= 2; break;
478 /* Ensure that (e.g.) 0x-5 fails to parse */
479 if (str
[i
] != '-' && str
[i
] != '+' && !isspace(UCHAR(str
[i
]))) {
480 /* Parse according to this base */
483 /* Parse as base 10 */
488 /* Converts a number as per strtol(..., 0) except leading zeros do *not*
489 * imply octal. Instead, decimal is assumed unless the number begins with 0x, 0o or 0b
491 static long jim_strtol(const char *str
, char **endptr
)
495 int i
= JimNumberBase(str
, &base
, &sign
);
498 long value
= strtol(str
+ i
, endptr
, base
);
499 if (endptr
== NULL
|| *endptr
!= str
+ i
) {
504 /* Can just do a regular base-10 conversion */
505 return strtol(str
, endptr
, 10);
509 /* Converts a number as per strtoull(..., 0) except leading zeros do *not*
510 * imply octal. Instead, decimal is assumed unless the number begins with 0x, 0o or 0b
512 static jim_wide
jim_strtoull(const char *str
, char **endptr
)
514 #ifdef HAVE_LONG_LONG
517 int i
= JimNumberBase(str
, &base
, &sign
);
520 jim_wide value
= strtoull(str
+ i
, endptr
, base
);
521 if (endptr
== NULL
|| *endptr
!= str
+ i
) {
526 /* Can just do a regular base-10 conversion */
527 return strtoull(str
, endptr
, 10);
529 return (unsigned long)jim_strtol(str
, endptr
);
533 int Jim_StringToWide(const char *str
, jim_wide
* widePtr
, int base
)
538 *widePtr
= strtoull(str
, &endptr
, base
);
541 *widePtr
= jim_strtoull(str
, &endptr
);
544 return JimCheckConversion(str
, endptr
);
547 int Jim_StringToDouble(const char *str
, double *doublePtr
)
551 /* Callers can check for underflow via ERANGE */
554 *doublePtr
= strtod(str
, &endptr
);
556 return JimCheckConversion(str
, endptr
);
559 static jim_wide
JimPowWide(jim_wide b
, jim_wide e
)
572 /* Only special case is -1 ^ -n
590 /* -----------------------------------------------------------------------------
592 * ---------------------------------------------------------------------------*/
593 #ifdef JIM_DEBUG_PANIC
594 static void JimPanicDump(int condition
, const char *fmt
, ...)
604 fprintf(stderr
, "\nJIM INTERPRETER PANIC: ");
605 vfprintf(stderr
, fmt
, ap
);
606 fprintf(stderr
, "\n\n");
609 #ifdef HAVE_BACKTRACE
615 size
= backtrace(array
, 40);
616 strings
= backtrace_symbols(array
, size
);
617 for (i
= 0; i
< size
; i
++)
618 fprintf(stderr
, "[backtrace] %s\n", strings
[i
]);
619 fprintf(stderr
, "[backtrace] Include the above lines and the output\n");
620 fprintf(stderr
, "[backtrace] of 'nm <executable>' in the bug report.\n");
628 /* -----------------------------------------------------------------------------
630 * ---------------------------------------------------------------------------*/
632 void *Jim_Alloc(int size
)
634 return size
? malloc(size
) : NULL
;
637 void Jim_Free(void *ptr
)
642 void *Jim_Realloc(void *ptr
, int size
)
644 return realloc(ptr
, size
);
647 char *Jim_StrDup(const char *s
)
652 char *Jim_StrDupLen(const char *s
, int l
)
654 char *copy
= Jim_Alloc(l
+ 1);
656 memcpy(copy
, s
, l
+ 1);
657 copy
[l
] = 0; /* Just to be sure, original could be substring */
661 /* -----------------------------------------------------------------------------
662 * Time related functions
663 * ---------------------------------------------------------------------------*/
665 /* Returns current time in microseconds */
666 static jim_wide
JimClock(void)
670 gettimeofday(&tv
, NULL
);
671 return (jim_wide
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
674 /* -----------------------------------------------------------------------------
676 * ---------------------------------------------------------------------------*/
678 /* -------------------------- private prototypes ---------------------------- */
679 static void JimExpandHashTableIfNeeded(Jim_HashTable
*ht
);
680 static unsigned int JimHashTableNextPower(unsigned int size
);
681 static Jim_HashEntry
*JimInsertHashEntry(Jim_HashTable
*ht
, const void *key
, int replace
);
683 /* -------------------------- hash functions -------------------------------- */
685 /* Thomas Wang's 32 bit Mix Function */
686 unsigned int Jim_IntHashFunction(unsigned int key
)
697 /* Generic hash function (we are using to multiply by 9 and add the byte
699 unsigned int Jim_GenHashFunction(const unsigned char *buf
, int len
)
704 h
+= (h
<< 3) + *buf
++;
708 /* ----------------------------- API implementation ------------------------- */
711 * Reset a hashtable already initialized.
712 * The table data should already have been freed.
714 * Note that type and privdata are not initialised
715 * to allow the now-empty hashtable to be reused
717 static void JimResetHashTable(Jim_HashTable
*ht
)
724 #ifdef JIM_RANDOMISE_HASH
725 /* This is initialised to a random value to avoid a hash collision attack.
726 * See: n.runs-SA-2011.004
728 ht
->uniq
= (rand() ^ time(NULL
) ^ clock());
734 static void JimInitHashTableIterator(Jim_HashTable
*ht
, Jim_HashTableIterator
*iter
)
739 iter
->nextEntry
= NULL
;
742 /* Initialize the hash table */
743 int Jim_InitHashTable(Jim_HashTable
*ht
, const Jim_HashTableType
*type
, void *privDataPtr
)
745 JimResetHashTable(ht
);
747 ht
->privdata
= privDataPtr
;
751 /* Expand or create the hashtable */
752 void Jim_ExpandHashTable(Jim_HashTable
*ht
, unsigned int size
)
754 Jim_HashTable n
; /* the new hashtable */
755 unsigned int realsize
= JimHashTableNextPower(size
), i
;
757 /* the size is invalid if it is smaller than the number of
758 * elements already inside the hashtable */
759 if (size
<= ht
->used
)
762 Jim_InitHashTable(&n
, ht
->type
, ht
->privdata
);
764 n
.sizemask
= realsize
- 1;
765 n
.table
= Jim_Alloc(realsize
* sizeof(Jim_HashEntry
*));
766 /* Keep the same 'uniq' as the original */
769 /* Initialize all the pointers to NULL */
770 memset(n
.table
, 0, realsize
* sizeof(Jim_HashEntry
*));
772 /* Copy all the elements from the old to the new table:
773 * note that if the old hash table is empty ht->used is zero,
774 * so Jim_ExpandHashTable just creates an empty hash table. */
776 for (i
= 0; ht
->used
> 0; i
++) {
777 Jim_HashEntry
*he
, *nextHe
;
779 if (ht
->table
[i
] == NULL
)
782 /* For each hash entry on this slot... */
788 /* Get the new element index */
789 h
= Jim_HashKey(ht
, he
->key
) & n
.sizemask
;
790 he
->next
= n
.table
[h
];
793 /* Pass to the next element */
797 assert(ht
->used
== 0);
800 /* Remap the new hashtable in the old */
804 /* Add an element to the target hash table */
805 int Jim_AddHashEntry(Jim_HashTable
*ht
, const void *key
, void *val
)
807 Jim_HashEntry
*entry
;
809 /* Get the index of the new element, or -1 if
810 * the element already exists. */
811 entry
= JimInsertHashEntry(ht
, key
, 0);
815 /* Set the hash entry fields. */
816 Jim_SetHashKey(ht
, entry
, key
);
817 Jim_SetHashVal(ht
, entry
, val
);
821 /* Add an element, discarding the old if the key already exists */
822 int Jim_ReplaceHashEntry(Jim_HashTable
*ht
, const void *key
, void *val
)
825 Jim_HashEntry
*entry
;
827 /* Get the index of the new element, or -1 if
828 * the element already exists. */
829 entry
= JimInsertHashEntry(ht
, key
, 1);
831 /* It already exists, so only replace the value.
832 * Note if both a destructor and a duplicate function exist,
833 * need to dup before destroy. perhaps they are the same
834 * reference counted object
836 if (ht
->type
->valDestructor
&& ht
->type
->valDup
) {
837 void *newval
= ht
->type
->valDup(ht
->privdata
, val
);
838 ht
->type
->valDestructor(ht
->privdata
, entry
->u
.val
);
839 entry
->u
.val
= newval
;
842 Jim_FreeEntryVal(ht
, entry
);
843 Jim_SetHashVal(ht
, entry
, val
);
848 /* Doesn't exist, so set the key */
849 Jim_SetHashKey(ht
, entry
, key
);
850 Jim_SetHashVal(ht
, entry
, val
);
857 /* Search and remove an element */
858 int Jim_DeleteHashEntry(Jim_HashTable
*ht
, const void *key
)
861 Jim_HashEntry
*he
, *prevHe
;
865 h
= Jim_HashKey(ht
, key
) & ht
->sizemask
;
870 if (Jim_CompareHashKeys(ht
, key
, he
->key
)) {
871 /* Unlink the element from the list */
873 prevHe
->next
= he
->next
;
875 ht
->table
[h
] = he
->next
;
876 Jim_FreeEntryKey(ht
, he
);
877 Jim_FreeEntryVal(ht
, he
);
885 return JIM_ERR
; /* not found */
888 /* Remove all entries from the hash table
889 * and leave it empty for reuse
891 int Jim_FreeHashTable(Jim_HashTable
*ht
)
895 /* Free all the elements */
896 for (i
= 0; ht
->used
> 0; i
++) {
897 Jim_HashEntry
*he
, *nextHe
;
899 if ((he
= ht
->table
[i
]) == NULL
)
903 Jim_FreeEntryKey(ht
, he
);
904 Jim_FreeEntryVal(ht
, he
);
910 /* Free the table and the allocated cache structure */
912 /* Re-initialize the table */
913 JimResetHashTable(ht
);
914 return JIM_OK
; /* never fails */
917 Jim_HashEntry
*Jim_FindHashEntry(Jim_HashTable
*ht
, const void *key
)
924 h
= Jim_HashKey(ht
, key
) & ht
->sizemask
;
927 if (Jim_CompareHashKeys(ht
, key
, he
->key
))
934 Jim_HashTableIterator
*Jim_GetHashTableIterator(Jim_HashTable
*ht
)
936 Jim_HashTableIterator
*iter
= Jim_Alloc(sizeof(*iter
));
937 JimInitHashTableIterator(ht
, iter
);
941 Jim_HashEntry
*Jim_NextHashEntry(Jim_HashTableIterator
*iter
)
944 if (iter
->entry
== NULL
) {
946 if (iter
->index
>= (signed)iter
->ht
->size
)
948 iter
->entry
= iter
->ht
->table
[iter
->index
];
951 iter
->entry
= iter
->nextEntry
;
954 /* We need to save the 'next' here, the iterator user
955 * may delete the entry we are returning. */
956 iter
->nextEntry
= iter
->entry
->next
;
963 /* ------------------------- private functions ------------------------------ */
965 /* Expand the hash table if needed */
966 static void JimExpandHashTableIfNeeded(Jim_HashTable
*ht
)
968 /* If the hash table is empty expand it to the intial size,
969 * if the table is "full" double its size. */
971 Jim_ExpandHashTable(ht
, JIM_HT_INITIAL_SIZE
);
972 if (ht
->size
== ht
->used
)
973 Jim_ExpandHashTable(ht
, ht
->size
* 2);
976 /* Our hash table capability is a power of two */
977 static unsigned int JimHashTableNextPower(unsigned int size
)
979 unsigned int i
= JIM_HT_INITIAL_SIZE
;
981 if (size
>= 2147483648U)
990 /* Returns the index of a free slot that can be populated with
991 * a hash entry for the given 'key'.
992 * If the key already exists, -1 is returned. */
993 static Jim_HashEntry
*JimInsertHashEntry(Jim_HashTable
*ht
, const void *key
, int replace
)
998 /* Expand the hashtable if needed */
999 JimExpandHashTableIfNeeded(ht
);
1001 /* Compute the key hash value */
1002 h
= Jim_HashKey(ht
, key
) & ht
->sizemask
;
1003 /* Search if this slot does not already contain the given key */
1006 if (Jim_CompareHashKeys(ht
, key
, he
->key
))
1007 return replace
? he
: NULL
;
1011 /* Allocates the memory and stores key */
1012 he
= Jim_Alloc(sizeof(*he
));
1013 he
->next
= ht
->table
[h
];
1021 /* ----------------------- StringCopy Hash Table Type ------------------------*/
1023 static unsigned int JimStringCopyHTHashFunction(const void *key
)
1025 return Jim_GenHashFunction(key
, strlen(key
));
1028 static void *JimStringCopyHTDup(void *privdata
, const void *key
)
1030 return Jim_StrDup(key
);
1033 static int JimStringCopyHTKeyCompare(void *privdata
, const void *key1
, const void *key2
)
1035 return strcmp(key1
, key2
) == 0;
1038 static void JimStringCopyHTKeyDestructor(void *privdata
, void *key
)
1043 static const Jim_HashTableType JimPackageHashTableType
= {
1044 JimStringCopyHTHashFunction
, /* hash function */
1045 JimStringCopyHTDup
, /* key dup */
1047 JimStringCopyHTKeyCompare
, /* key compare */
1048 JimStringCopyHTKeyDestructor
, /* key destructor */
1049 NULL
/* val destructor */
1052 typedef struct AssocDataValue
1054 Jim_InterpDeleteProc
*delProc
;
1058 static void JimAssocDataHashTableValueDestructor(void *privdata
, void *data
)
1060 AssocDataValue
*assocPtr
= (AssocDataValue
*) data
;
1062 if (assocPtr
->delProc
!= NULL
)
1063 assocPtr
->delProc((Jim_Interp
*)privdata
, assocPtr
->data
);
1067 static const Jim_HashTableType JimAssocDataHashTableType
= {
1068 JimStringCopyHTHashFunction
, /* hash function */
1069 JimStringCopyHTDup
, /* key dup */
1071 JimStringCopyHTKeyCompare
, /* key compare */
1072 JimStringCopyHTKeyDestructor
, /* key destructor */
1073 JimAssocDataHashTableValueDestructor
/* val destructor */
1076 /* -----------------------------------------------------------------------------
1077 * Stack - This is a simple generic stack implementation. It is used for
1078 * example in the 'expr' expression compiler.
1079 * ---------------------------------------------------------------------------*/
1080 void Jim_InitStack(Jim_Stack
*stack
)
1084 stack
->vector
= NULL
;
1087 void Jim_FreeStack(Jim_Stack
*stack
)
1089 Jim_Free(stack
->vector
);
1092 int Jim_StackLen(Jim_Stack
*stack
)
1097 void Jim_StackPush(Jim_Stack
*stack
, void *element
)
1099 int neededLen
= stack
->len
+ 1;
1101 if (neededLen
> stack
->maxlen
) {
1102 stack
->maxlen
= neededLen
< 20 ? 20 : neededLen
* 2;
1103 stack
->vector
= Jim_Realloc(stack
->vector
, sizeof(void *) * stack
->maxlen
);
1105 stack
->vector
[stack
->len
] = element
;
1109 void *Jim_StackPop(Jim_Stack
*stack
)
1111 if (stack
->len
== 0)
1114 return stack
->vector
[stack
->len
];
1117 void *Jim_StackPeek(Jim_Stack
*stack
)
1119 if (stack
->len
== 0)
1121 return stack
->vector
[stack
->len
- 1];
1124 void Jim_FreeStackElements(Jim_Stack
*stack
, void (*freeFunc
) (void *ptr
))
1128 for (i
= 0; i
< stack
->len
; i
++)
1129 freeFunc(stack
->vector
[i
]);
1132 /* -----------------------------------------------------------------------------
1134 * ---------------------------------------------------------------------------*/
1137 #define JIM_TT_NONE 0 /* No token returned */
1138 #define JIM_TT_STR 1 /* simple string */
1139 #define JIM_TT_ESC 2 /* string that needs escape chars conversion */
1140 #define JIM_TT_VAR 3 /* var substitution */
1141 #define JIM_TT_DICTSUGAR 4 /* Syntax sugar for [dict get], $foo(bar) */
1142 #define JIM_TT_CMD 5 /* command substitution */
1143 /* Note: Keep these three together for TOKEN_IS_SEP() */
1144 #define JIM_TT_SEP 6 /* word separator (white space) */
1145 #define JIM_TT_EOL 7 /* line separator */
1146 #define JIM_TT_EOF 8 /* end of script */
1148 #define JIM_TT_LINE 9 /* special 'start-of-line' token. arg is # of arguments to the command. -ve if {*} */
1149 #define JIM_TT_WORD 10 /* special 'start-of-word' token. arg is # of tokens to combine. -ve if {*} */
1151 /* Additional token types needed for expressions */
1152 #define JIM_TT_SUBEXPR_START 11
1153 #define JIM_TT_SUBEXPR_END 12
1154 #define JIM_TT_SUBEXPR_COMMA 13
1155 #define JIM_TT_EXPR_INT 14
1156 #define JIM_TT_EXPR_DOUBLE 15
1157 #define JIM_TT_EXPR_BOOLEAN 16
1159 #define JIM_TT_EXPRSUGAR 17 /* $(expression) */
1161 /* Operator token types start here */
1162 #define JIM_TT_EXPR_OP 20
1164 #define TOKEN_IS_SEP(type) (type >= JIM_TT_SEP && type <= JIM_TT_EOF)
1165 /* Can this token start an expression? */
1166 #define TOKEN_IS_EXPR_START(type) (type == JIM_TT_NONE || type == JIM_TT_SUBEXPR_START || type == JIM_TT_SUBEXPR_COMMA)
1167 /* Is this token an expression operator? */
1168 #define TOKEN_IS_EXPR_OP(type) (type >= JIM_TT_EXPR_OP)
1171 * Results of missing quotes, braces, etc. from parsing.
1173 struct JimParseMissing
{
1174 int ch
; /* At end of parse, ' ' if complete or '{', '[', '"', '\\', '}' if incomplete */
1175 int line
; /* Line number starting the missing token */
1178 /* Parser context structure. The same context is used to parse
1179 * Tcl scripts, expressions and lists. */
1182 const char *p
; /* Pointer to the point of the program we are parsing */
1183 int len
; /* Remaining length */
1184 int linenr
; /* Current line number */
1186 const char *tend
; /* Returned token is at tstart-tend in 'prg'. */
1187 int tline
; /* Line number of the returned token */
1188 int tt
; /* Token type */
1189 int eof
; /* Non zero if EOF condition is true. */
1190 int inquote
; /* Parsing a quoted string */
1191 int comment
; /* Non zero if the next chars may be a comment. */
1192 struct JimParseMissing missing
; /* Details of any missing quotes, etc. */
1195 static int JimParseScript(struct JimParserCtx
*pc
);
1196 static int JimParseSep(struct JimParserCtx
*pc
);
1197 static int JimParseEol(struct JimParserCtx
*pc
);
1198 static int JimParseCmd(struct JimParserCtx
*pc
);
1199 static int JimParseQuote(struct JimParserCtx
*pc
);
1200 static int JimParseVar(struct JimParserCtx
*pc
);
1201 static int JimParseBrace(struct JimParserCtx
*pc
);
1202 static int JimParseStr(struct JimParserCtx
*pc
);
1203 static int JimParseComment(struct JimParserCtx
*pc
);
1204 static void JimParseSubCmd(struct JimParserCtx
*pc
);
1205 static int JimParseSubQuote(struct JimParserCtx
*pc
);
1206 static Jim_Obj
*JimParserGetTokenObj(Jim_Interp
*interp
, struct JimParserCtx
*pc
);
1208 /* Initialize a parser context.
1209 * 'prg' is a pointer to the program text, linenr is the line
1210 * number of the first line contained in the program. */
1211 static void JimParserInit(struct JimParserCtx
*pc
, const char *prg
, int len
, int linenr
)
1218 pc
->tt
= JIM_TT_NONE
;
1221 pc
->linenr
= linenr
;
1223 pc
->missing
.ch
= ' ';
1224 pc
->missing
.line
= linenr
;
1227 static int JimParseScript(struct JimParserCtx
*pc
)
1229 while (1) { /* the while is used to reiterate with continue if needed */
1232 pc
->tend
= pc
->p
- 1;
1233 pc
->tline
= pc
->linenr
;
1234 pc
->tt
= JIM_TT_EOL
;
1240 if (*(pc
->p
+ 1) == '\n' && !pc
->inquote
) {
1241 return JimParseSep(pc
);
1244 return JimParseStr(pc
);
1250 return JimParseSep(pc
);
1252 return JimParseStr(pc
);
1257 return JimParseEol(pc
);
1258 return JimParseStr(pc
);
1261 return JimParseCmd(pc
);
1264 if (JimParseVar(pc
) == JIM_ERR
) {
1265 /* An orphan $. Create as a separate token */
1266 pc
->tstart
= pc
->tend
= pc
->p
++;
1268 pc
->tt
= JIM_TT_ESC
;
1273 JimParseComment(pc
);
1276 return JimParseStr(pc
);
1279 return JimParseStr(pc
);
1285 static int JimParseSep(struct JimParserCtx
*pc
)
1288 pc
->tline
= pc
->linenr
;
1289 while (isspace(UCHAR(*pc
->p
)) || (*pc
->p
== '\\' && *(pc
->p
+ 1) == '\n')) {
1290 if (*pc
->p
== '\n') {
1293 if (*pc
->p
== '\\') {
1301 pc
->tend
= pc
->p
- 1;
1302 pc
->tt
= JIM_TT_SEP
;
1306 static int JimParseEol(struct JimParserCtx
*pc
)
1309 pc
->tline
= pc
->linenr
;
1310 while (isspace(UCHAR(*pc
->p
)) || *pc
->p
== ';') {
1316 pc
->tend
= pc
->p
- 1;
1317 pc
->tt
= JIM_TT_EOL
;
1322 ** Here are the rules for parsing:
1323 ** {braced expression}
1324 ** - Count open and closing braces
1325 ** - Backslash escapes meaning of braces but doesn't remove the backslash
1327 ** "quoted expression"
1328 ** - Unescaped double quote terminates the expression
1329 ** - Backslash escapes next char
1330 ** - [commands brackets] are counted/nested
1331 ** - command rules apply within [brackets], not quoting rules (i.e. brackets have their own rules)
1333 ** [command expression]
1334 ** - Count open and closing brackets
1335 ** - Backslash escapes next char
1336 ** - [commands brackets] are counted/nested
1337 ** - "quoted expressions" are parsed according to quoting rules
1338 ** - {braced expressions} are parsed according to brace rules
1340 ** For everything, backslash escapes the next char, newline increments current line
1344 * Parses a braced expression starting at pc->p.
1346 * Positions the parser at the end of the braced expression,
1347 * sets pc->tend and possibly pc->missing.
1349 static void JimParseSubBrace(struct JimParserCtx
*pc
)
1353 /* Skip the brace */
1360 if (*++pc
->p
== '\n') {
1373 pc
->tend
= pc
->p
- 1;
1387 pc
->missing
.ch
= '{';
1388 pc
->missing
.line
= pc
->tline
;
1389 pc
->tend
= pc
->p
- 1;
1393 * Parses a quoted expression starting at pc->p.
1395 * Positions the parser at the end of the quoted expression,
1396 * sets pc->tend and possibly pc->missing.
1398 * Returns the type of the token of the string,
1399 * either JIM_TT_ESC (if it contains values which need to be [subst]ed)
1402 static int JimParseSubQuote(struct JimParserCtx
*pc
)
1404 int tt
= JIM_TT_STR
;
1405 int line
= pc
->tline
;
1407 /* Skip the quote */
1414 if (*++pc
->p
== '\n') {
1423 pc
->tend
= pc
->p
- 1;
1444 pc
->missing
.ch
= '"';
1445 pc
->missing
.line
= line
;
1446 pc
->tend
= pc
->p
- 1;
1451 * Parses a [command] expression starting at pc->p.
1453 * Positions the parser at the end of the command expression,
1454 * sets pc->tend and possibly pc->missing.
1456 static void JimParseSubCmd(struct JimParserCtx
*pc
)
1459 int startofword
= 1;
1460 int line
= pc
->tline
;
1462 /* Skip the bracket */
1469 if (*++pc
->p
== '\n') {
1482 pc
->tend
= pc
->p
- 1;
1491 JimParseSubQuote(pc
);
1492 if (pc
->missing
.ch
== '"') {
1500 JimParseSubBrace(pc
);
1508 startofword
= isspace(UCHAR(*pc
->p
));
1512 pc
->missing
.ch
= '[';
1513 pc
->missing
.line
= line
;
1514 pc
->tend
= pc
->p
- 1;
1517 static int JimParseBrace(struct JimParserCtx
*pc
)
1519 pc
->tstart
= pc
->p
+ 1;
1520 pc
->tline
= pc
->linenr
;
1521 pc
->tt
= JIM_TT_STR
;
1522 JimParseSubBrace(pc
);
1526 static int JimParseCmd(struct JimParserCtx
*pc
)
1528 pc
->tstart
= pc
->p
+ 1;
1529 pc
->tline
= pc
->linenr
;
1530 pc
->tt
= JIM_TT_CMD
;
1535 static int JimParseQuote(struct JimParserCtx
*pc
)
1537 pc
->tstart
= pc
->p
+ 1;
1538 pc
->tline
= pc
->linenr
;
1539 pc
->tt
= JimParseSubQuote(pc
);
1543 static int JimParseVar(struct JimParserCtx
*pc
)
1549 #ifdef EXPRSUGAR_BRACKET
1550 if (*pc
->p
== '[') {
1551 /* Parse $[...] expr shorthand syntax */
1553 pc
->tt
= JIM_TT_EXPRSUGAR
;
1559 pc
->tt
= JIM_TT_VAR
;
1560 pc
->tline
= pc
->linenr
;
1562 if (*pc
->p
== '{') {
1563 pc
->tstart
= ++pc
->p
;
1566 while (pc
->len
&& *pc
->p
!= '}') {
1567 if (*pc
->p
== '\n') {
1573 pc
->tend
= pc
->p
- 1;
1581 /* Skip double colon, but not single colon! */
1582 if (pc
->p
[0] == ':' && pc
->p
[1] == ':') {
1583 while (*pc
->p
== ':') {
1589 /* Note that any char >= 0x80 must be part of a utf-8 char.
1590 * We consider all unicode points outside of ASCII as letters
1592 if (isalnum(UCHAR(*pc
->p
)) || *pc
->p
== '_' || UCHAR(*pc
->p
) >= 0x80) {
1599 /* Parse [dict get] syntax sugar. */
1600 if (*pc
->p
== '(') {
1602 const char *paren
= NULL
;
1604 pc
->tt
= JIM_TT_DICTSUGAR
;
1606 while (count
&& pc
->len
) {
1609 if (*pc
->p
== '\\' && pc
->len
>= 1) {
1613 else if (*pc
->p
== '(') {
1616 else if (*pc
->p
== ')') {
1626 /* Did not find a matching paren. Back up */
1628 pc
->len
+= (pc
->p
- paren
);
1631 #ifndef EXPRSUGAR_BRACKET
1632 if (*pc
->tstart
== '(') {
1633 pc
->tt
= JIM_TT_EXPRSUGAR
;
1637 pc
->tend
= pc
->p
- 1;
1639 /* Check if we parsed just the '$' character.
1640 * That's not a variable so an error is returned
1641 * to tell the state machine to consider this '$' just
1643 if (pc
->tstart
== pc
->p
) {
1651 static int JimParseStr(struct JimParserCtx
*pc
)
1653 if (pc
->tt
== JIM_TT_SEP
|| pc
->tt
== JIM_TT_EOL
||
1654 pc
->tt
== JIM_TT_NONE
|| pc
->tt
== JIM_TT_STR
) {
1655 /* Starting a new word */
1656 if (*pc
->p
== '{') {
1657 return JimParseBrace(pc
);
1659 if (*pc
->p
== '"') {
1663 /* In case the end quote is missing */
1664 pc
->missing
.line
= pc
->tline
;
1668 pc
->tline
= pc
->linenr
;
1672 pc
->missing
.ch
= '"';
1674 pc
->tend
= pc
->p
- 1;
1675 pc
->tt
= JIM_TT_ESC
;
1680 if (!pc
->inquote
&& *(pc
->p
+ 1) == '\n') {
1681 pc
->tend
= pc
->p
- 1;
1682 pc
->tt
= JIM_TT_ESC
;
1686 if (*(pc
->p
+ 1) == '\n') {
1692 else if (pc
->len
== 1) {
1693 /* End of script with trailing backslash */
1694 pc
->missing
.ch
= '\\';
1698 /* If the following token is not '$' just keep going */
1699 if (pc
->len
> 1 && pc
->p
[1] != '$') {
1704 /* Only need a separate ')' token if the previous was a var */
1705 if (*pc
->p
== '(' || pc
->tt
== JIM_TT_VAR
) {
1706 if (pc
->p
== pc
->tstart
) {
1707 /* At the start of the token, so just return this char */
1711 pc
->tend
= pc
->p
- 1;
1712 pc
->tt
= JIM_TT_ESC
;
1719 pc
->tend
= pc
->p
- 1;
1720 pc
->tt
= JIM_TT_ESC
;
1729 pc
->tend
= pc
->p
- 1;
1730 pc
->tt
= JIM_TT_ESC
;
1733 else if (*pc
->p
== '\n') {
1739 pc
->tend
= pc
->p
- 1;
1740 pc
->tt
= JIM_TT_ESC
;
1751 return JIM_OK
; /* unreached */
1754 static int JimParseComment(struct JimParserCtx
*pc
)
1757 if (*pc
->p
== '\\') {
1761 pc
->missing
.ch
= '\\';
1764 if (*pc
->p
== '\n') {
1768 else if (*pc
->p
== '\n') {
1780 /* xdigitval and odigitval are helper functions for JimEscape() */
1781 static int xdigitval(int c
)
1783 if (c
>= '0' && c
<= '9')
1785 if (c
>= 'a' && c
<= 'f')
1786 return c
- 'a' + 10;
1787 if (c
>= 'A' && c
<= 'F')
1788 return c
- 'A' + 10;
1792 static int odigitval(int c
)
1794 if (c
>= '0' && c
<= '7')
1799 /* Perform Tcl escape substitution of 's', storing the result
1800 * string into 'dest'. The escaped string is guaranteed to
1801 * be the same length or shorter than the source string.
1802 * slen is the length of the string at 's'.
1804 * The function returns the length of the resulting string. */
1805 static int JimEscape(char *dest
, const char *s
, int slen
)
1810 for (i
= 0; i
< slen
; i
++) {
1841 /* A unicode or hex sequence.
1842 * \x Expect 1-2 hex chars and convert to hex.
1843 * \u Expect 1-4 hex chars and convert to utf-8.
1844 * \U Expect 1-8 hex chars and convert to utf-8.
1845 * \u{NNN} supports 1-6 hex chars and convert to utf-8.
1846 * An invalid sequence means simply the escaped char.
1858 else if (s
[i
] == 'u') {
1859 if (s
[i
+ 1] == '{') {
1868 for (k
= 0; k
< maxchars
; k
++) {
1869 int c
= xdigitval(s
[i
+ k
+ 1]);
1873 val
= (val
<< 4) | c
;
1875 /* The \u{nnn} syntax supports up to 21 bit codepoints. */
1877 if (k
== 0 || val
> 0x1fffff || s
[i
+ k
+ 1] != '}') {
1883 /* Skip the closing brace */
1888 /* Got a valid sequence, so convert */
1893 p
+= utf8_fromunicode(p
, val
);
1898 /* Not a valid codepoint, just an escaped char */
1911 /* Replace all spaces and tabs after backslash newline with a single space*/
1915 } while (s
[i
+ 1] == ' ' || s
[i
+ 1] == '\t');
1928 int c
= odigitval(s
[i
+ 1]);
1931 c
= odigitval(s
[i
+ 2]);
1937 val
= (val
* 8) + c
;
1938 c
= odigitval(s
[i
+ 3]);
1944 val
= (val
* 8) + c
;
1965 /* Returns a dynamically allocated copy of the current token in the
1966 * parser context. The function performs conversion of escapes if
1967 * the token is of type JIM_TT_ESC.
1969 * Note that after the conversion, tokens that are grouped with
1970 * braces in the source code, are always recognizable from the
1971 * identical string obtained in a different way from the type.
1973 * For example the string:
1977 * will return as first token "*", of type JIM_TT_STR
1983 * will return as first token "*", of type JIM_TT_ESC
1985 static Jim_Obj
*JimParserGetTokenObj(Jim_Interp
*interp
, struct JimParserCtx
*pc
)
1987 const char *start
, *end
;
1993 len
= (end
- start
) + 1;
1997 token
= Jim_Alloc(len
+ 1);
1998 if (pc
->tt
!= JIM_TT_ESC
) {
1999 /* No escape conversion needed? Just copy it. */
2000 memcpy(token
, start
, len
);
2004 /* Else convert the escape chars. */
2005 len
= JimEscape(token
, start
, len
);
2008 return Jim_NewStringObjNoAlloc(interp
, token
, len
);
2011 /* -----------------------------------------------------------------------------
2013 * ---------------------------------------------------------------------------*/
2014 static int JimParseListSep(struct JimParserCtx
*pc
);
2015 static int JimParseListStr(struct JimParserCtx
*pc
);
2016 static int JimParseListQuote(struct JimParserCtx
*pc
);
2018 static int JimParseList(struct JimParserCtx
*pc
)
2020 if (isspace(UCHAR(*pc
->p
))) {
2021 return JimParseListSep(pc
);
2025 return JimParseListQuote(pc
);
2028 return JimParseBrace(pc
);
2032 return JimParseListStr(pc
);
2037 pc
->tstart
= pc
->tend
= pc
->p
;
2038 pc
->tline
= pc
->linenr
;
2039 pc
->tt
= JIM_TT_EOL
;
2044 static int JimParseListSep(struct JimParserCtx
*pc
)
2047 pc
->tline
= pc
->linenr
;
2048 while (isspace(UCHAR(*pc
->p
))) {
2049 if (*pc
->p
== '\n') {
2055 pc
->tend
= pc
->p
- 1;
2056 pc
->tt
= JIM_TT_SEP
;
2060 static int JimParseListQuote(struct JimParserCtx
*pc
)
2066 pc
->tline
= pc
->linenr
;
2067 pc
->tt
= JIM_TT_STR
;
2072 pc
->tt
= JIM_TT_ESC
;
2073 if (--pc
->len
== 0) {
2074 /* Trailing backslash */
2084 pc
->tend
= pc
->p
- 1;
2093 pc
->tend
= pc
->p
- 1;
2097 static int JimParseListStr(struct JimParserCtx
*pc
)
2100 pc
->tline
= pc
->linenr
;
2101 pc
->tt
= JIM_TT_STR
;
2104 if (isspace(UCHAR(*pc
->p
))) {
2105 pc
->tend
= pc
->p
- 1;
2108 if (*pc
->p
== '\\') {
2109 if (--pc
->len
== 0) {
2110 /* Trailing backslash */
2114 pc
->tt
= JIM_TT_ESC
;
2120 pc
->tend
= pc
->p
- 1;
2124 /* -----------------------------------------------------------------------------
2125 * Jim_Obj related functions
2126 * ---------------------------------------------------------------------------*/
2128 /* Return a new initialized object. */
2129 Jim_Obj
*Jim_NewObj(Jim_Interp
*interp
)
2133 /* -- Check if there are objects in the free list -- */
2134 if (interp
->freeList
!= NULL
) {
2135 /* -- Unlink the object from the free list -- */
2136 objPtr
= interp
->freeList
;
2137 interp
->freeList
= objPtr
->nextObjPtr
;
2140 /* -- No ready to use objects: allocate a new one -- */
2141 objPtr
= Jim_Alloc(sizeof(*objPtr
));
2144 /* Object is returned with refCount of 0. Every
2145 * kind of GC implemented should take care to avoid
2146 * scanning objects with refCount == 0. */
2147 objPtr
->refCount
= 0;
2148 /* All the other fields are left uninitialized to save time.
2149 * The caller will probably want to set them to the right
2152 /* -- Put the object into the live list -- */
2153 objPtr
->prevObjPtr
= NULL
;
2154 objPtr
->nextObjPtr
= interp
->liveList
;
2155 if (interp
->liveList
)
2156 interp
->liveList
->prevObjPtr
= objPtr
;
2157 interp
->liveList
= objPtr
;
2162 /* Free an object. Actually objects are never freed, but
2163 * just moved to the free objects list, where they will be
2164 * reused by Jim_NewObj(). */
2165 void Jim_FreeObj(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
2167 /* Check if the object was already freed, panic. */
2168 JimPanic((objPtr
->refCount
!= 0, "!!!Object %p freed with bad refcount %d, type=%s", objPtr
,
2169 objPtr
->refCount
, objPtr
->typePtr
? objPtr
->typePtr
->name
: "<none>"));
2171 /* Free the internal representation */
2172 Jim_FreeIntRep(interp
, objPtr
);
2173 /* Free the string representation */
2174 if (objPtr
->bytes
!= NULL
) {
2175 if (objPtr
->bytes
!= JimEmptyStringRep
)
2176 Jim_Free(objPtr
->bytes
);
2178 /* Unlink the object from the live objects list */
2179 if (objPtr
->prevObjPtr
)
2180 objPtr
->prevObjPtr
->nextObjPtr
= objPtr
->nextObjPtr
;
2181 if (objPtr
->nextObjPtr
)
2182 objPtr
->nextObjPtr
->prevObjPtr
= objPtr
->prevObjPtr
;
2183 if (interp
->liveList
== objPtr
)
2184 interp
->liveList
= objPtr
->nextObjPtr
;
2185 #ifdef JIM_DISABLE_OBJECT_POOL
2188 /* Link the object into the free objects list */
2189 objPtr
->prevObjPtr
= NULL
;
2190 objPtr
->nextObjPtr
= interp
->freeList
;
2191 if (interp
->freeList
)
2192 interp
->freeList
->prevObjPtr
= objPtr
;
2193 interp
->freeList
= objPtr
;
2194 objPtr
->refCount
= -1;
2198 /* Invalidate the string representation of an object. */
2199 void Jim_InvalidateStringRep(Jim_Obj
*objPtr
)
2201 if (objPtr
->bytes
!= NULL
) {
2202 if (objPtr
->bytes
!= JimEmptyStringRep
)
2203 Jim_Free(objPtr
->bytes
);
2205 objPtr
->bytes
= NULL
;
2208 /* Duplicate an object. The returned object has refcount = 0. */
2209 Jim_Obj
*Jim_DuplicateObj(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
2213 dupPtr
= Jim_NewObj(interp
);
2214 if (objPtr
->bytes
== NULL
) {
2215 /* Object does not have a valid string representation. */
2216 dupPtr
->bytes
= NULL
;
2218 else if (objPtr
->length
== 0) {
2219 /* Zero length, so don't even bother with the type-specific dup,
2220 * since all zero length objects look the same
2222 dupPtr
->bytes
= JimEmptyStringRep
;
2224 dupPtr
->typePtr
= NULL
;
2228 dupPtr
->bytes
= Jim_Alloc(objPtr
->length
+ 1);
2229 dupPtr
->length
= objPtr
->length
;
2230 /* Copy the null byte too */
2231 memcpy(dupPtr
->bytes
, objPtr
->bytes
, objPtr
->length
+ 1);
2234 /* By default, the new object has the same type as the old object */
2235 dupPtr
->typePtr
= objPtr
->typePtr
;
2236 if (objPtr
->typePtr
!= NULL
) {
2237 if (objPtr
->typePtr
->dupIntRepProc
== NULL
) {
2238 dupPtr
->internalRep
= objPtr
->internalRep
;
2241 /* The dup proc may set a different type, e.g. NULL */
2242 objPtr
->typePtr
->dupIntRepProc(interp
, objPtr
, dupPtr
);
2248 /* Return the string representation for objPtr. If the object's
2249 * string representation is invalid, calls the updateStringProc method to create
2250 * a new one from the internal representation of the object.
2252 const char *Jim_GetString(Jim_Obj
*objPtr
, int *lenPtr
)
2254 if (objPtr
->bytes
== NULL
) {
2255 /* Invalid string repr. Generate it. */
2256 JimPanic((objPtr
->typePtr
->updateStringProc
== NULL
, "UpdateStringProc called against '%s' type.", objPtr
->typePtr
->name
));
2257 objPtr
->typePtr
->updateStringProc(objPtr
);
2260 *lenPtr
= objPtr
->length
;
2261 return objPtr
->bytes
;
2264 /* Just returns the length (in bytes) of the object's string rep */
2265 int Jim_Length(Jim_Obj
*objPtr
)
2267 if (objPtr
->bytes
== NULL
) {
2268 /* Invalid string repr. Generate it. */
2269 Jim_GetString(objPtr
, NULL
);
2271 return objPtr
->length
;
2274 /* Just returns object's string rep */
2275 const char *Jim_String(Jim_Obj
*objPtr
)
2277 if (objPtr
->bytes
== NULL
) {
2278 /* Invalid string repr. Generate it. */
2279 Jim_GetString(objPtr
, NULL
);
2281 return objPtr
->bytes
;
2284 static void JimSetStringBytes(Jim_Obj
*objPtr
, const char *str
)
2286 objPtr
->bytes
= Jim_StrDup(str
);
2287 objPtr
->length
= strlen(str
);
2290 static void FreeDictSubstInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
);
2291 static void DupDictSubstInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
);
2293 static const Jim_ObjType dictSubstObjType
= {
2294 "dict-substitution",
2295 FreeDictSubstInternalRep
,
2296 DupDictSubstInternalRep
,
2301 static void FreeInterpolatedInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
);
2302 static void DupInterpolatedInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
);
2304 static const Jim_ObjType interpolatedObjType
= {
2306 FreeInterpolatedInternalRep
,
2307 DupInterpolatedInternalRep
,
2312 static void FreeInterpolatedInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
2314 Jim_DecrRefCount(interp
, objPtr
->internalRep
.dictSubstValue
.indexObjPtr
);
2317 static void DupInterpolatedInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
)
2319 /* Copy the interal rep */
2320 dupPtr
->internalRep
= srcPtr
->internalRep
;
2321 /* Need to increment the key ref count */
2322 Jim_IncrRefCount(dupPtr
->internalRep
.dictSubstValue
.indexObjPtr
);
2325 /* -----------------------------------------------------------------------------
2327 * ---------------------------------------------------------------------------*/
2328 static void DupStringInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
);
2329 static int SetStringFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
);
2331 static const Jim_ObjType stringObjType
= {
2334 DupStringInternalRep
,
2336 JIM_TYPE_REFERENCES
,
2339 static void DupStringInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
)
2341 JIM_NOTUSED(interp
);
2343 /* This is a bit subtle: the only caller of this function
2344 * should be Jim_DuplicateObj(), that will copy the
2345 * string representaion. After the copy, the duplicated
2346 * object will not have more room in the buffer than
2347 * srcPtr->length bytes. So we just set it to length. */
2348 dupPtr
->internalRep
.strValue
.maxLength
= srcPtr
->length
;
2349 dupPtr
->internalRep
.strValue
.charLength
= srcPtr
->internalRep
.strValue
.charLength
;
2352 static int SetStringFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
2354 if (objPtr
->typePtr
!= &stringObjType
) {
2355 /* Get a fresh string representation. */
2356 if (objPtr
->bytes
== NULL
) {
2357 /* Invalid string repr. Generate it. */
2358 JimPanic((objPtr
->typePtr
->updateStringProc
== NULL
, "UpdateStringProc called against '%s' type.", objPtr
->typePtr
->name
));
2359 objPtr
->typePtr
->updateStringProc(objPtr
);
2361 /* Free any other internal representation. */
2362 Jim_FreeIntRep(interp
, objPtr
);
2363 /* Set it as string, i.e. just set the maxLength field. */
2364 objPtr
->typePtr
= &stringObjType
;
2365 objPtr
->internalRep
.strValue
.maxLength
= objPtr
->length
;
2366 /* Don't know the utf-8 length yet */
2367 objPtr
->internalRep
.strValue
.charLength
= -1;
2373 * Returns the length of the object string in chars, not bytes.
2375 * These may be different for a utf-8 string.
2377 int Jim_Utf8Length(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
2380 SetStringFromAny(interp
, objPtr
);
2382 if (objPtr
->internalRep
.strValue
.charLength
< 0) {
2383 objPtr
->internalRep
.strValue
.charLength
= utf8_strlen(objPtr
->bytes
, objPtr
->length
);
2385 return objPtr
->internalRep
.strValue
.charLength
;
2387 return Jim_Length(objPtr
);
2391 /* len is in bytes -- see also Jim_NewStringObjUtf8() */
2392 Jim_Obj
*Jim_NewStringObj(Jim_Interp
*interp
, const char *s
, int len
)
2394 Jim_Obj
*objPtr
= Jim_NewObj(interp
);
2396 /* Need to find out how many bytes the string requires */
2399 /* Alloc/Set the string rep. */
2401 objPtr
->bytes
= JimEmptyStringRep
;
2404 objPtr
->bytes
= Jim_StrDupLen(s
, len
);
2406 objPtr
->length
= len
;
2408 /* No typePtr field for the vanilla string object. */
2409 objPtr
->typePtr
= NULL
;
2413 /* charlen is in characters -- see also Jim_NewStringObj() */
2414 Jim_Obj
*Jim_NewStringObjUtf8(Jim_Interp
*interp
, const char *s
, int charlen
)
2417 /* Need to find out how many bytes the string requires */
2418 int bytelen
= utf8_index(s
, charlen
);
2420 Jim_Obj
*objPtr
= Jim_NewStringObj(interp
, s
, bytelen
);
2422 /* Remember the utf8 length, so set the type */
2423 objPtr
->typePtr
= &stringObjType
;
2424 objPtr
->internalRep
.strValue
.maxLength
= bytelen
;
2425 objPtr
->internalRep
.strValue
.charLength
= charlen
;
2429 return Jim_NewStringObj(interp
, s
, charlen
);
2433 /* This version does not try to duplicate the 's' pointer, but
2434 * use it directly. */
2435 Jim_Obj
*Jim_NewStringObjNoAlloc(Jim_Interp
*interp
, char *s
, int len
)
2437 Jim_Obj
*objPtr
= Jim_NewObj(interp
);
2440 objPtr
->length
= (len
== -1) ? strlen(s
) : len
;
2441 objPtr
->typePtr
= NULL
;
2445 /* Low-level string append. Use it only against unshared objects
2446 * of type "string". */
2447 static void StringAppendString(Jim_Obj
*objPtr
, const char *str
, int len
)
2453 needlen
= objPtr
->length
+ len
;
2454 if (objPtr
->internalRep
.strValue
.maxLength
< needlen
||
2455 objPtr
->internalRep
.strValue
.maxLength
== 0) {
2457 /* Inefficient to malloc() for less than 8 bytes */
2461 if (objPtr
->bytes
== JimEmptyStringRep
) {
2462 objPtr
->bytes
= Jim_Alloc(needlen
+ 1);
2465 objPtr
->bytes
= Jim_Realloc(objPtr
->bytes
, needlen
+ 1);
2467 objPtr
->internalRep
.strValue
.maxLength
= needlen
;
2469 memcpy(objPtr
->bytes
+ objPtr
->length
, str
, len
);
2470 objPtr
->bytes
[objPtr
->length
+ len
] = '\0';
2472 if (objPtr
->internalRep
.strValue
.charLength
>= 0) {
2473 /* Update the utf-8 char length */
2474 objPtr
->internalRep
.strValue
.charLength
+= utf8_strlen(objPtr
->bytes
+ objPtr
->length
, len
);
2476 objPtr
->length
+= len
;
2479 /* Higher level API to append strings to objects.
2480 * Object must not be unshared for each of these.
2482 void Jim_AppendString(Jim_Interp
*interp
, Jim_Obj
*objPtr
, const char *str
, int len
)
2484 JimPanic((Jim_IsShared(objPtr
), "Jim_AppendString called with shared object"));
2485 SetStringFromAny(interp
, objPtr
);
2486 StringAppendString(objPtr
, str
, len
);
2489 void Jim_AppendObj(Jim_Interp
*interp
, Jim_Obj
*objPtr
, Jim_Obj
*appendObjPtr
)
2492 const char *str
= Jim_GetString(appendObjPtr
, &len
);
2493 Jim_AppendString(interp
, objPtr
, str
, len
);
2496 void Jim_AppendStrings(Jim_Interp
*interp
, Jim_Obj
*objPtr
, ...)
2500 SetStringFromAny(interp
, objPtr
);
2501 va_start(ap
, objPtr
);
2503 const char *s
= va_arg(ap
, const char *);
2507 Jim_AppendString(interp
, objPtr
, s
, -1);
2512 int Jim_StringEqObj(Jim_Obj
*aObjPtr
, Jim_Obj
*bObjPtr
)
2514 if (aObjPtr
== bObjPtr
) {
2519 const char *sA
= Jim_GetString(aObjPtr
, &Alen
);
2520 const char *sB
= Jim_GetString(bObjPtr
, &Blen
);
2522 return Alen
== Blen
&& memcmp(sA
, sB
, Alen
) == 0;
2527 * Note. Does not support embedded nulls in either the pattern or the object.
2529 int Jim_StringMatchObj(Jim_Interp
*interp
, Jim_Obj
*patternObjPtr
, Jim_Obj
*objPtr
, int nocase
)
2531 return JimGlobMatch(Jim_String(patternObjPtr
), Jim_String(objPtr
), nocase
);
2534 int Jim_StringCompareObj(Jim_Interp
*interp
, Jim_Obj
*firstObjPtr
, Jim_Obj
*secondObjPtr
, int nocase
)
2536 const char *s1
= Jim_String(firstObjPtr
);
2537 int l1
= Jim_Utf8Length(interp
, firstObjPtr
);
2538 const char *s2
= Jim_String(secondObjPtr
);
2539 int l2
= Jim_Utf8Length(interp
, secondObjPtr
);
2540 return JimStringCompareUtf8(s1
, l1
, s2
, l2
, nocase
);
2543 /* Convert a range, as returned by Jim_GetRange(), into
2544 * an absolute index into an object of the specified length.
2545 * This function may return negative values, or values
2546 * greater than or equal to the length of the list if the index
2547 * is out of range. */
2548 static int JimRelToAbsIndex(int len
, int idx
)
2555 /* Convert a pair of indexes (*firstPtr, *lastPtr) as normalized by JimRelToAbsIndex(),
2556 * into a form suitable for implementation of commands like [string range] and [lrange].
2558 * The resulting range is guaranteed to address valid elements of
2561 static void JimRelToAbsRange(int len
, int *firstPtr
, int *lastPtr
, int *rangeLenPtr
)
2565 if (*firstPtr
> *lastPtr
) {
2569 rangeLen
= *lastPtr
- *firstPtr
+ 1;
2571 if (*firstPtr
< 0) {
2572 rangeLen
+= *firstPtr
;
2575 if (*lastPtr
>= len
) {
2576 rangeLen
-= (*lastPtr
- (len
- 1));
2584 *rangeLenPtr
= rangeLen
;
2587 static int JimStringGetRange(Jim_Interp
*interp
, Jim_Obj
*firstObjPtr
, Jim_Obj
*lastObjPtr
,
2588 int len
, int *first
, int *last
, int *range
)
2590 if (Jim_GetIndex(interp
, firstObjPtr
, first
) != JIM_OK
) {
2593 if (Jim_GetIndex(interp
, lastObjPtr
, last
) != JIM_OK
) {
2596 *first
= JimRelToAbsIndex(len
, *first
);
2597 *last
= JimRelToAbsIndex(len
, *last
);
2598 JimRelToAbsRange(len
, first
, last
, range
);
2602 Jim_Obj
*Jim_StringByteRangeObj(Jim_Interp
*interp
,
2603 Jim_Obj
*strObjPtr
, Jim_Obj
*firstObjPtr
, Jim_Obj
*lastObjPtr
)
2610 str
= Jim_GetString(strObjPtr
, &bytelen
);
2612 if (JimStringGetRange(interp
, firstObjPtr
, lastObjPtr
, bytelen
, &first
, &last
, &rangeLen
) != JIM_OK
) {
2616 if (first
== 0 && rangeLen
== bytelen
) {
2619 return Jim_NewStringObj(interp
, str
+ first
, rangeLen
);
2622 Jim_Obj
*Jim_StringRangeObj(Jim_Interp
*interp
,
2623 Jim_Obj
*strObjPtr
, Jim_Obj
*firstObjPtr
, Jim_Obj
*lastObjPtr
)
2631 str
= Jim_GetString(strObjPtr
, &bytelen
);
2632 len
= Jim_Utf8Length(interp
, strObjPtr
);
2634 if (JimStringGetRange(interp
, firstObjPtr
, lastObjPtr
, len
, &first
, &last
, &rangeLen
) != JIM_OK
) {
2638 if (first
== 0 && rangeLen
== len
) {
2641 if (len
== bytelen
) {
2642 /* ASCII optimisation */
2643 return Jim_NewStringObj(interp
, str
+ first
, rangeLen
);
2645 return Jim_NewStringObjUtf8(interp
, str
+ utf8_index(str
, first
), rangeLen
);
2647 return Jim_StringByteRangeObj(interp
, strObjPtr
, firstObjPtr
, lastObjPtr
);
2651 Jim_Obj
*JimStringReplaceObj(Jim_Interp
*interp
,
2652 Jim_Obj
*strObjPtr
, Jim_Obj
*firstObjPtr
, Jim_Obj
*lastObjPtr
, Jim_Obj
*newStrObj
)
2659 len
= Jim_Utf8Length(interp
, strObjPtr
);
2661 if (JimStringGetRange(interp
, firstObjPtr
, lastObjPtr
, len
, &first
, &last
, &rangeLen
) != JIM_OK
) {
2669 str
= Jim_String(strObjPtr
);
2672 objPtr
= Jim_NewStringObjUtf8(interp
, str
, first
);
2676 Jim_AppendObj(interp
, objPtr
, newStrObj
);
2680 Jim_AppendString(interp
, objPtr
, str
+ utf8_index(str
, last
+ 1), len
- last
- 1);
2686 * Note: does not support embedded nulls.
2688 static void JimStrCopyUpperLower(char *dest
, const char *str
, int uc
)
2692 str
+= utf8_tounicode(str
, &c
);
2693 dest
+= utf8_getchars(dest
, uc
? utf8_upper(c
) : utf8_lower(c
));
2699 * Note: does not support embedded nulls.
2701 static Jim_Obj
*JimStringToLower(Jim_Interp
*interp
, Jim_Obj
*strObjPtr
)
2707 str
= Jim_GetString(strObjPtr
, &len
);
2710 /* Case mapping can change the utf-8 length of the string.
2711 * But at worst it will be by one extra byte per char
2715 buf
= Jim_Alloc(len
+ 1);
2716 JimStrCopyUpperLower(buf
, str
, 0);
2717 return Jim_NewStringObjNoAlloc(interp
, buf
, -1);
2721 * Note: does not support embedded nulls.
2723 static Jim_Obj
*JimStringToUpper(Jim_Interp
*interp
, Jim_Obj
*strObjPtr
)
2729 str
= Jim_GetString(strObjPtr
, &len
);
2732 /* Case mapping can change the utf-8 length of the string.
2733 * But at worst it will be by one extra byte per char
2737 buf
= Jim_Alloc(len
+ 1);
2738 JimStrCopyUpperLower(buf
, str
, 1);
2739 return Jim_NewStringObjNoAlloc(interp
, buf
, -1);
2743 * Note: does not support embedded nulls.
2745 static Jim_Obj
*JimStringToTitle(Jim_Interp
*interp
, Jim_Obj
*strObjPtr
)
2752 str
= Jim_GetString(strObjPtr
, &len
);
2755 /* Case mapping can change the utf-8 length of the string.
2756 * But at worst it will be by one extra byte per char
2760 buf
= p
= Jim_Alloc(len
+ 1);
2762 str
+= utf8_tounicode(str
, &c
);
2763 p
+= utf8_getchars(p
, utf8_title(c
));
2765 JimStrCopyUpperLower(p
, str
, 0);
2767 return Jim_NewStringObjNoAlloc(interp
, buf
, -1);
2770 /* Similar to memchr() except searches a UTF-8 string 'str' of byte length 'len'
2771 * for unicode character 'c'.
2772 * Returns the position if found or NULL if not
2774 static const char *utf8_memchr(const char *str
, int len
, int c
)
2779 int n
= utf8_tounicode(str
, &sc
);
2788 return memchr(str
, c
, len
);
2793 * Searches for the first non-trim char in string (str, len)
2795 * If none is found, returns just past the last char.
2797 * Lengths are in bytes.
2799 static const char *JimFindTrimLeft(const char *str
, int len
, const char *trimchars
, int trimlen
)
2803 int n
= utf8_tounicode(str
, &c
);
2805 if (utf8_memchr(trimchars
, trimlen
, c
) == NULL
) {
2806 /* Not a trim char, so stop */
2816 * Searches backwards for a non-trim char in string (str, len).
2818 * Returns a pointer to just after the non-trim char, or NULL if not found.
2820 * Lengths are in bytes.
2822 static const char *JimFindTrimRight(const char *str
, int len
, const char *trimchars
, int trimlen
)
2828 int n
= utf8_prev_len(str
, len
);
2833 n
= utf8_tounicode(str
, &c
);
2835 if (utf8_memchr(trimchars
, trimlen
, c
) == NULL
) {
2843 static const char default_trim_chars
[] = " \t\n\r";
2844 /* sizeof() here includes the null byte */
2845 static int default_trim_chars_len
= sizeof(default_trim_chars
);
2847 static Jim_Obj
*JimStringTrimLeft(Jim_Interp
*interp
, Jim_Obj
*strObjPtr
, Jim_Obj
*trimcharsObjPtr
)
2850 const char *str
= Jim_GetString(strObjPtr
, &len
);
2851 const char *trimchars
= default_trim_chars
;
2852 int trimcharslen
= default_trim_chars_len
;
2855 if (trimcharsObjPtr
) {
2856 trimchars
= Jim_GetString(trimcharsObjPtr
, &trimcharslen
);
2859 newstr
= JimFindTrimLeft(str
, len
, trimchars
, trimcharslen
);
2860 if (newstr
== str
) {
2864 return Jim_NewStringObj(interp
, newstr
, len
- (newstr
- str
));
2867 static Jim_Obj
*JimStringTrimRight(Jim_Interp
*interp
, Jim_Obj
*strObjPtr
, Jim_Obj
*trimcharsObjPtr
)
2870 const char *trimchars
= default_trim_chars
;
2871 int trimcharslen
= default_trim_chars_len
;
2872 const char *nontrim
;
2874 if (trimcharsObjPtr
) {
2875 trimchars
= Jim_GetString(trimcharsObjPtr
, &trimcharslen
);
2878 SetStringFromAny(interp
, strObjPtr
);
2880 len
= Jim_Length(strObjPtr
);
2881 nontrim
= JimFindTrimRight(strObjPtr
->bytes
, len
, trimchars
, trimcharslen
);
2883 if (nontrim
== NULL
) {
2884 /* All trim, so return a zero-length string */
2885 return Jim_NewEmptyStringObj(interp
);
2887 if (nontrim
== strObjPtr
->bytes
+ len
) {
2888 /* All non-trim, so return the original object */
2892 if (Jim_IsShared(strObjPtr
)) {
2893 strObjPtr
= Jim_NewStringObj(interp
, strObjPtr
->bytes
, (nontrim
- strObjPtr
->bytes
));
2896 /* Can modify this string in place */
2897 strObjPtr
->bytes
[nontrim
- strObjPtr
->bytes
] = 0;
2898 strObjPtr
->length
= (nontrim
- strObjPtr
->bytes
);
2904 static Jim_Obj
*JimStringTrim(Jim_Interp
*interp
, Jim_Obj
*strObjPtr
, Jim_Obj
*trimcharsObjPtr
)
2906 /* First trim left. */
2907 Jim_Obj
*objPtr
= JimStringTrimLeft(interp
, strObjPtr
, trimcharsObjPtr
);
2909 /* Now trim right */
2910 strObjPtr
= JimStringTrimRight(interp
, objPtr
, trimcharsObjPtr
);
2912 /* Note: refCount check is needed since objPtr may be emptyObj */
2913 if (objPtr
!= strObjPtr
&& objPtr
->refCount
== 0) {
2914 /* We don't want this object to be leaked */
2915 Jim_FreeNewObj(interp
, objPtr
);
2921 /* Some platforms don't have isascii - need a non-macro version */
2923 #define jim_isascii isascii
2925 static int jim_isascii(int c
)
2927 return !(c
& ~0x7f);
2931 static int JimStringIs(Jim_Interp
*interp
, Jim_Obj
*strObjPtr
, Jim_Obj
*strClass
, int strict
)
2933 static const char * const strclassnames
[] = {
2934 "integer", "alpha", "alnum", "ascii", "digit",
2935 "double", "lower", "upper", "space", "xdigit",
2936 "control", "print", "graph", "punct", "boolean",
2940 STR_IS_INTEGER
, STR_IS_ALPHA
, STR_IS_ALNUM
, STR_IS_ASCII
, STR_IS_DIGIT
,
2941 STR_IS_DOUBLE
, STR_IS_LOWER
, STR_IS_UPPER
, STR_IS_SPACE
, STR_IS_XDIGIT
,
2942 STR_IS_CONTROL
, STR_IS_PRINT
, STR_IS_GRAPH
, STR_IS_PUNCT
, STR_IS_BOOLEAN
,
2948 int (*isclassfunc
)(int c
) = NULL
;
2950 if (Jim_GetEnum(interp
, strClass
, strclassnames
, &strclass
, "class", JIM_ERRMSG
| JIM_ENUM_ABBREV
) != JIM_OK
) {
2954 str
= Jim_GetString(strObjPtr
, &len
);
2956 Jim_SetResultBool(interp
, !strict
);
2961 case STR_IS_INTEGER
:
2964 Jim_SetResultBool(interp
, JimGetWideNoErr(interp
, strObjPtr
, &w
) == JIM_OK
);
2971 Jim_SetResultBool(interp
, Jim_GetDouble(interp
, strObjPtr
, &d
) == JIM_OK
&& errno
!= ERANGE
);
2975 case STR_IS_BOOLEAN
:
2978 Jim_SetResultBool(interp
, Jim_GetBoolean(interp
, strObjPtr
, &b
) == JIM_OK
);
2982 case STR_IS_ALPHA
: isclassfunc
= isalpha
; break;
2983 case STR_IS_ALNUM
: isclassfunc
= isalnum
; break;
2984 case STR_IS_ASCII
: isclassfunc
= jim_isascii
; break;
2985 case STR_IS_DIGIT
: isclassfunc
= isdigit
; break;
2986 case STR_IS_LOWER
: isclassfunc
= islower
; break;
2987 case STR_IS_UPPER
: isclassfunc
= isupper
; break;
2988 case STR_IS_SPACE
: isclassfunc
= isspace
; break;
2989 case STR_IS_XDIGIT
: isclassfunc
= isxdigit
; break;
2990 case STR_IS_CONTROL
: isclassfunc
= iscntrl
; break;
2991 case STR_IS_PRINT
: isclassfunc
= isprint
; break;
2992 case STR_IS_GRAPH
: isclassfunc
= isgraph
; break;
2993 case STR_IS_PUNCT
: isclassfunc
= ispunct
; break;
2998 for (i
= 0; i
< len
; i
++) {
2999 if (!isclassfunc(UCHAR(str
[i
]))) {
3000 Jim_SetResultBool(interp
, 0);
3004 Jim_SetResultBool(interp
, 1);
3008 /* -----------------------------------------------------------------------------
3009 * Compared String Object
3010 * ---------------------------------------------------------------------------*/
3012 /* This is strange object that allows comparison of a C literal string
3013 * with a Jim object in a very short time if the same comparison is done
3014 * multiple times. For example every time the [if] command is executed,
3015 * Jim has to check if a given argument is "else".
3016 * If the code has no errors, this comparison is true most of the time,
3017 * so we can cache the pointer of the string of the last matching
3018 * comparison inside the object. Because most C compilers perform literal sharing,
3019 * so that: char *x = "foo", char *y = "foo", will lead to x == y,
3020 * this works pretty well even if comparisons are at different places
3021 * inside the C code. */
3023 static const Jim_ObjType comparedStringObjType
= {
3028 JIM_TYPE_REFERENCES
,
3031 /* The only way this object is exposed to the API is via the following
3032 * function. Returns true if the string and the object string repr.
3033 * are the same, otherwise zero is returned.
3035 * Note: this isn't binary safe, but it hardly needs to be.*/
3036 int Jim_CompareStringImmediate(Jim_Interp
*interp
, Jim_Obj
*objPtr
, const char *str
)
3038 if (objPtr
->typePtr
== &comparedStringObjType
&& objPtr
->internalRep
.ptr
== str
) {
3042 if (strcmp(str
, Jim_String(objPtr
)) != 0)
3045 if (objPtr
->typePtr
!= &comparedStringObjType
) {
3046 Jim_FreeIntRep(interp
, objPtr
);
3047 objPtr
->typePtr
= &comparedStringObjType
;
3049 objPtr
->internalRep
.ptr
= (char *)str
; /*ATTENTION: const cast */
3054 static int qsortCompareStringPointers(const void *a
, const void *b
)
3056 char *const *sa
= (char *const *)a
;
3057 char *const *sb
= (char *const *)b
;
3059 return strcmp(*sa
, *sb
);
3063 /* -----------------------------------------------------------------------------
3066 * This object is just a string from the language point of view, but
3067 * the internal representation contains the filename and line number
3068 * where this token was read. This information is used by
3069 * Jim_EvalObj() if the object passed happens to be of type "source".
3071 * This allows propagation of the information about line numbers and file
3072 * names and gives error messages with absolute line numbers.
3074 * Note that this object uses the internal representation of the Jim_Object,
3075 * so there is almost no memory overhead. (One Jim_Obj for each filename).
3077 * Also the object will be converted to something else if the given
3078 * token it represents in the source file is not something to be
3079 * evaluated (not a script), and will be specialized in some other way,
3080 * so the time overhead is also almost zero.
3081 * ---------------------------------------------------------------------------*/
3083 static void FreeSourceInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
);
3084 static void DupSourceInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
);
3086 static const Jim_ObjType sourceObjType
= {
3088 FreeSourceInternalRep
,
3089 DupSourceInternalRep
,
3091 JIM_TYPE_REFERENCES
,
3094 void FreeSourceInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
3096 Jim_DecrRefCount(interp
, objPtr
->internalRep
.sourceValue
.fileNameObj
);
3099 void DupSourceInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
)
3101 dupPtr
->internalRep
.sourceValue
= srcPtr
->internalRep
.sourceValue
;
3102 Jim_IncrRefCount(dupPtr
->internalRep
.sourceValue
.fileNameObj
);
3105 static void JimSetSourceInfo(Jim_Interp
*interp
, Jim_Obj
*objPtr
,
3106 Jim_Obj
*fileNameObj
, int lineNumber
)
3108 JimPanic((Jim_IsShared(objPtr
), "JimSetSourceInfo called with shared object"));
3109 JimPanic((objPtr
->typePtr
!= NULL
, "JimSetSourceInfo called with typed object"));
3110 Jim_IncrRefCount(fileNameObj
);
3111 objPtr
->internalRep
.sourceValue
.fileNameObj
= fileNameObj
;
3112 objPtr
->internalRep
.sourceValue
.lineNumber
= lineNumber
;
3113 objPtr
->typePtr
= &sourceObjType
;
3116 /* -----------------------------------------------------------------------------
3119 * This object is used only in the Script internal represenation.
3120 * For each line of the script, it holds the number of tokens on the line
3121 * and the source line number.
3123 static const Jim_ObjType scriptLineObjType
= {
3131 static Jim_Obj
*JimNewScriptLineObj(Jim_Interp
*interp
, int argc
, int line
)
3135 #ifdef DEBUG_SHOW_SCRIPT
3137 snprintf(buf
, sizeof(buf
), "line=%d, argc=%d", line
, argc
);
3138 objPtr
= Jim_NewStringObj(interp
, buf
, -1);
3140 objPtr
= Jim_NewEmptyStringObj(interp
);
3142 objPtr
->typePtr
= &scriptLineObjType
;
3143 objPtr
->internalRep
.scriptLineValue
.argc
= argc
;
3144 objPtr
->internalRep
.scriptLineValue
.line
= line
;
3149 /* -----------------------------------------------------------------------------
3152 * This object holds the parsed internal representation of a script.
3153 * This representation is help within an allocated ScriptObj (see below)
3155 static void FreeScriptInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
);
3156 static void DupScriptInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
);
3158 static const Jim_ObjType scriptObjType
= {
3160 FreeScriptInternalRep
,
3161 DupScriptInternalRep
,
3163 JIM_TYPE_REFERENCES
,
3166 /* Each token of a script is represented by a ScriptToken.
3167 * The ScriptToken contains a type and a Jim_Obj. The Jim_Obj
3168 * can be specialized by commands operating on it.
3170 typedef struct ScriptToken
3176 /* This is the script object internal representation. An array of
3177 * ScriptToken structures, including a pre-computed representation of the
3178 * command length and arguments.
3180 * For example the script:
3183 * set $i $x$y [foo]BAR
3185 * will produce a ScriptObj with the following ScriptToken's:
3200 * "puts hello" has two args (LIN 2), composed of single tokens.
3201 * (Note that the WRD token is omitted for the common case of a single token.)
3203 * "set $i $x$y [foo]BAR" has four (LIN 4) args, the first word
3204 * has 1 token (ESC SET), and the last has two tokens (WRD 2 CMD foo ESC BAR)
3206 * The precomputation of the command structure makes Jim_Eval() faster,
3207 * and simpler because there aren't dynamic lengths / allocations.
3209 * -- {expand}/{*} handling --
3211 * Expand is handled in a special way.
3213 * If a "word" begins with {*}, the word token count is -ve.
3215 * For example the command:
3219 * Will produce the following cmdstruct array:
3226 * Note that the 'LIN' token also contains the source information for the
3227 * first word of the line for error reporting purposes
3229 * -- the substFlags field of the structure --
3231 * The scriptObj structure is used to represent both "script" objects
3232 * and "subst" objects. In the second case, there are no LIN and WRD
3233 * tokens. Instead SEP and EOL tokens are added as-is.
3234 * In addition, the field 'substFlags' is used to represent the flags used to turn
3235 * the string into the internal representation.
3236 * If these flags do not match what the application requires,
3237 * the scriptObj is created again. For example the script:
3239 * subst -nocommands $string
3240 * subst -novariables $string
3242 * Will (re)create the internal representation of the $string object
3245 typedef struct ScriptObj
3247 ScriptToken
*token
; /* Tokens array. */
3248 Jim_Obj
*fileNameObj
; /* Filename */
3249 int len
; /* Length of token[] */
3250 int substFlags
; /* flags used for the compilation of "subst" objects */
3251 int inUse
; /* Used to share a ScriptObj. Currently
3252 only used by Jim_EvalObj() as protection against
3253 shimmering of the currently evaluated object. */
3254 int firstline
; /* Line number of the first line */
3255 int linenr
; /* Error line number, if any */
3256 int missing
; /* Missing char if script failed to parse, (or space or backslash if OK) */
3259 static void JimSetScriptFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
);
3260 static int JimParseCheckMissing(Jim_Interp
*interp
, int ch
);
3261 static ScriptObj
*JimGetScript(Jim_Interp
*interp
, Jim_Obj
*objPtr
);
3263 void FreeScriptInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
3266 struct ScriptObj
*script
= (void *)objPtr
->internalRep
.ptr
;
3268 if (--script
->inUse
!= 0)
3270 for (i
= 0; i
< script
->len
; i
++) {
3271 Jim_DecrRefCount(interp
, script
->token
[i
].objPtr
);
3273 Jim_Free(script
->token
);
3274 Jim_DecrRefCount(interp
, script
->fileNameObj
);
3278 void DupScriptInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
)
3280 JIM_NOTUSED(interp
);
3281 JIM_NOTUSED(srcPtr
);
3283 /* Just return a simple string. We don't try to preserve the source info
3284 * since in practice scripts are never duplicated
3286 dupPtr
->typePtr
= NULL
;
3289 /* A simple parse token.
3290 * As the script is parsed, the created tokens point into the script string rep.
3294 const char *token
; /* Pointer to the start of the token */
3295 int len
; /* Length of this token */
3296 int type
; /* Token type */
3297 int line
; /* Line number */
3300 /* A list of parsed tokens representing a script.
3301 * Tokens are added to this list as the script is parsed.
3302 * It grows as needed.
3306 /* Start with a statically allocated list of tokens which will be expanded with realloc if needed */
3307 ParseToken
*list
; /* Array of tokens */
3308 int size
; /* Current size of the list */
3309 int count
; /* Number of entries used */
3310 ParseToken static_list
[20]; /* Small initial token space to avoid allocation */
3313 static void ScriptTokenListInit(ParseTokenList
*tokenlist
)
3315 tokenlist
->list
= tokenlist
->static_list
;
3316 tokenlist
->size
= sizeof(tokenlist
->static_list
) / sizeof(ParseToken
);
3317 tokenlist
->count
= 0;
3320 static void ScriptTokenListFree(ParseTokenList
*tokenlist
)
3322 if (tokenlist
->list
!= tokenlist
->static_list
) {
3323 Jim_Free(tokenlist
->list
);
3328 * Adds the new token to the tokenlist.
3329 * The token has the given length, type and line number.
3330 * The token list is resized as necessary.
3332 static void ScriptAddToken(ParseTokenList
*tokenlist
, const char *token
, int len
, int type
,
3337 if (tokenlist
->count
== tokenlist
->size
) {
3338 /* Resize the list */
3339 tokenlist
->size
*= 2;
3340 if (tokenlist
->list
!= tokenlist
->static_list
) {
3342 Jim_Realloc(tokenlist
->list
, tokenlist
->size
* sizeof(*tokenlist
->list
));
3345 /* The list needs to become allocated */
3346 tokenlist
->list
= Jim_Alloc(tokenlist
->size
* sizeof(*tokenlist
->list
));
3347 memcpy(tokenlist
->list
, tokenlist
->static_list
,
3348 tokenlist
->count
* sizeof(*tokenlist
->list
));
3351 t
= &tokenlist
->list
[tokenlist
->count
++];
3358 /* Counts the number of adjoining non-separator tokens.
3360 * Returns -ve if the first token is the expansion
3361 * operator (in which case the count doesn't include
3364 static int JimCountWordTokens(struct ScriptObj
*script
, ParseToken
*t
)
3369 /* Is the first word {*} or {expand}? */
3370 if (t
->type
== JIM_TT_STR
&& !TOKEN_IS_SEP(t
[1].type
)) {
3371 if ((t
->len
== 1 && *t
->token
== '*') || (t
->len
== 6 && strncmp(t
->token
, "expand", 6) == 0)) {
3372 /* Create an expand token */
3377 if (script
->missing
== ' ') {
3378 /* This is a "extra characters after close-brace" error. Report the first error */
3379 script
->missing
= '}';
3380 script
->linenr
= t
[1].line
;
3385 /* Now count non-separator words */
3386 while (!TOKEN_IS_SEP(t
->type
)) {
3391 return count
* expand
;
3395 * Create a script/subst object from the given token.
3397 static Jim_Obj
*JimMakeScriptObj(Jim_Interp
*interp
, const ParseToken
*t
)
3401 if (t
->type
== JIM_TT_ESC
&& memchr(t
->token
, '\\', t
->len
) != NULL
) {
3402 /* Convert backlash escapes. The result will never be longer than the original */
3404 char *str
= Jim_Alloc(len
+ 1);
3405 len
= JimEscape(str
, t
->token
, len
);
3406 objPtr
= Jim_NewStringObjNoAlloc(interp
, str
, len
);
3409 /* XXX: For strict Tcl compatibility, JIM_TT_STR should replace <backslash><newline><whitespace>
3410 * with a single space.
3412 objPtr
= Jim_NewStringObj(interp
, t
->token
, t
->len
);
3418 * Takes a tokenlist and creates the allocated list of script tokens
3419 * in script->token, of length script->len.
3421 * Unnecessary tokens are discarded, and LINE and WORD tokens are inserted
3424 * Also sets script->line to the line number of the first token
3426 static void ScriptObjAddTokens(Jim_Interp
*interp
, struct ScriptObj
*script
,
3427 ParseTokenList
*tokenlist
)
3430 struct ScriptToken
*token
;
3431 /* Number of tokens so far for the current command */
3433 /* This is the first token for the current command */
3434 ScriptToken
*linefirst
;
3438 #ifdef DEBUG_SHOW_SCRIPT_TOKENS
3439 printf("==== Tokens ====\n");
3440 for (i
= 0; i
< tokenlist
->count
; i
++) {
3441 printf("[%2d]@%d %s '%.*s'\n", i
, tokenlist
->list
[i
].line
, jim_tt_name(tokenlist
->list
[i
].type
),
3442 tokenlist
->list
[i
].len
, tokenlist
->list
[i
].token
);
3446 /* May need up to one extra script token for each EOL in the worst case */
3447 count
= tokenlist
->count
;
3448 for (i
= 0; i
< tokenlist
->count
; i
++) {
3449 if (tokenlist
->list
[i
].type
== JIM_TT_EOL
) {
3453 linenr
= script
->firstline
= tokenlist
->list
[0].line
;
3455 token
= script
->token
= Jim_Alloc(sizeof(ScriptToken
) * count
);
3457 /* This is the first token for the current command */
3458 linefirst
= token
++;
3460 for (i
= 0; i
< tokenlist
->count
; ) {
3461 /* Look ahead to find out how many tokens make up the next word */
3464 /* Skip any leading separators */
3465 while (tokenlist
->list
[i
].type
== JIM_TT_SEP
) {
3469 wordtokens
= JimCountWordTokens(script
, tokenlist
->list
+ i
);
3471 if (wordtokens
== 0) {
3472 /* None, so at end of line */
3474 linefirst
->type
= JIM_TT_LINE
;
3475 linefirst
->objPtr
= JimNewScriptLineObj(interp
, lineargs
, linenr
);
3476 Jim_IncrRefCount(linefirst
->objPtr
);
3478 /* Reset for new line */
3480 linefirst
= token
++;
3485 else if (wordtokens
!= 1) {
3486 /* More than 1, or {*}, so insert a WORD token */
3487 token
->type
= JIM_TT_WORD
;
3488 token
->objPtr
= Jim_NewIntObj(interp
, wordtokens
);
3489 Jim_IncrRefCount(token
->objPtr
);
3491 if (wordtokens
< 0) {
3492 /* Skip the expand token */
3494 wordtokens
= -wordtokens
- 1;
3499 if (lineargs
== 0) {
3500 /* First real token on the line, so record the line number */
3501 linenr
= tokenlist
->list
[i
].line
;
3505 /* Add each non-separator word token to the line */
3506 while (wordtokens
--) {
3507 const ParseToken
*t
= &tokenlist
->list
[i
++];
3509 token
->type
= t
->type
;
3510 token
->objPtr
= JimMakeScriptObj(interp
, t
);
3511 Jim_IncrRefCount(token
->objPtr
);
3513 /* Every object is initially a string of type 'source', but the
3514 * internal type may be specialized during execution of the
3516 JimSetSourceInfo(interp
, token
->objPtr
, script
->fileNameObj
, t
->line
);
3521 if (lineargs
== 0) {
3525 script
->len
= token
- script
->token
;
3527 JimPanic((script
->len
>= count
, "allocated script array is too short"));
3529 #ifdef DEBUG_SHOW_SCRIPT
3530 printf("==== Script (%s) ====\n", Jim_String(script
->fileNameObj
));
3531 for (i
= 0; i
< script
->len
; i
++) {
3532 const ScriptToken
*t
= &script
->token
[i
];
3533 printf("[%2d] %s %s\n", i
, jim_tt_name(t
->type
), Jim_String(t
->objPtr
));
3539 /* Parses the given string object to determine if it represents a complete script.
3541 * This is useful for interactive shells implementation, for [info complete].
3543 * If 'stateCharPtr' != NULL, the function stores ' ' on complete script,
3544 * '{' on scripts incomplete missing one or more '}' to be balanced.
3545 * '[' on scripts incomplete missing one or more ']' to be balanced.
3546 * '"' on scripts incomplete missing a '"' char.
3547 * '\\' on scripts with a trailing backslash.
3549 * If the script is complete, 1 is returned, otherwise 0.
3551 * If the script has extra characters after a close brace, this still returns 1,
3552 * but sets *stateCharPtr to '}'
3553 * Evaluating the script will give the error "extra characters after close-brace".
3555 int Jim_ScriptIsComplete(Jim_Interp
*interp
, Jim_Obj
*scriptObj
, char *stateCharPtr
)
3557 ScriptObj
*script
= JimGetScript(interp
, scriptObj
);
3559 *stateCharPtr
= script
->missing
;
3561 return script
->missing
== ' ' || script
->missing
== '}';
3565 * Sets an appropriate error message for a missing script/expression terminator.
3567 * Returns JIM_ERR if 'ch' represents an unmatched/missing character.
3569 * Note that a trailing backslash is not considered to be an error.
3571 static int JimParseCheckMissing(Jim_Interp
*interp
, int ch
)
3581 msg
= "unmatched \"[\"";
3584 msg
= "missing close-brace";
3587 msg
= "extra characters after close-brace";
3591 msg
= "missing quote";
3595 Jim_SetResultString(interp
, msg
, -1);
3600 * Similar to ScriptObjAddTokens(), but for subst objects.
3602 static void SubstObjAddTokens(Jim_Interp
*interp
, struct ScriptObj
*script
,
3603 ParseTokenList
*tokenlist
)
3606 struct ScriptToken
*token
;
3608 token
= script
->token
= Jim_Alloc(sizeof(ScriptToken
) * tokenlist
->count
);
3610 for (i
= 0; i
< tokenlist
->count
; i
++) {
3611 const ParseToken
*t
= &tokenlist
->list
[i
];
3613 /* Create a token for 't' */
3614 token
->type
= t
->type
;
3615 token
->objPtr
= JimMakeScriptObj(interp
, t
);
3616 Jim_IncrRefCount(token
->objPtr
);
3623 /* This method takes the string representation of an object
3624 * as a Tcl script, and generates the pre-parsed internal representation
3627 * On parse error, sets an error message and returns JIM_ERR
3628 * (Note: the object is still converted to a script, even if an error occurs)
3630 static void JimSetScriptFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
)
3633 const char *scriptText
= Jim_GetString(objPtr
, &scriptTextLen
);
3634 struct JimParserCtx parser
;
3635 struct ScriptObj
*script
;
3636 ParseTokenList tokenlist
;
3639 /* Try to get information about filename / line number */
3640 if (objPtr
->typePtr
== &sourceObjType
) {
3641 line
= objPtr
->internalRep
.sourceValue
.lineNumber
;
3644 /* Initially parse the script into tokens (in tokenlist) */
3645 ScriptTokenListInit(&tokenlist
);
3647 JimParserInit(&parser
, scriptText
, scriptTextLen
, line
);
3648 while (!parser
.eof
) {
3649 JimParseScript(&parser
);
3650 ScriptAddToken(&tokenlist
, parser
.tstart
, parser
.tend
- parser
.tstart
+ 1, parser
.tt
,
3654 /* Add a final EOF token */
3655 ScriptAddToken(&tokenlist
, scriptText
+ scriptTextLen
, 0, JIM_TT_EOF
, 0);
3657 /* Create the "real" script tokens from the parsed tokens */
3658 script
= Jim_Alloc(sizeof(*script
));
3659 memset(script
, 0, sizeof(*script
));
3661 if (objPtr
->typePtr
== &sourceObjType
) {
3662 script
->fileNameObj
= objPtr
->internalRep
.sourceValue
.fileNameObj
;
3665 script
->fileNameObj
= interp
->emptyObj
;
3667 Jim_IncrRefCount(script
->fileNameObj
);
3668 script
->missing
= parser
.missing
.ch
;
3669 script
->linenr
= parser
.missing
.line
;
3671 ScriptObjAddTokens(interp
, script
, &tokenlist
);
3673 /* No longer need the token list */
3674 ScriptTokenListFree(&tokenlist
);
3676 /* Free the old internal rep and set the new one. */
3677 Jim_FreeIntRep(interp
, objPtr
);
3678 Jim_SetIntRepPtr(objPtr
, script
);
3679 objPtr
->typePtr
= &scriptObjType
;
3682 static void JimAddErrorToStack(Jim_Interp
*interp
, ScriptObj
*script
);
3685 * Returns the parsed script.
3686 * Note that if there is any possibility that the script is not valid,
3687 * call JimScriptValid() to check
3689 static ScriptObj
*JimGetScript(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
3691 if (objPtr
== interp
->emptyObj
) {
3692 /* Avoid converting emptyObj to a script. use nullScriptObj instead. */
3693 objPtr
= interp
->nullScriptObj
;
3696 if (objPtr
->typePtr
!= &scriptObjType
|| ((struct ScriptObj
*)Jim_GetIntRepPtr(objPtr
))->substFlags
) {
3697 JimSetScriptFromAny(interp
, objPtr
);
3700 return (ScriptObj
*)Jim_GetIntRepPtr(objPtr
);
3704 * Returns 1 if the script is valid (parsed ok), otherwise returns 0
3705 * and leaves an error message in the interp result.
3708 static int JimScriptValid(Jim_Interp
*interp
, ScriptObj
*script
)
3710 if (JimParseCheckMissing(interp
, script
->missing
) == JIM_ERR
) {
3711 JimAddErrorToStack(interp
, script
);
3718 /* -----------------------------------------------------------------------------
3720 * ---------------------------------------------------------------------------*/
3721 static void JimIncrCmdRefCount(Jim_Cmd
*cmdPtr
)
3726 static void JimDecrCmdRefCount(Jim_Interp
*interp
, Jim_Cmd
*cmdPtr
)
3728 if (--cmdPtr
->inUse
== 0) {
3729 if (cmdPtr
->isproc
) {
3730 Jim_DecrRefCount(interp
, cmdPtr
->u
.proc
.argListObjPtr
);
3731 Jim_DecrRefCount(interp
, cmdPtr
->u
.proc
.bodyObjPtr
);
3732 Jim_DecrRefCount(interp
, cmdPtr
->u
.proc
.nsObj
);
3733 if (cmdPtr
->u
.proc
.staticVars
) {
3734 Jim_FreeHashTable(cmdPtr
->u
.proc
.staticVars
);
3735 Jim_Free(cmdPtr
->u
.proc
.staticVars
);
3740 if (cmdPtr
->u
.native
.delProc
) {
3741 cmdPtr
->u
.native
.delProc(interp
, cmdPtr
->u
.native
.privData
);
3744 if (cmdPtr
->prevCmd
) {
3745 /* Delete any pushed command too */
3746 JimDecrCmdRefCount(interp
, cmdPtr
->prevCmd
);
3752 /* Variables HashTable Type.
3754 * Keys are dynamically allocated strings, Values are Jim_Var structures.
3756 static void JimVariablesHTValDestructor(void *interp
, void *val
)
3758 Jim_DecrRefCount(interp
, ((Jim_Var
*)val
)->objPtr
);
3762 static const Jim_HashTableType JimVariablesHashTableType
= {
3763 JimStringCopyHTHashFunction
, /* hash function */
3764 JimStringCopyHTDup
, /* key dup */
3766 JimStringCopyHTKeyCompare
, /* key compare */
3767 JimStringCopyHTKeyDestructor
, /* key destructor */
3768 JimVariablesHTValDestructor
/* val destructor */
3771 /* Commands HashTable Type.
3773 * Keys are dynamic allocated strings, Values are Jim_Cmd structures.
3775 static void JimCommandsHT_ValDestructor(void *interp
, void *val
)
3777 JimDecrCmdRefCount(interp
, val
);
3780 static const Jim_HashTableType JimCommandsHashTableType
= {
3781 JimStringCopyHTHashFunction
, /* hash function */
3782 JimStringCopyHTDup
, /* key dup */
3784 JimStringCopyHTKeyCompare
, /* key compare */
3785 JimStringCopyHTKeyDestructor
, /* key destructor */
3786 JimCommandsHT_ValDestructor
/* val destructor */
3789 /* ------------------------- Commands related functions --------------------- */
3791 #ifdef jim_ext_namespace
3793 * If nameObjPtr starts with "::", returns it.
3794 * Otherwise returns a new object with nameObjPtr prefixed with "::".
3795 * In this case, decrements the ref count of nameObjPtr.
3797 Jim_Obj
*Jim_MakeGlobalNamespaceName(Jim_Interp
*interp
, Jim_Obj
*nameObjPtr
)
3801 const char *name
= Jim_String(nameObjPtr
);
3802 if (name
[0] == ':' && name
[1] == ':') {
3805 Jim_IncrRefCount(nameObjPtr
);
3806 resultObj
= Jim_NewStringObj(interp
, "::", -1);
3807 Jim_AppendObj(interp
, resultObj
, nameObjPtr
);
3808 Jim_DecrRefCount(interp
, nameObjPtr
);
3814 * An efficient version of JimQualifyNameObj() where the name is
3815 * available (and needed) as a 'const char *'.
3816 * Avoids creating an object if not necessary.
3817 * The object stored in *objPtrPtr should be disposed of with JimFreeQualifiedName() after use.
3819 static const char *JimQualifyName(Jim_Interp
*interp
, const char *name
, Jim_Obj
**objPtrPtr
)
3821 Jim_Obj
*objPtr
= interp
->emptyObj
;
3823 if (name
[0] == ':' && name
[1] == ':') {
3824 /* This command is being defined in the global namespace */
3825 while (*++name
== ':') {
3828 else if (Jim_Length(interp
->framePtr
->nsObj
)) {
3829 /* This command is being defined in a non-global namespace */
3830 objPtr
= Jim_DuplicateObj(interp
, interp
->framePtr
->nsObj
);
3831 Jim_AppendStrings(interp
, objPtr
, "::", name
, NULL
);
3832 name
= Jim_String(objPtr
);
3834 Jim_IncrRefCount(objPtr
);
3835 *objPtrPtr
= objPtr
;
3839 #define JimFreeQualifiedName(INTERP, OBJ) Jim_DecrRefCount((INTERP), (OBJ))
3842 /* We can be more efficient in the no-namespace case */
3843 #define JimQualifyName(INTERP, NAME, DUMMY) (((NAME)[0] == ':' && (NAME)[1] == ':') ? (NAME) + 2 : (NAME))
3844 #define JimFreeQualifiedName(INTERP, DUMMY) (void)(DUMMY)
3846 Jim_Obj
*Jim_MakeGlobalNamespaceName(Jim_Interp
*interp
, Jim_Obj
*nameObjPtr
)
3852 static int JimCreateCommand(Jim_Interp
*interp
, const char *name
, Jim_Cmd
*cmd
)
3854 /* It may already exist, so we try to delete the old one.
3855 * Note that reference count means that it won't be deleted yet if
3856 * it exists in the call stack.
3858 * BUT, if 'local' is in force, instead of deleting the existing
3859 * proc, we stash a reference to the old proc here.
3861 Jim_HashEntry
*he
= Jim_FindHashEntry(&interp
->commands
, name
);
3863 /* There was an old cmd with the same name,
3864 * so this requires a 'proc epoch' update. */
3866 /* If a procedure with the same name didn't exist there is no need
3867 * to increment the 'proc epoch' because creation of a new procedure
3868 * can never affect existing cached commands. We don't do
3869 * negative caching. */
3870 Jim_InterpIncrProcEpoch(interp
);
3873 if (he
&& interp
->local
) {
3874 /* Push this command over the top of the previous one */
3875 cmd
->prevCmd
= Jim_GetHashEntryVal(he
);
3876 Jim_SetHashVal(&interp
->commands
, he
, cmd
);
3880 /* Replace the existing command */
3881 Jim_DeleteHashEntry(&interp
->commands
, name
);
3884 Jim_AddHashEntry(&interp
->commands
, name
, cmd
);
3890 int Jim_CreateCommand(Jim_Interp
*interp
, const char *cmdNameStr
,
3891 Jim_CmdProc
*cmdProc
, void *privData
, Jim_DelCmdProc
*delProc
)
3893 Jim_Cmd
*cmdPtr
= Jim_Alloc(sizeof(*cmdPtr
));
3895 /* Store the new details for this command */
3896 memset(cmdPtr
, 0, sizeof(*cmdPtr
));
3898 cmdPtr
->u
.native
.delProc
= delProc
;
3899 cmdPtr
->u
.native
.cmdProc
= cmdProc
;
3900 cmdPtr
->u
.native
.privData
= privData
;
3902 JimCreateCommand(interp
, cmdNameStr
, cmdPtr
);
3907 static int JimCreateProcedureStatics(Jim_Interp
*interp
, Jim_Cmd
*cmdPtr
, Jim_Obj
*staticsListObjPtr
)
3911 len
= Jim_ListLength(interp
, staticsListObjPtr
);
3916 cmdPtr
->u
.proc
.staticVars
= Jim_Alloc(sizeof(Jim_HashTable
));
3917 Jim_InitHashTable(cmdPtr
->u
.proc
.staticVars
, &JimVariablesHashTableType
, interp
);
3918 for (i
= 0; i
< len
; i
++) {
3919 Jim_Obj
*objPtr
, *initObjPtr
, *nameObjPtr
;
3923 objPtr
= Jim_ListGetIndex(interp
, staticsListObjPtr
, i
);
3924 /* Check if it's composed of two elements. */
3925 subLen
= Jim_ListLength(interp
, objPtr
);
3926 if (subLen
== 1 || subLen
== 2) {
3927 /* Try to get the variable value from the current
3929 nameObjPtr
= Jim_ListGetIndex(interp
, objPtr
, 0);
3931 initObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_NONE
);
3932 if (initObjPtr
== NULL
) {
3933 Jim_SetResultFormatted(interp
,
3934 "variable for initialization of static \"%#s\" not found in the local context",
3940 initObjPtr
= Jim_ListGetIndex(interp
, objPtr
, 1);
3942 if (JimValidName(interp
, "static variable", nameObjPtr
) != JIM_OK
) {
3946 varPtr
= Jim_Alloc(sizeof(*varPtr
));
3947 varPtr
->objPtr
= initObjPtr
;
3948 Jim_IncrRefCount(initObjPtr
);
3949 varPtr
->linkFramePtr
= NULL
;
3950 if (Jim_AddHashEntry(cmdPtr
->u
.proc
.staticVars
,
3951 Jim_String(nameObjPtr
), varPtr
) != JIM_OK
) {
3952 Jim_SetResultFormatted(interp
,
3953 "static variable name \"%#s\" duplicated in statics list", nameObjPtr
);
3954 Jim_DecrRefCount(interp
, initObjPtr
);
3960 Jim_SetResultFormatted(interp
, "too many fields in static specifier \"%#s\"",
3969 * If the command is a proc, sets/updates the cached namespace (nsObj)
3970 * based on the command name.
3972 static void JimUpdateProcNamespace(Jim_Interp
*interp
, Jim_Cmd
*cmdPtr
, const char *cmdname
)
3974 #ifdef jim_ext_namespace
3975 if (cmdPtr
->isproc
) {
3976 /* XXX: Really need JimNamespaceSplit() */
3977 const char *pt
= strrchr(cmdname
, ':');
3978 if (pt
&& pt
!= cmdname
&& pt
[-1] == ':') {
3979 Jim_DecrRefCount(interp
, cmdPtr
->u
.proc
.nsObj
);
3980 cmdPtr
->u
.proc
.nsObj
= Jim_NewStringObj(interp
, cmdname
, pt
- cmdname
- 1);
3981 Jim_IncrRefCount(cmdPtr
->u
.proc
.nsObj
);
3983 if (Jim_FindHashEntry(&interp
->commands
, pt
+ 1)) {
3984 /* This command shadows a global command, so a proc epoch update is required */
3985 Jim_InterpIncrProcEpoch(interp
);
3992 static Jim_Cmd
*JimCreateProcedureCmd(Jim_Interp
*interp
, Jim_Obj
*argListObjPtr
,
3993 Jim_Obj
*staticsListObjPtr
, Jim_Obj
*bodyObjPtr
, Jim_Obj
*nsObj
)
3999 argListLen
= Jim_ListLength(interp
, argListObjPtr
);
4001 /* Allocate space for both the command pointer and the arg list */
4002 cmdPtr
= Jim_Alloc(sizeof(*cmdPtr
) + sizeof(struct Jim_ProcArg
) * argListLen
);
4003 memset(cmdPtr
, 0, sizeof(*cmdPtr
));
4006 cmdPtr
->u
.proc
.argListObjPtr
= argListObjPtr
;
4007 cmdPtr
->u
.proc
.argListLen
= argListLen
;
4008 cmdPtr
->u
.proc
.bodyObjPtr
= bodyObjPtr
;
4009 cmdPtr
->u
.proc
.argsPos
= -1;
4010 cmdPtr
->u
.proc
.arglist
= (struct Jim_ProcArg
*)(cmdPtr
+ 1);
4011 cmdPtr
->u
.proc
.nsObj
= nsObj
? nsObj
: interp
->emptyObj
;
4012 Jim_IncrRefCount(argListObjPtr
);
4013 Jim_IncrRefCount(bodyObjPtr
);
4014 Jim_IncrRefCount(cmdPtr
->u
.proc
.nsObj
);
4016 /* Create the statics hash table. */
4017 if (staticsListObjPtr
&& JimCreateProcedureStatics(interp
, cmdPtr
, staticsListObjPtr
) != JIM_OK
) {
4021 /* Parse the args out into arglist, validating as we go */
4022 /* Examine the argument list for default parameters and 'args' */
4023 for (i
= 0; i
< argListLen
; i
++) {
4025 Jim_Obj
*nameObjPtr
;
4026 Jim_Obj
*defaultObjPtr
;
4029 /* Examine a parameter */
4030 argPtr
= Jim_ListGetIndex(interp
, argListObjPtr
, i
);
4031 len
= Jim_ListLength(interp
, argPtr
);
4033 Jim_SetResultString(interp
, "argument with no name", -1);
4035 JimDecrCmdRefCount(interp
, cmdPtr
);
4039 Jim_SetResultFormatted(interp
, "too many fields in argument specifier \"%#s\"", argPtr
);
4044 /* Optional parameter */
4045 nameObjPtr
= Jim_ListGetIndex(interp
, argPtr
, 0);
4046 defaultObjPtr
= Jim_ListGetIndex(interp
, argPtr
, 1);
4049 /* Required parameter */
4050 nameObjPtr
= argPtr
;
4051 defaultObjPtr
= NULL
;
4055 if (Jim_CompareStringImmediate(interp
, nameObjPtr
, "args")) {
4056 if (cmdPtr
->u
.proc
.argsPos
>= 0) {
4057 Jim_SetResultString(interp
, "'args' specified more than once", -1);
4060 cmdPtr
->u
.proc
.argsPos
= i
;
4064 cmdPtr
->u
.proc
.optArity
++;
4067 cmdPtr
->u
.proc
.reqArity
++;
4071 cmdPtr
->u
.proc
.arglist
[i
].nameObjPtr
= nameObjPtr
;
4072 cmdPtr
->u
.proc
.arglist
[i
].defaultObjPtr
= defaultObjPtr
;
4078 int Jim_DeleteCommand(Jim_Interp
*interp
, const char *name
)
4081 Jim_Obj
*qualifiedNameObj
;
4082 const char *qualname
= JimQualifyName(interp
, name
, &qualifiedNameObj
);
4084 if (Jim_DeleteHashEntry(&interp
->commands
, qualname
) == JIM_ERR
) {
4085 Jim_SetResultFormatted(interp
, "can't delete \"%s\": command doesn't exist", name
);
4089 Jim_InterpIncrProcEpoch(interp
);
4092 JimFreeQualifiedName(interp
, qualifiedNameObj
);
4097 int Jim_RenameCommand(Jim_Interp
*interp
, const char *oldName
, const char *newName
)
4102 Jim_Obj
*qualifiedOldNameObj
;
4103 Jim_Obj
*qualifiedNewNameObj
;
4107 if (newName
[0] == 0) {
4108 return Jim_DeleteCommand(interp
, oldName
);
4111 fqold
= JimQualifyName(interp
, oldName
, &qualifiedOldNameObj
);
4112 fqnew
= JimQualifyName(interp
, newName
, &qualifiedNewNameObj
);
4114 /* Does it exist? */
4115 he
= Jim_FindHashEntry(&interp
->commands
, fqold
);
4117 Jim_SetResultFormatted(interp
, "can't rename \"%s\": command doesn't exist", oldName
);
4119 else if (Jim_FindHashEntry(&interp
->commands
, fqnew
)) {
4120 Jim_SetResultFormatted(interp
, "can't rename to \"%s\": command already exists", newName
);
4123 cmdPtr
= Jim_GetHashEntryVal(he
);
4124 if (cmdPtr
->prevCmd
) {
4125 /* If the command replaced another command with 'local', renaming it
4126 * would break the usage of upcall, so don't allow it.
4128 Jim_SetResultFormatted(interp
, "can't rename local command \"%s\"", oldName
);
4131 /* Add the new name first */
4132 JimIncrCmdRefCount(cmdPtr
);
4133 JimUpdateProcNamespace(interp
, cmdPtr
, fqnew
);
4134 Jim_AddHashEntry(&interp
->commands
, fqnew
, cmdPtr
);
4136 /* Now remove the old name */
4137 Jim_DeleteHashEntry(&interp
->commands
, fqold
);
4139 /* Increment the epoch */
4140 Jim_InterpIncrProcEpoch(interp
);
4146 JimFreeQualifiedName(interp
, qualifiedOldNameObj
);
4147 JimFreeQualifiedName(interp
, qualifiedNewNameObj
);
4152 /* -----------------------------------------------------------------------------
4154 * ---------------------------------------------------------------------------*/
4156 static void FreeCommandInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
4158 Jim_DecrRefCount(interp
, objPtr
->internalRep
.cmdValue
.nsObj
);
4161 static void DupCommandInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
)
4163 dupPtr
->internalRep
.cmdValue
= srcPtr
->internalRep
.cmdValue
;
4164 dupPtr
->typePtr
= srcPtr
->typePtr
;
4165 Jim_IncrRefCount(dupPtr
->internalRep
.cmdValue
.nsObj
);
4168 static const Jim_ObjType commandObjType
= {
4170 FreeCommandInternalRep
,
4171 DupCommandInternalRep
,
4173 JIM_TYPE_REFERENCES
,
4176 /* This function returns the command structure for the command name
4177 * stored in objPtr. It specializes the objPtr to contain
4178 * cached info instead of performing the lookup into the hash table
4179 * every time. The information cached may not be up-to-date, in this
4180 * case the lookup is performed and the cache updated.
4182 * Respects the 'upcall' setting.
4184 Jim_Cmd
*Jim_GetCommand(Jim_Interp
*interp
, Jim_Obj
*objPtr
, int flags
)
4188 /* In order to be valid, the proc epoch must match and
4189 * the lookup must have occurred in the same namespace
4191 if (objPtr
->typePtr
!= &commandObjType
||
4192 objPtr
->internalRep
.cmdValue
.procEpoch
!= interp
->procEpoch
4193 #ifdef jim_ext_namespace
4194 || !Jim_StringEqObj(objPtr
->internalRep
.cmdValue
.nsObj
, interp
->framePtr
->nsObj
)
4197 /* Not cached or out of date, so lookup */
4199 /* Do we need to try the local namespace? */
4200 const char *name
= Jim_String(objPtr
);
4203 if (name
[0] == ':' && name
[1] == ':') {
4204 while (*++name
== ':') {
4207 #ifdef jim_ext_namespace
4208 else if (Jim_Length(interp
->framePtr
->nsObj
)) {
4209 /* This command is being defined in a non-global namespace */
4210 Jim_Obj
*nameObj
= Jim_DuplicateObj(interp
, interp
->framePtr
->nsObj
);
4211 Jim_AppendStrings(interp
, nameObj
, "::", name
, NULL
);
4212 he
= Jim_FindHashEntry(&interp
->commands
, Jim_String(nameObj
));
4213 Jim_FreeNewObj(interp
, nameObj
);
4220 /* Lookup in the global namespace */
4221 he
= Jim_FindHashEntry(&interp
->commands
, name
);
4223 if (flags
& JIM_ERRMSG
) {
4224 Jim_SetResultFormatted(interp
, "invalid command name \"%#s\"", objPtr
);
4228 #ifdef jim_ext_namespace
4231 cmd
= Jim_GetHashEntryVal(he
);
4233 /* Free the old internal rep and set the new one. */
4234 Jim_FreeIntRep(interp
, objPtr
);
4235 objPtr
->typePtr
= &commandObjType
;
4236 objPtr
->internalRep
.cmdValue
.procEpoch
= interp
->procEpoch
;
4237 objPtr
->internalRep
.cmdValue
.cmdPtr
= cmd
;
4238 objPtr
->internalRep
.cmdValue
.nsObj
= interp
->framePtr
->nsObj
;
4239 Jim_IncrRefCount(interp
->framePtr
->nsObj
);
4242 cmd
= objPtr
->internalRep
.cmdValue
.cmdPtr
;
4244 while (cmd
->u
.proc
.upcall
) {
4250 /* -----------------------------------------------------------------------------
4252 * ---------------------------------------------------------------------------*/
4254 /* -----------------------------------------------------------------------------
4256 * ---------------------------------------------------------------------------*/
4258 #define JIM_DICT_SUGAR 100 /* Only returned by SetVariableFromAny() */
4260 static int SetVariableFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
);
4262 static const Jim_ObjType variableObjType
= {
4267 JIM_TYPE_REFERENCES
,
4271 * Check that the name does not contain embedded nulls.
4273 * Variable and procedure names are manipulated as null terminated strings, so
4274 * don't allow names with embedded nulls.
4276 static int JimValidName(Jim_Interp
*interp
, const char *type
, Jim_Obj
*nameObjPtr
)
4278 /* Variable names and proc names can't contain embedded nulls */
4279 if (nameObjPtr
->typePtr
!= &variableObjType
) {
4281 const char *str
= Jim_GetString(nameObjPtr
, &len
);
4282 if (memchr(str
, '\0', len
)) {
4283 Jim_SetResultFormatted(interp
, "%s name contains embedded null", type
);
4290 /* This method should be called only by the variable API.
4291 * It returns JIM_OK on success (variable already exists),
4292 * JIM_ERR if it does not exist, JIM_DICT_SUGAR if it's not
4293 * a variable name, but syntax glue for [dict] i.e. the last
4294 * character is ')' */
4295 static int SetVariableFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
)
4297 const char *varName
;
4298 Jim_CallFrame
*framePtr
;
4303 /* Check if the object is already an uptodate variable */
4304 if (objPtr
->typePtr
== &variableObjType
) {
4305 framePtr
= objPtr
->internalRep
.varValue
.global
? interp
->topFramePtr
: interp
->framePtr
;
4306 if (objPtr
->internalRep
.varValue
.callFrameId
== framePtr
->id
) {
4310 /* Need to re-resolve the variable in the updated callframe */
4312 else if (objPtr
->typePtr
== &dictSubstObjType
) {
4313 return JIM_DICT_SUGAR
;
4315 else if (JimValidName(interp
, "variable", objPtr
) != JIM_OK
) {
4320 varName
= Jim_GetString(objPtr
, &len
);
4322 /* Make sure it's not syntax glue to get/set dict. */
4323 if (len
&& varName
[len
- 1] == ')' && strchr(varName
, '(') != NULL
) {
4324 return JIM_DICT_SUGAR
;
4327 if (varName
[0] == ':' && varName
[1] == ':') {
4328 while (*++varName
== ':') {
4331 framePtr
= interp
->topFramePtr
;
4335 framePtr
= interp
->framePtr
;
4338 /* Resolve this name in the variables hash table */
4339 he
= Jim_FindHashEntry(&framePtr
->vars
, varName
);
4341 if (!global
&& framePtr
->staticVars
) {
4342 /* Try with static vars. */
4343 he
= Jim_FindHashEntry(framePtr
->staticVars
, varName
);
4350 /* Free the old internal repr and set the new one. */
4351 Jim_FreeIntRep(interp
, objPtr
);
4352 objPtr
->typePtr
= &variableObjType
;
4353 objPtr
->internalRep
.varValue
.callFrameId
= framePtr
->id
;
4354 objPtr
->internalRep
.varValue
.varPtr
= Jim_GetHashEntryVal(he
);
4355 objPtr
->internalRep
.varValue
.global
= global
;
4359 /* -------------------- Variables related functions ------------------------- */
4360 static int JimDictSugarSet(Jim_Interp
*interp
, Jim_Obj
*ObjPtr
, Jim_Obj
*valObjPtr
);
4361 static Jim_Obj
*JimDictSugarGet(Jim_Interp
*interp
, Jim_Obj
*ObjPtr
, int flags
);
4363 static Jim_Var
*JimCreateVariable(Jim_Interp
*interp
, Jim_Obj
*nameObjPtr
, Jim_Obj
*valObjPtr
)
4366 Jim_CallFrame
*framePtr
;
4369 /* New variable to create */
4370 Jim_Var
*var
= Jim_Alloc(sizeof(*var
));
4372 var
->objPtr
= valObjPtr
;
4373 Jim_IncrRefCount(valObjPtr
);
4374 var
->linkFramePtr
= NULL
;
4376 name
= Jim_String(nameObjPtr
);
4377 if (name
[0] == ':' && name
[1] == ':') {
4378 while (*++name
== ':') {
4380 framePtr
= interp
->topFramePtr
;
4384 framePtr
= interp
->framePtr
;
4388 /* Insert the new variable */
4389 Jim_AddHashEntry(&framePtr
->vars
, name
, var
);
4391 /* Make the object int rep a variable */
4392 Jim_FreeIntRep(interp
, nameObjPtr
);
4393 nameObjPtr
->typePtr
= &variableObjType
;
4394 nameObjPtr
->internalRep
.varValue
.callFrameId
= framePtr
->id
;
4395 nameObjPtr
->internalRep
.varValue
.varPtr
= var
;
4396 nameObjPtr
->internalRep
.varValue
.global
= global
;
4401 /* For now that's dummy. Variables lookup should be optimized
4402 * in many ways, with caching of lookups, and possibly with
4403 * a table of pre-allocated vars in every CallFrame for local vars.
4404 * All the caching should also have an 'epoch' mechanism similar
4405 * to the one used by Tcl for procedures lookup caching. */
4408 * Set the variable nameObjPtr to value valObjptr.
4410 int Jim_SetVariable(Jim_Interp
*interp
, Jim_Obj
*nameObjPtr
, Jim_Obj
*valObjPtr
)
4415 switch (SetVariableFromAny(interp
, nameObjPtr
)) {
4416 case JIM_DICT_SUGAR
:
4417 return JimDictSugarSet(interp
, nameObjPtr
, valObjPtr
);
4420 if (JimValidName(interp
, "variable", nameObjPtr
) != JIM_OK
) {
4423 JimCreateVariable(interp
, nameObjPtr
, valObjPtr
);
4427 var
= nameObjPtr
->internalRep
.varValue
.varPtr
;
4428 if (var
->linkFramePtr
== NULL
) {
4429 Jim_IncrRefCount(valObjPtr
);
4430 Jim_DecrRefCount(interp
, var
->objPtr
);
4431 var
->objPtr
= valObjPtr
;
4433 else { /* Else handle the link */
4434 Jim_CallFrame
*savedCallFrame
;
4436 savedCallFrame
= interp
->framePtr
;
4437 interp
->framePtr
= var
->linkFramePtr
;
4438 err
= Jim_SetVariable(interp
, var
->objPtr
, valObjPtr
);
4439 interp
->framePtr
= savedCallFrame
;
4447 int Jim_SetVariableStr(Jim_Interp
*interp
, const char *name
, Jim_Obj
*objPtr
)
4449 Jim_Obj
*nameObjPtr
;
4452 nameObjPtr
= Jim_NewStringObj(interp
, name
, -1);
4453 Jim_IncrRefCount(nameObjPtr
);
4454 result
= Jim_SetVariable(interp
, nameObjPtr
, objPtr
);
4455 Jim_DecrRefCount(interp
, nameObjPtr
);
4459 int Jim_SetGlobalVariableStr(Jim_Interp
*interp
, const char *name
, Jim_Obj
*objPtr
)
4461 Jim_CallFrame
*savedFramePtr
;
4464 savedFramePtr
= interp
->framePtr
;
4465 interp
->framePtr
= interp
->topFramePtr
;
4466 result
= Jim_SetVariableStr(interp
, name
, objPtr
);
4467 interp
->framePtr
= savedFramePtr
;
4471 int Jim_SetVariableStrWithStr(Jim_Interp
*interp
, const char *name
, const char *val
)
4476 valObjPtr
= Jim_NewStringObj(interp
, val
, -1);
4477 Jim_IncrRefCount(valObjPtr
);
4478 result
= Jim_SetVariableStr(interp
, name
, valObjPtr
);
4479 Jim_DecrRefCount(interp
, valObjPtr
);
4483 int Jim_SetVariableLink(Jim_Interp
*interp
, Jim_Obj
*nameObjPtr
,
4484 Jim_Obj
*targetNameObjPtr
, Jim_CallFrame
*targetCallFrame
)
4486 const char *varName
;
4487 const char *targetName
;
4488 Jim_CallFrame
*framePtr
;
4491 /* Check for an existing variable or link */
4492 switch (SetVariableFromAny(interp
, nameObjPtr
)) {
4493 case JIM_DICT_SUGAR
:
4494 /* XXX: This message seem unnecessarily verbose, but it matches Tcl */
4495 Jim_SetResultFormatted(interp
, "bad variable name \"%#s\": upvar won't create a scalar variable that looks like an array element", nameObjPtr
);
4499 varPtr
= nameObjPtr
->internalRep
.varValue
.varPtr
;
4501 if (varPtr
->linkFramePtr
== NULL
) {
4502 Jim_SetResultFormatted(interp
, "variable \"%#s\" already exists", nameObjPtr
);
4506 /* It exists, but is a link, so first delete the link */
4507 varPtr
->linkFramePtr
= NULL
;
4511 /* Resolve the call frames for both variables */
4512 /* XXX: SetVariableFromAny() already did this! */
4513 varName
= Jim_String(nameObjPtr
);
4515 if (varName
[0] == ':' && varName
[1] == ':') {
4516 while (*++varName
== ':') {
4518 /* Linking a global var does nothing */
4519 framePtr
= interp
->topFramePtr
;
4522 framePtr
= interp
->framePtr
;
4525 targetName
= Jim_String(targetNameObjPtr
);
4526 if (targetName
[0] == ':' && targetName
[1] == ':') {
4527 while (*++targetName
== ':') {
4529 targetNameObjPtr
= Jim_NewStringObj(interp
, targetName
, -1);
4530 targetCallFrame
= interp
->topFramePtr
;
4532 Jim_IncrRefCount(targetNameObjPtr
);
4534 if (framePtr
->level
< targetCallFrame
->level
) {
4535 Jim_SetResultFormatted(interp
,
4536 "bad variable name \"%#s\": upvar won't create namespace variable that refers to procedure variable",
4538 Jim_DecrRefCount(interp
, targetNameObjPtr
);
4542 /* Check for cycles. */
4543 if (framePtr
== targetCallFrame
) {
4544 Jim_Obj
*objPtr
= targetNameObjPtr
;
4546 /* Cycles are only possible with 'uplevel 0' */
4548 if (strcmp(Jim_String(objPtr
), varName
) == 0) {
4549 Jim_SetResultString(interp
, "can't upvar from variable to itself", -1);
4550 Jim_DecrRefCount(interp
, targetNameObjPtr
);
4553 if (SetVariableFromAny(interp
, objPtr
) != JIM_OK
)
4555 varPtr
= objPtr
->internalRep
.varValue
.varPtr
;
4556 if (varPtr
->linkFramePtr
!= targetCallFrame
)
4558 objPtr
= varPtr
->objPtr
;
4562 /* Perform the binding */
4563 Jim_SetVariable(interp
, nameObjPtr
, targetNameObjPtr
);
4564 /* We are now sure 'nameObjPtr' type is variableObjType */
4565 nameObjPtr
->internalRep
.varValue
.varPtr
->linkFramePtr
= targetCallFrame
;
4566 Jim_DecrRefCount(interp
, targetNameObjPtr
);
4570 /* Return the Jim_Obj pointer associated with a variable name,
4571 * or NULL if the variable was not found in the current context.
4572 * The same optimization discussed in the comment to the
4573 * 'SetVariable' function should apply here.
4575 * If JIM_UNSHARED is set and the variable is an array element (dict sugar)
4576 * in a dictionary which is shared, the array variable value is duplicated first.
4577 * This allows the array element to be updated (e.g. append, lappend) without
4578 * affecting other references to the dictionary.
4580 Jim_Obj
*Jim_GetVariable(Jim_Interp
*interp
, Jim_Obj
*nameObjPtr
, int flags
)
4582 switch (SetVariableFromAny(interp
, nameObjPtr
)) {
4584 Jim_Var
*varPtr
= nameObjPtr
->internalRep
.varValue
.varPtr
;
4586 if (varPtr
->linkFramePtr
== NULL
) {
4587 return varPtr
->objPtr
;
4592 /* The variable is a link? Resolve it. */
4593 Jim_CallFrame
*savedCallFrame
= interp
->framePtr
;
4595 interp
->framePtr
= varPtr
->linkFramePtr
;
4596 objPtr
= Jim_GetVariable(interp
, varPtr
->objPtr
, flags
);
4597 interp
->framePtr
= savedCallFrame
;
4601 /* Error, so fall through to the error message */
4606 case JIM_DICT_SUGAR
:
4607 /* [dict] syntax sugar. */
4608 return JimDictSugarGet(interp
, nameObjPtr
, flags
);
4610 if (flags
& JIM_ERRMSG
) {
4611 Jim_SetResultFormatted(interp
, "can't read \"%#s\": no such variable", nameObjPtr
);
4616 Jim_Obj
*Jim_GetGlobalVariable(Jim_Interp
*interp
, Jim_Obj
*nameObjPtr
, int flags
)
4618 Jim_CallFrame
*savedFramePtr
;
4621 savedFramePtr
= interp
->framePtr
;
4622 interp
->framePtr
= interp
->topFramePtr
;
4623 objPtr
= Jim_GetVariable(interp
, nameObjPtr
, flags
);
4624 interp
->framePtr
= savedFramePtr
;
4629 Jim_Obj
*Jim_GetVariableStr(Jim_Interp
*interp
, const char *name
, int flags
)
4631 Jim_Obj
*nameObjPtr
, *varObjPtr
;
4633 nameObjPtr
= Jim_NewStringObj(interp
, name
, -1);
4634 Jim_IncrRefCount(nameObjPtr
);
4635 varObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, flags
);
4636 Jim_DecrRefCount(interp
, nameObjPtr
);
4640 Jim_Obj
*Jim_GetGlobalVariableStr(Jim_Interp
*interp
, const char *name
, int flags
)
4642 Jim_CallFrame
*savedFramePtr
;
4645 savedFramePtr
= interp
->framePtr
;
4646 interp
->framePtr
= interp
->topFramePtr
;
4647 objPtr
= Jim_GetVariableStr(interp
, name
, flags
);
4648 interp
->framePtr
= savedFramePtr
;
4653 /* Unset a variable.
4654 * Note: On success unset invalidates all the (cached) variable objects
4655 * by incrementing callFrameEpoch
4657 int Jim_UnsetVariable(Jim_Interp
*interp
, Jim_Obj
*nameObjPtr
, int flags
)
4661 Jim_CallFrame
*framePtr
;
4663 retval
= SetVariableFromAny(interp
, nameObjPtr
);
4664 if (retval
== JIM_DICT_SUGAR
) {
4665 /* [dict] syntax sugar. */
4666 return JimDictSugarSet(interp
, nameObjPtr
, NULL
);
4668 else if (retval
== JIM_OK
) {
4669 varPtr
= nameObjPtr
->internalRep
.varValue
.varPtr
;
4671 /* If it's a link call UnsetVariable recursively */
4672 if (varPtr
->linkFramePtr
) {
4673 framePtr
= interp
->framePtr
;
4674 interp
->framePtr
= varPtr
->linkFramePtr
;
4675 retval
= Jim_UnsetVariable(interp
, varPtr
->objPtr
, JIM_NONE
);
4676 interp
->framePtr
= framePtr
;
4679 const char *name
= Jim_String(nameObjPtr
);
4680 if (nameObjPtr
->internalRep
.varValue
.global
) {
4682 framePtr
= interp
->topFramePtr
;
4685 framePtr
= interp
->framePtr
;
4688 retval
= Jim_DeleteHashEntry(&framePtr
->vars
, name
);
4689 if (retval
== JIM_OK
) {
4690 /* Change the callframe id, invalidating var lookup caching */
4691 framePtr
->id
= interp
->callFrameEpoch
++;
4695 if (retval
!= JIM_OK
&& (flags
& JIM_ERRMSG
)) {
4696 Jim_SetResultFormatted(interp
, "can't unset \"%#s\": no such variable", nameObjPtr
);
4701 /* ---------- Dict syntax sugar (similar to array Tcl syntax) -------------- */
4703 /* Given a variable name for [dict] operation syntax sugar,
4704 * this function returns two objects, the first with the name
4705 * of the variable to set, and the second with the respective key.
4706 * For example "foo(bar)" will return objects with string repr. of
4709 * The returned objects have refcount = 1. The function can't fail. */
4710 static void JimDictSugarParseVarKey(Jim_Interp
*interp
, Jim_Obj
*objPtr
,
4711 Jim_Obj
**varPtrPtr
, Jim_Obj
**keyPtrPtr
)
4713 const char *str
, *p
;
4715 Jim_Obj
*varObjPtr
, *keyObjPtr
;
4717 str
= Jim_GetString(objPtr
, &len
);
4719 p
= strchr(str
, '(');
4720 JimPanic((p
== NULL
, "JimDictSugarParseVarKey() called for non-dict-sugar (%s)", str
));
4722 varObjPtr
= Jim_NewStringObj(interp
, str
, p
- str
);
4725 keyLen
= (str
+ len
) - p
;
4726 if (str
[len
- 1] == ')') {
4730 /* Create the objects with the variable name and key. */
4731 keyObjPtr
= Jim_NewStringObj(interp
, p
, keyLen
);
4733 Jim_IncrRefCount(varObjPtr
);
4734 Jim_IncrRefCount(keyObjPtr
);
4735 *varPtrPtr
= varObjPtr
;
4736 *keyPtrPtr
= keyObjPtr
;
4739 /* Helper of Jim_SetVariable() to deal with dict-syntax variable names.
4740 * Also used by Jim_UnsetVariable() with valObjPtr = NULL. */
4741 static int JimDictSugarSet(Jim_Interp
*interp
, Jim_Obj
*objPtr
, Jim_Obj
*valObjPtr
)
4745 SetDictSubstFromAny(interp
, objPtr
);
4747 err
= Jim_SetDictKeysVector(interp
, objPtr
->internalRep
.dictSubstValue
.varNameObjPtr
,
4748 &objPtr
->internalRep
.dictSubstValue
.indexObjPtr
, 1, valObjPtr
, JIM_MUSTEXIST
);
4750 if (err
== JIM_OK
) {
4751 /* Don't keep an extra ref to the result */
4752 Jim_SetEmptyResult(interp
);
4756 /* Better error message for unset a(2) where a exists but a(2) doesn't */
4757 if (Jim_GetVariable(interp
, objPtr
->internalRep
.dictSubstValue
.varNameObjPtr
, JIM_NONE
)) {
4758 Jim_SetResultFormatted(interp
, "can't unset \"%#s\": no such element in array",
4763 /* Make the error more informative and Tcl-compatible */
4764 Jim_SetResultFormatted(interp
, "can't %s \"%#s\": variable isn't array",
4765 (valObjPtr
? "set" : "unset"), objPtr
);
4771 * Expands the array variable (dict sugar) and returns the result, or NULL on error.
4773 * If JIM_UNSHARED is set and the dictionary is shared, it will be duplicated
4774 * and stored back to the variable before expansion.
4776 static Jim_Obj
*JimDictExpandArrayVariable(Jim_Interp
*interp
, Jim_Obj
*varObjPtr
,
4777 Jim_Obj
*keyObjPtr
, int flags
)
4779 Jim_Obj
*dictObjPtr
;
4780 Jim_Obj
*resObjPtr
= NULL
;
4783 dictObjPtr
= Jim_GetVariable(interp
, varObjPtr
, JIM_ERRMSG
);
4788 ret
= Jim_DictKey(interp
, dictObjPtr
, keyObjPtr
, &resObjPtr
, JIM_NONE
);
4789 if (ret
!= JIM_OK
) {
4790 Jim_SetResultFormatted(interp
,
4791 "can't read \"%#s(%#s)\": %s array", varObjPtr
, keyObjPtr
,
4792 ret
< 0 ? "variable isn't" : "no such element in");
4794 else if ((flags
& JIM_UNSHARED
) && Jim_IsShared(dictObjPtr
)) {
4795 /* Update the variable to have an unshared copy */
4796 Jim_SetVariable(interp
, varObjPtr
, Jim_DuplicateObj(interp
, dictObjPtr
));
4802 /* Helper of Jim_GetVariable() to deal with dict-syntax variable names */
4803 static Jim_Obj
*JimDictSugarGet(Jim_Interp
*interp
, Jim_Obj
*objPtr
, int flags
)
4805 SetDictSubstFromAny(interp
, objPtr
);
4807 return JimDictExpandArrayVariable(interp
,
4808 objPtr
->internalRep
.dictSubstValue
.varNameObjPtr
,
4809 objPtr
->internalRep
.dictSubstValue
.indexObjPtr
, flags
);
4812 /* --------- $var(INDEX) substitution, using a specialized object ----------- */
4814 void FreeDictSubstInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
4816 Jim_DecrRefCount(interp
, objPtr
->internalRep
.dictSubstValue
.varNameObjPtr
);
4817 Jim_DecrRefCount(interp
, objPtr
->internalRep
.dictSubstValue
.indexObjPtr
);
4820 static void DupDictSubstInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
)
4822 /* Copy the internal rep */
4823 dupPtr
->internalRep
= srcPtr
->internalRep
;
4824 /* Need to increment the ref counts */
4825 Jim_IncrRefCount(dupPtr
->internalRep
.dictSubstValue
.varNameObjPtr
);
4826 Jim_IncrRefCount(dupPtr
->internalRep
.dictSubstValue
.indexObjPtr
);
4829 /* Note: The object *must* be in dict-sugar format */
4830 static void SetDictSubstFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
4832 if (objPtr
->typePtr
!= &dictSubstObjType
) {
4833 Jim_Obj
*varObjPtr
, *keyObjPtr
;
4835 if (objPtr
->typePtr
== &interpolatedObjType
) {
4836 /* An interpolated object in dict-sugar form */
4838 varObjPtr
= objPtr
->internalRep
.dictSubstValue
.varNameObjPtr
;
4839 keyObjPtr
= objPtr
->internalRep
.dictSubstValue
.indexObjPtr
;
4841 Jim_IncrRefCount(varObjPtr
);
4842 Jim_IncrRefCount(keyObjPtr
);
4845 JimDictSugarParseVarKey(interp
, objPtr
, &varObjPtr
, &keyObjPtr
);
4848 Jim_FreeIntRep(interp
, objPtr
);
4849 objPtr
->typePtr
= &dictSubstObjType
;
4850 objPtr
->internalRep
.dictSubstValue
.varNameObjPtr
= varObjPtr
;
4851 objPtr
->internalRep
.dictSubstValue
.indexObjPtr
= keyObjPtr
;
4855 /* This function is used to expand [dict get] sugar in the form
4856 * of $var(INDEX). The function is mainly used by Jim_EvalObj()
4857 * to deal with tokens of type JIM_TT_DICTSUGAR. objPtr points to an
4858 * object that is *guaranteed* to be in the form VARNAME(INDEX).
4859 * The 'index' part is [subst]ituted, and is used to lookup a key inside
4860 * the [dict]ionary contained in variable VARNAME. */
4861 static Jim_Obj
*JimExpandDictSugar(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
4863 Jim_Obj
*resObjPtr
= NULL
;
4864 Jim_Obj
*substKeyObjPtr
= NULL
;
4866 SetDictSubstFromAny(interp
, objPtr
);
4868 if (Jim_SubstObj(interp
, objPtr
->internalRep
.dictSubstValue
.indexObjPtr
,
4869 &substKeyObjPtr
, JIM_NONE
)
4873 Jim_IncrRefCount(substKeyObjPtr
);
4875 JimDictExpandArrayVariable(interp
, objPtr
->internalRep
.dictSubstValue
.varNameObjPtr
,
4877 Jim_DecrRefCount(interp
, substKeyObjPtr
);
4882 /* -----------------------------------------------------------------------------
4884 * ---------------------------------------------------------------------------*/
4886 static Jim_CallFrame
*JimCreateCallFrame(Jim_Interp
*interp
, Jim_CallFrame
*parent
, Jim_Obj
*nsObj
)
4890 if (interp
->freeFramesList
) {
4891 cf
= interp
->freeFramesList
;
4892 interp
->freeFramesList
= cf
->next
;
4896 cf
->procArgsObjPtr
= NULL
;
4897 cf
->procBodyObjPtr
= NULL
;
4899 cf
->staticVars
= NULL
;
4900 cf
->localCommands
= NULL
;
4901 cf
->tailcallObj
= NULL
;
4902 cf
->tailcallCmd
= NULL
;
4905 cf
= Jim_Alloc(sizeof(*cf
));
4906 memset(cf
, 0, sizeof(*cf
));
4908 Jim_InitHashTable(&cf
->vars
, &JimVariablesHashTableType
, interp
);
4911 cf
->id
= interp
->callFrameEpoch
++;
4912 cf
->parent
= parent
;
4913 cf
->level
= parent
? parent
->level
+ 1 : 0;
4915 Jim_IncrRefCount(nsObj
);
4920 static int JimDeleteLocalProcs(Jim_Interp
*interp
, Jim_Stack
*localCommands
)
4922 /* Delete any local procs */
4923 if (localCommands
) {
4924 Jim_Obj
*cmdNameObj
;
4926 while ((cmdNameObj
= Jim_StackPop(localCommands
)) != NULL
) {
4929 Jim_HashTable
*ht
= &interp
->commands
;
4931 const char *fqname
= JimQualifyName(interp
, Jim_String(cmdNameObj
), &fqObjName
);
4933 he
= Jim_FindHashEntry(ht
, fqname
);
4936 Jim_Cmd
*cmd
= Jim_GetHashEntryVal(he
);
4938 Jim_Cmd
*prevCmd
= cmd
->prevCmd
;
4939 cmd
->prevCmd
= NULL
;
4941 /* Delete the old command */
4942 JimDecrCmdRefCount(interp
, cmd
);
4944 /* And restore the original */
4945 Jim_SetHashVal(ht
, he
, prevCmd
);
4948 Jim_DeleteHashEntry(ht
, fqname
);
4950 Jim_InterpIncrProcEpoch(interp
);
4952 Jim_DecrRefCount(interp
, cmdNameObj
);
4953 JimFreeQualifiedName(interp
, fqObjName
);
4955 Jim_FreeStack(localCommands
);
4956 Jim_Free(localCommands
);
4962 * Run any $jim::defer scripts for the current call frame.
4964 * retcode is the return code from the current proc.
4966 * Returns the new return code.
4968 static int JimInvokeDefer(Jim_Interp
*interp
, int retcode
)
4972 /* Fast check for the likely case that the variable doesn't exist */
4973 if (Jim_FindHashEntry(&interp
->framePtr
->vars
, "jim::defer") == NULL
) {
4977 objPtr
= Jim_GetVariableStr(interp
, "jim::defer", JIM_NONE
);
4982 int listLen
= Jim_ListLength(interp
, objPtr
);
4983 Jim_Obj
*resultObjPtr
;
4985 Jim_IncrRefCount(objPtr
);
4987 /* Need to save away the current interp result and
4988 * restore it if appropriate
4990 resultObjPtr
= Jim_GetResult(interp
);
4991 Jim_IncrRefCount(resultObjPtr
);
4992 Jim_SetEmptyResult(interp
);
4994 /* Invoke in reverse order */
4995 for (i
= listLen
; i
> 0; i
--) {
4996 /* If a defer script returns an error, don't evaluate remaining scripts */
4997 Jim_Obj
*scriptObjPtr
= Jim_ListGetIndex(interp
, objPtr
, i
- 1);
4998 ret
= Jim_EvalObj(interp
, scriptObjPtr
);
4999 if (ret
!= JIM_OK
) {
5004 if (ret
== JIM_OK
|| retcode
== JIM_ERR
) {
5005 /* defer script had no error, or proc had an error so restore proc result */
5006 Jim_SetResult(interp
, resultObjPtr
);
5012 Jim_DecrRefCount(interp
, resultObjPtr
);
5013 Jim_DecrRefCount(interp
, objPtr
);
5018 #define JIM_FCF_FULL 0 /* Always free the vars hash table */
5019 #define JIM_FCF_REUSE 1 /* Reuse the vars hash table if possible */
5020 static void JimFreeCallFrame(Jim_Interp
*interp
, Jim_CallFrame
*cf
, int action
)
5022 JimDeleteLocalProcs(interp
, cf
->localCommands
);
5024 if (cf
->procArgsObjPtr
)
5025 Jim_DecrRefCount(interp
, cf
->procArgsObjPtr
);
5026 if (cf
->procBodyObjPtr
)
5027 Jim_DecrRefCount(interp
, cf
->procBodyObjPtr
);
5028 Jim_DecrRefCount(interp
, cf
->nsObj
);
5029 if (action
== JIM_FCF_FULL
|| cf
->vars
.size
!= JIM_HT_INITIAL_SIZE
)
5030 Jim_FreeHashTable(&cf
->vars
);
5033 Jim_HashEntry
**table
= cf
->vars
.table
, *he
;
5035 for (i
= 0; i
< JIM_HT_INITIAL_SIZE
; i
++) {
5037 while (he
!= NULL
) {
5038 Jim_HashEntry
*nextEntry
= he
->next
;
5039 Jim_Var
*varPtr
= Jim_GetHashEntryVal(he
);
5041 Jim_DecrRefCount(interp
, varPtr
->objPtr
);
5042 Jim_Free(Jim_GetHashEntryKey(he
));
5051 cf
->next
= interp
->freeFramesList
;
5052 interp
->freeFramesList
= cf
;
5056 /* -----------------------------------------------------------------------------
5058 * ---------------------------------------------------------------------------*/
5059 #if defined(JIM_REFERENCES) && !defined(JIM_BOOTSTRAP)
5061 /* References HashTable Type.
5063 * Keys are unsigned long integers, dynamically allocated for now but in the
5064 * future it's worth to cache this 4 bytes objects. Values are pointers
5065 * to Jim_References. */
5066 static void JimReferencesHTValDestructor(void *interp
, void *val
)
5068 Jim_Reference
*refPtr
= (void *)val
;
5070 Jim_DecrRefCount(interp
, refPtr
->objPtr
);
5071 if (refPtr
->finalizerCmdNamePtr
!= NULL
) {
5072 Jim_DecrRefCount(interp
, refPtr
->finalizerCmdNamePtr
);
5077 static unsigned int JimReferencesHTHashFunction(const void *key
)
5079 /* Only the least significant bits are used. */
5080 const unsigned long *widePtr
= key
;
5081 unsigned int intValue
= (unsigned int)*widePtr
;
5083 return Jim_IntHashFunction(intValue
);
5086 static void *JimReferencesHTKeyDup(void *privdata
, const void *key
)
5088 void *copy
= Jim_Alloc(sizeof(unsigned long));
5090 JIM_NOTUSED(privdata
);
5092 memcpy(copy
, key
, sizeof(unsigned long));
5096 static int JimReferencesHTKeyCompare(void *privdata
, const void *key1
, const void *key2
)
5098 JIM_NOTUSED(privdata
);
5100 return memcmp(key1
, key2
, sizeof(unsigned long)) == 0;
5103 static void JimReferencesHTKeyDestructor(void *privdata
, void *key
)
5105 JIM_NOTUSED(privdata
);
5110 static const Jim_HashTableType JimReferencesHashTableType
= {
5111 JimReferencesHTHashFunction
, /* hash function */
5112 JimReferencesHTKeyDup
, /* key dup */
5114 JimReferencesHTKeyCompare
, /* key compare */
5115 JimReferencesHTKeyDestructor
, /* key destructor */
5116 JimReferencesHTValDestructor
/* val destructor */
5119 /* -----------------------------------------------------------------------------
5120 * Reference object type and References API
5121 * ---------------------------------------------------------------------------*/
5123 /* The string representation of references has two features in order
5124 * to make the GC faster. The first is that every reference starts
5125 * with a non common character '<', in order to make the string matching
5126 * faster. The second is that the reference string rep is 42 characters
5127 * in length, this means that it is not necessary to check any object with a string
5128 * repr < 42, and usually there aren't many of these objects. */
5130 #define JIM_REFERENCE_SPACE (35+JIM_REFERENCE_TAGLEN)
5132 static int JimFormatReference(char *buf
, Jim_Reference
*refPtr
, unsigned long id
)
5134 const char *fmt
= "<reference.<%s>.%020lu>";
5136 sprintf(buf
, fmt
, refPtr
->tag
, id
);
5137 return JIM_REFERENCE_SPACE
;
5140 static void UpdateStringOfReference(struct Jim_Obj
*objPtr
);
5142 static const Jim_ObjType referenceObjType
= {
5146 UpdateStringOfReference
,
5147 JIM_TYPE_REFERENCES
,
5150 static void UpdateStringOfReference(struct Jim_Obj
*objPtr
)
5152 char buf
[JIM_REFERENCE_SPACE
+ 1];
5154 JimFormatReference(buf
, objPtr
->internalRep
.refValue
.refPtr
, objPtr
->internalRep
.refValue
.id
);
5155 JimSetStringBytes(objPtr
, buf
);
5158 /* returns true if 'c' is a valid reference tag character.
5159 * i.e. inside the range [_a-zA-Z0-9] */
5160 static int isrefchar(int c
)
5162 return (c
== '_' || isalnum(c
));
5165 static int SetReferenceFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
5167 unsigned long value
;
5169 const char *str
, *start
, *end
;
5171 Jim_Reference
*refPtr
;
5175 /* Get the string representation */
5176 str
= Jim_GetString(objPtr
, &len
);
5177 /* Check if it looks like a reference */
5178 if (len
< JIM_REFERENCE_SPACE
)
5182 end
= str
+ len
- 1;
5183 while (*start
== ' ')
5185 while (*end
== ' ' && end
> start
)
5187 if (end
- start
+ 1 != JIM_REFERENCE_SPACE
)
5189 /* <reference.<1234567>.%020> */
5190 if (memcmp(start
, "<reference.<", 12) != 0)
5192 if (start
[12 + JIM_REFERENCE_TAGLEN
] != '>' || end
[0] != '>')
5194 /* The tag can't contain chars other than a-zA-Z0-9 + '_'. */
5195 for (i
= 0; i
< JIM_REFERENCE_TAGLEN
; i
++) {
5196 if (!isrefchar(start
[12 + i
]))
5199 /* Extract info from the reference. */
5200 memcpy(refId
, start
+ 14 + JIM_REFERENCE_TAGLEN
, 20);
5202 /* Try to convert the ID into an unsigned long */
5203 value
= strtoul(refId
, &endptr
, 10);
5204 if (JimCheckConversion(refId
, endptr
) != JIM_OK
)
5206 /* Check if the reference really exists! */
5207 he
= Jim_FindHashEntry(&interp
->references
, &value
);
5209 Jim_SetResultFormatted(interp
, "invalid reference id \"%#s\"", objPtr
);
5212 refPtr
= Jim_GetHashEntryVal(he
);
5213 /* Free the old internal repr and set the new one. */
5214 Jim_FreeIntRep(interp
, objPtr
);
5215 objPtr
->typePtr
= &referenceObjType
;
5216 objPtr
->internalRep
.refValue
.id
= value
;
5217 objPtr
->internalRep
.refValue
.refPtr
= refPtr
;
5221 Jim_SetResultFormatted(interp
, "expected reference but got \"%#s\"", objPtr
);
5225 /* Returns a new reference pointing to objPtr, having cmdNamePtr
5226 * as finalizer command (or NULL if there is no finalizer).
5227 * The returned reference object has refcount = 0. */
5228 Jim_Obj
*Jim_NewReference(Jim_Interp
*interp
, Jim_Obj
*objPtr
, Jim_Obj
*tagPtr
, Jim_Obj
*cmdNamePtr
)
5230 struct Jim_Reference
*refPtr
;
5236 /* Perform the Garbage Collection if needed. */
5237 Jim_CollectIfNeeded(interp
);
5239 refPtr
= Jim_Alloc(sizeof(*refPtr
));
5240 refPtr
->objPtr
= objPtr
;
5241 Jim_IncrRefCount(objPtr
);
5242 refPtr
->finalizerCmdNamePtr
= cmdNamePtr
;
5244 Jim_IncrRefCount(cmdNamePtr
);
5245 id
= interp
->referenceNextId
++;
5246 Jim_AddHashEntry(&interp
->references
, &id
, refPtr
);
5247 refObjPtr
= Jim_NewObj(interp
);
5248 refObjPtr
->typePtr
= &referenceObjType
;
5249 refObjPtr
->bytes
= NULL
;
5250 refObjPtr
->internalRep
.refValue
.id
= id
;
5251 refObjPtr
->internalRep
.refValue
.refPtr
= refPtr
;
5252 interp
->referenceNextId
++;
5253 /* Set the tag. Trimmed at JIM_REFERENCE_TAGLEN. Everything
5254 * that does not pass the 'isrefchar' test is replaced with '_' */
5255 tag
= Jim_GetString(tagPtr
, &tagLen
);
5256 if (tagLen
> JIM_REFERENCE_TAGLEN
)
5257 tagLen
= JIM_REFERENCE_TAGLEN
;
5258 for (i
= 0; i
< JIM_REFERENCE_TAGLEN
; i
++) {
5259 if (i
< tagLen
&& isrefchar(tag
[i
]))
5260 refPtr
->tag
[i
] = tag
[i
];
5262 refPtr
->tag
[i
] = '_';
5264 refPtr
->tag
[JIM_REFERENCE_TAGLEN
] = '\0';
5268 Jim_Reference
*Jim_GetReference(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
5270 if (objPtr
->typePtr
!= &referenceObjType
&& SetReferenceFromAny(interp
, objPtr
) == JIM_ERR
)
5272 return objPtr
->internalRep
.refValue
.refPtr
;
5275 int Jim_SetFinalizer(Jim_Interp
*interp
, Jim_Obj
*objPtr
, Jim_Obj
*cmdNamePtr
)
5277 Jim_Reference
*refPtr
;
5279 if ((refPtr
= Jim_GetReference(interp
, objPtr
)) == NULL
)
5281 Jim_IncrRefCount(cmdNamePtr
);
5282 if (refPtr
->finalizerCmdNamePtr
)
5283 Jim_DecrRefCount(interp
, refPtr
->finalizerCmdNamePtr
);
5284 refPtr
->finalizerCmdNamePtr
= cmdNamePtr
;
5288 int Jim_GetFinalizer(Jim_Interp
*interp
, Jim_Obj
*objPtr
, Jim_Obj
**cmdNamePtrPtr
)
5290 Jim_Reference
*refPtr
;
5292 if ((refPtr
= Jim_GetReference(interp
, objPtr
)) == NULL
)
5294 *cmdNamePtrPtr
= refPtr
->finalizerCmdNamePtr
;
5298 /* -----------------------------------------------------------------------------
5299 * References Garbage Collection
5300 * ---------------------------------------------------------------------------*/
5302 /* This the hash table type for the "MARK" phase of the GC */
5303 static const Jim_HashTableType JimRefMarkHashTableType
= {
5304 JimReferencesHTHashFunction
, /* hash function */
5305 JimReferencesHTKeyDup
, /* key dup */
5307 JimReferencesHTKeyCompare
, /* key compare */
5308 JimReferencesHTKeyDestructor
, /* key destructor */
5309 NULL
/* val destructor */
5312 /* Performs the garbage collection. */
5313 int Jim_Collect(Jim_Interp
*interp
)
5316 Jim_HashTable marks
;
5317 Jim_HashTableIterator htiter
;
5321 /* Avoid recursive calls */
5322 if (interp
->lastCollectId
== (unsigned long)~0) {
5323 /* Jim_Collect() already running. Return just now. */
5326 interp
->lastCollectId
= ~0;
5328 /* Mark all the references found into the 'mark' hash table.
5329 * The references are searched in every live object that
5330 * is of a type that can contain references. */
5331 Jim_InitHashTable(&marks
, &JimRefMarkHashTableType
, NULL
);
5332 objPtr
= interp
->liveList
;
5334 if (objPtr
->typePtr
== NULL
|| objPtr
->typePtr
->flags
& JIM_TYPE_REFERENCES
) {
5335 const char *str
, *p
;
5338 /* If the object is of type reference, to get the
5339 * Id is simple... */
5340 if (objPtr
->typePtr
== &referenceObjType
) {
5341 Jim_AddHashEntry(&marks
, &objPtr
->internalRep
.refValue
.id
, NULL
);
5343 printf("MARK (reference): %d refcount: %d\n",
5344 (int)objPtr
->internalRep
.refValue
.id
, objPtr
->refCount
);
5346 objPtr
= objPtr
->nextObjPtr
;
5349 /* Get the string repr of the object we want
5350 * to scan for references. */
5351 p
= str
= Jim_GetString(objPtr
, &len
);
5352 /* Skip objects too little to contain references. */
5353 if (len
< JIM_REFERENCE_SPACE
) {
5354 objPtr
= objPtr
->nextObjPtr
;
5357 /* Extract references from the object string repr. */
5362 if ((p
= strstr(p
, "<reference.<")) == NULL
)
5364 /* Check if it's a valid reference. */
5365 if (len
- (p
- str
) < JIM_REFERENCE_SPACE
)
5367 if (p
[41] != '>' || p
[19] != '>' || p
[20] != '.')
5369 for (i
= 21; i
<= 40; i
++)
5370 if (!isdigit(UCHAR(p
[i
])))
5373 id
= strtoul(p
+ 21, NULL
, 10);
5375 /* Ok, a reference for the given ID
5376 * was found. Mark it. */
5377 Jim_AddHashEntry(&marks
, &id
, NULL
);
5379 printf("MARK: %d\n", (int)id
);
5381 p
+= JIM_REFERENCE_SPACE
;
5384 objPtr
= objPtr
->nextObjPtr
;
5387 /* Run the references hash table to destroy every reference that
5388 * is not referenced outside (not present in the mark HT). */
5389 JimInitHashTableIterator(&interp
->references
, &htiter
);
5390 while ((he
= Jim_NextHashEntry(&htiter
)) != NULL
) {
5391 const unsigned long *refId
;
5392 Jim_Reference
*refPtr
;
5395 /* Check if in the mark phase we encountered
5396 * this reference. */
5397 if (Jim_FindHashEntry(&marks
, refId
) == NULL
) {
5399 printf("COLLECTING %d\n", (int)*refId
);
5402 /* Drop the reference, but call the
5403 * finalizer first if registered. */
5404 refPtr
= Jim_GetHashEntryVal(he
);
5405 if (refPtr
->finalizerCmdNamePtr
) {
5406 char *refstr
= Jim_Alloc(JIM_REFERENCE_SPACE
+ 1);
5407 Jim_Obj
*objv
[3], *oldResult
;
5409 JimFormatReference(refstr
, refPtr
, *refId
);
5411 objv
[0] = refPtr
->finalizerCmdNamePtr
;
5412 objv
[1] = Jim_NewStringObjNoAlloc(interp
, refstr
, JIM_REFERENCE_SPACE
);
5413 objv
[2] = refPtr
->objPtr
;
5415 /* Drop the reference itself */
5416 /* Avoid the finaliser being freed here */
5417 Jim_IncrRefCount(objv
[0]);
5418 /* Don't remove the reference from the hash table just yet
5419 * since that will free refPtr, and hence refPtr->objPtr
5422 /* Call the finalizer. Errors ignored. (should we use bgerror?) */
5423 oldResult
= interp
->result
;
5424 Jim_IncrRefCount(oldResult
);
5425 Jim_EvalObjVector(interp
, 3, objv
);
5426 Jim_SetResult(interp
, oldResult
);
5427 Jim_DecrRefCount(interp
, oldResult
);
5429 Jim_DecrRefCount(interp
, objv
[0]);
5431 Jim_DeleteHashEntry(&interp
->references
, refId
);
5434 Jim_FreeHashTable(&marks
);
5435 interp
->lastCollectId
= interp
->referenceNextId
;
5436 interp
->lastCollectTime
= JimClock();
5440 #define JIM_COLLECT_ID_PERIOD 5000000
5441 #define JIM_COLLECT_TIME_PERIOD 300000
5443 void Jim_CollectIfNeeded(Jim_Interp
*interp
)
5445 unsigned long elapsedId
;
5446 jim_wide elapsedTime
;
5448 elapsedId
= interp
->referenceNextId
- interp
->lastCollectId
;
5449 elapsedTime
= JimClock() - interp
->lastCollectTime
;
5452 if (elapsedId
> JIM_COLLECT_ID_PERIOD
|| elapsedTime
> JIM_COLLECT_TIME_PERIOD
) {
5453 Jim_Collect(interp
);
5456 #endif /* JIM_REFERENCES && !JIM_BOOTSTRAP */
5458 int Jim_IsBigEndian(void)
5465 return uval
.c
[0] == 1;
5468 /* -----------------------------------------------------------------------------
5469 * Interpreter related functions
5470 * ---------------------------------------------------------------------------*/
5472 Jim_Interp
*Jim_CreateInterp(void)
5474 Jim_Interp
*i
= Jim_Alloc(sizeof(*i
));
5476 memset(i
, 0, sizeof(*i
));
5478 i
->maxCallFrameDepth
= JIM_MAX_CALLFRAME_DEPTH
;
5479 i
->maxEvalDepth
= JIM_MAX_EVAL_DEPTH
;
5480 i
->lastCollectTime
= JimClock();
5482 /* Note that we can create objects only after the
5483 * interpreter liveList and freeList pointers are
5484 * initialized to NULL. */
5485 Jim_InitHashTable(&i
->commands
, &JimCommandsHashTableType
, i
);
5486 #ifdef JIM_REFERENCES
5487 Jim_InitHashTable(&i
->references
, &JimReferencesHashTableType
, i
);
5489 Jim_InitHashTable(&i
->assocData
, &JimAssocDataHashTableType
, i
);
5490 Jim_InitHashTable(&i
->packages
, &JimPackageHashTableType
, NULL
);
5491 i
->emptyObj
= Jim_NewEmptyStringObj(i
);
5492 i
->trueObj
= Jim_NewIntObj(i
, 1);
5493 i
->falseObj
= Jim_NewIntObj(i
, 0);
5494 i
->framePtr
= i
->topFramePtr
= JimCreateCallFrame(i
, NULL
, i
->emptyObj
);
5495 i
->errorFileNameObj
= i
->emptyObj
;
5496 i
->result
= i
->emptyObj
;
5497 i
->stackTrace
= Jim_NewListObj(i
, NULL
, 0);
5498 i
->unknown
= Jim_NewStringObj(i
, "unknown", -1);
5499 i
->errorProc
= i
->emptyObj
;
5500 i
->currentScriptObj
= Jim_NewEmptyStringObj(i
);
5501 i
->nullScriptObj
= Jim_NewEmptyStringObj(i
);
5502 Jim_IncrRefCount(i
->emptyObj
);
5503 Jim_IncrRefCount(i
->errorFileNameObj
);
5504 Jim_IncrRefCount(i
->result
);
5505 Jim_IncrRefCount(i
->stackTrace
);
5506 Jim_IncrRefCount(i
->unknown
);
5507 Jim_IncrRefCount(i
->currentScriptObj
);
5508 Jim_IncrRefCount(i
->nullScriptObj
);
5509 Jim_IncrRefCount(i
->errorProc
);
5510 Jim_IncrRefCount(i
->trueObj
);
5511 Jim_IncrRefCount(i
->falseObj
);
5513 /* Initialize key variables every interpreter should contain */
5514 Jim_SetVariableStrWithStr(i
, JIM_LIBPATH
, TCL_LIBRARY
);
5515 Jim_SetVariableStrWithStr(i
, JIM_INTERACTIVE
, "0");
5517 Jim_SetVariableStrWithStr(i
, "tcl_platform(engine)", "Jim");
5518 Jim_SetVariableStrWithStr(i
, "tcl_platform(os)", TCL_PLATFORM_OS
);
5519 Jim_SetVariableStrWithStr(i
, "tcl_platform(platform)", TCL_PLATFORM_PLATFORM
);
5520 Jim_SetVariableStrWithStr(i
, "tcl_platform(pathSeparator)", TCL_PLATFORM_PATH_SEPARATOR
);
5521 Jim_SetVariableStrWithStr(i
, "tcl_platform(byteOrder)", Jim_IsBigEndian() ? "bigEndian" : "littleEndian");
5522 Jim_SetVariableStrWithStr(i
, "tcl_platform(threaded)", "0");
5523 Jim_SetVariableStr(i
, "tcl_platform(pointerSize)", Jim_NewIntObj(i
, sizeof(void *)));
5524 Jim_SetVariableStr(i
, "tcl_platform(wordSize)", Jim_NewIntObj(i
, sizeof(jim_wide
)));
5529 void Jim_FreeInterp(Jim_Interp
*i
)
5531 Jim_CallFrame
*cf
, *cfx
;
5533 Jim_Obj
*objPtr
, *nextObjPtr
;
5535 /* Free the active call frames list - must be done before i->commands is destroyed */
5536 for (cf
= i
->framePtr
; cf
; cf
= cfx
) {
5537 /* Note that we ignore any errors */
5538 JimInvokeDefer(i
, JIM_OK
);
5540 JimFreeCallFrame(i
, cf
, JIM_FCF_FULL
);
5543 Jim_DecrRefCount(i
, i
->emptyObj
);
5544 Jim_DecrRefCount(i
, i
->trueObj
);
5545 Jim_DecrRefCount(i
, i
->falseObj
);
5546 Jim_DecrRefCount(i
, i
->result
);
5547 Jim_DecrRefCount(i
, i
->stackTrace
);
5548 Jim_DecrRefCount(i
, i
->errorProc
);
5549 Jim_DecrRefCount(i
, i
->unknown
);
5550 Jim_DecrRefCount(i
, i
->errorFileNameObj
);
5551 Jim_DecrRefCount(i
, i
->currentScriptObj
);
5552 Jim_DecrRefCount(i
, i
->nullScriptObj
);
5553 Jim_FreeHashTable(&i
->commands
);
5554 #ifdef JIM_REFERENCES
5555 Jim_FreeHashTable(&i
->references
);
5557 Jim_FreeHashTable(&i
->packages
);
5558 Jim_Free(i
->prngState
);
5559 Jim_FreeHashTable(&i
->assocData
);
5561 /* Check that the live object list is empty, otherwise
5562 * there is a memory leak. */
5563 #ifdef JIM_MAINTAINER
5564 if (i
->liveList
!= NULL
) {
5565 objPtr
= i
->liveList
;
5567 printf("\n-------------------------------------\n");
5568 printf("Objects still in the free list:\n");
5570 const char *type
= objPtr
->typePtr
? objPtr
->typePtr
->name
: "string";
5573 if (objPtr
->bytes
&& strlen(objPtr
->bytes
) > 20) {
5574 printf("%p (%d) %-10s: '%.20s...'\n",
5575 (void *)objPtr
, objPtr
->refCount
, type
, objPtr
->bytes
);
5578 printf("%p (%d) %-10s: '%s'\n",
5579 (void *)objPtr
, objPtr
->refCount
, type
, objPtr
->bytes
? objPtr
->bytes
: "(null)");
5581 if (objPtr
->typePtr
== &sourceObjType
) {
5582 printf("FILE %s LINE %d\n",
5583 Jim_String(objPtr
->internalRep
.sourceValue
.fileNameObj
),
5584 objPtr
->internalRep
.sourceValue
.lineNumber
);
5586 objPtr
= objPtr
->nextObjPtr
;
5588 printf("-------------------------------------\n\n");
5589 JimPanic((1, "Live list non empty freeing the interpreter! Leak?"));
5593 /* Free all the freed objects. */
5594 objPtr
= i
->freeList
;
5596 nextObjPtr
= objPtr
->nextObjPtr
;
5598 objPtr
= nextObjPtr
;
5601 /* Free the free call frames list */
5602 for (cf
= i
->freeFramesList
; cf
; cf
= cfx
) {
5605 Jim_FreeHashTable(&cf
->vars
);
5609 /* Free the interpreter structure. */
5613 /* Returns the call frame relative to the level represented by
5614 * levelObjPtr. If levelObjPtr == NULL, the level is assumed to be '1'.
5616 * This function accepts the 'level' argument in the form
5617 * of the commands [uplevel] and [upvar].
5619 * Returns NULL on error.
5621 * Note: for a function accepting a relative integer as level suitable
5622 * for implementation of [info level ?level?], see JimGetCallFrameByInteger()
5624 Jim_CallFrame
*Jim_GetCallFrameByLevel(Jim_Interp
*interp
, Jim_Obj
*levelObjPtr
)
5628 Jim_CallFrame
*framePtr
;
5631 str
= Jim_String(levelObjPtr
);
5632 if (str
[0] == '#') {
5635 level
= jim_strtol(str
+ 1, &endptr
);
5636 if (str
[1] == '\0' || endptr
[0] != '\0') {
5641 if (Jim_GetLong(interp
, levelObjPtr
, &level
) != JIM_OK
|| level
< 0) {
5645 /* Convert from a relative to an absolute level */
5646 level
= interp
->framePtr
->level
- level
;
5651 str
= "1"; /* Needed to format the error message. */
5652 level
= interp
->framePtr
->level
- 1;
5656 return interp
->topFramePtr
;
5660 for (framePtr
= interp
->framePtr
; framePtr
; framePtr
= framePtr
->parent
) {
5661 if (framePtr
->level
== level
) {
5667 Jim_SetResultFormatted(interp
, "bad level \"%s\"", str
);
5671 /* Similar to Jim_GetCallFrameByLevel() but the level is specified
5672 * as a relative integer like in the [info level ?level?] command.
5674 static Jim_CallFrame
*JimGetCallFrameByInteger(Jim_Interp
*interp
, Jim_Obj
*levelObjPtr
)
5677 Jim_CallFrame
*framePtr
;
5679 if (Jim_GetLong(interp
, levelObjPtr
, &level
) == JIM_OK
) {
5681 /* Convert from a relative to an absolute level */
5682 level
= interp
->framePtr
->level
+ level
;
5686 return interp
->topFramePtr
;
5690 for (framePtr
= interp
->framePtr
; framePtr
; framePtr
= framePtr
->parent
) {
5691 if (framePtr
->level
== level
) {
5697 Jim_SetResultFormatted(interp
, "bad level \"%#s\"", levelObjPtr
);
5701 static void JimResetStackTrace(Jim_Interp
*interp
)
5703 Jim_DecrRefCount(interp
, interp
->stackTrace
);
5704 interp
->stackTrace
= Jim_NewListObj(interp
, NULL
, 0);
5705 Jim_IncrRefCount(interp
->stackTrace
);
5708 static void JimSetStackTrace(Jim_Interp
*interp
, Jim_Obj
*stackTraceObj
)
5712 /* Increment reference first in case these are the same object */
5713 Jim_IncrRefCount(stackTraceObj
);
5714 Jim_DecrRefCount(interp
, interp
->stackTrace
);
5715 interp
->stackTrace
= stackTraceObj
;
5716 interp
->errorFlag
= 1;
5718 /* This is a bit ugly.
5719 * If the filename of the last entry of the stack trace is empty,
5720 * the next stack level should be added.
5722 len
= Jim_ListLength(interp
, interp
->stackTrace
);
5724 if (Jim_Length(Jim_ListGetIndex(interp
, interp
->stackTrace
, len
- 2)) == 0) {
5725 interp
->addStackTrace
= 1;
5730 static void JimAppendStackTrace(Jim_Interp
*interp
, const char *procname
,
5731 Jim_Obj
*fileNameObj
, int linenr
)
5733 if (strcmp(procname
, "unknown") == 0) {
5736 if (!*procname
&& !Jim_Length(fileNameObj
)) {
5737 /* No useful info here */
5741 if (Jim_IsShared(interp
->stackTrace
)) {
5742 Jim_DecrRefCount(interp
, interp
->stackTrace
);
5743 interp
->stackTrace
= Jim_DuplicateObj(interp
, interp
->stackTrace
);
5744 Jim_IncrRefCount(interp
->stackTrace
);
5747 /* If we have no procname but the previous element did, merge with that frame */
5748 if (!*procname
&& Jim_Length(fileNameObj
)) {
5749 /* Just a filename. Check the previous entry */
5750 int len
= Jim_ListLength(interp
, interp
->stackTrace
);
5753 Jim_Obj
*objPtr
= Jim_ListGetIndex(interp
, interp
->stackTrace
, len
- 3);
5754 if (Jim_Length(objPtr
)) {
5755 /* Yes, the previous level had procname */
5756 objPtr
= Jim_ListGetIndex(interp
, interp
->stackTrace
, len
- 2);
5757 if (Jim_Length(objPtr
) == 0) {
5758 /* But no filename, so merge the new info with that frame */
5759 ListSetIndex(interp
, interp
->stackTrace
, len
- 2, fileNameObj
, 0);
5760 ListSetIndex(interp
, interp
->stackTrace
, len
- 1, Jim_NewIntObj(interp
, linenr
), 0);
5767 Jim_ListAppendElement(interp
, interp
->stackTrace
, Jim_NewStringObj(interp
, procname
, -1));
5768 Jim_ListAppendElement(interp
, interp
->stackTrace
, fileNameObj
);
5769 Jim_ListAppendElement(interp
, interp
->stackTrace
, Jim_NewIntObj(interp
, linenr
));
5772 int Jim_SetAssocData(Jim_Interp
*interp
, const char *key
, Jim_InterpDeleteProc
* delProc
,
5775 AssocDataValue
*assocEntryPtr
= (AssocDataValue
*) Jim_Alloc(sizeof(AssocDataValue
));
5777 assocEntryPtr
->delProc
= delProc
;
5778 assocEntryPtr
->data
= data
;
5779 return Jim_AddHashEntry(&interp
->assocData
, key
, assocEntryPtr
);
5782 void *Jim_GetAssocData(Jim_Interp
*interp
, const char *key
)
5784 Jim_HashEntry
*entryPtr
= Jim_FindHashEntry(&interp
->assocData
, key
);
5786 if (entryPtr
!= NULL
) {
5787 AssocDataValue
*assocEntryPtr
= Jim_GetHashEntryVal(entryPtr
);
5788 return assocEntryPtr
->data
;
5793 int Jim_DeleteAssocData(Jim_Interp
*interp
, const char *key
)
5795 return Jim_DeleteHashEntry(&interp
->assocData
, key
);
5798 int Jim_GetExitCode(Jim_Interp
*interp
)
5800 return interp
->exitCode
;
5803 /* -----------------------------------------------------------------------------
5805 * ---------------------------------------------------------------------------*/
5806 static void UpdateStringOfInt(struct Jim_Obj
*objPtr
);
5807 static int SetIntFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
, int flags
);
5809 static const Jim_ObjType intObjType
= {
5817 /* A coerced double is closer to an int than a double.
5818 * It is an int value temporarily masquerading as a double value.
5819 * i.e. it has the same string value as an int and Jim_GetWide()
5820 * succeeds, but also Jim_GetDouble() returns the value directly.
5822 static const Jim_ObjType coercedDoubleObjType
= {
5831 static void UpdateStringOfInt(struct Jim_Obj
*objPtr
)
5833 char buf
[JIM_INTEGER_SPACE
+ 1];
5834 jim_wide wideValue
= JimWideValue(objPtr
);
5837 if (wideValue
== 0) {
5841 char tmp
[JIM_INTEGER_SPACE
];
5845 if (wideValue
< 0) {
5848 /* C89 is implementation defined as to whether (-106 % 10) is -6 or 4,
5849 * whereas C99 is always -6
5850 * coverity[dead_error_line]
5852 tmp
[num
++] = (i
> 0) ? (10 - i
) : -i
;
5857 tmp
[num
++] = wideValue
% 10;
5861 for (i
= 0; i
< num
; i
++) {
5862 buf
[pos
++] = '0' + tmp
[num
- i
- 1];
5867 JimSetStringBytes(objPtr
, buf
);
5870 static int SetIntFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
, int flags
)
5875 if (objPtr
->typePtr
== &coercedDoubleObjType
) {
5877 objPtr
->typePtr
= &intObjType
;
5881 /* Get the string representation */
5882 str
= Jim_String(objPtr
);
5883 /* Try to convert into a jim_wide */
5884 if (Jim_StringToWide(str
, &wideValue
, 0) != JIM_OK
) {
5885 if (flags
& JIM_ERRMSG
) {
5886 Jim_SetResultFormatted(interp
, "expected integer but got \"%#s\"", objPtr
);
5890 if ((wideValue
== JIM_WIDE_MIN
|| wideValue
== JIM_WIDE_MAX
) && errno
== ERANGE
) {
5891 Jim_SetResultString(interp
, "Integer value too big to be represented", -1);
5894 /* Free the old internal repr and set the new one. */
5895 Jim_FreeIntRep(interp
, objPtr
);
5896 objPtr
->typePtr
= &intObjType
;
5897 objPtr
->internalRep
.wideValue
= wideValue
;
5901 #ifdef JIM_OPTIMIZATION
5902 static int JimIsWide(Jim_Obj
*objPtr
)
5904 return objPtr
->typePtr
== &intObjType
;
5908 int Jim_GetWide(Jim_Interp
*interp
, Jim_Obj
*objPtr
, jim_wide
* widePtr
)
5910 if (objPtr
->typePtr
!= &intObjType
&& SetIntFromAny(interp
, objPtr
, JIM_ERRMSG
) == JIM_ERR
)
5912 *widePtr
= JimWideValue(objPtr
);
5916 /* Get a wide but does not set an error if the format is bad. */
5917 static int JimGetWideNoErr(Jim_Interp
*interp
, Jim_Obj
*objPtr
, jim_wide
* widePtr
)
5919 if (objPtr
->typePtr
!= &intObjType
&& SetIntFromAny(interp
, objPtr
, JIM_NONE
) == JIM_ERR
)
5921 *widePtr
= JimWideValue(objPtr
);
5925 int Jim_GetLong(Jim_Interp
*interp
, Jim_Obj
*objPtr
, long *longPtr
)
5930 retval
= Jim_GetWide(interp
, objPtr
, &wideValue
);
5931 if (retval
== JIM_OK
) {
5932 *longPtr
= (long)wideValue
;
5938 Jim_Obj
*Jim_NewIntObj(Jim_Interp
*interp
, jim_wide wideValue
)
5942 objPtr
= Jim_NewObj(interp
);
5943 objPtr
->typePtr
= &intObjType
;
5944 objPtr
->bytes
= NULL
;
5945 objPtr
->internalRep
.wideValue
= wideValue
;
5949 /* -----------------------------------------------------------------------------
5951 * ---------------------------------------------------------------------------*/
5952 #define JIM_DOUBLE_SPACE 30
5954 static void UpdateStringOfDouble(struct Jim_Obj
*objPtr
);
5955 static int SetDoubleFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
);
5957 static const Jim_ObjType doubleObjType
= {
5961 UpdateStringOfDouble
,
5967 #define isnan(X) ((X) != (X))
5971 #define isinf(X) (1.0 / (X) == 0.0)
5974 static void UpdateStringOfDouble(struct Jim_Obj
*objPtr
)
5976 double value
= objPtr
->internalRep
.doubleValue
;
5979 JimSetStringBytes(objPtr
, "NaN");
5984 JimSetStringBytes(objPtr
, "-Inf");
5987 JimSetStringBytes(objPtr
, "Inf");
5992 char buf
[JIM_DOUBLE_SPACE
+ 1];
5994 int len
= sprintf(buf
, "%.12g", value
);
5996 /* Add a final ".0" if necessary */
5997 for (i
= 0; i
< len
; i
++) {
5998 if (buf
[i
] == '.' || buf
[i
] == 'e') {
5999 #if defined(JIM_SPRINTF_DOUBLE_NEEDS_FIX)
6000 /* If 'buf' ends in e-0nn or e+0nn, remove
6001 * the 0 after the + or - and reduce the length by 1
6003 char *e
= strchr(buf
, 'e');
6004 if (e
&& (e
[1] == '-' || e
[1] == '+') && e
[2] == '0') {
6007 memmove(e
, e
+ 1, len
- (e
- buf
));
6013 if (buf
[i
] == '\0') {
6018 JimSetStringBytes(objPtr
, buf
);
6022 static int SetDoubleFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
6028 #ifdef HAVE_LONG_LONG
6029 /* Assume a 53 bit mantissa */
6030 #define MIN_INT_IN_DOUBLE -(1LL << 53)
6031 #define MAX_INT_IN_DOUBLE -(MIN_INT_IN_DOUBLE + 1)
6033 if (objPtr
->typePtr
== &intObjType
6034 && JimWideValue(objPtr
) >= MIN_INT_IN_DOUBLE
6035 && JimWideValue(objPtr
) <= MAX_INT_IN_DOUBLE
) {
6037 /* Direct conversion to coerced double */
6038 objPtr
->typePtr
= &coercedDoubleObjType
;
6042 /* Preserve the string representation.
6043 * Needed so we can convert back to int without loss
6045 str
= Jim_String(objPtr
);
6047 if (Jim_StringToWide(str
, &wideValue
, 10) == JIM_OK
) {
6048 /* Managed to convert to an int, so we can use this as a cooerced double */
6049 Jim_FreeIntRep(interp
, objPtr
);
6050 objPtr
->typePtr
= &coercedDoubleObjType
;
6051 objPtr
->internalRep
.wideValue
= wideValue
;
6055 /* Try to convert into a double */
6056 if (Jim_StringToDouble(str
, &doubleValue
) != JIM_OK
) {
6057 Jim_SetResultFormatted(interp
, "expected floating-point number but got \"%#s\"", objPtr
);
6060 /* Free the old internal repr and set the new one. */
6061 Jim_FreeIntRep(interp
, objPtr
);
6063 objPtr
->typePtr
= &doubleObjType
;
6064 objPtr
->internalRep
.doubleValue
= doubleValue
;
6068 int Jim_GetDouble(Jim_Interp
*interp
, Jim_Obj
*objPtr
, double *doublePtr
)
6070 if (objPtr
->typePtr
== &coercedDoubleObjType
) {
6071 *doublePtr
= JimWideValue(objPtr
);
6074 if (objPtr
->typePtr
!= &doubleObjType
&& SetDoubleFromAny(interp
, objPtr
) == JIM_ERR
)
6077 if (objPtr
->typePtr
== &coercedDoubleObjType
) {
6078 *doublePtr
= JimWideValue(objPtr
);
6081 *doublePtr
= objPtr
->internalRep
.doubleValue
;
6086 Jim_Obj
*Jim_NewDoubleObj(Jim_Interp
*interp
, double doubleValue
)
6090 objPtr
= Jim_NewObj(interp
);
6091 objPtr
->typePtr
= &doubleObjType
;
6092 objPtr
->bytes
= NULL
;
6093 objPtr
->internalRep
.doubleValue
= doubleValue
;
6097 /* -----------------------------------------------------------------------------
6098 * Boolean conversion
6099 * ---------------------------------------------------------------------------*/
6100 static int SetBooleanFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
, int flags
);
6102 int Jim_GetBoolean(Jim_Interp
*interp
, Jim_Obj
*objPtr
, int * booleanPtr
)
6104 if (objPtr
->typePtr
!= &intObjType
&& SetBooleanFromAny(interp
, objPtr
, JIM_ERRMSG
) == JIM_ERR
)
6106 *booleanPtr
= (int) JimWideValue(objPtr
);
6110 static int SetBooleanFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
, int flags
)
6112 static const char * const falses
[] = {
6113 "0", "false", "no", "off", NULL
6115 static const char * const trues
[] = {
6116 "1", "true", "yes", "on", NULL
6122 if (Jim_GetEnum(interp
, objPtr
, falses
, &index
, NULL
, 0) == JIM_OK
) {
6124 } else if (Jim_GetEnum(interp
, objPtr
, trues
, &index
, NULL
, 0) == JIM_OK
) {
6127 if (flags
& JIM_ERRMSG
) {
6128 Jim_SetResultFormatted(interp
, "expected boolean but got \"%#s\"", objPtr
);
6133 /* Free the old internal repr and set the new one. */
6134 Jim_FreeIntRep(interp
, objPtr
);
6135 objPtr
->typePtr
= &intObjType
;
6136 objPtr
->internalRep
.wideValue
= boolean
;
6140 /* -----------------------------------------------------------------------------
6142 * ---------------------------------------------------------------------------*/
6143 static void ListInsertElements(Jim_Obj
*listPtr
, int idx
, int elemc
, Jim_Obj
*const *elemVec
);
6144 static void ListAppendElement(Jim_Obj
*listPtr
, Jim_Obj
*objPtr
);
6145 static void FreeListInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
);
6146 static void DupListInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
);
6147 static void UpdateStringOfList(struct Jim_Obj
*objPtr
);
6148 static int SetListFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
);
6150 /* Note that while the elements of the list may contain references,
6151 * the list object itself can't. This basically means that the
6152 * list object string representation as a whole can't contain references
6153 * that are not presents in the single elements. */
6154 static const Jim_ObjType listObjType
= {
6156 FreeListInternalRep
,
6162 void FreeListInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
6166 for (i
= 0; i
< objPtr
->internalRep
.listValue
.len
; i
++) {
6167 Jim_DecrRefCount(interp
, objPtr
->internalRep
.listValue
.ele
[i
]);
6169 Jim_Free(objPtr
->internalRep
.listValue
.ele
);
6172 void DupListInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
)
6176 JIM_NOTUSED(interp
);
6178 dupPtr
->internalRep
.listValue
.len
= srcPtr
->internalRep
.listValue
.len
;
6179 dupPtr
->internalRep
.listValue
.maxLen
= srcPtr
->internalRep
.listValue
.maxLen
;
6180 dupPtr
->internalRep
.listValue
.ele
=
6181 Jim_Alloc(sizeof(Jim_Obj
*) * srcPtr
->internalRep
.listValue
.maxLen
);
6182 memcpy(dupPtr
->internalRep
.listValue
.ele
, srcPtr
->internalRep
.listValue
.ele
,
6183 sizeof(Jim_Obj
*) * srcPtr
->internalRep
.listValue
.len
);
6184 for (i
= 0; i
< dupPtr
->internalRep
.listValue
.len
; i
++) {
6185 Jim_IncrRefCount(dupPtr
->internalRep
.listValue
.ele
[i
]);
6187 dupPtr
->typePtr
= &listObjType
;
6190 /* The following function checks if a given string can be encoded
6191 * into a list element without any kind of quoting, surrounded by braces,
6192 * or using escapes to quote. */
6193 #define JIM_ELESTR_SIMPLE 0
6194 #define JIM_ELESTR_BRACE 1
6195 #define JIM_ELESTR_QUOTE 2
6196 static unsigned char ListElementQuotingType(const char *s
, int len
)
6198 int i
, level
, blevel
, trySimple
= 1;
6200 /* Try with the SIMPLE case */
6202 return JIM_ELESTR_BRACE
;
6203 if (s
[0] == '"' || s
[0] == '{') {
6207 for (i
= 0; i
< len
; i
++) {
6228 return JIM_ELESTR_SIMPLE
;
6231 /* Test if it's possible to do with braces */
6232 if (s
[len
- 1] == '\\')
6233 return JIM_ELESTR_QUOTE
;
6236 for (i
= 0; i
< len
; i
++) {
6244 return JIM_ELESTR_QUOTE
;
6253 if (s
[i
+ 1] == '\n')
6254 return JIM_ELESTR_QUOTE
;
6255 else if (s
[i
+ 1] != '\0')
6261 return JIM_ELESTR_QUOTE
;
6266 return JIM_ELESTR_BRACE
;
6267 for (i
= 0; i
< len
; i
++) {
6281 return JIM_ELESTR_BRACE
;
6285 return JIM_ELESTR_SIMPLE
;
6287 return JIM_ELESTR_QUOTE
;
6290 /* Backslashes-escapes the null-terminated string 's' into the buffer at 'q'
6291 * The buffer must be at least strlen(s) * 2 + 1 bytes long for the worst-case
6293 * Returns the length of the result.
6295 static int BackslashQuoteString(const char *s
, int len
, char *q
)
6348 static void JimMakeListStringRep(Jim_Obj
*objPtr
, Jim_Obj
**objv
, int objc
)
6350 #define STATIC_QUOTING_LEN 32
6351 int i
, bufLen
, realLength
;
6354 unsigned char *quotingType
, staticQuoting
[STATIC_QUOTING_LEN
];
6356 /* Estimate the space needed. */
6357 if (objc
> STATIC_QUOTING_LEN
) {
6358 quotingType
= Jim_Alloc(objc
);
6361 quotingType
= staticQuoting
;
6364 for (i
= 0; i
< objc
; i
++) {
6367 strRep
= Jim_GetString(objv
[i
], &len
);
6368 quotingType
[i
] = ListElementQuotingType(strRep
, len
);
6369 switch (quotingType
[i
]) {
6370 case JIM_ELESTR_SIMPLE
:
6371 if (i
!= 0 || strRep
[0] != '#') {
6375 /* Special case '#' on first element needs braces */
6376 quotingType
[i
] = JIM_ELESTR_BRACE
;
6378 case JIM_ELESTR_BRACE
:
6381 case JIM_ELESTR_QUOTE
:
6385 bufLen
++; /* elements separator. */
6389 /* Generate the string rep. */
6390 p
= objPtr
->bytes
= Jim_Alloc(bufLen
+ 1);
6392 for (i
= 0; i
< objc
; i
++) {
6395 strRep
= Jim_GetString(objv
[i
], &len
);
6397 switch (quotingType
[i
]) {
6398 case JIM_ELESTR_SIMPLE
:
6399 memcpy(p
, strRep
, len
);
6403 case JIM_ELESTR_BRACE
:
6405 memcpy(p
, strRep
, len
);
6408 realLength
+= len
+ 2;
6410 case JIM_ELESTR_QUOTE
:
6411 if (i
== 0 && strRep
[0] == '#') {
6415 qlen
= BackslashQuoteString(strRep
, len
, p
);
6420 /* Add a separating space */
6421 if (i
+ 1 != objc
) {
6426 *p
= '\0'; /* nul term. */
6427 objPtr
->length
= realLength
;
6429 if (quotingType
!= staticQuoting
) {
6430 Jim_Free(quotingType
);
6434 static void UpdateStringOfList(struct Jim_Obj
*objPtr
)
6436 JimMakeListStringRep(objPtr
, objPtr
->internalRep
.listValue
.ele
, objPtr
->internalRep
.listValue
.len
);
6439 static int SetListFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
)
6441 struct JimParserCtx parser
;
6444 Jim_Obj
*fileNameObj
;
6447 if (objPtr
->typePtr
== &listObjType
) {
6451 /* Optimise dict -> list for object with no string rep. Note that this may only save a little time, but
6452 * it also preserves any source location of the dict elements
6453 * which can be very useful
6455 if (Jim_IsDict(objPtr
) && objPtr
->bytes
== NULL
) {
6456 Jim_Obj
**listObjPtrPtr
;
6460 listObjPtrPtr
= JimDictPairs(objPtr
, &len
);
6461 for (i
= 0; i
< len
; i
++) {
6462 Jim_IncrRefCount(listObjPtrPtr
[i
]);
6465 /* Now just switch the internal rep */
6466 Jim_FreeIntRep(interp
, objPtr
);
6467 objPtr
->typePtr
= &listObjType
;
6468 objPtr
->internalRep
.listValue
.len
= len
;
6469 objPtr
->internalRep
.listValue
.maxLen
= len
;
6470 objPtr
->internalRep
.listValue
.ele
= listObjPtrPtr
;
6475 /* Try to preserve information about filename / line number */
6476 if (objPtr
->typePtr
== &sourceObjType
) {
6477 fileNameObj
= objPtr
->internalRep
.sourceValue
.fileNameObj
;
6478 linenr
= objPtr
->internalRep
.sourceValue
.lineNumber
;
6481 fileNameObj
= interp
->emptyObj
;
6484 Jim_IncrRefCount(fileNameObj
);
6486 /* Get the string representation */
6487 str
= Jim_GetString(objPtr
, &strLen
);
6489 /* Free the old internal repr just now and initialize the
6490 * new one just now. The string->list conversion can't fail. */
6491 Jim_FreeIntRep(interp
, objPtr
);
6492 objPtr
->typePtr
= &listObjType
;
6493 objPtr
->internalRep
.listValue
.len
= 0;
6494 objPtr
->internalRep
.listValue
.maxLen
= 0;
6495 objPtr
->internalRep
.listValue
.ele
= NULL
;
6497 /* Convert into a list */
6499 JimParserInit(&parser
, str
, strLen
, linenr
);
6500 while (!parser
.eof
) {
6501 Jim_Obj
*elementPtr
;
6503 JimParseList(&parser
);
6504 if (parser
.tt
!= JIM_TT_STR
&& parser
.tt
!= JIM_TT_ESC
)
6506 elementPtr
= JimParserGetTokenObj(interp
, &parser
);
6507 JimSetSourceInfo(interp
, elementPtr
, fileNameObj
, parser
.tline
);
6508 ListAppendElement(objPtr
, elementPtr
);
6511 Jim_DecrRefCount(interp
, fileNameObj
);
6515 Jim_Obj
*Jim_NewListObj(Jim_Interp
*interp
, Jim_Obj
*const *elements
, int len
)
6519 objPtr
= Jim_NewObj(interp
);
6520 objPtr
->typePtr
= &listObjType
;
6521 objPtr
->bytes
= NULL
;
6522 objPtr
->internalRep
.listValue
.ele
= NULL
;
6523 objPtr
->internalRep
.listValue
.len
= 0;
6524 objPtr
->internalRep
.listValue
.maxLen
= 0;
6527 ListInsertElements(objPtr
, 0, len
, elements
);
6533 /* Return a vector of Jim_Obj with the elements of a Jim list, and the
6534 * length of the vector. Note that the user of this function should make
6535 * sure that the list object can't shimmer while the vector returned
6536 * is in use, this vector is the one stored inside the internal representation
6537 * of the list object. This function is not exported, extensions should
6538 * always access to the List object elements using Jim_ListIndex(). */
6539 static void JimListGetElements(Jim_Interp
*interp
, Jim_Obj
*listObj
, int *listLen
,
6542 *listLen
= Jim_ListLength(interp
, listObj
);
6543 *listVec
= listObj
->internalRep
.listValue
.ele
;
6546 /* Sorting uses ints, but commands may return wide */
6547 static int JimSign(jim_wide w
)
6558 /* ListSortElements type values */
6574 int (*subfn
)(Jim_Obj
**, Jim_Obj
**);
6577 static struct lsort_info
*sort_info
;
6579 static int ListSortIndexHelper(Jim_Obj
**lhsObj
, Jim_Obj
**rhsObj
)
6581 Jim_Obj
*lObj
, *rObj
;
6583 if (Jim_ListIndex(sort_info
->interp
, *lhsObj
, sort_info
->index
, &lObj
, JIM_ERRMSG
) != JIM_OK
||
6584 Jim_ListIndex(sort_info
->interp
, *rhsObj
, sort_info
->index
, &rObj
, JIM_ERRMSG
) != JIM_OK
) {
6585 longjmp(sort_info
->jmpbuf
, JIM_ERR
);
6587 return sort_info
->subfn(&lObj
, &rObj
);
6590 /* Sort the internal rep of a list. */
6591 static int ListSortString(Jim_Obj
**lhsObj
, Jim_Obj
**rhsObj
)
6593 return Jim_StringCompareObj(sort_info
->interp
, *lhsObj
, *rhsObj
, 0) * sort_info
->order
;
6596 static int ListSortStringNoCase(Jim_Obj
**lhsObj
, Jim_Obj
**rhsObj
)
6598 return Jim_StringCompareObj(sort_info
->interp
, *lhsObj
, *rhsObj
, 1) * sort_info
->order
;
6601 static int ListSortInteger(Jim_Obj
**lhsObj
, Jim_Obj
**rhsObj
)
6603 jim_wide lhs
= 0, rhs
= 0;
6605 if (Jim_GetWide(sort_info
->interp
, *lhsObj
, &lhs
) != JIM_OK
||
6606 Jim_GetWide(sort_info
->interp
, *rhsObj
, &rhs
) != JIM_OK
) {
6607 longjmp(sort_info
->jmpbuf
, JIM_ERR
);
6610 return JimSign(lhs
- rhs
) * sort_info
->order
;
6613 static int ListSortReal(Jim_Obj
**lhsObj
, Jim_Obj
**rhsObj
)
6615 double lhs
= 0, rhs
= 0;
6617 if (Jim_GetDouble(sort_info
->interp
, *lhsObj
, &lhs
) != JIM_OK
||
6618 Jim_GetDouble(sort_info
->interp
, *rhsObj
, &rhs
) != JIM_OK
) {
6619 longjmp(sort_info
->jmpbuf
, JIM_ERR
);
6625 return sort_info
->order
;
6627 return -sort_info
->order
;
6630 static int ListSortCommand(Jim_Obj
**lhsObj
, Jim_Obj
**rhsObj
)
6632 Jim_Obj
*compare_script
;
6637 /* This must be a valid list */
6638 compare_script
= Jim_DuplicateObj(sort_info
->interp
, sort_info
->command
);
6639 Jim_ListAppendElement(sort_info
->interp
, compare_script
, *lhsObj
);
6640 Jim_ListAppendElement(sort_info
->interp
, compare_script
, *rhsObj
);
6642 rc
= Jim_EvalObj(sort_info
->interp
, compare_script
);
6644 if (rc
!= JIM_OK
|| Jim_GetWide(sort_info
->interp
, Jim_GetResult(sort_info
->interp
), &ret
) != JIM_OK
) {
6645 longjmp(sort_info
->jmpbuf
, rc
);
6648 return JimSign(ret
) * sort_info
->order
;
6651 /* Remove duplicate elements from the (sorted) list in-place, according to the
6652 * comparison function, comp.
6654 * Note that the last unique value is kept, not the first
6656 static void ListRemoveDuplicates(Jim_Obj
*listObjPtr
, int (*comp
)(Jim_Obj
**lhs
, Jim_Obj
**rhs
))
6660 Jim_Obj
**ele
= listObjPtr
->internalRep
.listValue
.ele
;
6662 for (src
= 1; src
< listObjPtr
->internalRep
.listValue
.len
; src
++) {
6663 if (comp(&ele
[dst
], &ele
[src
]) == 0) {
6664 /* Match, so replace the dest with the current source */
6665 Jim_DecrRefCount(sort_info
->interp
, ele
[dst
]);
6668 /* No match, so keep the current source and move to the next destination */
6671 ele
[dst
] = ele
[src
];
6674 /* At end of list, keep the final element unless all elements were kept */
6676 if (dst
< listObjPtr
->internalRep
.listValue
.len
) {
6677 ele
[dst
] = ele
[src
];
6680 /* Set the new length */
6681 listObjPtr
->internalRep
.listValue
.len
= dst
;
6684 /* Sort a list *in place*. MUST be called with a non-shared list. */
6685 static int ListSortElements(Jim_Interp
*interp
, Jim_Obj
*listObjPtr
, struct lsort_info
*info
)
6687 struct lsort_info
*prev_info
;
6689 typedef int (qsort_comparator
) (const void *, const void *);
6690 int (*fn
) (Jim_Obj
**, Jim_Obj
**);
6695 JimPanic((Jim_IsShared(listObjPtr
), "ListSortElements called with shared object"));
6696 SetListFromAny(interp
, listObjPtr
);
6698 /* Allow lsort to be called reentrantly */
6699 prev_info
= sort_info
;
6702 vector
= listObjPtr
->internalRep
.listValue
.ele
;
6703 len
= listObjPtr
->internalRep
.listValue
.len
;
6704 switch (info
->type
) {
6705 case JIM_LSORT_ASCII
:
6706 fn
= ListSortString
;
6708 case JIM_LSORT_NOCASE
:
6709 fn
= ListSortStringNoCase
;
6711 case JIM_LSORT_INTEGER
:
6712 fn
= ListSortInteger
;
6714 case JIM_LSORT_REAL
:
6717 case JIM_LSORT_COMMAND
:
6718 fn
= ListSortCommand
;
6721 fn
= NULL
; /* avoid warning */
6722 JimPanic((1, "ListSort called with invalid sort type"));
6723 return -1; /* Should not be run but keeps static analysers happy */
6726 if (info
->indexed
) {
6727 /* Need to interpose a "list index" function */
6729 fn
= ListSortIndexHelper
;
6732 if ((rc
= setjmp(info
->jmpbuf
)) == 0) {
6733 qsort(vector
, len
, sizeof(Jim_Obj
*), (qsort_comparator
*) fn
);
6735 if (info
->unique
&& len
> 1) {
6736 ListRemoveDuplicates(listObjPtr
, fn
);
6739 Jim_InvalidateStringRep(listObjPtr
);
6741 sort_info
= prev_info
;
6746 /* This is the low-level function to insert elements into a list.
6747 * The higher-level Jim_ListInsertElements() performs shared object
6748 * check and invalidates the string repr. This version is used
6749 * in the internals of the List Object and is not exported.
6751 * NOTE: this function can be called only against objects
6752 * with internal type of List.
6754 * An insertion point (idx) of -1 means end-of-list.
6756 static void ListInsertElements(Jim_Obj
*listPtr
, int idx
, int elemc
, Jim_Obj
*const *elemVec
)
6758 int currentLen
= listPtr
->internalRep
.listValue
.len
;
6759 int requiredLen
= currentLen
+ elemc
;
6763 if (requiredLen
> listPtr
->internalRep
.listValue
.maxLen
) {
6764 if (requiredLen
< 2) {
6765 /* Don't do allocations of under 4 pointers. */
6772 listPtr
->internalRep
.listValue
.ele
= Jim_Realloc(listPtr
->internalRep
.listValue
.ele
,
6773 sizeof(Jim_Obj
*) * requiredLen
);
6775 listPtr
->internalRep
.listValue
.maxLen
= requiredLen
;
6780 point
= listPtr
->internalRep
.listValue
.ele
+ idx
;
6781 memmove(point
+ elemc
, point
, (currentLen
- idx
) * sizeof(Jim_Obj
*));
6782 for (i
= 0; i
< elemc
; ++i
) {
6783 point
[i
] = elemVec
[i
];
6784 Jim_IncrRefCount(point
[i
]);
6786 listPtr
->internalRep
.listValue
.len
+= elemc
;
6789 /* Convenience call to ListInsertElements() to append a single element.
6791 static void ListAppendElement(Jim_Obj
*listPtr
, Jim_Obj
*objPtr
)
6793 ListInsertElements(listPtr
, -1, 1, &objPtr
);
6796 /* Appends every element of appendListPtr into listPtr.
6797 * Both have to be of the list type.
6798 * Convenience call to ListInsertElements()
6800 static void ListAppendList(Jim_Obj
*listPtr
, Jim_Obj
*appendListPtr
)
6802 ListInsertElements(listPtr
, -1,
6803 appendListPtr
->internalRep
.listValue
.len
, appendListPtr
->internalRep
.listValue
.ele
);
6806 void Jim_ListAppendElement(Jim_Interp
*interp
, Jim_Obj
*listPtr
, Jim_Obj
*objPtr
)
6808 JimPanic((Jim_IsShared(listPtr
), "Jim_ListAppendElement called with shared object"));
6809 SetListFromAny(interp
, listPtr
);
6810 Jim_InvalidateStringRep(listPtr
);
6811 ListAppendElement(listPtr
, objPtr
);
6814 void Jim_ListAppendList(Jim_Interp
*interp
, Jim_Obj
*listPtr
, Jim_Obj
*appendListPtr
)
6816 JimPanic((Jim_IsShared(listPtr
), "Jim_ListAppendList called with shared object"));
6817 SetListFromAny(interp
, listPtr
);
6818 SetListFromAny(interp
, appendListPtr
);
6819 Jim_InvalidateStringRep(listPtr
);
6820 ListAppendList(listPtr
, appendListPtr
);
6823 int Jim_ListLength(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
6825 SetListFromAny(interp
, objPtr
);
6826 return objPtr
->internalRep
.listValue
.len
;
6829 void Jim_ListInsertElements(Jim_Interp
*interp
, Jim_Obj
*listPtr
, int idx
,
6830 int objc
, Jim_Obj
*const *objVec
)
6832 JimPanic((Jim_IsShared(listPtr
), "Jim_ListInsertElement called with shared object"));
6833 SetListFromAny(interp
, listPtr
);
6834 if (idx
>= 0 && idx
> listPtr
->internalRep
.listValue
.len
)
6835 idx
= listPtr
->internalRep
.listValue
.len
;
6838 Jim_InvalidateStringRep(listPtr
);
6839 ListInsertElements(listPtr
, idx
, objc
, objVec
);
6842 Jim_Obj
*Jim_ListGetIndex(Jim_Interp
*interp
, Jim_Obj
*listPtr
, int idx
)
6844 SetListFromAny(interp
, listPtr
);
6845 if ((idx
>= 0 && idx
>= listPtr
->internalRep
.listValue
.len
) ||
6846 (idx
< 0 && (-idx
- 1) >= listPtr
->internalRep
.listValue
.len
)) {
6850 idx
= listPtr
->internalRep
.listValue
.len
+ idx
;
6851 return listPtr
->internalRep
.listValue
.ele
[idx
];
6854 int Jim_ListIndex(Jim_Interp
*interp
, Jim_Obj
*listPtr
, int idx
, Jim_Obj
**objPtrPtr
, int flags
)
6856 *objPtrPtr
= Jim_ListGetIndex(interp
, listPtr
, idx
);
6857 if (*objPtrPtr
== NULL
) {
6858 if (flags
& JIM_ERRMSG
) {
6859 Jim_SetResultString(interp
, "list index out of range", -1);
6866 static int ListSetIndex(Jim_Interp
*interp
, Jim_Obj
*listPtr
, int idx
,
6867 Jim_Obj
*newObjPtr
, int flags
)
6869 SetListFromAny(interp
, listPtr
);
6870 if ((idx
>= 0 && idx
>= listPtr
->internalRep
.listValue
.len
) ||
6871 (idx
< 0 && (-idx
- 1) >= listPtr
->internalRep
.listValue
.len
)) {
6872 if (flags
& JIM_ERRMSG
) {
6873 Jim_SetResultString(interp
, "list index out of range", -1);
6878 idx
= listPtr
->internalRep
.listValue
.len
+ idx
;
6879 Jim_DecrRefCount(interp
, listPtr
->internalRep
.listValue
.ele
[idx
]);
6880 listPtr
->internalRep
.listValue
.ele
[idx
] = newObjPtr
;
6881 Jim_IncrRefCount(newObjPtr
);
6885 /* Modify the list stored in the variable named 'varNamePtr'
6886 * setting the element specified by the 'indexc' indexes objects in 'indexv',
6887 * with the new element 'newObjptr'. (implements the [lset] command) */
6888 int Jim_ListSetIndex(Jim_Interp
*interp
, Jim_Obj
*varNamePtr
,
6889 Jim_Obj
*const *indexv
, int indexc
, Jim_Obj
*newObjPtr
)
6891 Jim_Obj
*varObjPtr
, *objPtr
, *listObjPtr
;
6894 varObjPtr
= objPtr
= Jim_GetVariable(interp
, varNamePtr
, JIM_ERRMSG
| JIM_UNSHARED
);
6897 if ((shared
= Jim_IsShared(objPtr
)))
6898 varObjPtr
= objPtr
= Jim_DuplicateObj(interp
, objPtr
);
6899 for (i
= 0; i
< indexc
- 1; i
++) {
6900 listObjPtr
= objPtr
;
6901 if (Jim_GetIndex(interp
, indexv
[i
], &idx
) != JIM_OK
)
6903 if (Jim_ListIndex(interp
, listObjPtr
, idx
, &objPtr
, JIM_ERRMSG
) != JIM_OK
) {
6906 if (Jim_IsShared(objPtr
)) {
6907 objPtr
= Jim_DuplicateObj(interp
, objPtr
);
6908 ListSetIndex(interp
, listObjPtr
, idx
, objPtr
, JIM_NONE
);
6910 Jim_InvalidateStringRep(listObjPtr
);
6912 if (Jim_GetIndex(interp
, indexv
[indexc
- 1], &idx
) != JIM_OK
)
6914 if (ListSetIndex(interp
, objPtr
, idx
, newObjPtr
, JIM_ERRMSG
) == JIM_ERR
)
6916 Jim_InvalidateStringRep(objPtr
);
6917 Jim_InvalidateStringRep(varObjPtr
);
6918 if (Jim_SetVariable(interp
, varNamePtr
, varObjPtr
) != JIM_OK
)
6920 Jim_SetResult(interp
, varObjPtr
);
6924 Jim_FreeNewObj(interp
, varObjPtr
);
6929 Jim_Obj
*Jim_ListJoin(Jim_Interp
*interp
, Jim_Obj
*listObjPtr
, const char *joinStr
, int joinStrLen
)
6932 int listLen
= Jim_ListLength(interp
, listObjPtr
);
6933 Jim_Obj
*resObjPtr
= Jim_NewEmptyStringObj(interp
);
6935 for (i
= 0; i
< listLen
; ) {
6936 Jim_AppendObj(interp
, resObjPtr
, Jim_ListGetIndex(interp
, listObjPtr
, i
));
6937 if (++i
!= listLen
) {
6938 Jim_AppendString(interp
, resObjPtr
, joinStr
, joinStrLen
);
6944 Jim_Obj
*Jim_ConcatObj(Jim_Interp
*interp
, int objc
, Jim_Obj
*const *objv
)
6948 /* If all the objects in objv are lists,
6949 * it's possible to return a list as result, that's the
6950 * concatenation of all the lists. */
6951 for (i
= 0; i
< objc
; i
++) {
6952 if (!Jim_IsList(objv
[i
]))
6956 Jim_Obj
*objPtr
= Jim_NewListObj(interp
, NULL
, 0);
6958 for (i
= 0; i
< objc
; i
++)
6959 ListAppendList(objPtr
, objv
[i
]);
6963 /* Else... we have to glue strings together */
6964 int len
= 0, objLen
;
6967 /* Compute the length */
6968 for (i
= 0; i
< objc
; i
++) {
6969 len
+= Jim_Length(objv
[i
]);
6973 /* Create the string rep, and a string object holding it. */
6974 p
= bytes
= Jim_Alloc(len
+ 1);
6975 for (i
= 0; i
< objc
; i
++) {
6976 const char *s
= Jim_GetString(objv
[i
], &objLen
);
6978 /* Remove leading space */
6979 while (objLen
&& isspace(UCHAR(*s
))) {
6984 /* And trailing space */
6985 while (objLen
&& isspace(UCHAR(s
[objLen
- 1]))) {
6986 /* Handle trailing backslash-space case */
6987 if (objLen
> 1 && s
[objLen
- 2] == '\\') {
6993 memcpy(p
, s
, objLen
);
6995 if (i
+ 1 != objc
) {
6999 /* Drop the space calculated for this
7000 * element that is instead null. */
7006 return Jim_NewStringObjNoAlloc(interp
, bytes
, len
);
7010 /* Returns a list composed of the elements in the specified range.
7011 * first and start are directly accepted as Jim_Objects and
7012 * processed for the end?-index? case. */
7013 Jim_Obj
*Jim_ListRange(Jim_Interp
*interp
, Jim_Obj
*listObjPtr
, Jim_Obj
*firstObjPtr
,
7014 Jim_Obj
*lastObjPtr
)
7019 if (Jim_GetIndex(interp
, firstObjPtr
, &first
) != JIM_OK
||
7020 Jim_GetIndex(interp
, lastObjPtr
, &last
) != JIM_OK
)
7022 len
= Jim_ListLength(interp
, listObjPtr
); /* will convert into list */
7023 first
= JimRelToAbsIndex(len
, first
);
7024 last
= JimRelToAbsIndex(len
, last
);
7025 JimRelToAbsRange(len
, &first
, &last
, &rangeLen
);
7026 if (first
== 0 && last
== len
) {
7029 return Jim_NewListObj(interp
, listObjPtr
->internalRep
.listValue
.ele
+ first
, rangeLen
);
7032 /* -----------------------------------------------------------------------------
7034 * ---------------------------------------------------------------------------*/
7035 static void FreeDictInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
);
7036 static void DupDictInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
);
7037 static void UpdateStringOfDict(struct Jim_Obj
*objPtr
);
7038 static int SetDictFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
);
7040 /* Dict HashTable Type.
7042 * Keys and Values are Jim objects. */
7044 static unsigned int JimObjectHTHashFunction(const void *key
)
7047 const char *str
= Jim_GetString((Jim_Obj
*)key
, &len
);
7048 return Jim_GenHashFunction((const unsigned char *)str
, len
);
7051 static int JimObjectHTKeyCompare(void *privdata
, const void *key1
, const void *key2
)
7053 return Jim_StringEqObj((Jim_Obj
*)key1
, (Jim_Obj
*)key2
);
7056 static void *JimObjectHTKeyValDup(void *privdata
, const void *val
)
7058 Jim_IncrRefCount((Jim_Obj
*)val
);
7062 static void JimObjectHTKeyValDestructor(void *interp
, void *val
)
7064 Jim_DecrRefCount(interp
, (Jim_Obj
*)val
);
7067 static const Jim_HashTableType JimDictHashTableType
= {
7068 JimObjectHTHashFunction
, /* hash function */
7069 JimObjectHTKeyValDup
, /* key dup */
7070 JimObjectHTKeyValDup
, /* val dup */
7071 JimObjectHTKeyCompare
, /* key compare */
7072 JimObjectHTKeyValDestructor
, /* key destructor */
7073 JimObjectHTKeyValDestructor
/* val destructor */
7076 /* Note that while the elements of the dict may contain references,
7077 * the list object itself can't. This basically means that the
7078 * dict object string representation as a whole can't contain references
7079 * that are not presents in the single elements. */
7080 static const Jim_ObjType dictObjType
= {
7082 FreeDictInternalRep
,
7088 void FreeDictInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
7090 JIM_NOTUSED(interp
);
7092 Jim_FreeHashTable(objPtr
->internalRep
.ptr
);
7093 Jim_Free(objPtr
->internalRep
.ptr
);
7096 void DupDictInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
)
7098 Jim_HashTable
*ht
, *dupHt
;
7099 Jim_HashTableIterator htiter
;
7102 /* Create a new hash table */
7103 ht
= srcPtr
->internalRep
.ptr
;
7104 dupHt
= Jim_Alloc(sizeof(*dupHt
));
7105 Jim_InitHashTable(dupHt
, &JimDictHashTableType
, interp
);
7107 Jim_ExpandHashTable(dupHt
, ht
->size
);
7108 /* Copy every element from the source to the dup hash table */
7109 JimInitHashTableIterator(ht
, &htiter
);
7110 while ((he
= Jim_NextHashEntry(&htiter
)) != NULL
) {
7111 Jim_AddHashEntry(dupHt
, he
->key
, he
->u
.val
);
7114 dupPtr
->internalRep
.ptr
= dupHt
;
7115 dupPtr
->typePtr
= &dictObjType
;
7118 static Jim_Obj
**JimDictPairs(Jim_Obj
*dictPtr
, int *len
)
7121 Jim_HashTableIterator htiter
;
7126 ht
= dictPtr
->internalRep
.ptr
;
7128 /* Turn the hash table into a flat vector of Jim_Objects. */
7129 objv
= Jim_Alloc((ht
->used
* 2) * sizeof(Jim_Obj
*));
7130 JimInitHashTableIterator(ht
, &htiter
);
7132 while ((he
= Jim_NextHashEntry(&htiter
)) != NULL
) {
7133 objv
[i
++] = Jim_GetHashEntryKey(he
);
7134 objv
[i
++] = Jim_GetHashEntryVal(he
);
7140 static void UpdateStringOfDict(struct Jim_Obj
*objPtr
)
7142 /* Turn the hash table into a flat vector of Jim_Objects. */
7144 Jim_Obj
**objv
= JimDictPairs(objPtr
, &len
);
7146 /* And now generate the string rep as a list */
7147 JimMakeListStringRep(objPtr
, objv
, len
);
7152 static int SetDictFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
)
7156 if (objPtr
->typePtr
== &dictObjType
) {
7160 if (Jim_IsList(objPtr
) && Jim_IsShared(objPtr
)) {
7161 /* A shared list, so get the string representation now to avoid
7162 * changing the order in case of fast conversion to dict.
7167 /* For simplicity, convert a non-list object to a list and then to a dict */
7168 listlen
= Jim_ListLength(interp
, objPtr
);
7170 Jim_SetResultString(interp
, "missing value to go with key", -1);
7174 /* Converting from a list to a dict can't fail */
7178 ht
= Jim_Alloc(sizeof(*ht
));
7179 Jim_InitHashTable(ht
, &JimDictHashTableType
, interp
);
7181 for (i
= 0; i
< listlen
; i
+= 2) {
7182 Jim_Obj
*keyObjPtr
= Jim_ListGetIndex(interp
, objPtr
, i
);
7183 Jim_Obj
*valObjPtr
= Jim_ListGetIndex(interp
, objPtr
, i
+ 1);
7185 Jim_ReplaceHashEntry(ht
, keyObjPtr
, valObjPtr
);
7188 Jim_FreeIntRep(interp
, objPtr
);
7189 objPtr
->typePtr
= &dictObjType
;
7190 objPtr
->internalRep
.ptr
= ht
;
7196 /* Dict object API */
7198 /* Add an element to a dict. objPtr must be of the "dict" type.
7199 * The higher-level exported function is Jim_DictAddElement().
7200 * If an element with the specified key already exists, the value
7201 * associated is replaced with the new one.
7203 * if valueObjPtr == NULL, the key is instead removed if it exists. */
7204 static int DictAddElement(Jim_Interp
*interp
, Jim_Obj
*objPtr
,
7205 Jim_Obj
*keyObjPtr
, Jim_Obj
*valueObjPtr
)
7207 Jim_HashTable
*ht
= objPtr
->internalRep
.ptr
;
7209 if (valueObjPtr
== NULL
) { /* unset */
7210 return Jim_DeleteHashEntry(ht
, keyObjPtr
);
7212 Jim_ReplaceHashEntry(ht
, keyObjPtr
, valueObjPtr
);
7216 /* Add an element, higher-level interface for DictAddElement().
7217 * If valueObjPtr == NULL, the key is removed if it exists. */
7218 int Jim_DictAddElement(Jim_Interp
*interp
, Jim_Obj
*objPtr
,
7219 Jim_Obj
*keyObjPtr
, Jim_Obj
*valueObjPtr
)
7221 JimPanic((Jim_IsShared(objPtr
), "Jim_DictAddElement called with shared object"));
7222 if (SetDictFromAny(interp
, objPtr
) != JIM_OK
) {
7225 Jim_InvalidateStringRep(objPtr
);
7226 return DictAddElement(interp
, objPtr
, keyObjPtr
, valueObjPtr
);
7229 Jim_Obj
*Jim_NewDictObj(Jim_Interp
*interp
, Jim_Obj
*const *elements
, int len
)
7234 JimPanic((len
% 2, "Jim_NewDictObj() 'len' argument must be even"));
7236 objPtr
= Jim_NewObj(interp
);
7237 objPtr
->typePtr
= &dictObjType
;
7238 objPtr
->bytes
= NULL
;
7239 objPtr
->internalRep
.ptr
= Jim_Alloc(sizeof(Jim_HashTable
));
7240 Jim_InitHashTable(objPtr
->internalRep
.ptr
, &JimDictHashTableType
, interp
);
7241 for (i
= 0; i
< len
; i
+= 2)
7242 DictAddElement(interp
, objPtr
, elements
[i
], elements
[i
+ 1]);
7246 /* Return the value associated to the specified dict key
7247 * Returns JIM_OK if OK, JIM_ERR if entry not found or -1 if can't create dict value
7249 * Sets *objPtrPtr to non-NULL only upon success.
7251 int Jim_DictKey(Jim_Interp
*interp
, Jim_Obj
*dictPtr
, Jim_Obj
*keyPtr
,
7252 Jim_Obj
**objPtrPtr
, int flags
)
7257 if (SetDictFromAny(interp
, dictPtr
) != JIM_OK
) {
7260 ht
= dictPtr
->internalRep
.ptr
;
7261 if ((he
= Jim_FindHashEntry(ht
, keyPtr
)) == NULL
) {
7262 if (flags
& JIM_ERRMSG
) {
7263 Jim_SetResultFormatted(interp
, "key \"%#s\" not known in dictionary", keyPtr
);
7268 *objPtrPtr
= Jim_GetHashEntryVal(he
);
7273 /* Return an allocated array of key/value pairs for the dictionary. Stores the length in *len */
7274 int Jim_DictPairs(Jim_Interp
*interp
, Jim_Obj
*dictPtr
, Jim_Obj
***objPtrPtr
, int *len
)
7276 if (SetDictFromAny(interp
, dictPtr
) != JIM_OK
) {
7279 *objPtrPtr
= JimDictPairs(dictPtr
, len
);
7285 /* Return the value associated to the specified dict keys */
7286 int Jim_DictKeysVector(Jim_Interp
*interp
, Jim_Obj
*dictPtr
,
7287 Jim_Obj
*const *keyv
, int keyc
, Jim_Obj
**objPtrPtr
, int flags
)
7292 *objPtrPtr
= dictPtr
;
7296 for (i
= 0; i
< keyc
; i
++) {
7299 int rc
= Jim_DictKey(interp
, dictPtr
, keyv
[i
], &objPtr
, flags
);
7305 *objPtrPtr
= dictPtr
;
7309 /* Modify the dict stored into the variable named 'varNamePtr'
7310 * setting the element specified by the 'keyc' keys objects in 'keyv',
7311 * with the new value of the element 'newObjPtr'.
7313 * If newObjPtr == NULL the operation is to remove the given key
7314 * from the dictionary.
7316 * If flags & JIM_ERRMSG, then failure to remove the key is considered an error
7317 * and JIM_ERR is returned. Otherwise it is ignored and JIM_OK is returned.
7319 int Jim_SetDictKeysVector(Jim_Interp
*interp
, Jim_Obj
*varNamePtr
,
7320 Jim_Obj
*const *keyv
, int keyc
, Jim_Obj
*newObjPtr
, int flags
)
7322 Jim_Obj
*varObjPtr
, *objPtr
, *dictObjPtr
;
7325 varObjPtr
= objPtr
= Jim_GetVariable(interp
, varNamePtr
, flags
);
7326 if (objPtr
== NULL
) {
7327 if (newObjPtr
== NULL
&& (flags
& JIM_MUSTEXIST
)) {
7328 /* Cannot remove a key from non existing var */
7331 varObjPtr
= objPtr
= Jim_NewDictObj(interp
, NULL
, 0);
7332 if (Jim_SetVariable(interp
, varNamePtr
, objPtr
) != JIM_OK
) {
7333 Jim_FreeNewObj(interp
, varObjPtr
);
7337 if ((shared
= Jim_IsShared(objPtr
)))
7338 varObjPtr
= objPtr
= Jim_DuplicateObj(interp
, objPtr
);
7339 for (i
= 0; i
< keyc
; i
++) {
7340 dictObjPtr
= objPtr
;
7342 /* Check if it's a valid dictionary */
7343 if (SetDictFromAny(interp
, dictObjPtr
) != JIM_OK
) {
7347 if (i
== keyc
- 1) {
7348 /* Last key: Note that error on unset with missing last key is OK */
7349 if (Jim_DictAddElement(interp
, objPtr
, keyv
[keyc
- 1], newObjPtr
) != JIM_OK
) {
7350 if (newObjPtr
|| (flags
& JIM_MUSTEXIST
)) {
7357 /* Check if the given key exists. */
7358 Jim_InvalidateStringRep(dictObjPtr
);
7359 if (Jim_DictKey(interp
, dictObjPtr
, keyv
[i
], &objPtr
,
7360 newObjPtr
? JIM_NONE
: JIM_ERRMSG
) == JIM_OK
) {
7361 /* This key exists at the current level.
7362 * Make sure it's not shared!. */
7363 if (Jim_IsShared(objPtr
)) {
7364 objPtr
= Jim_DuplicateObj(interp
, objPtr
);
7365 DictAddElement(interp
, dictObjPtr
, keyv
[i
], objPtr
);
7369 /* Key not found. If it's an [unset] operation
7370 * this is an error. Only the last key may not
7372 if (newObjPtr
== NULL
) {
7375 /* Otherwise set an empty dictionary
7376 * as key's value. */
7377 objPtr
= Jim_NewDictObj(interp
, NULL
, 0);
7378 DictAddElement(interp
, dictObjPtr
, keyv
[i
], objPtr
);
7381 /* XXX: Is this necessary? */
7382 Jim_InvalidateStringRep(objPtr
);
7383 Jim_InvalidateStringRep(varObjPtr
);
7384 if (Jim_SetVariable(interp
, varNamePtr
, varObjPtr
) != JIM_OK
) {
7387 Jim_SetResult(interp
, varObjPtr
);
7391 Jim_FreeNewObj(interp
, varObjPtr
);
7396 /* -----------------------------------------------------------------------------
7398 * ---------------------------------------------------------------------------*/
7399 static void UpdateStringOfIndex(struct Jim_Obj
*objPtr
);
7400 static int SetIndexFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
);
7402 static const Jim_ObjType indexObjType
= {
7406 UpdateStringOfIndex
,
7410 static void UpdateStringOfIndex(struct Jim_Obj
*objPtr
)
7412 if (objPtr
->internalRep
.intValue
== -1) {
7413 JimSetStringBytes(objPtr
, "end");
7416 char buf
[JIM_INTEGER_SPACE
+ 1];
7417 if (objPtr
->internalRep
.intValue
>= 0) {
7418 sprintf(buf
, "%d", objPtr
->internalRep
.intValue
);
7422 sprintf(buf
, "end%d", objPtr
->internalRep
.intValue
+ 1);
7424 JimSetStringBytes(objPtr
, buf
);
7428 static int SetIndexFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
7434 /* Get the string representation */
7435 str
= Jim_String(objPtr
);
7437 /* Try to convert into an index */
7438 if (strncmp(str
, "end", 3) == 0) {
7444 idx
= jim_strtol(str
, &endptr
);
7446 if (endptr
== str
) {
7452 /* Now str may include or +<num> or -<num> */
7453 if (*str
== '+' || *str
== '-') {
7454 int sign
= (*str
== '+' ? 1 : -1);
7456 idx
+= sign
* jim_strtol(++str
, &endptr
);
7457 if (str
== endptr
|| *endptr
) {
7462 /* The only thing left should be spaces */
7463 while (isspace(UCHAR(*str
))) {
7474 /* end-1 is repesented as -2 */
7482 /* Free the old internal repr and set the new one. */
7483 Jim_FreeIntRep(interp
, objPtr
);
7484 objPtr
->typePtr
= &indexObjType
;
7485 objPtr
->internalRep
.intValue
= idx
;
7489 Jim_SetResultFormatted(interp
,
7490 "bad index \"%#s\": must be integer?[+-]integer? or end?[+-]integer?", objPtr
);
7494 int Jim_GetIndex(Jim_Interp
*interp
, Jim_Obj
*objPtr
, int *indexPtr
)
7496 /* Avoid shimmering if the object is an integer. */
7497 if (objPtr
->typePtr
== &intObjType
) {
7498 jim_wide val
= JimWideValue(objPtr
);
7501 *indexPtr
= -INT_MAX
;
7502 else if (val
> INT_MAX
)
7503 *indexPtr
= INT_MAX
;
7505 *indexPtr
= (int)val
;
7508 if (objPtr
->typePtr
!= &indexObjType
&& SetIndexFromAny(interp
, objPtr
) == JIM_ERR
)
7510 *indexPtr
= objPtr
->internalRep
.intValue
;
7514 /* -----------------------------------------------------------------------------
7515 * Return Code Object.
7516 * ---------------------------------------------------------------------------*/
7518 /* NOTE: These must be kept in the same order as JIM_OK, JIM_ERR, ... */
7519 static const char * const jimReturnCodes
[] = {
7531 #define jimReturnCodesSize (sizeof(jimReturnCodes)/sizeof(*jimReturnCodes) - 1)
7533 static const Jim_ObjType returnCodeObjType
= {
7541 /* Converts a (standard) return code to a string. Returns "?" for
7542 * non-standard return codes.
7544 const char *Jim_ReturnCode(int code
)
7546 if (code
< 0 || code
>= (int)jimReturnCodesSize
) {
7550 return jimReturnCodes
[code
];
7554 static int SetReturnCodeFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
7559 /* Try to convert into an integer */
7560 if (JimGetWideNoErr(interp
, objPtr
, &wideValue
) != JIM_ERR
)
7561 returnCode
= (int)wideValue
;
7562 else if (Jim_GetEnum(interp
, objPtr
, jimReturnCodes
, &returnCode
, NULL
, JIM_NONE
) != JIM_OK
) {
7563 Jim_SetResultFormatted(interp
, "expected return code but got \"%#s\"", objPtr
);
7566 /* Free the old internal repr and set the new one. */
7567 Jim_FreeIntRep(interp
, objPtr
);
7568 objPtr
->typePtr
= &returnCodeObjType
;
7569 objPtr
->internalRep
.intValue
= returnCode
;
7573 int Jim_GetReturnCode(Jim_Interp
*interp
, Jim_Obj
*objPtr
, int *intPtr
)
7575 if (objPtr
->typePtr
!= &returnCodeObjType
&& SetReturnCodeFromAny(interp
, objPtr
) == JIM_ERR
)
7577 *intPtr
= objPtr
->internalRep
.intValue
;
7581 /* -----------------------------------------------------------------------------
7582 * Expression Parsing
7583 * ---------------------------------------------------------------------------*/
7584 static int JimParseExprOperator(struct JimParserCtx
*pc
);
7585 static int JimParseExprNumber(struct JimParserCtx
*pc
);
7586 static int JimParseExprIrrational(struct JimParserCtx
*pc
);
7587 static int JimParseExprBoolean(struct JimParserCtx
*pc
);
7589 /* expr operator opcodes. */
7592 /* Continues on from the JIM_TT_ space */
7594 /* Binary operators (numbers) */
7595 JIM_EXPROP_MUL
= JIM_TT_EXPR_OP
, /* 20 */
7610 JIM_EXPROP_BITAND
, /* 35 */
7613 JIM_EXPROP_LOGICAND
, /* 38 */
7614 JIM_EXPROP_LOGICOR
, /* 39 */
7615 JIM_EXPROP_TERNARY
, /* 40 */
7616 JIM_EXPROP_COLON
, /* 41 */
7617 JIM_EXPROP_POW
, /* 42 */
7619 /* Binary operators (strings) */
7620 JIM_EXPROP_STREQ
, /* 43 */
7625 /* Unary operators (numbers) */
7626 JIM_EXPROP_NOT
, /* 47 */
7628 JIM_EXPROP_UNARYMINUS
,
7629 JIM_EXPROP_UNARYPLUS
,
7632 JIM_EXPROP_FUNC_INT
, /* 51 */
7633 JIM_EXPROP_FUNC_WIDE
,
7634 JIM_EXPROP_FUNC_ABS
,
7635 JIM_EXPROP_FUNC_DOUBLE
,
7636 JIM_EXPROP_FUNC_ROUND
,
7637 JIM_EXPROP_FUNC_RAND
,
7638 JIM_EXPROP_FUNC_SRAND
,
7640 /* math functions from libm */
7641 JIM_EXPROP_FUNC_SIN
, /* 65 */
7642 JIM_EXPROP_FUNC_COS
,
7643 JIM_EXPROP_FUNC_TAN
,
7644 JIM_EXPROP_FUNC_ASIN
,
7645 JIM_EXPROP_FUNC_ACOS
,
7646 JIM_EXPROP_FUNC_ATAN
,
7647 JIM_EXPROP_FUNC_ATAN2
,
7648 JIM_EXPROP_FUNC_SINH
,
7649 JIM_EXPROP_FUNC_COSH
,
7650 JIM_EXPROP_FUNC_TANH
,
7651 JIM_EXPROP_FUNC_CEIL
,
7652 JIM_EXPROP_FUNC_FLOOR
,
7653 JIM_EXPROP_FUNC_EXP
,
7654 JIM_EXPROP_FUNC_LOG
,
7655 JIM_EXPROP_FUNC_LOG10
,
7656 JIM_EXPROP_FUNC_SQRT
,
7657 JIM_EXPROP_FUNC_POW
,
7658 JIM_EXPROP_FUNC_HYPOT
,
7659 JIM_EXPROP_FUNC_FMOD
,
7662 /* A expression node is either a term or an operator
7663 * If a node is an operator, 'op' points to the details of the operator and it's terms.
7665 struct JimExprNode
{
7666 int type
; /* JIM_TT_xxx */
7667 struct Jim_Obj
*objPtr
; /* The object for a term, or NULL for an operator */
7669 struct JimExprNode
*left
; /* For all operators */
7670 struct JimExprNode
*right
; /* For binary operators */
7671 struct JimExprNode
*ternary
; /* For ternary operator only */
7674 /* Operators table */
7675 typedef struct Jim_ExprOperator
7678 int (*funcop
) (Jim_Interp
*interp
, struct JimExprNode
*opnode
);
7679 unsigned char precedence
;
7680 unsigned char arity
;
7682 unsigned char namelen
;
7685 static int JimExprGetTerm(Jim_Interp
*interp
, struct JimExprNode
*node
, Jim_Obj
**objPtrPtr
);
7686 static int JimExprGetTermBoolean(Jim_Interp
*interp
, struct JimExprNode
*node
);
7687 static int JimExprEvalTermNode(Jim_Interp
*interp
, struct JimExprNode
*node
);
7689 static int JimExprOpNumUnary(Jim_Interp
*interp
, struct JimExprNode
*node
)
7694 jim_wide wA
, wC
= 0;
7697 if ((rc
= JimExprGetTerm(interp
, node
->left
, &A
)) != JIM_OK
) {
7701 if ((A
->typePtr
!= &doubleObjType
|| A
->bytes
) && JimGetWideNoErr(interp
, A
, &wA
) == JIM_OK
) {
7702 switch (node
->type
) {
7703 case JIM_EXPROP_FUNC_INT
:
7704 case JIM_EXPROP_FUNC_WIDE
:
7705 case JIM_EXPROP_FUNC_ROUND
:
7706 case JIM_EXPROP_UNARYPLUS
:
7709 case JIM_EXPROP_FUNC_DOUBLE
:
7713 case JIM_EXPROP_FUNC_ABS
:
7714 wC
= wA
>= 0 ? wA
: -wA
;
7716 case JIM_EXPROP_UNARYMINUS
:
7719 case JIM_EXPROP_NOT
:
7726 else if ((rc
= Jim_GetDouble(interp
, A
, &dA
)) == JIM_OK
) {
7727 switch (node
->type
) {
7728 case JIM_EXPROP_FUNC_INT
:
7729 case JIM_EXPROP_FUNC_WIDE
:
7732 case JIM_EXPROP_FUNC_ROUND
:
7733 wC
= dA
< 0 ? (dA
- 0.5) : (dA
+ 0.5);
7735 case JIM_EXPROP_FUNC_DOUBLE
:
7736 case JIM_EXPROP_UNARYPLUS
:
7740 case JIM_EXPROP_FUNC_ABS
:
7741 #ifdef JIM_MATH_FUNCTIONS
7744 dC
= dA
>= 0 ? dA
: -dA
;
7748 case JIM_EXPROP_UNARYMINUS
:
7752 case JIM_EXPROP_NOT
:
7762 Jim_SetResultInt(interp
, wC
);
7765 Jim_SetResult(interp
, Jim_NewDoubleObj(interp
, dC
));
7769 Jim_DecrRefCount(interp
, A
);
7774 static double JimRandDouble(Jim_Interp
*interp
)
7777 JimRandomBytes(interp
, &x
, sizeof(x
));
7779 return (double)x
/ (unsigned long)~0;
7782 static int JimExprOpIntUnary(Jim_Interp
*interp
, struct JimExprNode
*node
)
7788 if ((rc
= JimExprGetTerm(interp
, node
->left
, &A
)) != JIM_OK
) {
7792 rc
= Jim_GetWide(interp
, A
, &wA
);
7794 switch (node
->type
) {
7795 case JIM_EXPROP_BITNOT
:
7796 Jim_SetResultInt(interp
, ~wA
);
7798 case JIM_EXPROP_FUNC_SRAND
:
7799 JimPrngSeed(interp
, (unsigned char *)&wA
, sizeof(wA
));
7800 Jim_SetResult(interp
, Jim_NewDoubleObj(interp
, JimRandDouble(interp
)));
7807 Jim_DecrRefCount(interp
, A
);
7812 static int JimExprOpNone(Jim_Interp
*interp
, struct JimExprNode
*node
)
7814 JimPanic((node
->type
!= JIM_EXPROP_FUNC_RAND
, "JimExprOpNone only support rand()"));
7816 Jim_SetResult(interp
, Jim_NewDoubleObj(interp
, JimRandDouble(interp
)));
7821 #ifdef JIM_MATH_FUNCTIONS
7822 static int JimExprOpDoubleUnary(Jim_Interp
*interp
, struct JimExprNode
*node
)
7828 if ((rc
= JimExprGetTerm(interp
, node
->left
, &A
)) != JIM_OK
) {
7832 rc
= Jim_GetDouble(interp
, A
, &dA
);
7834 switch (node
->type
) {
7835 case JIM_EXPROP_FUNC_SIN
:
7838 case JIM_EXPROP_FUNC_COS
:
7841 case JIM_EXPROP_FUNC_TAN
:
7844 case JIM_EXPROP_FUNC_ASIN
:
7847 case JIM_EXPROP_FUNC_ACOS
:
7850 case JIM_EXPROP_FUNC_ATAN
:
7853 case JIM_EXPROP_FUNC_SINH
:
7856 case JIM_EXPROP_FUNC_COSH
:
7859 case JIM_EXPROP_FUNC_TANH
:
7862 case JIM_EXPROP_FUNC_CEIL
:
7865 case JIM_EXPROP_FUNC_FLOOR
:
7868 case JIM_EXPROP_FUNC_EXP
:
7871 case JIM_EXPROP_FUNC_LOG
:
7874 case JIM_EXPROP_FUNC_LOG10
:
7877 case JIM_EXPROP_FUNC_SQRT
:
7883 Jim_SetResult(interp
, Jim_NewDoubleObj(interp
, dC
));
7886 Jim_DecrRefCount(interp
, A
);
7892 /* A binary operation on two ints */
7893 static int JimExprOpIntBin(Jim_Interp
*interp
, struct JimExprNode
*node
)
7899 if ((rc
= JimExprGetTerm(interp
, node
->left
, &A
)) != JIM_OK
) {
7902 if ((rc
= JimExprGetTerm(interp
, node
->right
, &B
)) != JIM_OK
) {
7903 Jim_DecrRefCount(interp
, A
);
7909 if (Jim_GetWide(interp
, A
, &wA
) == JIM_OK
&& Jim_GetWide(interp
, B
, &wB
) == JIM_OK
) {
7914 switch (node
->type
) {
7915 case JIM_EXPROP_LSHIFT
:
7918 case JIM_EXPROP_RSHIFT
:
7921 case JIM_EXPROP_BITAND
:
7924 case JIM_EXPROP_BITXOR
:
7927 case JIM_EXPROP_BITOR
:
7930 case JIM_EXPROP_MOD
:
7933 Jim_SetResultString(interp
, "Division by zero", -1);
7940 * This code is tricky: C doesn't guarantee much
7941 * about the quotient or remainder, but Tcl does.
7942 * The remainder always has the same sign as the
7943 * divisor and a smaller absolute value.
7961 case JIM_EXPROP_ROTL
:
7962 case JIM_EXPROP_ROTR
:{
7963 /* uint32_t would be better. But not everyone has inttypes.h? */
7964 unsigned long uA
= (unsigned long)wA
;
7965 unsigned long uB
= (unsigned long)wB
;
7966 const unsigned int S
= sizeof(unsigned long) * 8;
7968 /* Shift left by the word size or more is undefined. */
7971 if (node
->type
== JIM_EXPROP_ROTR
) {
7974 wC
= (unsigned long)(uA
<< uB
) | (uA
>> (S
- uB
));
7980 Jim_SetResultInt(interp
, wC
);
7983 Jim_DecrRefCount(interp
, A
);
7984 Jim_DecrRefCount(interp
, B
);
7990 /* A binary operation on two ints or two doubles (or two strings for some ops) */
7991 static int JimExprOpBin(Jim_Interp
*interp
, struct JimExprNode
*node
)
7994 double dA
, dB
, dC
= 0;
7995 jim_wide wA
, wB
, wC
= 0;
7998 if ((rc
= JimExprGetTerm(interp
, node
->left
, &A
)) != JIM_OK
) {
8001 if ((rc
= JimExprGetTerm(interp
, node
->right
, &B
)) != JIM_OK
) {
8002 Jim_DecrRefCount(interp
, A
);
8006 if ((A
->typePtr
!= &doubleObjType
|| A
->bytes
) &&
8007 (B
->typePtr
!= &doubleObjType
|| B
->bytes
) &&
8008 JimGetWideNoErr(interp
, A
, &wA
) == JIM_OK
&& JimGetWideNoErr(interp
, B
, &wB
) == JIM_OK
) {
8012 switch (node
->type
) {
8013 case JIM_EXPROP_POW
:
8014 case JIM_EXPROP_FUNC_POW
:
8015 if (wA
== 0 && wB
< 0) {
8016 Jim_SetResultString(interp
, "exponentiation of zero by negative power", -1);
8020 wC
= JimPowWide(wA
, wB
);
8022 case JIM_EXPROP_ADD
:
8025 case JIM_EXPROP_SUB
:
8028 case JIM_EXPROP_MUL
:
8031 case JIM_EXPROP_DIV
:
8033 Jim_SetResultString(interp
, "Division by zero", -1);
8041 * This code is tricky: C doesn't guarantee much
8042 * about the quotient or remainder, but Tcl does.
8043 * The remainder always has the same sign as the
8044 * divisor and a smaller absolute value.
8062 case JIM_EXPROP_LTE
:
8065 case JIM_EXPROP_GTE
:
8068 case JIM_EXPROP_NUMEQ
:
8071 case JIM_EXPROP_NUMNE
:
8076 if (Jim_GetDouble(interp
, A
, &dA
) == JIM_OK
&& Jim_GetDouble(interp
, B
, &dB
) == JIM_OK
) {
8077 switch (node
->type
) {
8078 #ifndef JIM_MATH_FUNCTIONS
8079 case JIM_EXPROP_POW
:
8080 case JIM_EXPROP_FUNC_POW
:
8081 case JIM_EXPROP_FUNC_ATAN2
:
8082 case JIM_EXPROP_FUNC_HYPOT
:
8083 case JIM_EXPROP_FUNC_FMOD
:
8084 Jim_SetResultString(interp
, "unsupported", -1);
8088 case JIM_EXPROP_POW
:
8089 case JIM_EXPROP_FUNC_POW
:
8092 case JIM_EXPROP_FUNC_ATAN2
:
8095 case JIM_EXPROP_FUNC_HYPOT
:
8098 case JIM_EXPROP_FUNC_FMOD
:
8102 case JIM_EXPROP_ADD
:
8105 case JIM_EXPROP_SUB
:
8108 case JIM_EXPROP_MUL
:
8111 case JIM_EXPROP_DIV
:
8114 dC
= dA
< 0 ? -INFINITY
: INFINITY
;
8116 dC
= (dA
< 0 ? -1.0 : 1.0) * strtod("Inf", NULL
);
8129 case JIM_EXPROP_LTE
:
8132 case JIM_EXPROP_GTE
:
8135 case JIM_EXPROP_NUMEQ
:
8138 case JIM_EXPROP_NUMNE
:
8144 /* Handle the string case */
8146 /* XXX: Could optimise the eq/ne case by checking lengths */
8147 int i
= Jim_StringCompareObj(interp
, A
, B
, 0);
8149 switch (node
->type
) {
8156 case JIM_EXPROP_LTE
:
8159 case JIM_EXPROP_GTE
:
8162 case JIM_EXPROP_NUMEQ
:
8165 case JIM_EXPROP_NUMNE
:
8170 /* If we get here, it is an error */
8173 Jim_DecrRefCount(interp
, A
);
8174 Jim_DecrRefCount(interp
, B
);
8177 Jim_SetResultInt(interp
, wC
);
8180 Jim_SetResult(interp
, Jim_NewDoubleObj(interp
, dC
));
8184 static int JimSearchList(Jim_Interp
*interp
, Jim_Obj
*listObjPtr
, Jim_Obj
*valObj
)
8189 listlen
= Jim_ListLength(interp
, listObjPtr
);
8190 for (i
= 0; i
< listlen
; i
++) {
8191 if (Jim_StringEqObj(Jim_ListGetIndex(interp
, listObjPtr
, i
), valObj
)) {
8200 static int JimExprOpStrBin(Jim_Interp
*interp
, struct JimExprNode
*node
)
8206 if ((rc
= JimExprGetTerm(interp
, node
->left
, &A
)) != JIM_OK
) {
8209 if ((rc
= JimExprGetTerm(interp
, node
->right
, &B
)) != JIM_OK
) {
8210 Jim_DecrRefCount(interp
, A
);
8214 switch (node
->type
) {
8215 case JIM_EXPROP_STREQ
:
8216 case JIM_EXPROP_STRNE
:
8217 wC
= Jim_StringEqObj(A
, B
);
8218 if (node
->type
== JIM_EXPROP_STRNE
) {
8222 case JIM_EXPROP_STRIN
:
8223 wC
= JimSearchList(interp
, B
, A
);
8225 case JIM_EXPROP_STRNI
:
8226 wC
= !JimSearchList(interp
, B
, A
);
8231 Jim_SetResultInt(interp
, wC
);
8233 Jim_DecrRefCount(interp
, A
);
8234 Jim_DecrRefCount(interp
, B
);
8239 static int ExprBool(Jim_Interp
*interp
, Jim_Obj
*obj
)
8246 /* In case the object is interp->result with refcount 1*/
8247 Jim_IncrRefCount(obj
);
8249 if (Jim_GetLong(interp
, obj
, &l
) == JIM_OK
) {
8252 else if (Jim_GetDouble(interp
, obj
, &d
) == JIM_OK
) {
8255 else if (Jim_GetBoolean(interp
, obj
, &b
) == JIM_OK
) {
8259 Jim_DecrRefCount(interp
, obj
);
8263 static int JimExprOpAnd(Jim_Interp
*interp
, struct JimExprNode
*node
)
8266 int result
= JimExprGetTermBoolean(interp
, node
->left
);
8269 /* true so evaluate right */
8270 result
= JimExprGetTermBoolean(interp
, node
->right
);
8275 Jim_SetResultInt(interp
, result
);
8279 static int JimExprOpOr(Jim_Interp
*interp
, struct JimExprNode
*node
)
8282 int result
= JimExprGetTermBoolean(interp
, node
->left
);
8285 /* false so evaluate right */
8286 result
= JimExprGetTermBoolean(interp
, node
->right
);
8291 Jim_SetResultInt(interp
, result
);
8295 static int JimExprOpTernary(Jim_Interp
*interp
, struct JimExprNode
*node
)
8298 int result
= JimExprGetTermBoolean(interp
, node
->left
);
8301 /* true so select right */
8302 return JimExprEvalTermNode(interp
, node
->right
);
8304 else if (result
== 0) {
8305 /* false so select ternary */
8306 return JimExprEvalTermNode(interp
, node
->ternary
);
8314 OP_FUNC
= 0x0001, /* function syntax */
8315 OP_RIGHT_ASSOC
= 0x0002, /* right associative */
8318 /* name - precedence - arity - opcode
8320 * This array *must* be kept in sync with the JIM_EXPROP enum.
8322 * The following macros pre-compute the string length at compile time.
8324 #define OPRINIT_ATTR(N, P, ARITY, F, ATTR) {N, F, P, ARITY, ATTR, sizeof(N) - 1}
8325 #define OPRINIT(N, P, ARITY, F) OPRINIT_ATTR(N, P, ARITY, F, 0)
8327 static const struct Jim_ExprOperator Jim_ExprOperators
[] = {
8328 OPRINIT("*", 110, 2, JimExprOpBin
),
8329 OPRINIT("/", 110, 2, JimExprOpBin
),
8330 OPRINIT("%", 110, 2, JimExprOpIntBin
),
8332 OPRINIT("-", 100, 2, JimExprOpBin
),
8333 OPRINIT("+", 100, 2, JimExprOpBin
),
8335 OPRINIT("<<", 90, 2, JimExprOpIntBin
),
8336 OPRINIT(">>", 90, 2, JimExprOpIntBin
),
8338 OPRINIT("<<<", 90, 2, JimExprOpIntBin
),
8339 OPRINIT(">>>", 90, 2, JimExprOpIntBin
),
8341 OPRINIT("<", 80, 2, JimExprOpBin
),
8342 OPRINIT(">", 80, 2, JimExprOpBin
),
8343 OPRINIT("<=", 80, 2, JimExprOpBin
),
8344 OPRINIT(">=", 80, 2, JimExprOpBin
),
8346 OPRINIT("==", 70, 2, JimExprOpBin
),
8347 OPRINIT("!=", 70, 2, JimExprOpBin
),
8349 OPRINIT("&", 50, 2, JimExprOpIntBin
),
8350 OPRINIT("^", 49, 2, JimExprOpIntBin
),
8351 OPRINIT("|", 48, 2, JimExprOpIntBin
),
8353 OPRINIT("&&", 10, 2, JimExprOpAnd
),
8354 OPRINIT("||", 9, 2, JimExprOpOr
),
8355 OPRINIT_ATTR("?", 5, 3, JimExprOpTernary
, OP_RIGHT_ASSOC
),
8356 OPRINIT_ATTR(":", 5, 3, NULL
, OP_RIGHT_ASSOC
),
8358 /* Precedence is higher than * and / but lower than ! and ~, and right-associative */
8359 OPRINIT_ATTR("**", 120, 2, JimExprOpBin
, OP_RIGHT_ASSOC
),
8361 OPRINIT("eq", 60, 2, JimExprOpStrBin
),
8362 OPRINIT("ne", 60, 2, JimExprOpStrBin
),
8364 OPRINIT("in", 55, 2, JimExprOpStrBin
),
8365 OPRINIT("ni", 55, 2, JimExprOpStrBin
),
8367 OPRINIT_ATTR("!", 150, 1, JimExprOpNumUnary
, OP_RIGHT_ASSOC
),
8368 OPRINIT_ATTR("~", 150, 1, JimExprOpIntUnary
, OP_RIGHT_ASSOC
),
8369 OPRINIT_ATTR(" -", 150, 1, JimExprOpNumUnary
, OP_RIGHT_ASSOC
),
8370 OPRINIT_ATTR(" +", 150, 1, JimExprOpNumUnary
, OP_RIGHT_ASSOC
),
8374 OPRINIT_ATTR("int", 200, 1, JimExprOpNumUnary
, OP_FUNC
),
8375 OPRINIT_ATTR("wide", 200, 1, JimExprOpNumUnary
, OP_FUNC
),
8376 OPRINIT_ATTR("abs", 200, 1, JimExprOpNumUnary
, OP_FUNC
),
8377 OPRINIT_ATTR("double", 200, 1, JimExprOpNumUnary
, OP_FUNC
),
8378 OPRINIT_ATTR("round", 200, 1, JimExprOpNumUnary
, OP_FUNC
),
8379 OPRINIT_ATTR("rand", 200, 0, JimExprOpNone
, OP_FUNC
),
8380 OPRINIT_ATTR("srand", 200, 1, JimExprOpIntUnary
, OP_FUNC
),
8382 #ifdef JIM_MATH_FUNCTIONS
8383 OPRINIT_ATTR("sin", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8384 OPRINIT_ATTR("cos", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8385 OPRINIT_ATTR("tan", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8386 OPRINIT_ATTR("asin", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8387 OPRINIT_ATTR("acos", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8388 OPRINIT_ATTR("atan", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8389 OPRINIT_ATTR("atan2", 200, 2, JimExprOpBin
, OP_FUNC
),
8390 OPRINIT_ATTR("sinh", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8391 OPRINIT_ATTR("cosh", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8392 OPRINIT_ATTR("tanh", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8393 OPRINIT_ATTR("ceil", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8394 OPRINIT_ATTR("floor", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8395 OPRINIT_ATTR("exp", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8396 OPRINIT_ATTR("log", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8397 OPRINIT_ATTR("log10", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8398 OPRINIT_ATTR("sqrt", 200, 1, JimExprOpDoubleUnary
, OP_FUNC
),
8399 OPRINIT_ATTR("pow", 200, 2, JimExprOpBin
, OP_FUNC
),
8400 OPRINIT_ATTR("hypot", 200, 2, JimExprOpBin
, OP_FUNC
),
8401 OPRINIT_ATTR("fmod", 200, 2, JimExprOpBin
, OP_FUNC
),
8407 #define JIM_EXPR_OPERATORS_NUM \
8408 (sizeof(Jim_ExprOperators)/sizeof(struct Jim_ExprOperator))
8410 static int JimParseExpression(struct JimParserCtx
*pc
)
8412 /* Discard spaces and quoted newline */
8413 while (isspace(UCHAR(*pc
->p
)) || (*(pc
->p
) == '\\' && *(pc
->p
+ 1) == '\n')) {
8414 if (*pc
->p
== '\n') {
8422 pc
->tline
= pc
->linenr
;
8427 pc
->tt
= JIM_TT_EOL
;
8433 pc
->tt
= JIM_TT_SUBEXPR_START
;
8436 pc
->tt
= JIM_TT_SUBEXPR_END
;
8439 pc
->tt
= JIM_TT_SUBEXPR_COMMA
;
8446 return JimParseCmd(pc
);
8448 if (JimParseVar(pc
) == JIM_ERR
)
8449 return JimParseExprOperator(pc
);
8451 /* Don't allow expr sugar in expressions */
8452 if (pc
->tt
== JIM_TT_EXPRSUGAR
) {
8469 return JimParseExprNumber(pc
);
8471 return JimParseQuote(pc
);
8473 return JimParseBrace(pc
);
8479 if (JimParseExprIrrational(pc
) == JIM_ERR
)
8480 if (JimParseExprBoolean(pc
) == JIM_ERR
)
8481 return JimParseExprOperator(pc
);
8487 if (JimParseExprBoolean(pc
) == JIM_ERR
)
8488 return JimParseExprOperator(pc
);
8491 return JimParseExprOperator(pc
);
8497 static int JimParseExprNumber(struct JimParserCtx
*pc
)
8501 /* Assume an integer for now */
8502 pc
->tt
= JIM_TT_EXPR_INT
;
8504 jim_strtoull(pc
->p
, (char **)&pc
->p
);
8505 /* Tried as an integer, but perhaps it parses as a double */
8506 if (strchr("eENnIi.", *pc
->p
) || pc
->p
== pc
->tstart
) {
8507 /* Some stupid compilers insist they are cleverer that
8508 * we are. Even a (void) cast doesn't prevent this warning!
8510 if (strtod(pc
->tstart
, &end
)) { /* nothing */ }
8511 if (end
== pc
->tstart
)
8514 /* Yes, double captured more chars */
8515 pc
->tt
= JIM_TT_EXPR_DOUBLE
;
8519 pc
->tend
= pc
->p
- 1;
8520 pc
->len
-= (pc
->p
- pc
->tstart
);
8524 static int JimParseExprIrrational(struct JimParserCtx
*pc
)
8526 const char *irrationals
[] = { "NaN", "nan", "NAN", "Inf", "inf", "INF", NULL
};
8529 for (i
= 0; irrationals
[i
]; i
++) {
8530 const char *irr
= irrationals
[i
];
8532 if (strncmp(irr
, pc
->p
, 3) == 0) {
8535 pc
->tend
= pc
->p
- 1;
8536 pc
->tt
= JIM_TT_EXPR_DOUBLE
;
8543 static int JimParseExprBoolean(struct JimParserCtx
*pc
)
8545 const char *booleans
[] = { "false", "no", "off", "true", "yes", "on", NULL
};
8546 const int lengths
[] = { 5, 2, 3, 4, 3, 2, 0 };
8549 for (i
= 0; booleans
[i
]; i
++) {
8550 const char *boolean
= booleans
[i
];
8551 int length
= lengths
[i
];
8553 if (strncmp(boolean
, pc
->p
, length
) == 0) {
8556 pc
->tend
= pc
->p
- 1;
8557 pc
->tt
= JIM_TT_EXPR_BOOLEAN
;
8564 static const struct Jim_ExprOperator
*JimExprOperatorInfoByOpcode(int opcode
)
8566 static Jim_ExprOperator dummy_op
;
8567 if (opcode
< JIM_TT_EXPR_OP
) {
8570 return &Jim_ExprOperators
[opcode
- JIM_TT_EXPR_OP
];
8573 static int JimParseExprOperator(struct JimParserCtx
*pc
)
8576 const struct Jim_ExprOperator
*bestOp
= NULL
;
8579 /* Try to get the longest match. */
8580 for (i
= 0; i
< (signed)JIM_EXPR_OPERATORS_NUM
; i
++) {
8581 const struct Jim_ExprOperator
*op
= &Jim_ExprOperators
[i
];
8583 if (op
->name
[0] != pc
->p
[0]) {
8587 if (op
->namelen
> bestLen
&& strncmp(op
->name
, pc
->p
, op
->namelen
) == 0) {
8589 bestLen
= op
->namelen
;
8592 if (bestOp
== NULL
) {
8596 /* Validate paretheses around function arguments */
8597 if (bestOp
->attr
& OP_FUNC
) {
8598 const char *p
= pc
->p
+ bestLen
;
8599 int len
= pc
->len
- bestLen
;
8601 while (len
&& isspace(UCHAR(*p
))) {
8609 pc
->tend
= pc
->p
+ bestLen
- 1;
8613 pc
->tt
= (bestOp
- Jim_ExprOperators
) + JIM_TT_EXPR_OP
;
8617 const char *jim_tt_name(int type
)
8619 static const char * const tt_names
[JIM_TT_EXPR_OP
] =
8620 { "NIL", "STR", "ESC", "VAR", "ARY", "CMD", "SEP", "EOL", "EOF", "LIN", "WRD", "(((", ")))", ",,,", "INT",
8621 "DBL", "BOO", "$()" };
8622 if (type
< JIM_TT_EXPR_OP
) {
8623 return tt_names
[type
];
8625 else if (type
== JIM_EXPROP_UNARYMINUS
) {
8628 else if (type
== JIM_EXPROP_UNARYPLUS
) {
8632 const struct Jim_ExprOperator
*op
= JimExprOperatorInfoByOpcode(type
);
8633 static char buf
[20];
8638 sprintf(buf
, "(%d)", type
);
8643 /* -----------------------------------------------------------------------------
8645 * ---------------------------------------------------------------------------*/
8646 static void FreeExprInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
);
8647 static void DupExprInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
);
8648 static int SetExprFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
);
8650 static const Jim_ObjType exprObjType
= {
8652 FreeExprInternalRep
,
8655 JIM_TYPE_REFERENCES
,
8658 /* expr tree structure */
8661 struct JimExprNode
*expr
; /* The first operator or term */
8662 struct JimExprNode
*nodes
; /* Storage of all nodes in the tree */
8663 int len
; /* Number of nodes in use */
8664 int inUse
; /* Used for sharing. */
8667 static void ExprTreeFreeNodes(Jim_Interp
*interp
, struct JimExprNode
*nodes
, int num
)
8670 for (i
= 0; i
< num
; i
++) {
8671 if (nodes
[i
].objPtr
) {
8672 Jim_DecrRefCount(interp
, nodes
[i
].objPtr
);
8678 static void ExprTreeFree(Jim_Interp
*interp
, struct ExprTree
*expr
)
8680 ExprTreeFreeNodes(interp
, expr
->nodes
, expr
->len
);
8684 static void FreeExprInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
8686 struct ExprTree
*expr
= (void *)objPtr
->internalRep
.ptr
;
8689 if (--expr
->inUse
!= 0) {
8693 ExprTreeFree(interp
, expr
);
8697 static void DupExprInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
)
8699 JIM_NOTUSED(interp
);
8700 JIM_NOTUSED(srcPtr
);
8702 /* Just returns an simple string. */
8703 dupPtr
->typePtr
= NULL
;
8706 struct ExprBuilder
{
8707 int parencount
; /* count of outstanding parentheses */
8708 int level
; /* recursion depth */
8709 ParseToken
*token
; /* The current token */
8710 ParseToken
*first_token
; /* The first token */
8711 Jim_Stack stack
; /* stack of pending terms */
8712 Jim_Obj
*exprObjPtr
; /* the original expression */
8713 Jim_Obj
*fileNameObj
; /* filename of the original expression */
8714 struct JimExprNode
*nodes
; /* storage for all nodes */
8715 struct JimExprNode
*next
; /* storage for the next node */
8718 #ifdef DEBUG_SHOW_EXPR
8719 static void JimShowExprNode(struct JimExprNode
*node
, int level
)
8722 for (i
= 0; i
< level
; i
++) {
8725 if (TOKEN_IS_EXPR_OP(node
->type
)) {
8726 printf("%s\n", jim_tt_name(node
->type
));
8728 JimShowExprNode(node
->left
, level
+ 1);
8731 JimShowExprNode(node
->right
, level
+ 1);
8733 if (node
->ternary
) {
8734 JimShowExprNode(node
->ternary
, level
+ 1);
8738 printf("[%s] %s\n", jim_tt_name(node
->type
), Jim_String(node
->objPtr
));
8743 #define EXPR_UNTIL_CLOSE 0x0001
8744 #define EXPR_FUNC_ARGS 0x0002
8745 #define EXPR_TERNARY 0x0004
8748 * Parse the subexpression at builder->token and return with the node on the stack.
8749 * builder->token is advanced to the next unconsumed token.
8750 * Returns JIM_OK if OK or JIM_ERR on error and leaves a message in the interpreter result.
8752 * 'precedence' is the precedence of the current operator. Tokens are consumed until an operator
8753 * with an equal or lower precedence is reached (or strictly lower if right associative).
8755 * If EXPR_UNTIL_CLOSE is set, the subexpression extends up to and including the next close parenthesis.
8756 * If EXPR_FUNC_ARGS is set, multiple subexpressions (terms) are expected separated by comma
8757 * If EXPR_TERNARY is set, two subexpressions (terms) are expected separated by colon
8759 * 'exp_numterms' indicates how many terms are expected. Normally this is 1, but may be more for EXPR_FUNC_ARGS and EXPR_TERNARY.
8761 static int ExprTreeBuildTree(Jim_Interp
*interp
, struct ExprBuilder
*builder
, int precedence
, int flags
, int exp_numterms
)
8764 struct JimExprNode
*node
;
8765 /* Calculate the stack length expected after pushing the number of expected terms */
8766 int exp_stacklen
= builder
->stack
.len
+ exp_numterms
;
8768 if (builder
->level
++ > 200) {
8769 Jim_SetResultString(interp
, "Expression too complex", -1);
8773 while (builder
->token
->type
!= JIM_TT_EOL
) {
8774 ParseToken
*t
= builder
->token
++;
8777 if (t
== builder
->first_token
) {
8778 prevtt
= JIM_TT_NONE
;
8781 prevtt
= t
[-1].type
;
8784 if (t
->type
== JIM_TT_SUBEXPR_START
) {
8785 if (builder
->stack
.len
== exp_stacklen
) {
8786 Jim_SetResultFormatted(interp
, "unexpected open parenthesis in expression: \"%#s\"", builder
->exprObjPtr
);
8789 builder
->parencount
++;
8790 rc
= ExprTreeBuildTree(interp
, builder
, 0, EXPR_UNTIL_CLOSE
, 1);
8794 /* A complete subexpression is on the stack */
8796 else if (t
->type
== JIM_TT_SUBEXPR_END
) {
8797 if (!(flags
& EXPR_UNTIL_CLOSE
)) {
8798 if (builder
->stack
.len
== exp_stacklen
&& builder
->level
> 1) {
8803 Jim_SetResultFormatted(interp
, "unexpected closing parenthesis in expression: \"%#s\"", builder
->exprObjPtr
);
8806 builder
->parencount
--;
8807 if (builder
->stack
.len
== exp_stacklen
) {
8808 /* Return with the expected number of subexpressions on the stack */
8812 else if (t
->type
== JIM_TT_SUBEXPR_COMMA
) {
8813 if (!(flags
& EXPR_FUNC_ARGS
)) {
8814 if (builder
->stack
.len
== exp_stacklen
) {
8815 /* handle the comma back at the parent level */
8820 Jim_SetResultFormatted(interp
, "unexpected comma in expression: \"%#s\"", builder
->exprObjPtr
);
8824 /* If we see more terms than expected, it is an error */
8825 if (builder
->stack
.len
> exp_stacklen
) {
8826 Jim_SetResultFormatted(interp
, "too many arguments to math function");
8830 /* just go onto the next arg */
8832 else if (t
->type
== JIM_EXPROP_COLON
) {
8833 if (!(flags
& EXPR_TERNARY
)) {
8834 if (builder
->level
!= 1) {
8835 /* handle the comma back at the parent level */
8840 Jim_SetResultFormatted(interp
, ": without ? in expression: \"%#s\"", builder
->exprObjPtr
);
8843 if (builder
->stack
.len
== exp_stacklen
) {
8844 /* handle the comma back at the parent level */
8849 /* just go onto the next term */
8851 else if (TOKEN_IS_EXPR_OP(t
->type
)) {
8852 const struct Jim_ExprOperator
*op
;
8854 /* Convert -/+ to unary minus or unary plus if necessary */
8855 if (TOKEN_IS_EXPR_OP(prevtt
) || TOKEN_IS_EXPR_START(prevtt
)) {
8856 if (t
->type
== JIM_EXPROP_SUB
) {
8857 t
->type
= JIM_EXPROP_UNARYMINUS
;
8859 else if (t
->type
== JIM_EXPROP_ADD
) {
8860 t
->type
= JIM_EXPROP_UNARYPLUS
;
8864 op
= JimExprOperatorInfoByOpcode(t
->type
);
8866 if (op
->precedence
< precedence
|| (!(op
->attr
& OP_RIGHT_ASSOC
) && op
->precedence
== precedence
)) {
8867 /* next op is lower precedence, or equal and left associative, so done here */
8872 if (op
->attr
& OP_FUNC
) {
8873 if (builder
->token
->type
!= JIM_TT_SUBEXPR_START
) {
8874 Jim_SetResultString(interp
, "missing arguments for math function", -1);
8878 if (op
->arity
== 0) {
8879 if (builder
->token
->type
!= JIM_TT_SUBEXPR_END
) {
8880 Jim_SetResultString(interp
, "too many arguments for math function", -1);
8886 builder
->parencount
++;
8888 /* This will push left and return right */
8889 rc
= ExprTreeBuildTree(interp
, builder
, 0, EXPR_FUNC_ARGS
| EXPR_UNTIL_CLOSE
, op
->arity
);
8891 else if (t
->type
== JIM_EXPROP_TERNARY
) {
8892 /* Collect the two arguments to the ternary operator */
8893 rc
= ExprTreeBuildTree(interp
, builder
, op
->precedence
, EXPR_TERNARY
, 2);
8896 /* Recursively handle everything on the right until we see a precendence <= op->precedence or == and right associative
8897 * and push that on the term stack
8899 rc
= ExprTreeBuildTree(interp
, builder
, op
->precedence
, 0, 1);
8907 node
= builder
->next
++;
8908 node
->type
= t
->type
;
8910 if (op
->arity
>= 3) {
8911 node
->ternary
= Jim_StackPop(&builder
->stack
);
8912 if (node
->ternary
== NULL
) {
8913 goto missingoperand
;
8916 if (op
->arity
>= 2) {
8917 node
->right
= Jim_StackPop(&builder
->stack
);
8918 if (node
->right
== NULL
) {
8919 goto missingoperand
;
8922 if (op
->arity
>= 1) {
8923 node
->left
= Jim_StackPop(&builder
->stack
);
8924 if (node
->left
== NULL
) {
8926 Jim_SetResultFormatted(interp
, "missing operand to %s in expression: \"%#s\"", op
->name
, builder
->exprObjPtr
);
8933 /* Now push the node */
8934 Jim_StackPush(&builder
->stack
, node
);
8937 Jim_Obj
*objPtr
= NULL
;
8939 /* This is a simple non-operator term, so create and push the appropriate object */
8941 /* Two consecutive terms without an operator is invalid */
8942 if (!TOKEN_IS_EXPR_START(prevtt
) && !TOKEN_IS_EXPR_OP(prevtt
)) {
8943 Jim_SetResultFormatted(interp
, "missing operator in expression: \"%#s\"", builder
->exprObjPtr
);
8947 /* Immediately create a double or int object? */
8948 if (t
->type
== JIM_TT_EXPR_INT
|| t
->type
== JIM_TT_EXPR_DOUBLE
) {
8950 if (t
->type
== JIM_TT_EXPR_INT
) {
8951 objPtr
= Jim_NewIntObj(interp
, jim_strtoull(t
->token
, &endptr
));
8954 objPtr
= Jim_NewDoubleObj(interp
, strtod(t
->token
, &endptr
));
8956 if (endptr
!= t
->token
+ t
->len
) {
8957 /* Conversion failed, so just store it as a string */
8958 Jim_FreeNewObj(interp
, objPtr
);
8964 /* Everything else is stored a simple string term */
8965 objPtr
= Jim_NewStringObj(interp
, t
->token
, t
->len
);
8966 if (t
->type
== JIM_TT_CMD
) {
8967 /* Only commands need source info */
8968 JimSetSourceInfo(interp
, objPtr
, builder
->fileNameObj
, t
->line
);
8972 /* Now push a term node */
8973 node
= builder
->next
++;
8974 node
->objPtr
= objPtr
;
8975 Jim_IncrRefCount(node
->objPtr
);
8976 node
->type
= t
->type
;
8977 Jim_StackPush(&builder
->stack
, node
);
8981 if (builder
->stack
.len
== exp_stacklen
) {
8986 if ((flags
& EXPR_FUNC_ARGS
)) {
8987 Jim_SetResultFormatted(interp
, "too %s arguments for math function", (builder
->stack
.len
< exp_stacklen
) ? "few" : "many");
8990 if (builder
->stack
.len
< exp_stacklen
) {
8991 if (builder
->level
== 0) {
8992 Jim_SetResultFormatted(interp
, "empty expression");
8995 Jim_SetResultFormatted(interp
, "syntax error in expression \"%#s\": premature end of expression", builder
->exprObjPtr
);
8999 Jim_SetResultFormatted(interp
, "extra terms after expression");
9006 static struct ExprTree
*ExprTreeCreateTree(Jim_Interp
*interp
, const ParseTokenList
*tokenlist
, Jim_Obj
*exprObjPtr
, Jim_Obj
*fileNameObj
)
9008 struct ExprTree
*expr
;
9009 struct ExprBuilder builder
;
9011 struct JimExprNode
*top
= NULL
;
9013 builder
.parencount
= 0;
9015 builder
.token
= builder
.first_token
= tokenlist
->list
;
9016 builder
.exprObjPtr
= exprObjPtr
;
9017 builder
.fileNameObj
= fileNameObj
;
9018 /* The bytecode will never produce more nodes than there are tokens - 1 (for EOL)*/
9019 builder
.nodes
= Jim_Alloc(sizeof(struct JimExprNode
) * (tokenlist
->count
- 1));
9020 memset(builder
.nodes
, 0, sizeof(struct JimExprNode
) * (tokenlist
->count
- 1));
9021 builder
.next
= builder
.nodes
;
9022 Jim_InitStack(&builder
.stack
);
9024 rc
= ExprTreeBuildTree(interp
, &builder
, 0, 0, 1);
9027 top
= Jim_StackPop(&builder
.stack
);
9029 if (builder
.parencount
) {
9030 Jim_SetResultString(interp
, "missing close parenthesis", -1);
9035 /* Free the stack used for the compilation. */
9036 Jim_FreeStack(&builder
.stack
);
9039 ExprTreeFreeNodes(interp
, builder
.nodes
, builder
.next
- builder
.nodes
);
9043 expr
= Jim_Alloc(sizeof(*expr
));
9046 expr
->nodes
= builder
.nodes
;
9047 expr
->len
= builder
.next
- builder
.nodes
;
9049 assert(expr
->len
<= tokenlist
->count
- 1);
9054 /* This method takes the string representation of an expression
9055 * and generates a program for the expr engine */
9056 static int SetExprFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
)
9059 const char *exprText
;
9060 struct JimParserCtx parser
;
9061 struct ExprTree
*expr
;
9062 ParseTokenList tokenlist
;
9064 Jim_Obj
*fileNameObj
;
9067 /* Try to get information about filename / line number */
9068 if (objPtr
->typePtr
== &sourceObjType
) {
9069 fileNameObj
= objPtr
->internalRep
.sourceValue
.fileNameObj
;
9070 line
= objPtr
->internalRep
.sourceValue
.lineNumber
;
9073 fileNameObj
= interp
->emptyObj
;
9076 Jim_IncrRefCount(fileNameObj
);
9078 exprText
= Jim_GetString(objPtr
, &exprTextLen
);
9080 /* Initially tokenise the expression into tokenlist */
9081 ScriptTokenListInit(&tokenlist
);
9083 JimParserInit(&parser
, exprText
, exprTextLen
, line
);
9084 while (!parser
.eof
) {
9085 if (JimParseExpression(&parser
) != JIM_OK
) {
9086 ScriptTokenListFree(&tokenlist
);
9087 Jim_SetResultFormatted(interp
, "syntax error in expression: \"%#s\"", objPtr
);
9092 ScriptAddToken(&tokenlist
, parser
.tstart
, parser
.tend
- parser
.tstart
+ 1, parser
.tt
,
9096 #ifdef DEBUG_SHOW_EXPR_TOKENS
9099 printf("==== Expr Tokens (%s) ====\n", Jim_String(fileNameObj
));
9100 for (i
= 0; i
< tokenlist
.count
; i
++) {
9101 printf("[%2d]@%d %s '%.*s'\n", i
, tokenlist
.list
[i
].line
, jim_tt_name(tokenlist
.list
[i
].type
),
9102 tokenlist
.list
[i
].len
, tokenlist
.list
[i
].token
);
9107 if (JimParseCheckMissing(interp
, parser
.missing
.ch
) == JIM_ERR
) {
9108 ScriptTokenListFree(&tokenlist
);
9109 Jim_DecrRefCount(interp
, fileNameObj
);
9113 /* Now create the expression bytecode from the tokenlist */
9114 expr
= ExprTreeCreateTree(interp
, &tokenlist
, objPtr
, fileNameObj
);
9116 /* No longer need the token list */
9117 ScriptTokenListFree(&tokenlist
);
9123 #ifdef DEBUG_SHOW_EXPR
9124 printf("==== Expr ====\n");
9125 JimShowExprNode(expr
->expr
, 0);
9131 /* Free the old internal rep and set the new one. */
9132 Jim_DecrRefCount(interp
, fileNameObj
);
9133 Jim_FreeIntRep(interp
, objPtr
);
9134 Jim_SetIntRepPtr(objPtr
, expr
);
9135 objPtr
->typePtr
= &exprObjType
;
9139 static struct ExprTree
*JimGetExpression(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
9141 if (objPtr
->typePtr
!= &exprObjType
) {
9142 if (SetExprFromAny(interp
, objPtr
) != JIM_OK
) {
9146 return (struct ExprTree
*) Jim_GetIntRepPtr(objPtr
);
9149 #ifdef JIM_OPTIMIZATION
9150 static Jim_Obj
*JimExprIntValOrVar(Jim_Interp
*interp
, struct JimExprNode
*node
)
9152 if (node
->type
== JIM_TT_EXPR_INT
)
9153 return node
->objPtr
;
9154 else if (node
->type
== JIM_TT_VAR
)
9155 return Jim_GetVariable(interp
, node
->objPtr
, JIM_NONE
);
9156 else if (node
->type
== JIM_TT_DICTSUGAR
)
9157 return JimExpandDictSugar(interp
, node
->objPtr
);
9163 /* -----------------------------------------------------------------------------
9164 * Expressions evaluation.
9165 * Jim uses a recursive evaluation engine for expressions,
9166 * that takes advantage of the fact that expr's operators
9167 * can't be redefined.
9169 * Jim_EvalExpression() uses the expression tree compiled by
9170 * SetExprFromAny() method of the "expression" object.
9172 * On success a Tcl Object containing the result of the evaluation
9173 * is stored into expResultPtrPtr (having refcount of 1), and JIM_OK is
9175 * On error the function returns a retcode != to JIM_OK and set a suitable
9176 * error on the interp.
9177 * ---------------------------------------------------------------------------*/
9179 static int JimExprEvalTermNode(Jim_Interp
*interp
, struct JimExprNode
*node
)
9181 if (TOKEN_IS_EXPR_OP(node
->type
)) {
9182 const struct Jim_ExprOperator
*op
= JimExprOperatorInfoByOpcode(node
->type
);
9183 return op
->funcop(interp
, node
);
9189 switch (node
->type
) {
9190 case JIM_TT_EXPR_INT
:
9191 case JIM_TT_EXPR_DOUBLE
:
9192 case JIM_TT_EXPR_BOOLEAN
:
9194 Jim_SetResult(interp
, node
->objPtr
);
9198 objPtr
= Jim_GetVariable(interp
, node
->objPtr
, JIM_ERRMSG
);
9200 Jim_SetResult(interp
, objPtr
);
9205 case JIM_TT_DICTSUGAR
:
9206 objPtr
= JimExpandDictSugar(interp
, node
->objPtr
);
9208 Jim_SetResult(interp
, objPtr
);
9214 if (Jim_SubstObj(interp
, node
->objPtr
, &objPtr
, JIM_NONE
) == JIM_OK
) {
9215 Jim_SetResult(interp
, objPtr
);
9221 return Jim_EvalObj(interp
, node
->objPtr
);
9224 /* Should never get here */
9230 static int JimExprGetTerm(Jim_Interp
*interp
, struct JimExprNode
*node
, Jim_Obj
**objPtrPtr
)
9232 int rc
= JimExprEvalTermNode(interp
, node
);
9234 *objPtrPtr
= Jim_GetResult(interp
);
9235 Jim_IncrRefCount(*objPtrPtr
);
9240 static int JimExprGetTermBoolean(Jim_Interp
*interp
, struct JimExprNode
*node
)
9242 if (JimExprEvalTermNode(interp
, node
) == JIM_OK
) {
9243 return ExprBool(interp
, Jim_GetResult(interp
));
9248 int Jim_EvalExpression(Jim_Interp
*interp
, Jim_Obj
*exprObjPtr
)
9250 struct ExprTree
*expr
;
9251 int retcode
= JIM_OK
;
9253 Jim_IncrRefCount(exprObjPtr
); /* Make sure it's shared. */
9254 expr
= JimGetExpression(interp
, exprObjPtr
);
9260 #ifdef JIM_OPTIMIZATION
9261 /* Check for one of the following common expressions used by while/for
9266 * $a < CONST, $a < $b
9267 * $a <= CONST, $a <= $b
9268 * $a > CONST, $a > $b
9269 * $a >= CONST, $a >= $b
9270 * $a != CONST, $a != $b
9271 * $a == CONST, $a == $b
9276 /* STEP 1 -- Check if there are the conditions to run the specialized
9277 * version of while */
9279 switch (expr
->len
) {
9281 objPtr
= JimExprIntValOrVar(interp
, expr
->expr
);
9283 Jim_SetResult(interp
, objPtr
);
9289 if (expr
->expr
->type
== JIM_EXPROP_NOT
) {
9290 objPtr
= JimExprIntValOrVar(interp
, expr
->expr
->left
);
9292 if (objPtr
&& JimIsWide(objPtr
)) {
9293 Jim_SetResult(interp
, JimWideValue(objPtr
) ? interp
->falseObj
: interp
->trueObj
);
9300 objPtr
= JimExprIntValOrVar(interp
, expr
->expr
->left
);
9301 if (objPtr
&& JimIsWide(objPtr
)) {
9302 Jim_Obj
*objPtr2
= JimExprIntValOrVar(interp
, expr
->expr
->right
);
9303 if (objPtr2
&& JimIsWide(objPtr2
)) {
9304 jim_wide wideValueA
= JimWideValue(objPtr
);
9305 jim_wide wideValueB
= JimWideValue(objPtr2
);
9307 switch (expr
->expr
->type
) {
9309 cmpRes
= wideValueA
< wideValueB
;
9311 case JIM_EXPROP_LTE
:
9312 cmpRes
= wideValueA
<= wideValueB
;
9315 cmpRes
= wideValueA
> wideValueB
;
9317 case JIM_EXPROP_GTE
:
9318 cmpRes
= wideValueA
>= wideValueB
;
9320 case JIM_EXPROP_NUMEQ
:
9321 cmpRes
= wideValueA
== wideValueB
;
9323 case JIM_EXPROP_NUMNE
:
9324 cmpRes
= wideValueA
!= wideValueB
;
9329 Jim_SetResult(interp
, cmpRes
? interp
->trueObj
: interp
->falseObj
);
9339 /* In order to avoid the internal repr being freed due to
9340 * shimmering of the exprObjPtr's object, we increment the use count
9341 * and keep our own pointer outside the object.
9345 /* Evaluate with the recursive expr engine */
9346 retcode
= JimExprEvalTermNode(interp
, expr
->expr
);
9348 /* Now transfer ownership of expr back into the object in case it shimmered away */
9349 Jim_FreeIntRep(interp
, exprObjPtr
);
9350 exprObjPtr
->typePtr
= &exprObjType
;
9351 Jim_SetIntRepPtr(exprObjPtr
, expr
);
9354 Jim_DecrRefCount(interp
, exprObjPtr
);
9359 int Jim_GetBoolFromExpr(Jim_Interp
*interp
, Jim_Obj
*exprObjPtr
, int *boolPtr
)
9361 int retcode
= Jim_EvalExpression(interp
, exprObjPtr
);
9363 if (retcode
== JIM_OK
) {
9364 switch (ExprBool(interp
, Jim_GetResult(interp
))) {
9381 /* -----------------------------------------------------------------------------
9382 * ScanFormat String Object
9383 * ---------------------------------------------------------------------------*/
9385 /* This Jim_Obj will held a parsed representation of a format string passed to
9386 * the Jim_ScanString command. For error diagnostics, the scanformat string has
9387 * to be parsed in its entirely first and then, if correct, can be used for
9388 * scanning. To avoid endless re-parsing, the parsed representation will be
9389 * stored in an internal representation and re-used for performance reason. */
9391 /* A ScanFmtPartDescr will held the information of /one/ part of the whole
9392 * scanformat string. This part will later be used to extract information
9393 * out from the string to be parsed by Jim_ScanString */
9395 typedef struct ScanFmtPartDescr
9397 const char *arg
; /* Specification of a CHARSET conversion */
9398 const char *prefix
; /* Prefix to be scanned literally before conversion */
9399 size_t width
; /* Maximal width of input to be converted */
9400 int pos
; /* -1 - no assign, 0 - natural pos, >0 - XPG3 pos */
9401 char type
; /* Type of conversion (e.g. c, d, f) */
9402 char modifier
; /* Modify type (e.g. l - long, h - short */
9405 /* The ScanFmtStringObj will hold the internal representation of a scanformat
9406 * string parsed and separated in part descriptions. Furthermore it contains
9407 * the original string representation of the scanformat string to allow for
9408 * fast update of the Jim_Obj's string representation part.
9410 * As an add-on the internal object representation adds some scratch pad area
9411 * for usage by Jim_ScanString to avoid endless allocating and freeing of
9412 * memory for purpose of string scanning.
9414 * The error member points to a static allocated string in case of a mal-
9415 * formed scanformat string or it contains '0' (NULL) in case of a valid
9416 * parse representation.
9418 * The whole memory of the internal representation is allocated as a single
9419 * area of memory that will be internally separated. So freeing and duplicating
9420 * of such an object is cheap */
9422 typedef struct ScanFmtStringObj
9424 jim_wide size
; /* Size of internal repr in bytes */
9425 char *stringRep
; /* Original string representation */
9426 size_t count
; /* Number of ScanFmtPartDescr contained */
9427 size_t convCount
; /* Number of conversions that will assign */
9428 size_t maxPos
; /* Max position index if XPG3 is used */
9429 const char *error
; /* Ptr to error text (NULL if no error */
9430 char *scratch
; /* Some scratch pad used by Jim_ScanString */
9431 ScanFmtPartDescr descr
[1]; /* The vector of partial descriptions */
9435 static void FreeScanFmtInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
);
9436 static void DupScanFmtInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
);
9437 static void UpdateStringOfScanFmt(Jim_Obj
*objPtr
);
9439 static const Jim_ObjType scanFmtStringObjType
= {
9441 FreeScanFmtInternalRep
,
9442 DupScanFmtInternalRep
,
9443 UpdateStringOfScanFmt
,
9447 void FreeScanFmtInternalRep(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
9449 JIM_NOTUSED(interp
);
9450 Jim_Free((char *)objPtr
->internalRep
.ptr
);
9451 objPtr
->internalRep
.ptr
= 0;
9454 void DupScanFmtInternalRep(Jim_Interp
*interp
, Jim_Obj
*srcPtr
, Jim_Obj
*dupPtr
)
9456 size_t size
= (size_t) ((ScanFmtStringObj
*) srcPtr
->internalRep
.ptr
)->size
;
9457 ScanFmtStringObj
*newVec
= (ScanFmtStringObj
*) Jim_Alloc(size
);
9459 JIM_NOTUSED(interp
);
9460 memcpy(newVec
, srcPtr
->internalRep
.ptr
, size
);
9461 dupPtr
->internalRep
.ptr
= newVec
;
9462 dupPtr
->typePtr
= &scanFmtStringObjType
;
9465 static void UpdateStringOfScanFmt(Jim_Obj
*objPtr
)
9467 JimSetStringBytes(objPtr
, ((ScanFmtStringObj
*) objPtr
->internalRep
.ptr
)->stringRep
);
9470 /* SetScanFmtFromAny will parse a given string and create the internal
9471 * representation of the format specification. In case of an error
9472 * the error data member of the internal representation will be set
9473 * to an descriptive error text and the function will be left with
9474 * JIM_ERR to indicate unsucessful parsing (aka. malformed scanformat
9477 static int SetScanFmtFromAny(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
9479 ScanFmtStringObj
*fmtObj
;
9481 int maxCount
, i
, approxSize
, lastPos
= -1;
9482 const char *fmt
= Jim_String(objPtr
);
9483 int maxFmtLen
= Jim_Length(objPtr
);
9484 const char *fmtEnd
= fmt
+ maxFmtLen
;
9487 Jim_FreeIntRep(interp
, objPtr
);
9488 /* Count how many conversions could take place maximally */
9489 for (i
= 0, maxCount
= 0; i
< maxFmtLen
; ++i
)
9492 /* Calculate an approximation of the memory necessary */
9493 approxSize
= sizeof(ScanFmtStringObj
) /* Size of the container */
9494 +(maxCount
+ 1) * sizeof(ScanFmtPartDescr
) /* Size of all partials */
9495 +maxFmtLen
* sizeof(char) + 3 + 1 /* Scratch + "%n" + '\0' */
9496 + maxFmtLen
* sizeof(char) + 1 /* Original stringrep */
9497 + maxFmtLen
* sizeof(char) /* Arg for CHARSETs */
9498 +(maxCount
+ 1) * sizeof(char) /* '\0' for every partial */
9499 +1; /* safety byte */
9500 fmtObj
= (ScanFmtStringObj
*) Jim_Alloc(approxSize
);
9501 memset(fmtObj
, 0, approxSize
);
9502 fmtObj
->size
= approxSize
;
9504 fmtObj
->scratch
= (char *)&fmtObj
->descr
[maxCount
+ 1];
9505 fmtObj
->stringRep
= fmtObj
->scratch
+ maxFmtLen
+ 3 + 1;
9506 memcpy(fmtObj
->stringRep
, fmt
, maxFmtLen
);
9507 buffer
= fmtObj
->stringRep
+ maxFmtLen
+ 1;
9508 objPtr
->internalRep
.ptr
= fmtObj
;
9509 objPtr
->typePtr
= &scanFmtStringObjType
;
9510 for (i
= 0, curr
= 0; fmt
< fmtEnd
; ++fmt
) {
9511 int width
= 0, skip
;
9512 ScanFmtPartDescr
*descr
= &fmtObj
->descr
[curr
];
9515 descr
->width
= 0; /* Assume width unspecified */
9516 /* Overread and store any "literal" prefix */
9517 if (*fmt
!= '%' || fmt
[1] == '%') {
9519 descr
->prefix
= &buffer
[i
];
9520 for (; fmt
< fmtEnd
; ++fmt
) {
9530 /* Skip the conversion introducing '%' sign */
9532 /* End reached due to non-conversion literal only? */
9535 descr
->pos
= 0; /* Assume "natural" positioning */
9537 descr
->pos
= -1; /* Okay, conversion will not be assigned */
9541 fmtObj
->convCount
++; /* Otherwise count as assign-conversion */
9542 /* Check if next token is a number (could be width or pos */
9543 if (sscanf(fmt
, "%d%n", &width
, &skip
) == 1) {
9545 /* Was the number a XPG3 position specifier? */
9546 if (descr
->pos
!= -1 && *fmt
== '$') {
9552 /* Look if "natural" postioning and XPG3 one was mixed */
9553 if ((lastPos
== 0 && descr
->pos
> 0)
9554 || (lastPos
> 0 && descr
->pos
== 0)) {
9555 fmtObj
->error
= "cannot mix \"%\" and \"%n$\" conversion specifiers";
9558 /* Look if this position was already used */
9559 for (prev
= 0; prev
< curr
; ++prev
) {
9560 if (fmtObj
->descr
[prev
].pos
== -1)
9562 if (fmtObj
->descr
[prev
].pos
== descr
->pos
) {
9564 "variable is assigned by multiple \"%n$\" conversion specifiers";
9568 if (descr
->pos
< 0) {
9570 "\"%n$\" conversion specifier is negative";
9573 /* Try to find a width after the XPG3 specifier */
9574 if (sscanf(fmt
, "%d%n", &width
, &skip
) == 1) {
9575 descr
->width
= width
;
9578 if (descr
->pos
> 0 && (size_t) descr
->pos
> fmtObj
->maxPos
)
9579 fmtObj
->maxPos
= descr
->pos
;
9582 /* Number was not a XPG3, so it has to be a width */
9583 descr
->width
= width
;
9586 /* If positioning mode was undetermined yet, fix this */
9588 lastPos
= descr
->pos
;
9589 /* Handle CHARSET conversion type ... */
9591 int swapped
= 1, beg
= i
, end
, j
;
9594 descr
->arg
= &buffer
[i
];
9597 buffer
[i
++] = *fmt
++;
9599 buffer
[i
++] = *fmt
++;
9600 while (*fmt
&& *fmt
!= ']')
9601 buffer
[i
++] = *fmt
++;
9603 fmtObj
->error
= "unmatched [ in format string";
9608 /* In case a range fence was given "backwards", swap it */
9611 for (j
= beg
+ 1; j
< end
- 1; ++j
) {
9612 if (buffer
[j
] == '-' && buffer
[j
- 1] > buffer
[j
+ 1]) {
9613 char tmp
= buffer
[j
- 1];
9615 buffer
[j
- 1] = buffer
[j
+ 1];
9616 buffer
[j
+ 1] = tmp
;
9623 /* Remember any valid modifier if given */
9624 if (fmt
< fmtEnd
&& strchr("hlL", *fmt
))
9625 descr
->modifier
= tolower((int)*fmt
++);
9627 if (fmt
>= fmtEnd
) {
9628 fmtObj
->error
= "missing scan conversion character";
9633 if (strchr("efgcsndoxui", *fmt
) == 0) {
9634 fmtObj
->error
= "bad scan conversion character";
9637 else if (*fmt
== 'c' && descr
->width
!= 0) {
9638 fmtObj
->error
= "field width may not be specified in %c " "conversion";
9641 else if (*fmt
== 'u' && descr
->modifier
== 'l') {
9642 fmtObj
->error
= "unsigned wide not supported";
9652 /* Some accessor macros to allow lowlevel access to fields of internal repr */
9654 #define FormatGetCnvCount(_fo_) \
9655 ((ScanFmtStringObj*)((_fo_)->internalRep.ptr))->convCount
9656 #define FormatGetMaxPos(_fo_) \
9657 ((ScanFmtStringObj*)((_fo_)->internalRep.ptr))->maxPos
9658 #define FormatGetError(_fo_) \
9659 ((ScanFmtStringObj*)((_fo_)->internalRep.ptr))->error
9661 /* JimScanAString is used to scan an unspecified string that ends with
9662 * next WS, or a string that is specified via a charset.
9665 static Jim_Obj
*JimScanAString(Jim_Interp
*interp
, const char *sdescr
, const char *str
)
9667 char *buffer
= Jim_StrDup(str
);
9674 if (!sdescr
&& isspace(UCHAR(*str
)))
9675 break; /* EOS via WS if unspecified */
9677 n
= utf8_tounicode(str
, &c
);
9678 if (sdescr
&& !JimCharsetMatch(sdescr
, c
, JIM_CHARSET_SCAN
))
9684 return Jim_NewStringObjNoAlloc(interp
, buffer
, p
- buffer
);
9687 /* ScanOneEntry will scan one entry out of the string passed as argument.
9688 * It use the sscanf() function for this task. After extracting and
9689 * converting of the value, the count of scanned characters will be
9690 * returned of -1 in case of no conversion tool place and string was
9691 * already scanned thru */
9693 static int ScanOneEntry(Jim_Interp
*interp
, const char *str
, int pos
, int str_bytelen
,
9694 ScanFmtStringObj
* fmtObj
, long idx
, Jim_Obj
**valObjPtr
)
9697 const ScanFmtPartDescr
*descr
= &fmtObj
->descr
[idx
];
9699 size_t anchor
= pos
;
9701 Jim_Obj
*tmpObj
= NULL
;
9703 /* First pessimistically assume, we will not scan anything :-) */
9705 if (descr
->prefix
) {
9706 /* There was a prefix given before the conversion, skip it and adjust
9707 * the string-to-be-parsed accordingly */
9708 for (i
= 0; pos
< str_bytelen
&& descr
->prefix
[i
]; ++i
) {
9709 /* If prefix require, skip WS */
9710 if (isspace(UCHAR(descr
->prefix
[i
])))
9711 while (pos
< str_bytelen
&& isspace(UCHAR(str
[pos
])))
9713 else if (descr
->prefix
[i
] != str
[pos
])
9714 break; /* Prefix do not match here, leave the loop */
9716 ++pos
; /* Prefix matched so far, next round */
9718 if (pos
>= str_bytelen
) {
9719 return -1; /* All of str consumed: EOF condition */
9721 else if (descr
->prefix
[i
] != 0)
9722 return 0; /* Not whole prefix consumed, no conversion possible */
9724 /* For all but following conversion, skip leading WS */
9725 if (descr
->type
!= 'c' && descr
->type
!= '[' && descr
->type
!= 'n')
9726 while (isspace(UCHAR(str
[pos
])))
9729 /* Determine how much skipped/scanned so far */
9730 scanned
= pos
- anchor
;
9732 /* %c is a special, simple case. no width */
9733 if (descr
->type
== 'n') {
9734 /* Return pseudo conversion means: how much scanned so far? */
9735 *valObjPtr
= Jim_NewIntObj(interp
, anchor
+ scanned
);
9737 else if (pos
>= str_bytelen
) {
9738 /* Cannot scan anything, as str is totally consumed */
9741 else if (descr
->type
== 'c') {
9743 scanned
+= utf8_tounicode(&str
[pos
], &c
);
9744 *valObjPtr
= Jim_NewIntObj(interp
, c
);
9748 /* Processing of conversions follows ... */
9749 if (descr
->width
> 0) {
9750 /* Do not try to scan as fas as possible but only the given width.
9751 * To ensure this, we copy the part that should be scanned. */
9752 size_t sLen
= utf8_strlen(&str
[pos
], str_bytelen
- pos
);
9753 size_t tLen
= descr
->width
> sLen
? sLen
: descr
->width
;
9755 tmpObj
= Jim_NewStringObjUtf8(interp
, str
+ pos
, tLen
);
9756 tok
= tmpObj
->bytes
;
9759 /* As no width was given, simply refer to the original string */
9762 switch (descr
->type
) {
9768 char *endp
; /* Position where the number finished */
9771 int base
= descr
->type
== 'o' ? 8
9772 : descr
->type
== 'x' ? 16 : descr
->type
== 'i' ? 0 : 10;
9774 /* Try to scan a number with the given base */
9776 w
= jim_strtoull(tok
, &endp
);
9779 w
= strtoull(tok
, &endp
, base
);
9783 /* There was some number sucessfully scanned! */
9784 *valObjPtr
= Jim_NewIntObj(interp
, w
);
9786 /* Adjust the number-of-chars scanned so far */
9787 scanned
+= endp
- tok
;
9790 /* Nothing was scanned. We have to determine if this
9791 * happened due to e.g. prefix mismatch or input str
9793 scanned
= *tok
? 0 : -1;
9799 *valObjPtr
= JimScanAString(interp
, descr
->arg
, tok
);
9800 scanned
+= Jim_Length(*valObjPtr
);
9807 double value
= strtod(tok
, &endp
);
9810 /* There was some number sucessfully scanned! */
9811 *valObjPtr
= Jim_NewDoubleObj(interp
, value
);
9812 /* Adjust the number-of-chars scanned so far */
9813 scanned
+= endp
- tok
;
9816 /* Nothing was scanned. We have to determine if this
9817 * happened due to e.g. prefix mismatch or input str
9819 scanned
= *tok
? 0 : -1;
9824 /* If a substring was allocated (due to pre-defined width) do not
9825 * forget to free it */
9827 Jim_FreeNewObj(interp
, tmpObj
);
9833 /* Jim_ScanString is the workhorse of string scanning. It will scan a given
9834 * string and returns all converted (and not ignored) values in a list back
9835 * to the caller. If an error occured, a NULL pointer will be returned */
9837 Jim_Obj
*Jim_ScanString(Jim_Interp
*interp
, Jim_Obj
*strObjPtr
, Jim_Obj
*fmtObjPtr
, int flags
)
9841 const char *str
= Jim_String(strObjPtr
);
9842 int str_bytelen
= Jim_Length(strObjPtr
);
9843 Jim_Obj
*resultList
= 0;
9844 Jim_Obj
**resultVec
= 0;
9846 Jim_Obj
*emptyStr
= 0;
9847 ScanFmtStringObj
*fmtObj
;
9849 /* This should never happen. The format object should already be of the correct type */
9850 JimPanic((fmtObjPtr
->typePtr
!= &scanFmtStringObjType
, "Jim_ScanString() for non-scan format"));
9852 fmtObj
= (ScanFmtStringObj
*) fmtObjPtr
->internalRep
.ptr
;
9853 /* Check if format specification was valid */
9854 if (fmtObj
->error
!= 0) {
9855 if (flags
& JIM_ERRMSG
)
9856 Jim_SetResultString(interp
, fmtObj
->error
, -1);
9859 /* Allocate a new "shared" empty string for all unassigned conversions */
9860 emptyStr
= Jim_NewEmptyStringObj(interp
);
9861 Jim_IncrRefCount(emptyStr
);
9862 /* Create a list and fill it with empty strings up to max specified XPG3 */
9863 resultList
= Jim_NewListObj(interp
, NULL
, 0);
9864 if (fmtObj
->maxPos
> 0) {
9865 for (i
= 0; i
< fmtObj
->maxPos
; ++i
)
9866 Jim_ListAppendElement(interp
, resultList
, emptyStr
);
9867 JimListGetElements(interp
, resultList
, &resultc
, &resultVec
);
9869 /* Now handle every partial format description */
9870 for (i
= 0, pos
= 0; i
< fmtObj
->count
; ++i
) {
9871 ScanFmtPartDescr
*descr
= &(fmtObj
->descr
[i
]);
9874 /* Only last type may be "literal" w/o conversion - skip it! */
9875 if (descr
->type
== 0)
9877 /* As long as any conversion could be done, we will proceed */
9879 scanned
= ScanOneEntry(interp
, str
, pos
, str_bytelen
, fmtObj
, i
, &value
);
9880 /* In case our first try results in EOF, we will leave */
9881 if (scanned
== -1 && i
== 0)
9883 /* Advance next pos-to-be-scanned for the amount scanned already */
9886 /* value == 0 means no conversion took place so take empty string */
9888 value
= Jim_NewEmptyStringObj(interp
);
9889 /* If value is a non-assignable one, skip it */
9890 if (descr
->pos
== -1) {
9891 Jim_FreeNewObj(interp
, value
);
9893 else if (descr
->pos
== 0)
9894 /* Otherwise append it to the result list if no XPG3 was given */
9895 Jim_ListAppendElement(interp
, resultList
, value
);
9896 else if (resultVec
[descr
->pos
- 1] == emptyStr
) {
9897 /* But due to given XPG3, put the value into the corr. slot */
9898 Jim_DecrRefCount(interp
, resultVec
[descr
->pos
- 1]);
9899 Jim_IncrRefCount(value
);
9900 resultVec
[descr
->pos
- 1] = value
;
9903 /* Otherwise, the slot was already used - free obj and ERROR */
9904 Jim_FreeNewObj(interp
, value
);
9908 Jim_DecrRefCount(interp
, emptyStr
);
9911 Jim_DecrRefCount(interp
, emptyStr
);
9912 Jim_FreeNewObj(interp
, resultList
);
9913 return (Jim_Obj
*)EOF
;
9915 Jim_DecrRefCount(interp
, emptyStr
);
9916 Jim_FreeNewObj(interp
, resultList
);
9920 /* -----------------------------------------------------------------------------
9921 * Pseudo Random Number Generation
9922 * ---------------------------------------------------------------------------*/
9923 /* Initialize the sbox with the numbers from 0 to 255 */
9924 static void JimPrngInit(Jim_Interp
*interp
)
9926 #define PRNG_SEED_SIZE 256
9929 time_t t
= time(NULL
);
9931 interp
->prngState
= Jim_Alloc(sizeof(Jim_PrngState
));
9933 seed
= Jim_Alloc(PRNG_SEED_SIZE
* sizeof(*seed
));
9934 for (i
= 0; i
< PRNG_SEED_SIZE
; i
++) {
9935 seed
[i
] = (rand() ^ t
^ clock());
9937 JimPrngSeed(interp
, (unsigned char *)seed
, PRNG_SEED_SIZE
* sizeof(*seed
));
9941 /* Generates N bytes of random data */
9942 static void JimRandomBytes(Jim_Interp
*interp
, void *dest
, unsigned int len
)
9944 Jim_PrngState
*prng
;
9945 unsigned char *destByte
= (unsigned char *)dest
;
9946 unsigned int si
, sj
, x
;
9948 /* initialization, only needed the first time */
9949 if (interp
->prngState
== NULL
)
9950 JimPrngInit(interp
);
9951 prng
= interp
->prngState
;
9952 /* generates 'len' bytes of pseudo-random numbers */
9953 for (x
= 0; x
< len
; x
++) {
9954 prng
->i
= (prng
->i
+ 1) & 0xff;
9955 si
= prng
->sbox
[prng
->i
];
9956 prng
->j
= (prng
->j
+ si
) & 0xff;
9957 sj
= prng
->sbox
[prng
->j
];
9958 prng
->sbox
[prng
->i
] = sj
;
9959 prng
->sbox
[prng
->j
] = si
;
9960 *destByte
++ = prng
->sbox
[(si
+ sj
) & 0xff];
9964 /* Re-seed the generator with user-provided bytes */
9965 static void JimPrngSeed(Jim_Interp
*interp
, unsigned char *seed
, int seedLen
)
9968 Jim_PrngState
*prng
;
9970 /* initialization, only needed the first time */
9971 if (interp
->prngState
== NULL
)
9972 JimPrngInit(interp
);
9973 prng
= interp
->prngState
;
9975 /* Set the sbox[i] with i */
9976 for (i
= 0; i
< 256; i
++)
9978 /* Now use the seed to perform a random permutation of the sbox */
9979 for (i
= 0; i
< seedLen
; i
++) {
9982 t
= prng
->sbox
[i
& 0xFF];
9983 prng
->sbox
[i
& 0xFF] = prng
->sbox
[seed
[i
]];
9984 prng
->sbox
[seed
[i
]] = t
;
9986 prng
->i
= prng
->j
= 0;
9988 /* discard at least the first 256 bytes of stream.
9989 * borrow the seed buffer for this
9991 for (i
= 0; i
< 256; i
+= seedLen
) {
9992 JimRandomBytes(interp
, seed
, seedLen
);
9997 static int Jim_IncrCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
9999 jim_wide wideValue
, increment
= 1;
10000 Jim_Obj
*intObjPtr
;
10002 if (argc
!= 2 && argc
!= 3) {
10003 Jim_WrongNumArgs(interp
, 1, argv
, "varName ?increment?");
10007 if (Jim_GetWide(interp
, argv
[2], &increment
) != JIM_OK
)
10010 intObjPtr
= Jim_GetVariable(interp
, argv
[1], JIM_UNSHARED
);
10012 /* Set missing variable to 0 */
10015 else if (Jim_GetWide(interp
, intObjPtr
, &wideValue
) != JIM_OK
) {
10018 if (!intObjPtr
|| Jim_IsShared(intObjPtr
)) {
10019 intObjPtr
= Jim_NewIntObj(interp
, wideValue
+ increment
);
10020 if (Jim_SetVariable(interp
, argv
[1], intObjPtr
) != JIM_OK
) {
10021 Jim_FreeNewObj(interp
, intObjPtr
);
10026 /* Can do it the quick way */
10027 Jim_InvalidateStringRep(intObjPtr
);
10028 JimWideValue(intObjPtr
) = wideValue
+ increment
;
10030 /* The following step is required in order to invalidate the
10031 * string repr of "FOO" if the var name is on the form of "FOO(IDX)" */
10032 if (argv
[1]->typePtr
!= &variableObjType
) {
10033 /* Note that this can't fail since GetVariable already succeeded */
10034 Jim_SetVariable(interp
, argv
[1], intObjPtr
);
10037 Jim_SetResult(interp
, intObjPtr
);
10042 /* -----------------------------------------------------------------------------
10044 * ---------------------------------------------------------------------------*/
10045 #define JIM_EVAL_SARGV_LEN 8 /* static arguments vector length */
10046 #define JIM_EVAL_SINTV_LEN 8 /* static interpolation vector length */
10048 /* Handle calls to the [unknown] command */
10049 static int JimUnknown(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
10053 /* If JimUnknown() is recursively called too many times...
10056 if (interp
->unknown_called
> 50) {
10060 /* The object interp->unknown just contains
10061 * the "unknown" string, it is used in order to
10062 * avoid to lookup the unknown command every time
10063 * but instead to cache the result. */
10065 /* If the [unknown] command does not exist ... */
10066 if (Jim_GetCommand(interp
, interp
->unknown
, JIM_NONE
) == NULL
)
10069 interp
->unknown_called
++;
10070 /* XXX: Are we losing fileNameObj and linenr? */
10071 retcode
= Jim_EvalObjPrefix(interp
, interp
->unknown
, argc
, argv
);
10072 interp
->unknown_called
--;
10077 static int JimInvokeCommand(Jim_Interp
*interp
, int objc
, Jim_Obj
*const *objv
)
10081 void *prevPrivData
;
10082 Jim_Obj
*tailcallObj
= NULL
;
10087 for (j
= 0; j
< objc
; j
++) {
10088 printf(" '%s'", Jim_String(objv
[j
]));
10093 cmdPtr
= Jim_GetCommand(interp
, objv
[0], JIM_ERRMSG
);
10094 if (cmdPtr
== NULL
) {
10095 return JimUnknown(interp
, objc
, objv
);
10097 JimIncrCmdRefCount(cmdPtr
);
10099 if (interp
->evalDepth
== interp
->maxEvalDepth
) {
10100 Jim_SetResultString(interp
, "Infinite eval recursion", -1);
10104 interp
->evalDepth
++;
10105 prevPrivData
= interp
->cmdPrivData
;
10109 /* Call it -- Make sure result is an empty object. */
10110 Jim_SetEmptyResult(interp
);
10111 if (cmdPtr
->isproc
) {
10112 retcode
= JimCallProcedure(interp
, cmdPtr
, objc
, objv
);
10114 /* Handle the JIM_RETURN return code */
10115 if (retcode
== JIM_RETURN
) {
10116 if (--interp
->returnLevel
<= 0) {
10117 retcode
= interp
->returnCode
;
10118 interp
->returnCode
= JIM_OK
;
10119 interp
->returnLevel
= 0;
10122 else if (retcode
== JIM_ERR
) {
10123 interp
->addStackTrace
++;
10124 Jim_DecrRefCount(interp
, interp
->errorProc
);
10125 interp
->errorProc
= objv
[0];
10126 Jim_IncrRefCount(interp
->errorProc
);
10130 interp
->cmdPrivData
= cmdPtr
->u
.native
.privData
;
10131 retcode
= cmdPtr
->u
.native
.cmdProc(interp
, objc
, objv
);
10135 /* clean up previous tailcall if we were invoking one */
10136 Jim_DecrRefCount(interp
, tailcallObj
);
10137 tailcallObj
= NULL
;
10140 /* If a tailcall is returned for this frame, loop to invoke the new command */
10141 if (retcode
== JIM_EVAL
&& interp
->framePtr
->tailcallObj
) {
10142 JimDecrCmdRefCount(interp
, cmdPtr
);
10144 /* Replace the current command with the new tailcall command */
10145 cmdPtr
= interp
->framePtr
->tailcallCmd
;
10146 interp
->framePtr
->tailcallCmd
= NULL
;
10147 tailcallObj
= interp
->framePtr
->tailcallObj
;
10148 interp
->framePtr
->tailcallObj
= NULL
;
10149 /* We can access the internal rep here because the object can only
10150 * be constructed by the tailcall command
10152 objc
= tailcallObj
->internalRep
.listValue
.len
;
10153 objv
= tailcallObj
->internalRep
.listValue
.ele
;
10157 interp
->cmdPrivData
= prevPrivData
;
10158 interp
->evalDepth
--;
10161 JimDecrCmdRefCount(interp
, cmdPtr
);
10163 if (interp
->framePtr
->tailcallObj
) {
10164 /* We might have skipped invoking a tailcall, perhaps because of an error
10165 * in defer handling so cleanup now
10167 JimDecrCmdRefCount(interp
, interp
->framePtr
->tailcallCmd
);
10168 Jim_DecrRefCount(interp
, interp
->framePtr
->tailcallObj
);
10169 interp
->framePtr
->tailcallCmd
= NULL
;
10170 interp
->framePtr
->tailcallObj
= NULL
;
10176 /* Eval the object vector 'objv' composed of 'objc' elements.
10177 * Every element is used as single argument.
10178 * Jim_EvalObj() will call this function every time its object
10179 * argument is of "list" type, with no string representation.
10181 * This is possible because the string representation of a
10182 * list object generated by the UpdateStringOfList is made
10183 * in a way that ensures that every list element is a different
10184 * command argument. */
10185 int Jim_EvalObjVector(Jim_Interp
*interp
, int objc
, Jim_Obj
*const *objv
)
10189 /* Incr refcount of arguments. */
10190 for (i
= 0; i
< objc
; i
++)
10191 Jim_IncrRefCount(objv
[i
]);
10193 retcode
= JimInvokeCommand(interp
, objc
, objv
);
10195 /* Decr refcount of arguments and return the retcode */
10196 for (i
= 0; i
< objc
; i
++)
10197 Jim_DecrRefCount(interp
, objv
[i
]);
10203 * Invokes 'prefix' as a command with the objv array as arguments.
10205 int Jim_EvalObjPrefix(Jim_Interp
*interp
, Jim_Obj
*prefix
, int objc
, Jim_Obj
*const *objv
)
10208 Jim_Obj
**nargv
= Jim_Alloc((objc
+ 1) * sizeof(*nargv
));
10211 memcpy(&nargv
[1], &objv
[0], sizeof(nargv
[0]) * objc
);
10212 ret
= Jim_EvalObjVector(interp
, objc
+ 1, nargv
);
10217 static void JimAddErrorToStack(Jim_Interp
*interp
, ScriptObj
*script
)
10219 if (!interp
->errorFlag
) {
10220 /* This is the first error, so save the file/line information and reset the stack */
10221 interp
->errorFlag
= 1;
10222 Jim_IncrRefCount(script
->fileNameObj
);
10223 Jim_DecrRefCount(interp
, interp
->errorFileNameObj
);
10224 interp
->errorFileNameObj
= script
->fileNameObj
;
10225 interp
->errorLine
= script
->linenr
;
10227 JimResetStackTrace(interp
);
10228 /* Always add a level where the error first occurs */
10229 interp
->addStackTrace
++;
10232 /* Now if this is an "interesting" level, add it to the stack trace */
10233 if (interp
->addStackTrace
> 0) {
10234 /* Add the stack info for the current level */
10236 JimAppendStackTrace(interp
, Jim_String(interp
->errorProc
), script
->fileNameObj
, script
->linenr
);
10238 /* Note: if we didn't have a filename for this level,
10239 * don't clear the addStackTrace flag
10240 * so we can pick it up at the next level
10242 if (Jim_Length(script
->fileNameObj
)) {
10243 interp
->addStackTrace
= 0;
10246 Jim_DecrRefCount(interp
, interp
->errorProc
);
10247 interp
->errorProc
= interp
->emptyObj
;
10248 Jim_IncrRefCount(interp
->errorProc
);
10252 static int JimSubstOneToken(Jim_Interp
*interp
, const ScriptToken
*token
, Jim_Obj
**objPtrPtr
)
10257 switch (token
->type
) {
10260 objPtr
= token
->objPtr
;
10263 objPtr
= Jim_GetVariable(interp
, token
->objPtr
, JIM_ERRMSG
);
10265 case JIM_TT_DICTSUGAR
:
10266 objPtr
= JimExpandDictSugar(interp
, token
->objPtr
);
10268 case JIM_TT_EXPRSUGAR
:
10269 ret
= Jim_EvalExpression(interp
, token
->objPtr
);
10270 if (ret
== JIM_OK
) {
10271 objPtr
= Jim_GetResult(interp
);
10278 ret
= Jim_EvalObj(interp
, token
->objPtr
);
10279 if (ret
== JIM_OK
|| ret
== JIM_RETURN
) {
10280 objPtr
= interp
->result
;
10282 /* includes JIM_BREAK, JIM_CONTINUE */
10288 "default token type (%d) reached " "in Jim_SubstObj().", token
->type
));
10293 *objPtrPtr
= objPtr
;
10299 /* Interpolate the given tokens into a unique Jim_Obj returned by reference
10300 * via *objPtrPtr. This function is only called by Jim_EvalObj() and Jim_SubstObj()
10301 * The returned object has refcount = 0.
10303 static Jim_Obj
*JimInterpolateTokens(Jim_Interp
*interp
, const ScriptToken
* token
, int tokens
, int flags
)
10307 Jim_Obj
*sintv
[JIM_EVAL_SINTV_LEN
];
10311 if (tokens
<= JIM_EVAL_SINTV_LEN
)
10314 intv
= Jim_Alloc(sizeof(Jim_Obj
*) * tokens
);
10316 /* Compute every token forming the argument
10317 * in the intv objects vector. */
10318 for (i
= 0; i
< tokens
; i
++) {
10319 switch (JimSubstOneToken(interp
, &token
[i
], &intv
[i
])) {
10324 if (flags
& JIM_SUBST_FLAG
) {
10329 /* XXX: Should probably set an error about break outside loop */
10330 /* fall through to error */
10332 if (flags
& JIM_SUBST_FLAG
) {
10336 /* XXX: Ditto continue outside loop */
10337 /* fall through to error */
10340 Jim_DecrRefCount(interp
, intv
[i
]);
10342 if (intv
!= sintv
) {
10347 Jim_IncrRefCount(intv
[i
]);
10348 Jim_String(intv
[i
]);
10349 totlen
+= intv
[i
]->length
;
10352 /* Fast path return for a single token */
10353 if (tokens
== 1 && intv
[0] && intv
== sintv
) {
10354 /* Reverse the Jim_IncrRefCount() above, but don't free the object */
10355 intv
[0]->refCount
--;
10359 /* Concatenate every token in an unique
10361 objPtr
= Jim_NewStringObjNoAlloc(interp
, NULL
, 0);
10363 if (tokens
== 4 && token
[0].type
== JIM_TT_ESC
&& token
[1].type
== JIM_TT_ESC
10364 && token
[2].type
== JIM_TT_VAR
) {
10365 /* May be able to do fast interpolated object -> dictSubst */
10366 objPtr
->typePtr
= &interpolatedObjType
;
10367 objPtr
->internalRep
.dictSubstValue
.varNameObjPtr
= token
[0].objPtr
;
10368 objPtr
->internalRep
.dictSubstValue
.indexObjPtr
= intv
[2];
10369 Jim_IncrRefCount(intv
[2]);
10371 else if (tokens
&& intv
[0] && intv
[0]->typePtr
== &sourceObjType
) {
10372 /* The first interpolated token is source, so preserve the source info */
10373 JimSetSourceInfo(interp
, objPtr
, intv
[0]->internalRep
.sourceValue
.fileNameObj
, intv
[0]->internalRep
.sourceValue
.lineNumber
);
10377 s
= objPtr
->bytes
= Jim_Alloc(totlen
+ 1);
10378 objPtr
->length
= totlen
;
10379 for (i
= 0; i
< tokens
; i
++) {
10381 memcpy(s
, intv
[i
]->bytes
, intv
[i
]->length
);
10382 s
+= intv
[i
]->length
;
10383 Jim_DecrRefCount(interp
, intv
[i
]);
10386 objPtr
->bytes
[totlen
] = '\0';
10387 /* Free the intv vector if not static. */
10388 if (intv
!= sintv
) {
10396 /* listPtr *must* be a list.
10397 * The contents of the list is evaluated with the first element as the command and
10398 * the remaining elements as the arguments.
10400 static int JimEvalObjList(Jim_Interp
*interp
, Jim_Obj
*listPtr
)
10402 int retcode
= JIM_OK
;
10404 JimPanic((Jim_IsList(listPtr
) == 0, "JimEvalObjList() invoked on non-list."));
10406 if (listPtr
->internalRep
.listValue
.len
) {
10407 Jim_IncrRefCount(listPtr
);
10408 retcode
= JimInvokeCommand(interp
,
10409 listPtr
->internalRep
.listValue
.len
,
10410 listPtr
->internalRep
.listValue
.ele
);
10411 Jim_DecrRefCount(interp
, listPtr
);
10416 int Jim_EvalObjList(Jim_Interp
*interp
, Jim_Obj
*listPtr
)
10418 SetListFromAny(interp
, listPtr
);
10419 return JimEvalObjList(interp
, listPtr
);
10422 int Jim_EvalObj(Jim_Interp
*interp
, Jim_Obj
*scriptObjPtr
)
10426 ScriptToken
*token
;
10427 int retcode
= JIM_OK
;
10428 Jim_Obj
*sargv
[JIM_EVAL_SARGV_LEN
], **argv
= NULL
;
10429 Jim_Obj
*prevScriptObj
;
10431 /* If the object is of type "list", with no string rep we can call
10432 * a specialized version of Jim_EvalObj() */
10433 if (Jim_IsList(scriptObjPtr
) && scriptObjPtr
->bytes
== NULL
) {
10434 return JimEvalObjList(interp
, scriptObjPtr
);
10437 Jim_IncrRefCount(scriptObjPtr
); /* Make sure it's shared. */
10438 script
= JimGetScript(interp
, scriptObjPtr
);
10439 if (!JimScriptValid(interp
, script
)) {
10440 Jim_DecrRefCount(interp
, scriptObjPtr
);
10444 /* Reset the interpreter result. This is useful to
10445 * return the empty result in the case of empty program. */
10446 Jim_SetEmptyResult(interp
);
10448 token
= script
->token
;
10450 #ifdef JIM_OPTIMIZATION
10451 /* Check for one of the following common scripts used by for, while
10456 if (script
->len
== 0) {
10457 Jim_DecrRefCount(interp
, scriptObjPtr
);
10460 if (script
->len
== 3
10461 && token
[1].objPtr
->typePtr
== &commandObjType
10462 && token
[1].objPtr
->internalRep
.cmdValue
.cmdPtr
->isproc
== 0
10463 && token
[1].objPtr
->internalRep
.cmdValue
.cmdPtr
->u
.native
.cmdProc
== Jim_IncrCoreCommand
10464 && token
[2].objPtr
->typePtr
== &variableObjType
) {
10466 Jim_Obj
*objPtr
= Jim_GetVariable(interp
, token
[2].objPtr
, JIM_NONE
);
10468 if (objPtr
&& !Jim_IsShared(objPtr
) && objPtr
->typePtr
== &intObjType
) {
10469 JimWideValue(objPtr
)++;
10470 Jim_InvalidateStringRep(objPtr
);
10471 Jim_DecrRefCount(interp
, scriptObjPtr
);
10472 Jim_SetResult(interp
, objPtr
);
10478 /* Now we have to make sure the internal repr will not be
10479 * freed on shimmering.
10481 * Think for example to this:
10483 * set x {llength $x; ... some more code ...}; eval $x
10485 * In order to preserve the internal rep, we increment the
10486 * inUse field of the script internal rep structure. */
10489 /* Stash the current script */
10490 prevScriptObj
= interp
->currentScriptObj
;
10491 interp
->currentScriptObj
= scriptObjPtr
;
10493 interp
->errorFlag
= 0;
10496 /* Execute every command sequentially until the end of the script
10497 * or an error occurs.
10499 for (i
= 0; i
< script
->len
&& retcode
== JIM_OK
; ) {
10503 /* First token of the line is always JIM_TT_LINE */
10504 argc
= token
[i
].objPtr
->internalRep
.scriptLineValue
.argc
;
10505 script
->linenr
= token
[i
].objPtr
->internalRep
.scriptLineValue
.line
;
10507 /* Allocate the arguments vector if required */
10508 if (argc
> JIM_EVAL_SARGV_LEN
)
10509 argv
= Jim_Alloc(sizeof(Jim_Obj
*) * argc
);
10511 /* Skip the JIM_TT_LINE token */
10514 /* Populate the arguments objects.
10515 * If an error occurs, retcode will be set and
10516 * 'j' will be set to the number of args expanded
10518 for (j
= 0; j
< argc
; j
++) {
10519 long wordtokens
= 1;
10521 Jim_Obj
*wordObjPtr
= NULL
;
10523 if (token
[i
].type
== JIM_TT_WORD
) {
10524 wordtokens
= JimWideValue(token
[i
++].objPtr
);
10525 if (wordtokens
< 0) {
10527 wordtokens
= -wordtokens
;
10531 if (wordtokens
== 1) {
10532 /* Fast path if the token does not
10533 * need interpolation */
10535 switch (token
[i
].type
) {
10538 wordObjPtr
= token
[i
].objPtr
;
10541 wordObjPtr
= Jim_GetVariable(interp
, token
[i
].objPtr
, JIM_ERRMSG
);
10543 case JIM_TT_EXPRSUGAR
:
10544 retcode
= Jim_EvalExpression(interp
, token
[i
].objPtr
);
10545 if (retcode
== JIM_OK
) {
10546 wordObjPtr
= Jim_GetResult(interp
);
10552 case JIM_TT_DICTSUGAR
:
10553 wordObjPtr
= JimExpandDictSugar(interp
, token
[i
].objPtr
);
10556 retcode
= Jim_EvalObj(interp
, token
[i
].objPtr
);
10557 if (retcode
== JIM_OK
) {
10558 wordObjPtr
= Jim_GetResult(interp
);
10562 JimPanic((1, "default token type reached " "in Jim_EvalObj()."));
10566 /* For interpolation we call a helper
10567 * function to do the work for us. */
10568 wordObjPtr
= JimInterpolateTokens(interp
, token
+ i
, wordtokens
, JIM_NONE
);
10572 if (retcode
== JIM_OK
) {
10578 Jim_IncrRefCount(wordObjPtr
);
10582 argv
[j
] = wordObjPtr
;
10585 /* Need to expand wordObjPtr into multiple args from argv[j] ... */
10586 int len
= Jim_ListLength(interp
, wordObjPtr
);
10587 int newargc
= argc
+ len
- 1;
10591 if (argv
== sargv
) {
10592 if (newargc
> JIM_EVAL_SARGV_LEN
) {
10593 argv
= Jim_Alloc(sizeof(*argv
) * newargc
);
10594 memcpy(argv
, sargv
, sizeof(*argv
) * j
);
10598 /* Need to realloc to make room for (len - 1) more entries */
10599 argv
= Jim_Realloc(argv
, sizeof(*argv
) * newargc
);
10603 /* Now copy in the expanded version */
10604 for (k
= 0; k
< len
; k
++) {
10605 argv
[j
++] = wordObjPtr
->internalRep
.listValue
.ele
[k
];
10606 Jim_IncrRefCount(wordObjPtr
->internalRep
.listValue
.ele
[k
]);
10609 /* The original object reference is no longer needed,
10610 * after the expansion it is no longer present on
10611 * the argument vector, but the single elements are
10613 Jim_DecrRefCount(interp
, wordObjPtr
);
10615 /* And update the indexes */
10621 if (retcode
== JIM_OK
&& argc
) {
10622 /* Invoke the command */
10623 retcode
= JimInvokeCommand(interp
, argc
, argv
);
10624 /* Check for a signal after each command */
10625 if (Jim_CheckSignal(interp
)) {
10626 retcode
= JIM_SIGNAL
;
10630 /* Finished with the command, so decrement ref counts of each argument */
10632 Jim_DecrRefCount(interp
, argv
[j
]);
10635 if (argv
!= sargv
) {
10641 /* Possibly add to the error stack trace */
10642 if (retcode
== JIM_ERR
) {
10643 JimAddErrorToStack(interp
, script
);
10645 /* Propagate the addStackTrace value through 'return -code error' */
10646 else if (retcode
!= JIM_RETURN
|| interp
->returnCode
!= JIM_ERR
) {
10647 /* No need to add stack trace */
10648 interp
->addStackTrace
= 0;
10651 /* Restore the current script */
10652 interp
->currentScriptObj
= prevScriptObj
;
10654 /* Note that we don't have to decrement inUse, because the
10655 * following code transfers our use of the reference again to
10656 * the script object. */
10657 Jim_FreeIntRep(interp
, scriptObjPtr
);
10658 scriptObjPtr
->typePtr
= &scriptObjType
;
10659 Jim_SetIntRepPtr(scriptObjPtr
, script
);
10660 Jim_DecrRefCount(interp
, scriptObjPtr
);
10665 static int JimSetProcArg(Jim_Interp
*interp
, Jim_Obj
*argNameObj
, Jim_Obj
*argValObj
)
10668 /* If argObjPtr begins with '&', do an automatic upvar */
10669 const char *varname
= Jim_String(argNameObj
);
10670 if (*varname
== '&') {
10671 /* First check that the target variable exists */
10673 Jim_CallFrame
*savedCallFrame
= interp
->framePtr
;
10675 interp
->framePtr
= interp
->framePtr
->parent
;
10676 objPtr
= Jim_GetVariable(interp
, argValObj
, JIM_ERRMSG
);
10677 interp
->framePtr
= savedCallFrame
;
10682 /* It exists, so perform the binding. */
10683 objPtr
= Jim_NewStringObj(interp
, varname
+ 1, -1);
10684 Jim_IncrRefCount(objPtr
);
10685 retcode
= Jim_SetVariableLink(interp
, objPtr
, argValObj
, interp
->framePtr
->parent
);
10686 Jim_DecrRefCount(interp
, objPtr
);
10689 retcode
= Jim_SetVariable(interp
, argNameObj
, argValObj
);
10695 * Sets the interp result to be an error message indicating the required proc args.
10697 static void JimSetProcWrongArgs(Jim_Interp
*interp
, Jim_Obj
*procNameObj
, Jim_Cmd
*cmd
)
10699 /* Create a nice error message, consistent with Tcl 8.5 */
10700 Jim_Obj
*argmsg
= Jim_NewStringObj(interp
, "", 0);
10703 for (i
= 0; i
< cmd
->u
.proc
.argListLen
; i
++) {
10704 Jim_AppendString(interp
, argmsg
, " ", 1);
10706 if (i
== cmd
->u
.proc
.argsPos
) {
10707 if (cmd
->u
.proc
.arglist
[i
].defaultObjPtr
) {
10709 Jim_AppendString(interp
, argmsg
, "?", 1);
10710 Jim_AppendObj(interp
, argmsg
, cmd
->u
.proc
.arglist
[i
].defaultObjPtr
);
10711 Jim_AppendString(interp
, argmsg
, " ...?", -1);
10714 /* We have plain args */
10715 Jim_AppendString(interp
, argmsg
, "?arg...?", -1);
10719 if (cmd
->u
.proc
.arglist
[i
].defaultObjPtr
) {
10720 Jim_AppendString(interp
, argmsg
, "?", 1);
10721 Jim_AppendObj(interp
, argmsg
, cmd
->u
.proc
.arglist
[i
].nameObjPtr
);
10722 Jim_AppendString(interp
, argmsg
, "?", 1);
10725 const char *arg
= Jim_String(cmd
->u
.proc
.arglist
[i
].nameObjPtr
);
10729 Jim_AppendString(interp
, argmsg
, arg
, -1);
10733 Jim_SetResultFormatted(interp
, "wrong # args: should be \"%#s%#s\"", procNameObj
, argmsg
);
10736 #ifdef jim_ext_namespace
10740 int Jim_EvalNamespace(Jim_Interp
*interp
, Jim_Obj
*scriptObj
, Jim_Obj
*nsObj
)
10742 Jim_CallFrame
*callFramePtr
;
10745 /* Create a new callframe */
10746 callFramePtr
= JimCreateCallFrame(interp
, interp
->framePtr
, nsObj
);
10747 callFramePtr
->argv
= &interp
->emptyObj
;
10748 callFramePtr
->argc
= 0;
10749 callFramePtr
->procArgsObjPtr
= NULL
;
10750 callFramePtr
->procBodyObjPtr
= scriptObj
;
10751 callFramePtr
->staticVars
= NULL
;
10752 callFramePtr
->fileNameObj
= interp
->emptyObj
;
10753 callFramePtr
->line
= 0;
10754 Jim_IncrRefCount(scriptObj
);
10755 interp
->framePtr
= callFramePtr
;
10757 /* Check if there are too nested calls */
10758 if (interp
->framePtr
->level
== interp
->maxCallFrameDepth
) {
10759 Jim_SetResultString(interp
, "Too many nested calls. Infinite recursion?", -1);
10763 /* Eval the body */
10764 retcode
= Jim_EvalObj(interp
, scriptObj
);
10767 /* Destroy the callframe */
10768 interp
->framePtr
= interp
->framePtr
->parent
;
10769 JimFreeCallFrame(interp
, callFramePtr
, JIM_FCF_REUSE
);
10775 /* Call a procedure implemented in Tcl.
10776 * It's possible to speed-up a lot this function, currently
10777 * the callframes are not cached, but allocated and
10778 * destroied every time. What is expecially costly is
10779 * to create/destroy the local vars hash table every time.
10781 * This can be fixed just implementing callframes caching
10782 * in JimCreateCallFrame() and JimFreeCallFrame(). */
10783 static int JimCallProcedure(Jim_Interp
*interp
, Jim_Cmd
*cmd
, int argc
, Jim_Obj
*const *argv
)
10785 Jim_CallFrame
*callFramePtr
;
10786 int i
, d
, retcode
, optargs
;
10790 if (argc
- 1 < cmd
->u
.proc
.reqArity
||
10791 (cmd
->u
.proc
.argsPos
< 0 && argc
- 1 > cmd
->u
.proc
.reqArity
+ cmd
->u
.proc
.optArity
)) {
10792 JimSetProcWrongArgs(interp
, argv
[0], cmd
);
10796 if (Jim_Length(cmd
->u
.proc
.bodyObjPtr
) == 0) {
10797 /* Optimise for procedure with no body - useful for optional debugging */
10801 /* Check if there are too nested calls */
10802 if (interp
->framePtr
->level
== interp
->maxCallFrameDepth
) {
10803 Jim_SetResultString(interp
, "Too many nested calls. Infinite recursion?", -1);
10807 /* Create a new callframe */
10808 callFramePtr
= JimCreateCallFrame(interp
, interp
->framePtr
, cmd
->u
.proc
.nsObj
);
10809 callFramePtr
->argv
= argv
;
10810 callFramePtr
->argc
= argc
;
10811 callFramePtr
->procArgsObjPtr
= cmd
->u
.proc
.argListObjPtr
;
10812 callFramePtr
->procBodyObjPtr
= cmd
->u
.proc
.bodyObjPtr
;
10813 callFramePtr
->staticVars
= cmd
->u
.proc
.staticVars
;
10815 /* Remember where we were called from. */
10816 script
= JimGetScript(interp
, interp
->currentScriptObj
);
10817 callFramePtr
->fileNameObj
= script
->fileNameObj
;
10818 callFramePtr
->line
= script
->linenr
;
10820 Jim_IncrRefCount(cmd
->u
.proc
.argListObjPtr
);
10821 Jim_IncrRefCount(cmd
->u
.proc
.bodyObjPtr
);
10822 interp
->framePtr
= callFramePtr
;
10824 /* How many optional args are available */
10825 optargs
= (argc
- 1 - cmd
->u
.proc
.reqArity
);
10827 /* Step 'i' along the actual args, and step 'd' along the formal args */
10829 for (d
= 0; d
< cmd
->u
.proc
.argListLen
; d
++) {
10830 Jim_Obj
*nameObjPtr
= cmd
->u
.proc
.arglist
[d
].nameObjPtr
;
10831 if (d
== cmd
->u
.proc
.argsPos
) {
10833 Jim_Obj
*listObjPtr
;
10835 if (cmd
->u
.proc
.reqArity
+ cmd
->u
.proc
.optArity
< argc
- 1) {
10836 argsLen
= argc
- 1 - (cmd
->u
.proc
.reqArity
+ cmd
->u
.proc
.optArity
);
10838 listObjPtr
= Jim_NewListObj(interp
, &argv
[i
], argsLen
);
10840 /* It is possible to rename args. */
10841 if (cmd
->u
.proc
.arglist
[d
].defaultObjPtr
) {
10842 nameObjPtr
=cmd
->u
.proc
.arglist
[d
].defaultObjPtr
;
10844 retcode
= Jim_SetVariable(interp
, nameObjPtr
, listObjPtr
);
10845 if (retcode
!= JIM_OK
) {
10853 /* Optional or required? */
10854 if (cmd
->u
.proc
.arglist
[d
].defaultObjPtr
== NULL
|| optargs
-- > 0) {
10855 retcode
= JimSetProcArg(interp
, nameObjPtr
, argv
[i
++]);
10858 /* Ran out, so use the default */
10859 retcode
= Jim_SetVariable(interp
, nameObjPtr
, cmd
->u
.proc
.arglist
[d
].defaultObjPtr
);
10861 if (retcode
!= JIM_OK
) {
10866 /* Eval the body */
10867 retcode
= Jim_EvalObj(interp
, cmd
->u
.proc
.bodyObjPtr
);
10871 /* Invoke $jim::defer then destroy the callframe */
10872 retcode
= JimInvokeDefer(interp
, retcode
);
10873 interp
->framePtr
= interp
->framePtr
->parent
;
10874 JimFreeCallFrame(interp
, callFramePtr
, JIM_FCF_REUSE
);
10879 int Jim_EvalSource(Jim_Interp
*interp
, const char *filename
, int lineno
, const char *script
)
10882 Jim_Obj
*scriptObjPtr
;
10884 scriptObjPtr
= Jim_NewStringObj(interp
, script
, -1);
10885 Jim_IncrRefCount(scriptObjPtr
);
10888 Jim_Obj
*prevScriptObj
;
10890 JimSetSourceInfo(interp
, scriptObjPtr
, Jim_NewStringObj(interp
, filename
, -1), lineno
);
10892 prevScriptObj
= interp
->currentScriptObj
;
10893 interp
->currentScriptObj
= scriptObjPtr
;
10895 retval
= Jim_EvalObj(interp
, scriptObjPtr
);
10897 interp
->currentScriptObj
= prevScriptObj
;
10900 retval
= Jim_EvalObj(interp
, scriptObjPtr
);
10902 Jim_DecrRefCount(interp
, scriptObjPtr
);
10906 int Jim_Eval(Jim_Interp
*interp
, const char *script
)
10908 return Jim_EvalObj(interp
, Jim_NewStringObj(interp
, script
, -1));
10911 /* Execute script in the scope of the global level */
10912 int Jim_EvalGlobal(Jim_Interp
*interp
, const char *script
)
10915 Jim_CallFrame
*savedFramePtr
= interp
->framePtr
;
10917 interp
->framePtr
= interp
->topFramePtr
;
10918 retval
= Jim_Eval(interp
, script
);
10919 interp
->framePtr
= savedFramePtr
;
10924 int Jim_EvalFileGlobal(Jim_Interp
*interp
, const char *filename
)
10927 Jim_CallFrame
*savedFramePtr
= interp
->framePtr
;
10929 interp
->framePtr
= interp
->topFramePtr
;
10930 retval
= Jim_EvalFile(interp
, filename
);
10931 interp
->framePtr
= savedFramePtr
;
10936 #include <sys/stat.h>
10938 int Jim_EvalFile(Jim_Interp
*interp
, const char *filename
)
10942 Jim_Obj
*scriptObjPtr
;
10943 Jim_Obj
*prevScriptObj
;
10948 if (stat(filename
, &sb
) != 0 || (fp
= fopen(filename
, "rt")) == NULL
) {
10949 Jim_SetResultFormatted(interp
, "couldn't read file \"%s\": %s", filename
, strerror(errno
));
10952 if (sb
.st_size
== 0) {
10957 buf
= Jim_Alloc(sb
.st_size
+ 1);
10958 readlen
= fread(buf
, 1, sb
.st_size
, fp
);
10962 Jim_SetResultFormatted(interp
, "failed to load file \"%s\": %s", filename
, strerror(errno
));
10968 scriptObjPtr
= Jim_NewStringObjNoAlloc(interp
, buf
, readlen
);
10969 JimSetSourceInfo(interp
, scriptObjPtr
, Jim_NewStringObj(interp
, filename
, -1), 1);
10970 Jim_IncrRefCount(scriptObjPtr
);
10972 prevScriptObj
= interp
->currentScriptObj
;
10973 interp
->currentScriptObj
= scriptObjPtr
;
10975 retcode
= Jim_EvalObj(interp
, scriptObjPtr
);
10977 /* Handle the JIM_RETURN return code */
10978 if (retcode
== JIM_RETURN
) {
10979 if (--interp
->returnLevel
<= 0) {
10980 retcode
= interp
->returnCode
;
10981 interp
->returnCode
= JIM_OK
;
10982 interp
->returnLevel
= 0;
10985 if (retcode
== JIM_ERR
) {
10986 /* EvalFile changes context, so add a stack frame here */
10987 interp
->addStackTrace
++;
10990 interp
->currentScriptObj
= prevScriptObj
;
10992 Jim_DecrRefCount(interp
, scriptObjPtr
);
10997 /* -----------------------------------------------------------------------------
10999 * ---------------------------------------------------------------------------*/
11000 static void JimParseSubst(struct JimParserCtx
*pc
, int flags
)
11002 pc
->tstart
= pc
->p
;
11003 pc
->tline
= pc
->linenr
;
11005 if (pc
->len
== 0) {
11007 pc
->tt
= JIM_TT_EOL
;
11011 if (*pc
->p
== '[' && !(flags
& JIM_SUBST_NOCMD
)) {
11015 if (*pc
->p
== '$' && !(flags
& JIM_SUBST_NOVAR
)) {
11016 if (JimParseVar(pc
) == JIM_OK
) {
11019 /* Not a var, so treat as a string */
11020 pc
->tstart
= pc
->p
;
11021 flags
|= JIM_SUBST_NOVAR
;
11024 if (*pc
->p
== '$' && !(flags
& JIM_SUBST_NOVAR
)) {
11027 if (*pc
->p
== '[' && !(flags
& JIM_SUBST_NOCMD
)) {
11030 if (*pc
->p
== '\\' && pc
->len
> 1) {
11037 pc
->tend
= pc
->p
- 1;
11038 pc
->tt
= (flags
& JIM_SUBST_NOESC
) ? JIM_TT_STR
: JIM_TT_ESC
;
11041 /* The subst object type reuses most of the data structures and functions
11042 * of the script object. Script's data structures are a bit more complex
11043 * for what is needed for [subst]itution tasks, but the reuse helps to
11044 * deal with a single data structure at the cost of some more memory
11045 * usage for substitutions. */
11047 /* This method takes the string representation of an object
11048 * as a Tcl string where to perform [subst]itution, and generates
11049 * the pre-parsed internal representation. */
11050 static int SetSubstFromAny(Jim_Interp
*interp
, struct Jim_Obj
*objPtr
, int flags
)
11053 const char *scriptText
= Jim_GetString(objPtr
, &scriptTextLen
);
11054 struct JimParserCtx parser
;
11055 struct ScriptObj
*script
= Jim_Alloc(sizeof(*script
));
11056 ParseTokenList tokenlist
;
11058 /* Initially parse the subst into tokens (in tokenlist) */
11059 ScriptTokenListInit(&tokenlist
);
11061 JimParserInit(&parser
, scriptText
, scriptTextLen
, 1);
11063 JimParseSubst(&parser
, flags
);
11065 /* Note that subst doesn't need the EOL token */
11068 ScriptAddToken(&tokenlist
, parser
.tstart
, parser
.tend
- parser
.tstart
+ 1, parser
.tt
,
11072 /* Create the "real" subst/script tokens from the initial token list */
11074 script
->substFlags
= flags
;
11075 script
->fileNameObj
= interp
->emptyObj
;
11076 Jim_IncrRefCount(script
->fileNameObj
);
11077 SubstObjAddTokens(interp
, script
, &tokenlist
);
11079 /* No longer need the token list */
11080 ScriptTokenListFree(&tokenlist
);
11082 #ifdef DEBUG_SHOW_SUBST
11086 printf("==== Subst ====\n");
11087 for (i
= 0; i
< script
->len
; i
++) {
11088 printf("[%2d] %s '%s'\n", i
, jim_tt_name(script
->token
[i
].type
),
11089 Jim_String(script
->token
[i
].objPtr
));
11094 /* Free the old internal rep and set the new one. */
11095 Jim_FreeIntRep(interp
, objPtr
);
11096 Jim_SetIntRepPtr(objPtr
, script
);
11097 objPtr
->typePtr
= &scriptObjType
;
11101 static ScriptObj
*Jim_GetSubst(Jim_Interp
*interp
, Jim_Obj
*objPtr
, int flags
)
11103 if (objPtr
->typePtr
!= &scriptObjType
|| ((ScriptObj
*)Jim_GetIntRepPtr(objPtr
))->substFlags
!= flags
)
11104 SetSubstFromAny(interp
, objPtr
, flags
);
11105 return (ScriptObj
*) Jim_GetIntRepPtr(objPtr
);
11108 /* Performs commands,variables,blackslashes substitution,
11109 * storing the result object (with refcount 0) into
11111 int Jim_SubstObj(Jim_Interp
*interp
, Jim_Obj
*substObjPtr
, Jim_Obj
**resObjPtrPtr
, int flags
)
11115 JimPanic((substObjPtr
->refCount
== 0, "Jim_SubstObj() called with zero refcount object"));
11117 script
= Jim_GetSubst(interp
, substObjPtr
, flags
);
11119 Jim_IncrRefCount(substObjPtr
); /* Make sure it's shared. */
11120 /* In order to preserve the internal rep, we increment the
11121 * inUse field of the script internal rep structure. */
11124 *resObjPtrPtr
= JimInterpolateTokens(interp
, script
->token
, script
->len
, flags
);
11127 Jim_DecrRefCount(interp
, substObjPtr
);
11128 if (*resObjPtrPtr
== NULL
) {
11134 /* -----------------------------------------------------------------------------
11135 * Core commands utility functions
11136 * ---------------------------------------------------------------------------*/
11137 void Jim_WrongNumArgs(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
, const char *msg
)
11140 Jim_Obj
*listObjPtr
;
11142 JimPanic((argc
== 0, "Jim_WrongNumArgs() called with argc=0"));
11144 listObjPtr
= Jim_NewListObj(interp
, argv
, argc
);
11147 Jim_ListAppendElement(interp
, listObjPtr
, Jim_NewStringObj(interp
, msg
, -1));
11149 Jim_IncrRefCount(listObjPtr
);
11150 objPtr
= Jim_ListJoin(interp
, listObjPtr
, " ", 1);
11151 Jim_DecrRefCount(interp
, listObjPtr
);
11153 Jim_SetResultFormatted(interp
, "wrong # args: should be \"%#s\"", objPtr
);
11157 * May add the key and/or value to the list.
11159 typedef void JimHashtableIteratorCallbackType(Jim_Interp
*interp
, Jim_Obj
*listObjPtr
,
11160 Jim_HashEntry
*he
, int type
);
11162 #define JimTrivialMatch(pattern) (strpbrk((pattern), "*[?\\") == NULL)
11165 * For each key of the hash table 'ht' (with string keys) which matches the glob pattern (all if NULL),
11166 * invoke the callback to add entries to a list.
11167 * Returns the list.
11169 static Jim_Obj
*JimHashtablePatternMatch(Jim_Interp
*interp
, Jim_HashTable
*ht
, Jim_Obj
*patternObjPtr
,
11170 JimHashtableIteratorCallbackType
*callback
, int type
)
11173 Jim_Obj
*listObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
11175 /* Check for the non-pattern case. We can do this much more efficiently. */
11176 if (patternObjPtr
&& JimTrivialMatch(Jim_String(patternObjPtr
))) {
11177 he
= Jim_FindHashEntry(ht
, Jim_String(patternObjPtr
));
11179 callback(interp
, listObjPtr
, he
, type
);
11183 Jim_HashTableIterator htiter
;
11184 JimInitHashTableIterator(ht
, &htiter
);
11185 while ((he
= Jim_NextHashEntry(&htiter
)) != NULL
) {
11186 if (patternObjPtr
== NULL
|| JimGlobMatch(Jim_String(patternObjPtr
), he
->key
, 0)) {
11187 callback(interp
, listObjPtr
, he
, type
);
11194 /* Keep these in order */
11195 #define JIM_CMDLIST_COMMANDS 0
11196 #define JIM_CMDLIST_PROCS 1
11197 #define JIM_CMDLIST_CHANNELS 2
11200 * Adds matching command names (procs, channels) to the list.
11202 static void JimCommandMatch(Jim_Interp
*interp
, Jim_Obj
*listObjPtr
,
11203 Jim_HashEntry
*he
, int type
)
11205 Jim_Cmd
*cmdPtr
= Jim_GetHashEntryVal(he
);
11208 if (type
== JIM_CMDLIST_PROCS
&& !cmdPtr
->isproc
) {
11213 objPtr
= Jim_NewStringObj(interp
, he
->key
, -1);
11214 Jim_IncrRefCount(objPtr
);
11216 if (type
!= JIM_CMDLIST_CHANNELS
|| Jim_AioFilehandle(interp
, objPtr
)) {
11217 Jim_ListAppendElement(interp
, listObjPtr
, objPtr
);
11219 Jim_DecrRefCount(interp
, objPtr
);
11222 /* type is JIM_CMDLIST_xxx */
11223 static Jim_Obj
*JimCommandsList(Jim_Interp
*interp
, Jim_Obj
*patternObjPtr
, int type
)
11225 return JimHashtablePatternMatch(interp
, &interp
->commands
, patternObjPtr
, JimCommandMatch
, type
);
11228 /* Keep these in order */
11229 #define JIM_VARLIST_GLOBALS 0
11230 #define JIM_VARLIST_LOCALS 1
11231 #define JIM_VARLIST_VARS 2
11233 #define JIM_VARLIST_VALUES 0x1000
11236 * Adds matching variable names to the list.
11238 static void JimVariablesMatch(Jim_Interp
*interp
, Jim_Obj
*listObjPtr
,
11239 Jim_HashEntry
*he
, int type
)
11241 Jim_Var
*varPtr
= Jim_GetHashEntryVal(he
);
11243 if (type
!= JIM_VARLIST_LOCALS
|| varPtr
->linkFramePtr
== NULL
) {
11244 Jim_ListAppendElement(interp
, listObjPtr
, Jim_NewStringObj(interp
, he
->key
, -1));
11245 if (type
& JIM_VARLIST_VALUES
) {
11246 Jim_ListAppendElement(interp
, listObjPtr
, varPtr
->objPtr
);
11251 /* mode is JIM_VARLIST_xxx */
11252 static Jim_Obj
*JimVariablesList(Jim_Interp
*interp
, Jim_Obj
*patternObjPtr
, int mode
)
11254 if (mode
== JIM_VARLIST_LOCALS
&& interp
->framePtr
== interp
->topFramePtr
) {
11255 /* For [info locals], if we are at top level an emtpy list
11256 * is returned. I don't agree, but we aim at compatibility (SS) */
11257 return interp
->emptyObj
;
11260 Jim_CallFrame
*framePtr
= (mode
== JIM_VARLIST_GLOBALS
) ? interp
->topFramePtr
: interp
->framePtr
;
11261 return JimHashtablePatternMatch(interp
, &framePtr
->vars
, patternObjPtr
, JimVariablesMatch
, mode
);
11265 static int JimInfoLevel(Jim_Interp
*interp
, Jim_Obj
*levelObjPtr
,
11266 Jim_Obj
**objPtrPtr
, int info_level_cmd
)
11268 Jim_CallFrame
*targetCallFrame
;
11270 targetCallFrame
= JimGetCallFrameByInteger(interp
, levelObjPtr
);
11271 if (targetCallFrame
== NULL
) {
11274 /* No proc call at toplevel callframe */
11275 if (targetCallFrame
== interp
->topFramePtr
) {
11276 Jim_SetResultFormatted(interp
, "bad level \"%#s\"", levelObjPtr
);
11279 if (info_level_cmd
) {
11280 *objPtrPtr
= Jim_NewListObj(interp
, targetCallFrame
->argv
, targetCallFrame
->argc
);
11283 Jim_Obj
*listObj
= Jim_NewListObj(interp
, NULL
, 0);
11285 Jim_ListAppendElement(interp
, listObj
, targetCallFrame
->argv
[0]);
11286 Jim_ListAppendElement(interp
, listObj
, targetCallFrame
->fileNameObj
);
11287 Jim_ListAppendElement(interp
, listObj
, Jim_NewIntObj(interp
, targetCallFrame
->line
));
11288 *objPtrPtr
= listObj
;
11293 /* -----------------------------------------------------------------------------
11295 * ---------------------------------------------------------------------------*/
11297 /* fake [puts] -- not the real puts, just for debugging. */
11298 static int Jim_PutsCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11300 if (argc
!= 2 && argc
!= 3) {
11301 Jim_WrongNumArgs(interp
, 1, argv
, "?-nonewline? string");
11305 if (!Jim_CompareStringImmediate(interp
, argv
[1], "-nonewline")) {
11306 Jim_SetResultString(interp
, "The second argument must " "be -nonewline", -1);
11310 fputs(Jim_String(argv
[2]), stdout
);
11314 puts(Jim_String(argv
[1]));
11319 /* Helper for [+] and [*] */
11320 static int JimAddMulHelper(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
, int op
)
11322 jim_wide wideValue
, res
;
11323 double doubleValue
, doubleRes
;
11326 res
= (op
== JIM_EXPROP_ADD
) ? 0 : 1;
11328 for (i
= 1; i
< argc
; i
++) {
11329 if (Jim_GetWide(interp
, argv
[i
], &wideValue
) != JIM_OK
)
11331 if (op
== JIM_EXPROP_ADD
)
11336 Jim_SetResultInt(interp
, res
);
11339 doubleRes
= (double)res
;
11340 for (; i
< argc
; i
++) {
11341 if (Jim_GetDouble(interp
, argv
[i
], &doubleValue
) != JIM_OK
)
11343 if (op
== JIM_EXPROP_ADD
)
11344 doubleRes
+= doubleValue
;
11346 doubleRes
*= doubleValue
;
11348 Jim_SetResult(interp
, Jim_NewDoubleObj(interp
, doubleRes
));
11352 /* Helper for [-] and [/] */
11353 static int JimSubDivHelper(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
, int op
)
11355 jim_wide wideValue
, res
= 0;
11356 double doubleValue
, doubleRes
= 0;
11360 Jim_WrongNumArgs(interp
, 1, argv
, "number ?number ... number?");
11363 else if (argc
== 2) {
11364 /* The arity = 2 case is different. For [- x] returns -x,
11365 * while [/ x] returns 1/x. */
11366 if (Jim_GetWide(interp
, argv
[1], &wideValue
) != JIM_OK
) {
11367 if (Jim_GetDouble(interp
, argv
[1], &doubleValue
) != JIM_OK
) {
11371 if (op
== JIM_EXPROP_SUB
)
11372 doubleRes
= -doubleValue
;
11374 doubleRes
= 1.0 / doubleValue
;
11375 Jim_SetResult(interp
, Jim_NewDoubleObj(interp
, doubleRes
));
11379 if (op
== JIM_EXPROP_SUB
) {
11381 Jim_SetResultInt(interp
, res
);
11384 doubleRes
= 1.0 / wideValue
;
11385 Jim_SetResult(interp
, Jim_NewDoubleObj(interp
, doubleRes
));
11390 if (Jim_GetWide(interp
, argv
[1], &res
) != JIM_OK
) {
11391 if (Jim_GetDouble(interp
, argv
[1], &doubleRes
)
11400 for (i
= 2; i
< argc
; i
++) {
11401 if (Jim_GetWide(interp
, argv
[i
], &wideValue
) != JIM_OK
) {
11402 doubleRes
= (double)res
;
11405 if (op
== JIM_EXPROP_SUB
)
11408 if (wideValue
== 0) {
11409 Jim_SetResultString(interp
, "Division by zero", -1);
11415 Jim_SetResultInt(interp
, res
);
11418 for (; i
< argc
; i
++) {
11419 if (Jim_GetDouble(interp
, argv
[i
], &doubleValue
) != JIM_OK
)
11421 if (op
== JIM_EXPROP_SUB
)
11422 doubleRes
-= doubleValue
;
11424 doubleRes
/= doubleValue
;
11426 Jim_SetResult(interp
, Jim_NewDoubleObj(interp
, doubleRes
));
11432 static int Jim_AddCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11434 return JimAddMulHelper(interp
, argc
, argv
, JIM_EXPROP_ADD
);
11438 static int Jim_MulCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11440 return JimAddMulHelper(interp
, argc
, argv
, JIM_EXPROP_MUL
);
11444 static int Jim_SubCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11446 return JimSubDivHelper(interp
, argc
, argv
, JIM_EXPROP_SUB
);
11450 static int Jim_DivCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11452 return JimSubDivHelper(interp
, argc
, argv
, JIM_EXPROP_DIV
);
11456 static int Jim_SetCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11458 if (argc
!= 2 && argc
!= 3) {
11459 Jim_WrongNumArgs(interp
, 1, argv
, "varName ?newValue?");
11465 objPtr
= Jim_GetVariable(interp
, argv
[1], JIM_ERRMSG
);
11468 Jim_SetResult(interp
, objPtr
);
11471 /* argc == 3 case. */
11472 if (Jim_SetVariable(interp
, argv
[1], argv
[2]) != JIM_OK
)
11474 Jim_SetResult(interp
, argv
[2]);
11480 * unset ?-nocomplain? ?--? ?varName ...?
11482 static int Jim_UnsetCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11488 if (Jim_CompareStringImmediate(interp
, argv
[i
], "--")) {
11492 if (Jim_CompareStringImmediate(interp
, argv
[i
], "-nocomplain")) {
11501 if (Jim_UnsetVariable(interp
, argv
[i
], complain
? JIM_ERRMSG
: JIM_NONE
) != JIM_OK
11511 static int Jim_WhileCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11514 Jim_WrongNumArgs(interp
, 1, argv
, "condition body");
11518 /* The general purpose implementation of while starts here */
11520 int boolean
, retval
;
11522 if ((retval
= Jim_GetBoolFromExpr(interp
, argv
[1], &boolean
)) != JIM_OK
)
11527 if ((retval
= Jim_EvalObj(interp
, argv
[2])) != JIM_OK
) {
11541 Jim_SetEmptyResult(interp
);
11546 static int Jim_ForCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11550 Jim_Obj
*varNamePtr
= NULL
;
11551 Jim_Obj
*stopVarNamePtr
= NULL
;
11554 Jim_WrongNumArgs(interp
, 1, argv
, "start test next body");
11558 /* Do the initialisation */
11559 if ((retval
= Jim_EvalObj(interp
, argv
[1])) != JIM_OK
) {
11563 /* And do the first test now. Better for optimisation
11564 * if we can do next/test at the bottom of the loop
11566 retval
= Jim_GetBoolFromExpr(interp
, argv
[2], &boolean
);
11568 /* Ready to do the body as follows:
11570 * body // check retcode
11571 * next // check retcode
11572 * test // check retcode/test bool
11576 #ifdef JIM_OPTIMIZATION
11577 /* Check if the for is on the form:
11578 * for ... {$i < CONST} {incr i}
11579 * for ... {$i < $j} {incr i}
11581 if (retval
== JIM_OK
&& boolean
) {
11582 ScriptObj
*incrScript
;
11583 struct ExprTree
*expr
;
11584 jim_wide stop
, currentVal
;
11588 /* Do it only if there aren't shared arguments */
11589 expr
= JimGetExpression(interp
, argv
[2]);
11590 incrScript
= JimGetScript(interp
, argv
[3]);
11592 /* Ensure proper lengths to start */
11593 if (incrScript
== NULL
|| incrScript
->len
!= 3 || !expr
|| expr
->len
!= 3) {
11596 /* Ensure proper token types. */
11597 if (incrScript
->token
[1].type
!= JIM_TT_ESC
) {
11601 if (expr
->expr
->type
== JIM_EXPROP_LT
) {
11604 else if (expr
->expr
->type
== JIM_EXPROP_LTE
) {
11611 if (expr
->expr
->left
->type
!= JIM_TT_VAR
) {
11615 if (expr
->expr
->right
->type
!= JIM_TT_VAR
&& expr
->expr
->right
->type
!= JIM_TT_EXPR_INT
) {
11619 /* Update command must be incr */
11620 if (!Jim_CompareStringImmediate(interp
, incrScript
->token
[1].objPtr
, "incr")) {
11624 /* incr, expression must be about the same variable */
11625 if (!Jim_StringEqObj(incrScript
->token
[2].objPtr
, expr
->expr
->left
->objPtr
)) {
11629 /* Get the stop condition (must be a variable or integer) */
11630 if (expr
->expr
->right
->type
== JIM_TT_EXPR_INT
) {
11631 if (Jim_GetWide(interp
, expr
->expr
->right
->objPtr
, &stop
) == JIM_ERR
) {
11636 stopVarNamePtr
= expr
->expr
->right
->objPtr
;
11637 Jim_IncrRefCount(stopVarNamePtr
);
11638 /* Keep the compiler happy */
11642 /* Initialization */
11643 varNamePtr
= expr
->expr
->left
->objPtr
;
11644 Jim_IncrRefCount(varNamePtr
);
11646 objPtr
= Jim_GetVariable(interp
, varNamePtr
, JIM_NONE
);
11647 if (objPtr
== NULL
|| Jim_GetWide(interp
, objPtr
, ¤tVal
) != JIM_OK
) {
11651 /* --- OPTIMIZED FOR --- */
11652 while (retval
== JIM_OK
) {
11653 /* === Check condition === */
11654 /* Note that currentVal is already set here */
11656 /* Immediate or Variable? get the 'stop' value if the latter. */
11657 if (stopVarNamePtr
) {
11658 objPtr
= Jim_GetVariable(interp
, stopVarNamePtr
, JIM_NONE
);
11659 if (objPtr
== NULL
|| Jim_GetWide(interp
, objPtr
, &stop
) != JIM_OK
) {
11664 if (currentVal
>= stop
+ cmpOffset
) {
11669 retval
= Jim_EvalObj(interp
, argv
[4]);
11670 if (retval
== JIM_OK
|| retval
== JIM_CONTINUE
) {
11673 objPtr
= Jim_GetVariable(interp
, varNamePtr
, JIM_ERRMSG
);
11676 if (objPtr
== NULL
) {
11680 if (!Jim_IsShared(objPtr
) && objPtr
->typePtr
== &intObjType
) {
11681 currentVal
= ++JimWideValue(objPtr
);
11682 Jim_InvalidateStringRep(objPtr
);
11685 if (Jim_GetWide(interp
, objPtr
, ¤tVal
) != JIM_OK
||
11686 Jim_SetVariable(interp
, varNamePtr
, Jim_NewIntObj(interp
,
11687 ++currentVal
)) != JIM_OK
) {
11698 while (boolean
&& (retval
== JIM_OK
|| retval
== JIM_CONTINUE
)) {
11700 retval
= Jim_EvalObj(interp
, argv
[4]);
11702 if (retval
== JIM_OK
|| retval
== JIM_CONTINUE
) {
11704 JIM_IF_OPTIM(evalnext
:)
11705 retval
= Jim_EvalObj(interp
, argv
[3]);
11706 if (retval
== JIM_OK
|| retval
== JIM_CONTINUE
) {
11708 JIM_IF_OPTIM(testcond
:)
11709 retval
= Jim_GetBoolFromExpr(interp
, argv
[2], &boolean
);
11714 if (stopVarNamePtr
) {
11715 Jim_DecrRefCount(interp
, stopVarNamePtr
);
11718 Jim_DecrRefCount(interp
, varNamePtr
);
11721 if (retval
== JIM_CONTINUE
|| retval
== JIM_BREAK
|| retval
== JIM_OK
) {
11722 Jim_SetEmptyResult(interp
);
11730 static int Jim_LoopCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11736 Jim_Obj
*bodyObjPtr
;
11738 if (argc
!= 5 && argc
!= 6) {
11739 Jim_WrongNumArgs(interp
, 1, argv
, "var first limit ?incr? body");
11743 if (Jim_GetWide(interp
, argv
[2], &i
) != JIM_OK
||
11744 Jim_GetWide(interp
, argv
[3], &limit
) != JIM_OK
||
11745 (argc
== 6 && Jim_GetWide(interp
, argv
[4], &incr
) != JIM_OK
)) {
11748 bodyObjPtr
= (argc
== 5) ? argv
[4] : argv
[5];
11750 retval
= Jim_SetVariable(interp
, argv
[1], argv
[2]);
11752 while (((i
< limit
&& incr
> 0) || (i
> limit
&& incr
< 0)) && retval
== JIM_OK
) {
11753 retval
= Jim_EvalObj(interp
, bodyObjPtr
);
11754 if (retval
== JIM_OK
|| retval
== JIM_CONTINUE
) {
11755 Jim_Obj
*objPtr
= Jim_GetVariable(interp
, argv
[1], JIM_ERRMSG
);
11762 if (objPtr
&& !Jim_IsShared(objPtr
) && objPtr
->typePtr
== &intObjType
) {
11763 if (argv
[1]->typePtr
!= &variableObjType
) {
11764 if (Jim_SetVariable(interp
, argv
[1], objPtr
) != JIM_OK
) {
11768 JimWideValue(objPtr
) = i
;
11769 Jim_InvalidateStringRep(objPtr
);
11771 /* The following step is required in order to invalidate the
11772 * string repr of "FOO" if the var name is of the form of "FOO(IDX)" */
11773 if (argv
[1]->typePtr
!= &variableObjType
) {
11774 if (Jim_SetVariable(interp
, argv
[1], objPtr
) != JIM_OK
) {
11781 objPtr
= Jim_NewIntObj(interp
, i
);
11782 retval
= Jim_SetVariable(interp
, argv
[1], objPtr
);
11783 if (retval
!= JIM_OK
) {
11784 Jim_FreeNewObj(interp
, objPtr
);
11790 if (retval
== JIM_OK
|| retval
== JIM_CONTINUE
|| retval
== JIM_BREAK
) {
11791 Jim_SetEmptyResult(interp
);
11797 /* List iterators make it easy to iterate over a list.
11798 * At some point iterators will be expanded to support generators.
11806 * Initialise the iterator at the start of the list.
11808 static void JimListIterInit(Jim_ListIter
*iter
, Jim_Obj
*objPtr
)
11810 iter
->objPtr
= objPtr
;
11815 * Returns the next object from the list, or NULL on end-of-list.
11817 static Jim_Obj
*JimListIterNext(Jim_Interp
*interp
, Jim_ListIter
*iter
)
11819 if (iter
->idx
>= Jim_ListLength(interp
, iter
->objPtr
)) {
11822 return iter
->objPtr
->internalRep
.listValue
.ele
[iter
->idx
++];
11826 * Returns 1 if end-of-list has been reached.
11828 static int JimListIterDone(Jim_Interp
*interp
, Jim_ListIter
*iter
)
11830 return iter
->idx
>= Jim_ListLength(interp
, iter
->objPtr
);
11833 /* foreach + lmap implementation. */
11834 static int JimForeachMapHelper(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
, int doMap
)
11836 int result
= JIM_OK
;
11838 Jim_ListIter twoiters
[2]; /* Avoid allocation for a single list */
11839 Jim_ListIter
*iters
;
11841 Jim_Obj
*resultObj
;
11843 if (argc
< 4 || argc
% 2 != 0) {
11844 Jim_WrongNumArgs(interp
, 1, argv
, "varList list ?varList list ...? script");
11847 script
= argv
[argc
- 1]; /* Last argument is a script */
11848 numargs
= (argc
- 1 - 1); /* argc - 'foreach' - script */
11850 if (numargs
== 2) {
11854 iters
= Jim_Alloc(numargs
* sizeof(*iters
));
11856 for (i
= 0; i
< numargs
; i
++) {
11857 JimListIterInit(&iters
[i
], argv
[i
+ 1]);
11858 if (i
% 2 == 0 && JimListIterDone(interp
, &iters
[i
])) {
11862 if (result
!= JIM_OK
) {
11863 Jim_SetResultString(interp
, "foreach varlist is empty", -1);
11864 goto empty_varlist
;
11868 resultObj
= Jim_NewListObj(interp
, NULL
, 0);
11871 resultObj
= interp
->emptyObj
;
11873 Jim_IncrRefCount(resultObj
);
11876 /* Have we expired all lists? */
11877 for (i
= 0; i
< numargs
; i
+= 2) {
11878 if (!JimListIterDone(interp
, &iters
[i
+ 1])) {
11882 if (i
== numargs
) {
11887 /* For each list */
11888 for (i
= 0; i
< numargs
; i
+= 2) {
11892 JimListIterInit(&iters
[i
], argv
[i
+ 1]);
11893 while ((varName
= JimListIterNext(interp
, &iters
[i
])) != NULL
) {
11894 Jim_Obj
*valObj
= JimListIterNext(interp
, &iters
[i
+ 1]);
11896 /* Ran out, so store the empty string */
11897 valObj
= interp
->emptyObj
;
11899 /* Avoid shimmering */
11900 Jim_IncrRefCount(valObj
);
11901 result
= Jim_SetVariable(interp
, varName
, valObj
);
11902 Jim_DecrRefCount(interp
, valObj
);
11903 if (result
!= JIM_OK
) {
11908 switch (result
= Jim_EvalObj(interp
, script
)) {
11911 Jim_ListAppendElement(interp
, resultObj
, interp
->result
);
11924 Jim_SetResult(interp
, resultObj
);
11926 Jim_DecrRefCount(interp
, resultObj
);
11935 static int Jim_ForeachCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11937 return JimForeachMapHelper(interp
, argc
, argv
, 0);
11941 static int Jim_LmapCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11943 return JimForeachMapHelper(interp
, argc
, argv
, 1);
11947 static int Jim_LassignCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11949 int result
= JIM_ERR
;
11952 Jim_Obj
*resultObj
;
11955 Jim_WrongNumArgs(interp
, 1, argv
, "varList list ?varName ...?");
11959 JimListIterInit(&iter
, argv
[1]);
11961 for (i
= 2; i
< argc
; i
++) {
11962 Jim_Obj
*valObj
= JimListIterNext(interp
, &iter
);
11963 result
= Jim_SetVariable(interp
, argv
[i
], valObj
? valObj
: interp
->emptyObj
);
11964 if (result
!= JIM_OK
) {
11969 resultObj
= Jim_NewListObj(interp
, NULL
, 0);
11970 while (!JimListIterDone(interp
, &iter
)) {
11971 Jim_ListAppendElement(interp
, resultObj
, JimListIterNext(interp
, &iter
));
11974 Jim_SetResult(interp
, resultObj
);
11980 static int Jim_IfCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
11982 int boolean
, retval
, current
= 1, falsebody
= 0;
11986 /* Far not enough arguments given! */
11987 if (current
>= argc
)
11989 if ((retval
= Jim_GetBoolFromExpr(interp
, argv
[current
++], &boolean
))
11992 /* There lacks something, isn't it? */
11993 if (current
>= argc
)
11995 if (Jim_CompareStringImmediate(interp
, argv
[current
], "then"))
11997 /* Tsk tsk, no then-clause? */
11998 if (current
>= argc
)
12001 return Jim_EvalObj(interp
, argv
[current
]);
12002 /* Ok: no else-clause follows */
12003 if (++current
>= argc
) {
12004 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
12007 falsebody
= current
++;
12008 if (Jim_CompareStringImmediate(interp
, argv
[falsebody
], "else")) {
12009 /* IIICKS - else-clause isn't last cmd? */
12010 if (current
!= argc
- 1)
12012 return Jim_EvalObj(interp
, argv
[current
]);
12014 else if (Jim_CompareStringImmediate(interp
, argv
[falsebody
], "elseif"))
12015 /* Ok: elseif follows meaning all the stuff
12016 * again (how boring...) */
12018 /* OOPS - else-clause is not last cmd? */
12019 else if (falsebody
!= argc
- 1)
12021 return Jim_EvalObj(interp
, argv
[falsebody
]);
12026 Jim_WrongNumArgs(interp
, 1, argv
, "condition ?then? trueBody ?elseif ...? ?else? falseBody");
12031 /* Returns 1 if match, 0 if no match or -<error> on error (e.g. -JIM_ERR, -JIM_BREAK)*/
12032 int Jim_CommandMatchObj(Jim_Interp
*interp
, Jim_Obj
*commandObj
, Jim_Obj
*patternObj
,
12033 Jim_Obj
*stringObj
, int nocase
)
12040 parms
[argc
++] = commandObj
;
12042 parms
[argc
++] = Jim_NewStringObj(interp
, "-nocase", -1);
12044 parms
[argc
++] = patternObj
;
12045 parms
[argc
++] = stringObj
;
12047 rc
= Jim_EvalObjVector(interp
, argc
, parms
);
12049 if (rc
!= JIM_OK
|| Jim_GetLong(interp
, Jim_GetResult(interp
), &eq
) != JIM_OK
) {
12057 static int Jim_SwitchCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12059 enum { SWITCH_EXACT
, SWITCH_GLOB
, SWITCH_RE
, SWITCH_CMD
};
12060 int matchOpt
= SWITCH_EXACT
, opt
= 1, patCount
, i
;
12061 Jim_Obj
*command
= NULL
, *scriptObj
= NULL
, *strObj
;
12062 Jim_Obj
**caseList
;
12066 Jim_WrongNumArgs(interp
, 1, argv
, "?options? string "
12067 "pattern body ... ?default body? or " "{pattern body ?pattern body ...?}");
12070 for (opt
= 1; opt
< argc
; ++opt
) {
12071 const char *option
= Jim_String(argv
[opt
]);
12073 if (*option
!= '-')
12075 else if (strncmp(option
, "--", 2) == 0) {
12079 else if (strncmp(option
, "-exact", 2) == 0)
12080 matchOpt
= SWITCH_EXACT
;
12081 else if (strncmp(option
, "-glob", 2) == 0)
12082 matchOpt
= SWITCH_GLOB
;
12083 else if (strncmp(option
, "-regexp", 2) == 0)
12084 matchOpt
= SWITCH_RE
;
12085 else if (strncmp(option
, "-command", 2) == 0) {
12086 matchOpt
= SWITCH_CMD
;
12087 if ((argc
- opt
) < 2)
12089 command
= argv
[++opt
];
12092 Jim_SetResultFormatted(interp
,
12093 "bad option \"%#s\": must be -exact, -glob, -regexp, -command procname or --",
12097 if ((argc
- opt
) < 2)
12100 strObj
= argv
[opt
++];
12101 patCount
= argc
- opt
;
12102 if (patCount
== 1) {
12103 JimListGetElements(interp
, argv
[opt
], &patCount
, &caseList
);
12106 caseList
= (Jim_Obj
**)&argv
[opt
];
12107 if (patCount
== 0 || patCount
% 2 != 0)
12109 for (i
= 0; scriptObj
== NULL
&& i
< patCount
; i
+= 2) {
12110 Jim_Obj
*patObj
= caseList
[i
];
12112 if (!Jim_CompareStringImmediate(interp
, patObj
, "default")
12113 || i
< (patCount
- 2)) {
12114 switch (matchOpt
) {
12116 if (Jim_StringEqObj(strObj
, patObj
))
12117 scriptObj
= caseList
[i
+ 1];
12120 if (Jim_StringMatchObj(interp
, patObj
, strObj
, 0))
12121 scriptObj
= caseList
[i
+ 1];
12124 command
= Jim_NewStringObj(interp
, "regexp", -1);
12125 /* Fall thru intentionally */
12127 int rc
= Jim_CommandMatchObj(interp
, command
, patObj
, strObj
, 0);
12129 /* After the execution of a command we need to
12130 * make sure to reconvert the object into a list
12131 * again. Only for the single-list style [switch]. */
12132 if (argc
- opt
== 1) {
12133 JimListGetElements(interp
, argv
[opt
], &patCount
, &caseList
);
12135 /* command is here already decref'd */
12140 scriptObj
= caseList
[i
+ 1];
12146 scriptObj
= caseList
[i
+ 1];
12149 for (; i
< patCount
&& Jim_CompareStringImmediate(interp
, scriptObj
, "-"); i
+= 2)
12150 scriptObj
= caseList
[i
+ 1];
12151 if (scriptObj
&& Jim_CompareStringImmediate(interp
, scriptObj
, "-")) {
12152 Jim_SetResultFormatted(interp
, "no body specified for pattern \"%#s\"", caseList
[i
- 2]);
12155 Jim_SetEmptyResult(interp
);
12157 return Jim_EvalObj(interp
, scriptObj
);
12163 static int Jim_ListCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12165 Jim_Obj
*listObjPtr
;
12167 listObjPtr
= Jim_NewListObj(interp
, argv
+ 1, argc
- 1);
12168 Jim_SetResult(interp
, listObjPtr
);
12173 static int Jim_LindexCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12175 Jim_Obj
*objPtr
, *listObjPtr
;
12180 Jim_WrongNumArgs(interp
, 1, argv
, "list ?index ...?");
12184 Jim_IncrRefCount(objPtr
);
12185 for (i
= 2; i
< argc
; i
++) {
12186 listObjPtr
= objPtr
;
12187 if (Jim_GetIndex(interp
, argv
[i
], &idx
) != JIM_OK
) {
12188 Jim_DecrRefCount(interp
, listObjPtr
);
12191 if (Jim_ListIndex(interp
, listObjPtr
, idx
, &objPtr
, JIM_NONE
) != JIM_OK
) {
12192 /* Returns an empty object if the index
12193 * is out of range. */
12194 Jim_DecrRefCount(interp
, listObjPtr
);
12195 Jim_SetEmptyResult(interp
);
12198 Jim_IncrRefCount(objPtr
);
12199 Jim_DecrRefCount(interp
, listObjPtr
);
12201 Jim_SetResult(interp
, objPtr
);
12202 Jim_DecrRefCount(interp
, objPtr
);
12207 static int Jim_LlengthCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12210 Jim_WrongNumArgs(interp
, 1, argv
, "list");
12213 Jim_SetResultInt(interp
, Jim_ListLength(interp
, argv
[1]));
12218 static int Jim_LsearchCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12220 static const char * const options
[] = {
12221 "-bool", "-not", "-nocase", "-exact", "-glob", "-regexp", "-all", "-inline", "-command",
12225 { OPT_BOOL
, OPT_NOT
, OPT_NOCASE
, OPT_EXACT
, OPT_GLOB
, OPT_REGEXP
, OPT_ALL
, OPT_INLINE
,
12230 int opt_nocase
= 0;
12232 int opt_inline
= 0;
12233 int opt_match
= OPT_EXACT
;
12236 Jim_Obj
*listObjPtr
= NULL
;
12237 Jim_Obj
*commandObj
= NULL
;
12241 Jim_WrongNumArgs(interp
, 1, argv
,
12242 "?-exact|-glob|-regexp|-command 'command'? ?-bool|-inline? ?-not? ?-nocase? ?-all? list value");
12246 for (i
= 1; i
< argc
- 2; i
++) {
12249 if (Jim_GetEnum(interp
, argv
[i
], options
, &option
, NULL
, JIM_ERRMSG
) != JIM_OK
) {
12271 if (i
>= argc
- 2) {
12274 commandObj
= argv
[++i
];
12279 opt_match
= option
;
12287 listObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
12289 if (opt_match
== OPT_REGEXP
) {
12290 commandObj
= Jim_NewStringObj(interp
, "regexp", -1);
12293 Jim_IncrRefCount(commandObj
);
12296 listlen
= Jim_ListLength(interp
, argv
[0]);
12297 for (i
= 0; i
< listlen
; i
++) {
12299 Jim_Obj
*objPtr
= Jim_ListGetIndex(interp
, argv
[0], i
);
12301 switch (opt_match
) {
12303 eq
= Jim_StringCompareObj(interp
, argv
[1], objPtr
, opt_nocase
) == 0;
12307 eq
= Jim_StringMatchObj(interp
, argv
[1], objPtr
, opt_nocase
);
12312 eq
= Jim_CommandMatchObj(interp
, commandObj
, argv
[1], objPtr
, opt_nocase
);
12315 Jim_FreeNewObj(interp
, listObjPtr
);
12323 /* If we have a non-match with opt_bool, opt_not, !opt_all, can't exit early */
12324 if (!eq
&& opt_bool
&& opt_not
&& !opt_all
) {
12328 if ((!opt_bool
&& eq
== !opt_not
) || (opt_bool
&& (eq
|| opt_all
))) {
12329 /* Got a match (or non-match for opt_not), or (opt_bool && opt_all) */
12330 Jim_Obj
*resultObj
;
12333 resultObj
= Jim_NewIntObj(interp
, eq
^ opt_not
);
12335 else if (!opt_inline
) {
12336 resultObj
= Jim_NewIntObj(interp
, i
);
12339 resultObj
= objPtr
;
12343 Jim_ListAppendElement(interp
, listObjPtr
, resultObj
);
12346 Jim_SetResult(interp
, resultObj
);
12353 Jim_SetResult(interp
, listObjPtr
);
12358 Jim_SetResultBool(interp
, opt_not
);
12360 else if (!opt_inline
) {
12361 Jim_SetResultInt(interp
, -1);
12367 Jim_DecrRefCount(interp
, commandObj
);
12373 static int Jim_LappendCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12375 Jim_Obj
*listObjPtr
;
12380 Jim_WrongNumArgs(interp
, 1, argv
, "varName ?value value ...?");
12383 listObjPtr
= Jim_GetVariable(interp
, argv
[1], JIM_UNSHARED
);
12385 /* Create the list if it does not exist */
12386 listObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
12389 else if (Jim_IsShared(listObjPtr
)) {
12390 listObjPtr
= Jim_DuplicateObj(interp
, listObjPtr
);
12393 for (i
= 2; i
< argc
; i
++)
12394 Jim_ListAppendElement(interp
, listObjPtr
, argv
[i
]);
12395 if (Jim_SetVariable(interp
, argv
[1], listObjPtr
) != JIM_OK
) {
12397 Jim_FreeNewObj(interp
, listObjPtr
);
12400 Jim_SetResult(interp
, listObjPtr
);
12405 static int Jim_LinsertCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12411 Jim_WrongNumArgs(interp
, 1, argv
, "list index ?element ...?");
12415 if (Jim_IsShared(listPtr
))
12416 listPtr
= Jim_DuplicateObj(interp
, listPtr
);
12417 if (Jim_GetIndex(interp
, argv
[2], &idx
) != JIM_OK
)
12419 len
= Jim_ListLength(interp
, listPtr
);
12423 idx
= len
+ idx
+ 1;
12424 Jim_ListInsertElements(interp
, listPtr
, idx
, argc
- 3, &argv
[3]);
12425 Jim_SetResult(interp
, listPtr
);
12428 if (listPtr
!= argv
[1]) {
12429 Jim_FreeNewObj(interp
, listPtr
);
12435 static int Jim_LreplaceCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12437 int first
, last
, len
, rangeLen
;
12439 Jim_Obj
*newListObj
;
12442 Jim_WrongNumArgs(interp
, 1, argv
, "list first last ?element ...?");
12445 if (Jim_GetIndex(interp
, argv
[2], &first
) != JIM_OK
||
12446 Jim_GetIndex(interp
, argv
[3], &last
) != JIM_OK
) {
12451 len
= Jim_ListLength(interp
, listObj
);
12453 first
= JimRelToAbsIndex(len
, first
);
12454 last
= JimRelToAbsIndex(len
, last
);
12455 JimRelToAbsRange(len
, &first
, &last
, &rangeLen
);
12457 /* Now construct a new list which consists of:
12458 * <elements before first> <supplied elements> <elements after last>
12461 /* Trying to replace past the end of the list means end of list
12468 /* Add the first set of elements */
12469 newListObj
= Jim_NewListObj(interp
, listObj
->internalRep
.listValue
.ele
, first
);
12471 /* Add supplied elements */
12472 ListInsertElements(newListObj
, -1, argc
- 4, argv
+ 4);
12474 /* Add the remaining elements */
12475 ListInsertElements(newListObj
, -1, len
- first
- rangeLen
, listObj
->internalRep
.listValue
.ele
+ first
+ rangeLen
);
12477 Jim_SetResult(interp
, newListObj
);
12482 static int Jim_LsetCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12485 Jim_WrongNumArgs(interp
, 1, argv
, "listVar ?index...? newVal");
12488 else if (argc
== 3) {
12489 /* With no indexes, simply implements [set] */
12490 if (Jim_SetVariable(interp
, argv
[1], argv
[2]) != JIM_OK
)
12492 Jim_SetResult(interp
, argv
[2]);
12495 return Jim_ListSetIndex(interp
, argv
[1], argv
+ 2, argc
- 3, argv
[argc
- 1]);
12499 static int Jim_LsortCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const argv
[])
12501 static const char * const options
[] = {
12502 "-ascii", "-nocase", "-increasing", "-decreasing", "-command", "-integer", "-real", "-index", "-unique", NULL
12505 { OPT_ASCII
, OPT_NOCASE
, OPT_INCREASING
, OPT_DECREASING
, OPT_COMMAND
, OPT_INTEGER
, OPT_REAL
, OPT_INDEX
, OPT_UNIQUE
};
12511 struct lsort_info info
;
12514 Jim_WrongNumArgs(interp
, 1, argv
, "?options? list");
12518 info
.type
= JIM_LSORT_ASCII
;
12522 info
.command
= NULL
;
12523 info
.interp
= interp
;
12525 for (i
= 1; i
< (argc
- 1); i
++) {
12528 if (Jim_GetEnum(interp
, argv
[i
], options
, &option
, NULL
, JIM_ENUM_ABBREV
| JIM_ERRMSG
)
12533 info
.type
= JIM_LSORT_ASCII
;
12536 info
.type
= JIM_LSORT_NOCASE
;
12539 info
.type
= JIM_LSORT_INTEGER
;
12542 info
.type
= JIM_LSORT_REAL
;
12544 case OPT_INCREASING
:
12547 case OPT_DECREASING
:
12554 if (i
>= (argc
- 2)) {
12555 Jim_SetResultString(interp
, "\"-command\" option must be followed by comparison command", -1);
12558 info
.type
= JIM_LSORT_COMMAND
;
12559 info
.command
= argv
[i
+ 1];
12563 if (i
>= (argc
- 2)) {
12564 Jim_SetResultString(interp
, "\"-index\" option must be followed by list index", -1);
12567 if (Jim_GetIndex(interp
, argv
[i
+ 1], &info
.index
) != JIM_OK
) {
12575 resObj
= argv
[argc
- 1];
12576 if ((shared
= Jim_IsShared(resObj
)))
12577 resObj
= Jim_DuplicateObj(interp
, resObj
);
12578 retCode
= ListSortElements(interp
, resObj
, &info
);
12579 if (retCode
== JIM_OK
) {
12580 Jim_SetResult(interp
, resObj
);
12583 Jim_FreeNewObj(interp
, resObj
);
12589 static int Jim_AppendCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12591 Jim_Obj
*stringObjPtr
;
12595 Jim_WrongNumArgs(interp
, 1, argv
, "varName ?value ...?");
12599 stringObjPtr
= Jim_GetVariable(interp
, argv
[1], JIM_ERRMSG
);
12605 stringObjPtr
= Jim_GetVariable(interp
, argv
[1], JIM_UNSHARED
);
12606 if (!stringObjPtr
) {
12607 /* Create the string if it doesn't exist */
12608 stringObjPtr
= Jim_NewEmptyStringObj(interp
);
12611 else if (Jim_IsShared(stringObjPtr
)) {
12613 stringObjPtr
= Jim_DuplicateObj(interp
, stringObjPtr
);
12615 for (i
= 2; i
< argc
; i
++) {
12616 Jim_AppendObj(interp
, stringObjPtr
, argv
[i
]);
12618 if (Jim_SetVariable(interp
, argv
[1], stringObjPtr
) != JIM_OK
) {
12620 Jim_FreeNewObj(interp
, stringObjPtr
);
12625 Jim_SetResult(interp
, stringObjPtr
);
12629 #if defined(JIM_DEBUG_COMMAND) && !defined(JIM_BOOTSTRAP)
12631 * Returns a zero-refcount list describing the expression at 'node'
12633 static Jim_Obj
*JimGetExprAsList(Jim_Interp
*interp
, struct JimExprNode
*node
)
12635 Jim_Obj
*listObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
12637 Jim_ListAppendElement(interp
, listObjPtr
, Jim_NewStringObj(interp
, jim_tt_name(node
->type
), -1));
12638 if (TOKEN_IS_EXPR_OP(node
->type
)) {
12640 Jim_ListAppendElement(interp
, listObjPtr
, JimGetExprAsList(interp
, node
->left
));
12643 Jim_ListAppendElement(interp
, listObjPtr
, JimGetExprAsList(interp
, node
->right
));
12645 if (node
->ternary
) {
12646 Jim_ListAppendElement(interp
, listObjPtr
, JimGetExprAsList(interp
, node
->ternary
));
12650 Jim_ListAppendElement(interp
, listObjPtr
, node
->objPtr
);
12654 #endif /* JIM_DEBUG_COMMAND && !JIM_BOOTSTRAP */
12657 #if defined(JIM_DEBUG_COMMAND) && !defined(JIM_BOOTSTRAP)
12658 static int Jim_DebugCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12660 static const char * const options
[] = {
12661 "refcount", "objcount", "objects", "invstr", "scriptlen", "exprlen",
12667 OPT_REFCOUNT
, OPT_OBJCOUNT
, OPT_OBJECTS
, OPT_INVSTR
, OPT_SCRIPTLEN
,
12668 OPT_EXPRLEN
, OPT_EXPRBC
, OPT_SHOW
,
12673 Jim_WrongNumArgs(interp
, 1, argv
, "subcommand ?...?");
12676 if (Jim_GetEnum(interp
, argv
[1], options
, &option
, "subcommand", JIM_ERRMSG
) != JIM_OK
)
12677 return Jim_CheckShowCommands(interp
, argv
[1], options
);
12678 if (option
== OPT_REFCOUNT
) {
12680 Jim_WrongNumArgs(interp
, 2, argv
, "object");
12683 Jim_SetResultInt(interp
, argv
[2]->refCount
);
12686 else if (option
== OPT_OBJCOUNT
) {
12687 int freeobj
= 0, liveobj
= 0;
12692 Jim_WrongNumArgs(interp
, 2, argv
, "");
12695 /* Count the number of free objects. */
12696 objPtr
= interp
->freeList
;
12699 objPtr
= objPtr
->nextObjPtr
;
12701 /* Count the number of live objects. */
12702 objPtr
= interp
->liveList
;
12705 objPtr
= objPtr
->nextObjPtr
;
12707 /* Set the result string and return. */
12708 sprintf(buf
, "free %d used %d", freeobj
, liveobj
);
12709 Jim_SetResultString(interp
, buf
, -1);
12712 else if (option
== OPT_OBJECTS
) {
12713 Jim_Obj
*objPtr
, *listObjPtr
, *subListObjPtr
;
12716 Jim_WrongNumArgs(interp
, 2, argv
, "");
12720 /* Count the number of live objects. */
12721 objPtr
= interp
->liveList
;
12722 listObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
12725 const char *type
= objPtr
->typePtr
? objPtr
->typePtr
->name
: "";
12727 subListObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
12728 sprintf(buf
, "%p", objPtr
);
12729 Jim_ListAppendElement(interp
, subListObjPtr
, Jim_NewStringObj(interp
, buf
, -1));
12730 Jim_ListAppendElement(interp
, subListObjPtr
, Jim_NewStringObj(interp
, type
, -1));
12731 Jim_ListAppendElement(interp
, subListObjPtr
, Jim_NewIntObj(interp
, objPtr
->refCount
));
12732 Jim_ListAppendElement(interp
, subListObjPtr
, objPtr
);
12733 Jim_ListAppendElement(interp
, listObjPtr
, subListObjPtr
);
12734 objPtr
= objPtr
->nextObjPtr
;
12736 Jim_SetResult(interp
, listObjPtr
);
12739 else if (option
== OPT_INVSTR
) {
12743 Jim_WrongNumArgs(interp
, 2, argv
, "object");
12747 if (objPtr
->typePtr
!= NULL
)
12748 Jim_InvalidateStringRep(objPtr
);
12749 Jim_SetEmptyResult(interp
);
12752 else if (option
== OPT_SHOW
) {
12757 Jim_WrongNumArgs(interp
, 2, argv
, "object");
12760 s
= Jim_GetString(argv
[2], &len
);
12762 charlen
= utf8_strlen(s
, len
);
12767 snprintf(buf
, sizeof(buf
), "refcount: %d, type: %s\n"
12769 argv
[2]->refCount
, JimObjTypeName(argv
[2]), charlen
);
12770 Jim_SetResultFormatted(interp
, "%s <<%s>>\n", buf
, s
);
12771 snprintf(buf
, sizeof(buf
), "bytes (%d):", len
);
12772 Jim_AppendString(interp
, Jim_GetResult(interp
), buf
, -1);
12774 snprintf(buf
, sizeof(buf
), " %02x", (unsigned char)*s
++);
12775 Jim_AppendString(interp
, Jim_GetResult(interp
), buf
, -1);
12779 else if (option
== OPT_SCRIPTLEN
) {
12783 Jim_WrongNumArgs(interp
, 2, argv
, "script");
12786 script
= JimGetScript(interp
, argv
[2]);
12787 if (script
== NULL
)
12789 Jim_SetResultInt(interp
, script
->len
);
12792 else if (option
== OPT_EXPRLEN
) {
12793 struct ExprTree
*expr
;
12796 Jim_WrongNumArgs(interp
, 2, argv
, "expression");
12799 expr
= JimGetExpression(interp
, argv
[2]);
12802 Jim_SetResultInt(interp
, expr
->len
);
12805 else if (option
== OPT_EXPRBC
) {
12806 struct ExprTree
*expr
;
12809 Jim_WrongNumArgs(interp
, 2, argv
, "expression");
12812 expr
= JimGetExpression(interp
, argv
[2]);
12815 Jim_SetResult(interp
, JimGetExprAsList(interp
, expr
->expr
));
12819 Jim_SetResultString(interp
,
12820 "bad option. Valid options are refcount, " "objcount, objects, invstr", -1);
12825 #endif /* JIM_DEBUG_COMMAND && !JIM_BOOTSTRAP */
12828 static int Jim_EvalCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12833 Jim_WrongNumArgs(interp
, 1, argv
, "arg ?arg ...?");
12838 rc
= Jim_EvalObj(interp
, argv
[1]);
12841 rc
= Jim_EvalObj(interp
, Jim_ConcatObj(interp
, argc
- 1, argv
+ 1));
12844 if (rc
== JIM_ERR
) {
12845 /* eval is "interesting", so add a stack frame here */
12846 interp
->addStackTrace
++;
12852 static int Jim_UplevelCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12856 Jim_CallFrame
*savedCallFrame
, *targetCallFrame
;
12859 /* Save the old callframe pointer */
12860 savedCallFrame
= interp
->framePtr
;
12862 /* Lookup the target frame pointer */
12863 str
= Jim_String(argv
[1]);
12864 if ((str
[0] >= '0' && str
[0] <= '9') || str
[0] == '#') {
12865 targetCallFrame
= Jim_GetCallFrameByLevel(interp
, argv
[1]);
12870 targetCallFrame
= Jim_GetCallFrameByLevel(interp
, NULL
);
12872 if (targetCallFrame
== NULL
) {
12876 Jim_WrongNumArgs(interp
, 1, argv
- 1, "?level? command ?arg ...?");
12879 /* Eval the code in the target callframe. */
12880 interp
->framePtr
= targetCallFrame
;
12882 retcode
= Jim_EvalObj(interp
, argv
[1]);
12885 retcode
= Jim_EvalObj(interp
, Jim_ConcatObj(interp
, argc
- 1, argv
+ 1));
12887 interp
->framePtr
= savedCallFrame
;
12891 Jim_WrongNumArgs(interp
, 1, argv
, "?level? command ?arg ...?");
12897 static int Jim_ExprCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12902 retcode
= Jim_EvalExpression(interp
, argv
[1]);
12904 else if (argc
> 2) {
12907 objPtr
= Jim_ConcatObj(interp
, argc
- 1, argv
+ 1);
12908 Jim_IncrRefCount(objPtr
);
12909 retcode
= Jim_EvalExpression(interp
, objPtr
);
12910 Jim_DecrRefCount(interp
, objPtr
);
12913 Jim_WrongNumArgs(interp
, 1, argv
, "expression ?...?");
12916 if (retcode
!= JIM_OK
)
12922 static int Jim_BreakCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12925 Jim_WrongNumArgs(interp
, 1, argv
, "");
12932 static int Jim_ContinueCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12935 Jim_WrongNumArgs(interp
, 1, argv
, "");
12938 return JIM_CONTINUE
;
12942 static int Jim_ReturnCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12945 Jim_Obj
*stackTraceObj
= NULL
;
12946 Jim_Obj
*errorCodeObj
= NULL
;
12947 int returnCode
= JIM_OK
;
12950 for (i
= 1; i
< argc
- 1; i
+= 2) {
12951 if (Jim_CompareStringImmediate(interp
, argv
[i
], "-code")) {
12952 if (Jim_GetReturnCode(interp
, argv
[i
+ 1], &returnCode
) == JIM_ERR
) {
12956 else if (Jim_CompareStringImmediate(interp
, argv
[i
], "-errorinfo")) {
12957 stackTraceObj
= argv
[i
+ 1];
12959 else if (Jim_CompareStringImmediate(interp
, argv
[i
], "-errorcode")) {
12960 errorCodeObj
= argv
[i
+ 1];
12962 else if (Jim_CompareStringImmediate(interp
, argv
[i
], "-level")) {
12963 if (Jim_GetLong(interp
, argv
[i
+ 1], &level
) != JIM_OK
|| level
< 0) {
12964 Jim_SetResultFormatted(interp
, "bad level \"%#s\"", argv
[i
+ 1]);
12973 if (i
!= argc
- 1 && i
!= argc
) {
12974 Jim_WrongNumArgs(interp
, 1, argv
,
12975 "?-code code? ?-errorinfo stacktrace? ?-level level? ?result?");
12978 /* If a stack trace is supplied and code is error, set the stack trace */
12979 if (stackTraceObj
&& returnCode
== JIM_ERR
) {
12980 JimSetStackTrace(interp
, stackTraceObj
);
12982 /* If an error code list is supplied, set the global $errorCode */
12983 if (errorCodeObj
&& returnCode
== JIM_ERR
) {
12984 Jim_SetGlobalVariableStr(interp
, "errorCode", errorCodeObj
);
12986 interp
->returnCode
= returnCode
;
12987 interp
->returnLevel
= level
;
12989 if (i
== argc
- 1) {
12990 Jim_SetResult(interp
, argv
[i
]);
12992 return level
== 0 ? returnCode
: JIM_RETURN
;
12996 static int Jim_TailcallCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
12998 if (interp
->framePtr
->level
== 0) {
12999 Jim_SetResultString(interp
, "tailcall can only be called from a proc or lambda", -1);
13002 else if (argc
>= 2) {
13003 /* Need to resolve the tailcall command in the current context */
13004 Jim_CallFrame
*cf
= interp
->framePtr
->parent
;
13006 Jim_Cmd
*cmdPtr
= Jim_GetCommand(interp
, argv
[1], JIM_ERRMSG
);
13007 if (cmdPtr
== NULL
) {
13011 JimPanic((cf
->tailcallCmd
!= NULL
, "Already have a tailcallCmd"));
13013 /* And stash this pre-resolved command */
13014 JimIncrCmdRefCount(cmdPtr
);
13015 cf
->tailcallCmd
= cmdPtr
;
13017 /* And stash the command list */
13018 JimPanic((cf
->tailcallObj
!= NULL
, "Already have a tailcallobj"));
13020 cf
->tailcallObj
= Jim_NewListObj(interp
, argv
+ 1, argc
- 1);
13021 Jim_IncrRefCount(cf
->tailcallObj
);
13023 /* When the stack unwinds to the previous proc, the stashed command will be evaluated */
13029 static int JimAliasCmd(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13032 Jim_Obj
*prefixListObj
= Jim_CmdPrivData(interp
);
13034 /* prefixListObj is a list to which the args need to be appended */
13035 cmdList
= Jim_DuplicateObj(interp
, prefixListObj
);
13036 Jim_ListInsertElements(interp
, cmdList
, Jim_ListLength(interp
, cmdList
), argc
- 1, argv
+ 1);
13038 return JimEvalObjList(interp
, cmdList
);
13041 static void JimAliasCmdDelete(Jim_Interp
*interp
, void *privData
)
13043 Jim_Obj
*prefixListObj
= privData
;
13044 Jim_DecrRefCount(interp
, prefixListObj
);
13047 static int Jim_AliasCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13049 Jim_Obj
*prefixListObj
;
13050 const char *newname
;
13053 Jim_WrongNumArgs(interp
, 1, argv
, "newname command ?args ...?");
13057 prefixListObj
= Jim_NewListObj(interp
, argv
+ 2, argc
- 2);
13058 Jim_IncrRefCount(prefixListObj
);
13059 newname
= Jim_String(argv
[1]);
13060 if (newname
[0] == ':' && newname
[1] == ':') {
13061 while (*++newname
== ':') {
13065 Jim_SetResult(interp
, argv
[1]);
13067 return Jim_CreateCommand(interp
, newname
, JimAliasCmd
, prefixListObj
, JimAliasCmdDelete
);
13071 static int Jim_ProcCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13075 if (argc
!= 4 && argc
!= 5) {
13076 Jim_WrongNumArgs(interp
, 1, argv
, "name arglist ?statics? body");
13080 if (JimValidName(interp
, "procedure", argv
[1]) != JIM_OK
) {
13085 cmd
= JimCreateProcedureCmd(interp
, argv
[2], NULL
, argv
[3], NULL
);
13088 cmd
= JimCreateProcedureCmd(interp
, argv
[2], argv
[3], argv
[4], NULL
);
13092 /* Add the new command */
13093 Jim_Obj
*qualifiedCmdNameObj
;
13094 const char *cmdname
= JimQualifyName(interp
, Jim_String(argv
[1]), &qualifiedCmdNameObj
);
13096 JimCreateCommand(interp
, cmdname
, cmd
);
13098 /* Calculate and set the namespace for this proc */
13099 JimUpdateProcNamespace(interp
, cmd
, cmdname
);
13101 JimFreeQualifiedName(interp
, qualifiedCmdNameObj
);
13103 /* Unlike Tcl, set the name of the proc as the result */
13104 Jim_SetResult(interp
, argv
[1]);
13111 static int Jim_LocalCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13116 Jim_WrongNumArgs(interp
, 1, argv
, "cmd ?args ...?");
13120 /* Evaluate the arguments with 'local' in force */
13122 retcode
= Jim_EvalObjVector(interp
, argc
- 1, argv
+ 1);
13126 /* If OK, and the result is a proc, add it to the list of local procs */
13127 if (retcode
== 0) {
13128 Jim_Obj
*cmdNameObj
= Jim_GetResult(interp
);
13130 if (Jim_GetCommand(interp
, cmdNameObj
, JIM_ERRMSG
) == NULL
) {
13133 if (interp
->framePtr
->localCommands
== NULL
) {
13134 interp
->framePtr
->localCommands
= Jim_Alloc(sizeof(*interp
->framePtr
->localCommands
));
13135 Jim_InitStack(interp
->framePtr
->localCommands
);
13137 Jim_IncrRefCount(cmdNameObj
);
13138 Jim_StackPush(interp
->framePtr
->localCommands
, cmdNameObj
);
13145 static int Jim_UpcallCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13148 Jim_WrongNumArgs(interp
, 1, argv
, "cmd ?args ...?");
13154 Jim_Cmd
*cmdPtr
= Jim_GetCommand(interp
, argv
[1], JIM_ERRMSG
);
13155 if (cmdPtr
== NULL
|| !cmdPtr
->isproc
|| !cmdPtr
->prevCmd
) {
13156 Jim_SetResultFormatted(interp
, "no previous command: \"%#s\"", argv
[1]);
13159 /* OK. Mark this command as being in an upcall */
13160 cmdPtr
->u
.proc
.upcall
++;
13161 JimIncrCmdRefCount(cmdPtr
);
13163 /* Invoke the command as normal */
13164 retcode
= Jim_EvalObjVector(interp
, argc
- 1, argv
+ 1);
13166 /* No longer in an upcall */
13167 cmdPtr
->u
.proc
.upcall
--;
13168 JimDecrCmdRefCount(interp
, cmdPtr
);
13175 static int Jim_ApplyCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13178 Jim_WrongNumArgs(interp
, 1, argv
, "lambdaExpr ?arg ...?");
13184 Jim_Obj
*argListObjPtr
;
13185 Jim_Obj
*bodyObjPtr
;
13186 Jim_Obj
*nsObj
= NULL
;
13189 int len
= Jim_ListLength(interp
, argv
[1]);
13190 if (len
!= 2 && len
!= 3) {
13191 Jim_SetResultFormatted(interp
, "can't interpret \"%#s\" as a lambda expression", argv
[1]);
13196 #ifdef jim_ext_namespace
13197 /* Need to canonicalise the given namespaces, but it is always treated as global */
13199 nsObj
= Jim_ListGetIndex(interp
, argv
[1], 2);
13200 name
= Jim_String(nsObj
);
13201 if (name
[0] == ':' && name
[1] == ':') {
13202 while (*++name
== ':') {
13204 nsObj
= Jim_NewStringObj(interp
, name
, -1);
13207 Jim_SetResultString(interp
, "namespaces not enabled", -1);
13211 argListObjPtr
= Jim_ListGetIndex(interp
, argv
[1], 0);
13212 bodyObjPtr
= Jim_ListGetIndex(interp
, argv
[1], 1);
13214 cmd
= JimCreateProcedureCmd(interp
, argListObjPtr
, NULL
, bodyObjPtr
, nsObj
);
13217 /* Create a new argv array with a dummy argv[0], for error messages */
13218 nargv
= Jim_Alloc((argc
- 2 + 1) * sizeof(*nargv
));
13219 nargv
[0] = Jim_NewStringObj(interp
, "apply lambdaExpr", -1);
13220 Jim_IncrRefCount(nargv
[0]);
13221 memcpy(&nargv
[1], argv
+ 2, (argc
- 2) * sizeof(*nargv
));
13222 ret
= JimCallProcedure(interp
, cmd
, argc
- 2 + 1, nargv
);
13223 Jim_DecrRefCount(interp
, nargv
[0]);
13226 JimDecrCmdRefCount(interp
, cmd
);
13235 static int Jim_ConcatCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13237 Jim_SetResult(interp
, Jim_ConcatObj(interp
, argc
- 1, argv
+ 1));
13242 static int Jim_UpvarCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13245 Jim_CallFrame
*targetCallFrame
;
13247 /* Lookup the target frame pointer */
13248 if (argc
> 3 && (argc
% 2 == 0)) {
13249 targetCallFrame
= Jim_GetCallFrameByLevel(interp
, argv
[1]);
13254 targetCallFrame
= Jim_GetCallFrameByLevel(interp
, NULL
);
13256 if (targetCallFrame
== NULL
) {
13260 /* Check for arity */
13262 Jim_WrongNumArgs(interp
, 1, argv
, "?level? otherVar localVar ?otherVar localVar ...?");
13266 /* Now... for every other/local couple: */
13267 for (i
= 1; i
< argc
; i
+= 2) {
13268 if (Jim_SetVariableLink(interp
, argv
[i
+ 1], argv
[i
], targetCallFrame
) != JIM_OK
)
13275 static int Jim_GlobalCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13280 Jim_WrongNumArgs(interp
, 1, argv
, "varName ?varName ...?");
13283 /* Link every var to the toplevel having the same name */
13284 if (interp
->framePtr
->level
== 0)
13285 return JIM_OK
; /* global at toplevel... */
13286 for (i
= 1; i
< argc
; i
++) {
13287 /* global ::blah does nothing */
13288 const char *name
= Jim_String(argv
[i
]);
13289 if (name
[0] != ':' || name
[1] != ':') {
13290 if (Jim_SetVariableLink(interp
, argv
[i
], argv
[i
], interp
->topFramePtr
) != JIM_OK
)
13297 /* does the [string map] operation. On error NULL is returned,
13298 * otherwise a new string object with the result, having refcount = 0,
13300 static Jim_Obj
*JimStringMap(Jim_Interp
*interp
, Jim_Obj
*mapListObjPtr
,
13301 Jim_Obj
*objPtr
, int nocase
)
13304 const char *str
, *noMatchStart
= NULL
;
13306 Jim_Obj
*resultObjPtr
;
13308 numMaps
= Jim_ListLength(interp
, mapListObjPtr
);
13310 Jim_SetResultString(interp
, "list must contain an even number of elements", -1);
13314 str
= Jim_String(objPtr
);
13315 strLen
= Jim_Utf8Length(interp
, objPtr
);
13318 resultObjPtr
= Jim_NewStringObj(interp
, "", 0);
13320 for (i
= 0; i
< numMaps
; i
+= 2) {
13321 Jim_Obj
*eachObjPtr
;
13325 eachObjPtr
= Jim_ListGetIndex(interp
, mapListObjPtr
, i
);
13326 k
= Jim_String(eachObjPtr
);
13327 kl
= Jim_Utf8Length(interp
, eachObjPtr
);
13329 if (strLen
>= kl
&& kl
) {
13331 rc
= JimStringCompareUtf8(str
, kl
, k
, kl
, nocase
);
13333 if (noMatchStart
) {
13334 Jim_AppendString(interp
, resultObjPtr
, noMatchStart
, str
- noMatchStart
);
13335 noMatchStart
= NULL
;
13337 Jim_AppendObj(interp
, resultObjPtr
, Jim_ListGetIndex(interp
, mapListObjPtr
, i
+ 1));
13338 str
+= utf8_index(str
, kl
);
13344 if (i
== numMaps
) { /* no match */
13346 if (noMatchStart
== NULL
)
13347 noMatchStart
= str
;
13348 str
+= utf8_tounicode(str
, &c
);
13352 if (noMatchStart
) {
13353 Jim_AppendString(interp
, resultObjPtr
, noMatchStart
, str
- noMatchStart
);
13355 return resultObjPtr
;
13359 static int Jim_StringCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13364 static const char * const options
[] = {
13365 "bytelength", "length", "compare", "match", "equal", "is", "byterange", "range", "replace",
13366 "map", "repeat", "reverse", "index", "first", "last", "cat",
13367 "trim", "trimleft", "trimright", "tolower", "toupper", "totitle", NULL
13371 OPT_BYTELENGTH
, OPT_LENGTH
, OPT_COMPARE
, OPT_MATCH
, OPT_EQUAL
, OPT_IS
, OPT_BYTERANGE
, OPT_RANGE
, OPT_REPLACE
,
13372 OPT_MAP
, OPT_REPEAT
, OPT_REVERSE
, OPT_INDEX
, OPT_FIRST
, OPT_LAST
, OPT_CAT
,
13373 OPT_TRIM
, OPT_TRIMLEFT
, OPT_TRIMRIGHT
, OPT_TOLOWER
, OPT_TOUPPER
, OPT_TOTITLE
13375 static const char * const nocase_options
[] = {
13378 static const char * const nocase_length_options
[] = {
13379 "-nocase", "-length", NULL
13383 Jim_WrongNumArgs(interp
, 1, argv
, "option ?arguments ...?");
13386 if (Jim_GetEnum(interp
, argv
[1], options
, &option
, NULL
,
13387 JIM_ERRMSG
| JIM_ENUM_ABBREV
) != JIM_OK
)
13388 return Jim_CheckShowCommands(interp
, argv
[1], options
);
13392 case OPT_BYTELENGTH
:
13394 Jim_WrongNumArgs(interp
, 2, argv
, "string");
13397 if (option
== OPT_LENGTH
) {
13398 len
= Jim_Utf8Length(interp
, argv
[2]);
13401 len
= Jim_Length(argv
[2]);
13403 Jim_SetResultInt(interp
, len
);
13409 /* optimise the one-arg case */
13415 objPtr
= Jim_NewStringObj(interp
, "", 0);
13417 for (i
= 2; i
< argc
; i
++) {
13418 Jim_AppendObj(interp
, objPtr
, argv
[i
]);
13421 Jim_SetResult(interp
, objPtr
);
13428 /* n is the number of remaining option args */
13429 long opt_length
= -1;
13434 if (Jim_GetEnum(interp
, argv
[i
++], nocase_length_options
, &subopt
, NULL
,
13435 JIM_ENUM_ABBREV
) != JIM_OK
) {
13437 Jim_WrongNumArgs(interp
, 2, argv
, "?-nocase? ?-length int? string1 string2");
13448 goto badcompareargs
;
13450 if (Jim_GetLong(interp
, argv
[i
++], &opt_length
) != JIM_OK
) {
13457 goto badcompareargs
;
13460 if (opt_length
< 0 && option
!= OPT_COMPARE
&& opt_case
) {
13461 /* Fast version - [string equal], case sensitive, no length */
13462 Jim_SetResultBool(interp
, Jim_StringEqObj(argv
[0], argv
[1]));
13465 const char *s1
= Jim_String(argv
[0]);
13466 int l1
= Jim_Utf8Length(interp
, argv
[0]);
13467 const char *s2
= Jim_String(argv
[1]);
13468 int l2
= Jim_Utf8Length(interp
, argv
[1]);
13469 if (opt_length
>= 0) {
13470 if (l1
> opt_length
) {
13473 if (l2
> opt_length
) {
13477 n
= JimStringCompareUtf8(s1
, l1
, s2
, l2
, !opt_case
);
13478 Jim_SetResultInt(interp
, option
== OPT_COMPARE
? n
: n
== 0);
13486 Jim_GetEnum(interp
, argv
[2], nocase_options
, &opt_case
, NULL
,
13487 JIM_ENUM_ABBREV
) != JIM_OK
)) {
13488 Jim_WrongNumArgs(interp
, 2, argv
, "?-nocase? pattern string");
13491 if (opt_case
== 0) {
13494 Jim_SetResultBool(interp
, Jim_StringMatchObj(interp
, argv
[2], argv
[3], !opt_case
));
13502 Jim_GetEnum(interp
, argv
[2], nocase_options
, &opt_case
, NULL
,
13503 JIM_ENUM_ABBREV
) != JIM_OK
)) {
13504 Jim_WrongNumArgs(interp
, 2, argv
, "?-nocase? mapList string");
13508 if (opt_case
== 0) {
13511 objPtr
= JimStringMap(interp
, argv
[2], argv
[3], !opt_case
);
13512 if (objPtr
== NULL
) {
13515 Jim_SetResult(interp
, objPtr
);
13520 case OPT_BYTERANGE
:{
13524 Jim_WrongNumArgs(interp
, 2, argv
, "string first last");
13527 if (option
== OPT_RANGE
) {
13528 objPtr
= Jim_StringRangeObj(interp
, argv
[2], argv
[3], argv
[4]);
13532 objPtr
= Jim_StringByteRangeObj(interp
, argv
[2], argv
[3], argv
[4]);
13535 if (objPtr
== NULL
) {
13538 Jim_SetResult(interp
, objPtr
);
13545 if (argc
!= 5 && argc
!= 6) {
13546 Jim_WrongNumArgs(interp
, 2, argv
, "string first last ?string?");
13549 objPtr
= JimStringReplaceObj(interp
, argv
[2], argv
[3], argv
[4], argc
== 6 ? argv
[5] : NULL
);
13550 if (objPtr
== NULL
) {
13553 Jim_SetResult(interp
, objPtr
);
13563 Jim_WrongNumArgs(interp
, 2, argv
, "string count");
13566 if (Jim_GetWide(interp
, argv
[3], &count
) != JIM_OK
) {
13569 objPtr
= Jim_NewStringObj(interp
, "", 0);
13572 Jim_AppendObj(interp
, objPtr
, argv
[2]);
13575 Jim_SetResult(interp
, objPtr
);
13585 Jim_WrongNumArgs(interp
, 2, argv
, "string");
13589 str
= Jim_GetString(argv
[2], &len
);
13590 buf
= Jim_Alloc(len
+ 1);
13593 for (i
= 0; i
< len
; ) {
13595 int l
= utf8_tounicode(str
, &c
);
13596 memcpy(p
- l
, str
, l
);
13601 Jim_SetResult(interp
, Jim_NewStringObjNoAlloc(interp
, buf
, len
));
13610 Jim_WrongNumArgs(interp
, 2, argv
, "string index");
13613 if (Jim_GetIndex(interp
, argv
[3], &idx
) != JIM_OK
) {
13616 str
= Jim_String(argv
[2]);
13617 len
= Jim_Utf8Length(interp
, argv
[2]);
13618 if (idx
!= INT_MIN
&& idx
!= INT_MAX
) {
13619 idx
= JimRelToAbsIndex(len
, idx
);
13621 if (idx
< 0 || idx
>= len
|| str
== NULL
) {
13622 Jim_SetResultString(interp
, "", 0);
13624 else if (len
== Jim_Length(argv
[2])) {
13625 /* ASCII optimisation */
13626 Jim_SetResultString(interp
, str
+ idx
, 1);
13630 int i
= utf8_index(str
, idx
);
13631 Jim_SetResultString(interp
, str
+ i
, utf8_tounicode(str
+ i
, &c
));
13638 int idx
= 0, l1
, l2
;
13639 const char *s1
, *s2
;
13641 if (argc
!= 4 && argc
!= 5) {
13642 Jim_WrongNumArgs(interp
, 2, argv
, "subString string ?index?");
13645 s1
= Jim_String(argv
[2]);
13646 s2
= Jim_String(argv
[3]);
13647 l1
= Jim_Utf8Length(interp
, argv
[2]);
13648 l2
= Jim_Utf8Length(interp
, argv
[3]);
13650 if (Jim_GetIndex(interp
, argv
[4], &idx
) != JIM_OK
) {
13653 idx
= JimRelToAbsIndex(l2
, idx
);
13655 else if (option
== OPT_LAST
) {
13658 if (option
== OPT_FIRST
) {
13659 Jim_SetResultInt(interp
, JimStringFirst(s1
, l1
, s2
, l2
, idx
));
13663 Jim_SetResultInt(interp
, JimStringLastUtf8(s1
, l1
, s2
, idx
));
13665 Jim_SetResultInt(interp
, JimStringLast(s1
, l1
, s2
, idx
));
13673 case OPT_TRIMRIGHT
:{
13674 Jim_Obj
*trimchars
;
13676 if (argc
!= 3 && argc
!= 4) {
13677 Jim_WrongNumArgs(interp
, 2, argv
, "string ?trimchars?");
13680 trimchars
= (argc
== 4 ? argv
[3] : NULL
);
13681 if (option
== OPT_TRIM
) {
13682 Jim_SetResult(interp
, JimStringTrim(interp
, argv
[2], trimchars
));
13684 else if (option
== OPT_TRIMLEFT
) {
13685 Jim_SetResult(interp
, JimStringTrimLeft(interp
, argv
[2], trimchars
));
13687 else if (option
== OPT_TRIMRIGHT
) {
13688 Jim_SetResult(interp
, JimStringTrimRight(interp
, argv
[2], trimchars
));
13697 Jim_WrongNumArgs(interp
, 2, argv
, "string");
13700 if (option
== OPT_TOLOWER
) {
13701 Jim_SetResult(interp
, JimStringToLower(interp
, argv
[2]));
13703 else if (option
== OPT_TOUPPER
) {
13704 Jim_SetResult(interp
, JimStringToUpper(interp
, argv
[2]));
13707 Jim_SetResult(interp
, JimStringToTitle(interp
, argv
[2]));
13712 if (argc
== 4 || (argc
== 5 && Jim_CompareStringImmediate(interp
, argv
[3], "-strict"))) {
13713 return JimStringIs(interp
, argv
[argc
- 1], argv
[2], argc
== 5);
13715 Jim_WrongNumArgs(interp
, 2, argv
, "class ?-strict? str");
13722 static int Jim_TimeCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13725 jim_wide start
, elapsed
;
13727 const char *fmt
= "%" JIM_WIDE_MODIFIER
" microseconds per iteration";
13730 Jim_WrongNumArgs(interp
, 1, argv
, "script ?count?");
13734 if (Jim_GetLong(interp
, argv
[2], &count
) != JIM_OK
)
13740 start
= JimClock();
13744 retval
= Jim_EvalObj(interp
, argv
[1]);
13745 if (retval
!= JIM_OK
) {
13749 elapsed
= JimClock() - start
;
13750 sprintf(buf
, fmt
, count
== 0 ? 0 : elapsed
/ count
);
13751 Jim_SetResultString(interp
, buf
, -1);
13756 static int Jim_ExitCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13761 Jim_WrongNumArgs(interp
, 1, argv
, "?exitCode?");
13765 if (Jim_GetLong(interp
, argv
[1], &exitCode
) != JIM_OK
)
13768 interp
->exitCode
= exitCode
;
13773 static int Jim_CatchCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13779 /* Which return codes are ignored (passed through)? By default, only exit, eval and signal */
13780 jim_wide ignore_mask
= (1 << JIM_EXIT
) | (1 << JIM_EVAL
) | (1 << JIM_SIGNAL
);
13781 static const int max_ignore_code
= sizeof(ignore_mask
) * 8;
13783 /* Reset the error code before catch.
13784 * Note that this is not strictly correct.
13786 Jim_SetGlobalVariableStr(interp
, "errorCode", Jim_NewStringObj(interp
, "NONE", -1));
13788 for (i
= 1; i
< argc
- 1; i
++) {
13789 const char *arg
= Jim_String(argv
[i
]);
13793 /* It's a pity we can't use Jim_GetEnum here :-( */
13794 if (strcmp(arg
, "--") == 0) {
13802 if (strncmp(arg
, "-no", 3) == 0) {
13811 if (Jim_StringToWide(arg
, &option
, 10) != JIM_OK
) {
13815 option
= Jim_FindByName(arg
, jimReturnCodes
, jimReturnCodesSize
);
13822 ignore_mask
|= ((jim_wide
)1 << option
);
13825 ignore_mask
&= (~((jim_wide
)1 << option
));
13830 if (argc
< 1 || argc
> 3) {
13832 Jim_WrongNumArgs(interp
, 1, argv
,
13833 "?-?no?code ... --? script ?resultVarName? ?optionVarName?");
13838 if ((ignore_mask
& (1 << JIM_SIGNAL
)) == 0) {
13842 interp
->signal_level
+= sig
;
13843 if (Jim_CheckSignal(interp
)) {
13844 /* If a signal is set, don't even try to execute the body */
13845 exitCode
= JIM_SIGNAL
;
13848 exitCode
= Jim_EvalObj(interp
, argv
[0]);
13849 /* Don't want any caught error included in a later stack trace */
13850 interp
->errorFlag
= 0;
13852 interp
->signal_level
-= sig
;
13854 /* Catch or pass through? Only the first 32/64 codes can be passed through */
13855 if (exitCode
>= 0 && exitCode
< max_ignore_code
&& (((unsigned jim_wide
)1 << exitCode
) & ignore_mask
)) {
13856 /* Not caught, pass it up */
13860 if (sig
&& exitCode
== JIM_SIGNAL
) {
13861 /* Catch the signal at this level */
13862 if (interp
->signal_set_result
) {
13863 interp
->signal_set_result(interp
, interp
->sigmask
);
13866 Jim_SetResultInt(interp
, interp
->sigmask
);
13868 interp
->sigmask
= 0;
13872 if (Jim_SetVariable(interp
, argv
[1], Jim_GetResult(interp
)) != JIM_OK
) {
13876 Jim_Obj
*optListObj
= Jim_NewListObj(interp
, NULL
, 0);
13878 Jim_ListAppendElement(interp
, optListObj
, Jim_NewStringObj(interp
, "-code", -1));
13879 Jim_ListAppendElement(interp
, optListObj
,
13880 Jim_NewIntObj(interp
, exitCode
== JIM_RETURN
? interp
->returnCode
: exitCode
));
13881 Jim_ListAppendElement(interp
, optListObj
, Jim_NewStringObj(interp
, "-level", -1));
13882 Jim_ListAppendElement(interp
, optListObj
, Jim_NewIntObj(interp
, interp
->returnLevel
));
13883 if (exitCode
== JIM_ERR
) {
13884 Jim_Obj
*errorCode
;
13885 Jim_ListAppendElement(interp
, optListObj
, Jim_NewStringObj(interp
, "-errorinfo",
13887 Jim_ListAppendElement(interp
, optListObj
, interp
->stackTrace
);
13889 errorCode
= Jim_GetGlobalVariableStr(interp
, "errorCode", JIM_NONE
);
13891 Jim_ListAppendElement(interp
, optListObj
, Jim_NewStringObj(interp
, "-errorcode", -1));
13892 Jim_ListAppendElement(interp
, optListObj
, errorCode
);
13895 if (Jim_SetVariable(interp
, argv
[2], optListObj
) != JIM_OK
) {
13900 Jim_SetResultInt(interp
, exitCode
);
13904 #if defined(JIM_REFERENCES) && !defined(JIM_BOOTSTRAP)
13907 static int Jim_RefCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13909 if (argc
!= 3 && argc
!= 4) {
13910 Jim_WrongNumArgs(interp
, 1, argv
, "string tag ?finalizer?");
13914 Jim_SetResult(interp
, Jim_NewReference(interp
, argv
[1], argv
[2], NULL
));
13917 Jim_SetResult(interp
, Jim_NewReference(interp
, argv
[1], argv
[2], argv
[3]));
13923 static int Jim_GetrefCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13925 Jim_Reference
*refPtr
;
13928 Jim_WrongNumArgs(interp
, 1, argv
, "reference");
13931 if ((refPtr
= Jim_GetReference(interp
, argv
[1])) == NULL
)
13933 Jim_SetResult(interp
, refPtr
->objPtr
);
13938 static int Jim_SetrefCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13940 Jim_Reference
*refPtr
;
13943 Jim_WrongNumArgs(interp
, 1, argv
, "reference newValue");
13946 if ((refPtr
= Jim_GetReference(interp
, argv
[1])) == NULL
)
13948 Jim_IncrRefCount(argv
[2]);
13949 Jim_DecrRefCount(interp
, refPtr
->objPtr
);
13950 refPtr
->objPtr
= argv
[2];
13951 Jim_SetResult(interp
, argv
[2]);
13956 static int Jim_CollectCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13959 Jim_WrongNumArgs(interp
, 1, argv
, "");
13962 Jim_SetResultInt(interp
, Jim_Collect(interp
));
13964 /* Free all the freed objects. */
13965 while (interp
->freeList
) {
13966 Jim_Obj
*nextObjPtr
= interp
->freeList
->nextObjPtr
;
13967 Jim_Free(interp
->freeList
);
13968 interp
->freeList
= nextObjPtr
;
13974 /* [finalize] reference ?newValue? */
13975 static int Jim_FinalizeCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
13977 if (argc
!= 2 && argc
!= 3) {
13978 Jim_WrongNumArgs(interp
, 1, argv
, "reference ?finalizerProc?");
13982 Jim_Obj
*cmdNamePtr
;
13984 if (Jim_GetFinalizer(interp
, argv
[1], &cmdNamePtr
) != JIM_OK
)
13986 if (cmdNamePtr
!= NULL
) /* otherwise the null string is returned. */
13987 Jim_SetResult(interp
, cmdNamePtr
);
13990 if (Jim_SetFinalizer(interp
, argv
[1], argv
[2]) != JIM_OK
)
13992 Jim_SetResult(interp
, argv
[2]);
13997 /* [info references] */
13998 static int JimInfoReferences(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14000 Jim_Obj
*listObjPtr
;
14001 Jim_HashTableIterator htiter
;
14004 listObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
14006 JimInitHashTableIterator(&interp
->references
, &htiter
);
14007 while ((he
= Jim_NextHashEntry(&htiter
)) != NULL
) {
14008 char buf
[JIM_REFERENCE_SPACE
+ 1];
14009 Jim_Reference
*refPtr
= Jim_GetHashEntryVal(he
);
14010 const unsigned long *refId
= he
->key
;
14012 JimFormatReference(buf
, refPtr
, *refId
);
14013 Jim_ListAppendElement(interp
, listObjPtr
, Jim_NewStringObj(interp
, buf
, -1));
14015 Jim_SetResult(interp
, listObjPtr
);
14018 #endif /* JIM_REFERENCES && !JIM_BOOTSTRAP */
14021 static int Jim_RenameCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14024 Jim_WrongNumArgs(interp
, 1, argv
, "oldName newName");
14028 if (JimValidName(interp
, "new procedure", argv
[2])) {
14032 return Jim_RenameCommand(interp
, Jim_String(argv
[1]), Jim_String(argv
[2]));
14035 #define JIM_DICTMATCH_KEYS 0x0001
14036 #define JIM_DICTMATCH_VALUES 0x002
14039 * match_type must be one of JIM_DICTMATCH_KEYS or JIM_DICTMATCH_VALUES
14040 * return_types should be either or both
14042 int Jim_DictMatchTypes(Jim_Interp
*interp
, Jim_Obj
*objPtr
, Jim_Obj
*patternObj
, int match_type
, int return_types
)
14045 Jim_Obj
*listObjPtr
;
14046 Jim_HashTableIterator htiter
;
14048 if (SetDictFromAny(interp
, objPtr
) != JIM_OK
) {
14052 listObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
14054 JimInitHashTableIterator(objPtr
->internalRep
.ptr
, &htiter
);
14055 while ((he
= Jim_NextHashEntry(&htiter
)) != NULL
) {
14057 Jim_Obj
*matchObj
= (match_type
== JIM_DICTMATCH_KEYS
) ? (Jim_Obj
*)he
->key
: Jim_GetHashEntryVal(he
);
14058 if (!JimGlobMatch(Jim_String(patternObj
), Jim_String(matchObj
), 0)) {
14063 if (return_types
& JIM_DICTMATCH_KEYS
) {
14064 Jim_ListAppendElement(interp
, listObjPtr
, (Jim_Obj
*)he
->key
);
14066 if (return_types
& JIM_DICTMATCH_VALUES
) {
14067 Jim_ListAppendElement(interp
, listObjPtr
, Jim_GetHashEntryVal(he
));
14071 Jim_SetResult(interp
, listObjPtr
);
14075 int Jim_DictSize(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
14077 if (SetDictFromAny(interp
, objPtr
) != JIM_OK
) {
14080 return ((Jim_HashTable
*)objPtr
->internalRep
.ptr
)->used
;
14084 * Must be called with at least one object.
14085 * Returns the new dictionary, or NULL on error.
14087 Jim_Obj
*Jim_DictMerge(Jim_Interp
*interp
, int objc
, Jim_Obj
*const *objv
)
14089 Jim_Obj
*objPtr
= Jim_NewDictObj(interp
, NULL
, 0);
14092 JimPanic((objc
== 0, "Jim_DictMerge called with objc=0"));
14094 /* Note that we don't optimise the trivial case of a single argument */
14096 for (i
= 0; i
< objc
; i
++) {
14098 Jim_HashTableIterator htiter
;
14101 if (SetDictFromAny(interp
, objv
[i
]) != JIM_OK
) {
14102 Jim_FreeNewObj(interp
, objPtr
);
14105 ht
= objv
[i
]->internalRep
.ptr
;
14106 JimInitHashTableIterator(ht
, &htiter
);
14107 while ((he
= Jim_NextHashEntry(&htiter
)) != NULL
) {
14108 Jim_ReplaceHashEntry(objPtr
->internalRep
.ptr
, Jim_GetHashEntryKey(he
), Jim_GetHashEntryVal(he
));
14114 int Jim_DictInfo(Jim_Interp
*interp
, Jim_Obj
*objPtr
)
14120 int nonzero_count
= 0;
14122 int bucket_counts
[11] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
14124 if (SetDictFromAny(interp
, objPtr
) != JIM_OK
) {
14128 ht
= (Jim_HashTable
*)objPtr
->internalRep
.ptr
;
14130 /* Note that this uses internal knowledge of the hash table */
14131 snprintf(buffer
, sizeof(buffer
), "%d entries in table, %d buckets\n", ht
->used
, ht
->size
);
14132 output
= Jim_NewStringObj(interp
, buffer
, -1);
14134 for (i
= 0; i
< ht
->size
; i
++) {
14135 Jim_HashEntry
*he
= ht
->table
[i
];
14142 bucket_counts
[10]++;
14145 bucket_counts
[entries
]++;
14152 for (i
= 0; i
< 10; i
++) {
14153 snprintf(buffer
, sizeof(buffer
), "number of buckets with %d entries: %d\n", i
, bucket_counts
[i
]);
14154 Jim_AppendString(interp
, output
, buffer
, -1);
14156 snprintf(buffer
, sizeof(buffer
), "number of buckets with 10 or more entries: %d\n", bucket_counts
[10]);
14157 Jim_AppendString(interp
, output
, buffer
, -1);
14158 snprintf(buffer
, sizeof(buffer
), "average search distance for entry: %.1f", nonzero_count
? (double)sum
/ nonzero_count
: 0.0);
14159 Jim_AppendString(interp
, output
, buffer
, -1);
14160 Jim_SetResult(interp
, output
);
14164 static int Jim_EvalEnsemble(Jim_Interp
*interp
, const char *basecmd
, const char *subcmd
, int argc
, Jim_Obj
*const *argv
)
14166 Jim_Obj
*prefixObj
= Jim_NewStringObj(interp
, basecmd
, -1);
14168 Jim_AppendString(interp
, prefixObj
, " ", 1);
14169 Jim_AppendString(interp
, prefixObj
, subcmd
, -1);
14171 return Jim_EvalObjPrefix(interp
, prefixObj
, argc
, argv
);
14175 * Implements the [dict with] command
14177 static int JimDictWith(Jim_Interp
*interp
, Jim_Obj
*dictVarName
, Jim_Obj
*const *keyv
, int keyc
, Jim_Obj
*scriptObj
)
14182 Jim_Obj
**dictValues
;
14186 /* Open up the appropriate level of the dictionary */
14187 dictObj
= Jim_GetVariable(interp
, dictVarName
, JIM_ERRMSG
);
14188 if (dictObj
== NULL
|| Jim_DictKeysVector(interp
, dictObj
, keyv
, keyc
, &objPtr
, JIM_ERRMSG
) != JIM_OK
) {
14191 /* Set the local variables */
14192 if (Jim_DictPairs(interp
, objPtr
, &dictValues
, &len
) == JIM_ERR
) {
14195 for (i
= 0; i
< len
; i
+= 2) {
14196 if (Jim_SetVariable(interp
, dictValues
[i
], dictValues
[i
+ 1]) == JIM_ERR
) {
14197 Jim_Free(dictValues
);
14202 /* As an optimisation, if the script is empty, no need to evaluate it or update the dict */
14203 if (Jim_Length(scriptObj
)) {
14204 ret
= Jim_EvalObj(interp
, scriptObj
);
14206 /* Now if the dictionary still exists, update it based on the local variables */
14207 if (ret
== JIM_OK
&& Jim_GetVariable(interp
, dictVarName
, 0) != NULL
) {
14208 /* We need a copy of keyv with one extra element at the end for Jim_SetDictKeysVector() */
14209 Jim_Obj
**newkeyv
= Jim_Alloc(sizeof(*newkeyv
) * (keyc
+ 1));
14210 for (i
= 0; i
< keyc
; i
++) {
14211 newkeyv
[i
] = keyv
[i
];
14214 for (i
= 0; i
< len
; i
+= 2) {
14215 /* This will be NULL if the variable no longer exists, thus deleting the variable */
14216 objPtr
= Jim_GetVariable(interp
, dictValues
[i
], 0);
14217 newkeyv
[keyc
] = dictValues
[i
];
14218 Jim_SetDictKeysVector(interp
, dictVarName
, newkeyv
, keyc
+ 1, objPtr
, 0);
14224 Jim_Free(dictValues
);
14230 static int Jim_DictCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14233 int types
= JIM_DICTMATCH_KEYS
;
14235 static const char * const options
[] = {
14236 "create", "get", "set", "unset", "exists", "keys", "size", "info",
14237 "merge", "with", "append", "lappend", "incr", "remove", "values", "for",
14238 "replace", "update", NULL
14242 OPT_CREATE
, OPT_GET
, OPT_SET
, OPT_UNSET
, OPT_EXISTS
, OPT_KEYS
, OPT_SIZE
, OPT_INFO
,
14243 OPT_MERGE
, OPT_WITH
, OPT_APPEND
, OPT_LAPPEND
, OPT_INCR
, OPT_REMOVE
, OPT_VALUES
, OPT_FOR
,
14244 OPT_REPLACE
, OPT_UPDATE
,
14248 Jim_WrongNumArgs(interp
, 1, argv
, "subcommand ?arguments ...?");
14252 if (Jim_GetEnum(interp
, argv
[1], options
, &option
, "subcommand", JIM_ERRMSG
) != JIM_OK
) {
14253 return Jim_CheckShowCommands(interp
, argv
[1], options
);
14259 Jim_WrongNumArgs(interp
, 2, argv
, "dictionary ?key ...?");
14262 if (Jim_DictKeysVector(interp
, argv
[2], argv
+ 3, argc
- 3, &objPtr
,
14263 JIM_ERRMSG
) != JIM_OK
) {
14266 Jim_SetResult(interp
, objPtr
);
14271 Jim_WrongNumArgs(interp
, 2, argv
, "varName key ?key ...? value");
14274 return Jim_SetDictKeysVector(interp
, argv
[2], argv
+ 3, argc
- 4, argv
[argc
- 1], JIM_ERRMSG
);
14278 Jim_WrongNumArgs(interp
, 2, argv
, "dictionary key ?key ...?");
14282 int rc
= Jim_DictKeysVector(interp
, argv
[2], argv
+ 3, argc
- 3, &objPtr
, JIM_ERRMSG
);
14286 Jim_SetResultBool(interp
, rc
== JIM_OK
);
14292 Jim_WrongNumArgs(interp
, 2, argv
, "varName key ?key ...?");
14295 if (Jim_SetDictKeysVector(interp
, argv
[2], argv
+ 3, argc
- 3, NULL
, 0) != JIM_OK
) {
14301 types
= JIM_DICTMATCH_VALUES
;
14304 if (argc
!= 3 && argc
!= 4) {
14305 Jim_WrongNumArgs(interp
, 2, argv
, "dictionary ?pattern?");
14308 return Jim_DictMatchTypes(interp
, argv
[2], argc
== 4 ? argv
[3] : NULL
, types
, types
);
14312 Jim_WrongNumArgs(interp
, 2, argv
, "dictionary");
14315 else if (Jim_DictSize(interp
, argv
[2]) < 0) {
14318 Jim_SetResultInt(interp
, Jim_DictSize(interp
, argv
[2]));
14325 objPtr
= Jim_DictMerge(interp
, argc
- 2, argv
+ 2);
14326 if (objPtr
== NULL
) {
14329 Jim_SetResult(interp
, objPtr
);
14333 if (argc
< 6 || argc
% 2) {
14334 /* Better error message */
14341 Jim_WrongNumArgs(interp
, 2, argv
, "?key value ...?");
14344 objPtr
= Jim_NewDictObj(interp
, argv
+ 2, argc
- 2);
14345 Jim_SetResult(interp
, objPtr
);
14350 Jim_WrongNumArgs(interp
, 2, argv
, "dictionary");
14353 return Jim_DictInfo(interp
, argv
[2]);
14357 Jim_WrongNumArgs(interp
, 2, argv
, "dictVar ?key ...? script");
14360 return JimDictWith(interp
, argv
[2], argv
+ 3, argc
- 4, argv
[argc
- 1]);
14362 /* Handle command as an ensemble */
14363 return Jim_EvalEnsemble(interp
, "dict", options
[option
], argc
- 2, argv
+ 2);
14367 static int Jim_SubstCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14369 static const char * const options
[] = {
14370 "-nobackslashes", "-nocommands", "-novariables", NULL
14373 { OPT_NOBACKSLASHES
, OPT_NOCOMMANDS
, OPT_NOVARIABLES
};
14375 int flags
= JIM_SUBST_FLAG
;
14379 Jim_WrongNumArgs(interp
, 1, argv
, "?options? string");
14382 for (i
= 1; i
< (argc
- 1); i
++) {
14385 if (Jim_GetEnum(interp
, argv
[i
], options
, &option
, NULL
,
14386 JIM_ERRMSG
| JIM_ENUM_ABBREV
) != JIM_OK
) {
14390 case OPT_NOBACKSLASHES
:
14391 flags
|= JIM_SUBST_NOESC
;
14393 case OPT_NOCOMMANDS
:
14394 flags
|= JIM_SUBST_NOCMD
;
14396 case OPT_NOVARIABLES
:
14397 flags
|= JIM_SUBST_NOVAR
;
14401 if (Jim_SubstObj(interp
, argv
[argc
- 1], &objPtr
, flags
) != JIM_OK
) {
14404 Jim_SetResult(interp
, objPtr
);
14409 static int Jim_InfoCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14415 static const char * const commands
[] = {
14416 "body", "statics", "commands", "procs", "channels", "exists", "globals", "level", "frame", "locals",
14417 "vars", "version", "patchlevel", "complete", "args", "hostname",
14418 "script", "source", "stacktrace", "nameofexecutable", "returncodes",
14419 "references", "alias", NULL
14422 { INFO_BODY
, INFO_STATICS
, INFO_COMMANDS
, INFO_PROCS
, INFO_CHANNELS
, INFO_EXISTS
, INFO_GLOBALS
, INFO_LEVEL
,
14423 INFO_FRAME
, INFO_LOCALS
, INFO_VARS
, INFO_VERSION
, INFO_PATCHLEVEL
, INFO_COMPLETE
, INFO_ARGS
,
14424 INFO_HOSTNAME
, INFO_SCRIPT
, INFO_SOURCE
, INFO_STACKTRACE
, INFO_NAMEOFEXECUTABLE
,
14425 INFO_RETURNCODES
, INFO_REFERENCES
, INFO_ALIAS
,
14428 #ifdef jim_ext_namespace
14431 if (argc
> 2 && Jim_CompareStringImmediate(interp
, argv
[1], "-nons")) {
14432 /* This is for internal use only */
14440 Jim_WrongNumArgs(interp
, 1, argv
, "subcommand ?args ...?");
14443 if (Jim_GetEnum(interp
, argv
[1], commands
, &cmd
, "subcommand", JIM_ERRMSG
| JIM_ENUM_ABBREV
) != JIM_OK
) {
14444 return Jim_CheckShowCommands(interp
, argv
[1], commands
);
14447 /* Test for the most common commands first, just in case it makes a difference */
14451 Jim_WrongNumArgs(interp
, 2, argv
, "varName");
14454 Jim_SetResultBool(interp
, Jim_GetVariable(interp
, argv
[2], 0) != NULL
);
14461 Jim_WrongNumArgs(interp
, 2, argv
, "command");
14464 if ((cmdPtr
= Jim_GetCommand(interp
, argv
[2], JIM_ERRMSG
)) == NULL
) {
14467 if (cmdPtr
->isproc
|| cmdPtr
->u
.native
.cmdProc
!= JimAliasCmd
) {
14468 Jim_SetResultFormatted(interp
, "command \"%#s\" is not an alias", argv
[2]);
14471 Jim_SetResult(interp
, (Jim_Obj
*)cmdPtr
->u
.native
.privData
);
14475 case INFO_CHANNELS
:
14476 mode
++; /* JIM_CMDLIST_CHANNELS */
14477 #ifndef jim_ext_aio
14478 Jim_SetResultString(interp
, "aio not enabled", -1);
14483 mode
++; /* JIM_CMDLIST_PROCS */
14485 case INFO_COMMANDS
:
14486 /* mode 0 => JIM_CMDLIST_COMMANDS */
14487 if (argc
!= 2 && argc
!= 3) {
14488 Jim_WrongNumArgs(interp
, 2, argv
, "?pattern?");
14491 #ifdef jim_ext_namespace
14493 if (Jim_Length(interp
->framePtr
->nsObj
) || (argc
== 3 && JimGlobMatch("::*", Jim_String(argv
[2]), 0))) {
14494 return Jim_EvalPrefix(interp
, "namespace info", argc
- 1, argv
+ 1);
14498 Jim_SetResult(interp
, JimCommandsList(interp
, (argc
== 3) ? argv
[2] : NULL
, mode
));
14502 mode
++; /* JIM_VARLIST_VARS */
14505 mode
++; /* JIM_VARLIST_LOCALS */
14508 /* mode 0 => JIM_VARLIST_GLOBALS */
14509 if (argc
!= 2 && argc
!= 3) {
14510 Jim_WrongNumArgs(interp
, 2, argv
, "?pattern?");
14513 #ifdef jim_ext_namespace
14515 if (Jim_Length(interp
->framePtr
->nsObj
) || (argc
== 3 && JimGlobMatch("::*", Jim_String(argv
[2]), 0))) {
14516 return Jim_EvalPrefix(interp
, "namespace info", argc
- 1, argv
+ 1);
14520 Jim_SetResult(interp
, JimVariablesList(interp
, argc
== 3 ? argv
[2] : NULL
, mode
));
14525 Jim_WrongNumArgs(interp
, 2, argv
, "");
14528 Jim_SetResult(interp
, JimGetScript(interp
, interp
->currentScriptObj
)->fileNameObj
);
14533 Jim_Obj
*resObjPtr
;
14534 Jim_Obj
*fileNameObj
;
14536 if (argc
!= 3 && argc
!= 5) {
14537 Jim_WrongNumArgs(interp
, 2, argv
, "source ?filename line?");
14541 if (Jim_GetWide(interp
, argv
[4], &line
) != JIM_OK
) {
14544 resObjPtr
= Jim_NewStringObj(interp
, Jim_String(argv
[2]), Jim_Length(argv
[2]));
14545 JimSetSourceInfo(interp
, resObjPtr
, argv
[3], line
);
14548 if (argv
[2]->typePtr
== &sourceObjType
) {
14549 fileNameObj
= argv
[2]->internalRep
.sourceValue
.fileNameObj
;
14550 line
= argv
[2]->internalRep
.sourceValue
.lineNumber
;
14552 else if (argv
[2]->typePtr
== &scriptObjType
) {
14553 ScriptObj
*script
= JimGetScript(interp
, argv
[2]);
14554 fileNameObj
= script
->fileNameObj
;
14555 line
= script
->firstline
;
14558 fileNameObj
= interp
->emptyObj
;
14561 resObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
14562 Jim_ListAppendElement(interp
, resObjPtr
, fileNameObj
);
14563 Jim_ListAppendElement(interp
, resObjPtr
, Jim_NewIntObj(interp
, line
));
14565 Jim_SetResult(interp
, resObjPtr
);
14569 case INFO_STACKTRACE
:
14570 Jim_SetResult(interp
, interp
->stackTrace
);
14577 Jim_SetResultInt(interp
, interp
->framePtr
->level
);
14581 if (JimInfoLevel(interp
, argv
[2], &objPtr
, cmd
== INFO_LEVEL
) != JIM_OK
) {
14584 Jim_SetResult(interp
, objPtr
);
14588 Jim_WrongNumArgs(interp
, 2, argv
, "?levelNum?");
14599 Jim_WrongNumArgs(interp
, 2, argv
, "procname");
14602 if ((cmdPtr
= Jim_GetCommand(interp
, argv
[2], JIM_ERRMSG
)) == NULL
) {
14605 if (!cmdPtr
->isproc
) {
14606 Jim_SetResultFormatted(interp
, "command \"%#s\" is not a procedure", argv
[2]);
14611 Jim_SetResult(interp
, cmdPtr
->u
.proc
.bodyObjPtr
);
14614 Jim_SetResult(interp
, cmdPtr
->u
.proc
.argListObjPtr
);
14617 if (cmdPtr
->u
.proc
.staticVars
) {
14618 Jim_SetResult(interp
, JimHashtablePatternMatch(interp
, cmdPtr
->u
.proc
.staticVars
,
14619 NULL
, JimVariablesMatch
, JIM_VARLIST_LOCALS
| JIM_VARLIST_VALUES
));
14627 case INFO_PATCHLEVEL
:{
14628 char buf
[(JIM_INTEGER_SPACE
* 2) + 1];
14630 sprintf(buf
, "%d.%d", JIM_VERSION
/ 100, JIM_VERSION
% 100);
14631 Jim_SetResultString(interp
, buf
, -1);
14635 case INFO_COMPLETE
:
14636 if (argc
!= 3 && argc
!= 4) {
14637 Jim_WrongNumArgs(interp
, 2, argv
, "script ?missing?");
14643 Jim_SetResultBool(interp
, Jim_ScriptIsComplete(interp
, argv
[2], &missing
));
14644 if (missing
!= ' ' && argc
== 4) {
14645 Jim_SetVariable(interp
, argv
[3], Jim_NewStringObj(interp
, &missing
, 1));
14650 case INFO_HOSTNAME
:
14651 /* Redirect to os.gethostname if it exists */
14652 return Jim_Eval(interp
, "os.gethostname");
14654 case INFO_NAMEOFEXECUTABLE
:
14655 /* Redirect to Tcl proc */
14656 return Jim_Eval(interp
, "{info nameofexecutable}");
14658 case INFO_RETURNCODES
:
14661 Jim_Obj
*listObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
14663 for (i
= 0; jimReturnCodes
[i
]; i
++) {
14664 Jim_ListAppendElement(interp
, listObjPtr
, Jim_NewIntObj(interp
, i
));
14665 Jim_ListAppendElement(interp
, listObjPtr
, Jim_NewStringObj(interp
,
14666 jimReturnCodes
[i
], -1));
14669 Jim_SetResult(interp
, listObjPtr
);
14671 else if (argc
== 3) {
14675 if (Jim_GetLong(interp
, argv
[2], &code
) != JIM_OK
) {
14678 name
= Jim_ReturnCode(code
);
14679 if (*name
== '?') {
14680 Jim_SetResultInt(interp
, code
);
14683 Jim_SetResultString(interp
, name
, -1);
14687 Jim_WrongNumArgs(interp
, 2, argv
, "?code?");
14691 case INFO_REFERENCES
:
14692 #ifdef JIM_REFERENCES
14693 return JimInfoReferences(interp
, argc
, argv
);
14695 Jim_SetResultString(interp
, "not supported", -1);
14703 static int Jim_ExistsCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14708 static const char * const options
[] = {
14709 "-command", "-proc", "-alias", "-var", NULL
14713 OPT_COMMAND
, OPT_PROC
, OPT_ALIAS
, OPT_VAR
14721 else if (argc
== 3) {
14722 if (Jim_GetEnum(interp
, argv
[1], options
, &option
, NULL
, JIM_ERRMSG
| JIM_ENUM_ABBREV
) != JIM_OK
) {
14728 Jim_WrongNumArgs(interp
, 1, argv
, "?option? name");
14732 if (option
== OPT_VAR
) {
14733 result
= Jim_GetVariable(interp
, objPtr
, 0) != NULL
;
14736 /* Now different kinds of commands */
14737 Jim_Cmd
*cmd
= Jim_GetCommand(interp
, objPtr
, JIM_NONE
);
14746 result
= cmd
->isproc
== 0 && cmd
->u
.native
.cmdProc
== JimAliasCmd
;
14750 result
= cmd
->isproc
;
14755 Jim_SetResultBool(interp
, result
);
14760 static int Jim_SplitCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14762 const char *str
, *splitChars
, *noMatchStart
;
14763 int splitLen
, strLen
;
14764 Jim_Obj
*resObjPtr
;
14768 if (argc
!= 2 && argc
!= 3) {
14769 Jim_WrongNumArgs(interp
, 1, argv
, "string ?splitChars?");
14773 str
= Jim_GetString(argv
[1], &len
);
14777 strLen
= Jim_Utf8Length(interp
, argv
[1]);
14781 splitChars
= " \n\t\r";
14785 splitChars
= Jim_String(argv
[2]);
14786 splitLen
= Jim_Utf8Length(interp
, argv
[2]);
14789 noMatchStart
= str
;
14790 resObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
14796 const char *sc
= splitChars
;
14797 int scLen
= splitLen
;
14798 int sl
= utf8_tounicode(str
, &c
);
14801 sc
+= utf8_tounicode(sc
, &pc
);
14803 objPtr
= Jim_NewStringObj(interp
, noMatchStart
, (str
- noMatchStart
));
14804 Jim_ListAppendElement(interp
, resObjPtr
, objPtr
);
14805 noMatchStart
= str
+ sl
;
14811 objPtr
= Jim_NewStringObj(interp
, noMatchStart
, (str
- noMatchStart
));
14812 Jim_ListAppendElement(interp
, resObjPtr
, objPtr
);
14815 /* This handles the special case of splitchars eq {}
14816 * Optimise by sharing common (ASCII) characters
14818 Jim_Obj
**commonObj
= NULL
;
14819 #define NUM_COMMON (128 - 9)
14821 int n
= utf8_tounicode(str
, &c
);
14822 #ifdef JIM_OPTIMIZATION
14823 if (c
>= 9 && c
< 128) {
14824 /* Common ASCII char. Note that 9 is the tab character */
14827 commonObj
= Jim_Alloc(sizeof(*commonObj
) * NUM_COMMON
);
14828 memset(commonObj
, 0, sizeof(*commonObj
) * NUM_COMMON
);
14830 if (!commonObj
[c
]) {
14831 commonObj
[c
] = Jim_NewStringObj(interp
, str
, 1);
14833 Jim_ListAppendElement(interp
, resObjPtr
, commonObj
[c
]);
14838 Jim_ListAppendElement(interp
, resObjPtr
, Jim_NewStringObjUtf8(interp
, str
, 1));
14841 Jim_Free(commonObj
);
14844 Jim_SetResult(interp
, resObjPtr
);
14849 static int Jim_JoinCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14851 const char *joinStr
;
14854 if (argc
!= 2 && argc
!= 3) {
14855 Jim_WrongNumArgs(interp
, 1, argv
, "list ?joinString?");
14864 joinStr
= Jim_GetString(argv
[2], &joinStrLen
);
14866 Jim_SetResult(interp
, Jim_ListJoin(interp
, argv
[1], joinStr
, joinStrLen
));
14871 static int Jim_FormatCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14876 Jim_WrongNumArgs(interp
, 1, argv
, "formatString ?arg arg ...?");
14879 objPtr
= Jim_FormatString(interp
, argv
[1], argc
- 2, argv
+ 2);
14880 if (objPtr
== NULL
)
14882 Jim_SetResult(interp
, objPtr
);
14887 static int Jim_ScanCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14889 Jim_Obj
*listPtr
, **outVec
;
14893 Jim_WrongNumArgs(interp
, 1, argv
, "string format ?varName varName ...?");
14896 if (argv
[2]->typePtr
!= &scanFmtStringObjType
)
14897 SetScanFmtFromAny(interp
, argv
[2]);
14898 if (FormatGetError(argv
[2]) != 0) {
14899 Jim_SetResultString(interp
, FormatGetError(argv
[2]), -1);
14903 int maxPos
= FormatGetMaxPos(argv
[2]);
14904 int count
= FormatGetCnvCount(argv
[2]);
14906 if (maxPos
> argc
- 3) {
14907 Jim_SetResultString(interp
, "\"%n$\" argument index out of range", -1);
14910 else if (count
> argc
- 3) {
14911 Jim_SetResultString(interp
, "different numbers of variable names and "
14912 "field specifiers", -1);
14915 else if (count
< argc
- 3) {
14916 Jim_SetResultString(interp
, "variable is not assigned by any "
14917 "conversion specifiers", -1);
14921 listPtr
= Jim_ScanString(interp
, argv
[1], argv
[2], JIM_ERRMSG
);
14928 if (listPtr
!= 0 && listPtr
!= (Jim_Obj
*)EOF
) {
14929 int len
= Jim_ListLength(interp
, listPtr
);
14932 JimListGetElements(interp
, listPtr
, &outc
, &outVec
);
14933 for (i
= 0; i
< outc
; ++i
) {
14934 if (Jim_Length(outVec
[i
]) > 0) {
14936 if (Jim_SetVariable(interp
, argv
[3 + i
], outVec
[i
]) != JIM_OK
) {
14942 Jim_FreeNewObj(interp
, listPtr
);
14947 if (rc
== JIM_OK
) {
14948 Jim_SetResultInt(interp
, count
);
14953 if (listPtr
== (Jim_Obj
*)EOF
) {
14954 Jim_SetResult(interp
, Jim_NewListObj(interp
, 0, 0));
14957 Jim_SetResult(interp
, listPtr
);
14963 static int Jim_ErrorCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14965 if (argc
!= 2 && argc
!= 3) {
14966 Jim_WrongNumArgs(interp
, 1, argv
, "message ?stacktrace?");
14969 Jim_SetResult(interp
, argv
[1]);
14971 JimSetStackTrace(interp
, argv
[2]);
14974 interp
->addStackTrace
++;
14979 static int Jim_LrangeCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14984 Jim_WrongNumArgs(interp
, 1, argv
, "list first last");
14987 if ((objPtr
= Jim_ListRange(interp
, argv
[1], argv
[2], argv
[3])) == NULL
)
14989 Jim_SetResult(interp
, objPtr
);
14994 static int Jim_LrepeatCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
14999 if (argc
< 2 || Jim_GetLong(interp
, argv
[1], &count
) != JIM_OK
|| count
< 0) {
15000 Jim_WrongNumArgs(interp
, 1, argv
, "count ?value ...?");
15004 if (count
== 0 || argc
== 2) {
15011 objPtr
= Jim_NewListObj(interp
, argv
, argc
);
15013 ListInsertElements(objPtr
, -1, argc
, argv
);
15016 Jim_SetResult(interp
, objPtr
);
15020 char **Jim_GetEnviron(void)
15022 #if defined(HAVE__NSGETENVIRON)
15023 return *_NSGetEnviron();
15025 #if !defined(NO_ENVIRON_EXTERN)
15026 extern char **environ
;
15033 void Jim_SetEnviron(char **env
)
15035 #if defined(HAVE__NSGETENVIRON)
15036 *_NSGetEnviron() = env
;
15038 #if !defined(NO_ENVIRON_EXTERN)
15039 extern char **environ
;
15047 static int Jim_EnvCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
15053 char **e
= Jim_GetEnviron();
15056 Jim_Obj
*listObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
15058 for (i
= 0; e
[i
]; i
++) {
15059 const char *equals
= strchr(e
[i
], '=');
15062 Jim_ListAppendElement(interp
, listObjPtr
, Jim_NewStringObj(interp
, e
[i
],
15064 Jim_ListAppendElement(interp
, listObjPtr
, Jim_NewStringObj(interp
, equals
+ 1, -1));
15068 Jim_SetResult(interp
, listObjPtr
);
15073 Jim_WrongNumArgs(interp
, 1, argv
, "varName ?default?");
15076 key
= Jim_String(argv
[1]);
15080 Jim_SetResultFormatted(interp
, "environment variable \"%#s\" does not exist", argv
[1]);
15083 val
= Jim_String(argv
[2]);
15085 Jim_SetResult(interp
, Jim_NewStringObj(interp
, val
, -1));
15090 static int Jim_SourceCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
15095 Jim_WrongNumArgs(interp
, 1, argv
, "fileName");
15098 retval
= Jim_EvalFile(interp
, Jim_String(argv
[1]));
15099 if (retval
== JIM_RETURN
)
15105 static int Jim_LreverseCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
15107 Jim_Obj
*revObjPtr
, **ele
;
15111 Jim_WrongNumArgs(interp
, 1, argv
, "list");
15114 JimListGetElements(interp
, argv
[1], &len
, &ele
);
15116 revObjPtr
= Jim_NewListObj(interp
, NULL
, 0);
15118 ListAppendElement(revObjPtr
, ele
[len
--]);
15119 Jim_SetResult(interp
, revObjPtr
);
15123 static int JimRangeLen(jim_wide start
, jim_wide end
, jim_wide step
)
15131 else if (step
> 0 && start
> end
)
15133 else if (step
< 0 && end
> start
)
15137 len
= -len
; /* abs(len) */
15139 step
= -step
; /* abs(step) */
15140 len
= 1 + ((len
- 1) / step
);
15141 /* We can truncate safely to INT_MAX, the range command
15142 * will always return an error for a such long range
15143 * because Tcl lists can't be so long. */
15146 return (int)((len
< 0) ? -1 : len
);
15150 static int Jim_RangeCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
15152 jim_wide start
= 0, end
, step
= 1;
15156 if (argc
< 2 || argc
> 4) {
15157 Jim_WrongNumArgs(interp
, 1, argv
, "?start? end ?step?");
15161 if (Jim_GetWide(interp
, argv
[1], &end
) != JIM_OK
)
15165 if (Jim_GetWide(interp
, argv
[1], &start
) != JIM_OK
||
15166 Jim_GetWide(interp
, argv
[2], &end
) != JIM_OK
)
15168 if (argc
== 4 && Jim_GetWide(interp
, argv
[3], &step
) != JIM_OK
)
15171 if ((len
= JimRangeLen(start
, end
, step
)) == -1) {
15172 Jim_SetResultString(interp
, "Invalid (infinite?) range specified", -1);
15175 objPtr
= Jim_NewListObj(interp
, NULL
, 0);
15176 for (i
= 0; i
< len
; i
++)
15177 ListAppendElement(objPtr
, Jim_NewIntObj(interp
, start
+ i
* step
));
15178 Jim_SetResult(interp
, objPtr
);
15183 static int Jim_RandCoreCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
15185 jim_wide min
= 0, max
= 0, len
, maxMul
;
15187 if (argc
< 1 || argc
> 3) {
15188 Jim_WrongNumArgs(interp
, 1, argv
, "?min? max");
15192 max
= JIM_WIDE_MAX
;
15193 } else if (argc
== 2) {
15194 if (Jim_GetWide(interp
, argv
[1], &max
) != JIM_OK
)
15196 } else if (argc
== 3) {
15197 if (Jim_GetWide(interp
, argv
[1], &min
) != JIM_OK
||
15198 Jim_GetWide(interp
, argv
[2], &max
) != JIM_OK
)
15203 Jim_SetResultString(interp
, "Invalid arguments (max < min)", -1);
15206 maxMul
= JIM_WIDE_MAX
- (len
? (JIM_WIDE_MAX
%len
) : 0);
15210 JimRandomBytes(interp
, &r
, sizeof(jim_wide
));
15211 if (r
< 0 || r
>= maxMul
) continue;
15212 r
= (len
== 0) ? 0 : r
%len
;
15213 Jim_SetResultInt(interp
, min
+r
);
15218 static const struct {
15220 Jim_CmdProc
*cmdProc
;
15221 } Jim_CoreCommandsTable
[] = {
15222 {"alias", Jim_AliasCoreCommand
},
15223 {"set", Jim_SetCoreCommand
},
15224 {"unset", Jim_UnsetCoreCommand
},
15225 {"puts", Jim_PutsCoreCommand
},
15226 {"+", Jim_AddCoreCommand
},
15227 {"*", Jim_MulCoreCommand
},
15228 {"-", Jim_SubCoreCommand
},
15229 {"/", Jim_DivCoreCommand
},
15230 {"incr", Jim_IncrCoreCommand
},
15231 {"while", Jim_WhileCoreCommand
},
15232 {"loop", Jim_LoopCoreCommand
},
15233 {"for", Jim_ForCoreCommand
},
15234 {"foreach", Jim_ForeachCoreCommand
},
15235 {"lmap", Jim_LmapCoreCommand
},
15236 {"lassign", Jim_LassignCoreCommand
},
15237 {"if", Jim_IfCoreCommand
},
15238 {"switch", Jim_SwitchCoreCommand
},
15239 {"list", Jim_ListCoreCommand
},
15240 {"lindex", Jim_LindexCoreCommand
},
15241 {"lset", Jim_LsetCoreCommand
},
15242 {"lsearch", Jim_LsearchCoreCommand
},
15243 {"llength", Jim_LlengthCoreCommand
},
15244 {"lappend", Jim_LappendCoreCommand
},
15245 {"linsert", Jim_LinsertCoreCommand
},
15246 {"lreplace", Jim_LreplaceCoreCommand
},
15247 {"lsort", Jim_LsortCoreCommand
},
15248 {"append", Jim_AppendCoreCommand
},
15249 #if defined(JIM_DEBUG_COMMAND) && !defined(JIM_BOOTSTRAP)
15250 {"debug", Jim_DebugCoreCommand
},
15252 {"eval", Jim_EvalCoreCommand
},
15253 {"uplevel", Jim_UplevelCoreCommand
},
15254 {"expr", Jim_ExprCoreCommand
},
15255 {"break", Jim_BreakCoreCommand
},
15256 {"continue", Jim_ContinueCoreCommand
},
15257 {"proc", Jim_ProcCoreCommand
},
15258 {"concat", Jim_ConcatCoreCommand
},
15259 {"return", Jim_ReturnCoreCommand
},
15260 {"upvar", Jim_UpvarCoreCommand
},
15261 {"global", Jim_GlobalCoreCommand
},
15262 {"string", Jim_StringCoreCommand
},
15263 {"time", Jim_TimeCoreCommand
},
15264 {"exit", Jim_ExitCoreCommand
},
15265 {"catch", Jim_CatchCoreCommand
},
15266 #ifdef JIM_REFERENCES
15267 {"ref", Jim_RefCoreCommand
},
15268 {"getref", Jim_GetrefCoreCommand
},
15269 {"setref", Jim_SetrefCoreCommand
},
15270 {"finalize", Jim_FinalizeCoreCommand
},
15271 {"collect", Jim_CollectCoreCommand
},
15273 {"rename", Jim_RenameCoreCommand
},
15274 {"dict", Jim_DictCoreCommand
},
15275 {"subst", Jim_SubstCoreCommand
},
15276 {"info", Jim_InfoCoreCommand
},
15277 {"exists", Jim_ExistsCoreCommand
},
15278 {"split", Jim_SplitCoreCommand
},
15279 {"join", Jim_JoinCoreCommand
},
15280 {"format", Jim_FormatCoreCommand
},
15281 {"scan", Jim_ScanCoreCommand
},
15282 {"error", Jim_ErrorCoreCommand
},
15283 {"lrange", Jim_LrangeCoreCommand
},
15284 {"lrepeat", Jim_LrepeatCoreCommand
},
15285 {"env", Jim_EnvCoreCommand
},
15286 {"source", Jim_SourceCoreCommand
},
15287 {"lreverse", Jim_LreverseCoreCommand
},
15288 {"range", Jim_RangeCoreCommand
},
15289 {"rand", Jim_RandCoreCommand
},
15290 {"tailcall", Jim_TailcallCoreCommand
},
15291 {"local", Jim_LocalCoreCommand
},
15292 {"upcall", Jim_UpcallCoreCommand
},
15293 {"apply", Jim_ApplyCoreCommand
},
15297 void Jim_RegisterCoreCommands(Jim_Interp
*interp
)
15301 while (Jim_CoreCommandsTable
[i
].name
!= NULL
) {
15302 Jim_CreateCommand(interp
,
15303 Jim_CoreCommandsTable
[i
].name
, Jim_CoreCommandsTable
[i
].cmdProc
, NULL
, NULL
);
15308 /* -----------------------------------------------------------------------------
15309 * Interactive prompt
15310 * ---------------------------------------------------------------------------*/
15311 void Jim_MakeErrorMessage(Jim_Interp
*interp
)
15315 argv
[0] = Jim_NewStringObj(interp
, "errorInfo", -1);
15316 argv
[1] = interp
->result
;
15318 Jim_EvalObjVector(interp
, 2, argv
);
15322 * Given a null terminated array of strings, returns an allocated, sorted
15323 * copy of the array.
15325 static char **JimSortStringTable(const char *const *tablePtr
)
15328 char **tablePtrSorted
;
15330 /* Find the size of the table */
15331 for (count
= 0; tablePtr
[count
]; count
++) {
15334 /* Allocate one extra for the terminating NULL pointer */
15335 tablePtrSorted
= Jim_Alloc(sizeof(char *) * (count
+ 1));
15336 memcpy(tablePtrSorted
, tablePtr
, sizeof(char *) * count
);
15337 qsort(tablePtrSorted
, count
, sizeof(char *), qsortCompareStringPointers
);
15338 tablePtrSorted
[count
] = NULL
;
15340 return tablePtrSorted
;
15343 static void JimSetFailedEnumResult(Jim_Interp
*interp
, const char *arg
, const char *badtype
,
15344 const char *prefix
, const char *const *tablePtr
, const char *name
)
15346 char **tablePtrSorted
;
15349 if (name
== NULL
) {
15353 Jim_SetResultFormatted(interp
, "%s%s \"%s\": must be ", badtype
, name
, arg
);
15354 tablePtrSorted
= JimSortStringTable(tablePtr
);
15355 for (i
= 0; tablePtrSorted
[i
]; i
++) {
15356 if (tablePtrSorted
[i
+ 1] == NULL
&& i
> 0) {
15357 Jim_AppendString(interp
, Jim_GetResult(interp
), "or ", -1);
15359 Jim_AppendStrings(interp
, Jim_GetResult(interp
), prefix
, tablePtrSorted
[i
], NULL
);
15360 if (tablePtrSorted
[i
+ 1]) {
15361 Jim_AppendString(interp
, Jim_GetResult(interp
), ", ", -1);
15364 Jim_Free(tablePtrSorted
);
15369 * If objPtr is "-commands" sets the Jim result as a sorted list of options in the table
15370 * and returns JIM_OK.
15372 * Otherwise returns JIM_ERR.
15374 int Jim_CheckShowCommands(Jim_Interp
*interp
, Jim_Obj
*objPtr
, const char *const *tablePtr
)
15376 if (Jim_CompareStringImmediate(interp
, objPtr
, "-commands")) {
15378 char **tablePtrSorted
= JimSortStringTable(tablePtr
);
15379 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
15380 for (i
= 0; tablePtrSorted
[i
]; i
++) {
15381 Jim_ListAppendElement(interp
, Jim_GetResult(interp
), Jim_NewStringObj(interp
, tablePtrSorted
[i
], -1));
15383 Jim_Free(tablePtrSorted
);
15389 /* internal rep is stored in ptrIntvalue
15394 static const Jim_ObjType getEnumObjType
= {
15399 JIM_TYPE_REFERENCES
15402 int Jim_GetEnum(Jim_Interp
*interp
, Jim_Obj
*objPtr
,
15403 const char *const *tablePtr
, int *indexPtr
, const char *name
, int flags
)
15405 const char *bad
= "bad ";
15406 const char *const *entryPtr
= NULL
;
15412 if (objPtr
->typePtr
== &getEnumObjType
) {
15413 if (objPtr
->internalRep
.ptrIntValue
.ptr
== tablePtr
&& objPtr
->internalRep
.ptrIntValue
.int1
== flags
) {
15414 *indexPtr
= objPtr
->internalRep
.ptrIntValue
.int2
;
15419 arg
= Jim_GetString(objPtr
, &arglen
);
15423 for (entryPtr
= tablePtr
, i
= 0; *entryPtr
!= NULL
; entryPtr
++, i
++) {
15424 if (Jim_CompareStringImmediate(interp
, objPtr
, *entryPtr
)) {
15425 /* Found an exact match */
15429 if (flags
& JIM_ENUM_ABBREV
) {
15430 /* Accept an unambiguous abbreviation.
15431 * Note that '-' doesnt' consitute a valid abbreviation
15433 if (strncmp(arg
, *entryPtr
, arglen
) == 0) {
15434 if (*arg
== '-' && arglen
== 1) {
15438 bad
= "ambiguous ";
15446 /* If we had an unambiguous partial match */
15449 /* Record the match in the object */
15450 Jim_FreeIntRep(interp
, objPtr
);
15451 objPtr
->typePtr
= &getEnumObjType
;
15452 objPtr
->internalRep
.ptrIntValue
.ptr
= (void *)tablePtr
;
15453 objPtr
->internalRep
.ptrIntValue
.int1
= flags
;
15454 objPtr
->internalRep
.ptrIntValue
.int2
= match
;
15455 /* Return the result */
15461 if (flags
& JIM_ERRMSG
) {
15462 JimSetFailedEnumResult(interp
, arg
, bad
, "", tablePtr
, name
);
15467 int Jim_FindByName(const char *name
, const char * const array
[], size_t len
)
15471 for (i
= 0; i
< (int)len
; i
++) {
15472 if (array
[i
] && strcmp(array
[i
], name
) == 0) {
15479 int Jim_IsDict(Jim_Obj
*objPtr
)
15481 return objPtr
->typePtr
== &dictObjType
;
15484 int Jim_IsList(Jim_Obj
*objPtr
)
15486 return objPtr
->typePtr
== &listObjType
;
15490 * Very simple printf-like formatting, designed for error messages.
15492 * The format may contain up to 5 '%s' or '%#s', corresponding to variable arguments.
15493 * The resulting string is created and set as the result.
15495 * Each '%s' should correspond to a regular string parameter.
15496 * Each '%#s' should correspond to a (Jim_Obj *) parameter.
15497 * Any other printf specifier is not allowed (but %% is allowed for the % character).
15499 * e.g. Jim_SetResultFormatted(interp, "Bad option \"%#s\" in proc \"%#s\"", optionObjPtr, procNamePtr);
15501 * Note: We take advantage of the fact that printf has the same behaviour for both %s and %#s
15503 * Note that any Jim_Obj parameters with zero ref count will be freed as a result of this call.
15505 void Jim_SetResultFormatted(Jim_Interp
*interp
, const char *format
, ...)
15507 /* Initial space needed */
15508 int len
= strlen(format
);
15511 const char *params
[5];
15513 Jim_Obj
*objparam
[5];
15518 va_start(args
, format
);
15520 for (i
= 0; i
< len
&& n
< 5; i
++) {
15523 if (strncmp(format
+ i
, "%s", 2) == 0) {
15524 params
[n
] = va_arg(args
, char *);
15526 l
= strlen(params
[n
]);
15528 else if (strncmp(format
+ i
, "%#s", 3) == 0) {
15529 Jim_Obj
*objPtr
= va_arg(args
, Jim_Obj
*);
15531 params
[n
] = Jim_GetString(objPtr
, &l
);
15532 objparam
[nobjparam
++] = objPtr
;
15533 Jim_IncrRefCount(objPtr
);
15536 if (format
[i
] == '%') {
15546 buf
= Jim_Alloc(len
+ 1);
15547 len
= snprintf(buf
, len
+ 1, format
, params
[0], params
[1], params
[2], params
[3], params
[4]);
15551 Jim_SetResult(interp
, Jim_NewStringObjNoAlloc(interp
, buf
, len
));
15553 for (i
= 0; i
< nobjparam
; i
++) {
15554 Jim_DecrRefCount(interp
, objparam
[i
]);
15559 #ifndef jim_ext_package
15560 int Jim_PackageProvide(Jim_Interp
*interp
, const char *name
, const char *ver
, int flags
)
15565 #ifndef jim_ext_aio
15566 FILE *Jim_AioFilehandle(Jim_Interp
*interp
, Jim_Obj
*fhObj
)
15568 Jim_SetResultString(interp
, "aio not enabled", -1);
15575 * Local Variables: ***
15576 * c-basic-offset: 4 ***