### Eclipse Workspace Patch 1.0 #P ecos Index: services/jimtcl/current/ChangeLog =================================================================== RCS file: services/jimtcl/current/ChangeLog diff -N services/jimtcl/current/ChangeLog --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ services/jimtcl/current/ChangeLog 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,39 @@ +2008-01-04 Oyvind Harboe + + * Split out jim from athttpd. athttpd can be subsequently updated to + use this module + * src/jim.c: fixed bug in parsing hex in expr. This patch has been submitted + to jim-devel + +//=========================================================================== +//####ECOSGPLCOPYRIGHTBEGIN#### +// ------------------------------------------- +// This file is part of eCos, the Embedded Configurable Operating System. +// Copyright (C) 2005, 2006 eCosCentric Ltd. +// +// eCos is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 2 or (at your option) any later version. +// +// eCos is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License along +// with eCos; if not, write to the Free Software Foundation, Inc., +// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. +// +// As a special exception, if other files instantiate templates or use macros +// or inline functions from this file, or you compile this file and link it +// with other works to produce a work based on this file, this file does not +// by itself cause the resulting work to be covered by the GNU General Public +// License. However the source code for this file must still be made available +// in accordance with section (3) of the GNU General Public License. +// +// This exception does not invalidate any other reasons why a work based on +// this file might be covered by the GNU General Public License. +// +// ------------------------------------------- +//####ECOSGPLCOPYRIGHTEND#### +//=========================================================================== Index: services/jimtcl/current/src/jim.c =================================================================== RCS file: services/jimtcl/current/src/jim.c diff -N services/jimtcl/current/src/jim.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ services/jimtcl/current/src/jim.c 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,11814 @@ +/* Jim - A small embeddable Tcl interpreter + * Copyright 2005 Salvatore Sanfilippo + * Copyright 2005 Clemens Hintze + * + * $Id: jim.c,v 1.170 2006/11/06 21:48:57 antirez Exp $ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * A copy of the license is also included in the source distribution + * of Jim, as a TXT file name called LICENSE. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define __JIM_CORE__ +#define JIM_OPTIMIZATION /* comment to avoid optimizations and reduce size */ + +#include + +#ifndef JIM_ANSIC +#define JIM_DYNLIB /* Dynamic library support for UNIX and WIN32 */ +#endif /* JIM_ANSIC */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Include the platform dependent libraries for + * dynamic loading of libraries. */ +#ifdef JIM_DYNLIB +#if defined(_WIN32) || defined(WIN32) +#ifndef WIN32 +#define WIN32 1 +#endif +#define STRICT +#define WIN32_LEAN_AND_MEAN +#include +#if _MSC_VER >= 1000 +#pragma warning(disable:4146) +#endif /* _MSC_VER */ +#else +#include +#endif /* WIN32 */ +#endif /* JIM_DYNLIB */ + +#include + +#ifdef HAVE_BACKTRACE +#include +#endif + +/* ----------------------------------------------------------------------------- + * Global variables + * ---------------------------------------------------------------------------*/ + +/* A shared empty string for the objects string representation. + * Jim_InvalidateStringRep knows about it and don't try to free. */ +static char *JimEmptyStringRep = (char*) ""; + +/* ----------------------------------------------------------------------------- + * Required prototypes of not exported functions + * ---------------------------------------------------------------------------*/ +static void JimChangeCallFrameId(Jim_Interp *interp, Jim_CallFrame *cf); +static void JimFreeCallFrame(Jim_Interp *interp, Jim_CallFrame *cf, int flags); +static void JimRegisterCoreApi(Jim_Interp *interp); + +static Jim_HashTableType JimVariablesHashTableType; + +/* ----------------------------------------------------------------------------- + * Utility functions + * ---------------------------------------------------------------------------*/ + +/* + * Convert a string to a jim_wide INTEGER. + * This function originates from BSD. + * + * Ignores `locale' stuff. Assumes that the upper and lower case + * alphabets and digits are each contiguous. + */ +#ifdef HAVE_LONG_LONG +#define JimIsAscii(c) (((c) & ~0x7f) == 0) +static jim_wide JimStrtoll(const char *nptr, char **endptr, register int base) +{ + register const char *s; + register unsigned jim_wide acc; + register unsigned char c; + register unsigned jim_wide qbase, cutoff; + register int neg, any, cutlim; + + /* + * Skip white space and pick up leading +/- sign if any. + * If base is 0, allow 0x for hex and 0 for octal, else + * assume decimal; if base is already 16, allow 0x. + */ + s = nptr; + do { + c = *s++; + } while (isspace(c)); + if (c == '-') { + neg = 1; + c = *s++; + } else { + neg = 0; + if (c == '+') + c = *s++; + } + if ((base == 0 || base == 16) && + c == '0' && (*s == 'x' || *s == 'X')) { + c = s[1]; + s += 2; + base = 16; + } + if (base == 0) + base = c == '0' ? 8 : 10; + + /* + * Compute the cutoff value between legal numbers and illegal + * numbers. That is the largest legal value, divided by the + * base. An input number that is greater than this value, if + * followed by a legal input character, is too big. One that + * is equal to this value may be valid or not; the limit + * between valid and invalid numbers is then based on the last + * digit. For instance, if the range for quads is + * [-9223372036854775808..9223372036854775807] and the input base + * is 10, cutoff will be set to 922337203685477580 and cutlim to + * either 7 (neg==0) or 8 (neg==1), meaning that if we have + * accumulated a value > 922337203685477580, or equal but the + * next digit is > 7 (or 8), the number is too big, and we will + * return a range error. + * + * Set any if any `digits' consumed; make it negative to indicate + * overflow. + */ + qbase = (unsigned)base; + cutoff = neg ? (unsigned jim_wide)-(LLONG_MIN + LLONG_MAX) + LLONG_MAX + : LLONG_MAX; + cutlim = (int)(cutoff % qbase); + cutoff /= qbase; + for (acc = 0, any = 0;; c = *s++) { + if (!JimIsAscii(c)) + break; + if (isdigit(c)) + c -= '0'; + else if (isalpha(c)) + c -= isupper(c) ? 'A' - 10 : 'a' - 10; + else + break; + if (c >= base) + break; + if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) + any = -1; + else { + any = 1; + acc *= qbase; + acc += c; + } + } + if (any < 0) { + acc = neg ? LLONG_MIN : LLONG_MAX; + errno = ERANGE; + } else if (neg) + acc = -acc; + if (endptr != 0) + *endptr = (char *)(any ? s - 1 : nptr); + return (acc); +} +#endif + +/* Glob-style pattern matching. */ +static int JimStringMatch(const char *pattern, int patternLen, + const char *string, int stringLen, int nocase) +{ + while(patternLen) { + switch(pattern[0]) { + case '*': + while (pattern[1] == '*') { + pattern++; + patternLen--; + } + if (patternLen == 1) + return 1; /* match */ + while(stringLen) { + if (JimStringMatch(pattern+1, patternLen-1, + string, stringLen, nocase)) + return 1; /* match */ + string++; + stringLen--; + } + return 0; /* no match */ + break; + case '?': + if (stringLen == 0) + return 0; /* no match */ + string++; + stringLen--; + break; + case '[': + { + int not, match; + + pattern++; + patternLen--; + not = pattern[0] == '^'; + if (not) { + pattern++; + patternLen--; + } + match = 0; + while(1) { + if (pattern[0] == '\\') { + pattern++; + patternLen--; + if (pattern[0] == string[0]) + match = 1; + } else if (pattern[0] == ']') { + break; + } else if (patternLen == 0) { + pattern--; + patternLen++; + break; + } else if (pattern[1] == '-' && patternLen >= 3) { + int start = pattern[0]; + int end = pattern[2]; + int c = string[0]; + if (start > end) { + int t = start; + start = end; + end = t; + } + if (nocase) { + start = tolower(start); + end = tolower(end); + c = tolower(c); + } + pattern += 2; + patternLen -= 2; + if (c >= start && c <= end) + match = 1; + } else { + if (!nocase) { + if (pattern[0] == string[0]) + match = 1; + } else { + if (tolower((int)pattern[0]) == tolower((int)string[0])) + match = 1; + } + } + pattern++; + patternLen--; + } + if (not) + match = !match; + if (!match) + return 0; /* no match */ + string++; + stringLen--; + break; + } + case '\\': + if (patternLen >= 2) { + pattern++; + patternLen--; + } + /* fall through */ + default: + if (!nocase) { + if (pattern[0] != string[0]) + return 0; /* no match */ + } else { + if (tolower((int)pattern[0]) != tolower((int)string[0])) + return 0; /* no match */ + } + string++; + stringLen--; + break; + } + pattern++; + patternLen--; + if (stringLen == 0) { + while(*pattern == '*') { + pattern++; + patternLen--; + } + break; + } + } + if (patternLen == 0 && stringLen == 0) + return 1; + return 0; +} + +int JimStringCompare(const char *s1, int l1, const char *s2, int l2, + int nocase) +{ + unsigned char *u1 = (unsigned char*) s1, *u2 = (unsigned char*) s2; + + if (nocase == 0) { + while(l1 && l2) { + if (*u1 != *u2) + return (int)*u1-*u2; + u1++; u2++; l1--; l2--; + } + if (!l1 && !l2) return 0; + return l1-l2; + } else { + while(l1 && l2) { + if (tolower((int)*u1) != tolower((int)*u2)) + return tolower((int)*u1)-tolower((int)*u2); + u1++; u2++; l1--; l2--; + } + if (!l1 && !l2) return 0; + return l1-l2; + } +} + +/* Search 's1' inside 's2', starting to search from char 'index' of 's2'. + * The index of the first occurrence of s1 in s2 is returned. + * If s1 is not found inside s2, -1 is returned. */ +int JimStringFirst(const char *s1, int l1, const char *s2, int l2, int index) +{ + int i; + + if (!l1 || !l2 || l1 > l2) return -1; + if (index < 0) index = 0; + s2 += index; + for (i = index; i <= l2-l1; i++) { + if (memcmp(s2, s1, l1) == 0) + return i; + s2++; + } + return -1; +} + +int Jim_WideToString(char *buf, jim_wide wideValue) +{ + const char *fmt = "%" JIM_WIDE_MODIFIER; + return sprintf(buf, fmt, wideValue); +} + +int Jim_StringToWide(const char *str, jim_wide *widePtr, int base) +{ + char *endptr; + +#ifdef HAVE_LONG_LONG + *widePtr = JimStrtoll(str, &endptr, base); +#else + *widePtr = strtol(str, &endptr, base); +#endif + if (str[0] == '\0') + return JIM_ERR; + if (endptr[0] != '\0') { + while(*endptr) { + if (!isspace((int)*endptr)) + return JIM_ERR; + endptr++; + } + } + return JIM_OK; +} + +int Jim_StringToIndex(const char *str, int *intPtr) +{ + char *endptr; + + *intPtr = strtol(str, &endptr, 10); + if (str[0] == '\0') + return JIM_ERR; + if (endptr[0] != '\0') { + while(*endptr) { + if (!isspace((int)*endptr)) + return JIM_ERR; + endptr++; + } + } + return JIM_OK; +} + +/* The string representation of references has two features in order + * to make the GC faster. The first is that every reference starts + * with a non common character '~', in order to make the string matching + * fater. The second is that the reference string rep his 32 characters + * in length, this allows to avoid to check every object with a string + * repr < 32, and usually there are many of this objects. */ + +#define JIM_REFERENCE_SPACE (35+JIM_REFERENCE_TAGLEN) + +static int JimFormatReference(char *buf, Jim_Reference *refPtr, jim_wide id) +{ + const char *fmt = ".%020" JIM_WIDE_MODIFIER ">"; + sprintf(buf, fmt, refPtr->tag, id); + return JIM_REFERENCE_SPACE; +} + +int Jim_DoubleToString(char *buf, double doubleValue) +{ + char *s; + int len; + + len = sprintf(buf, "%.17g", doubleValue); + s = buf; + while(*s) { + if (*s == '.') return len; + s++; + } + /* Add a final ".0" if it's a number. But not + * for NaN or InF */ + if (isdigit((int)buf[0]) + || ((buf[0] == '-' || buf[0] == '+') + && isdigit((int)buf[1]))) { + s[0] = '.'; + s[1] = '0'; + s[2] = '\0'; + return len+2; + } + return len; +} + +int Jim_StringToDouble(const char *str, double *doublePtr) +{ + char *endptr; + + *doublePtr = strtod(str, &endptr); + if (str[0] == '\0' || endptr[0] != '\0') + return JIM_ERR; + return JIM_OK; +} + +static jim_wide JimPowWide(jim_wide b, jim_wide e) +{ + jim_wide i, res = 1; + if ((b==0 && e!=0) || (e<0)) return 0; + for(i=0; istderr : stderr; + + va_start(ap, fmt); + fprintf(fp, JIM_NL "JIM INTERPRETER PANIC: "); + vfprintf(fp, fmt, ap); + fprintf(fp, JIM_NL JIM_NL); + va_end(ap); +#ifdef HAVE_BACKTRACE + { + void *array[40]; + int size, i; + char **strings; + + size = backtrace(array, 40); + strings = backtrace_symbols(array, size); + for (i = 0; i < size; i++) + fprintf(fp,"[backtrace] %s" JIM_NL, strings[i]); + fprintf(fp,"[backtrace] Include the above lines and the output" JIM_NL); + fprintf(fp,"[backtrace] of 'nm ' in the bug report." JIM_NL); + } +#endif + abort(); +} + +/* ----------------------------------------------------------------------------- + * Memory allocation + * ---------------------------------------------------------------------------*/ + +/* Macro used for memory debugging. + * In order for they to work you have to rename Jim_Alloc into _Jim_Alloc + * and similary for Jim_Realloc and Jim_Free */ +#if 0 +#define Jim_Alloc(s) (printf("%s %d: Jim_Alloc(%d)\n",__FILE__,__LINE__,s),_Jim_Alloc(s)) +#define Jim_Free(p) (printf("%s %d: Jim_Free(%p)\n",__FILE__,__LINE__,p),_Jim_Free(p)) +#define Jim_Realloc(p,s) (printf("%s %d: Jim_Realloc(%p,%d)\n",__FILE__,__LINE__,p,s),_Jim_Realloc(p,s)) +#endif + +void *Jim_Alloc(int size) +{ + void *p = malloc(size); + if (p == NULL) + Jim_Panic(NULL,"Out of memory"); + return p; +} + +void Jim_Free(void *ptr) { + free(ptr); +} + +void *Jim_Realloc(void *ptr, int size) +{ + void *p = realloc(ptr, size); + if (p == NULL) + Jim_Panic(NULL,"Out of memory"); + return p; +} + +char *Jim_StrDup(const char *s) +{ + int l = strlen(s); + char *copy = Jim_Alloc(l+1); + + memcpy(copy, s, l+1); + return copy; +} + +char *Jim_StrDupLen(const char *s, int l) +{ + char *copy = Jim_Alloc(l+1); + + memcpy(copy, s, l+1); + copy[l] = 0; /* Just to be sure, original could be substring */ + return copy; +} + +/* ----------------------------------------------------------------------------- + * Time related functions + * ---------------------------------------------------------------------------*/ +/* Returns microseconds of CPU used since start. */ +static jim_wide JimClock(void) +{ +#if (defined WIN32) && !(defined JIM_ANSIC) + LARGE_INTEGER t, f; + QueryPerformanceFrequency(&f); + QueryPerformanceCounter(&t); + return (long)((t.QuadPart * 1000000) / f.QuadPart); +#else /* !WIN32 */ + clock_t clocks = clock(); + + return (long)(clocks*(1000000/CLOCKS_PER_SEC)); +#endif /* WIN32 */ +} + +/* ----------------------------------------------------------------------------- + * Hash Tables + * ---------------------------------------------------------------------------*/ + +/* -------------------------- private prototypes ---------------------------- */ +static int JimExpandHashTableIfNeeded(Jim_HashTable *ht); +static unsigned int JimHashTableNextPower(unsigned int size); +static int JimInsertHashEntry(Jim_HashTable *ht, const void *key); + +/* -------------------------- hash functions -------------------------------- */ + +/* Thomas Wang's 32 bit Mix Function */ +unsigned int Jim_IntHashFunction(unsigned int key) +{ + key += ~(key << 15); + key ^= (key >> 10); + key += (key << 3); + key ^= (key >> 6); + key += ~(key << 11); + key ^= (key >> 16); + return key; +} + +/* Identity hash function for integer keys */ +unsigned int Jim_IdentityHashFunction(unsigned int key) +{ + return key; +} + +/* Generic hash function (we are using to multiply by 9 and add the byte + * as Tcl) */ +unsigned int Jim_GenHashFunction(const unsigned char *buf, int len) +{ + unsigned int h = 0; + while(len--) + h += (h<<3)+*buf++; + return h; +} + +/* ----------------------------- API implementation ------------------------- */ +/* reset an hashtable already initialized with ht_init(). + * NOTE: This function should only called by ht_destroy(). */ +static void JimResetHashTable(Jim_HashTable *ht) +{ + ht->table = NULL; + ht->size = 0; + ht->sizemask = 0; + ht->used = 0; + ht->collisions = 0; +} + +/* Initialize the hash table */ +int Jim_InitHashTable(Jim_HashTable *ht, Jim_HashTableType *type, + void *privDataPtr) +{ + JimResetHashTable(ht); + ht->type = type; + ht->privdata = privDataPtr; + return JIM_OK; +} + +/* Resize the table to the minimal size that contains all the elements, + * but with the invariant of a USER/BUCKETS ration near to <= 1 */ +int Jim_ResizeHashTable(Jim_HashTable *ht) +{ + int minimal = ht->used; + + if (minimal < JIM_HT_INITIAL_SIZE) + minimal = JIM_HT_INITIAL_SIZE; + return Jim_ExpandHashTable(ht, minimal); +} + +/* Expand or create the hashtable */ +int Jim_ExpandHashTable(Jim_HashTable *ht, unsigned int size) +{ + Jim_HashTable n; /* the new hashtable */ + unsigned int realsize = JimHashTableNextPower(size), i; + + /* the size is invalid if it is smaller than the number of + * elements already inside the hashtable */ + if (ht->used >= size) + return JIM_ERR; + + Jim_InitHashTable(&n, ht->type, ht->privdata); + n.size = realsize; + n.sizemask = realsize-1; + n.table = Jim_Alloc(realsize*sizeof(Jim_HashEntry*)); + + /* Initialize all the pointers to NULL */ + memset(n.table, 0, realsize*sizeof(Jim_HashEntry*)); + + /* Copy all the elements from the old to the new table: + * note that if the old hash table is empty ht->size is zero, + * so Jim_ExpandHashTable just creates an hash table. */ + n.used = ht->used; + for (i = 0; i < ht->size && ht->used > 0; i++) { + Jim_HashEntry *he, *nextHe; + + if (ht->table[i] == NULL) continue; + + /* For each hash entry on this slot... */ + he = ht->table[i]; + while(he) { + unsigned int h; + + nextHe = he->next; + /* Get the new element index */ + h = Jim_HashKey(ht, he->key) & n.sizemask; + he->next = n.table[h]; + n.table[h] = he; + ht->used--; + /* Pass to the next element */ + he = nextHe; + } + } + assert(ht->used == 0); + Jim_Free(ht->table); + + /* Remap the new hashtable in the old */ + *ht = n; + return JIM_OK; +} + +/* Add an element to the target hash table */ +int Jim_AddHashEntry(Jim_HashTable *ht, const void *key, void *val) +{ + int index; + Jim_HashEntry *entry; + + /* Get the index of the new element, or -1 if + * the element already exists. */ + if ((index = JimInsertHashEntry(ht, key)) == -1) + return JIM_ERR; + + /* Allocates the memory and stores key */ + entry = Jim_Alloc(sizeof(*entry)); + entry->next = ht->table[index]; + ht->table[index] = entry; + + /* Set the hash entry fields. */ + Jim_SetHashKey(ht, entry, key); + Jim_SetHashVal(ht, entry, val); + ht->used++; + return JIM_OK; +} + +/* Add an element, discarding the old if the key already exists */ +int Jim_ReplaceHashEntry(Jim_HashTable *ht, const void *key, void *val) +{ + Jim_HashEntry *entry; + + /* Try to add the element. If the key + * does not exists Jim_AddHashEntry will suceed. */ + if (Jim_AddHashEntry(ht, key, val) == JIM_OK) + return JIM_OK; + /* It already exists, get the entry */ + entry = Jim_FindHashEntry(ht, key); + /* Free the old value and set the new one */ + Jim_FreeEntryVal(ht, entry); + Jim_SetHashVal(ht, entry, val); + return JIM_OK; +} + +/* Search and remove an element */ +int Jim_DeleteHashEntry(Jim_HashTable *ht, const void *key) +{ + unsigned int h; + Jim_HashEntry *he, *prevHe; + + if (ht->size == 0) + return JIM_ERR; + h = Jim_HashKey(ht, key) & ht->sizemask; + he = ht->table[h]; + + prevHe = NULL; + while(he) { + if (Jim_CompareHashKeys(ht, key, he->key)) { + /* Unlink the element from the list */ + if (prevHe) + prevHe->next = he->next; + else + ht->table[h] = he->next; + Jim_FreeEntryKey(ht, he); + Jim_FreeEntryVal(ht, he); + Jim_Free(he); + ht->used--; + return JIM_OK; + } + prevHe = he; + he = he->next; + } + return JIM_ERR; /* not found */ +} + +/* Destroy an entire hash table */ +int Jim_FreeHashTable(Jim_HashTable *ht) +{ + unsigned int i; + + /* Free all the elements */ + for (i = 0; i < ht->size && ht->used > 0; i++) { + Jim_HashEntry *he, *nextHe; + + if ((he = ht->table[i]) == NULL) continue; + while(he) { + nextHe = he->next; + Jim_FreeEntryKey(ht, he); + Jim_FreeEntryVal(ht, he); + Jim_Free(he); + ht->used--; + he = nextHe; + } + } + /* Free the table and the allocated cache structure */ + Jim_Free(ht->table); + /* Re-initialize the table */ + JimResetHashTable(ht); + return JIM_OK; /* never fails */ +} + +Jim_HashEntry *Jim_FindHashEntry(Jim_HashTable *ht, const void *key) +{ + Jim_HashEntry *he; + unsigned int h; + + if (ht->size == 0) return NULL; + h = Jim_HashKey(ht, key) & ht->sizemask; + he = ht->table[h]; + while(he) { + if (Jim_CompareHashKeys(ht, key, he->key)) + return he; + he = he->next; + } + return NULL; +} + +Jim_HashTableIterator *Jim_GetHashTableIterator(Jim_HashTable *ht) +{ + Jim_HashTableIterator *iter = Jim_Alloc(sizeof(*iter)); + + iter->ht = ht; + iter->index = -1; + iter->entry = NULL; + iter->nextEntry = NULL; + return iter; +} + +Jim_HashEntry *Jim_NextHashEntry(Jim_HashTableIterator *iter) +{ + while (1) { + if (iter->entry == NULL) { + iter->index++; + if (iter->index >= + (signed)iter->ht->size) break; + iter->entry = iter->ht->table[iter->index]; + } else { + iter->entry = iter->nextEntry; + } + if (iter->entry) { + /* We need to save the 'next' here, the iterator user + * may delete the entry we are returning. */ + iter->nextEntry = iter->entry->next; + return iter->entry; + } + } + return NULL; +} + +/* ------------------------- private functions ------------------------------ */ + +/* Expand the hash table if needed */ +static int JimExpandHashTableIfNeeded(Jim_HashTable *ht) +{ + /* If the hash table is empty expand it to the intial size, + * if the table is "full" dobule its size. */ + if (ht->size == 0) + return Jim_ExpandHashTable(ht, JIM_HT_INITIAL_SIZE); + if (ht->size == ht->used) + return Jim_ExpandHashTable(ht, ht->size*2); + return JIM_OK; +} + +/* Our hash table capability is a power of two */ +static unsigned int JimHashTableNextPower(unsigned int size) +{ + unsigned int i = JIM_HT_INITIAL_SIZE; + + if (size >= 2147483648U) + return 2147483648U; + while(1) { + if (i >= size) + return i; + i *= 2; + } +} + +/* Returns the index of a free slot that can be populated with + * an hash entry for the given 'key'. + * If the key already exists, -1 is returned. */ +static int JimInsertHashEntry(Jim_HashTable *ht, const void *key) +{ + unsigned int h; + Jim_HashEntry *he; + + /* Expand the hashtable if needed */ + if (JimExpandHashTableIfNeeded(ht) == JIM_ERR) + return -1; + /* Compute the key hash value */ + h = Jim_HashKey(ht, key) & ht->sizemask; + /* Search if this slot does not already contain the given key */ + he = ht->table[h]; + while(he) { + if (Jim_CompareHashKeys(ht, key, he->key)) + return -1; + he = he->next; + } + return h; +} + +/* ----------------------- StringCopy Hash Table Type ------------------------*/ + +static unsigned int JimStringCopyHTHashFunction(const void *key) +{ + return Jim_GenHashFunction(key, strlen(key)); +} + +static const void *JimStringCopyHTKeyDup(void *privdata, const void *key) +{ + int len = strlen(key); + char *copy = Jim_Alloc(len+1); + JIM_NOTUSED(privdata); + + memcpy(copy, key, len); + copy[len] = '\0'; + return copy; +} + +static void *JimStringKeyValCopyHTValDup(void *privdata, const void *val) +{ + int len = strlen(val); + char *copy = Jim_Alloc(len+1); + JIM_NOTUSED(privdata); + + memcpy(copy, val, len); + copy[len] = '\0'; + return copy; +} + +static int JimStringCopyHTKeyCompare(void *privdata, const void *key1, + const void *key2) +{ + JIM_NOTUSED(privdata); + + return strcmp(key1, key2) == 0; +} + +static void JimStringCopyHTKeyDestructor(void *privdata, const void *key) +{ + JIM_NOTUSED(privdata); + + Jim_Free((void*)key); /* ATTENTION: const cast */ +} + +static void JimStringKeyValCopyHTValDestructor(void *privdata, void *val) +{ + JIM_NOTUSED(privdata); + + Jim_Free((void*)val); /* ATTENTION: const cast */ +} + +static Jim_HashTableType JimStringCopyHashTableType = { + JimStringCopyHTHashFunction, /* hash function */ + JimStringCopyHTKeyDup, /* key dup */ + NULL, /* val dup */ + JimStringCopyHTKeyCompare, /* key compare */ + JimStringCopyHTKeyDestructor, /* key destructor */ + NULL /* val destructor */ +}; + +/* This is like StringCopy but does not auto-duplicate the key. + * It's used for intepreter's shared strings. */ +static Jim_HashTableType JimSharedStringsHashTableType = { + JimStringCopyHTHashFunction, /* hash function */ + NULL, /* key dup */ + NULL, /* val dup */ + JimStringCopyHTKeyCompare, /* key compare */ + JimStringCopyHTKeyDestructor, /* key destructor */ + NULL /* val destructor */ +}; + +/* This is like StringCopy but also automatically handle dynamic + * allocated C strings as values. */ +static Jim_HashTableType JimStringKeyValCopyHashTableType = { + JimStringCopyHTHashFunction, /* hash function */ + JimStringCopyHTKeyDup, /* key dup */ + JimStringKeyValCopyHTValDup, /* val dup */ + JimStringCopyHTKeyCompare, /* key compare */ + JimStringCopyHTKeyDestructor, /* key destructor */ + JimStringKeyValCopyHTValDestructor, /* val destructor */ +}; + +typedef struct AssocDataValue { + Jim_InterpDeleteProc *delProc; + void *data; +} AssocDataValue; + +static void JimAssocDataHashTableValueDestructor(void *privdata, void *data) +{ + AssocDataValue *assocPtr = (AssocDataValue *)data; + if (assocPtr->delProc != NULL) + assocPtr->delProc((Jim_Interp *)privdata, assocPtr->data); + Jim_Free(data); +} + +static Jim_HashTableType JimAssocDataHashTableType = { + JimStringCopyHTHashFunction, /* hash function */ + JimStringCopyHTKeyDup, /* key dup */ + NULL, /* val dup */ + JimStringCopyHTKeyCompare, /* key compare */ + JimStringCopyHTKeyDestructor, /* key destructor */ + JimAssocDataHashTableValueDestructor /* val destructor */ +}; + +/* ----------------------------------------------------------------------------- + * Stack - This is a simple generic stack implementation. It is used for + * example in the 'expr' expression compiler. + * ---------------------------------------------------------------------------*/ +void Jim_InitStack(Jim_Stack *stack) +{ + stack->len = 0; + stack->maxlen = 0; + stack->vector = NULL; +} + +void Jim_FreeStack(Jim_Stack *stack) +{ + Jim_Free(stack->vector); +} + +int Jim_StackLen(Jim_Stack *stack) +{ + return stack->len; +} + +void Jim_StackPush(Jim_Stack *stack, void *element) { + int neededLen = stack->len+1; + if (neededLen > stack->maxlen) { + stack->maxlen = neededLen*2; + stack->vector = Jim_Realloc(stack->vector, sizeof(void*)*stack->maxlen); + } + stack->vector[stack->len] = element; + stack->len++; +} + +void *Jim_StackPop(Jim_Stack *stack) +{ + if (stack->len == 0) return NULL; + stack->len--; + return stack->vector[stack->len]; +} + +void *Jim_StackPeek(Jim_Stack *stack) +{ + if (stack->len == 0) return NULL; + return stack->vector[stack->len-1]; +} + +void Jim_FreeStackElements(Jim_Stack *stack, void (*freeFunc)(void *ptr)) +{ + int i; + + for (i = 0; i < stack->len; i++) + freeFunc(stack->vector[i]); +} + +/* ----------------------------------------------------------------------------- + * Parser + * ---------------------------------------------------------------------------*/ + +/* Token types */ +#define JIM_TT_NONE -1 /* No token returned */ +#define JIM_TT_STR 0 /* simple string */ +#define JIM_TT_ESC 1 /* string that needs escape chars conversion */ +#define JIM_TT_VAR 2 /* var substitution */ +#define JIM_TT_DICTSUGAR 3 /* Syntax sugar for [dict get], $foo(bar) */ +#define JIM_TT_CMD 4 /* command substitution */ +#define JIM_TT_SEP 5 /* word separator */ +#define JIM_TT_EOL 6 /* line separator */ + +/* Additional token types needed for expressions */ +#define JIM_TT_SUBEXPR_START 7 +#define JIM_TT_SUBEXPR_END 8 +#define JIM_TT_EXPR_NUMBER 9 +#define JIM_TT_EXPR_OPERATOR 10 + +/* Parser states */ +#define JIM_PS_DEF 0 /* Default state */ +#define JIM_PS_QUOTE 1 /* Inside "" */ + +/* Parser context structure. The same context is used both to parse + * Tcl scripts and lists. */ +struct JimParserCtx { + const char *prg; /* Program text */ + const char *p; /* Pointer to the point of the program we are parsing */ + int len; /* Left length of 'prg' */ + int linenr; /* Current line number */ + const char *tstart; + const char *tend; /* Returned token is at tstart-tend in 'prg'. */ + int tline; /* Line number of the returned token */ + int tt; /* Token type */ + int eof; /* Non zero if EOF condition is true. */ + int state; /* Parser state */ + int comment; /* Non zero if the next chars may be a comment. */ +}; + +#define JimParserEof(c) ((c)->eof) +#define JimParserTstart(c) ((c)->tstart) +#define JimParserTend(c) ((c)->tend) +#define JimParserTtype(c) ((c)->tt) +#define JimParserTline(c) ((c)->tline) + +static int JimParseScript(struct JimParserCtx *pc); +static int JimParseSep(struct JimParserCtx *pc); +static int JimParseEol(struct JimParserCtx *pc); +static int JimParseCmd(struct JimParserCtx *pc); +static int JimParseVar(struct JimParserCtx *pc); +static int JimParseBrace(struct JimParserCtx *pc); +static int JimParseStr(struct JimParserCtx *pc); +static int JimParseComment(struct JimParserCtx *pc); +static char *JimParserGetToken(struct JimParserCtx *pc, + int *lenPtr, int *typePtr, int *linePtr); + +/* Initialize a parser context. + * 'prg' is a pointer to the program text, linenr is the line + * number of the first line contained in the program. */ +void JimParserInit(struct JimParserCtx *pc, const char *prg, + int len, int linenr) +{ + pc->prg = prg; + pc->p = prg; + pc->len = len; + pc->tstart = NULL; + pc->tend = NULL; + pc->tline = 0; + pc->tt = JIM_TT_NONE; + pc->eof = 0; + pc->state = JIM_PS_DEF; + pc->linenr = linenr; + pc->comment = 1; +} + +int JimParseScript(struct JimParserCtx *pc) +{ + while(1) { /* the while is used to reiterate with continue if needed */ + if (!pc->len) { + pc->tstart = pc->p; + pc->tend = pc->p-1; + pc->tline = pc->linenr; + pc->tt = JIM_TT_EOL; + pc->eof = 1; + return JIM_OK; + } + switch(*(pc->p)) { + case '\\': + if (*(pc->p+1) == '\n') + return JimParseSep(pc); + else { + pc->comment = 0; + return JimParseStr(pc); + } + break; + case ' ': + case '\t': + case '\r': + if (pc->state == JIM_PS_DEF) + return JimParseSep(pc); + else { + pc->comment = 0; + return JimParseStr(pc); + } + break; + case '\n': + case ';': + pc->comment = 1; + if (pc->state == JIM_PS_DEF) + return JimParseEol(pc); + else + return JimParseStr(pc); + break; + case '[': + pc->comment = 0; + return JimParseCmd(pc); + break; + case '$': + pc->comment = 0; + if (JimParseVar(pc) == JIM_ERR) { + pc->tstart = pc->tend = pc->p++; pc->len--; + pc->tline = pc->linenr; + pc->tt = JIM_TT_STR; + return JIM_OK; + } else + return JIM_OK; + break; + case '#': + if (pc->comment) { + JimParseComment(pc); + continue; + } else { + return JimParseStr(pc); + } + default: + pc->comment = 0; + return JimParseStr(pc); + break; + } + return JIM_OK; + } +} + +int JimParseSep(struct JimParserCtx *pc) +{ + pc->tstart = pc->p; + pc->tline = pc->linenr; + while (*pc->p == ' ' || *pc->p == '\t' || *pc->p == '\r' || + (*pc->p == '\\' && *(pc->p+1) == '\n')) { + if (*pc->p == '\\') { + pc->p++; pc->len--; + pc->linenr++; + } + pc->p++; pc->len--; + } + pc->tend = pc->p-1; + pc->tt = JIM_TT_SEP; + return JIM_OK; +} + +int JimParseEol(struct JimParserCtx *pc) +{ + pc->tstart = pc->p; + pc->tline = pc->linenr; + while (*pc->p == ' ' || *pc->p == '\n' || + *pc->p == '\t' || *pc->p == '\r' || *pc->p == ';') { + if (*pc->p == '\n') + pc->linenr++; + pc->p++; pc->len--; + } + pc->tend = pc->p-1; + pc->tt = JIM_TT_EOL; + return JIM_OK; +} + +/* Todo. Don't stop if ']' appears inside {} or quoted. + * Also should handle the case of puts [string length "]"] */ +int JimParseCmd(struct JimParserCtx *pc) +{ + int level = 1; + int blevel = 0; + + pc->tstart = ++pc->p; pc->len--; + pc->tline = pc->linenr; + while (1) { + if (pc->len == 0) { + break; + } else if (*pc->p == '[' && blevel == 0) { + level++; + } else if (*pc->p == ']' && blevel == 0) { + level--; + if (!level) break; + } else if (*pc->p == '\\') { + pc->p++; pc->len--; + } else if (*pc->p == '{') { + blevel++; + } else if (*pc->p == '}') { + if (blevel != 0) + blevel--; + } else if (*pc->p == '\n') + pc->linenr++; + pc->p++; pc->len--; + } + pc->tend = pc->p-1; + pc->tt = JIM_TT_CMD; + if (*pc->p == ']') { + pc->p++; pc->len--; + } + return JIM_OK; +} + +int JimParseVar(struct JimParserCtx *pc) +{ + int brace = 0, stop = 0, ttype = JIM_TT_VAR; + + pc->tstart = ++pc->p; pc->len--; /* skip the $ */ + pc->tline = pc->linenr; + if (*pc->p == '{') { + pc->tstart = ++pc->p; pc->len--; + brace = 1; + } + if (brace) { + while (!stop) { + if (*pc->p == '}' || pc->len == 0) { + stop = 1; + if (pc->len == 0) + continue; + } + else if (*pc->p == '\n') + pc->linenr++; + pc->p++; pc->len--; + } + if (pc->len == 0) + pc->tend = pc->p-1; + else + pc->tend = pc->p-2; + } else { + while (!stop) { + if (!((*pc->p >= 'a' && *pc->p <= 'z') || + (*pc->p >= 'A' && *pc->p <= 'Z') || + (*pc->p >= '0' && *pc->p <= '9') || *pc->p == '_')) + stop = 1; + else { + pc->p++; pc->len--; + } + } + /* Parse [dict get] syntax sugar. */ + if (*pc->p == '(') { + while (*pc->p != ')' && pc->len) { + pc->p++; pc->len--; + if (*pc->p == '\\' && pc->len >= 2) { + pc->p += 2; pc->len -= 2; + } + } + if (*pc->p != '\0') { + pc->p++; pc->len--; + } + ttype = JIM_TT_DICTSUGAR; + } + pc->tend = pc->p-1; + } + /* Check if we parsed just the '$' character. + * That's not a variable so an error is returned + * to tell the state machine to consider this '$' just + * a string. */ + if (pc->tstart == pc->p) { + pc->p--; pc->len++; + return JIM_ERR; + } + pc->tt = ttype; + return JIM_OK; +} + +int JimParseBrace(struct JimParserCtx *pc) +{ + int level = 1; + + pc->tstart = ++pc->p; pc->len--; + pc->tline = pc->linenr; + while (1) { + if (*pc->p == '\\' && pc->len >= 2) { + pc->p++; pc->len--; + if (*pc->p == '\n') + pc->linenr++; + } else if (*pc->p == '{') { + level++; + } else if (pc->len == 0 || *pc->p == '}') { + level--; + if (pc->len == 0 || level == 0) { + pc->tend = pc->p-1; + if (pc->len != 0) { + pc->p++; pc->len--; + } + pc->tt = JIM_TT_STR; + return JIM_OK; + } + } else if (*pc->p == '\n') { + pc->linenr++; + } + pc->p++; pc->len--; + } + return JIM_OK; /* unreached */ +} + +int JimParseStr(struct JimParserCtx *pc) +{ + int newword = (pc->tt == JIM_TT_SEP || pc->tt == JIM_TT_EOL || + pc->tt == JIM_TT_NONE || pc->tt == JIM_TT_STR); + if (newword && *pc->p == '{') { + return JimParseBrace(pc); + } else if (newword && *pc->p == '"') { + pc->state = JIM_PS_QUOTE; + pc->p++; pc->len--; + } + pc->tstart = pc->p; + pc->tline = pc->linenr; + while (1) { + if (pc->len == 0) { + pc->tend = pc->p-1; + pc->tt = JIM_TT_ESC; + return JIM_OK; + } + switch(*pc->p) { + case '\\': + if (pc->state == JIM_PS_DEF && + *(pc->p+1) == '\n') { + pc->tend = pc->p-1; + pc->tt = JIM_TT_ESC; + return JIM_OK; + } + if (pc->len >= 2) { + pc->p++; pc->len--; + } + break; + case '$': + case '[': + pc->tend = pc->p-1; + pc->tt = JIM_TT_ESC; + return JIM_OK; + case ' ': + case '\t': + case '\n': + case '\r': + case ';': + if (pc->state == JIM_PS_DEF) { + pc->tend = pc->p-1; + pc->tt = JIM_TT_ESC; + return JIM_OK; + } else if (*pc->p == '\n') { + pc->linenr++; + } + break; + case '"': + if (pc->state == JIM_PS_QUOTE) { + pc->tend = pc->p-1; + pc->tt = JIM_TT_ESC; + pc->p++; pc->len--; + pc->state = JIM_PS_DEF; + return JIM_OK; + } + break; + } + pc->p++; pc->len--; + } + return JIM_OK; /* unreached */ +} + +int JimParseComment(struct JimParserCtx *pc) +{ + while (*pc->p) { + if (*pc->p == '\n') { + pc->linenr++; + if (*(pc->p-1) != '\\') { + pc->p++; pc->len--; + return JIM_OK; + } + } + pc->p++; pc->len--; + } + return JIM_OK; +} + +/* xdigitval and odigitval are helper functions for JimParserGetToken() */ +static int xdigitval(int c) +{ + if (c >= '0' && c <= '9') return c-'0'; + if (c >= 'a' && c <= 'f') return c-'a'+10; + if (c >= 'A' && c <= 'F') return c-'A'+10; + return -1; +} + +static int odigitval(int c) +{ + if (c >= '0' && c <= '7') return c-'0'; + return -1; +} + +/* Perform Tcl escape substitution of 's', storing the result + * string into 'dest'. The escaped string is guaranteed to + * be the same length or shorted than the source string. + * Slen is the length of the string at 's', if it's -1 the string + * length will be calculated by the function. + * + * The function returns the length of the resulting string. */ +static int JimEscape(char *dest, const char *s, int slen) +{ + char *p = dest; + int i, len; + + if (slen == -1) + slen = strlen(s); + + for (i = 0; i < slen; i++) { + switch(s[i]) { + case '\\': + switch(s[i+1]) { + case 'a': *p++ = 0x7; i++; break; + case 'b': *p++ = 0x8; i++; break; + case 'f': *p++ = 0xc; i++; break; + case 'n': *p++ = 0xa; i++; break; + case 'r': *p++ = 0xd; i++; break; + case 't': *p++ = 0x9; i++; break; + case 'v': *p++ = 0xb; i++; break; + case '\0': *p++ = '\\'; i++; break; + case '\n': *p++ = ' '; i++; break; + default: + if (s[i+1] == 'x') { + int val = 0; + int c = xdigitval(s[i+2]); + if (c == -1) { + *p++ = 'x'; + i++; + break; + } + val = c; + c = xdigitval(s[i+3]); + if (c == -1) { + *p++ = val; + i += 2; + break; + } + val = (val*16)+c; + *p++ = val; + i += 3; + break; + } else if (s[i+1] >= '0' && s[i+1] <= '7') + { + int val = 0; + int c = odigitval(s[i+1]); + val = c; + c = odigitval(s[i+2]); + if (c == -1) { + *p++ = val; + i ++; + break; + } + val = (val*8)+c; + c = odigitval(s[i+3]); + if (c == -1) { + *p++ = val; + i += 2; + break; + } + val = (val*8)+c; + *p++ = val; + i += 3; + } else { + *p++ = s[i+1]; + i++; + } + break; + } + break; + default: + *p++ = s[i]; + break; + } + } + len = p-dest; + *p++ = '\0'; + return len; +} + +/* Returns a dynamically allocated copy of the current token in the + * parser context. The function perform conversion of escapes if + * the token is of type JIM_TT_ESC. + * + * Note that after the conversion, tokens that are grouped with + * braces in the source code, are always recognizable from the + * identical string obtained in a different way from the type. + * + * For exmple the string: + * + * {expand}$a + * + * will return as first token "expand", of type JIM_TT_STR + * + * While the string: + * + * expand$a + * + * will return as first token "expand", of type JIM_TT_ESC + */ +char *JimParserGetToken(struct JimParserCtx *pc, + int *lenPtr, int *typePtr, int *linePtr) +{ + const char *start, *end; + char *token; + int len; + + start = JimParserTstart(pc); + end = JimParserTend(pc); + if (start > end) { + if (lenPtr) *lenPtr = 0; + if (typePtr) *typePtr = JimParserTtype(pc); + if (linePtr) *linePtr = JimParserTline(pc); + token = Jim_Alloc(1); + token[0] = '\0'; + return token; + } + len = (end-start)+1; + token = Jim_Alloc(len+1); + if (JimParserTtype(pc) != JIM_TT_ESC) { + /* No escape conversion needed? Just copy it. */ + memcpy(token, start, len); + token[len] = '\0'; + } else { + /* Else convert the escape chars. */ + len = JimEscape(token, start, len); + } + if (lenPtr) *lenPtr = len; + if (typePtr) *typePtr = JimParserTtype(pc); + if (linePtr) *linePtr = JimParserTline(pc); + return token; +} + +/* The following functin is not really part of the parsing engine of Jim, + * but it somewhat related. Given an string and its length, it tries + * to guess if the script is complete or there are instead " " or { } + * open and not completed. This is useful for interactive shells + * implementation and for [info complete]. + * + * If 'stateCharPtr' != NULL, the function stores ' ' on complete script, + * '{' on scripts incomplete missing one or more '}' to be balanced. + * '"' on scripts incomplete missing a '"' char. + * + * If the script is complete, 1 is returned, otherwise 0. */ +int Jim_ScriptIsComplete(const char *s, int len, char *stateCharPtr) +{ + int level = 0; + int state = ' '; + + while(len) { + switch (*s) { + case '\\': + if (len > 1) + s++; + break; + case '"': + if (state == ' ') { + state = '"'; + } else if (state == '"') { + state = ' '; + } + break; + case '{': + if (state == '{') { + level++; + } else if (state == ' ') { + state = '{'; + level++; + } + break; + case '}': + if (state == '{') { + level--; + if (level == 0) + state = ' '; + } + break; + } + s++; + len--; + } + if (stateCharPtr) + *stateCharPtr = state; + return state == ' '; +} + +/* ----------------------------------------------------------------------------- + * Tcl Lists parsing + * ---------------------------------------------------------------------------*/ +static int JimParseListSep(struct JimParserCtx *pc); +static int JimParseListStr(struct JimParserCtx *pc); + +int JimParseList(struct JimParserCtx *pc) +{ + if (pc->len == 0) { + pc->tstart = pc->tend = pc->p; + pc->tline = pc->linenr; + pc->tt = JIM_TT_EOL; + pc->eof = 1; + return JIM_OK; + } + switch(*pc->p) { + case ' ': + case '\n': + case '\t': + case '\r': + if (pc->state == JIM_PS_DEF) + return JimParseListSep(pc); + else + return JimParseListStr(pc); + break; + default: + return JimParseListStr(pc); + break; + } + return JIM_OK; +} + +int JimParseListSep(struct JimParserCtx *pc) +{ + pc->tstart = pc->p; + pc->tline = pc->linenr; + while (*pc->p == ' ' || *pc->p == '\t' || *pc->p == '\r' || *pc->p == '\n') + { + pc->p++; pc->len--; + } + pc->tend = pc->p-1; + pc->tt = JIM_TT_SEP; + return JIM_OK; +} + +int JimParseListStr(struct JimParserCtx *pc) +{ + int newword = (pc->tt == JIM_TT_SEP || pc->tt == JIM_TT_EOL || + pc->tt == JIM_TT_NONE); + if (newword && *pc->p == '{') { + return JimParseBrace(pc); + } else if (newword && *pc->p == '"') { + pc->state = JIM_PS_QUOTE; + pc->p++; pc->len--; + } + pc->tstart = pc->p; + pc->tline = pc->linenr; + while (1) { + if (pc->len == 0) { + pc->tend = pc->p-1; + pc->tt = JIM_TT_ESC; + return JIM_OK; + } + switch(*pc->p) { + case '\\': + pc->p++; pc->len--; + break; + case ' ': + case '\t': + case '\n': + case '\r': + if (pc->state == JIM_PS_DEF) { + pc->tend = pc->p-1; + pc->tt = JIM_TT_ESC; + return JIM_OK; + } else if (*pc->p == '\n') { + pc->linenr++; + } + break; + case '"': + if (pc->state == JIM_PS_QUOTE) { + pc->tend = pc->p-1; + pc->tt = JIM_TT_ESC; + pc->p++; pc->len--; + pc->state = JIM_PS_DEF; + return JIM_OK; + } + break; + } + pc->p++; pc->len--; + } + return JIM_OK; /* unreached */ +} + +/* ----------------------------------------------------------------------------- + * Jim_Obj related functions + * ---------------------------------------------------------------------------*/ + +/* Return a new initialized object. */ +Jim_Obj *Jim_NewObj(Jim_Interp *interp) +{ + Jim_Obj *objPtr; + + /* -- Check if there are objects in the free list -- */ + if (interp->freeList != NULL) { + /* -- Unlink the object from the free list -- */ + objPtr = interp->freeList; + interp->freeList = objPtr->nextObjPtr; + } else { + /* -- No ready to use objects: allocate a new one -- */ + objPtr = Jim_Alloc(sizeof(*objPtr)); + } + + /* Object is returned with refCount of 0. Every + * kind of GC implemented should take care to don't try + * to scan objects with refCount == 0. */ + objPtr->refCount = 0; + /* All the other fields are left not initialized to save time. + * The caller will probably want set they to the right + * value anyway. */ + + /* -- Put the object into the live list -- */ + objPtr->prevObjPtr = NULL; + objPtr->nextObjPtr = interp->liveList; + if (interp->liveList) + interp->liveList->prevObjPtr = objPtr; + interp->liveList = objPtr; + + return objPtr; +} + +/* Free an object. Actually objects are never freed, but + * just moved to the free objects list, where they will be + * reused by Jim_NewObj(). */ +void Jim_FreeObj(Jim_Interp *interp, Jim_Obj *objPtr) +{ + /* Check if the object was already freed, panic. */ + if (objPtr->refCount != 0) { + Jim_Panic(interp,"!!!Object %p freed with bad refcount %d", objPtr, + objPtr->refCount); + } + /* Free the internal representation */ + Jim_FreeIntRep(interp, objPtr); + /* Free the string representation */ + if (objPtr->bytes != NULL) { + if (objPtr->bytes != JimEmptyStringRep) + Jim_Free(objPtr->bytes); + } + /* Unlink the object from the live objects list */ + if (objPtr->prevObjPtr) + objPtr->prevObjPtr->nextObjPtr = objPtr->nextObjPtr; + if (objPtr->nextObjPtr) + objPtr->nextObjPtr->prevObjPtr = objPtr->prevObjPtr; + if (interp->liveList == objPtr) + interp->liveList = objPtr->nextObjPtr; + /* Link the object into the free objects list */ + objPtr->prevObjPtr = NULL; + objPtr->nextObjPtr = interp->freeList; + if (interp->freeList) + interp->freeList->prevObjPtr = objPtr; + interp->freeList = objPtr; + objPtr->refCount = -1; +} + +/* Invalidate the string representation of an object. */ +void Jim_InvalidateStringRep(Jim_Obj *objPtr) +{ + if (objPtr->bytes != NULL) { + if (objPtr->bytes != JimEmptyStringRep) + Jim_Free(objPtr->bytes); + } + objPtr->bytes = NULL; +} + +#define Jim_SetStringRep(o, b, l) \ + do { (o)->bytes = b; (o)->length = l; } while (0) + +/* Set the initial string representation for an object. + * Does not try to free an old one. */ +void Jim_InitStringRep(Jim_Obj *objPtr, const char *bytes, int length) +{ + if (length == 0) { + objPtr->bytes = JimEmptyStringRep; + objPtr->length = 0; + } else { + objPtr->bytes = Jim_Alloc(length+1); + objPtr->length = length; + memcpy(objPtr->bytes, bytes, length); + objPtr->bytes[length] = '\0'; + } +} + +/* Duplicate an object. The returned object has refcount = 0. */ +Jim_Obj *Jim_DuplicateObj(Jim_Interp *interp, Jim_Obj *objPtr) +{ + Jim_Obj *dupPtr; + + dupPtr = Jim_NewObj(interp); + if (objPtr->bytes == NULL) { + /* Object does not have a valid string representation. */ + dupPtr->bytes = NULL; + } else { + Jim_InitStringRep(dupPtr, objPtr->bytes, objPtr->length); + } + if (objPtr->typePtr != NULL) { + if (objPtr->typePtr->dupIntRepProc == NULL) { + dupPtr->internalRep = objPtr->internalRep; + } else { + objPtr->typePtr->dupIntRepProc(interp, objPtr, dupPtr); + } + dupPtr->typePtr = objPtr->typePtr; + } else { + dupPtr->typePtr = NULL; + } + return dupPtr; +} + +/* Return the string representation for objPtr. If the object + * string representation is invalid, calls the method to create + * a new one starting from the internal representation of the object. */ +const char *Jim_GetString(Jim_Obj *objPtr, int *lenPtr) +{ + if (objPtr->bytes == NULL) { + /* Invalid string repr. Generate it. */ + if (objPtr->typePtr->updateStringProc == NULL) { + Jim_Panic(NULL,"UpdataStringProc called against '%s' type.", + objPtr->typePtr->name); + } + objPtr->typePtr->updateStringProc(objPtr); + } + if (lenPtr) + *lenPtr = objPtr->length; + return objPtr->bytes; +} + +/* Just returns the length of the object's string rep */ +int Jim_Length(Jim_Obj *objPtr) +{ + int len; + + Jim_GetString(objPtr, &len); + return len; +} + +/* ----------------------------------------------------------------------------- + * String Object + * ---------------------------------------------------------------------------*/ +static void DupStringInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr); +static int SetStringFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr); + +static Jim_ObjType stringObjType = { + "string", + NULL, + DupStringInternalRep, + NULL, + JIM_TYPE_REFERENCES, +}; + +void DupStringInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) +{ + JIM_NOTUSED(interp); + + /* This is a bit subtle: the only caller of this function + * should be Jim_DuplicateObj(), that will copy the + * string representaion. After the copy, the duplicated + * object will not have more room in teh buffer than + * srcPtr->length bytes. So we just set it to length. */ + dupPtr->internalRep.strValue.maxLength = srcPtr->length; +} + +int SetStringFromAny(Jim_Interp *interp, Jim_Obj *objPtr) +{ + /* Get a fresh string representation. */ + (void) Jim_GetString(objPtr, NULL); + /* Free any other internal representation. */ + Jim_FreeIntRep(interp, objPtr); + /* Set it as string, i.e. just set the maxLength field. */ + objPtr->typePtr = &stringObjType; + objPtr->internalRep.strValue.maxLength = objPtr->length; + return JIM_OK; +} + +Jim_Obj *Jim_NewStringObj(Jim_Interp *interp, const char *s, int len) +{ + Jim_Obj *objPtr = Jim_NewObj(interp); + + if (len == -1) + len = strlen(s); + /* Alloc/Set the string rep. */ + if (len == 0) { + objPtr->bytes = JimEmptyStringRep; + objPtr->length = 0; + } else { + objPtr->bytes = Jim_Alloc(len+1); + objPtr->length = len; + memcpy(objPtr->bytes, s, len); + objPtr->bytes[len] = '\0'; + } + + /* No typePtr field for the vanilla string object. */ + objPtr->typePtr = NULL; + return objPtr; +} + +/* This version does not try to duplicate the 's' pointer, but + * use it directly. */ +Jim_Obj *Jim_NewStringObjNoAlloc(Jim_Interp *interp, char *s, int len) +{ + Jim_Obj *objPtr = Jim_NewObj(interp); + + if (len == -1) + len = strlen(s); + Jim_SetStringRep(objPtr, s, len); + objPtr->typePtr = NULL; + return objPtr; +} + +/* Low-level string append. Use it only against objects + * of type "string". */ +void StringAppendString(Jim_Obj *objPtr, const char *str, int len) +{ + int needlen; + + if (len == -1) + len = strlen(str); + needlen = objPtr->length + len; + if (objPtr->internalRep.strValue.maxLength < needlen || + objPtr->internalRep.strValue.maxLength == 0) { + if (objPtr->bytes == JimEmptyStringRep) { + objPtr->bytes = Jim_Alloc((needlen*2)+1); + } else { + objPtr->bytes = Jim_Realloc(objPtr->bytes, (needlen*2)+1); + } + objPtr->internalRep.strValue.maxLength = needlen*2; + } + memcpy(objPtr->bytes + objPtr->length, str, len); + objPtr->bytes[objPtr->length+len] = '\0'; + objPtr->length += len; +} + +/* Low-level wrapper to append an object. */ +void StringAppendObj(Jim_Obj *objPtr, Jim_Obj *appendObjPtr) +{ + int len; + const char *str; + + str = Jim_GetString(appendObjPtr, &len); + StringAppendString(objPtr, str, len); +} + +/* Higher level API to append strings to objects. */ +void Jim_AppendString(Jim_Interp *interp, Jim_Obj *objPtr, const char *str, + int len) +{ + if (Jim_IsShared(objPtr)) + Jim_Panic(interp,"Jim_AppendString called with shared object"); + if (objPtr->typePtr != &stringObjType) + SetStringFromAny(interp, objPtr); + StringAppendString(objPtr, str, len); +} + +void Jim_AppendObj(Jim_Interp *interp, Jim_Obj *objPtr, + Jim_Obj *appendObjPtr) +{ + int len; + const char *str; + + str = Jim_GetString(appendObjPtr, &len); + Jim_AppendString(interp, objPtr, str, len); +} + +void Jim_AppendStrings(Jim_Interp *interp, Jim_Obj *objPtr, ...) +{ + va_list ap; + + if (objPtr->typePtr != &stringObjType) + SetStringFromAny(interp, objPtr); + va_start(ap, objPtr); + while (1) { + char *s = va_arg(ap, char*); + + if (s == NULL) break; + Jim_AppendString(interp, objPtr, s, -1); + } + va_end(ap); +} + +int Jim_StringEqObj(Jim_Obj *aObjPtr, Jim_Obj *bObjPtr, int nocase) +{ + const char *aStr, *bStr; + int aLen, bLen, i; + + if (aObjPtr == bObjPtr) return 1; + aStr = Jim_GetString(aObjPtr, &aLen); + bStr = Jim_GetString(bObjPtr, &bLen); + if (aLen != bLen) return 0; + if (nocase == 0) + return memcmp(aStr, bStr, aLen) == 0; + for (i = 0; i < aLen; i++) { + if (tolower((int)aStr[i]) != tolower((int)bStr[i])) + return 0; + } + return 1; +} + +int Jim_StringMatchObj(Jim_Obj *patternObjPtr, Jim_Obj *objPtr, + int nocase) +{ + const char *pattern, *string; + int patternLen, stringLen; + + pattern = Jim_GetString(patternObjPtr, &patternLen); + string = Jim_GetString(objPtr, &stringLen); + return JimStringMatch(pattern, patternLen, string, stringLen, nocase); +} + +int Jim_StringCompareObj(Jim_Obj *firstObjPtr, + Jim_Obj *secondObjPtr, int nocase) +{ + const char *s1, *s2; + int l1, l2; + + s1 = Jim_GetString(firstObjPtr, &l1); + s2 = Jim_GetString(secondObjPtr, &l2); + return JimStringCompare(s1, l1, s2, l2, nocase); +} + +/* Convert a range, as returned by Jim_GetRange(), into + * an absolute index into an object of the specified length. + * This function may return negative values, or values + * bigger or equal to the length of the list if the index + * is out of range. */ +static int JimRelToAbsIndex(int len, int index) +{ + if (index < 0) + return len + index; + return index; +} + +/* Convert a pair of index as normalize by JimRelToAbsIndex(), + * into a range stored in *firstPtr, *lastPtr, *rangeLenPtr, suitable + * for implementation of commands like [string range] and [lrange]. + * + * The resulting range is guaranteed to address valid elements of + * the structure. */ +static void JimRelToAbsRange(int len, int first, int last, + int *firstPtr, int *lastPtr, int *rangeLenPtr) +{ + int rangeLen; + + if (first > last) { + rangeLen = 0; + } else { + rangeLen = last-first+1; + if (rangeLen) { + if (first < 0) { + rangeLen += first; + first = 0; + } + if (last >= len) { + rangeLen -= (last-(len-1)); + last = len-1; + } + } + } + if (rangeLen < 0) rangeLen = 0; + + *firstPtr = first; + *lastPtr = last; + *rangeLenPtr = rangeLen; +} + +Jim_Obj *Jim_StringRangeObj(Jim_Interp *interp, + Jim_Obj *strObjPtr, Jim_Obj *firstObjPtr, Jim_Obj *lastObjPtr) +{ + int first, last; + const char *str; + int len, rangeLen; + + if (Jim_GetIndex(interp, firstObjPtr, &first) != JIM_OK || + Jim_GetIndex(interp, lastObjPtr, &last) != JIM_OK) + return NULL; + str = Jim_GetString(strObjPtr, &len); + first = JimRelToAbsIndex(len, first); + last = JimRelToAbsIndex(len, last); + JimRelToAbsRange(len, first, last, &first, &last, &rangeLen); + return Jim_NewStringObj(interp, str+first, rangeLen); +} + +static Jim_Obj *JimStringToLower(Jim_Interp *interp, Jim_Obj *strObjPtr) +{ + char *buf = Jim_Alloc(strObjPtr->length+1); + int i; + + memcpy(buf, strObjPtr->bytes, strObjPtr->length+1); + for (i = 0; i < strObjPtr->length; i++) + buf[i] = tolower(buf[i]); + return Jim_NewStringObjNoAlloc(interp, buf, strObjPtr->length); +} + +static Jim_Obj *JimStringToUpper(Jim_Interp *interp, Jim_Obj *strObjPtr) +{ + char *buf = Jim_Alloc(strObjPtr->length+1); + int i; + + memcpy(buf, strObjPtr->bytes, strObjPtr->length+1); + for (i = 0; i < strObjPtr->length; i++) + buf[i] = toupper(buf[i]); + return Jim_NewStringObjNoAlloc(interp, buf, strObjPtr->length); +} + +/* This is the core of the [format] command. + * TODO: Export it, make it real... for now only %s and %% + * specifiers supported. */ +Jim_Obj *Jim_FormatString(Jim_Interp *interp, Jim_Obj *fmtObjPtr, + int objc, Jim_Obj *const *objv) +{ + const char *fmt; + int fmtLen; + Jim_Obj *resObjPtr; + + fmt = Jim_GetString(fmtObjPtr, &fmtLen); + resObjPtr = Jim_NewStringObj(interp, "", 0); + while (fmtLen) { + const char *p = fmt; + char spec[2], c; + jim_wide wideValue; + + while (*fmt != '%' && fmtLen) { + fmt++; fmtLen--; + } + Jim_AppendString(interp, resObjPtr, p, fmt-p); + if (fmtLen == 0) + break; + fmt++; fmtLen--; /* skip '%' */ + if (*fmt != '%') { + if (objc == 0) { + Jim_FreeNewObj(interp, resObjPtr); + Jim_SetResultString(interp, + "not enough arguments for all format specifiers", -1); + return NULL; + } else { + objc--; + } + } + switch(*fmt) { + case 's': + Jim_AppendObj(interp, resObjPtr, objv[0]); + objv++; + break; + case 'c': + if (Jim_GetWide(interp, objv[0], &wideValue) == JIM_ERR) { + Jim_FreeNewObj(interp, resObjPtr); + return NULL; + } + c = (char) wideValue; + Jim_AppendString(interp, resObjPtr, &c, 1); + break; + case '%': + Jim_AppendString(interp, resObjPtr, "%" , 1); + break; + default: + spec[0] = *fmt; spec[1] = '\0'; + Jim_FreeNewObj(interp, resObjPtr); + Jim_SetResult(interp, Jim_NewEmptyStringObj(interp)); + Jim_AppendStrings(interp, Jim_GetResult(interp), + "bad field specifier \"", spec, "\"", NULL); + return NULL; + } + fmt++; + fmtLen--; + } + return resObjPtr; +} + +/* ----------------------------------------------------------------------------- + * Compared String Object + * ---------------------------------------------------------------------------*/ + +/* This is strange object that allows to compare a C literal string + * with a Jim object in very short time if the same comparison is done + * multiple times. For example every time the [if] command is executed, + * Jim has to check if a given argument is "else". This comparions if + * the code has no errors are true most of the times, so we can cache + * inside the object the pointer of the string of the last matching + * comparison. Because most C compilers perform literal sharing, + * so that: char *x = "foo", char *y = "foo", will lead to x == y, + * this works pretty well even if comparisons are at different places + * inside the C code. */ + +static Jim_ObjType comparedStringObjType = { + "compared-string", + NULL, + NULL, + NULL, + JIM_TYPE_REFERENCES, +}; + +/* The only way this object is exposed to the API is via the following + * function. Returns true if the string and the object string repr. + * are the same, otherwise zero is returned. + * + * Note: this isn't binary safe, but it hardly needs to be.*/ +int Jim_CompareStringImmediate(Jim_Interp *interp, Jim_Obj *objPtr, + const char *str) +{ + if (objPtr->typePtr == &comparedStringObjType && + objPtr->internalRep.ptr == str) + return 1; + else { + const char *objStr = Jim_GetString(objPtr, NULL); + if (strcmp(str, objStr) != 0) return 0; + if (objPtr->typePtr != &comparedStringObjType) { + Jim_FreeIntRep(interp, objPtr); + objPtr->typePtr = &comparedStringObjType; + } + objPtr->internalRep.ptr = (char*)str; /*ATTENTION: const cast */ + return 1; + } +} + +int qsortCompareStringPointers(const void *a, const void *b) +{ + char * const *sa = (char * const *)a; + char * const *sb = (char * const *)b; + return strcmp(*sa, *sb); +} + +int Jim_GetEnum(Jim_Interp *interp, Jim_Obj *objPtr, + const char **tablePtr, int *indexPtr, const char *name, int flags) +{ + const char **entryPtr = NULL; + char **tablePtrSorted; + int i, count = 0; + + *indexPtr = -1; + for (entryPtr = tablePtr, i = 0; *entryPtr != NULL; entryPtr++, i++) { + if (Jim_CompareStringImmediate(interp, objPtr, *entryPtr)) { + *indexPtr = i; + return JIM_OK; + } + count++; /* If nothing matches, this will reach the len of tablePtr */ + } + if (flags & JIM_ERRMSG) { + if (name == NULL) + name = "option"; + Jim_SetResult(interp, Jim_NewEmptyStringObj(interp)); + Jim_AppendStrings(interp, Jim_GetResult(interp), + "bad ", name, " \"", Jim_GetString(objPtr, NULL), "\": must be one of ", + NULL); + tablePtrSorted = Jim_Alloc(sizeof(char*)*count); + memcpy(tablePtrSorted, tablePtr, sizeof(char*)*count); + qsort(tablePtrSorted, count, sizeof(char*), qsortCompareStringPointers); + for (i = 0; i < count; i++) { + if (i+1 == count && count > 1) + Jim_AppendString(interp, Jim_GetResult(interp), "or ", -1); + Jim_AppendString(interp, Jim_GetResult(interp), + tablePtrSorted[i], -1); + if (i+1 != count) + Jim_AppendString(interp, Jim_GetResult(interp), ", ", -1); + } + Jim_Free(tablePtrSorted); + } + return JIM_ERR; +} + +/* ----------------------------------------------------------------------------- + * Source Object + * + * This object is just a string from the language point of view, but + * in the internal representation it contains the filename and line number + * where this given token was read. This information is used by + * Jim_EvalObj() if the object passed happens to be of type "source". + * + * This allows to propagate the information about line numbers and file + * names and give error messages with absolute line numbers. + * + * Note that this object uses shared strings for filenames, and the + * pointer to the filename together with the line number is taken into + * the space for the "inline" internal represenation of the Jim_Object, + * so there is almost memory zero-overhead. + * + * Also the object will be converted to something else if the given + * token it represents in the source file is not something to be + * evaluated (not a script), and will be specialized in some other way, + * so the time overhead is alzo null. + * ---------------------------------------------------------------------------*/ + +static void FreeSourceInternalRep(Jim_Interp *interp, Jim_Obj *objPtr); +static void DupSourceInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr); + +static Jim_ObjType sourceObjType = { + "source", + FreeSourceInternalRep, + DupSourceInternalRep, + NULL, + JIM_TYPE_REFERENCES, +}; + +void FreeSourceInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) +{ + Jim_ReleaseSharedString(interp, + objPtr->internalRep.sourceValue.fileName); +} + +void DupSourceInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) +{ + dupPtr->internalRep.sourceValue.fileName = + Jim_GetSharedString(interp, + srcPtr->internalRep.sourceValue.fileName); + dupPtr->internalRep.sourceValue.lineNumber = + dupPtr->internalRep.sourceValue.lineNumber; + dupPtr->typePtr = &sourceObjType; +} + +static void JimSetSourceInfo(Jim_Interp *interp, Jim_Obj *objPtr, + const char *fileName, int lineNumber) +{ + if (Jim_IsShared(objPtr)) + Jim_Panic(interp,"JimSetSourceInfo called with shared object"); + if (objPtr->typePtr != NULL) + Jim_Panic(interp,"JimSetSourceInfo called with typePtr != NULL"); + objPtr->internalRep.sourceValue.fileName = + Jim_GetSharedString(interp, fileName); + objPtr->internalRep.sourceValue.lineNumber = lineNumber; + objPtr->typePtr = &sourceObjType; +} + +/* ----------------------------------------------------------------------------- + * Script Object + * ---------------------------------------------------------------------------*/ + +#define JIM_CMDSTRUCT_EXPAND -1 + +static void FreeScriptInternalRep(Jim_Interp *interp, Jim_Obj *objPtr); +static void DupScriptInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr); +static int SetScriptFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr); + +static Jim_ObjType scriptObjType = { + "script", + FreeScriptInternalRep, + DupScriptInternalRep, + NULL, + JIM_TYPE_REFERENCES, +}; + +/* The ScriptToken structure represents every token into a scriptObj. + * Every token contains an associated Jim_Obj that can be specialized + * by commands operating on it. */ +typedef struct ScriptToken { + int type; + Jim_Obj *objPtr; + int linenr; +} ScriptToken; + +/* This is the script object internal representation. An array of + * ScriptToken structures, with an associated command structure array. + * The command structure is a pre-computed representation of the + * command length and arguments structure as a simple liner array + * of integers. + * + * For example the script: + * + * puts hello + * set $i $x$y [foo]BAR + * + * will produce a ScriptObj with the following Tokens: + * + * ESC puts + * SEP + * ESC hello + * EOL + * ESC set + * EOL + * VAR i + * SEP + * VAR x + * VAR y + * SEP + * CMD foo + * ESC BAR + * EOL + * + * This is a description of the tokens, separators, and of lines. + * The command structure instead represents the number of arguments + * of every command, followed by the tokens of which every argument + * is composed. So for the example script, the cmdstruct array will + * contain: + * + * 2 1 1 4 1 1 2 2 + * + * Because "puts hello" has two args (2), composed of single tokens (1 1) + * While "set $i $x$y [foo]BAR" has four (4) args, the first two + * composed of single tokens (1 1) and the last two of double tokens + * (2 2). + * + * The precomputation of the command structure makes Jim_Eval() faster, + * and simpler because there aren't dynamic lengths / allocations. + * + * -- {expand} handling -- + * + * Expand is handled in a special way. When a command + * contains at least an argument with the {expand} prefix, + * the command structure presents a -1 before the integer + * describing the number of arguments. This is used in order + * to send the command exection to a different path in case + * of {expand} and guarantee a fast path for the more common + * case. Also, the integers describing the number of tokens + * are expressed with negative sign, to allow for fast check + * of what's an {expand}-prefixed argument and what not. + * + * For example the command: + * + * list {expand}{1 2} + * + * Will produce the following cmdstruct array: + * + * -1 2 1 -2 + * + * -- the substFlags field of the structure -- + * + * The scriptObj structure is used to represent both "script" objects + * and "subst" objects. In the second case, the cmdStruct related + * fields are not used at all, but there is an additional field used + * that is 'substFlags': this represents the flags used to turn + * the string into the intenral representation used to perform the + * substitution. If this flags are not what the application requires + * the scriptObj is created again. For example the script: + * + * subst -nocommands $string + * subst -novariables $string + * + * Will recreate the internal representation of the $string object + * two times. + */ +typedef struct ScriptObj { + int len; /* Length as number of tokens. */ + int commands; /* number of top-level commands in script. */ + ScriptToken *token; /* Tokens array. */ + int *cmdStruct; /* commands structure */ + int csLen; /* length of the cmdStruct array. */ + int substFlags; /* flags used for the compilation of "subst" objects */ + int inUse; /* Used to share a ScriptObj. Currently + only used by Jim_EvalObj() as protection against + shimmering of the currently evaluated object. */ + char *fileName; +} ScriptObj; + +void FreeScriptInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) +{ + int i; + struct ScriptObj *script = (void*) objPtr->internalRep.ptr; + + script->inUse--; + if (script->inUse != 0) return; + for (i = 0; i < script->len; i++) { + if (script->token[i].objPtr != NULL) + Jim_DecrRefCount(interp, script->token[i].objPtr); + } + Jim_Free(script->token); + Jim_Free(script->cmdStruct); + Jim_Free(script->fileName); + Jim_Free(script); +} + +void DupScriptInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) +{ + JIM_NOTUSED(interp); + JIM_NOTUSED(srcPtr); + + /* Just returns an simple string. */ + dupPtr->typePtr = NULL; +} + +/* Add a new token to the internal repr of a script object */ +static void ScriptObjAddToken(Jim_Interp *interp, struct ScriptObj *script, + char *strtoken, int len, int type, char *filename, int linenr) +{ + int prevtype; + struct ScriptToken *token; + + prevtype = (script->len == 0) ? JIM_TT_EOL : \ + script->token[script->len-1].type; + /* Skip tokens without meaning, like words separators + * following a word separator or an end of command and + * so on. */ + if (prevtype == JIM_TT_EOL) { + if (type == JIM_TT_EOL || type == JIM_TT_SEP) { + Jim_Free(strtoken); + return; + } + } else if (prevtype == JIM_TT_SEP) { + if (type == JIM_TT_SEP) { + Jim_Free(strtoken); + return; + } else if (type == JIM_TT_EOL) { + /* If an EOL is following by a SEP, drop the previous + * separator. */ + script->len--; + Jim_DecrRefCount(interp, script->token[script->len].objPtr); + } + } else if (prevtype != JIM_TT_EOL && prevtype != JIM_TT_SEP && + type == JIM_TT_ESC && len == 0) + { + /* Don't add empty tokens used in interpolation */ + Jim_Free(strtoken); + return; + } + /* Make space for a new istruction */ + script->len++; + script->token = Jim_Realloc(script->token, + sizeof(ScriptToken)*script->len); + /* Initialize the new token */ + token = script->token+(script->len-1); + token->type = type; + /* Every object is intially as a string, but the + * internal type may be specialized during execution of the + * script. */ + token->objPtr = Jim_NewStringObjNoAlloc(interp, strtoken, len); + /* To add source info to SEP and EOL tokens is useless because + * they will never by called as arguments of Jim_EvalObj(). */ + if (filename && type != JIM_TT_SEP && type != JIM_TT_EOL) + JimSetSourceInfo(interp, token->objPtr, filename, linenr); + Jim_IncrRefCount(token->objPtr); + token->linenr = linenr; +} + +/* Add an integer into the command structure field of the script object. */ +static void ScriptObjAddInt(struct ScriptObj *script, int val) +{ + script->csLen++; + script->cmdStruct = Jim_Realloc(script->cmdStruct, + sizeof(int)*script->csLen); + script->cmdStruct[script->csLen-1] = val; +} + +/* Search a Jim_Obj contained in 'script' with the same stinrg repr. + * of objPtr. Search nested script objects recursively. */ +static Jim_Obj *ScriptSearchLiteral(Jim_Interp *interp, ScriptObj *script, + ScriptObj *scriptBarrier, Jim_Obj *objPtr) +{ + int i; + + for (i = 0; i < script->len; i++) { + if (script->token[i].objPtr != objPtr && + Jim_StringEqObj(script->token[i].objPtr, objPtr, 0)) { + return script->token[i].objPtr; + } + /* Enter recursively on scripts only if the object + * is not the same as the one we are searching for + * shared occurrences. */ + if (script->token[i].objPtr->typePtr == &scriptObjType && + script->token[i].objPtr != objPtr) { + Jim_Obj *foundObjPtr; + + ScriptObj *subScript = + script->token[i].objPtr->internalRep.ptr; + /* Don't recursively enter the script we are trying + * to make shared to avoid circular references. */ + if (subScript == scriptBarrier) continue; + if (subScript != script) { + foundObjPtr = + ScriptSearchLiteral(interp, subScript, + scriptBarrier, objPtr); + if (foundObjPtr != NULL) + return foundObjPtr; + } + } + } + return NULL; +} + +/* Share literals of a script recursively sharing sub-scripts literals. */ +static void ScriptShareLiterals(Jim_Interp *interp, ScriptObj *script, + ScriptObj *topLevelScript) +{ + int i, j; + + return; + /* Try to share with toplevel object. */ + if (topLevelScript != NULL) { + for (i = 0; i < script->len; i++) { + Jim_Obj *foundObjPtr; + char *str = script->token[i].objPtr->bytes; + + if (script->token[i].objPtr->refCount != 1) continue; + if (script->token[i].objPtr->typePtr == &scriptObjType) continue; + if (strchr(str, ' ') || strchr(str, '\n')) continue; + foundObjPtr = ScriptSearchLiteral(interp, + topLevelScript, + script, /* barrier */ + script->token[i].objPtr); + if (foundObjPtr != NULL) { + Jim_IncrRefCount(foundObjPtr); + Jim_DecrRefCount(interp, + script->token[i].objPtr); + script->token[i].objPtr = foundObjPtr; + } + } + } + /* Try to share locally */ + for (i = 0; i < script->len; i++) { + char *str = script->token[i].objPtr->bytes; + + if (script->token[i].objPtr->refCount != 1) continue; + if (strchr(str, ' ') || strchr(str, '\n')) continue; + for (j = 0; j < script->len; j++) { + if (script->token[i].objPtr != + script->token[j].objPtr && + Jim_StringEqObj(script->token[i].objPtr, + script->token[j].objPtr, 0)) + { + Jim_IncrRefCount(script->token[j].objPtr); + Jim_DecrRefCount(interp, + script->token[i].objPtr); + script->token[i].objPtr = + script->token[j].objPtr; + } + } + } +} + +/* This method takes the string representation of an object + * as a Tcl script, and generates the pre-parsed internal representation + * of the script. */ +int SetScriptFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr) +{ + int scriptTextLen; + const char *scriptText = Jim_GetString(objPtr, &scriptTextLen); + struct JimParserCtx parser; + struct ScriptObj *script = Jim_Alloc(sizeof(*script)); + ScriptToken *token; + int args, tokens, start, end, i; + int initialLineNumber; + int propagateSourceInfo = 0; + + script->len = 0; + script->csLen = 0; + script->commands = 0; + script->token = NULL; + script->cmdStruct = NULL; + script->inUse = 1; + /* Try to get information about filename / line number */ + if (objPtr->typePtr == &sourceObjType) { + script->fileName = + Jim_StrDup(objPtr->internalRep.sourceValue.fileName); + initialLineNumber = objPtr->internalRep.sourceValue.lineNumber; + propagateSourceInfo = 1; + } else { + script->fileName = Jim_StrDup("?"); + initialLineNumber = 1; + } + + JimParserInit(&parser, scriptText, scriptTextLen, initialLineNumber); + while(!JimParserEof(&parser)) { + char *token; + int len, type, linenr; + + JimParseScript(&parser); + token = JimParserGetToken(&parser, &len, &type, &linenr); + ScriptObjAddToken(interp, script, token, len, type, + propagateSourceInfo ? script->fileName : NULL, + linenr); + } + token = script->token; + + /* Compute the command structure array + * (see the ScriptObj struct definition for more info) */ + start = 0; /* Current command start token index */ + end = -1; /* Current command end token index */ + while (1) { + int expand = 0; /* expand flag. set to 1 on {expand} form. */ + int interpolation = 0; /* set to 1 if there is at least one + argument of the command obtained via + interpolation of more tokens. */ + /* Search for the end of command, while + * count the number of args. */ + start = ++end; + if (start >= script->len) break; + args = 1; /* Number of args in current command */ + while (token[end].type != JIM_TT_EOL) { + if (end == 0 || token[end-1].type == JIM_TT_SEP || + token[end-1].type == JIM_TT_EOL) + { + if (token[end].type == JIM_TT_STR && + token[end+1].type != JIM_TT_SEP && + token[end+1].type != JIM_TT_EOL && + (!strcmp(token[end].objPtr->bytes, "expand") || + !strcmp(token[end].objPtr->bytes, "*"))) + expand++; + } + if (token[end].type == JIM_TT_SEP) + args++; + end++; + } + interpolation = !((end-start+1) == args*2); + /* Add the 'number of arguments' info into cmdstruct. + * Negative value if there is list expansion involved. */ + if (expand) + ScriptObjAddInt(script, -1); + ScriptObjAddInt(script, args); + /* Now add info about the number of tokens. */ + tokens = 0; /* Number of tokens in current argument. */ + expand = 0; + for (i = start; i <= end; i++) { + if (token[i].type == JIM_TT_SEP || + token[i].type == JIM_TT_EOL) + { + if (tokens == 1 && expand) + expand = 0; + ScriptObjAddInt(script, + expand ? -tokens : tokens); + + expand = 0; + tokens = 0; + continue; + } else if (tokens == 0 && token[i].type == JIM_TT_STR && + (!strcmp(token[i].objPtr->bytes, "expand") || + !strcmp(token[i].objPtr->bytes, "*"))) + { + expand++; + } + tokens++; + } + } + /* Perform literal sharing, but only for objects that appear + * to be scripts written as literals inside the source code, + * and not computed at runtime. Literal sharing is a costly + * operation that should be done only against objects that + * are likely to require compilation only the first time, and + * then are executed multiple times. */ + if (propagateSourceInfo && interp->framePtr->procBodyObjPtr) { + Jim_Obj *bodyObjPtr = interp->framePtr->procBodyObjPtr; + if (bodyObjPtr->typePtr == &scriptObjType) { + ScriptObj *bodyScript = + bodyObjPtr->internalRep.ptr; + ScriptShareLiterals(interp, script, bodyScript); + } + } else if (propagateSourceInfo) { + ScriptShareLiterals(interp, script, NULL); + } + /* Free the old internal rep and set the new one. */ + Jim_FreeIntRep(interp, objPtr); + Jim_SetIntRepPtr(objPtr, script); + objPtr->typePtr = &scriptObjType; + return JIM_OK; +} + +ScriptObj *Jim_GetScript(Jim_Interp *interp, Jim_Obj *objPtr) +{ + if (objPtr->typePtr != &scriptObjType) { + SetScriptFromAny(interp, objPtr); + } + return (ScriptObj*) Jim_GetIntRepPtr(objPtr); +} + +/* ----------------------------------------------------------------------------- + * Commands + * ---------------------------------------------------------------------------*/ + +/* Commands HashTable Type. + * + * Keys are dynamic allocated strings, Values are Jim_Cmd structures. */ +static void Jim_CommandsHT_ValDestructor(void *interp, void *val) +{ + Jim_Cmd *cmdPtr = (void*) val; + + if (cmdPtr->cmdProc == NULL) { + Jim_DecrRefCount(interp, cmdPtr->argListObjPtr); + Jim_DecrRefCount(interp, cmdPtr->bodyObjPtr); + if (cmdPtr->staticVars) { + Jim_FreeHashTable(cmdPtr->staticVars); + Jim_Free(cmdPtr->staticVars); + } + } else if (cmdPtr->delProc != NULL) { + /* If it was a C coded command, call the delProc if any */ + cmdPtr->delProc(interp, cmdPtr->privData); + } + Jim_Free(val); +} + +static Jim_HashTableType JimCommandsHashTableType = { + JimStringCopyHTHashFunction, /* hash function */ + JimStringCopyHTKeyDup, /* key dup */ + NULL, /* val dup */ + JimStringCopyHTKeyCompare, /* key compare */ + JimStringCopyHTKeyDestructor, /* key destructor */ + Jim_CommandsHT_ValDestructor /* val destructor */ +}; + +/* ------------------------- Commands related functions --------------------- */ + +int Jim_CreateCommand(Jim_Interp *interp, const char *cmdName, + Jim_CmdProc cmdProc, void *privData, Jim_DelCmdProc delProc) +{ + Jim_HashEntry *he; + Jim_Cmd *cmdPtr; + + he = Jim_FindHashEntry(&interp->commands, cmdName); + if (he == NULL) { /* New command to create */ + cmdPtr = Jim_Alloc(sizeof(*cmdPtr)); + cmdPtr->cmdProc = cmdProc; + cmdPtr->privData = privData; + cmdPtr->delProc = delProc; + Jim_AddHashEntry(&interp->commands, cmdName, cmdPtr); + } else { + Jim_InterpIncrProcEpoch(interp); + /* Free the arglist/body objects if it was a Tcl procedure */ + cmdPtr = he->val; + if (cmdPtr->cmdProc == NULL) { + Jim_DecrRefCount(interp, cmdPtr->argListObjPtr); + Jim_DecrRefCount(interp, cmdPtr->bodyObjPtr); + if (cmdPtr->staticVars) { + Jim_FreeHashTable(cmdPtr->staticVars); + Jim_Free(cmdPtr->staticVars); + } + cmdPtr->staticVars = NULL; + } else if (cmdPtr->delProc != NULL) { + /* If it was a C coded command, call the delProc if any */ + cmdPtr->delProc(interp, cmdPtr->privData); + } + cmdPtr->cmdProc = cmdProc; + cmdPtr->privData = privData; + } + /* There is no need to increment the 'proc epoch' because + * creation of a new procedure can never affect existing + * cached commands. We don't do negative caching. */ + return JIM_OK; +} + +int Jim_CreateProcedure(Jim_Interp *interp, const char *cmdName, + Jim_Obj *argListObjPtr, Jim_Obj *staticsListObjPtr, Jim_Obj *bodyObjPtr, + int arityMin, int arityMax) +{ + Jim_Cmd *cmdPtr; + + cmdPtr = Jim_Alloc(sizeof(*cmdPtr)); + cmdPtr->cmdProc = NULL; /* Not a C coded command */ + cmdPtr->argListObjPtr = argListObjPtr; + cmdPtr->bodyObjPtr = bodyObjPtr; + Jim_IncrRefCount(argListObjPtr); + Jim_IncrRefCount(bodyObjPtr); + cmdPtr->arityMin = arityMin; + cmdPtr->arityMax = arityMax; + cmdPtr->staticVars = NULL; + + /* Create the statics hash table. */ + if (staticsListObjPtr) { + int len, i; + + Jim_ListLength(interp, staticsListObjPtr, &len); + if (len != 0) { + cmdPtr->staticVars = Jim_Alloc(sizeof(Jim_HashTable)); + Jim_InitHashTable(cmdPtr->staticVars, &JimVariablesHashTableType, + interp); + for (i = 0; i < len; i++) { + Jim_Obj *objPtr, *initObjPtr, *nameObjPtr; + Jim_Var *varPtr; + int subLen; + + Jim_ListIndex(interp, staticsListObjPtr, i, &objPtr, JIM_NONE); + /* Check if it's composed of two elements. */ + Jim_ListLength(interp, objPtr, &subLen); + if (subLen == 1 || subLen == 2) { + /* Try to get the variable value from the current + * environment. */ + Jim_ListIndex(interp, objPtr, 0, &nameObjPtr, JIM_NONE); + if (subLen == 1) { + initObjPtr = Jim_GetVariable(interp, nameObjPtr, + JIM_NONE); + if (initObjPtr == NULL) { + Jim_SetResult(interp, + Jim_NewEmptyStringObj(interp)); + Jim_AppendStrings(interp, Jim_GetResult(interp), + "variable for initialization of static \"", + Jim_GetString(nameObjPtr, NULL), + "\" not found in the local context", + NULL); + goto err; + } + } else { + Jim_ListIndex(interp, objPtr, 1, &initObjPtr, JIM_NONE); + } + varPtr = Jim_Alloc(sizeof(*varPtr)); + varPtr->objPtr = initObjPtr; + Jim_IncrRefCount(initObjPtr); + varPtr->linkFramePtr = NULL; + if (Jim_AddHashEntry(cmdPtr->staticVars, + Jim_GetString(nameObjPtr, NULL), + varPtr) != JIM_OK) + { + Jim_SetResult(interp, Jim_NewEmptyStringObj(interp)); + Jim_AppendStrings(interp, Jim_GetResult(interp), + "static variable name \"", + Jim_GetString(objPtr, NULL), "\"", + " duplicated in statics list", NULL); + Jim_DecrRefCount(interp, initObjPtr); + Jim_Free(varPtr); + goto err; + } + } else { + Jim_SetResult(interp, Jim_NewEmptyStringObj(interp)); + Jim_AppendStrings(interp, Jim_GetResult(interp), + "too many fields in static specifier \"", + objPtr, "\"", NULL); + goto err; + } + } + } + } + + /* Add the new command */ + + /* it may already exist, so we try to delete the old one */ + if (Jim_DeleteHashEntry(&interp->commands, cmdName) != JIM_ERR) { + /* There was an old procedure with the same name, this requires + * a 'proc epoch' update. */ + Jim_InterpIncrProcEpoch(interp); + } + /* If a procedure with the same name didn't existed there is no need + * to increment the 'proc epoch' because creation of a new procedure + * can never affect existing cached commands. We don't do + * negative caching. */ + Jim_AddHashEntry(&interp->commands, cmdName, cmdPtr); + return JIM_OK; + +err: + Jim_FreeHashTable(cmdPtr->staticVars); + Jim_Free(cmdPtr->staticVars); + Jim_DecrRefCount(interp, argListObjPtr); + Jim_DecrRefCount(interp, bodyObjPtr); + Jim_Free(cmdPtr); + return JIM_ERR; +} + +int Jim_DeleteCommand(Jim_Interp *interp, const char *cmdName) +{ + if (Jim_DeleteHashEntry(&interp->commands, cmdName) == JIM_ERR) + return JIM_ERR; + Jim_InterpIncrProcEpoch(interp); + return JIM_OK; +} + +int Jim_RenameCommand(Jim_Interp *interp, const char *oldName, + const char *newName) +{ + Jim_Cmd *cmdPtr; + Jim_HashEntry *he; + Jim_Cmd *copyCmdPtr; + + if (newName[0] == '\0') /* Delete! */ + return Jim_DeleteCommand(interp, oldName); + /* Rename */ + he = Jim_FindHashEntry(&interp->commands, oldName); + if (he == NULL) + return JIM_ERR; /* Invalid command name */ + cmdPtr = he->val; + copyCmdPtr = Jim_Alloc(sizeof(Jim_Cmd)); + *copyCmdPtr = *cmdPtr; + /* In order to avoid that a procedure will get arglist/body/statics + * freed by the hash table methods, fake a C-coded command + * setting cmdPtr->cmdProc as not NULL */ + cmdPtr->cmdProc = (void*)1; + /* Also make sure delProc is NULL. */ + cmdPtr->delProc = NULL; + /* Destroy the old command, and make sure the new is freed + * as well. */ + Jim_DeleteHashEntry(&interp->commands, oldName); + Jim_DeleteHashEntry(&interp->commands, newName); + /* Now the new command. We are sure it can't fail because + * the target name was already freed. */ + Jim_AddHashEntry(&interp->commands, newName, copyCmdPtr); + /* Increment the epoch */ + Jim_InterpIncrProcEpoch(interp); + return JIM_OK; +} + +/* ----------------------------------------------------------------------------- + * Command object + * ---------------------------------------------------------------------------*/ + +static int SetCommandFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr); + +static Jim_ObjType commandObjType = { + "command", + NULL, + NULL, + NULL, + JIM_TYPE_REFERENCES, +}; + +int SetCommandFromAny(Jim_Interp *interp, Jim_Obj *objPtr) +{ + Jim_HashEntry *he; + const char *cmdName; + + /* Get the string representation */ + cmdName = Jim_GetString(objPtr, NULL); + /* Lookup this name into the commands hash table */ + he = Jim_FindHashEntry(&interp->commands, cmdName); + if (he == NULL) + return JIM_ERR; + + /* Free the old internal repr and set the new one. */ + Jim_FreeIntRep(interp, objPtr); + objPtr->typePtr = &commandObjType; + objPtr->internalRep.cmdValue.procEpoch = interp->procEpoch; + objPtr->internalRep.cmdValue.cmdPtr = (void*)he->val; + return JIM_OK; +} + +/* This function returns the command structure for the command name + * stored in objPtr. It tries to specialize the objPtr to contain + * a cached info instead to perform the lookup into the hash table + * every time. The information cached may not be uptodate, in such + * a case the lookup is performed and the cache updated. */ +Jim_Cmd *Jim_GetCommand(Jim_Interp *interp, Jim_Obj *objPtr, int flags) +{ + if ((objPtr->typePtr != &commandObjType || + objPtr->internalRep.cmdValue.procEpoch != interp->procEpoch) && + SetCommandFromAny(interp, objPtr) == JIM_ERR) { + if (flags & JIM_ERRMSG) { + Jim_SetResult(interp, Jim_NewEmptyStringObj(interp)); + Jim_AppendStrings(interp, Jim_GetResult(interp), + "invalid command name \"", objPtr->bytes, "\"", + NULL); + } + return NULL; + } + return objPtr->internalRep.cmdValue.cmdPtr; +} + +/* ----------------------------------------------------------------------------- + * Variables + * ---------------------------------------------------------------------------*/ + +/* Variables HashTable Type. + * + * Keys are dynamic allocated strings, Values are Jim_Var structures. */ +static void JimVariablesHTValDestructor(void *interp, void *val) +{ + Jim_Var *varPtr = (void*) val; + + Jim_DecrRefCount(interp, varPtr->objPtr); + Jim_Free(val); +} + +static Jim_HashTableType JimVariablesHashTableType = { + JimStringCopyHTHashFunction, /* hash function */ + JimStringCopyHTKeyDup, /* key dup */ + NULL, /* val dup */ + JimStringCopyHTKeyCompare, /* key compare */ + JimStringCopyHTKeyDestructor, /* key destructor */ + JimVariablesHTValDestructor /* val destructor */ +}; + +/* ----------------------------------------------------------------------------- + * Variable object + * ---------------------------------------------------------------------------*/ + +#define JIM_DICT_SUGAR 100 /* Only returned by SetVariableFromAny() */ + +static int SetVariableFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr); + +static Jim_ObjType variableObjType = { + "variable", + NULL, + NULL, + NULL, + JIM_TYPE_REFERENCES, +}; + +/* Return true if the string "str" looks like syntax sugar for [dict]. I.e. + * is in the form "varname(key)". */ +static int Jim_NameIsDictSugar(const char *str, int len) +{ + if (len == -1) + len = strlen(str); + if (len && str[len-1] == ')' && strchr(str, '(') != NULL) + return 1; + return 0; +} + +/* This method should be called only by the variable API. + * It returns JIM_OK on success (variable already exists), + * JIM_ERR if it does not exists, JIM_DICT_GLUE if it's not + * a variable name, but syntax glue for [dict] i.e. the last + * character is ')' */ +int SetVariableFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr) +{ + Jim_HashEntry *he; + const char *varName; + int len; + + /* Check if the object is already an uptodate variable */ + if (objPtr->typePtr == &variableObjType && + objPtr->internalRep.varValue.callFrameId == interp->framePtr->id) + return JIM_OK; /* nothing to do */ + /* Get the string representation */ + varName = Jim_GetString(objPtr, &len); + /* Make sure it's not syntax glue to get/set dict. */ + if (Jim_NameIsDictSugar(varName, len)) + return JIM_DICT_SUGAR; + /* Lookup this name into the variables hash table */ + he = Jim_FindHashEntry(&interp->framePtr->vars, varName); + if (he == NULL) { + /* Try with static vars. */ + if (interp->framePtr->staticVars == NULL) + return JIM_ERR; + if (!(he = Jim_FindHashEntry(interp->framePtr->staticVars, varName))) + return JIM_ERR; + } + /* Free the old internal repr and set the new one. */ + Jim_FreeIntRep(interp, objPtr); + objPtr->typePtr = &variableObjType; + objPtr->internalRep.varValue.callFrameId = interp->framePtr->id; + objPtr->internalRep.varValue.varPtr = (void*)he->val; + return JIM_OK; +} + +/* -------------------- Variables related functions ------------------------- */ +static int JimDictSugarSet(Jim_Interp *interp, Jim_Obj *ObjPtr, + Jim_Obj *valObjPtr); +static Jim_Obj *JimDictSugarGet(Jim_Interp *interp, Jim_Obj *ObjPtr); + +/* For now that's dummy. Variables lookup should be optimized + * in many ways, with caching of lookups, and possibly with + * a table of pre-allocated vars in every CallFrame for local vars. + * All the caching should also have an 'epoch' mechanism similar + * to the one used by Tcl for procedures lookup caching. */ + +int Jim_SetVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, Jim_Obj *valObjPtr) +{ + const char *name; + Jim_Var *var; + int err; + + if ((err = SetVariableFromAny(interp, nameObjPtr)) != JIM_OK) { + /* Check for [dict] syntax sugar. */ + if (err == JIM_DICT_SUGAR) + return JimDictSugarSet(interp, nameObjPtr, valObjPtr); + /* New variable to create */ + name = Jim_GetString(nameObjPtr, NULL); + + var = Jim_Alloc(sizeof(*var)); + var->objPtr = valObjPtr; + Jim_IncrRefCount(valObjPtr); + var->linkFramePtr = NULL; + /* Insert the new variable */ + Jim_AddHashEntry(&interp->framePtr->vars, name, var); + /* Make the object int rep a variable */ + Jim_FreeIntRep(interp, nameObjPtr); + nameObjPtr->typePtr = &variableObjType; + nameObjPtr->internalRep.varValue.callFrameId = + interp->framePtr->id; + nameObjPtr->internalRep.varValue.varPtr = var; + } else { + var = nameObjPtr->internalRep.varValue.varPtr; + if (var->linkFramePtr == NULL) { + Jim_IncrRefCount(valObjPtr); + Jim_DecrRefCount(interp, var->objPtr); + var->objPtr = valObjPtr; + } else { /* Else handle the link */ + Jim_CallFrame *savedCallFrame; + + savedCallFrame = interp->framePtr; + interp->framePtr = var->linkFramePtr; + err = Jim_SetVariable(interp, var->objPtr, valObjPtr); + interp->framePtr = savedCallFrame; + if (err != JIM_OK) + return err; + } + } + return JIM_OK; +} + +int Jim_SetVariableStr(Jim_Interp *interp, const char *name, Jim_Obj *objPtr) +{ + Jim_Obj *nameObjPtr; + int result; + + nameObjPtr = Jim_NewStringObj(interp, name, -1); + Jim_IncrRefCount(nameObjPtr); + result = Jim_SetVariable(interp, nameObjPtr, objPtr); + Jim_DecrRefCount(interp, nameObjPtr); + return result; +} + +int Jim_SetGlobalVariableStr(Jim_Interp *interp, const char *name, Jim_Obj *objPtr) +{ + Jim_CallFrame *savedFramePtr; + int result; + + savedFramePtr = interp->framePtr; + interp->framePtr = interp->topFramePtr; + result = Jim_SetVariableStr(interp, name, objPtr); + interp->framePtr = savedFramePtr; + return result; +} + +int Jim_SetVariableStrWithStr(Jim_Interp *interp, const char *name, const char *val) +{ + Jim_Obj *nameObjPtr, *valObjPtr; + int result; + + nameObjPtr = Jim_NewStringObj(interp, name, -1); + valObjPtr = Jim_NewStringObj(interp, val, -1); + Jim_IncrRefCount(nameObjPtr); + Jim_IncrRefCount(valObjPtr); + result = Jim_SetVariable(interp, nameObjPtr, valObjPtr); + Jim_DecrRefCount(interp, nameObjPtr); + Jim_DecrRefCount(interp, valObjPtr); + return result; +} + +int Jim_SetVariableLink(Jim_Interp *interp, Jim_Obj *nameObjPtr, + Jim_Obj *targetNameObjPtr, Jim_CallFrame *targetCallFrame) +{ + const char *varName; + int len; + + /* Check for cycles. */ + if (interp->framePtr == targetCallFrame) { + Jim_Obj *objPtr = targetNameObjPtr; + Jim_Var *varPtr; + /* Cycles are only possible with 'uplevel 0' */ + while(1) { + if (Jim_StringEqObj(objPtr, nameObjPtr, 0)) { + Jim_SetResultString(interp, + "can't upvar from variable to itself", -1); + return JIM_ERR; + } + if (SetVariableFromAny(interp, objPtr) != JIM_OK) + break; + varPtr = objPtr->internalRep.varValue.varPtr; + if (varPtr->linkFramePtr != targetCallFrame) break; + objPtr = varPtr->objPtr; + } + } + varName = Jim_GetString(nameObjPtr, &len); + if (Jim_NameIsDictSugar(varName, len)) { + Jim_SetResultString(interp, + "Dict key syntax invalid as link source", -1); + return JIM_ERR; + } + /* Perform the binding */ + Jim_SetVariable(interp, nameObjPtr, targetNameObjPtr); + /* We are now sure 'nameObjPtr' type is variableObjType */ + nameObjPtr->internalRep.varValue.varPtr->linkFramePtr = targetCallFrame; + return JIM_OK; +} + +/* Return the Jim_Obj pointer associated with a variable name, + * or NULL if the variable was not found in the current context. + * The same optimization discussed in the comment to the + * 'SetVariable' function should apply here. */ +Jim_Obj *Jim_GetVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, int flags) +{ + int err; + + /* All the rest is handled here */ + if ((err = SetVariableFromAny(interp, nameObjPtr)) != JIM_OK) { + /* Check for [dict] syntax sugar. */ + if (err == JIM_DICT_SUGAR) + return JimDictSugarGet(interp, nameObjPtr); + if (flags & JIM_ERRMSG) { + Jim_SetResult(interp, Jim_NewEmptyStringObj(interp)); + Jim_AppendStrings(interp, Jim_GetResult(interp), + "can't read \"", nameObjPtr->bytes, + "\": no such variable", NULL); + } + return NULL; + } else { + Jim_Var *varPtr; + Jim_Obj *objPtr; + Jim_CallFrame *savedCallFrame; + + varPtr = nameObjPtr->internalRep.varValue.varPtr; + if (varPtr->linkFramePtr == NULL) + return varPtr->objPtr; + /* The variable is a link? Resolve it. */ + savedCallFrame = interp->framePtr; + interp->framePtr = varPtr->linkFramePtr; + objPtr = Jim_GetVariable(interp, varPtr->objPtr, JIM_NONE); + if (objPtr == NULL && flags & JIM_ERRMSG) { + Jim_SetResult(interp, Jim_NewEmptyStringObj(interp)); + Jim_AppendStrings(interp, Jim_GetResult(interp), + "can't read \"", nameObjPtr->bytes, + "\": no such variable", NULL); + } + interp->framePtr = savedCallFrame; + return objPtr; + } +} + +Jim_Obj *Jim_GetGlobalVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, + int flags) +{ + Jim_CallFrame *savedFramePtr; + Jim_Obj *objPtr; + + savedFramePtr = interp->framePtr; + interp->framePtr = interp->topFramePtr; + objPtr = Jim_GetVariable(interp, nameObjPtr, flags); + interp->framePtr = savedFramePtr; + + return objPtr; +} + +Jim_Obj *Jim_GetVariableStr(Jim_Interp *interp, const char *name, int flags) +{ + Jim_Obj *nameObjPtr, *varObjPtr; + + nameObjPtr = Jim_NewStringObj(interp, name, -1); + Jim_IncrRefCount(nameObjPtr); + varObjPtr = Jim_GetVariable(interp, nameObjPtr, flags); + Jim_DecrRefCount(interp, nameObjPtr); + return varObjPtr; +} + +Jim_Obj *Jim_GetGlobalVariableStr(Jim_Interp *interp, const char *name, + int flags) +{ + Jim_CallFrame *savedFramePtr; + Jim_Obj *objPtr; + + savedFramePtr = interp->framePtr; + interp->framePtr = interp->topFramePtr; + objPtr = Jim_GetVariableStr(interp, name, flags); + interp->framePtr = savedFramePtr; + + return objPtr; +} + +/* Unset a variable. + * Note: On success unset invalidates all the variable objects created + * in the current call frame incrementing. */ +int Jim_UnsetVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, int flags) +{ + const char *name; + Jim_Var *varPtr; + int err; + + if ((err = SetVariableFromAny(interp, nameObjPtr)) != JIM_OK) { + /* Check for [dict] syntax sugar. */ + if (err == JIM_DICT_SUGAR) + return JimDictSugarSet(interp, nameObjPtr, NULL); + Jim_SetResult(interp, Jim_NewEmptyStringObj(interp)); + Jim_AppendStrings(interp, Jim_GetResult(interp), + "can't unset \"", nameObjPtr->bytes, + "\": no such variable", NULL); + return JIM_ERR; /* var not found */ + } + varPtr = nameObjPtr->internalRep.varValue.varPtr; + /* If it's a link call UnsetVariable recursively */ + if (varPtr->linkFramePtr) { + int retval; + + Jim_CallFrame *savedCallFrame; + + savedCallFrame = interp->framePtr; + interp->framePtr = varPtr->linkFramePtr; + retval = Jim_UnsetVariable(interp, varPtr->objPtr, JIM_NONE); + interp->framePtr = savedCallFrame; + if (retval != JIM_OK && flags & JIM_ERRMSG) { + Jim_SetResult(interp, Jim_NewEmptyStringObj(interp)); + Jim_AppendStrings(interp, Jim_GetResult(interp), + "can't unset \"", nameObjPtr->bytes, + "\": no such variable", NULL); + } + return retval; + } else { + name = Jim_GetString(nameObjPtr, NULL); + if (Jim_DeleteHashEntry(&interp->framePtr->vars, name) + != JIM_OK) return JIM_ERR; + /* Change the callframe id, invalidating var lookup caching */ + JimChangeCallFrameId(interp, interp->framePtr); + return JIM_OK; + } +} + +/* ---------- Dict syntax sugar (similar to array Tcl syntax) -------------- */ + +/* Given a variable name for [dict] operation syntax sugar, + * this function returns two objects, the first with the name + * of the variable to set, and the second with the rispective key. + * For example "foo(bar)" will return objects with string repr. of + * "foo" and "bar". + * + * The returned objects have refcount = 1. The function can't fail. */ +static void JimDictSugarParseVarKey(Jim_Interp *interp, Jim_Obj *objPtr, + Jim_Obj **varPtrPtr, Jim_Obj **keyPtrPtr) +{ + const char *str, *p; + char *t; + int len, keyLen, nameLen; + Jim_Obj *varObjPtr, *keyObjPtr; + + str = Jim_GetString(objPtr, &len); + p = strchr(str, '('); + p++; + keyLen = len-((p-str)+1); + nameLen = (p-str)-1; + /* Create the objects with the variable name and key. */ + t = Jim_Alloc(nameLen+1); + memcpy(t, str, nameLen); + t[nameLen] = '\0'; + varObjPtr = Jim_NewStringObjNoAlloc(interp, t, nameLen); + + t = Jim_Alloc(keyLen+1); + memcpy(t, p, keyLen); + t[keyLen] = '\0'; + keyObjPtr = Jim_NewStringObjNoAlloc(interp, t, keyLen); + + Jim_IncrRefCount(varObjPtr); + Jim_IncrRefCount(keyObjPtr); + *varPtrPtr = varObjPtr; + *keyPtrPtr = keyObjPtr; +} + +/* Helper of Jim_SetVariable() to deal with dict-syntax variable names. + * Also used by Jim_UnsetVariable() with valObjPtr = NULL. */ +static int JimDictSugarSet(Jim_Interp *interp, Jim_Obj *objPtr, + Jim_Obj *valObjPtr) +{ + Jim_Obj *varObjPtr, *keyObjPtr; + int err = JIM_OK; + + JimDictSugarParseVarKey(interp, objPtr, &varObjPtr, &keyObjPtr); + err = Jim_SetDictKeysVector(interp, varObjPtr, &keyObjPtr, 1, + valObjPtr); + Jim_DecrRefCount(interp, varObjPtr); + Jim_DecrRefCount(interp, keyObjPtr); + return err; +} + +/* Helper of Jim_GetVariable() to deal with dict-syntax variable names */ +static Jim_Obj *JimDictSugarGet(Jim_Interp *interp, Jim_Obj *objPtr) +{ + Jim_Obj *varObjPtr, *keyObjPtr, *dictObjPtr, *resObjPtr; + + JimDictSugarParseVarKey(interp, objPtr, &varObjPtr, &keyObjPtr); + dictObjPtr = Jim_GetVariable(interp, varObjPtr, JIM_ERRMSG); + if (!dictObjPtr) { + resObjPtr = NULL; + goto err; + } + if (Jim_DictKey(interp, dictObjPtr, keyObjPtr, &resObjPtr, JIM_ERRMSG) + != JIM_OK) { + resObjPtr = NULL; + } +err: + Jim_DecrRefCount(interp, varObjPtr); + Jim_DecrRefCount(interp, keyObjPtr); + return resObjPtr; +} + +/* --------- $var(INDEX) substitution, using a specialized object ----------- */ + +static void FreeDictSubstInternalRep(Jim_Interp *interp, Jim_Obj *objPtr); +static void DupDictSubstInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, + Jim_Obj *dupPtr); + +static Jim_ObjType dictSubstObjType = { + "dict-substitution", + FreeDictSubstInternalRep, + DupDictSubstInternalRep, + NULL, + JIM_TYPE_NONE, +}; + +void FreeDictSubstInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) +{ + Jim_DecrRefCount(interp, objPtr->internalRep.dictSubstValue.varNameObjPtr); + Jim_DecrRefCount(interp, objPtr->internalRep.dictSubstValue.indexObjPtr); +} + +void DupDictSubstInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, + Jim_Obj *dupPtr) +{ + JIM_NOTUSED(interp); + + dupPtr->internalRep.dictSubstValue.varNameObjPtr = + srcPtr->internalRep.dictSubstValue.varNameObjPtr; + dupPtr->internalRep.dictSubstValue.indexObjPtr = + srcPtr->internalRep.dictSubstValue.indexObjPtr; + dupPtr->typePtr = &dictSubstObjType; +} + +/* This function is used to expand [dict get] sugar in the form + * of $var(INDEX). The function is mainly used by Jim_EvalObj() + * to deal with tokens of type JIM_TT_DICTSUGAR. objPtr points to an + * object that is *guaranteed* to be in the form VARNAME(INDEX). + * The 'index' part is [subst]ituted, and is used to lookup a key inside + * the [dict]ionary contained in variable VARNAME. */ +Jim_Obj *Jim_ExpandDictSugar(Jim_Interp *interp, Jim_Obj *objPtr) +{ + Jim_Obj *varObjPtr, *keyObjPtr, *dictObjPtr, *resObjPtr; + Jim_Obj *substKeyObjPtr = NULL; + + if (objPtr->typePtr != &dictSubstObjType) { + JimDictSugarParseVarKey(interp, objPtr, &varObjPtr, &keyObjPtr); + Jim_FreeIntRep(interp, objPtr); + objPtr->typePtr = &dictSubstObjType; + objPtr->internalRep.dictSubstValue.varNameObjPtr = varObjPtr; + objPtr->internalRep.dictSubstValue.indexObjPtr = keyObjPtr; + } + if (Jim_SubstObj(interp, objPtr->internalRep.dictSubstValue.indexObjPtr, + &substKeyObjPtr, JIM_NONE) + != JIM_OK) { + substKeyObjPtr = NULL; + goto err; + } + Jim_IncrRefCount(substKeyObjPtr); + dictObjPtr = Jim_GetVariable(interp, + objPtr->internalRep.dictSubstValue.varNameObjPtr, JIM_ERRMSG); + if (!dictObjPtr) { + resObjPtr = NULL; + goto err; + } + if (Jim_DictKey(interp, dictObjPtr, substKeyObjPtr, &resObjPtr, JIM_ERRMSG) + != JIM_OK) { + resObjPtr = NULL; + goto err; + } +err: + if (substKeyObjPtr) Jim_DecrRefCount(interp, substKeyObjPtr); + return resObjPtr; +} + +/* ----------------------------------------------------------------------------- + * CallFrame + * ---------------------------------------------------------------------------*/ + +static Jim_CallFrame *JimCreateCallFrame(Jim_Interp *interp) +{ + Jim_CallFrame *cf; + if (interp->freeFramesList) { + cf = interp->freeFramesList; + interp->freeFramesList = cf->nextFramePtr; + } else { + cf = Jim_Alloc(sizeof(*cf)); + cf->vars.table = NULL; + } + + cf->id = interp->callFrameEpoch++; + cf->parentCallFrame = NULL; + cf->argv = NULL; + cf->argc = 0; + cf->procArgsObjPtr = NULL; + cf->procBodyObjPtr = NULL; + cf->nextFramePtr = NULL; + cf->staticVars = NULL; + if (cf->vars.table == NULL) + Jim_InitHashTable(&cf->vars, &JimVariablesHashTableType, interp); + return cf; +} + +/* Used to invalidate every caching related to callframe stability. */ +static void JimChangeCallFrameId(Jim_Interp *interp, Jim_CallFrame *cf) +{ + cf->id = interp->callFrameEpoch++; +} + +#define JIM_FCF_NONE 0 /* no flags */ +#define JIM_FCF_NOHT 1 /* don't free the hash table */ +static void JimFreeCallFrame(Jim_Interp *interp, Jim_CallFrame *cf, + int flags) +{ + if (cf->procArgsObjPtr) Jim_DecrRefCount(interp, cf->procArgsObjPtr); + if (cf->procBodyObjPtr) Jim_DecrRefCount(interp, cf->procBodyObjPtr); + if (!(flags & JIM_FCF_NOHT)) + Jim_FreeHashTable(&cf->vars); + else { + int i; + Jim_HashEntry **table = cf->vars.table, *he; + + for (i = 0; i < JIM_HT_INITIAL_SIZE; i++) { + he = table[i]; + while (he != NULL) { + Jim_HashEntry *nextEntry = he->next; + Jim_Var *varPtr = (void*) he->val; + + Jim_DecrRefCount(interp, varPtr->objPtr); + Jim_Free(he->val); + Jim_Free((void*)he->key); /* ATTENTION: const cast */ + Jim_Free(he); + table[i] = NULL; + he = nextEntry; + } + } + cf->vars.used = 0; + } + cf->nextFramePtr = interp->freeFramesList; + interp->freeFramesList = cf; +} + +/* ----------------------------------------------------------------------------- + * References + * ---------------------------------------------------------------------------*/ + +/* References HashTable Type. + * + * Keys are jim_wide integers, dynamically allocated for now but in the + * future it's worth to cache this 8 bytes objects. Values are poitners + * to Jim_References. */ +static void JimReferencesHTValDestructor(void *interp, void *val) +{ + Jim_Reference *refPtr = (void*) val; + + Jim_DecrRefCount(interp, refPtr->objPtr); + if (refPtr->finalizerCmdNamePtr != NULL) { + Jim_DecrRefCount(interp, refPtr->finalizerCmdNamePtr); + } + Jim_Free(val); +} + +unsigned int JimReferencesHTHashFunction(const void *key) +{ + /* Only the least significant bits are used. */ + const jim_wide *widePtr = key; + unsigned int intValue = (unsigned int) *widePtr; + return Jim_IntHashFunction(intValue); +} + +unsigned int JimReferencesHTDoubleHashFunction(const void *key) +{ + /* Only the least significant bits are used. */ + const jim_wide *widePtr = key; + unsigned int intValue = (unsigned int) *widePtr; + return intValue; /* identity function. */ +} + +const void *JimReferencesHTKeyDup(void *privdata, const void *key) +{ + void *copy = Jim_Alloc(sizeof(jim_wide)); + JIM_NOTUSED(privdata); + + memcpy(copy, key, sizeof(jim_wide)); + return copy; +} + +int JimReferencesHTKeyCompare(void *privdata, const void *key1, + const void *key2) +{ + JIM_NOTUSED(privdata); + + return memcmp(key1, key2, sizeof(jim_wide)) == 0; +} + +void JimReferencesHTKeyDestructor(void *privdata, const void *key) +{ + JIM_NOTUSED(privdata); + + Jim_Free((void*)key); +} + +static Jim_HashTableType JimReferencesHashTableType = { + JimReferencesHTHashFunction, /* hash function */ + JimReferencesHTKeyDup, /* key dup */ + NULL, /* val dup */ + JimReferencesHTKeyCompare, /* key compare */ + JimReferencesHTKeyDestructor, /* key destructor */ + JimReferencesHTValDestructor /* val destructor */ +}; + +/* ----------------------------------------------------------------------------- + * Reference object type and References API + * ---------------------------------------------------------------------------*/ + +static void UpdateStringOfReference(struct Jim_Obj *objPtr); + +static Jim_ObjType referenceObjType = { + "reference", + NULL, + NULL, + UpdateStringOfReference, + JIM_TYPE_REFERENCES, +}; + +void UpdateStringOfReference(struct Jim_Obj *objPtr) +{ + int len; + char buf[JIM_REFERENCE_SPACE+1]; + Jim_Reference *refPtr; + + refPtr = objPtr->internalRep.refValue.refPtr; + len = JimFormatReference(buf, refPtr, objPtr->internalRep.refValue.id); + objPtr->bytes = Jim_Alloc(len+1); + memcpy(objPtr->bytes, buf, len+1); + objPtr->length = len; +} + +/* returns true if 'c' is a valid reference tag character. + * i.e. inside the range [_a-zA-Z0-9] */ +static int isrefchar(int c) +{ + if (c == '_' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || + (c >= '0' && c <= '9')) return 1; + return 0; +} + +int SetReferenceFromAny(Jim_Interp *interp, Jim_Obj *objPtr) +{ + jim_wide wideValue; + int i, len; + const char *str, *start, *end; + char refId[21]; + Jim_Reference *refPtr; + Jim_HashEntry *he; + + /* Get the string representation */ + str = Jim_GetString(objPtr, &len); + /* Check if it looks like a reference */ + if (len < JIM_REFERENCE_SPACE) goto badformat; + /* Trim spaces */ + start = str; + end = str+len-1; + while (*start == ' ') start++; + while (*end == ' ' && end > start) end--; + if (end-start+1 != JIM_REFERENCE_SPACE) goto badformat; + /* .%020> */ + if (memcmp(start, "references, &wideValue); + if (he == NULL) { + Jim_SetResult(interp, Jim_NewEmptyStringObj(interp)); + Jim_AppendStrings(interp, Jim_GetResult(interp), + "Invalid reference ID \"", str, "\"", NULL); + return JIM_ERR; + } + refPtr = he->val; + /* Free the old internal repr and set the new one. */ + Jim_FreeIntRep(interp, objPtr); + objPtr->typePtr = &referenceObjType; + objPtr->internalRep.refValue.id = wideValue; + objPtr->internalRep.refValue.refPtr = refPtr; + return JIM_OK; + +badformat: + Jim_SetResult(interp, Jim_NewEmptyStringObj(interp)); + Jim_AppendStrings(interp, Jim_GetResult(interp), + "expected reference but got \"", str, "\"", NULL); + return JIM_ERR; +} + +/* Returns a new reference pointing to objPtr, having cmdNamePtr + * as finalizer command (or NULL if there is no finalizer). + * The returned reference object has refcount = 0. */ +Jim_Obj *Jim_New