gdiplus: Convert formats that can't be drawn by gdi32 when drawing bitmaps.
[wine.git] / dlls / msi / cond.y
blobf3db635a10aaed3a9d5a8777c6ed573a1e7b2d78
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"
43 #include "wine/list.h"
45 #define YYLEX_PARAM info
46 #define YYPARSE_PARAM info
48 static int cond_error(const char *str);
50 WINE_DEFAULT_DEBUG_CHANNEL(msi);
52 typedef struct tag_yyinput
54 MSIPACKAGE *package;
55 LPCWSTR str;
56 INT n;
57 MSICONDITION result;
58 struct list mem;
59 } COND_input;
61 struct cond_str {
62 LPCWSTR data;
63 INT len;
66 static LPWSTR COND_GetString( COND_input *info, const struct cond_str *str );
67 static LPWSTR COND_GetLiteral( COND_input *info, const struct cond_str *str );
68 static int cond_lex( void *COND_lval, COND_input *info);
70 static void *cond_alloc( COND_input *cond, unsigned int sz );
71 static void *cond_track_mem( COND_input *cond, void *ptr, unsigned int sz );
72 static void cond_free( void *ptr );
74 static INT compare_int( INT a, INT operator, INT b );
75 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b, BOOL convert );
77 static INT compare_and_free_strings( LPWSTR a, INT op, LPWSTR b, BOOL convert )
79 INT r;
81 r = compare_string( a, op, b, convert );
82 cond_free( a );
83 cond_free( b );
84 return r;
87 static BOOL num_from_prop( LPCWSTR p, INT *val )
89 INT ret = 0, sign = 1;
91 if (!p)
92 return FALSE;
93 if (*p == '-')
95 sign = -1;
96 p++;
98 if (!*p)
99 return FALSE;
100 while (*p)
102 if( *p < '0' || *p > '9' )
103 return FALSE;
104 ret = ret*10 + (*p - '0');
105 p++;
107 *val = ret*sign;
108 return TRUE;
113 %pure-parser
115 %union
117 struct cond_str str;
118 LPWSTR string;
119 INT value;
122 %token COND_SPACE COND_EOF
123 %token COND_OR COND_AND COND_NOT COND_XOR COND_IMP COND_EQV
124 %token COND_LT COND_GT COND_EQ COND_NE COND_GE COND_LE
125 %token COND_ILT COND_IGT COND_IEQ COND_INE COND_IGE COND_ILE
126 %token COND_LPAR COND_RPAR COND_TILDA COND_SS COND_ISS
127 %token COND_ILHS COND_IRHS COND_LHS COND_RHS
128 %token COND_PERCENT COND_DOLLARS COND_QUESTION COND_AMPER COND_EXCLAM
129 %token <str> COND_IDENT <str> COND_NUMBER <str> COND_LITER
131 %nonassoc COND_ERROR COND_EOF
133 %type <value> expression boolean_term boolean_factor
134 %type <value> value_i integer operator
135 %type <string> identifier symbol_s value_s literal
139 condition:
140 expression
142 COND_input* cond = (COND_input*) info;
143 cond->result = $1;
145 | /* empty */
147 COND_input* cond = (COND_input*) info;
148 cond->result = MSICONDITION_NONE;
152 expression:
153 boolean_term
155 $$ = $1;
157 | expression COND_OR boolean_term
159 $$ = $1 || $3;
161 | expression COND_IMP boolean_term
163 $$ = !$1 || $3;
165 | expression COND_XOR boolean_term
167 $$ = ( $1 || $3 ) && !( $1 && $3 );
169 | expression COND_EQV boolean_term
171 $$ = ( $1 && $3 ) || ( !$1 && !$3 );
175 boolean_term:
176 boolean_factor
178 $$ = $1;
180 | boolean_term COND_AND boolean_factor
182 $$ = $1 && $3;
186 boolean_factor:
187 COND_NOT boolean_factor
189 $$ = $2 ? 0 : 1;
191 | value_i
193 $$ = $1 ? 1 : 0;
195 | value_s
197 $$ = ($1 && $1[0]) ? 1 : 0;
198 cond_free( $1 );
200 | value_i operator value_i
202 $$ = compare_int( $1, $2, $3 );
204 | symbol_s operator value_i
206 int num;
207 if (num_from_prop( $1, &num ))
208 $$ = compare_int( num, $2, $3 );
209 else
210 $$ = ($2 == COND_NE || $2 == COND_INE );
211 cond_free( $1 );
213 | value_i operator symbol_s
215 int num;
216 if (num_from_prop( $3, &num ))
217 $$ = compare_int( $1, $2, num );
218 else
219 $$ = ($2 == COND_NE || $2 == COND_INE );
220 cond_free( $3 );
222 | symbol_s operator symbol_s
224 $$ = compare_and_free_strings( $1, $2, $3, TRUE );
226 | symbol_s operator literal
228 $$ = compare_and_free_strings( $1, $2, $3, TRUE );
230 | literal operator symbol_s
232 $$ = compare_and_free_strings( $1, $2, $3, TRUE );
234 | literal operator literal
236 $$ = compare_and_free_strings( $1, $2, $3, FALSE );
238 | literal operator value_i
240 $$ = 0;
241 cond_free( $1 );
243 | value_i operator literal
245 $$ = 0;
246 cond_free( $3 );
248 | COND_LPAR expression COND_RPAR
250 $$ = $2;
254 operator:
255 /* common functions */
256 COND_EQ { $$ = COND_EQ; }
257 | COND_NE { $$ = COND_NE; }
258 | COND_LT { $$ = COND_LT; }
259 | COND_GT { $$ = COND_GT; }
260 | COND_LE { $$ = COND_LE; }
261 | COND_GE { $$ = COND_GE; }
262 | COND_SS { $$ = COND_SS; }
263 | COND_IEQ { $$ = COND_IEQ; }
264 | COND_INE { $$ = COND_INE; }
265 | COND_ILT { $$ = COND_ILT; }
266 | COND_IGT { $$ = COND_IGT; }
267 | COND_ILE { $$ = COND_ILE; }
268 | COND_IGE { $$ = COND_IGE; }
269 | COND_ISS { $$ = COND_ISS; }
270 | COND_LHS { $$ = COND_LHS; }
271 | COND_RHS { $$ = COND_RHS; }
272 | COND_ILHS { $$ = COND_ILHS; }
273 | COND_IRHS { $$ = COND_IRHS; }
276 value_s:
277 symbol_s
279 $$ = $1;
281 | literal
283 $$ = $1;
287 literal:
288 COND_LITER
290 COND_input* cond = (COND_input*) info;
291 $$ = COND_GetLiteral( cond, &$1 );
292 if( !$$ )
293 YYABORT;
297 value_i:
298 integer
300 $$ = $1;
302 | COND_DOLLARS identifier
304 COND_input* cond = (COND_input*) info;
305 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
307 MSI_GetComponentStateW(cond->package, $2, &install, &action );
308 $$ = action;
309 cond_free( $2 );
311 | COND_QUESTION identifier
313 COND_input* cond = (COND_input*) info;
314 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
316 MSI_GetComponentStateW(cond->package, $2, &install, &action );
317 $$ = install;
318 cond_free( $2 );
320 | COND_AMPER identifier
322 COND_input* cond = (COND_input*) info;
323 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
325 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
326 if (action == INSTALLSTATE_UNKNOWN)
327 $$ = MSICONDITION_FALSE;
328 else
329 $$ = action;
331 cond_free( $2 );
333 | COND_EXCLAM identifier
335 COND_input* cond = (COND_input*) info;
336 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
338 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
339 $$ = install;
340 cond_free( $2 );
344 symbol_s:
345 identifier
347 COND_input* cond = (COND_input*) info;
348 UINT len;
350 $$ = msi_dup_property( cond->package, $1 );
351 if ($$)
353 len = (lstrlenW($$) + 1) * sizeof (WCHAR);
354 $$ = cond_track_mem( cond, $$, len );
356 cond_free( $1 );
358 | COND_PERCENT identifier
360 COND_input* cond = (COND_input*) info;
361 UINT len = GetEnvironmentVariableW( $2, NULL, 0 );
362 $$ = NULL;
363 if (len++)
365 $$ = cond_alloc( cond, len*sizeof (WCHAR) );
366 if( !$$ )
367 YYABORT;
368 GetEnvironmentVariableW( $2, $$, len );
370 cond_free( $2 );
374 identifier:
375 COND_IDENT
377 COND_input* cond = (COND_input*) info;
378 $$ = COND_GetString( cond, &$1 );
379 if( !$$ )
380 YYABORT;
384 integer:
385 COND_NUMBER
387 COND_input* cond = (COND_input*) info;
388 LPWSTR szNum = COND_GetString( cond, &$1 );
389 if( !szNum )
390 YYABORT;
391 $$ = atoiW( szNum );
392 cond_free( szNum );
399 static int COND_IsAlpha( WCHAR x )
401 return( ( ( x >= 'A' ) && ( x <= 'Z' ) ) ||
402 ( ( x >= 'a' ) && ( x <= 'z' ) ) ||
403 ( ( x == '_' ) ) );
406 static int COND_IsNumber( WCHAR x )
408 return( (( x >= '0' ) && ( x <= '9' )) || (x =='-') || (x =='.') );
411 static WCHAR *strstriW( const WCHAR *str, const WCHAR *sub )
413 LPWSTR strlower, sublower, r;
414 strlower = CharLowerW( strdupW( str ) );
415 sublower = CharLowerW( strdupW( sub ) );
416 r = strstrW( strlower, sublower );
417 if (r)
418 r = (LPWSTR)str + (r - strlower);
419 msi_free( strlower );
420 msi_free( sublower );
421 return r;
424 static BOOL str_is_number( LPCWSTR str )
426 int i;
428 if (!*str)
429 return FALSE;
431 for (i = 0; i < lstrlenW( str ); i++)
432 if (!isdigitW(str[i]))
433 return FALSE;
435 return TRUE;
438 static INT compare_substring( LPCWSTR a, INT operator, LPCWSTR b )
440 int lhs, rhs;
442 /* substring operators return 0 if LHS is missing */
443 if (!a || !*a)
444 return 0;
446 /* substring operators return 1 if RHS is missing */
447 if (!b || !*b)
448 return 1;
450 /* if both strings contain only numbers, use integer comparison */
451 lhs = atoiW(a);
452 rhs = atoiW(b);
453 if (str_is_number(a) && str_is_number(b))
454 return compare_int( lhs, operator, rhs );
456 switch (operator)
458 case COND_SS:
459 return strstrW( a, b ) ? 1 : 0;
460 case COND_ISS:
461 return strstriW( a, b ) ? 1 : 0;
462 case COND_LHS:
463 return 0 == strncmpW( a, b, lstrlenW( b ) );
464 case COND_RHS:
465 return 0 == lstrcmpW( a + (lstrlenW( a ) - lstrlenW( b )), b );
466 case COND_ILHS:
467 return 0 == strncmpiW( a, b, lstrlenW( b ) );
468 case COND_IRHS:
469 return 0 == lstrcmpiW( a + (lstrlenW( a ) - lstrlenW( b )), b );
470 default:
471 ERR("invalid substring operator\n");
472 return 0;
474 return 0;
477 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b, BOOL convert )
479 if (operator >= COND_SS && operator <= COND_RHS)
480 return compare_substring( a, operator, b );
482 /* null and empty string are equivalent */
483 if (!a) a = szEmpty;
484 if (!b) b = szEmpty;
486 if (convert && str_is_number(a) && str_is_number(b))
487 return compare_int( atoiW(a), operator, atoiW(b) );
489 /* a or b may be NULL */
490 switch (operator)
492 case COND_LT:
493 return -1 == lstrcmpW( a, b );
494 case COND_GT:
495 return 1 == lstrcmpW( a, b );
496 case COND_EQ:
497 return 0 == lstrcmpW( a, b );
498 case COND_NE:
499 return 0 != lstrcmpW( a, b );
500 case COND_GE:
501 return -1 != lstrcmpW( a, b );
502 case COND_LE:
503 return 1 != lstrcmpW( a, b );
504 case COND_ILT:
505 return -1 == lstrcmpiW( a, b );
506 case COND_IGT:
507 return 1 == lstrcmpiW( a, b );
508 case COND_IEQ:
509 return 0 == lstrcmpiW( a, b );
510 case COND_INE:
511 return 0 != lstrcmpiW( a, b );
512 case COND_IGE:
513 return -1 != lstrcmpiW( a, b );
514 case COND_ILE:
515 return 1 != lstrcmpiW( a, b );
516 default:
517 ERR("invalid string operator\n");
518 return 0;
520 return 0;
524 static INT compare_int( INT a, INT operator, INT b )
526 switch (operator)
528 case COND_LT:
529 case COND_ILT:
530 return a < b;
531 case COND_GT:
532 case COND_IGT:
533 return a > b;
534 case COND_EQ:
535 case COND_IEQ:
536 return a == b;
537 case COND_NE:
538 case COND_INE:
539 return a != b;
540 case COND_GE:
541 case COND_IGE:
542 return a >= b;
543 case COND_LE:
544 case COND_ILE:
545 return a <= b;
546 case COND_SS:
547 case COND_ISS:
548 return ( a & b ) ? 1 : 0;
549 case COND_RHS:
550 return ( ( a & 0xffff ) == b ) ? 1 : 0;
551 case COND_LHS:
552 return ( ( (a>>16) & 0xffff ) == b ) ? 1 : 0;
553 default:
554 ERR("invalid integer operator\n");
555 return 0;
557 return 0;
561 static int COND_IsIdent( WCHAR x )
563 return( COND_IsAlpha( x ) || COND_IsNumber( x ) || ( x == '_' )
564 || ( x == '#' ) || (x == '.') );
567 static int COND_GetOperator( COND_input *cond )
569 static const struct {
570 const WCHAR str[4];
571 int id;
572 } table[] = {
573 { {'~','<','=',0}, COND_ILE },
574 { {'~','>','<',0}, COND_ISS },
575 { {'~','>','>',0}, COND_IRHS },
576 { {'~','<','>',0}, COND_INE },
577 { {'~','>','=',0}, COND_IGE },
578 { {'~','<','<',0}, COND_ILHS },
579 { {'~','=',0}, COND_IEQ },
580 { {'~','<',0}, COND_ILT },
581 { {'~','>',0}, COND_IGT },
582 { {'>','=',0}, COND_GE },
583 { {'>','<',0}, COND_SS },
584 { {'<','<',0}, COND_LHS },
585 { {'<','>',0}, COND_NE },
586 { {'<','=',0}, COND_LE },
587 { {'>','>',0}, COND_RHS },
588 { {'>',0}, COND_GT },
589 { {'<',0}, COND_LT },
590 { {0}, 0 }
592 LPCWSTR p = &cond->str[cond->n];
593 int i = 0, len;
595 while ( 1 )
597 len = lstrlenW( table[i].str );
598 if ( !len || 0 == strncmpW( table[i].str, p, len ) )
599 break;
600 i++;
602 cond->n += len;
603 return table[i].id;
606 static int COND_GetOne( struct cond_str *str, COND_input *cond )
608 int rc, len = 1;
609 WCHAR ch;
611 str->data = &cond->str[cond->n];
613 ch = str->data[0];
615 switch( ch )
617 case 0: return 0;
618 case '(': rc = COND_LPAR; break;
619 case ')': rc = COND_RPAR; break;
620 case '&': rc = COND_AMPER; break;
621 case '!': rc = COND_EXCLAM; break;
622 case '$': rc = COND_DOLLARS; break;
623 case '?': rc = COND_QUESTION; break;
624 case '%': rc = COND_PERCENT; break;
625 case ' ': rc = COND_SPACE; break;
626 case '=': rc = COND_EQ; break;
628 case '~':
629 case '<':
630 case '>':
631 rc = COND_GetOperator( cond );
632 if (!rc)
633 rc = COND_ERROR;
634 return rc;
635 default:
636 rc = 0;
639 if ( rc )
641 cond->n += len;
642 return rc;
645 if (ch == '"' )
647 LPCWSTR p = strchrW( str->data + 1, '"' );
648 if (!p)
649 return COND_ERROR;
650 len = p - str->data + 1;
651 rc = COND_LITER;
653 else if( COND_IsAlpha( ch ) )
655 static const WCHAR szNot[] = {'N','O','T',0};
656 static const WCHAR szAnd[] = {'A','N','D',0};
657 static const WCHAR szXor[] = {'X','O','R',0};
658 static const WCHAR szEqv[] = {'E','Q','V',0};
659 static const WCHAR szImp[] = {'I','M','P',0};
660 static const WCHAR szOr[] = {'O','R',0};
662 while( COND_IsIdent( str->data[len] ) )
663 len++;
664 rc = COND_IDENT;
666 if ( len == 3 )
668 if ( !strncmpiW( str->data, szNot, len ) )
669 rc = COND_NOT;
670 else if( !strncmpiW( str->data, szAnd, len ) )
671 rc = COND_AND;
672 else if( !strncmpiW( str->data, szXor, len ) )
673 rc = COND_XOR;
674 else if( !strncmpiW( str->data, szEqv, len ) )
675 rc = COND_EQV;
676 else if( !strncmpiW( str->data, szImp, len ) )
677 rc = COND_IMP;
679 else if( (len == 2) && !strncmpiW( str->data, szOr, len ) )
680 rc = COND_OR;
682 else if( COND_IsNumber( ch ) )
684 while( COND_IsNumber( str->data[len] ) )
685 len++;
686 rc = COND_NUMBER;
688 else
690 ERR("Got unknown character %c(%x)\n",ch,ch);
691 return COND_ERROR;
694 cond->n += len;
695 str->len = len;
697 return rc;
700 static int cond_lex( void *COND_lval, COND_input *cond )
702 int rc;
703 struct cond_str *str = COND_lval;
705 do {
706 rc = COND_GetOne( str, cond );
707 } while (rc == COND_SPACE);
709 return rc;
712 static LPWSTR COND_GetString( COND_input *cond, const struct cond_str *str )
714 LPWSTR ret;
716 ret = cond_alloc( cond, (str->len+1) * sizeof (WCHAR) );
717 if( ret )
719 memcpy( ret, str->data, str->len * sizeof(WCHAR));
720 ret[str->len]=0;
722 TRACE("Got identifier %s\n",debugstr_w(ret));
723 return ret;
726 static LPWSTR COND_GetLiteral( 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+1, (str->len-2) * sizeof(WCHAR) );
734 ret[str->len - 2]=0;
736 TRACE("Got literal %s\n",debugstr_w(ret));
737 return ret;
740 static void *cond_alloc( COND_input *cond, unsigned int sz )
742 struct list *mem;
744 mem = msi_alloc( sizeof (struct list) + sz );
745 if( !mem )
746 return NULL;
748 list_add_head( &(cond->mem), mem );
749 return mem + 1;
752 static void *cond_track_mem( COND_input *cond, void *ptr, unsigned int sz )
754 void *new_ptr;
756 if( !ptr )
757 return ptr;
759 new_ptr = cond_alloc( cond, sz );
760 if( !new_ptr )
762 msi_free( ptr );
763 return NULL;
766 memcpy( new_ptr, ptr, sz );
767 msi_free( ptr );
768 return new_ptr;
771 static void cond_free( void *ptr )
773 struct list *mem = (struct list *)ptr - 1;
775 if( ptr )
777 list_remove( mem );
778 msi_free( mem );
782 static int cond_error(const char *str)
784 TRACE("%s\n", str );
785 return 0;
788 MSICONDITION MSI_EvaluateConditionW( MSIPACKAGE *package, LPCWSTR szCondition )
790 COND_input cond;
791 MSICONDITION r;
792 struct list *mem, *safety;
794 TRACE("%s\n", debugstr_w( szCondition ) );
796 if ( szCondition == NULL )
797 return MSICONDITION_NONE;
799 cond.package = package;
800 cond.str = szCondition;
801 cond.n = 0;
802 cond.result = MSICONDITION_ERROR;
804 list_init( &cond.mem );
806 if ( !cond_parse( &cond ) )
807 r = cond.result;
808 else
809 r = MSICONDITION_ERROR;
811 LIST_FOR_EACH_SAFE( mem, safety, &cond.mem )
813 /* The tracked memory lives directly after the list struct */
814 void *ptr = mem + 1;
815 if ( r != MSICONDITION_ERROR )
816 WARN( "condition parser failed to free up some memory: %p\n", ptr );
817 cond_free( ptr );
820 TRACE("%i <- %s\n", r, debugstr_w(szCondition));
821 return r;
824 MSICONDITION WINAPI MsiEvaluateConditionW( MSIHANDLE hInstall, LPCWSTR szCondition )
826 MSIPACKAGE *package;
827 UINT ret;
829 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
830 if( !package )
832 HRESULT hr;
833 BSTR condition;
834 IWineMsiRemotePackage *remote_package;
836 remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
837 if (!remote_package)
838 return MSICONDITION_ERROR;
840 condition = SysAllocString( szCondition );
841 if (!condition)
843 IWineMsiRemotePackage_Release( remote_package );
844 return ERROR_OUTOFMEMORY;
847 hr = IWineMsiRemotePackage_EvaluateCondition( remote_package, condition );
849 SysFreeString( condition );
850 IWineMsiRemotePackage_Release( remote_package );
852 if (FAILED(hr))
854 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
855 return HRESULT_CODE(hr);
857 return ERROR_FUNCTION_FAILED;
860 return ERROR_SUCCESS;
863 ret = MSI_EvaluateConditionW( package, szCondition );
864 msiobj_release( &package->hdr );
865 return ret;
868 MSICONDITION WINAPI MsiEvaluateConditionA( MSIHANDLE hInstall, LPCSTR szCondition )
870 LPWSTR szwCond = NULL;
871 MSICONDITION r;
873 szwCond = strdupAtoW( szCondition );
874 if( szCondition && !szwCond )
875 return MSICONDITION_ERROR;
877 r = MsiEvaluateConditionW( hInstall, szwCond );
878 msi_free( szwCond );
879 return r;