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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
40 #define YYLEX_PARAM info
41 #define YYPARSE_PARAM info
43 static int cond_error(const char *str);
45 WINE_DEFAULT_DEBUG_CHANNEL(msi);
47 typedef struct tag_yyinput
60 static LPWSTR COND_GetString( struct cond_str *str );
61 static LPWSTR COND_GetLiteral( struct cond_str *str );
62 static int cond_lex( void *COND_lval, COND_input *info);
63 static const WCHAR szEmpty[] = { 0 };
65 static INT compare_int( INT a, INT operator, INT b );
66 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b );
68 static INT compare_and_free_strings( LPWSTR a, INT op, LPWSTR b )
72 r = compare_string( a, op, b );
78 static BOOL num_from_prop( LPCWSTR p, INT *val )
80 INT ret = 0, sign = 1;
93 if( *p < '0' || *p > '9' )
95 ret = ret*10 + (*p - '0');
113 %token COND_SPACE COND_EOF
114 %token COND_OR COND_AND COND_NOT COND_XOR COND_IMP COND_EQV
115 %token COND_LT COND_GT COND_EQ COND_NE COND_GE COND_LE
116 %token COND_ILT COND_IGT COND_IEQ COND_INE COND_IGE COND_ILE
117 %token COND_LPAR COND_RPAR COND_TILDA COND_SS COND_ISS
118 %token COND_ILHS COND_IRHS COND_LHS COND_RHS
119 %token COND_PERCENT COND_DOLLARS COND_QUESTION COND_AMPER COND_EXCLAM
120 %token <str> COND_IDENT <str> COND_NUMBER <str> COND_LITER
122 %nonassoc COND_ERROR COND_EOF
124 %type <value> expression boolean_term boolean_factor
125 %type <value> value_i integer operator
126 %type <string> identifier symbol_s value_s literal
133 COND_input* cond = (COND_input*) info;
138 COND_input* cond = (COND_input*) info;
139 cond->result = MSICONDITION_NONE;
148 | expression COND_OR boolean_term
152 | expression COND_IMP boolean_term
156 | expression COND_XOR boolean_term
158 $$ = ( $1 || $3 ) && !( $1 && $3 );
160 | expression COND_EQV boolean_term
162 $$ = ( $1 && $3 ) || ( !$1 && !$3 );
171 | boolean_term COND_AND boolean_factor
178 COND_NOT boolean_factor
188 $$ = ($1 && $1[0]) ? 1 : 0;
191 | value_i operator value_i
193 $$ = compare_int( $1, $2, $3 );
195 | symbol_s operator value_i
198 if (num_from_prop( $1, &num ))
199 $$ = compare_int( num, $2, $3 );
201 $$ = ($2 == COND_NE || $2 == COND_INE );
204 | value_i operator symbol_s
207 if (num_from_prop( $3, &num ))
208 $$ = compare_int( $1, $2, num );
210 $$ = ($2 == COND_NE || $2 == COND_INE );
213 | symbol_s operator symbol_s
215 $$ = compare_and_free_strings( $1, $2, $3 );
217 | symbol_s operator literal
219 $$ = compare_and_free_strings( $1, $2, $3 );
221 | literal operator symbol_s
223 $$ = compare_and_free_strings( $1, $2, $3 );
225 | literal operator literal
227 $$ = compare_and_free_strings( $1, $2, $3 );
229 | literal operator value_i
234 | value_i operator literal
239 | COND_LPAR expression COND_RPAR
246 /* common functions */
247 COND_EQ { $$ = COND_EQ; }
248 | COND_NE { $$ = COND_NE; }
249 | COND_LT { $$ = COND_LT; }
250 | COND_GT { $$ = COND_GT; }
251 | COND_LE { $$ = COND_LE; }
252 | COND_GE { $$ = COND_GE; }
253 | COND_SS { $$ = COND_SS; }
254 | COND_IEQ { $$ = COND_IEQ; }
255 | COND_INE { $$ = COND_INE; }
256 | COND_ILT { $$ = COND_ILT; }
257 | COND_IGT { $$ = COND_IGT; }
258 | COND_ILE { $$ = COND_ILE; }
259 | COND_IGE { $$ = COND_IGE; }
260 | COND_ISS { $$ = COND_ISS; }
261 | COND_LHS { $$ = COND_LHS; }
262 | COND_RHS { $$ = COND_RHS; }
263 | COND_ILHS { $$ = COND_ILHS; }
264 | COND_IRHS { $$ = COND_IRHS; }
281 $$ = COND_GetLiteral(&$1);
292 | COND_DOLLARS identifier
294 COND_input* cond = (COND_input*) info;
295 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
297 MSI_GetComponentStateW(cond->package, $2, &install, &action );
301 | COND_QUESTION identifier
303 COND_input* cond = (COND_input*) info;
304 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
306 MSI_GetComponentStateW(cond->package, $2, &install, &action );
310 | COND_AMPER identifier
312 COND_input* cond = (COND_input*) info;
313 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
315 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
319 | COND_EXCLAM identifier
321 COND_input* cond = (COND_input*) info;
322 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
324 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
333 COND_input* cond = (COND_input*) info;
335 $$ = msi_dup_property( cond->package, $1 );
338 | COND_PERCENT identifier
340 UINT len = GetEnvironmentVariableW( $2, NULL, 0 );
344 $$ = msi_alloc( len*sizeof (WCHAR) );
345 GetEnvironmentVariableW( $2, $$, len );
354 $$ = COND_GetString(&$1);
363 LPWSTR szNum = COND_GetString(&$1);
374 static int COND_IsAlpha( WCHAR x )
376 return( ( ( x >= 'A' ) && ( x <= 'Z' ) ) ||
377 ( ( x >= 'a' ) && ( x <= 'z' ) ) ||
381 static int COND_IsNumber( WCHAR x )
383 return( (( x >= '0' ) && ( x <= '9' )) || (x =='-') || (x =='.') );
386 static WCHAR *strstriW( const WCHAR *str, const WCHAR *sub )
388 LPWSTR strlower, sublower, r;
389 strlower = CharLowerW( strdupW( str ) );
390 sublower = CharLowerW( strdupW( sub ) );
391 r = strstrW( strlower, sublower );
393 r = (LPWSTR)str + (r - strlower);
394 msi_free( strlower );
395 msi_free( sublower );
399 static BOOL str_is_number( LPCWSTR str )
403 for (i = 0; i < lstrlenW( str ); i++)
404 if (!isdigitW(str[i]))
410 static INT compare_substring( LPCWSTR a, INT operator, LPCWSTR b )
414 /* substring operators return 0 if LHS is missing */
418 /* substring operators return 1 if RHS is missing */
422 /* if both strings contain only numbers, use integer comparison */
425 if (str_is_number(a) && str_is_number(b))
426 return compare_int( lhs, operator, rhs );
431 return strstrW( a, b ) ? 1 : 0;
433 return strstriW( a, b ) ? 1 : 0;
435 return 0 == strncmpW( a, b, lstrlenW( b ) );
437 return 0 == lstrcmpW( a + (lstrlenW( a ) - lstrlenW( b )), b );
439 return 0 == strncmpiW( a, b, lstrlenW( b ) );
441 return 0 == lstrcmpiW( a + (lstrlenW( a ) - lstrlenW( b )), b );
443 ERR("invalid substring operator\n");
449 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b )
451 if (operator >= COND_SS && operator <= COND_RHS)
452 return compare_substring( a, operator, b );
454 /* null and empty string are equivalent */
458 /* a or b may be NULL */
462 return -1 == lstrcmpW( a, b );
464 return 1 == lstrcmpW( a, b );
466 return 0 == lstrcmpW( a, b );
468 return 0 != lstrcmpW( a, b );
470 return -1 != lstrcmpW( a, b );
472 return 1 != lstrcmpW( a, b );
474 return -1 == lstrcmpiW( a, b );
476 return 1 == lstrcmpiW( a, b );
478 return 0 == lstrcmpiW( a, b );
480 return 0 != lstrcmpiW( a, b );
482 return -1 != lstrcmpiW( a, b );
484 return 1 != lstrcmpiW( a, b );
486 ERR("invalid string operator\n");
493 static INT compare_int( INT a, INT operator, INT b )
517 return ( a & b ) ? 1 : 0;
519 return ( ( a & 0xffff ) == b ) ? 1 : 0;
521 return ( ( (a>>16) & 0xffff ) == b ) ? 1 : 0;
523 ERR("invalid integer operator\n");
530 static int COND_IsIdent( WCHAR x )
532 return( COND_IsAlpha( x ) || COND_IsNumber( x ) || ( x == '_' )
533 || ( x == '#' ) || (x == '.') );
536 static int COND_GetOperator( COND_input *cond )
538 static const struct {
542 { {'~','=',0}, COND_IEQ },
543 { {'~','<','=',0}, COND_ILE },
544 { {'~','>','<',0}, COND_ISS },
545 { {'~','>','>',0}, COND_IRHS },
546 { {'~','<','>',0}, COND_INE },
547 { {'~','<',0}, COND_ILT },
548 { {'~','>','=',0}, COND_IGE },
549 { {'~','<','<',0}, COND_ILHS },
550 { {'~','>',0}, COND_IGT },
551 { {'>','=',0}, COND_GE },
552 { {'>','<',0}, COND_SS },
553 { {'<','<',0}, COND_LHS },
554 { {'<','>',0}, COND_NE },
555 { {'<','=',0}, COND_LE },
556 { {'>','>',0}, COND_RHS },
557 { {'>',0}, COND_GT },
558 { {'<',0}, COND_LT },
561 LPCWSTR p = &cond->str[cond->n];
566 len = lstrlenW( table[i].str );
567 if ( !len || 0 == strncmpW( table[i].str, p, len ) )
575 static int COND_GetOne( struct cond_str *str, COND_input *cond )
580 str->data = &cond->str[cond->n];
587 case '(': rc = COND_LPAR; break;
588 case ')': rc = COND_RPAR; break;
589 case '&': rc = COND_AMPER; break;
590 case '!': rc = COND_EXCLAM; break;
591 case '$': rc = COND_DOLLARS; break;
592 case '?': rc = COND_QUESTION; break;
593 case '%': rc = COND_PERCENT; break;
594 case ' ': rc = COND_SPACE; break;
595 case '=': rc = COND_EQ; break;
601 rc = COND_GetOperator( cond );
617 LPCWSTR p = strchrW( str->data + 1, '"' );
620 len = p - str->data + 1;
623 else if( COND_IsAlpha( ch ) )
625 static const WCHAR szNot[] = {'N','O','T',0};
626 static const WCHAR szAnd[] = {'A','N','D',0};
627 static const WCHAR szXor[] = {'X','O','R',0};
628 static const WCHAR szEqv[] = {'E','Q','V',0};
629 static const WCHAR szImp[] = {'I','M','P',0};
630 static const WCHAR szOr[] = {'O','R',0};
632 while( COND_IsIdent( str->data[len] ) )
638 if ( !strncmpiW( str->data, szNot, len ) )
640 else if( !strncmpiW( str->data, szAnd, len ) )
642 else if( !strncmpiW( str->data, szXor, len ) )
644 else if( !strncmpiW( str->data, szEqv, len ) )
646 else if( !strncmpiW( str->data, szImp, len ) )
649 else if( (len == 2) && !strncmpiW( str->data, szOr, len ) )
652 else if( COND_IsNumber( ch ) )
654 while( COND_IsNumber( str->data[len] ) )
660 ERR("Got unknown character %c(%x)\n",ch,ch);
670 static int cond_lex( void *COND_lval, COND_input *cond )
673 struct cond_str *str = COND_lval;
676 rc = COND_GetOne( str, cond );
677 } while (rc == COND_SPACE);
682 static LPWSTR COND_GetString( struct cond_str *str )
686 ret = msi_alloc( (str->len+1) * sizeof (WCHAR) );
689 memcpy( ret, str->data, str->len * sizeof(WCHAR));
692 TRACE("Got identifier %s\n",debugstr_w(ret));
696 static LPWSTR COND_GetLiteral( struct cond_str *str )
700 ret = msi_alloc( (str->len-1) * sizeof (WCHAR) );
703 memcpy( ret, str->data+1, (str->len-2) * sizeof(WCHAR) );
706 TRACE("Got literal %s\n",debugstr_w(ret));
710 static int cond_error(const char *str)
716 MSICONDITION MSI_EvaluateConditionW( MSIPACKAGE *package, LPCWSTR szCondition )
721 TRACE("%s\n", debugstr_w( szCondition ) );
723 if ( szCondition == NULL )
724 return MSICONDITION_NONE;
726 cond.package = package;
727 cond.str = szCondition;
729 cond.result = MSICONDITION_ERROR;
731 if ( !cond_parse( &cond ) )
734 r = MSICONDITION_ERROR;
736 TRACE("%i <- %s\n", r, debugstr_w(szCondition));
740 MSICONDITION WINAPI MsiEvaluateConditionW( MSIHANDLE hInstall, LPCWSTR szCondition )
745 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
747 return MSICONDITION_ERROR;
748 ret = MSI_EvaluateConditionW( package, szCondition );
749 msiobj_release( &package->hdr );
753 MSICONDITION WINAPI MsiEvaluateConditionA( MSIHANDLE hInstall, LPCSTR szCondition )
755 LPWSTR szwCond = NULL;
758 szwCond = strdupAtoW( szCondition );
759 if( szCondition && !szwCond )
760 return MSICONDITION_ERROR;
762 r = MsiEvaluateConditionW( hInstall, szwCond );