|
588bb7c…
|
aku
|
1 |
|
|
588bb7c…
|
aku
|
2 |
/* |
|
1ebe4b0…
|
mistachkin
|
3 |
** The implementation of the TH core. This file contains the parser, and |
|
588bb7c…
|
aku
|
4 |
** the implementation of the interface in th.h. |
|
588bb7c…
|
aku
|
5 |
*/ |
|
588bb7c…
|
aku
|
6 |
|
|
c30cd93…
|
jan.nijtmans
|
7 |
#include "config.h" |
|
588bb7c…
|
aku
|
8 |
#include "th.h" |
|
588bb7c…
|
aku
|
9 |
#include <string.h> |
|
588bb7c…
|
aku
|
10 |
#include <assert.h> |
|
588bb7c…
|
aku
|
11 |
|
|
ea7813d…
|
mistachkin
|
12 |
/* |
|
2116238…
|
drh
|
13 |
** External routines |
|
2116238…
|
drh
|
14 |
*/ |
|
2116238…
|
drh
|
15 |
void fossil_panic(const char*,...); |
|
2116238…
|
drh
|
16 |
void fossil_errorlog(const char*,...); |
|
2116238…
|
drh
|
17 |
|
|
2116238…
|
drh
|
18 |
/* |
|
ea7813d…
|
mistachkin
|
19 |
** Values used for element values in the tcl_platform array. |
|
ea7813d…
|
mistachkin
|
20 |
*/ |
|
ea7813d…
|
mistachkin
|
21 |
|
|
ea7813d…
|
mistachkin
|
22 |
#if !defined(TH_ENGINE) |
|
ea7813d…
|
mistachkin
|
23 |
# define TH_ENGINE "TH1" |
|
ea7813d…
|
mistachkin
|
24 |
#endif |
|
ea7813d…
|
mistachkin
|
25 |
|
|
ea7813d…
|
mistachkin
|
26 |
#if !defined(TH_PLATFORM) |
|
ea7813d…
|
mistachkin
|
27 |
# if defined(_WIN32) || defined(WIN32) |
|
ea7813d…
|
mistachkin
|
28 |
# define TH_PLATFORM "windows" |
|
ea7813d…
|
mistachkin
|
29 |
# else |
|
ea7813d…
|
mistachkin
|
30 |
# define TH_PLATFORM "unix" |
|
ea7813d…
|
mistachkin
|
31 |
# endif |
|
ea7813d…
|
mistachkin
|
32 |
#endif |
|
ea7813d…
|
mistachkin
|
33 |
|
|
ea7813d…
|
mistachkin
|
34 |
/* |
|
ea7813d…
|
mistachkin
|
35 |
** Forward declarations for structures defined below. |
|
ea7813d…
|
mistachkin
|
36 |
*/ |
|
ea7813d…
|
mistachkin
|
37 |
|
|
810e6c1…
|
mistachkin
|
38 |
typedef struct Th_Command Th_Command; |
|
810e6c1…
|
mistachkin
|
39 |
typedef struct Th_Frame Th_Frame; |
|
810e6c1…
|
mistachkin
|
40 |
typedef struct Th_Variable Th_Variable; |
|
810e6c1…
|
mistachkin
|
41 |
typedef struct Th_InterpAndList Th_InterpAndList; |
|
588bb7c…
|
aku
|
42 |
|
|
588bb7c…
|
aku
|
43 |
/* |
|
588bb7c…
|
aku
|
44 |
** Interpreter structure. |
|
588bb7c…
|
aku
|
45 |
*/ |
|
588bb7c…
|
aku
|
46 |
struct Th_Interp { |
|
588bb7c…
|
aku
|
47 |
Th_Vtab *pVtab; /* Copy of the argument passed to Th_CreateInterp() */ |
|
1ebe4b0…
|
mistachkin
|
48 |
char *zResult; /* Current interpreter result (Th_Malloc()ed) */ |
|
588bb7c…
|
aku
|
49 |
int nResult; /* number of bytes in zResult */ |
|
588bb7c…
|
aku
|
50 |
Th_Hash *paCmd; /* Table of registered commands */ |
|
588bb7c…
|
aku
|
51 |
Th_Frame *pFrame; /* Current execution frame */ |
|
588bb7c…
|
aku
|
52 |
int isListMode; /* True if thSplitList() should operate in "list" mode */ |
|
588bb7c…
|
aku
|
53 |
}; |
|
588bb7c…
|
aku
|
54 |
|
|
588bb7c…
|
aku
|
55 |
/* |
|
588bb7c…
|
aku
|
56 |
** Each TH command registered using Th_CreateCommand() is represented |
|
588bb7c…
|
aku
|
57 |
** by an instance of the following structure stored in the Th_Interp.paCmd |
|
588bb7c…
|
aku
|
58 |
** hash-table. |
|
588bb7c…
|
aku
|
59 |
*/ |
|
588bb7c…
|
aku
|
60 |
struct Th_Command { |
|
0c99a15…
|
drh
|
61 |
int (*xProc)(Th_Interp *, void *, int, const char **, int *); |
|
588bb7c…
|
aku
|
62 |
void *pContext; |
|
588bb7c…
|
aku
|
63 |
void (*xDel)(Th_Interp *, void *); |
|
588bb7c…
|
aku
|
64 |
}; |
|
588bb7c…
|
aku
|
65 |
|
|
588bb7c…
|
aku
|
66 |
/* |
|
588bb7c…
|
aku
|
67 |
** Each stack frame (variable scope) is represented by an instance |
|
588bb7c…
|
aku
|
68 |
** of this structure. Variable values set using the Th_SetVar command |
|
588bb7c…
|
aku
|
69 |
** are stored in the Th_Frame.paVar hash table member of the associated |
|
588bb7c…
|
aku
|
70 |
** stack frame object. |
|
588bb7c…
|
aku
|
71 |
** |
|
588bb7c…
|
aku
|
72 |
** When an interpreter is created, a single Th_Frame structure is also |
|
588bb7c…
|
aku
|
73 |
** allocated - the global variable scope. Th_Interp.pFrame (the current |
|
1ebe4b0…
|
mistachkin
|
74 |
** interpreter frame) is initialised to point to this Th_Frame. It is |
|
1ebe4b0…
|
mistachkin
|
75 |
** not deleted for the lifetime of the interpreter (because the global |
|
588bb7c…
|
aku
|
76 |
** frame never goes out of scope). |
|
588bb7c…
|
aku
|
77 |
** |
|
588bb7c…
|
aku
|
78 |
** New stack frames are created by the Th_InFrame() function. Before |
|
588bb7c…
|
aku
|
79 |
** invoking its callback function, Th_InFrame() allocates a new Th_Frame |
|
588bb7c…
|
aku
|
80 |
** structure with pCaller set to the current frame (Th_Interp.pFrame), |
|
588bb7c…
|
aku
|
81 |
** and sets the current frame to the new frame object. After the callback |
|
588bb7c…
|
aku
|
82 |
** has been invoked, the allocated Th_Frame is deleted and the value |
|
588bb7c…
|
aku
|
83 |
** of the current frame pointer restored. |
|
1ebe4b0…
|
mistachkin
|
84 |
** |
|
1ebe4b0…
|
mistachkin
|
85 |
** By default, the Th_SetVar(), Th_UnsetVar() and Th_GetVar() functions |
|
1ebe4b0…
|
mistachkin
|
86 |
** access variable values in the current frame. If they need to access |
|
588bb7c…
|
aku
|
87 |
** the global frame, they do so by traversing the pCaller pointer list. |
|
1ebe4b0…
|
mistachkin
|
88 |
** Likewise, the Th_LinkVar() function uses the pCaller pointers to |
|
588bb7c…
|
aku
|
89 |
** link to variables located in the global or other stack frames. |
|
588bb7c…
|
aku
|
90 |
*/ |
|
588bb7c…
|
aku
|
91 |
struct Th_Frame { |
|
588bb7c…
|
aku
|
92 |
Th_Hash *paVar; /* Variables defined in this scope */ |
|
588bb7c…
|
aku
|
93 |
Th_Frame *pCaller; /* Calling frame */ |
|
588bb7c…
|
aku
|
94 |
}; |
|
588bb7c…
|
aku
|
95 |
|
|
588bb7c…
|
aku
|
96 |
/* |
|
588bb7c…
|
aku
|
97 |
** This structure represents a value assigned to a th1 variable. |
|
588bb7c…
|
aku
|
98 |
** |
|
588bb7c…
|
aku
|
99 |
** The Th_Frame.paVar hash table maps from variable name (a th1 string) |
|
588bb7c…
|
aku
|
100 |
** to a pointer to an instance of the following structure. More than |
|
588bb7c…
|
aku
|
101 |
** one hash table entry may map to a single structure if variable |
|
588bb7c…
|
aku
|
102 |
** links have been created using Th_LinkVar(). The number of references |
|
588bb7c…
|
aku
|
103 |
** is stored in Th_Variable.nRef. |
|
588bb7c…
|
aku
|
104 |
** |
|
588bb7c…
|
aku
|
105 |
** For scalar variables, Th_Variable.zData is never 0. Th_Variable.nData |
|
588bb7c…
|
aku
|
106 |
** stores the number of bytes in the value pointed to by zData. |
|
588bb7c…
|
aku
|
107 |
** |
|
588bb7c…
|
aku
|
108 |
** For an array variable, Th_Variable.zData is 0 and pHash points to |
|
588bb7c…
|
aku
|
109 |
** a hash table mapping between array key name (a th1 string) and |
|
db0c512…
|
drh
|
110 |
** a pointer to the Th_Variable structure holding the scalar |
|
588bb7c…
|
aku
|
111 |
** value. |
|
588bb7c…
|
aku
|
112 |
*/ |
|
588bb7c…
|
aku
|
113 |
struct Th_Variable { |
|
588bb7c…
|
aku
|
114 |
int nRef; /* Number of references to this structure */ |
|
588bb7c…
|
aku
|
115 |
int nData; /* Number of bytes at Th_Variable.zData */ |
|
1ebe4b0…
|
mistachkin
|
116 |
char *zData; /* Data for scalar variables */ |
|
588bb7c…
|
aku
|
117 |
Th_Hash *pHash; /* Data for array variables */ |
|
810e6c1…
|
mistachkin
|
118 |
}; |
|
810e6c1…
|
mistachkin
|
119 |
|
|
810e6c1…
|
mistachkin
|
120 |
/* |
|
810e6c1…
|
mistachkin
|
121 |
** This structure is used to pass complete context information to the |
|
810e6c1…
|
mistachkin
|
122 |
** hash iteration callback functions that need a Th_Interp and a list |
|
22b3d0b…
|
mistachkin
|
123 |
** to operate on, e.g. thListAppendHashKey(). |
|
810e6c1…
|
mistachkin
|
124 |
*/ |
|
810e6c1…
|
mistachkin
|
125 |
struct Th_InterpAndList { |
|
810e6c1…
|
mistachkin
|
126 |
Th_Interp *interp; /* Associated interpreter context */ |
|
810e6c1…
|
mistachkin
|
127 |
char **pzList; /* IN/OUT: Ptr to ptr to list */ |
|
810e6c1…
|
mistachkin
|
128 |
int *pnList; /* IN/OUT: Current length of *pzList */ |
|
99901fb…
|
jan.nijtmans
|
129 |
}; |
|
99901fb…
|
jan.nijtmans
|
130 |
|
|
99901fb…
|
jan.nijtmans
|
131 |
/* |
|
588bb7c…
|
aku
|
132 |
** Hash table API: |
|
588bb7c…
|
aku
|
133 |
*/ |
|
588bb7c…
|
aku
|
134 |
#define TH_HASHSIZE 257 |
|
588bb7c…
|
aku
|
135 |
struct Th_Hash { |
|
588bb7c…
|
aku
|
136 |
Th_HashEntry *a[TH_HASHSIZE]; |
|
588bb7c…
|
aku
|
137 |
}; |
|
588bb7c…
|
aku
|
138 |
|
|
0c99a15…
|
drh
|
139 |
static int thEvalLocal(Th_Interp *, const char *, int); |
|
0c99a15…
|
drh
|
140 |
static int thSplitList(Th_Interp*, const char*, int, char***, int **, int*); |
|
588bb7c…
|
aku
|
141 |
|
|
0c99a15…
|
drh
|
142 |
static int thHexdigit(char c); |
|
0c99a15…
|
drh
|
143 |
static int thEndOfLine(const char *, int); |
|
588bb7c…
|
aku
|
144 |
|
|
588bb7c…
|
aku
|
145 |
static int thPushFrame(Th_Interp*, Th_Frame*); |
|
588bb7c…
|
aku
|
146 |
static void thPopFrame(Th_Interp*); |
|
588bb7c…
|
aku
|
147 |
|
|
1aeb272…
|
drh
|
148 |
static int thFreeVariable(Th_HashEntry*, void*); |
|
1aeb272…
|
drh
|
149 |
static int thFreeCommand(Th_HashEntry*, void*); |
|
588bb7c…
|
aku
|
150 |
|
|
588bb7c…
|
aku
|
151 |
/* |
|
588bb7c…
|
aku
|
152 |
** The following are used by both the expression and language parsers. |
|
1ebe4b0…
|
mistachkin
|
153 |
** Given that the start of the input string (z, n) is a language |
|
588bb7c…
|
aku
|
154 |
** construct of the relevant type (a command enclosed in [], an escape |
|
588bb7c…
|
aku
|
155 |
** sequence etc.), these functions determine the number of bytes |
|
588bb7c…
|
aku
|
156 |
** of the input consumed by the construct. For example: |
|
588bb7c…
|
aku
|
157 |
** |
|
588bb7c…
|
aku
|
158 |
** int nByte; |
|
588bb7c…
|
aku
|
159 |
** thNextCommand(interp, "[expr $a+1] $nIter", 18, &nByte); |
|
588bb7c…
|
aku
|
160 |
** |
|
1ebe4b0…
|
mistachkin
|
161 |
** results in variable nByte being set to 11. Or, |
|
588bb7c…
|
aku
|
162 |
** |
|
588bb7c…
|
aku
|
163 |
** thNextVarname(interp, "$a+1", 4, &nByte); |
|
588bb7c…
|
aku
|
164 |
** |
|
588bb7c…
|
aku
|
165 |
** results in nByte being set to 2. |
|
588bb7c…
|
aku
|
166 |
*/ |
|
0c99a15…
|
drh
|
167 |
static int thNextCommand(Th_Interp*, const char *z, int n, int *pN); |
|
0c99a15…
|
drh
|
168 |
static int thNextEscape (Th_Interp*, const char *z, int n, int *pN); |
|
0c99a15…
|
drh
|
169 |
static int thNextVarname(Th_Interp*, const char *z, int n, int *pN); |
|
0c99a15…
|
drh
|
170 |
static int thNextNumber (Th_Interp*, const char *z, int n, int *pN); |
|
99901fb…
|
jan.nijtmans
|
171 |
static int thNextInteger (Th_Interp*, const char *z, int n, int *pN); |
|
0c99a15…
|
drh
|
172 |
static int thNextSpace (Th_Interp*, const char *z, int n, int *pN); |
|
588bb7c…
|
aku
|
173 |
|
|
588bb7c…
|
aku
|
174 |
/* |
|
588bb7c…
|
aku
|
175 |
** Given that the input string (z, n) contains a language construct of |
|
1ebe4b0…
|
mistachkin
|
176 |
** the relevant type (a command enclosed in [], an escape sequence |
|
588bb7c…
|
aku
|
177 |
** like "\xFF" or a variable reference like "${varname}", perform |
|
588bb7c…
|
aku
|
178 |
** substitution on the string and store the resulting string in |
|
588bb7c…
|
aku
|
179 |
** the interpreter result. |
|
588bb7c…
|
aku
|
180 |
*/ |
|
0c99a15…
|
drh
|
181 |
static int thSubstCommand(Th_Interp*, const char *z, int n); |
|
0c99a15…
|
drh
|
182 |
static int thSubstEscape (Th_Interp*, const char *z, int n); |
|
0c99a15…
|
drh
|
183 |
static int thSubstVarname(Th_Interp*, const char *z, int n); |
|
588bb7c…
|
aku
|
184 |
|
|
588bb7c…
|
aku
|
185 |
/* |
|
1ebe4b0…
|
mistachkin
|
186 |
** Given that there is a th1 word located at the start of the input |
|
588bb7c…
|
aku
|
187 |
** string (z, n), determine the length in bytes of that word. If the |
|
1ebe4b0…
|
mistachkin
|
188 |
** isCmd argument is non-zero, then an unescaped ";" byte not |
|
1ebe4b0…
|
mistachkin
|
189 |
** located inside of a block or quoted string is considered to mark |
|
588bb7c…
|
aku
|
190 |
** the end of the word. |
|
588bb7c…
|
aku
|
191 |
*/ |
|
0c99a15…
|
drh
|
192 |
static int thNextWord(Th_Interp*, const char *z, int n, int *pN, int isCmd); |
|
588bb7c…
|
aku
|
193 |
|
|
588bb7c…
|
aku
|
194 |
/* |
|
588bb7c…
|
aku
|
195 |
** Perform substitution on the word contained in the input string (z, n). |
|
588bb7c…
|
aku
|
196 |
** Store the resulting string in the interpreter result. |
|
588bb7c…
|
aku
|
197 |
*/ |
|
0c99a15…
|
drh
|
198 |
static int thSubstWord(Th_Interp*, const char *z, int n); |
|
588bb7c…
|
aku
|
199 |
|
|
588bb7c…
|
aku
|
200 |
/* |
|
588bb7c…
|
aku
|
201 |
** The Buffer structure and the thBufferXXX() functions are used to make |
|
588bb7c…
|
aku
|
202 |
** memory allocation easier when building up a result. |
|
588bb7c…
|
aku
|
203 |
*/ |
|
588bb7c…
|
aku
|
204 |
struct Buffer { |
|
0c99a15…
|
drh
|
205 |
char *zBuf; |
|
588bb7c…
|
aku
|
206 |
int nBuf; |
|
588bb7c…
|
aku
|
207 |
int nBufAlloc; |
|
2116238…
|
drh
|
208 |
int bTaint; |
|
588bb7c…
|
aku
|
209 |
}; |
|
588bb7c…
|
aku
|
210 |
typedef struct Buffer Buffer; |
|
588bb7c…
|
aku
|
211 |
static void thBufferInit(Buffer *); |
|
588bb7c…
|
aku
|
212 |
static void thBufferFree(Th_Interp *interp, Buffer *); |
|
7ae4b1a…
|
drh
|
213 |
|
|
7ae4b1a…
|
drh
|
214 |
/* |
|
ab2b8de…
|
andybradford
|
215 |
** This version of memcpy() allows the first and second argument to |
|
7ae4b1a…
|
drh
|
216 |
** be NULL as long as the number of bytes to copy is zero. |
|
7ae4b1a…
|
drh
|
217 |
*/ |
|
b26eea7…
|
drh
|
218 |
static void th_memcpy(void *dest, const void *src, size_t n){ |
|
b26eea7…
|
drh
|
219 |
if( n>0 ) memcpy(dest,src,n); |
|
b26eea7…
|
drh
|
220 |
} |
|
b26eea7…
|
drh
|
221 |
|
|
b26eea7…
|
drh
|
222 |
/* |
|
2116238…
|
drh
|
223 |
** An oversized string has been encountered. Do not try to recover. |
|
2116238…
|
drh
|
224 |
** Panic the process. |
|
2116238…
|
drh
|
225 |
*/ |
|
2116238…
|
drh
|
226 |
void Th_OversizeString(void){ |
|
2116238…
|
drh
|
227 |
fossil_panic("string too large. maximum size 286MB."); |
|
2116238…
|
drh
|
228 |
} |
|
2116238…
|
drh
|
229 |
|
|
2116238…
|
drh
|
230 |
/* |
|
588bb7c…
|
aku
|
231 |
** Append nAdd bytes of content copied from zAdd to the end of buffer |
|
588bb7c…
|
aku
|
232 |
** pBuffer. If there is not enough space currently allocated, resize |
|
588bb7c…
|
aku
|
233 |
** the allocation to make space. |
|
588bb7c…
|
aku
|
234 |
*/ |
|
b26eea7…
|
drh
|
235 |
static void thBufferWriteResize( |
|
b26eea7…
|
drh
|
236 |
Th_Interp *interp, |
|
b26eea7…
|
drh
|
237 |
Buffer *pBuffer, |
|
b26eea7…
|
drh
|
238 |
const char *zAdd, |
|
2116238…
|
drh
|
239 |
int nAddX |
|
b26eea7…
|
drh
|
240 |
){ |
|
2116238…
|
drh
|
241 |
int nAdd = TH1_LEN(nAddX); |
|
b26eea7…
|
drh
|
242 |
int nNew = (pBuffer->nBuf+nAdd)*2+32; |
|
999e33c…
|
mistachkin
|
243 |
#if defined(TH_MEMDEBUG) |
|
999e33c…
|
mistachkin
|
244 |
char *zNew = (char *)Th_Malloc(interp, nNew); |
|
2116238…
|
drh
|
245 |
TH1_SIZECHECK(nNew); |
|
999e33c…
|
mistachkin
|
246 |
th_memcpy(zNew, pBuffer->zBuf, pBuffer->nBuf); |
|
999e33c…
|
mistachkin
|
247 |
Th_Free(interp, pBuffer->zBuf); |
|
999e33c…
|
mistachkin
|
248 |
pBuffer->zBuf = zNew; |
|
999e33c…
|
mistachkin
|
249 |
#else |
|
999e33c…
|
mistachkin
|
250 |
int nOld = pBuffer->nBufAlloc; |
|
2116238…
|
drh
|
251 |
TH1_SIZECHECK(nNew); |
|
9b4f2d8…
|
drh
|
252 |
pBuffer->zBuf = Th_Realloc(interp, pBuffer->zBuf, nNew); |
|
999e33c…
|
mistachkin
|
253 |
memset(pBuffer->zBuf+nOld, 0, nNew-nOld); |
|
999e33c…
|
mistachkin
|
254 |
#endif |
|
9b4f2d8…
|
drh
|
255 |
pBuffer->nBufAlloc = nNew; |
|
b26eea7…
|
drh
|
256 |
th_memcpy(&pBuffer->zBuf[pBuffer->nBuf], zAdd, nAdd); |
|
b26eea7…
|
drh
|
257 |
pBuffer->nBuf += nAdd; |
|
2116238…
|
drh
|
258 |
TH1_XFER_TAINT(pBuffer->bTaint, nAddX); |
|
b26eea7…
|
drh
|
259 |
} |
|
b26eea7…
|
drh
|
260 |
static void thBufferWriteFast( |
|
1ebe4b0…
|
mistachkin
|
261 |
Th_Interp *interp, |
|
1ebe4b0…
|
mistachkin
|
262 |
Buffer *pBuffer, |
|
1ebe4b0…
|
mistachkin
|
263 |
const char *zAdd, |
|
2116238…
|
drh
|
264 |
int nAddX |
|
b26eea7…
|
drh
|
265 |
){ |
|
2116238…
|
drh
|
266 |
int nAdd = TH1_LEN(nAddX); |
|
b26eea7…
|
drh
|
267 |
if( pBuffer->nBuf+nAdd > pBuffer->nBufAlloc ){ |
|
2116238…
|
drh
|
268 |
thBufferWriteResize(interp, pBuffer, zAdd, nAddX); |
|
b26eea7…
|
drh
|
269 |
}else{ |
|
39cac07…
|
wyoung
|
270 |
if( pBuffer->zBuf ){ |
|
39cac07…
|
wyoung
|
271 |
memcpy(pBuffer->zBuf + pBuffer->nBuf, zAdd, nAdd); |
|
39cac07…
|
wyoung
|
272 |
} |
|
b26eea7…
|
drh
|
273 |
pBuffer->nBuf += nAdd; |
|
2116238…
|
drh
|
274 |
TH1_XFER_TAINT(pBuffer->bTaint, nAddX); |
|
b26eea7…
|
drh
|
275 |
} |
|
b26eea7…
|
drh
|
276 |
} |
|
b26eea7…
|
drh
|
277 |
#define thBufferWrite(a,b,c,d) thBufferWriteFast(a,b,(const char *)c,d) |
|
b26eea7…
|
drh
|
278 |
|
|
b26eea7…
|
drh
|
279 |
/* |
|
b26eea7…
|
drh
|
280 |
** Add a single character to a buffer |
|
b26eea7…
|
drh
|
281 |
*/ |
|
b26eea7…
|
drh
|
282 |
static void thBufferAddChar( |
|
b26eea7…
|
drh
|
283 |
Th_Interp *interp, |
|
b26eea7…
|
drh
|
284 |
Buffer *pBuffer, |
|
b26eea7…
|
drh
|
285 |
char c |
|
1ebe4b0…
|
mistachkin
|
286 |
){ |
|
b26eea7…
|
drh
|
287 |
if( pBuffer->nBuf+1 > pBuffer->nBufAlloc ){ |
|
b26eea7…
|
drh
|
288 |
thBufferWriteResize(interp, pBuffer, &c, 1); |
|
b26eea7…
|
drh
|
289 |
}else{ |
|
b26eea7…
|
drh
|
290 |
pBuffer->zBuf[pBuffer->nBuf++] = c; |
|
b26eea7…
|
drh
|
291 |
} |
|
b26eea7…
|
drh
|
292 |
} |
|
588bb7c…
|
aku
|
293 |
|
|
588bb7c…
|
aku
|
294 |
/* |
|
588bb7c…
|
aku
|
295 |
** Initialize the Buffer structure pointed to by pBuffer. |
|
588bb7c…
|
aku
|
296 |
*/ |
|
588bb7c…
|
aku
|
297 |
static void thBufferInit(Buffer *pBuffer){ |
|
588bb7c…
|
aku
|
298 |
memset(pBuffer, 0, sizeof(Buffer)); |
|
588bb7c…
|
aku
|
299 |
} |
|
588bb7c…
|
aku
|
300 |
|
|
588bb7c…
|
aku
|
301 |
/* |
|
588bb7c…
|
aku
|
302 |
** Zero the buffer pointed to by pBuffer and free the associated memory |
|
588bb7c…
|
aku
|
303 |
** allocation. |
|
588bb7c…
|
aku
|
304 |
*/ |
|
588bb7c…
|
aku
|
305 |
static void thBufferFree(Th_Interp *interp, Buffer *pBuffer){ |
|
588bb7c…
|
aku
|
306 |
Th_Free(interp, pBuffer->zBuf); |
|
588bb7c…
|
aku
|
307 |
thBufferInit(pBuffer); |
|
588bb7c…
|
aku
|
308 |
} |
|
588bb7c…
|
aku
|
309 |
|
|
588bb7c…
|
aku
|
310 |
/* |
|
588bb7c…
|
aku
|
311 |
** Assuming parameter c contains a hexadecimal digit character, |
|
588bb7c…
|
aku
|
312 |
** return the corresponding value of that digit. If c is not |
|
588bb7c…
|
aku
|
313 |
** a hexadecimal digit character, -1 is returned. |
|
588bb7c…
|
aku
|
314 |
*/ |
|
0c99a15…
|
drh
|
315 |
static int thHexdigit(char c){ |
|
588bb7c…
|
aku
|
316 |
switch (c) { |
|
588bb7c…
|
aku
|
317 |
case '0': return 0; |
|
588bb7c…
|
aku
|
318 |
case '1': return 1; |
|
588bb7c…
|
aku
|
319 |
case '2': return 2; |
|
588bb7c…
|
aku
|
320 |
case '3': return 3; |
|
588bb7c…
|
aku
|
321 |
case '4': return 4; |
|
588bb7c…
|
aku
|
322 |
case '5': return 5; |
|
588bb7c…
|
aku
|
323 |
case '6': return 6; |
|
588bb7c…
|
aku
|
324 |
case '7': return 7; |
|
588bb7c…
|
aku
|
325 |
case '8': return 8; |
|
588bb7c…
|
aku
|
326 |
case '9': return 9; |
|
588bb7c…
|
aku
|
327 |
case 'a': case 'A': return 10; |
|
588bb7c…
|
aku
|
328 |
case 'b': case 'B': return 11; |
|
588bb7c…
|
aku
|
329 |
case 'c': case 'C': return 12; |
|
588bb7c…
|
aku
|
330 |
case 'd': case 'D': return 13; |
|
588bb7c…
|
aku
|
331 |
case 'e': case 'E': return 14; |
|
588bb7c…
|
aku
|
332 |
case 'f': case 'F': return 15; |
|
588bb7c…
|
aku
|
333 |
} |
|
588bb7c…
|
aku
|
334 |
return -1; |
|
588bb7c…
|
aku
|
335 |
} |
|
588bb7c…
|
aku
|
336 |
|
|
588bb7c…
|
aku
|
337 |
/* |
|
588bb7c…
|
aku
|
338 |
** Argument pEntry points to an entry in a stack frame hash table |
|
db0c512…
|
drh
|
339 |
** (Th_Frame.paVar). Decrement the reference count of the Th_Variable |
|
588bb7c…
|
aku
|
340 |
** structure that the entry points to. Free the Th_Variable if its |
|
588bb7c…
|
aku
|
341 |
** reference count reaches 0. |
|
588bb7c…
|
aku
|
342 |
** |
|
588bb7c…
|
aku
|
343 |
** Argument pContext is a pointer to the interpreter structure. |
|
1aeb272…
|
drh
|
344 |
** |
|
1aeb272…
|
drh
|
345 |
** Returns non-zero if the Th_Variable was actually freed. |
|
588bb7c…
|
aku
|
346 |
*/ |
|
1aeb272…
|
drh
|
347 |
static int thFreeVariable(Th_HashEntry *pEntry, void *pContext){ |
|
588bb7c…
|
aku
|
348 |
Th_Variable *pValue = (Th_Variable *)pEntry->pData; |
|
588bb7c…
|
aku
|
349 |
pValue->nRef--; |
|
588bb7c…
|
aku
|
350 |
assert( pValue->nRef>=0 ); |
|
588bb7c…
|
aku
|
351 |
if( pValue->nRef==0 ){ |
|
588bb7c…
|
aku
|
352 |
Th_Interp *interp = (Th_Interp *)pContext; |
|
588bb7c…
|
aku
|
353 |
Th_Free(interp, pValue->zData); |
|
588bb7c…
|
aku
|
354 |
if( pValue->pHash ){ |
|
588bb7c…
|
aku
|
355 |
Th_HashIterate(interp, pValue->pHash, thFreeVariable, pContext); |
|
588bb7c…
|
aku
|
356 |
Th_HashDelete(interp, pValue->pHash); |
|
588bb7c…
|
aku
|
357 |
} |
|
588bb7c…
|
aku
|
358 |
Th_Free(interp, pValue); |
|
1aeb272…
|
drh
|
359 |
pEntry->pData = 0; |
|
1aeb272…
|
drh
|
360 |
return 1; |
|
588bb7c…
|
aku
|
361 |
} |
|
1aeb272…
|
drh
|
362 |
return 0; |
|
588bb7c…
|
aku
|
363 |
} |
|
588bb7c…
|
aku
|
364 |
|
|
588bb7c…
|
aku
|
365 |
/* |
|
588bb7c…
|
aku
|
366 |
** Argument pEntry points to an entry in the command hash table |
|
588bb7c…
|
aku
|
367 |
** (Th_Interp.paCmd). Delete the Th_Command structure that the |
|
588bb7c…
|
aku
|
368 |
** entry points to. |
|
588bb7c…
|
aku
|
369 |
** |
|
588bb7c…
|
aku
|
370 |
** Argument pContext is a pointer to the interpreter structure. |
|
1aeb272…
|
drh
|
371 |
** |
|
1aeb272…
|
drh
|
372 |
** Always returns non-zero. |
|
588bb7c…
|
aku
|
373 |
*/ |
|
1aeb272…
|
drh
|
374 |
static int thFreeCommand(Th_HashEntry *pEntry, void *pContext){ |
|
588bb7c…
|
aku
|
375 |
Th_Command *pCommand = (Th_Command *)pEntry->pData; |
|
588bb7c…
|
aku
|
376 |
if( pCommand->xDel ){ |
|
588bb7c…
|
aku
|
377 |
pCommand->xDel((Th_Interp *)pContext, pCommand->pContext); |
|
588bb7c…
|
aku
|
378 |
} |
|
588bb7c…
|
aku
|
379 |
Th_Free((Th_Interp *)pContext, pEntry->pData); |
|
588bb7c…
|
aku
|
380 |
pEntry->pData = 0; |
|
810e6c1…
|
mistachkin
|
381 |
return 1; |
|
810e6c1…
|
mistachkin
|
382 |
} |
|
810e6c1…
|
mistachkin
|
383 |
|
|
810e6c1…
|
mistachkin
|
384 |
/* |
|
810e6c1…
|
mistachkin
|
385 |
** Argument pEntry points to an entry in a hash table. The key is |
|
810e6c1…
|
mistachkin
|
386 |
** the list element to be added. |
|
810e6c1…
|
mistachkin
|
387 |
** |
|
810e6c1…
|
mistachkin
|
388 |
** Argument pContext is a pointer to the Th_InterpAndList structure. |
|
810e6c1…
|
mistachkin
|
389 |
** |
|
810e6c1…
|
mistachkin
|
390 |
** Always returns non-zero. |
|
810e6c1…
|
mistachkin
|
391 |
*/ |
|
22b3d0b…
|
mistachkin
|
392 |
static int thListAppendHashKey(Th_HashEntry *pEntry, void *pContext){ |
|
810e6c1…
|
mistachkin
|
393 |
Th_InterpAndList *pInterpAndList = (Th_InterpAndList *)pContext; |
|
810e6c1…
|
mistachkin
|
394 |
Th_ListAppend(pInterpAndList->interp, pInterpAndList->pzList, |
|
810e6c1…
|
mistachkin
|
395 |
pInterpAndList->pnList, pEntry->zKey, pEntry->nKey); |
|
1aeb272…
|
drh
|
396 |
return 1; |
|
588bb7c…
|
aku
|
397 |
} |
|
588bb7c…
|
aku
|
398 |
|
|
588bb7c…
|
aku
|
399 |
/* |
|
588bb7c…
|
aku
|
400 |
** Push a new frame onto the stack. |
|
588bb7c…
|
aku
|
401 |
*/ |
|
588bb7c…
|
aku
|
402 |
static int thPushFrame(Th_Interp *interp, Th_Frame *pFrame){ |
|
588bb7c…
|
aku
|
403 |
pFrame->paVar = Th_HashNew(interp); |
|
588bb7c…
|
aku
|
404 |
pFrame->pCaller = interp->pFrame; |
|
588bb7c…
|
aku
|
405 |
interp->pFrame = pFrame; |
|
588bb7c…
|
aku
|
406 |
return TH_OK; |
|
588bb7c…
|
aku
|
407 |
} |
|
588bb7c…
|
aku
|
408 |
|
|
588bb7c…
|
aku
|
409 |
/* |
|
588bb7c…
|
aku
|
410 |
** Pop a frame off the top of the stack. |
|
588bb7c…
|
aku
|
411 |
*/ |
|
588bb7c…
|
aku
|
412 |
static void thPopFrame(Th_Interp *interp){ |
|
588bb7c…
|
aku
|
413 |
Th_Frame *pFrame = interp->pFrame; |
|
588bb7c…
|
aku
|
414 |
Th_HashIterate(interp, pFrame->paVar, thFreeVariable, (void *)interp); |
|
588bb7c…
|
aku
|
415 |
Th_HashDelete(interp, pFrame->paVar); |
|
588bb7c…
|
aku
|
416 |
interp->pFrame = pFrame->pCaller; |
|
588bb7c…
|
aku
|
417 |
} |
|
588bb7c…
|
aku
|
418 |
|
|
588bb7c…
|
aku
|
419 |
/* |
|
1ebe4b0…
|
mistachkin
|
420 |
** The first part of the string (zInput,nInput) contains an escape |
|
588bb7c…
|
aku
|
421 |
** sequence. Set *pnEscape to the number of bytes in the escape sequence. |
|
588bb7c…
|
aku
|
422 |
** If there is a parse error, return TH_ERROR and set the interpreter |
|
588bb7c…
|
aku
|
423 |
** result to an error message. Otherwise return TH_OK. |
|
588bb7c…
|
aku
|
424 |
*/ |
|
588bb7c…
|
aku
|
425 |
static int thNextEscape( |
|
588bb7c…
|
aku
|
426 |
Th_Interp *interp, |
|
1ebe4b0…
|
mistachkin
|
427 |
const char *zInput, |
|
1ebe4b0…
|
mistachkin
|
428 |
int nInput, |
|
588bb7c…
|
aku
|
429 |
int *pnEscape |
|
588bb7c…
|
aku
|
430 |
){ |
|
588bb7c…
|
aku
|
431 |
int i = 2; |
|
588bb7c…
|
aku
|
432 |
|
|
588bb7c…
|
aku
|
433 |
assert(nInput>0); |
|
588bb7c…
|
aku
|
434 |
assert(zInput[0]=='\\'); |
|
588bb7c…
|
aku
|
435 |
|
|
588bb7c…
|
aku
|
436 |
if( nInput<=1 ){ |
|
588bb7c…
|
aku
|
437 |
return TH_ERROR; |
|
588bb7c…
|
aku
|
438 |
} |
|
588bb7c…
|
aku
|
439 |
|
|
588bb7c…
|
aku
|
440 |
switch( zInput[1] ){ |
|
588bb7c…
|
aku
|
441 |
case 'x': i = 4; |
|
588bb7c…
|
aku
|
442 |
} |
|
588bb7c…
|
aku
|
443 |
|
|
588bb7c…
|
aku
|
444 |
if( i>nInput ){ |
|
588bb7c…
|
aku
|
445 |
return TH_ERROR; |
|
588bb7c…
|
aku
|
446 |
} |
|
588bb7c…
|
aku
|
447 |
*pnEscape = i; |
|
588bb7c…
|
aku
|
448 |
return TH_OK; |
|
588bb7c…
|
aku
|
449 |
} |
|
588bb7c…
|
aku
|
450 |
|
|
588bb7c…
|
aku
|
451 |
/* |
|
588bb7c…
|
aku
|
452 |
** The first part of the string (zInput,nInput) contains a variable |
|
1ebe4b0…
|
mistachkin
|
453 |
** reference. Set *pnVarname to the number of bytes in the variable |
|
1ebe4b0…
|
mistachkin
|
454 |
** reference. If there is a parse error, return TH_ERROR and set the |
|
588bb7c…
|
aku
|
455 |
** interpreter result to an error message. Otherwise return TH_OK. |
|
588bb7c…
|
aku
|
456 |
*/ |
|
588bb7c…
|
aku
|
457 |
int thNextVarname( |
|
588bb7c…
|
aku
|
458 |
Th_Interp *interp, |
|
1ebe4b0…
|
mistachkin
|
459 |
const char *zInput, |
|
1ebe4b0…
|
mistachkin
|
460 |
int nInput, |
|
588bb7c…
|
aku
|
461 |
int *pnVarname |
|
588bb7c…
|
aku
|
462 |
){ |
|
588bb7c…
|
aku
|
463 |
int i; |
|
588bb7c…
|
aku
|
464 |
|
|
588bb7c…
|
aku
|
465 |
assert(nInput>0); |
|
588bb7c…
|
aku
|
466 |
assert(zInput[0]=='$'); |
|
588bb7c…
|
aku
|
467 |
|
|
588bb7c…
|
aku
|
468 |
if( nInput>0 && zInput[1]=='{' ){ |
|
588bb7c…
|
aku
|
469 |
for(i=2; i<nInput && zInput[i]!='}'; i++); |
|
588bb7c…
|
aku
|
470 |
if( i==nInput ){ |
|
588bb7c…
|
aku
|
471 |
return TH_ERROR; |
|
588bb7c…
|
aku
|
472 |
} |
|
588bb7c…
|
aku
|
473 |
i++; |
|
588bb7c…
|
aku
|
474 |
}else{ |
|
588bb7c…
|
aku
|
475 |
i = 1; |
|
588bb7c…
|
aku
|
476 |
if( nInput>2 && zInput[1]==':' && zInput[2]==':' ){ |
|
588bb7c…
|
aku
|
477 |
i += 2; |
|
588bb7c…
|
aku
|
478 |
} |
|
588bb7c…
|
aku
|
479 |
for(; i<nInput; i++){ |
|
588bb7c…
|
aku
|
480 |
if( zInput[i]=='(' ){ |
|
588bb7c…
|
aku
|
481 |
for(i++; i<nInput; i++){ |
|
588bb7c…
|
aku
|
482 |
if( zInput[i]==')' ) break; |
|
588bb7c…
|
aku
|
483 |
if( zInput[i]=='\\' ) i++; |
|
588bb7c…
|
aku
|
484 |
if( zInput[i]=='{' || zInput[i]=='[' || zInput[i]=='"' ){ |
|
588bb7c…
|
aku
|
485 |
int nWord; |
|
588bb7c…
|
aku
|
486 |
int rc = thNextWord(interp, &zInput[i], nInput-i, &nWord, 0); |
|
588bb7c…
|
aku
|
487 |
if( rc!=TH_OK ){ |
|
588bb7c…
|
aku
|
488 |
return rc; |
|
588bb7c…
|
aku
|
489 |
} |
|
588bb7c…
|
aku
|
490 |
i += nWord; |
|
588bb7c…
|
aku
|
491 |
} |
|
588bb7c…
|
aku
|
492 |
} |
|
588bb7c…
|
aku
|
493 |
if( i>=nInput ){ |
|
588bb7c…
|
aku
|
494 |
Th_ErrorMessage(interp, "Unmatched brackets:", zInput, nInput); |
|
588bb7c…
|
aku
|
495 |
return TH_ERROR; |
|
588bb7c…
|
aku
|
496 |
} |
|
588bb7c…
|
aku
|
497 |
i++; |
|
588bb7c…
|
aku
|
498 |
break; |
|
588bb7c…
|
aku
|
499 |
} |
|
588bb7c…
|
aku
|
500 |
if( !th_isalnum(zInput[i]) && zInput[i]!='_' ) break; |
|
588bb7c…
|
aku
|
501 |
} |
|
588bb7c…
|
aku
|
502 |
} |
|
588bb7c…
|
aku
|
503 |
|
|
588bb7c…
|
aku
|
504 |
*pnVarname = i; |
|
588bb7c…
|
aku
|
505 |
return TH_OK; |
|
588bb7c…
|
aku
|
506 |
} |
|
588bb7c…
|
aku
|
507 |
|
|
588bb7c…
|
aku
|
508 |
/* |
|
588bb7c…
|
aku
|
509 |
** The first part of the string (zInput,nInput) contains a command |
|
1ebe4b0…
|
mistachkin
|
510 |
** enclosed in a "[]" block. Set *pnCommand to the number of bytes in |
|
1ebe4b0…
|
mistachkin
|
511 |
** the variable reference. If there is a parse error, return TH_ERROR |
|
1ebe4b0…
|
mistachkin
|
512 |
** and set the interpreter result to an error message. Otherwise return |
|
588bb7c…
|
aku
|
513 |
** TH_OK. |
|
588bb7c…
|
aku
|
514 |
*/ |
|
588bb7c…
|
aku
|
515 |
int thNextCommand( |
|
588bb7c…
|
aku
|
516 |
Th_Interp *interp, |
|
1ebe4b0…
|
mistachkin
|
517 |
const char *zInput, |
|
1ebe4b0…
|
mistachkin
|
518 |
int nInput, |
|
588bb7c…
|
aku
|
519 |
int *pnCommand |
|
588bb7c…
|
aku
|
520 |
){ |
|
588bb7c…
|
aku
|
521 |
int nBrace = 0; |
|
588bb7c…
|
aku
|
522 |
int nSquare = 0; |
|
588bb7c…
|
aku
|
523 |
int i; |
|
588bb7c…
|
aku
|
524 |
|
|
588bb7c…
|
aku
|
525 |
assert(nInput>0); |
|
588bb7c…
|
aku
|
526 |
assert( zInput[0]=='[' || zInput[0]=='{' ); |
|
588bb7c…
|
aku
|
527 |
|
|
588bb7c…
|
aku
|
528 |
for(i=0; i<nInput && (i==0 || nBrace>0 || nSquare>0); i++){ |
|
588bb7c…
|
aku
|
529 |
switch( zInput[i] ){ |
|
588bb7c…
|
aku
|
530 |
case '\\': i++; break; |
|
588bb7c…
|
aku
|
531 |
case '{': nBrace++; break; |
|
588bb7c…
|
aku
|
532 |
case '}': nBrace--; break; |
|
588bb7c…
|
aku
|
533 |
case '[': nSquare++; break; |
|
588bb7c…
|
aku
|
534 |
case ']': nSquare--; break; |
|
588bb7c…
|
aku
|
535 |
} |
|
588bb7c…
|
aku
|
536 |
} |
|
588bb7c…
|
aku
|
537 |
if( nBrace || nSquare ){ |
|
588bb7c…
|
aku
|
538 |
return TH_ERROR; |
|
588bb7c…
|
aku
|
539 |
} |
|
588bb7c…
|
aku
|
540 |
|
|
588bb7c…
|
aku
|
541 |
*pnCommand = i; |
|
588bb7c…
|
aku
|
542 |
|
|
588bb7c…
|
aku
|
543 |
return TH_OK; |
|
588bb7c…
|
aku
|
544 |
} |
|
588bb7c…
|
aku
|
545 |
|
|
588bb7c…
|
aku
|
546 |
/* |
|
1ebe4b0…
|
mistachkin
|
547 |
** Set *pnSpace to the number of whitespace bytes at the start of |
|
588bb7c…
|
aku
|
548 |
** input string (zInput, nInput). Always return TH_OK. |
|
588bb7c…
|
aku
|
549 |
*/ |
|
588bb7c…
|
aku
|
550 |
int thNextSpace( |
|
588bb7c…
|
aku
|
551 |
Th_Interp *interp, |
|
1ebe4b0…
|
mistachkin
|
552 |
const char *zInput, |
|
1ebe4b0…
|
mistachkin
|
553 |
int nInput, |
|
588bb7c…
|
aku
|
554 |
int *pnSpace |
|
588bb7c…
|
aku
|
555 |
){ |
|
588bb7c…
|
aku
|
556 |
int i; |
|
588bb7c…
|
aku
|
557 |
for(i=0; i<nInput && th_isspace(zInput[i]); i++); |
|
588bb7c…
|
aku
|
558 |
*pnSpace = i; |
|
588bb7c…
|
aku
|
559 |
return TH_OK; |
|
588bb7c…
|
aku
|
560 |
} |
|
588bb7c…
|
aku
|
561 |
|
|
588bb7c…
|
aku
|
562 |
/* |
|
588bb7c…
|
aku
|
563 |
** The first byte of the string (zInput,nInput) is not white-space. |
|
588bb7c…
|
aku
|
564 |
** Set *pnWord to the number of bytes in the th1 word that starts |
|
588bb7c…
|
aku
|
565 |
** with this byte. If a complete word cannot be parsed or some other |
|
1ebe4b0…
|
mistachkin
|
566 |
** error occurs, return TH_ERROR and set the interpreter result to |
|
588bb7c…
|
aku
|
567 |
** an error message. Otherwise return TH_OK. |
|
588bb7c…
|
aku
|
568 |
** |
|
1ebe4b0…
|
mistachkin
|
569 |
** If the isCmd argument is non-zero, then an unescaped ";" byte not |
|
1ebe4b0…
|
mistachkin
|
570 |
** located inside of a block or quoted string is considered to mark |
|
588bb7c…
|
aku
|
571 |
** the end of the word. |
|
588bb7c…
|
aku
|
572 |
*/ |
|
588bb7c…
|
aku
|
573 |
static int thNextWord( |
|
588bb7c…
|
aku
|
574 |
Th_Interp *interp, |
|
1ebe4b0…
|
mistachkin
|
575 |
const char *zInput, |
|
1ebe4b0…
|
mistachkin
|
576 |
int nInput, |
|
588bb7c…
|
aku
|
577 |
int *pnWord, |
|
588bb7c…
|
aku
|
578 |
int isCmd |
|
588bb7c…
|
aku
|
579 |
){ |
|
588bb7c…
|
aku
|
580 |
int iEnd = 0; |
|
588bb7c…
|
aku
|
581 |
|
|
588bb7c…
|
aku
|
582 |
assert( !th_isspace(zInput[0]) ); |
|
588bb7c…
|
aku
|
583 |
|
|
588bb7c…
|
aku
|
584 |
if( zInput[0]=='"' ){ |
|
588bb7c…
|
aku
|
585 |
/* The word is terminated by the next unescaped '"' character. */ |
|
588bb7c…
|
aku
|
586 |
iEnd++; |
|
588bb7c…
|
aku
|
587 |
while( iEnd<nInput && zInput[iEnd]!='"' ){ |
|
588bb7c…
|
aku
|
588 |
if( zInput[iEnd]=='\\' ){ |
|
588bb7c…
|
aku
|
589 |
iEnd++; |
|
588bb7c…
|
aku
|
590 |
} |
|
588bb7c…
|
aku
|
591 |
iEnd++; |
|
588bb7c…
|
aku
|
592 |
} |
|
588bb7c…
|
aku
|
593 |
iEnd++; |
|
588bb7c…
|
aku
|
594 |
}else{ |
|
588bb7c…
|
aku
|
595 |
int nBrace = 0; |
|
588bb7c…
|
aku
|
596 |
int nSq = 0; |
|
588bb7c…
|
aku
|
597 |
while( iEnd<nInput && (nBrace>0 || nSq>0 || |
|
588bb7c…
|
aku
|
598 |
(!th_isspace(zInput[iEnd]) && (!isCmd || zInput[iEnd]!=';')) |
|
588bb7c…
|
aku
|
599 |
)){ |
|
588bb7c…
|
aku
|
600 |
switch( zInput[iEnd] ){ |
|
588bb7c…
|
aku
|
601 |
case '\\': iEnd++; break; |
|
588bb7c…
|
aku
|
602 |
case '{': if( nSq==0 ) nBrace++; break; |
|
588bb7c…
|
aku
|
603 |
case '}': if( nSq==0 ) nBrace--; break; |
|
588bb7c…
|
aku
|
604 |
case '[': if( nBrace==0 ) nSq++; break; |
|
588bb7c…
|
aku
|
605 |
case ']': if( nBrace==0 ) nSq--; break; |
|
588bb7c…
|
aku
|
606 |
} |
|
588bb7c…
|
aku
|
607 |
iEnd++; |
|
588bb7c…
|
aku
|
608 |
} |
|
588bb7c…
|
aku
|
609 |
if( nBrace>0 || nSq>0 ){ |
|
588bb7c…
|
aku
|
610 |
/* Parse error */ |
|
2ba71f5…
|
stephan
|
611 |
Th_SetResult(interp, "parse error", -1); |
|
588bb7c…
|
aku
|
612 |
return TH_ERROR; |
|
588bb7c…
|
aku
|
613 |
} |
|
588bb7c…
|
aku
|
614 |
} |
|
588bb7c…
|
aku
|
615 |
|
|
588bb7c…
|
aku
|
616 |
if( iEnd>nInput ){ |
|
588bb7c…
|
aku
|
617 |
/* Parse error */ |
|
2ba71f5…
|
stephan
|
618 |
Th_SetResult(interp, "parse error", -1); |
|
588bb7c…
|
aku
|
619 |
return TH_ERROR; |
|
588bb7c…
|
aku
|
620 |
} |
|
588bb7c…
|
aku
|
621 |
*pnWord = iEnd; |
|
588bb7c…
|
aku
|
622 |
return TH_OK; |
|
588bb7c…
|
aku
|
623 |
} |
|
588bb7c…
|
aku
|
624 |
|
|
588bb7c…
|
aku
|
625 |
/* |
|
588bb7c…
|
aku
|
626 |
** The input string (zWord, nWord) contains a th1 script enclosed in |
|
588bb7c…
|
aku
|
627 |
** a [] block. Perform substitution on the input string and store the |
|
588bb7c…
|
aku
|
628 |
** resulting string in the interpreter result. |
|
588bb7c…
|
aku
|
629 |
*/ |
|
588bb7c…
|
aku
|
630 |
static int thSubstCommand( |
|
588bb7c…
|
aku
|
631 |
Th_Interp *interp, |
|
0c99a15…
|
drh
|
632 |
const char *zWord, |
|
588bb7c…
|
aku
|
633 |
int nWord |
|
588bb7c…
|
aku
|
634 |
){ |
|
588bb7c…
|
aku
|
635 |
assert(nWord>=2); |
|
588bb7c…
|
aku
|
636 |
assert(zWord[0]=='[' && zWord[nWord-1]==']'); |
|
588bb7c…
|
aku
|
637 |
return thEvalLocal(interp, &zWord[1], nWord-2); |
|
588bb7c…
|
aku
|
638 |
} |
|
588bb7c…
|
aku
|
639 |
|
|
588bb7c…
|
aku
|
640 |
/* |
|
588bb7c…
|
aku
|
641 |
** The input string (zWord, nWord) contains a th1 variable reference |
|
1ebe4b0…
|
mistachkin
|
642 |
** (a '$' byte followed by a variable name). Perform substitution on |
|
1ebe4b0…
|
mistachkin
|
643 |
** the input string and store the resulting string in the interpreter |
|
588bb7c…
|
aku
|
644 |
** result. |
|
588bb7c…
|
aku
|
645 |
*/ |
|
588bb7c…
|
aku
|
646 |
static int thSubstVarname( |
|
588bb7c…
|
aku
|
647 |
Th_Interp *interp, |
|
0c99a15…
|
drh
|
648 |
const char *zWord, |
|
588bb7c…
|
aku
|
649 |
int nWord |
|
588bb7c…
|
aku
|
650 |
){ |
|
588bb7c…
|
aku
|
651 |
assert(nWord>=1); |
|
588bb7c…
|
aku
|
652 |
assert(zWord[0]=='$'); |
|
588bb7c…
|
aku
|
653 |
assert(nWord==1 || zWord[1]!='{' || zWord[nWord-1]=='}'); |
|
588bb7c…
|
aku
|
654 |
if( nWord>1 && zWord[1]=='{' ){ |
|
588bb7c…
|
aku
|
655 |
zWord++; |
|
588bb7c…
|
aku
|
656 |
nWord -= 2; |
|
588bb7c…
|
aku
|
657 |
}else if( zWord[nWord-1]==')' ){ |
|
588bb7c…
|
aku
|
658 |
int i; |
|
588bb7c…
|
aku
|
659 |
for(i=1; i<nWord && zWord[i]!='('; i++); |
|
588bb7c…
|
aku
|
660 |
if( i<nWord ){ |
|
588bb7c…
|
aku
|
661 |
Buffer varname; |
|
588bb7c…
|
aku
|
662 |
int nInner; |
|
0c99a15…
|
drh
|
663 |
const char *zInner; |
|
588bb7c…
|
aku
|
664 |
|
|
588bb7c…
|
aku
|
665 |
int rc = thSubstWord(interp, &zWord[i+1], nWord-i-2); |
|
588bb7c…
|
aku
|
666 |
if( rc!=TH_OK ) return rc; |
|
588bb7c…
|
aku
|
667 |
|
|
588bb7c…
|
aku
|
668 |
zInner = Th_GetResult(interp, &nInner); |
|
588bb7c…
|
aku
|
669 |
thBufferInit(&varname); |
|
588bb7c…
|
aku
|
670 |
thBufferWrite(interp, &varname, &zWord[1], i); |
|
588bb7c…
|
aku
|
671 |
thBufferWrite(interp, &varname, zInner, nInner); |
|
b26eea7…
|
drh
|
672 |
thBufferAddChar(interp, &varname, ')'); |
|
588bb7c…
|
aku
|
673 |
rc = Th_GetVar(interp, varname.zBuf, varname.nBuf); |
|
588bb7c…
|
aku
|
674 |
thBufferFree(interp, &varname); |
|
588bb7c…
|
aku
|
675 |
return rc; |
|
588bb7c…
|
aku
|
676 |
} |
|
588bb7c…
|
aku
|
677 |
} |
|
588bb7c…
|
aku
|
678 |
return Th_GetVar(interp, &zWord[1], nWord-1); |
|
588bb7c…
|
aku
|
679 |
} |
|
588bb7c…
|
aku
|
680 |
|
|
588bb7c…
|
aku
|
681 |
/* |
|
588bb7c…
|
aku
|
682 |
** The input string (zWord, nWord) contains a th1 escape sequence. |
|
1ebe4b0…
|
mistachkin
|
683 |
** Perform substitution on the input string and store the resulting |
|
588bb7c…
|
aku
|
684 |
** string in the interpreter result. |
|
588bb7c…
|
aku
|
685 |
*/ |
|
588bb7c…
|
aku
|
686 |
static int thSubstEscape( |
|
588bb7c…
|
aku
|
687 |
Th_Interp *interp, |
|
0c99a15…
|
drh
|
688 |
const char *zWord, |
|
588bb7c…
|
aku
|
689 |
int nWord |
|
588bb7c…
|
aku
|
690 |
){ |
|
0c99a15…
|
drh
|
691 |
char c; |
|
588bb7c…
|
aku
|
692 |
|
|
588bb7c…
|
aku
|
693 |
assert(nWord>=2); |
|
588bb7c…
|
aku
|
694 |
assert(zWord[0]=='\\'); |
|
588bb7c…
|
aku
|
695 |
|
|
588bb7c…
|
aku
|
696 |
switch( zWord[1] ){ |
|
588bb7c…
|
aku
|
697 |
case 'x': { |
|
588bb7c…
|
aku
|
698 |
assert(nWord==4); |
|
588bb7c…
|
aku
|
699 |
c = ((thHexdigit(zWord[2])<<4) + thHexdigit(zWord[3])); |
|
588bb7c…
|
aku
|
700 |
break; |
|
588bb7c…
|
aku
|
701 |
} |
|
588bb7c…
|
aku
|
702 |
case 'n': { |
|
588bb7c…
|
aku
|
703 |
c = '\n'; |
|
588bb7c…
|
aku
|
704 |
break; |
|
588bb7c…
|
aku
|
705 |
} |
|
588bb7c…
|
aku
|
706 |
default: { |
|
588bb7c…
|
aku
|
707 |
assert(nWord==2); |
|
588bb7c…
|
aku
|
708 |
c = zWord[1]; |
|
588bb7c…
|
aku
|
709 |
break; |
|
588bb7c…
|
aku
|
710 |
} |
|
588bb7c…
|
aku
|
711 |
} |
|
588bb7c…
|
aku
|
712 |
|
|
588bb7c…
|
aku
|
713 |
Th_SetResult(interp, &c, 1); |
|
588bb7c…
|
aku
|
714 |
return TH_OK; |
|
588bb7c…
|
aku
|
715 |
} |
|
588bb7c…
|
aku
|
716 |
|
|
588bb7c…
|
aku
|
717 |
/* |
|
588bb7c…
|
aku
|
718 |
** The input string (zWord, nWord) contains a th1 word. Perform |
|
1ebe4b0…
|
mistachkin
|
719 |
** substitution on the input string and store the resulting |
|
588bb7c…
|
aku
|
720 |
** string in the interpreter result. |
|
588bb7c…
|
aku
|
721 |
*/ |
|
588bb7c…
|
aku
|
722 |
static int thSubstWord( |
|
588bb7c…
|
aku
|
723 |
Th_Interp *interp, |
|
0c99a15…
|
drh
|
724 |
const char *zWord, |
|
588bb7c…
|
aku
|
725 |
int nWord |
|
588bb7c…
|
aku
|
726 |
){ |
|
588bb7c…
|
aku
|
727 |
int rc = TH_OK; |
|
588bb7c…
|
aku
|
728 |
Buffer output; |
|
588bb7c…
|
aku
|
729 |
int i; |
|
2116238…
|
drh
|
730 |
int nn = TH1_LEN(nWord); |
|
588bb7c…
|
aku
|
731 |
|
|
588bb7c…
|
aku
|
732 |
thBufferInit(&output); |
|
588bb7c…
|
aku
|
733 |
|
|
2116238…
|
drh
|
734 |
if( nn>1 && (zWord[0]=='{' && zWord[nn-1]=='}') ){ |
|
2116238…
|
drh
|
735 |
thBufferWrite(interp, &output, &zWord[1], nn-2); |
|
588bb7c…
|
aku
|
736 |
}else{ |
|
588bb7c…
|
aku
|
737 |
|
|
588bb7c…
|
aku
|
738 |
/* If the word is surrounded by double-quotes strip these away. */ |
|
2116238…
|
drh
|
739 |
if( nn>1 && (zWord[0]=='"' && zWord[nn-1]=='"') ){ |
|
588bb7c…
|
aku
|
740 |
zWord++; |
|
2116238…
|
drh
|
741 |
nn -= 2; |
|
588bb7c…
|
aku
|
742 |
} |
|
588bb7c…
|
aku
|
743 |
|
|
2116238…
|
drh
|
744 |
for(i=0; rc==TH_OK && i<nn; i++){ |
|
588bb7c…
|
aku
|
745 |
int nGet; |
|
588bb7c…
|
aku
|
746 |
|
|
0c99a15…
|
drh
|
747 |
int (*xGet)(Th_Interp *, const char*, int, int *) = 0; |
|
0c99a15…
|
drh
|
748 |
int (*xSubst)(Th_Interp *, const char*, int) = 0; |
|
588bb7c…
|
aku
|
749 |
|
|
588bb7c…
|
aku
|
750 |
switch( zWord[i] ){ |
|
588bb7c…
|
aku
|
751 |
case '\\': |
|
1ebe4b0…
|
mistachkin
|
752 |
xGet = thNextEscape; xSubst = thSubstEscape; |
|
588bb7c…
|
aku
|
753 |
break; |
|
588bb7c…
|
aku
|
754 |
case '[': |
|
588bb7c…
|
aku
|
755 |
if( !interp->isListMode ){ |
|
1ebe4b0…
|
mistachkin
|
756 |
xGet = thNextCommand; xSubst = thSubstCommand; |
|
588bb7c…
|
aku
|
757 |
break; |
|
588bb7c…
|
aku
|
758 |
} |
|
588bb7c…
|
aku
|
759 |
case '$': |
|
588bb7c…
|
aku
|
760 |
if( !interp->isListMode ){ |
|
1ebe4b0…
|
mistachkin
|
761 |
xGet = thNextVarname; xSubst = thSubstVarname; |
|
588bb7c…
|
aku
|
762 |
break; |
|
588bb7c…
|
aku
|
763 |
} |
|
588bb7c…
|
aku
|
764 |
default: { |
|
b26eea7…
|
drh
|
765 |
thBufferAddChar(interp, &output, zWord[i]); |
|
588bb7c…
|
aku
|
766 |
continue; /* Go to the next iteration of the for(...) loop */ |
|
588bb7c…
|
aku
|
767 |
} |
|
588bb7c…
|
aku
|
768 |
} |
|
588bb7c…
|
aku
|
769 |
|
|
2116238…
|
drh
|
770 |
rc = xGet(interp, &zWord[i], nn-i, &nGet); |
|
588bb7c…
|
aku
|
771 |
if( rc==TH_OK ){ |
|
588bb7c…
|
aku
|
772 |
rc = xSubst(interp, &zWord[i], nGet); |
|
588bb7c…
|
aku
|
773 |
} |
|
588bb7c…
|
aku
|
774 |
if( rc==TH_OK ){ |
|
0c99a15…
|
drh
|
775 |
const char *zRes; |
|
588bb7c…
|
aku
|
776 |
int nRes; |
|
588bb7c…
|
aku
|
777 |
zRes = Th_GetResult(interp, &nRes); |
|
b26eea7…
|
drh
|
778 |
thBufferWrite(interp, &output, zRes, nRes); |
|
588bb7c…
|
aku
|
779 |
i += (nGet-1); |
|
588bb7c…
|
aku
|
780 |
} |
|
588bb7c…
|
aku
|
781 |
} |
|
588bb7c…
|
aku
|
782 |
} |
|
588bb7c…
|
aku
|
783 |
|
|
588bb7c…
|
aku
|
784 |
if( rc==TH_OK ){ |
|
2116238…
|
drh
|
785 |
Th_SetResult(interp, output.zBuf, output.nBuf|output.bTaint); |
|
588bb7c…
|
aku
|
786 |
} |
|
588bb7c…
|
aku
|
787 |
thBufferFree(interp, &output); |
|
588bb7c…
|
aku
|
788 |
return rc; |
|
588bb7c…
|
aku
|
789 |
} |
|
588bb7c…
|
aku
|
790 |
|
|
588bb7c…
|
aku
|
791 |
/* |
|
588bb7c…
|
aku
|
792 |
** Return true if one of the following is true of the buffer pointed |
|
588bb7c…
|
aku
|
793 |
** to by zInput, length nInput: |
|
588bb7c…
|
aku
|
794 |
** |
|
588bb7c…
|
aku
|
795 |
** + It is empty, or |
|
588bb7c…
|
aku
|
796 |
** + It contains nothing but white-space, or |
|
1ebe4b0…
|
mistachkin
|
797 |
** + It contains no non-white-space characters before the first |
|
588bb7c…
|
aku
|
798 |
** newline character. |
|
588bb7c…
|
aku
|
799 |
** |
|
588bb7c…
|
aku
|
800 |
** Otherwise return false. |
|
588bb7c…
|
aku
|
801 |
*/ |
|
0c99a15…
|
drh
|
802 |
static int thEndOfLine(const char *zInput, int nInput){ |
|
588bb7c…
|
aku
|
803 |
int i; |
|
588bb7c…
|
aku
|
804 |
for(i=0; i<nInput && zInput[i]!='\n' && th_isspace(zInput[i]); i++); |
|
588bb7c…
|
aku
|
805 |
return ((i==nInput || zInput[i]=='\n')?1:0); |
|
588bb7c…
|
aku
|
806 |
} |
|
588bb7c…
|
aku
|
807 |
|
|
588bb7c…
|
aku
|
808 |
/* |
|
588bb7c…
|
aku
|
809 |
** This function splits the supplied th1 list (contained in buffer zList, |
|
588bb7c…
|
aku
|
810 |
** size nList) into elements and performs word-substitution on each |
|
588bb7c…
|
aku
|
811 |
** element. If the Th_Interp.isListMode variable is true, then only |
|
588bb7c…
|
aku
|
812 |
** escape sequences are substituted (used by the Th_SplitList() function). |
|
588bb7c…
|
aku
|
813 |
** If Th_Interp.isListMode is false, then variable and command substitution |
|
588bb7c…
|
aku
|
814 |
** is also performed (used by Th_Eval()). |
|
588bb7c…
|
aku
|
815 |
** |
|
588bb7c…
|
aku
|
816 |
** If zList/nList does not contain a valid list, TH_ERROR is returned |
|
588bb7c…
|
aku
|
817 |
** and an error message stored in interp. |
|
588bb7c…
|
aku
|
818 |
** |
|
588bb7c…
|
aku
|
819 |
** If TH_OK is returned and pazElem is not NULL, the caller should free the |
|
588bb7c…
|
aku
|
820 |
** pointer written to (*pazElem) using Th_Free(). This releases memory |
|
588bb7c…
|
aku
|
821 |
** allocated for both the (*pazElem) and (*panElem) arrays. Example: |
|
588bb7c…
|
aku
|
822 |
** |
|
0c99a15…
|
drh
|
823 |
** char **argv; |
|
588bb7c…
|
aku
|
824 |
** int *argl; |
|
588bb7c…
|
aku
|
825 |
** int argc; |
|
588bb7c…
|
aku
|
826 |
** |
|
588bb7c…
|
aku
|
827 |
** // After this call, argv and argl point to valid arrays. The |
|
588bb7c…
|
aku
|
828 |
** // number of elements in each is argc. |
|
588bb7c…
|
aku
|
829 |
** // |
|
588bb7c…
|
aku
|
830 |
** Th_SplitList(interp, zList, nList, &argv, &argl, &argc); |
|
588bb7c…
|
aku
|
831 |
** |
|
588bb7c…
|
aku
|
832 |
** // Free all memory allocated by Th_SplitList(). The arrays pointed |
|
588bb7c…
|
aku
|
833 |
** // to by argv and argl are invalidated by this call. |
|
588bb7c…
|
aku
|
834 |
** // |
|
588bb7c…
|
aku
|
835 |
** Th_Free(interp, argv); |
|
588bb7c…
|
aku
|
836 |
** |
|
1ebe4b0…
|
mistachkin
|
837 |
*/ |
|
588bb7c…
|
aku
|
838 |
static int thSplitList( |
|
588bb7c…
|
aku
|
839 |
Th_Interp *interp, /* Interpreter context */ |
|
1ebe4b0…
|
mistachkin
|
840 |
const char *zList, /* Pointer to buffer containing input list */ |
|
588bb7c…
|
aku
|
841 |
int nList, /* Size of buffer pointed to by zList */ |
|
1ebe4b0…
|
mistachkin
|
842 |
char ***pazElem, /* OUT: Array of list elements */ |
|
588bb7c…
|
aku
|
843 |
int **panElem, /* OUT: Lengths of each list element */ |
|
588bb7c…
|
aku
|
844 |
int *pnCount /* OUT: Number of list elements */ |
|
588bb7c…
|
aku
|
845 |
){ |
|
588bb7c…
|
aku
|
846 |
int rc = TH_OK; |
|
588bb7c…
|
aku
|
847 |
|
|
588bb7c…
|
aku
|
848 |
Buffer strbuf; |
|
588bb7c…
|
aku
|
849 |
Buffer lenbuf; |
|
588bb7c…
|
aku
|
850 |
int nCount = 0; |
|
588bb7c…
|
aku
|
851 |
|
|
0c99a15…
|
drh
|
852 |
const char *zInput = zList; |
|
2116238…
|
drh
|
853 |
int nInput = TH1_LEN(nList); |
|
588bb7c…
|
aku
|
854 |
|
|
588bb7c…
|
aku
|
855 |
thBufferInit(&strbuf); |
|
588bb7c…
|
aku
|
856 |
thBufferInit(&lenbuf); |
|
588bb7c…
|
aku
|
857 |
|
|
588bb7c…
|
aku
|
858 |
while( nInput>0 ){ |
|
0c99a15…
|
drh
|
859 |
const char *zWord; |
|
588bb7c…
|
aku
|
860 |
int nWord; |
|
588bb7c…
|
aku
|
861 |
|
|
588bb7c…
|
aku
|
862 |
thNextSpace(interp, zInput, nInput, &nWord); |
|
588bb7c…
|
aku
|
863 |
zInput += nWord; |
|
2116238…
|
drh
|
864 |
nInput = TH1_LEN(nList)-(zInput-zList); |
|
588bb7c…
|
aku
|
865 |
|
|
588bb7c…
|
aku
|
866 |
if( TH_OK!=(rc = thNextWord(interp, zInput, nInput, &nWord, 0)) |
|
588bb7c…
|
aku
|
867 |
|| TH_OK!=(rc = thSubstWord(interp, zInput, nWord)) |
|
588bb7c…
|
aku
|
868 |
){ |
|
588bb7c…
|
aku
|
869 |
goto finish; |
|
588bb7c…
|
aku
|
870 |
} |
|
2116238…
|
drh
|
871 |
zInput = &zInput[TH1_LEN(nWord)]; |
|
2116238…
|
drh
|
872 |
nInput = TH1_LEN(nList)-(zInput-zList); |
|
588bb7c…
|
aku
|
873 |
if( nWord>0 ){ |
|
588bb7c…
|
aku
|
874 |
zWord = Th_GetResult(interp, &nWord); |
|
588bb7c…
|
aku
|
875 |
thBufferWrite(interp, &strbuf, zWord, nWord); |
|
b26eea7…
|
drh
|
876 |
thBufferAddChar(interp, &strbuf, 0); |
|
588bb7c…
|
aku
|
877 |
thBufferWrite(interp, &lenbuf, &nWord, sizeof(int)); |
|
588bb7c…
|
aku
|
878 |
nCount++; |
|
588bb7c…
|
aku
|
879 |
} |
|
588bb7c…
|
aku
|
880 |
} |
|
53db40e…
|
drh
|
881 |
assert((int)(lenbuf.nBuf/sizeof(int))==nCount); |
|
588bb7c…
|
aku
|
882 |
|
|
588bb7c…
|
aku
|
883 |
assert((pazElem && panElem) || (!pazElem && !panElem)); |
|
588bb7c…
|
aku
|
884 |
if( pazElem && rc==TH_OK ){ |
|
588bb7c…
|
aku
|
885 |
int i; |
|
1ebe4b0…
|
mistachkin
|
886 |
char *zElem; |
|
588bb7c…
|
aku
|
887 |
int *anElem; |
|
0c99a15…
|
drh
|
888 |
char **azElem = Th_Malloc(interp, |
|
1ebe4b0…
|
mistachkin
|
889 |
sizeof(char*) * nCount + /* azElem */ |
|
588bb7c…
|
aku
|
890 |
sizeof(int) * nCount + /* anElem */ |
|
588bb7c…
|
aku
|
891 |
strbuf.nBuf /* space for list element strings */ |
|
588bb7c…
|
aku
|
892 |
); |
|
588bb7c…
|
aku
|
893 |
anElem = (int *)&azElem[nCount]; |
|
0c99a15…
|
drh
|
894 |
zElem = (char *)&anElem[nCount]; |
|
7ae4b1a…
|
drh
|
895 |
th_memcpy(anElem, lenbuf.zBuf, lenbuf.nBuf); |
|
7ae4b1a…
|
drh
|
896 |
th_memcpy(zElem, strbuf.zBuf, strbuf.nBuf); |
|
588bb7c…
|
aku
|
897 |
for(i=0; i<nCount;i++){ |
|
588bb7c…
|
aku
|
898 |
azElem[i] = zElem; |
|
2116238…
|
drh
|
899 |
zElem += (TH1_LEN(anElem[i]) + 1); |
|
588bb7c…
|
aku
|
900 |
} |
|
588bb7c…
|
aku
|
901 |
*pazElem = azElem; |
|
588bb7c…
|
aku
|
902 |
*panElem = anElem; |
|
588bb7c…
|
aku
|
903 |
} |
|
588bb7c…
|
aku
|
904 |
if( pnCount ){ |
|
588bb7c…
|
aku
|
905 |
*pnCount = nCount; |
|
588bb7c…
|
aku
|
906 |
} |
|
1ebe4b0…
|
mistachkin
|
907 |
|
|
588bb7c…
|
aku
|
908 |
finish: |
|
588bb7c…
|
aku
|
909 |
thBufferFree(interp, &strbuf); |
|
588bb7c…
|
aku
|
910 |
thBufferFree(interp, &lenbuf); |
|
588bb7c…
|
aku
|
911 |
return rc; |
|
588bb7c…
|
aku
|
912 |
} |
|
588bb7c…
|
aku
|
913 |
|
|
588bb7c…
|
aku
|
914 |
/* |
|
588bb7c…
|
aku
|
915 |
** Evaluate the th1 script contained in the string (zProgram, nProgram) |
|
588bb7c…
|
aku
|
916 |
** in the current stack frame. |
|
588bb7c…
|
aku
|
917 |
*/ |
|
0c99a15…
|
drh
|
918 |
static int thEvalLocal(Th_Interp *interp, const char *zProgram, int nProgram){ |
|
588bb7c…
|
aku
|
919 |
int rc = TH_OK; |
|
0c99a15…
|
drh
|
920 |
const char *zInput = zProgram; |
|
2116238…
|
drh
|
921 |
int nInput = TH1_LEN(nProgram); |
|
588bb7c…
|
aku
|
922 |
|
|
2116238…
|
drh
|
923 |
if( TH1_TAINTED(nProgram) |
|
2116238…
|
drh
|
924 |
&& Th_ReportTaint(interp, "script", zProgram, nProgram) |
|
2116238…
|
drh
|
925 |
){ |
|
2116238…
|
drh
|
926 |
return TH_ERROR; |
|
2116238…
|
drh
|
927 |
} |
|
588bb7c…
|
aku
|
928 |
while( rc==TH_OK && nInput ){ |
|
588bb7c…
|
aku
|
929 |
Th_HashEntry *pEntry; |
|
588bb7c…
|
aku
|
930 |
int nSpace; |
|
0c99a15…
|
drh
|
931 |
const char *zFirst; |
|
588bb7c…
|
aku
|
932 |
|
|
0c99a15…
|
drh
|
933 |
char **argv; |
|
588bb7c…
|
aku
|
934 |
int *argl; |
|
588bb7c…
|
aku
|
935 |
int argc; |
|
588bb7c…
|
aku
|
936 |
|
|
588bb7c…
|
aku
|
937 |
assert(nInput>=0); |
|
588bb7c…
|
aku
|
938 |
|
|
588bb7c…
|
aku
|
939 |
/* Skip a semi-colon */ |
|
588bb7c…
|
aku
|
940 |
if( *zInput==';' ){ |
|
588bb7c…
|
aku
|
941 |
zInput++; |
|
588bb7c…
|
aku
|
942 |
nInput--; |
|
588bb7c…
|
aku
|
943 |
} |
|
588bb7c…
|
aku
|
944 |
|
|
588bb7c…
|
aku
|
945 |
/* Skip past leading white-space. */ |
|
588bb7c…
|
aku
|
946 |
thNextSpace(interp, zInput, nInput, &nSpace); |
|
588bb7c…
|
aku
|
947 |
zInput += nSpace; |
|
588bb7c…
|
aku
|
948 |
nInput -= nSpace; |
|
588bb7c…
|
aku
|
949 |
zFirst = zInput; |
|
588bb7c…
|
aku
|
950 |
|
|
588bb7c…
|
aku
|
951 |
/* Check for a comment. If found, skip to the end of the line. */ |
|
588bb7c…
|
aku
|
952 |
if( zInput[0]=='#' ){ |
|
588bb7c…
|
aku
|
953 |
while( !thEndOfLine(zInput, nInput) ){ |
|
588bb7c…
|
aku
|
954 |
zInput++; |
|
588bb7c…
|
aku
|
955 |
nInput--; |
|
588bb7c…
|
aku
|
956 |
} |
|
588bb7c…
|
aku
|
957 |
continue; |
|
588bb7c…
|
aku
|
958 |
} |
|
588bb7c…
|
aku
|
959 |
|
|
588bb7c…
|
aku
|
960 |
/* Gobble up input a word at a time until the end of the command |
|
588bb7c…
|
aku
|
961 |
** (a semi-colon or end of line). |
|
588bb7c…
|
aku
|
962 |
*/ |
|
588bb7c…
|
aku
|
963 |
while( rc==TH_OK && *zInput!=';' && !thEndOfLine(zInput, nInput) ){ |
|
3c326ea…
|
ron
|
964 |
int nWord=0; |
|
588bb7c…
|
aku
|
965 |
thNextSpace(interp, zInput, nInput, &nSpace); |
|
588bb7c…
|
aku
|
966 |
rc = thNextWord(interp, &zInput[nSpace], nInput-nSpace, &nWord, 1); |
|
588bb7c…
|
aku
|
967 |
zInput += (nSpace+nWord); |
|
588bb7c…
|
aku
|
968 |
nInput -= (nSpace+nWord); |
|
588bb7c…
|
aku
|
969 |
} |
|
588bb7c…
|
aku
|
970 |
if( rc!=TH_OK ) continue; |
|
588bb7c…
|
aku
|
971 |
|
|
588bb7c…
|
aku
|
972 |
/* Split the command into an array of words. This call also does |
|
588bb7c…
|
aku
|
973 |
** substitution of each individual word. |
|
588bb7c…
|
aku
|
974 |
*/ |
|
588bb7c…
|
aku
|
975 |
rc = thSplitList(interp, zFirst, zInput-zFirst, &argv, &argl, &argc); |
|
588bb7c…
|
aku
|
976 |
if( rc!=TH_OK ) continue; |
|
588bb7c…
|
aku
|
977 |
|
|
588bb7c…
|
aku
|
978 |
if( argc>0 ){ |
|
588bb7c…
|
aku
|
979 |
|
|
588bb7c…
|
aku
|
980 |
/* Look up the command name in the command hash-table. */ |
|
2116238…
|
drh
|
981 |
pEntry = Th_HashFind(interp, interp->paCmd, argv[0], TH1_LEN(argl[0]),0); |
|
588bb7c…
|
aku
|
982 |
if( !pEntry ){ |
|
2116238…
|
drh
|
983 |
Th_ErrorMessage(interp, "no such command: ", argv[0], TH1_LEN(argl[0])); |
|
588bb7c…
|
aku
|
984 |
rc = TH_ERROR; |
|
588bb7c…
|
aku
|
985 |
} |
|
588bb7c…
|
aku
|
986 |
|
|
588bb7c…
|
aku
|
987 |
/* Call the command procedure. */ |
|
588bb7c…
|
aku
|
988 |
if( rc==TH_OK ){ |
|
588bb7c…
|
aku
|
989 |
Th_Command *p = (Th_Command *)(pEntry->pData); |
|
0c99a15…
|
drh
|
990 |
const char **azArg = (const char **)argv; |
|
588bb7c…
|
aku
|
991 |
rc = p->xProc(interp, p->pContext, argc, azArg, argl); |
|
588bb7c…
|
aku
|
992 |
} |
|
1ebe4b0…
|
mistachkin
|
993 |
|
|
db0c512…
|
drh
|
994 |
/* If an error occurred, add this command to the stack trace report. */ |
|
588bb7c…
|
aku
|
995 |
if( rc==TH_ERROR ){ |
|
0c99a15…
|
drh
|
996 |
char *zRes; |
|
588bb7c…
|
aku
|
997 |
int nRes; |
|
0c99a15…
|
drh
|
998 |
char *zStack = 0; |
|
588bb7c…
|
aku
|
999 |
int nStack = 0; |
|
1ebe4b0…
|
mistachkin
|
1000 |
|
|
588bb7c…
|
aku
|
1001 |
zRes = Th_TakeResult(interp, &nRes); |
|
0c99a15…
|
drh
|
1002 |
if( TH_OK==Th_GetVar(interp, (char *)"::th_stack_trace", -1) ){ |
|
588bb7c…
|
aku
|
1003 |
zStack = Th_TakeResult(interp, &nStack); |
|
588bb7c…
|
aku
|
1004 |
} |
|
588bb7c…
|
aku
|
1005 |
Th_ListAppend(interp, &zStack, &nStack, zFirst, zInput-zFirst); |
|
0c99a15…
|
drh
|
1006 |
Th_SetVar(interp, (char *)"::th_stack_trace", -1, zStack, nStack); |
|
588bb7c…
|
aku
|
1007 |
Th_SetResult(interp, zRes, nRes); |
|
588bb7c…
|
aku
|
1008 |
Th_Free(interp, zRes); |
|
588bb7c…
|
aku
|
1009 |
Th_Free(interp, zStack); |
|
588bb7c…
|
aku
|
1010 |
} |
|
588bb7c…
|
aku
|
1011 |
} |
|
588bb7c…
|
aku
|
1012 |
|
|
588bb7c…
|
aku
|
1013 |
Th_Free(interp, argv); |
|
588bb7c…
|
aku
|
1014 |
} |
|
588bb7c…
|
aku
|
1015 |
|
|
588bb7c…
|
aku
|
1016 |
return rc; |
|
588bb7c…
|
aku
|
1017 |
} |
|
588bb7c…
|
aku
|
1018 |
|
|
588bb7c…
|
aku
|
1019 |
/* |
|
588bb7c…
|
aku
|
1020 |
** Interpret an integer frame identifier passed to either Th_Eval() or |
|
588bb7c…
|
aku
|
1021 |
** Th_LinkVar(). If successful, return a pointer to the identified |
|
588bb7c…
|
aku
|
1022 |
** Th_Frame structure. If unsuccessful (no such frame), return 0 and |
|
588bb7c…
|
aku
|
1023 |
** leave an error message in the interpreter result. |
|
588bb7c…
|
aku
|
1024 |
** |
|
588bb7c…
|
aku
|
1025 |
** Argument iFrame is interpreted as follows: |
|
588bb7c…
|
aku
|
1026 |
** |
|
588bb7c…
|
aku
|
1027 |
** * If iFrame is 0, this means the current frame. |
|
588bb7c…
|
aku
|
1028 |
** |
|
1ebe4b0…
|
mistachkin
|
1029 |
** * If iFrame is negative, then the nth frame up the stack, where |
|
1ebe4b0…
|
mistachkin
|
1030 |
** n is the absolute value of iFrame. A value of -1 means the |
|
588bb7c…
|
aku
|
1031 |
** calling procedure. |
|
588bb7c…
|
aku
|
1032 |
** |
|
1ebe4b0…
|
mistachkin
|
1033 |
** * If iFrame is +ve, then the nth frame from the bottom of the |
|
e2bdc10…
|
danield
|
1034 |
** stack. An iFrame value of 1 means the top level (global) frame. |
|
588bb7c…
|
aku
|
1035 |
*/ |
|
588bb7c…
|
aku
|
1036 |
static Th_Frame *getFrame(Th_Interp *interp, int iFrame){ |
|
588bb7c…
|
aku
|
1037 |
Th_Frame *p = interp->pFrame; |
|
588bb7c…
|
aku
|
1038 |
int i; |
|
588bb7c…
|
aku
|
1039 |
if( iFrame>0 ){ |
|
588bb7c…
|
aku
|
1040 |
for(i=0; p; i++){ |
|
588bb7c…
|
aku
|
1041 |
p = p->pCaller; |
|
588bb7c…
|
aku
|
1042 |
} |
|
588bb7c…
|
aku
|
1043 |
iFrame = (i*-1) + iFrame; |
|
588bb7c…
|
aku
|
1044 |
p = interp->pFrame; |
|
588bb7c…
|
aku
|
1045 |
} |
|
588bb7c…
|
aku
|
1046 |
for(i=0; p && i<(iFrame*-1); i++){ |
|
588bb7c…
|
aku
|
1047 |
p = p->pCaller; |
|
588bb7c…
|
aku
|
1048 |
} |
|
588bb7c…
|
aku
|
1049 |
|
|
588bb7c…
|
aku
|
1050 |
if( !p ){ |
|
0c99a15…
|
drh
|
1051 |
char *zFrame; |
|
588bb7c…
|
aku
|
1052 |
int nFrame; |
|
588bb7c…
|
aku
|
1053 |
Th_SetResultInt(interp, iFrame); |
|
588bb7c…
|
aku
|
1054 |
zFrame = Th_TakeResult(interp, &nFrame); |
|
588bb7c…
|
aku
|
1055 |
Th_ErrorMessage(interp, "no such frame:", zFrame, nFrame); |
|
588bb7c…
|
aku
|
1056 |
Th_Free(interp, zFrame); |
|
588bb7c…
|
aku
|
1057 |
} |
|
588bb7c…
|
aku
|
1058 |
return p; |
|
588bb7c…
|
aku
|
1059 |
} |
|
588bb7c…
|
aku
|
1060 |
|
|
588bb7c…
|
aku
|
1061 |
|
|
588bb7c…
|
aku
|
1062 |
/* |
|
588bb7c…
|
aku
|
1063 |
** Evaluate th1 script (zProgram, nProgram) in the frame identified by |
|
588bb7c…
|
aku
|
1064 |
** argument iFrame. Leave either an error message or a result in the |
|
1ebe4b0…
|
mistachkin
|
1065 |
** interpreter result and return a th1 error code (TH_OK, TH_ERROR, |
|
588bb7c…
|
aku
|
1066 |
** TH_RETURN, TH_CONTINUE or TH_BREAK). |
|
588bb7c…
|
aku
|
1067 |
*/ |
|
0c99a15…
|
drh
|
1068 |
int Th_Eval(Th_Interp *interp, int iFrame, const char *zProgram, int nProgram){ |
|
588bb7c…
|
aku
|
1069 |
int rc = TH_OK; |
|
588bb7c…
|
aku
|
1070 |
Th_Frame *pSavedFrame = interp->pFrame; |
|
588bb7c…
|
aku
|
1071 |
|
|
1ebe4b0…
|
mistachkin
|
1072 |
/* Set Th_Interp.pFrame to the frame that this script is to be |
|
588bb7c…
|
aku
|
1073 |
** evaluated in. The current frame is saved in pSavedFrame and will |
|
588bb7c…
|
aku
|
1074 |
** be restored before this function returns. |
|
588bb7c…
|
aku
|
1075 |
*/ |
|
588bb7c…
|
aku
|
1076 |
interp->pFrame = getFrame(interp, iFrame); |
|
588bb7c…
|
aku
|
1077 |
|
|
588bb7c…
|
aku
|
1078 |
if( !interp->pFrame ){ |
|
588bb7c…
|
aku
|
1079 |
rc = TH_ERROR; |
|
588bb7c…
|
aku
|
1080 |
}else{ |
|
588bb7c…
|
aku
|
1081 |
int nInput = nProgram; |
|
1ebe4b0…
|
mistachkin
|
1082 |
|
|
588bb7c…
|
aku
|
1083 |
if( nInput<0 ){ |
|
588bb7c…
|
aku
|
1084 |
nInput = th_strlen(zProgram); |
|
2116238…
|
drh
|
1085 |
}else{ |
|
2116238…
|
drh
|
1086 |
nInput = TH1_LEN(nInput); |
|
588bb7c…
|
aku
|
1087 |
} |
|
588bb7c…
|
aku
|
1088 |
rc = thEvalLocal(interp, zProgram, nInput); |
|
588bb7c…
|
aku
|
1089 |
} |
|
588bb7c…
|
aku
|
1090 |
|
|
588bb7c…
|
aku
|
1091 |
interp->pFrame = pSavedFrame; |
|
588bb7c…
|
aku
|
1092 |
return rc; |
|
588bb7c…
|
aku
|
1093 |
} |
|
588bb7c…
|
aku
|
1094 |
|
|
588bb7c…
|
aku
|
1095 |
/* |
|
588bb7c…
|
aku
|
1096 |
** Input string (zVarname, nVarname) contains a th1 variable name. It |
|
588bb7c…
|
aku
|
1097 |
** may be a simple scalar variable name or it may be a reference |
|
588bb7c…
|
aku
|
1098 |
** to an array member. The variable name may or may not begin with |
|
588bb7c…
|
aku
|
1099 |
** "::", indicating that the name refers to a global variable, not |
|
588bb7c…
|
aku
|
1100 |
** a local scope one. |
|
588bb7c…
|
aku
|
1101 |
** |
|
588bb7c…
|
aku
|
1102 |
** This function inspects and categorizes the supplied variable name. |
|
588bb7c…
|
aku
|
1103 |
** |
|
588bb7c…
|
aku
|
1104 |
** If the name is a global reference, *pisGlobal is set to true. Otherwise |
|
588bb7c…
|
aku
|
1105 |
** false. Output string (*pzOuter, *pnOuter) is set to the variable name |
|
588bb7c…
|
aku
|
1106 |
** if it is a scalar reference, or the name of the array if it is an |
|
588bb7c…
|
aku
|
1107 |
** array variable. If the variable is a scalar, *pzInner is set to 0. |
|
588bb7c…
|
aku
|
1108 |
** If it is an array variable, (*pzInner, *pnInner) is set to the |
|
588bb7c…
|
aku
|
1109 |
** array key name. |
|
588bb7c…
|
aku
|
1110 |
*/ |
|
588bb7c…
|
aku
|
1111 |
static int thAnalyseVarname( |
|
0c99a15…
|
drh
|
1112 |
const char *zVarname, |
|
588bb7c…
|
aku
|
1113 |
int nVarname, |
|
1ebe4b0…
|
mistachkin
|
1114 |
const char **pzOuter, /* OUT: Pointer to scalar/array name */ |
|
588bb7c…
|
aku
|
1115 |
int *pnOuter, /* OUT: Number of bytes at *pzOuter */ |
|
1ebe4b0…
|
mistachkin
|
1116 |
const char **pzInner, /* OUT: Pointer to array key (or null) */ |
|
588bb7c…
|
aku
|
1117 |
int *pnInner, /* OUT: Number of bytes at *pzInner */ |
|
588bb7c…
|
aku
|
1118 |
int *pisGlobal /* OUT: Set to true if this is a global ref */ |
|
588bb7c…
|
aku
|
1119 |
){ |
|
0c99a15…
|
drh
|
1120 |
const char *zOuter = zVarname; |
|
588bb7c…
|
aku
|
1121 |
int nOuter; |
|
0c99a15…
|
drh
|
1122 |
const char *zInner = 0; |
|
588bb7c…
|
aku
|
1123 |
int nInner = 0; |
|
588bb7c…
|
aku
|
1124 |
int isGlobal = 0; |
|
588bb7c…
|
aku
|
1125 |
int i; |
|
588bb7c…
|
aku
|
1126 |
|
|
588bb7c…
|
aku
|
1127 |
if( nVarname<0 ){ |
|
588bb7c…
|
aku
|
1128 |
nVarname = th_strlen(zVarname); |
|
2116238…
|
drh
|
1129 |
}else{ |
|
2116238…
|
drh
|
1130 |
nVarname = TH1_LEN(nVarname); |
|
588bb7c…
|
aku
|
1131 |
} |
|
588bb7c…
|
aku
|
1132 |
nOuter = nVarname; |
|
588bb7c…
|
aku
|
1133 |
|
|
588bb7c…
|
aku
|
1134 |
/* If the variable name starts with "::", then do the lookup is in the |
|
588bb7c…
|
aku
|
1135 |
** uppermost (global) frame. |
|
588bb7c…
|
aku
|
1136 |
*/ |
|
588bb7c…
|
aku
|
1137 |
if( nVarname>2 && zVarname[0]==':' && zVarname[1]==':' ){ |
|
588bb7c…
|
aku
|
1138 |
zOuter += 2; |
|
588bb7c…
|
aku
|
1139 |
nOuter -= 2; |
|
588bb7c…
|
aku
|
1140 |
isGlobal = 1; |
|
588bb7c…
|
aku
|
1141 |
} |
|
588bb7c…
|
aku
|
1142 |
|
|
588bb7c…
|
aku
|
1143 |
/* Check if this is an array reference. */ |
|
588bb7c…
|
aku
|
1144 |
if( zOuter[nOuter-1]==')' ){ |
|
588bb7c…
|
aku
|
1145 |
for(i=0; i<nOuter; i++){ |
|
588bb7c…
|
aku
|
1146 |
if( zOuter[i]=='(' ){ |
|
588bb7c…
|
aku
|
1147 |
zInner = &zOuter[i+1]; |
|
588bb7c…
|
aku
|
1148 |
nInner = nOuter-i-2; |
|
588bb7c…
|
aku
|
1149 |
nOuter = i; |
|
588bb7c…
|
aku
|
1150 |
break; |
|
588bb7c…
|
aku
|
1151 |
} |
|
588bb7c…
|
aku
|
1152 |
} |
|
588bb7c…
|
aku
|
1153 |
} |
|
588bb7c…
|
aku
|
1154 |
|
|
588bb7c…
|
aku
|
1155 |
*pzOuter = zOuter; |
|
588bb7c…
|
aku
|
1156 |
*pnOuter = nOuter; |
|
588bb7c…
|
aku
|
1157 |
*pzInner = zInner; |
|
588bb7c…
|
aku
|
1158 |
*pnInner = nInner; |
|
588bb7c…
|
aku
|
1159 |
*pisGlobal = isGlobal; |
|
588bb7c…
|
aku
|
1160 |
return TH_OK; |
|
588bb7c…
|
aku
|
1161 |
} |
|
588bb7c…
|
aku
|
1162 |
|
|
588bb7c…
|
aku
|
1163 |
/* |
|
1aeb272…
|
drh
|
1164 |
** The Find structure is used to return extra information to callers of the |
|
1aeb272…
|
drh
|
1165 |
** thFindValue function. The fields within it are populated by thFindValue |
|
1aeb272…
|
drh
|
1166 |
** as soon as the necessary information is available. Callers should check |
|
1aeb272…
|
drh
|
1167 |
** each field of interest upon return. |
|
1aeb272…
|
drh
|
1168 |
*/ |
|
1aeb272…
|
drh
|
1169 |
|
|
1aeb272…
|
drh
|
1170 |
struct Find { |
|
1aeb272…
|
drh
|
1171 |
Th_HashEntry *pValueEntry; /* Pointer to the scalar or array hash entry */ |
|
5e368e9…
|
mistachkin
|
1172 |
Th_HashEntry *pElemEntry; /* Pointer to array element hash entry, if any */ |
|
1aeb272…
|
drh
|
1173 |
const char *zElem; /* Name of array element, if applicable */ |
|
1aeb272…
|
drh
|
1174 |
int nElem; /* Length of array element name, if applicable */ |
|
1aeb272…
|
drh
|
1175 |
}; |
|
1aeb272…
|
drh
|
1176 |
typedef struct Find Find; |
|
1aeb272…
|
drh
|
1177 |
|
|
1aeb272…
|
drh
|
1178 |
/* |
|
588bb7c…
|
aku
|
1179 |
** Input string (zVar, nVar) contains a variable name. This function locates |
|
1ebe4b0…
|
mistachkin
|
1180 |
** the Th_Variable structure associated with the named variable. The |
|
588bb7c…
|
aku
|
1181 |
** variable name may be a global or local scalar or array variable |
|
588bb7c…
|
aku
|
1182 |
** |
|
588bb7c…
|
aku
|
1183 |
** If the create argument is non-zero and the named variable does not exist |
|
588bb7c…
|
aku
|
1184 |
** it is created. Otherwise, an error is left in the interpreter result |
|
588bb7c…
|
aku
|
1185 |
** and NULL returned. |
|
588bb7c…
|
aku
|
1186 |
** |
|
588bb7c…
|
aku
|
1187 |
** If the arrayok argument is false and the named variable is an array, |
|
588bb7c…
|
aku
|
1188 |
** an error is left in the interpreter result and NULL returned. If |
|
b92e460…
|
wyoung
|
1189 |
** arrayok is true an array name is OK. |
|
588bb7c…
|
aku
|
1190 |
*/ |
|
1aeb272…
|
drh
|
1191 |
|
|
588bb7c…
|
aku
|
1192 |
static Th_Variable *thFindValue( |
|
588bb7c…
|
aku
|
1193 |
Th_Interp *interp, |
|
1aeb272…
|
drh
|
1194 |
const char *zVar, /* Pointer to variable name */ |
|
1aeb272…
|
drh
|
1195 |
int nVar, /* Number of bytes at nVar */ |
|
1aeb272…
|
drh
|
1196 |
int create, /* If true, create the variable if not found */ |
|
b92e460…
|
wyoung
|
1197 |
int arrayok, /* If true, an array is OK. Otherwise array==error */ |
|
1aeb272…
|
drh
|
1198 |
int noerror, /* If false, set interpreter result to error */ |
|
1aeb272…
|
drh
|
1199 |
Find *pFind /* If non-zero, place output here */ |
|
588bb7c…
|
aku
|
1200 |
){ |
|
0c99a15…
|
drh
|
1201 |
const char *zOuter; |
|
588bb7c…
|
aku
|
1202 |
int nOuter; |
|
0c99a15…
|
drh
|
1203 |
const char *zInner; |
|
588bb7c…
|
aku
|
1204 |
int nInner; |
|
588bb7c…
|
aku
|
1205 |
int isGlobal; |
|
588bb7c…
|
aku
|
1206 |
|
|
588bb7c…
|
aku
|
1207 |
Th_HashEntry *pEntry; |
|
588bb7c…
|
aku
|
1208 |
Th_Frame *pFrame = interp->pFrame; |
|
588bb7c…
|
aku
|
1209 |
Th_Variable *pValue; |
|
588bb7c…
|
aku
|
1210 |
|
|
588bb7c…
|
aku
|
1211 |
thAnalyseVarname(zVar, nVar, &zOuter, &nOuter, &zInner, &nInner, &isGlobal); |
|
1aeb272…
|
drh
|
1212 |
if( pFind ){ |
|
1aeb272…
|
drh
|
1213 |
memset(pFind, 0, sizeof(Find)); |
|
1aeb272…
|
drh
|
1214 |
pFind->zElem = zInner; |
|
1aeb272…
|
drh
|
1215 |
pFind->nElem = nInner; |
|
1aeb272…
|
drh
|
1216 |
} |
|
588bb7c…
|
aku
|
1217 |
if( isGlobal ){ |
|
588bb7c…
|
aku
|
1218 |
while( pFrame->pCaller ) pFrame = pFrame->pCaller; |
|
588bb7c…
|
aku
|
1219 |
} |
|
588bb7c…
|
aku
|
1220 |
|
|
588bb7c…
|
aku
|
1221 |
pEntry = Th_HashFind(interp, pFrame->paVar, zOuter, nOuter, create); |
|
1ebe4b0…
|
mistachkin
|
1222 |
assert(pEntry || create<=0); |
|
1aeb272…
|
drh
|
1223 |
if( pFind ){ |
|
1aeb272…
|
drh
|
1224 |
pFind->pValueEntry = pEntry; |
|
1aeb272…
|
drh
|
1225 |
} |
|
588bb7c…
|
aku
|
1226 |
if( !pEntry ){ |
|
588bb7c…
|
aku
|
1227 |
goto no_such_var; |
|
588bb7c…
|
aku
|
1228 |
} |
|
588bb7c…
|
aku
|
1229 |
|
|
588bb7c…
|
aku
|
1230 |
pValue = (Th_Variable *)pEntry->pData; |
|
588bb7c…
|
aku
|
1231 |
if( !pValue ){ |
|
588bb7c…
|
aku
|
1232 |
assert(create); |
|
588bb7c…
|
aku
|
1233 |
pValue = Th_Malloc(interp, sizeof(Th_Variable)); |
|
588bb7c…
|
aku
|
1234 |
pValue->nRef = 1; |
|
588bb7c…
|
aku
|
1235 |
pEntry->pData = (void *)pValue; |
|
588bb7c…
|
aku
|
1236 |
} |
|
588bb7c…
|
aku
|
1237 |
|
|
588bb7c…
|
aku
|
1238 |
if( zInner ){ |
|
588bb7c…
|
aku
|
1239 |
if( pValue->zData ){ |
|
9765b03…
|
mistachkin
|
1240 |
if( !noerror ){ |
|
9765b03…
|
mistachkin
|
1241 |
Th_ErrorMessage(interp, "variable is a scalar:", zOuter, nOuter); |
|
9765b03…
|
mistachkin
|
1242 |
} |
|
588bb7c…
|
aku
|
1243 |
return 0; |
|
588bb7c…
|
aku
|
1244 |
} |
|
588bb7c…
|
aku
|
1245 |
if( !pValue->pHash ){ |
|
588bb7c…
|
aku
|
1246 |
if( !create ){ |
|
588bb7c…
|
aku
|
1247 |
goto no_such_var; |
|
588bb7c…
|
aku
|
1248 |
} |
|
588bb7c…
|
aku
|
1249 |
pValue->pHash = Th_HashNew(interp); |
|
588bb7c…
|
aku
|
1250 |
} |
|
588bb7c…
|
aku
|
1251 |
pEntry = Th_HashFind(interp, pValue->pHash, zInner, nInner, create); |
|
1aeb272…
|
drh
|
1252 |
assert(pEntry || create<=0); |
|
1aeb272…
|
drh
|
1253 |
if( pFind ){ |
|
1aeb272…
|
drh
|
1254 |
pFind->pElemEntry = pEntry; |
|
1aeb272…
|
drh
|
1255 |
} |
|
588bb7c…
|
aku
|
1256 |
if( !pEntry ){ |
|
588bb7c…
|
aku
|
1257 |
goto no_such_var; |
|
588bb7c…
|
aku
|
1258 |
} |
|
588bb7c…
|
aku
|
1259 |
pValue = (Th_Variable *)pEntry->pData; |
|
588bb7c…
|
aku
|
1260 |
if( !pValue ){ |
|
588bb7c…
|
aku
|
1261 |
assert(create); |
|
588bb7c…
|
aku
|
1262 |
pValue = Th_Malloc(interp, sizeof(Th_Variable)); |
|
588bb7c…
|
aku
|
1263 |
pValue->nRef = 1; |
|
588bb7c…
|
aku
|
1264 |
pEntry->pData = (void *)pValue; |
|
588bb7c…
|
aku
|
1265 |
} |
|
588bb7c…
|
aku
|
1266 |
}else{ |
|
588bb7c…
|
aku
|
1267 |
if( pValue->pHash && !arrayok ){ |
|
9765b03…
|
mistachkin
|
1268 |
if( !noerror ){ |
|
9765b03…
|
mistachkin
|
1269 |
Th_ErrorMessage(interp, "variable is an array:", zOuter, nOuter); |
|
9765b03…
|
mistachkin
|
1270 |
} |
|
588bb7c…
|
aku
|
1271 |
return 0; |
|
588bb7c…
|
aku
|
1272 |
} |
|
588bb7c…
|
aku
|
1273 |
} |
|
588bb7c…
|
aku
|
1274 |
|
|
588bb7c…
|
aku
|
1275 |
return pValue; |
|
588bb7c…
|
aku
|
1276 |
|
|
588bb7c…
|
aku
|
1277 |
no_such_var: |
|
9765b03…
|
mistachkin
|
1278 |
if( !noerror ){ |
|
9765b03…
|
mistachkin
|
1279 |
Th_ErrorMessage(interp, "no such variable:", zVar, nVar); |
|
9765b03…
|
mistachkin
|
1280 |
} |
|
588bb7c…
|
aku
|
1281 |
return 0; |
|
588bb7c…
|
aku
|
1282 |
} |
|
588bb7c…
|
aku
|
1283 |
|
|
588bb7c…
|
aku
|
1284 |
/* |
|
1ebe4b0…
|
mistachkin
|
1285 |
** String (zVar, nVar) must contain the name of a scalar variable or |
|
1ebe4b0…
|
mistachkin
|
1286 |
** array member. Look up the variable, store its current value in |
|
588bb7c…
|
aku
|
1287 |
** the interpreter result and return TH_OK. |
|
588bb7c…
|
aku
|
1288 |
** |
|
588bb7c…
|
aku
|
1289 |
** If the named variable does not exist, return TH_ERROR and leave |
|
588bb7c…
|
aku
|
1290 |
** an error message in the interpreter result. |
|
588bb7c…
|
aku
|
1291 |
*/ |
|
0c99a15…
|
drh
|
1292 |
int Th_GetVar(Th_Interp *interp, const char *zVar, int nVar){ |
|
588bb7c…
|
aku
|
1293 |
Th_Variable *pValue; |
|
588bb7c…
|
aku
|
1294 |
|
|
1aeb272…
|
drh
|
1295 |
pValue = thFindValue(interp, zVar, nVar, 0, 0, 0, 0); |
|
588bb7c…
|
aku
|
1296 |
if( !pValue ){ |
|
588bb7c…
|
aku
|
1297 |
return TH_ERROR; |
|
588bb7c…
|
aku
|
1298 |
} |
|
588bb7c…
|
aku
|
1299 |
if( !pValue->zData ){ |
|
588bb7c…
|
aku
|
1300 |
Th_ErrorMessage(interp, "no such variable:", zVar, nVar); |
|
588bb7c…
|
aku
|
1301 |
return TH_ERROR; |
|
588bb7c…
|
aku
|
1302 |
} |
|
588bb7c…
|
aku
|
1303 |
|
|
588bb7c…
|
aku
|
1304 |
return Th_SetResult(interp, pValue->zData, pValue->nData); |
|
588bb7c…
|
aku
|
1305 |
} |
|
588bb7c…
|
aku
|
1306 |
|
|
588bb7c…
|
aku
|
1307 |
/* |
|
4f8c897…
|
drh
|
1308 |
** Return true if variable (zVar, nVar) exists. |
|
4f8c897…
|
drh
|
1309 |
*/ |
|
4f8c897…
|
drh
|
1310 |
int Th_ExistsVar(Th_Interp *interp, const char *zVar, int nVar){ |
|
1aeb272…
|
drh
|
1311 |
Th_Variable *pValue = thFindValue(interp, zVar, nVar, 0, 1, 1, 0); |
|
1aeb272…
|
drh
|
1312 |
return pValue && (pValue->zData || pValue->pHash); |
|
1aeb272…
|
drh
|
1313 |
} |
|
1aeb272…
|
drh
|
1314 |
|
|
1aeb272…
|
drh
|
1315 |
/* |
|
ea7813d…
|
mistachkin
|
1316 |
** Return true if array variable (zVar, nVar) exists. |
|
ea7813d…
|
mistachkin
|
1317 |
*/ |
|
ea7813d…
|
mistachkin
|
1318 |
int Th_ExistsArrayVar(Th_Interp *interp, const char *zVar, int nVar){ |
|
ea7813d…
|
mistachkin
|
1319 |
Th_Variable *pValue = thFindValue(interp, zVar, nVar, 0, 1, 1, 0); |
|
ea7813d…
|
mistachkin
|
1320 |
return pValue && !pValue->zData && pValue->pHash; |
|
ea7813d…
|
mistachkin
|
1321 |
} |
|
ea7813d…
|
mistachkin
|
1322 |
|
|
ea7813d…
|
mistachkin
|
1323 |
/* |
|
588bb7c…
|
aku
|
1324 |
** String (zVar, nVar) must contain the name of a scalar variable or |
|
588bb7c…
|
aku
|
1325 |
** array member. If the variable does not exist it is created. The |
|
588bb7c…
|
aku
|
1326 |
** variable is set to the value supplied in string (zValue, nValue). |
|
588bb7c…
|
aku
|
1327 |
** |
|
588bb7c…
|
aku
|
1328 |
** If (zVar, nVar) refers to an existing array, TH_ERROR is returned |
|
588bb7c…
|
aku
|
1329 |
** and an error message left in the interpreter result. |
|
588bb7c…
|
aku
|
1330 |
*/ |
|
588bb7c…
|
aku
|
1331 |
int Th_SetVar( |
|
1ebe4b0…
|
mistachkin
|
1332 |
Th_Interp *interp, |
|
1ebe4b0…
|
mistachkin
|
1333 |
const char *zVar, |
|
588bb7c…
|
aku
|
1334 |
int nVar, |
|
0c99a15…
|
drh
|
1335 |
const char *zValue, |
|
588bb7c…
|
aku
|
1336 |
int nValue |
|
588bb7c…
|
aku
|
1337 |
){ |
|
588bb7c…
|
aku
|
1338 |
Th_Variable *pValue; |
|
2116238…
|
drh
|
1339 |
int nn; |
|
588bb7c…
|
aku
|
1340 |
|
|
2116238…
|
drh
|
1341 |
nVar = TH1_LEN(nVar); |
|
1aeb272…
|
drh
|
1342 |
pValue = thFindValue(interp, zVar, nVar, 1, 0, 0, 0); |
|
588bb7c…
|
aku
|
1343 |
if( !pValue ){ |
|
588bb7c…
|
aku
|
1344 |
return TH_ERROR; |
|
588bb7c…
|
aku
|
1345 |
} |
|
588bb7c…
|
aku
|
1346 |
|
|
588bb7c…
|
aku
|
1347 |
if( nValue<0 ){ |
|
2116238…
|
drh
|
1348 |
nn = th_strlen(zValue); |
|
2116238…
|
drh
|
1349 |
}else{ |
|
2116238…
|
drh
|
1350 |
nn = TH1_LEN(nValue); |
|
588bb7c…
|
aku
|
1351 |
} |
|
588bb7c…
|
aku
|
1352 |
if( pValue->zData ){ |
|
588bb7c…
|
aku
|
1353 |
Th_Free(interp, pValue->zData); |
|
588bb7c…
|
aku
|
1354 |
pValue->zData = 0; |
|
588bb7c…
|
aku
|
1355 |
} |
|
588bb7c…
|
aku
|
1356 |
|
|
2116238…
|
drh
|
1357 |
assert(zValue || nn==0); |
|
2116238…
|
drh
|
1358 |
pValue->zData = Th_Malloc(interp, nn+1); |
|
2116238…
|
drh
|
1359 |
pValue->zData[nn] = '\0'; |
|
2116238…
|
drh
|
1360 |
th_memcpy(pValue->zData, zValue, nn); |
|
588bb7c…
|
aku
|
1361 |
pValue->nData = nValue; |
|
588bb7c…
|
aku
|
1362 |
|
|
588bb7c…
|
aku
|
1363 |
return TH_OK; |
|
588bb7c…
|
aku
|
1364 |
} |
|
588bb7c…
|
aku
|
1365 |
|
|
588bb7c…
|
aku
|
1366 |
/* |
|
588bb7c…
|
aku
|
1367 |
** Create a variable link so that accessing variable (zLocal, nLocal) is |
|
588bb7c…
|
aku
|
1368 |
** the same as accessing variable (zLink, nLink) in stack frame iFrame. |
|
588bb7c…
|
aku
|
1369 |
*/ |
|
588bb7c…
|
aku
|
1370 |
int Th_LinkVar( |
|
588bb7c…
|
aku
|
1371 |
Th_Interp *interp, /* Interpreter */ |
|
1ebe4b0…
|
mistachkin
|
1372 |
const char *zLocal, int nLocal, /* Local varname */ |
|
588bb7c…
|
aku
|
1373 |
int iFrame, /* Stack frame of linked var */ |
|
1ebe4b0…
|
mistachkin
|
1374 |
const char *zLink, int nLink /* Linked varname */ |
|
588bb7c…
|
aku
|
1375 |
){ |
|
588bb7c…
|
aku
|
1376 |
Th_Frame *pSavedFrame = interp->pFrame; |
|
588bb7c…
|
aku
|
1377 |
Th_Frame *pFrame; |
|
588bb7c…
|
aku
|
1378 |
Th_HashEntry *pEntry; |
|
588bb7c…
|
aku
|
1379 |
Th_Variable *pValue; |
|
588bb7c…
|
aku
|
1380 |
|
|
588bb7c…
|
aku
|
1381 |
pFrame = getFrame(interp, iFrame); |
|
588bb7c…
|
aku
|
1382 |
if( !pFrame ){ |
|
588bb7c…
|
aku
|
1383 |
return TH_ERROR; |
|
588bb7c…
|
aku
|
1384 |
} |
|
588bb7c…
|
aku
|
1385 |
pSavedFrame = interp->pFrame; |
|
588bb7c…
|
aku
|
1386 |
interp->pFrame = pFrame; |
|
1aeb272…
|
drh
|
1387 |
pValue = thFindValue(interp, zLink, nLink, 1, 1, 0, 0); |
|
588bb7c…
|
aku
|
1388 |
interp->pFrame = pSavedFrame; |
|
588bb7c…
|
aku
|
1389 |
|
|
588bb7c…
|
aku
|
1390 |
pEntry = Th_HashFind(interp, interp->pFrame->paVar, zLocal, nLocal, 1); |
|
588bb7c…
|
aku
|
1391 |
if( pEntry->pData ){ |
|
588bb7c…
|
aku
|
1392 |
Th_ErrorMessage(interp, "variable exists:", zLocal, nLocal); |
|
588bb7c…
|
aku
|
1393 |
return TH_ERROR; |
|
588bb7c…
|
aku
|
1394 |
} |
|
588bb7c…
|
aku
|
1395 |
pEntry->pData = (void *)pValue; |
|
588bb7c…
|
aku
|
1396 |
pValue->nRef++; |
|
588bb7c…
|
aku
|
1397 |
|
|
588bb7c…
|
aku
|
1398 |
return TH_OK; |
|
588bb7c…
|
aku
|
1399 |
} |
|
588bb7c…
|
aku
|
1400 |
|
|
588bb7c…
|
aku
|
1401 |
/* |
|
588bb7c…
|
aku
|
1402 |
** Input string (zVar, nVar) must contain the name of a scalar variable, |
|
588bb7c…
|
aku
|
1403 |
** an array, or an array member. If the identified variable exists, it |
|
588bb7c…
|
aku
|
1404 |
** is deleted and TH_OK returned. Otherwise, an error message is left |
|
588bb7c…
|
aku
|
1405 |
** in the interpreter result and TH_ERROR is returned. |
|
588bb7c…
|
aku
|
1406 |
*/ |
|
0c99a15…
|
drh
|
1407 |
int Th_UnsetVar(Th_Interp *interp, const char *zVar, int nVar){ |
|
1aeb272…
|
drh
|
1408 |
Find find; |
|
588bb7c…
|
aku
|
1409 |
Th_Variable *pValue; |
|
1aeb272…
|
drh
|
1410 |
Th_HashEntry *pEntry; |
|
1aeb272…
|
drh
|
1411 |
int rc = TH_ERROR; |
|
588bb7c…
|
aku
|
1412 |
|
|
1aeb272…
|
drh
|
1413 |
pValue = thFindValue(interp, zVar, nVar, 0, 1, 0, &find); |
|
588bb7c…
|
aku
|
1414 |
if( !pValue ){ |
|
1aeb272…
|
drh
|
1415 |
return rc; |
|
1aeb272…
|
drh
|
1416 |
} |
|
1aeb272…
|
drh
|
1417 |
|
|
1aeb272…
|
drh
|
1418 |
if( pValue->zData || pValue->pHash ){ |
|
1aeb272…
|
drh
|
1419 |
rc = TH_OK; |
|
1aeb272…
|
drh
|
1420 |
}else { |
|
1aeb272…
|
drh
|
1421 |
Th_ErrorMessage(interp, "no such variable:", zVar, nVar); |
|
1aeb272…
|
drh
|
1422 |
} |
|
1aeb272…
|
drh
|
1423 |
|
|
1aeb272…
|
drh
|
1424 |
/* |
|
1aeb272…
|
drh
|
1425 |
** The variable may be shared by more than one frame; therefore, make sure |
|
1aeb272…
|
drh
|
1426 |
** it is actually freed prior to freeing the parent structure. The values |
|
1aeb272…
|
drh
|
1427 |
** for the variable must be freed now so the variable appears undefined in |
|
1aeb272…
|
drh
|
1428 |
** all frames. The hash entry in the current frame must also be deleted |
|
1aeb272…
|
drh
|
1429 |
** now; otherwise, if the current stack frame is later popped, it will try |
|
1aeb272…
|
drh
|
1430 |
** to delete a variable which has already been freed. |
|
1aeb272…
|
drh
|
1431 |
*/ |
|
1aeb272…
|
drh
|
1432 |
if( find.zElem ){ |
|
1aeb272…
|
drh
|
1433 |
pEntry = find.pElemEntry; |
|
1aeb272…
|
drh
|
1434 |
}else{ |
|
1aeb272…
|
drh
|
1435 |
pEntry = find.pValueEntry; |
|
1aeb272…
|
drh
|
1436 |
} |
|
1aeb272…
|
drh
|
1437 |
assert( pEntry ); |
|
1aeb272…
|
drh
|
1438 |
assert( pValue ); |
|
1aeb272…
|
drh
|
1439 |
if( thFreeVariable(pEntry, (void *)interp) ){ |
|
1aeb272…
|
drh
|
1440 |
if( find.zElem ){ |
|
1aeb272…
|
drh
|
1441 |
Th_Variable *pValue2 = find.pValueEntry->pData; |
|
1aeb272…
|
drh
|
1442 |
Th_HashFind(interp, pValue2->pHash, find.zElem, find.nElem, -1); |
|
1aeb272…
|
drh
|
1443 |
}else if( pEntry->pData ){ |
|
1aeb272…
|
drh
|
1444 |
Th_Free(interp, pEntry->pData); |
|
1aeb272…
|
drh
|
1445 |
pEntry->pData = 0; |
|
1aeb272…
|
drh
|
1446 |
} |
|
1aeb272…
|
drh
|
1447 |
}else{ |
|
1aeb272…
|
drh
|
1448 |
if( pValue->zData ){ |
|
1aeb272…
|
drh
|
1449 |
Th_Free(interp, pValue->zData); |
|
1aeb272…
|
drh
|
1450 |
pValue->zData = 0; |
|
1aeb272…
|
drh
|
1451 |
} |
|
1aeb272…
|
drh
|
1452 |
if( pValue->pHash ){ |
|
1aeb272…
|
drh
|
1453 |
Th_HashIterate(interp, pValue->pHash, thFreeVariable, (void *)interp); |
|
1aeb272…
|
drh
|
1454 |
Th_HashDelete(interp, pValue->pHash); |
|
1aeb272…
|
drh
|
1455 |
pValue->pHash = 0; |
|
1aeb272…
|
drh
|
1456 |
} |
|
1aeb272…
|
drh
|
1457 |
if( find.zElem ){ |
|
1aeb272…
|
drh
|
1458 |
Th_Variable *pValue2 = find.pValueEntry->pData; |
|
1aeb272…
|
drh
|
1459 |
Th_HashFind(interp, pValue2->pHash, find.zElem, find.nElem, -1); |
|
1aeb272…
|
drh
|
1460 |
} |
|
1aeb272…
|
drh
|
1461 |
} |
|
1aeb272…
|
drh
|
1462 |
if( !find.zElem ){ |
|
95292a1…
|
mistachkin
|
1463 |
Th_HashFind(interp, interp->pFrame->paVar, zVar, nVar, -1); |
|
1aeb272…
|
drh
|
1464 |
} |
|
1aeb272…
|
drh
|
1465 |
return rc; |
|
588bb7c…
|
aku
|
1466 |
} |
|
588bb7c…
|
aku
|
1467 |
|
|
588bb7c…
|
aku
|
1468 |
/* |
|
588bb7c…
|
aku
|
1469 |
** Return an allocated buffer containing a copy of string (z, n). The |
|
588bb7c…
|
aku
|
1470 |
** caller is responsible for eventually calling Th_Free() to free |
|
588bb7c…
|
aku
|
1471 |
** the returned buffer. |
|
588bb7c…
|
aku
|
1472 |
*/ |
|
0c99a15…
|
drh
|
1473 |
char *th_strdup(Th_Interp *interp, const char *z, int n){ |
|
0c99a15…
|
drh
|
1474 |
char *zRes; |
|
588bb7c…
|
aku
|
1475 |
if( n<0 ){ |
|
588bb7c…
|
aku
|
1476 |
n = th_strlen(z); |
|
2116238…
|
drh
|
1477 |
}else{ |
|
2116238…
|
drh
|
1478 |
n = TH1_LEN(n); |
|
588bb7c…
|
aku
|
1479 |
} |
|
588bb7c…
|
aku
|
1480 |
zRes = Th_Malloc(interp, n+1); |
|
7ae4b1a…
|
drh
|
1481 |
th_memcpy(zRes, z, n); |
|
588bb7c…
|
aku
|
1482 |
zRes[n] = '\0'; |
|
588bb7c…
|
aku
|
1483 |
return zRes; |
|
588bb7c…
|
aku
|
1484 |
} |
|
588bb7c…
|
aku
|
1485 |
|
|
588bb7c…
|
aku
|
1486 |
/* |
|
588bb7c…
|
aku
|
1487 |
** Argument zPre must be a nul-terminated string. Set the interpreter |
|
588bb7c…
|
aku
|
1488 |
** result to a string containing the contents of zPre, followed by |
|
588bb7c…
|
aku
|
1489 |
** a space (" ") character, followed by a copy of string (z, n). |
|
588bb7c…
|
aku
|
1490 |
** |
|
588bb7c…
|
aku
|
1491 |
** In other words, the equivalent of: |
|
588bb7c…
|
aku
|
1492 |
* |
|
588bb7c…
|
aku
|
1493 |
** printf("%s %.*s", zPre, n, z); |
|
588bb7c…
|
aku
|
1494 |
** |
|
588bb7c…
|
aku
|
1495 |
** Example: |
|
588bb7c…
|
aku
|
1496 |
** |
|
588bb7c…
|
aku
|
1497 |
** Th_ErrorMessage(interp, "no such variable:", zVarname, nVarname); |
|
588bb7c…
|
aku
|
1498 |
** |
|
588bb7c…
|
aku
|
1499 |
*/ |
|
0c99a15…
|
drh
|
1500 |
int Th_ErrorMessage(Th_Interp *interp, const char *zPre, const char *z, int n){ |
|
588bb7c…
|
aku
|
1501 |
if( interp ){ |
|
0c99a15…
|
drh
|
1502 |
char *zRes = 0; |
|
588bb7c…
|
aku
|
1503 |
int nRes = 0; |
|
588bb7c…
|
aku
|
1504 |
|
|
0c99a15…
|
drh
|
1505 |
Th_SetVar(interp, (char *)"::th_stack_trace", -1, 0, 0); |
|
1ebe4b0…
|
mistachkin
|
1506 |
|
|
588bb7c…
|
aku
|
1507 |
Th_StringAppend(interp, &zRes, &nRes, zPre, -1); |
|
588bb7c…
|
aku
|
1508 |
if( zRes[nRes-1]=='"' ){ |
|
588bb7c…
|
aku
|
1509 |
Th_StringAppend(interp, &zRes, &nRes, z, n); |
|
0c99a15…
|
drh
|
1510 |
Th_StringAppend(interp, &zRes, &nRes, (const char *)"\"", 1); |
|
588bb7c…
|
aku
|
1511 |
}else{ |
|
0c99a15…
|
drh
|
1512 |
Th_StringAppend(interp, &zRes, &nRes, (const char *)" ", 1); |
|
588bb7c…
|
aku
|
1513 |
Th_StringAppend(interp, &zRes, &nRes, z, n); |
|
588bb7c…
|
aku
|
1514 |
} |
|
588bb7c…
|
aku
|
1515 |
|
|
588bb7c…
|
aku
|
1516 |
Th_SetResult(interp, zRes, nRes); |
|
588bb7c…
|
aku
|
1517 |
Th_Free(interp, zRes); |
|
588bb7c…
|
aku
|
1518 |
} |
|
588bb7c…
|
aku
|
1519 |
|
|
588bb7c…
|
aku
|
1520 |
return TH_OK; |
|
588bb7c…
|
aku
|
1521 |
} |
|
588bb7c…
|
aku
|
1522 |
|
|
588bb7c…
|
aku
|
1523 |
/* |
|
588bb7c…
|
aku
|
1524 |
** Set the current interpreter result by taking a copy of the buffer |
|
588bb7c…
|
aku
|
1525 |
** pointed to by z, size n bytes. TH_OK is always returned. |
|
588bb7c…
|
aku
|
1526 |
*/ |
|
0c99a15…
|
drh
|
1527 |
int Th_SetResult(Th_Interp *pInterp, const char *z, int n){ |
|
588bb7c…
|
aku
|
1528 |
|
|
588bb7c…
|
aku
|
1529 |
/* Free the current result */ |
|
588bb7c…
|
aku
|
1530 |
Th_Free(pInterp, pInterp->zResult); |
|
588bb7c…
|
aku
|
1531 |
pInterp->zResult = 0; |
|
588bb7c…
|
aku
|
1532 |
pInterp->nResult = 0; |
|
588bb7c…
|
aku
|
1533 |
|
|
588bb7c…
|
aku
|
1534 |
if( n<0 ){ |
|
588bb7c…
|
aku
|
1535 |
n = th_strlen(z); |
|
588bb7c…
|
aku
|
1536 |
} |
|
588bb7c…
|
aku
|
1537 |
|
|
588bb7c…
|
aku
|
1538 |
if( z && n>0 ){ |
|
0c99a15…
|
drh
|
1539 |
char *zResult; |
|
2116238…
|
drh
|
1540 |
int nn = TH1_LEN(n); |
|
2116238…
|
drh
|
1541 |
zResult = Th_Malloc(pInterp, nn+1); |
|
2116238…
|
drh
|
1542 |
th_memcpy(zResult, z, nn); |
|
2116238…
|
drh
|
1543 |
zResult[nn] = '\0'; |
|
588bb7c…
|
aku
|
1544 |
pInterp->zResult = zResult; |
|
588bb7c…
|
aku
|
1545 |
pInterp->nResult = n; |
|
588bb7c…
|
aku
|
1546 |
} |
|
588bb7c…
|
aku
|
1547 |
|
|
588bb7c…
|
aku
|
1548 |
return TH_OK; |
|
588bb7c…
|
aku
|
1549 |
} |
|
588bb7c…
|
aku
|
1550 |
|
|
588bb7c…
|
aku
|
1551 |
/* |
|
588bb7c…
|
aku
|
1552 |
** Return a pointer to the buffer containing the current interpreter |
|
588bb7c…
|
aku
|
1553 |
** result. If pN is not NULL, set *pN to the size of the returned |
|
588bb7c…
|
aku
|
1554 |
** buffer. |
|
588bb7c…
|
aku
|
1555 |
*/ |
|
0c99a15…
|
drh
|
1556 |
const char *Th_GetResult(Th_Interp *pInterp, int *pN){ |
|
588bb7c…
|
aku
|
1557 |
assert(pInterp->zResult || pInterp->nResult==0); |
|
588bb7c…
|
aku
|
1558 |
if( pN ){ |
|
588bb7c…
|
aku
|
1559 |
*pN = pInterp->nResult; |
|
588bb7c…
|
aku
|
1560 |
} |
|
0c99a15…
|
drh
|
1561 |
return (pInterp->zResult ? pInterp->zResult : (const char *)""); |
|
588bb7c…
|
aku
|
1562 |
} |
|
588bb7c…
|
aku
|
1563 |
|
|
588bb7c…
|
aku
|
1564 |
/* |
|
588bb7c…
|
aku
|
1565 |
** Return a pointer to the buffer containing the current interpreter |
|
588bb7c…
|
aku
|
1566 |
** result. If pN is not NULL, set *pN to the size of the returned |
|
588bb7c…
|
aku
|
1567 |
** buffer. |
|
588bb7c…
|
aku
|
1568 |
** |
|
588bb7c…
|
aku
|
1569 |
** This function is the same as Th_GetResult() except that the |
|
588bb7c…
|
aku
|
1570 |
** caller is responsible for eventually calling Th_Free() on the |
|
588bb7c…
|
aku
|
1571 |
** returned buffer. The internal interpreter result is cleared |
|
588bb7c…
|
aku
|
1572 |
** after this function is called. |
|
588bb7c…
|
aku
|
1573 |
*/ |
|
0c99a15…
|
drh
|
1574 |
char *Th_TakeResult(Th_Interp *pInterp, int *pN){ |
|
588bb7c…
|
aku
|
1575 |
if( pN ){ |
|
588bb7c…
|
aku
|
1576 |
*pN = pInterp->nResult; |
|
588bb7c…
|
aku
|
1577 |
} |
|
588bb7c…
|
aku
|
1578 |
if( pInterp->zResult ){ |
|
0c99a15…
|
drh
|
1579 |
char *zResult = pInterp->zResult; |
|
588bb7c…
|
aku
|
1580 |
pInterp->zResult = 0; |
|
588bb7c…
|
aku
|
1581 |
pInterp->nResult = 0; |
|
588bb7c…
|
aku
|
1582 |
return zResult; |
|
588bb7c…
|
aku
|
1583 |
}else{ |
|
0c99a15…
|
drh
|
1584 |
return (char *)Th_Malloc(pInterp, 1); |
|
588bb7c…
|
aku
|
1585 |
} |
|
588bb7c…
|
aku
|
1586 |
} |
|
999e33c…
|
mistachkin
|
1587 |
|
|
999e33c…
|
mistachkin
|
1588 |
#if defined(TH_MEMDEBUG) |
|
999e33c…
|
mistachkin
|
1589 |
/* |
|
999e33c…
|
mistachkin
|
1590 |
** Wrappers around the supplied malloc() and free() |
|
999e33c…
|
mistachkin
|
1591 |
*/ |
|
999e33c…
|
mistachkin
|
1592 |
void *Th_DbgMalloc(Th_Interp *pInterp, int nByte){ |
|
999e33c…
|
mistachkin
|
1593 |
void *p; |
|
999e33c…
|
mistachkin
|
1594 |
Th_Vtab *pVtab = pInterp->pVtab; |
|
999e33c…
|
mistachkin
|
1595 |
if( pVtab ){ |
|
999e33c…
|
mistachkin
|
1596 |
p = pVtab->xMalloc(nByte); |
|
999e33c…
|
mistachkin
|
1597 |
if( p ) memset(p, 0, nByte); |
|
999e33c…
|
mistachkin
|
1598 |
}else{ |
|
999e33c…
|
mistachkin
|
1599 |
p = Th_SysMalloc(pInterp, nByte); |
|
999e33c…
|
mistachkin
|
1600 |
} |
|
999e33c…
|
mistachkin
|
1601 |
return p; |
|
999e33c…
|
mistachkin
|
1602 |
} |
|
999e33c…
|
mistachkin
|
1603 |
void Th_DbgFree(Th_Interp *pInterp, void *z){ |
|
999e33c…
|
mistachkin
|
1604 |
if( z ){ |
|
999e33c…
|
mistachkin
|
1605 |
Th_Vtab *pVtab = pInterp->pVtab; |
|
999e33c…
|
mistachkin
|
1606 |
if( pVtab ){ |
|
999e33c…
|
mistachkin
|
1607 |
pVtab->xFree(z); |
|
999e33c…
|
mistachkin
|
1608 |
}else{ |
|
999e33c…
|
mistachkin
|
1609 |
Th_SysFree(pInterp, z); |
|
999e33c…
|
mistachkin
|
1610 |
} |
|
999e33c…
|
mistachkin
|
1611 |
} |
|
999e33c…
|
mistachkin
|
1612 |
} |
|
999e33c…
|
mistachkin
|
1613 |
#endif |
|
588bb7c…
|
aku
|
1614 |
|
|
588bb7c…
|
aku
|
1615 |
/* |
|
1ebe4b0…
|
mistachkin
|
1616 |
** Install a new th1 command. |
|
588bb7c…
|
aku
|
1617 |
** |
|
588bb7c…
|
aku
|
1618 |
** If a command of the same name already exists, it is deleted automatically. |
|
588bb7c…
|
aku
|
1619 |
*/ |
|
588bb7c…
|
aku
|
1620 |
int Th_CreateCommand( |
|
1ebe4b0…
|
mistachkin
|
1621 |
Th_Interp *interp, |
|
588bb7c…
|
aku
|
1622 |
const char *zName, /* New command name */ |
|
588bb7c…
|
aku
|
1623 |
Th_CommandProc xProc, /* Command callback proc */ |
|
588bb7c…
|
aku
|
1624 |
void *pContext, /* Value to pass as second arg to xProc */ |
|
588bb7c…
|
aku
|
1625 |
void (*xDel)(Th_Interp *, void *) /* Command destructor callback */ |
|
588bb7c…
|
aku
|
1626 |
){ |
|
588bb7c…
|
aku
|
1627 |
Th_HashEntry *pEntry; |
|
588bb7c…
|
aku
|
1628 |
Th_Command *pCommand; |
|
588bb7c…
|
aku
|
1629 |
|
|
0c99a15…
|
drh
|
1630 |
pEntry = Th_HashFind(interp, interp->paCmd, (const char *)zName, -1, 1); |
|
588bb7c…
|
aku
|
1631 |
if( pEntry->pData ){ |
|
588bb7c…
|
aku
|
1632 |
pCommand = pEntry->pData; |
|
588bb7c…
|
aku
|
1633 |
if( pCommand->xDel ){ |
|
588bb7c…
|
aku
|
1634 |
pCommand->xDel(interp, pCommand->pContext); |
|
588bb7c…
|
aku
|
1635 |
} |
|
588bb7c…
|
aku
|
1636 |
}else{ |
|
588bb7c…
|
aku
|
1637 |
pCommand = Th_Malloc(interp, sizeof(Th_Command)); |
|
588bb7c…
|
aku
|
1638 |
} |
|
588bb7c…
|
aku
|
1639 |
pCommand->xProc = xProc; |
|
588bb7c…
|
aku
|
1640 |
pCommand->pContext = pContext; |
|
588bb7c…
|
aku
|
1641 |
pCommand->xDel = xDel; |
|
588bb7c…
|
aku
|
1642 |
pEntry->pData = (void *)pCommand; |
|
1ebe4b0…
|
mistachkin
|
1643 |
|
|
588bb7c…
|
aku
|
1644 |
return TH_OK; |
|
588bb7c…
|
aku
|
1645 |
} |
|
588bb7c…
|
aku
|
1646 |
|
|
588bb7c…
|
aku
|
1647 |
/* |
|
1ebe4b0…
|
mistachkin
|
1648 |
** Rename the existing command (zName, nName) to (zNew, nNew). If nNew is 0, |
|
588bb7c…
|
aku
|
1649 |
** the command is deleted instead of renamed. |
|
588bb7c…
|
aku
|
1650 |
** |
|
588bb7c…
|
aku
|
1651 |
** If successful, TH_OK is returned. If command zName does not exist, or |
|
1ebe4b0…
|
mistachkin
|
1652 |
** if command zNew already exists, an error message is left in the |
|
588bb7c…
|
aku
|
1653 |
** interpreter result and TH_ERROR is returned. |
|
588bb7c…
|
aku
|
1654 |
*/ |
|
588bb7c…
|
aku
|
1655 |
int Th_RenameCommand( |
|
1ebe4b0…
|
mistachkin
|
1656 |
Th_Interp *interp, |
|
1ebe4b0…
|
mistachkin
|
1657 |
const char *zName, /* Existing command name */ |
|
588bb7c…
|
aku
|
1658 |
int nName, /* Number of bytes at zName */ |
|
1ebe4b0…
|
mistachkin
|
1659 |
const char *zNew, /* New command name */ |
|
588bb7c…
|
aku
|
1660 |
int nNew /* Number of bytes at zNew */ |
|
588bb7c…
|
aku
|
1661 |
){ |
|
588bb7c…
|
aku
|
1662 |
Th_HashEntry *pEntry; |
|
588bb7c…
|
aku
|
1663 |
Th_HashEntry *pNewEntry; |
|
588bb7c…
|
aku
|
1664 |
|
|
588bb7c…
|
aku
|
1665 |
pEntry = Th_HashFind(interp, interp->paCmd, zName, nName, 0); |
|
588bb7c…
|
aku
|
1666 |
if( !pEntry ){ |
|
588bb7c…
|
aku
|
1667 |
Th_ErrorMessage(interp, "no such command:", zName, nName); |
|
588bb7c…
|
aku
|
1668 |
return TH_ERROR; |
|
588bb7c…
|
aku
|
1669 |
} |
|
588bb7c…
|
aku
|
1670 |
assert(pEntry->pData); |
|
588bb7c…
|
aku
|
1671 |
|
|
588bb7c…
|
aku
|
1672 |
if( nNew>0 ){ |
|
588bb7c…
|
aku
|
1673 |
pNewEntry = Th_HashFind(interp, interp->paCmd, zNew, nNew, 1); |
|
588bb7c…
|
aku
|
1674 |
if( pNewEntry->pData ){ |
|
588bb7c…
|
aku
|
1675 |
Th_ErrorMessage(interp, "command exists:", zNew, nNew); |
|
588bb7c…
|
aku
|
1676 |
return TH_ERROR; |
|
588bb7c…
|
aku
|
1677 |
} |
|
588bb7c…
|
aku
|
1678 |
pNewEntry->pData = pEntry->pData; |
|
588bb7c…
|
aku
|
1679 |
}else{ |
|
588bb7c…
|
aku
|
1680 |
Th_Command *pCommand = (Th_Command *)(pEntry->pData); |
|
588bb7c…
|
aku
|
1681 |
if( pCommand->xDel ){ |
|
588bb7c…
|
aku
|
1682 |
pCommand->xDel(interp, pCommand->pContext); |
|
588bb7c…
|
aku
|
1683 |
} |
|
588bb7c…
|
aku
|
1684 |
Th_Free(interp, pCommand); |
|
588bb7c…
|
aku
|
1685 |
} |
|
588bb7c…
|
aku
|
1686 |
|
|
588bb7c…
|
aku
|
1687 |
Th_HashFind(interp, interp->paCmd, zName, nName, -1); |
|
588bb7c…
|
aku
|
1688 |
return TH_OK; |
|
588bb7c…
|
aku
|
1689 |
} |
|
588bb7c…
|
aku
|
1690 |
|
|
588bb7c…
|
aku
|
1691 |
/* |
|
588bb7c…
|
aku
|
1692 |
** Push a stack frame onto the interpreter stack, invoke the |
|
588bb7c…
|
aku
|
1693 |
** callback, and pop the frame back off again. See the implementation |
|
588bb7c…
|
aku
|
1694 |
** of [proc] (th_lang.c) for an example. |
|
588bb7c…
|
aku
|
1695 |
*/ |
|
588bb7c…
|
aku
|
1696 |
int Th_InFrame(Th_Interp *interp, |
|
588bb7c…
|
aku
|
1697 |
int (*xCall)(Th_Interp *, void *pContext1, void *pContext2), |
|
588bb7c…
|
aku
|
1698 |
void *pContext1, |
|
588bb7c…
|
aku
|
1699 |
void *pContext2 |
|
588bb7c…
|
aku
|
1700 |
){ |
|
588bb7c…
|
aku
|
1701 |
Th_Frame frame; |
|
588bb7c…
|
aku
|
1702 |
int rc; |
|
588bb7c…
|
aku
|
1703 |
thPushFrame(interp, &frame); |
|
588bb7c…
|
aku
|
1704 |
rc = xCall(interp, pContext1, pContext2); |
|
588bb7c…
|
aku
|
1705 |
thPopFrame(interp); |
|
588bb7c…
|
aku
|
1706 |
return rc; |
|
588bb7c…
|
aku
|
1707 |
} |
|
588bb7c…
|
aku
|
1708 |
|
|
588bb7c…
|
aku
|
1709 |
/* |
|
588bb7c…
|
aku
|
1710 |
** Split a th1 list into its component elements. The list to split is |
|
588bb7c…
|
aku
|
1711 |
** passed via arguments (zList, nList). If successful, TH_OK is returned. |
|
588bb7c…
|
aku
|
1712 |
** If an error occurs (if (zList, nList) is not a valid list) an error |
|
588bb7c…
|
aku
|
1713 |
** message is left in the interpreter result and TH_ERROR returned. |
|
588bb7c…
|
aku
|
1714 |
** |
|
588bb7c…
|
aku
|
1715 |
** If successful, *pnCount is set to the number of elements in the list. |
|
588bb7c…
|
aku
|
1716 |
** panElem is set to point at an array of *pnCount integers - the lengths |
|
1ebe4b0…
|
mistachkin
|
1717 |
** of the element values. *pazElem is set to point at an array of |
|
588bb7c…
|
aku
|
1718 |
** pointers to buffers containing the array element's data. |
|
588bb7c…
|
aku
|
1719 |
** |
|
588bb7c…
|
aku
|
1720 |
** To free the arrays allocated at *pazElem and *panElem, the caller |
|
588bb7c…
|
aku
|
1721 |
** should call Th_Free() on *pazElem only. Exactly one such call to |
|
588bb7c…
|
aku
|
1722 |
** Th_Free() must be made per call to Th_SplitList(). |
|
588bb7c…
|
aku
|
1723 |
** |
|
588bb7c…
|
aku
|
1724 |
** Example: |
|
588bb7c…
|
aku
|
1725 |
** |
|
588bb7c…
|
aku
|
1726 |
** int nElem; |
|
588bb7c…
|
aku
|
1727 |
** int *anElem; |
|
0c99a15…
|
drh
|
1728 |
** char **azElem; |
|
588bb7c…
|
aku
|
1729 |
** int i; |
|
588bb7c…
|
aku
|
1730 |
** |
|
588bb7c…
|
aku
|
1731 |
** Th_SplitList(interp, zList, nList, &azElem, &anElem, &nElem); |
|
588bb7c…
|
aku
|
1732 |
** for(i=0; i<nElem; i++){ |
|
588bb7c…
|
aku
|
1733 |
** int nData = anElem[i]; |
|
0c99a15…
|
drh
|
1734 |
** char *zData = azElem[i]; |
|
588bb7c…
|
aku
|
1735 |
** ... |
|
588bb7c…
|
aku
|
1736 |
** } |
|
588bb7c…
|
aku
|
1737 |
** |
|
588bb7c…
|
aku
|
1738 |
** Th_Free(interp, azElem); |
|
588bb7c…
|
aku
|
1739 |
** |
|
588bb7c…
|
aku
|
1740 |
*/ |
|
588bb7c…
|
aku
|
1741 |
int Th_SplitList( |
|
588bb7c…
|
aku
|
1742 |
Th_Interp *interp, |
|
1ebe4b0…
|
mistachkin
|
1743 |
const char *zList, /* Pointer to buffer containing list */ |
|
588bb7c…
|
aku
|
1744 |
int nList, /* Number of bytes at zList */ |
|
1ebe4b0…
|
mistachkin
|
1745 |
char ***pazElem, /* OUT: Array of pointers to element data */ |
|
588bb7c…
|
aku
|
1746 |
int **panElem, /* OUT: Array of element data lengths */ |
|
588bb7c…
|
aku
|
1747 |
int *pnCount /* OUT: Number of elements in list */ |
|
588bb7c…
|
aku
|
1748 |
){ |
|
588bb7c…
|
aku
|
1749 |
int rc; |
|
588bb7c…
|
aku
|
1750 |
interp->isListMode = 1; |
|
588bb7c…
|
aku
|
1751 |
rc = thSplitList(interp, zList, nList, pazElem, panElem, pnCount); |
|
588bb7c…
|
aku
|
1752 |
interp->isListMode = 0; |
|
588bb7c…
|
aku
|
1753 |
if( rc ){ |
|
588bb7c…
|
aku
|
1754 |
Th_ErrorMessage(interp, "Expected list, got: \"", zList, nList); |
|
588bb7c…
|
aku
|
1755 |
} |
|
588bb7c…
|
aku
|
1756 |
return rc; |
|
588bb7c…
|
aku
|
1757 |
} |
|
588bb7c…
|
aku
|
1758 |
|
|
588bb7c…
|
aku
|
1759 |
/* |
|
1ebe4b0…
|
mistachkin
|
1760 |
** Append a new element to an existing th1 list. The element to append |
|
588bb7c…
|
aku
|
1761 |
** to the list is (zElem, nElem). |
|
588bb7c…
|
aku
|
1762 |
** |
|
588bb7c…
|
aku
|
1763 |
** A pointer to the existing list must be stored at *pzList when this |
|
1ebe4b0…
|
mistachkin
|
1764 |
** function is called. The length must be stored in *pnList. The value |
|
1ebe4b0…
|
mistachkin
|
1765 |
** of *pzList must either be NULL (in which case *pnList must be 0), or |
|
588bb7c…
|
aku
|
1766 |
** a pointer to memory obtained from Th_Malloc(). |
|
588bb7c…
|
aku
|
1767 |
** |
|
588bb7c…
|
aku
|
1768 |
** This function calls Th_Free() to free the buffer at *pzList and sets |
|
588bb7c…
|
aku
|
1769 |
** *pzList to point to a new buffer containing the new list value. *pnList |
|
588bb7c…
|
aku
|
1770 |
** is similarly updated before returning. The return value is always TH_OK. |
|
588bb7c…
|
aku
|
1771 |
** |
|
588bb7c…
|
aku
|
1772 |
** Example: |
|
588bb7c…
|
aku
|
1773 |
** |
|
0c99a15…
|
drh
|
1774 |
** char *zList = 0; |
|
588bb7c…
|
aku
|
1775 |
** int nList = 0; |
|
588bb7c…
|
aku
|
1776 |
** for (...) { |
|
0c99a15…
|
drh
|
1777 |
** char *zElem = <some expression>; |
|
588bb7c…
|
aku
|
1778 |
** Th_ListAppend(interp, &zList, &nList, zElem, -1); |
|
588bb7c…
|
aku
|
1779 |
** } |
|
588bb7c…
|
aku
|
1780 |
** Th_SetResult(interp, zList, nList); |
|
588bb7c…
|
aku
|
1781 |
** Th_Free(interp, zList); |
|
588bb7c…
|
aku
|
1782 |
** |
|
588bb7c…
|
aku
|
1783 |
*/ |
|
588bb7c…
|
aku
|
1784 |
int Th_ListAppend( |
|
588bb7c…
|
aku
|
1785 |
Th_Interp *interp, /* Interpreter context */ |
|
1ebe4b0…
|
mistachkin
|
1786 |
char **pzList, /* IN/OUT: Ptr to ptr to list */ |
|
588bb7c…
|
aku
|
1787 |
int *pnList, /* IN/OUT: Current length of *pzList */ |
|
1ebe4b0…
|
mistachkin
|
1788 |
const char *zElem, /* Data to append */ |
|
588bb7c…
|
aku
|
1789 |
int nElem /* Length of nElem */ |
|
588bb7c…
|
aku
|
1790 |
){ |
|
588bb7c…
|
aku
|
1791 |
Buffer output; |
|
588bb7c…
|
aku
|
1792 |
int i; |
|
588bb7c…
|
aku
|
1793 |
|
|
4ec65eb…
|
drh
|
1794 |
int hasSpecialChar = 0; /* Whitespace or {}[]'" */ |
|
4ec65eb…
|
drh
|
1795 |
int hasEscapeChar = 0; /* '}' without matching '{' to the left or a '\\' */ |
|
0c99a15…
|
drh
|
1796 |
int nBrace = 0; |
|
0c99a15…
|
drh
|
1797 |
|
|
0c99a15…
|
drh
|
1798 |
output.zBuf = *pzList; |
|
aa66767…
|
drh
|
1799 |
output.nBuf = TH1_LEN(*pnList); |
|
0c99a15…
|
drh
|
1800 |
output.nBufAlloc = output.nBuf; |
|
aa66767…
|
drh
|
1801 |
output.bTaint = 0; |
|
aa66767…
|
drh
|
1802 |
TH1_XFER_TAINT(output.bTaint, *pnList); |
|
0c99a15…
|
drh
|
1803 |
|
|
0c99a15…
|
drh
|
1804 |
if( nElem<0 ){ |
|
0c99a15…
|
drh
|
1805 |
nElem = th_strlen(zElem); |
|
ded2126…
|
drh
|
1806 |
}else{ |
|
ded2126…
|
drh
|
1807 |
nElem = TH1_LEN(nElem); |
|
0c99a15…
|
drh
|
1808 |
} |
|
0c99a15…
|
drh
|
1809 |
if( output.nBuf>0 ){ |
|
b26eea7…
|
drh
|
1810 |
thBufferAddChar(interp, &output, ' '); |
|
0c99a15…
|
drh
|
1811 |
} |
|
0c99a15…
|
drh
|
1812 |
|
|
0c99a15…
|
drh
|
1813 |
for(i=0; i<nElem; i++){ |
|
0c99a15…
|
drh
|
1814 |
char c = zElem[i]; |
|
0c99a15…
|
drh
|
1815 |
if( th_isspecial(c) ) hasSpecialChar = 1; |
|
4ec65eb…
|
drh
|
1816 |
if( c=='\\' ){ hasEscapeChar = 1; break; } |
|
0c99a15…
|
drh
|
1817 |
if( c=='{' ) nBrace++; |
|
4ec65eb…
|
drh
|
1818 |
if( c=='}' ){ |
|
4ec65eb…
|
drh
|
1819 |
if( nBrace==0 ){ |
|
4ec65eb…
|
drh
|
1820 |
/* A closing brace that does not have a matching open brace to |
|
4ec65eb…
|
drh
|
1821 |
** its left needs to be excaped. See ticket 4d73b4a2258a78e2 */ |
|
4ec65eb…
|
drh
|
1822 |
hasEscapeChar = 1; |
|
4ec65eb…
|
drh
|
1823 |
break; |
|
4ec65eb…
|
drh
|
1824 |
}else{ |
|
4ec65eb…
|
drh
|
1825 |
nBrace--; |
|
4ec65eb…
|
drh
|
1826 |
} |
|
4ec65eb…
|
drh
|
1827 |
} |
|
0c99a15…
|
drh
|
1828 |
} |
|
0c99a15…
|
drh
|
1829 |
|
|
0c99a15…
|
drh
|
1830 |
if( nElem==0 || (!hasEscapeChar && hasSpecialChar && nBrace==0) ){ |
|
b26eea7…
|
drh
|
1831 |
thBufferAddChar(interp, &output, '{'); |
|
0c99a15…
|
drh
|
1832 |
thBufferWrite(interp, &output, zElem, nElem); |
|
b26eea7…
|
drh
|
1833 |
thBufferAddChar(interp, &output, '}'); |
|
0c99a15…
|
drh
|
1834 |
}else{ |
|
0c99a15…
|
drh
|
1835 |
for(i=0; i<nElem; i++){ |
|
0c99a15…
|
drh
|
1836 |
char c = zElem[i]; |
|
b26eea7…
|
drh
|
1837 |
if( th_isspecial(c) ) thBufferAddChar(interp, &output, '\\'); |
|
b26eea7…
|
drh
|
1838 |
thBufferAddChar(interp, &output, c); |
|
588bb7c…
|
aku
|
1839 |
} |
|
588bb7c…
|
aku
|
1840 |
} |
|
588bb7c…
|
aku
|
1841 |
|
|
588bb7c…
|
aku
|
1842 |
*pzList = output.zBuf; |
|
588bb7c…
|
aku
|
1843 |
*pnList = output.nBuf; |
|
588bb7c…
|
aku
|
1844 |
|
|
588bb7c…
|
aku
|
1845 |
return TH_OK; |
|
588bb7c…
|
aku
|
1846 |
} |
|
588bb7c…
|
aku
|
1847 |
|
|
588bb7c…
|
aku
|
1848 |
/* |
|
588bb7c…
|
aku
|
1849 |
** Append a new element to an existing th1 string. This function uses |
|
588bb7c…
|
aku
|
1850 |
** the same interface as the Th_ListAppend() function. |
|
588bb7c…
|
aku
|
1851 |
*/ |
|
588bb7c…
|
aku
|
1852 |
int Th_StringAppend( |
|
588bb7c…
|
aku
|
1853 |
Th_Interp *interp, /* Interpreter context */ |
|
1ebe4b0…
|
mistachkin
|
1854 |
char **pzStr, /* IN/OUT: Ptr to ptr to list */ |
|
588bb7c…
|
aku
|
1855 |
int *pnStr, /* IN/OUT: Current length of *pzStr */ |
|
1ebe4b0…
|
mistachkin
|
1856 |
const char *zElem, /* Data to append */ |
|
588bb7c…
|
aku
|
1857 |
int nElem /* Length of nElem */ |
|
588bb7c…
|
aku
|
1858 |
){ |
|
0c99a15…
|
drh
|
1859 |
char *zNew; |
|
2116238…
|
drh
|
1860 |
long long int nNew; |
|
2116238…
|
drh
|
1861 |
int nn; |
|
588bb7c…
|
aku
|
1862 |
|
|
588bb7c…
|
aku
|
1863 |
if( nElem<0 ){ |
|
2116238…
|
drh
|
1864 |
nn = th_strlen(zElem); |
|
2116238…
|
drh
|
1865 |
}else{ |
|
2116238…
|
drh
|
1866 |
nn = TH1_LEN(nElem); |
|
588bb7c…
|
aku
|
1867 |
} |
|
588bb7c…
|
aku
|
1868 |
|
|
2116238…
|
drh
|
1869 |
nNew = TH1_LEN(*pnStr) + nn; |
|
2116238…
|
drh
|
1870 |
TH1_SIZECHECK(nNew); |
|
588bb7c…
|
aku
|
1871 |
zNew = Th_Malloc(interp, nNew); |
|
7ae4b1a…
|
drh
|
1872 |
th_memcpy(zNew, *pzStr, *pnStr); |
|
2116238…
|
drh
|
1873 |
th_memcpy(&zNew[TH1_LEN(*pnStr)], zElem, nn); |
|
588bb7c…
|
aku
|
1874 |
|
|
588bb7c…
|
aku
|
1875 |
Th_Free(interp, *pzStr); |
|
588bb7c…
|
aku
|
1876 |
*pzStr = zNew; |
|
2116238…
|
drh
|
1877 |
*pnStr = (int)nNew; |
|
ea7813d…
|
mistachkin
|
1878 |
|
|
ea7813d…
|
mistachkin
|
1879 |
return TH_OK; |
|
ea7813d…
|
mistachkin
|
1880 |
} |
|
ea7813d…
|
mistachkin
|
1881 |
|
|
ea7813d…
|
mistachkin
|
1882 |
/* |
|
ea7813d…
|
mistachkin
|
1883 |
** Initialize an interpreter. |
|
ea7813d…
|
mistachkin
|
1884 |
*/ |
|
ea7813d…
|
mistachkin
|
1885 |
static int thInitialize(Th_Interp *interp){ |
|
ea7813d…
|
mistachkin
|
1886 |
assert(interp->pFrame); |
|
ea7813d…
|
mistachkin
|
1887 |
|
|
ea7813d…
|
mistachkin
|
1888 |
Th_SetVar(interp, (char *)"::tcl_platform(engine)", -1, TH_ENGINE, -1); |
|
ea7813d…
|
mistachkin
|
1889 |
Th_SetVar(interp, (char *)"::tcl_platform(platform)", -1, TH_PLATFORM, -1); |
|
588bb7c…
|
aku
|
1890 |
|
|
588bb7c…
|
aku
|
1891 |
return TH_OK; |
|
588bb7c…
|
aku
|
1892 |
} |
|
588bb7c…
|
aku
|
1893 |
|
|
1ebe4b0…
|
mistachkin
|
1894 |
/* |
|
588bb7c…
|
aku
|
1895 |
** Delete an interpreter. |
|
588bb7c…
|
aku
|
1896 |
*/ |
|
588bb7c…
|
aku
|
1897 |
void Th_DeleteInterp(Th_Interp *interp){ |
|
588bb7c…
|
aku
|
1898 |
assert(interp->pFrame); |
|
588bb7c…
|
aku
|
1899 |
assert(0==interp->pFrame->pCaller); |
|
588bb7c…
|
aku
|
1900 |
|
|
588bb7c…
|
aku
|
1901 |
/* Delete the contents of the global frame. */ |
|
588bb7c…
|
aku
|
1902 |
thPopFrame(interp); |
|
588bb7c…
|
aku
|
1903 |
|
|
588bb7c…
|
aku
|
1904 |
/* Delete any result currently stored in the interpreter. */ |
|
588bb7c…
|
aku
|
1905 |
Th_SetResult(interp, 0, 0); |
|
588bb7c…
|
aku
|
1906 |
|
|
588bb7c…
|
aku
|
1907 |
/* Delete all registered commands and the command hash-table itself. */ |
|
588bb7c…
|
aku
|
1908 |
Th_HashIterate(interp, interp->paCmd, thFreeCommand, (void *)interp); |
|
588bb7c…
|
aku
|
1909 |
Th_HashDelete(interp, interp->paCmd); |
|
588bb7c…
|
aku
|
1910 |
|
|
588bb7c…
|
aku
|
1911 |
/* Delete the interpreter structure itself. */ |
|
588bb7c…
|
aku
|
1912 |
Th_Free(interp, (void *)interp); |
|
588bb7c…
|
aku
|
1913 |
} |
|
588bb7c…
|
aku
|
1914 |
|
|
1ebe4b0…
|
mistachkin
|
1915 |
/* |
|
588bb7c…
|
aku
|
1916 |
** Create a new interpreter. |
|
588bb7c…
|
aku
|
1917 |
*/ |
|
999e33c…
|
mistachkin
|
1918 |
Th_Interp * Th_CreateInterp(Th_Vtab *pVtab){ |
|
999e33c…
|
mistachkin
|
1919 |
int nByte = sizeof(Th_Interp) + sizeof(Th_Frame); |
|
588bb7c…
|
aku
|
1920 |
Th_Interp *p; |
|
588bb7c…
|
aku
|
1921 |
|
|
588bb7c…
|
aku
|
1922 |
/* Allocate and initialise the interpreter and the global frame */ |
|
999e33c…
|
mistachkin
|
1923 |
#if defined(TH_MEMDEBUG) |
|
999e33c…
|
mistachkin
|
1924 |
if( pVtab ){ |
|
999e33c…
|
mistachkin
|
1925 |
p = pVtab->xMalloc(nByte); |
|
999e33c…
|
mistachkin
|
1926 |
memset(p, 0, nByte); |
|
999e33c…
|
mistachkin
|
1927 |
p->pVtab = pVtab; |
|
999e33c…
|
mistachkin
|
1928 |
}else |
|
999e33c…
|
mistachkin
|
1929 |
#endif |
|
999e33c…
|
mistachkin
|
1930 |
p = Th_SysMalloc(0, nByte); |
|
999e33c…
|
mistachkin
|
1931 |
|
|
588bb7c…
|
aku
|
1932 |
p->paCmd = Th_HashNew(p); |
|
588bb7c…
|
aku
|
1933 |
thPushFrame(p, (Th_Frame *)&p[1]); |
|
ea7813d…
|
mistachkin
|
1934 |
thInitialize(p); |
|
588bb7c…
|
aku
|
1935 |
|
|
588bb7c…
|
aku
|
1936 |
return p; |
|
588bb7c…
|
aku
|
1937 |
} |
|
588bb7c…
|
aku
|
1938 |
|
|
588bb7c…
|
aku
|
1939 |
/* |
|
588bb7c…
|
aku
|
1940 |
** These two types are used only by the expression module, where |
|
588bb7c…
|
aku
|
1941 |
** the expression module means the Th_Expr() and exprXXX() functions. |
|
588bb7c…
|
aku
|
1942 |
*/ |
|
588bb7c…
|
aku
|
1943 |
typedef struct Operator Operator; |
|
588bb7c…
|
aku
|
1944 |
struct Operator { |
|
588bb7c…
|
aku
|
1945 |
const char *zOp; |
|
66ae70a…
|
drh
|
1946 |
int nOp; |
|
588bb7c…
|
aku
|
1947 |
int eOp; |
|
588bb7c…
|
aku
|
1948 |
int iPrecedence; |
|
588bb7c…
|
aku
|
1949 |
int eArgType; |
|
588bb7c…
|
aku
|
1950 |
}; |
|
588bb7c…
|
aku
|
1951 |
typedef struct Expr Expr; |
|
588bb7c…
|
aku
|
1952 |
struct Expr { |
|
588bb7c…
|
aku
|
1953 |
Operator *pOp; |
|
588bb7c…
|
aku
|
1954 |
Expr *pParent; |
|
588bb7c…
|
aku
|
1955 |
Expr *pLeft; |
|
588bb7c…
|
aku
|
1956 |
Expr *pRight; |
|
588bb7c…
|
aku
|
1957 |
|
|
1ebe4b0…
|
mistachkin
|
1958 |
char *zValue; /* Pointer to literal value */ |
|
588bb7c…
|
aku
|
1959 |
int nValue; /* Length of literal value buffer */ |
|
588bb7c…
|
aku
|
1960 |
}; |
|
588bb7c…
|
aku
|
1961 |
|
|
588bb7c…
|
aku
|
1962 |
/* Unary operators */ |
|
588bb7c…
|
aku
|
1963 |
#define OP_UNARY_MINUS 2 |
|
588bb7c…
|
aku
|
1964 |
#define OP_UNARY_PLUS 3 |
|
588bb7c…
|
aku
|
1965 |
#define OP_BITWISE_NOT 4 |
|
588bb7c…
|
aku
|
1966 |
#define OP_LOGICAL_NOT 5 |
|
588bb7c…
|
aku
|
1967 |
|
|
588bb7c…
|
aku
|
1968 |
/* Binary operators */ |
|
588bb7c…
|
aku
|
1969 |
#define OP_MULTIPLY 6 |
|
588bb7c…
|
aku
|
1970 |
#define OP_DIVIDE 7 |
|
588bb7c…
|
aku
|
1971 |
#define OP_MODULUS 8 |
|
588bb7c…
|
aku
|
1972 |
#define OP_ADD 9 |
|
588bb7c…
|
aku
|
1973 |
#define OP_SUBTRACT 10 |
|
588bb7c…
|
aku
|
1974 |
#define OP_LEFTSHIFT 11 |
|
588bb7c…
|
aku
|
1975 |
#define OP_RIGHTSHIFT 12 |
|
588bb7c…
|
aku
|
1976 |
#define OP_LT 13 |
|
588bb7c…
|
aku
|
1977 |
#define OP_GT 14 |
|
588bb7c…
|
aku
|
1978 |
#define OP_LE 15 |
|
588bb7c…
|
aku
|
1979 |
#define OP_GE 16 |
|
588bb7c…
|
aku
|
1980 |
#define OP_EQ 17 |
|
588bb7c…
|
aku
|
1981 |
#define OP_NE 18 |
|
588bb7c…
|
aku
|
1982 |
#define OP_SEQ 19 |
|
588bb7c…
|
aku
|
1983 |
#define OP_SNE 20 |
|
588bb7c…
|
aku
|
1984 |
#define OP_BITWISE_AND 21 |
|
588bb7c…
|
aku
|
1985 |
#define OP_BITWISE_XOR 22 |
|
588bb7c…
|
aku
|
1986 |
#define OP_BITWISE_OR 24 |
|
588bb7c…
|
aku
|
1987 |
#define OP_LOGICAL_AND 25 |
|
588bb7c…
|
aku
|
1988 |
#define OP_LOGICAL_OR 26 |
|
588bb7c…
|
aku
|
1989 |
|
|
588bb7c…
|
aku
|
1990 |
/* Other symbols */ |
|
588bb7c…
|
aku
|
1991 |
#define OP_OPEN_BRACKET 27 |
|
588bb7c…
|
aku
|
1992 |
#define OP_CLOSE_BRACKET 28 |
|
588bb7c…
|
aku
|
1993 |
|
|
588bb7c…
|
aku
|
1994 |
/* Argument types. Each operator in the expression syntax is defined |
|
588bb7c…
|
aku
|
1995 |
** as requiring either integer, number (real or integer) or string |
|
588bb7c…
|
aku
|
1996 |
** operands. |
|
588bb7c…
|
aku
|
1997 |
*/ |
|
588bb7c…
|
aku
|
1998 |
#define ARG_INTEGER 1 |
|
588bb7c…
|
aku
|
1999 |
#define ARG_NUMBER 2 |
|
588bb7c…
|
aku
|
2000 |
#define ARG_STRING 3 |
|
588bb7c…
|
aku
|
2001 |
|
|
588bb7c…
|
aku
|
2002 |
static Operator aOperator[] = { |
|
588bb7c…
|
aku
|
2003 |
|
|
66ae70a…
|
drh
|
2004 |
{"(", 1, OP_OPEN_BRACKET, -1, 0}, |
|
66ae70a…
|
drh
|
2005 |
{")", 1, OP_CLOSE_BRACKET, -1, 0}, |
|
588bb7c…
|
aku
|
2006 |
|
|
588bb7c…
|
aku
|
2007 |
/* Note: all unary operators have (iPrecedence==1) */ |
|
66ae70a…
|
drh
|
2008 |
{"-", 1, OP_UNARY_MINUS, 1, ARG_NUMBER}, |
|
66ae70a…
|
drh
|
2009 |
{"+", 1, OP_UNARY_PLUS, 1, ARG_NUMBER}, |
|
66ae70a…
|
drh
|
2010 |
{"~", 1, OP_BITWISE_NOT, 1, ARG_INTEGER}, |
|
66ae70a…
|
drh
|
2011 |
{"!", 1, OP_LOGICAL_NOT, 1, ARG_INTEGER}, |
|
588bb7c…
|
aku
|
2012 |
|
|
588bb7c…
|
aku
|
2013 |
/* Binary operators. It is important to the parsing in Th_Expr() that |
|
1ebe4b0…
|
mistachkin
|
2014 |
* the two-character symbols ("==") appear before the one-character |
|
588bb7c…
|
aku
|
2015 |
* ones ("="). And that the priorities of all binary operators are |
|
588bb7c…
|
aku
|
2016 |
* integers between 2 and 12. |
|
588bb7c…
|
aku
|
2017 |
*/ |
|
66ae70a…
|
drh
|
2018 |
{"<<", 2, OP_LEFTSHIFT, 4, ARG_INTEGER}, |
|
66ae70a…
|
drh
|
2019 |
{">>", 2, OP_RIGHTSHIFT, 4, ARG_INTEGER}, |
|
66ae70a…
|
drh
|
2020 |
{"<=", 2, OP_LE, 5, ARG_NUMBER}, |
|
66ae70a…
|
drh
|
2021 |
{">=", 2, OP_GE, 5, ARG_NUMBER}, |
|
66ae70a…
|
drh
|
2022 |
{"==", 2, OP_EQ, 6, ARG_NUMBER}, |
|
66ae70a…
|
drh
|
2023 |
{"!=", 2, OP_NE, 6, ARG_NUMBER}, |
|
66ae70a…
|
drh
|
2024 |
{"eq", 2, OP_SEQ, 7, ARG_STRING}, |
|
66ae70a…
|
drh
|
2025 |
{"ne", 2, OP_SNE, 7, ARG_STRING}, |
|
66ae70a…
|
drh
|
2026 |
{"&&", 2, OP_LOGICAL_AND, 11, ARG_INTEGER}, |
|
66ae70a…
|
drh
|
2027 |
{"||", 2, OP_LOGICAL_OR, 12, ARG_INTEGER}, |
|
66ae70a…
|
drh
|
2028 |
|
|
66ae70a…
|
drh
|
2029 |
{"*", 1, OP_MULTIPLY, 2, ARG_NUMBER}, |
|
66ae70a…
|
drh
|
2030 |
{"/", 1, OP_DIVIDE, 2, ARG_NUMBER}, |
|
66ae70a…
|
drh
|
2031 |
{"%", 1, OP_MODULUS, 2, ARG_INTEGER}, |
|
66ae70a…
|
drh
|
2032 |
{"+", 1, OP_ADD, 3, ARG_NUMBER}, |
|
66ae70a…
|
drh
|
2033 |
{"-", 1, OP_SUBTRACT, 3, ARG_NUMBER}, |
|
66ae70a…
|
drh
|
2034 |
{"<", 1, OP_LT, 5, ARG_NUMBER}, |
|
66ae70a…
|
drh
|
2035 |
{">", 1, OP_GT, 5, ARG_NUMBER}, |
|
66ae70a…
|
drh
|
2036 |
{"&", 1, OP_BITWISE_AND, 8, ARG_INTEGER}, |
|
66ae70a…
|
drh
|
2037 |
{"^", 1, OP_BITWISE_XOR, 9, ARG_INTEGER}, |
|
66ae70a…
|
drh
|
2038 |
{"|", 1, OP_BITWISE_OR, 10, ARG_INTEGER}, |
|
66ae70a…
|
drh
|
2039 |
|
|
66ae70a…
|
drh
|
2040 |
{0,0,0,0,0} |
|
588bb7c…
|
aku
|
2041 |
}; |
|
99901fb…
|
jan.nijtmans
|
2042 |
|
|
99901fb…
|
jan.nijtmans
|
2043 |
/* |
|
99901fb…
|
jan.nijtmans
|
2044 |
** The first part of the string (zInput,nInput) contains an integer. |
|
99901fb…
|
jan.nijtmans
|
2045 |
** Set *pnVarname to the number of bytes in the numeric string. |
|
99901fb…
|
jan.nijtmans
|
2046 |
*/ |
|
99901fb…
|
jan.nijtmans
|
2047 |
static int thNextInteger( |
|
99901fb…
|
jan.nijtmans
|
2048 |
Th_Interp *interp, |
|
99901fb…
|
jan.nijtmans
|
2049 |
const char *zInput, |
|
99901fb…
|
jan.nijtmans
|
2050 |
int nInput, |
|
99901fb…
|
jan.nijtmans
|
2051 |
int *pnLiteral |
|
99901fb…
|
jan.nijtmans
|
2052 |
){ |
|
8d62798…
|
jan.nijtmans
|
2053 |
int i; |
|
99901fb…
|
jan.nijtmans
|
2054 |
int (*isdigit)(char) = th_isdigit; |
|
8d62798…
|
jan.nijtmans
|
2055 |
char c; |
|
8d62798…
|
jan.nijtmans
|
2056 |
|
|
b78ae90…
|
jan.nijtmans
|
2057 |
if( nInput<2) return TH_ERROR; |
|
8d62798…
|
jan.nijtmans
|
2058 |
assert(zInput[0]=='0'); |
|
8d62798…
|
jan.nijtmans
|
2059 |
c = zInput[1]; |
|
8d62798…
|
jan.nijtmans
|
2060 |
if( c>='A' && c<='Z' ) c += 'a' - 'A'; |
|
8d62798…
|
jan.nijtmans
|
2061 |
if( c=='x' ){ |
|
8d62798…
|
jan.nijtmans
|
2062 |
isdigit = th_ishexdig; |
|
8d62798…
|
jan.nijtmans
|
2063 |
}else if( c!='o' && c!='b' ){ |
|
8d62798…
|
jan.nijtmans
|
2064 |
return TH_ERROR; |
|
8d62798…
|
jan.nijtmans
|
2065 |
} |
|
8d62798…
|
jan.nijtmans
|
2066 |
for(i=2; i<nInput; i++){ |
|
8d62798…
|
jan.nijtmans
|
2067 |
c = zInput[i]; |
|
99901fb…
|
jan.nijtmans
|
2068 |
if( !isdigit(c) ){ |
|
99901fb…
|
jan.nijtmans
|
2069 |
break; |
|
99901fb…
|
jan.nijtmans
|
2070 |
} |
|
99901fb…
|
jan.nijtmans
|
2071 |
} |
|
99901fb…
|
jan.nijtmans
|
2072 |
*pnLiteral = i; |
|
99901fb…
|
jan.nijtmans
|
2073 |
return TH_OK; |
|
99901fb…
|
jan.nijtmans
|
2074 |
} |
|
1ebe4b0…
|
mistachkin
|
2075 |
|
|
1ebe4b0…
|
mistachkin
|
2076 |
/* |
|
1ebe4b0…
|
mistachkin
|
2077 |
** The first part of the string (zInput,nInput) contains a number. |
|
1ebe4b0…
|
mistachkin
|
2078 |
** Set *pnVarname to the number of bytes in the numeric string. |
|
1ebe4b0…
|
mistachkin
|
2079 |
*/ |
|
1ebe4b0…
|
mistachkin
|
2080 |
static int thNextNumber( |
|
1ebe4b0…
|
mistachkin
|
2081 |
Th_Interp *interp, |
|
1ebe4b0…
|
mistachkin
|
2082 |
const char *zInput, |
|
1ebe4b0…
|
mistachkin
|
2083 |
int nInput, |
|
1ebe4b0…
|
mistachkin
|
2084 |
int *pnLiteral |
|
1ebe4b0…
|
mistachkin
|
2085 |
){ |
|
1f6734c…
|
jan.nijtmans
|
2086 |
int i = 0; |
|
588bb7c…
|
aku
|
2087 |
int seenDot = 0; |
|
1f6734c…
|
jan.nijtmans
|
2088 |
for(; i<nInput; i++){ |
|
0c99a15…
|
drh
|
2089 |
char c = zInput[i]; |
|
99901fb…
|
jan.nijtmans
|
2090 |
if( (seenDot || c!='.') && !th_isdigit(c) ) break; |
|
588bb7c…
|
aku
|
2091 |
if( c=='.' ) seenDot = 1; |
|
588bb7c…
|
aku
|
2092 |
} |
|
588bb7c…
|
aku
|
2093 |
*pnLiteral = i; |
|
588bb7c…
|
aku
|
2094 |
return TH_OK; |
|
588bb7c…
|
aku
|
2095 |
} |
|
588bb7c…
|
aku
|
2096 |
|
|
588bb7c…
|
aku
|
2097 |
/* |
|
588bb7c…
|
aku
|
2098 |
** Free an expression tree. |
|
588bb7c…
|
aku
|
2099 |
*/ |
|
588bb7c…
|
aku
|
2100 |
static void exprFree(Th_Interp *interp, Expr *pExpr){ |
|
588bb7c…
|
aku
|
2101 |
if( pExpr ){ |
|
588bb7c…
|
aku
|
2102 |
exprFree(interp, pExpr->pLeft); |
|
588bb7c…
|
aku
|
2103 |
exprFree(interp, pExpr->pRight); |
|
588bb7c…
|
aku
|
2104 |
Th_Free(interp, pExpr->zValue); |
|
588bb7c…
|
aku
|
2105 |
Th_Free(interp, pExpr); |
|
588bb7c…
|
aku
|
2106 |
} |
|
588bb7c…
|
aku
|
2107 |
} |
|
588bb7c…
|
aku
|
2108 |
|
|
588bb7c…
|
aku
|
2109 |
/* |
|
588bb7c…
|
aku
|
2110 |
** Evaluate an expression tree. |
|
588bb7c…
|
aku
|
2111 |
*/ |
|
588bb7c…
|
aku
|
2112 |
static int exprEval(Th_Interp *interp, Expr *pExpr){ |
|
588bb7c…
|
aku
|
2113 |
int rc = TH_OK; |
|
588bb7c…
|
aku
|
2114 |
|
|
588bb7c…
|
aku
|
2115 |
if( pExpr->pOp==0 ){ |
|
588bb7c…
|
aku
|
2116 |
/* A literal */ |
|
588bb7c…
|
aku
|
2117 |
rc = thSubstWord(interp, pExpr->zValue, pExpr->nValue); |
|
588bb7c…
|
aku
|
2118 |
}else{ |
|
cd965de…
|
drh
|
2119 |
int eArgType = 0; /* Actual type of arguments */ |
|
588bb7c…
|
aku
|
2120 |
|
|
588bb7c…
|
aku
|
2121 |
/* Argument values */ |
|
496edc2…
|
drh
|
2122 |
int iLeft = 0; |
|
496edc2…
|
drh
|
2123 |
int iRight = 0; |
|
588bb7c…
|
aku
|
2124 |
double fLeft; |
|
588bb7c…
|
aku
|
2125 |
double fRight; |
|
588bb7c…
|
aku
|
2126 |
|
|
588bb7c…
|
aku
|
2127 |
/* Left and right arguments as strings */ |
|
dffe11c…
|
drh
|
2128 |
char *zLeft = 0; int nLeft = 0; |
|
dffe11c…
|
drh
|
2129 |
char *zRight = 0; int nRight = 0; |
|
588bb7c…
|
aku
|
2130 |
|
|
588bb7c…
|
aku
|
2131 |
/* Evaluate left and right arguments, if they exist. */ |
|
588bb7c…
|
aku
|
2132 |
if( pExpr->pLeft ){ |
|
588bb7c…
|
aku
|
2133 |
rc = exprEval(interp, pExpr->pLeft); |
|
588bb7c…
|
aku
|
2134 |
if( rc==TH_OK ){ |
|
588bb7c…
|
aku
|
2135 |
zLeft = Th_TakeResult(interp, &nLeft); |
|
45f3a45…
|
drh
|
2136 |
nLeft = TH1_LEN(nLeft); |
|
588bb7c…
|
aku
|
2137 |
} |
|
588bb7c…
|
aku
|
2138 |
} |
|
588bb7c…
|
aku
|
2139 |
if( rc==TH_OK && pExpr->pRight ){ |
|
588bb7c…
|
aku
|
2140 |
rc = exprEval(interp, pExpr->pRight); |
|
588bb7c…
|
aku
|
2141 |
if( rc==TH_OK ){ |
|
588bb7c…
|
aku
|
2142 |
zRight = Th_TakeResult(interp, &nRight); |
|
45f3a45…
|
drh
|
2143 |
nRight = TH1_LEN(nRight); |
|
588bb7c…
|
aku
|
2144 |
} |
|
588bb7c…
|
aku
|
2145 |
} |
|
588bb7c…
|
aku
|
2146 |
|
|
588bb7c…
|
aku
|
2147 |
/* Convert arguments to their required forms. */ |
|
588bb7c…
|
aku
|
2148 |
if( rc==TH_OK ){ |
|
588bb7c…
|
aku
|
2149 |
eArgType = pExpr->pOp->eArgType; |
|
588bb7c…
|
aku
|
2150 |
if( eArgType==ARG_NUMBER ){ |
|
588bb7c…
|
aku
|
2151 |
if( (zLeft==0 || TH_OK==Th_ToInt(0, zLeft, nLeft, &iLeft)) |
|
588bb7c…
|
aku
|
2152 |
&& (zRight==0 || TH_OK==Th_ToInt(0, zRight, nRight, &iRight)) |
|
588bb7c…
|
aku
|
2153 |
){ |
|
588bb7c…
|
aku
|
2154 |
eArgType = ARG_INTEGER; |
|
1ebe4b0…
|
mistachkin
|
2155 |
}else if( |
|
588bb7c…
|
aku
|
2156 |
(zLeft && TH_OK!=Th_ToDouble(interp, zLeft, nLeft, &fLeft)) || |
|
588bb7c…
|
aku
|
2157 |
(zRight && TH_OK!=Th_ToDouble(interp, zRight, nRight, &fRight)) |
|
588bb7c…
|
aku
|
2158 |
){ |
|
588bb7c…
|
aku
|
2159 |
/* A type error. */ |
|
588bb7c…
|
aku
|
2160 |
rc = TH_ERROR; |
|
588bb7c…
|
aku
|
2161 |
} |
|
588bb7c…
|
aku
|
2162 |
}else if( eArgType==ARG_INTEGER ){ |
|
588bb7c…
|
aku
|
2163 |
rc = Th_ToInt(interp, zLeft, nLeft, &iLeft); |
|
588bb7c…
|
aku
|
2164 |
if( rc==TH_OK && zRight ){ |
|
588bb7c…
|
aku
|
2165 |
rc = Th_ToInt(interp, zRight, nRight, &iRight); |
|
588bb7c…
|
aku
|
2166 |
} |
|
1ebe4b0…
|
mistachkin
|
2167 |
} |
|
588bb7c…
|
aku
|
2168 |
} |
|
588bb7c…
|
aku
|
2169 |
|
|
588bb7c…
|
aku
|
2170 |
if( rc==TH_OK && eArgType==ARG_INTEGER ){ |
|
cd965de…
|
drh
|
2171 |
int iRes = 0; |
|
588bb7c…
|
aku
|
2172 |
switch( pExpr->pOp->eOp ) { |
|
588bb7c…
|
aku
|
2173 |
case OP_MULTIPLY: iRes = iLeft*iRight; break; |
|
fded1d0…
|
stephan
|
2174 |
case OP_DIVIDE: |
|
479b3de…
|
mistachkin
|
2175 |
if( !iRight ){ |
|
fded1d0…
|
stephan
|
2176 |
Th_ErrorMessage(interp, "Divide by 0:", zLeft, nLeft); |
|
60acb31…
|
mistachkin
|
2177 |
rc = TH_ERROR; |
|
60acb31…
|
mistachkin
|
2178 |
goto finish; |
|
fded1d0…
|
stephan
|
2179 |
} |
|
fded1d0…
|
stephan
|
2180 |
iRes = iLeft/iRight; |
|
fded1d0…
|
stephan
|
2181 |
break; |
|
fded1d0…
|
stephan
|
2182 |
case OP_MODULUS: |
|
479b3de…
|
mistachkin
|
2183 |
if( !iRight ){ |
|
fded1d0…
|
stephan
|
2184 |
Th_ErrorMessage(interp, "Modulo by 0:", zLeft, nLeft); |
|
60acb31…
|
mistachkin
|
2185 |
rc = TH_ERROR; |
|
60acb31…
|
mistachkin
|
2186 |
goto finish; |
|
fded1d0…
|
stephan
|
2187 |
} |
|
fded1d0…
|
stephan
|
2188 |
iRes = iLeft%iRight; |
|
fded1d0…
|
stephan
|
2189 |
break; |
|
588bb7c…
|
aku
|
2190 |
case OP_ADD: iRes = iLeft+iRight; break; |
|
588bb7c…
|
aku
|
2191 |
case OP_SUBTRACT: iRes = iLeft-iRight; break; |
|
c66b196…
|
drh
|
2192 |
case OP_LEFTSHIFT: { |
|
c66b196…
|
drh
|
2193 |
iRes = (int)(((unsigned int)iLeft)<<(iRight&0x1f)); |
|
c66b196…
|
drh
|
2194 |
break; |
|
c66b196…
|
drh
|
2195 |
} |
|
c66b196…
|
drh
|
2196 |
case OP_RIGHTSHIFT: iRes = iLeft>>(iRight&0x1f); break; |
|
588bb7c…
|
aku
|
2197 |
case OP_LT: iRes = iLeft<iRight; break; |
|
588bb7c…
|
aku
|
2198 |
case OP_GT: iRes = iLeft>iRight; break; |
|
588bb7c…
|
aku
|
2199 |
case OP_LE: iRes = iLeft<=iRight; break; |
|
588bb7c…
|
aku
|
2200 |
case OP_GE: iRes = iLeft>=iRight; break; |
|
588bb7c…
|
aku
|
2201 |
case OP_EQ: iRes = iLeft==iRight; break; |
|
588bb7c…
|
aku
|
2202 |
case OP_NE: iRes = iLeft!=iRight; break; |
|
588bb7c…
|
aku
|
2203 |
case OP_BITWISE_AND: iRes = iLeft&iRight; break; |
|
588bb7c…
|
aku
|
2204 |
case OP_BITWISE_XOR: iRes = iLeft^iRight; break; |
|
588bb7c…
|
aku
|
2205 |
case OP_BITWISE_OR: iRes = iLeft|iRight; break; |
|
588bb7c…
|
aku
|
2206 |
case OP_LOGICAL_AND: iRes = iLeft&&iRight; break; |
|
588bb7c…
|
aku
|
2207 |
case OP_LOGICAL_OR: iRes = iLeft||iRight; break; |
|
588bb7c…
|
aku
|
2208 |
case OP_UNARY_MINUS: iRes = -iLeft; break; |
|
588bb7c…
|
aku
|
2209 |
case OP_UNARY_PLUS: iRes = +iLeft; break; |
|
ee1aa46…
|
mistachkin
|
2210 |
case OP_BITWISE_NOT: iRes = ~iLeft; break; |
|
588bb7c…
|
aku
|
2211 |
case OP_LOGICAL_NOT: iRes = !iLeft; break; |
|
588bb7c…
|
aku
|
2212 |
default: assert(!"Internal error"); |
|
588bb7c…
|
aku
|
2213 |
} |
|
588bb7c…
|
aku
|
2214 |
Th_SetResultInt(interp, iRes); |
|
588bb7c…
|
aku
|
2215 |
}else if( rc==TH_OK && eArgType==ARG_NUMBER ){ |
|
588bb7c…
|
aku
|
2216 |
switch( pExpr->pOp->eOp ) { |
|
e4e2b2e…
|
mistachkin
|
2217 |
case OP_MULTIPLY: Th_SetResultDouble(interp, fLeft*fRight); break; |
|
479b3de…
|
mistachkin
|
2218 |
case OP_DIVIDE: |
|
479b3de…
|
mistachkin
|
2219 |
if( fRight==0.0 ){ |
|
479b3de…
|
mistachkin
|
2220 |
Th_ErrorMessage(interp, "Divide by 0:", zLeft, nLeft); |
|
60acb31…
|
mistachkin
|
2221 |
rc = TH_ERROR; |
|
60acb31…
|
mistachkin
|
2222 |
goto finish; |
|
479b3de…
|
mistachkin
|
2223 |
} |
|
479b3de…
|
mistachkin
|
2224 |
Th_SetResultDouble(interp, fLeft/fRight); |
|
479b3de…
|
mistachkin
|
2225 |
break; |
|
e4e2b2e…
|
mistachkin
|
2226 |
case OP_ADD: Th_SetResultDouble(interp, fLeft+fRight); break; |
|
e4e2b2e…
|
mistachkin
|
2227 |
case OP_SUBTRACT: Th_SetResultDouble(interp, fLeft-fRight); break; |
|
e4e2b2e…
|
mistachkin
|
2228 |
case OP_LT: Th_SetResultInt(interp, fLeft<fRight); break; |
|
e4e2b2e…
|
mistachkin
|
2229 |
case OP_GT: Th_SetResultInt(interp, fLeft>fRight); break; |
|
e4e2b2e…
|
mistachkin
|
2230 |
case OP_LE: Th_SetResultInt(interp, fLeft<=fRight); break; |
|
e4e2b2e…
|
mistachkin
|
2231 |
case OP_GE: Th_SetResultInt(interp, fLeft>=fRight); break; |
|
e4e2b2e…
|
mistachkin
|
2232 |
case OP_EQ: Th_SetResultInt(interp, fLeft==fRight); break; |
|
e4e2b2e…
|
mistachkin
|
2233 |
case OP_NE: Th_SetResultInt(interp, fLeft!=fRight); break; |
|
e4e2b2e…
|
mistachkin
|
2234 |
case OP_UNARY_MINUS: Th_SetResultDouble(interp, -fLeft); break; |
|
e4e2b2e…
|
mistachkin
|
2235 |
case OP_UNARY_PLUS: Th_SetResultDouble(interp, +fLeft); break; |
|
588bb7c…
|
aku
|
2236 |
default: assert(!"Internal error"); |
|
588bb7c…
|
aku
|
2237 |
} |
|
588bb7c…
|
aku
|
2238 |
}else if( rc==TH_OK ){ |
|
588bb7c…
|
aku
|
2239 |
int iEqual = 0; |
|
588bb7c…
|
aku
|
2240 |
assert( eArgType==ARG_STRING ); |
|
588bb7c…
|
aku
|
2241 |
if( nRight==nLeft && 0==memcmp(zRight, zLeft, nRight) ){ |
|
588bb7c…
|
aku
|
2242 |
iEqual = 1; |
|
588bb7c…
|
aku
|
2243 |
} |
|
588bb7c…
|
aku
|
2244 |
switch( pExpr->pOp->eOp ) { |
|
588bb7c…
|
aku
|
2245 |
case OP_SEQ: Th_SetResultInt(interp, iEqual); break; |
|
588bb7c…
|
aku
|
2246 |
case OP_SNE: Th_SetResultInt(interp, !iEqual); break; |
|
588bb7c…
|
aku
|
2247 |
default: assert(!"Internal error"); |
|
588bb7c…
|
aku
|
2248 |
} |
|
588bb7c…
|
aku
|
2249 |
} |
|
60acb31…
|
mistachkin
|
2250 |
|
|
60acb31…
|
mistachkin
|
2251 |
finish: |
|
588bb7c…
|
aku
|
2252 |
|
|
588bb7c…
|
aku
|
2253 |
Th_Free(interp, zLeft); |
|
588bb7c…
|
aku
|
2254 |
Th_Free(interp, zRight); |
|
588bb7c…
|
aku
|
2255 |
} |
|
588bb7c…
|
aku
|
2256 |
|
|
588bb7c…
|
aku
|
2257 |
return rc; |
|
588bb7c…
|
aku
|
2258 |
} |
|
588bb7c…
|
aku
|
2259 |
|
|
588bb7c…
|
aku
|
2260 |
/* |
|
588bb7c…
|
aku
|
2261 |
** Create an expression tree from an array of tokens. If successful, |
|
588bb7c…
|
aku
|
2262 |
** the root of the tree is stored in apToken[0]. |
|
588bb7c…
|
aku
|
2263 |
*/ |
|
588bb7c…
|
aku
|
2264 |
int exprMakeTree(Th_Interp *interp, Expr **apToken, int nToken){ |
|
588bb7c…
|
aku
|
2265 |
int iLeft; |
|
588bb7c…
|
aku
|
2266 |
int i; |
|
588bb7c…
|
aku
|
2267 |
int jj; |
|
588bb7c…
|
aku
|
2268 |
|
|
588bb7c…
|
aku
|
2269 |
assert(nToken>0); |
|
588bb7c…
|
aku
|
2270 |
#define ISTERM(x) (apToken[x] && (!apToken[x]->pOp || apToken[x]->pLeft)) |
|
588bb7c…
|
aku
|
2271 |
|
|
588bb7c…
|
aku
|
2272 |
for(jj=0; jj<nToken; jj++){ |
|
588bb7c…
|
aku
|
2273 |
if( apToken[jj]->pOp && apToken[jj]->pOp->eOp==OP_OPEN_BRACKET ){ |
|
588bb7c…
|
aku
|
2274 |
int nNest = 1; |
|
1ebe4b0…
|
mistachkin
|
2275 |
int iLeft = jj; |
|
588bb7c…
|
aku
|
2276 |
|
|
588bb7c…
|
aku
|
2277 |
for(jj++; jj<nToken; jj++){ |
|
588bb7c…
|
aku
|
2278 |
Operator *pOp = apToken[jj]->pOp; |
|
588bb7c…
|
aku
|
2279 |
if( pOp && pOp->eOp==OP_OPEN_BRACKET ) nNest++; |
|
588bb7c…
|
aku
|
2280 |
if( pOp && pOp->eOp==OP_CLOSE_BRACKET ) nNest--; |
|
588bb7c…
|
aku
|
2281 |
if( nNest==0 ) break; |
|
588bb7c…
|
aku
|
2282 |
} |
|
588bb7c…
|
aku
|
2283 |
if( jj==nToken ){ |
|
588bb7c…
|
aku
|
2284 |
return TH_ERROR; |
|
588bb7c…
|
aku
|
2285 |
} |
|
588bb7c…
|
aku
|
2286 |
if( (jj-iLeft)>1 ){ |
|
588bb7c…
|
aku
|
2287 |
if( exprMakeTree(interp, &apToken[iLeft+1], jj-iLeft-1) ){ |
|
588bb7c…
|
aku
|
2288 |
return TH_ERROR; |
|
588bb7c…
|
aku
|
2289 |
} |
|
588bb7c…
|
aku
|
2290 |
exprFree(interp, apToken[jj]); |
|
588bb7c…
|
aku
|
2291 |
exprFree(interp, apToken[iLeft]); |
|
588bb7c…
|
aku
|
2292 |
apToken[jj] = 0; |
|
588bb7c…
|
aku
|
2293 |
apToken[iLeft] = 0; |
|
588bb7c…
|
aku
|
2294 |
} |
|
588bb7c…
|
aku
|
2295 |
} |
|
588bb7c…
|
aku
|
2296 |
} |
|
588bb7c…
|
aku
|
2297 |
|
|
588bb7c…
|
aku
|
2298 |
iLeft = 0; |
|
588bb7c…
|
aku
|
2299 |
for(jj=nToken-1; jj>=0; jj--){ |
|
588bb7c…
|
aku
|
2300 |
if( apToken[jj] ){ |
|
66ae70a…
|
drh
|
2301 |
if( apToken[jj]->pOp && apToken[jj]->pOp->iPrecedence==1 |
|
66ae70a…
|
drh
|
2302 |
&& iLeft>0 && ISTERM(iLeft) ){ |
|
588bb7c…
|
aku
|
2303 |
apToken[jj]->pLeft = apToken[iLeft]; |
|
588bb7c…
|
aku
|
2304 |
apToken[jj]->pLeft->pParent = apToken[jj]; |
|
588bb7c…
|
aku
|
2305 |
apToken[iLeft] = 0; |
|
588bb7c…
|
aku
|
2306 |
} |
|
588bb7c…
|
aku
|
2307 |
iLeft = jj; |
|
588bb7c…
|
aku
|
2308 |
} |
|
588bb7c…
|
aku
|
2309 |
} |
|
588bb7c…
|
aku
|
2310 |
for(i=2; i<=12; i++){ |
|
588bb7c…
|
aku
|
2311 |
iLeft = -1; |
|
588bb7c…
|
aku
|
2312 |
for(jj=0; jj<nToken; jj++){ |
|
588bb7c…
|
aku
|
2313 |
Expr *pToken = apToken[jj]; |
|
588bb7c…
|
aku
|
2314 |
if( apToken[jj] ){ |
|
588bb7c…
|
aku
|
2315 |
if( pToken->pOp && !pToken->pLeft && pToken->pOp->iPrecedence==i ){ |
|
588bb7c…
|
aku
|
2316 |
int iRight = jj+1; |
|
66ae70a…
|
drh
|
2317 |
for(; !apToken[iRight] && iRight<nToken; iRight++); |
|
588bb7c…
|
aku
|
2318 |
if( iRight==nToken || iLeft<0 || !ISTERM(iRight) || !ISTERM(iLeft) ){ |
|
588bb7c…
|
aku
|
2319 |
return TH_ERROR; |
|
588bb7c…
|
aku
|
2320 |
} |
|
588bb7c…
|
aku
|
2321 |
pToken->pLeft = apToken[iLeft]; |
|
588bb7c…
|
aku
|
2322 |
apToken[iLeft] = 0; |
|
588bb7c…
|
aku
|
2323 |
pToken->pLeft->pParent = pToken; |
|
588bb7c…
|
aku
|
2324 |
pToken->pRight = apToken[iRight]; |
|
588bb7c…
|
aku
|
2325 |
apToken[iRight] = 0; |
|
588bb7c…
|
aku
|
2326 |
pToken->pRight->pParent = pToken; |
|
588bb7c…
|
aku
|
2327 |
} |
|
588bb7c…
|
aku
|
2328 |
iLeft = jj; |
|
588bb7c…
|
aku
|
2329 |
} |
|
588bb7c…
|
aku
|
2330 |
} |
|
588bb7c…
|
aku
|
2331 |
} |
|
588bb7c…
|
aku
|
2332 |
for(jj=1; jj<nToken; jj++){ |
|
588bb7c…
|
aku
|
2333 |
assert( !apToken[jj] || !apToken[0] ); |
|
588bb7c…
|
aku
|
2334 |
if( apToken[jj] ){ |
|
588bb7c…
|
aku
|
2335 |
apToken[0] = apToken[jj]; |
|
588bb7c…
|
aku
|
2336 |
apToken[jj] = 0; |
|
588bb7c…
|
aku
|
2337 |
} |
|
588bb7c…
|
aku
|
2338 |
} |
|
588bb7c…
|
aku
|
2339 |
|
|
588bb7c…
|
aku
|
2340 |
return TH_OK; |
|
588bb7c…
|
aku
|
2341 |
} |
|
588bb7c…
|
aku
|
2342 |
|
|
588bb7c…
|
aku
|
2343 |
/* |
|
588bb7c…
|
aku
|
2344 |
** Parse a string containing a TH expression to a list of tokens. |
|
588bb7c…
|
aku
|
2345 |
*/ |
|
588bb7c…
|
aku
|
2346 |
static int exprParse( |
|
588bb7c…
|
aku
|
2347 |
Th_Interp *interp, /* Interpreter to leave error message in */ |
|
1ebe4b0…
|
mistachkin
|
2348 |
const char *zExpr, /* Pointer to input string */ |
|
588bb7c…
|
aku
|
2349 |
int nExpr, /* Number of bytes at zExpr */ |
|
588bb7c…
|
aku
|
2350 |
Expr ***papToken, /* OUT: Array of tokens. */ |
|
588bb7c…
|
aku
|
2351 |
int *pnToken /* OUT: Size of token array */ |
|
588bb7c…
|
aku
|
2352 |
){ |
|
588bb7c…
|
aku
|
2353 |
int i; |
|
588bb7c…
|
aku
|
2354 |
|
|
588bb7c…
|
aku
|
2355 |
int rc = TH_OK; |
|
66ae70a…
|
drh
|
2356 |
int nNest = 0; |
|
588bb7c…
|
aku
|
2357 |
int nToken = 0; |
|
588bb7c…
|
aku
|
2358 |
Expr **apToken = 0; |
|
588bb7c…
|
aku
|
2359 |
|
|
588bb7c…
|
aku
|
2360 |
for(i=0; rc==TH_OK && i<nExpr; ){ |
|
0c99a15…
|
drh
|
2361 |
char c = zExpr[i]; |
|
588bb7c…
|
aku
|
2362 |
if( th_isspace(c) ){ /* White-space */ |
|
588bb7c…
|
aku
|
2363 |
i++; |
|
588bb7c…
|
aku
|
2364 |
}else{ |
|
588bb7c…
|
aku
|
2365 |
Expr *pNew = (Expr *)Th_Malloc(interp, sizeof(Expr)); |
|
0c99a15…
|
drh
|
2366 |
const char *z = &zExpr[i]; |
|
588bb7c…
|
aku
|
2367 |
|
|
588bb7c…
|
aku
|
2368 |
switch (c) { |
|
99901fb…
|
jan.nijtmans
|
2369 |
case '0': |
|
8d62798…
|
jan.nijtmans
|
2370 |
if( thNextInteger(interp, z, nExpr-i, &pNew->nValue)==TH_OK ){ |
|
8d62798…
|
jan.nijtmans
|
2371 |
break; |
|
99901fb…
|
jan.nijtmans
|
2372 |
} |
|
99901fb…
|
jan.nijtmans
|
2373 |
/* fall through */ |
|
99901fb…
|
jan.nijtmans
|
2374 |
case '1': case '2': case '3': case '4': case '5': |
|
99901fb…
|
jan.nijtmans
|
2375 |
case '6': case '7': case '8': case '9': |
|
588bb7c…
|
aku
|
2376 |
thNextNumber(interp, z, nExpr-i, &pNew->nValue); |
|
588bb7c…
|
aku
|
2377 |
break; |
|
588bb7c…
|
aku
|
2378 |
|
|
588bb7c…
|
aku
|
2379 |
case '$': |
|
588bb7c…
|
aku
|
2380 |
thNextVarname(interp, z, nExpr-i, &pNew->nValue); |
|
588bb7c…
|
aku
|
2381 |
break; |
|
588bb7c…
|
aku
|
2382 |
|
|
588bb7c…
|
aku
|
2383 |
case '{': case '[': { |
|
588bb7c…
|
aku
|
2384 |
thNextCommand(interp, z, nExpr-i, &pNew->nValue); |
|
588bb7c…
|
aku
|
2385 |
break; |
|
588bb7c…
|
aku
|
2386 |
} |
|
588bb7c…
|
aku
|
2387 |
|
|
588bb7c…
|
aku
|
2388 |
case '"': { |
|
588bb7c…
|
aku
|
2389 |
int iEnd = i; |
|
588bb7c…
|
aku
|
2390 |
while( ++iEnd<nExpr && zExpr[iEnd]!='"' ){ |
|
588bb7c…
|
aku
|
2391 |
if( zExpr[iEnd]=='\\' ) iEnd++; |
|
588bb7c…
|
aku
|
2392 |
} |
|
588bb7c…
|
aku
|
2393 |
if( iEnd<nExpr ){ |
|
588bb7c…
|
aku
|
2394 |
pNew->nValue = iEnd+1-i; |
|
588bb7c…
|
aku
|
2395 |
} |
|
588bb7c…
|
aku
|
2396 |
break; |
|
588bb7c…
|
aku
|
2397 |
} |
|
588bb7c…
|
aku
|
2398 |
|
|
588bb7c…
|
aku
|
2399 |
default: { |
|
588bb7c…
|
aku
|
2400 |
int j; |
|
66ae70a…
|
drh
|
2401 |
const char *zOp; |
|
66ae70a…
|
drh
|
2402 |
for(j=0; (zOp=aOperator[j].zOp); j++){ |
|
66ae70a…
|
drh
|
2403 |
int nOp = aOperator[j].nOp; |
|
fc248d6…
|
mistachkin
|
2404 |
int nRemain = nExpr - i; |
|
66ae70a…
|
drh
|
2405 |
int isMatch = 0; |
|
fc248d6…
|
mistachkin
|
2406 |
if( nRemain>=nOp && 0==memcmp(zOp, &zExpr[i], nOp) ){ |
|
66ae70a…
|
drh
|
2407 |
isMatch = 1; |
|
66ae70a…
|
drh
|
2408 |
} |
|
fc248d6…
|
mistachkin
|
2409 |
if( isMatch ){ |
|
fc248d6…
|
mistachkin
|
2410 |
if( aOperator[j].eOp==OP_CLOSE_BRACKET ){ |
|
fc248d6…
|
mistachkin
|
2411 |
nNest--; |
|
fc248d6…
|
mistachkin
|
2412 |
}else if( nRemain>nOp ){ |
|
fc248d6…
|
mistachkin
|
2413 |
if( aOperator[j].eOp==OP_OPEN_BRACKET ){ |
|
fc248d6…
|
mistachkin
|
2414 |
nNest++; |
|
fc248d6…
|
mistachkin
|
2415 |
} |
|
fc248d6…
|
mistachkin
|
2416 |
}else{ |
|
fc248d6…
|
mistachkin
|
2417 |
/* |
|
fc248d6…
|
mistachkin
|
2418 |
** This is not really a match because this operator cannot |
|
fc248d6…
|
mistachkin
|
2419 |
** legally appear at the end of the string. |
|
fc248d6…
|
mistachkin
|
2420 |
*/ |
|
fc248d6…
|
mistachkin
|
2421 |
isMatch = 0; |
|
fc248d6…
|
mistachkin
|
2422 |
} |
|
66ae70a…
|
drh
|
2423 |
} |
|
66ae70a…
|
drh
|
2424 |
if( nToken>0 && aOperator[j].iPrecedence==1 ){ |
|
588bb7c…
|
aku
|
2425 |
Expr *pPrev = apToken[nToken-1]; |
|
588bb7c…
|
aku
|
2426 |
if( !pPrev->pOp || pPrev->pOp->eOp==OP_CLOSE_BRACKET ){ |
|
588bb7c…
|
aku
|
2427 |
continue; |
|
588bb7c…
|
aku
|
2428 |
} |
|
588bb7c…
|
aku
|
2429 |
} |
|
66ae70a…
|
drh
|
2430 |
if( isMatch ){ |
|
588bb7c…
|
aku
|
2431 |
pNew->pOp = &aOperator[j]; |
|
588bb7c…
|
aku
|
2432 |
i += nOp; |
|
588bb7c…
|
aku
|
2433 |
break; |
|
588bb7c…
|
aku
|
2434 |
} |
|
588bb7c…
|
aku
|
2435 |
} |
|
588bb7c…
|
aku
|
2436 |
} |
|
588bb7c…
|
aku
|
2437 |
} |
|
588bb7c…
|
aku
|
2438 |
|
|
588bb7c…
|
aku
|
2439 |
if( pNew->pOp || pNew->nValue ){ |
|
588bb7c…
|
aku
|
2440 |
if( pNew->nValue ){ |
|
588bb7c…
|
aku
|
2441 |
/* A terminal. Copy the string value. */ |
|
588bb7c…
|
aku
|
2442 |
assert( !pNew->pOp ); |
|
588bb7c…
|
aku
|
2443 |
pNew->zValue = Th_Malloc(interp, pNew->nValue); |
|
7ae4b1a…
|
drh
|
2444 |
th_memcpy(pNew->zValue, z, pNew->nValue); |
|
588bb7c…
|
aku
|
2445 |
i += pNew->nValue; |
|
588bb7c…
|
aku
|
2446 |
} |
|
588bb7c…
|
aku
|
2447 |
if( (nToken%16)==0 ){ |
|
588bb7c…
|
aku
|
2448 |
/* Grow the apToken array. */ |
|
1ebe4b0…
|
mistachkin
|
2449 |
Expr **apTokenOld = apToken; |
|
588bb7c…
|
aku
|
2450 |
apToken = Th_Malloc(interp, sizeof(Expr *)*(nToken+16)); |
|
7ae4b1a…
|
drh
|
2451 |
th_memcpy(apToken, apTokenOld, sizeof(Expr *)*nToken); |
|
588bb7c…
|
aku
|
2452 |
} |
|
588bb7c…
|
aku
|
2453 |
|
|
588bb7c…
|
aku
|
2454 |
/* Put the new token at the end of the apToken array */ |
|
588bb7c…
|
aku
|
2455 |
apToken[nToken] = pNew; |
|
588bb7c…
|
aku
|
2456 |
nToken++; |
|
588bb7c…
|
aku
|
2457 |
}else{ |
|
588bb7c…
|
aku
|
2458 |
Th_Free(interp, pNew); |
|
588bb7c…
|
aku
|
2459 |
rc = TH_ERROR; |
|
588bb7c…
|
aku
|
2460 |
} |
|
588bb7c…
|
aku
|
2461 |
} |
|
66ae70a…
|
drh
|
2462 |
} |
|
66ae70a…
|
drh
|
2463 |
|
|
66ae70a…
|
drh
|
2464 |
if( nNest!=0 ){ |
|
66ae70a…
|
drh
|
2465 |
rc = TH_ERROR; |
|
588bb7c…
|
aku
|
2466 |
} |
|
588bb7c…
|
aku
|
2467 |
|
|
588bb7c…
|
aku
|
2468 |
*papToken = apToken; |
|
588bb7c…
|
aku
|
2469 |
*pnToken = nToken; |
|
588bb7c…
|
aku
|
2470 |
return rc; |
|
588bb7c…
|
aku
|
2471 |
} |
|
588bb7c…
|
aku
|
2472 |
|
|
588bb7c…
|
aku
|
2473 |
/* |
|
588bb7c…
|
aku
|
2474 |
** Evaluate the string (zExpr, nExpr) as a Th expression. Store |
|
588bb7c…
|
aku
|
2475 |
** the result in the interpreter interp and return TH_OK if |
|
588bb7c…
|
aku
|
2476 |
** successful. If an error occurs, store an error message in |
|
588bb7c…
|
aku
|
2477 |
** the interpreter result and return an error code. |
|
1ebe4b0…
|
mistachkin
|
2478 |
*/ |
|
0c99a15…
|
drh
|
2479 |
int Th_Expr(Th_Interp *interp, const char *zExpr, int nExpr){ |
|
588bb7c…
|
aku
|
2480 |
int rc; /* Return Code */ |
|
588bb7c…
|
aku
|
2481 |
int i; /* Loop counter */ |
|
588bb7c…
|
aku
|
2482 |
|
|
588bb7c…
|
aku
|
2483 |
int nToken = 0; |
|
588bb7c…
|
aku
|
2484 |
Expr **apToken = 0; |
|
588bb7c…
|
aku
|
2485 |
|
|
588bb7c…
|
aku
|
2486 |
if( nExpr<0 ){ |
|
588bb7c…
|
aku
|
2487 |
nExpr = th_strlen(zExpr); |
|
2116238…
|
drh
|
2488 |
}else{ |
|
2116238…
|
drh
|
2489 |
nExpr = TH1_LEN(nExpr); |
|
588bb7c…
|
aku
|
2490 |
} |
|
588bb7c…
|
aku
|
2491 |
|
|
588bb7c…
|
aku
|
2492 |
/* Parse the expression to a list of tokens. */ |
|
588bb7c…
|
aku
|
2493 |
rc = exprParse(interp, zExpr, nExpr, &apToken, &nToken); |
|
588bb7c…
|
aku
|
2494 |
|
|
588bb7c…
|
aku
|
2495 |
/* If the parsing was successful, create an expression tree from |
|
588bb7c…
|
aku
|
2496 |
** the parsed list of tokens. If successful, apToken[0] is set |
|
1ebe4b0…
|
mistachkin
|
2497 |
** to point to the root of the expression tree. |
|
588bb7c…
|
aku
|
2498 |
*/ |
|
588bb7c…
|
aku
|
2499 |
if( rc==TH_OK ){ |
|
588bb7c…
|
aku
|
2500 |
rc = exprMakeTree(interp, apToken, nToken); |
|
588bb7c…
|
aku
|
2501 |
} |
|
588bb7c…
|
aku
|
2502 |
|
|
588bb7c…
|
aku
|
2503 |
if( rc!=TH_OK ){ |
|
588bb7c…
|
aku
|
2504 |
Th_ErrorMessage(interp, "syntax error in expression: \"", zExpr, nExpr); |
|
588bb7c…
|
aku
|
2505 |
} |
|
588bb7c…
|
aku
|
2506 |
|
|
588bb7c…
|
aku
|
2507 |
/* Evaluate the expression tree. */ |
|
588bb7c…
|
aku
|
2508 |
if( rc==TH_OK ){ |
|
588bb7c…
|
aku
|
2509 |
rc = exprEval(interp, apToken[0]); |
|
588bb7c…
|
aku
|
2510 |
} |
|
588bb7c…
|
aku
|
2511 |
|
|
588bb7c…
|
aku
|
2512 |
/* Free memory allocated by exprParse(). */ |
|
588bb7c…
|
aku
|
2513 |
for(i=0; i<nToken; i++){ |
|
588bb7c…
|
aku
|
2514 |
exprFree(interp, apToken[i]); |
|
588bb7c…
|
aku
|
2515 |
} |
|
588bb7c…
|
aku
|
2516 |
Th_Free(interp, apToken); |
|
588bb7c…
|
aku
|
2517 |
|
|
588bb7c…
|
aku
|
2518 |
return rc; |
|
588bb7c…
|
aku
|
2519 |
} |
|
588bb7c…
|
aku
|
2520 |
|
|
588bb7c…
|
aku
|
2521 |
/* |
|
588bb7c…
|
aku
|
2522 |
** Allocate and return a pointer to a new hash-table. The caller should |
|
588bb7c…
|
aku
|
2523 |
** (eventually) delete the hash-table by passing it to Th_HashDelete(). |
|
588bb7c…
|
aku
|
2524 |
*/ |
|
588bb7c…
|
aku
|
2525 |
Th_Hash *Th_HashNew(Th_Interp *interp){ |
|
588bb7c…
|
aku
|
2526 |
Th_Hash *p; |
|
588bb7c…
|
aku
|
2527 |
p = Th_Malloc(interp, sizeof(Th_Hash)); |
|
588bb7c…
|
aku
|
2528 |
return p; |
|
588bb7c…
|
aku
|
2529 |
} |
|
588bb7c…
|
aku
|
2530 |
|
|
588bb7c…
|
aku
|
2531 |
/* |
|
588bb7c…
|
aku
|
2532 |
** Iterate through all values currently stored in the hash table. Invoke |
|
588bb7c…
|
aku
|
2533 |
** the callback function xCallback for each entry. The second argument |
|
588bb7c…
|
aku
|
2534 |
** passed to xCallback is a copy of the fourth argument passed to this |
|
1aeb272…
|
drh
|
2535 |
** function. The return value from the callback function xCallback is |
|
1aeb272…
|
drh
|
2536 |
** ignored. |
|
588bb7c…
|
aku
|
2537 |
*/ |
|
588bb7c…
|
aku
|
2538 |
void Th_HashIterate( |
|
1ebe4b0…
|
mistachkin
|
2539 |
Th_Interp *interp, |
|
588bb7c…
|
aku
|
2540 |
Th_Hash *pHash, |
|
1aeb272…
|
drh
|
2541 |
int (*xCallback)(Th_HashEntry *pEntry, void *pContext), |
|
588bb7c…
|
aku
|
2542 |
void *pContext |
|
588bb7c…
|
aku
|
2543 |
){ |
|
588bb7c…
|
aku
|
2544 |
int i; |
|
588bb7c…
|
aku
|
2545 |
for(i=0; i<TH_HASHSIZE; i++){ |
|
588bb7c…
|
aku
|
2546 |
Th_HashEntry *pEntry; |
|
588bb7c…
|
aku
|
2547 |
Th_HashEntry *pNext; |
|
588bb7c…
|
aku
|
2548 |
for(pEntry=pHash->a[i]; pEntry; pEntry=pNext){ |
|
588bb7c…
|
aku
|
2549 |
pNext = pEntry->pNext; |
|
588bb7c…
|
aku
|
2550 |
xCallback(pEntry, pContext); |
|
588bb7c…
|
aku
|
2551 |
} |
|
588bb7c…
|
aku
|
2552 |
} |
|
588bb7c…
|
aku
|
2553 |
} |
|
588bb7c…
|
aku
|
2554 |
|
|
588bb7c…
|
aku
|
2555 |
/* |
|
1aeb272…
|
drh
|
2556 |
** Helper function for Th_HashDelete(). Always returns non-zero. |
|
588bb7c…
|
aku
|
2557 |
*/ |
|
1aeb272…
|
drh
|
2558 |
static int xFreeHashEntry(Th_HashEntry *pEntry, void *pContext){ |
|
588bb7c…
|
aku
|
2559 |
Th_Free((Th_Interp *)pContext, (void *)pEntry); |
|
1aeb272…
|
drh
|
2560 |
return 1; |
|
588bb7c…
|
aku
|
2561 |
} |
|
588bb7c…
|
aku
|
2562 |
|
|
588bb7c…
|
aku
|
2563 |
/* |
|
588bb7c…
|
aku
|
2564 |
** Free a hash-table previously allocated by Th_HashNew(). |
|
588bb7c…
|
aku
|
2565 |
*/ |
|
588bb7c…
|
aku
|
2566 |
void Th_HashDelete(Th_Interp *interp, Th_Hash *pHash){ |
|
588bb7c…
|
aku
|
2567 |
if( pHash ){ |
|
588bb7c…
|
aku
|
2568 |
Th_HashIterate(interp, pHash, xFreeHashEntry, (void *)interp); |
|
588bb7c…
|
aku
|
2569 |
Th_Free(interp, pHash); |
|
588bb7c…
|
aku
|
2570 |
} |
|
588bb7c…
|
aku
|
2571 |
} |
|
588bb7c…
|
aku
|
2572 |
|
|
588bb7c…
|
aku
|
2573 |
/* |
|
1ebe4b0…
|
mistachkin
|
2574 |
** This function is used to insert or delete hash table items, or to |
|
588bb7c…
|
aku
|
2575 |
** query a hash table for an existing item. |
|
588bb7c…
|
aku
|
2576 |
** |
|
1ebe4b0…
|
mistachkin
|
2577 |
** If parameter op is less than zero, then the hash-table element |
|
588bb7c…
|
aku
|
2578 |
** identified by (zKey, nKey) is removed from the hash-table if it |
|
588bb7c…
|
aku
|
2579 |
** exists. NULL is returned. |
|
588bb7c…
|
aku
|
2580 |
** |
|
588bb7c…
|
aku
|
2581 |
** Otherwise, if the hash-table contains an item with key (zKey, nKey), |
|
588bb7c…
|
aku
|
2582 |
** a pointer to the associated Th_HashEntry is returned. If parameter |
|
588bb7c…
|
aku
|
2583 |
** op is greater than zero, then a new entry is added if one cannot |
|
588bb7c…
|
aku
|
2584 |
** be found. If op is zero, then NULL is returned if the item is |
|
588bb7c…
|
aku
|
2585 |
** not already present in the hash-table. |
|
588bb7c…
|
aku
|
2586 |
*/ |
|
588bb7c…
|
aku
|
2587 |
Th_HashEntry *Th_HashFind( |
|
1ebe4b0…
|
mistachkin
|
2588 |
Th_Interp *interp, |
|
588bb7c…
|
aku
|
2589 |
Th_Hash *pHash, |
|
0c99a15…
|
drh
|
2590 |
const char *zKey, |
|
588bb7c…
|
aku
|
2591 |
int nKey, |
|
588bb7c…
|
aku
|
2592 |
int op /* -ve = delete, 0 = find, +ve = insert */ |
|
588bb7c…
|
aku
|
2593 |
){ |
|
588bb7c…
|
aku
|
2594 |
unsigned int iKey = 0; |
|
588bb7c…
|
aku
|
2595 |
int i; |
|
588bb7c…
|
aku
|
2596 |
Th_HashEntry *pRet; |
|
588bb7c…
|
aku
|
2597 |
Th_HashEntry **ppRet; |
|
588bb7c…
|
aku
|
2598 |
|
|
588bb7c…
|
aku
|
2599 |
if( nKey<0 ){ |
|
588bb7c…
|
aku
|
2600 |
nKey = th_strlen(zKey); |
|
2116238…
|
drh
|
2601 |
}else{ |
|
2116238…
|
drh
|
2602 |
nKey = TH1_LEN(nKey); |
|
588bb7c…
|
aku
|
2603 |
} |
|
588bb7c…
|
aku
|
2604 |
|
|
588bb7c…
|
aku
|
2605 |
for(i=0; i<nKey; i++){ |
|
588bb7c…
|
aku
|
2606 |
iKey = (iKey<<3) ^ iKey ^ zKey[i]; |
|
588bb7c…
|
aku
|
2607 |
} |
|
588bb7c…
|
aku
|
2608 |
iKey = iKey % TH_HASHSIZE; |
|
588bb7c…
|
aku
|
2609 |
|
|
588bb7c…
|
aku
|
2610 |
for(ppRet=&pHash->a[iKey]; (pRet=*ppRet); ppRet=&pRet->pNext){ |
|
588bb7c…
|
aku
|
2611 |
assert( pRet && ppRet && *ppRet==pRet ); |
|
588bb7c…
|
aku
|
2612 |
if( pRet->nKey==nKey && 0==memcmp(pRet->zKey, zKey, nKey) ) break; |
|
588bb7c…
|
aku
|
2613 |
} |
|
588bb7c…
|
aku
|
2614 |
|
|
588bb7c…
|
aku
|
2615 |
if( op<0 && pRet ){ |
|
588bb7c…
|
aku
|
2616 |
assert( ppRet && *ppRet==pRet ); |
|
588bb7c…
|
aku
|
2617 |
*ppRet = pRet->pNext; |
|
588bb7c…
|
aku
|
2618 |
Th_Free(interp, pRet); |
|
588bb7c…
|
aku
|
2619 |
pRet = 0; |
|
588bb7c…
|
aku
|
2620 |
} |
|
588bb7c…
|
aku
|
2621 |
|
|
588bb7c…
|
aku
|
2622 |
if( op>0 && !pRet ){ |
|
588bb7c…
|
aku
|
2623 |
pRet = (Th_HashEntry *)Th_Malloc(interp, sizeof(Th_HashEntry) + nKey); |
|
0c99a15…
|
drh
|
2624 |
pRet->zKey = (char *)&pRet[1]; |
|
588bb7c…
|
aku
|
2625 |
pRet->nKey = nKey; |
|
7ae4b1a…
|
drh
|
2626 |
th_memcpy(pRet->zKey, zKey, nKey); |
|
588bb7c…
|
aku
|
2627 |
pRet->pNext = pHash->a[iKey]; |
|
588bb7c…
|
aku
|
2628 |
pHash->a[iKey] = pRet; |
|
588bb7c…
|
aku
|
2629 |
} |
|
588bb7c…
|
aku
|
2630 |
|
|
588bb7c…
|
aku
|
2631 |
return pRet; |
|
588bb7c…
|
aku
|
2632 |
} |
|
588bb7c…
|
aku
|
2633 |
|
|
588bb7c…
|
aku
|
2634 |
/* |
|
588bb7c…
|
aku
|
2635 |
** This function is the same as the standard strlen() function, except |
|
588bb7c…
|
aku
|
2636 |
** that it returns 0 (instead of being undefined) if the argument is |
|
588bb7c…
|
aku
|
2637 |
** a null pointer. |
|
588bb7c…
|
aku
|
2638 |
*/ |
|
0c99a15…
|
drh
|
2639 |
int th_strlen(const char *zStr){ |
|
588bb7c…
|
aku
|
2640 |
int n = 0; |
|
588bb7c…
|
aku
|
2641 |
if( zStr ){ |
|
588bb7c…
|
aku
|
2642 |
while( zStr[n] ) n++; |
|
588bb7c…
|
aku
|
2643 |
} |
|
588bb7c…
|
aku
|
2644 |
return n; |
|
588bb7c…
|
aku
|
2645 |
} |
|
588bb7c…
|
aku
|
2646 |
|
|
588bb7c…
|
aku
|
2647 |
/* Whitespace characters: |
|
588bb7c…
|
aku
|
2648 |
** |
|
588bb7c…
|
aku
|
2649 |
** ' ' 0x20 |
|
588bb7c…
|
aku
|
2650 |
** '\t' 0x09 |
|
588bb7c…
|
aku
|
2651 |
** '\n' 0x0A |
|
588bb7c…
|
aku
|
2652 |
** '\v' 0x0B |
|
588bb7c…
|
aku
|
2653 |
** '\f' 0x0C |
|
588bb7c…
|
aku
|
2654 |
** '\r' 0x0D |
|
588bb7c…
|
aku
|
2655 |
** |
|
588bb7c…
|
aku
|
2656 |
** Whitespace characters have the 0x01 flag set. Decimal digits have the |
|
588bb7c…
|
aku
|
2657 |
** 0x2 flag set. Single byte printable characters have the 0x4 flag set. |
|
1f6734c…
|
jan.nijtmans
|
2658 |
** Alphabet characters have the 0x8 bit set. Hexadecimal digits have the |
|
1f6734c…
|
jan.nijtmans
|
2659 |
** 0x20 flag set. |
|
588bb7c…
|
aku
|
2660 |
** |
|
588bb7c…
|
aku
|
2661 |
** The special list characters have the 0x10 flag set |
|
588bb7c…
|
aku
|
2662 |
** |
|
588bb7c…
|
aku
|
2663 |
** { } [ ] \ ; ' " |
|
588bb7c…
|
aku
|
2664 |
** |
|
588bb7c…
|
aku
|
2665 |
** " 0x22 |
|
588bb7c…
|
aku
|
2666 |
** |
|
588bb7c…
|
aku
|
2667 |
*/ |
|
588bb7c…
|
aku
|
2668 |
static unsigned char aCharProp[256] = { |
|
588bb7c…
|
aku
|
2669 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, /* 0x0. */ |
|
588bb7c…
|
aku
|
2670 |
0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x1. */ |
|
588bb7c…
|
aku
|
2671 |
5, 4, 20, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, /* 0x2. */ |
|
1f6734c…
|
jan.nijtmans
|
2672 |
38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 4, 20, 4, 4, 4, 4, /* 0x3. */ |
|
1f6734c…
|
jan.nijtmans
|
2673 |
4, 44, 44, 44, 44, 44, 44, 12, 12, 12, 12, 12, 12, 12, 12, 12, /* 0x4. */ |
|
588bb7c…
|
aku
|
2674 |
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 20, 20, 20, 4, 4, /* 0x5. */ |
|
1f6734c…
|
jan.nijtmans
|
2675 |
4, 44, 44, 44, 44, 44, 44, 12, 12, 12, 12, 12, 12, 12, 12, 12, /* 0x6. */ |
|
588bb7c…
|
aku
|
2676 |
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 20, 4, 20, 4, 4, /* 0x7. */ |
|
588bb7c…
|
aku
|
2677 |
|
|
588bb7c…
|
aku
|
2678 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x8. */ |
|
588bb7c…
|
aku
|
2679 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x9. */ |
|
588bb7c…
|
aku
|
2680 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xA. */ |
|
588bb7c…
|
aku
|
2681 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xB. */ |
|
588bb7c…
|
aku
|
2682 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xC. */ |
|
588bb7c…
|
aku
|
2683 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xD. */ |
|
588bb7c…
|
aku
|
2684 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xE. */ |
|
588bb7c…
|
aku
|
2685 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xF. */ |
|
588bb7c…
|
aku
|
2686 |
}; |
|
588bb7c…
|
aku
|
2687 |
|
|
588bb7c…
|
aku
|
2688 |
/* |
|
588bb7c…
|
aku
|
2689 |
** Clone of the standard isspace() and isdigit function/macros. |
|
588bb7c…
|
aku
|
2690 |
*/ |
|
0c99a15…
|
drh
|
2691 |
int th_isspace(char c){ |
|
0c99a15…
|
drh
|
2692 |
return (aCharProp[(unsigned char)c] & 0x01); |
|
0c99a15…
|
drh
|
2693 |
} |
|
0c99a15…
|
drh
|
2694 |
int th_isdigit(char c){ |
|
0c99a15…
|
drh
|
2695 |
return (aCharProp[(unsigned char)c] & 0x02); |
|
0c99a15…
|
drh
|
2696 |
} |
|
0c99a15…
|
drh
|
2697 |
int th_isspecial(char c){ |
|
0c99a15…
|
drh
|
2698 |
return (aCharProp[(unsigned char)c] & 0x11); |
|
0c99a15…
|
drh
|
2699 |
} |
|
0c99a15…
|
drh
|
2700 |
int th_isalnum(char c){ |
|
0c99a15…
|
drh
|
2701 |
return (aCharProp[(unsigned char)c] & 0x0A); |
|
99901fb…
|
jan.nijtmans
|
2702 |
} |
|
99901fb…
|
jan.nijtmans
|
2703 |
int th_isalpha(char c){ |
|
99901fb…
|
jan.nijtmans
|
2704 |
return (aCharProp[(unsigned char)c] & 0x08); |
|
1f6734c…
|
jan.nijtmans
|
2705 |
} |
|
1f6734c…
|
jan.nijtmans
|
2706 |
int th_ishexdig(char c){ |
|
1f6734c…
|
jan.nijtmans
|
2707 |
return (aCharProp[(unsigned char)c] & 0x20); |
|
1f6734c…
|
jan.nijtmans
|
2708 |
} |
|
1f6734c…
|
jan.nijtmans
|
2709 |
int th_isoctdig(char c){ |
|
1f6734c…
|
jan.nijtmans
|
2710 |
return ((c|7) == '7'); |
|
1f6734c…
|
jan.nijtmans
|
2711 |
} |
|
1f6734c…
|
jan.nijtmans
|
2712 |
int th_isbindig(char c){ |
|
1f6734c…
|
jan.nijtmans
|
2713 |
return ((c|1) == '1'); |
|
c30cd93…
|
jan.nijtmans
|
2714 |
} |
|
588bb7c…
|
aku
|
2715 |
|
|
588bb7c…
|
aku
|
2716 |
#ifndef LONGDOUBLE_TYPE |
|
588bb7c…
|
aku
|
2717 |
# define LONGDOUBLE_TYPE long double |
|
588bb7c…
|
aku
|
2718 |
#endif |
|
588bb7c…
|
aku
|
2719 |
|
|
588bb7c…
|
aku
|
2720 |
|
|
588bb7c…
|
aku
|
2721 |
/* |
|
588bb7c…
|
aku
|
2722 |
** Return TRUE if z is a pure numeric string. Return FALSE if the |
|
588bb7c…
|
aku
|
2723 |
** string contains any character which is not part of a number. If |
|
588bb7c…
|
aku
|
2724 |
** the string is numeric and contains the '.' character, set *realnum |
|
588bb7c…
|
aku
|
2725 |
** to TRUE (otherwise FALSE). |
|
588bb7c…
|
aku
|
2726 |
** |
|
588bb7c…
|
aku
|
2727 |
** An empty string is considered non-numeric. |
|
588bb7c…
|
aku
|
2728 |
*/ |
|
588bb7c…
|
aku
|
2729 |
static int sqlite3IsNumber(const char *z, int *realnum){ |
|
588bb7c…
|
aku
|
2730 |
int incr = 1; |
|
588bb7c…
|
aku
|
2731 |
if( *z=='-' || *z=='+' ) z += incr; |
|
588bb7c…
|
aku
|
2732 |
if( !th_isdigit(*(u8*)z) ){ |
|
588bb7c…
|
aku
|
2733 |
return 0; |
|
588bb7c…
|
aku
|
2734 |
} |
|
588bb7c…
|
aku
|
2735 |
z += incr; |
|
588bb7c…
|
aku
|
2736 |
if( realnum ) *realnum = 0; |
|
588bb7c…
|
aku
|
2737 |
while( th_isdigit(*(u8*)z) ){ z += incr; } |
|
588bb7c…
|
aku
|
2738 |
if( *z=='.' ){ |
|
588bb7c…
|
aku
|
2739 |
z += incr; |
|
588bb7c…
|
aku
|
2740 |
if( !th_isdigit(*(u8*)z) ) return 0; |
|
588bb7c…
|
aku
|
2741 |
while( th_isdigit(*(u8*)z) ){ z += incr; } |
|
588bb7c…
|
aku
|
2742 |
if( realnum ) *realnum = 1; |
|
588bb7c…
|
aku
|
2743 |
} |
|
588bb7c…
|
aku
|
2744 |
if( *z=='e' || *z=='E' ){ |
|
588bb7c…
|
aku
|
2745 |
z += incr; |
|
588bb7c…
|
aku
|
2746 |
if( *z=='+' || *z=='-' ) z += incr; |
|
588bb7c…
|
aku
|
2747 |
if( !th_isdigit(*(u8*)z) ) return 0; |
|
588bb7c…
|
aku
|
2748 |
while( th_isdigit(*(u8*)z) ){ z += incr; } |
|
588bb7c…
|
aku
|
2749 |
if( realnum ) *realnum = 1; |
|
588bb7c…
|
aku
|
2750 |
} |
|
588bb7c…
|
aku
|
2751 |
return *z==0; |
|
588bb7c…
|
aku
|
2752 |
} |
|
588bb7c…
|
aku
|
2753 |
|
|
588bb7c…
|
aku
|
2754 |
/* |
|
a10f931…
|
drh
|
2755 |
** The string z[] is an ascii representation of a real number. |
|
a10f931…
|
drh
|
2756 |
** Convert this string to a double. |
|
a10f931…
|
drh
|
2757 |
** |
|
a10f931…
|
drh
|
2758 |
** This routine assumes that z[] really is a valid number. If it |
|
a10f931…
|
drh
|
2759 |
** is not, the result is undefined. |
|
a10f931…
|
drh
|
2760 |
** |
|
a10f931…
|
drh
|
2761 |
** This routine is used instead of the library atof() function because |
|
a10f931…
|
drh
|
2762 |
** the library atof() might want to use "," as the decimal point instead |
|
a10f931…
|
drh
|
2763 |
** of "." depending on how locale is set. But that would cause problems |
|
a10f931…
|
drh
|
2764 |
** for SQL. So this routine always uses "." regardless of locale. |
|
a10f931…
|
drh
|
2765 |
*/ |
|
a10f931…
|
drh
|
2766 |
static int sqlite3AtoF(const char *z, double *pResult){ |
|
a10f931…
|
drh
|
2767 |
int sign = 1; |
|
a10f931…
|
drh
|
2768 |
const char *zBegin = z; |
|
a10f931…
|
drh
|
2769 |
LONGDOUBLE_TYPE v1 = 0.0; |
|
a10f931…
|
drh
|
2770 |
while( th_isspace(*(u8*)z) ) z++; |
|
a10f931…
|
drh
|
2771 |
if( *z=='-' ){ |
|
a10f931…
|
drh
|
2772 |
sign = -1; |
|
a10f931…
|
drh
|
2773 |
z++; |
|
a10f931…
|
drh
|
2774 |
}else if( *z=='+' ){ |
|
a10f931…
|
drh
|
2775 |
z++; |
|
a10f931…
|
drh
|
2776 |
} |
|
a10f931…
|
drh
|
2777 |
while( th_isdigit(*(u8*)z) ){ |
|
a10f931…
|
drh
|
2778 |
v1 = v1*10.0 + (*z - '0'); |
|
a10f931…
|
drh
|
2779 |
z++; |
|
a10f931…
|
drh
|
2780 |
} |
|
a10f931…
|
drh
|
2781 |
if( *z=='.' ){ |
|
a10f931…
|
drh
|
2782 |
LONGDOUBLE_TYPE divisor = 1.0; |
|
a10f931…
|
drh
|
2783 |
z++; |
|
a10f931…
|
drh
|
2784 |
while( th_isdigit(*(u8*)z) ){ |
|
a10f931…
|
drh
|
2785 |
v1 = v1*10.0 + (*z - '0'); |
|
a10f931…
|
drh
|
2786 |
divisor *= 10.0; |
|
a10f931…
|
drh
|
2787 |
z++; |
|
a10f931…
|
drh
|
2788 |
} |
|
a10f931…
|
drh
|
2789 |
v1 /= divisor; |
|
a10f931…
|
drh
|
2790 |
} |
|
a10f931…
|
drh
|
2791 |
if( *z=='e' || *z=='E' ){ |
|
a10f931…
|
drh
|
2792 |
int esign = 1; |
|
a10f931…
|
drh
|
2793 |
int eval = 0; |
|
a10f931…
|
drh
|
2794 |
LONGDOUBLE_TYPE scale = 1.0; |
|
a10f931…
|
drh
|
2795 |
z++; |
|
a10f931…
|
drh
|
2796 |
if( *z=='-' ){ |
|
a10f931…
|
drh
|
2797 |
esign = -1; |
|
a10f931…
|
drh
|
2798 |
z++; |
|
a10f931…
|
drh
|
2799 |
}else if( *z=='+' ){ |
|
a10f931…
|
drh
|
2800 |
z++; |
|
a10f931…
|
drh
|
2801 |
} |
|
a10f931…
|
drh
|
2802 |
while( th_isdigit(*(u8*)z) ){ |
|
a10f931…
|
drh
|
2803 |
eval = eval*10 + *z - '0'; |
|
a10f931…
|
drh
|
2804 |
z++; |
|
a10f931…
|
drh
|
2805 |
} |
|
a10f931…
|
drh
|
2806 |
while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; } |
|
a10f931…
|
drh
|
2807 |
while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; } |
|
a10f931…
|
drh
|
2808 |
while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; } |
|
a10f931…
|
drh
|
2809 |
while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; } |
|
a10f931…
|
drh
|
2810 |
if( esign<0 ){ |
|
a10f931…
|
drh
|
2811 |
v1 /= scale; |
|
a10f931…
|
drh
|
2812 |
}else{ |
|
a10f931…
|
drh
|
2813 |
v1 *= scale; |
|
a10f931…
|
drh
|
2814 |
} |
|
a10f931…
|
drh
|
2815 |
} |
|
a10f931…
|
drh
|
2816 |
*pResult = sign<0 ? -v1 : v1; |
|
a10f931…
|
drh
|
2817 |
return z - zBegin; |
|
a10f931…
|
drh
|
2818 |
} |
|
a10f931…
|
drh
|
2819 |
|
|
a10f931…
|
drh
|
2820 |
/* |
|
588bb7c…
|
aku
|
2821 |
** Try to convert the string passed as arguments (z, n) to an integer. |
|
1ebe4b0…
|
mistachkin
|
2822 |
** If successful, store the result in *piOut and return TH_OK. |
|
588bb7c…
|
aku
|
2823 |
** |
|
1ebe4b0…
|
mistachkin
|
2824 |
** If the string cannot be converted to an integer, return TH_ERROR. |
|
1ebe4b0…
|
mistachkin
|
2825 |
** If the interp argument is not NULL, leave an error message in the |
|
588bb7c…
|
aku
|
2826 |
** interpreter result too. |
|
588bb7c…
|
aku
|
2827 |
*/ |
|
0c99a15…
|
drh
|
2828 |
int Th_ToInt(Th_Interp *interp, const char *z, int n, int *piOut){ |
|
588bb7c…
|
aku
|
2829 |
int i = 0; |
|
588bb7c…
|
aku
|
2830 |
int iOut = 0; |
|
1f6734c…
|
jan.nijtmans
|
2831 |
int base = 10; |
|
1f6734c…
|
jan.nijtmans
|
2832 |
int (*isdigit)(char) = th_isdigit; |
|
588bb7c…
|
aku
|
2833 |
|
|
588bb7c…
|
aku
|
2834 |
if( n<0 ){ |
|
588bb7c…
|
aku
|
2835 |
n = th_strlen(z); |
|
2116238…
|
drh
|
2836 |
}else{ |
|
2116238…
|
drh
|
2837 |
n = TH1_LEN(n); |
|
588bb7c…
|
aku
|
2838 |
} |
|
588bb7c…
|
aku
|
2839 |
|
|
4ae42e3…
|
jan.nijtmans
|
2840 |
if( n>1 && (z[0]=='-' || z[0]=='+') ){ |
|
588bb7c…
|
aku
|
2841 |
i = 1; |
|
588bb7c…
|
aku
|
2842 |
} |
|
fc248d6…
|
mistachkin
|
2843 |
if( (n-i)>2 && z[i]=='0' ){ |
|
4ae42e3…
|
jan.nijtmans
|
2844 |
if( z[i+1]=='x' || z[i+1]=='X' ){ |
|
4ae42e3…
|
jan.nijtmans
|
2845 |
i += 2; |
|
4ae42e3…
|
jan.nijtmans
|
2846 |
base = 16; |
|
4ae42e3…
|
jan.nijtmans
|
2847 |
isdigit = th_ishexdig; |
|
4ae42e3…
|
jan.nijtmans
|
2848 |
}else if( z[i+1]=='o' || z[i+1]=='O' ){ |
|
4ae42e3…
|
jan.nijtmans
|
2849 |
i += 2; |
|
4ae42e3…
|
jan.nijtmans
|
2850 |
base = 8; |
|
4ae42e3…
|
jan.nijtmans
|
2851 |
isdigit = th_isoctdig; |
|
4ae42e3…
|
jan.nijtmans
|
2852 |
}else if( z[i+1]=='b' || z[i+1]=='B' ){ |
|
4ae42e3…
|
jan.nijtmans
|
2853 |
i += 2; |
|
4ae42e3…
|
jan.nijtmans
|
2854 |
base = 2; |
|
4ae42e3…
|
jan.nijtmans
|
2855 |
isdigit = th_isbindig; |
|
1f6734c…
|
jan.nijtmans
|
2856 |
} |
|
1f6734c…
|
jan.nijtmans
|
2857 |
} |
|
588bb7c…
|
aku
|
2858 |
for(; i<n; i++){ |
|
99901fb…
|
jan.nijtmans
|
2859 |
char c = z[i]; |
|
99901fb…
|
jan.nijtmans
|
2860 |
if( !isdigit(c) ){ |
|
588bb7c…
|
aku
|
2861 |
Th_ErrorMessage(interp, "expected integer, got: \"", z, n); |
|
588bb7c…
|
aku
|
2862 |
return TH_ERROR; |
|
588bb7c…
|
aku
|
2863 |
} |
|
99901fb…
|
jan.nijtmans
|
2864 |
if( c>='a' ){ |
|
99901fb…
|
jan.nijtmans
|
2865 |
c -= 'a'-10; |
|
99901fb…
|
jan.nijtmans
|
2866 |
}else if( c>='A' ){ |
|
99901fb…
|
jan.nijtmans
|
2867 |
c -= 'A'-10; |
|
1f6734c…
|
jan.nijtmans
|
2868 |
}else{ |
|
99901fb…
|
jan.nijtmans
|
2869 |
c -= '0'; |
|
1f6734c…
|
jan.nijtmans
|
2870 |
} |
|
99901fb…
|
jan.nijtmans
|
2871 |
iOut = iOut * base + c; |
|
588bb7c…
|
aku
|
2872 |
} |
|
588bb7c…
|
aku
|
2873 |
|
|
588bb7c…
|
aku
|
2874 |
if( n>0 && z[0]=='-' ){ |
|
588bb7c…
|
aku
|
2875 |
iOut *= -1; |
|
588bb7c…
|
aku
|
2876 |
} |
|
588bb7c…
|
aku
|
2877 |
|
|
588bb7c…
|
aku
|
2878 |
*piOut = iOut; |
|
588bb7c…
|
aku
|
2879 |
return TH_OK; |
|
588bb7c…
|
aku
|
2880 |
} |
|
588bb7c…
|
aku
|
2881 |
|
|
588bb7c…
|
aku
|
2882 |
/* |
|
588bb7c…
|
aku
|
2883 |
** Try to convert the string passed as arguments (z, n) to a double. |
|
1ebe4b0…
|
mistachkin
|
2884 |
** If successful, store the result in *pfOut and return TH_OK. |
|
588bb7c…
|
aku
|
2885 |
** |
|
1ebe4b0…
|
mistachkin
|
2886 |
** If the string cannot be converted to a double, return TH_ERROR. |
|
1ebe4b0…
|
mistachkin
|
2887 |
** If the interp argument is not NULL, leave an error message in the |
|
588bb7c…
|
aku
|
2888 |
** interpreter result too. |
|
588bb7c…
|
aku
|
2889 |
*/ |
|
588bb7c…
|
aku
|
2890 |
int Th_ToDouble( |
|
1ebe4b0…
|
mistachkin
|
2891 |
Th_Interp *interp, |
|
1ebe4b0…
|
mistachkin
|
2892 |
const char *z, |
|
1ebe4b0…
|
mistachkin
|
2893 |
int n, |
|
588bb7c…
|
aku
|
2894 |
double *pfOut |
|
588bb7c…
|
aku
|
2895 |
){ |
|
a10f931…
|
drh
|
2896 |
if( !sqlite3IsNumber((const char *)z, 0) ){ |
|
2116238…
|
drh
|
2897 |
Th_ErrorMessage(interp, "expected number, got: \"", z, TH1_LEN(n)); |
|
588bb7c…
|
aku
|
2898 |
return TH_ERROR; |
|
588bb7c…
|
aku
|
2899 |
} |
|
a10f931…
|
drh
|
2900 |
|
|
a10f931…
|
drh
|
2901 |
sqlite3AtoF((const char *)z, pfOut); |
|
588bb7c…
|
aku
|
2902 |
return TH_OK; |
|
588bb7c…
|
aku
|
2903 |
} |
|
588bb7c…
|
aku
|
2904 |
|
|
588bb7c…
|
aku
|
2905 |
/* |
|
588bb7c…
|
aku
|
2906 |
** Set the result of the interpreter to the th1 representation of |
|
588bb7c…
|
aku
|
2907 |
** the integer iVal and return TH_OK. |
|
588bb7c…
|
aku
|
2908 |
*/ |
|
588bb7c…
|
aku
|
2909 |
int Th_SetResultInt(Th_Interp *interp, int iVal){ |
|
588bb7c…
|
aku
|
2910 |
int isNegative = 0; |
|
301edfc…
|
preben
|
2911 |
unsigned int uVal = iVal; |
|
0c99a15…
|
drh
|
2912 |
char zBuf[32]; |
|
0c99a15…
|
drh
|
2913 |
char *z = &zBuf[32]; |
|
588bb7c…
|
aku
|
2914 |
|
|
588bb7c…
|
aku
|
2915 |
if( iVal<0 ){ |
|
e546a2b…
|
drh
|
2916 |
if( iVal==0x80000000 ){ |
|
7d2e4ed…
|
drh
|
2917 |
return Th_SetResult(interp, "-2147483648", -1); |
|
7d2e4ed…
|
drh
|
2918 |
} |
|
588bb7c…
|
aku
|
2919 |
isNegative = 1; |
|
301edfc…
|
preben
|
2920 |
uVal = iVal * -1; |
|
588bb7c…
|
aku
|
2921 |
} |
|
588bb7c…
|
aku
|
2922 |
*(--z) = '\0'; |
|
301edfc…
|
preben
|
2923 |
*(--z) = (char)(48+(uVal%10)); |
|
301edfc…
|
preben
|
2924 |
while( (uVal = (uVal/10))>0 ){ |
|
301edfc…
|
preben
|
2925 |
*(--z) = (char)(48+(uVal%10)); |
|
588bb7c…
|
aku
|
2926 |
assert(z>zBuf); |
|
588bb7c…
|
aku
|
2927 |
} |
|
588bb7c…
|
aku
|
2928 |
if( isNegative ){ |
|
588bb7c…
|
aku
|
2929 |
*(--z) = '-'; |
|
588bb7c…
|
aku
|
2930 |
} |
|
588bb7c…
|
aku
|
2931 |
|
|
588bb7c…
|
aku
|
2932 |
return Th_SetResult(interp, z, -1); |
|
588bb7c…
|
aku
|
2933 |
} |
|
588bb7c…
|
aku
|
2934 |
|
|
588bb7c…
|
aku
|
2935 |
/* |
|
588bb7c…
|
aku
|
2936 |
** Set the result of the interpreter to the th1 representation of |
|
588bb7c…
|
aku
|
2937 |
** the double fVal and return TH_OK. |
|
588bb7c…
|
aku
|
2938 |
*/ |
|
588bb7c…
|
aku
|
2939 |
int Th_SetResultDouble(Th_Interp *interp, double fVal){ |
|
588bb7c…
|
aku
|
2940 |
int i; /* Iterator variable */ |
|
588bb7c…
|
aku
|
2941 |
double v = fVal; /* Input value */ |
|
1ebe4b0…
|
mistachkin
|
2942 |
char zBuf[128]; /* Output buffer */ |
|
1ebe4b0…
|
mistachkin
|
2943 |
char *z = zBuf; /* Output cursor */ |
|
588bb7c…
|
aku
|
2944 |
int iDot = 0; /* Digit after which to place decimal point */ |
|
588bb7c…
|
aku
|
2945 |
int iExp = 0; /* Exponent (NN in eNN) */ |
|
1ebe4b0…
|
mistachkin
|
2946 |
const char *zExp; /* String representation of iExp */ |
|
588bb7c…
|
aku
|
2947 |
|
|
588bb7c…
|
aku
|
2948 |
/* Precision: */ |
|
588bb7c…
|
aku
|
2949 |
#define INSIGNIFICANT 0.000000000001 |
|
588bb7c…
|
aku
|
2950 |
#define ROUNDER 0.0000000000005 |
|
588bb7c…
|
aku
|
2951 |
double insignificant = INSIGNIFICANT; |
|
588bb7c…
|
aku
|
2952 |
|
|
588bb7c…
|
aku
|
2953 |
/* If the real value is negative, write a '-' character to the |
|
588bb7c…
|
aku
|
2954 |
* output and transform v to the corresponding positive number. |
|
1ebe4b0…
|
mistachkin
|
2955 |
*/ |
|
588bb7c…
|
aku
|
2956 |
if( v<0.0 ){ |
|
588bb7c…
|
aku
|
2957 |
*z++ = '-'; |
|
588bb7c…
|
aku
|
2958 |
v *= -1.0; |
|
588bb7c…
|
aku
|
2959 |
} |
|
588bb7c…
|
aku
|
2960 |
|
|
1ebe4b0…
|
mistachkin
|
2961 |
/* Normalize v to a value between 1.0 and 10.0. Integer |
|
588bb7c…
|
aku
|
2962 |
* variable iExp is set to the exponent. i.e the original |
|
588bb7c…
|
aku
|
2963 |
* value is (v * 10^iExp) (or the negative thereof). |
|
1ebe4b0…
|
mistachkin
|
2964 |
*/ |
|
588bb7c…
|
aku
|
2965 |
if( v>0.0 ){ |
|
588bb7c…
|
aku
|
2966 |
while( (v+ROUNDER)>=10.0 ) { iExp++; v *= 0.1; } |
|
588bb7c…
|
aku
|
2967 |
while( (v+ROUNDER)<1.0 ) { iExp--; v *= 10.0; } |
|
588bb7c…
|
aku
|
2968 |
} |
|
588bb7c…
|
aku
|
2969 |
v += ROUNDER; |
|
588bb7c…
|
aku
|
2970 |
|
|
588bb7c…
|
aku
|
2971 |
/* For a small (<12) positive exponent, move the decimal point |
|
588bb7c…
|
aku
|
2972 |
* instead of using the "eXX" notation. |
|
588bb7c…
|
aku
|
2973 |
*/ |
|
588bb7c…
|
aku
|
2974 |
if( iExp>0 && iExp<12 ){ |
|
588bb7c…
|
aku
|
2975 |
iDot = iExp; |
|
588bb7c…
|
aku
|
2976 |
iExp = 0; |
|
588bb7c…
|
aku
|
2977 |
} |
|
588bb7c…
|
aku
|
2978 |
|
|
588bb7c…
|
aku
|
2979 |
/* For a small (>-4) negative exponent, write leading zeroes |
|
588bb7c…
|
aku
|
2980 |
* instead of using the "eXX" notation. |
|
588bb7c…
|
aku
|
2981 |
*/ |
|
588bb7c…
|
aku
|
2982 |
if( iExp<0 && iExp>-4 ){ |
|
588bb7c…
|
aku
|
2983 |
*z++ = '0'; |
|
588bb7c…
|
aku
|
2984 |
*z++ = '.'; |
|
588bb7c…
|
aku
|
2985 |
for(i=0; i>(iExp+1); i--){ |
|
588bb7c…
|
aku
|
2986 |
*z++ = '0'; |
|
588bb7c…
|
aku
|
2987 |
} |
|
588bb7c…
|
aku
|
2988 |
iDot = -1; |
|
588bb7c…
|
aku
|
2989 |
iExp = 0; |
|
588bb7c…
|
aku
|
2990 |
} |
|
588bb7c…
|
aku
|
2991 |
|
|
588bb7c…
|
aku
|
2992 |
/* Output the digits in real value v. The value of iDot determines |
|
588bb7c…
|
aku
|
2993 |
* where (if at all) the decimal point is placed. |
|
588bb7c…
|
aku
|
2994 |
*/ |
|
588bb7c…
|
aku
|
2995 |
for(i=0; i<=(iDot+1) || v>=insignificant; i++){ |
|
0c99a15…
|
drh
|
2996 |
*z++ = (char)(48 + (int)v); |
|
588bb7c…
|
aku
|
2997 |
v = (v - ((double)(int)v)) * 10.0; |
|
588bb7c…
|
aku
|
2998 |
insignificant *= 10.0; |
|
588bb7c…
|
aku
|
2999 |
if( iDot==i ){ |
|
588bb7c…
|
aku
|
3000 |
*z++ = '.'; |
|
588bb7c…
|
aku
|
3001 |
} |
|
588bb7c…
|
aku
|
3002 |
} |
|
588bb7c…
|
aku
|
3003 |
|
|
588bb7c…
|
aku
|
3004 |
/* If the exponent is not zero, add the "eXX" notation to the |
|
588bb7c…
|
aku
|
3005 |
* end of the string. |
|
588bb7c…
|
aku
|
3006 |
*/ |
|
588bb7c…
|
aku
|
3007 |
if( iExp!=0 ){ |
|
588bb7c…
|
aku
|
3008 |
*z++ = 'e'; |
|
588bb7c…
|
aku
|
3009 |
Th_SetResultInt(interp, iExp); |
|
588bb7c…
|
aku
|
3010 |
zExp = Th_GetResult(interp, 0); |
|
588bb7c…
|
aku
|
3011 |
for(i=0; zExp[i]; i++){ |
|
588bb7c…
|
aku
|
3012 |
*z++ = zExp[i]; |
|
588bb7c…
|
aku
|
3013 |
} |
|
588bb7c…
|
aku
|
3014 |
} |
|
588bb7c…
|
aku
|
3015 |
|
|
588bb7c…
|
aku
|
3016 |
*z = '\0'; |
|
588bb7c…
|
aku
|
3017 |
return Th_SetResult(interp, zBuf, -1); |
|
810e6c1…
|
mistachkin
|
3018 |
} |
|
810e6c1…
|
mistachkin
|
3019 |
|
|
810e6c1…
|
mistachkin
|
3020 |
/* |
|
810e6c1…
|
mistachkin
|
3021 |
** Appends all currently registered command names to the specified list |
|
810e6c1…
|
mistachkin
|
3022 |
** and returns TH_OK upon success. Any other return value indicates an |
|
810e6c1…
|
mistachkin
|
3023 |
** error. |
|
810e6c1…
|
mistachkin
|
3024 |
*/ |
|
dda5d11…
|
mistachkin
|
3025 |
int Th_ListAppendCommands( |
|
dda5d11…
|
mistachkin
|
3026 |
Th_Interp *interp, /* Interpreter context */ |
|
dda5d11…
|
mistachkin
|
3027 |
char **pzList, /* OUT: List of command names */ |
|
dda5d11…
|
mistachkin
|
3028 |
int *pnList /* OUT: Number of command names */ |
|
dda5d11…
|
mistachkin
|
3029 |
){ |
|
810e6c1…
|
mistachkin
|
3030 |
Th_InterpAndList *p = (Th_InterpAndList *)Th_Malloc( |
|
810e6c1…
|
mistachkin
|
3031 |
interp, sizeof(Th_InterpAndList) |
|
810e6c1…
|
mistachkin
|
3032 |
); |
|
810e6c1…
|
mistachkin
|
3033 |
p->interp = interp; |
|
810e6c1…
|
mistachkin
|
3034 |
p->pzList = pzList; |
|
810e6c1…
|
mistachkin
|
3035 |
p->pnList = pnList; |
|
22b3d0b…
|
mistachkin
|
3036 |
Th_HashIterate(interp, interp->paCmd, thListAppendHashKey, p); |
|
810e6c1…
|
mistachkin
|
3037 |
Th_Free(interp, p); |
|
810e6c1…
|
mistachkin
|
3038 |
return TH_OK; |
|
810e6c1…
|
mistachkin
|
3039 |
} |
|
810e6c1…
|
mistachkin
|
3040 |
|
|
810e6c1…
|
mistachkin
|
3041 |
/* |
|
810e6c1…
|
mistachkin
|
3042 |
** Appends all variable names for the current frame to the specified list |
|
810e6c1…
|
mistachkin
|
3043 |
** and returns TH_OK upon success. Any other return value indicates an |
|
810e6c1…
|
mistachkin
|
3044 |
** error. If the current frame cannot be obtained, TH_ERROR is returned. |
|
810e6c1…
|
mistachkin
|
3045 |
*/ |
|
dda5d11…
|
mistachkin
|
3046 |
int Th_ListAppendVariables( |
|
dda5d11…
|
mistachkin
|
3047 |
Th_Interp *interp, /* Interpreter context */ |
|
dda5d11…
|
mistachkin
|
3048 |
char **pzList, /* OUT: List of variable names */ |
|
dda5d11…
|
mistachkin
|
3049 |
int *pnList /* OUT: Number of variable names */ |
|
dda5d11…
|
mistachkin
|
3050 |
){ |
|
810e6c1…
|
mistachkin
|
3051 |
Th_Frame *pFrame = getFrame(interp, 0); |
|
810e6c1…
|
mistachkin
|
3052 |
if( pFrame ){ |
|
810e6c1…
|
mistachkin
|
3053 |
Th_InterpAndList *p = (Th_InterpAndList *)Th_Malloc( |
|
810e6c1…
|
mistachkin
|
3054 |
interp, sizeof(Th_InterpAndList) |
|
810e6c1…
|
mistachkin
|
3055 |
); |
|
810e6c1…
|
mistachkin
|
3056 |
p->interp = interp; |
|
810e6c1…
|
mistachkin
|
3057 |
p->pzList = pzList; |
|
810e6c1…
|
mistachkin
|
3058 |
p->pnList = pnList; |
|
22b3d0b…
|
mistachkin
|
3059 |
Th_HashIterate(interp, pFrame->paVar, thListAppendHashKey, p); |
|
810e6c1…
|
mistachkin
|
3060 |
Th_Free(interp, p); |
|
810e6c1…
|
mistachkin
|
3061 |
return TH_OK; |
|
810e6c1…
|
mistachkin
|
3062 |
}else{ |
|
810e6c1…
|
mistachkin
|
3063 |
return TH_ERROR; |
|
810e6c1…
|
mistachkin
|
3064 |
} |
|
ea7813d…
|
mistachkin
|
3065 |
} |
|
ea7813d…
|
mistachkin
|
3066 |
|
|
ea7813d…
|
mistachkin
|
3067 |
/* |
|
ea7813d…
|
mistachkin
|
3068 |
** Appends all array element names for the specified array variable to the |
|
ea7813d…
|
mistachkin
|
3069 |
** specified list and returns TH_OK upon success. Any other return value |
|
ea7813d…
|
mistachkin
|
3070 |
** indicates an error. |
|
ea7813d…
|
mistachkin
|
3071 |
*/ |
|
ea7813d…
|
mistachkin
|
3072 |
int Th_ListAppendArray( |
|
dda5d11…
|
mistachkin
|
3073 |
Th_Interp *interp, /* Interpreter context */ |
|
ea7813d…
|
mistachkin
|
3074 |
const char *zVar, /* Pointer to variable name */ |
|
ea7813d…
|
mistachkin
|
3075 |
int nVar, /* Number of bytes at nVar */ |
|
ea7813d…
|
mistachkin
|
3076 |
char **pzList, /* OUT: List of array element names */ |
|
ea7813d…
|
mistachkin
|
3077 |
int *pnList /* OUT: Number of array element names */ |
|
ea7813d…
|
mistachkin
|
3078 |
){ |
|
ea7813d…
|
mistachkin
|
3079 |
Th_Variable *pValue = thFindValue(interp, zVar, nVar, 0, 1, 1, 0); |
|
ea7813d…
|
mistachkin
|
3080 |
if( pValue && !pValue->zData && pValue->pHash ){ |
|
ea7813d…
|
mistachkin
|
3081 |
Th_InterpAndList *p = (Th_InterpAndList *)Th_Malloc( |
|
ea7813d…
|
mistachkin
|
3082 |
interp, sizeof(Th_InterpAndList) |
|
ea7813d…
|
mistachkin
|
3083 |
); |
|
ea7813d…
|
mistachkin
|
3084 |
p->interp = interp; |
|
ea7813d…
|
mistachkin
|
3085 |
p->pzList = pzList; |
|
ea7813d…
|
mistachkin
|
3086 |
p->pnList = pnList; |
|
ea7813d…
|
mistachkin
|
3087 |
Th_HashIterate(interp, pValue->pHash, thListAppendHashKey, p); |
|
ea7813d…
|
mistachkin
|
3088 |
Th_Free(interp, p); |
|
ea7813d…
|
mistachkin
|
3089 |
}else{ |
|
ea7813d…
|
mistachkin
|
3090 |
*pzList = 0; |
|
ea7813d…
|
mistachkin
|
3091 |
*pnList = 0; |
|
ea7813d…
|
mistachkin
|
3092 |
} |
|
ea7813d…
|
mistachkin
|
3093 |
return TH_OK; |
|
4e683ef…
|
drh
|
3094 |
} |