ntoskrnl.exe: Change calling conventions for Interlocked* functions.
[wine/multimedia.git] / dlls / msi / cond.y
blob062af07b4b5485f028b32f0f7e19d6837c2f6c99
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 );
72 static INT compare_and_free_strings( LPWSTR a, INT op, LPWSTR b )
74 INT r;
76 r = compare_string( a, op, b );
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 );
221 | symbol_s operator literal
223 $$ = compare_and_free_strings( $1, $2, $3 );
225 | literal operator symbol_s
227 $$ = compare_and_free_strings( $1, $2, $3 );
229 | literal operator literal
231 $$ = compare_and_free_strings( $1, $2, $3 );
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 for (i = 0; i < lstrlenW( str ); i++)
412 if (!isdigitW(str[i]))
413 return FALSE;
415 return TRUE;
418 static INT compare_substring( LPCWSTR a, INT operator, LPCWSTR b )
420 int lhs, rhs;
422 /* substring operators return 0 if LHS is missing */
423 if (!a || !*a)
424 return 0;
426 /* substring operators return 1 if RHS is missing */
427 if (!b || !*b)
428 return 1;
430 /* if both strings contain only numbers, use integer comparison */
431 lhs = atoiW(a);
432 rhs = atoiW(b);
433 if (str_is_number(a) && str_is_number(b))
434 return compare_int( lhs, operator, rhs );
436 switch (operator)
438 case COND_SS:
439 return strstrW( a, b ) ? 1 : 0;
440 case COND_ISS:
441 return strstriW( a, b ) ? 1 : 0;
442 case COND_LHS:
443 return 0 == strncmpW( a, b, lstrlenW( b ) );
444 case COND_RHS:
445 return 0 == lstrcmpW( a + (lstrlenW( a ) - lstrlenW( b )), b );
446 case COND_ILHS:
447 return 0 == strncmpiW( a, b, lstrlenW( b ) );
448 case COND_IRHS:
449 return 0 == lstrcmpiW( a + (lstrlenW( a ) - lstrlenW( b )), b );
450 default:
451 ERR("invalid substring operator\n");
452 return 0;
454 return 0;
457 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b )
459 if (operator >= COND_SS && operator <= COND_RHS)
460 return compare_substring( a, operator, b );
462 /* null and empty string are equivalent */
463 if (!a) a = szEmpty;
464 if (!b) b = szEmpty;
466 /* a or b may be NULL */
467 switch (operator)
469 case COND_LT:
470 return -1 == lstrcmpW( a, b );
471 case COND_GT:
472 return 1 == lstrcmpW( a, b );
473 case COND_EQ:
474 return 0 == lstrcmpW( a, b );
475 case COND_NE:
476 return 0 != lstrcmpW( a, b );
477 case COND_GE:
478 return -1 != lstrcmpW( a, b );
479 case COND_LE:
480 return 1 != lstrcmpW( a, b );
481 case COND_ILT:
482 return -1 == lstrcmpiW( a, b );
483 case COND_IGT:
484 return 1 == lstrcmpiW( a, b );
485 case COND_IEQ:
486 return 0 == lstrcmpiW( a, b );
487 case COND_INE:
488 return 0 != lstrcmpiW( a, b );
489 case COND_IGE:
490 return -1 != lstrcmpiW( a, b );
491 case COND_ILE:
492 return 1 != lstrcmpiW( a, b );
493 default:
494 ERR("invalid string operator\n");
495 return 0;
497 return 0;
501 static INT compare_int( INT a, INT operator, INT b )
503 switch (operator)
505 case COND_LT:
506 case COND_ILT:
507 return a < b;
508 case COND_GT:
509 case COND_IGT:
510 return a > b;
511 case COND_EQ:
512 case COND_IEQ:
513 return a == b;
514 case COND_NE:
515 case COND_INE:
516 return a != b;
517 case COND_GE:
518 case COND_IGE:
519 return a >= b;
520 case COND_LE:
521 case COND_ILE:
522 return a <= b;
523 case COND_SS:
524 case COND_ISS:
525 return ( a & b ) ? 1 : 0;
526 case COND_RHS:
527 return ( ( a & 0xffff ) == b ) ? 1 : 0;
528 case COND_LHS:
529 return ( ( (a>>16) & 0xffff ) == b ) ? 1 : 0;
530 default:
531 ERR("invalid integer operator\n");
532 return 0;
534 return 0;
538 static int COND_IsIdent( WCHAR x )
540 return( COND_IsAlpha( x ) || COND_IsNumber( x ) || ( x == '_' )
541 || ( x == '#' ) || (x == '.') );
544 static int COND_GetOperator( COND_input *cond )
546 static const struct {
547 const WCHAR str[4];
548 int id;
549 } table[] = {
550 { {'~','=',0}, COND_IEQ },
551 { {'~','<','=',0}, COND_ILE },
552 { {'~','>','<',0}, COND_ISS },
553 { {'~','>','>',0}, COND_IRHS },
554 { {'~','<','>',0}, COND_INE },
555 { {'~','<',0}, COND_ILT },
556 { {'~','>','=',0}, COND_IGE },
557 { {'~','<','<',0}, COND_ILHS },
558 { {'~','>',0}, COND_IGT },
559 { {'>','=',0}, COND_GE },
560 { {'>','<',0}, COND_SS },
561 { {'<','<',0}, COND_LHS },
562 { {'<','>',0}, COND_NE },
563 { {'<','=',0}, COND_LE },
564 { {'>','>',0}, COND_RHS },
565 { {'>',0}, COND_GT },
566 { {'<',0}, COND_LT },
567 { {0}, 0 }
569 LPCWSTR p = &cond->str[cond->n];
570 int i = 0, len;
572 while ( 1 )
574 len = lstrlenW( table[i].str );
575 if ( !len || 0 == strncmpW( table[i].str, p, len ) )
576 break;
577 i++;
579 cond->n += len;
580 return table[i].id;
583 static int COND_GetOne( struct cond_str *str, COND_input *cond )
585 int rc, len = 1;
586 WCHAR ch;
588 str->data = &cond->str[cond->n];
590 ch = str->data[0];
592 switch( ch )
594 case 0: return 0;
595 case '(': rc = COND_LPAR; break;
596 case ')': rc = COND_RPAR; break;
597 case '&': rc = COND_AMPER; break;
598 case '!': rc = COND_EXCLAM; break;
599 case '$': rc = COND_DOLLARS; break;
600 case '?': rc = COND_QUESTION; break;
601 case '%': rc = COND_PERCENT; break;
602 case ' ': rc = COND_SPACE; break;
603 case '=': rc = COND_EQ; break;
605 case '~':
606 case '<':
607 case '>':
608 rc = COND_GetOperator( cond );
609 if (!rc)
610 rc = COND_ERROR;
611 return rc;
612 default:
613 rc = 0;
616 if ( rc )
618 cond->n += len;
619 return rc;
622 if (ch == '"' )
624 LPCWSTR p = strchrW( str->data + 1, '"' );
625 if (!p)
626 return COND_ERROR;
627 len = p - str->data + 1;
628 rc = COND_LITER;
630 else if( COND_IsAlpha( ch ) )
632 static const WCHAR szNot[] = {'N','O','T',0};
633 static const WCHAR szAnd[] = {'A','N','D',0};
634 static const WCHAR szXor[] = {'X','O','R',0};
635 static const WCHAR szEqv[] = {'E','Q','V',0};
636 static const WCHAR szImp[] = {'I','M','P',0};
637 static const WCHAR szOr[] = {'O','R',0};
639 while( COND_IsIdent( str->data[len] ) )
640 len++;
641 rc = COND_IDENT;
643 if ( len == 3 )
645 if ( !strncmpiW( str->data, szNot, len ) )
646 rc = COND_NOT;
647 else if( !strncmpiW( str->data, szAnd, len ) )
648 rc = COND_AND;
649 else if( !strncmpiW( str->data, szXor, len ) )
650 rc = COND_XOR;
651 else if( !strncmpiW( str->data, szEqv, len ) )
652 rc = COND_EQV;
653 else if( !strncmpiW( str->data, szImp, len ) )
654 rc = COND_IMP;
656 else if( (len == 2) && !strncmpiW( str->data, szOr, len ) )
657 rc = COND_OR;
659 else if( COND_IsNumber( ch ) )
661 while( COND_IsNumber( str->data[len] ) )
662 len++;
663 rc = COND_NUMBER;
665 else
667 ERR("Got unknown character %c(%x)\n",ch,ch);
668 return COND_ERROR;
671 cond->n += len;
672 str->len = len;
674 return rc;
677 static int cond_lex( void *COND_lval, COND_input *cond )
679 int rc;
680 struct cond_str *str = COND_lval;
682 do {
683 rc = COND_GetOne( str, cond );
684 } while (rc == COND_SPACE);
686 return rc;
689 static LPWSTR COND_GetString( const struct cond_str *str )
691 LPWSTR ret;
693 ret = msi_alloc( (str->len+1) * sizeof (WCHAR) );
694 if( ret )
696 memcpy( ret, str->data, str->len * sizeof(WCHAR));
697 ret[str->len]=0;
699 TRACE("Got identifier %s\n",debugstr_w(ret));
700 return ret;
703 static LPWSTR COND_GetLiteral( const struct cond_str *str )
705 LPWSTR ret;
707 ret = msi_alloc( (str->len-1) * sizeof (WCHAR) );
708 if( ret )
710 memcpy( ret, str->data+1, (str->len-2) * sizeof(WCHAR) );
711 ret[str->len - 2]=0;
713 TRACE("Got literal %s\n",debugstr_w(ret));
714 return ret;
717 static int cond_error(const char *str)
719 TRACE("%s\n", str );
720 return 0;
723 MSICONDITION MSI_EvaluateConditionW( MSIPACKAGE *package, LPCWSTR szCondition )
725 COND_input cond;
726 MSICONDITION r;
728 TRACE("%s\n", debugstr_w( szCondition ) );
730 if ( szCondition == NULL )
731 return MSICONDITION_NONE;
733 cond.package = package;
734 cond.str = szCondition;
735 cond.n = 0;
736 cond.result = MSICONDITION_ERROR;
738 if ( !cond_parse( &cond ) )
739 r = cond.result;
740 else
741 r = MSICONDITION_ERROR;
743 TRACE("%i <- %s\n", r, debugstr_w(szCondition));
744 return r;
747 MSICONDITION WINAPI MsiEvaluateConditionW( MSIHANDLE hInstall, LPCWSTR szCondition )
749 MSIPACKAGE *package;
750 UINT ret;
752 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
753 if( !package )
755 HRESULT hr;
756 BSTR condition;
757 IWineMsiRemotePackage *remote_package;
759 remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
760 if (!remote_package)
761 return MSICONDITION_ERROR;
763 condition = SysAllocString( szCondition );
764 if (!condition)
766 IWineMsiRemotePackage_Release( remote_package );
767 return ERROR_OUTOFMEMORY;
770 hr = IWineMsiRemotePackage_EvaluateCondition( remote_package, condition );
772 SysFreeString( condition );
773 IWineMsiRemotePackage_Release( remote_package );
775 if (FAILED(hr))
777 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
778 return HRESULT_CODE(hr);
780 return ERROR_FUNCTION_FAILED;
783 return ERROR_SUCCESS;
786 ret = MSI_EvaluateConditionW( package, szCondition );
787 msiobj_release( &package->hdr );
788 return ret;
791 MSICONDITION WINAPI MsiEvaluateConditionA( MSIHANDLE hInstall, LPCSTR szCondition )
793 LPWSTR szwCond = NULL;
794 MSICONDITION r;
796 szwCond = strdupAtoW( szCondition );
797 if( szCondition && !szwCond )
798 return MSICONDITION_ERROR;
800 r = MsiEvaluateConditionW( hInstall, szwCond );
801 msi_free( szwCond );
802 return r;