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 WINE_DEFAULT_DEBUG_CHANNEL(msi);
47 typedef struct tag_yyinput
73 static LPWSTR COND_GetString( COND_input *info, const struct cond_str *str );
74 static LPWSTR COND_GetLiteral( COND_input *info, const struct cond_str *str );
75 static int cond_lex( void *COND_lval, COND_input *info);
76 static int cond_error( COND_input *info, const char *str);
78 static void *cond_alloc( COND_input *cond, unsigned int sz );
79 static void *cond_track_mem( COND_input *cond, void *ptr, unsigned int sz );
80 static void cond_free( void *ptr );
82 static INT compare_int( INT a, INT operator, INT b );
83 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b, BOOL convert );
85 static BOOL num_from_prop( LPCWSTR p, INT *val )
87 INT ret = 0, sign = 1;
100 if( *p < '0' || *p > '9' )
102 ret = ret*10 + (*p - '0');
109 static void value_free( struct value val )
111 if (val.type != VALUE_INTEGER)
112 cond_free( val.u.string );
117 %lex-param { COND_input *info }
118 %parse-param { COND_input *info }
130 %token COND_SPACE COND_EOF
131 %token COND_OR COND_AND COND_NOT COND_XOR COND_IMP COND_EQV
132 %token COND_LT COND_GT COND_EQ COND_NE COND_GE COND_LE
133 %token COND_ILT COND_IGT COND_IEQ COND_INE COND_IGE COND_ILE
134 %token COND_LPAR COND_RPAR COND_TILDA COND_SS COND_ISS
135 %token COND_ILHS COND_IRHS COND_LHS COND_RHS
136 %token COND_PERCENT COND_DOLLARS COND_QUESTION COND_AMPER COND_EXCLAM
137 %token <str> COND_IDENT <str> COND_NUMBER <str> COND_LITER
139 %nonassoc COND_ERROR COND_EOF
141 %type <bool> expression boolean_term boolean_factor
143 %type <identifier> identifier
144 %type <operator> operator
151 COND_input* cond = (COND_input*) info;
156 COND_input* cond = (COND_input*) info;
157 cond->result = MSICONDITION_NONE;
166 | expression COND_OR boolean_term
170 | expression COND_IMP boolean_term
174 | expression COND_XOR boolean_term
176 $$ = ( $1 || $3 ) && !( $1 && $3 );
178 | expression COND_EQV boolean_term
180 $$ = ( $1 && $3 ) || ( !$1 && !$3 );
189 | boolean_term COND_AND boolean_factor
196 COND_NOT boolean_factor
202 if ($1.type == VALUE_INTEGER)
203 $$ = $1.u.integer ? 1 : 0;
205 $$ = $1.u.string && $1.u.string[0];
208 | value operator value
210 if ($1.type == VALUE_INTEGER && $3.type == VALUE_INTEGER)
212 $$ = compare_int($1.u.integer, $2, $3.u.integer);
214 else if ($1.type != VALUE_INTEGER && $3.type != VALUE_INTEGER)
216 $$ = compare_string($1.u.string, $2, $3.u.string,
217 $1.type == VALUE_SYMBOL || $3.type == VALUE_SYMBOL);
219 else if ($1.type == VALUE_LITERAL || $3.type == VALUE_LITERAL)
223 else if ($1.type == VALUE_SYMBOL) /* symbol operator integer */
226 if (num_from_prop( $1.u.string, &num ))
227 $$ = compare_int( num, $2, $3.u.integer );
229 $$ = ($2 == COND_NE || $2 == COND_INE );
231 else /* integer operator symbol */
234 if (num_from_prop( $3.u.string, &num ))
235 $$ = compare_int( $1.u.integer, $2, num );
237 $$ = ($2 == COND_NE || $2 == COND_INE );
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; }
274 COND_input* cond = (COND_input*) info;
277 $$.type = VALUE_SYMBOL;
278 $$.u.string = msi_dup_property( cond->package->db, $1 );
281 len = (lstrlenW($$.u.string) + 1) * sizeof (WCHAR);
282 $$.u.string = cond_track_mem( cond, $$.u.string, len );
286 | COND_PERCENT identifier
288 COND_input* cond = (COND_input*) info;
289 UINT len = GetEnvironmentVariableW( $2, NULL, 0 );
290 $$.type = VALUE_SYMBOL;
294 $$.u.string = cond_alloc( cond, len*sizeof (WCHAR) );
297 GetEnvironmentVariableW( $2, $$.u.string, len );
303 COND_input* cond = (COND_input*) info;
304 $$.type = VALUE_LITERAL;
305 $$.u.string = COND_GetLiteral( cond, &$1 );
311 COND_input* cond = (COND_input*) info;
312 LPWSTR szNum = COND_GetString( cond, &$1 );
315 $$.type = VALUE_INTEGER;
316 $$.u.integer = atoiW( szNum );
319 | COND_DOLLARS identifier
321 COND_input* cond = (COND_input*) info;
322 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
324 if(MSI_GetComponentStateW(cond->package, $2, &install, &action ) != ERROR_SUCCESS)
326 $$.type = VALUE_LITERAL;
331 $$.type = VALUE_INTEGER;
332 $$.u.integer = action;
336 | COND_QUESTION identifier
338 COND_input* cond = (COND_input*) info;
339 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
341 if(MSI_GetComponentStateW(cond->package, $2, &install, &action ) != ERROR_SUCCESS)
343 $$.type = VALUE_LITERAL;
348 $$.type = VALUE_INTEGER;
349 $$.u.integer = install;
353 | COND_AMPER identifier
355 COND_input* cond = (COND_input*) info;
356 INSTALLSTATE install, action;
358 if (MSI_GetFeatureStateW(cond->package, $2, &install, &action ) != ERROR_SUCCESS)
360 $$.type = VALUE_LITERAL;
365 $$.type = VALUE_INTEGER;
366 $$.u.integer = action;
370 | COND_EXCLAM identifier
372 COND_input* cond = (COND_input*) info;
373 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
375 if(MSI_GetFeatureStateW(cond->package, $2, &install, &action ) != ERROR_SUCCESS)
377 $$.type = VALUE_LITERAL;
382 $$.type = VALUE_INTEGER;
383 $$.u.integer = install;
392 COND_input* cond = (COND_input*) info;
393 $$ = COND_GetString( cond, &$1 );
402 static int COND_IsAlpha( WCHAR x )
404 return( ( ( x >= 'A' ) && ( x <= 'Z' ) ) ||
405 ( ( x >= 'a' ) && ( x <= 'z' ) ) ||
409 static int COND_IsNumber( WCHAR x )
411 return( (( x >= '0' ) && ( x <= '9' )) || (x =='-') || (x =='.') );
414 static WCHAR *strstriW( const WCHAR *str, const WCHAR *sub )
416 LPWSTR strlower, sublower, r;
417 strlower = CharLowerW( strdupW( str ) );
418 sublower = CharLowerW( strdupW( sub ) );
419 r = strstrW( strlower, sublower );
421 r = (LPWSTR)str + (r - strlower);
422 msi_free( strlower );
423 msi_free( sublower );
427 static BOOL str_is_number( LPCWSTR str )
434 for (i = 0; i < lstrlenW( str ); i++)
435 if (!isdigitW(str[i]))
441 static INT compare_substring( LPCWSTR a, INT operator, LPCWSTR b )
445 /* substring operators return 0 if LHS is missing */
449 /* substring operators return 1 if RHS is missing */
453 /* if both strings contain only numbers, use integer comparison */
456 if (str_is_number(a) && str_is_number(b))
457 return compare_int( lhs, operator, rhs );
462 return strstrW( a, b ) != 0;
464 return strstriW( a, b ) != 0;
467 int l = strlenW( a );
468 int r = strlenW( b );
470 return !strncmpW( a, b, r );
474 int l = strlenW( a );
475 int r = strlenW( b );
477 return !strncmpW( a + (l - r), b, r );
481 int l = strlenW( a );
482 int r = strlenW( b );
484 return !strncmpiW( a, b, r );
488 int l = strlenW( a );
489 int r = strlenW( b );
491 return !strncmpiW( a + (l - r), b, r );
494 ERR("invalid substring operator\n");
500 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b, BOOL convert )
502 if (operator >= COND_SS && operator <= COND_RHS)
503 return compare_substring( a, operator, b );
505 /* null and empty string are equivalent */
509 if (convert && str_is_number(a) && str_is_number(b))
510 return compare_int( atoiW(a), operator, atoiW(b) );
512 /* a or b may be NULL */
516 return strcmpW( a, b ) < 0;
518 return strcmpW( a, b ) > 0;
520 return strcmpW( a, b ) == 0;
522 return strcmpW( a, b ) != 0;
524 return strcmpW( a, b ) >= 0;
526 return strcmpW( a, b ) <= 0;
528 return strcmpiW( a, b ) < 0;
530 return strcmpiW( a, b ) > 0;
532 return strcmpiW( a, b ) == 0;
534 return strcmpiW( a, b ) != 0;
536 return strcmpiW( a, b ) >= 0;
538 return strcmpiW( a, b ) <= 0;
540 ERR("invalid string operator\n");
547 static INT compare_int( INT a, INT operator, INT b )
571 return ( a & b ) ? 1 : 0;
573 return ( ( a & 0xffff ) == b ) ? 1 : 0;
575 return ( ( (a>>16) & 0xffff ) == b ) ? 1 : 0;
577 ERR("invalid integer operator\n");
584 static int COND_IsIdent( WCHAR x )
586 return( COND_IsAlpha( x ) || COND_IsNumber( x ) || ( x == '_' )
587 || ( x == '#' ) || (x == '.') );
590 static int COND_GetOperator( COND_input *cond )
592 static const struct {
596 { {'~','<','=',0}, COND_ILE },
597 { {'~','>','<',0}, COND_ISS },
598 { {'~','>','>',0}, COND_IRHS },
599 { {'~','<','>',0}, COND_INE },
600 { {'~','>','=',0}, COND_IGE },
601 { {'~','<','<',0}, COND_ILHS },
602 { {'~','=',0}, COND_IEQ },
603 { {'~','<',0}, COND_ILT },
604 { {'~','>',0}, COND_IGT },
605 { {'>','=',0}, COND_GE },
606 { {'>','<',0}, COND_SS },
607 { {'<','<',0}, COND_LHS },
608 { {'<','>',0}, COND_NE },
609 { {'<','=',0}, COND_LE },
610 { {'>','>',0}, COND_RHS },
611 { {'>',0}, COND_GT },
612 { {'<',0}, COND_LT },
615 LPCWSTR p = &cond->str[cond->n];
620 len = lstrlenW( table[i].str );
621 if ( !len || 0 == strncmpW( table[i].str, p, len ) )
629 static int COND_GetOne( struct cond_str *str, COND_input *cond )
634 str->data = &cond->str[cond->n];
641 case '(': rc = COND_LPAR; break;
642 case ')': rc = COND_RPAR; break;
643 case '&': rc = COND_AMPER; break;
644 case '!': rc = COND_EXCLAM; break;
645 case '$': rc = COND_DOLLARS; break;
646 case '?': rc = COND_QUESTION; break;
647 case '%': rc = COND_PERCENT; break;
648 case ' ': rc = COND_SPACE; break;
649 case '=': rc = COND_EQ; break;
654 rc = COND_GetOperator( cond );
670 LPCWSTR p = strchrW( str->data + 1, '"' );
671 if (!p) return COND_ERROR;
672 len = p - str->data + 1;
675 else if( COND_IsAlpha( ch ) )
677 static const WCHAR szNot[] = {'N','O','T',0};
678 static const WCHAR szAnd[] = {'A','N','D',0};
679 static const WCHAR szXor[] = {'X','O','R',0};
680 static const WCHAR szEqv[] = {'E','Q','V',0};
681 static const WCHAR szImp[] = {'I','M','P',0};
682 static const WCHAR szOr[] = {'O','R',0};
684 while( COND_IsIdent( str->data[len] ) )
690 if ( !strncmpiW( str->data, szNot, len ) )
692 else if( !strncmpiW( str->data, szAnd, len ) )
694 else if( !strncmpiW( str->data, szXor, len ) )
696 else if( !strncmpiW( str->data, szEqv, len ) )
698 else if( !strncmpiW( str->data, szImp, len ) )
701 else if( (len == 2) && !strncmpiW( str->data, szOr, len ) )
704 else if( COND_IsNumber( ch ) )
706 while( COND_IsNumber( str->data[len] ) )
712 ERR("Got unknown character %c(%x)\n",ch,ch);
722 static int cond_lex( void *COND_lval, COND_input *cond )
725 struct cond_str *str = COND_lval;
728 rc = COND_GetOne( str, cond );
729 } while (rc == COND_SPACE);
734 static LPWSTR COND_GetString( COND_input *cond, const struct cond_str *str )
738 ret = cond_alloc( cond, (str->len+1) * sizeof (WCHAR) );
741 memcpy( ret, str->data, str->len * sizeof(WCHAR));
744 TRACE("Got identifier %s\n",debugstr_w(ret));
748 static LPWSTR COND_GetLiteral( COND_input *cond, const struct cond_str *str )
752 ret = cond_alloc( cond, (str->len-1) * sizeof (WCHAR) );
755 memcpy( ret, str->data+1, (str->len-2) * sizeof(WCHAR) );
758 TRACE("Got literal %s\n",debugstr_w(ret));
762 static void *cond_alloc( COND_input *cond, unsigned int sz )
766 mem = msi_alloc( sizeof (struct list) + sz );
770 list_add_head( &(cond->mem), mem );
774 static void *cond_track_mem( COND_input *cond, void *ptr, unsigned int sz )
781 new_ptr = cond_alloc( cond, sz );
788 memcpy( new_ptr, ptr, sz );
793 static void cond_free( void *ptr )
795 struct list *mem = (struct list *)ptr - 1;
804 static int cond_error( COND_input *info, const char *str )
810 MSICONDITION MSI_EvaluateConditionW( MSIPACKAGE *package, LPCWSTR szCondition )
814 struct list *mem, *safety;
816 TRACE("%s\n", debugstr_w( szCondition ) );
818 if (szCondition == NULL) return MSICONDITION_NONE;
820 cond.package = package;
821 cond.str = szCondition;
823 cond.result = MSICONDITION_ERROR;
825 list_init( &cond.mem );
827 if ( !cond_parse( &cond ) )
830 r = MSICONDITION_ERROR;
832 LIST_FOR_EACH_SAFE( mem, safety, &cond.mem )
834 /* The tracked memory lives directly after the list struct */
836 if ( r != MSICONDITION_ERROR )
837 WARN( "condition parser failed to free up some memory: %p\n", ptr );
841 TRACE("%i <- %s\n", r, debugstr_w(szCondition));
845 MSICONDITION WINAPI MsiEvaluateConditionW( MSIHANDLE hInstall, LPCWSTR szCondition )
850 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
855 IWineMsiRemotePackage *remote_package;
857 remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
859 return MSICONDITION_ERROR;
861 condition = SysAllocString( szCondition );
864 IWineMsiRemotePackage_Release( remote_package );
865 return ERROR_OUTOFMEMORY;
868 hr = IWineMsiRemotePackage_EvaluateCondition( remote_package, condition );
870 SysFreeString( condition );
871 IWineMsiRemotePackage_Release( remote_package );
875 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
876 return HRESULT_CODE(hr);
878 return ERROR_FUNCTION_FAILED;
881 return ERROR_SUCCESS;
884 ret = MSI_EvaluateConditionW( package, szCondition );
885 msiobj_release( &package->hdr );
889 MSICONDITION WINAPI MsiEvaluateConditionA( MSIHANDLE hInstall, LPCSTR szCondition )
891 LPWSTR szwCond = NULL;
894 szwCond = strdupAtoW( szCondition );
895 if( szCondition && !szwCond )
896 return MSICONDITION_ERROR;
898 r = MsiEvaluateConditionW( hInstall, szwCond );