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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
40 #define YYLEX_PARAM info
41 #define YYPARSE_PARAM info
43 static int COND_error(const char *str);
45 WINE_DEFAULT_DEBUG_CHANNEL(msi);
47 typedef struct tag_yyinput
60 static LPWSTR COND_GetString( struct cond_str *str );
61 static LPWSTR COND_GetLiteral( struct cond_str *str );
62 static int COND_lex( void *COND_lval, COND_input *info);
63 static const WCHAR szEmpty[] = { 0 };
65 static INT compare_int( INT a, INT operator, INT b );
66 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b );
68 static INT compare_and_free_strings( LPWSTR a, INT op, LPWSTR b )
72 r = compare_string( a, op, b );
89 %token COND_SPACE COND_EOF COND_SPACE
90 %token COND_OR COND_AND COND_NOT COND_XOR COND_IMP COND_EQV
91 %token COND_LT COND_GT COND_EQ COND_NE COND_GE COND_LE
92 %token COND_ILT COND_IGT COND_IEQ COND_INE COND_IGE COND_ILE
93 %token COND_LPAR COND_RPAR COND_TILDA COND_SS COND_ISS
94 %token COND_ILHS COND_IRHS COND_LHS COND_RHS
95 %token COND_PERCENT COND_DOLLARS COND_QUESTION COND_AMPER COND_EXCLAM
96 %token <str> COND_IDENT <str> COND_NUMBER <str> COND_LITER
98 %nonassoc COND_ERROR COND_EOF
100 %type <value> expression boolean_term boolean_factor
101 %type <value> value_i integer operator
102 %type <string> identifier symbol_s value_s literal
109 COND_input* cond = (COND_input*) info;
114 COND_input* cond = (COND_input*) info;
115 cond->result = MSICONDITION_NONE;
124 | expression COND_OR boolean_term
128 | expression COND_IMP boolean_term
132 | expression COND_XOR boolean_term
134 $$ = ( $1 || $3 ) && !( $1 && $3 );
136 | expression COND_EQV boolean_term
138 $$ = ( $1 && $3 ) || ( !$1 && !$3 );
147 | boolean_term COND_AND boolean_factor
154 COND_NOT boolean_factor
164 $$ = ($1 && $1[0]) ? 1 : 0;
166 | value_i operator value_i
168 $$ = compare_int( $1, $2, $3 );
170 | symbol_s operator value_i
172 $$ = compare_int( $1 ? atoiW($1) : 0, $2, $3 );
174 | value_i operator symbol_s
176 $$ = compare_int( $1, $2, $3 ? atoiW($3) : 0 );
178 | symbol_s operator symbol_s
180 $$ = compare_and_free_strings( $1, $2, $3 );
182 | symbol_s operator literal
184 $$ = compare_and_free_strings( $1, $2, $3 );
186 | literal operator symbol_s
188 $$ = compare_and_free_strings( $1, $2, $3 );
190 | literal operator literal
192 $$ = compare_and_free_strings( $1, $2, $3 );
194 | literal operator value_i
198 | value_i operator literal
202 | COND_LPAR expression COND_RPAR
209 /* common functions */
210 COND_EQ { $$ = COND_EQ }
211 | COND_NE { $$ = COND_NE }
212 | COND_LT { $$ = COND_LT }
213 | COND_GT { $$ = COND_GT }
214 | COND_LE { $$ = COND_LE }
215 | COND_GE { $$ = COND_GE }
216 | COND_SS { $$ = COND_SS }
217 | COND_IEQ { $$ = COND_IEQ }
218 | COND_INE { $$ = COND_INE }
219 | COND_ILT { $$ = COND_ILT }
220 | COND_IGT { $$ = COND_IGT }
221 | COND_ILE { $$ = COND_ILE }
222 | COND_IGE { $$ = COND_IGE }
223 | COND_ISS { $$ = COND_ISS }
224 | COND_LHS { $$ = COND_LHS }
225 | COND_RHS { $$ = COND_RHS }
226 | COND_ILHS { $$ = COND_ILHS }
227 | COND_IRHS { $$ = COND_IRHS }
244 $$ = COND_GetLiteral(&$1);
255 | COND_DOLLARS identifier
257 COND_input* cond = (COND_input*) info;
258 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
260 MSI_GetComponentStateW(cond->package, $2, &install, &action );
264 | COND_QUESTION identifier
266 COND_input* cond = (COND_input*) info;
267 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
269 MSI_GetComponentStateW(cond->package, $2, &install, &action );
273 | COND_AMPER identifier
275 COND_input* cond = (COND_input*) info;
276 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
278 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
282 | COND_EXCLAM identifier
284 COND_input* cond = (COND_input*) info;
285 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
287 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
296 COND_input* cond = (COND_input*) info;
298 $$ = msi_dup_property( cond->package, $1 );
301 | COND_PERCENT identifier
303 UINT len = GetEnvironmentVariableW( $2, NULL, 0 );
307 $$ = msi_alloc( len*sizeof (WCHAR) );
308 GetEnvironmentVariableW( $2, $$, len );
317 $$ = COND_GetString(&$1);
326 LPWSTR szNum = COND_GetString(&$1);
337 static int COND_IsAlpha( WCHAR x )
339 return( ( ( x >= 'A' ) && ( x <= 'Z' ) ) ||
340 ( ( x >= 'a' ) && ( x <= 'z' ) ) ||
344 static int COND_IsNumber( WCHAR x )
346 return( (( x >= '0' ) && ( x <= '9' )) || (x =='-') || (x =='.') );
349 static WCHAR *strstriW( const WCHAR *str, const WCHAR *sub )
351 LPWSTR strlower, sublower, r;
352 strlower = CharLowerW( strdupW( str ) );
353 sublower = CharLowerW( strdupW( sub ) );
354 r = strstrW( strlower, sublower );
356 r = (LPWSTR)str + (r - strlower);
357 msi_free( strlower );
358 msi_free( sublower );
362 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b )
364 /* a or b may be NULL */
368 return -1 == lstrcmpW( a, b );
370 return 1 == lstrcmpW( a, b );
372 return 0 == lstrcmpW( a, b );
374 return 0 != lstrcmpW( a, b );
376 return -1 != lstrcmpW( a, b );
378 return 1 != lstrcmpW( a, b );
379 case COND_SS: /* substring */
380 return ( a && b && strstrW( a, b ) ) ? 1 : 0;
382 return -1 == lstrcmpiW( a, b );
384 return 1 == lstrcmpiW( a, b );
386 return 0 == lstrcmpiW( a, b );
388 return 0 != lstrcmpiW( a, b );
390 return -1 != lstrcmpiW( a, b );
392 return 1 != lstrcmpiW( a, b );
394 return ( a && b && strstriW( a, b ) ) ? 1 : 0;
399 ERR("unimplemented string comparison\n");
402 ERR("invalid integer operator\n");
409 static INT compare_int( INT a, INT operator, INT b )
433 return ( a & b ) ? 1 : 0;
435 return ( ( a & 0xffff ) == b ) ? 1 : 0;
437 return ( ( (a>>16) & 0xffff ) == b ) ? 1 : 0;
439 ERR("invalid integer operator\n");
446 static int COND_IsIdent( WCHAR x )
448 return( COND_IsAlpha( x ) || COND_IsNumber( x ) || ( x == '_' )
449 || ( x == '#' ) || (x == '.') );
452 static int COND_GetOperator( COND_input *cond )
454 static const struct {
458 { {'~','=',0}, COND_IEQ },
459 { {'~','>','=',0}, COND_ILE },
460 { {'~','>','<',0}, COND_ISS },
461 { {'~','>','>',0}, COND_IRHS },
462 { {'~','>',0}, COND_ILT },
463 { {'~','<','>',0}, COND_INE },
464 { {'~','<','=',0}, COND_IGE },
465 { {'~','<','<',0}, COND_ILHS },
466 { {'~','<',0}, COND_IGT },
467 { {'>','=',0}, COND_GE },
468 { {'>','<',0}, COND_SS },
469 { {'>','>',0}, COND_LHS },
470 { {'>',0}, COND_GT },
471 { {'<','>',0}, COND_NE },
472 { {'<','=',0}, COND_LE },
473 { {'<','<',0}, COND_RHS },
474 { {'<',0}, COND_LT },
477 LPCWSTR p = &cond->str[cond->n];
482 len = lstrlenW( table[i].str );
483 if ( !len || 0 == strncmpW( table[i].str, p, len ) )
491 static int COND_GetOne( struct cond_str *str, COND_input *cond )
496 str->data = &cond->str[cond->n];
503 case '(': rc = COND_LPAR; break;
504 case ')': rc = COND_RPAR; break;
505 case '&': rc = COND_AMPER; break;
506 case '!': rc = COND_EXCLAM; break;
507 case '$': rc = COND_DOLLARS; break;
508 case '?': rc = COND_QUESTION; break;
509 case '%': rc = COND_PERCENT; break;
510 case ' ': rc = COND_SPACE; break;
511 case '=': rc = COND_EQ; break;
517 rc = COND_GetOperator( cond );
533 LPCWSTR p = strchrW( str->data + 1, '"' );
536 len = p - str->data + 1;
539 else if( COND_IsAlpha( ch ) )
541 static const WCHAR szNot[] = {'N','O','T',0};
542 static const WCHAR szAnd[] = {'A','N','D',0};
543 static const WCHAR szXor[] = {'X','O','R',0};
544 static const WCHAR szEqv[] = {'E','Q','V',0};
545 static const WCHAR szImp[] = {'I','M','P',0};
546 static const WCHAR szOr[] = {'O','R',0};
548 while( COND_IsIdent( str->data[len] ) )
554 if ( !strncmpiW( str->data, szNot, len ) )
556 else if( !strncmpiW( str->data, szAnd, len ) )
558 else if( !strncmpiW( str->data, szXor, len ) )
560 else if( !strncmpiW( str->data, szEqv, len ) )
562 else if( !strncmpiW( str->data, szImp, len ) )
565 else if( (len == 2) && !strncmpiW( str->data, szOr, len ) )
568 else if( COND_IsNumber( ch ) )
570 while( COND_IsNumber( str->data[len] ) )
576 ERR("Got unknown character %c(%x)\n",ch,ch);
586 static int COND_lex( void *COND_lval, COND_input *cond )
589 struct cond_str *str = COND_lval;
592 rc = COND_GetOne( str, cond );
593 } while (rc == COND_SPACE);
598 static LPWSTR COND_GetString( struct cond_str *str )
602 ret = msi_alloc( (str->len+1) * sizeof (WCHAR) );
605 memcpy( ret, str->data, str->len * sizeof(WCHAR));
608 TRACE("Got identifier %s\n",debugstr_w(ret));
612 static LPWSTR COND_GetLiteral( struct cond_str *str )
616 ret = msi_alloc( (str->len-1) * sizeof (WCHAR) );
619 memcpy( ret, str->data+1, (str->len-2) * sizeof(WCHAR) );
622 TRACE("Got literal %s\n",debugstr_w(ret));
626 static int COND_error(const char *str)
632 MSICONDITION MSI_EvaluateConditionW( MSIPACKAGE *package, LPCWSTR szCondition )
637 TRACE("%s\n", debugstr_w( szCondition ) );
639 if ( szCondition == NULL )
640 return MSICONDITION_NONE;
642 cond.package = package;
643 cond.str = szCondition;
645 cond.result = MSICONDITION_ERROR;
647 if ( !COND_parse( &cond ) )
650 r = MSICONDITION_ERROR;
652 TRACE("%i <- %s\n", r, debugstr_w(szCondition));
656 MSICONDITION WINAPI MsiEvaluateConditionW( MSIHANDLE hInstall, LPCWSTR szCondition )
661 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
663 return MSICONDITION_ERROR;
664 ret = MSI_EvaluateConditionW( package, szCondition );
665 msiobj_release( &package->hdr );
669 MSICONDITION WINAPI MsiEvaluateConditionA( MSIHANDLE hInstall, LPCSTR szCondition )
671 LPWSTR szwCond = NULL;
674 szwCond = strdupAtoW( szCondition );
675 if( szCondition && !szwCond )
676 return MSICONDITION_ERROR;
678 r = MsiEvaluateConditionW( hInstall, szwCond );