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"
43 #include "wine/list.h"
45 #define YYLEX_PARAM info
46 #define YYPARSE_PARAM info
48 static int cond_error(const char *str);
50 WINE_DEFAULT_DEBUG_CHANNEL(msi);
52 typedef struct tag_yyinput
66 static LPWSTR COND_GetString( COND_input *info, const struct cond_str *str );
67 static LPWSTR COND_GetLiteral( COND_input *info, const struct cond_str *str );
68 static int cond_lex( void *COND_lval, COND_input *info);
70 static void *cond_alloc( COND_input *cond, unsigned int sz );
71 static void *cond_track_mem( COND_input *cond, void *ptr, unsigned int sz );
72 static void cond_free( void *ptr );
74 static INT compare_int( INT a, INT operator, INT b );
75 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b, BOOL convert );
77 static INT compare_and_free_strings( LPWSTR a, INT op, LPWSTR b, BOOL convert )
81 r = compare_string( a, op, b, convert );
87 static BOOL num_from_prop( LPCWSTR p, INT *val )
89 INT ret = 0, sign = 1;
102 if( *p < '0' || *p > '9' )
104 ret = ret*10 + (*p - '0');
122 %token COND_SPACE COND_EOF
123 %token COND_OR COND_AND COND_NOT COND_XOR COND_IMP COND_EQV
124 %token COND_LT COND_GT COND_EQ COND_NE COND_GE COND_LE
125 %token COND_ILT COND_IGT COND_IEQ COND_INE COND_IGE COND_ILE
126 %token COND_LPAR COND_RPAR COND_TILDA COND_SS COND_ISS
127 %token COND_ILHS COND_IRHS COND_LHS COND_RHS
128 %token COND_PERCENT COND_DOLLARS COND_QUESTION COND_AMPER COND_EXCLAM
129 %token <str> COND_IDENT <str> COND_NUMBER <str> COND_LITER
131 %nonassoc COND_ERROR COND_EOF
133 %type <value> expression boolean_term boolean_factor
134 %type <value> value_i integer operator
135 %type <string> identifier symbol_s value_s literal
142 COND_input* cond = (COND_input*) info;
147 COND_input* cond = (COND_input*) info;
148 cond->result = MSICONDITION_NONE;
157 | expression COND_OR boolean_term
161 | expression COND_IMP boolean_term
165 | expression COND_XOR boolean_term
167 $$ = ( $1 || $3 ) && !( $1 && $3 );
169 | expression COND_EQV boolean_term
171 $$ = ( $1 && $3 ) || ( !$1 && !$3 );
180 | boolean_term COND_AND boolean_factor
187 COND_NOT boolean_factor
197 $$ = ($1 && $1[0]) ? 1 : 0;
200 | value_i operator value_i
202 $$ = compare_int( $1, $2, $3 );
204 | symbol_s operator value_i
207 if (num_from_prop( $1, &num ))
208 $$ = compare_int( num, $2, $3 );
210 $$ = ($2 == COND_NE || $2 == COND_INE );
213 | value_i operator symbol_s
216 if (num_from_prop( $3, &num ))
217 $$ = compare_int( $1, $2, num );
219 $$ = ($2 == COND_NE || $2 == COND_INE );
222 | symbol_s operator symbol_s
224 $$ = compare_and_free_strings( $1, $2, $3, TRUE );
226 | symbol_s operator literal
228 $$ = compare_and_free_strings( $1, $2, $3, TRUE );
230 | literal operator symbol_s
232 $$ = compare_and_free_strings( $1, $2, $3, TRUE );
234 | literal operator literal
236 $$ = compare_and_free_strings( $1, $2, $3, FALSE );
238 | literal operator value_i
243 | value_i operator literal
248 | COND_LPAR expression COND_RPAR
255 /* common functions */
256 COND_EQ { $$ = COND_EQ; }
257 | COND_NE { $$ = COND_NE; }
258 | COND_LT { $$ = COND_LT; }
259 | COND_GT { $$ = COND_GT; }
260 | COND_LE { $$ = COND_LE; }
261 | COND_GE { $$ = COND_GE; }
262 | COND_SS { $$ = COND_SS; }
263 | COND_IEQ { $$ = COND_IEQ; }
264 | COND_INE { $$ = COND_INE; }
265 | COND_ILT { $$ = COND_ILT; }
266 | COND_IGT { $$ = COND_IGT; }
267 | COND_ILE { $$ = COND_ILE; }
268 | COND_IGE { $$ = COND_IGE; }
269 | COND_ISS { $$ = COND_ISS; }
270 | COND_LHS { $$ = COND_LHS; }
271 | COND_RHS { $$ = COND_RHS; }
272 | COND_ILHS { $$ = COND_ILHS; }
273 | COND_IRHS { $$ = COND_IRHS; }
290 COND_input* cond = (COND_input*) info;
291 $$ = COND_GetLiteral( cond, &$1 );
302 | COND_DOLLARS identifier
304 COND_input* cond = (COND_input*) info;
305 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
307 MSI_GetComponentStateW(cond->package, $2, &install, &action );
311 | COND_QUESTION identifier
313 COND_input* cond = (COND_input*) info;
314 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
316 MSI_GetComponentStateW(cond->package, $2, &install, &action );
320 | COND_AMPER identifier
322 COND_input* cond = (COND_input*) info;
323 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
325 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
326 if (action == INSTALLSTATE_UNKNOWN)
327 $$ = MSICONDITION_FALSE;
333 | COND_EXCLAM identifier
335 COND_input* cond = (COND_input*) info;
336 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
338 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
347 COND_input* cond = (COND_input*) info;
350 $$ = msi_dup_property( cond->package->db, $1 );
353 len = (lstrlenW($$) + 1) * sizeof (WCHAR);
354 $$ = cond_track_mem( cond, $$, len );
358 | COND_PERCENT identifier
360 COND_input* cond = (COND_input*) info;
361 UINT len = GetEnvironmentVariableW( $2, NULL, 0 );
365 $$ = cond_alloc( cond, len*sizeof (WCHAR) );
368 GetEnvironmentVariableW( $2, $$, len );
377 COND_input* cond = (COND_input*) info;
378 $$ = COND_GetString( cond, &$1 );
387 COND_input* cond = (COND_input*) info;
388 LPWSTR szNum = COND_GetString( cond, &$1 );
399 static int COND_IsAlpha( WCHAR x )
401 return( ( ( x >= 'A' ) && ( x <= 'Z' ) ) ||
402 ( ( x >= 'a' ) && ( x <= 'z' ) ) ||
406 static int COND_IsNumber( WCHAR x )
408 return( (( x >= '0' ) && ( x <= '9' )) || (x =='-') || (x =='.') );
411 static WCHAR *strstriW( const WCHAR *str, const WCHAR *sub )
413 LPWSTR strlower, sublower, r;
414 strlower = CharLowerW( strdupW( str ) );
415 sublower = CharLowerW( strdupW( sub ) );
416 r = strstrW( strlower, sublower );
418 r = (LPWSTR)str + (r - strlower);
419 msi_free( strlower );
420 msi_free( sublower );
424 static BOOL str_is_number( LPCWSTR str )
431 for (i = 0; i < lstrlenW( str ); i++)
432 if (!isdigitW(str[i]))
438 static INT compare_substring( LPCWSTR a, INT operator, LPCWSTR b )
442 /* substring operators return 0 if LHS is missing */
446 /* substring operators return 1 if RHS is missing */
450 /* if both strings contain only numbers, use integer comparison */
453 if (str_is_number(a) && str_is_number(b))
454 return compare_int( lhs, operator, rhs );
459 return strstrW( a, b ) ? 1 : 0;
461 return strstriW( a, b ) ? 1 : 0;
463 return 0 == strncmpW( a, b, lstrlenW( b ) );
465 return 0 == lstrcmpW( a + (lstrlenW( a ) - lstrlenW( b )), b );
467 return 0 == strncmpiW( a, b, lstrlenW( b ) );
469 return 0 == lstrcmpiW( a + (lstrlenW( a ) - lstrlenW( b )), b );
471 ERR("invalid substring operator\n");
477 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b, BOOL convert )
479 if (operator >= COND_SS && operator <= COND_RHS)
480 return compare_substring( a, operator, b );
482 /* null and empty string are equivalent */
486 if (convert && str_is_number(a) && str_is_number(b))
487 return compare_int( atoiW(a), operator, atoiW(b) );
489 /* a or b may be NULL */
493 return -1 == lstrcmpW( a, b );
495 return 1 == lstrcmpW( a, b );
497 return 0 == lstrcmpW( a, b );
499 return 0 != lstrcmpW( a, b );
501 return -1 != lstrcmpW( a, b );
503 return 1 != lstrcmpW( a, b );
505 return -1 == lstrcmpiW( a, b );
507 return 1 == lstrcmpiW( a, b );
509 return 0 == lstrcmpiW( a, b );
511 return 0 != lstrcmpiW( a, b );
513 return -1 != lstrcmpiW( a, b );
515 return 1 != lstrcmpiW( a, b );
517 ERR("invalid string operator\n");
524 static INT compare_int( INT a, INT operator, INT b )
548 return ( a & b ) ? 1 : 0;
550 return ( ( a & 0xffff ) == b ) ? 1 : 0;
552 return ( ( (a>>16) & 0xffff ) == b ) ? 1 : 0;
554 ERR("invalid integer operator\n");
561 static int COND_IsIdent( WCHAR x )
563 return( COND_IsAlpha( x ) || COND_IsNumber( x ) || ( x == '_' )
564 || ( x == '#' ) || (x == '.') );
567 static int COND_GetOperator( COND_input *cond )
569 static const struct {
573 { {'~','<','=',0}, COND_ILE },
574 { {'~','>','<',0}, COND_ISS },
575 { {'~','>','>',0}, COND_IRHS },
576 { {'~','<','>',0}, COND_INE },
577 { {'~','>','=',0}, COND_IGE },
578 { {'~','<','<',0}, COND_ILHS },
579 { {'~','=',0}, COND_IEQ },
580 { {'~','<',0}, COND_ILT },
581 { {'~','>',0}, COND_IGT },
582 { {'>','=',0}, COND_GE },
583 { {'>','<',0}, COND_SS },
584 { {'<','<',0}, COND_LHS },
585 { {'<','>',0}, COND_NE },
586 { {'<','=',0}, COND_LE },
587 { {'>','>',0}, COND_RHS },
588 { {'>',0}, COND_GT },
589 { {'<',0}, COND_LT },
592 LPCWSTR p = &cond->str[cond->n];
597 len = lstrlenW( table[i].str );
598 if ( !len || 0 == strncmpW( table[i].str, p, len ) )
606 static int COND_GetOne( struct cond_str *str, COND_input *cond )
611 str->data = &cond->str[cond->n];
618 case '(': rc = COND_LPAR; break;
619 case ')': rc = COND_RPAR; break;
620 case '&': rc = COND_AMPER; break;
621 case '!': rc = COND_EXCLAM; break;
622 case '$': rc = COND_DOLLARS; break;
623 case '?': rc = COND_QUESTION; break;
624 case '%': rc = COND_PERCENT; break;
625 case ' ': rc = COND_SPACE; break;
626 case '=': rc = COND_EQ; break;
631 rc = COND_GetOperator( cond );
647 LPCWSTR p = strchrW( str->data + 1, '"' );
650 len = p - str->data + 1;
653 else if( COND_IsAlpha( ch ) )
655 static const WCHAR szNot[] = {'N','O','T',0};
656 static const WCHAR szAnd[] = {'A','N','D',0};
657 static const WCHAR szXor[] = {'X','O','R',0};
658 static const WCHAR szEqv[] = {'E','Q','V',0};
659 static const WCHAR szImp[] = {'I','M','P',0};
660 static const WCHAR szOr[] = {'O','R',0};
662 while( COND_IsIdent( str->data[len] ) )
668 if ( !strncmpiW( str->data, szNot, len ) )
670 else if( !strncmpiW( str->data, szAnd, len ) )
672 else if( !strncmpiW( str->data, szXor, len ) )
674 else if( !strncmpiW( str->data, szEqv, len ) )
676 else if( !strncmpiW( str->data, szImp, len ) )
679 else if( (len == 2) && !strncmpiW( str->data, szOr, len ) )
682 else if( COND_IsNumber( ch ) )
684 while( COND_IsNumber( str->data[len] ) )
690 ERR("Got unknown character %c(%x)\n",ch,ch);
700 static int cond_lex( void *COND_lval, COND_input *cond )
703 struct cond_str *str = COND_lval;
706 rc = COND_GetOne( str, cond );
707 } while (rc == COND_SPACE);
712 static LPWSTR COND_GetString( COND_input *cond, const struct cond_str *str )
716 ret = cond_alloc( cond, (str->len+1) * sizeof (WCHAR) );
719 memcpy( ret, str->data, str->len * sizeof(WCHAR));
722 TRACE("Got identifier %s\n",debugstr_w(ret));
726 static LPWSTR COND_GetLiteral( COND_input *cond, const struct cond_str *str )
730 ret = cond_alloc( cond, (str->len-1) * sizeof (WCHAR) );
733 memcpy( ret, str->data+1, (str->len-2) * sizeof(WCHAR) );
736 TRACE("Got literal %s\n",debugstr_w(ret));
740 static void *cond_alloc( COND_input *cond, unsigned int sz )
744 mem = msi_alloc( sizeof (struct list) + sz );
748 list_add_head( &(cond->mem), mem );
752 static void *cond_track_mem( COND_input *cond, void *ptr, unsigned int sz )
759 new_ptr = cond_alloc( cond, sz );
766 memcpy( new_ptr, ptr, sz );
771 static void cond_free( void *ptr )
773 struct list *mem = (struct list *)ptr - 1;
782 static int cond_error(const char *str)
788 MSICONDITION MSI_EvaluateConditionW( MSIPACKAGE *package, LPCWSTR szCondition )
792 struct list *mem, *safety;
794 TRACE("%s\n", debugstr_w( szCondition ) );
796 if ( szCondition == NULL )
797 return MSICONDITION_NONE;
799 cond.package = package;
800 cond.str = szCondition;
802 cond.result = MSICONDITION_ERROR;
804 list_init( &cond.mem );
806 if ( !cond_parse( &cond ) )
809 r = MSICONDITION_ERROR;
811 LIST_FOR_EACH_SAFE( mem, safety, &cond.mem )
813 /* The tracked memory lives directly after the list struct */
815 if ( r != MSICONDITION_ERROR )
816 WARN( "condition parser failed to free up some memory: %p\n", ptr );
820 TRACE("%i <- %s\n", r, debugstr_w(szCondition));
824 MSICONDITION WINAPI MsiEvaluateConditionW( MSIHANDLE hInstall, LPCWSTR szCondition )
829 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
834 IWineMsiRemotePackage *remote_package;
836 remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
838 return MSICONDITION_ERROR;
840 condition = SysAllocString( szCondition );
843 IWineMsiRemotePackage_Release( remote_package );
844 return ERROR_OUTOFMEMORY;
847 hr = IWineMsiRemotePackage_EvaluateCondition( remote_package, condition );
849 SysFreeString( condition );
850 IWineMsiRemotePackage_Release( remote_package );
854 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
855 return HRESULT_CODE(hr);
857 return ERROR_FUNCTION_FAILED;
860 return ERROR_SUCCESS;
863 ret = MSI_EvaluateConditionW( package, szCondition );
864 msiobj_release( &package->hdr );
868 MSICONDITION WINAPI MsiEvaluateConditionA( MSIHANDLE hInstall, LPCSTR szCondition )
870 LPWSTR szwCond = NULL;
873 szwCond = strdupAtoW( szCondition );
874 if( szCondition && !szwCond )
875 return MSICONDITION_ERROR;
877 r = MsiEvaluateConditionW( hInstall, szwCond );