user32: Simplify some maximized MDI child checks.
[wine/dibdrv.git] / dlls / msi / cond.y
blob9cfb00114faa7703295d0f4fd6cc29b1b583a0d7
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 #include "config.h"
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 "wine/debug.h"
33 #include "wine/unicode.h"
35 #include "msi.h"
36 #include "msiquery.h"
37 #include "msipriv.h"
39 #define YYLEX_PARAM info
40 #define YYPARSE_PARAM info
42 static int cond_error(const char *str);
44 WINE_DEFAULT_DEBUG_CHANNEL(msi);
46 typedef struct tag_yyinput
48 MSIPACKAGE *package;
49 LPCWSTR str;
50 INT n;
51 MSICONDITION result;
52 } COND_input;
54 struct cond_str {
55 LPCWSTR data;
56 INT len;
59 static LPWSTR COND_GetString( struct cond_str *str );
60 static LPWSTR COND_GetLiteral( struct cond_str *str );
61 static int cond_lex( void *COND_lval, COND_input *info);
62 static const WCHAR szEmpty[] = { 0 };
64 static INT compare_int( INT a, INT operator, INT b );
65 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b );
67 static INT compare_and_free_strings( LPWSTR a, INT op, LPWSTR b )
69 INT r;
71 r = compare_string( a, op, b );
72 msi_free( a );
73 msi_free( b );
74 return r;
77 static BOOL num_from_prop( LPCWSTR p, INT *val )
79 INT ret = 0, sign = 1;
81 if (!p)
82 return FALSE;
83 if (*p == '-')
85 sign = -1;
86 p++;
88 if (!*p)
89 return FALSE;
90 while (*p)
92 if( *p < '0' || *p > '9' )
93 return FALSE;
94 ret = ret*10 + (*p - '0');
95 p++;
97 *val = ret*sign;
98 return TRUE;
103 %pure-parser
105 %union
107 struct cond_str str;
108 LPWSTR string;
109 INT value;
112 %token COND_SPACE COND_EOF
113 %token COND_OR COND_AND COND_NOT COND_XOR COND_IMP COND_EQV
114 %token COND_LT COND_GT COND_EQ COND_NE COND_GE COND_LE
115 %token COND_ILT COND_IGT COND_IEQ COND_INE COND_IGE COND_ILE
116 %token COND_LPAR COND_RPAR COND_TILDA COND_SS COND_ISS
117 %token COND_ILHS COND_IRHS COND_LHS COND_RHS
118 %token COND_PERCENT COND_DOLLARS COND_QUESTION COND_AMPER COND_EXCLAM
119 %token <str> COND_IDENT <str> COND_NUMBER <str> COND_LITER
121 %nonassoc COND_ERROR COND_EOF
123 %type <value> expression boolean_term boolean_factor
124 %type <value> value_i integer operator
125 %type <string> identifier symbol_s value_s literal
129 condition:
130 expression
132 COND_input* cond = (COND_input*) info;
133 cond->result = $1;
135 | /* empty */
137 COND_input* cond = (COND_input*) info;
138 cond->result = MSICONDITION_NONE;
142 expression:
143 boolean_term
145 $$ = $1;
147 | expression COND_OR boolean_term
149 $$ = $1 || $3;
151 | expression COND_IMP boolean_term
153 $$ = !$1 || $3;
155 | expression COND_XOR boolean_term
157 $$ = ( $1 || $3 ) && !( $1 && $3 );
159 | expression COND_EQV boolean_term
161 $$ = ( $1 && $3 ) || ( !$1 && !$3 );
165 boolean_term:
166 boolean_factor
168 $$ = $1;
170 | boolean_term COND_AND boolean_factor
172 $$ = $1 && $3;
176 boolean_factor:
177 COND_NOT boolean_factor
179 $$ = $2 ? 0 : 1;
181 | value_i
183 $$ = $1 ? 1 : 0;
185 | value_s
187 $$ = ($1 && $1[0]) ? 1 : 0;
188 msi_free($1);
190 | value_i operator value_i
192 $$ = compare_int( $1, $2, $3 );
194 | symbol_s operator value_i
196 int num;
197 if (num_from_prop( $1, &num ))
198 $$ = compare_int( num, $2, $3 );
199 else
200 $$ = ($2 == COND_NE || $2 == COND_INE );
201 msi_free($1);
203 | value_i operator symbol_s
205 int num;
206 if (num_from_prop( $3, &num ))
207 $$ = compare_int( $1, $2, num );
208 else
209 $$ = ($2 == COND_NE || $2 == COND_INE );
210 msi_free($3);
212 | symbol_s operator symbol_s
214 $$ = compare_and_free_strings( $1, $2, $3 );
216 | symbol_s operator literal
218 $$ = compare_and_free_strings( $1, $2, $3 );
220 | literal operator symbol_s
222 $$ = compare_and_free_strings( $1, $2, $3 );
224 | literal operator literal
226 $$ = compare_and_free_strings( $1, $2, $3 );
228 | literal operator value_i
230 $$ = 0;
231 msi_free($1);
233 | value_i operator literal
235 $$ = 0;
236 msi_free($3);
238 | COND_LPAR expression COND_RPAR
240 $$ = $2;
244 operator:
245 /* common functions */
246 COND_EQ { $$ = COND_EQ; }
247 | COND_NE { $$ = COND_NE; }
248 | COND_LT { $$ = COND_LT; }
249 | COND_GT { $$ = COND_GT; }
250 | COND_LE { $$ = COND_LE; }
251 | COND_GE { $$ = COND_GE; }
252 | COND_SS { $$ = COND_SS; }
253 | COND_IEQ { $$ = COND_IEQ; }
254 | COND_INE { $$ = COND_INE; }
255 | COND_ILT { $$ = COND_ILT; }
256 | COND_IGT { $$ = COND_IGT; }
257 | COND_ILE { $$ = COND_ILE; }
258 | COND_IGE { $$ = COND_IGE; }
259 | COND_ISS { $$ = COND_ISS; }
260 | COND_LHS { $$ = COND_LHS; }
261 | COND_RHS { $$ = COND_RHS; }
262 | COND_ILHS { $$ = COND_ILHS; }
263 | COND_IRHS { $$ = COND_IRHS; }
266 value_s:
267 symbol_s
269 $$ = $1;
271 | literal
273 $$ = $1;
277 literal:
278 COND_LITER
280 $$ = COND_GetLiteral(&$1);
281 if( !$$ )
282 YYABORT;
286 value_i:
287 integer
289 $$ = $1;
291 | COND_DOLLARS identifier
293 COND_input* cond = (COND_input*) info;
294 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
296 MSI_GetComponentStateW(cond->package, $2, &install, &action );
297 $$ = action;
298 msi_free( $2 );
300 | COND_QUESTION identifier
302 COND_input* cond = (COND_input*) info;
303 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
305 MSI_GetComponentStateW(cond->package, $2, &install, &action );
306 $$ = install;
307 msi_free( $2 );
309 | COND_AMPER identifier
311 COND_input* cond = (COND_input*) info;
312 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
314 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
315 $$ = action;
316 msi_free( $2 );
318 | COND_EXCLAM identifier
320 COND_input* cond = (COND_input*) info;
321 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
323 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
324 $$ = install;
325 msi_free( $2 );
329 symbol_s:
330 identifier
332 COND_input* cond = (COND_input*) info;
334 $$ = msi_dup_property( cond->package, $1 );
335 msi_free( $1 );
337 | COND_PERCENT identifier
339 UINT len = GetEnvironmentVariableW( $2, NULL, 0 );
340 $$ = NULL;
341 if (len++)
343 $$ = msi_alloc( len*sizeof (WCHAR) );
344 GetEnvironmentVariableW( $2, $$, len );
346 msi_free( $2 );
350 identifier:
351 COND_IDENT
353 $$ = COND_GetString(&$1);
354 if( !$$ )
355 YYABORT;
359 integer:
360 COND_NUMBER
362 LPWSTR szNum = COND_GetString(&$1);
363 if( !szNum )
364 YYABORT;
365 $$ = atoiW( szNum );
366 msi_free( szNum );
373 static int COND_IsAlpha( WCHAR x )
375 return( ( ( x >= 'A' ) && ( x <= 'Z' ) ) ||
376 ( ( x >= 'a' ) && ( x <= 'z' ) ) ||
377 ( ( x == '_' ) ) );
380 static int COND_IsNumber( WCHAR x )
382 return( (( x >= '0' ) && ( x <= '9' )) || (x =='-') || (x =='.') );
385 static WCHAR *strstriW( const WCHAR *str, const WCHAR *sub )
387 LPWSTR strlower, sublower, r;
388 strlower = CharLowerW( strdupW( str ) );
389 sublower = CharLowerW( strdupW( sub ) );
390 r = strstrW( strlower, sublower );
391 if (r)
392 r = (LPWSTR)str + (r - strlower);
393 msi_free( strlower );
394 msi_free( sublower );
395 return r;
398 static BOOL str_is_number( LPCWSTR str )
400 int i;
402 for (i = 0; i < lstrlenW( str ); i++)
403 if (!isdigitW(str[i]))
404 return FALSE;
406 return TRUE;
409 static INT compare_substring( LPCWSTR a, INT operator, LPCWSTR b )
411 int lhs, rhs;
413 /* substring operators return 0 if LHS is missing */
414 if (!a || !*a)
415 return 0;
417 /* substring operators return 1 if RHS is missing */
418 if (!b || !*b)
419 return 1;
421 /* if both strings contain only numbers, use integer comparison */
422 lhs = atoiW(a);
423 rhs = atoiW(b);
424 if (str_is_number(a) && str_is_number(b))
425 return compare_int( lhs, operator, rhs );
427 switch (operator)
429 case COND_SS:
430 return strstrW( a, b ) ? 1 : 0;
431 case COND_ISS:
432 return strstriW( a, b ) ? 1 : 0;
433 case COND_LHS:
434 return 0 == strncmpW( a, b, lstrlenW( b ) );
435 case COND_RHS:
436 return 0 == lstrcmpW( a + (lstrlenW( a ) - lstrlenW( b )), b );
437 case COND_ILHS:
438 return 0 == strncmpiW( a, b, lstrlenW( b ) );
439 case COND_IRHS:
440 return 0 == lstrcmpiW( a + (lstrlenW( a ) - lstrlenW( b )), b );
441 default:
442 ERR("invalid substring operator\n");
443 return 0;
445 return 0;
448 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b )
450 if (operator >= COND_SS && operator <= COND_RHS)
451 return compare_substring( a, operator, b );
453 /* null and empty string are equivalent */
454 if (!a) a = szEmpty;
455 if (!b) b = szEmpty;
457 /* a or b may be NULL */
458 switch (operator)
460 case COND_LT:
461 return -1 == lstrcmpW( a, b );
462 case COND_GT:
463 return 1 == lstrcmpW( a, b );
464 case COND_EQ:
465 return 0 == lstrcmpW( a, b );
466 case COND_NE:
467 return 0 != lstrcmpW( a, b );
468 case COND_GE:
469 return -1 != lstrcmpW( a, b );
470 case COND_LE:
471 return 1 != lstrcmpW( a, b );
472 case COND_ILT:
473 return -1 == lstrcmpiW( a, b );
474 case COND_IGT:
475 return 1 == lstrcmpiW( a, b );
476 case COND_IEQ:
477 return 0 == lstrcmpiW( a, b );
478 case COND_INE:
479 return 0 != lstrcmpiW( a, b );
480 case COND_IGE:
481 return -1 != lstrcmpiW( a, b );
482 case COND_ILE:
483 return 1 != lstrcmpiW( a, b );
484 default:
485 ERR("invalid string operator\n");
486 return 0;
488 return 0;
492 static INT compare_int( INT a, INT operator, INT b )
494 switch (operator)
496 case COND_LT:
497 case COND_ILT:
498 return a < b;
499 case COND_GT:
500 case COND_IGT:
501 return a > b;
502 case COND_EQ:
503 case COND_IEQ:
504 return a == b;
505 case COND_NE:
506 case COND_INE:
507 return a != b;
508 case COND_GE:
509 case COND_IGE:
510 return a >= b;
511 case COND_LE:
512 case COND_ILE:
513 return a <= b;
514 case COND_SS:
515 case COND_ISS:
516 return ( a & b ) ? 1 : 0;
517 case COND_RHS:
518 return ( ( a & 0xffff ) == b ) ? 1 : 0;
519 case COND_LHS:
520 return ( ( (a>>16) & 0xffff ) == b ) ? 1 : 0;
521 default:
522 ERR("invalid integer operator\n");
523 return 0;
525 return 0;
529 static int COND_IsIdent( WCHAR x )
531 return( COND_IsAlpha( x ) || COND_IsNumber( x ) || ( x == '_' )
532 || ( x == '#' ) || (x == '.') );
535 static int COND_GetOperator( COND_input *cond )
537 static const struct {
538 const WCHAR str[4];
539 int id;
540 } table[] = {
541 { {'~','=',0}, COND_IEQ },
542 { {'~','<','=',0}, COND_ILE },
543 { {'~','>','<',0}, COND_ISS },
544 { {'~','>','>',0}, COND_IRHS },
545 { {'~','<','>',0}, COND_INE },
546 { {'~','<',0}, COND_ILT },
547 { {'~','>','=',0}, COND_IGE },
548 { {'~','<','<',0}, COND_ILHS },
549 { {'~','>',0}, COND_IGT },
550 { {'>','=',0}, COND_GE },
551 { {'>','<',0}, COND_SS },
552 { {'<','<',0}, COND_LHS },
553 { {'<','>',0}, COND_NE },
554 { {'<','=',0}, COND_LE },
555 { {'>','>',0}, COND_RHS },
556 { {'>',0}, COND_GT },
557 { {'<',0}, COND_LT },
558 { {0}, 0 }
560 LPCWSTR p = &cond->str[cond->n];
561 int i = 0, len;
563 while ( 1 )
565 len = lstrlenW( table[i].str );
566 if ( !len || 0 == strncmpW( table[i].str, p, len ) )
567 break;
568 i++;
570 cond->n += len;
571 return table[i].id;
574 static int COND_GetOne( struct cond_str *str, COND_input *cond )
576 int rc, len = 1;
577 WCHAR ch;
579 str->data = &cond->str[cond->n];
581 ch = str->data[0];
583 switch( ch )
585 case 0: return 0;
586 case '(': rc = COND_LPAR; break;
587 case ')': rc = COND_RPAR; break;
588 case '&': rc = COND_AMPER; break;
589 case '!': rc = COND_EXCLAM; break;
590 case '$': rc = COND_DOLLARS; break;
591 case '?': rc = COND_QUESTION; break;
592 case '%': rc = COND_PERCENT; break;
593 case ' ': rc = COND_SPACE; break;
594 case '=': rc = COND_EQ; break;
595 break;
597 case '~':
598 case '<':
599 case '>':
600 rc = COND_GetOperator( cond );
601 if (!rc)
602 rc = COND_ERROR;
603 return rc;
604 default:
605 rc = 0;
608 if ( rc )
610 cond->n += len;
611 return rc;
614 if (ch == '"' )
616 LPCWSTR p = strchrW( str->data + 1, '"' );
617 if (!p)
618 return COND_ERROR;
619 len = p - str->data + 1;
620 rc = COND_LITER;
622 else if( COND_IsAlpha( ch ) )
624 static const WCHAR szNot[] = {'N','O','T',0};
625 static const WCHAR szAnd[] = {'A','N','D',0};
626 static const WCHAR szXor[] = {'X','O','R',0};
627 static const WCHAR szEqv[] = {'E','Q','V',0};
628 static const WCHAR szImp[] = {'I','M','P',0};
629 static const WCHAR szOr[] = {'O','R',0};
631 while( COND_IsIdent( str->data[len] ) )
632 len++;
633 rc = COND_IDENT;
635 if ( len == 3 )
637 if ( !strncmpiW( str->data, szNot, len ) )
638 rc = COND_NOT;
639 else if( !strncmpiW( str->data, szAnd, len ) )
640 rc = COND_AND;
641 else if( !strncmpiW( str->data, szXor, len ) )
642 rc = COND_XOR;
643 else if( !strncmpiW( str->data, szEqv, len ) )
644 rc = COND_EQV;
645 else if( !strncmpiW( str->data, szImp, len ) )
646 rc = COND_IMP;
648 else if( (len == 2) && !strncmpiW( str->data, szOr, len ) )
649 rc = COND_OR;
651 else if( COND_IsNumber( ch ) )
653 while( COND_IsNumber( str->data[len] ) )
654 len++;
655 rc = COND_NUMBER;
657 else
659 ERR("Got unknown character %c(%x)\n",ch,ch);
660 return COND_ERROR;
663 cond->n += len;
664 str->len = len;
666 return rc;
669 static int cond_lex( void *COND_lval, COND_input *cond )
671 int rc;
672 struct cond_str *str = COND_lval;
674 do {
675 rc = COND_GetOne( str, cond );
676 } while (rc == COND_SPACE);
678 return rc;
681 static LPWSTR COND_GetString( struct cond_str *str )
683 LPWSTR ret;
685 ret = msi_alloc( (str->len+1) * sizeof (WCHAR) );
686 if( ret )
688 memcpy( ret, str->data, str->len * sizeof(WCHAR));
689 ret[str->len]=0;
691 TRACE("Got identifier %s\n",debugstr_w(ret));
692 return ret;
695 static LPWSTR COND_GetLiteral( struct cond_str *str )
697 LPWSTR ret;
699 ret = msi_alloc( (str->len-1) * sizeof (WCHAR) );
700 if( ret )
702 memcpy( ret, str->data+1, (str->len-2) * sizeof(WCHAR) );
703 ret[str->len - 2]=0;
705 TRACE("Got literal %s\n",debugstr_w(ret));
706 return ret;
709 static int cond_error(const char *str)
711 TRACE("%s\n", str );
712 return 0;
715 MSICONDITION MSI_EvaluateConditionW( MSIPACKAGE *package, LPCWSTR szCondition )
717 COND_input cond;
718 MSICONDITION r;
720 TRACE("%s\n", debugstr_w( szCondition ) );
722 if ( szCondition == NULL )
723 return MSICONDITION_NONE;
725 cond.package = package;
726 cond.str = szCondition;
727 cond.n = 0;
728 cond.result = MSICONDITION_ERROR;
730 if ( !cond_parse( &cond ) )
731 r = cond.result;
732 else
733 r = MSICONDITION_ERROR;
735 TRACE("%i <- %s\n", r, debugstr_w(szCondition));
736 return r;
739 MSICONDITION WINAPI MsiEvaluateConditionW( MSIHANDLE hInstall, LPCWSTR szCondition )
741 MSIPACKAGE *package;
742 UINT ret;
744 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
745 if( !package)
746 return MSICONDITION_ERROR;
747 ret = MSI_EvaluateConditionW( package, szCondition );
748 msiobj_release( &package->hdr );
749 return ret;
752 MSICONDITION WINAPI MsiEvaluateConditionA( MSIHANDLE hInstall, LPCSTR szCondition )
754 LPWSTR szwCond = NULL;
755 MSICONDITION r;
757 szwCond = strdupAtoW( szCondition );
758 if( szCondition && !szwCond )
759 return MSICONDITION_ERROR;
761 r = MsiEvaluateConditionW( hInstall, szwCond );
762 msi_free( szwCond );
763 return r;