Moved the WindowFromPoint functionality to the server so that we can
[wine/wine-kai.git] / dlls / msi / cond.y
blobf82ac55e7afba928545deab8b7679bd14caffdc0
1 %{
3 /*
4 * Implementation of the Microsoft Installer (msi.dll)
6 * Copyright 2003 Mike McCormack for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config.h"
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
34 #include "msi.h"
35 #include "msiquery.h"
36 #include "msipriv.h"
38 #define YYLEX_PARAM info
39 #define YYPARSE_PARAM info
41 static int COND_error(char *str);
43 WINE_DEFAULT_DEBUG_CHANNEL(msi);
45 typedef struct tag_yyinput
47 MSIPACKAGE *package;
48 LPCWSTR str;
49 INT n;
50 MSICONDITION result;
51 } COND_input;
53 struct cond_str {
54 LPCWSTR data;
55 INT len;
58 static LPWSTR COND_GetString( struct cond_str *str );
59 static int COND_lex( void *COND_lval, COND_input *info);
61 typedef INT (*comp_int)(INT a, INT b);
62 typedef INT (*comp_str)(LPWSTR a, LPWSTR b, BOOL caseless);
63 typedef INT (*comp_m1)(LPWSTR a,int b);
64 typedef INT (*comp_m2)(int a,LPWSTR b);
66 static INT comp_lt_i(INT a, INT b);
67 static INT comp_gt_i(INT a, INT b);
68 static INT comp_le_i(INT a, INT b);
69 static INT comp_ge_i(INT a, INT b);
70 static INT comp_eq_i(INT a, INT b);
71 static INT comp_ne_i(INT a, INT b);
72 static INT comp_bitand(INT a, INT b);
73 static INT comp_highcomp(INT a, INT b);
74 static INT comp_lowcomp(INT a, INT b);
76 static INT comp_eq_s(LPWSTR a, LPWSTR b, BOOL casless);
77 static INT comp_ne_s(LPWSTR a, LPWSTR b, BOOL casless);
78 static INT comp_lt_s(LPWSTR a, LPWSTR b, BOOL casless);
79 static INT comp_gt_s(LPWSTR a, LPWSTR b, BOOL casless);
80 static INT comp_le_s(LPWSTR a, LPWSTR b, BOOL casless);
81 static INT comp_ge_s(LPWSTR a, LPWSTR b, BOOL casless);
82 static INT comp_substring(LPWSTR a, LPWSTR b, BOOL casless);
83 static INT comp_start(LPWSTR a, LPWSTR b, BOOL casless);
84 static INT comp_end(LPWSTR a, LPWSTR b, BOOL casless);
86 static INT comp_eq_m1(LPWSTR a, INT b);
87 static INT comp_ne_m1(LPWSTR a, INT b);
88 static INT comp_lt_m1(LPWSTR a, INT b);
89 static INT comp_gt_m1(LPWSTR a, INT b);
90 static INT comp_le_m1(LPWSTR a, INT b);
91 static INT comp_ge_m1(LPWSTR a, INT b);
93 static INT comp_eq_m2(INT a, LPWSTR b);
94 static INT comp_ne_m2(INT a, LPWSTR b);
95 static INT comp_lt_m2(INT a, LPWSTR b);
96 static INT comp_gt_m2(INT a, LPWSTR b);
97 static INT comp_le_m2(INT a, LPWSTR b);
98 static INT comp_ge_m2(INT a, LPWSTR b);
102 %pure-parser
104 %union
106 struct cond_str str;
107 LPWSTR string;
108 INT value;
109 comp_int fn_comp_int;
110 comp_str fn_comp_str;
111 comp_m1 fn_comp_m1;
112 comp_m2 fn_comp_m2;
115 %token COND_SPACE COND_EOF COND_SPACE
116 %token COND_OR COND_AND COND_NOT
117 %token COND_LT COND_GT COND_EQ
118 %token COND_LPAR COND_RPAR COND_DBLQ COND_TILDA
119 %token COND_PERCENT COND_DOLLARS COND_QUESTION COND_AMPER COND_EXCLAM
120 %token <str> COND_IDENT <str> COND_NUMBER
122 %nonassoc COND_EOF COND_ERROR
124 %type <value> expression boolean_term boolean_factor
125 %type <value> term value_i symbol_i integer
126 %type <string> identifier value_s symbol_s literal
127 %type <fn_comp_int> comp_op_i
128 %type <fn_comp_str> comp_op_s
129 %type <fn_comp_m1> comp_op_m1
130 %type <fn_comp_m2> comp_op_m2
134 condition:
135 expression
137 COND_input* cond = (COND_input*) info;
138 cond->result = $1;
142 expression:
143 boolean_term
145 $$ = $1;
147 | boolean_term COND_OR expression
149 $$ = $1 || $3;
153 boolean_term:
154 boolean_factor
156 $$ = $1;
158 | boolean_term COND_AND boolean_factor
160 $$ = $1 && $3;
164 boolean_factor:
165 term
167 $$ = $1;
169 | COND_NOT term
171 $$ = ! $2;
176 term:
177 value_i
179 $$ = $1;
181 | value_s
183 $$ = atoiW($1);
185 | value_i comp_op_i value_i
187 $$ = $2( $1, $3 );
189 | value_s comp_op_s value_s
191 $$ = $2( $1, $3, FALSE );
193 | value_s COND_TILDA comp_op_s value_s
195 $$ = $3( $1, $4, TRUE );
197 | value_s comp_op_m1 value_i
199 $$ = $2( $1, $3 );
201 | value_i comp_op_m2 value_s
203 $$ = $2( $1, $3 );
205 | COND_LPAR expression COND_RPAR
207 $$ = $2;
211 comp_op_i:
212 /* common functions */
213 COND_EQ
215 $$ = comp_eq_i;
217 | COND_LT COND_GT
219 $$ = comp_ne_i;
221 | COND_LT
223 $$ = comp_lt_i;
225 | COND_GT
227 $$ = comp_gt_i;
229 | COND_LT COND_EQ
231 $$ = comp_le_i;
233 | COND_GT COND_EQ
235 $$ = comp_ge_i;
237 /*Int only*/
238 | COND_GT COND_LT
240 $$ = comp_bitand;
242 | COND_LT COND_LT
244 $$ = comp_highcomp;
246 | COND_GT COND_GT
248 $$ = comp_lowcomp;
252 comp_op_s:
253 /* common functions */
254 COND_EQ
256 $$ = comp_eq_s;
258 | COND_LT COND_GT
260 $$ = comp_ne_s;
262 | COND_LT
264 $$ = comp_lt_s;
266 | COND_GT
268 $$ = comp_gt_s;
270 | COND_LT COND_EQ
272 $$ = comp_le_s;
274 | COND_GT COND_EQ
276 $$ = comp_ge_s;
278 /*string only*/
279 | COND_GT COND_LT
281 $$ = comp_substring;
283 | COND_LT COND_LT
285 $$ = comp_start;
287 | COND_GT COND_GT
289 $$ = comp_end;
293 comp_op_m1:
294 /* common functions */
295 COND_EQ
297 $$ = comp_eq_m1;
299 | COND_LT COND_GT
301 $$ = comp_ne_m1;
303 | COND_LT
305 $$ = comp_lt_m1;
307 | COND_GT
309 $$ = comp_gt_m1;
311 | COND_LT COND_EQ
313 $$ = comp_le_m1;
315 | COND_GT COND_EQ
317 $$ = comp_ge_m1;
319 /*Not valid for mixed compares*/
320 | COND_GT COND_LT
322 $$ = 0;
324 | COND_LT COND_LT
326 $$ = 0;
328 | COND_GT COND_GT
330 $$ = 0;
334 comp_op_m2:
335 /* common functions */
336 COND_EQ
338 $$ = comp_eq_m2;
340 | COND_LT COND_GT
342 $$ = comp_ne_m2;
344 | COND_LT
346 $$ = comp_lt_m2;
348 | COND_GT
350 $$ = comp_gt_m2;
352 | COND_LT COND_EQ
354 $$ = comp_le_m2;
356 | COND_GT COND_EQ
358 $$ = comp_ge_m2;
360 /*Not valid for mixed compares*/
361 | COND_GT COND_LT
363 $$ = 0;
365 | COND_LT COND_LT
367 $$ = 0;
369 | COND_GT COND_GT
371 $$ = 0;
375 value_i:
376 symbol_i
378 $$ = $1;
380 | integer
382 $$ = $1;
386 value_s:
387 symbol_s
389 $$ = $1;
391 | literal
393 $$ = $1;
397 literal:
398 COND_DBLQ identifier COND_DBLQ
400 $$ = $2;
402 | COND_DBLQ integer COND_DBLQ
404 static const WCHAR pi[] = {'%','i',0};
405 sprintfW($$,pi,$2);
409 symbol_i:
410 COND_DOLLARS identifier
412 COND_input* cond = (COND_input*) info;
413 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
415 MSI_GetComponentStateW(cond->package, $2, &install, &action );
416 $$ = action;
417 HeapFree( GetProcessHeap(), 0, $2 );
419 | COND_QUESTION identifier
421 COND_input* cond = (COND_input*) info;
422 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
424 MSI_GetComponentStateW(cond->package, $2, &install, &action );
425 $$ = install;
426 HeapFree( GetProcessHeap(), 0, $2 );
428 | COND_AMPER identifier
430 COND_input* cond = (COND_input*) info;
431 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
433 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
434 $$ = action;
435 HeapFree( GetProcessHeap(), 0, $2 );
437 | COND_EXCLAM identifier
439 COND_input* cond = (COND_input*) info;
440 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
442 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
443 $$ = install;
444 HeapFree( GetProcessHeap(), 0, $2 );
448 symbol_s:
449 identifier
451 DWORD sz;
452 COND_input* cond = (COND_input*) info;
453 $$ = HeapAlloc( GetProcessHeap(), 0, 0x100*sizeof (WCHAR) );
455 /* Lookup the identifier */
457 sz=0x100;
458 if (MSI_GetPropertyW(cond->package,$1,$$,&sz) != ERROR_SUCCESS)
460 $$[0]=0;
462 HeapFree( GetProcessHeap(), 0, $1 );
464 | COND_PERCENT identifier
466 UINT len = GetEnvironmentVariableW( $2, NULL, 0 );
467 if( len++ )
469 $$ = HeapAlloc( GetProcessHeap(), 0, len*sizeof (WCHAR) );
470 if( $$ )
471 GetEnvironmentVariableW( $2, $$, len );
473 HeapFree( GetProcessHeap(), 0, $2 );
477 identifier:
478 COND_IDENT
480 $$ = COND_GetString(&$1);
481 if( !$$ )
482 YYABORT;
486 integer:
487 COND_NUMBER
489 LPWSTR szNum = COND_GetString(&$1);
490 if( !szNum )
491 YYABORT;
492 $$ = atoiW( szNum );
493 HeapFree( GetProcessHeap(), 0, szNum );
500 static int COND_IsAlpha( WCHAR x )
502 return( ( ( x >= 'A' ) && ( x <= 'Z' ) ) ||
503 ( ( x >= 'a' ) && ( x <= 'z' ) ) ||
504 ( ( x == '_' ) ) );
507 static int COND_IsNumber( WCHAR x )
509 return( (( x >= '0' ) && ( x <= '9' )) || (x =='-') || (x =='.') );
513 /* the mess of comparison functions */
515 static INT comp_lt_i(INT a, INT b)
516 { return (a < b); }
517 static INT comp_gt_i(INT a, INT b)
518 { return (a > b); }
519 static INT comp_le_i(INT a, INT b)
520 { return (a <= b); }
521 static INT comp_ge_i(INT a, INT b)
522 { return (a >= b); }
523 static INT comp_eq_i(INT a, INT b)
524 { return (a == b); }
525 static INT comp_ne_i(INT a, INT b)
526 { return (a != b); }
527 static INT comp_bitand(INT a, INT b)
528 { return a & b;}
529 static INT comp_highcomp(INT a, INT b)
530 { return HIWORD(a)==b; }
531 static INT comp_lowcomp(INT a, INT b)
532 { return LOWORD(a)==b; }
534 static INT comp_eq_s(LPWSTR a, LPWSTR b, BOOL casless)
535 { if (casless) return !strcmpiW(a,b); else return !strcmpW(a,b);}
536 static INT comp_ne_s(LPWSTR a, LPWSTR b, BOOL casless)
537 { if (casless) return strcmpiW(a,b); else return strcmpW(a,b);}
538 static INT comp_lt_s(LPWSTR a, LPWSTR b, BOOL casless)
539 { if (casless) return strcmpiW(a,b)<0; else return strcmpW(a,b)<0;}
540 static INT comp_gt_s(LPWSTR a, LPWSTR b, BOOL casless)
541 { if (casless) return strcmpiW(a,b)>0; else return strcmpW(a,b)>0;}
542 static INT comp_le_s(LPWSTR a, LPWSTR b, BOOL casless)
543 { if (casless) return strcmpiW(a,b)<=0; else return strcmpW(a,b)<=0;}
544 static INT comp_ge_s(LPWSTR a, LPWSTR b, BOOL casless)
545 { if (casless) return strcmpiW(a,b)>=0; else return strcmpW(a,b)>=0;}
546 static INT comp_substring(LPWSTR a, LPWSTR b, BOOL casless)
547 /* ERROR NOT WORKING REWRITE */
548 { if (casless) return strstrW(a,b)!=NULL; else return strstrW(a,b)!=NULL;}
549 static INT comp_start(LPWSTR a, LPWSTR b, BOOL casless)
550 { if (casless) return strncmpiW(a,b,strlenW(b))==0;
551 else return strncmpW(a,b,strlenW(b))==0;}
552 static INT comp_end(LPWSTR a, LPWSTR b, BOOL casless)
554 int i = strlenW(a);
555 int j = strlenW(b);
556 if (j>i)
557 return 0;
558 if (casless) return (!strcmpiW(&a[i-j-1],b));
559 else return (!strcmpW(&a[i-j-1],b));
563 static INT comp_eq_m1(LPWSTR a, INT b)
564 { if (COND_IsNumber(a[0])) return atoiW(a)==b; else return 0;}
565 static INT comp_ne_m1(LPWSTR a, INT b)
566 { if (COND_IsNumber(a[0])) return atoiW(a)!=b; else return 1;}
567 static INT comp_lt_m1(LPWSTR a, INT b)
568 { if (COND_IsNumber(a[0])) return atoiW(a)<b; else return 0;}
569 static INT comp_gt_m1(LPWSTR a, INT b)
570 { if (COND_IsNumber(a[0])) return atoiW(a)>b; else return 0;}
571 static INT comp_le_m1(LPWSTR a, INT b)
572 { if (COND_IsNumber(a[0])) return atoiW(a)<=b; else return 0;}
573 static INT comp_ge_m1(LPWSTR a, INT b)
574 { if (COND_IsNumber(a[0])) return atoiW(a)>=b; else return 0;}
576 static INT comp_eq_m2(INT a, LPWSTR b)
577 { if (COND_IsNumber(b[0])) return a == atoiW(b); else return 0;}
578 static INT comp_ne_m2(INT a, LPWSTR b)
579 { if (COND_IsNumber(b[0])) return a != atoiW(b); else return 1;}
580 static INT comp_lt_m2(INT a, LPWSTR b)
581 { if (COND_IsNumber(b[0])) return a < atoiW(b); else return 0;}
582 static INT comp_gt_m2(INT a, LPWSTR b)
583 { if (COND_IsNumber(b[0])) return a > atoiW(b); else return 0;}
584 static INT comp_le_m2(INT a, LPWSTR b)
585 { if (COND_IsNumber(b[0])) return a <= atoiW(b); else return 0;}
586 static INT comp_ge_m2(INT a, LPWSTR b)
587 { if (COND_IsNumber(b[0])) return a >= atoiW(b); else return 0;}
591 static int COND_IsIdent( WCHAR x )
593 return( COND_IsAlpha( x ) || COND_IsNumber( x ) || ( x == '_' )
594 || ( x == '#' ) || (x == '.') );
597 static int COND_GetOne( struct cond_str *str, COND_input *cond )
599 static const WCHAR szNot[] = {'N','O','T',0};
600 static const WCHAR szAnd[] = {'A','N','D',0};
601 static const WCHAR szOr[] = {'O','R',0};
602 WCHAR ch;
603 int rc, len = 1;
605 str->data = &cond->str[cond->n];
607 ch = str->data[0];
608 switch( ch )
610 case 0: return 0;
611 case '(': rc = COND_LPAR; break;
612 case ')': rc = COND_RPAR; break;
613 case '&': rc = COND_AMPER; break;
614 case '!': rc = COND_EXCLAM; break;
615 case '$': rc = COND_DOLLARS; break;
616 case '?': rc = COND_QUESTION; break;
617 case '%': rc = COND_PERCENT; break;
618 case ' ': rc = COND_SPACE; break;
619 case '=': rc = COND_EQ; break;
620 case '"': rc = COND_DBLQ; break;
621 case '~': rc = COND_TILDA; break;
622 case '<': rc = COND_LT; break;
623 case '>': rc = COND_GT; break;
624 default:
625 if( COND_IsAlpha( ch ) )
627 while( COND_IsIdent( str->data[len] ) )
628 len++;
629 rc = COND_IDENT;
630 break;
633 if( COND_IsNumber( ch ) )
635 while( COND_IsNumber( str->data[len] ) )
636 len++;
637 rc = COND_NUMBER;
638 break;
641 ERR("Got unknown character %c(%x)\n",ch,ch);
642 rc = COND_ERROR;
643 break;
646 /* keyword identifiers */
647 if( rc == COND_IDENT )
649 if( (len==3) && (strncmpiW(str->data,szNot,len)==0) )
650 rc = COND_NOT;
651 else if( (len==3) && (strncmpiW(str->data,szAnd,len)==0) )
652 rc = COND_AND;
653 else if( (len==2) && (strncmpiW(str->data,szOr,len)==0) )
654 rc = COND_OR;
657 cond->n += len;
658 str->len = len;
660 return rc;
663 static int COND_lex( void *COND_lval, COND_input *cond )
665 int rc;
666 struct cond_str *str = COND_lval;
668 do {
669 rc = COND_GetOne( str, cond );
670 } while (rc == COND_SPACE);
672 return rc;
675 static LPWSTR COND_GetString( struct cond_str *str )
677 LPWSTR ret;
679 ret = HeapAlloc( GetProcessHeap(), 0, (str->len+1) * sizeof (WCHAR) );
680 if( ret )
682 strncpyW( ret, str->data, str->len );
683 ret[str->len]=0;
685 TRACE("Got identifier %s\n",debugstr_w(ret));
686 return ret;
689 static int COND_error(char *str)
691 return 0;
694 MSICONDITION MSI_EvaluateConditionW( MSIPACKAGE *package, LPCWSTR szCondition )
696 COND_input cond;
697 MSICONDITION r;
699 cond.package = package;
700 cond.str = szCondition;
701 cond.n = 0;
702 cond.result = -1;
704 TRACE("Evaluating %s\n",debugstr_w(szCondition));
706 if( !COND_parse( &cond ) )
707 r = cond.result;
708 else
709 r = MSICONDITION_ERROR;
711 TRACE("Evaluates to %i\n",r);
712 return r;
715 MSICONDITION WINAPI MsiEvaluateConditionW( MSIHANDLE hInstall, LPCWSTR szCondition )
717 MSIPACKAGE *package;
718 UINT ret;
720 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
721 if( !package)
722 return ERROR_INVALID_HANDLE;
723 ret = MSI_EvaluateConditionW( package, szCondition );
724 msiobj_release( &package->hdr );
725 return ret;
728 MSICONDITION WINAPI MsiEvaluateConditionA( MSIHANDLE hInstall, LPCSTR szCondition )
730 LPWSTR szwCond = NULL;
731 MSICONDITION r;
733 if( szCondition )
735 UINT len = MultiByteToWideChar( CP_ACP, 0, szCondition, -1, NULL, 0 );
736 szwCond = HeapAlloc( GetProcessHeap(), 0, len * sizeof (WCHAR) );
737 MultiByteToWideChar( CP_ACP, 0, szCondition, -1, szwCond, len );
740 r = MsiEvaluateConditionW( hInstall, szwCond );
742 if( szwCond )
743 HeapFree( GetProcessHeap(), 0, szwCond );
745 return r;