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
39 #include "wine/debug.h"
40 #include "wine/exception.h"
41 #include "wine/list.h"
43 WINE_DEFAULT_DEBUG_CHANNEL
(msi
);
45 typedef
struct tag_yyinput
71 static LPWSTR COND_GetString
( COND_input
*info
, const struct cond_str
*str
);
72 static LPWSTR COND_GetLiteral
( COND_input
*info
, const struct cond_str
*str
);
73 static int cond_lex
( void *COND_lval
, COND_input
*info
);
74 static int cond_error
( COND_input
*info
, const char *str
);
76 static void *cond_alloc
( COND_input
*cond
, unsigned int sz
);
77 static void *cond_track_mem
( COND_input
*cond
, void *ptr
, unsigned int sz
);
78 static void cond_free
( void *ptr
);
80 static INT compare_int
( INT a
, INT operator
, INT b
);
81 static INT compare_string
( LPCWSTR a
, INT operator
, LPCWSTR b
, BOOL convert
);
83 static BOOL num_from_prop
( LPCWSTR p
, INT
*val
)
85 INT ret
= 0, sign
= 1;
98 if
( *p
< '0' ||
*p
> '9' )
100 ret
= ret
*10 + (*p
- '0');
107 static void value_free
( struct value val
)
109 if
(val.type
!= VALUE_INTEGER
)
110 cond_free
( val.u.
string );
115 %lex
-param
{ COND_input
*info
}
116 %parse
-param
{ COND_input
*info
}
117 %define api.prefix
{cond_
}
130 %token COND_OR COND_AND COND_NOT COND_XOR COND_IMP COND_EQV
131 %token COND_LT COND_GT COND_EQ COND_NE COND_GE COND_LE
132 %token COND_ILT COND_IGT COND_IEQ COND_INE COND_IGE COND_ILE
133 %token COND_LPAR COND_RPAR COND_TILDA COND_SS COND_ISS
134 %token COND_ILHS COND_IRHS COND_LHS COND_RHS
135 %token COND_PERCENT COND_DOLLARS COND_QUESTION COND_AMPER COND_EXCLAM
136 %token
<str
> COND_IDENT
<str
> COND_NUMBER
<str
> COND_LITER
140 %type
<bool> expression boolean_term boolean_factor
142 %type
<identifier
> identifier
143 %type
<operator
> operator
150 COND_input
* cond
= (COND_input
*) info
;
155 COND_input
* cond
= (COND_input
*) info
;
156 cond
->result
= MSICONDITION_NONE
;
165 | expression COND_OR boolean_term
169 | expression COND_IMP boolean_term
173 | expression COND_XOR boolean_term
175 $$
= ( $1 ||
$3 ) && !( $1 && $3 );
177 | expression COND_EQV boolean_term
179 $$
= ( $1 && $3 ) ||
( !$1 && !$3 );
188 | boolean_term COND_AND boolean_factor
195 COND_NOT boolean_factor
201 if
($1.type
== VALUE_INTEGER
)
202 $$
= $1.u.integer ?
1 : 0;
204 $$
= $1.u.
string && $1.u.
string[0];
207 | value operator value
209 if
($1.type
== VALUE_INTEGER
&& $3.type
== VALUE_INTEGER
)
211 $$
= compare_int
($1.u.integer
, $2, $3.u.integer
);
213 else if
($1.type
!= VALUE_INTEGER
&& $3.type
!= VALUE_INTEGER
)
215 $$
= compare_string
($1.u.
string, $2, $3.u.
string,
216 $1.type
== VALUE_SYMBOL ||
$3.type
== VALUE_SYMBOL
);
218 else if
($1.type
== VALUE_LITERAL ||
$3.type
== VALUE_LITERAL
)
220 $$
= ($2 == COND_NE ||
$2 == COND_INE
);
222 else if
($1.type
== VALUE_SYMBOL
) /* symbol operator integer */
225 if
(num_from_prop
( $1.u.
string, &num
))
226 $$
= compare_int
( num
, $2, $3.u.integer
);
228 $$
= ($2 == COND_NE ||
$2 == COND_INE
);
230 else
/* integer operator symbol */
233 if
(num_from_prop
( $3.u.
string, &num
))
234 $$
= compare_int
( $1.u.integer
, $2, num
);
236 $$
= ($2 == COND_NE ||
$2 == COND_INE
);
242 | COND_LPAR expression COND_RPAR
249 /* common functions */
250 COND_EQ
{ $$
= COND_EQ
; }
251 | COND_NE
{ $$
= COND_NE
; }
252 | COND_LT
{ $$
= COND_LT
; }
253 | COND_GT
{ $$
= COND_GT
; }
254 | COND_LE
{ $$
= COND_LE
; }
255 | COND_GE
{ $$
= COND_GE
; }
256 | COND_SS
{ $$
= COND_SS
; }
257 | COND_IEQ
{ $$
= COND_IEQ
; }
258 | COND_INE
{ $$
= COND_INE
; }
259 | COND_ILT
{ $$
= COND_ILT
; }
260 | COND_IGT
{ $$
= COND_IGT
; }
261 | COND_ILE
{ $$
= COND_ILE
; }
262 | COND_IGE
{ $$
= COND_IGE
; }
263 | COND_ISS
{ $$
= COND_ISS
; }
264 | COND_LHS
{ $$
= COND_LHS
; }
265 | COND_RHS
{ $$
= COND_RHS
; }
266 | COND_ILHS
{ $$
= COND_ILHS
; }
267 | COND_IRHS
{ $$
= COND_IRHS
; }
273 COND_input
* cond
= (COND_input
*) info
;
276 $$.type
= VALUE_SYMBOL
;
277 $$.u.
string = msi_dup_property
( cond
->package
->db
, $1 );
280 len
= (lstrlenW
($$.u.
string) + 1) * sizeof
(WCHAR
);
281 $$.u.
string = cond_track_mem
( cond
, $$.u.
string, len
);
285 | COND_PERCENT identifier
287 COND_input
* cond
= (COND_input
*) info
;
288 UINT len
= GetEnvironmentVariableW
( $2, NULL
, 0 );
289 $$.type
= VALUE_SYMBOL
;
293 $$.u.
string = cond_alloc
( cond
, len
*sizeof
(WCHAR
) );
296 GetEnvironmentVariableW
( $2, $$.u.
string, len
);
302 COND_input
* cond
= (COND_input
*) info
;
303 $$.type
= VALUE_LITERAL
;
304 $$.u.
string = COND_GetLiteral
( cond
, &$1 );
310 COND_input
* cond
= (COND_input
*) info
;
311 LPWSTR szNum
= COND_GetString
( cond
, &$1 );
314 $$.type
= VALUE_INTEGER
;
315 $$.u.integer
= wcstol
( szNum
, NULL
, 10 );
318 | COND_DOLLARS identifier
320 COND_input
* cond
= (COND_input
*) info
;
321 INSTALLSTATE install
= INSTALLSTATE_UNKNOWN
, action
= INSTALLSTATE_UNKNOWN
;
323 if
(MSI_GetComponentStateW
(cond
->package
, $2, &install
, &action
) != ERROR_SUCCESS
)
325 $$.type
= VALUE_LITERAL
;
330 $$.type
= VALUE_INTEGER
;
331 $$.u.integer
= action
;
335 | COND_QUESTION identifier
337 COND_input
* cond
= (COND_input
*) info
;
338 INSTALLSTATE install
= INSTALLSTATE_UNKNOWN
, action
= INSTALLSTATE_UNKNOWN
;
340 if
(MSI_GetComponentStateW
(cond
->package
, $2, &install
, &action
) != ERROR_SUCCESS
)
342 $$.type
= VALUE_LITERAL
;
347 $$.type
= VALUE_INTEGER
;
348 $$.u.integer
= install
;
352 | COND_AMPER identifier
354 COND_input
* cond
= (COND_input
*) info
;
355 INSTALLSTATE install
, action
;
357 if
(MSI_GetFeatureStateW
(cond
->package
, $2, &install
, &action
) != ERROR_SUCCESS
)
359 $$.type
= VALUE_LITERAL
;
364 $$.type
= VALUE_INTEGER
;
365 $$.u.integer
= action
;
369 | COND_EXCLAM identifier
371 COND_input
* cond
= (COND_input
*) info
;
372 INSTALLSTATE install
= INSTALLSTATE_UNKNOWN
, action
= INSTALLSTATE_UNKNOWN
;
374 if
(MSI_GetFeatureStateW
(cond
->package
, $2, &install
, &action
) != ERROR_SUCCESS
)
376 $$.type
= VALUE_LITERAL
;
381 $$.type
= VALUE_INTEGER
;
382 $$.u.integer
= install
;
391 COND_input
* cond
= (COND_input
*) info
;
392 $$
= COND_GetString
( cond
, &$1 );
401 static int COND_IsAlpha
( WCHAR x
)
403 return
( ( ( x
>= 'A' ) && ( x
<= 'Z' ) ) ||
404 ( ( x
>= 'a' ) && ( x
<= 'z' ) ) ||
408 static int COND_IsNumber
( WCHAR x
)
410 return
( (( x
>= '0' ) && ( x
<= '9' )) ||
(x
=='-') ||
(x
=='.') );
413 static WCHAR
*strstriW
( const WCHAR
*str
, const WCHAR
*sub
)
415 LPWSTR strlower
, sublower
, r
;
416 strlower
= CharLowerW
( wcsdup
( str
) );
417 sublower
= CharLowerW
( wcsdup
( sub
) );
418 r
= wcsstr
( strlower
, sublower
);
420 r
= (LPWSTR
)str
+ (r
- strlower
);
426 static BOOL str_is_number
( LPCWSTR str
)
433 for
(i
= 0; i
< lstrlenW
( str
); i
++)
434 if
(!iswdigit
(str
[i
]))
440 static INT compare_substring
( LPCWSTR a
, INT operator
, LPCWSTR b
)
444 /* substring operators return 0 if LHS is missing */
448 /* substring operators return 1 if RHS is missing */
452 /* if both strings contain only numbers, use integer comparison */
453 lhs
= wcstol
(a
, NULL
, 10);
454 rhs
= wcstol
(b
, NULL
, 10);
455 if
(str_is_number
(a
) && str_is_number
(b
))
456 return compare_int
( lhs
, operator
, rhs
);
461 return wcsstr
( a
, b
) != 0;
463 return strstriW
( a
, b
) != 0;
466 int l
= lstrlenW
( a
);
467 int r
= lstrlenW
( b
);
469 return
!wcsncmp
( a
, b
, r
);
473 int l
= lstrlenW
( a
);
474 int r
= lstrlenW
( b
);
476 return
!wcsncmp
( a
+ (l
- r
), b
, r
);
480 int l
= lstrlenW
( a
);
481 int r
= lstrlenW
( b
);
483 return
!wcsnicmp
( a
, b
, r
);
487 int l
= lstrlenW
( a
);
488 int r
= lstrlenW
( b
);
490 return
!wcsnicmp
( a
+ (l
- r
), b
, r
);
493 ERR
("invalid substring operator\n");
499 static INT compare_string
( LPCWSTR a
, INT operator
, LPCWSTR b
, BOOL convert
)
501 if
(operator
>= COND_SS
&& operator
<= COND_RHS
)
502 return compare_substring
( a
, operator
, b
);
504 /* null and empty string are equivalent */
508 if
(convert
&& str_is_number
(a
) && str_is_number
(b
))
509 return compare_int
( wcstol
(a
, NULL
, 10), operator
, wcstol
(b
, NULL
, 10) );
511 /* a or b may be NULL */
515 return wcscmp
( a
, b
) < 0;
517 return wcscmp
( a
, b
) > 0;
519 return wcscmp
( a
, b
) == 0;
521 return wcscmp
( a
, b
) != 0;
523 return wcscmp
( a
, b
) >= 0;
525 return wcscmp
( a
, b
) <= 0;
527 return wcsicmp
( a
, b
) < 0;
529 return wcsicmp
( a
, b
) > 0;
531 return wcsicmp
( a
, b
) == 0;
533 return wcsicmp
( a
, b
) != 0;
535 return wcsicmp
( a
, b
) >= 0;
537 return wcsicmp
( a
, b
) <= 0;
539 ERR
("invalid string operator\n");
546 static INT compare_int
( INT a
, INT operator
, INT b
)
570 return
( a
& b
) ?
1 : 0;
572 return
( ( a
& 0xffff ) == b
) ?
1 : 0;
574 return
( ( (a
>>16) & 0xffff ) == b
) ?
1 : 0;
576 ERR
("invalid integer operator\n");
583 static int COND_IsIdent
( WCHAR x
)
585 return
( COND_IsAlpha
( x
) || COND_IsNumber
( x
) ||
( x
== '_' )
586 ||
( x
== '#' ) ||
(x
== '.') );
589 static int COND_GetOperator
( COND_input
*cond
)
591 static const struct {
595 { L
"~<=", COND_ILE
},
596 { L
"~><", COND_ISS
},
597 { L
"~>>", COND_IRHS
},
598 { L
"~<>", COND_INE
},
599 { L
"~>=", COND_IGE
},
600 { L
"~<<", COND_ILHS
},
614 LPCWSTR p
= &cond
->str
[cond
->n
];
619 len
= lstrlenW
( table
[i
].str
);
620 if
( !len ||
0 == wcsncmp
( table
[i
].str
, p
, len
) )
628 static int COND_GetOne
( struct cond_str
*str
, COND_input
*cond
)
633 str
->data
= &cond
->str
[cond
->n
];
640 case
'(': rc
= COND_LPAR
; break
;
641 case
')': rc
= COND_RPAR
; break
;
642 case
'&': rc
= COND_AMPER
; break
;
643 case
'!': rc
= COND_EXCLAM
; break
;
644 case
'$': rc
= COND_DOLLARS
; break
;
645 case
'?': rc
= COND_QUESTION
; break
;
646 case
'%': rc
= COND_PERCENT
; break
;
647 case
' ': rc
= COND_SPACE
; break
;
648 case
'=': rc
= COND_EQ
; break
;
653 rc
= COND_GetOperator
( cond
);
669 LPCWSTR p
= wcschr
( str
->data
+ 1, '"' );
670 if
(!p
) return COND_ERROR
;
671 len
= p
- str
->data
+ 1;
674 else if
( COND_IsAlpha
( ch
) )
676 while
( COND_IsIdent
( str
->data
[len
] ) )
682 if
( !wcsnicmp
( str
->data
, L
"NOT", len
) )
684 else if
( !wcsnicmp
( str
->data
, L
"AND", len
) )
686 else if
( !wcsnicmp
( str
->data
, L
"XOR", len
) )
688 else if
( !wcsnicmp
( str
->data
, L
"EQV", len
) )
690 else if
( !wcsnicmp
( str
->data
, L
"IMP", len
) )
693 else if
( (len
== 2) && !wcsnicmp
( str
->data
, L
"OR", len
) )
696 else if
( COND_IsNumber
( ch
) )
698 while
( COND_IsNumber
( str
->data
[len
] ) )
704 ERR
("Got unknown character %c(%x)\n",ch
,ch
);
714 static int cond_lex
( void *COND_lval
, COND_input
*cond
)
717 struct cond_str
*str
= COND_lval
;
720 rc
= COND_GetOne
( str
, cond
);
721 } while
(rc
== COND_SPACE
);
726 static LPWSTR COND_GetString
( COND_input
*cond
, const struct cond_str
*str
)
730 ret
= cond_alloc
( cond
, (str
->len
+1) * sizeof
(WCHAR
) );
733 memcpy
( ret
, str
->data
, str
->len
* sizeof
(WCHAR
));
736 TRACE
("Got identifier %s\n",debugstr_w
(ret
));
740 static LPWSTR COND_GetLiteral
( COND_input
*cond
, const struct cond_str
*str
)
744 ret
= cond_alloc
( cond
, (str
->len
-1) * sizeof
(WCHAR
) );
747 memcpy
( ret
, str
->data
+1, (str
->len
-2) * sizeof
(WCHAR
) );
750 TRACE
("Got literal %s\n",debugstr_w
(ret
));
754 static void *cond_alloc
( COND_input
*cond
, unsigned int sz
)
758 mem
= malloc
( sizeof
(struct list
) + sz
);
762 list_add_head
( &(cond
->mem
), mem
);
766 static void *cond_track_mem
( COND_input
*cond
, void *ptr
, unsigned int sz
)
773 new_ptr
= cond_alloc
( cond
, sz
);
780 memcpy
( new_ptr
, ptr
, sz
);
785 static void cond_free
( void *ptr
)
787 struct list
*mem
= (struct list
*)ptr
- 1;
796 static int cond_error
( COND_input
*info
, const char *str
)
802 MSICONDITION MSI_EvaluateConditionW
( MSIPACKAGE
*package
, LPCWSTR szCondition
)
806 struct list
*mem
, *safety
;
808 TRACE
("%s\n", debugstr_w
( szCondition
) );
810 if
(szCondition
== NULL
) return MSICONDITION_NONE
;
812 cond.package
= package
;
813 cond.str
= szCondition
;
815 cond.result
= MSICONDITION_ERROR
;
817 list_init
( &cond.mem
);
819 if
( !cond_parse
( &cond
) )
822 r
= MSICONDITION_ERROR
;
824 LIST_FOR_EACH_SAFE
( mem
, safety
, &cond.mem
)
826 /* The tracked memory lives directly after the list struct */
828 if
( r
!= MSICONDITION_ERROR
)
829 WARN
( "condition parser failed to free up some memory: %p\n", ptr
);
833 TRACE
("%i <- %s\n", r
, debugstr_w
(szCondition
));
837 MSICONDITION WINAPI MsiEvaluateConditionW
( MSIHANDLE hInstall
, LPCWSTR szCondition
)
842 package
= msihandle2msiinfo
( hInstall
, MSIHANDLETYPE_PACKAGE
);
847 if
(!(remote
= msi_get_remote
(hInstall
)))
848 return MSICONDITION_ERROR
;
851 return MSICONDITION_NONE
;
855 ret
= remote_EvaluateCondition
(remote
, szCondition
);
859 ret
= GetExceptionCode
();
866 ret
= MSI_EvaluateConditionW
( package
, szCondition
);
867 msiobj_release
( &package
->hdr
);
871 MSICONDITION WINAPI MsiEvaluateConditionA
( MSIHANDLE hInstall
, LPCSTR szCondition
)
873 LPWSTR szwCond
= NULL
;
876 szwCond
= strdupAtoW
( szCondition
);
877 if
( szCondition
&& !szwCond
)
878 return MSICONDITION_ERROR
;
880 r
= MsiEvaluateConditionW
( hInstall
, szwCond
);