include: Use known variables names in SYSTEM_INTERRUPT_INFORMATION.
[wine.git] / dlls / msi / cond.y
blob636999a104fb7369f3ffed6e092e2c512faa015a
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 <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "msi.h"
33 #include "msiquery.h"
34 #include "objbase.h"
35 #include "oleauto.h"
37 #include "msipriv.h"
38 #include "winemsi.h"
39 #include "wine/debug.h"
40 #include "wine/exception.h"
41 #include "wine/list.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(msi);
45 typedef struct tag_yyinput
47 MSIPACKAGE *package;
48 LPCWSTR str;
49 INT n;
50 MSICONDITION result;
51 struct list mem;
52 } COND_input;
54 struct cond_str {
55 LPCWSTR data;
56 INT len;
59 struct value {
60 enum value_type {
61 VALUE_INTEGER,
62 VALUE_LITERAL,
63 VALUE_SYMBOL
64 } type;
65 union {
66 INT integer;
67 WCHAR *string;
68 } u;
71 static LPWSTR COND_GetString( COND_input *info, const struct cond_str *str );
72 static LPWSTR COND_GetLiteral( COND_input *info, const struct cond_str *str );
73 static int cond_lex( void *COND_lval, COND_input *info);
74 static int cond_error( COND_input *info, const char *str);
76 static void *cond_alloc( COND_input *cond, unsigned int sz );
77 static void *cond_track_mem( COND_input *cond, void *ptr, unsigned int sz );
78 static void cond_free( void *ptr );
80 static INT compare_int( INT a, INT operator, INT b );
81 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b, BOOL convert );
83 static BOOL num_from_prop( LPCWSTR p, INT *val )
85 INT ret = 0, sign = 1;
87 if (!p)
88 return FALSE;
89 if (*p == '-')
91 sign = -1;
92 p++;
94 if (!*p)
95 return FALSE;
96 while (*p)
98 if( *p < '0' || *p > '9' )
99 return FALSE;
100 ret = ret*10 + (*p - '0');
101 p++;
103 *val = ret*sign;
104 return TRUE;
107 static void value_free( struct value val )
109 if (val.type != VALUE_INTEGER)
110 cond_free( val.u.string );
115 %lex-param { COND_input *info }
116 %parse-param { COND_input *info }
117 %define api.pure
119 %union
121 struct cond_str str;
122 struct value value;
123 LPWSTR identifier;
124 INT operator;
125 BOOL bool;
128 %token COND_SPACE COND_EOF
129 %token COND_OR COND_AND COND_NOT COND_XOR COND_IMP COND_EQV
130 %token COND_LT COND_GT COND_EQ COND_NE COND_GE COND_LE
131 %token COND_ILT COND_IGT COND_IEQ COND_INE COND_IGE COND_ILE
132 %token COND_LPAR COND_RPAR COND_TILDA COND_SS COND_ISS
133 %token COND_ILHS COND_IRHS COND_LHS COND_RHS
134 %token COND_PERCENT COND_DOLLARS COND_QUESTION COND_AMPER COND_EXCLAM
135 %token <str> COND_IDENT <str> COND_NUMBER <str> COND_LITER
137 %nonassoc COND_ERROR COND_EOF
139 %type <bool> expression boolean_term boolean_factor
140 %type <value> value
141 %type <identifier> identifier
142 %type <operator> operator
146 condition:
147 expression
149 COND_input* cond = (COND_input*) info;
150 cond->result = $1;
152 | /* empty */
154 COND_input* cond = (COND_input*) info;
155 cond->result = MSICONDITION_NONE;
159 expression:
160 boolean_term
162 $$ = $1;
164 | expression COND_OR boolean_term
166 $$ = $1 || $3;
168 | expression COND_IMP boolean_term
170 $$ = !$1 || $3;
172 | expression COND_XOR boolean_term
174 $$ = ( $1 || $3 ) && !( $1 && $3 );
176 | expression COND_EQV boolean_term
178 $$ = ( $1 && $3 ) || ( !$1 && !$3 );
182 boolean_term:
183 boolean_factor
185 $$ = $1;
187 | boolean_term COND_AND boolean_factor
189 $$ = $1 && $3;
193 boolean_factor:
194 COND_NOT boolean_factor
196 $$ = !$2;
198 | value
200 if ($1.type == VALUE_INTEGER)
201 $$ = $1.u.integer ? 1 : 0;
202 else
203 $$ = $1.u.string && $1.u.string[0];
204 value_free( $1 );
206 | value operator value
208 if ($1.type == VALUE_INTEGER && $3.type == VALUE_INTEGER)
210 $$ = compare_int($1.u.integer, $2, $3.u.integer);
212 else if ($1.type != VALUE_INTEGER && $3.type != VALUE_INTEGER)
214 $$ = compare_string($1.u.string, $2, $3.u.string,
215 $1.type == VALUE_SYMBOL || $3.type == VALUE_SYMBOL);
217 else if ($1.type == VALUE_LITERAL || $3.type == VALUE_LITERAL)
219 $$ = ($2 == COND_NE || $2 == COND_INE );
221 else if ($1.type == VALUE_SYMBOL) /* symbol operator integer */
223 int num;
224 if (num_from_prop( $1.u.string, &num ))
225 $$ = compare_int( num, $2, $3.u.integer );
226 else
227 $$ = ($2 == COND_NE || $2 == COND_INE );
229 else /* integer operator symbol */
231 int num;
232 if (num_from_prop( $3.u.string, &num ))
233 $$ = compare_int( $1.u.integer, $2, num );
234 else
235 $$ = ($2 == COND_NE || $2 == COND_INE );
238 value_free( $1 );
239 value_free( $3 );
241 | COND_LPAR expression COND_RPAR
243 $$ = $2;
247 operator:
248 /* common functions */
249 COND_EQ { $$ = COND_EQ; }
250 | COND_NE { $$ = COND_NE; }
251 | COND_LT { $$ = COND_LT; }
252 | COND_GT { $$ = COND_GT; }
253 | COND_LE { $$ = COND_LE; }
254 | COND_GE { $$ = COND_GE; }
255 | COND_SS { $$ = COND_SS; }
256 | COND_IEQ { $$ = COND_IEQ; }
257 | COND_INE { $$ = COND_INE; }
258 | COND_ILT { $$ = COND_ILT; }
259 | COND_IGT { $$ = COND_IGT; }
260 | COND_ILE { $$ = COND_ILE; }
261 | COND_IGE { $$ = COND_IGE; }
262 | COND_ISS { $$ = COND_ISS; }
263 | COND_LHS { $$ = COND_LHS; }
264 | COND_RHS { $$ = COND_RHS; }
265 | COND_ILHS { $$ = COND_ILHS; }
266 | COND_IRHS { $$ = COND_IRHS; }
269 value:
270 identifier
272 COND_input* cond = (COND_input*) info;
273 UINT len;
275 $$.type = VALUE_SYMBOL;
276 $$.u.string = msi_dup_property( cond->package->db, $1 );
277 if ($$.u.string)
279 len = (lstrlenW($$.u.string) + 1) * sizeof (WCHAR);
280 $$.u.string = cond_track_mem( cond, $$.u.string, len );
282 cond_free( $1 );
284 | COND_PERCENT identifier
286 COND_input* cond = (COND_input*) info;
287 UINT len = GetEnvironmentVariableW( $2, NULL, 0 );
288 $$.type = VALUE_SYMBOL;
289 $$.u.string = NULL;
290 if (len++)
292 $$.u.string = cond_alloc( cond, len*sizeof (WCHAR) );
293 if( !$$.u.string )
294 YYABORT;
295 GetEnvironmentVariableW( $2, $$.u.string, len );
297 cond_free( $2 );
299 | COND_LITER
301 COND_input* cond = (COND_input*) info;
302 $$.type = VALUE_LITERAL;
303 $$.u.string = COND_GetLiteral( cond, &$1 );
304 if( !$$.u.string )
305 YYABORT;
307 | COND_NUMBER
309 COND_input* cond = (COND_input*) info;
310 LPWSTR szNum = COND_GetString( cond, &$1 );
311 if( !szNum )
312 YYABORT;
313 $$.type = VALUE_INTEGER;
314 $$.u.integer = wcstol( szNum, NULL, 10 );
315 cond_free( szNum );
317 | COND_DOLLARS identifier
319 COND_input* cond = (COND_input*) info;
320 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
322 if(MSI_GetComponentStateW(cond->package, $2, &install, &action ) != ERROR_SUCCESS)
324 $$.type = VALUE_LITERAL;
325 $$.u.string = NULL;
327 else
329 $$.type = VALUE_INTEGER;
330 $$.u.integer = action;
332 cond_free( $2 );
334 | COND_QUESTION identifier
336 COND_input* cond = (COND_input*) info;
337 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
339 if(MSI_GetComponentStateW(cond->package, $2, &install, &action ) != ERROR_SUCCESS)
341 $$.type = VALUE_LITERAL;
342 $$.u.string = NULL;
344 else
346 $$.type = VALUE_INTEGER;
347 $$.u.integer = install;
349 cond_free( $2 );
351 | COND_AMPER identifier
353 COND_input* cond = (COND_input*) info;
354 INSTALLSTATE install, action;
356 if (MSI_GetFeatureStateW(cond->package, $2, &install, &action ) != ERROR_SUCCESS)
358 $$.type = VALUE_LITERAL;
359 $$.u.string = NULL;
361 else
363 $$.type = VALUE_INTEGER;
364 $$.u.integer = action;
366 cond_free( $2 );
368 | COND_EXCLAM identifier
370 COND_input* cond = (COND_input*) info;
371 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
373 if(MSI_GetFeatureStateW(cond->package, $2, &install, &action ) != ERROR_SUCCESS)
375 $$.type = VALUE_LITERAL;
376 $$.u.string = NULL;
378 else
380 $$.type = VALUE_INTEGER;
381 $$.u.integer = install;
383 cond_free( $2 );
387 identifier:
388 COND_IDENT
390 COND_input* cond = (COND_input*) info;
391 $$ = COND_GetString( cond, &$1 );
392 if( !$$ )
393 YYABORT;
400 static int COND_IsAlpha( WCHAR x )
402 return( ( ( x >= 'A' ) && ( x <= 'Z' ) ) ||
403 ( ( x >= 'a' ) && ( x <= 'z' ) ) ||
404 ( ( x == '_' ) ) );
407 static int COND_IsNumber( WCHAR x )
409 return( (( x >= '0' ) && ( x <= '9' )) || (x =='-') || (x =='.') );
412 static WCHAR *strstriW( const WCHAR *str, const WCHAR *sub )
414 LPWSTR strlower, sublower, r;
415 strlower = CharLowerW( strdupW( str ) );
416 sublower = CharLowerW( strdupW( sub ) );
417 r = wcsstr( strlower, sublower );
418 if (r)
419 r = (LPWSTR)str + (r - strlower);
420 msi_free( strlower );
421 msi_free( sublower );
422 return r;
425 static BOOL str_is_number( LPCWSTR str )
427 int i;
429 if (!*str)
430 return FALSE;
432 for (i = 0; i < lstrlenW( str ); i++)
433 if (!iswdigit(str[i]))
434 return FALSE;
436 return TRUE;
439 static INT compare_substring( LPCWSTR a, INT operator, LPCWSTR b )
441 int lhs, rhs;
443 /* substring operators return 0 if LHS is missing */
444 if (!a || !*a)
445 return 0;
447 /* substring operators return 1 if RHS is missing */
448 if (!b || !*b)
449 return 1;
451 /* if both strings contain only numbers, use integer comparison */
452 lhs = wcstol(a, NULL, 10);
453 rhs = wcstol(b, NULL, 10);
454 if (str_is_number(a) && str_is_number(b))
455 return compare_int( lhs, operator, rhs );
457 switch (operator)
459 case COND_SS:
460 return wcsstr( a, b ) != 0;
461 case COND_ISS:
462 return strstriW( a, b ) != 0;
463 case COND_LHS:
465 int l = lstrlenW( a );
466 int r = lstrlenW( b );
467 if (r > l) return 0;
468 return !wcsncmp( a, b, r );
470 case COND_RHS:
472 int l = lstrlenW( a );
473 int r = lstrlenW( b );
474 if (r > l) return 0;
475 return !wcsncmp( a + (l - r), b, r );
477 case COND_ILHS:
479 int l = lstrlenW( a );
480 int r = lstrlenW( b );
481 if (r > l) return 0;
482 return !wcsnicmp( a, b, r );
484 case COND_IRHS:
486 int l = lstrlenW( a );
487 int r = lstrlenW( b );
488 if (r > l) return 0;
489 return !wcsnicmp( a + (l - r), b, r );
491 default:
492 ERR("invalid substring operator\n");
493 return 0;
495 return 0;
498 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b, BOOL convert )
500 if (operator >= COND_SS && operator <= COND_RHS)
501 return compare_substring( a, operator, b );
503 /* null and empty string are equivalent */
504 if (!a) a = L"";
505 if (!b) b = L"";
507 if (convert && str_is_number(a) && str_is_number(b))
508 return compare_int( wcstol(a, NULL, 10), operator, wcstol(b, NULL, 10) );
510 /* a or b may be NULL */
511 switch (operator)
513 case COND_LT:
514 return wcscmp( a, b ) < 0;
515 case COND_GT:
516 return wcscmp( a, b ) > 0;
517 case COND_EQ:
518 return wcscmp( a, b ) == 0;
519 case COND_NE:
520 return wcscmp( a, b ) != 0;
521 case COND_GE:
522 return wcscmp( a, b ) >= 0;
523 case COND_LE:
524 return wcscmp( a, b ) <= 0;
525 case COND_ILT:
526 return wcsicmp( a, b ) < 0;
527 case COND_IGT:
528 return wcsicmp( a, b ) > 0;
529 case COND_IEQ:
530 return wcsicmp( a, b ) == 0;
531 case COND_INE:
532 return wcsicmp( a, b ) != 0;
533 case COND_IGE:
534 return wcsicmp( a, b ) >= 0;
535 case COND_ILE:
536 return wcsicmp( a, b ) <= 0;
537 default:
538 ERR("invalid string operator\n");
539 return 0;
541 return 0;
545 static INT compare_int( INT a, INT operator, INT b )
547 switch (operator)
549 case COND_LT:
550 case COND_ILT:
551 return a < b;
552 case COND_GT:
553 case COND_IGT:
554 return a > b;
555 case COND_EQ:
556 case COND_IEQ:
557 return a == b;
558 case COND_NE:
559 case COND_INE:
560 return a != b;
561 case COND_GE:
562 case COND_IGE:
563 return a >= b;
564 case COND_LE:
565 case COND_ILE:
566 return a <= b;
567 case COND_SS:
568 case COND_ISS:
569 return ( a & b ) ? 1 : 0;
570 case COND_RHS:
571 return ( ( a & 0xffff ) == b ) ? 1 : 0;
572 case COND_LHS:
573 return ( ( (a>>16) & 0xffff ) == b ) ? 1 : 0;
574 default:
575 ERR("invalid integer operator\n");
576 return 0;
578 return 0;
582 static int COND_IsIdent( WCHAR x )
584 return( COND_IsAlpha( x ) || COND_IsNumber( x ) || ( x == '_' )
585 || ( x == '#' ) || (x == '.') );
588 static int COND_GetOperator( COND_input *cond )
590 static const struct {
591 const WCHAR str[4];
592 int id;
593 } table[] = {
594 { L"~<=", COND_ILE },
595 { L"~><", COND_ISS },
596 { L"~>>", COND_IRHS },
597 { L"~<>", COND_INE },
598 { L"~>=", COND_IGE },
599 { L"~<<", COND_ILHS },
600 { L"~=", COND_IEQ },
601 { L"~<", COND_ILT },
602 { L"~>", COND_IGT },
603 { L">=", COND_GE },
604 { L"><", COND_SS },
605 { L"<<", COND_LHS },
606 { L"<>", COND_NE },
607 { L"<=", COND_LE },
608 { L">>", COND_RHS },
609 { L">", COND_GT },
610 { L"<", COND_LT },
611 { L"", 0 }
613 LPCWSTR p = &cond->str[cond->n];
614 int i = 0, len;
616 while ( 1 )
618 len = lstrlenW( table[i].str );
619 if ( !len || 0 == wcsncmp( table[i].str, p, len ) )
620 break;
621 i++;
623 cond->n += len;
624 return table[i].id;
627 static int COND_GetOne( struct cond_str *str, COND_input *cond )
629 int rc, len = 1;
630 WCHAR ch;
632 str->data = &cond->str[cond->n];
634 ch = str->data[0];
636 switch( ch )
638 case 0: return 0;
639 case '(': rc = COND_LPAR; break;
640 case ')': rc = COND_RPAR; break;
641 case '&': rc = COND_AMPER; break;
642 case '!': rc = COND_EXCLAM; break;
643 case '$': rc = COND_DOLLARS; break;
644 case '?': rc = COND_QUESTION; break;
645 case '%': rc = COND_PERCENT; break;
646 case ' ': rc = COND_SPACE; break;
647 case '=': rc = COND_EQ; break;
649 case '~':
650 case '<':
651 case '>':
652 rc = COND_GetOperator( cond );
653 if (!rc)
654 rc = COND_ERROR;
655 return rc;
656 default:
657 rc = 0;
660 if ( rc )
662 cond->n += len;
663 return rc;
666 if (ch == '"' )
668 LPCWSTR p = wcschr( str->data + 1, '"' );
669 if (!p) return COND_ERROR;
670 len = p - str->data + 1;
671 rc = COND_LITER;
673 else if( COND_IsAlpha( ch ) )
675 while( COND_IsIdent( str->data[len] ) )
676 len++;
677 rc = COND_IDENT;
679 if ( len == 3 )
681 if ( !wcsnicmp( str->data, L"NOT", len ) )
682 rc = COND_NOT;
683 else if( !wcsnicmp( str->data, L"AND", len ) )
684 rc = COND_AND;
685 else if( !wcsnicmp( str->data, L"XOR", len ) )
686 rc = COND_XOR;
687 else if( !wcsnicmp( str->data, L"EQV", len ) )
688 rc = COND_EQV;
689 else if( !wcsnicmp( str->data, L"IMP", len ) )
690 rc = COND_IMP;
692 else if( (len == 2) && !wcsnicmp( str->data, L"OR", len ) )
693 rc = COND_OR;
695 else if( COND_IsNumber( ch ) )
697 while( COND_IsNumber( str->data[len] ) )
698 len++;
699 rc = COND_NUMBER;
701 else
703 ERR("Got unknown character %c(%x)\n",ch,ch);
704 return COND_ERROR;
707 cond->n += len;
708 str->len = len;
710 return rc;
713 static int cond_lex( void *COND_lval, COND_input *cond )
715 int rc;
716 struct cond_str *str = COND_lval;
718 do {
719 rc = COND_GetOne( str, cond );
720 } while (rc == COND_SPACE);
722 return rc;
725 static LPWSTR COND_GetString( COND_input *cond, const struct cond_str *str )
727 LPWSTR ret;
729 ret = cond_alloc( cond, (str->len+1) * sizeof (WCHAR) );
730 if( ret )
732 memcpy( ret, str->data, str->len * sizeof(WCHAR));
733 ret[str->len]=0;
735 TRACE("Got identifier %s\n",debugstr_w(ret));
736 return ret;
739 static LPWSTR COND_GetLiteral( COND_input *cond, const struct cond_str *str )
741 LPWSTR ret;
743 ret = cond_alloc( cond, (str->len-1) * sizeof (WCHAR) );
744 if( ret )
746 memcpy( ret, str->data+1, (str->len-2) * sizeof(WCHAR) );
747 ret[str->len - 2]=0;
749 TRACE("Got literal %s\n",debugstr_w(ret));
750 return ret;
753 static void *cond_alloc( COND_input *cond, unsigned int sz )
755 struct list *mem;
757 mem = msi_alloc( sizeof (struct list) + sz );
758 if( !mem )
759 return NULL;
761 list_add_head( &(cond->mem), mem );
762 return mem + 1;
765 static void *cond_track_mem( COND_input *cond, void *ptr, unsigned int sz )
767 void *new_ptr;
769 if( !ptr )
770 return ptr;
772 new_ptr = cond_alloc( cond, sz );
773 if( !new_ptr )
775 msi_free( ptr );
776 return NULL;
779 memcpy( new_ptr, ptr, sz );
780 msi_free( ptr );
781 return new_ptr;
784 static void cond_free( void *ptr )
786 struct list *mem = (struct list *)ptr - 1;
788 if( ptr )
790 list_remove( mem );
791 msi_free( mem );
795 static int cond_error( COND_input *info, const char *str )
797 TRACE("%s\n", str );
798 return 0;
801 MSICONDITION MSI_EvaluateConditionW( MSIPACKAGE *package, LPCWSTR szCondition )
803 COND_input cond;
804 MSICONDITION r;
805 struct list *mem, *safety;
807 TRACE("%s\n", debugstr_w( szCondition ) );
809 if (szCondition == NULL) return MSICONDITION_NONE;
811 cond.package = package;
812 cond.str = szCondition;
813 cond.n = 0;
814 cond.result = MSICONDITION_ERROR;
816 list_init( &cond.mem );
818 if ( !cond_parse( &cond ) )
819 r = cond.result;
820 else
821 r = MSICONDITION_ERROR;
823 LIST_FOR_EACH_SAFE( mem, safety, &cond.mem )
825 /* The tracked memory lives directly after the list struct */
826 void *ptr = mem + 1;
827 if ( r != MSICONDITION_ERROR )
828 WARN( "condition parser failed to free up some memory: %p\n", ptr );
829 cond_free( ptr );
832 TRACE("%i <- %s\n", r, debugstr_w(szCondition));
833 return r;
836 MSICONDITION WINAPI MsiEvaluateConditionW( MSIHANDLE hInstall, LPCWSTR szCondition )
838 MSIPACKAGE *package;
839 UINT ret;
841 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
842 if( !package )
844 MSIHANDLE remote;
846 if (!(remote = msi_get_remote(hInstall)))
847 return MSICONDITION_ERROR;
849 if (!szCondition)
850 return MSICONDITION_NONE;
852 __TRY
854 ret = remote_EvaluateCondition(remote, szCondition);
856 __EXCEPT(rpc_filter)
858 ret = GetExceptionCode();
860 __ENDTRY
862 return ret;
865 ret = MSI_EvaluateConditionW( package, szCondition );
866 msiobj_release( &package->hdr );
867 return ret;
870 MSICONDITION WINAPI MsiEvaluateConditionA( MSIHANDLE hInstall, LPCSTR szCondition )
872 LPWSTR szwCond = NULL;
873 MSICONDITION r;
875 szwCond = strdupAtoW( szCondition );
876 if( szCondition && !szwCond )
877 return MSICONDITION_ERROR;
879 r = MsiEvaluateConditionW( hInstall, szwCond );
880 msi_free( szwCond );
881 return r;