|
588bb7c…
|
aku
|
1 |
/* This header file defines the external interface to the custom Scripting |
|
9670503…
|
mistachkin
|
2 |
** Language (TH) interpreter. TH is very similar to Tcl but is not an |
|
0c99a15…
|
drh
|
3 |
** exact clone. |
|
2116238…
|
drh
|
4 |
** |
|
2116238…
|
drh
|
5 |
** TH1 was original developed to run SQLite tests on SymbianOS. This version |
|
2116238…
|
drh
|
6 |
** of TH1 was repurposed as a scripted language for Fossil, and was heavily |
|
2116238…
|
drh
|
7 |
** modified for that purpose, beginning in early 2008. |
|
2116238…
|
drh
|
8 |
** |
|
2116238…
|
drh
|
9 |
** More recently, TH1 has been enhanced to distinguish between regular text |
|
2116238…
|
drh
|
10 |
** and "tainted" text. "Tainted" text is text that might have originated |
|
2116238…
|
drh
|
11 |
** from an outside source and hence might not be trustworthy. To prevent |
|
2116238…
|
drh
|
12 |
** cross-site scripting (XSS) and SQL-injections and similar attacks, |
|
2116238…
|
drh
|
13 |
** tainted text should not be used for the following purposes: |
|
2116238…
|
drh
|
14 |
** |
|
2116238…
|
drh
|
15 |
** * executed as TH1 script or expression. |
|
2116238…
|
drh
|
16 |
** * output as HTML or Javascript |
|
2116238…
|
drh
|
17 |
** * used as part of an SQL query |
|
2116238…
|
drh
|
18 |
** |
|
2116238…
|
drh
|
19 |
** Tainted text can be converted into a safe form using commands like |
|
2116238…
|
drh
|
20 |
** "htmlize". And some commands ("query" and "expr") know how to use |
|
2116238…
|
drh
|
21 |
** potentially tainted variable values directly, and thus can bypass |
|
2116238…
|
drh
|
22 |
** the restrictions above. |
|
2116238…
|
drh
|
23 |
** |
|
2116238…
|
drh
|
24 |
** Whether a string is clean or tainted is determined by its length integer. |
|
2116238…
|
drh
|
25 |
** TH1 limits strings to be no more than 0x0fffffff bytes bytes in length |
|
2116238…
|
drh
|
26 |
** (about 268MB - more than sufficient for the purposes of Fossil). The top |
|
2116238…
|
drh
|
27 |
** bit of the length integer is the sign bit, of course. The next three bits |
|
2116238…
|
drh
|
28 |
** are reserved. One of those, the 0x10000000 bit, marks tainted strings. |
|
588bb7c…
|
aku
|
29 |
*/ |
|
2116238…
|
drh
|
30 |
#define TH1_MX_STRLEN 0x0fffffff /* Maximum length of a TH1-C string */ |
|
2116238…
|
drh
|
31 |
#define TH1_TAINT_BIT 0x10000000 /* The taint bit */ |
|
2116238…
|
drh
|
32 |
#define TH1_SIGN 0x80000000 |
|
2116238…
|
drh
|
33 |
|
|
2116238…
|
drh
|
34 |
/* Convert an integer into a string length. Negative values remain negative */ |
|
2116238…
|
drh
|
35 |
#define TH1_LEN(X) ((TH1_SIGN|TH1_MX_STRLEN)&(X)) |
|
2116238…
|
drh
|
36 |
|
|
2116238…
|
drh
|
37 |
/* Return true if the string is tainted */ |
|
2116238…
|
drh
|
38 |
#define TH1_TAINTED(X) (((X)&TH1_TAINT_BIT)!=0) |
|
2116238…
|
drh
|
39 |
|
|
2116238…
|
drh
|
40 |
/* Remove taint from a string */ |
|
2116238…
|
drh
|
41 |
#define TH1_RM_TAINT(X) ((X)&~TH1_TAINT_BIT) |
|
2116238…
|
drh
|
42 |
|
|
2116238…
|
drh
|
43 |
/* Add taint to a string */ |
|
2116238…
|
drh
|
44 |
#define TH1_ADD_TAINT(X) ((X)|TH1_TAINT_BIT) |
|
2116238…
|
drh
|
45 |
|
|
2116238…
|
drh
|
46 |
/* If B is tainted, make A tainted too */ |
|
2116238…
|
drh
|
47 |
#define TH1_XFER_TAINT(A,B) (A)|=(TH1_TAINT_BIT&(B)) |
|
2116238…
|
drh
|
48 |
|
|
2116238…
|
drh
|
49 |
/* Check to see if a string is too big for TH1 */ |
|
2116238…
|
drh
|
50 |
#define TH1_SIZECHECK(N) if((N)>TH1_MX_STRLEN){Th_OversizeString();} |
|
2116238…
|
drh
|
51 |
void Th_OversizeString(void); |
|
588bb7c…
|
aku
|
52 |
|
|
588bb7c…
|
aku
|
53 |
/* |
|
588bb7c…
|
aku
|
54 |
** Before creating an interpreter, the application must allocate and |
|
588bb7c…
|
aku
|
55 |
** populate an instance of the following structure. It must remain valid |
|
588bb7c…
|
aku
|
56 |
** for the lifetime of the interpreter. |
|
588bb7c…
|
aku
|
57 |
*/ |
|
4321595…
|
mistachkin
|
58 |
typedef struct Th_Vtab Th_Vtab; |
|
588bb7c…
|
aku
|
59 |
struct Th_Vtab { |
|
588bb7c…
|
aku
|
60 |
void *(*xMalloc)(unsigned int); |
|
588bb7c…
|
aku
|
61 |
void (*xFree)(void *); |
|
588bb7c…
|
aku
|
62 |
}; |
|
588bb7c…
|
aku
|
63 |
|
|
588bb7c…
|
aku
|
64 |
/* |
|
588bb7c…
|
aku
|
65 |
** Opaque handle for interpeter. |
|
588bb7c…
|
aku
|
66 |
*/ |
|
588bb7c…
|
aku
|
67 |
typedef struct Th_Interp Th_Interp; |
|
588bb7c…
|
aku
|
68 |
|
|
1ebe4b0…
|
mistachkin
|
69 |
/* |
|
1ebe4b0…
|
mistachkin
|
70 |
** Create and delete interpreters. |
|
588bb7c…
|
aku
|
71 |
*/ |
|
999e33c…
|
mistachkin
|
72 |
Th_Interp * Th_CreateInterp(Th_Vtab *); |
|
588bb7c…
|
aku
|
73 |
void Th_DeleteInterp(Th_Interp *); |
|
2116238…
|
drh
|
74 |
|
|
2116238…
|
drh
|
75 |
/* |
|
2116238…
|
drh
|
76 |
** Report taint in the string zStr,nStr. That string represents "zTitle" |
|
2116238…
|
drh
|
77 |
** If non-zero is returned error out of the caller. |
|
2116238…
|
drh
|
78 |
*/ |
|
2116238…
|
drh
|
79 |
int Th_ReportTaint(Th_Interp*,const char*,const char*zStr,int nStr); |
|
1ebe4b0…
|
mistachkin
|
80 |
|
|
1ebe4b0…
|
mistachkin
|
81 |
/* |
|
588bb7c…
|
aku
|
82 |
** Evaluate an TH program in the stack frame identified by parameter |
|
588bb7c…
|
aku
|
83 |
** iFrame, according to the following rules: |
|
588bb7c…
|
aku
|
84 |
** |
|
588bb7c…
|
aku
|
85 |
** * If iFrame is 0, this means the current frame. |
|
588bb7c…
|
aku
|
86 |
** |
|
1ebe4b0…
|
mistachkin
|
87 |
** * If iFrame is negative, then the nth frame up the stack, where n is |
|
588bb7c…
|
aku
|
88 |
** the absolute value of iFrame. A value of -1 means the calling |
|
588bb7c…
|
aku
|
89 |
** procedure. |
|
588bb7c…
|
aku
|
90 |
** |
|
588bb7c…
|
aku
|
91 |
** * If iFrame is +ve, then the nth frame from the bottom of the stack. |
|
588bb7c…
|
aku
|
92 |
** An iFrame value of 1 means the toplevel (global) frame. |
|
588bb7c…
|
aku
|
93 |
*/ |
|
0c99a15…
|
drh
|
94 |
int Th_Eval(Th_Interp *interp, int iFrame, const char *zProg, int nProg); |
|
588bb7c…
|
aku
|
95 |
|
|
588bb7c…
|
aku
|
96 |
/* |
|
1ebe4b0…
|
mistachkin
|
97 |
** Evaluate a TH expression. The result is stored in the |
|
588bb7c…
|
aku
|
98 |
** interpreter result. |
|
588bb7c…
|
aku
|
99 |
*/ |
|
0c99a15…
|
drh
|
100 |
int Th_Expr(Th_Interp *interp, const char *, int); |
|
588bb7c…
|
aku
|
101 |
|
|
1ebe4b0…
|
mistachkin
|
102 |
/* |
|
588bb7c…
|
aku
|
103 |
** Access TH variables in the current stack frame. If the variable name |
|
1ebe4b0…
|
mistachkin
|
104 |
** begins with "::", the lookup is in the top level (global) frame. |
|
588bb7c…
|
aku
|
105 |
*/ |
|
4f8c897…
|
drh
|
106 |
int Th_ExistsVar(Th_Interp *, const char *, int); |
|
ea7813d…
|
mistachkin
|
107 |
int Th_ExistsArrayVar(Th_Interp *, const char *, int); |
|
0c99a15…
|
drh
|
108 |
int Th_GetVar(Th_Interp *, const char *, int); |
|
0c99a15…
|
drh
|
109 |
int Th_SetVar(Th_Interp *, const char *, int, const char *, int); |
|
0c99a15…
|
drh
|
110 |
int Th_LinkVar(Th_Interp *, const char *, int, int, const char *, int); |
|
0c99a15…
|
drh
|
111 |
int Th_UnsetVar(Th_Interp *, const char *, int); |
|
0c99a15…
|
drh
|
112 |
|
|
0c99a15…
|
drh
|
113 |
typedef int (*Th_CommandProc)(Th_Interp *, void *, int, const char **, int *); |
|
0c99a15…
|
drh
|
114 |
|
|
1ebe4b0…
|
mistachkin
|
115 |
/* |
|
1ebe4b0…
|
mistachkin
|
116 |
** Register new commands. |
|
588bb7c…
|
aku
|
117 |
*/ |
|
588bb7c…
|
aku
|
118 |
int Th_CreateCommand( |
|
1ebe4b0…
|
mistachkin
|
119 |
Th_Interp *interp, |
|
1ebe4b0…
|
mistachkin
|
120 |
const char *zName, |
|
0c99a15…
|
drh
|
121 |
/* int (*xProc)(Th_Interp *, void *, int, const char **, int *), */ |
|
588bb7c…
|
aku
|
122 |
Th_CommandProc xProc, |
|
588bb7c…
|
aku
|
123 |
void *pContext, |
|
588bb7c…
|
aku
|
124 |
void (*xDel)(Th_Interp *, void *) |
|
588bb7c…
|
aku
|
125 |
); |
|
588bb7c…
|
aku
|
126 |
|
|
1ebe4b0…
|
mistachkin
|
127 |
/* |
|
588bb7c…
|
aku
|
128 |
** Delete or rename commands. |
|
588bb7c…
|
aku
|
129 |
*/ |
|
0c99a15…
|
drh
|
130 |
int Th_RenameCommand(Th_Interp *, const char *, int, const char *, int); |
|
588bb7c…
|
aku
|
131 |
|
|
1ebe4b0…
|
mistachkin
|
132 |
/* |
|
1ebe4b0…
|
mistachkin
|
133 |
** Push a new stack frame (local variable context) onto the interpreter |
|
1ebe4b0…
|
mistachkin
|
134 |
** stack, call the function supplied as parameter xCall with the two |
|
1ebe4b0…
|
mistachkin
|
135 |
** context arguments, |
|
588bb7c…
|
aku
|
136 |
** |
|
588bb7c…
|
aku
|
137 |
** xCall(interp, pContext1, pContext2) |
|
588bb7c…
|
aku
|
138 |
** |
|
588bb7c…
|
aku
|
139 |
** , then pop the frame off of the interpreter stack. The value returned |
|
588bb7c…
|
aku
|
140 |
** by the xCall() function is returned as the result of this function. |
|
588bb7c…
|
aku
|
141 |
** |
|
588bb7c…
|
aku
|
142 |
** This is intended for use by the implementation of commands such as |
|
588bb7c…
|
aku
|
143 |
** those created by [proc]. |
|
588bb7c…
|
aku
|
144 |
*/ |
|
588bb7c…
|
aku
|
145 |
int Th_InFrame(Th_Interp *interp, |
|
588bb7c…
|
aku
|
146 |
int (*xCall)(Th_Interp *, void *pContext1, void *pContext2), |
|
588bb7c…
|
aku
|
147 |
void *pContext1, |
|
588bb7c…
|
aku
|
148 |
void *pContext2 |
|
588bb7c…
|
aku
|
149 |
); |
|
588bb7c…
|
aku
|
150 |
|
|
1ebe4b0…
|
mistachkin
|
151 |
/* |
|
588bb7c…
|
aku
|
152 |
** Valid return codes for xProc callbacks. |
|
588bb7c…
|
aku
|
153 |
*/ |
|
588bb7c…
|
aku
|
154 |
#define TH_OK 0 |
|
588bb7c…
|
aku
|
155 |
#define TH_ERROR 1 |
|
588bb7c…
|
aku
|
156 |
#define TH_BREAK 2 |
|
588bb7c…
|
aku
|
157 |
#define TH_RETURN 3 |
|
588bb7c…
|
aku
|
158 |
#define TH_CONTINUE 4 |
|
36f0e47…
|
mistachkin
|
159 |
#define TH_RETURN2 5 |
|
588bb7c…
|
aku
|
160 |
|
|
1ebe4b0…
|
mistachkin
|
161 |
/* |
|
1ebe4b0…
|
mistachkin
|
162 |
** Set and get the interpreter result. |
|
588bb7c…
|
aku
|
163 |
*/ |
|
0c99a15…
|
drh
|
164 |
int Th_SetResult(Th_Interp *, const char *, int); |
|
0c99a15…
|
drh
|
165 |
const char *Th_GetResult(Th_Interp *, int *); |
|
0c99a15…
|
drh
|
166 |
char *Th_TakeResult(Th_Interp *, int *); |
|
588bb7c…
|
aku
|
167 |
|
|
588bb7c…
|
aku
|
168 |
/* |
|
588bb7c…
|
aku
|
169 |
** Set an error message as the interpreter result. This also |
|
588bb7c…
|
aku
|
170 |
** sets the global stack-trace variable $::th_stack_trace. |
|
588bb7c…
|
aku
|
171 |
*/ |
|
0c99a15…
|
drh
|
172 |
int Th_ErrorMessage(Th_Interp *, const char *, const char *, int); |
|
588bb7c…
|
aku
|
173 |
|
|
1ebe4b0…
|
mistachkin
|
174 |
/* |
|
588bb7c…
|
aku
|
175 |
** Access the memory management functions associated with the specified |
|
588bb7c…
|
aku
|
176 |
** interpreter. |
|
588bb7c…
|
aku
|
177 |
*/ |
|
999e33c…
|
mistachkin
|
178 |
#if defined(TH_MEMDEBUG) |
|
999e33c…
|
mistachkin
|
179 |
void *Th_DbgMalloc(Th_Interp *, int); |
|
999e33c…
|
mistachkin
|
180 |
void Th_DbgFree(Th_Interp *, void *); |
|
999e33c…
|
mistachkin
|
181 |
#endif |
|
999e33c…
|
mistachkin
|
182 |
|
|
9b4f2d8…
|
drh
|
183 |
void *fossil_malloc_zero(size_t); |
|
999e33c…
|
mistachkin
|
184 |
void *fossil_realloc(void *, size_t); |
|
999e33c…
|
mistachkin
|
185 |
void fossil_free(void *); |
|
999e33c…
|
mistachkin
|
186 |
|
|
999e33c…
|
mistachkin
|
187 |
#define Th_SysMalloc(I,N) fossil_malloc_zero((N)) |
|
999e33c…
|
mistachkin
|
188 |
#define Th_SysRealloc(I,P,N) fossil_realloc((P),(N)) |
|
999e33c…
|
mistachkin
|
189 |
#define Th_SysFree(I,P) fossil_free((P)) |
|
999e33c…
|
mistachkin
|
190 |
|
|
999e33c…
|
mistachkin
|
191 |
#if defined(TH_MEMDEBUG) |
|
999e33c…
|
mistachkin
|
192 |
# define Th_Malloc(I,N) Th_DbgMalloc((I),(N)) |
|
999e33c…
|
mistachkin
|
193 |
# define Th_Free(I,P) Th_DbgFree((I),(P)) |
|
999e33c…
|
mistachkin
|
194 |
#else |
|
999e33c…
|
mistachkin
|
195 |
# define Th_Malloc(I,N) Th_SysMalloc((I),(N)) |
|
999e33c…
|
mistachkin
|
196 |
# define Th_Realloc(I,P,N) Th_SysRealloc((I),(P),(N)) |
|
999e33c…
|
mistachkin
|
197 |
# define Th_Free(I,P) Th_SysFree((I),(P)) |
|
999e33c…
|
mistachkin
|
198 |
#endif |
|
588bb7c…
|
aku
|
199 |
|
|
1ebe4b0…
|
mistachkin
|
200 |
/* |
|
588bb7c…
|
aku
|
201 |
** Functions for handling TH lists. |
|
588bb7c…
|
aku
|
202 |
*/ |
|
0c99a15…
|
drh
|
203 |
int Th_ListAppend(Th_Interp *, char **, int *, const char *, int); |
|
0c99a15…
|
drh
|
204 |
int Th_SplitList(Th_Interp *, const char *, int, char ***, int **, int *); |
|
588bb7c…
|
aku
|
205 |
|
|
0c99a15…
|
drh
|
206 |
int Th_StringAppend(Th_Interp *, char **, int *, const char *, int); |
|
588bb7c…
|
aku
|
207 |
|
|
1ebe4b0…
|
mistachkin
|
208 |
/* |
|
588bb7c…
|
aku
|
209 |
** Functions for handling numbers and pointers. |
|
588bb7c…
|
aku
|
210 |
*/ |
|
0c99a15…
|
drh
|
211 |
int Th_ToInt(Th_Interp *, const char *, int, int *); |
|
0c99a15…
|
drh
|
212 |
int Th_ToDouble(Th_Interp *, const char *, int, double *); |
|
588bb7c…
|
aku
|
213 |
int Th_SetResultInt(Th_Interp *, int); |
|
588bb7c…
|
aku
|
214 |
int Th_SetResultDouble(Th_Interp *, double); |
|
810e6c1…
|
mistachkin
|
215 |
|
|
810e6c1…
|
mistachkin
|
216 |
/* |
|
810e6c1…
|
mistachkin
|
217 |
** Functions for handling command and variable introspection. |
|
810e6c1…
|
mistachkin
|
218 |
*/ |
|
810e6c1…
|
mistachkin
|
219 |
int Th_ListAppendCommands(Th_Interp *, char **, int *); |
|
810e6c1…
|
mistachkin
|
220 |
int Th_ListAppendVariables(Th_Interp *, char **, int *); |
|
ea7813d…
|
mistachkin
|
221 |
int Th_ListAppendArray(Th_Interp *, const char *, int, char **, int *); |
|
99901fb…
|
jan.nijtmans
|
222 |
|
|
99901fb…
|
jan.nijtmans
|
223 |
/* |
|
588bb7c…
|
aku
|
224 |
** Drop in replacements for the corresponding standard library functions. |
|
588bb7c…
|
aku
|
225 |
*/ |
|
0c99a15…
|
drh
|
226 |
int th_strlen(const char *); |
|
0c99a15…
|
drh
|
227 |
int th_isdigit(char); |
|
0c99a15…
|
drh
|
228 |
int th_isspace(char); |
|
0c99a15…
|
drh
|
229 |
int th_isalnum(char); |
|
99901fb…
|
jan.nijtmans
|
230 |
int th_isalpha(char); |
|
0c99a15…
|
drh
|
231 |
int th_isspecial(char); |
|
1f6734c…
|
jan.nijtmans
|
232 |
int th_ishexdig(char); |
|
1f6734c…
|
jan.nijtmans
|
233 |
int th_isoctdig(char); |
|
1f6734c…
|
jan.nijtmans
|
234 |
int th_isbindig(char); |
|
0c99a15…
|
drh
|
235 |
char *th_strdup(Th_Interp *interp, const char *z, int n); |
|
588bb7c…
|
aku
|
236 |
|
|
588bb7c…
|
aku
|
237 |
/* |
|
588bb7c…
|
aku
|
238 |
** Interfaces to register the language extensions. |
|
588bb7c…
|
aku
|
239 |
*/ |
|
588bb7c…
|
aku
|
240 |
int th_register_language(Th_Interp *interp); /* th_lang.c */ |
|
588bb7c…
|
aku
|
241 |
int th_register_sqlite(Th_Interp *interp); /* th_sqlite.c */ |
|
588bb7c…
|
aku
|
242 |
int th_register_vfs(Th_Interp *interp); /* th_vfs.c */ |
|
588bb7c…
|
aku
|
243 |
int th_register_testvfs(Th_Interp *interp); /* th_testvfs.c */ |
|
6b58c67…
|
mistachkin
|
244 |
|
|
6b58c67…
|
mistachkin
|
245 |
#ifdef FOSSIL_ENABLE_TCL |
|
fe9990a…
|
mistachkin
|
246 |
/* |
|
fe9990a…
|
mistachkin
|
247 |
** Interfaces to the full Tcl core library from "th_tcl.c". |
|
fe9990a…
|
mistachkin
|
248 |
*/ |
|
fe9990a…
|
mistachkin
|
249 |
int th_register_tcl(Th_Interp *, void *); |
|
fe9990a…
|
mistachkin
|
250 |
int unloadTcl(Th_Interp *, void *); |
|
18fc492…
|
mistachkin
|
251 |
int evaluateTclWithEvents(Th_Interp *,void *,const char *,int,int,int,int); |
|
6b58c67…
|
mistachkin
|
252 |
#endif |
|
0c99a15…
|
drh
|
253 |
|
|
588bb7c…
|
aku
|
254 |
/* |
|
588bb7c…
|
aku
|
255 |
** General purpose hash table from th_lang.c. |
|
588bb7c…
|
aku
|
256 |
*/ |
|
588bb7c…
|
aku
|
257 |
typedef struct Th_Hash Th_Hash; |
|
588bb7c…
|
aku
|
258 |
typedef struct Th_HashEntry Th_HashEntry; |
|
588bb7c…
|
aku
|
259 |
struct Th_HashEntry { |
|
588bb7c…
|
aku
|
260 |
void *pData; |
|
0c99a15…
|
drh
|
261 |
char *zKey; |
|
588bb7c…
|
aku
|
262 |
int nKey; |
|
588bb7c…
|
aku
|
263 |
Th_HashEntry *pNext; /* Internal use only */ |
|
588bb7c…
|
aku
|
264 |
}; |
|
588bb7c…
|
aku
|
265 |
Th_Hash *Th_HashNew(Th_Interp *); |
|
588bb7c…
|
aku
|
266 |
void Th_HashDelete(Th_Interp *, Th_Hash *); |
|
1aeb272…
|
drh
|
267 |
void Th_HashIterate(Th_Interp*,Th_Hash*,int (*x)(Th_HashEntry*, void*),void*); |
|
0c99a15…
|
drh
|
268 |
Th_HashEntry *Th_HashFind(Th_Interp*, Th_Hash*, const char*, int, int); |
|
588bb7c…
|
aku
|
269 |
|
|
588bb7c…
|
aku
|
270 |
/* |
|
588bb7c…
|
aku
|
271 |
** Useful functions from th_lang.c. |
|
588bb7c…
|
aku
|
272 |
*/ |
|
588bb7c…
|
aku
|
273 |
int Th_WrongNumArgs(Th_Interp *interp, const char *zMsg); |
|
588bb7c…
|
aku
|
274 |
|
|
dce90fe…
|
jan.nijtmans
|
275 |
typedef struct Th_SubCommand {const char *zName; Th_CommandProc xProc;} Th_SubCommand; |
|
dce90fe…
|
jan.nijtmans
|
276 |
int Th_CallSubCommand(Th_Interp*,void*,int,const char**,int*,const Th_SubCommand*); |