msi: Convert string variables to ints when appropriate.
[wine.git] / dlls / msi / cond.y
blob7eb48c53b5894f71b4ca58bca66aa3aae30969c5
1 %{
3 /*
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
23 #define COBJMACROS
25 #include "config.h"
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winuser.h"
34 #include "msi.h"
35 #include "msiquery.h"
36 #include "objbase.h"
37 #include "oleauto.h"
39 #include "msipriv.h"
40 #include "msiserver.h"
41 #include "wine/debug.h"
42 #include "wine/unicode.h"
44 #define YYLEX_PARAM info
45 #define YYPARSE_PARAM info
47 static int cond_error(const char *str);
49 WINE_DEFAULT_DEBUG_CHANNEL(msi);
51 typedef struct tag_yyinput
53 MSIPACKAGE *package;
54 LPCWSTR str;
55 INT n;
56 MSICONDITION result;
57 } COND_input;
59 struct cond_str {
60 LPCWSTR data;
61 INT len;
64 static LPWSTR COND_GetString( const struct cond_str *str );
65 static LPWSTR COND_GetLiteral( const struct cond_str *str );
66 static int cond_lex( void *COND_lval, COND_input *info);
67 static const WCHAR szEmpty[] = { 0 };
69 static INT compare_int( INT a, INT operator, INT b );
70 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b, BOOL convert );
72 static INT compare_and_free_strings( LPWSTR a, INT op, LPWSTR b, BOOL convert )
74 INT r;
76 r = compare_string( a, op, b, convert );
77 msi_free( a );
78 msi_free( b );
79 return r;
82 static BOOL num_from_prop( LPCWSTR p, INT *val )
84 INT ret = 0, sign = 1;
86 if (!p)
87 return FALSE;
88 if (*p == '-')
90 sign = -1;
91 p++;
93 if (!*p)
94 return FALSE;
95 while (*p)
97 if( *p < '0' || *p > '9' )
98 return FALSE;
99 ret = ret*10 + (*p - '0');
100 p++;
102 *val = ret*sign;
103 return TRUE;
108 %pure-parser
110 %union
112 struct cond_str str;
113 LPWSTR string;
114 INT value;
117 %token COND_SPACE COND_EOF
118 %token COND_OR COND_AND COND_NOT COND_XOR COND_IMP COND_EQV
119 %token COND_LT COND_GT COND_EQ COND_NE COND_GE COND_LE
120 %token COND_ILT COND_IGT COND_IEQ COND_INE COND_IGE COND_ILE
121 %token COND_LPAR COND_RPAR COND_TILDA COND_SS COND_ISS
122 %token COND_ILHS COND_IRHS COND_LHS COND_RHS
123 %token COND_PERCENT COND_DOLLARS COND_QUESTION COND_AMPER COND_EXCLAM
124 %token <str> COND_IDENT <str> COND_NUMBER <str> COND_LITER
126 %nonassoc COND_ERROR COND_EOF
128 %type <value> expression boolean_term boolean_factor
129 %type <value> value_i integer operator
130 %type <string> identifier symbol_s value_s literal
134 condition:
135 expression
137 COND_input* cond = (COND_input*) info;
138 cond->result = $1;
140 | /* empty */
142 COND_input* cond = (COND_input*) info;
143 cond->result = MSICONDITION_NONE;
147 expression:
148 boolean_term
150 $$ = $1;
152 | expression COND_OR boolean_term
154 $$ = $1 || $3;
156 | expression COND_IMP boolean_term
158 $$ = !$1 || $3;
160 | expression COND_XOR boolean_term
162 $$ = ( $1 || $3 ) && !( $1 && $3 );
164 | expression COND_EQV boolean_term
166 $$ = ( $1 && $3 ) || ( !$1 && !$3 );
170 boolean_term:
171 boolean_factor
173 $$ = $1;
175 | boolean_term COND_AND boolean_factor
177 $$ = $1 && $3;
181 boolean_factor:
182 COND_NOT boolean_factor
184 $$ = $2 ? 0 : 1;
186 | value_i
188 $$ = $1 ? 1 : 0;
190 | value_s
192 $$ = ($1 && $1[0]) ? 1 : 0;
193 msi_free($1);
195 | value_i operator value_i
197 $$ = compare_int( $1, $2, $3 );
199 | symbol_s operator value_i
201 int num;
202 if (num_from_prop( $1, &num ))
203 $$ = compare_int( num, $2, $3 );
204 else
205 $$ = ($2 == COND_NE || $2 == COND_INE );
206 msi_free($1);
208 | value_i operator symbol_s
210 int num;
211 if (num_from_prop( $3, &num ))
212 $$ = compare_int( $1, $2, num );
213 else
214 $$ = ($2 == COND_NE || $2 == COND_INE );
215 msi_free($3);
217 | symbol_s operator symbol_s
219 $$ = compare_and_free_strings( $1, $2, $3, TRUE );
221 | symbol_s operator literal
223 $$ = compare_and_free_strings( $1, $2, $3, TRUE );
225 | literal operator symbol_s
227 $$ = compare_and_free_strings( $1, $2, $3, TRUE );
229 | literal operator literal
231 $$ = compare_and_free_strings( $1, $2, $3, FALSE );
233 | literal operator value_i
235 $$ = 0;
236 msi_free($1);
238 | value_i operator literal
240 $$ = 0;
241 msi_free($3);
243 | COND_LPAR expression COND_RPAR
245 $$ = $2;
249 operator:
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; }
271 value_s:
272 symbol_s
274 $$ = $1;
276 | literal
278 $$ = $1;
282 literal:
283 COND_LITER
285 $$ = COND_GetLiteral(&$1);
286 if( !$$ )
287 YYABORT;
291 value_i:
292 integer
294 $$ = $1;
296 | COND_DOLLARS identifier
298 COND_input* cond = (COND_input*) info;
299 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
301 MSI_GetComponentStateW(cond->package, $2, &install, &action );
302 $$ = action;
303 msi_free( $2 );
305 | COND_QUESTION identifier
307 COND_input* cond = (COND_input*) info;
308 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
310 MSI_GetComponentStateW(cond->package, $2, &install, &action );
311 $$ = install;
312 msi_free( $2 );
314 | COND_AMPER identifier
316 COND_input* cond = (COND_input*) info;
317 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
319 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
320 if (action == INSTALLSTATE_UNKNOWN)
321 $$ = MSICONDITION_FALSE;
322 else
323 $$ = action;
325 msi_free( $2 );
327 | COND_EXCLAM identifier
329 COND_input* cond = (COND_input*) info;
330 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
332 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
333 $$ = install;
334 msi_free( $2 );
338 symbol_s:
339 identifier
341 COND_input* cond = (COND_input*) info;
343 $$ = msi_dup_property( cond->package, $1 );
344 msi_free( $1 );
346 | COND_PERCENT identifier
348 UINT len = GetEnvironmentVariableW( $2, NULL, 0 );
349 $$ = NULL;
350 if (len++)
352 $$ = msi_alloc( len*sizeof (WCHAR) );
353 GetEnvironmentVariableW( $2, $$, len );
355 msi_free( $2 );
359 identifier:
360 COND_IDENT
362 $$ = COND_GetString(&$1);
363 if( !$$ )
364 YYABORT;
368 integer:
369 COND_NUMBER
371 LPWSTR szNum = COND_GetString(&$1);
372 if( !szNum )
373 YYABORT;
374 $$ = atoiW( szNum );
375 msi_free( szNum );
382 static int COND_IsAlpha( WCHAR x )
384 return( ( ( x >= 'A' ) && ( x <= 'Z' ) ) ||
385 ( ( x >= 'a' ) && ( x <= 'z' ) ) ||
386 ( ( x == '_' ) ) );
389 static int COND_IsNumber( WCHAR x )
391 return( (( x >= '0' ) && ( x <= '9' )) || (x =='-') || (x =='.') );
394 static WCHAR *strstriW( const WCHAR *str, const WCHAR *sub )
396 LPWSTR strlower, sublower, r;
397 strlower = CharLowerW( strdupW( str ) );
398 sublower = CharLowerW( strdupW( sub ) );
399 r = strstrW( strlower, sublower );
400 if (r)
401 r = (LPWSTR)str + (r - strlower);
402 msi_free( strlower );
403 msi_free( sublower );
404 return r;
407 static BOOL str_is_number( LPCWSTR str )
409 int i;
411 if (!*str)
412 return FALSE;
414 for (i = 0; i < lstrlenW( str ); i++)
415 if (!isdigitW(str[i]))
416 return FALSE;
418 return TRUE;
421 static INT compare_substring( LPCWSTR a, INT operator, LPCWSTR b )
423 int lhs, rhs;
425 /* substring operators return 0 if LHS is missing */
426 if (!a || !*a)
427 return 0;
429 /* substring operators return 1 if RHS is missing */
430 if (!b || !*b)
431 return 1;
433 /* if both strings contain only numbers, use integer comparison */
434 lhs = atoiW(a);
435 rhs = atoiW(b);
436 if (str_is_number(a) && str_is_number(b))
437 return compare_int( lhs, operator, rhs );
439 switch (operator)
441 case COND_SS:
442 return strstrW( a, b ) ? 1 : 0;
443 case COND_ISS:
444 return strstriW( a, b ) ? 1 : 0;
445 case COND_LHS:
446 return 0 == strncmpW( a, b, lstrlenW( b ) );
447 case COND_RHS:
448 return 0 == lstrcmpW( a + (lstrlenW( a ) - lstrlenW( b )), b );
449 case COND_ILHS:
450 return 0 == strncmpiW( a, b, lstrlenW( b ) );
451 case COND_IRHS:
452 return 0 == lstrcmpiW( a + (lstrlenW( a ) - lstrlenW( b )), b );
453 default:
454 ERR("invalid substring operator\n");
455 return 0;
457 return 0;
460 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b, BOOL convert )
462 if (operator >= COND_SS && operator <= COND_RHS)
463 return compare_substring( a, operator, b );
465 /* null and empty string are equivalent */
466 if (!a) a = szEmpty;
467 if (!b) b = szEmpty;
469 if (convert && str_is_number(a) && str_is_number(b))
470 return compare_int( atoiW(a), operator, atoiW(b) );
472 /* a or b may be NULL */
473 switch (operator)
475 case COND_LT:
476 return -1 == lstrcmpW( a, b );
477 case COND_GT:
478 return 1 == lstrcmpW( a, b );
479 case COND_EQ:
480 return 0 == lstrcmpW( a, b );
481 case COND_NE:
482 return 0 != lstrcmpW( a, b );
483 case COND_GE:
484 return -1 != lstrcmpW( a, b );
485 case COND_LE:
486 return 1 != lstrcmpW( a, b );
487 case COND_ILT:
488 return -1 == lstrcmpiW( a, b );
489 case COND_IGT:
490 return 1 == lstrcmpiW( a, b );
491 case COND_IEQ:
492 return 0 == lstrcmpiW( a, b );
493 case COND_INE:
494 return 0 != lstrcmpiW( a, b );
495 case COND_IGE:
496 return -1 != lstrcmpiW( a, b );
497 case COND_ILE:
498 return 1 != lstrcmpiW( a, b );
499 default:
500 ERR("invalid string operator\n");
501 return 0;
503 return 0;
507 static INT compare_int( INT a, INT operator, INT b )
509 switch (operator)
511 case COND_LT:
512 case COND_ILT:
513 return a < b;
514 case COND_GT:
515 case COND_IGT:
516 return a > b;
517 case COND_EQ:
518 case COND_IEQ:
519 return a == b;
520 case COND_NE:
521 case COND_INE:
522 return a != b;
523 case COND_GE:
524 case COND_IGE:
525 return a >= b;
526 case COND_LE:
527 case COND_ILE:
528 return a <= b;
529 case COND_SS:
530 case COND_ISS:
531 return ( a & b ) ? 1 : 0;
532 case COND_RHS:
533 return ( ( a & 0xffff ) == b ) ? 1 : 0;
534 case COND_LHS:
535 return ( ( (a>>16) & 0xffff ) == b ) ? 1 : 0;
536 default:
537 ERR("invalid integer operator\n");
538 return 0;
540 return 0;
544 static int COND_IsIdent( WCHAR x )
546 return( COND_IsAlpha( x ) || COND_IsNumber( x ) || ( x == '_' )
547 || ( x == '#' ) || (x == '.') );
550 static int COND_GetOperator( COND_input *cond )
552 static const struct {
553 const WCHAR str[4];
554 int id;
555 } table[] = {
556 { {'~','=',0}, COND_IEQ },
557 { {'~','<','=',0}, COND_ILE },
558 { {'~','>','<',0}, COND_ISS },
559 { {'~','>','>',0}, COND_IRHS },
560 { {'~','<','>',0}, COND_INE },
561 { {'~','<',0}, COND_ILT },
562 { {'~','>','=',0}, COND_IGE },
563 { {'~','<','<',0}, COND_ILHS },
564 { {'~','>',0}, COND_IGT },
565 { {'>','=',0}, COND_GE },
566 { {'>','<',0}, COND_SS },
567 { {'<','<',0}, COND_LHS },
568 { {'<','>',0}, COND_NE },
569 { {'<','=',0}, COND_LE },
570 { {'>','>',0}, COND_RHS },
571 { {'>',0}, COND_GT },
572 { {'<',0}, COND_LT },
573 { {0}, 0 }
575 LPCWSTR p = &cond->str[cond->n];
576 int i = 0, len;
578 while ( 1 )
580 len = lstrlenW( table[i].str );
581 if ( !len || 0 == strncmpW( table[i].str, p, len ) )
582 break;
583 i++;
585 cond->n += len;
586 return table[i].id;
589 static int COND_GetOne( struct cond_str *str, COND_input *cond )
591 int rc, len = 1;
592 WCHAR ch;
594 str->data = &cond->str[cond->n];
596 ch = str->data[0];
598 switch( ch )
600 case 0: return 0;
601 case '(': rc = COND_LPAR; break;
602 case ')': rc = COND_RPAR; break;
603 case '&': rc = COND_AMPER; break;
604 case '!': rc = COND_EXCLAM; break;
605 case '$': rc = COND_DOLLARS; break;
606 case '?': rc = COND_QUESTION; break;
607 case '%': rc = COND_PERCENT; break;
608 case ' ': rc = COND_SPACE; break;
609 case '=': rc = COND_EQ; break;
611 case '~':
612 case '<':
613 case '>':
614 rc = COND_GetOperator( cond );
615 if (!rc)
616 rc = COND_ERROR;
617 return rc;
618 default:
619 rc = 0;
622 if ( rc )
624 cond->n += len;
625 return rc;
628 if (ch == '"' )
630 LPCWSTR p = strchrW( str->data + 1, '"' );
631 if (!p)
632 return COND_ERROR;
633 len = p - str->data + 1;
634 rc = COND_LITER;
636 else if( COND_IsAlpha( ch ) )
638 static const WCHAR szNot[] = {'N','O','T',0};
639 static const WCHAR szAnd[] = {'A','N','D',0};
640 static const WCHAR szXor[] = {'X','O','R',0};
641 static const WCHAR szEqv[] = {'E','Q','V',0};
642 static const WCHAR szImp[] = {'I','M','P',0};
643 static const WCHAR szOr[] = {'O','R',0};
645 while( COND_IsIdent( str->data[len] ) )
646 len++;
647 rc = COND_IDENT;
649 if ( len == 3 )
651 if ( !strncmpiW( str->data, szNot, len ) )
652 rc = COND_NOT;
653 else if( !strncmpiW( str->data, szAnd, len ) )
654 rc = COND_AND;
655 else if( !strncmpiW( str->data, szXor, len ) )
656 rc = COND_XOR;
657 else if( !strncmpiW( str->data, szEqv, len ) )
658 rc = COND_EQV;
659 else if( !strncmpiW( str->data, szImp, len ) )
660 rc = COND_IMP;
662 else if( (len == 2) && !strncmpiW( str->data, szOr, len ) )
663 rc = COND_OR;
665 else if( COND_IsNumber( ch ) )
667 while( COND_IsNumber( str->data[len] ) )
668 len++;
669 rc = COND_NUMBER;
671 else
673 ERR("Got unknown character %c(%x)\n",ch,ch);
674 return COND_ERROR;
677 cond->n += len;
678 str->len = len;
680 return rc;
683 static int cond_lex( void *COND_lval, COND_input *cond )
685 int rc;
686 struct cond_str *str = COND_lval;
688 do {
689 rc = COND_GetOne( str, cond );
690 } while (rc == COND_SPACE);
692 return rc;
695 static LPWSTR COND_GetString( const struct cond_str *str )
697 LPWSTR ret;
699 ret = msi_alloc( (str->len+1) * sizeof (WCHAR) );
700 if( ret )
702 memcpy( ret, str->data, str->len * sizeof(WCHAR));
703 ret[str->len]=0;
705 TRACE("Got identifier %s\n",debugstr_w(ret));
706 return ret;
709 static LPWSTR COND_GetLiteral( const struct cond_str *str )
711 LPWSTR ret;
713 ret = msi_alloc( (str->len-1) * sizeof (WCHAR) );
714 if( ret )
716 memcpy( ret, str->data+1, (str->len-2) * sizeof(WCHAR) );
717 ret[str->len - 2]=0;
719 TRACE("Got literal %s\n",debugstr_w(ret));
720 return ret;
723 static int cond_error(const char *str)
725 TRACE("%s\n", str );
726 return 0;
729 MSICONDITION MSI_EvaluateConditionW( MSIPACKAGE *package, LPCWSTR szCondition )
731 COND_input cond;
732 MSICONDITION r;
734 TRACE("%s\n", debugstr_w( szCondition ) );
736 if ( szCondition == NULL )
737 return MSICONDITION_NONE;
739 cond.package = package;
740 cond.str = szCondition;
741 cond.n = 0;
742 cond.result = MSICONDITION_ERROR;
744 if ( !cond_parse( &cond ) )
745 r = cond.result;
746 else
747 r = MSICONDITION_ERROR;
749 TRACE("%i <- %s\n", r, debugstr_w(szCondition));
750 return r;
753 MSICONDITION WINAPI MsiEvaluateConditionW( MSIHANDLE hInstall, LPCWSTR szCondition )
755 MSIPACKAGE *package;
756 UINT ret;
758 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
759 if( !package )
761 HRESULT hr;
762 BSTR condition;
763 IWineMsiRemotePackage *remote_package;
765 remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
766 if (!remote_package)
767 return MSICONDITION_ERROR;
769 condition = SysAllocString( szCondition );
770 if (!condition)
772 IWineMsiRemotePackage_Release( remote_package );
773 return ERROR_OUTOFMEMORY;
776 hr = IWineMsiRemotePackage_EvaluateCondition( remote_package, condition );
778 SysFreeString( condition );
779 IWineMsiRemotePackage_Release( remote_package );
781 if (FAILED(hr))
783 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
784 return HRESULT_CODE(hr);
786 return ERROR_FUNCTION_FAILED;
789 return ERROR_SUCCESS;
792 ret = MSI_EvaluateConditionW( package, szCondition );
793 msiobj_release( &package->hdr );
794 return ret;
797 MSICONDITION WINAPI MsiEvaluateConditionA( MSIHANDLE hInstall, LPCSTR szCondition )
799 LPWSTR szwCond = NULL;
800 MSICONDITION r;
802 szwCond = strdupAtoW( szCondition );
803 if( szCondition && !szwCond )
804 return MSICONDITION_ERROR;
806 r = MsiEvaluateConditionW( hInstall, szwCond );
807 msi_free( szwCond );
808 return r;