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"
39 #define YYLEX_PARAM info
40 #define YYPARSE_PARAM info
42 static int cond_error(const char *str);
44 WINE_DEFAULT_DEBUG_CHANNEL(msi);
46 typedef struct tag_yyinput
59 static LPWSTR COND_GetString( struct cond_str *str );
60 static LPWSTR COND_GetLiteral( struct cond_str *str );
61 static int cond_lex( void *COND_lval, COND_input *info);
62 static const WCHAR szEmpty[] = { 0 };
64 static INT compare_int( INT a, INT operator, INT b );
65 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b );
67 static INT compare_and_free_strings( LPWSTR a, INT op, LPWSTR b )
71 r = compare_string( a, op, b );
77 static BOOL num_from_prop( LPCWSTR p, INT *val )
79 INT ret = 0, sign = 1;
92 if( *p < '0' || *p > '9' )
94 ret = ret*10 + (*p - '0');
112 %token COND_SPACE COND_EOF
113 %token COND_OR COND_AND COND_NOT COND_XOR COND_IMP COND_EQV
114 %token COND_LT COND_GT COND_EQ COND_NE COND_GE COND_LE
115 %token COND_ILT COND_IGT COND_IEQ COND_INE COND_IGE COND_ILE
116 %token COND_LPAR COND_RPAR COND_TILDA COND_SS COND_ISS
117 %token COND_ILHS COND_IRHS COND_LHS COND_RHS
118 %token COND_PERCENT COND_DOLLARS COND_QUESTION COND_AMPER COND_EXCLAM
119 %token <str> COND_IDENT <str> COND_NUMBER <str> COND_LITER
121 %nonassoc COND_ERROR COND_EOF
123 %type <value> expression boolean_term boolean_factor
124 %type <value> value_i integer operator
125 %type <string> identifier symbol_s value_s literal
132 COND_input* cond = (COND_input*) info;
137 COND_input* cond = (COND_input*) info;
138 cond->result = MSICONDITION_NONE;
147 | expression COND_OR boolean_term
151 | expression COND_IMP boolean_term
155 | expression COND_XOR boolean_term
157 $$ = ( $1 || $3 ) && !( $1 && $3 );
159 | expression COND_EQV boolean_term
161 $$ = ( $1 && $3 ) || ( !$1 && !$3 );
170 | boolean_term COND_AND boolean_factor
177 COND_NOT boolean_factor
187 $$ = ($1 && $1[0]) ? 1 : 0;
190 | value_i operator value_i
192 $$ = compare_int( $1, $2, $3 );
194 | symbol_s operator value_i
197 if (num_from_prop( $1, &num ))
198 $$ = compare_int( num, $2, $3 );
200 $$ = ($2 == COND_NE || $2 == COND_INE );
203 | value_i operator symbol_s
206 if (num_from_prop( $3, &num ))
207 $$ = compare_int( $1, $2, num );
209 $$ = ($2 == COND_NE || $2 == COND_INE );
212 | symbol_s operator symbol_s
214 $$ = compare_and_free_strings( $1, $2, $3 );
216 | symbol_s operator literal
218 $$ = compare_and_free_strings( $1, $2, $3 );
220 | literal operator symbol_s
222 $$ = compare_and_free_strings( $1, $2, $3 );
224 | literal operator literal
226 $$ = compare_and_free_strings( $1, $2, $3 );
228 | literal operator value_i
233 | value_i operator literal
238 | COND_LPAR expression COND_RPAR
245 /* common functions */
246 COND_EQ { $$ = COND_EQ; }
247 | COND_NE { $$ = COND_NE; }
248 | COND_LT { $$ = COND_LT; }
249 | COND_GT { $$ = COND_GT; }
250 | COND_LE { $$ = COND_LE; }
251 | COND_GE { $$ = COND_GE; }
252 | COND_SS { $$ = COND_SS; }
253 | COND_IEQ { $$ = COND_IEQ; }
254 | COND_INE { $$ = COND_INE; }
255 | COND_ILT { $$ = COND_ILT; }
256 | COND_IGT { $$ = COND_IGT; }
257 | COND_ILE { $$ = COND_ILE; }
258 | COND_IGE { $$ = COND_IGE; }
259 | COND_ISS { $$ = COND_ISS; }
260 | COND_LHS { $$ = COND_LHS; }
261 | COND_RHS { $$ = COND_RHS; }
262 | COND_ILHS { $$ = COND_ILHS; }
263 | COND_IRHS { $$ = COND_IRHS; }
280 $$ = COND_GetLiteral(&$1);
291 | COND_DOLLARS identifier
293 COND_input* cond = (COND_input*) info;
294 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
296 MSI_GetComponentStateW(cond->package, $2, &install, &action );
300 | COND_QUESTION identifier
302 COND_input* cond = (COND_input*) info;
303 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
305 MSI_GetComponentStateW(cond->package, $2, &install, &action );
309 | COND_AMPER identifier
311 COND_input* cond = (COND_input*) info;
312 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
314 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
318 | COND_EXCLAM identifier
320 COND_input* cond = (COND_input*) info;
321 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
323 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
332 COND_input* cond = (COND_input*) info;
334 $$ = msi_dup_property( cond->package, $1 );
337 | COND_PERCENT identifier
339 UINT len = GetEnvironmentVariableW( $2, NULL, 0 );
343 $$ = msi_alloc( len*sizeof (WCHAR) );
344 GetEnvironmentVariableW( $2, $$, len );
353 $$ = COND_GetString(&$1);
362 LPWSTR szNum = COND_GetString(&$1);
373 static int COND_IsAlpha( WCHAR x )
375 return( ( ( x >= 'A' ) && ( x <= 'Z' ) ) ||
376 ( ( x >= 'a' ) && ( x <= 'z' ) ) ||
380 static int COND_IsNumber( WCHAR x )
382 return( (( x >= '0' ) && ( x <= '9' )) || (x =='-') || (x =='.') );
385 static WCHAR *strstriW( const WCHAR *str, const WCHAR *sub )
387 LPWSTR strlower, sublower, r;
388 strlower = CharLowerW( strdupW( str ) );
389 sublower = CharLowerW( strdupW( sub ) );
390 r = strstrW( strlower, sublower );
392 r = (LPWSTR)str + (r - strlower);
393 msi_free( strlower );
394 msi_free( sublower );
398 static BOOL str_is_number( LPCWSTR str )
402 for (i = 0; i < lstrlenW( str ); i++)
403 if (!isdigitW(str[i]))
409 static INT compare_substring( LPCWSTR a, INT operator, LPCWSTR b )
413 /* substring operators return 0 if LHS is missing */
417 /* substring operators return 1 if RHS is missing */
421 /* if both strings contain only numbers, use integer comparison */
424 if (str_is_number(a) && str_is_number(b))
425 return compare_int( lhs, operator, rhs );
430 return strstrW( a, b ) ? 1 : 0;
432 return strstriW( a, b ) ? 1 : 0;
434 return 0 == strncmpW( a, b, lstrlenW( b ) );
436 return 0 == lstrcmpW( a + (lstrlenW( a ) - lstrlenW( b )), b );
438 return 0 == strncmpiW( a, b, lstrlenW( b ) );
440 return 0 == lstrcmpiW( a + (lstrlenW( a ) - lstrlenW( b )), b );
442 ERR("invalid substring operator\n");
448 static BOOL is_empty( LPCWSTR p )
453 static BOOL is_alphaless( LPCWSTR p )
457 if (isalphaW( *p ) || (*p == '_'))
464 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b )
468 if (operator >= COND_SS && operator <= COND_RHS)
469 return compare_substring( a, operator, b );
471 if (is_empty( a ) && is_empty( b ))
474 else if (is_empty( a ) && is_alphaless( b ))
477 else if (is_empty( b ) && is_alphaless( a ))
482 /* null and empty string are equivalent */
494 r = lstrcmpW( a, b );
502 r = lstrcmpiW( a, b );
505 ERR("invalid string operator\n");
531 ERR("invalid string operator\n");
537 static INT compare_int( INT a, INT operator, INT b )
561 return ( a & b ) ? 1 : 0;
563 return ( ( a & 0xffff ) == b ) ? 1 : 0;
565 return ( ( (a>>16) & 0xffff ) == b ) ? 1 : 0;
567 ERR("invalid integer operator\n");
574 static int COND_IsIdent( WCHAR x )
576 return( COND_IsAlpha( x ) || COND_IsNumber( x ) || ( x == '_' )
577 || ( x == '#' ) || (x == '.') );
580 static int COND_GetOperator( COND_input *cond )
582 static const struct {
586 { {'~','=',0}, COND_IEQ },
587 { {'~','<','=',0}, COND_ILE },
588 { {'~','>','<',0}, COND_ISS },
589 { {'~','>','>',0}, COND_IRHS },
590 { {'~','<','>',0}, COND_INE },
591 { {'~','<',0}, COND_ILT },
592 { {'~','>','=',0}, COND_IGE },
593 { {'~','<','<',0}, COND_ILHS },
594 { {'~','>',0}, COND_IGT },
595 { {'>','=',0}, COND_GE },
596 { {'>','<',0}, COND_SS },
597 { {'<','<',0}, COND_LHS },
598 { {'<','>',0}, COND_NE },
599 { {'<','=',0}, COND_LE },
600 { {'>','>',0}, COND_RHS },
601 { {'>',0}, COND_GT },
602 { {'<',0}, COND_LT },
605 LPCWSTR p = &cond->str[cond->n];
610 len = lstrlenW( table[i].str );
611 if ( !len || 0 == strncmpW( table[i].str, p, len ) )
619 static int COND_GetOne( struct cond_str *str, COND_input *cond )
624 str->data = &cond->str[cond->n];
631 case '(': rc = COND_LPAR; break;
632 case ')': rc = COND_RPAR; break;
633 case '&': rc = COND_AMPER; break;
634 case '!': rc = COND_EXCLAM; break;
635 case '$': rc = COND_DOLLARS; break;
636 case '?': rc = COND_QUESTION; break;
637 case '%': rc = COND_PERCENT; break;
638 case ' ': rc = COND_SPACE; break;
639 case '=': rc = COND_EQ; break;
645 rc = COND_GetOperator( cond );
661 LPCWSTR p = strchrW( str->data + 1, '"' );
664 len = p - str->data + 1;
667 else if( COND_IsAlpha( ch ) )
669 static const WCHAR szNot[] = {'N','O','T',0};
670 static const WCHAR szAnd[] = {'A','N','D',0};
671 static const WCHAR szXor[] = {'X','O','R',0};
672 static const WCHAR szEqv[] = {'E','Q','V',0};
673 static const WCHAR szImp[] = {'I','M','P',0};
674 static const WCHAR szOr[] = {'O','R',0};
676 while( COND_IsIdent( str->data[len] ) )
682 if ( !strncmpiW( str->data, szNot, len ) )
684 else if( !strncmpiW( str->data, szAnd, len ) )
686 else if( !strncmpiW( str->data, szXor, len ) )
688 else if( !strncmpiW( str->data, szEqv, len ) )
690 else if( !strncmpiW( str->data, szImp, len ) )
693 else if( (len == 2) && !strncmpiW( str->data, szOr, len ) )
696 else if( COND_IsNumber( ch ) )
698 while( COND_IsNumber( str->data[len] ) )
704 ERR("Got unknown character %c(%x)\n",ch,ch);
714 static int cond_lex( void *COND_lval, COND_input *cond )
717 struct cond_str *str = COND_lval;
720 rc = COND_GetOne( str, cond );
721 } while (rc == COND_SPACE);
726 static LPWSTR COND_GetString( struct cond_str *str )
730 ret = msi_alloc( (str->len+1) * sizeof (WCHAR) );
733 memcpy( ret, str->data, str->len * sizeof(WCHAR));
736 TRACE("Got identifier %s\n",debugstr_w(ret));
740 static LPWSTR COND_GetLiteral( struct cond_str *str )
744 ret = msi_alloc( (str->len-1) * sizeof (WCHAR) );
747 memcpy( ret, str->data+1, (str->len-2) * sizeof(WCHAR) );
750 TRACE("Got literal %s\n",debugstr_w(ret));
754 static int cond_error(const char *str)
760 MSICONDITION MSI_EvaluateConditionW( MSIPACKAGE *package, LPCWSTR szCondition )
765 TRACE("%s\n", debugstr_w( szCondition ) );
767 if ( szCondition == NULL )
768 return MSICONDITION_NONE;
770 cond.package = package;
771 cond.str = szCondition;
773 cond.result = MSICONDITION_ERROR;
775 if ( !cond_parse( &cond ) )
778 r = MSICONDITION_ERROR;
780 TRACE("%i <- %s\n", r, debugstr_w(szCondition));
784 MSICONDITION WINAPI MsiEvaluateConditionW( MSIHANDLE hInstall, LPCWSTR szCondition )
789 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
791 return MSICONDITION_ERROR;
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 );