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 );
72 static INT compare_and_free_strings( LPWSTR a, INT op, LPWSTR b )
76 r = compare_string( a, op, b );
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 );
221 | symbol_s operator literal
223 $$ = compare_and_free_strings( $1, $2, $3 );
225 | literal operator symbol_s
227 $$ = compare_and_free_strings( $1, $2, $3 );
229 | literal operator literal
231 $$ = compare_and_free_strings( $1, $2, $3 );
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 );
323 | COND_EXCLAM identifier
325 COND_input* cond = (COND_input*) info;
326 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
328 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
337 COND_input* cond = (COND_input*) info;
339 $$ = msi_dup_property( cond->package, $1 );
342 | COND_PERCENT identifier
344 UINT len = GetEnvironmentVariableW( $2, NULL, 0 );
348 $$ = msi_alloc( len*sizeof (WCHAR) );
349 GetEnvironmentVariableW( $2, $$, len );
358 $$ = COND_GetString(&$1);
367 LPWSTR szNum = COND_GetString(&$1);
378 static int COND_IsAlpha( WCHAR x )
380 return( ( ( x >= 'A' ) && ( x <= 'Z' ) ) ||
381 ( ( x >= 'a' ) && ( x <= 'z' ) ) ||
385 static int COND_IsNumber( WCHAR x )
387 return( (( x >= '0' ) && ( x <= '9' )) || (x =='-') || (x =='.') );
390 static WCHAR *strstriW( const WCHAR *str, const WCHAR *sub )
392 LPWSTR strlower, sublower, r;
393 strlower = CharLowerW( strdupW( str ) );
394 sublower = CharLowerW( strdupW( sub ) );
395 r = strstrW( strlower, sublower );
397 r = (LPWSTR)str + (r - strlower);
398 msi_free( strlower );
399 msi_free( sublower );
403 static BOOL str_is_number( LPCWSTR str )
407 for (i = 0; i < lstrlenW( str ); i++)
408 if (!isdigitW(str[i]))
414 static INT compare_substring( LPCWSTR a, INT operator, LPCWSTR b )
418 /* substring operators return 0 if LHS is missing */
422 /* substring operators return 1 if RHS is missing */
426 /* if both strings contain only numbers, use integer comparison */
429 if (str_is_number(a) && str_is_number(b))
430 return compare_int( lhs, operator, rhs );
435 return strstrW( a, b ) ? 1 : 0;
437 return strstriW( a, b ) ? 1 : 0;
439 return 0 == strncmpW( a, b, lstrlenW( b ) );
441 return 0 == lstrcmpW( a + (lstrlenW( a ) - lstrlenW( b )), b );
443 return 0 == strncmpiW( a, b, lstrlenW( b ) );
445 return 0 == lstrcmpiW( a + (lstrlenW( a ) - lstrlenW( b )), b );
447 ERR("invalid substring operator\n");
453 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b )
455 if (operator >= COND_SS && operator <= COND_RHS)
456 return compare_substring( a, operator, b );
458 /* null and empty string are equivalent */
462 /* a or b may be NULL */
466 return -1 == lstrcmpW( a, b );
468 return 1 == lstrcmpW( a, b );
470 return 0 == lstrcmpW( a, b );
472 return 0 != lstrcmpW( a, b );
474 return -1 != lstrcmpW( a, b );
476 return 1 != lstrcmpW( a, b );
478 return -1 == lstrcmpiW( a, b );
480 return 1 == lstrcmpiW( a, b );
482 return 0 == lstrcmpiW( a, b );
484 return 0 != lstrcmpiW( a, b );
486 return -1 != lstrcmpiW( a, b );
488 return 1 != lstrcmpiW( a, b );
490 ERR("invalid string operator\n");
497 static INT compare_int( INT a, INT operator, INT b )
521 return ( a & b ) ? 1 : 0;
523 return ( ( a & 0xffff ) == b ) ? 1 : 0;
525 return ( ( (a>>16) & 0xffff ) == b ) ? 1 : 0;
527 ERR("invalid integer operator\n");
534 static int COND_IsIdent( WCHAR x )
536 return( COND_IsAlpha( x ) || COND_IsNumber( x ) || ( x == '_' )
537 || ( x == '#' ) || (x == '.') );
540 static int COND_GetOperator( COND_input *cond )
542 static const struct {
546 { {'~','=',0}, COND_IEQ },
547 { {'~','<','=',0}, COND_ILE },
548 { {'~','>','<',0}, COND_ISS },
549 { {'~','>','>',0}, COND_IRHS },
550 { {'~','<','>',0}, COND_INE },
551 { {'~','<',0}, COND_ILT },
552 { {'~','>','=',0}, COND_IGE },
553 { {'~','<','<',0}, COND_ILHS },
554 { {'~','>',0}, COND_IGT },
555 { {'>','=',0}, COND_GE },
556 { {'>','<',0}, COND_SS },
557 { {'<','<',0}, COND_LHS },
558 { {'<','>',0}, COND_NE },
559 { {'<','=',0}, COND_LE },
560 { {'>','>',0}, COND_RHS },
561 { {'>',0}, COND_GT },
562 { {'<',0}, COND_LT },
565 LPCWSTR p = &cond->str[cond->n];
570 len = lstrlenW( table[i].str );
571 if ( !len || 0 == strncmpW( table[i].str, p, len ) )
579 static int COND_GetOne( struct cond_str *str, COND_input *cond )
584 str->data = &cond->str[cond->n];
591 case '(': rc = COND_LPAR; break;
592 case ')': rc = COND_RPAR; break;
593 case '&': rc = COND_AMPER; break;
594 case '!': rc = COND_EXCLAM; break;
595 case '$': rc = COND_DOLLARS; break;
596 case '?': rc = COND_QUESTION; break;
597 case '%': rc = COND_PERCENT; break;
598 case ' ': rc = COND_SPACE; break;
599 case '=': rc = COND_EQ; break;
604 rc = COND_GetOperator( cond );
620 LPCWSTR p = strchrW( str->data + 1, '"' );
623 len = p - str->data + 1;
626 else if( COND_IsAlpha( ch ) )
628 static const WCHAR szNot[] = {'N','O','T',0};
629 static const WCHAR szAnd[] = {'A','N','D',0};
630 static const WCHAR szXor[] = {'X','O','R',0};
631 static const WCHAR szEqv[] = {'E','Q','V',0};
632 static const WCHAR szImp[] = {'I','M','P',0};
633 static const WCHAR szOr[] = {'O','R',0};
635 while( COND_IsIdent( str->data[len] ) )
641 if ( !strncmpiW( str->data, szNot, len ) )
643 else if( !strncmpiW( str->data, szAnd, len ) )
645 else if( !strncmpiW( str->data, szXor, len ) )
647 else if( !strncmpiW( str->data, szEqv, len ) )
649 else if( !strncmpiW( str->data, szImp, len ) )
652 else if( (len == 2) && !strncmpiW( str->data, szOr, len ) )
655 else if( COND_IsNumber( ch ) )
657 while( COND_IsNumber( str->data[len] ) )
663 ERR("Got unknown character %c(%x)\n",ch,ch);
673 static int cond_lex( void *COND_lval, COND_input *cond )
676 struct cond_str *str = COND_lval;
679 rc = COND_GetOne( str, cond );
680 } while (rc == COND_SPACE);
685 static LPWSTR COND_GetString( const struct cond_str *str )
689 ret = msi_alloc( (str->len+1) * sizeof (WCHAR) );
692 memcpy( ret, str->data, str->len * sizeof(WCHAR));
695 TRACE("Got identifier %s\n",debugstr_w(ret));
699 static LPWSTR COND_GetLiteral( const struct cond_str *str )
703 ret = msi_alloc( (str->len-1) * sizeof (WCHAR) );
706 memcpy( ret, str->data+1, (str->len-2) * sizeof(WCHAR) );
709 TRACE("Got literal %s\n",debugstr_w(ret));
713 static int cond_error(const char *str)
719 MSICONDITION MSI_EvaluateConditionW( MSIPACKAGE *package, LPCWSTR szCondition )
724 TRACE("%s\n", debugstr_w( szCondition ) );
726 if ( szCondition == NULL )
727 return MSICONDITION_NONE;
729 cond.package = package;
730 cond.str = szCondition;
732 cond.result = MSICONDITION_ERROR;
734 if ( !cond_parse( &cond ) )
737 r = MSICONDITION_ERROR;
739 TRACE("%i <- %s\n", r, debugstr_w(szCondition));
743 MSICONDITION WINAPI MsiEvaluateConditionW( MSIHANDLE hInstall, LPCWSTR szCondition )
748 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
753 IWineMsiRemotePackage *remote_package;
755 remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
757 return MSICONDITION_ERROR;
759 condition = SysAllocString( szCondition );
762 IWineMsiRemotePackage_Release( remote_package );
763 return ERROR_OUTOFMEMORY;
766 hr = IWineMsiRemotePackage_EvaluateCondition( remote_package, condition );
768 SysFreeString( condition );
769 IWineMsiRemotePackage_Release( remote_package );
773 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
774 return HRESULT_CODE(hr);
776 return ERROR_FUNCTION_FAILED;
779 return ERROR_SUCCESS;
782 ret = MSI_EvaluateConditionW( package, szCondition );
783 msiobj_release( &package->hdr );
787 MSICONDITION WINAPI MsiEvaluateConditionA( MSIHANDLE hInstall, LPCSTR szCondition )
789 LPWSTR szwCond = NULL;
792 szwCond = strdupAtoW( szCondition );
793 if( szCondition && !szwCond )
794 return MSICONDITION_ERROR;
796 r = MsiEvaluateConditionW( hInstall, szwCond );