Fix parsing of string literals.
[wine.git] / dlls / msi / cond.y
blobfe7551f38824f44b6460e3361a7636f4ce9b4154
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 LPWSTR COND_GetLiteral( struct cond_str *str );
60 static int COND_lex( void *COND_lval, COND_input *info);
62 typedef INT (*comp_int)(INT a, INT b);
63 typedef INT (*comp_str)(LPWSTR a, LPWSTR b, BOOL caseless);
64 typedef INT (*comp_m1)(LPWSTR a,int b);
65 typedef INT (*comp_m2)(int a,LPWSTR b);
67 static INT comp_lt_i(INT a, INT b);
68 static INT comp_gt_i(INT a, INT b);
69 static INT comp_le_i(INT a, INT b);
70 static INT comp_ge_i(INT a, INT b);
71 static INT comp_eq_i(INT a, INT b);
72 static INT comp_ne_i(INT a, INT b);
73 static INT comp_bitand(INT a, INT b);
74 static INT comp_highcomp(INT a, INT b);
75 static INT comp_lowcomp(INT a, INT b);
77 static INT comp_eq_s(LPWSTR a, LPWSTR b, BOOL casless);
78 static INT comp_ne_s(LPWSTR a, LPWSTR b, BOOL casless);
79 static INT comp_lt_s(LPWSTR a, LPWSTR b, BOOL casless);
80 static INT comp_gt_s(LPWSTR a, LPWSTR b, BOOL casless);
81 static INT comp_le_s(LPWSTR a, LPWSTR b, BOOL casless);
82 static INT comp_ge_s(LPWSTR a, LPWSTR b, BOOL casless);
83 static INT comp_substring(LPWSTR a, LPWSTR b, BOOL casless);
84 static INT comp_start(LPWSTR a, LPWSTR b, BOOL casless);
85 static INT comp_end(LPWSTR a, LPWSTR b, BOOL casless);
87 static INT comp_eq_m1(LPWSTR a, INT b);
88 static INT comp_ne_m1(LPWSTR a, INT b);
89 static INT comp_lt_m1(LPWSTR a, INT b);
90 static INT comp_gt_m1(LPWSTR a, INT b);
91 static INT comp_le_m1(LPWSTR a, INT b);
92 static INT comp_ge_m1(LPWSTR a, INT b);
94 static INT comp_eq_m2(INT a, LPWSTR b);
95 static INT comp_ne_m2(INT a, LPWSTR b);
96 static INT comp_lt_m2(INT a, LPWSTR b);
97 static INT comp_gt_m2(INT a, LPWSTR b);
98 static INT comp_le_m2(INT a, LPWSTR b);
99 static INT comp_ge_m2(INT a, LPWSTR b);
103 %pure-parser
105 %union
107 struct cond_str str;
108 LPWSTR string;
109 INT value;
110 comp_int fn_comp_int;
111 comp_str fn_comp_str;
112 comp_m1 fn_comp_m1;
113 comp_m2 fn_comp_m2;
116 %token COND_SPACE COND_EOF COND_SPACE
117 %token COND_OR COND_AND COND_NOT
118 %token COND_LT COND_GT COND_EQ
119 %token COND_LPAR COND_RPAR COND_TILDA
120 %token COND_PERCENT COND_DOLLARS COND_QUESTION COND_AMPER COND_EXCLAM
121 %token <str> COND_IDENT <str> COND_NUMBER <str> COND_LITER
123 %nonassoc COND_EOF COND_ERROR
125 %type <value> expression boolean_term boolean_factor
126 %type <value> term value_i symbol_i integer
127 %type <string> identifier value_s symbol_s literal
128 %type <fn_comp_int> comp_op_i
129 %type <fn_comp_str> comp_op_s
130 %type <fn_comp_m1> comp_op_m1
131 %type <fn_comp_m2> comp_op_m2
135 condition:
136 expression
138 COND_input* cond = (COND_input*) info;
139 cond->result = $1;
143 expression:
144 boolean_term
146 $$ = $1;
148 | boolean_term COND_OR expression
150 $$ = $1 || $3;
154 boolean_term:
155 boolean_factor
157 $$ = $1;
159 | boolean_term COND_AND boolean_factor
161 $$ = $1 && $3;
165 boolean_factor:
166 term
168 $$ = $1;
170 | COND_NOT term
172 $$ = ! $2;
177 term:
178 value_i
180 $$ = $1;
182 | value_s
184 $$ = atoiW($1);
186 | value_i comp_op_i value_i
188 $$ = $2( $1, $3 );
190 | value_s comp_op_s value_s
192 $$ = $2( $1, $3, FALSE );
194 | value_s COND_TILDA comp_op_s value_s
196 $$ = $3( $1, $4, TRUE );
198 | value_s comp_op_m1 value_i
200 $$ = $2( $1, $3 );
202 | value_i comp_op_m2 value_s
204 $$ = $2( $1, $3 );
206 | COND_LPAR expression COND_RPAR
208 $$ = $2;
212 comp_op_i:
213 /* common functions */
214 COND_EQ
216 $$ = comp_eq_i;
218 | COND_LT COND_GT
220 $$ = comp_ne_i;
222 | COND_LT
224 $$ = comp_lt_i;
226 | COND_GT
228 $$ = comp_gt_i;
230 | COND_LT COND_EQ
232 $$ = comp_le_i;
234 | COND_GT COND_EQ
236 $$ = comp_ge_i;
238 /*Int only*/
239 | COND_GT COND_LT
241 $$ = comp_bitand;
243 | COND_LT COND_LT
245 $$ = comp_highcomp;
247 | COND_GT COND_GT
249 $$ = comp_lowcomp;
253 comp_op_s:
254 /* common functions */
255 COND_EQ
257 $$ = comp_eq_s;
259 | COND_LT COND_GT
261 $$ = comp_ne_s;
263 | COND_LT
265 $$ = comp_lt_s;
267 | COND_GT
269 $$ = comp_gt_s;
271 | COND_LT COND_EQ
273 $$ = comp_le_s;
275 | COND_GT COND_EQ
277 $$ = comp_ge_s;
279 /*string only*/
280 | COND_GT COND_LT
282 $$ = comp_substring;
284 | COND_LT COND_LT
286 $$ = comp_start;
288 | COND_GT COND_GT
290 $$ = comp_end;
294 comp_op_m1:
295 /* common functions */
296 COND_EQ
298 $$ = comp_eq_m1;
300 | COND_LT COND_GT
302 $$ = comp_ne_m1;
304 | COND_LT
306 $$ = comp_lt_m1;
308 | COND_GT
310 $$ = comp_gt_m1;
312 | COND_LT COND_EQ
314 $$ = comp_le_m1;
316 | COND_GT COND_EQ
318 $$ = comp_ge_m1;
320 /*Not valid for mixed compares*/
321 | COND_GT COND_LT
323 $$ = 0;
325 | COND_LT COND_LT
327 $$ = 0;
329 | COND_GT COND_GT
331 $$ = 0;
335 comp_op_m2:
336 /* common functions */
337 COND_EQ
339 $$ = comp_eq_m2;
341 | COND_LT COND_GT
343 $$ = comp_ne_m2;
345 | COND_LT
347 $$ = comp_lt_m2;
349 | COND_GT
351 $$ = comp_gt_m2;
353 | COND_LT COND_EQ
355 $$ = comp_le_m2;
357 | COND_GT COND_EQ
359 $$ = comp_ge_m2;
361 /*Not valid for mixed compares*/
362 | COND_GT COND_LT
364 $$ = 0;
366 | COND_LT COND_LT
368 $$ = 0;
370 | COND_GT COND_GT
372 $$ = 0;
376 value_i:
377 symbol_i
379 $$ = $1;
381 | integer
383 $$ = $1;
387 value_s:
388 symbol_s
390 $$ = $1;
392 | literal
394 $$ = $1;
398 literal:
399 COND_LITER
401 $$ = COND_GetLiteral(&$1);
402 if( !$$ )
403 YYABORT;
407 symbol_i:
408 COND_DOLLARS identifier
410 COND_input* cond = (COND_input*) info;
411 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
413 MSI_GetComponentStateW(cond->package, $2, &install, &action );
414 $$ = action;
415 HeapFree( GetProcessHeap(), 0, $2 );
417 | COND_QUESTION identifier
419 COND_input* cond = (COND_input*) info;
420 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
422 MSI_GetComponentStateW(cond->package, $2, &install, &action );
423 $$ = install;
424 HeapFree( GetProcessHeap(), 0, $2 );
426 | COND_AMPER identifier
428 COND_input* cond = (COND_input*) info;
429 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
431 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
432 $$ = action;
433 HeapFree( GetProcessHeap(), 0, $2 );
435 | COND_EXCLAM identifier
437 COND_input* cond = (COND_input*) info;
438 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
440 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
441 $$ = install;
442 HeapFree( GetProcessHeap(), 0, $2 );
446 symbol_s:
447 identifier
449 DWORD sz;
450 COND_input* cond = (COND_input*) info;
451 $$ = HeapAlloc( GetProcessHeap(), 0, 0x100*sizeof (WCHAR) );
453 /* Lookup the identifier */
455 sz=0x100;
456 if (MSI_GetPropertyW(cond->package,$1,$$,&sz) != ERROR_SUCCESS)
458 $$[0]=0;
460 HeapFree( GetProcessHeap(), 0, $1 );
462 | COND_PERCENT identifier
464 UINT len = GetEnvironmentVariableW( $2, NULL, 0 );
465 if( len++ )
467 $$ = HeapAlloc( GetProcessHeap(), 0, len*sizeof (WCHAR) );
468 if( $$ )
469 GetEnvironmentVariableW( $2, $$, len );
471 HeapFree( GetProcessHeap(), 0, $2 );
475 identifier:
476 COND_IDENT
478 $$ = COND_GetString(&$1);
479 if( !$$ )
480 YYABORT;
484 integer:
485 COND_NUMBER
487 LPWSTR szNum = COND_GetString(&$1);
488 if( !szNum )
489 YYABORT;
490 $$ = atoiW( szNum );
491 HeapFree( GetProcessHeap(), 0, szNum );
498 static int COND_IsAlpha( WCHAR x )
500 return( ( ( x >= 'A' ) && ( x <= 'Z' ) ) ||
501 ( ( x >= 'a' ) && ( x <= 'z' ) ) ||
502 ( ( x == '_' ) ) );
505 static int COND_IsNumber( WCHAR x )
507 return( (( x >= '0' ) && ( x <= '9' )) || (x =='-') || (x =='.') );
511 /* the mess of comparison functions */
513 static INT comp_lt_i(INT a, INT b)
514 { return (a < b); }
515 static INT comp_gt_i(INT a, INT b)
516 { return (a > b); }
517 static INT comp_le_i(INT a, INT b)
518 { return (a <= b); }
519 static INT comp_ge_i(INT a, INT b)
520 { return (a >= b); }
521 static INT comp_eq_i(INT a, INT b)
522 { return (a == b); }
523 static INT comp_ne_i(INT a, INT b)
524 { return (a != b); }
525 static INT comp_bitand(INT a, INT b)
526 { return a & b;}
527 static INT comp_highcomp(INT a, INT b)
528 { return HIWORD(a)==b; }
529 static INT comp_lowcomp(INT a, INT b)
530 { return LOWORD(a)==b; }
532 static INT comp_eq_s(LPWSTR a, LPWSTR b, BOOL casless)
533 { if (casless) return !strcmpiW(a,b); else return !strcmpW(a,b);}
534 static INT comp_ne_s(LPWSTR a, LPWSTR b, BOOL casless)
535 { if (casless) return strcmpiW(a,b); else return strcmpW(a,b);}
536 static INT comp_lt_s(LPWSTR a, LPWSTR b, BOOL casless)
537 { if (casless) return strcmpiW(a,b)<0; else return strcmpW(a,b)<0;}
538 static INT comp_gt_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_le_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_ge_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_substring(LPWSTR a, LPWSTR b, BOOL casless)
545 /* ERROR NOT WORKING REWRITE */
546 { if (casless) return strstrW(a,b)!=NULL; else return strstrW(a,b)!=NULL;}
547 static INT comp_start(LPWSTR a, LPWSTR b, BOOL casless)
548 { if (casless) return strncmpiW(a,b,strlenW(b))==0;
549 else return strncmpW(a,b,strlenW(b))==0;}
550 static INT comp_end(LPWSTR a, LPWSTR b, BOOL casless)
552 int i = strlenW(a);
553 int j = strlenW(b);
554 if (j>i)
555 return 0;
556 if (casless) return (!strcmpiW(&a[i-j-1],b));
557 else return (!strcmpW(&a[i-j-1],b));
561 static INT comp_eq_m1(LPWSTR a, INT b)
562 { if (COND_IsNumber(a[0])) return atoiW(a)==b; else return 0;}
563 static INT comp_ne_m1(LPWSTR a, INT b)
564 { if (COND_IsNumber(a[0])) return atoiW(a)!=b; else return 1;}
565 static INT comp_lt_m1(LPWSTR a, INT b)
566 { if (COND_IsNumber(a[0])) return atoiW(a)<b; else return 0;}
567 static INT comp_gt_m1(LPWSTR a, INT b)
568 { if (COND_IsNumber(a[0])) return atoiW(a)>b; else return 0;}
569 static INT comp_le_m1(LPWSTR a, INT b)
570 { if (COND_IsNumber(a[0])) return atoiW(a)<=b; else return 0;}
571 static INT comp_ge_m1(LPWSTR a, INT b)
572 { if (COND_IsNumber(a[0])) return atoiW(a)>=b; else return 0;}
574 static INT comp_eq_m2(INT a, LPWSTR b)
575 { if (COND_IsNumber(b[0])) return a == atoiW(b); else return 0;}
576 static INT comp_ne_m2(INT a, LPWSTR b)
577 { if (COND_IsNumber(b[0])) return a != atoiW(b); else return 1;}
578 static INT comp_lt_m2(INT a, LPWSTR b)
579 { if (COND_IsNumber(b[0])) return a < atoiW(b); else return 0;}
580 static INT comp_gt_m2(INT a, LPWSTR b)
581 { if (COND_IsNumber(b[0])) return a > atoiW(b); else return 0;}
582 static INT comp_le_m2(INT a, LPWSTR b)
583 { if (COND_IsNumber(b[0])) return a <= atoiW(b); else return 0;}
584 static INT comp_ge_m2(INT a, LPWSTR b)
585 { if (COND_IsNumber(b[0])) return a >= atoiW(b); else return 0;}
589 static int COND_IsIdent( WCHAR x )
591 return( COND_IsAlpha( x ) || COND_IsNumber( x ) || ( x == '_' )
592 || ( x == '#' ) || (x == '.') );
595 static int COND_GetOne( struct cond_str *str, COND_input *cond )
597 static const WCHAR szNot[] = {'N','O','T',0};
598 static const WCHAR szAnd[] = {'A','N','D',0};
599 static const WCHAR szOr[] = {'O','R',0};
600 WCHAR ch;
601 int rc, len = 1;
603 str->data = &cond->str[cond->n];
605 ch = str->data[0];
606 switch( ch )
608 case 0: return 0;
609 case '(': rc = COND_LPAR; break;
610 case ')': rc = COND_RPAR; break;
611 case '&': rc = COND_AMPER; break;
612 case '!': rc = COND_EXCLAM; break;
613 case '$': rc = COND_DOLLARS; break;
614 case '?': rc = COND_QUESTION; break;
615 case '%': rc = COND_PERCENT; break;
616 case ' ': rc = COND_SPACE; break;
617 case '=': rc = COND_EQ; break;
618 case '~': rc = COND_TILDA; break;
619 case '<': rc = COND_LT; break;
620 case '>': rc = COND_GT; break;
621 case '"':
623 const WCHAR *ch2 = str->data + 1;
626 while ( *ch2 && *ch2 != '"' )
627 ++ch2;
628 if (*ch2 == '"')
630 len = ch2 - str->data + 1;
631 rc = COND_LITER;
632 break;
635 ERR("Unterminated string\n");
636 rc = COND_ERROR;
637 break;
638 default:
639 if( COND_IsAlpha( ch ) )
641 while( COND_IsIdent( str->data[len] ) )
642 len++;
643 rc = COND_IDENT;
644 break;
647 if( COND_IsNumber( ch ) )
649 while( COND_IsNumber( str->data[len] ) )
650 len++;
651 rc = COND_NUMBER;
652 break;
655 ERR("Got unknown character %c(%x)\n",ch,ch);
656 rc = COND_ERROR;
657 break;
660 /* keyword identifiers */
661 if( rc == COND_IDENT )
663 if( (len==3) && (strncmpiW(str->data,szNot,len)==0) )
664 rc = COND_NOT;
665 else if( (len==3) && (strncmpiW(str->data,szAnd,len)==0) )
666 rc = COND_AND;
667 else if( (len==2) && (strncmpiW(str->data,szOr,len)==0) )
668 rc = COND_OR;
671 cond->n += len;
672 str->len = len;
674 return rc;
677 static int COND_lex( void *COND_lval, COND_input *cond )
679 int rc;
680 struct cond_str *str = COND_lval;
682 do {
683 rc = COND_GetOne( str, cond );
684 } while (rc == COND_SPACE);
686 return rc;
689 static LPWSTR COND_GetString( struct cond_str *str )
691 LPWSTR ret;
693 ret = HeapAlloc( GetProcessHeap(), 0, (str->len+1) * sizeof (WCHAR) );
694 if( ret )
696 strncpyW( ret, str->data, str->len );
697 ret[str->len]=0;
699 TRACE("Got identifier %s\n",debugstr_w(ret));
700 return ret;
703 static LPWSTR COND_GetLiteral( struct cond_str *str )
705 LPWSTR ret;
707 ret = HeapAlloc( GetProcessHeap(), 0, (str->len-1) * sizeof (WCHAR) );
708 if( ret )
710 memcpy( ret, str->data+1, (str->len-2) * sizeof(WCHAR) );
711 ret[str->len - 2]=0;
713 TRACE("Got literal %s\n",debugstr_w(ret));
714 return ret;
717 static int COND_error(char *str)
719 return 0;
722 MSICONDITION MSI_EvaluateConditionW( MSIPACKAGE *package, LPCWSTR szCondition )
724 COND_input cond;
725 MSICONDITION r;
727 cond.package = package;
728 cond.str = szCondition;
729 cond.n = 0;
730 cond.result = -1;
732 TRACE("Evaluating %s\n",debugstr_w(szCondition));
734 if( !COND_parse( &cond ) )
735 r = cond.result;
736 else
737 r = MSICONDITION_ERROR;
739 TRACE("Evaluates to %i\n",r);
740 return r;
743 MSICONDITION WINAPI MsiEvaluateConditionW( MSIHANDLE hInstall, LPCWSTR szCondition )
745 MSIPACKAGE *package;
746 UINT ret;
748 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
749 if( !package)
750 return ERROR_INVALID_HANDLE;
751 ret = MSI_EvaluateConditionW( package, szCondition );
752 msiobj_release( &package->hdr );
753 return ret;
756 MSICONDITION WINAPI MsiEvaluateConditionA( MSIHANDLE hInstall, LPCSTR szCondition )
758 LPWSTR szwCond = NULL;
759 MSICONDITION r;
761 if( szCondition )
763 UINT len = MultiByteToWideChar( CP_ACP, 0, szCondition, -1, NULL, 0 );
764 szwCond = HeapAlloc( GetProcessHeap(), 0, len * sizeof (WCHAR) );
765 MultiByteToWideChar( CP_ACP, 0, szCondition, -1, szwCond, len );
768 r = MsiEvaluateConditionW( hInstall, szwCond );
770 if( szwCond )
771 HeapFree( GetProcessHeap(), 0, szwCond );
773 return r;