dmime: Remove DECLSPEC_HIDDEN usage.
[wine.git] / dlls / msi / cond.y
blobb08c67447dd315355312dd8d821ce23042f10202
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.prefix {cond_}
118 %define api.pure
120 %union
122 struct cond_str str;
123 struct value value;
124 LPWSTR identifier;
125 INT operator;
126 BOOL bool;
129 %token COND_SPACE
130 %token COND_OR COND_AND COND_NOT COND_XOR COND_IMP COND_EQV
131 %token COND_LT COND_GT COND_EQ COND_NE COND_GE COND_LE
132 %token COND_ILT COND_IGT COND_IEQ COND_INE COND_IGE COND_ILE
133 %token COND_LPAR COND_RPAR COND_TILDA COND_SS COND_ISS
134 %token COND_ILHS COND_IRHS COND_LHS COND_RHS
135 %token COND_PERCENT COND_DOLLARS COND_QUESTION COND_AMPER COND_EXCLAM
136 %token <str> COND_IDENT <str> COND_NUMBER <str> COND_LITER
138 %nonassoc COND_ERROR
140 %type <bool> expression boolean_term boolean_factor
141 %type <value> value
142 %type <identifier> identifier
143 %type <operator> operator
147 condition:
148 expression
150 COND_input* cond = (COND_input*) info;
151 cond->result = $1;
153 | /* empty */
155 COND_input* cond = (COND_input*) info;
156 cond->result = MSICONDITION_NONE;
160 expression:
161 boolean_term
163 $$ = $1;
165 | expression COND_OR boolean_term
167 $$ = $1 || $3;
169 | expression COND_IMP boolean_term
171 $$ = !$1 || $3;
173 | expression COND_XOR boolean_term
175 $$ = ( $1 || $3 ) && !( $1 && $3 );
177 | expression COND_EQV boolean_term
179 $$ = ( $1 && $3 ) || ( !$1 && !$3 );
183 boolean_term:
184 boolean_factor
186 $$ = $1;
188 | boolean_term COND_AND boolean_factor
190 $$ = $1 && $3;
194 boolean_factor:
195 COND_NOT boolean_factor
197 $$ = !$2;
199 | value
201 if ($1.type == VALUE_INTEGER)
202 $$ = $1.u.integer ? 1 : 0;
203 else
204 $$ = $1.u.string && $1.u.string[0];
205 value_free( $1 );
207 | value operator value
209 if ($1.type == VALUE_INTEGER && $3.type == VALUE_INTEGER)
211 $$ = compare_int($1.u.integer, $2, $3.u.integer);
213 else if ($1.type != VALUE_INTEGER && $3.type != VALUE_INTEGER)
215 $$ = compare_string($1.u.string, $2, $3.u.string,
216 $1.type == VALUE_SYMBOL || $3.type == VALUE_SYMBOL);
218 else if ($1.type == VALUE_LITERAL || $3.type == VALUE_LITERAL)
220 $$ = ($2 == COND_NE || $2 == COND_INE );
222 else if ($1.type == VALUE_SYMBOL) /* symbol operator integer */
224 int num;
225 if (num_from_prop( $1.u.string, &num ))
226 $$ = compare_int( num, $2, $3.u.integer );
227 else
228 $$ = ($2 == COND_NE || $2 == COND_INE );
230 else /* integer operator symbol */
232 int num;
233 if (num_from_prop( $3.u.string, &num ))
234 $$ = compare_int( $1.u.integer, $2, num );
235 else
236 $$ = ($2 == COND_NE || $2 == COND_INE );
239 value_free( $1 );
240 value_free( $3 );
242 | COND_LPAR expression COND_RPAR
244 $$ = $2;
248 operator:
249 /* common functions */
250 COND_EQ { $$ = COND_EQ; }
251 | COND_NE { $$ = COND_NE; }
252 | COND_LT { $$ = COND_LT; }
253 | COND_GT { $$ = COND_GT; }
254 | COND_LE { $$ = COND_LE; }
255 | COND_GE { $$ = COND_GE; }
256 | COND_SS { $$ = COND_SS; }
257 | COND_IEQ { $$ = COND_IEQ; }
258 | COND_INE { $$ = COND_INE; }
259 | COND_ILT { $$ = COND_ILT; }
260 | COND_IGT { $$ = COND_IGT; }
261 | COND_ILE { $$ = COND_ILE; }
262 | COND_IGE { $$ = COND_IGE; }
263 | COND_ISS { $$ = COND_ISS; }
264 | COND_LHS { $$ = COND_LHS; }
265 | COND_RHS { $$ = COND_RHS; }
266 | COND_ILHS { $$ = COND_ILHS; }
267 | COND_IRHS { $$ = COND_IRHS; }
270 value:
271 identifier
273 COND_input* cond = (COND_input*) info;
274 UINT len;
276 $$.type = VALUE_SYMBOL;
277 $$.u.string = msi_dup_property( cond->package->db, $1 );
278 if ($$.u.string)
280 len = (lstrlenW($$.u.string) + 1) * sizeof (WCHAR);
281 $$.u.string = cond_track_mem( cond, $$.u.string, len );
283 cond_free( $1 );
285 | COND_PERCENT identifier
287 COND_input* cond = (COND_input*) info;
288 UINT len = GetEnvironmentVariableW( $2, NULL, 0 );
289 $$.type = VALUE_SYMBOL;
290 $$.u.string = NULL;
291 if (len++)
293 $$.u.string = cond_alloc( cond, len*sizeof (WCHAR) );
294 if( !$$.u.string )
295 YYABORT;
296 GetEnvironmentVariableW( $2, $$.u.string, len );
298 cond_free( $2 );
300 | COND_LITER
302 COND_input* cond = (COND_input*) info;
303 $$.type = VALUE_LITERAL;
304 $$.u.string = COND_GetLiteral( cond, &$1 );
305 if( !$$.u.string )
306 YYABORT;
308 | COND_NUMBER
310 COND_input* cond = (COND_input*) info;
311 LPWSTR szNum = COND_GetString( cond, &$1 );
312 if( !szNum )
313 YYABORT;
314 $$.type = VALUE_INTEGER;
315 $$.u.integer = wcstol( szNum, NULL, 10 );
316 cond_free( szNum );
318 | COND_DOLLARS identifier
320 COND_input* cond = (COND_input*) info;
321 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
323 if(MSI_GetComponentStateW(cond->package, $2, &install, &action ) != ERROR_SUCCESS)
325 $$.type = VALUE_LITERAL;
326 $$.u.string = NULL;
328 else
330 $$.type = VALUE_INTEGER;
331 $$.u.integer = action;
333 cond_free( $2 );
335 | COND_QUESTION identifier
337 COND_input* cond = (COND_input*) info;
338 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
340 if(MSI_GetComponentStateW(cond->package, $2, &install, &action ) != ERROR_SUCCESS)
342 $$.type = VALUE_LITERAL;
343 $$.u.string = NULL;
345 else
347 $$.type = VALUE_INTEGER;
348 $$.u.integer = install;
350 cond_free( $2 );
352 | COND_AMPER identifier
354 COND_input* cond = (COND_input*) info;
355 INSTALLSTATE install, action;
357 if (MSI_GetFeatureStateW(cond->package, $2, &install, &action ) != ERROR_SUCCESS)
359 $$.type = VALUE_LITERAL;
360 $$.u.string = NULL;
362 else
364 $$.type = VALUE_INTEGER;
365 $$.u.integer = action;
367 cond_free( $2 );
369 | COND_EXCLAM identifier
371 COND_input* cond = (COND_input*) info;
372 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
374 if(MSI_GetFeatureStateW(cond->package, $2, &install, &action ) != ERROR_SUCCESS)
376 $$.type = VALUE_LITERAL;
377 $$.u.string = NULL;
379 else
381 $$.type = VALUE_INTEGER;
382 $$.u.integer = install;
384 cond_free( $2 );
388 identifier:
389 COND_IDENT
391 COND_input* cond = (COND_input*) info;
392 $$ = COND_GetString( cond, &$1 );
393 if( !$$ )
394 YYABORT;
401 static int COND_IsAlpha( WCHAR x )
403 return( ( ( x >= 'A' ) && ( x <= 'Z' ) ) ||
404 ( ( x >= 'a' ) && ( x <= 'z' ) ) ||
405 ( ( x == '_' ) ) );
408 static int COND_IsNumber( WCHAR x )
410 return( (( x >= '0' ) && ( x <= '9' )) || (x =='-') || (x =='.') );
413 static WCHAR *strstriW( const WCHAR *str, const WCHAR *sub )
415 LPWSTR strlower, sublower, r;
416 strlower = CharLowerW( wcsdup( str ) );
417 sublower = CharLowerW( wcsdup( sub ) );
418 r = wcsstr( strlower, sublower );
419 if (r)
420 r = (LPWSTR)str + (r - strlower);
421 free( strlower );
422 free( sublower );
423 return r;
426 static BOOL str_is_number( LPCWSTR str )
428 int i;
430 if (!*str)
431 return FALSE;
433 for (i = 0; i < lstrlenW( str ); i++)
434 if (!iswdigit(str[i]))
435 return FALSE;
437 return TRUE;
440 static INT compare_substring( LPCWSTR a, INT operator, LPCWSTR b )
442 int lhs, rhs;
444 /* substring operators return 0 if LHS is missing */
445 if (!a || !*a)
446 return 0;
448 /* substring operators return 1 if RHS is missing */
449 if (!b || !*b)
450 return 1;
452 /* if both strings contain only numbers, use integer comparison */
453 lhs = wcstol(a, NULL, 10);
454 rhs = wcstol(b, NULL, 10);
455 if (str_is_number(a) && str_is_number(b))
456 return compare_int( lhs, operator, rhs );
458 switch (operator)
460 case COND_SS:
461 return wcsstr( a, b ) != 0;
462 case COND_ISS:
463 return strstriW( a, b ) != 0;
464 case COND_LHS:
466 int l = lstrlenW( a );
467 int r = lstrlenW( b );
468 if (r > l) return 0;
469 return !wcsncmp( a, b, r );
471 case COND_RHS:
473 int l = lstrlenW( a );
474 int r = lstrlenW( b );
475 if (r > l) return 0;
476 return !wcsncmp( a + (l - r), b, r );
478 case COND_ILHS:
480 int l = lstrlenW( a );
481 int r = lstrlenW( b );
482 if (r > l) return 0;
483 return !wcsnicmp( a, b, r );
485 case COND_IRHS:
487 int l = lstrlenW( a );
488 int r = lstrlenW( b );
489 if (r > l) return 0;
490 return !wcsnicmp( a + (l - r), b, r );
492 default:
493 ERR("invalid substring operator\n");
494 return 0;
496 return 0;
499 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b, BOOL convert )
501 if (operator >= COND_SS && operator <= COND_RHS)
502 return compare_substring( a, operator, b );
504 /* null and empty string are equivalent */
505 if (!a) a = L"";
506 if (!b) b = L"";
508 if (convert && str_is_number(a) && str_is_number(b))
509 return compare_int( wcstol(a, NULL, 10), operator, wcstol(b, NULL, 10) );
511 /* a or b may be NULL */
512 switch (operator)
514 case COND_LT:
515 return wcscmp( a, b ) < 0;
516 case COND_GT:
517 return wcscmp( a, b ) > 0;
518 case COND_EQ:
519 return wcscmp( a, b ) == 0;
520 case COND_NE:
521 return wcscmp( a, b ) != 0;
522 case COND_GE:
523 return wcscmp( a, b ) >= 0;
524 case COND_LE:
525 return wcscmp( a, b ) <= 0;
526 case COND_ILT:
527 return wcsicmp( a, b ) < 0;
528 case COND_IGT:
529 return wcsicmp( a, b ) > 0;
530 case COND_IEQ:
531 return wcsicmp( a, b ) == 0;
532 case COND_INE:
533 return wcsicmp( a, b ) != 0;
534 case COND_IGE:
535 return wcsicmp( a, b ) >= 0;
536 case COND_ILE:
537 return wcsicmp( a, b ) <= 0;
538 default:
539 ERR("invalid string operator\n");
540 return 0;
542 return 0;
546 static INT compare_int( INT a, INT operator, INT b )
548 switch (operator)
550 case COND_LT:
551 case COND_ILT:
552 return a < b;
553 case COND_GT:
554 case COND_IGT:
555 return a > b;
556 case COND_EQ:
557 case COND_IEQ:
558 return a == b;
559 case COND_NE:
560 case COND_INE:
561 return a != b;
562 case COND_GE:
563 case COND_IGE:
564 return a >= b;
565 case COND_LE:
566 case COND_ILE:
567 return a <= b;
568 case COND_SS:
569 case COND_ISS:
570 return ( a & b ) ? 1 : 0;
571 case COND_RHS:
572 return ( ( a & 0xffff ) == b ) ? 1 : 0;
573 case COND_LHS:
574 return ( ( (a>>16) & 0xffff ) == b ) ? 1 : 0;
575 default:
576 ERR("invalid integer operator\n");
577 return 0;
579 return 0;
583 static int COND_IsIdent( WCHAR x )
585 return( COND_IsAlpha( x ) || COND_IsNumber( x ) || ( x == '_' )
586 || ( x == '#' ) || (x == '.') );
589 static int COND_GetOperator( COND_input *cond )
591 static const struct {
592 const WCHAR str[4];
593 int id;
594 } table[] = {
595 { L"~<=", COND_ILE },
596 { L"~><", COND_ISS },
597 { L"~>>", COND_IRHS },
598 { L"~<>", COND_INE },
599 { L"~>=", COND_IGE },
600 { L"~<<", COND_ILHS },
601 { L"~=", COND_IEQ },
602 { L"~<", COND_ILT },
603 { L"~>", COND_IGT },
604 { L">=", COND_GE },
605 { L"><", COND_SS },
606 { L"<<", COND_LHS },
607 { L"<>", COND_NE },
608 { L"<=", COND_LE },
609 { L">>", COND_RHS },
610 { L">", COND_GT },
611 { L"<", COND_LT },
612 { L"", 0 }
614 LPCWSTR p = &cond->str[cond->n];
615 int i = 0, len;
617 while ( 1 )
619 len = lstrlenW( table[i].str );
620 if ( !len || 0 == wcsncmp( table[i].str, p, len ) )
621 break;
622 i++;
624 cond->n += len;
625 return table[i].id;
628 static int COND_GetOne( struct cond_str *str, COND_input *cond )
630 int rc, len = 1;
631 WCHAR ch;
633 str->data = &cond->str[cond->n];
635 ch = str->data[0];
637 switch( ch )
639 case 0: return 0;
640 case '(': rc = COND_LPAR; break;
641 case ')': rc = COND_RPAR; break;
642 case '&': rc = COND_AMPER; break;
643 case '!': rc = COND_EXCLAM; break;
644 case '$': rc = COND_DOLLARS; break;
645 case '?': rc = COND_QUESTION; break;
646 case '%': rc = COND_PERCENT; break;
647 case ' ': rc = COND_SPACE; break;
648 case '=': rc = COND_EQ; break;
650 case '~':
651 case '<':
652 case '>':
653 rc = COND_GetOperator( cond );
654 if (!rc)
655 rc = COND_ERROR;
656 return rc;
657 default:
658 rc = 0;
661 if ( rc )
663 cond->n += len;
664 return rc;
667 if (ch == '"' )
669 LPCWSTR p = wcschr( str->data + 1, '"' );
670 if (!p) return COND_ERROR;
671 len = p - str->data + 1;
672 rc = COND_LITER;
674 else if( COND_IsAlpha( ch ) )
676 while( COND_IsIdent( str->data[len] ) )
677 len++;
678 rc = COND_IDENT;
680 if ( len == 3 )
682 if ( !wcsnicmp( str->data, L"NOT", len ) )
683 rc = COND_NOT;
684 else if( !wcsnicmp( str->data, L"AND", len ) )
685 rc = COND_AND;
686 else if( !wcsnicmp( str->data, L"XOR", len ) )
687 rc = COND_XOR;
688 else if( !wcsnicmp( str->data, L"EQV", len ) )
689 rc = COND_EQV;
690 else if( !wcsnicmp( str->data, L"IMP", len ) )
691 rc = COND_IMP;
693 else if( (len == 2) && !wcsnicmp( str->data, L"OR", len ) )
694 rc = COND_OR;
696 else if( COND_IsNumber( ch ) )
698 while( COND_IsNumber( str->data[len] ) )
699 len++;
700 rc = COND_NUMBER;
702 else
704 ERR("Got unknown character %c(%x)\n",ch,ch);
705 return COND_ERROR;
708 cond->n += len;
709 str->len = len;
711 return rc;
714 static int cond_lex( void *COND_lval, COND_input *cond )
716 int rc;
717 struct cond_str *str = COND_lval;
719 do {
720 rc = COND_GetOne( str, cond );
721 } while (rc == COND_SPACE);
723 return rc;
726 static LPWSTR COND_GetString( COND_input *cond, const struct cond_str *str )
728 LPWSTR ret;
730 ret = cond_alloc( cond, (str->len+1) * sizeof (WCHAR) );
731 if( ret )
733 memcpy( ret, str->data, str->len * sizeof(WCHAR));
734 ret[str->len]=0;
736 TRACE("Got identifier %s\n",debugstr_w(ret));
737 return ret;
740 static LPWSTR COND_GetLiteral( COND_input *cond, const struct cond_str *str )
742 LPWSTR ret;
744 ret = cond_alloc( cond, (str->len-1) * sizeof (WCHAR) );
745 if( ret )
747 memcpy( ret, str->data+1, (str->len-2) * sizeof(WCHAR) );
748 ret[str->len - 2]=0;
750 TRACE("Got literal %s\n",debugstr_w(ret));
751 return ret;
754 static void *cond_alloc( COND_input *cond, unsigned int sz )
756 struct list *mem;
758 mem = malloc( sizeof (struct list) + sz );
759 if( !mem )
760 return NULL;
762 list_add_head( &(cond->mem), mem );
763 return mem + 1;
766 static void *cond_track_mem( COND_input *cond, void *ptr, unsigned int sz )
768 void *new_ptr;
770 if( !ptr )
771 return ptr;
773 new_ptr = cond_alloc( cond, sz );
774 if( !new_ptr )
776 free( ptr );
777 return NULL;
780 memcpy( new_ptr, ptr, sz );
781 free( ptr );
782 return new_ptr;
785 static void cond_free( void *ptr )
787 struct list *mem = (struct list *)ptr - 1;
789 if( ptr )
791 list_remove( mem );
792 free( mem );
796 static int cond_error( COND_input *info, const char *str )
798 TRACE("%s\n", str );
799 return 0;
802 MSICONDITION MSI_EvaluateConditionW( MSIPACKAGE *package, LPCWSTR szCondition )
804 COND_input cond;
805 MSICONDITION r;
806 struct list *mem, *safety;
808 TRACE("%s\n", debugstr_w( szCondition ) );
810 if (szCondition == NULL) return MSICONDITION_NONE;
812 cond.package = package;
813 cond.str = szCondition;
814 cond.n = 0;
815 cond.result = MSICONDITION_ERROR;
817 list_init( &cond.mem );
819 if ( !cond_parse( &cond ) )
820 r = cond.result;
821 else
822 r = MSICONDITION_ERROR;
824 LIST_FOR_EACH_SAFE( mem, safety, &cond.mem )
826 /* The tracked memory lives directly after the list struct */
827 void *ptr = mem + 1;
828 if ( r != MSICONDITION_ERROR )
829 WARN( "condition parser failed to free up some memory: %p\n", ptr );
830 cond_free( ptr );
833 TRACE("%i <- %s\n", r, debugstr_w(szCondition));
834 return r;
837 MSICONDITION WINAPI MsiEvaluateConditionW( MSIHANDLE hInstall, LPCWSTR szCondition )
839 MSIPACKAGE *package;
840 UINT ret;
842 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
843 if( !package )
845 MSIHANDLE remote;
847 if (!(remote = msi_get_remote(hInstall)))
848 return MSICONDITION_ERROR;
850 if (!szCondition)
851 return MSICONDITION_NONE;
853 __TRY
855 ret = remote_EvaluateCondition(remote, szCondition);
857 __EXCEPT(rpc_filter)
859 ret = GetExceptionCode();
861 __ENDTRY
863 return ret;
866 ret = MSI_EvaluateConditionW( package, szCondition );
867 msiobj_release( &package->hdr );
868 return ret;
871 MSICONDITION WINAPI MsiEvaluateConditionA( MSIHANDLE hInstall, LPCSTR szCondition )
873 LPWSTR szwCond = NULL;
874 MSICONDITION r;
876 szwCond = strdupAtoW( szCondition );
877 if( szCondition && !szwCond )
878 return MSICONDITION_ERROR;
880 r = MsiEvaluateConditionW( hInstall, szwCond );
881 free( szwCond );
882 return r;