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
31 #include "wine/debug.h"
32 #include "wine/unicode.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
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);
109 comp_int fn_comp_int;
110 comp_str fn_comp_str;
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
137 COND_input* cond = (COND_input*) info;
147 | boolean_term COND_OR expression
158 | boolean_term COND_AND boolean_factor
185 | value_i comp_op_i value_i
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
201 | value_i comp_op_m2 value_s
205 | COND_LPAR expression COND_RPAR
212 /* common functions */
253 /* common functions */
294 /* common functions */
319 /*Not valid for mixed compares*/
335 /* common functions */
360 /*Not valid for mixed compares*/
398 COND_DBLQ identifier COND_DBLQ
405 COND_DOLLARS identifier
407 COND_input* cond = (COND_input*) info;
408 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
410 MSI_GetComponentStateW(cond->package, $2, &install, &action );
412 HeapFree( GetProcessHeap(), 0, $2 );
414 | COND_QUESTION identifier
416 COND_input* cond = (COND_input*) info;
417 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
419 MSI_GetComponentStateW(cond->package, $2, &install, &action );
421 HeapFree( GetProcessHeap(), 0, $2 );
423 | COND_AMPER identifier
425 COND_input* cond = (COND_input*) info;
426 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
428 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
430 HeapFree( GetProcessHeap(), 0, $2 );
432 | COND_EXCLAM identifier
434 COND_input* cond = (COND_input*) info;
435 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
437 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
439 HeapFree( GetProcessHeap(), 0, $2 );
447 COND_input* cond = (COND_input*) info;
448 $$ = HeapAlloc( GetProcessHeap(), 0, 0x100*sizeof (WCHAR) );
450 /* Lookup the identifier */
453 if (MSI_GetPropertyW(cond->package,$1,$$,&sz) != ERROR_SUCCESS)
457 HeapFree( GetProcessHeap(), 0, $1 );
459 | COND_PERCENT identifier
461 UINT len = GetEnvironmentVariableW( $2, NULL, 0 );
464 $$ = HeapAlloc( GetProcessHeap(), 0, len*sizeof (WCHAR) );
466 GetEnvironmentVariableW( $2, $$, len );
468 HeapFree( GetProcessHeap(), 0, $2 );
475 $$ = COND_GetString(&$1);
484 LPWSTR szNum = COND_GetString(&$1);
488 HeapFree( GetProcessHeap(), 0, szNum );
495 static int COND_IsAlpha( WCHAR x )
497 return( ( ( x >= 'A' ) && ( x <= 'Z' ) ) ||
498 ( ( x >= 'a' ) && ( x <= 'z' ) ) ||
502 static int COND_IsNumber( WCHAR x )
504 return( ( x >= '0' ) && ( x <= '9' ) );
508 /* the mess of comparison functions */
510 static INT comp_lt_i(INT a, INT b)
512 static INT comp_gt_i(INT a, INT b)
514 static INT comp_le_i(INT a, INT b)
516 static INT comp_ge_i(INT a, INT b)
518 static INT comp_eq_i(INT a, INT b)
520 static INT comp_ne_i(INT a, INT b)
522 static INT comp_bitand(INT a, INT b)
524 static INT comp_highcomp(INT a, INT b)
525 { return HIWORD(a)==b; }
526 static INT comp_lowcomp(INT a, INT b)
527 { return LOWORD(a)==b; }
529 static INT comp_eq_s(LPWSTR a, LPWSTR b, BOOL casless)
530 { if (casless) return !strcmpiW(a,b); else return !strcmpW(a,b);}
531 static INT comp_ne_s(LPWSTR a, LPWSTR b, BOOL casless)
532 { if (casless) return strcmpiW(a,b); else return strcmpW(a,b);}
533 static INT comp_lt_s(LPWSTR a, LPWSTR b, BOOL casless)
534 { if (casless) return strcmpiW(a,b)<0; else return strcmpW(a,b)<0;}
535 static INT comp_gt_s(LPWSTR a, LPWSTR b, BOOL casless)
536 { if (casless) return strcmpiW(a,b)>0; else return strcmpW(a,b)>0;}
537 static INT comp_le_s(LPWSTR a, LPWSTR b, BOOL casless)
538 { if (casless) return strcmpiW(a,b)<=0; else return strcmpW(a,b)<=0;}
539 static INT comp_ge_s(LPWSTR a, LPWSTR b, BOOL casless)
540 { if (casless) return strcmpiW(a,b)>=0; else return strcmpW(a,b)>=0;}
541 static INT comp_substring(LPWSTR a, LPWSTR b, BOOL casless)
542 /* ERROR NOT WORKING REWRITE */
543 { if (casless) return strstrW(a,b)!=NULL; else return strstrW(a,b)!=NULL;}
544 static INT comp_start(LPWSTR a, LPWSTR b, BOOL casless)
545 { if (casless) return strncmpiW(a,b,strlenW(b))==0;
546 else return strncmpW(a,b,strlenW(b))==0;}
547 static INT comp_end(LPWSTR a, LPWSTR b, BOOL casless)
553 if (casless) return (!strcmpiW(&a[i-j-1],b));
554 else return (!strcmpW(&a[i-j-1],b));
558 static INT comp_eq_m1(LPWSTR a, INT b)
559 { if (COND_IsNumber(a[0])) return atoiW(a)==b; else return 0;}
560 static INT comp_ne_m1(LPWSTR a, INT b)
561 { if (COND_IsNumber(a[0])) return atoiW(a)!=b; else return 1;}
562 static INT comp_lt_m1(LPWSTR a, INT b)
563 { if (COND_IsNumber(a[0])) return atoiW(a)<b; else return 0;}
564 static INT comp_gt_m1(LPWSTR a, INT b)
565 { if (COND_IsNumber(a[0])) return atoiW(a)>b; else return 0;}
566 static INT comp_le_m1(LPWSTR a, INT b)
567 { if (COND_IsNumber(a[0])) return atoiW(a)<=b; else return 0;}
568 static INT comp_ge_m1(LPWSTR a, INT b)
569 { if (COND_IsNumber(a[0])) return atoiW(a)>=b; else return 0;}
571 static INT comp_eq_m2(INT a, LPWSTR b)
572 { if (COND_IsNumber(b[0])) return a == atoiW(b); else return 0;}
573 static INT comp_ne_m2(INT a, LPWSTR b)
574 { if (COND_IsNumber(b[0])) return a != atoiW(b); else return 1;}
575 static INT comp_lt_m2(INT a, LPWSTR b)
576 { if (COND_IsNumber(b[0])) return a < atoiW(b); else return 0;}
577 static INT comp_gt_m2(INT a, LPWSTR b)
578 { if (COND_IsNumber(b[0])) return a > atoiW(b); else return 0;}
579 static INT comp_le_m2(INT a, LPWSTR b)
580 { if (COND_IsNumber(b[0])) return a <= atoiW(b); else return 0;}
581 static INT comp_ge_m2(INT a, LPWSTR b)
582 { if (COND_IsNumber(b[0])) return a >= atoiW(b); else return 0;}
586 static int COND_IsIdent( WCHAR x )
588 return( COND_IsAlpha( x ) || COND_IsNumber( x ) || ( x == '_' )
589 || ( x == '#' ) || (x == '.') );
592 static int COND_GetOne( struct cond_str *str, COND_input *cond )
594 static const WCHAR szNot[] = {'N','O','T',0};
595 static const WCHAR szAnd[] = {'A','N','D',0};
596 static const WCHAR szOr[] = {'O','R',0};
600 str->data = &cond->str[cond->n];
606 case '(': rc = COND_LPAR; break;
607 case ')': rc = COND_RPAR; break;
608 case '&': rc = COND_AMPER; break;
609 case '!': rc = COND_EXCLAM; break;
610 case '$': rc = COND_DOLLARS; break;
611 case '?': rc = COND_QUESTION; break;
612 case '%': rc = COND_PERCENT; break;
613 case ' ': rc = COND_SPACE; break;
614 case '=': rc = COND_EQ; break;
615 case '"': rc = COND_DBLQ; break;
616 case '~': rc = COND_TILDA; break;
617 case '<': rc = COND_LT; break;
618 case '>': rc = COND_GT; break;
620 if( COND_IsAlpha( ch ) )
622 while( COND_IsIdent( str->data[len] ) )
628 if( COND_IsNumber( ch ) )
630 while( COND_IsNumber( str->data[len] ) )
636 ERR("Got unknown character %c(%x)\n",ch,ch);
641 /* keyword identifiers */
642 if( rc == COND_IDENT )
644 if( (len==3) && (strncmpiW(str->data,szNot,len)==0) )
646 else if( (len==3) && (strncmpiW(str->data,szAnd,len)==0) )
648 else if( (len==2) && (strncmpiW(str->data,szOr,len)==0) )
658 static int COND_lex( void *COND_lval, COND_input *cond )
661 struct cond_str *str = COND_lval;
664 rc = COND_GetOne( str, cond );
665 } while (rc == COND_SPACE);
670 static LPWSTR COND_GetString( struct cond_str *str )
674 ret = HeapAlloc( GetProcessHeap(), 0, (str->len+1) * sizeof (WCHAR) );
677 strncpyW( ret, str->data, str->len );
680 TRACE("Got identifier %s\n",debugstr_w(ret));
684 static int COND_error(char *str)
689 MSICONDITION MSI_EvaluateConditionW( MSIPACKAGE *package, LPCWSTR szCondition )
694 cond.package = package;
695 cond.str = szCondition;
699 TRACE("Evaluating %s\n",debugstr_w(szCondition));
701 if( !COND_parse( &cond ) )
704 r = MSICONDITION_ERROR;
706 TRACE("Evaluates to %i\n",r);
710 MSICONDITION WINAPI MsiEvaluateConditionW( MSIHANDLE hInstall, LPCWSTR szCondition )
715 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
717 return ERROR_INVALID_HANDLE;
718 ret = MSI_EvaluateConditionW( package, szCondition );
719 msiobj_release( &package->hdr );
723 MSICONDITION WINAPI MsiEvaluateConditionA( MSIHANDLE hInstall, LPCSTR szCondition )
725 LPWSTR szwCond = NULL;
730 UINT len = MultiByteToWideChar( CP_ACP, 0, szCondition, -1, NULL, 0 );
731 szwCond = HeapAlloc( GetProcessHeap(), 0, len * sizeof (WCHAR) );
732 MultiByteToWideChar( CP_ACP, 0, szCondition, -1, szwCond, len );
735 r = MsiEvaluateConditionW( hInstall, szwCond );
738 HeapFree( GetProcessHeap(), 0, szwCond );