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 ) != 0;
461 return strstriW( a, b ) != 0;
464 int l = strlenW( a );
465 int r = strlenW( b );
467 return !strncmpW( a, b, r );
471 int l = strlenW( a );
472 int r = strlenW( b );
474 return !strncmpW( a + (l - r), b, r );
478 int l = strlenW( a );
479 int r = strlenW( b );
481 return !strncmpiW( a, b, r );
485 int l = strlenW( a );
486 int r = strlenW( b );
488 return !strncmpiW( a + (l - r), b, r );
491 ERR("invalid substring operator\n");
497 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b, BOOL convert )
499 if (operator >= COND_SS && operator <= COND_RHS)
500 return compare_substring( a, operator, b );
502 /* null and empty string are equivalent */
506 if (convert && str_is_number(a) && str_is_number(b))
507 return compare_int( atoiW(a), operator, atoiW(b) );
509 /* a or b may be NULL */
513 return strcmpW( a, b ) < 0;
515 return strcmpW( a, b ) > 0;
517 return strcmpW( a, b ) == 0;
519 return strcmpW( a, b ) != 0;
521 return strcmpW( a, b ) >= 0;
523 return strcmpW( a, b ) <= 0;
525 return strcmpiW( a, b ) < 0;
527 return strcmpiW( a, b ) > 0;
529 return strcmpiW( a, b ) == 0;
531 return strcmpiW( a, b ) != 0;
533 return strcmpiW( a, b ) >= 0;
535 return strcmpiW( a, b ) <= 0;
537 ERR("invalid string operator\n");
544 static INT compare_int( INT a, INT operator, INT b )
568 return ( a & b ) ? 1 : 0;
570 return ( ( a & 0xffff ) == b ) ? 1 : 0;
572 return ( ( (a>>16) & 0xffff ) == b ) ? 1 : 0;
574 ERR("invalid integer operator\n");
581 static int COND_IsIdent( WCHAR x )
583 return( COND_IsAlpha( x ) || COND_IsNumber( x ) || ( x == '_' )
584 || ( x == '#' ) || (x == '.') );
587 static int COND_GetOperator( COND_input *cond )
589 static const struct {
593 { {'~','<','=',0}, COND_ILE },
594 { {'~','>','<',0}, COND_ISS },
595 { {'~','>','>',0}, COND_IRHS },
596 { {'~','<','>',0}, COND_INE },
597 { {'~','>','=',0}, COND_IGE },
598 { {'~','<','<',0}, COND_ILHS },
599 { {'~','=',0}, COND_IEQ },
600 { {'~','<',0}, COND_ILT },
601 { {'~','>',0}, COND_IGT },
602 { {'>','=',0}, COND_GE },
603 { {'>','<',0}, COND_SS },
604 { {'<','<',0}, COND_LHS },
605 { {'<','>',0}, COND_NE },
606 { {'<','=',0}, COND_LE },
607 { {'>','>',0}, COND_RHS },
608 { {'>',0}, COND_GT },
609 { {'<',0}, COND_LT },
612 LPCWSTR p = &cond->str[cond->n];
617 len = lstrlenW( table[i].str );
618 if ( !len || 0 == strncmpW( table[i].str, p, len ) )
626 static int COND_GetOne( struct cond_str *str, COND_input *cond )
631 str->data = &cond->str[cond->n];
638 case '(': rc = COND_LPAR; break;
639 case ')': rc = COND_RPAR; break;
640 case '&': rc = COND_AMPER; break;
641 case '!': rc = COND_EXCLAM; break;
642 case '$': rc = COND_DOLLARS; break;
643 case '?': rc = COND_QUESTION; break;
644 case '%': rc = COND_PERCENT; break;
645 case ' ': rc = COND_SPACE; break;
646 case '=': rc = COND_EQ; break;
651 rc = COND_GetOperator( cond );
667 LPCWSTR p = strchrW( str->data + 1, '"' );
670 len = p - str->data + 1;
673 else if( COND_IsAlpha( ch ) )
675 static const WCHAR szNot[] = {'N','O','T',0};
676 static const WCHAR szAnd[] = {'A','N','D',0};
677 static const WCHAR szXor[] = {'X','O','R',0};
678 static const WCHAR szEqv[] = {'E','Q','V',0};
679 static const WCHAR szImp[] = {'I','M','P',0};
680 static const WCHAR szOr[] = {'O','R',0};
682 while( COND_IsIdent( str->data[len] ) )
688 if ( !strncmpiW( str->data, szNot, len ) )
690 else if( !strncmpiW( str->data, szAnd, len ) )
692 else if( !strncmpiW( str->data, szXor, len ) )
694 else if( !strncmpiW( str->data, szEqv, len ) )
696 else if( !strncmpiW( str->data, szImp, len ) )
699 else if( (len == 2) && !strncmpiW( str->data, szOr, len ) )
702 else if( COND_IsNumber( ch ) )
704 while( COND_IsNumber( str->data[len] ) )
710 ERR("Got unknown character %c(%x)\n",ch,ch);
720 static int cond_lex( void *COND_lval, COND_input *cond )
723 struct cond_str *str = COND_lval;
726 rc = COND_GetOne( str, cond );
727 } while (rc == COND_SPACE);
732 static LPWSTR COND_GetString( COND_input *cond, const struct cond_str *str )
736 ret = cond_alloc( cond, (str->len+1) * sizeof (WCHAR) );
739 memcpy( ret, str->data, str->len * sizeof(WCHAR));
742 TRACE("Got identifier %s\n",debugstr_w(ret));
746 static LPWSTR COND_GetLiteral( COND_input *cond, const struct cond_str *str )
750 ret = cond_alloc( cond, (str->len-1) * sizeof (WCHAR) );
753 memcpy( ret, str->data+1, (str->len-2) * sizeof(WCHAR) );
756 TRACE("Got literal %s\n",debugstr_w(ret));
760 static void *cond_alloc( COND_input *cond, unsigned int sz )
764 mem = msi_alloc( sizeof (struct list) + sz );
768 list_add_head( &(cond->mem), mem );
772 static void *cond_track_mem( COND_input *cond, void *ptr, unsigned int sz )
779 new_ptr = cond_alloc( cond, sz );
786 memcpy( new_ptr, ptr, sz );
791 static void cond_free( void *ptr )
793 struct list *mem = (struct list *)ptr - 1;
802 static int cond_error(const char *str)
808 MSICONDITION MSI_EvaluateConditionW( MSIPACKAGE *package, LPCWSTR szCondition )
812 struct list *mem, *safety;
814 TRACE("%s\n", debugstr_w( szCondition ) );
816 if ( szCondition == NULL )
817 return MSICONDITION_NONE;
819 cond.package = package;
820 cond.str = szCondition;
822 cond.result = MSICONDITION_ERROR;
824 list_init( &cond.mem );
826 if ( !cond_parse( &cond ) )
829 r = MSICONDITION_ERROR;
831 LIST_FOR_EACH_SAFE( mem, safety, &cond.mem )
833 /* The tracked memory lives directly after the list struct */
835 if ( r != MSICONDITION_ERROR )
836 WARN( "condition parser failed to free up some memory: %p\n", ptr );
840 TRACE("%i <- %s\n", r, debugstr_w(szCondition));
844 MSICONDITION WINAPI MsiEvaluateConditionW( MSIHANDLE hInstall, LPCWSTR szCondition )
849 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
854 IWineMsiRemotePackage *remote_package;
856 remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
858 return MSICONDITION_ERROR;
860 condition = SysAllocString( szCondition );
863 IWineMsiRemotePackage_Release( remote_package );
864 return ERROR_OUTOFMEMORY;
867 hr = IWineMsiRemotePackage_EvaluateCondition( remote_package, condition );
869 SysFreeString( condition );
870 IWineMsiRemotePackage_Release( remote_package );
874 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
875 return HRESULT_CODE(hr);
877 return ERROR_FUNCTION_FAILED;
880 return ERROR_SUCCESS;
883 ret = MSI_EvaluateConditionW( package, szCondition );
884 msiobj_release( &package->hdr );
888 MSICONDITION WINAPI MsiEvaluateConditionA( MSIHANDLE hInstall, LPCSTR szCondition )
890 LPWSTR szwCond = NULL;
893 szwCond = strdupAtoW( szCondition );
894 if( szCondition && !szwCond )
895 return MSICONDITION_ERROR;
897 r = MsiEvaluateConditionW( hInstall, szwCond );