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);
68 static INT compare_int( INT a, INT operator, INT b );
69 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b, BOOL convert );
71 static INT compare_and_free_strings( LPWSTR a, INT op, LPWSTR b, BOOL convert )
75 r = compare_string( a, op, b, convert );
81 static BOOL num_from_prop( LPCWSTR p, INT *val )
83 INT ret = 0, sign = 1;
96 if( *p < '0' || *p > '9' )
98 ret = ret*10 + (*p - '0');
116 %token COND_SPACE COND_EOF
117 %token COND_OR COND_AND COND_NOT COND_XOR COND_IMP COND_EQV
118 %token COND_LT COND_GT COND_EQ COND_NE COND_GE COND_LE
119 %token COND_ILT COND_IGT COND_IEQ COND_INE COND_IGE COND_ILE
120 %token COND_LPAR COND_RPAR COND_TILDA COND_SS COND_ISS
121 %token COND_ILHS COND_IRHS COND_LHS COND_RHS
122 %token COND_PERCENT COND_DOLLARS COND_QUESTION COND_AMPER COND_EXCLAM
123 %token <str> COND_IDENT <str> COND_NUMBER <str> COND_LITER
125 %nonassoc COND_ERROR COND_EOF
127 %type <value> expression boolean_term boolean_factor
128 %type <value> value_i integer operator
129 %type <string> identifier symbol_s value_s literal
136 COND_input* cond = (COND_input*) info;
141 COND_input* cond = (COND_input*) info;
142 cond->result = MSICONDITION_NONE;
151 | expression COND_OR boolean_term
155 | expression COND_IMP boolean_term
159 | expression COND_XOR boolean_term
161 $$ = ( $1 || $3 ) && !( $1 && $3 );
163 | expression COND_EQV boolean_term
165 $$ = ( $1 && $3 ) || ( !$1 && !$3 );
174 | boolean_term COND_AND boolean_factor
181 COND_NOT boolean_factor
191 $$ = ($1 && $1[0]) ? 1 : 0;
194 | value_i operator value_i
196 $$ = compare_int( $1, $2, $3 );
198 | symbol_s operator value_i
201 if (num_from_prop( $1, &num ))
202 $$ = compare_int( num, $2, $3 );
204 $$ = ($2 == COND_NE || $2 == COND_INE );
207 | value_i operator symbol_s
210 if (num_from_prop( $3, &num ))
211 $$ = compare_int( $1, $2, num );
213 $$ = ($2 == COND_NE || $2 == COND_INE );
216 | symbol_s operator symbol_s
218 $$ = compare_and_free_strings( $1, $2, $3, TRUE );
220 | symbol_s operator literal
222 $$ = compare_and_free_strings( $1, $2, $3, TRUE );
224 | literal operator symbol_s
226 $$ = compare_and_free_strings( $1, $2, $3, TRUE );
228 | literal operator literal
230 $$ = compare_and_free_strings( $1, $2, $3, FALSE );
232 | literal operator value_i
237 | value_i operator literal
242 | COND_LPAR expression COND_RPAR
249 /* common functions */
250 COND_EQ { $$ = COND_EQ; }
251 | COND_NE { $$ = COND_NE; }
252 | COND_LT { $$ = COND_LT; }
253 | COND_GT { $$ = COND_GT; }
254 | COND_LE { $$ = COND_LE; }
255 | COND_GE { $$ = COND_GE; }
256 | COND_SS { $$ = COND_SS; }
257 | COND_IEQ { $$ = COND_IEQ; }
258 | COND_INE { $$ = COND_INE; }
259 | COND_ILT { $$ = COND_ILT; }
260 | COND_IGT { $$ = COND_IGT; }
261 | COND_ILE { $$ = COND_ILE; }
262 | COND_IGE { $$ = COND_IGE; }
263 | COND_ISS { $$ = COND_ISS; }
264 | COND_LHS { $$ = COND_LHS; }
265 | COND_RHS { $$ = COND_RHS; }
266 | COND_ILHS { $$ = COND_ILHS; }
267 | COND_IRHS { $$ = COND_IRHS; }
284 $$ = COND_GetLiteral(&$1);
295 | COND_DOLLARS identifier
297 COND_input* cond = (COND_input*) info;
298 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
300 MSI_GetComponentStateW(cond->package, $2, &install, &action );
304 | COND_QUESTION identifier
306 COND_input* cond = (COND_input*) info;
307 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
309 MSI_GetComponentStateW(cond->package, $2, &install, &action );
313 | COND_AMPER identifier
315 COND_input* cond = (COND_input*) info;
316 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
318 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
319 if (action == INSTALLSTATE_UNKNOWN)
320 $$ = MSICONDITION_FALSE;
326 | COND_EXCLAM identifier
328 COND_input* cond = (COND_input*) info;
329 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
331 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
340 COND_input* cond = (COND_input*) info;
342 $$ = msi_dup_property( cond->package, $1 );
345 | COND_PERCENT identifier
347 UINT len = GetEnvironmentVariableW( $2, NULL, 0 );
351 $$ = msi_alloc( len*sizeof (WCHAR) );
352 GetEnvironmentVariableW( $2, $$, len );
361 $$ = COND_GetString(&$1);
370 LPWSTR szNum = COND_GetString(&$1);
381 static int COND_IsAlpha( WCHAR x )
383 return( ( ( x >= 'A' ) && ( x <= 'Z' ) ) ||
384 ( ( x >= 'a' ) && ( x <= 'z' ) ) ||
388 static int COND_IsNumber( WCHAR x )
390 return( (( x >= '0' ) && ( x <= '9' )) || (x =='-') || (x =='.') );
393 static WCHAR *strstriW( const WCHAR *str, const WCHAR *sub )
395 LPWSTR strlower, sublower, r;
396 strlower = CharLowerW( strdupW( str ) );
397 sublower = CharLowerW( strdupW( sub ) );
398 r = strstrW( strlower, sublower );
400 r = (LPWSTR)str + (r - strlower);
401 msi_free( strlower );
402 msi_free( sublower );
406 static BOOL str_is_number( LPCWSTR str )
413 for (i = 0; i < lstrlenW( str ); i++)
414 if (!isdigitW(str[i]))
420 static INT compare_substring( LPCWSTR a, INT operator, LPCWSTR b )
424 /* substring operators return 0 if LHS is missing */
428 /* substring operators return 1 if RHS is missing */
432 /* if both strings contain only numbers, use integer comparison */
435 if (str_is_number(a) && str_is_number(b))
436 return compare_int( lhs, operator, rhs );
441 return strstrW( a, b ) ? 1 : 0;
443 return strstriW( a, b ) ? 1 : 0;
445 return 0 == strncmpW( a, b, lstrlenW( b ) );
447 return 0 == lstrcmpW( a + (lstrlenW( a ) - lstrlenW( b )), b );
449 return 0 == strncmpiW( a, b, lstrlenW( b ) );
451 return 0 == lstrcmpiW( a + (lstrlenW( a ) - lstrlenW( b )), b );
453 ERR("invalid substring operator\n");
459 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b, BOOL convert )
461 if (operator >= COND_SS && operator <= COND_RHS)
462 return compare_substring( a, operator, b );
464 /* null and empty string are equivalent */
468 if (convert && str_is_number(a) && str_is_number(b))
469 return compare_int( atoiW(a), operator, atoiW(b) );
471 /* a or b may be NULL */
475 return -1 == lstrcmpW( a, b );
477 return 1 == lstrcmpW( a, b );
479 return 0 == lstrcmpW( a, b );
481 return 0 != lstrcmpW( a, b );
483 return -1 != lstrcmpW( a, b );
485 return 1 != lstrcmpW( a, b );
487 return -1 == lstrcmpiW( a, b );
489 return 1 == lstrcmpiW( a, b );
491 return 0 == lstrcmpiW( a, b );
493 return 0 != lstrcmpiW( a, b );
495 return -1 != lstrcmpiW( a, b );
497 return 1 != lstrcmpiW( a, b );
499 ERR("invalid string operator\n");
506 static INT compare_int( INT a, INT operator, INT b )
530 return ( a & b ) ? 1 : 0;
532 return ( ( a & 0xffff ) == b ) ? 1 : 0;
534 return ( ( (a>>16) & 0xffff ) == b ) ? 1 : 0;
536 ERR("invalid integer operator\n");
543 static int COND_IsIdent( WCHAR x )
545 return( COND_IsAlpha( x ) || COND_IsNumber( x ) || ( x == '_' )
546 || ( x == '#' ) || (x == '.') );
549 static int COND_GetOperator( COND_input *cond )
551 static const struct {
555 { {'~','<','=',0}, COND_ILE },
556 { {'~','>','<',0}, COND_ISS },
557 { {'~','>','>',0}, COND_IRHS },
558 { {'~','<','>',0}, COND_INE },
559 { {'~','>','=',0}, COND_IGE },
560 { {'~','<','<',0}, COND_ILHS },
561 { {'~','=',0}, COND_IEQ },
562 { {'~','<',0}, COND_ILT },
563 { {'~','>',0}, COND_IGT },
564 { {'>','=',0}, COND_GE },
565 { {'>','<',0}, COND_SS },
566 { {'<','<',0}, COND_LHS },
567 { {'<','>',0}, COND_NE },
568 { {'<','=',0}, COND_LE },
569 { {'>','>',0}, COND_RHS },
570 { {'>',0}, COND_GT },
571 { {'<',0}, COND_LT },
574 LPCWSTR p = &cond->str[cond->n];
579 len = lstrlenW( table[i].str );
580 if ( !len || 0 == strncmpW( table[i].str, p, len ) )
588 static int COND_GetOne( struct cond_str *str, COND_input *cond )
593 str->data = &cond->str[cond->n];
600 case '(': rc = COND_LPAR; break;
601 case ')': rc = COND_RPAR; break;
602 case '&': rc = COND_AMPER; break;
603 case '!': rc = COND_EXCLAM; break;
604 case '$': rc = COND_DOLLARS; break;
605 case '?': rc = COND_QUESTION; break;
606 case '%': rc = COND_PERCENT; break;
607 case ' ': rc = COND_SPACE; break;
608 case '=': rc = COND_EQ; break;
613 rc = COND_GetOperator( cond );
629 LPCWSTR p = strchrW( str->data + 1, '"' );
632 len = p - str->data + 1;
635 else if( COND_IsAlpha( ch ) )
637 static const WCHAR szNot[] = {'N','O','T',0};
638 static const WCHAR szAnd[] = {'A','N','D',0};
639 static const WCHAR szXor[] = {'X','O','R',0};
640 static const WCHAR szEqv[] = {'E','Q','V',0};
641 static const WCHAR szImp[] = {'I','M','P',0};
642 static const WCHAR szOr[] = {'O','R',0};
644 while( COND_IsIdent( str->data[len] ) )
650 if ( !strncmpiW( str->data, szNot, len ) )
652 else if( !strncmpiW( str->data, szAnd, len ) )
654 else if( !strncmpiW( str->data, szXor, len ) )
656 else if( !strncmpiW( str->data, szEqv, len ) )
658 else if( !strncmpiW( str->data, szImp, len ) )
661 else if( (len == 2) && !strncmpiW( str->data, szOr, len ) )
664 else if( COND_IsNumber( ch ) )
666 while( COND_IsNumber( str->data[len] ) )
672 ERR("Got unknown character %c(%x)\n",ch,ch);
682 static int cond_lex( void *COND_lval, COND_input *cond )
685 struct cond_str *str = COND_lval;
688 rc = COND_GetOne( str, cond );
689 } while (rc == COND_SPACE);
694 static LPWSTR COND_GetString( const struct cond_str *str )
698 ret = msi_alloc( (str->len+1) * sizeof (WCHAR) );
701 memcpy( ret, str->data, str->len * sizeof(WCHAR));
704 TRACE("Got identifier %s\n",debugstr_w(ret));
708 static LPWSTR COND_GetLiteral( const struct cond_str *str )
712 ret = msi_alloc( (str->len-1) * sizeof (WCHAR) );
715 memcpy( ret, str->data+1, (str->len-2) * sizeof(WCHAR) );
718 TRACE("Got literal %s\n",debugstr_w(ret));
722 static int cond_error(const char *str)
728 MSICONDITION MSI_EvaluateConditionW( MSIPACKAGE *package, LPCWSTR szCondition )
733 TRACE("%s\n", debugstr_w( szCondition ) );
735 if ( szCondition == NULL )
736 return MSICONDITION_NONE;
738 cond.package = package;
739 cond.str = szCondition;
741 cond.result = MSICONDITION_ERROR;
743 if ( !cond_parse( &cond ) )
746 r = MSICONDITION_ERROR;
748 TRACE("%i <- %s\n", r, debugstr_w(szCondition));
752 MSICONDITION WINAPI MsiEvaluateConditionW( MSIHANDLE hInstall, LPCWSTR szCondition )
757 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
762 IWineMsiRemotePackage *remote_package;
764 remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
766 return MSICONDITION_ERROR;
768 condition = SysAllocString( szCondition );
771 IWineMsiRemotePackage_Release( remote_package );
772 return ERROR_OUTOFMEMORY;
775 hr = IWineMsiRemotePackage_EvaluateCondition( remote_package, condition );
777 SysFreeString( condition );
778 IWineMsiRemotePackage_Release( remote_package );
782 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
783 return HRESULT_CODE(hr);
785 return ERROR_FUNCTION_FAILED;
788 return ERROR_SUCCESS;
791 ret = MSI_EvaluateConditionW( package, szCondition );
792 msiobj_release( &package->hdr );
796 MSICONDITION WINAPI MsiEvaluateConditionA( MSIHANDLE hInstall, LPCSTR szCondition )
798 LPWSTR szwCond = NULL;
801 szwCond = strdupAtoW( szCondition );
802 if( szCondition && !szwCond )
803 return MSICONDITION_ERROR;
805 r = MsiEvaluateConditionW( hInstall, szwCond );