po: Update simplified Chinese translation.
[wine/multimedia.git] / dlls / msi / cond.y
blob97bb002aefb37da2ed10ededb84a079ebbb2452e
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->db, $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 ) != 0;
460 case COND_ISS:
461 return strstriW( a, b ) != 0;
462 case COND_LHS:
464 int l = strlenW( a );
465 int r = strlenW( b );
466 if (r > l) return 0;
467 return !strncmpW( a, b, r );
469 case COND_RHS:
471 int l = strlenW( a );
472 int r = strlenW( b );
473 if (r > l) return 0;
474 return !strncmpW( a + (l - r), b, r );
476 case COND_ILHS:
478 int l = strlenW( a );
479 int r = strlenW( b );
480 if (r > l) return 0;
481 return !strncmpiW( a, b, r );
483 case COND_IRHS:
485 int l = strlenW( a );
486 int r = strlenW( b );
487 if (r > l) return 0;
488 return !strncmpiW( a + (l - r), b, r );
490 default:
491 ERR("invalid substring operator\n");
492 return 0;
494 return 0;
497 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b, BOOL convert )
499 if (operator >= COND_SS && operator <= COND_RHS)
500 return compare_substring( a, operator, b );
502 /* null and empty string are equivalent */
503 if (!a) a = szEmpty;
504 if (!b) b = szEmpty;
506 if (convert && str_is_number(a) && str_is_number(b))
507 return compare_int( atoiW(a), operator, atoiW(b) );
509 /* a or b may be NULL */
510 switch (operator)
512 case COND_LT:
513 return strcmpW( a, b ) < 0;
514 case COND_GT:
515 return strcmpW( a, b ) > 0;
516 case COND_EQ:
517 return strcmpW( a, b ) == 0;
518 case COND_NE:
519 return strcmpW( a, b ) != 0;
520 case COND_GE:
521 return strcmpW( a, b ) >= 0;
522 case COND_LE:
523 return strcmpW( a, b ) <= 0;
524 case COND_ILT:
525 return strcmpiW( a, b ) < 0;
526 case COND_IGT:
527 return strcmpiW( a, b ) > 0;
528 case COND_IEQ:
529 return strcmpiW( a, b ) == 0;
530 case COND_INE:
531 return strcmpiW( a, b ) != 0;
532 case COND_IGE:
533 return strcmpiW( a, b ) >= 0;
534 case COND_ILE:
535 return strcmpiW( a, b ) <= 0;
536 default:
537 ERR("invalid string operator\n");
538 return 0;
540 return 0;
544 static INT compare_int( INT a, INT operator, INT b )
546 switch (operator)
548 case COND_LT:
549 case COND_ILT:
550 return a < b;
551 case COND_GT:
552 case COND_IGT:
553 return a > b;
554 case COND_EQ:
555 case COND_IEQ:
556 return a == b;
557 case COND_NE:
558 case COND_INE:
559 return a != b;
560 case COND_GE:
561 case COND_IGE:
562 return a >= b;
563 case COND_LE:
564 case COND_ILE:
565 return a <= b;
566 case COND_SS:
567 case COND_ISS:
568 return ( a & b ) ? 1 : 0;
569 case COND_RHS:
570 return ( ( a & 0xffff ) == b ) ? 1 : 0;
571 case COND_LHS:
572 return ( ( (a>>16) & 0xffff ) == b ) ? 1 : 0;
573 default:
574 ERR("invalid integer operator\n");
575 return 0;
577 return 0;
581 static int COND_IsIdent( WCHAR x )
583 return( COND_IsAlpha( x ) || COND_IsNumber( x ) || ( x == '_' )
584 || ( x == '#' ) || (x == '.') );
587 static int COND_GetOperator( COND_input *cond )
589 static const struct {
590 const WCHAR str[4];
591 int id;
592 } table[] = {
593 { {'~','<','=',0}, COND_ILE },
594 { {'~','>','<',0}, COND_ISS },
595 { {'~','>','>',0}, COND_IRHS },
596 { {'~','<','>',0}, COND_INE },
597 { {'~','>','=',0}, COND_IGE },
598 { {'~','<','<',0}, COND_ILHS },
599 { {'~','=',0}, COND_IEQ },
600 { {'~','<',0}, COND_ILT },
601 { {'~','>',0}, COND_IGT },
602 { {'>','=',0}, COND_GE },
603 { {'>','<',0}, COND_SS },
604 { {'<','<',0}, COND_LHS },
605 { {'<','>',0}, COND_NE },
606 { {'<','=',0}, COND_LE },
607 { {'>','>',0}, COND_RHS },
608 { {'>',0}, COND_GT },
609 { {'<',0}, COND_LT },
610 { {0}, 0 }
612 LPCWSTR p = &cond->str[cond->n];
613 int i = 0, len;
615 while ( 1 )
617 len = lstrlenW( table[i].str );
618 if ( !len || 0 == strncmpW( table[i].str, p, len ) )
619 break;
620 i++;
622 cond->n += len;
623 return table[i].id;
626 static int COND_GetOne( struct cond_str *str, COND_input *cond )
628 int rc, len = 1;
629 WCHAR ch;
631 str->data = &cond->str[cond->n];
633 ch = str->data[0];
635 switch( ch )
637 case 0: return 0;
638 case '(': rc = COND_LPAR; break;
639 case ')': rc = COND_RPAR; break;
640 case '&': rc = COND_AMPER; break;
641 case '!': rc = COND_EXCLAM; break;
642 case '$': rc = COND_DOLLARS; break;
643 case '?': rc = COND_QUESTION; break;
644 case '%': rc = COND_PERCENT; break;
645 case ' ': rc = COND_SPACE; break;
646 case '=': rc = COND_EQ; break;
648 case '~':
649 case '<':
650 case '>':
651 rc = COND_GetOperator( cond );
652 if (!rc)
653 rc = COND_ERROR;
654 return rc;
655 default:
656 rc = 0;
659 if ( rc )
661 cond->n += len;
662 return rc;
665 if (ch == '"' )
667 LPCWSTR p = strchrW( str->data + 1, '"' );
668 if (!p) return COND_ERROR;
669 len = p - str->data + 1;
670 rc = COND_LITER;
672 else if( COND_IsAlpha( ch ) )
674 static const WCHAR szNot[] = {'N','O','T',0};
675 static const WCHAR szAnd[] = {'A','N','D',0};
676 static const WCHAR szXor[] = {'X','O','R',0};
677 static const WCHAR szEqv[] = {'E','Q','V',0};
678 static const WCHAR szImp[] = {'I','M','P',0};
679 static const WCHAR szOr[] = {'O','R',0};
681 while( COND_IsIdent( str->data[len] ) )
682 len++;
683 rc = COND_IDENT;
685 if ( len == 3 )
687 if ( !strncmpiW( str->data, szNot, len ) )
688 rc = COND_NOT;
689 else if( !strncmpiW( str->data, szAnd, len ) )
690 rc = COND_AND;
691 else if( !strncmpiW( str->data, szXor, len ) )
692 rc = COND_XOR;
693 else if( !strncmpiW( str->data, szEqv, len ) )
694 rc = COND_EQV;
695 else if( !strncmpiW( str->data, szImp, len ) )
696 rc = COND_IMP;
698 else if( (len == 2) && !strncmpiW( str->data, szOr, len ) )
699 rc = COND_OR;
701 else if( COND_IsNumber( ch ) )
703 while( COND_IsNumber( str->data[len] ) )
704 len++;
705 rc = COND_NUMBER;
707 else
709 ERR("Got unknown character %c(%x)\n",ch,ch);
710 return COND_ERROR;
713 cond->n += len;
714 str->len = len;
716 return rc;
719 static int cond_lex( void *COND_lval, COND_input *cond )
721 int rc;
722 struct cond_str *str = COND_lval;
724 do {
725 rc = COND_GetOne( str, cond );
726 } while (rc == COND_SPACE);
728 return rc;
731 static LPWSTR COND_GetString( COND_input *cond, const struct cond_str *str )
733 LPWSTR ret;
735 ret = cond_alloc( cond, (str->len+1) * sizeof (WCHAR) );
736 if( ret )
738 memcpy( ret, str->data, str->len * sizeof(WCHAR));
739 ret[str->len]=0;
741 TRACE("Got identifier %s\n",debugstr_w(ret));
742 return ret;
745 static LPWSTR COND_GetLiteral( COND_input *cond, const struct cond_str *str )
747 LPWSTR ret;
749 ret = cond_alloc( cond, (str->len-1) * sizeof (WCHAR) );
750 if( ret )
752 memcpy( ret, str->data+1, (str->len-2) * sizeof(WCHAR) );
753 ret[str->len - 2]=0;
755 TRACE("Got literal %s\n",debugstr_w(ret));
756 return ret;
759 static void *cond_alloc( COND_input *cond, unsigned int sz )
761 struct list *mem;
763 mem = msi_alloc( sizeof (struct list) + sz );
764 if( !mem )
765 return NULL;
767 list_add_head( &(cond->mem), mem );
768 return mem + 1;
771 static void *cond_track_mem( COND_input *cond, void *ptr, unsigned int sz )
773 void *new_ptr;
775 if( !ptr )
776 return ptr;
778 new_ptr = cond_alloc( cond, sz );
779 if( !new_ptr )
781 msi_free( ptr );
782 return NULL;
785 memcpy( new_ptr, ptr, sz );
786 msi_free( ptr );
787 return new_ptr;
790 static void cond_free( void *ptr )
792 struct list *mem = (struct list *)ptr - 1;
794 if( ptr )
796 list_remove( mem );
797 msi_free( mem );
801 static int cond_error(const char *str)
803 TRACE("%s\n", str );
804 return 0;
807 MSICONDITION MSI_EvaluateConditionW( MSIPACKAGE *package, LPCWSTR szCondition )
809 COND_input cond;
810 MSICONDITION r;
811 struct list *mem, *safety;
813 TRACE("%s\n", debugstr_w( szCondition ) );
815 if (szCondition == NULL) return MSICONDITION_NONE;
817 cond.package = package;
818 cond.str = szCondition;
819 cond.n = 0;
820 cond.result = MSICONDITION_ERROR;
822 list_init( &cond.mem );
824 if ( !cond_parse( &cond ) )
825 r = cond.result;
826 else
827 r = MSICONDITION_ERROR;
829 LIST_FOR_EACH_SAFE( mem, safety, &cond.mem )
831 /* The tracked memory lives directly after the list struct */
832 void *ptr = mem + 1;
833 if ( r != MSICONDITION_ERROR )
834 WARN( "condition parser failed to free up some memory: %p\n", ptr );
835 cond_free( ptr );
838 TRACE("%i <- %s\n", r, debugstr_w(szCondition));
839 return r;
842 MSICONDITION WINAPI MsiEvaluateConditionW( MSIHANDLE hInstall, LPCWSTR szCondition )
844 MSIPACKAGE *package;
845 UINT ret;
847 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
848 if( !package )
850 HRESULT hr;
851 BSTR condition;
852 IWineMsiRemotePackage *remote_package;
854 remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
855 if (!remote_package)
856 return MSICONDITION_ERROR;
858 condition = SysAllocString( szCondition );
859 if (!condition)
861 IWineMsiRemotePackage_Release( remote_package );
862 return ERROR_OUTOFMEMORY;
865 hr = IWineMsiRemotePackage_EvaluateCondition( remote_package, condition );
867 SysFreeString( condition );
868 IWineMsiRemotePackage_Release( remote_package );
870 if (FAILED(hr))
872 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
873 return HRESULT_CODE(hr);
875 return ERROR_FUNCTION_FAILED;
878 return ERROR_SUCCESS;
881 ret = MSI_EvaluateConditionW( package, szCondition );
882 msiobj_release( &package->hdr );
883 return ret;
886 MSICONDITION WINAPI MsiEvaluateConditionA( MSIHANDLE hInstall, LPCSTR szCondition )
888 LPWSTR szwCond = NULL;
889 MSICONDITION r;
891 szwCond = strdupAtoW( szCondition );
892 if( szCondition && !szwCond )
893 return MSICONDITION_ERROR;
895 r = MsiEvaluateConditionW( hInstall, szwCond );
896 msi_free( szwCond );
897 return r;