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
40 #include "msiserver.h"
41 #include "wine/debug.h"
42 #include "wine/unicode.h"
44 #define YYLEX_PARAM info
45 #define YYPARSE_PARAM info
47 static int cond_error
(const char *str
);
49 WINE_DEFAULT_DEBUG_CHANNEL
(msi
);
51 typedef
struct tag_yyinput
64 static LPWSTR COND_GetString
( const struct cond_str
*str
);
65 static LPWSTR COND_GetLiteral
( const struct cond_str
*str
);
66 static int cond_lex
( void *COND_lval
, COND_input
*info
);
67 static const WCHAR szEmpty
[] = { 0 };
69 static INT compare_int
( INT a
, INT operator
, INT b
);
70 static INT compare_string
( LPCWSTR a
, INT operator
, LPCWSTR b
);
72 static INT compare_and_free_strings
( LPWSTR a
, INT op
, LPWSTR b
)
76 r
= compare_string
( a
, op
, b
);
82 static BOOL num_from_prop
( LPCWSTR p
, INT
*val
)
84 INT ret
= 0, sign
= 1;
97 if
( *p
< '0' ||
*p
> '9' )
99 ret
= ret
*10 + (*p
- '0');
117 %token COND_SPACE COND_EOF
118 %token COND_OR COND_AND COND_NOT COND_XOR COND_IMP COND_EQV
119 %token COND_LT COND_GT COND_EQ COND_NE COND_GE COND_LE
120 %token COND_ILT COND_IGT COND_IEQ COND_INE COND_IGE COND_ILE
121 %token COND_LPAR COND_RPAR COND_TILDA COND_SS COND_ISS
122 %token COND_ILHS COND_IRHS COND_LHS COND_RHS
123 %token COND_PERCENT COND_DOLLARS COND_QUESTION COND_AMPER COND_EXCLAM
124 %token
<str
> COND_IDENT
<str
> COND_NUMBER
<str
> COND_LITER
126 %nonassoc COND_ERROR COND_EOF
128 %type
<value
> expression boolean_term boolean_factor
129 %type
<value
> value_i integer operator
130 %type
<string> identifier symbol_s value_s literal
137 COND_input
* cond
= (COND_input
*) info
;
142 COND_input
* cond
= (COND_input
*) info
;
143 cond
->result
= MSICONDITION_NONE
;
152 | expression COND_OR boolean_term
156 | expression COND_IMP boolean_term
160 | expression COND_XOR boolean_term
162 $$
= ( $1 ||
$3 ) && !( $1 && $3 );
164 | expression COND_EQV boolean_term
166 $$
= ( $1 && $3 ) ||
( !$1 && !$3 );
175 | boolean_term COND_AND boolean_factor
182 COND_NOT boolean_factor
192 $$
= ($1 && $1[0]) ?
1 : 0;
195 | value_i operator value_i
197 $$
= compare_int
( $1, $2, $3 );
199 | symbol_s operator value_i
202 if
(num_from_prop
( $1, &num
))
203 $$
= compare_int
( num
, $2, $3 );
205 $$
= ($2 == COND_NE ||
$2 == COND_INE
);
208 | value_i operator symbol_s
211 if
(num_from_prop
( $3, &num
))
212 $$
= compare_int
( $1, $2, num
);
214 $$
= ($2 == COND_NE ||
$2 == COND_INE
);
217 | symbol_s operator symbol_s
219 $$
= compare_and_free_strings
( $1, $2, $3 );
221 | symbol_s operator literal
223 $$
= compare_and_free_strings
( $1, $2, $3 );
225 | literal operator symbol_s
227 $$
= compare_and_free_strings
( $1, $2, $3 );
229 | literal operator literal
231 $$
= compare_and_free_strings
( $1, $2, $3 );
233 | literal operator value_i
238 | value_i operator literal
243 | COND_LPAR expression COND_RPAR
250 /* common functions */
251 COND_EQ
{ $$
= COND_EQ
; }
252 | COND_NE
{ $$
= COND_NE
; }
253 | COND_LT
{ $$
= COND_LT
; }
254 | COND_GT
{ $$
= COND_GT
; }
255 | COND_LE
{ $$
= COND_LE
; }
256 | COND_GE
{ $$
= COND_GE
; }
257 | COND_SS
{ $$
= COND_SS
; }
258 | COND_IEQ
{ $$
= COND_IEQ
; }
259 | COND_INE
{ $$
= COND_INE
; }
260 | COND_ILT
{ $$
= COND_ILT
; }
261 | COND_IGT
{ $$
= COND_IGT
; }
262 | COND_ILE
{ $$
= COND_ILE
; }
263 | COND_IGE
{ $$
= COND_IGE
; }
264 | COND_ISS
{ $$
= COND_ISS
; }
265 | COND_LHS
{ $$
= COND_LHS
; }
266 | COND_RHS
{ $$
= COND_RHS
; }
267 | COND_ILHS
{ $$
= COND_ILHS
; }
268 | COND_IRHS
{ $$
= COND_IRHS
; }
285 $$
= COND_GetLiteral
(&$1);
296 | COND_DOLLARS identifier
298 COND_input
* cond
= (COND_input
*) info
;
299 INSTALLSTATE install
= INSTALLSTATE_UNKNOWN
, action
= INSTALLSTATE_UNKNOWN
;
301 MSI_GetComponentStateW
(cond
->package
, $2, &install
, &action
);
305 | COND_QUESTION identifier
307 COND_input
* cond
= (COND_input
*) info
;
308 INSTALLSTATE install
= INSTALLSTATE_UNKNOWN
, action
= INSTALLSTATE_UNKNOWN
;
310 MSI_GetComponentStateW
(cond
->package
, $2, &install
, &action
);
314 | COND_AMPER identifier
316 COND_input
* cond
= (COND_input
*) info
;
317 INSTALLSTATE install
= INSTALLSTATE_UNKNOWN
, action
= INSTALLSTATE_UNKNOWN
;
319 MSI_GetFeatureStateW
(cond
->package
, $2, &install
, &action
);
320 if
(action
== INSTALLSTATE_UNKNOWN
)
321 $$
= MSICONDITION_FALSE
;
327 | COND_EXCLAM identifier
329 COND_input
* cond
= (COND_input
*) info
;
330 INSTALLSTATE install
= INSTALLSTATE_UNKNOWN
, action
= INSTALLSTATE_UNKNOWN
;
332 MSI_GetFeatureStateW
(cond
->package
, $2, &install
, &action
);
341 COND_input
* cond
= (COND_input
*) info
;
343 $$
= msi_dup_property
( cond
->package
, $1 );
346 | COND_PERCENT identifier
348 UINT len
= GetEnvironmentVariableW
( $2, NULL
, 0 );
352 $$
= msi_alloc
( len
*sizeof
(WCHAR
) );
353 GetEnvironmentVariableW
( $2, $$
, len
);
362 $$
= COND_GetString
(&$1);
371 LPWSTR szNum
= COND_GetString
(&$1);
382 static int COND_IsAlpha
( WCHAR x
)
384 return
( ( ( x
>= 'A' ) && ( x
<= 'Z' ) ) ||
385 ( ( x
>= 'a' ) && ( x
<= 'z' ) ) ||
389 static int COND_IsNumber
( WCHAR x
)
391 return
( (( x
>= '0' ) && ( x
<= '9' )) ||
(x
=='-') ||
(x
=='.') );
394 static WCHAR
*strstriW
( const WCHAR
*str
, const WCHAR
*sub
)
396 LPWSTR strlower
, sublower
, r
;
397 strlower
= CharLowerW
( strdupW
( str
) );
398 sublower
= CharLowerW
( strdupW
( sub
) );
399 r
= strstrW
( strlower
, sublower
);
401 r
= (LPWSTR
)str
+ (r
- strlower
);
402 msi_free
( strlower
);
403 msi_free
( sublower
);
407 static BOOL str_is_number
( LPCWSTR str
)
411 for
(i
= 0; i
< lstrlenW
( str
); i
++)
412 if
(!isdigitW
(str
[i
]))
418 static INT compare_substring
( LPCWSTR a
, INT operator
, LPCWSTR b
)
422 /* substring operators return 0 if LHS is missing */
426 /* substring operators return 1 if RHS is missing */
430 /* if both strings contain only numbers, use integer comparison */
433 if
(str_is_number
(a
) && str_is_number
(b
))
434 return compare_int
( lhs
, operator
, rhs
);
439 return strstrW
( a
, b
) ?
1 : 0;
441 return strstriW
( a
, b
) ?
1 : 0;
443 return
0 == strncmpW
( a
, b
, lstrlenW
( b
) );
445 return
0 == lstrcmpW
( a
+ (lstrlenW
( a
) - lstrlenW
( b
)), b
);
447 return
0 == strncmpiW
( a
, b
, lstrlenW
( b
) );
449 return
0 == lstrcmpiW
( a
+ (lstrlenW
( a
) - lstrlenW
( b
)), b
);
451 ERR
("invalid substring operator\n");
457 static INT compare_string
( LPCWSTR a
, INT operator
, LPCWSTR b
)
459 if
(operator
>= COND_SS
&& operator
<= COND_RHS
)
460 return compare_substring
( a
, operator
, b
);
462 /* null and empty string are equivalent */
466 /* a or b may be NULL */
470 return
-1 == lstrcmpW
( a
, b
);
472 return
1 == lstrcmpW
( a
, b
);
474 return
0 == lstrcmpW
( a
, b
);
476 return
0 != lstrcmpW
( a
, b
);
478 return
-1 != lstrcmpW
( a
, b
);
480 return
1 != lstrcmpW
( a
, b
);
482 return
-1 == lstrcmpiW
( a
, b
);
484 return
1 == lstrcmpiW
( a
, b
);
486 return
0 == lstrcmpiW
( a
, b
);
488 return
0 != lstrcmpiW
( a
, b
);
490 return
-1 != lstrcmpiW
( a
, b
);
492 return
1 != lstrcmpiW
( a
, b
);
494 ERR
("invalid string operator\n");
501 static INT compare_int
( INT a
, INT operator
, INT b
)
525 return
( a
& b
) ?
1 : 0;
527 return
( ( a
& 0xffff ) == b
) ?
1 : 0;
529 return
( ( (a
>>16) & 0xffff ) == b
) ?
1 : 0;
531 ERR
("invalid integer operator\n");
538 static int COND_IsIdent
( WCHAR x
)
540 return
( COND_IsAlpha
( x
) || COND_IsNumber
( x
) ||
( x
== '_' )
541 ||
( x
== '#' ) ||
(x
== '.') );
544 static int COND_GetOperator
( COND_input
*cond
)
546 static const struct {
550 { {'~','=',0}, COND_IEQ
},
551 { {'~','<','=',0}, COND_ILE
},
552 { {'~','>','<',0}, COND_ISS
},
553 { {'~','>','>',0}, COND_IRHS
},
554 { {'~','<','>',0}, COND_INE
},
555 { {'~','<',0}, COND_ILT
},
556 { {'~','>','=',0}, COND_IGE
},
557 { {'~','<','<',0}, COND_ILHS
},
558 { {'~','>',0}, COND_IGT
},
559 { {'>','=',0}, COND_GE
},
560 { {'>','<',0}, COND_SS
},
561 { {'<','<',0}, COND_LHS
},
562 { {'<','>',0}, COND_NE
},
563 { {'<','=',0}, COND_LE
},
564 { {'>','>',0}, COND_RHS
},
565 { {'>',0}, COND_GT
},
566 { {'<',0}, COND_LT
},
569 LPCWSTR p
= &cond
->str
[cond
->n
];
574 len
= lstrlenW
( table
[i
].str
);
575 if
( !len ||
0 == strncmpW
( table
[i
].str
, p
, len
) )
583 static int COND_GetOne
( struct cond_str
*str
, COND_input
*cond
)
588 str
->data
= &cond
->str
[cond
->n
];
595 case
'(': rc
= COND_LPAR
; break
;
596 case
')': rc
= COND_RPAR
; break
;
597 case
'&': rc
= COND_AMPER
; break
;
598 case
'!': rc
= COND_EXCLAM
; break
;
599 case
'$': rc
= COND_DOLLARS
; break
;
600 case
'?': rc
= COND_QUESTION
; break
;
601 case
'%': rc
= COND_PERCENT
; break
;
602 case
' ': rc
= COND_SPACE
; break
;
603 case
'=': rc
= COND_EQ
; break
;
608 rc
= COND_GetOperator
( cond
);
624 LPCWSTR p
= strchrW
( str
->data
+ 1, '"' );
627 len
= p
- str
->data
+ 1;
630 else if
( COND_IsAlpha
( ch
) )
632 static const WCHAR szNot
[] = {'N','O','T',0};
633 static const WCHAR szAnd
[] = {'A','N','D',0};
634 static const WCHAR szXor
[] = {'X','O','R',0};
635 static const WCHAR szEqv
[] = {'E','Q','V',0};
636 static const WCHAR szImp
[] = {'I','M','P',0};
637 static const WCHAR szOr
[] = {'O','R',0};
639 while
( COND_IsIdent
( str
->data
[len
] ) )
645 if
( !strncmpiW
( str
->data
, szNot
, len
) )
647 else if
( !strncmpiW
( str
->data
, szAnd
, len
) )
649 else if
( !strncmpiW
( str
->data
, szXor
, len
) )
651 else if
( !strncmpiW
( str
->data
, szEqv
, len
) )
653 else if
( !strncmpiW
( str
->data
, szImp
, len
) )
656 else if
( (len
== 2) && !strncmpiW
( str
->data
, szOr
, len
) )
659 else if
( COND_IsNumber
( ch
) )
661 while
( COND_IsNumber
( str
->data
[len
] ) )
667 ERR
("Got unknown character %c(%x)\n",ch
,ch
);
677 static int cond_lex
( void *COND_lval
, COND_input
*cond
)
680 struct cond_str
*str
= COND_lval
;
683 rc
= COND_GetOne
( str
, cond
);
684 } while
(rc
== COND_SPACE
);
689 static LPWSTR COND_GetString
( const struct cond_str
*str
)
693 ret
= msi_alloc
( (str
->len
+1) * sizeof
(WCHAR
) );
696 memcpy
( ret
, str
->data
, str
->len
* sizeof
(WCHAR
));
699 TRACE
("Got identifier %s\n",debugstr_w
(ret
));
703 static LPWSTR COND_GetLiteral
( const struct cond_str
*str
)
707 ret
= msi_alloc
( (str
->len
-1) * sizeof
(WCHAR
) );
710 memcpy
( ret
, str
->data
+1, (str
->len
-2) * sizeof
(WCHAR
) );
713 TRACE
("Got literal %s\n",debugstr_w
(ret
));
717 static int cond_error
(const char *str
)
723 MSICONDITION MSI_EvaluateConditionW
( MSIPACKAGE
*package
, LPCWSTR szCondition
)
728 TRACE
("%s\n", debugstr_w
( szCondition
) );
730 if
( szCondition
== NULL
)
731 return MSICONDITION_NONE
;
733 cond.package
= package
;
734 cond.str
= szCondition
;
736 cond.result
= MSICONDITION_ERROR
;
738 if
( !cond_parse
( &cond
) )
741 r
= MSICONDITION_ERROR
;
743 TRACE
("%i <- %s\n", r
, debugstr_w
(szCondition
));
747 MSICONDITION WINAPI MsiEvaluateConditionW
( MSIHANDLE hInstall
, LPCWSTR szCondition
)
752 package
= msihandle2msiinfo
( hInstall
, MSIHANDLETYPE_PACKAGE
);
757 IWineMsiRemotePackage
*remote_package
;
759 remote_package
= (IWineMsiRemotePackage
*)msi_get_remote
( hInstall
);
761 return MSICONDITION_ERROR
;
763 condition
= SysAllocString
( szCondition
);
766 IWineMsiRemotePackage_Release
( remote_package
);
767 return ERROR_OUTOFMEMORY
;
770 hr
= IWineMsiRemotePackage_EvaluateCondition
( remote_package
, condition
);
772 SysFreeString
( condition
);
773 IWineMsiRemotePackage_Release
( remote_package
);
777 if
(HRESULT_FACILITY
(hr
) == FACILITY_WIN32
)
778 return HRESULT_CODE
(hr
);
780 return ERROR_FUNCTION_FAILED
;
783 return ERROR_SUCCESS
;
786 ret
= MSI_EvaluateConditionW
( package
, szCondition
);
787 msiobj_release
( &package
->hdr
);
791 MSICONDITION WINAPI MsiEvaluateConditionA
( MSIHANDLE hInstall
, LPCSTR szCondition
)
793 LPWSTR szwCond
= NULL
;
796 szwCond
= strdupAtoW
( szCondition
);
797 if
( szCondition
&& !szwCond
)
798 return MSICONDITION_ERROR
;
800 r
= MsiEvaluateConditionW
( hInstall
, szwCond
);