opengl32: Avoid generating a wrapper for internal functions when we can call the...
[wine] / dlls / msi / cond.y
1 %{
2
3 /*
4  * Implementation of the Microsoft Installer (msi.dll)
5  *
6  * Copyright 2003 Mike McCormack for CodeWeavers
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "config.h"
24
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
34
35 #include "msi.h"
36 #include "msiquery.h"
37 #include "msipriv.h"
38
39 #define YYLEX_PARAM info
40 #define YYPARSE_PARAM info
41
42 static int cond_error(const char *str);
43
44 WINE_DEFAULT_DEBUG_CHANNEL(msi);
45
46 typedef struct tag_yyinput
47 {
48     MSIPACKAGE *package;
49     LPCWSTR str;
50     INT    n;
51     MSICONDITION result;
52 } COND_input;
53
54 struct cond_str {
55     LPCWSTR data;
56     INT len;
57 };
58
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 };
63
64 static INT compare_int( INT a, INT operator, INT b );
65 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b );
66
67 static INT compare_and_free_strings( LPWSTR a, INT op, LPWSTR b )
68 {
69     INT r;
70
71     r = compare_string( a, op, b );
72     msi_free( a );
73     msi_free( b );
74     return r;
75 }
76
77 static BOOL num_from_prop( LPCWSTR p, INT *val )
78 {
79     INT ret = 0, sign = 1;
80
81     if (!p)
82         return FALSE;
83     if (*p == '-')
84     {
85         sign = -1;
86         p++;
87     }
88     if (!*p)
89         return FALSE;
90     while (*p)
91     {
92         if( *p < '0' || *p > '9' )
93             return FALSE;
94         ret = ret*10 + (*p - '0');
95         p++;
96     }
97     *val = ret*sign;
98     return TRUE;
99 }
100
101 %}
102
103 %pure-parser
104
105 %union
106 {
107     struct cond_str str;
108     LPWSTR    string;
109     INT       value;
110 }
111
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
120
121 %nonassoc COND_ERROR COND_EOF
122
123 %type <value> expression boolean_term boolean_factor 
124 %type <value> value_i integer operator
125 %type <string> identifier symbol_s value_s literal
126
127 %%
128
129 condition:
130     expression 
131         {
132             COND_input* cond = (COND_input*) info;
133             cond->result = $1;
134         }
135   | /* empty */
136         {
137             COND_input* cond = (COND_input*) info;
138             cond->result = MSICONDITION_NONE;
139         }
140     ;
141
142 expression:
143     boolean_term 
144         {
145             $$ = $1;
146         }
147   | expression COND_OR boolean_term
148         {
149             $$ = $1 || $3;
150         }
151   | expression COND_IMP boolean_term
152         {
153             $$ = !$1 || $3;
154         }
155   | expression COND_XOR boolean_term
156         {
157             $$ = ( $1 || $3 ) && !( $1 && $3 );
158         }
159   | expression COND_EQV boolean_term
160         {
161             $$ = ( $1 && $3 ) || ( !$1 && !$3 );
162         }
163     ;
164
165 boolean_term:
166     boolean_factor
167         {
168             $$ = $1;
169         }
170   | boolean_term COND_AND boolean_factor
171         {
172             $$ = $1 && $3;
173         }
174     ;
175
176 boolean_factor:
177     COND_NOT boolean_factor
178         {
179             $$ = $2 ? 0 : 1;
180         }
181   | value_i
182         {
183             $$ = $1 ? 1 : 0;
184         }
185   | value_s
186         {
187             $$ = ($1 && $1[0]) ? 1 : 0;
188             msi_free($1);
189         }
190   | value_i operator value_i
191         {
192             $$ = compare_int( $1, $2, $3 );
193         }
194   | symbol_s operator value_i
195         {
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);
202         }
203   | value_i operator symbol_s
204         {
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);
211         }
212   | symbol_s operator symbol_s
213         {
214             $$ = compare_and_free_strings( $1, $2, $3 );
215         }
216   | symbol_s operator literal
217         {
218             $$ = compare_and_free_strings( $1, $2, $3 );
219         }
220   | literal operator symbol_s
221         {
222             $$ = compare_and_free_strings( $1, $2, $3 );
223         }
224   | literal operator literal
225         {
226             $$ = compare_and_free_strings( $1, $2, $3 );
227         }
228   | literal operator value_i
229         {
230             $$ = 0;
231             msi_free($1);
232         }
233   | value_i operator literal
234         {
235             $$ = 0;
236             msi_free($3);
237         }
238   | COND_LPAR expression COND_RPAR
239         {
240             $$ = $2;
241         }
242     ;
243
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; }
264     ;
265
266 value_s:
267     symbol_s
268     {
269         $$ = $1;
270     } 
271   | literal
272     {
273         $$ = $1;
274     }
275     ;
276
277 literal:
278     COND_LITER
279         {
280             $$ = COND_GetLiteral(&$1);
281             if( !$$ )
282                 YYABORT;
283         }
284     ;
285
286 value_i:
287     integer
288         {
289             $$ = $1;
290         }
291   | COND_DOLLARS identifier
292         {
293             COND_input* cond = (COND_input*) info;
294             INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
295       
296             MSI_GetComponentStateW(cond->package, $2, &install, &action );
297             $$ = action;
298             msi_free( $2 );
299         }
300   | COND_QUESTION identifier
301         {
302             COND_input* cond = (COND_input*) info;
303             INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
304       
305             MSI_GetComponentStateW(cond->package, $2, &install, &action );
306             $$ = install;
307             msi_free( $2 );
308         }
309   | COND_AMPER identifier
310         {
311             COND_input* cond = (COND_input*) info;
312             INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
313       
314             MSI_GetFeatureStateW(cond->package, $2, &install, &action );
315             $$ = action;
316             msi_free( $2 );
317         }
318   | COND_EXCLAM identifier
319         {
320             COND_input* cond = (COND_input*) info;
321             INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
322       
323             MSI_GetFeatureStateW(cond->package, $2, &install, &action );
324             $$ = install;
325             msi_free( $2 );
326         }
327     ;
328
329 symbol_s:
330     identifier
331         {
332             COND_input* cond = (COND_input*) info;
333
334             $$ = msi_dup_property( cond->package, $1 );
335             msi_free( $1 );
336         }
337     | COND_PERCENT identifier
338         {
339             UINT len = GetEnvironmentVariableW( $2, NULL, 0 );
340             $$ = NULL;
341             if (len++)
342             {
343                 $$ = msi_alloc( len*sizeof (WCHAR) );
344                 GetEnvironmentVariableW( $2, $$, len );
345             }
346             msi_free( $2 );
347         }
348     ;
349
350 identifier:
351     COND_IDENT
352         {
353             $$ = COND_GetString(&$1);
354             if( !$$ )
355                 YYABORT;
356         }
357     ;
358
359 integer:
360     COND_NUMBER
361         {
362             LPWSTR szNum = COND_GetString(&$1);
363             if( !szNum )
364                 YYABORT;
365             $$ = atoiW( szNum );
366             msi_free( szNum );
367         }
368     ;
369
370 %%
371
372
373 static int COND_IsAlpha( WCHAR x )
374 {
375     return( ( ( x >= 'A' ) && ( x <= 'Z' ) ) ||
376             ( ( x >= 'a' ) && ( x <= 'z' ) ) ||
377             ( ( x == '_' ) ) );
378 }
379
380 static int COND_IsNumber( WCHAR x )
381 {
382     return( (( x >= '0' ) && ( x <= '9' ))  || (x =='-') || (x =='.') );
383 }
384
385 static WCHAR *strstriW( const WCHAR *str, const WCHAR *sub )
386 {
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;
396 }
397
398 static BOOL str_is_number( LPCWSTR str )
399 {
400     int i;
401
402     for (i = 0; i < lstrlenW( str ); i++)
403         if (!isdigitW(str[i]))
404             return FALSE;
405
406     return TRUE;
407 }
408
409 static INT compare_substring( LPCWSTR a, INT operator, LPCWSTR b )
410 {
411     int lhs, rhs;
412
413     /* substring operators return 0 if LHS is missing */
414     if (!a || !*a)
415         return 0;
416
417     /* substring operators return 1 if RHS is missing */
418     if (!b || !*b)
419         return 1;
420
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 );
426
427     switch (operator)
428     {
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;
444     }
445     return 0;
446 }
447
448 static BOOL is_empty( LPCWSTR p )
449 {
450     return !p || !p[0];
451 }
452
453 static BOOL is_alphaless( LPCWSTR p )
454 {
455     while (*p)
456     {
457         if (isalphaW( *p ) || (*p == '_'))
458              return FALSE;
459         p++;
460     }
461     return TRUE;
462 }
463
464 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b )
465 {
466     int r;
467
468     if (operator >= COND_SS && operator <= COND_RHS)
469         return compare_substring( a, operator, b );
470
471     if (is_empty( a ) && is_empty( b ))
472         r = 0;
473
474     else if (is_empty( a ) && is_alphaless( b ))
475         r = 1;
476
477     else if (is_empty( b ) && is_alphaless( a ))
478         r = -1;
479
480     else
481     {
482         /* null and empty string are equivalent */
483         if (!a) a = szEmpty;
484         if (!b) b = szEmpty;
485
486         switch (operator)
487         {
488         case COND_LT:
489         case COND_GT:
490         case COND_EQ:
491         case COND_NE:
492         case COND_GE:
493         case COND_LE:
494             r = lstrcmpW( a, b );
495             break;
496         case COND_ILT:
497         case COND_IGT:
498         case COND_IEQ:
499         case COND_INE:
500         case COND_IGE:
501         case COND_ILE:
502             r = lstrcmpiW( a, b );
503             break;
504         default:
505             ERR("invalid string operator\n");
506             return 0;
507         }
508     }
509
510     switch (operator)
511     {
512     case COND_LT:
513     case COND_ILT:
514         return -1 == r;
515     case COND_GT:
516     case COND_IGT:
517         return  1 == r;
518     case COND_EQ:
519     case COND_IEQ:
520         return  0 == r;
521     case COND_NE:
522     case COND_INE:
523         return  0 != r;
524     case COND_GE:
525     case COND_IGE:
526         return -1 != r;
527     case COND_LE:
528     case COND_ILE:
529         return  1 != r;
530     default:
531         ERR("invalid string operator\n");
532     }
533     return 0;
534 }
535
536
537 static INT compare_int( INT a, INT operator, INT b )
538 {
539     switch (operator)
540     {
541     case COND_LT:
542     case COND_ILT:
543         return a < b;
544     case COND_GT:
545     case COND_IGT:
546         return a > b;
547     case COND_EQ:
548     case COND_IEQ:
549         return a == b;
550     case COND_NE:
551     case COND_INE:
552         return a != b;
553     case COND_GE:
554     case COND_IGE:
555         return a >= b;
556     case COND_LE:
557     case COND_ILE:
558         return a <= b;
559     case COND_SS:
560     case COND_ISS:
561         return ( a & b ) ? 1 : 0;
562     case COND_RHS:
563         return ( ( a & 0xffff ) == b ) ? 1 : 0;
564     case COND_LHS:
565         return ( ( (a>>16) & 0xffff ) == b ) ? 1 : 0;
566     default:
567         ERR("invalid integer operator\n");
568         return 0;
569     }
570     return 0;
571 }
572
573
574 static int COND_IsIdent( WCHAR x )
575 {
576     return( COND_IsAlpha( x ) || COND_IsNumber( x ) || ( x == '_' ) 
577             || ( x == '#' ) || (x == '.') );
578 }
579
580 static int COND_GetOperator( COND_input *cond )
581 {
582     static const struct {
583         const WCHAR str[4];
584         int id;
585     } table[] = {
586         { {'~','=',0},     COND_IEQ },
587         { {'~','<','=',0}, COND_ILE },
588         { {'~','>','<',0}, COND_ISS },
589         { {'~','>','>',0}, COND_IRHS },
590         { {'~','<','>',0}, COND_INE },
591         { {'~','<',0},     COND_ILT },
592         { {'~','>','=',0}, COND_IGE },
593         { {'~','<','<',0}, COND_ILHS },
594         { {'~','>',0},     COND_IGT },
595         { {'>','=',0},     COND_GE  },
596         { {'>','<',0},     COND_SS  },
597         { {'<','<',0},     COND_LHS },
598         { {'<','>',0},     COND_NE  },
599         { {'<','=',0},     COND_LE  },
600         { {'>','>',0},     COND_RHS },
601         { {'>',0},         COND_GT  },
602         { {'<',0},         COND_LT  },
603         { {0},             0        }
604     };
605     LPCWSTR p = &cond->str[cond->n];
606     int i = 0, len;
607
608     while ( 1 )
609     {
610         len = lstrlenW( table[i].str );
611         if ( !len || 0 == strncmpW( table[i].str, p, len ) )
612             break;
613         i++;
614     }
615     cond->n += len;
616     return table[i].id;
617 }
618
619 static int COND_GetOne( struct cond_str *str, COND_input *cond )
620 {
621     int rc, len = 1;
622     WCHAR ch;
623
624     str->data = &cond->str[cond->n];
625
626     ch = str->data[0];
627
628     switch( ch )
629     {
630     case 0: return 0;
631     case '(': rc = COND_LPAR; break;
632     case ')': rc = COND_RPAR; break;
633     case '&': rc = COND_AMPER; break;
634     case '!': rc = COND_EXCLAM; break;
635     case '$': rc = COND_DOLLARS; break;
636     case '?': rc = COND_QUESTION; break;
637     case '%': rc = COND_PERCENT; break;
638     case ' ': rc = COND_SPACE; break;
639     case '=': rc = COND_EQ; break;
640         break;
641
642     case '~':
643     case '<':
644     case '>':
645         rc = COND_GetOperator( cond );
646         if (!rc)
647             rc = COND_ERROR;
648         return rc;
649     default:
650         rc = 0;
651     }
652
653     if ( rc )
654     {
655         cond->n += len;
656         return rc;
657     }
658
659     if (ch == '"' )
660     {
661         LPCWSTR p = strchrW( str->data + 1, '"' );
662         if (!p)
663             return COND_ERROR;
664         len = p - str->data + 1;
665         rc = COND_LITER;
666     }
667     else if( COND_IsAlpha( ch ) )
668     {
669         static const WCHAR szNot[] = {'N','O','T',0};
670         static const WCHAR szAnd[] = {'A','N','D',0};
671         static const WCHAR szXor[] = {'X','O','R',0};
672         static const WCHAR szEqv[] = {'E','Q','V',0};
673         static const WCHAR szImp[] = {'I','M','P',0};
674         static const WCHAR szOr[] = {'O','R',0};
675
676         while( COND_IsIdent( str->data[len] ) )
677             len++;
678         rc = COND_IDENT;
679
680         if ( len == 3 )
681         {
682             if ( !strncmpiW( str->data, szNot, len ) )
683                 rc = COND_NOT;
684             else if( !strncmpiW( str->data, szAnd, len ) )
685                 rc = COND_AND;
686             else if( !strncmpiW( str->data, szXor, len ) )
687                 rc = COND_XOR;
688             else if( !strncmpiW( str->data, szEqv, len ) )
689                 rc = COND_EQV;
690             else if( !strncmpiW( str->data, szImp, len ) )
691                 rc = COND_IMP;
692         }
693         else if( (len == 2) && !strncmpiW( str->data, szOr, len ) )
694             rc = COND_OR;
695     }
696     else if( COND_IsNumber( ch ) )
697     {
698         while( COND_IsNumber( str->data[len] ) )
699             len++;
700         rc = COND_NUMBER;
701     }
702     else
703     {
704         ERR("Got unknown character %c(%x)\n",ch,ch);
705         return COND_ERROR;
706     }
707
708     cond->n += len;
709     str->len = len;
710
711     return rc;
712 }
713
714 static int cond_lex( void *COND_lval, COND_input *cond )
715 {
716     int rc;
717     struct cond_str *str = COND_lval;
718
719     do {
720         rc = COND_GetOne( str, cond );
721     } while (rc == COND_SPACE);
722     
723     return rc;
724 }
725
726 static LPWSTR COND_GetString( struct cond_str *str )
727 {
728     LPWSTR ret;
729
730     ret = msi_alloc( (str->len+1) * sizeof (WCHAR) );
731     if( ret )
732     {
733         memcpy( ret, str->data, str->len * sizeof(WCHAR));
734         ret[str->len]=0;
735     }
736     TRACE("Got identifier %s\n",debugstr_w(ret));
737     return ret;
738 }
739
740 static LPWSTR COND_GetLiteral( struct cond_str *str )
741 {
742     LPWSTR ret;
743
744     ret = msi_alloc( (str->len-1) * sizeof (WCHAR) );
745     if( ret )
746     {
747         memcpy( ret, str->data+1, (str->len-2) * sizeof(WCHAR) );
748         ret[str->len - 2]=0;
749     }
750     TRACE("Got literal %s\n",debugstr_w(ret));
751     return ret;
752 }
753
754 static int cond_error(const char *str)
755 {
756     TRACE("%s\n", str );
757     return 0;
758 }
759
760 MSICONDITION MSI_EvaluateConditionW( MSIPACKAGE *package, LPCWSTR szCondition )
761 {
762     COND_input cond;
763     MSICONDITION r;
764
765     TRACE("%s\n", debugstr_w( szCondition ) );
766
767     if ( szCondition == NULL )
768         return MSICONDITION_NONE;
769
770     cond.package = package;
771     cond.str   = szCondition;
772     cond.n     = 0;
773     cond.result = MSICONDITION_ERROR;
774     
775     if ( !cond_parse( &cond ) )
776         r = cond.result;
777     else
778         r = MSICONDITION_ERROR;
779
780     TRACE("%i <- %s\n", r, debugstr_w(szCondition));
781     return r;
782 }
783
784 MSICONDITION WINAPI MsiEvaluateConditionW( MSIHANDLE hInstall, LPCWSTR szCondition )
785 {
786     MSIPACKAGE *package;
787     UINT ret;
788
789     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
790     if( !package)
791         return MSICONDITION_ERROR;
792     ret = MSI_EvaluateConditionW( package, szCondition );
793     msiobj_release( &package->hdr );
794     return ret;
795 }
796
797 MSICONDITION WINAPI MsiEvaluateConditionA( MSIHANDLE hInstall, LPCSTR szCondition )
798 {
799     LPWSTR szwCond = NULL;
800     MSICONDITION r;
801
802     szwCond = strdupAtoW( szCondition );
803     if( szCondition && !szwCond )
804         return MSICONDITION_ERROR;
805
806     r = MsiEvaluateConditionW( hInstall, szwCond );
807     msi_free( szwCond );
808     return r;
809 }