Debian Codegraph @

Contrib/Math/Source/Math.c

raw
1 // Unicode support by Jim Park -- 08/22/2007
2
3 #include <windows.h>
4 #include <nsis/pluginapi.h> // nsis plugin
5 #include "MyMath.h"
6 #include "Math.h"
7
8 extern "C" int _fltused;
9
10 #ifdef __MINGW32__
11 int _fltused = 1;
12 #endif
13
14 #define EIPtrToInt(pEI) ( (int) (INT_PTR) (pEI) ) // Make GCC (64-bit) happy. BUGBUG64: Somebody should verify that this truncation is OK
15
16 ExpressionItem *stack;
17
18 int UserVarsCount, UserFuncsCount;
19 UserVar UserVars[MAX_USER_VARS];
20 UserFunc UserFuncs[MAX_USER_FUNCS];
21
22 void PrintTree(ExpressionItem *root, TCHAR *str);
23 void ParseString(TCHAR *&sp, ExpressionItem* &itemplace);
24 void CleanupItems(ExpressionItem* &itemplace);
25 void PlaceVariable(TCHAR *&vb, ParseInfo *pi);
26
27 void PlaceNewItem(TCHAR *&vb, ParseInfo *pi, int precedence)
28 {
29 ExpressionItem *newroot;
30 PlaceVariable(vb, pi);
31 if (pi->item == NULL) return;
32
33 while ((pi->OpsStack) && (((EIPtrToInt(pi->OpsStack->param2)) < precedence)
34 || (((EIPtrToInt(pi->OpsStack->param2)) == precedence)
35 && (precedence != OPERATOR_SET_PRECEDENCE))))
36 {
37 // second operand for our operator
38 newroot = pi->OpsStack;
39 *((ExpressionItem **)&(newroot->param2)) = pi->item;
40 pi->OpsStack = newroot->next;
41 newroot->next = NULL;
42 pi->item = newroot;
43 }
44 // finally we have got new root
45 newroot = pi->item;
46
47 if (pi->SetupNewRoot)
48 {
49 (*pi->root)->next = newroot;
50 pi->root = &((*pi->root)->next);
51 pi->SetupNewRoot = 0;
52 }
53 if (*pi->place == *pi->root) *pi->place = *pi->root = newroot;
54 else *pi->root = newroot;
55 // no item at our pockets
56 pi->item = NULL;
57 }
58
59 #define NSIS_VARS_COUNT 27
60 #define NSIS_VARS_STACK 25
61 #define NSIS_VARS_NSTACK 26
62
63 typedef TCHAR smallstr[2];
64 const smallstr NSISVariablesNames[NSIS_VARS_COUNT] = {{_T('r'),_T('0')}, {_T('r'),_T('1')}, {_T('r'),_T('2')}, {_T('r'),_T('3')}, {_T('r'),_T('4')}, {_T('r'),_T('5')}, {_T('r'),_T('6')}, {_T('r'),_T('7')}, {_T('r'),_T('8')}, {_T('r'),_T('9')},
65 {_T('R'),_T('0')}, {_T('R'),_T('1')}, {_T('R'),_T('2')}, {_T('R'),_T('3')}, {_T('R'),_T('4')}, {_T('R'),_T('5')}, {_T('R'),_T('6')}, {_T('R'),_T('7')}, {_T('R'),_T('8')}, {_T('R'),_T('9')},
66 {_T('C'),_T('L')}, {_T('I'),_T('D')}, {_T('O'),_T('D')}, {_T('E'),_T('D')}, {_T('L'),_T('G')}, {_T('S'),0}, {_T('N'),_T('S')}};
67
68 ExpressionItem *FindVariable(TCHAR *varname)
69 {
70 int i;
71
72 ExpressionItem *item = AllocItem();
73 // check NSIS variables
74 for (i = 0; i < NSIS_VARS_COUNT; i++)
75 {
76 if (lstrcmpn(varname, NSISVariablesNames[i],2) == 0)
77 {
78 if (i == NSIS_VARS_STACK) item->type = IT_VARIABLE | ITV_STACK;
79 else if (i == NSIS_VARS_NSTACK) item->type = IT_VARIABLE | ITV_NSTACK;
80 else
81 item->type = (IT_VARIABLE | ITV_NSIS) + i;
82 return item;
83 }
84 }
85 // no.. that's user variable
86 for (i = 0; i < UserVarsCount; i++)
87 {
88 if (lstrcmp(varname, UserVars[i].name) == 0)
89 {
90 // ok. we found user var expression needed
91 break;
92 }
93 }
94 if (i == UserVarsCount)
95 {
96 // new variable
97 UserVarsCount++;
98 lstrcpy(UserVars[i].name, varname);
99 UserVars[i].item = NULL;
100 }
101 item->type = (IT_VARIABLE | ITV_USER) + i;
102 return item;
103 }
104
105 void PlaceVariable(TCHAR *&vb, ParseInfo *pi)
106 {
107 if (vb <= pi->valbuf) return;
108 *vb = 0;
109 pi->item = FindVariable(pi->valbuf);
110 vb = pi->valbuf;
111 }
112
113 typedef double (*math_d2)(double, double);
114 typedef double (*math_ddp)(double, double*);
115 typedef double (*math_di)(double, int*);
116
117 #define MATHFUNCNUM 29
118 const MathFunction MathFunctions[MATHFUNCNUM] = {
119 {{_T('s'),_T('i'),_T('n')}, ITF_MATH1 >> 8, sin},
120 {{_T('s'),_T('n'),_T('h')}, ITF_MATH1 >> 8, sinh},
121 {{_T('a'),_T('s'),_T('n')}, ITF_MATH1 >> 8, asin},
122 {{_T('c'),_T('o'),_T('s')}, ITF_MATH1 >> 8, cos},
123 {{_T('c'),_T('s'),_T('h')}, ITF_MATH1 >> 8, cosh},
124 {{_T('a'),_T('c'),_T('s')}, ITF_MATH1 >> 8, acos},
125 {{_T('t'),_T('a'),_T('n')}, ITF_MATH1 >> 8, tan},
126 {{_T('t'),_T('n'),_T('h')}, ITF_MATH1 >> 8, tanh},
127 {{_T('a'),_T('t'),_T('n')}, ITF_MATH1 >> 8, atan},
128 {{_T('a'),_T('b'),_T('s')}, ITF_MATH1 >> 8, fabs},
129 {{_T('l'),_T('n'),0}, ITF_MATH1 >> 8, log},
130 {{_T('l'),_T('o'),_T('g')}, ITF_MATH1 >> 8, log10},
131 {{_T('e'),_T('x'),_T('p')}, ITF_MATH1 >> 8, exp},
132 {{_T('s'),_T('q'),_T('t')}, ITF_MATH1 >> 8, sqrt},
133 {{_T('c'),_T('e'),_T('l')}, ITF_MATH1 >> 8, ceil},
134 {{_T('f'),_T('l'),_T('r')}, ITF_MATH1 >> 8, floor},
135
136 {{_T('a'),_T('t'),_T('2')}, ITF_MATH2 >> 8, (Math1FuncPtr)(math_d2)atan2},
137 {{_T('p'),_T('o'),_T('w')}, ITF_MATH2 >> 8, (Math1FuncPtr)(math_d2)pow},
138 {{_T('f'),_T('m'),_T('d')}, ITF_MATH2 >> 8, (Math1FuncPtr)(math_d2)fmod},
139
140 // type conversions
141 {{_T('i'),0,0}, ITF_TYPE >> 8, (Math1FuncPtr)ITC_INT},
142 {{_T('s'),0,0}, ITF_TYPE >> 8, (Math1FuncPtr)ITC_STRING},
143 {{_T('f'),0,0}, ITF_TYPE >> 8, (Math1FuncPtr)ITC_FLOAT},
144 {{_T('a'),0,0}, ITF_TYPE >> 8, (Math1FuncPtr)ITC_ARRAY},
145 #define ITFT_CARRAY_ID 23
146 {{_T('c'),_T('a'),0}, ITF_TYPE >> 8, (Math1FuncPtr)ITC_ARRAY},
147 {{_T('f'),_T('f'),0}, ITF_TYPE >> 8, (Math1FuncPtr)FTT_FLOATF},
148 {{_T('l'),0,0}, ITF_TYPE >> 8, (Math1FuncPtr)FTT_LEN},
149 {{_T('c'),0,0}, ITF_TYPE >> 8, (Math1FuncPtr)FTT_CHAR},
150
151 {{_T('f'),_T('e'),_T('x')}, ITF_MATH2 >> 8, (Math1FuncPtr)(math_di)frexp},
152 {{_T('m'),_T('d'),_T('f')}, ITF_MATH2 >> 8, (Math1FuncPtr)(math_ddp)modf},
153 };
154
155 void PlaceFunction(TCHAR *&vb, TCHAR *&sp, ParseInfo *pi, int redefine)
156 {
157 int i;
158 ExpressionItem *item = pi->item = AllocItem();
159 *vb = 0;
160
161 // check BUILTIN functions
162 for (i = 0; i < MATHFUNCNUM; i++)
163 {
164 if (lstrcmpn(pi->valbuf, MathFunctions[i].name, 3) == 0)
165 {
166 item->type = IT_FUNCTION | (MathFunctions[i].type << 8) | i;
167 // get first argument
168 sp++;
169 ParseString(sp, *((ExpressionItem **)(&item->param1)));
170 if (*sp == _T(','))
171 {
172 // get second argument
173 sp++;
174 ParseString(sp, *((ExpressionItem **)(&item->param2)));
175 }
176 sp++; vb = pi->valbuf;
177 return;
178 }
179 }
180
181 // heh, may be it user function
182 for (i = 0; i < UserFuncsCount; i++)
183 {
184 if (lstrcmp(pi->valbuf, UserFuncs[i].name) == 0)
185 {
186 // Function found? Redefine option specified?
187 if (redefine) break;
188
189 item->type = IT_FUNCTION | ITF_USER | i;
190 // get arguments list
191 ExpressionItem **newplace = ((ExpressionItem **)(&pi->item->param1));
192 while (*sp != _T(')'))
193 {
194 *newplace = AllocItem();
195 (*newplace)->type = IT_EXPRESSION;
196 sp++;
197 ParseString(sp, *((ExpressionItem **)(&(*newplace)->param1)));
198 newplace = &((*newplace)->next);
199 }
200 sp++; vb = pi->valbuf;
201 return;
202 }
203 }
204
205 // oops, we need no item for function definition
206 CleanupItems(item); pi->item = NULL;
207
208 // it's user function define
209 int flags = 0;
210 TCHAR buffer[128], *buf = buffer;
211
212 // workaround for redefine flag - if the function already present,
213 // it will be cleared and redefined
214 UserFunc *f = &UserFuncs[i];
215 if (i == UserFuncsCount) UserFuncsCount++;
216 else CleanupItems(f->root);
217
218 lstrcpy(f->name, pi->valbuf);
219 f->varflags = 0;
220 f->varsnum = 0;
221 do
222 {
223 sp++;
224 switch (*sp)
225 {
226 case _T(' '):
227 break;
228 case _T(','):
229 case _T(')'):
230 if (buf > buffer)
231 {
232 *buf = 0;
233 // it should be user variable
234 ExpressionItem *it = FindVariable(buffer);
235 f->vars[f->varsnum++] = (it->type) & ITEMOPTIONS;
236 CleanupItems(it);
237 buf = buffer;
238 flags <<= 1;
239 }
240 break;
241 case _T('&'):
242 flags |= 1;
243 break;
244 default:
245 *(buf++) = *sp;
246 break;
247 }
248 }
249 while (*sp != _T(')'));
250
251 // prepare flag for fast analisys
252 for (i = 0; i < f->varsnum; i++)
253 {
254 f->varflags <<= 1;
255 flags >>= 1;
256 f->varflags |= flags&1;
257 }
258
259 // find nearest round bracket - function body
260 while (*sp != _T('(') && *sp) sp++;
261 if (!*sp)
262 return;
263 sp++;
264
265 // now we are ready to parse function body
266 ParseString(sp, f->root);
267 sp++; // closing bracket
268 vb = pi->valbuf;
269
270 #ifdef _DEBUG
271 // dump function (in debug mode)
272 TCHAR place[1024];
273 wsprintf(place, _T("function %s("), f->name);
274 flags = f->varflags;
275 for (i = 0; i < f->varsnum; i++)
276 {
277 if (flags&1) lstrcat(place, _T("&"));
278 lstrcat(place, UserVars[f->vars[i]].name);
279 if (i < f->varsnum-1) lstrcat(place, _T(", "));
280 flags >>= 1;
281 }
282 lstrcat(place, _T(")"));
283 PrintTree(f->root, place);
284 #endif
285 }
286
287 // operator options
288 #define PO_UNARYPRE 0x1 // this operator can be uniary pre (--a) for ex
289 #define PO_UNARYPOST 0x2 // this op can be uniary post (a++) (couldn't be binary)
290 #define PO_PRENONCONST 0x4 // pre argument (a = b) -> a is non const
291 #define PO_POSTNONCONST 0x8 // post argument (b--) is non const
292 #define PO_LASTOP 0x10 // op should be the last item at expression (=, -=, etc)
293 #define PO_SET 0x20 // op will set new value to one of args
294 #define PO_USESPRE 0x40 // operator will use pre operand
295 #define PO_USESPOST 0x80 // operator will use post operan
296
297 void PlaceOp(TCHAR *&vb, int type, int precedence, ParseInfo *pi)
298 {
299 PlaceVariable(vb, pi);
300 if ((type & PO_UNARYPRE) && (!pi->item))
301 {
302 // uniary pre op
303 ExpressionItem *item = AllocItem();
304 item->type = type;
305 item->param2 = (EIPARAM) (INT_PTR) precedence;
306 item->next = pi->OpsStack;
307 pi->OpsStack = item;
308 }
309 else
310 {
311 // get previous tree as items and operators of lower precedence
312 PlaceNewItem(vb, pi, precedence);
313 // post operators
314 ExpressionItem *item = AllocItem();
315 item->type = type;
316 item->param1 = (EIPARAM) (*pi->root);
317
318 if (*pi->place == *pi->root) *pi->place = *pi->root = NULL;
319 else *pi->root = NULL;
320
321 if (type & PO_UNARYPOST)
322 {
323 // uniary post op
324 pi->item = item;
325 } else
326 {
327 // binary operator
328 item->param2 = (EIPARAM) (INT_PTR) precedence;
329 item->next = pi->OpsStack;
330 pi->OpsStack = item;
331 }
332 }
333 }
334
335 #define OPSNUM 35
336 const OpStruct Operators[OPSNUM] =
337 {
338 // three byte ops
339 {{_T('>'),_T('>'),_T('=')}, 14, ITO_SHR | PO_PRENONCONST | PO_LASTOP | PO_SET | PO_USESPRE | PO_USESPOST},
340 {{_T('<'),_T('<'),_T('=')}, 14, ITO_SHL | PO_PRENONCONST | PO_LASTOP | PO_SET | PO_USESPRE | PO_USESPOST},
341
342 // two byte ops
343 // !!! don't forget to change Set Operator Precedence !!!
344 {_T("-="), 14, ITO_MINUS | PO_PRENONCONST | PO_LASTOP | PO_SET | PO_USESPRE | PO_USESPOST},
345 {_T("+="), 14, ITO_PLUS | PO_PRENONCONST | PO_LASTOP | PO_SET | PO_USESPRE | PO_USESPOST},
346 {_T("/="), 14, ITO_DIV | PO_PRENONCONST | PO_LASTOP | PO_SET | PO_USESPRE | PO_USESPOST},
347 {_T("*="), 14, ITO_MUL | PO_PRENONCONST | PO_LASTOP | PO_SET | PO_USESPRE | PO_USESPOST},
348 {_T("|="), 14, ITO_OR | PO_PRENONCONST | PO_LASTOP | PO_SET | PO_USESPRE | PO_USESPOST},
349 {_T("&="), 14, ITO_AND | PO_PRENONCONST | PO_LASTOP | PO_SET | PO_USESPRE | PO_USESPOST},
350 {_T("^="), 14, ITO_XOR | PO_PRENONCONST | PO_LASTOP | PO_SET | PO_USESPRE | PO_USESPOST},
351 {_T("%="), 14, ITO_MOD | PO_PRENONCONST | PO_LASTOP | PO_SET | PO_USESPRE | PO_USESPOST},
352 {_T("--"), 2, ITO_DEC | PO_POSTNONCONST | PO_PRENONCONST | PO_UNARYPRE | PO_UNARYPOST | PO_SET | PO_USESPRE | PO_USESPOST},
353 {_T("++"), 2, ITO_INC | PO_POSTNONCONST | PO_PRENONCONST | PO_UNARYPRE | PO_UNARYPOST | PO_SET | PO_USESPRE | PO_USESPOST},
354 {_T(">>"), 6, ITO_SHR | PO_USESPRE | PO_USESPOST},
355 {_T("<<"), 6, ITO_SHL | PO_USESPRE | PO_USESPOST},
356
357 // logical
358 {_T("&&"), 12, ITO_LAND | PO_USESPRE | PO_USESPOST},
359 {_T("||"), 13, ITO_LOR | PO_USESPRE | PO_USESPOST},
360
361 // comparisons
362 {_T("<="), 7, ITO_LE | PO_USESPRE | PO_USESPOST},
363 {_T("=<"), 7, ITO_LE | PO_USESPRE | PO_USESPOST},
364 {_T(">="), 7, ITO_GE | PO_USESPRE | PO_USESPOST},
365 {_T("=>"), 7, ITO_GE | PO_USESPRE | PO_USESPOST},
366 {_T("!="), 8, ITO_NE | PO_USESPRE | PO_USESPOST},
367 {_T("=="), 8, ITO_EQ | PO_USESPRE | PO_USESPOST},
368
369 // single byte ops
370 // !!! don't forget to change Set Operator Precedence !!!
371 {_T("="), 14, ITO_SET | PO_PRENONCONST | PO_LASTOP | PO_SET | PO_USESPOST},
372 {_T("+"), 5, ITO_PLUS | PO_USESPRE | PO_USESPOST},
373 {_T("-"), 5, ITO_MINUS | PO_USESPRE | PO_USESPOST | PO_UNARYPRE},
374 {_T("*"), 4, ITO_MUL | PO_USESPRE | PO_USESPOST | PO_UNARYPRE},
375 {_T("/"), 4, ITO_DIV | PO_USESPRE | PO_USESPOST},
376 {_T("%"), 4, ITO_MOD | PO_USESPRE | PO_USESPOST},
377 {_T("<"), 7, ITO_LS | PO_USESPRE | PO_USESPOST},
378 {_T(">"), 7, ITO_GR | PO_USESPRE | PO_USESPOST},
379 {_T("&"), 9, ITO_AND | PO_USESPRE | PO_USESPOST | PO_UNARYPRE},
380 {_T("|"), 11, ITO_OR | PO_USESPRE | PO_USESPOST},
381 {_T("^"), 10, ITO_XOR | PO_USESPRE | PO_USESPOST},
382 {_T("~"), 3, ITO_NOT | PO_USESPOST | PO_UNARYPRE},
383 {_T("!"), 3, ITO_LNOT |PO_USESPOST | PO_UNARYPRE}
384 };
385
386 void CheckForOperator(TCHAR *&sp, TCHAR *&vb, ParseInfo *pi)
387 {
388 for (int op = 0; op < OPSNUM; op++)
389 {
390 int c = lstrlen(Operators[op].name);
391 if (c > 3) c = 3; // real operator length
392 if (lstrcmpn(sp, Operators[op].name, c))
393 {
394 // wrong - different op
395 continue;
396 }
397 // that is our op
398 sp += c;
399 PlaceOp(vb, ((int) Operators[op].type) | IT_OPERATOR, Operators[op].precedence, pi);
400 break;
401 }
402 }
403
404 void ParseString(TCHAR *&sp, ExpressionItem* &itemplace)
405 {
406 ParseInfo pi = {0, NULL, NULL, &itemplace, &itemplace};
407
408 int redefine = 0;
409 TCHAR *vb = pi.valbuf;
410 // cycle until current expression end
411 while ((*sp != 0) && (*sp != _T(')')) && (*sp != _T('}')) &&
412 (*sp != _T(']')) && (*sp != _T(',')))
413 {
414 int processed = 1;
415 switch (*sp)
416 {
417 case _T(' '):
418 case _T('\t'):
419 sp++;
420 break;
421 case _T(';'):
422 // expression delimiter
423 PlaceNewItem(vb, &pi, 255);
424 if (*pi.root) pi.SetupNewRoot = 1;
425 sp++;
426 break;
427 case _T('0'): case _T('1'): case _T('2'): case _T('3'): case _T('4'):
428 case _T('5'): case _T('6'): case _T('7'): case _T('8'): case _T('9'):
429 // variable & function names could contain numbers as non first chars
430 if (vb > pi.valbuf)
431 {
432 processed = FALSE;
433 break;
434 }
435 case _T('\''): case _T('\"'): case _T('`'):
436 // constant meet
437 pi.item = AllocItem();
438 StringToItem(sp, pi.item, STI_STRING | STI_FLOAT | STI_INT);
439 break;
440
441 case _T('('): // start of function or expression
442 if (vb > pi.valbuf)
443 {
444 // thats function
445 PlaceFunction(vb, sp, &pi, redefine);
446 } else
447 {
448 // expression
449 sp++;
450 ParseString(sp, pi.item);
451 if (*sp == _T(')')) sp++;
452 }
453 redefine = 0;
454 break;
455
456 case _T('#'): // start of one of logical expressions
457 sp++;
458 if ((*sp != _T('[')) && (*sp != _T('{')))
459 {
460 // function redefine flag
461 redefine = 1;
462 break;
463 }
464 {
465 pi.item = AllocItem();
466 // IF or WHILE
467 pi.item->type = ((*sp == _T('['))?(IT_LOGIC | ITL_IF):(IT_LOGIC | ITL_WHILE));
468 // first expr - logic statement
469 sp++;
470
471 ParseString(sp, *((ExpressionItem **)(&pi.item->param1)));
472 // ok, second expr - then, third - else statement.. others???
473 ExpressionItem **newplace = ((ExpressionItem **)(&pi.item->param2));
474 while (*sp == _T(','))
475 {
476 *newplace = AllocItem();
477 (*newplace)->type = IT_EXPRESSION;
478 sp++;
479 ParseString(sp, *((ExpressionItem **)(&(*newplace)->param1)));
480 newplace = &((*newplace)->next);
481 }
482 }
483 sp++;
484 break;
485
486 case _T('['):
487 {
488 // thats array access
489 PlaceOp(vb, IT_ARRAY | ITA_ACCESS | PO_UNARYPOST, 1, &pi);
490 sp++;
491 // item index
492 ParseString(sp, *(ExpressionItem **)&(pi.item->param2));
493 if (*sp == _T(','))
494 {
495 // two indexes - string access
496 ExpressionItem *it = AllocItem();
497 it->type = IT_EXPRESSION;
498 it->param1 = (EIPARAM) *(ExpressionItem **)&(pi.item->param2);
499 *(ExpressionItem **)&(pi.item->param2) = it;
500 it = it->next = AllocItem();
501 it->type = IT_EXPRESSION;
502 sp++;
503 ParseString(sp, *((ExpressionItem **)(&it->param1)));
504 }
505 sp++;
506 }
507 break;
508
509 case _T('{'): // start of array define
510 {
511 // array define - consists of array copy operator and array define itself
512
513 // type conversion item (to create a copy of array)
514 pi.item = AllocItem();
515 pi.item->type = IT_FUNCTION | ITF_TYPE | ITFT_CARRAY_ID | ITFA_COPY;
516
517 // during first create our array descriptor and array pointers
518 ExpressionItem *ai = AllocArray(DEFAULT_ARRAY_SIZE);
519 pi.item->param1 = (EIPARAM) ai;
520 ArrayDesc *ad = *((ArrayDesc**)&(ai->param1));
521
522 // parse array initializers
523 while (*sp != _T('}'))
524 {
525 sp++;
526 ParseString(sp, ad->array[ad->count]);
527 if (ad->array[ad->count])
528 ad->count++;
529 }
530
531 sp++;
532 }
533 break;
534 case _T('-'): case _T('+'): case _T('<'): case _T('='): case _T('>'):
535 case _T('/'): case _T('*'): case _T('~'): case _T('^'): case _T('!'):
536 case _T('&'): case _T('|'): case _T('%'):
537 CheckForOperator(sp, vb, &pi);
538 break;
539
540 // non expression? ok, then it should be common char, like function or var name
541 default:
542 processed = FALSE;
543 break;
544 }
545 if (!processed) *(vb++) = *(sp++);
546 }
547 PlaceNewItem(vb, &pi, 255);
548 }
549
550 void CleanupArray(ArrayDesc *ad)
551 {
552 if (!(--(ad->references)))
553 {
554 // last array reference, we could kill it
555 // cleanup array items
556 for (int i = 0; i < ad->count; i++)
557 {
558 ExpressionItem *aritem = ad->array[i];
559 if (aritem)
560 CleanupItems(aritem);
561 }
562 // cleanup ptrs and descriptor
563 dbgGlobalFree(ad->array);
564 dbgGlobalFree(ad);
565 }
566 }
567
568 void CleanupItems(ExpressionItem* &itemplace)
569 {
570 if (itemplace == NULL) return;
571 ExpressionItem *item = itemplace, *itemnext;
572 do
573 {
574 if (((item->type & (ITEMTYPE|ITEMSUBTYPE)) == (IT_VARIABLE|ITV_ARRITEM))
575 ||
576 ((item->type & (ITEMTYPE|ITEMSUBTYPE)) == (IT_CONST|ITC_ARRAY)))
577 {
578 CleanupArray((ArrayDesc *)item->param1);
579 }
580 else
581 if ((item->type & ITEMTYPE) == IT_CONST)
582 {
583 if ((item->type & ITEMSUBTYPE) == ITC_STRING)
584 dbgGlobalFree((HGLOBAL) item->param1);
585 } else
586 {
587 CleanupItems(*((ExpressionItem**) &item->param1));
588 CleanupItems(*((ExpressionItem**) &item->param2));
589 }
590 // free the item itself
591 itemnext = item->next;
592 dbgGlobalFree((HGLOBAL) item);
593 item = itemnext;
594 } while (item != NULL);
595 itemplace = NULL;
596 }
597
598 #ifdef _DEBUG
599 HANDLE myfile;
600
601 TCHAR *opsnames[] = {_T(""), _T("-"), _T("+"), _T("<<"), _T(">>"), _T("*"), _T("/"), _T("="), _T("&&"), _T("||"), _T("++"), _T("--"),
602 _T("=<"), _T(">="), _T("!="), _T("=="), _T("<"), _T(">"), _T("&"), _T("%"), _T("|"), _T("^"), _T("~"), _T("!")};
603
604 void PrintNode(int index, int spaces, ExpressionItem* itemplace)
605 {
606 if (itemplace == NULL) return;
607
608 ExpressionItem *item = itemplace;
609 do
610 {
611 DWORD wrote;
612 TCHAR buffer[1024], *place = buffer;
613 for (int k = 0; k < spaces; k++)
614 *(place++) = 32;
615 *place = 0;
616
617 switch (item->type & ITEMTYPE)
618 {
619 case IT_EXPRESSION:
620 wsprintf(place, _T("Expression Place-Holder "));
621 break;
622 case IT_CONST:
623 switch (item->type & ITEMSUBTYPE)
624 {
625 case ITC_STRING:
626 wsprintf(place, _T("String: \"%s\""), (TCHAR *) item->param1);
627 break;
628 case ITC_INT:
629 wsprintf(place, _T("Int: "));
630 itoa64(*((__int64*)&(item->param1)), place+5);
631 break;
632 case ITC_FLOAT:
633 wsprintf(place, _T("Float: "));
634 FloatFormat(place+7, *((double*)&(item->param1)), 6);
635 break;
636 case ITC_ARRAY:
637 ArrayDesc *ad = (ArrayDesc*) item->param1;
638 wsprintf(place, _T("Array, ptr %08X, size %d, count %d, references %d"),
639 ad->array, ad->size, ad->count, ad->references);
640 break;
641 }
642 _tcscat(place, _T(" "));
643 break;
644 case IT_OPERATOR:
645 wsprintf(place, _T("Op: %s%s "), opsnames[(item->type & ITEMSUBTYPE) >> 8], (item->type & PO_SET)?(_T("(=)")):(_T("")));
646 break;
647 case IT_VARIABLE:
648 switch (item->type & ITEMSUBTYPE)
649 {
650 case ITV_NSIS:
651 {
652 TCHAR buffer[128];
653 buffer[0] = NSISVariablesNames[item->type & ITEMOPTIONS][0];
654 buffer[1] = NSISVariablesNames[item->type & ITEMOPTIONS][1];
655 buffer[2] = 0;
656 wsprintf(place, _T("Var: %s (%d) "),
657 buffer,
658 item->type & ITEMOPTIONS);
659 }
660 break;
661 case ITV_USER:
662 wsprintf(place, _T("Var: %s (%d) "), UserVars[item->type & ITEMOPTIONS].name, item->type & ITEMOPTIONS);
663 break;
664 case ITV_STACK:
665 wsprintf(place, _T("Plugin Stack "));
666 break;
667 case ITV_NSTACK:
668 wsprintf(place, _T("NSIS Stack "));
669 break;
670 }
671 break;
672 case IT_LOGIC:
673 if ((item->type & ITEMSUBTYPE) == ITL_IF)
674 wsprintf(place, _T("IF "));
675 else
676 wsprintf(place, _T("WHILE "));
677 break;
678 case IT_FUNCTION:
679 if (((item->type & ITEMSUBTYPE) == ITF_MATH1) ||
680 ((item->type & ITEMSUBTYPE) == ITF_MATH2) ||
681 ((item->type & ITEMSUBTYPE) == ITF_TYPE))
682 {
683 TCHAR buffer[128];
684 buffer[0] = (MathFunctions[item->type &ITEMOPTIONS].name)[0];
685 buffer[1] = (MathFunctions[item->type &ITEMOPTIONS].name)[1];
686 buffer[2] = (MathFunctions[item->type &ITEMOPTIONS].name)[2];
687 buffer[3] = 0;
688 wsprintf(place, _T("Built-In Function %s() [%d] "),
689 buffer,
690 item->type &ITEMOPTIONS);
691 }
692 else
693 {
694 UserFunc *f = &(UserFuncs[item->type & ITEMOPTIONS]);
695 wsprintf(place, _T("User Function: %s("), f->name);
696 int flags = f->varflags;
697 for (int i = 0; i < f->varsnum; i++)
698 {
699 if (flags&1) lstrcat(place, _T("&"));
700 lstrcat(place, UserVars[f->vars[i]].name);
701 if (i < f->varsnum-1) lstrcat(place, _T(", "));
702 flags >>= 1;
703 }
704 lstrcat(place, _T(") "));
705 }
706 break;
707 case IT_ARRAY:
708 wsprintf(place, _T("Array access [] "));
709 break;
710 }
711 place += lstrlen(place);
712 wsprintf(place, _T("Addr: %08X Type: %08X Next: %08X Param1: %08X Param2: %08X"), item, item->type, item->next, item->param1, item->param2);
713 lstrcat(place, _T("\n"));
714 WriteFile(myfile, buffer, lstrlen(buffer), &wrote, NULL);
715 if (((item->type & ITEMTYPE) != IT_CONST) && ((item->type & (ITEMTYPE|ITEMSUBTYPE)) != (IT_VARIABLE|ITV_ARRITEM)))
716 {
717 place = buffer;
718 for (int k = 0; k < spaces+2; k++)
719 *(place++) = 32;
720 int show = 0;
721 if (((item->param1 != NULL) && ((*((ExpressionItem**) &item->param1))->next != NULL)) ||
722 ((item->param2 != NULL) && ((*((ExpressionItem**) &item->param2))->next != NULL)))
723 show = 1;
724 if (show)
725 {
726 wsprintf(place, _T("Sub1:\n"));
727 WriteFile(myfile, buffer, lstrlen(buffer), &wrote, NULL);
728 }
729 PrintNode(1, spaces + 4, *((ExpressionItem**) &item->param1));
730 if (show)
731 {
732 wsprintf(place, _T("Sub2:\n"));
733 WriteFile(myfile, buffer, lstrlen(buffer), &wrote, NULL);
734 }
735 PrintNode(2, spaces + 4, *((ExpressionItem**) &item->param2));
736 } else if ((item->type & (ITEMSUBTYPE|ITEMTYPE)) == (ITC_ARRAY|IT_CONST))
737 {
738 ArrayDesc *ad = (ArrayDesc *) item->param1;
739 for (int i = 0; i < ad->count; i++)
740 {
741 ExpressionItem *aritem = ad->array[i];
742 if (aritem)
743 PrintNode(2, spaces + 4, aritem);
744 }
745 }
746 item = item->next;
747 } while (item != NULL);
748 }
749
750 void PrintTree(ExpressionItem *root, TCHAR *str)
751 {
752 myfile = CreateFile(_T("d:\\math.debug"), GENERIC_ALL, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, 0);
753 SetFilePointer(myfile, 0, NULL, FILE_END);
754 TCHAR buffer[1024]; DWORD wrote;
755 wsprintf(buffer, _T("New tree for \'%s\'\n"), str);
756 WriteFile(myfile, buffer, lstrlen(buffer), &wrote, NULL);
757
758 PrintNode(0, 4, root);
759 CloseHandle(myfile);
760 myfile = NULL;
761 }
762 #endif
763
764 void CopyArray(ExpressionItem *&item)
765 {
766 if (item == NULL) return;
767 // especial case - array to array conversion is requested array copy
768 ExpressionItem *olditem = item;
769 ArrayDesc *oad = (ArrayDesc *) (olditem->param1);
770 // Initialize the array of the same size
771 item = AllocArray(oad->size);
772 ArrayDesc *nad = (ArrayDesc *) (item->param1);
773 nad->count = oad->count;
774 // copy items
775 for (int i = 0; i < oad->count; i++)
776 nad->array[i] = CopyItem(oad->array[i], TRUE);
777 // cleanup old array pointer (may be array itself)
778 CleanupItems(olditem);
779 }
780
781 void ItemToType(ExpressionItem* &item, int type)
782 {
783 TCHAR *buffer, *bp;
784 if (item == NULL) return;
785 int itemt = item->type & ITEMTYPE, oldtype = item->type & ITEMSUBTYPE;
786
787 if (((itemt == IT_CONST) && (oldtype == type))
788 || (itemt != IT_CONST)) return;
789
790 switch (type)
791 {
792 case ITC_STRING:
793 buffer = AllocString();
794 ItemToString(buffer, item);
795 item->param1 = (EIPARAM) buffer;
796 item->param2 = 0;
797 break;
798 case ITC_FLOAT:
799 if (oldtype == ITC_INT)
800 *((double *)&(item->param1)) = (double) *((__int64 *)&(item->param1));
801 else
802 {
803 bp = buffer = (TCHAR*) item->param1;
804 StringToItem(buffer, item, STI_FLOAT);
805 dbgGlobalFree(bp);
806 }
807 break;
808 case ITC_INT:
809 if (oldtype == ITC_FLOAT)
810 *((__int64 *)&(item->param1)) = (__int64) *((double *)&(item->param1));
811 else
812 {
813 bp = buffer = (TCHAR*) item->param1;
814 StringToItem(buffer, item, STI_INT);
815 dbgGlobalFree(bp);
816 }
817 break;
818 case ITC_ARRAY:
819 if (oldtype == ITC_STRING)
820 {
821 TCHAR *buf = (TCHAR*) item->param1;
822 int len = lstrlen(buf)+1;
823 ExpressionItem *ni = AllocArray(lstrlen(buf)+1);
824 ArrayDesc *ad = (ArrayDesc*) ni->param1;
825 for (int i = 0; i < len; i++)
826 {
827 ad->array[i] = AllocItem();
828 *((__int64 *) &(ad->array[i]->param1)) = (__int64) buf[i];
829 }
830 ad->count = len;
831 CleanupItems(item);
832 item = ni;
833 }
834 break;
835 }
836 item->type = IT_CONST | type;
837 }
838
839 void SaveResult(ExpressionItem *var, ExpressionItem *result)
840 {
841 if ((var->type & ITEMTYPE) != IT_VARIABLE) return;
842
843 // result should be stored at variable to
844 int varindex = var->type & ITEMOPTIONS;
845 switch (var->type & ITEMSUBTYPE)
846 {
847 case ITV_NSIS:
848 {
849 // store string result direct to NSIS variable
850 TCHAR *ptr = g_variables + varindex*g_stringsize;
851 ItemToString(ptr, result);
852 }
853 break;
854 case ITV_USER:
855 {
856 CleanupItems(UserVars[varindex].item);
857 UserVars[varindex].item = CopyItem(result);
858 }
859 break;
860 case ITV_ARRITEM:
861 {
862 ExpressionItem *&ei = ((ArrayDesc*)(var->param1))->array[(UINT_PTR)(var->param2)];
863 CleanupItems(ei);
864 ei = CopyItem(result);
865 }
866 break;
867 case ITV_STACK:
868 {
869 ExpressionItem *newitem = CopyItem(result);
870 newitem->next = stack;
871 stack = newitem;
872 }
873 break;
874 case ITV_NSTACK:
875 {
876 TCHAR *buf = AllocString();
877 ItemToString(buf, result);
878 pushstring(buf);
879 dbgGlobalFree(buf);
880 }
881 break;
882 }
883 }
884
885 void RunAndGetConst(ExpressionItem* from, ExpressionItem* &result, int type)
886 {
887 RunTree(from, result, type | RTO_NEEDCONST);
888 ItemToType(result, type);
889 }
890
891 void RunTree(ExpressionItem *from, ExpressionItem* &result, int options)
892 {
893 ExpressionItem *item = from;
894 result = NULL;
895 while (item != NULL)
896 {
897 CleanupItems(result);
898 int type = item->type & ITEMTYPE,
899 subtype = item->type & ITEMSUBTYPE,
900 ioptions = item->type & ITEMOPTIONS;
901 switch (type)
902 {
903 case IT_EXPRESSION:
904 RunTree(*((ExpressionItem**)&(item->param1)), result, options);
905 break;
906 case IT_CONST:
907 result = CopyItem(item);
908 break;
909 case IT_VARIABLE:
910 if (options & RTO_NEEDCONST)
911 {
912 // we need const result - :) is it nsis or common variable
913 switch (subtype)
914 {
915 case ITV_NSIS:
916 {
917 // nsis
918 result = AllocItem();
919 TCHAR *variable = getuservariable(ioptions);
920 StringToItem(variable, result, options);
921 }
922 break;
923 case ITV_USER:
924 {
925 // usual variable
926 if (UserVars[ioptions].item)
927 result = CopyItem(UserVars[ioptions].item);
928 else
929 result = AllocItem();
930 }
931 break;
932 case ITV_ARRITEM:
933 {
934 // array item
935 ExpressionItem *ei = ((ArrayDesc*)(item->param1))->array[(UINT_PTR)(item->param2)];
936 if (ei)
937 result = CopyItem(ei);
938 else
939 result = AllocItem();
940 }
941 break;
942 case ITV_STACK:
943 {
944 // pop from plugin stack
945 result = stack;
946 if (result == NULL) result = AllocItem();
947 stack = result->next;
948 result->next = NULL;
949 }
950 break;
951 case ITV_NSTACK:
952 {
953 // NSIS stack
954 TCHAR buffer[1024], *buf = buffer;
955 result = AllocItem();
956 popstring(buffer);
957 StringToItem(buf, result, options);
958 }
959 break;
960 }
961 }
962 else
963 // if we don't need const - we will just pass variable record
964 result = CopyItem(item);
965 break;
966 case IT_OPERATOR:
967 {
968 ExpressionItem *var = NULL, *item1 = NULL, *item2 = NULL;
969 // prepare arguments in case of SET operator
970 if (ioptions & PO_SET)
971 {
972 if ((item->param1) && (ioptions & PO_PRENONCONST))
973 {
974 RunTree(*((ExpressionItem**)&(item->param1)), var, 0);
975 if (ioptions & PO_USESPRE)
976 RunTree(var, item1, RTO_NEEDCONST | STI_INT | STI_FLOAT | STI_STRING);
977 } else
978 if ((item->param2) && (ioptions & PO_POSTNONCONST))
979 {
980 RunTree(*((ExpressionItem**)&(item->param2)), var, 0);
981 if (ioptions & PO_USESPOST)
982 RunTree(var, item2, RTO_NEEDCONST | STI_INT | STI_FLOAT | STI_STRING);
983 }
984 }
985
986 // prepare arguments in case of any operator
987 int needmore = 1;
988 if ((!item1) && (item->param1) && (ioptions & PO_USESPRE))
989 {
990 RunTree(*((ExpressionItem**)&(item->param1)), item1, RTO_NEEDCONST | STI_INT | STI_FLOAT | STI_STRING);
991 // logical expressions && and || can make decision on first arg basis
992 if ((subtype == ITO_LAND) || (subtype == ITO_LOR) )
993 {
994 ItemToType(item1, ITC_INT);
995 int res = (int) *((__int64*) &(item1->param1));
996 if (((res)&&(subtype==ITO_LOR)) || ((!res)&&(subtype==ITO_LAND)))
997 needmore = 0;
998 }
999 }
1000
1001 // get-reference operator
1002 if ((!item1) && (subtype == ITO_AND) && (!item2) && (item->param2))
1003 {
1004 RunTree(*((ExpressionItem**)&(item->param2)), result, 0);
1005 break;
1006 }
1007
1008 if ((needmore) && (!item2) && (item->param2) && (ioptions & PO_USESPOST))
1009 RunTree(*((ExpressionItem**)&(item->param2)), item2, RTO_NEEDCONST | STI_INT | STI_FLOAT | STI_STRING);
1010
1011 // reference operator
1012 if ((!item1) && (subtype == ITO_MUL) && ((item2->type & ITEMTYPE) == (IT_VARIABLE)))
1013 {
1014 // ok, that's the result we need
1015 if (options & RTO_NEEDCONST)
1016 {
1017 RunTree(item2, result, options);
1018 CleanupItems(item2);
1019 } else
1020 result = item2;
1021 break;
1022 }
1023
1024 __int64 i1=0, i2=0, i3=0, i4=0;
1025 if (((!item1)||((item1->type & ITEMTYPE)==IT_CONST)) &&
1026 ((!item2)||((item2->type & ITEMTYPE)==IT_CONST)))
1027 {
1028
1029 // find the best type match for operation
1030 int it1 = (item1 && (ioptions & PO_USESPRE))?(item1->type & ITEMSUBTYPE):(ITC_UNKNOWN),
1031 it2 = (item2 && (ioptions & PO_USESPOST))?(item2->type & ITEMSUBTYPE):(ITC_UNKNOWN),
1032 type = (it1 < it2)?(it1):(it2);
1033
1034 // convert operands to desired type
1035 ItemToType(item1, type);
1036 ItemToType(item2, type);
1037
1038 switch (type)
1039 {
1040 case ITC_INT:
1041 {
1042 i1 = (item1)?(*((__int64*)&item1->param1)):(0);
1043 i2 = (item2)?(*((__int64*)&item2->param1)):(0);
1044
1045 switch (subtype)
1046 {
1047 case ITO_MINUS: i1 -= i2; break; // unary minus auto handled with NULL
1048 case ITO_PLUS: i1 += i2; break;
1049 case ITO_SHL: i1 <<= i2; break;
1050 case ITO_SHR: i1 >>= i2; break;
1051 case ITO_MUL: i1 *= i2; break;
1052 case ITO_MOD:
1053 case ITO_DIV:
1054 if (i2 == 0) { i3 = 0; i4 = i1; }
1055 else { i3 = i1 / i2; i4 = i1 % i2; }
1056 if (subtype == ITO_DIV) i1 = i3; else i1 = i4;
1057 break;
1058 case ITO_SET: i1 = i2; break;
1059 case ITO_LE: i1 = (i1 <= i2); break;
1060 case ITO_GE: i1 = (i1 >= i2); break;
1061 case ITO_NE: i1 = (i1 != i2); break;
1062 case ITO_EQ: i1 = (i1 == i2); break;
1063 case ITO_LS: i1 = (i1 < i2); break;
1064 case ITO_GR: i1 = (i1 > i2); break;
1065 case ITO_AND: i1 = (i1 & i2); break;
1066 case ITO_OR: i1 = (i1 | i2); break;
1067 case ITO_XOR: i1 = (i1 ^ i2); break;
1068 case ITO_NOT: i1 = ~i2; break;
1069 case ITO_LNOT: i1 = !i2; break;
1070 case ITO_LAND: i1 = i1 && i2; break;
1071 case ITO_LOR: i1 = i1 || i2; break;
1072 case ITO_INC:
1073 if (item1) i2 = i1++;
1074 else i1 = ++i2;
1075 break;
1076 case ITO_DEC:
1077 if (item1) i2 = i1--;
1078 else i1 = --i2;
1079 break;
1080 }
1081 result = AllocItem();
1082 *((__int64*)&result->param1) = i1;
1083 }
1084 break;
1085 case ITC_FLOAT:
1086 {
1087 int ir = -666;
1088 double i1 = (item1)?(*((double*)&item1->param1)):(0.0);
1089 double i2 = (item2)?(*((double*)&item2->param1)):(0.0);
1090
1091 switch (subtype)
1092 {
1093 case ITO_MINUS: i1 -= i2; break; // unary minus auto handled with NULL
1094 case ITO_PLUS: i1 += i2; break;
1095 case ITO_MUL: i1 *= i2; break;
1096 case ITO_DIV: i1 /= i2; break;
1097 case ITO_SET: i1 = i2; break;
1098 case ITO_LE: ir = (i1 <= i2); break;
1099 case ITO_GE: ir = (i1 >= i2); break;
1100 case ITO_NE: ir = (i1 != i2); break;
1101 case ITO_EQ: ir = (i1 == i2); break;
1102 case ITO_LS: ir = (i1 < i2); break;
1103 case ITO_GR: ir = (i1 > i2); break;
1104 }
1105 result = AllocItem();
1106 if (ir == -666)
1107 {
1108 // if ir value left intact - result is double
1109 result->type = IT_CONST | ITC_FLOAT;
1110 *((double*)&result->param1) = i1;
1111 } else
1112 *((__int64*)&result->param1) = (__int64) ir;
1113 }
1114 break;
1115 case ITC_STRING:
1116 {
1117 int ir = -666;
1118 TCHAR *i1 = (item1)?((TCHAR*)item1->param1):(NULL);
1119 TCHAR *i2 = (item2)?((TCHAR*)item2->param1):(NULL);
1120 int sc = (i1 && i2)?(lstrcmp(i1, i2)):((i1)?(1):((i2)?(-1):(0)));
1121
1122 switch (subtype)
1123 {
1124 case ITO_PLUS: lstrcat(i1, i2); break;
1125 case ITO_SET: i1 = i2; break;
1126 case ITO_LE: ir = (sc <= 0); break;
1127 case ITO_GE: ir = (sc >= 0); break;
1128 case ITO_NE: ir = (sc != 0); break;
1129 case ITO_EQ: ir = (sc == 0); break;
1130 case ITO_LS: ir = (sc < 0); break;
1131 case ITO_GR: ir = (sc > 0); break;
1132 }
1133 if (ir == -666)
1134 {
1135 result = CopyItem((item1)?(item1):(item2));
1136 } else
1137 {
1138 result = AllocItem();
1139 *((__int64*)&result->param1) = (__int64) ir;
1140 }
1141 }
1142 break;
1143 case ITC_ARRAY:
1144 result = CopyItem(item2);
1145 break;
1146 }
1147
1148 } // check for both items constant
1149 // the other case - usually UniaryPre operators working with non constants
1150 else result = CopyItem(item2);
1151
1152 if (ioptions & PO_SET)
1153 {
1154 // Save our result in output variable
1155 SaveResult(var, result);
1156 }
1157
1158 // Actual value to be returned as result is at i2 for ++ and -- ops
1159 if ((subtype == ITO_DEC) || (subtype == ITO_INC))
1160 *((__int64*)&result->param1) = i2;
1161
1162 CleanupItems(item1); CleanupItems(item2); CleanupItems(var);
1163 }
1164 break;
1165 case IT_LOGIC:
1166 {
1167 int ifmode = (subtype == ITL_IF);
1168 ExpressionItem *ifbr = *((ExpressionItem**)&(item->param1)),
1169 *dobr = *((ExpressionItem**)&(item->param2)),
1170 *thbr = NULL, *elbr = NULL;
1171 // check do branche for existence
1172 if (dobr && ifmode)
1173 {
1174 // then...
1175 thbr = *((ExpressionItem**)&(dobr->param1));
1176 // ... and else branches
1177 if (dobr->next) elbr = *((ExpressionItem**)&(dobr->next->param1));
1178 }
1179 while (true)
1180 {
1181 RunAndGetConst((ifbr), result, ITC_INT);
1182 if (ifmode)
1183 {
1184 // we need then or else branch?
1185 if ((*((__int64*)&result->param1))) dobr = thbr;
1186 else dobr = elbr;
1187 } else
1188 {
1189 // while mode
1190 if (!(*((__int64*)&result->param1))) break;
1191 }
1192 // ok, run the approtiate branch of if statement (if available)
1193 if (dobr)
1194 {
1195 CleanupItems(result);
1196 RunTree(dobr, result, options);
1197 }
1198 if (ifmode) break;
1199 CleanupItems(result);
1200 }
1201 }
1202 break;
1203 case IT_FUNCTION:
1204 if (subtype == ITF_USER)
1205 {
1206 int i;
1207 UserFunc *f = &(UserFuncs[ioptions]);
1208 int flags = f->varflags;
1209 ExpressionItem *ip = *((ExpressionItem**)&(item->param1));
1210 ExpressionItem *si = AllocItem(), *var = AllocItem();
1211 ExpressionItem *vals[32];
1212 si->type = IT_VARIABLE | ITV_STACK;
1213 for (i = 0; i < f->varsnum; i++)
1214 {
1215 // push every variable
1216 ExpressionItem *val;
1217 var->type = (IT_VARIABLE | ITV_USER) + f->vars[i];
1218 RunTree(var, val, RTO_NEEDCONST | ITC_STRING | ITC_INT | ITC_FLOAT | ITC_ARRAY);
1219 SaveResult(si, val);
1220 CleanupItems(val);
1221 // calculate argument value and for future
1222 if (ip)
1223 {
1224 if (flags&1)
1225 {
1226 // var ptr required
1227 RunTree(*((ExpressionItem**)&(ip->param1)), vals[i], 0);
1228 } else
1229 {
1230 RunTree(*((ExpressionItem**)&(ip->param1)), vals[i], RTO_NEEDCONST | ITC_STRING | ITC_INT | ITC_FLOAT | ITC_ARRAY);
1231 }
1232 ip = ip->next;
1233 } else vals[i] = AllocItem();
1234
1235 flags >>= 1;
1236 }
1237
1238 // now when all values got we could save them to variables
1239 for (i = 0; i < f->varsnum; i++)
1240 {
1241 var->type = (IT_VARIABLE | ITV_USER) + f->vars[i];
1242 SaveResult(var, vals[i]);
1243 CleanupItems(vals[i]);
1244 }
1245
1246
1247 // ok, call the func
1248 RunTree(f->root, result, RTO_NEEDCONST | ITC_STRING | ITC_INT | ITC_FLOAT | ITC_ARRAY);
1249
1250 // pop original params
1251 for (i = f->varsnum-1; i >= 0; i--)
1252 {
1253 // pop every variable
1254 ExpressionItem *val;
1255 var->type = (IT_VARIABLE | ITV_USER) + f->vars[i];
1256 RunTree(si, val, RTO_NEEDCONST | ITC_STRING | ITC_INT | ITC_FLOAT | ITC_ARRAY);
1257 SaveResult(var, val);
1258 CleanupItems(val);
1259 }
1260
1261 // free used items
1262 CleanupItems(si); CleanupItems(var);
1263 } else if (subtype == ITF_TYPE)
1264 {
1265 INT_PTR newtype = (INT_PTR) MathFunctions[ioptions].fptr;
1266 if (newtype < ITC_UNKNOWN)
1267 {
1268 // get as possibly close to ready expression
1269 int truncatednewtype = (int) newtype; // BUGBUG64: Make sure this is safe for 64-bit, meaning, can newtype be < INT_MIN?
1270 RunAndGetConst((item->param1), result, truncatednewtype);
1271 if (ioptions == ITFT_CARRAY_ID)
1272 CopyArray(result);
1273 } else if (newtype == FTT_FLOATF)
1274 {
1275 // float format function
1276 ExpressionItem *arg1, *arg2;
1277 RunAndGetConst((item->param1), arg1, ITC_FLOAT);
1278 double value = *((double*)&(arg1->param1));
1279 RunAndGetConst((item->param2), arg2, ITC_INT);
1280 int format = (int) *((__int64*)&(arg2->param1));
1281
1282 result = AllocItem();
1283 result->type = IT_CONST | ITC_STRING;
1284 result->param1 = (EIPARAM) AllocString();
1285 FloatFormat((TCHAR*) result->param1, value, format);
1286 CleanupItems(arg1); CleanupItems(arg2);
1287 } else if (newtype == FTT_LEN)
1288 {
1289 // length function
1290 RunTree(*((ExpressionItem **) &(item->param1)), result, RTO_NEEDCONST | ITC_STRING | ITC_ARRAY);
1291 if ((result->type & (ITEMTYPE|ITEMSUBTYPE)) == (IT_CONST|ITC_ARRAY))
1292 {
1293 int len = ((ArrayDesc*)(result->param1))->count;
1294 CleanupItems(result);
1295 result = AllocItem();
1296 *((__int64*)&(result->param1)) = (__int64) len;
1297 break;
1298 } else
1299 if ((result->type & (ITEMTYPE|ITEMSUBTYPE)) != (IT_CONST|ITC_STRING))
1300 ItemToType(result, ITC_STRING);
1301
1302 if ((result->type & (ITEMTYPE|ITEMSUBTYPE)) == (IT_CONST|ITC_STRING))
1303 {
1304 // ok, that's string
1305 int len = lstrlen((TCHAR*)result->param1);
1306 dbgGlobalFree((HGLOBAL) result->param1);
1307 *((__int64*)&(result->param1)) = (__int64) len;
1308 result->type = IT_CONST | ITC_INT;
1309 } else CleanupItems(result);
1310 } else
1311 {
1312 // only one left - c() - char/int/char conversion
1313 RunTree(*((ExpressionItem **) &(item->param1)), result, RTO_NEEDCONST | ITC_STRING | ITC_INT);
1314 if ((result->type & (ITEMTYPE|ITEMSUBTYPE)) == (IT_CONST|ITC_STRING))
1315 {
1316 // ok, that's string - convert first char to int
1317 int chr = (*((TCHAR*)result->param1)) & 0xFF;
1318 dbgGlobalFree((HGLOBAL) result->param1);
1319 *((__int64*)&(result->param1)) = (__int64) chr;
1320 result->type = IT_CONST | ITC_INT;
1321 break;
1322 }
1323 if ((result->type & (ITEMTYPE|ITEMSUBTYPE)) == (IT_CONST|ITC_INT))
1324 {
1325 // ok, that's int - convert to new string (char+0)
1326 int chr = (int) (*((__int64*)&(result->param1))) & 0xFF;
1327 result->param1 = (EIPARAM) AllocString();
1328 *((TCHAR*)result->param1) = (TCHAR) chr;
1329 *((TCHAR*)(result->param1+1)) = (TCHAR) 0;
1330 result->type = IT_CONST | ITC_STRING;
1331 break;
1332 } else CleanupItems(result);
1333 }
1334 } else
1335 {
1336 // oops :-o function call :)
1337 RunAndGetConst((item->param1), result, ITC_FLOAT);
1338 double &value = *((double*)&(result->param1));
1339 if (subtype == ITF_MATH1)
1340 {
1341 // Built-In math function with 1 arg
1342 value = MathFunctions[ioptions].fptr(value);
1343 } else
1344 if (subtype == ITF_MATH2)
1345 {
1346 // Built-In math function with 2 args
1347 if (ioptions >= MATHFUNCNUM-2)
1348 {
1349 // specific function - we need second arg as out
1350 ExpressionItem *arg2, *res2 = AllocItem();
1351 RunTree(*((ExpressionItem**)&(item->param2)), arg2, 0);
1352 if (ioptions == MATHFUNCNUM-1)
1353 {
1354 // fmodf function - second arg is ptr to double
1355 res2->type = IT_CONST | ITC_FLOAT;
1356 double &v = *((double*)&(res2->param1));
1357 value = ((Math2dFuncPtr)(MathFunctions[ioptions].fptr))(value, &v);
1358 } else
1359 {
1360 // frexp function - second arg is ptr to int
1361 int v = 0;
1362 value = ((Math2iFuncPtr)(MathFunctions[ioptions].fptr))(value, &v);
1363 *((__int64 *)&(res2->param1)) = (__int64) v;
1364 }
1365 SaveResult(arg2, res2);
1366 CleanupItems(arg2); CleanupItems(res2);
1367 } else
1368 {
1369 // normal 2-arg math function
1370 ExpressionItem *arg2;
1371 RunAndGetConst((item->param2), arg2, ITC_FLOAT);
1372 double value2 = *((double*)&(arg2->param1));
1373 value = ((Math2FuncPtr)(MathFunctions[ioptions].fptr))(value, value2);
1374 CleanupItems(arg2);
1375 }
1376 }
1377 }
1378 break;
1379 case IT_ARRAY:
1380 {
1381 // currently only array access is used
1382 ExpressionItem *index, *aritem;
1383 RunTree(*((ExpressionItem **) &(item->param1)), aritem, RTO_NEEDCONST | ITC_STRING | ITC_ARRAY);
1384
1385 if ((aritem->type & (ITEMTYPE|ITEMSUBTYPE)) == (IT_CONST | ITC_STRING))
1386 {
1387 // argument is string
1388 TCHAR *str = (TCHAR*)(aritem->param1);
1389 int len = lstrlen(str);
1390 // have we two indexes or one?
1391 if ((*((ExpressionItem **) &(item->param2)))->type != IT_EXPRESSION)
1392 {
1393 // one index - user need a char
1394 RunAndGetConst((item->param2), index, ITC_INT);
1395
1396 int pos = (int) *((__int64*)&(index->param1));
1397 if (pos < 0) pos += len; // -index - means from end
1398 if ((pos > len) || (pos < 0))
1399 *str = 0; // index is across string boundaries
1400 else
1401 {
1402 // new string - just a single char
1403 *str = *(str+pos);
1404 *(str+1) = 0;
1405 }
1406 } else
1407 {
1408 // two indexes
1409 ExpressionItem *index2;
1410 // if first index is skipped -> 0 (from first char)
1411 if ((*((ExpressionItem **) &(item->param2)))->param1 == 0)
1412 index = AllocItem();
1413 else
1414 RunAndGetConst(((*((ExpressionItem **) &(item->param2)))->param1), index, ITC_INT);
1415 if ((*((ExpressionItem **) &(item->param2)))->next->param1 == 0)
1416 {
1417 // if second index is skipped -> -1 (till last char)
1418 index2 = AllocItem();
1419 *((__int64*)&(index2->param1)) = -1;
1420 }
1421 else
1422 RunAndGetConst(((*((ExpressionItem **) &(item->param2)))->next->param1), index2, ITC_INT);
1423
1424 // ok, we've got two indexes
1425 int pos1 = (int) *((__int64*)&(index->param1));
1426 int pos2 = (int) *((__int64*)&(index2->param1));
1427 if (pos1 < 0) pos1 += len; // -index - means from end
1428 if (pos2 < 0) pos2 += len; // -index - means from end
1429 // limit start/stop positions
1430 if (pos1 < 0) pos1 = 0;
1431 if (pos2 < 0) pos2 = 0;
1432 if (pos1 > len) pos1 = len;
1433 if (pos2 >= len) pos2 = len-1;
1434
1435 // copy string part
1436 TCHAR* lpos = str + (pos2-pos1);
1437 while (str <= lpos)
1438 {
1439 *str = *(str + pos1);
1440 str++;
1441 }
1442 // null-termiante
1443 *str = 0;
1444
1445 CleanupItems(index2);
1446 }
1447
1448 } else
1449 {
1450 // argument is array
1451 RunAndGetConst((item->param2), index, ITC_INT);
1452
1453 // convert array pointer to array item pointer
1454 aritem->type = IT_VARIABLE | ITV_ARRITEM;
1455 aritem->param2 = (EIPARAM) *((__int64*)&(index->param1));
1456
1457 ArrayDesc *ad = (ArrayDesc*)aritem->param1;
1458 if ((EIPtrToInt(aritem->param2)) >= ad->count)
1459 {
1460 ad->count = (EIPtrToInt(aritem->param2))+1;
1461 while (ad->count > ad->size)
1462 {
1463 // resize array
1464 ExpressionItem **oldei = ad->array;
1465 ad->array = (ExpressionItem**) dbgGlobalAlloc(GPTR, 2*ad->size*sizeof(ExpressionItem*));
1466 for (int i = 0; i < ad->size; i++)
1467 ad->array[i] = oldei[i];
1468 ad->size*=2;
1469 dbgGlobalFree(oldei);
1470 }
1471 }
1472 }
1473
1474 CleanupItems(index);
1475
1476 // we need constant result?
1477 if (options & RTO_NEEDCONST)
1478 {
1479 RunTree(aritem, result, options);
1480 CleanupItems(aritem);
1481 } else result = aritem;
1482 }
1483 break;
1484 }
1485 item = item->next;
1486 }
1487 }
1488
1489 static UINT_PTR PluginCallback(enum NSPIM msg)
1490 {
1491 return 0;
1492 }
1493
1494 HINSTANCE g_hInstance;
1495
1496 extern "C"
1497 void __declspec(dllexport) Script(HWND hwndParent, int string_size,
1498 TCHAR *variables, stack_t **stacktop,
1499 extra_parameters *extra)
1500 {
1501 EXDLL_INIT();
1502 TCHAR *buffer = AllocString(), *buf = buffer;
1503 ExpressionItem *root = NULL; // root of current tree
1504
1505 // keep loaded to save user defined variables
1506 extra->RegisterPluginCallback(g_hInstance, PluginCallback);
1507
1508 // pop script string
1509 popstring(buffer);
1510
1511 // parse it
1512 ParseString(buf, root);
1513
1514 #ifdef _DEBUG
1515 // dump
1516 PrintTree(root, buffer);
1517 #endif
1518
1519 ExpressionItem *result;
1520 RunTree(root, result, 0);
1521 CleanupItems(result);
1522
1523 CleanupItems(root);
1524 dbgGlobalFree((HGLOBAL) buffer);
1525 }
1526
1527 double _infinity;
1528 extern "C" void _fpreset();
1529
1530 void CleanAll(int init)
1531 {
1532 if (init)
1533 {
1534 unsigned char _infinity_base[8] = {0, 0, 0, 0, 0, 0, 0xf0, 0x7f};
1535 _fltused = 0;
1536 _infinity = *((double*)(_infinity_base));
1537 _fpreset();
1538
1539 stack = NULL;
1540 UserVarsCount = 0;
1541 UserFuncsCount = 0;
1542 } else
1543 {
1544 int i;
1545 // cleanup stack
1546 CleanupItems(stack); stack = NULL;
1547 // cleanup user vars
1548 for (i = 0; i < UserVarsCount; i++)
1549 CleanupItems(UserVars[i].item);
1550 // cleanup user funcs
1551 for (i = 0; i < UserFuncsCount; i++)
1552 CleanupItems(UserFuncs[i].root);
1553 UserVarsCount = 0;
1554 UserFuncsCount = 0;
1555
1556 dbgGlobalCheck();
1557 }
1558 }
1559
1560 extern "C" BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
1561 {
1562 g_hInstance = hInst;
1563 CleanAll(ul_reason_for_call == DLL_PROCESS_ATTACH);
1564 return TRUE;
1565 }
1566