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
32 #include "wine/debug.h"
33 #include "wine/unicode.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
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
)
71 r
= compare_string
( a
, op
, b
);
77 static BOOL num_from_prop
( LPCWSTR p
, INT
*val
)
79 INT ret
= 0, sign
= 1;
92 if
( *p
< '0' ||
*p
> '9' )
94 ret
= ret
*10 + (*p
- '0');
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
132 COND_input
* cond
= (COND_input
*) info
;
137 COND_input
* cond
= (COND_input
*) info
;
138 cond
->result
= MSICONDITION_NONE
;
147 | expression COND_OR boolean_term
151 | expression COND_IMP boolean_term
155 | expression COND_XOR boolean_term
157 $$
= ( $1 ||
$3 ) && !( $1 && $3 );
159 | expression COND_EQV boolean_term
161 $$
= ( $1 && $3 ) ||
( !$1 && !$3 );
170 | boolean_term COND_AND boolean_factor
177 COND_NOT boolean_factor
187 $$
= ($1 && $1[0]) ?
1 : 0;
190 | value_i operator value_i
192 $$
= compare_int
( $1, $2, $3 );
194 | symbol_s operator value_i
197 if
(num_from_prop
( $1, &num
))
198 $$
= compare_int
( num
, $2, $3 );
200 $$
= ($2 == COND_NE ||
$2 == COND_INE
);
203 | value_i operator symbol_s
206 if
(num_from_prop
( $3, &num
))
207 $$
= compare_int
( $1, $2, num
);
209 $$
= ($2 == COND_NE ||
$2 == COND_INE
);
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
233 | value_i operator literal
238 | COND_LPAR expression COND_RPAR
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
; }
280 $$
= COND_GetLiteral
(&$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
);
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
);
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
);
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
);
332 COND_input
* cond
= (COND_input
*) info
;
334 $$
= msi_dup_property
( cond
->package
, $1 );
337 | COND_PERCENT identifier
339 UINT len
= GetEnvironmentVariableW
( $2, NULL
, 0 );
343 $$
= msi_alloc
( len
*sizeof
(WCHAR
) );
344 GetEnvironmentVariableW
( $2, $$
, len
);
353 $$
= COND_GetString
(&$1);
362 LPWSTR szNum
= COND_GetString
(&$1);
373 static int COND_IsAlpha
( WCHAR x
)
375 return
( ( ( x
>= 'A' ) && ( x
<= 'Z' ) ) ||
376 ( ( x
>= 'a' ) && ( x
<= 'z' ) ) ||
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
);
392 r
= (LPWSTR
)str
+ (r
- strlower
);
393 msi_free
( strlower
);
394 msi_free
( sublower
);
398 static BOOL str_is_number
( LPCWSTR str
)
402 for
(i
= 0; i
< lstrlenW
( str
); i
++)
403 if
(!isdigitW
(str
[i
]))
409 static INT compare_substring
( LPCWSTR a
, INT operator
, LPCWSTR b
)
413 /* substring operators return 0 if LHS is missing */
417 /* substring operators return 1 if RHS is missing */
421 /* if both strings contain only numbers, use integer comparison */
424 if
(str_is_number
(a
) && str_is_number
(b
))
425 return compare_int
( lhs
, operator
, rhs
);
430 return strstrW
( a
, b
) ?
1 : 0;
432 return strstriW
( a
, b
) ?
1 : 0;
434 return
0 == strncmpW
( a
, b
, lstrlenW
( b
) );
436 return
0 == lstrcmpW
( a
+ (lstrlenW
( a
) - lstrlenW
( b
)), b
);
438 return
0 == strncmpiW
( a
, b
, lstrlenW
( b
) );
440 return
0 == lstrcmpiW
( a
+ (lstrlenW
( a
) - lstrlenW
( b
)), b
);
442 ERR
("invalid substring operator\n");
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 */
457 /* a or b may be NULL */
461 return
-1 == lstrcmpW
( a
, b
);
463 return
1 == lstrcmpW
( a
, b
);
465 return
0 == lstrcmpW
( a
, b
);
467 return
0 != lstrcmpW
( a
, b
);
469 return
-1 != lstrcmpW
( a
, b
);
471 return
1 != lstrcmpW
( a
, b
);
473 return
-1 == lstrcmpiW
( a
, b
);
475 return
1 == lstrcmpiW
( a
, b
);
477 return
0 == lstrcmpiW
( a
, b
);
479 return
0 != lstrcmpiW
( a
, b
);
481 return
-1 != lstrcmpiW
( a
, b
);
483 return
1 != lstrcmpiW
( a
, b
);
485 ERR
("invalid string operator\n");
492 static INT compare_int
( INT a
, INT operator
, INT b
)
516 return
( a
& b
) ?
1 : 0;
518 return
( ( a
& 0xffff ) == b
) ?
1 : 0;
520 return
( ( (a
>>16) & 0xffff ) == b
) ?
1 : 0;
522 ERR
("invalid integer operator\n");
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 {
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
},
560 LPCWSTR p
= &cond
->str
[cond
->n
];
565 len
= lstrlenW
( table
[i
].str
);
566 if
( !len ||
0 == strncmpW
( table
[i
].str
, p
, len
) )
574 static int COND_GetOne
( struct cond_str
*str
, COND_input
*cond
)
579 str
->data
= &cond
->str
[cond
->n
];
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
;
600 rc
= COND_GetOperator
( cond
);
616 LPCWSTR p
= strchrW
( str
->data
+ 1, '"' );
619 len
= p
- str
->data
+ 1;
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
] ) )
637 if
( !strncmpiW
( str
->data
, szNot
, len
) )
639 else if
( !strncmpiW
( str
->data
, szAnd
, len
) )
641 else if
( !strncmpiW
( str
->data
, szXor
, len
) )
643 else if
( !strncmpiW
( str
->data
, szEqv
, len
) )
645 else if
( !strncmpiW
( str
->data
, szImp
, len
) )
648 else if
( (len
== 2) && !strncmpiW
( str
->data
, szOr
, len
) )
651 else if
( COND_IsNumber
( ch
) )
653 while
( COND_IsNumber
( str
->data
[len
] ) )
659 ERR
("Got unknown character %c(%x)\n",ch
,ch
);
669 static int cond_lex
( void *COND_lval
, COND_input
*cond
)
672 struct cond_str
*str
= COND_lval
;
675 rc
= COND_GetOne
( str
, cond
);
676 } while
(rc
== COND_SPACE
);
681 static LPWSTR COND_GetString
( struct cond_str
*str
)
685 ret
= msi_alloc
( (str
->len
+1) * sizeof
(WCHAR
) );
688 memcpy
( ret
, str
->data
, str
->len
* sizeof
(WCHAR
));
691 TRACE
("Got identifier %s\n",debugstr_w
(ret
));
695 static LPWSTR COND_GetLiteral
( struct cond_str
*str
)
699 ret
= msi_alloc
( (str
->len
-1) * sizeof
(WCHAR
) );
702 memcpy
( ret
, str
->data
+1, (str
->len
-2) * sizeof
(WCHAR
) );
705 TRACE
("Got literal %s\n",debugstr_w
(ret
));
709 static int cond_error
(const char *str
)
715 MSICONDITION MSI_EvaluateConditionW
( MSIPACKAGE
*package
, LPCWSTR szCondition
)
720 TRACE
("%s\n", debugstr_w
( szCondition
) );
722 if
( szCondition
== NULL
)
723 return MSICONDITION_NONE
;
725 cond.package
= package
;
726 cond.str
= szCondition
;
728 cond.result
= MSICONDITION_ERROR
;
730 if
( !cond_parse
( &cond
) )
733 r
= MSICONDITION_ERROR
;
735 TRACE
("%i <- %s\n", r
, debugstr_w
(szCondition
));
739 MSICONDITION WINAPI MsiEvaluateConditionW
( MSIHANDLE hInstall
, LPCWSTR szCondition
)
744 package
= msihandle2msiinfo
( hInstall
, MSIHANDLETYPE_PACKAGE
);
746 return MSICONDITION_ERROR
;
747 ret
= MSI_EvaluateConditionW
( package
, szCondition
);
748 msiobj_release
( &package
->hdr
);
752 MSICONDITION WINAPI MsiEvaluateConditionA
( MSIHANDLE hInstall
, LPCSTR szCondition
)
754 LPWSTR szwCond
= NULL
;
757 szwCond
= strdupAtoW
( szCondition
);
758 if
( szCondition
&& !szwCond
)
759 return MSICONDITION_ERROR
;
761 r
= MsiEvaluateConditionW
( hInstall
, szwCond
);