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
40 #include "msiserver.h"
41 #include "wine/debug.h"
42 #include "wine/unicode.h"
44 #define YYLEX_PARAM info
45 #define YYPARSE_PARAM info
47 static int cond_error(const char *str);
49 WINE_DEFAULT_DEBUG_CHANNEL(msi);
51 typedef struct tag_yyinput
64 static LPWSTR COND_GetString( const struct cond_str *str );
65 static LPWSTR COND_GetLiteral( const struct cond_str *str );
66 static int cond_lex( void *COND_lval, COND_input *info);
67 static const WCHAR szEmpty[] = { 0 };
69 static INT compare_int( INT a, INT operator, INT b );
70 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b, BOOL convert );
72 static INT compare_and_free_strings( LPWSTR a, INT op, LPWSTR b, BOOL convert )
76 r = compare_string( a, op, b, convert );
82 static BOOL num_from_prop( LPCWSTR p, INT *val )
84 INT ret = 0, sign = 1;
97 if( *p < '0' || *p > '9' )
99 ret = ret*10 + (*p - '0');
117 %token COND_SPACE COND_EOF
118 %token COND_OR COND_AND COND_NOT COND_XOR COND_IMP COND_EQV
119 %token COND_LT COND_GT COND_EQ COND_NE COND_GE COND_LE
120 %token COND_ILT COND_IGT COND_IEQ COND_INE COND_IGE COND_ILE
121 %token COND_LPAR COND_RPAR COND_TILDA COND_SS COND_ISS
122 %token COND_ILHS COND_IRHS COND_LHS COND_RHS
123 %token COND_PERCENT COND_DOLLARS COND_QUESTION COND_AMPER COND_EXCLAM
124 %token <str> COND_IDENT <str> COND_NUMBER <str> COND_LITER
126 %nonassoc COND_ERROR COND_EOF
128 %type <value> expression boolean_term boolean_factor
129 %type <value> value_i integer operator
130 %type <string> identifier symbol_s value_s literal
137 COND_input* cond = (COND_input*) info;
142 COND_input* cond = (COND_input*) info;
143 cond->result = MSICONDITION_NONE;
152 | expression COND_OR boolean_term
156 | expression COND_IMP boolean_term
160 | expression COND_XOR boolean_term
162 $$ = ( $1 || $3 ) && !( $1 && $3 );
164 | expression COND_EQV boolean_term
166 $$ = ( $1 && $3 ) || ( !$1 && !$3 );
175 | boolean_term COND_AND boolean_factor
182 COND_NOT boolean_factor
192 $$ = ($1 && $1[0]) ? 1 : 0;
195 | value_i operator value_i
197 $$ = compare_int( $1, $2, $3 );
199 | symbol_s operator value_i
202 if (num_from_prop( $1, &num ))
203 $$ = compare_int( num, $2, $3 );
205 $$ = ($2 == COND_NE || $2 == COND_INE );
208 | value_i operator symbol_s
211 if (num_from_prop( $3, &num ))
212 $$ = compare_int( $1, $2, num );
214 $$ = ($2 == COND_NE || $2 == COND_INE );
217 | symbol_s operator symbol_s
219 $$ = compare_and_free_strings( $1, $2, $3, TRUE );
221 | symbol_s operator literal
223 $$ = compare_and_free_strings( $1, $2, $3, TRUE );
225 | literal operator symbol_s
227 $$ = compare_and_free_strings( $1, $2, $3, TRUE );
229 | literal operator literal
231 $$ = compare_and_free_strings( $1, $2, $3, FALSE );
233 | literal operator value_i
238 | value_i operator literal
243 | COND_LPAR expression COND_RPAR
250 /* common functions */
251 COND_EQ { $$ = COND_EQ; }
252 | COND_NE { $$ = COND_NE; }
253 | COND_LT { $$ = COND_LT; }
254 | COND_GT { $$ = COND_GT; }
255 | COND_LE { $$ = COND_LE; }
256 | COND_GE { $$ = COND_GE; }
257 | COND_SS { $$ = COND_SS; }
258 | COND_IEQ { $$ = COND_IEQ; }
259 | COND_INE { $$ = COND_INE; }
260 | COND_ILT { $$ = COND_ILT; }
261 | COND_IGT { $$ = COND_IGT; }
262 | COND_ILE { $$ = COND_ILE; }
263 | COND_IGE { $$ = COND_IGE; }
264 | COND_ISS { $$ = COND_ISS; }
265 | COND_LHS { $$ = COND_LHS; }
266 | COND_RHS { $$ = COND_RHS; }
267 | COND_ILHS { $$ = COND_ILHS; }
268 | COND_IRHS { $$ = COND_IRHS; }
285 $$ = COND_GetLiteral(&$1);
296 | COND_DOLLARS identifier
298 COND_input* cond = (COND_input*) info;
299 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
301 MSI_GetComponentStateW(cond->package, $2, &install, &action );
305 | COND_QUESTION identifier
307 COND_input* cond = (COND_input*) info;
308 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
310 MSI_GetComponentStateW(cond->package, $2, &install, &action );
314 | COND_AMPER identifier
316 COND_input* cond = (COND_input*) info;
317 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
319 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
320 if (action == INSTALLSTATE_UNKNOWN)
321 $$ = MSICONDITION_FALSE;
327 | COND_EXCLAM identifier
329 COND_input* cond = (COND_input*) info;
330 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
332 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
341 COND_input* cond = (COND_input*) info;
343 $$ = msi_dup_property( cond->package, $1 );
346 | COND_PERCENT identifier
348 UINT len = GetEnvironmentVariableW( $2, NULL, 0 );
352 $$ = msi_alloc( len*sizeof (WCHAR) );
353 GetEnvironmentVariableW( $2, $$, len );
362 $$ = COND_GetString(&$1);
371 LPWSTR szNum = COND_GetString(&$1);
382 static int COND_IsAlpha( WCHAR x )
384 return( ( ( x >= 'A' ) && ( x <= 'Z' ) ) ||
385 ( ( x >= 'a' ) && ( x <= 'z' ) ) ||
389 static int COND_IsNumber( WCHAR x )
391 return( (( x >= '0' ) && ( x <= '9' )) || (x =='-') || (x =='.') );
394 static WCHAR *strstriW( const WCHAR *str, const WCHAR *sub )
396 LPWSTR strlower, sublower, r;
397 strlower = CharLowerW( strdupW( str ) );
398 sublower = CharLowerW( strdupW( sub ) );
399 r = strstrW( strlower, sublower );
401 r = (LPWSTR)str + (r - strlower);
402 msi_free( strlower );
403 msi_free( sublower );
407 static BOOL str_is_number( LPCWSTR str )
414 for (i = 0; i < lstrlenW( str ); i++)
415 if (!isdigitW(str[i]))
421 static INT compare_substring( LPCWSTR a, INT operator, LPCWSTR b )
425 /* substring operators return 0 if LHS is missing */
429 /* substring operators return 1 if RHS is missing */
433 /* if both strings contain only numbers, use integer comparison */
436 if (str_is_number(a) && str_is_number(b))
437 return compare_int( lhs, operator, rhs );
442 return strstrW( a, b ) ? 1 : 0;
444 return strstriW( a, b ) ? 1 : 0;
446 return 0 == strncmpW( a, b, lstrlenW( b ) );
448 return 0 == lstrcmpW( a + (lstrlenW( a ) - lstrlenW( b )), b );
450 return 0 == strncmpiW( a, b, lstrlenW( b ) );
452 return 0 == lstrcmpiW( a + (lstrlenW( a ) - lstrlenW( b )), b );
454 ERR("invalid substring operator\n");
460 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b, BOOL convert )
462 if (operator >= COND_SS && operator <= COND_RHS)
463 return compare_substring( a, operator, b );
465 /* null and empty string are equivalent */
469 if (convert && str_is_number(a) && str_is_number(b))
470 return compare_int( atoiW(a), operator, atoiW(b) );
472 /* a or b may be NULL */
476 return -1 == lstrcmpW( a, b );
478 return 1 == lstrcmpW( a, b );
480 return 0 == lstrcmpW( a, b );
482 return 0 != lstrcmpW( a, b );
484 return -1 != lstrcmpW( a, b );
486 return 1 != lstrcmpW( a, b );
488 return -1 == lstrcmpiW( a, b );
490 return 1 == lstrcmpiW( a, b );
492 return 0 == lstrcmpiW( a, b );
494 return 0 != lstrcmpiW( a, b );
496 return -1 != lstrcmpiW( a, b );
498 return 1 != lstrcmpiW( a, b );
500 ERR("invalid string operator\n");
507 static INT compare_int( INT a, INT operator, INT b )
531 return ( a & b ) ? 1 : 0;
533 return ( ( a & 0xffff ) == b ) ? 1 : 0;
535 return ( ( (a>>16) & 0xffff ) == b ) ? 1 : 0;
537 ERR("invalid integer operator\n");
544 static int COND_IsIdent( WCHAR x )
546 return( COND_IsAlpha( x ) || COND_IsNumber( x ) || ( x == '_' )
547 || ( x == '#' ) || (x == '.') );
550 static int COND_GetOperator( COND_input *cond )
552 static const struct {
556 { {'~','=',0}, COND_IEQ },
557 { {'~','<','=',0}, COND_ILE },
558 { {'~','>','<',0}, COND_ISS },
559 { {'~','>','>',0}, COND_IRHS },
560 { {'~','<','>',0}, COND_INE },
561 { {'~','<',0}, COND_ILT },
562 { {'~','>','=',0}, COND_IGE },
563 { {'~','<','<',0}, COND_ILHS },
564 { {'~','>',0}, COND_IGT },
565 { {'>','=',0}, COND_GE },
566 { {'>','<',0}, COND_SS },
567 { {'<','<',0}, COND_LHS },
568 { {'<','>',0}, COND_NE },
569 { {'<','=',0}, COND_LE },
570 { {'>','>',0}, COND_RHS },
571 { {'>',0}, COND_GT },
572 { {'<',0}, COND_LT },
575 LPCWSTR p = &cond->str[cond->n];
580 len = lstrlenW( table[i].str );
581 if ( !len || 0 == strncmpW( table[i].str, p, len ) )
589 static int COND_GetOne( struct cond_str *str, COND_input *cond )
594 str->data = &cond->str[cond->n];
601 case '(': rc = COND_LPAR; break;
602 case ')': rc = COND_RPAR; break;
603 case '&': rc = COND_AMPER; break;
604 case '!': rc = COND_EXCLAM; break;
605 case '$': rc = COND_DOLLARS; break;
606 case '?': rc = COND_QUESTION; break;
607 case '%': rc = COND_PERCENT; break;
608 case ' ': rc = COND_SPACE; break;
609 case '=': rc = COND_EQ; break;
614 rc = COND_GetOperator( cond );
630 LPCWSTR p = strchrW( str->data + 1, '"' );
633 len = p - str->data + 1;
636 else if( COND_IsAlpha( ch ) )
638 static const WCHAR szNot[] = {'N','O','T',0};
639 static const WCHAR szAnd[] = {'A','N','D',0};
640 static const WCHAR szXor[] = {'X','O','R',0};
641 static const WCHAR szEqv[] = {'E','Q','V',0};
642 static const WCHAR szImp[] = {'I','M','P',0};
643 static const WCHAR szOr[] = {'O','R',0};
645 while( COND_IsIdent( str->data[len] ) )
651 if ( !strncmpiW( str->data, szNot, len ) )
653 else if( !strncmpiW( str->data, szAnd, len ) )
655 else if( !strncmpiW( str->data, szXor, len ) )
657 else if( !strncmpiW( str->data, szEqv, len ) )
659 else if( !strncmpiW( str->data, szImp, len ) )
662 else if( (len == 2) && !strncmpiW( str->data, szOr, len ) )
665 else if( COND_IsNumber( ch ) )
667 while( COND_IsNumber( str->data[len] ) )
673 ERR("Got unknown character %c(%x)\n",ch,ch);
683 static int cond_lex( void *COND_lval, COND_input *cond )
686 struct cond_str *str = COND_lval;
689 rc = COND_GetOne( str, cond );
690 } while (rc == COND_SPACE);
695 static LPWSTR COND_GetString( const struct cond_str *str )
699 ret = msi_alloc( (str->len+1) * sizeof (WCHAR) );
702 memcpy( ret, str->data, str->len * sizeof(WCHAR));
705 TRACE("Got identifier %s\n",debugstr_w(ret));
709 static LPWSTR COND_GetLiteral( const struct cond_str *str )
713 ret = msi_alloc( (str->len-1) * sizeof (WCHAR) );
716 memcpy( ret, str->data+1, (str->len-2) * sizeof(WCHAR) );
719 TRACE("Got literal %s\n",debugstr_w(ret));
723 static int cond_error(const char *str)
729 MSICONDITION MSI_EvaluateConditionW( MSIPACKAGE *package, LPCWSTR szCondition )
734 TRACE("%s\n", debugstr_w( szCondition ) );
736 if ( szCondition == NULL )
737 return MSICONDITION_NONE;
739 cond.package = package;
740 cond.str = szCondition;
742 cond.result = MSICONDITION_ERROR;
744 if ( !cond_parse( &cond ) )
747 r = MSICONDITION_ERROR;
749 TRACE("%i <- %s\n", r, debugstr_w(szCondition));
753 MSICONDITION WINAPI MsiEvaluateConditionW( MSIHANDLE hInstall, LPCWSTR szCondition )
758 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
763 IWineMsiRemotePackage *remote_package;
765 remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
767 return MSICONDITION_ERROR;
769 condition = SysAllocString( szCondition );
772 IWineMsiRemotePackage_Release( remote_package );
773 return ERROR_OUTOFMEMORY;
776 hr = IWineMsiRemotePackage_EvaluateCondition( remote_package, condition );
778 SysFreeString( condition );
779 IWineMsiRemotePackage_Release( remote_package );
783 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
784 return HRESULT_CODE(hr);
786 return ERROR_FUNCTION_FAILED;
789 return ERROR_SUCCESS;
792 ret = MSI_EvaluateConditionW( package, szCondition );
793 msiobj_release( &package->hdr );
797 MSICONDITION WINAPI MsiEvaluateConditionA( MSIHANDLE hInstall, LPCSTR szCondition )
799 LPWSTR szwCond = NULL;
802 szwCond = strdupAtoW( szCondition );
803 if( szCondition && !szwCond )
804 return MSICONDITION_ERROR;
806 r = MsiEvaluateConditionW( hInstall, szwCond );