a3e158b68a5c95ee88c7ca76a8e8aa404b5c404e
[sip-router] / rvalue.c
1 /* 
2  * $Id$
3  * 
4  * Copyright (C) 2008 iptelorg GmbH
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 /**
19  * @file 
20  * @brief rvalue expressions
21  */
22 /* 
23  * History:
24  * --------
25  *  2008-12-01  initial version (andrei)
26  *  2009-04-24  added support for defined, strempty, strlen (andrei)
27  *  2009-04-28  int and str automatic conversions: (int)undef=0,
28  *               (str)undef="", (int)""=0, (int)"123"=123, (int)"abc"=0
29  *              handle undef == expr, in function of the UNDEF_EQ_* defines.
30  *              (andrei)
31  *  2009-05-05  casts operator for int & string (andrei)
32  */
33
34 /* special defines:
35  *
36  *  UNDEF_EQ_* - how to behave when undef is on the right side of a generic
37  *               compare operator
38  *  UNDEF_EQ_ALWAYS_FALSE:  undef  == something  is always false
39  *  UNDEF_EQ_UNDEF_TRUE  :  undef == something false except for undef==undef
40  *                          which is true
41  *  no UNDEF_EQ* define  :  undef == expr => convert undef to typeof(expr)
42  *                          and perform normal ==. undef == undef will be
43  *                          converted to string and it will be true
44  *                          ("" == "")
45  * NOTE: expr == undef, with defined(expr) is always evaluated this way:
46          expr == (type_of(expr))undef
47  *  RV_STR2INT_VERBOSE_ERR - if a string conversion to int fails, log (L_WARN)
48  *                           the string that caused it (only the string, not
49  *                           the expression position).
50  *  RV_STR2INT_ERR         - if a string conversion to int fails, don't ignore
51  *                           the error (return error).
52  *  RVAL_GET_INT_ERR_WARN  - if a conversion to int fails, log a warning with
53  *                           the expression position.
54  *                           Depends on RV_STR2INT_ERR.
55  *  RVAL_GET_INT_ERR_IGN   - if a conversion to int fails, ignore the error
56  *                           (the result will be 0). Can be combined with
57  *                           RVAL_GET_INT_ERR_WARN.
58  *                           Depends on RV_STR2INT_ERR.
59  */
60
61 /*!
62  * \file
63  * \brief SIP-router core :: 
64  * \ingroup core
65  * Module: \ref core
66  */
67
68 #include "rvalue.h"
69
70 #include <stdlib.h> /* abort() */
71
72 /* if defined warn when str2int conversions fail */
73 #define RV_STR2INT_VERBOSE_ERR
74
75 /* if defined rval_get_int will fail if str2int conversion fail
76    (else convert to 0) */
77 #define RV_STR2INT_ERR
78
79 /* if a rval_get_int fails (conversion to int), warn
80    Depends on RV_STR2INT_ERR.
81  */
82 #define RVAL_GET_INT_ERR_WARN
83
84 /* if a rval_get_int fails, ignore it (expression evaluation will not fail,
85    the int conversion will result in 0).
86    Can be combined with RVAL_GET_INT_ERR_WARN.
87    Depends on RV_STR2INT_ERR.
88  */
89 #define RVAL_GET_INT_ERR_IGN
90
91 /* minimum size alloc'ed for STR RVs (to accomodate
92  * strops without reallocs) */
93 #define RV_STR_EXTRA 80
94
95 #define rv_ref(rv) ((rv)->refcnt++)
96
97 /** unref rv and returns true if 0 */
98 #define rv_unref(rv) ((--(rv)->refcnt)==0)
99
100
101 inline static void rval_force_clean(struct rvalue* rv)
102 {
103         if (rv->flags & RV_CNT_ALLOCED_F){
104                 switch(rv->type){
105                         case RV_STR:
106                                 pkg_free(rv->v.s.s);
107                                 rv->v.s.s=0;
108                                 rv->v.s.len=0;
109                                 break;
110                         default:
111                                 BUG("RV_CNT_ALLOCED_F not supported for type %d\n", rv->type);
112                 }
113                 rv->flags&=~RV_CNT_ALLOCED_F;
114         }
115         if (rv->flags & RV_RE_ALLOCED_F){
116                 if (rv->v.re.regex){
117                         if (unlikely(rv->type!=RV_STR || !(rv->flags & RV_RE_F))){
118                                 BUG("RV_RE_ALLOCED_F not supported for type %d or "
119                                                 "bad flags %x\n", rv->type, rv->flags);
120                         }
121                         regfree(rv->v.re.regex);
122                         pkg_free(rv->v.re.regex);
123                         rv->v.re.regex=0;
124                 }
125                 rv->flags&=~(RV_RE_ALLOCED_F|RV_RE_F);
126         }
127 }
128
129
130
131 /** frees a rval returned by rval_new(), rval_convert() or rval_expr_eval().
132  *   Note: it will be freed only when refcnt reaches 0
133  */
134 void rval_destroy(struct rvalue* rv)
135 {
136         if (rv && rv_unref(rv)){
137                 rval_force_clean(rv);
138                 /* still an un-regfreed RE ? */
139                 if ((rv->flags & RV_RE_F) && rv->v.re.regex){
140                         if (unlikely(rv->type!=RV_STR))
141                                 BUG("RV_RE_F not supported for type %d\n", rv->type);
142                         regfree(rv->v.re.regex);
143                 }
144                 if (rv->flags & RV_RV_ALLOCED_F){
145                         pkg_free(rv);
146                 }
147         }
148 }
149
150
151
152 void rval_clean(struct rvalue* rv)
153 {
154         if (rv_unref(rv))
155                 rval_force_clean(rv);
156 }
157
158
159
160 void rve_destroy(struct rval_expr* rve)
161 {
162         if (rve){
163                 if (rve->op==RVE_RVAL_OP){
164                         if (rve->left.rval.refcnt){
165                                 if (rve->left.rval.refcnt==1)
166                                         rval_destroy(&rve->left.rval);
167                                 else
168                                         BUG("rval expr rval with invalid refcnt: %d (%d,%d-%d,%d)"
169                                                         "\n", rve->left.rval.refcnt,
170                                                         rve->fpos.s_line, rve->fpos.s_col,
171                                                         rve->fpos.e_line, rve->fpos.e_col);
172                         }
173                         if (rve->right.rval.refcnt){
174                                 if (rve->right.rval.refcnt==1)
175                                         rval_destroy(&rve->right.rval);
176                                 else
177                                         BUG("rval expr rval with invalid refcnt: %d (%d,%d-%d,%d)"
178                                                         "\n", rve->right.rval.refcnt,
179                                                         rve->fpos.s_line, rve->fpos.s_col,
180                                                         rve->fpos.e_line, rve->fpos.e_col);
181                         }
182                 }else{
183                         if (rve->left.rve)
184                                 rve_destroy(rve->left.rve);
185                         if (rve->right.rve)
186                                 rve_destroy(rve->right.rve);
187                 }
188                 pkg_free(rve);
189         }
190 }
191
192
193
194 void rval_cache_clean(struct rval_cache* rvc)
195 {
196         if ((rvc->cache_type==RV_CACHE_PVAR) && (rvc->val_type!=RV_NONE)){
197                 pv_value_destroy(&rvc->c.pval);
198         }
199         rvc->cache_type=RV_CACHE_EMPTY;
200         rvc->val_type=RV_NONE;
201 }
202
203
204 #define rv_chg_in_place(rv)  ((rv)->refcnt==1)
205
206
207
208 /** init a rvalue structure.
209  * Note: not needed if the structure is allocate with one of the 
210  * rval_new* functions
211  */
212 void rval_init(struct rvalue* rv, enum rval_type t, union rval_val* v, 
213                                 int flags)
214 {
215         rv->flags=flags;
216         rv->refcnt=1;
217         rv->type=t;
218         if (v){
219                 rv->v=*v;
220         }else{
221                 memset (&rv->v, 0, sizeof(rv->v));
222         }
223 }
224
225
226
227 /** create a new pk_malloc'ed empty rvalue.
228   *
229   * @param extra_size - extra space to allocate
230   *                    (e.g.: so that future string operation can reuse
231   *                     the space)
232   * @return new rv or 0 on error
233   */
234 struct rvalue* rval_new_empty(int extra_size)
235 {
236         struct rvalue* rv;
237         int size; /* extra size at the end */
238         
239         size=ROUND_LONG(sizeof(*rv)-sizeof(rv->buf)+extra_size); /* round up */
240         rv=pkg_malloc(size);
241         if (likely(rv)){
242                 rv->bsize=size-sizeof(*rv)-sizeof(rv->buf); /* remaining size->buffer*/
243                 rv->flags=RV_RV_ALLOCED_F;
244                 rv->refcnt=1;
245                 rv->type=RV_NONE;
246         }
247         return rv;
248 }
249
250
251
252 /** create a new pk_malloc'ed rv from a str.
253   *
254   * @param s - pointer to str, must be non-null
255   * @param extra_size - extra space to allocate
256   *                    (so that future string operation can reuse
257   *                     the space)
258   * @return new rv or 0 on error
259   */
260 struct rvalue* rval_new_str(str* s, int extra_size)
261 {
262         struct rvalue* rv;
263         
264         rv=rval_new_empty(extra_size+s->len+1/* 0 term */);
265         if (likely(rv)){
266                 rv->type=RV_STR;
267                 rv->v.s.s=&rv->buf[0];
268                 rv->v.s.len=s->len;
269                 memcpy(rv->v.s.s, s->s, s->len);
270                 rv->v.s.s[s->len]=0;
271         }
272         return rv;
273 }
274
275
276
277 /** create a new pk_malloc'ed RE rv from a str re.
278   * It acts as rval_new_str, but also compiles a RE from the str
279   * and sets v->re.regex.
280   * @param s - pointer to str, must be non-null, zero-term'ed and a valid RE.
281   * @return new rv or 0 on error
282   */
283 struct rvalue* rval_new_re(str* s)
284 {
285         struct rvalue* rv;
286         long offs;
287         
288         offs=(long)&((struct rvalue*)0)->buf[0]; /* offset of the buf. member */
289         /* make sure we reserve enough space so that we can satisfy any regex_t
290            alignment requirement (pointer) */
291         rv=rval_new_empty(ROUND_POINTER(offs)-offs+sizeof(*rv->v.re.regex)+
292                                                 s->len+1/* 0 */);
293         if (likely(rv)){
294                 rv->type=RV_STR;
295                 /* make sure regex points to a properly aligned address
296                    (use max./pointer alignment to be sure ) */
297                 rv->v.re.regex=(regex_t*)((char*)&rv->buf[0]+ROUND_POINTER(offs)-offs);
298                 rv->v.s.s=(char*)rv->v.re.regex+sizeof(*rv->v.re.regex);
299                 rv->v.s.len=s->len;
300                 memcpy(rv->v.s.s, s->s, s->len);
301                 rv->v.s.s[s->len]=0;
302                 /* compile the regex */
303                 /* same flags as for expr. =~ (fix_expr()) */
304                 if (unlikely(regcomp(rv->v.re.regex, s->s,
305                                                                 REG_EXTENDED|REG_NOSUB|REG_ICASE))){
306                         /* error */
307                         pkg_free(rv);
308                         rv=0;
309                 }else /* success */
310                         rv->flags|=RV_RE_F;
311         }
312         return rv;
313 }
314
315
316
317 /** get string name for a type.
318   *
319   * @return - null terminated name of the type
320   */
321 char* rval_type_name(enum rval_type type)
322 {
323         switch(type){
324                 case RV_NONE:
325                         return "none";
326                 case RV_INT:
327                         return "int";
328                 case RV_STR:
329                         return "str";
330                 case RV_BEXPR:
331                         return "bexpr_t";
332                 case RV_ACTION_ST:
333                         return "action_t";
334                 case RV_PVAR:
335                         return "pvar";
336                 case RV_AVP:
337                         return "avp";
338                         break;
339                 case RV_SEL:
340                         return "select";
341         }
342         return "error_unkown_type";
343 }
344
345
346
347 /** create a new pk_malloc'ed rvalue from a rval_val union.
348   *
349   * @param s - pointer to str, must be non-null
350   * @param extra_size - extra space to allocate
351   *                    (so that future string operation can reuse
352   *                     the space)
353   * @return new rv or 0 on error
354   */
355 struct rvalue* rval_new(enum rval_type t, union rval_val* v, int extra_size)
356 {
357         struct rvalue* rv;
358         
359         if (t==RV_STR && v && v->s.s)
360                 return rval_new_str(&v->s, extra_size);
361         rv=rval_new_empty(extra_size);
362         if (likely(rv)){
363                 rv->type=t;
364                 if (likely(v && t!=RV_STR)){
365                         rv->v=*v;
366                 }else if (t==RV_STR){
367                         rv->v.s.s=&rv->buf[0];
368                         rv->v.s.len=0;
369                         if (likely(extra_size)) rv->v.s.s[0]=0;
370                 }else
371                         memset (&rv->v, 0, sizeof(rv->v));
372         }
373         return rv;
374 }
375
376
377
378 /** get rvalue basic type (RV_INT or RV_STR).
379   *
380   * Given a rvalue it tries to determinte its basic type.
381   * Fills val_cache if non-null and empty (can be used in other rval*
382   * function calls, to avoid re-resolving avps or pvars). It must be
383   * rval_cache_clean()'en when no longer needed.
384   *
385   * @param rv - target rvalue
386   * @param val_cache - write-only: value cache, might be filled if non-null,
387   *                    it _must_ be rval_cache_clean()'en when done.
388   * @return - basic type or RV_NONE on error
389   */
390 inline static enum rval_type rval_get_btype(struct run_act_ctx* h,
391                                                                                         struct sip_msg* msg,
392                                                                                         struct rvalue* rv,
393                                                                                         struct rval_cache* val_cache)
394 {
395         avp_t* r_avp;
396         int_str tmp_avp_val;
397         int_str* avpv;
398         pv_value_t tmp_pval;
399         pv_value_t* pv;
400         enum rval_type tmp;
401         enum rval_type* ptype;
402         
403         switch(rv->type){
404                 case RV_INT:
405                 case RV_STR:
406                         return rv->type;
407                 case RV_BEXPR:
408                 case RV_ACTION_ST:
409                         return RV_INT;
410                 case RV_PVAR:
411                         if (likely(val_cache && val_cache->cache_type==RV_CACHE_EMPTY)){
412                                 pv=&val_cache->c.pval;
413                                 val_cache->cache_type=RV_CACHE_PVAR;
414                         }else{
415                                 val_cache=0;
416                                 pv=&tmp_pval;
417                         }
418                         memset(pv, 0, sizeof(tmp_pval));
419                         if (likely(pv_get_spec_value(msg, &rv->v.pvs, pv)==0)){
420                                 if (pv->flags & PV_TYPE_INT){
421                                         if (likely(val_cache!=0))
422                                                 val_cache->val_type=RV_INT;
423                                         else
424                                                 pv_value_destroy(pv);
425                                         return RV_INT;
426                                 }else if (pv->flags & PV_VAL_STR){
427                                         if (likely(val_cache!=0))
428                                                 val_cache->val_type=RV_STR;
429                                         else
430                                                 pv_value_destroy(pv);
431                                         return RV_STR;
432                                 }else{
433                                         pv_value_destroy(pv);
434                                         if (likely(val_cache!=0))
435                                                 val_cache->val_type=RV_NONE; /* undefined */
436                                         goto error;
437                                 }
438                         }else{
439                                 if (likely(val_cache!=0))
440                                         val_cache->val_type=RV_NONE; /* undefined */
441                                 goto error;
442                         }
443                         break;
444                 case RV_AVP:
445                         if (likely(val_cache && val_cache->cache_type==RV_CACHE_EMPTY)){
446                                 ptype=&val_cache->val_type;
447                                 avpv=&val_cache->c.avp_val;
448                                 val_cache->cache_type=RV_CACHE_AVP;
449                         }else{
450                                 ptype=&tmp;
451                                 avpv=&tmp_avp_val;
452                         }
453                         r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
454                                                                                         avpv, rv->v.avps.index);
455                         if (likely(r_avp)){
456                                 if (r_avp->flags & AVP_VAL_STR){
457                                         *ptype=RV_STR;
458                                         return RV_STR;
459                                 }else{
460                                         *ptype=RV_INT;
461                                         return RV_INT;
462                                 }
463                         }else{
464                                 *ptype=RV_NONE;
465                                 goto error;
466                         }
467                         break;
468                 case RV_SEL:
469                         return RV_STR;
470                 default:
471                         BUG("rv type %d not handled\n", rv->type);
472         }
473 error:
474         return RV_NONE;
475 }
476
477
478
479 /** guess the type of an expression.
480   * @return RV_INT, RV_STR or RV_NONE (when type could not be found,
481   * e.g. avp or pvar)
482   */
483 enum rval_type rve_guess_type( struct rval_expr* rve)
484 {
485         switch(rve->op){
486                 case RVE_RVAL_OP:
487                         switch(rve->left.rval.type){
488                                 case RV_STR:
489                                 case RV_SEL:
490                                         return RV_STR;
491                                 case RV_INT:
492                                 case RV_BEXPR:
493                                 case RV_ACTION_ST:
494                                         return RV_INT;
495                                 case RV_PVAR:
496                                 case RV_AVP:
497                                 case RV_NONE:
498                                         return RV_NONE;
499                         }
500                         break;
501                 case RVE_UMINUS_OP:
502                 case RVE_BOOL_OP:
503                 case RVE_LNOT_OP:
504                 case RVE_BNOT_OP:
505                 case RVE_MINUS_OP:
506                 case RVE_MUL_OP:
507                 case RVE_DIV_OP:
508                 case RVE_MOD_OP:
509                 case RVE_BOR_OP:
510                 case RVE_BAND_OP:
511                 case RVE_BXOR_OP:
512                 case RVE_BLSHIFT_OP:
513                 case RVE_BRSHIFT_OP:
514                 case RVE_LAND_OP:
515                 case RVE_LOR_OP:
516                 case RVE_GT_OP:
517                 case RVE_GTE_OP:
518                 case RVE_LT_OP:
519                 case RVE_LTE_OP:
520                 case RVE_EQ_OP:
521                 case RVE_DIFF_OP:
522                 case RVE_IEQ_OP:
523                 case RVE_IDIFF_OP:
524                 case RVE_STREQ_OP:
525                 case RVE_STRDIFF_OP:
526                 case RVE_MATCH_OP:
527                 case RVE_IPLUS_OP:
528                 case RVE_STRLEN_OP:
529                 case RVE_STREMPTY_OP:
530                 case RVE_DEFINED_OP:
531                 case RVE_INT_OP:
532                         return RV_INT;
533                 case RVE_PLUS_OP:
534                         /* '+' evaluates to the type of the left operand */
535                         return rve_guess_type(rve->left.rve);
536                 case RVE_CONCAT_OP:
537                 case RVE_STR_OP:
538                         return RV_STR;
539                 case RVE_NONE_OP:
540                         break;
541         }
542         return RV_NONE;
543 }
544
545
546
547 /** returns true if expression is constant.
548   * @return 0 or 1 on
549   *  non constant type
550   */
551 int rve_is_constant(struct rval_expr* rve)
552 {
553         switch(rve->op){
554                 case RVE_RVAL_OP:
555                         switch(rve->left.rval.type){
556                                 case RV_STR:
557                                         return 1;
558                                 case RV_INT:
559                                         return 1;
560                                 case RV_SEL:
561                                 case RV_BEXPR:
562                                 case RV_ACTION_ST:
563                                 case RV_PVAR:
564                                 case RV_AVP:
565                                 case RV_NONE:
566                                         return 0;
567                         }
568                         break;
569                 case RVE_UMINUS_OP:
570                 case RVE_BOOL_OP:
571                 case RVE_LNOT_OP:
572                 case RVE_BNOT_OP:
573                 case RVE_STRLEN_OP:
574                 case RVE_STREMPTY_OP:
575                 case RVE_DEFINED_OP:
576                 case RVE_INT_OP:
577                 case RVE_STR_OP:
578                         return rve_is_constant(rve->left.rve);
579                 case RVE_MINUS_OP:
580                 case RVE_MUL_OP:
581                 case RVE_DIV_OP:
582                 case RVE_MOD_OP:
583                 case RVE_BOR_OP:
584                 case RVE_BAND_OP:
585                 case RVE_BXOR_OP:
586                 case RVE_BLSHIFT_OP:
587                 case RVE_BRSHIFT_OP:
588                 case RVE_LAND_OP:
589                 case RVE_LOR_OP:
590                 case RVE_GT_OP:
591                 case RVE_GTE_OP:
592                 case RVE_LT_OP:
593                 case RVE_LTE_OP:
594                 case RVE_EQ_OP:
595                 case RVE_DIFF_OP:
596                 case RVE_IEQ_OP:
597                 case RVE_IDIFF_OP:
598                 case RVE_STREQ_OP:
599                 case RVE_STRDIFF_OP:
600                 case RVE_MATCH_OP:
601                 case RVE_PLUS_OP:
602                 case RVE_IPLUS_OP:
603                 case RVE_CONCAT_OP:
604                         return rve_is_constant(rve->left.rve) &&
605                                         rve_is_constant(rve->right.rve);
606                 case RVE_NONE_OP:
607                         break;
608         }
609         return 0;
610 }
611
612
613
614 /** returns true if the expression has side-effects.
615   * @return  1 for possible side-effects, 0 for no side-effects
616   * TODO: add better checks
617   */
618 int rve_has_side_effects(struct rval_expr* rve)
619 {
620         return !rve_is_constant(rve);
621 }
622
623
624
625 /** returns true if operator is unary (takes only 1 arg).
626   * @return 0 or 1 on
627   */
628 static int rve_op_unary(enum rval_expr_op op)
629 {
630         switch(op){
631                 case RVE_RVAL_OP: /* not realy an operator */
632                         return -1;
633                 case RVE_UMINUS_OP:
634                 case RVE_BOOL_OP:
635                 case RVE_LNOT_OP:
636                 case RVE_BNOT_OP:
637                 case RVE_STRLEN_OP:
638                 case RVE_STREMPTY_OP:
639                 case RVE_DEFINED_OP:
640                 case RVE_INT_OP:
641                 case RVE_STR_OP:
642                         return 1;
643                 case RVE_MINUS_OP:
644                 case RVE_MUL_OP:
645                 case RVE_DIV_OP:
646                 case RVE_MOD_OP:
647                 case RVE_BOR_OP:
648                 case RVE_BAND_OP:
649                 case RVE_BXOR_OP:
650                 case RVE_BLSHIFT_OP:
651                 case RVE_BRSHIFT_OP:
652                 case RVE_LAND_OP:
653                 case RVE_LOR_OP:
654                 case RVE_GT_OP:
655                 case RVE_GTE_OP:
656                 case RVE_LT_OP:
657                 case RVE_LTE_OP:
658                 case RVE_EQ_OP:
659                 case RVE_DIFF_OP:
660                 case RVE_IEQ_OP:
661                 case RVE_IDIFF_OP:
662                 case RVE_STREQ_OP:
663                 case RVE_STRDIFF_OP:
664                 case RVE_MATCH_OP:
665                 case RVE_PLUS_OP:
666                 case RVE_IPLUS_OP:
667                 case RVE_CONCAT_OP:
668                         return 0;
669                 case RVE_NONE_OP:
670                         return -1;
671                         break;
672         }
673         return 0;
674 }
675
676
677
678 /** returns 1 if expression is valid (type-wise).
679   * @param type - filled with the type of the expression (RV_INT, RV_STR or
680   *                RV_NONE if it's dynamic)
681   * @param rve  - checked expression
682   * @param bad_rve - set on failure to the subexpression for which the 
683   *                    type check failed
684   * @param bad_type - set on failure to the type of the bad subexpression
685   * @param exp_type - set on failure to the expected type for the bad
686   *                   subexpression
687   * @return 0 or 1  and sets *type to the resulting type
688   * (RV_INT, RV_STR or RV_NONE if it can be found only at runtime)
689   */
690 int rve_check_type(enum rval_type* type, struct rval_expr* rve,
691                                         struct rval_expr** bad_rve, 
692                                         enum rval_type* bad_t,
693                                         enum rval_type* exp_t)
694 {
695         enum rval_type type1, type2;
696         
697         switch(rve->op){
698                 case RVE_RVAL_OP:
699                         *type=rve_guess_type(rve);
700                         return 1;
701                 case RVE_UMINUS_OP:
702                 case RVE_BOOL_OP:
703                 case RVE_LNOT_OP:
704                 case RVE_BNOT_OP:
705                         *type=RV_INT;
706                         if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
707                                 if (type1==RV_STR){
708                                         if (bad_rve) *bad_rve=rve->left.rve;
709                                         if (bad_t) *bad_t=type1;
710                                         if (exp_t) *exp_t=RV_INT;
711                                         return 0;
712                                 }
713                                 return 1;
714                         }
715                         return 0;
716                         break;
717                 case RVE_MINUS_OP:
718                 case RVE_MUL_OP:
719                 case RVE_DIV_OP:
720                 case RVE_MOD_OP:
721                 case RVE_BOR_OP:
722                 case RVE_BAND_OP:
723                 case RVE_BXOR_OP:
724                 case RVE_BLSHIFT_OP:
725                 case RVE_BRSHIFT_OP:
726                 case RVE_LAND_OP:
727                 case RVE_LOR_OP:
728                 case RVE_GT_OP:
729                 case RVE_GTE_OP:
730                 case RVE_LT_OP:
731                 case RVE_LTE_OP:
732                 case RVE_IEQ_OP:
733                 case RVE_IDIFF_OP:
734                 case RVE_IPLUS_OP:
735                         *type=RV_INT;
736                         if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
737                                 if (type1==RV_STR){
738                                         if (bad_rve) *bad_rve=rve->left.rve;
739                                         if (bad_t) *bad_t=type1;
740                                         if (exp_t) *exp_t=RV_INT;
741                                         return 0;
742                                 }
743                                 if (rve_check_type(&type2, rve->right.rve, bad_rve,
744                                                                         bad_t, exp_t)){
745                                         if (type2==RV_STR){
746                                                 if (bad_rve) *bad_rve=rve->right.rve;
747                                                 if (bad_t) *bad_t=type2;
748                                                 if (exp_t) *exp_t=RV_INT;
749                                                 return 0;
750                                         }
751                                         return 1;
752                                 }
753                         }
754                         return 0;
755                 case RVE_EQ_OP:
756                 case RVE_DIFF_OP:
757                         *type=RV_INT;
758                         if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
759                                 if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
760                                                                                 exp_t)){
761                                         if ((type2!=type1) && (type1!=RV_NONE) &&
762                                                         (type2!=RV_NONE) && 
763                                                         !(type1==RV_STR && type2==RV_INT)){
764                                                 if (bad_rve) *bad_rve=rve->right.rve;
765                                                 if (bad_t) *bad_t=type2;
766                                                 if (exp_t) *exp_t=type1;
767                                                 return 0;
768                                         }
769                                         return 1;
770                                 }
771                         }
772                         return 0;
773                 case RVE_PLUS_OP:
774                         *type=RV_NONE;
775                         if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
776                                 if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
777                                                                         exp_t)){
778                                         if ((type2!=type1) && (type1!=RV_NONE) &&
779                                                         (type2!=RV_NONE) && 
780                                                         !(type1==RV_STR && type2==RV_INT)){
781                                                 if (bad_rve) *bad_rve=rve->right.rve;
782                                                 if (bad_t) *bad_t=type2;
783                                                 if (exp_t) *exp_t=type1;
784                                                 return 0;
785                                         }
786                                         *type=type1;
787                                         return 1;
788                                 }
789                         }
790                         break;
791                 case RVE_CONCAT_OP:
792                         *type=RV_STR;
793                         if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
794                                 if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
795                                                                         exp_t)){
796                                         if ((type2!=type1) && (type1!=RV_NONE) &&
797                                                         (type2!=RV_NONE) && 
798                                                         !(type1==RV_STR && type2==RV_INT)){
799                                                 if (bad_rve) *bad_rve=rve->right.rve;
800                                                 if (bad_t) *bad_t=type2;
801                                                 if (exp_t) *exp_t=type1;
802                                                 return 0;
803                                         }
804                                         if (type1==RV_INT){
805                                                 if (bad_rve) *bad_rve=rve->left.rve;
806                                                 if (bad_t) *bad_t=type1;
807                                                 if (exp_t) *exp_t=RV_STR;
808                                                 return 0;
809                                         }
810                                         return 1;
811                                 }
812                         }
813                         break;
814                 case RVE_STREQ_OP:
815                 case RVE_STRDIFF_OP:
816                 case RVE_MATCH_OP:
817                         *type=RV_INT;
818                         if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
819                                 if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
820                                                                         exp_t)){
821                                         if ((type2!=type1) && (type1!=RV_NONE) &&
822                                                         (type2!=RV_NONE) &&
823                                                         !(type1==RV_STR && type2==RV_INT)){
824                                                 if (bad_rve) *bad_rve=rve->right.rve;
825                                                 if (bad_t) *bad_t=type2;
826                                                 if (exp_t) *exp_t=type1;
827                                                 return 0;
828                                         }
829                                         if (type1==RV_INT){
830                                                 if (bad_rve) *bad_rve=rve->left.rve;
831                                                 if (bad_t) *bad_t=type1;
832                                                 if (exp_t) *exp_t=RV_STR;
833                                                 return 0;
834                                         }
835                                         return 1;
836                                 }
837                         }
838                         break;
839                 case RVE_STRLEN_OP:
840                 case RVE_STREMPTY_OP:
841                 case RVE_DEFINED_OP:
842                         *type=RV_INT;
843                         if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
844                                 if (type1==RV_INT){
845                                         if (bad_rve) *bad_rve=rve->left.rve;
846                                         if (bad_t) *bad_t=type1;
847                                         if (exp_t) *exp_t=RV_STR;
848                                         return 0;
849                                 }
850                                 return 1;
851                         }
852                         break;
853                 case RVE_INT_OP:
854                         *type=RV_INT;
855                         return 1;
856                         break;
857                 case RVE_STR_OP:
858                         *type=RV_STR;
859                         return 1;
860                         break;
861                 case RVE_NONE_OP:
862                 default:
863                         BUG("unexpected rve op %d (%d,%d-%d,%d)\n", rve->op,
864                                         rve->fpos.s_line, rve->fpos.s_col,
865                                         rve->fpos.e_line, rve->fpos.e_col);
866                         if (bad_rve) *bad_rve=rve;
867                         if (bad_t) *bad_t=RV_NONE;
868                         if (exp_t) *exp_t=RV_STR;
869                         break;
870         }
871         return 0;
872 }
873
874
875
876 /** get the integer value of an rvalue.
877   * *i=(int)rv
878   * if rv == undefined select, avp or pvar, return 0.
879   * if an error occurs while evaluating a select, avp or pvar, behave as
880   * for the undefined case (and return success).
881   * @param h - script context handle
882   * @param msg - sip msg
883   * @param i   - pointer to int, where the conversion result will be stored
884   * @param rv   - rvalue to be converted
885   * @param cache - cached rv value (read-only), can be 0
886   *
887   * @return 0 on success, \<0 on error and EXPR_DROP on drop
888  */
889 int rval_get_int(struct run_act_ctx* h, struct sip_msg* msg,
890                                                                 int* i, struct rvalue* rv,
891                                                                 struct rval_cache* cache)
892 {
893         avp_t* r_avp;
894         int_str avp_val;
895         pv_value_t pval;
896         str tmp;
897         str* s;
898         int r, ret;
899         int destroy_pval;
900         
901         destroy_pval=0;
902         s=0;
903         ret=0;
904         switch(rv->type){
905                 case RV_INT:
906                         *i=rv->v.l;
907                         break;
908                 case RV_STR:
909                         s=&rv->v.s;
910                         goto rv_str;
911                 case RV_BEXPR:
912                         *i=eval_expr(h, rv->v.bexpr, msg);
913                         if (*i==EXPR_DROP){
914                                 *i=0; /* false */
915                                 return EXPR_DROP;
916                         }
917                         break;
918                 case RV_ACTION_ST:
919                         if (rv->v.action) {
920                                 *i=(run_actions_safe(h, rv->v.action, msg)>0);
921                                 h->run_flags &= ~(RETURN_R_F|BREAK_R_F); /* catch return &
922                                                                                                                     break in expr*/
923                         } else
924                                 *i=0;
925                         break;
926                 case RV_SEL:
927                         r=run_select(&tmp, &rv->v.sel, msg);
928                         if (unlikely(r!=0)){
929                                 if (r<0)
930                                         goto eval_error;
931                                 else /* i>0  => undefined */
932                                         goto undef;
933                         }
934                         s=&tmp;
935                         goto rv_str;
936                 case RV_AVP:
937                         if (unlikely(cache && cache->cache_type==RV_CACHE_AVP)){
938                                 if (likely(cache->val_type==RV_INT)){
939                                         *i=cache->c.avp_val.n;
940                                 }else if (cache->val_type==RV_STR){
941                                         s=&cache->c.avp_val.s;
942                                         goto rv_str;
943                                 }else if (cache->val_type==RV_NONE)
944                                         goto undef;
945                                 else goto error_cache;
946                         }else{
947                                 r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
948                                                                                         &avp_val, rv->v.avps.index);
949                                 if (likely(r_avp)){
950                                         if (unlikely(r_avp->flags & AVP_VAL_STR)){
951                                                 s=&avp_val.s;
952                                                 goto rv_str;
953                                         }else{
954                                                 *i=avp_val.n;
955                                         }
956                                 }else{
957                                         goto undef;
958                                 }
959                         }
960                         break;
961                 case RV_PVAR:
962                         if (unlikely(cache && cache->cache_type==RV_CACHE_PVAR)){
963                                 if (likely((cache->val_type==RV_INT) ||
964                                                                 (cache->c.pval.flags & PV_VAL_INT))){
965                                         *i=cache->c.pval.ri;
966                                 }else if (cache->val_type==RV_STR){
967                                         s=&cache->c.pval.rs;
968                                         goto rv_str;
969                                 }else if (cache->val_type==RV_NONE)
970                                         goto undef;
971                                 else goto error_cache;
972                         }else{
973                                 memset(&pval, 0, sizeof(pval));
974                                 if (likely(pv_get_spec_value(msg, &rv->v.pvs, &pval)==0)){
975                                         if (likely(pval.flags & PV_VAL_INT)){
976                                                 *i=pval.ri;
977                                                 pv_value_destroy(&pval);
978                                         }else if (likely(pval.flags & PV_VAL_STR)){
979                                                 destroy_pval=1; /* we must pv_value_destroy() later*/
980                                                 s=&pval.rs;
981                                                 goto rv_str;
982                                         }else{
983                                                 /* no PV_VAL_STR and no PV_VAL_INT => undef
984                                                    (PV_VAL_NULL) */
985                                                 pv_value_destroy(&pval);
986                                                 goto undef;
987                                         }
988                                 }else{
989                                         goto eval_error;
990                                 }
991                         }
992                         break;
993                 default:
994                         BUG("rv type %d not handled\n", rv->type);
995                         goto error;
996         }
997         return ret;
998 undef:
999 eval_error: /* same as undefined */
1000         /* handle undefined => result 0, return success */
1001         *i=0;
1002         return 0;
1003 rv_str:
1004         /* rv is of string type => try to convert it to int */
1005         /* if "" => 0 (most likely case) */
1006         if (likely(s->len==0)) *i=0;
1007         else if (unlikely(str2sint(s, i)!=0)){
1008                 /* error converting to int => non numeric => 0 */
1009                 *i=0;
1010 #ifdef RV_STR2INT_VERBOSE_ERR
1011                 WARN("automatic string to int conversion for \"%.*s\" failed\n",
1012                                 s->len, ZSW(s->s));
1013                 /* return an error code */
1014 #endif
1015 #ifdef RV_STR2INT_ERR
1016                 ret=-1;
1017 #endif
1018         }
1019         if (destroy_pval)
1020                 pv_value_destroy(&pval);
1021         return ret;
1022 error_cache:
1023         BUG("invalid cached value:cache type %d, value type %d\n",
1024                         cache?cache->cache_type:0, cache?cache->val_type:0);
1025 error:
1026         if (destroy_pval)
1027                 pv_value_destroy(&pval);
1028         *i=0;
1029         return -1;
1030 }
1031
1032
1033
1034 /** log a message, appending rve position and a '\n'.*/
1035 #define RVE_LOG(lev, rve, txt) \
1036         LOG((lev), txt " (%d,%d-%d,%d)\n", \
1037                         (rve)->fpos.s_line, rve->fpos.s_col, \
1038                         (rve)->fpos.e_line, rve->fpos.e_col )
1039
1040
1041 /** macro for checking and handling rval_get_int() retcode.
1042  * check if the return code is an rval_get_int error and if so
1043  * handle the error (e.g. print a log message, ignore the error by
1044  * setting ret to 0 a.s.o.)
1045  * @param ret - retcode as returned by rval_get_int() (might be changed)
1046  * @param txt - warning message txt (no pointer allowed)
1047  * @param rve - rval_expr, used to access the config. pos
1048  */
1049 #if defined RVAL_GET_INT_ERR_WARN && defined RVAL_GET_INT_ERR_IGN
1050 #define rval_get_int_handle_ret(ret, txt, rve) \
1051         do { \
1052                 if (unlikely((ret)<0)) { \
1053                         RVE_LOG(L_WARN, rve, txt); \
1054                         (ret)=0; \
1055                 } \
1056         }while(0)
1057 #elif defined RVAL_GET_INT_ERR_WARN
1058 #define rval_get_int_handle_ret(ret, txt, rve) \
1059         do { \
1060                 if (unlikely((ret)<0)) \
1061                         RVE_LOG(L_WARN, rve, txt); \
1062         }while(0)
1063 #elif defined RVAL_GET_INT_ERR_IGN
1064 #define rval_get_int_handle_ret(ret, txt, rve) \
1065         do { \
1066                 if (unlikely((ret)<0)) \
1067                                 (ret)=0; \
1068         } while(0)
1069 #else
1070 #define rval_get_int_handle_ret(ret, txt, rve) /* do nothing */
1071 #endif
1072
1073
1074
1075
1076
1077
1078 /** get the string value of an rv in a tmp variable
1079   * *s=(str)rv
1080   * if rv == undefined select, avp or pvar, return "".
1081   * if an error occurs while evaluating a select, avp or pvar, behave as
1082   * for the undefined case (and return success).
1083   * The result points either inside the passed rv, inside
1084   * new_cache or inside an avp. new_cache must be non zero,
1085   * initialized previously and it _must_ be rval_cache_clean(...)'ed when
1086   * done.
1087   * WARNING: it's not intended for general use. It might return a pointer
1088   * inside rv so the result _must_ be treated as read-only. rv and new_cache
1089   * must not be released/freed until the result is no longer needed.
1090   * For general use see  rval_get_str().
1091   * @param h - script context handle
1092   * @param msg - sip msg
1093   * @param tmpv - str return value (pointer to a str struct that will be
1094   *               be filled with the conversion result)
1095   * @param rv   - rvalue to be converted
1096   * @param cache - cached rv value (read-only), can be 0
1097   * @param tmp_cache - used for temporary storage (so that tmpv will not
1098   *                 point to possible freed data), it must be non-null,
1099   *                 initialized and cleaned afterwards.
1100   * @return 0 on success, <0 on error and EXPR_DROP on drop
1101  */
1102 int rval_get_tmp_str(struct run_act_ctx* h, struct sip_msg* msg,
1103                                                                 str* tmpv, struct rvalue* rv,
1104                                                                 struct rval_cache* cache,
1105                                                                 struct rval_cache* tmp_cache)
1106 {
1107         avp_t* r_avp;
1108         int i;
1109         
1110         switch(rv->type){
1111                 case RV_INT:
1112                         tmpv->s=sint2strbuf(rv->v.l, tmp_cache->i2s,
1113                                                                 sizeof(tmp_cache->i2s), &tmpv->len);
1114                         tmp_cache->cache_type = RV_CACHE_INT2STR;
1115                         break;
1116                 case RV_STR:
1117                         *tmpv=rv->v.s;
1118                         break;
1119                 case RV_ACTION_ST:
1120                         if (rv->v.action) {
1121                                 i=(run_actions_safe(h, rv->v.action, msg)>0);
1122                                 h->run_flags &= ~(RETURN_R_F|BREAK_R_F); /* catch return &
1123                                                                                                                     break in expr*/
1124                         } else
1125                                 i=0;
1126                         tmpv->s=sint2strbuf(i, tmp_cache->i2s,
1127                                                                 sizeof(tmp_cache->i2s), &tmpv->len);
1128                         tmp_cache->cache_type = RV_CACHE_INT2STR;
1129                         break;
1130                 case RV_BEXPR:
1131                         i=eval_expr(h, rv->v.bexpr, msg);
1132                         if (i==EXPR_DROP){
1133                                 i=0; /* false */
1134                                 tmpv->s=sint2strbuf(i, tmp_cache->i2s,
1135                                                 sizeof(tmp_cache->i2s), &tmpv->len);
1136                                 tmp_cache->cache_type = RV_CACHE_INT2STR;
1137                                 return EXPR_DROP;
1138                         }
1139                         tmpv->s=sint2strbuf(i, tmp_cache->i2s, sizeof(tmp_cache->i2s),
1140                                                                 &tmpv->len);
1141                         tmp_cache->cache_type = RV_CACHE_INT2STR;
1142                         break;
1143                 case RV_SEL:
1144                         i=run_select(tmpv, &rv->v.sel, msg);
1145                         if (unlikely(i!=0)){
1146                                 if (i<0){
1147                                         goto eval_error;
1148                                 }else { /* i>0  => undefined */
1149                                         goto undef;
1150                                 }
1151                         }
1152                         break;
1153                 case RV_AVP:
1154                         if (likely(cache && cache->cache_type==RV_CACHE_AVP)){
1155                                 if (likely(cache->val_type==RV_STR)){
1156                                         *tmpv=cache->c.avp_val.s;
1157                                 }else if (cache->val_type==RV_INT){
1158                                         i=cache->c.avp_val.n;
1159                                         tmpv->s=sint2strbuf(i, tmp_cache->i2s,
1160                                                                                 sizeof(tmp_cache->i2s), &tmpv->len);
1161                                         tmp_cache->cache_type = RV_CACHE_INT2STR;
1162                                 }else if (cache->val_type==RV_NONE){
1163                                         goto undef;
1164                                 }else goto error_cache;
1165                         }else{
1166                                 r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
1167                                                                                         &tmp_cache->c.avp_val,
1168                                                                                         rv->v.avps.index);
1169                                 if (likely(r_avp)){
1170                                         if (likely(r_avp->flags & AVP_VAL_STR)){
1171                                                 tmp_cache->cache_type=RV_CACHE_AVP;
1172                                                 tmp_cache->val_type=RV_STR;
1173                                                 *tmpv=tmp_cache->c.avp_val.s;
1174                                         }else{
1175                                                 i=tmp_cache->c.avp_val.n;
1176                                                 tmpv->s=sint2strbuf(i, tmp_cache->i2s,
1177                                                                                 sizeof(tmp_cache->i2s), &tmpv->len);
1178                                                 tmp_cache->cache_type = RV_CACHE_INT2STR;
1179                                         }
1180                                 }else goto undef;
1181                         }
1182                         break;
1183                 case RV_PVAR:
1184                         if (likely(cache && cache->cache_type==RV_CACHE_PVAR)){
1185                                 if (likely(cache->val_type==RV_STR)){
1186                                         *tmpv=cache->c.pval.rs;
1187                                 }else if (cache->val_type==RV_INT){
1188                                         i=cache->c.pval.ri;
1189                                         tmpv->s=sint2strbuf(i, tmp_cache->i2s,
1190                                                                                 sizeof(tmp_cache->i2s), &tmpv->len);
1191                                         tmp_cache->cache_type = RV_CACHE_INT2STR;
1192                                 }else if (cache->val_type==RV_NONE){
1193                                         goto undef;
1194                                 }else goto error_cache;
1195                         }else{
1196                                 memset(&tmp_cache->c.pval, 0, sizeof(tmp_cache->c.pval));
1197                                 if (likely(pv_get_spec_value(msg, &rv->v.pvs,
1198                                                                                                 &tmp_cache->c.pval)==0)){
1199                                         if (likely(tmp_cache->c.pval.flags & PV_VAL_STR)){
1200                                                 /*  the value is not destroyed, but saved instead
1201                                                         in tmp_cache so that it can be destroyed later
1202                                                         when no longer needed */
1203                                                 tmp_cache->cache_type=RV_CACHE_PVAR;
1204                                                 tmp_cache->val_type=RV_STR;
1205                                                 *tmpv=tmp_cache->c.pval.rs;
1206                                         }else if (likely(tmp_cache->c.pval.flags & PV_VAL_INT)){
1207                                                 i=tmp_cache->c.pval.ri;
1208                                                 pv_value_destroy(&tmp_cache->c.pval);
1209                                                 tmpv->s=sint2strbuf(i, tmp_cache->i2s,
1210                                                                                 sizeof(tmp_cache->i2s), &tmpv->len);
1211                                                 tmp_cache->cache_type = RV_CACHE_INT2STR;
1212                                         }else{
1213                                                 /* no PV_VAL_STR and no PV_VAL_INT => undef
1214                                                    (PV_VAL_NULL) */
1215                                                 pv_value_destroy(&tmp_cache->c.pval);
1216                                                 goto undef;
1217                                         }
1218                                 }else{
1219                                         goto eval_error;
1220                                 }
1221                         }
1222                         break;
1223                 default:
1224                         BUG("rv type %d not handled\n", rv->type);
1225                         goto error;
1226         }
1227         return 0;
1228 undef:
1229 eval_error: /* same as undefined */
1230         /* handle undefined => result "", return success */
1231         tmpv->s="";
1232         tmpv->len=0;
1233         return 0;
1234 error_cache:
1235         BUG("invalid cached value:cache type %d, value type %d\n",
1236                         cache?cache->cache_type:0, cache?cache->val_type:0);
1237 error:
1238         tmpv->s="";
1239         tmpv->len=0;
1240         return -1;
1241 }
1242
1243
1244
1245 /** get the string value of an rv.
1246   * *s=(str)rv
1247   * The result is pkg malloc'ed (so it should be pkg_free()'ed when finished.
1248   * @return 0 on success, <0 on error and EXPR_DROP on drop
1249  */
1250 int rval_get_str(struct run_act_ctx* h, struct sip_msg* msg,
1251                                                                 str* s, struct rvalue* rv,
1252                                                                 struct rval_cache* cache)
1253 {
1254         str tmp;
1255         struct rval_cache tmp_cache;
1256         
1257         rval_cache_init(&tmp_cache);
1258         if (unlikely(rval_get_tmp_str(h, msg, &tmp, rv, cache, &tmp_cache)<0))
1259                 goto error;
1260         s->s=pkg_malloc(tmp.len+1/* 0 term */);
1261         if (unlikely(s->s==0)){
1262                 ERR("memory allocation error\n");
1263                 goto error;
1264         }
1265         s->len=tmp.len;
1266         memcpy(s->s, tmp.s, tmp.len);
1267         s->s[tmp.len]=0; /* 0 term */
1268         rval_cache_clean(&tmp_cache);
1269         return 0;
1270 error:
1271         rval_cache_clean(&tmp_cache);
1272         return -1;
1273 }
1274
1275
1276
1277 /** convert a rvalue to another rvalue, of a specific type.
1278  *
1279  * The result is read-only in most cases (can be a reference
1280  * to another rvalue, can be checked by using rv_chg_in_place()) and
1281  * _must_ be rval_destroy()'ed.
1282  *
1283  * @param type - type to convert to
1284  * @param v - rvalue to convert
1285  * @param c - rval_cache (cached v value if known/filled by another
1286  *            function), can be 0 (unknown/not needed)
1287  * @return pointer to a rvalue (reference to an existing one or a new
1288  *   one, @see rv_chg_in_place() and the above comment) or 0 on error.
1289  */
1290 struct rvalue* rval_convert(struct run_act_ctx* h, struct sip_msg* msg,
1291                                                         enum rval_type type, struct rvalue* v,
1292                                                         struct rval_cache* c)
1293 {
1294         int i;
1295         struct rval_cache tmp_cache;
1296         str tmp;
1297         struct rvalue* ret;
1298         union rval_val val;
1299         
1300         if (v->type==type){
1301                 rv_ref(v);
1302                 return v;
1303         }
1304         switch(type){
1305                 case RV_INT:
1306                         if (unlikely(rval_get_int(h, msg, &i, v, c) < 0))
1307                                 return 0;
1308                         val.l=i;
1309                         return rval_new(RV_INT, &val, 0);
1310                 case RV_STR:
1311                         rval_cache_init(&tmp_cache);
1312                         if (unlikely(rval_get_tmp_str(h, msg, &tmp, v, c, &tmp_cache) < 0))
1313                         {
1314                                 rval_cache_clean(&tmp_cache);
1315                                 return 0;
1316                         }
1317                         ret=rval_new_str(&tmp, RV_STR_EXTRA);
1318                         rval_cache_clean(&tmp_cache);
1319                         return ret;
1320                 case RV_NONE:
1321                 default:
1322                         BUG("unsupported conversion to type %d\n", type);
1323                         return 0;
1324         }
1325         return 0;
1326 }
1327
1328
1329
1330 /** integer operation: *res= op v.
1331   * @return 0 on succes, \<0 on error
1332   */
1333 inline static int int_intop1(int* res, enum rval_expr_op op, int v)
1334 {
1335         switch(op){
1336                 case RVE_UMINUS_OP:
1337                         *res=-v;
1338                         break;
1339                 case RVE_BOOL_OP:
1340                         *res=!!v;
1341                         break;
1342                 case RVE_LNOT_OP:
1343                         *res=!v;
1344                         break;
1345                 case RVE_BNOT_OP:
1346                         *res=~v;
1347                         break;
1348                 default:
1349                         BUG("rv unsupported intop1 %d\n", op);
1350                         return -1;
1351         }
1352         return 0;
1353 }
1354
1355
1356
1357 /** integer operation: *res= v1 op v2
1358   * @return 0 on succes, \<0 on error
1359   */
1360 inline static int int_intop2(int* res, enum rval_expr_op op, int v1, int v2)
1361 {
1362         switch(op){
1363                 case RVE_PLUS_OP:
1364                 case RVE_IPLUS_OP:
1365                         *res=v1+v2;
1366                         break;
1367                 case RVE_MINUS_OP:
1368                         *res=v1-v2;
1369                         break;
1370                 case RVE_MUL_OP:
1371                         *res=v1*v2;
1372                         break;
1373                 case RVE_DIV_OP:
1374                         if (unlikely(v2==0)){
1375                                 ERR("rv div by 0\n");
1376                                 return -1;
1377                         }
1378                         *res=v1/v2;
1379                         break;
1380                 case RVE_MOD_OP:
1381                         if (unlikely(v2==0)){
1382                                 ERR("rv mod by 0\n");
1383                                 return -1;
1384                         }
1385                         *res=v1%v2;
1386                         break;
1387                 case RVE_BOR_OP:
1388                         *res=v1|v2;
1389                         break;
1390                 case RVE_BAND_OP:
1391                         *res=v1&v2;
1392                         break;
1393                 case RVE_BXOR_OP:
1394                         *res=v1^v2;
1395                         break;
1396                 case RVE_BLSHIFT_OP:
1397                         *res=v1<<v2;
1398                         break;
1399                 case RVE_BRSHIFT_OP:
1400                         *res=v1>>v2;
1401                         break;
1402                 case RVE_LAND_OP:
1403                         *res=v1 && v2;
1404                         break;
1405                 case RVE_LOR_OP:
1406                         *res=v1 || v2;
1407                         break;
1408                 case RVE_GT_OP:
1409                         *res=v1 > v2;
1410                         break;
1411                 case RVE_GTE_OP:
1412                         *res=v1 >= v2;
1413                         break;
1414                 case RVE_LT_OP:
1415                         *res=v1 < v2;
1416                         break;
1417                 case RVE_LTE_OP:
1418                         *res=v1 <= v2;
1419                         break;
1420                 case RVE_EQ_OP:
1421                 case RVE_IEQ_OP:
1422                         *res=v1 == v2;
1423                         break;
1424                 case RVE_DIFF_OP:
1425                 case RVE_IDIFF_OP:
1426                         *res=v1 != v2;
1427                         break;
1428                 case RVE_CONCAT_OP:
1429                         *res=0;
1430                         /* invalid operand for int */
1431                         return -1;
1432                 default:
1433                         BUG("rv unsupported intop %d\n", op);
1434                         return -1;
1435         }
1436         return 0;
1437 }
1438
1439
1440
1441 /** internal helper: compare 2 RV_STR RVs.
1442   * Warning: rv1 & rv2 must be RV_STR
1443   * @return 0 on success, -1 on error
1444   */
1445 inline static int bool_rvstrop2( enum rval_expr_op op, int* res,
1446                                                                 struct rvalue* rv1, struct rvalue* rv2)
1447 {
1448         str* s1;
1449         str* s2;
1450         regex_t tmp_re;
1451         
1452         s1=&rv1->v.s;
1453         s2=&rv2->v.s;
1454         switch(op){
1455                 case RVE_EQ_OP:
1456                 case RVE_STREQ_OP:
1457                         *res= (s1->len==s2->len) && (memcmp(s1->s, s2->s, s1->len)==0);
1458                         break;
1459                 case RVE_DIFF_OP:
1460                 case RVE_STRDIFF_OP:
1461                         *res= (s1->len!=s2->len) || (memcmp(s1->s, s2->s, s1->len)!=0);
1462                         break;
1463                 case RVE_MATCH_OP:
1464                         if (likely(rv2->flags & RV_RE_F)){
1465                                 *res=(regexec(rv2->v.re.regex, rv1->v.s.s, 0, 0, 0)==0);
1466                         }else{
1467                                 /* we need to compile the RE on the fly */
1468                                 if (unlikely(regcomp(&tmp_re, s2->s,
1469                                                                                 REG_EXTENDED|REG_NOSUB|REG_ICASE))){
1470                                         /* error */
1471                                         ERR("Bad regular expression \"%s\"\n", s2->s);
1472                                         goto error;
1473                                 }
1474                                 *res=(regexec(&tmp_re, s1->s, 0, 0, 0)==0);
1475                                 regfree(&tmp_re);
1476                         }
1477                         break;
1478                 default:
1479                         BUG("rv unsupported intop %d\n", op);
1480                         goto error;
1481         }
1482         return 0;
1483 error:
1484         *res=0; /* false */
1485         return -1;
1486 }
1487
1488
1489
1490 /** integer returning operation on string: *res= op str (returns integer)
1491   * @return 0 on succes, \<0 on error
1492   */
1493 inline static int int_strop1(int* res, enum rval_expr_op op, str* s1)
1494 {
1495         switch(op){
1496                 case RVE_STRLEN_OP:
1497                         *res=s1->len;
1498                         break;
1499                 case RVE_STREMPTY_OP:
1500                         *res=(s1->len==0);
1501                         break;
1502                 default:
1503                         BUG("rv unsupported int_strop1 %d\n", op);
1504                         *res=0;
1505                         return -1;
1506         }
1507         return 0;
1508 }
1509
1510
1511
1512 /** integer operation: ret= op v (returns a rvalue).
1513  * @return rvalue on success, 0 on error
1514  */
1515 inline static struct rvalue* rval_intop1(struct run_act_ctx* h,
1516                                                                                         struct sip_msg* msg,
1517                                                                                         enum rval_expr_op op,
1518                                                                                         struct rvalue* v)
1519 {
1520         struct rvalue* rv2;
1521         struct rvalue* ret;
1522         int i;
1523         
1524         i=0;
1525         rv2=rval_convert(h, msg, RV_INT, v, 0);
1526         if (unlikely(rv2==0)){
1527                 ERR("rval int conversion failed\n");
1528                 goto error;
1529         }
1530         if (unlikely(int_intop1(&i, op, rv2->v.l)<0))
1531                 goto error;
1532         if (rv_chg_in_place(rv2)){
1533                 ret=rv2;
1534                 rv_ref(ret);
1535         }else if (rv_chg_in_place(v)){
1536                 ret=v;
1537                 rv_ref(ret);
1538         }else{
1539                 ret=rval_new(RV_INT, &rv2->v, 0);
1540                 if (unlikely(ret==0)){
1541                         ERR("eval out of memory\n");
1542                         goto error;
1543                 }
1544         }
1545         rval_destroy(rv2);
1546         ret->v.l=i;
1547         return ret;
1548 error:
1549         rval_destroy(rv2);
1550         return 0;
1551 }
1552
1553
1554
1555 /** integer operation: ret= l op r (returns a rvalue).
1556  * @return rvalue on success, 0 on error
1557  */
1558 inline static struct rvalue* rval_intop2(struct run_act_ctx* h,
1559                                                                                         struct sip_msg* msg,
1560                                                                                         enum rval_expr_op op,
1561                                                                                         struct rvalue* l,
1562                                                                                         struct rvalue* r)
1563 {
1564         struct rvalue* rv1;
1565         struct rvalue* rv2;
1566         struct rvalue* ret;
1567         int i;
1568
1569         rv2=rv1=0;
1570         ret=0;
1571         if ((rv1=rval_convert(h, msg, RV_INT, l, 0))==0)
1572                 goto error;
1573         if ((rv2=rval_convert(h, msg, RV_INT, r, 0))==0)
1574                 goto error;
1575         if (unlikely(int_intop2(&i, op, rv1->v.l, rv2->v.l)<0))
1576                 goto error;
1577         if (rv_chg_in_place(rv1)){
1578                 /* try reusing rv1 */
1579                 ret=rv1;
1580                 rv_ref(ret);
1581         }else if (rv_chg_in_place(rv2)){
1582                 /* try reusing rv2 */
1583                 ret=rv2;
1584                 rv_ref(ret);
1585         }else if ((l->type==RV_INT) && (rv_chg_in_place(l))){
1586                 ret=l;
1587                 rv_ref(ret);
1588         } else if ((r->type==RV_INT) && (rv_chg_in_place(r))){
1589                 ret=r;
1590                 rv_ref(ret);
1591         }else{
1592                 ret=rval_new(RV_INT, &rv1->v, 0);
1593                 if (unlikely(ret==0)){
1594                         ERR("rv eval out of memory\n");
1595                         goto error;
1596                 }
1597         }
1598         rval_destroy(rv1); 
1599         rval_destroy(rv2); 
1600         ret->v.l=i;
1601         return ret;
1602 error:
1603         rval_destroy(rv1); 
1604         rval_destroy(rv2); 
1605         return 0;
1606 }
1607
1608
1609
1610 /** string add operation: ret= l . r (returns a rvalue).
1611  * Can use cached rvalues (c1 & c2).
1612  * @return rvalue on success, 0 on error
1613  */
1614 inline static struct rvalue* rval_str_add2(struct run_act_ctx* h,
1615                                                                                         struct sip_msg* msg,
1616                                                                                         struct rvalue* l,
1617                                                                                         struct rval_cache* c1,
1618                                                                                         struct rvalue* r,
1619                                                                                         struct rval_cache* c2
1620                                                                                         )
1621 {
1622         struct rvalue* rv1;
1623         struct rvalue* rv2;
1624         struct rvalue* ret;
1625         str* s1;
1626         str* s2;
1627         str tmp;
1628         short flags;
1629         int len;
1630         
1631         rv2=rv1=0;
1632         ret=0;
1633         flags=0;
1634         s1=0;
1635         s2=0;
1636         if ((rv1=rval_convert(h, msg, RV_STR, l, c1))==0)
1637                 goto error;
1638         if ((rv2=rval_convert(h, msg, RV_STR, r, c2))==0)
1639                 goto error;
1640         
1641         len=rv1->v.s.len + rv2->v.s.len + 1 /* 0 */;
1642         
1643         if (rv_chg_in_place(rv1) && (rv1->bsize>=len)){
1644                 /* try reusing rv1 */
1645                 ret=rv1;
1646                 rv_ref(ret);
1647                 s2=&rv2->v.s;
1648                 if (ret->v.s.s == &ret->buf[0]) s1=0;
1649                 else{
1650                         tmp=ret->v.s;
1651                         flags=ret->flags;
1652                         ret->flags &= ~RV_CNT_ALLOCED_F;
1653                         ret->v.s.s=&ret->buf[0];
1654                         ret->v.s.len=0;
1655                         s1=&tmp;
1656                 }
1657         }else if (rv_chg_in_place(rv2) && (rv2->bsize>=len)){
1658                 /* try reusing rv2 */
1659                 ret=rv2;
1660                 rv_ref(ret);
1661                 s1=&rv1->v.s;
1662                 if (ret->v.s.s == &ret->buf[0]) 
1663                         s2=&ret->v.s;
1664                 else{
1665                         tmp=ret->v.s;
1666                         flags=ret->flags;
1667                         ret->flags &= ~RV_CNT_ALLOCED_F;
1668                         ret->v.s.s=&ret->buf[0];
1669                         ret->v.s.len=0;
1670                         s2=&tmp;
1671                 }
1672         }else if ((l->type==RV_STR) && (rv_chg_in_place(l)) && (l->bsize>=len)){
1673                 ret=l;
1674                 rv_ref(ret);
1675                 s2=&rv2->v.s;
1676                 if (ret->v.s.s == &ret->buf[0]) s1=0;
1677                 else{
1678                         tmp=ret->v.s;
1679                         flags=ret->flags;
1680                         ret->flags &= ~RV_CNT_ALLOCED_F;
1681                         ret->v.s.s=&ret->buf[0];
1682                         ret->v.s.len=0;
1683                         s1=&tmp;
1684                 }
1685         } else if ((r->type==RV_STR) && (rv_chg_in_place(r) && (r->bsize>=len))){
1686                 ret=r;
1687                 rv_ref(ret);
1688                 s1=&rv1->v.s;
1689                 if (ret->v.s.s == &ret->buf[0]) 
1690                         s2=&ret->v.s;
1691                 else{
1692                         tmp=ret->v.s;
1693                         flags=ret->flags;
1694                         ret->flags &= ~RV_CNT_ALLOCED_F;
1695                         ret->v.s.s=&ret->buf[0];
1696                         ret->v.s.len=0;
1697                         s2=&tmp;
1698                 }
1699         }else{
1700                 ret=rval_new(RV_STR, &rv1->v, len + RV_STR_EXTRA);
1701                 if (unlikely(ret==0)){
1702                         ERR("rv eval out of memory\n");
1703                         goto error;
1704                 }
1705                 s1=0;
1706                 s2=&rv2->v.s;
1707         }
1708         /* do the actual copy */
1709         memmove(ret->buf+rv1->v.s.len, s2->s, s2->len);
1710         if (s1){
1711                 memcpy(ret->buf, s1->s, s1->len);
1712         }
1713         ret->v.s.len=rv1->v.s.len+s2->len;
1714         ret->v.s.s[ret->v.s.len]=0;
1715         /* cleanup if needed */
1716         if (flags & RV_CNT_ALLOCED_F)
1717                 pkg_free(tmp.s);
1718         rval_destroy(rv1); 
1719         rval_destroy(rv2); 
1720         return ret;
1721 error:
1722         rval_destroy(rv1); 
1723         rval_destroy(rv2); 
1724         return 0;
1725 }
1726
1727
1728
1729 /** bool operation on rval evaluated as strings.
1730  * Can use cached rvalues (c1 & c2).
1731  * @return 0 success, -1 on error
1732  */
1733 inline static int rval_str_lop2(struct run_act_ctx* h,
1734                                                  struct sip_msg* msg,
1735                                                  int* res,
1736                                                  enum rval_expr_op op,
1737                                                  struct rvalue* l,
1738                                                  struct rval_cache* c1,
1739                                                  struct rvalue* r,
1740                                                  struct rval_cache* c2)
1741 {
1742         struct rvalue* rv1;
1743         struct rvalue* rv2;
1744         int ret;
1745         
1746         rv2=rv1=0;
1747         ret=0;
1748         if ((rv1=rval_convert(h, msg, RV_STR, l, c1))==0)
1749                 goto error;
1750         if ((rv2=rval_convert(h, msg, RV_STR, r, c2))==0)
1751                 goto error;
1752         ret=bool_rvstrop2(op, res, rv1, rv2);
1753         rval_destroy(rv1); 
1754         rval_destroy(rv2); 
1755         return ret;
1756 error:
1757         rval_destroy(rv1); 
1758         rval_destroy(rv2); 
1759         return 0;
1760 }
1761
1762
1763
1764 /** integer operation on rval evaluated as string.
1765  * Can use cached rvalues (c1 & c2).
1766  * @param res - will be set to the result
1767  * @return 0 success, -1 on error
1768  */
1769 inline static int rval_int_strop1(struct run_act_ctx* h,
1770                                                  struct sip_msg* msg,
1771                                                  int* res,
1772                                                  enum rval_expr_op op,
1773                                                  struct rvalue* l,
1774                                                  struct rval_cache* c1)
1775 {
1776         struct rvalue* rv1;
1777         int ret;
1778         
1779         rv1=0;
1780         ret=0;
1781         if ((rv1=rval_convert(h, msg, RV_STR, l, c1))==0)
1782                 goto error;
1783         ret=int_strop1(res, op, &rv1->v.s);
1784         rval_destroy(rv1); 
1785         return ret;
1786 error:
1787         *res=0;
1788         rval_destroy(rv1); 
1789         return -1;
1790 }
1791
1792
1793
1794 /** checks if rv is defined.
1795  * @param res - set to the result 1 - defined, 0 not defined
1796  * @return 0 on success, -1 on error
1797  * Can use cached rvalues (cache).
1798  * Note: a rv can be undefined if it's an undefined avp or pvar or select or
1799  * if it's NONE
1800  * Note2: an error in the avp, pvar or select search is equivalent to 
1801  *  undefined (and it's not reported)
1802  */
1803 inline static int rv_defined(struct run_act_ctx* h,
1804                                                  struct sip_msg* msg, int* res,
1805                                                  struct rvalue* rv, struct rval_cache* cache)
1806 {
1807         avp_t* r_avp;
1808         int_str avp_val;
1809         pv_value_t pval;
1810         str tmp;
1811         
1812         *res=1;
1813         switch(rv->type){
1814                 case RV_SEL:
1815                         if (unlikely(cache && cache->cache_type==RV_CACHE_SELECT)){
1816                                 *res=(cache->val_type!=RV_NONE);
1817                         }else
1818                                 /* run select returns 0 on success, -1 on error and >0 on 
1819                                    undefined. error is considered undefined */
1820                                 *res=(run_select(&tmp, &rv->v.sel, msg)==0);
1821                         break;
1822                 case RV_AVP:
1823                         if (unlikely(cache && cache->cache_type==RV_CACHE_AVP)){
1824                                 *res=(cache->val_type!=RV_NONE);
1825                         }else{
1826                                 r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
1827                                                                                         &avp_val, rv->v.avps.index);
1828                                 if (unlikely(r_avp==0)){
1829                                         *res=0;
1830                                 }
1831                         }
1832                         break;
1833                 case RV_PVAR:
1834                         /* PV_VAL_NULL or pv_get_spec_value error => undef */
1835                         if (unlikely(cache && cache->cache_type==RV_CACHE_PVAR)){
1836                                 *res=(cache->val_type!=RV_NONE);
1837                         }else{
1838                                 memset(&pval, 0, sizeof(pval));
1839                                 if (likely(pv_get_spec_value(msg, &rv->v.pvs, &pval)==0)){
1840                                         if ((pval.flags & PV_VAL_NULL) &&
1841                                                         ! (pval.flags & (PV_VAL_INT|PV_VAL_STR))){
1842                                                 *res=0;
1843                                         }
1844                                         pv_value_destroy(&pval);
1845                                 }else{
1846                                         *res=0; /* in case of error, consider it undef */
1847                                 }
1848                         }
1849                         break;
1850                 case RV_NONE:
1851                         *res=0;
1852                         break;
1853                 default:
1854                         break;
1855         }
1856         return 0;
1857 }
1858
1859
1860 /** defined (integer) operation on rve.
1861  * @param res - set to  1 defined, 0 not defined
1862  * @return - 0 on success, -1 on error
1863  */
1864 inline static int int_rve_defined(struct run_act_ctx* h,
1865                                                  struct sip_msg* msg, int* res,
1866                                                  struct rval_expr* rve)
1867 {
1868         /* only a rval can be undefined, any expression consisting on more
1869            then one rval => defined */
1870         if (likely(rve->op==RVE_RVAL_OP))
1871                 return rv_defined(h, msg, res, &rve->left.rval, 0);
1872         *res=1;
1873         return 0;
1874 }
1875
1876
1877
1878 /** evals an integer expr  to an int.
1879  * 
1880  *  *res=(int)eval(rve)
1881  *  @return 0 on success, \<0 on error
1882  */
1883 int rval_expr_eval_int( struct run_act_ctx* h, struct sip_msg* msg,
1884                                                 int* res, struct rval_expr* rve)
1885 {
1886         int i1, i2, ret;
1887         struct rval_cache c1, c2;
1888         struct rvalue* rv1;
1889         struct rvalue* rv2;
1890         
1891         ret=-1;
1892         switch(rve->op){
1893                 case RVE_RVAL_OP:
1894                         ret=rval_get_int(h, msg, res,  &rve->left.rval, 0);
1895                         rval_get_int_handle_ret(ret, "rval expression conversion to int"
1896                                                                                 " failed", rve);
1897                         break;
1898                 case RVE_UMINUS_OP:
1899                 case RVE_BOOL_OP:
1900                 case RVE_LNOT_OP:
1901                 case RVE_BNOT_OP:
1902                         if (unlikely(
1903                                         (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1904                                 break;
1905                         ret=int_intop1(res, rve->op, i1);
1906                         break;
1907                 case RVE_INT_OP:
1908                         ret=rval_expr_eval_int(h, msg, res, rve->left.rve);
1909                         break;
1910                 case RVE_MUL_OP:
1911                 case RVE_DIV_OP:
1912                 case RVE_MOD_OP:
1913                 case RVE_MINUS_OP:
1914                 case RVE_PLUS_OP:
1915                 case RVE_IPLUS_OP:
1916                 case RVE_BOR_OP:
1917                 case RVE_BAND_OP:
1918                 case RVE_BXOR_OP:
1919                 case RVE_BLSHIFT_OP:
1920                 case RVE_BRSHIFT_OP:
1921                 case RVE_GT_OP:
1922                 case RVE_GTE_OP:
1923                 case RVE_LT_OP:
1924                 case RVE_LTE_OP:
1925                 case RVE_IEQ_OP:
1926                 case RVE_IDIFF_OP:
1927                         if (unlikely(
1928                                         (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1929                                 break;
1930                         if (unlikely(
1931                                         (ret=rval_expr_eval_int(h, msg, &i2, rve->right.rve)) <0) )
1932                                 break;
1933                         ret=int_intop2(res, rve->op, i1, i2);
1934                         break;
1935                 case RVE_LAND_OP:
1936                         if (unlikely(
1937                                         (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1938                                 break;
1939                         if (i1==0){
1940                                 *res=0;
1941                         }else{
1942                                 if (unlikely( (ret=rval_expr_eval_int(h, msg, &i2,
1943                                                                                 rve->right.rve)) <0) )
1944                                         break;
1945                                 *res=i1 && i2;
1946                         }
1947                         ret=0;
1948                         break;
1949                 case RVE_LOR_OP:
1950                         if (unlikely(
1951                                         (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1952                                 break;
1953                         if (i1){
1954                                 *res=1;
1955                         }else{
1956                                 if (unlikely( (ret=rval_expr_eval_int(h, msg, &i2,
1957                                                                                 rve->right.rve)) <0) )
1958                                         break;
1959                                 *res=i1 || i2;
1960                         }
1961                         ret=0;
1962                         break;
1963                 case RVE_EQ_OP:
1964                 case RVE_DIFF_OP:
1965                         /* if left is string, eval left & right as string and
1966                          *   use string diff.
1967                          * if left is int eval as int using int diff
1968                          * if left is undef, look at right and convert to right type
1969                          */
1970                         rval_cache_init(&c1);
1971                         if (unlikely( (ret=rval_expr_eval_rvint(h, msg, &rv1, &i1,
1972                                                                                                         rve->left.rve, &c1))<0)){
1973                                 /* error */
1974                                 rval_cache_clean(&c1);
1975                                 break;
1976                         }
1977                         if (likely(rv1==0)){
1978                                 /* int */
1979                                 rval_cache_clean(&c1);
1980                                 if (unlikely( (ret=rval_expr_eval_int(h, msg, &i2,
1981                                                                                                                 rve->right.rve)) <0) )
1982                                         break;  /* error */
1983                                 ret=int_intop2(res, rve->op, i1, i2);
1984                         }else{
1985                                 /* not int => str or undef */
1986                                 /* check for undefined left operand */
1987                                 if (unlikely( c1.cache_type!=RV_CACHE_EMPTY &&
1988                                                                 c1.val_type==RV_NONE)){
1989 #ifdef UNDEF_EQ_ALWAYS_FALSE
1990                                         /* undef == something  always false
1991                                            undef != something  always true*/
1992                                         ret=(rve->op==RVE_DIFF_OP);
1993 #elif defined UNDEF_EQ_UNDEF_TRUE
1994                                         /* undef == something defined always false
1995                                            undef == undef true */
1996                                         if (int_rve_defined(h, msg, &i2, rve->right.rve)<0){
1997                                                 /* error */
1998                                                 rval_cache_clean(&c1);
1999                                                 rval_destroy(rv1);
2000                                                 break;
2001                                         }
2002                                         ret=(!i2) ^ (rve->op==RVE_DIFF_OP);
2003 #else  /* ! UNDEF_EQ_* */
2004                                         /*  undef == val
2005                                          *  => convert to (type_of(val)) (undef) == val */
2006                                         rval_cache_init(&c2);
2007                                         if (unlikely( (ret=rval_expr_eval_rvint(h, msg, &rv2, &i2,
2008                                                                                                         rve->right.rve, &c2))<0)){
2009                                                 /* error */
2010                                                 rval_cache_clean(&c1);
2011                                                 rval_cache_clean(&c2);
2012                                                 rval_destroy(rv1);
2013                                                 break;
2014                                         }
2015                                         if (rv2==0){
2016                                                 /* int */
2017                                                 ret=int_intop2(res, rve->op, 0 /* undef */, i2);
2018                                         }else{
2019                                                 /* str or undef */
2020                                                 ret=rval_str_lop2(h, msg, res, rve->op, rv1, &c1,
2021                                                                                         rv2, &c2);
2022                                                 rval_cache_clean(&c2);
2023                                                 rval_destroy(rv2);
2024                                         }
2025 #endif /* UNDEF_EQ_* */
2026                                         rval_cache_clean(&c1);
2027                                         rval_destroy(rv1);
2028                                 }else{
2029                                         /* left value == defined and != int => str
2030                                          * => lval == (str) val */
2031                                         if (unlikely((rv2=rval_expr_eval(h, msg,
2032                                                                                                                 rve->right.rve))==0)){
2033                                                 /* error */
2034                                                 rval_destroy(rv1);
2035                                                 rval_cache_clean(&c1);
2036                                                 break;
2037                                         }
2038                                         ret=rval_str_lop2(h, msg, res, rve->op, rv1, &c1, rv2, 0);
2039                                         rval_cache_clean(&c1);
2040                                         rval_destroy(rv1);
2041                                         rval_destroy(rv2);
2042                                 }
2043                         }
2044                         break;
2045                 case RVE_CONCAT_OP:
2046                         /* eval expression => string */
2047                         if (unlikely((rv1=rval_expr_eval(h, msg, rve))==0)){
2048                                 ret=-1;
2049                                 break;
2050                         }
2051                         /* convert to int */
2052                         ret=rval_get_int(h, msg, res, rv1, 0); /* convert to int */
2053                         rval_get_int_handle_ret(ret, "rval expression conversion to int"
2054                                                                                 " failed", rve);
2055                         rval_destroy(rv1);
2056                         break;
2057                 case RVE_STR_OP:
2058                         /* (str)expr => eval expression */
2059                         rval_cache_init(&c1);
2060                         if (unlikely((ret=rval_expr_eval_rvint(h, msg, &rv1, res,
2061                                                                                                         rve->left.rve, &c1))<0)){
2062                                 /* error */
2063                                 rval_cache_clean(&c1);
2064                                 break;
2065                         }
2066                         if (unlikely(rv1)){
2067                                 /* expr evaluated to string => (int)(str)v == (int)v */
2068                                 ret=rval_get_int(h, msg, res, rv1, &c1); /* convert to int */
2069                                 rval_get_int_handle_ret(ret, "rval expression conversion"
2070                                                                                                 " to int failed", rve);
2071                                 rval_destroy(rv1);
2072                                 rval_cache_clean(&c1);
2073                         } /* else (rv1==0)
2074                                  => expr evaluated to int => 
2075                                  return (int)(str)v == (int)v => do nothing */
2076                         break;
2077
2078 #if 0
2079                         /* same thing as above, but in a not optimized, easier to
2080                            understand way */
2081                         /* 1. (str) expr => eval expr */
2082                         if (unlikely((rv1=rval_expr_eval(h, msg, rve->left.rve))==0)){
2083                                 ret=-1;
2084                                 break;
2085                         }
2086                         /* 2. convert to str and then convert to int
2087                            but since (int)(str)v == (int)v skip over (str)v */
2088                         ret=rval_get_int(h, msg, res, rv1, 0); /* convert to int */
2089                         rval_destroy(rv1);
2090                         break;
2091 #endif
2092                 case RVE_DEFINED_OP:
2093                         ret=int_rve_defined(h, msg, res, rve->left.rve);
2094                         break;
2095                 case RVE_STREQ_OP:
2096                 case RVE_STRDIFF_OP:
2097                 case RVE_MATCH_OP:
2098                         if (unlikely((rv1=rval_expr_eval(h, msg, rve->left.rve))==0)){
2099                                 ret=-1;
2100                                 break;
2101                         }
2102                         if (unlikely((rv2=rval_expr_eval(h, msg, rve->right.rve))==0)){
2103                                 rval_destroy(rv1);
2104                                 ret=-1;
2105                                 break;
2106                         }
2107                         ret=rval_str_lop2(h, msg, res, rve->op, rv1, 0, rv2, 0);
2108                         rval_destroy(rv1);
2109                         rval_destroy(rv2);
2110                         break;
2111                 case RVE_STRLEN_OP:
2112                 case RVE_STREMPTY_OP:
2113                         if (unlikely((rv1=rval_expr_eval(h, msg, rve->left.rve))==0)){
2114                                         ret=-1;
2115                                         break;
2116                         }
2117                         ret=rval_int_strop1(h, msg, res, rve->op, rv1, 0);
2118                         rval_destroy(rv1);
2119                         break;
2120                 case RVE_NONE_OP:
2121                 /*default:*/
2122                         BUG("invalid rval int expression operation %d (%d,%d-%d,%d)\n",
2123                                         rve->op, rve->fpos.s_line, rve->fpos.s_col,
2124                                         rve->fpos.e_line, rve->fpos.e_col);
2125                         ret=-1;
2126         };
2127         return ret;
2128 }
2129
2130
2131
2132 /** evals a rval expr. into an int or another rv(str).
2133  * WARNING: rv result (rv_res) must be rval_destroy()'ed if non-null
2134  * (it might be a reference to another rval). The result can be
2135  * modified only if rv_chg_in_place() returns true.
2136  * @param res_rv - pointer to rvalue result, if non-null it means the 
2137  *                 expression evaluated to a non-int (str), which will be
2138  *                 stored here.
2139  * @param res_i  - pointer to int result, if res_rv==0 and the function
2140  *                 returns success => the result is an int which will be 
2141  *                 stored here.
2142  * @param rve    - expression that will be evaluated.
2143  * @param cache  - write-only value cache, it might be filled if non-null and
2144  *                 empty (rval_cache_init()). If non-null, it _must_ be 
2145  *                 rval_cache_clean()'ed when done. 
2146  *
2147  * @result  0 on success, -1 on error,  sets *res_rv or *res_i.
2148  */
2149 int rval_expr_eval_rvint(                          struct run_act_ctx* h,
2150                                                                            struct sip_msg* msg,
2151                                                                            struct rvalue** res_rv,
2152                                                                            int* res_i,
2153                                                                            struct rval_expr* rve,
2154                                                                            struct rval_cache* cache
2155                                                                            )
2156 {
2157         struct rvalue* rv1;
2158         struct rvalue* rv2;
2159         struct rval_cache c1; /* local cache */
2160         int ret;
2161         int r, i, j;
2162         enum rval_type type;
2163         
2164         rv1=0;
2165         rv2=0;
2166         ret=-1;
2167         switch(rve->op){
2168                 case RVE_RVAL_OP:
2169                         rv1=&rve->left.rval;
2170                         rv_ref(rv1);
2171                         type=rval_get_btype(h, msg, rv1, cache);
2172                         if (type==RV_INT){
2173                                         r=rval_get_int(h, msg, res_i, rv1, cache);
2174                                         rval_get_int_handle_ret(r, "rval expression conversion"
2175                                                                                                 " to int failed", rve);
2176                                         *res_rv=0;
2177                                         ret=r; /* equiv. to if (r<0) goto error */
2178                         }else{
2179                                 /* RV_STR, RV_PVAR, RV_AVP a.s.o => return rv1 and the 
2180                                    cached resolved value in cache*/
2181                                         *res_rv=rv1;
2182                                         rv_ref(rv1);
2183                                         ret=0;
2184                         }
2185                         break;
2186                 case RVE_UMINUS_OP:
2187                 case RVE_BOOL_OP:
2188                 case RVE_LNOT_OP:
2189                 case RVE_BNOT_OP:
2190                 case RVE_MINUS_OP:
2191                 case RVE_MUL_OP:
2192                 case RVE_DIV_OP:
2193                 case RVE_MOD_OP:
2194                 case RVE_BOR_OP:
2195                 case RVE_BAND_OP:
2196                 case RVE_BXOR_OP:
2197                 case RVE_BLSHIFT_OP:
2198                 case RVE_BRSHIFT_OP:
2199                 case RVE_LAND_OP:
2200                 case RVE_LOR_OP:
2201                 case RVE_GT_OP:
2202                 case RVE_GTE_OP:
2203                 case RVE_LT_OP:
2204                 case RVE_LTE_OP:
2205                 case RVE_EQ_OP:
2206                 case RVE_DIFF_OP:
2207                 case RVE_IEQ_OP:
2208                 case RVE_IDIFF_OP:
2209                 case RVE_IPLUS_OP:
2210                 case RVE_STREQ_OP:
2211                 case RVE_STRDIFF_OP:
2212                 case RVE_MATCH_OP:
2213                 case RVE_STRLEN_OP:
2214                 case RVE_STREMPTY_OP:
2215                 case RVE_DEFINED_OP:
2216                 case RVE_INT_OP:
2217                         /* operator forces integer type */
2218                         ret=rval_expr_eval_int(h, msg, res_i, rve);
2219                         *res_rv=0;
2220                         break;
2221                 case RVE_PLUS_OP:
2222                         rval_cache_init(&c1);
2223                         r=rval_expr_eval_rvint(h, msg, &rv1, &i, rve->left.rve, &c1);
2224                         if (unlikely(r<0)){
2225                                 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2226                                                 rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
2227                                                 rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col
2228                                         );
2229                                 rval_cache_clean(&c1);
2230                                 goto error;
2231                         }
2232                         if (rv1==0){
2233                                 if (unlikely((r=rval_expr_eval_int(h, msg, &j,
2234                                                                                                                 rve->right.rve))<0)){
2235                                                 ERR("rval expression evaluation failed (%d,%d-%d,%d)"
2236                                                                 "\n", rve->right.rve->fpos.s_line,
2237                                                                 rve->right.rve->fpos.s_col,
2238                                                                 rve->right.rve->fpos.e_line,
2239                                                                 rve->right.rve->fpos.e_col);
2240                                                 rval_cache_clean(&c1);
2241                                                 goto error;
2242                                 }
2243                                 ret=int_intop2(res_i, rve->op, i, j);
2244                                 *res_rv=0;
2245                         }else{
2246                                 rv2=rval_expr_eval(h, msg, rve->right.rve);
2247                                 if (unlikely(rv2==0)){
2248                                         ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2249                                                                 rve->right.rve->fpos.s_line,
2250                                                                 rve->right.rve->fpos.s_col,
2251                                                                 rve->right.rve->fpos.e_line,
2252                                                                 rve->right.rve->fpos.e_col);
2253                                         rval_cache_clean(&c1);
2254                                         goto error;
2255                                 }
2256                                 *res_rv=rval_str_add2(h, msg, rv1, &c1, rv2, 0);
2257                                 ret=-(*res_rv==0);
2258                         }
2259                         rval_cache_clean(&c1);
2260                         break;
2261                 case RVE_CONCAT_OP:
2262                 case RVE_STR_OP:
2263                         *res_rv=rval_expr_eval(h, msg, rve);
2264                         ret=-(*res_rv==0);
2265                         break;
2266                 case RVE_NONE_OP:
2267                 /*default:*/
2268                         BUG("invalid rval expression operation %d (%d,%d-%d,%d)\n",
2269                                         rve->op, rve->fpos.s_line, rve->fpos.s_col,
2270                                         rve->fpos.e_line, rve->fpos.e_col);
2271                         goto error;
2272         };
2273         rval_destroy(rv1);
2274         rval_destroy(rv2);
2275         return ret;
2276 error:
2277         rval_destroy(rv1);
2278         rval_destroy(rv2);
2279         return -1;
2280 }
2281
2282
2283
2284 /** evals a rval expr..
2285  * WARNING: result must be rval_destroy()'ed if non-null (it might be
2286  * a reference to another rval). The result can be modified only
2287  * if rv_chg_in_place() returns true.
2288  * @return rvalue on success, 0 on error
2289  */
2290 struct rvalue* rval_expr_eval(struct run_act_ctx* h, struct sip_msg* msg,
2291                                                                 struct rval_expr* rve)
2292 {
2293         struct rvalue* rv1;
2294         struct rvalue* rv2;
2295         struct rvalue* ret;
2296         struct rval_cache c1;
2297         union rval_val v;
2298         int r, i, j;
2299         enum rval_type type;
2300         
2301         rv1=0;
2302         rv2=0;
2303         ret=0;
2304         switch(rve->op){
2305                 case RVE_RVAL_OP:
2306                         rv_ref(&rve->left.rval);
2307                         return &rve->left.rval;
2308                         break;
2309                 case RVE_UMINUS_OP:
2310                 case RVE_BOOL_OP:
2311                 case RVE_LNOT_OP:
2312                 case RVE_BNOT_OP:
2313                 case RVE_MINUS_OP:
2314                 case RVE_MUL_OP:
2315                 case RVE_DIV_OP:
2316                 case RVE_MOD_OP:
2317                 case RVE_BOR_OP:
2318                 case RVE_BAND_OP:
2319                 case RVE_BXOR_OP:
2320                 case RVE_BLSHIFT_OP:
2321                 case RVE_BRSHIFT_OP:
2322                 case RVE_LAND_OP:
2323                 case RVE_LOR_OP:
2324                 case RVE_GT_OP:
2325                 case RVE_GTE_OP:
2326                 case RVE_LT_OP:
2327                 case RVE_LTE_OP:
2328                 case RVE_EQ_OP:
2329                 case RVE_DIFF_OP:
2330                 case RVE_IEQ_OP:
2331                 case RVE_IDIFF_OP:
2332                 case RVE_IPLUS_OP:
2333                 case RVE_STREQ_OP:
2334                 case RVE_STRDIFF_OP:
2335                 case RVE_MATCH_OP:
2336                 case RVE_STRLEN_OP:
2337                 case RVE_STREMPTY_OP:
2338                 case RVE_DEFINED_OP:
2339                 case RVE_INT_OP:
2340                         /* operator forces integer type */
2341                         r=rval_expr_eval_int(h, msg, &i, rve);
2342                         if (likely(r==0)){
2343                                 v.l=i;
2344                                 ret=rval_new(RV_INT, &v, 0);
2345                                 if (unlikely(ret==0)){
2346                                         ERR("rv eval int expression: out of memory\n");
2347                                         goto error;
2348                                 }
2349                                 return ret;
2350                         }else{
2351                                 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2352                                                 rve->fpos.s_line, rve->fpos.s_col,
2353                                                 rve->fpos.e_line, rve->fpos.e_col);
2354                                 goto error;
2355                         }
2356                         break;
2357                 case RVE_PLUS_OP:
2358                         rv1=rval_expr_eval(h, msg, rve->left.rve);
2359                         if (unlikely(rv1==0)){
2360                                 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2361                                                 rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
2362                                                 rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col);
2363                                 goto error;
2364                         }
2365                         rval_cache_init(&c1);
2366                         type=rval_get_btype(h, msg, rv1, &c1);
2367                         switch(type){
2368                                 case RV_INT:
2369                                         r=rval_get_int(h, msg, &i, rv1, &c1);
2370                                         rval_get_int_handle_ret(r, "rval expression left side "
2371                                                                                                 "conversion to int failed",
2372                                                                                         rve);
2373                                         if (unlikely(r<0)){
2374                                                 rval_cache_clean(&c1);
2375                                                 goto error;
2376                                         }
2377                                         if (unlikely((r=rval_expr_eval_int(h, msg, &j,
2378                                                                                                                 rve->right.rve))<0)){
2379                                                 rval_cache_clean(&c1);
2380                                                 ERR("rval expression evaluation failed (%d,%d-%d,%d):"
2381                                                                 " could not evaluate right side to int\n",
2382                                                                 rve->fpos.s_line, rve->fpos.s_col,
2383                                                                 rve->fpos.e_line, rve->fpos.e_col);
2384                                                 goto error;
2385                                         }
2386                                         int_intop2(&r, rve->op, i, j);
2387                                         if (rv_chg_in_place(rv1)){
2388                                                 rv1->v.l=r;
2389                                                 ret=rv1;
2390                                                 rv_ref(ret);
2391                                         }else{
2392                                                 v.l=r;
2393                                                 ret=rval_new(RV_INT, &v, 0);
2394                                                 if (unlikely(ret==0)){
2395                                                         rval_cache_clean(&c1);
2396                                                         ERR("rv eval int expression: out of memory\n");
2397                                                         goto error;
2398                                                 }
2399                                         }
2400                                         break;
2401                                 case RV_STR:
2402                                 case RV_NONE:
2403                                         rv2=rval_expr_eval(h, msg, rve->right.rve);
2404                                         if (unlikely(rv2==0)){
2405                                                 ERR("rval expression evaluation failed (%d,%d-%d,%d)"
2406                                                                 "\n", rve->right.rve->fpos.s_line,
2407                                                                 rve->right.rve->fpos.s_col,
2408                                                                 rve->right.rve->fpos.e_line,
2409                                                                 rve->right.rve->fpos.e_col);
2410                                                 rval_cache_clean(&c1);
2411                                                 goto error;
2412                                         }
2413                                         ret=rval_str_add2(h, msg, rv1, &c1, rv2, 0);
2414                                         break;
2415                                 default:
2416                                         BUG("rv unsupported basic type %d (%d,%d-%d,%d)\n", type,
2417                                                         rve->fpos.s_line, rve->fpos.s_col,
2418                                                         rve->fpos.e_line, rve->fpos.e_col);
2419                         }
2420                         rval_cache_clean(&c1);
2421                         break;
2422                 case RVE_CONCAT_OP:
2423                         rv1=rval_expr_eval(h, msg, rve->left.rve);
2424                         if (unlikely(rv1==0)){
2425                                 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2426                                                 rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
2427                                                 rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col);
2428                                 goto error;
2429                         }
2430                         rv2=rval_expr_eval(h, msg, rve->right.rve);
2431                         if (unlikely(rv2==0)){
2432                                 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2433                                                 rve->right.rve->fpos.s_line,
2434                                                 rve->right.rve->fpos.s_col,
2435                                                 rve->right.rve->fpos.e_line,
2436                                                 rve->right.rve->fpos.e_col);
2437                                 goto error;
2438                         }
2439                         ret=rval_str_add2(h, msg, rv1, 0, rv2, 0);
2440                         break;
2441                 case RVE_STR_OP:
2442                         rv1=rval_expr_eval(h, msg, rve->left.rve);
2443                         if (unlikely(rv1==0)){
2444                                 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2445                                                 rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
2446                                                 rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col);
2447                                 goto error;
2448                         }
2449                         ret=rval_convert(h, msg, RV_STR, rv1, 0);
2450                         break;
2451                 case RVE_NONE_OP:
2452                 /*default:*/
2453                         BUG("invalid rval expression operation %d (%d,%d-%d,%d)\n",
2454                                         rve->op, rve->fpos.s_line, rve->fpos.s_col,
2455                                         rve->fpos.e_line, rve->fpos.e_col);
2456                         goto error;
2457         };
2458         rval_destroy(rv1);
2459         rval_destroy(rv2);
2460         return ret;
2461 error:
2462         rval_destroy(rv1);
2463         rval_destroy(rv2);
2464         return 0;
2465 }
2466
2467
2468
2469 /** evals a rval expr and always returns a new rval.
2470  * like rval_expr_eval, but always returns a new rvalue (never a reference
2471  * to an exisiting one).
2472  * WARNING: result must be rval_destroy()'ed if non-null (it might be
2473  * a reference to another rval). The result can be modified only
2474  * if rv_chg_in_place() returns true.
2475  * @result rvalue on success, 0 on error
2476  */
2477 struct rvalue* rval_expr_eval_new(struct run_act_ctx* h, struct sip_msg* msg,
2478                                                                 struct rval_expr* rve)
2479 {
2480         struct rvalue* ret;
2481         struct rvalue* rv;
2482         
2483         ret=rval_expr_eval(h, msg, rve);
2484         if (ret && !rv_chg_in_place(ret)){
2485                 rv=ret;
2486                 /* create a new rv */
2487                 ret=rval_new(rv->type, &rv->v, 0);
2488                 rval_destroy(rv);
2489         }
2490         return ret;
2491 }
2492
2493
2494
2495 /** create a RVE_RVAL_OP rval_expr, containing a single rval of the given type.
2496  *
2497  * @param rv_type - rval type
2498  * @param val     - rval value
2499  * @param pos     - config position
2500  * @return new pkg_malloc'ed rval_expr or 0 on error.
2501  */
2502 struct rval_expr* mk_rval_expr_v(enum rval_type rv_type, void* val, 
2503                                                                         struct cfg_pos* pos)
2504 {
2505         struct rval_expr* rve;
2506         union rval_val v;
2507         str* s;
2508         int flags;
2509         
2510         rve=pkg_malloc(sizeof(*rve));
2511         if (rve==0) 
2512                 return 0;
2513         memset(rve, 0, sizeof(*rve));
2514         flags=0;
2515         switch(rv_type){
2516                 case RV_INT:
2517                         v.l=(long)val;
2518                         break;
2519                 case RV_STR:
2520                         s=(str*)val;
2521                         v.s.s=pkg_malloc(s->len+1 /*0*/);
2522                         if (v.s.s==0){
2523                                 ERR("memory allocation failure\n");
2524                                 return 0;
2525                         }
2526                         v.s.len=s->len;
2527                         memcpy(v.s.s, s->s, s->len);
2528                         v.s.s[s->len]=0;
2529                         flags=RV_CNT_ALLOCED_F;
2530                         break;
2531                 case RV_AVP:
2532                         v.avps=*(avp_spec_t*)val;
2533                         break;
2534                 case RV_PVAR:
2535                         v.pvs=*(pv_spec_t*)val;
2536                         break;
2537                 case RV_SEL:
2538                         v.sel=*(select_t*)val;
2539                         break;
2540                 case RV_BEXPR:
2541                         v.bexpr=(struct expr*)val;
2542                         break;
2543                 case RV_ACTION_ST:
2544                         v.action=(struct action*)val;
2545                         break;
2546                 default:
2547                         BUG("unsupported rv type %d\n", rv_type);
2548                         pkg_free(rve);
2549                         return 0;
2550         }
2551         rval_init(&rve->left.rval, rv_type, &v, flags);
2552         rve->op=RVE_RVAL_OP;
2553         if (pos) rve->fpos=*pos;
2554         return rve;
2555 }
2556
2557
2558
2559 /** create a unary op. rval_expr..
2560  * ret= op rve1
2561  * @param op   - rval expr. unary operator
2562  * @param rve1 - rval expr. on which the operator will act.
2563  * @return new pkg_malloc'ed rval_expr or 0 on error.
2564  */
2565 struct rval_expr* mk_rval_expr1(enum rval_expr_op op, struct rval_expr* rve1,
2566                                                                 struct cfg_pos* pos)
2567 {
2568         struct rval_expr* ret;
2569         
2570         switch(op){
2571                 case RVE_UMINUS_OP:
2572                 case RVE_BOOL_OP:
2573                 case RVE_LNOT_OP:
2574                 case RVE_BNOT_OP:
2575                 case RVE_STRLEN_OP:
2576                 case RVE_STREMPTY_OP:
2577                 case RVE_DEFINED_OP:
2578                 case RVE_INT_OP:
2579                 case RVE_STR_OP:
2580                         break;
2581                 default:
2582                         BUG("unsupported unary operator %d\n", op);
2583                         return 0;
2584         }
2585         ret=pkg_malloc(sizeof(*ret));
2586         if (ret==0) 
2587                 return 0;
2588         memset(ret, 0, sizeof(*ret));
2589         ret->op=op;
2590         ret->left.rve=rve1;
2591         if (pos) ret->fpos=*pos;
2592         return ret;
2593 }
2594
2595
2596
2597 /** create a rval_expr. from 2 other rval exprs, using op.
2598  * ret = rve1 op rve2
2599  * @param op   - rval expr. operator
2600  * @param rve1 - rval expr. on which the operator will act.
2601  * @param rve2 - rval expr. on which the operator will act.
2602  * @return new pkg_malloc'ed rval_expr or 0 on error.
2603  */
2604 struct rval_expr* mk_rval_expr2(enum rval_expr_op op, struct rval_expr* rve1,
2605                                                                                                           struct rval_expr* rve2,
2606                                                                                                           struct cfg_pos* pos)
2607 {
2608         struct rval_expr* ret;
2609         
2610         switch(op){
2611                 case RVE_MUL_OP:
2612                 case RVE_DIV_OP:
2613                 case RVE_MOD_OP:
2614                 case RVE_MINUS_OP:
2615                 case RVE_BOR_OP:
2616                 case RVE_BAND_OP:
2617                 case RVE_BXOR_OP:
2618                 case RVE_BLSHIFT_OP:
2619                 case RVE_BRSHIFT_OP:
2620                 case RVE_LAND_OP:
2621                 case RVE_LOR_OP:
2622                 case RVE_GT_OP:
2623                 case RVE_GTE_OP:
2624                 case RVE_LT_OP:
2625                 case RVE_LTE_OP:
2626                 case RVE_PLUS_OP:
2627                 case RVE_IPLUS_OP:
2628                 case RVE_EQ_OP:
2629                 case RVE_DIFF_OP:
2630                 case RVE_IEQ_OP:
2631                 case RVE_IDIFF_OP:
2632                 case RVE_STREQ_OP:
2633                 case RVE_STRDIFF_OP:
2634                 case RVE_MATCH_OP:
2635                 case RVE_CONCAT_OP:
2636                         break;
2637                 default:
2638                         BUG("unsupported operator %d\n", op);
2639                         return 0;
2640         }
2641         ret=pkg_malloc(sizeof(*ret));
2642         if (ret==0) 
2643                 return 0;
2644         memset(ret, 0, sizeof(*ret));
2645         ret->op=op;
2646         ret->left.rve=rve1;
2647         ret->right.rve=rve2;
2648         if (pos) ret->fpos=*pos;
2649         return ret;
2650 }
2651
2652
2653
2654 /** returns true if the operator is associative. */
2655 static int rve_op_is_assoc(enum rval_expr_op op)
2656 {
2657         switch(op){
2658                 case RVE_NONE_OP:
2659                 case RVE_RVAL_OP:
2660                 case RVE_UMINUS_OP:
2661                 case RVE_BOOL_OP:
2662                 case RVE_LNOT_OP:
2663                 case RVE_BNOT_OP:
2664                 case RVE_STRLEN_OP:
2665                 case RVE_STREMPTY_OP:
2666                 case RVE_DEFINED_OP:
2667                 case RVE_INT_OP:
2668                 case RVE_STR_OP:
2669                         /* one operand expression => cannot be assoc. */
2670                         return 0;
2671                 case RVE_DIV_OP:
2672                 case RVE_MOD_OP:
2673                 case RVE_MINUS_OP:
2674                 case RVE_BLSHIFT_OP:
2675                 case RVE_BRSHIFT_OP:
2676                         return 0;
2677                 case RVE_PLUS_OP:
2678                         /* the generic plus is not assoc, e.g.
2679                            "a" + 1 + "2" => "a12" in one case and "a3" in the other */
2680                         return 0;
2681                 case RVE_IPLUS_OP:
2682                 case RVE_CONCAT_OP:
2683                 case RVE_MUL_OP:
2684                 case RVE_BAND_OP:
2685                 case RVE_BOR_OP:
2686                 case RVE_BXOR_OP:
2687                         return 1;
2688                 case RVE_LAND_OP:
2689                 case RVE_LOR_OP:
2690                         return 1;
2691                 case RVE_GT_OP:
2692                 case RVE_GTE_OP:
2693                 case RVE_LT_OP:
2694                 case RVE_LTE_OP:
2695                 case RVE_EQ_OP:
2696                 case RVE_DIFF_OP:
2697                 case RVE_IEQ_OP:
2698                 case RVE_IDIFF_OP:
2699                 case RVE_STREQ_OP:
2700                 case RVE_STRDIFF_OP:
2701                 case RVE_MATCH_OP:
2702                         return 0;
2703         }
2704         return 0;
2705 }
2706
2707
2708
2709 /** returns true if the operator is commutative. */
2710 static int rve_op_is_commutative(enum rval_expr_op op)
2711 {
2712         switch(op){
2713                 case RVE_NONE_OP:
2714                 case RVE_RVAL_OP:
2715                 case RVE_UMINUS_OP:
2716                 case RVE_BOOL_OP:
2717                 case RVE_LNOT_OP:
2718                 case RVE_BNOT_OP:
2719                 case RVE_STRLEN_OP:
2720                 case RVE_STREMPTY_OP:
2721                 case RVE_DEFINED_OP:
2722                 case RVE_INT_OP:
2723                 case RVE_STR_OP:
2724                         /* one operand expression => cannot be commut. */
2725                         return 0;
2726                 case RVE_DIV_OP:
2727                 case RVE_MOD_OP:
2728                 case RVE_MINUS_OP:
2729                 case RVE_BLSHIFT_OP:
2730                 case RVE_BRSHIFT_OP:
2731                         return 0;
2732                 case RVE_PLUS_OP:
2733                         /* non commut. when diff. type 
2734                            (e.g 1 + "2" != "2" + 1 ) => non commut. in general
2735                            (specific same type versions are covered by IPLUS & CONCAT) */
2736                         return 0;
2737                 case RVE_IPLUS_OP:
2738                 case RVE_MUL_OP:
2739                 case RVE_BAND_OP:
2740                 case RVE_BOR_OP:
2741                 case RVE_BXOR_OP:
2742                 case RVE_LAND_OP:
2743                 case RVE_LOR_OP:
2744                 case RVE_IEQ_OP:
2745                 case RVE_IDIFF_OP:
2746                 case RVE_STREQ_OP:
2747                 case RVE_STRDIFF_OP:
2748                         return 1;
2749                 case RVE_GT_OP:
2750                 case RVE_GTE_OP:
2751                 case RVE_LT_OP:
2752                 case RVE_LTE_OP:
2753                 case RVE_CONCAT_OP:
2754                 case RVE_MATCH_OP:
2755                         return 0;
2756                 case RVE_DIFF_OP:
2757                 case RVE_EQ_OP:
2758                         /* non. commut. in general, only for same type e.g.:
2759                            "" == 0  diff. 0 == "" ( "" == "0" and 0 == 0)
2760                            same type versions are covered by IEQ, IDIFF, STREQ, STRDIFF
2761                          */
2762                         return 0 /* asymmetrical undef handling */;
2763         }
2764         return 0;
2765 }
2766
2767
2768 #if 0
2769 /** returns true if the rval expr can be optimized to an int.
2770  *  (if left & right are leafs (RVE_RVAL_OP) and both of them are
2771  *   ints return true, else false)
2772  *  @return 0 or 1
2773  */
2774 static int rve_can_optimize_int(struct rval_expr* rve)
2775 {
2776         if (scr_opt_lev<1)
2777                 return 0;
2778         if (rve->op == RVE_RVAL_OP)
2779                 return 0;
2780         if (rve->left.rve->op != RVE_RVAL_OP)
2781                 return 0;
2782         if (rve->left.rve->left.rval.type!=RV_INT)
2783                 return 0;
2784         if (rve->right.rve){
2785                 if  (rve->right.rve->op != RVE_RVAL_OP)
2786                         return 0;
2787                 if (rve->right.rve->left.rval.type!=RV_INT)
2788                         return 0;
2789         }
2790         DBG("rve_can_optimize_int: left %d, right %d\n", 
2791                         rve->left.rve->op, rve->right.rve?rve->right.rve->op:0);
2792         return 1;
2793 }
2794
2795
2796
2797 /** returns true if the rval expr can be optimized to a str.
2798  *  (if left & right are leafs (RVE_RVAL_OP) and both of them are
2799  *   str or left is str and right is int return true, else false)
2800  *  @return 0 or 1
2801  */
2802 static int rve_can_optimize_str(struct rval_expr* rve)
2803 {
2804         if (scr_opt_lev<1)
2805                 return 0;
2806         if (rve->op == RVE_RVAL_OP)
2807                 return 0;
2808         DBG("rve_can_optimize_str: left %d, right %d\n", 
2809                         rve->left.rve->op, rve->right.rve?rve->right.rve->op:0);
2810         if (rve->left.rve->op != RVE_RVAL_OP)
2811                 return 0;
2812         if (rve->left.rve->left.rval.type!=RV_STR)
2813                 return 0;
2814         if (rve->right.rve){
2815                 if  (rve->right.rve->op != RVE_RVAL_OP)
2816                         return 0;
2817                 if ((rve->right.rve->left.rval.type!=RV_STR) && 
2818                                 (rve->right.rve->left.rval.type!=RV_INT))
2819                         return 0;
2820         }
2821         return 1;
2822 }
2823 #endif
2824
2825
2826
2827 static int fix_rval(struct rvalue* rv)
2828 {
2829         DBG("RV fixing type %d\n", rv->type);
2830         switch(rv->type){
2831                 case RV_INT:
2832                         /*nothing to do*/
2833                         DBG("RV is int: %d\n", (int)rv->v.l);
2834                         return 0;
2835                 case RV_STR:
2836                         /*nothing to do*/
2837                         DBG("RV is str: \"%s\"\n", rv->v.s.s);
2838                         return 0;
2839                 case RV_BEXPR:
2840                         return fix_expr(rv->v.bexpr);
2841                 case RV_ACTION_ST:
2842                         return fix_actions(rv->v.action);
2843                 case RV_SEL:
2844                         if (resolve_select(&rv->v.sel)<0){
2845                                 ERR("Unable to resolve select\n");
2846                                 print_select(&rv->v.sel);
2847                         }
2848                         return 0;
2849                 case RV_AVP:
2850                         /* nothing to do, resolved at runtime */
2851                         return 0;
2852                 case RV_PVAR:
2853                         /* nothing to do, resolved at parsing time */
2854                         return 0;
2855                 case RV_NONE:
2856                         BUG("uninitialized rvalue\n");
2857                         return -1;
2858         }
2859         BUG("unknown rvalue type %d\n", rv->type);
2860         return -1;
2861 }
2862
2863
2864
2865 /** helper function: replace a rve (in-place) with a constant rval_val.
2866  * WARNING: since it replaces in-place, one should make sure that if
2867  * rve is in fact a rval (rve->op==RVE_RVAL_OP), no reference is kept
2868  * to the rval!
2869  * @param rve - expression to be replaced (in-place)
2870  * @param v   - pointer to a rval_val union containing the replacement
2871  *              value.
2872  * @param flags - value flags (how it was alloc'ed, e.g.: RV_CNT_ALLOCED_F)
2873  * @return 0 on success, -1 on error */
2874 static int rve_replace_with_val(struct rval_expr* rve, enum rval_type type,
2875                                                                 union rval_val* v, int flags)
2876 {
2877         int refcnt;
2878         
2879         refcnt=1; /* replaced-in-place rval refcnt */
2880         if (rve->op!=RVE_RVAL_OP){
2881                 rve_destroy(rve->left.rve);
2882                 if (rve_op_unary(rve->op)==0)
2883                         rve_destroy(rve->right.rve);
2884         }else{
2885                 if (rve->left.rval.refcnt!=1){
2886                         BUG("trying to replace a referenced rval! (refcnt=%d)\n",
2887                                         rve->left.rval.refcnt);
2888                         /* try to recover */
2889                         refcnt=rve->left.rval.refcnt;
2890                         abort(); /* find bugs quicker -- andrei */
2891                 }
2892                 rval_destroy(&rve->left.rval);
2893         }
2894         rval_init(&rve->left.rval, type, v, flags);
2895         rve->left.rval.refcnt=refcnt;
2896         rval_init(&rve->right.rval, RV_NONE, 0, 0);
2897         rve->op=RVE_RVAL_OP;
2898         return 0;
2899 }
2900
2901
2902
2903 /** helper function: replace a rve (in-place) with a constant rvalue.
2904  * @param rve - expression to be replaced (in-place)
2905  * @param rv   - pointer to the replacement _constant_ rvalue structure
2906  * @return 0 on success, -1 on error */
2907 static int rve_replace_with_ct_rv(struct rval_expr* rve, struct rvalue* rv)
2908 {
2909         enum rval_type type;
2910         int flags;
2911         int i;
2912         union rval_val v;
2913         
2914         type=rv->type;
2915         flags=0;
2916         if (rv->type==RV_INT){
2917                 if (rval_get_int(0, 0, &i, rv, 0)!=0){
2918                         BUG("unexpected int evaluation failure (%d,%d-%d,%d)\n",
2919                                         rve->fpos.s_line, rve->fpos.s_col,
2920                                         rve->fpos.e_line, rve->fpos.e_col);
2921                         return -1;
2922                 }
2923                 v.l=i;
2924         }else if(rv->type==RV_STR){
2925                 if (rval_get_str(0, 0, &v.s, rv, 0)<0){
2926                         BUG("unexpected str evaluation failure(%d,%d-%d,%d)\n",
2927                                         rve->fpos.s_line, rve->fpos.s_col,
2928                                         rve->fpos.e_line, rve->fpos.e_col);
2929                         return -1;
2930                 }
2931                 flags|=RV_CNT_ALLOCED_F;
2932         }else{
2933                 BUG("unknown constant expression type %d (%d,%d-%d,%d)\n", rv->type,
2934                                 rve->fpos.s_line, rve->fpos.s_col,
2935                                 rve->fpos.e_line, rve->fpos.e_col);
2936                 return -1;
2937         }
2938         return rve_replace_with_val(rve, type, &v, flags);
2939 }
2940
2941
2942
2943 /** try to replace the right side of the rve with a compiled regex.
2944   * @return 0 on success and -1 on error.
2945  */
2946 static int fix_match_rve(struct rval_expr* rve)
2947 {
2948         struct rvalue* rv;
2949         regex_t* re;
2950         union rval_val v;
2951         int flags;
2952         int ret;
2953
2954         rv=0;
2955         v.s.s=0;
2956         v.re.regex=0;
2957         /* normal fix-up for the  left side */
2958         ret=fix_rval_expr((void*)rve->left.rve);
2959         if (ret<0) return ret;
2960         
2961         /* fixup the right side (RE) */
2962         if (rve_is_constant(rve->right.rve)){
2963                 if ((rve_guess_type(rve->right.rve)!=RV_STR)){
2964                         ERR("fixup failure(%d,%d-%d,%d): left side of  =~ is not string"
2965                                         " (%d,%d)\n",   rve->fpos.s_line, rve->fpos.s_col,
2966                                                                         rve->fpos.e_line, rve->fpos.e_col,
2967                                                                         rve->right.rve->fpos.s_line,
2968                                                                         rve->right.rve->fpos.s_col);
2969                         goto error;
2970                 }
2971                 if ((rv=rval_expr_eval(0, 0, rve->right.rve))==0){
2972                         ERR("fixup failure(%d,%d-%d,%d):  bad RE expression\n",
2973                                         rve->right.rve->fpos.s_line, rve->right.rve->fpos.s_col,
2974                                         rve->right.rve->fpos.e_line, rve->right.rve->fpos.e_col);
2975                         goto error;
2976                 }
2977                 if (rval_get_str(0, 0, &v.s, rv, 0)<0){
2978                         BUG("fixup unexpected failure (%d,%d-%d,%d)\n",
2979                                         rve->fpos.s_line, rve->fpos.s_col,
2980                                         rve->fpos.e_line, rve->fpos.e_col);
2981                         goto error;
2982                 }
2983                 /* we have the str, we don't need the rv anymore */
2984                 rval_destroy(rv);
2985                 rv=0;
2986                 re=pkg_malloc(sizeof(*re));
2987                 if (re==0){
2988                         ERR("out of memory\n");
2989                         goto error;
2990                 }
2991                 /* same flags as for expr. =~ (fix_expr()) */
2992                 if (regcomp(re, v.s.s, REG_EXTENDED|REG_NOSUB|REG_ICASE)){
2993                         pkg_free(re);
2994                         ERR("Bad regular expression \"%s\"(%d,%d-%d,%d)\n", v.s.s,
2995                                         rve->right.rve->fpos.s_line, rve->right.rve->fpos.s_col,
2996                                         rve->right.rve->fpos.e_line, rve->right.rve->fpos.e_col);
2997                         goto error;
2998                 }
2999                 v.re.regex=re;
3000                 flags=RV_RE_F|RV_RE_ALLOCED_F|RV_CNT_ALLOCED_F;
3001                 if (rve_replace_with_val(rve->right.rve, RV_STR, &v, flags)<0)
3002                         goto error;
3003         }else{
3004                 /* right side is not constant => normal fixup */
3005                 return fix_rval_expr((void*)rve->right.rve);
3006         }
3007         return 0;
3008 error:
3009         if (rv) rval_destroy(rv);
3010         if (v.s.s) pkg_free(v.s.s);
3011         if (v.re.regex){
3012                 regfree(v.re.regex);
3013                 pkg_free(v.re.regex);
3014         }
3015         return -1;
3016 }
3017
3018
3019
3020 /** optimize op($v, 0) or op($v, 1).
3021  * Note: internal use only from rve_optimize
3022  * It should be called after ct optimization, for non-contant
3023  *  expressions (the left or right side is not constant).
3024  * @return 1 on success (rve was changed), 0 on failure and -1 on error
3025  */
3026 static int rve_opt_01(struct rval_expr* rve, enum rval_type rve_type)
3027 {
3028         struct rvalue* rv;
3029         struct rval_expr* ct_rve;
3030         struct rval_expr* v_rve;
3031         int i;
3032         int ret;
3033         enum rval_expr_op op;
3034         struct cfg_pos pos;
3035         int right; /* debugging msg */
3036         int dbg; /* debugging msg on/off */
3037
3038 /* helper macro: replace in-place a <ctype> type rve with v (another rve).
3039  * if type_of(v)== <ctype> => rve:=*v (copy v contents into rve and free v)
3040  * else if type_of(v)!=<ctype> => rve:= (ctype) v (casts v to <ctype>)
3041  * Uses pos.
3042  * ctype can be INT or STR
3043  * WARNING: - v might be pkg_free()'d
3044  *          - rve members _are_ _not_ freed or destroyed
3045  */
3046 #define replace_rve_type_cast(e, v, ctype) \
3047         do{\
3048                 if ( rve_guess_type((v)) == RV_##ctype ){\
3049                         /* if type_of($v)==int we don't need to add an \
3050                            int cast operator => replace with v */\
3051                         pos=(e)->fpos; \
3052                         *(e)=*(v); /* replace e with v (in-place) */ \
3053                         (e)->fpos=pos; \
3054                         pkg_free((v)); /* rve_destroy(v_rve) would free everything*/ \
3055                 }else{\
3056                         /* unknown type or str => (int) $v */ \
3057                         (e)->op=RVE_##ctype##_OP; \
3058                         (e)->left.rve=(v); \
3059                         (e)->right.rve=0; \
3060                 }\
3061         }while(0)
3062         
3063 /* helper macro: replace in-place an int type rve with v (another rve).*/
3064 #define replace_int_rve(e, v) replace_rve_type_cast(e, v, INT)
3065 /* helper macro: replace in-place a str type rve with v (another rve).*/
3066 #define replace_str_rve(e, v) replace_rve_type_cast(e, v, STR)
3067         
3068         
3069         rv=0;
3070         ret=0;
3071         right=0;
3072         dbg=1;
3073         
3074         if (rve_is_constant(rve->right.rve)){
3075                 ct_rve=rve->right.rve;
3076                 v_rve=rve->left.rve;
3077                 right=1;
3078         }else if (rve_is_constant(rve->left.rve)){
3079                 ct_rve=rve->left.rve;
3080                 v_rve=rve->right.rve;
3081                 right=0;
3082         }else
3083                 return 0; /* op($v, $w) */
3084         
3085         /* rval_expr_eval_new() instead of rval_expr_eval() to avoid
3086            referencing a ct_rve->left.rval if ct_rve is a rval, which
3087            would prevent rve_destroy(ct_rve) from working */
3088         if ((rv=rval_expr_eval_new(0, 0, ct_rve))==0){
3089                 ERR("optimization failure, bad expression (%d,%d-%d,%d)\n",
3090                                 ct_rve->fpos.s_line, ct_rve->fpos.s_col,
3091                                 ct_rve->fpos.e_line, ct_rve->fpos.e_col);
3092                 goto error;
3093         }
3094         op=rve->op;
3095         if (rv->type==RV_INT){
3096                 i=rv->v.l;
3097                 switch(op){
3098                         case RVE_MUL_OP:
3099                                 if (i==0){
3100                                         /* $v *  0 -> 0
3101                                          *  0 * $v -> 0 */
3102                                         if (rve_replace_with_ct_rv(rve, rv)<0)
3103                                                 goto error;
3104                                         ret=1;
3105                                 }else if (i==1){
3106                                         /* $v *  1 -> (int)$v
3107                                          *  1 * $v -> (int)$v */
3108                                         rve_destroy(ct_rve);
3109                                         replace_int_rve(rve, v_rve);
3110                                         ret=1;
3111                                 }
3112                                 break;
3113                         case RVE_DIV_OP:
3114                                 if (i==0){
3115                                         if (ct_rve==rve->left.rve){
3116                                                 /* 0 / $v -> 0 */
3117                                                 if (rve_replace_with_ct_rv(rve, rv)<0)
3118                                                         goto error;
3119                                                 ret=1;
3120                                         }else{
3121                                                 /* $v / 0 */
3122                                                 ERR("RVE divide by 0 at %d,%d\n",
3123                                                                 ct_rve->fpos.s_line, ct_rve->fpos.s_col);
3124                                         }
3125                                 }else if (i==1){
3126                                         if (ct_rve==rve->right.rve){
3127                                                 /* $v / 1 -> (int)$v */
3128                                                 rve_destroy(ct_rve);
3129                                                 replace_int_rve(rve, v_rve);
3130                                                 ret=1;
3131                                         }
3132                                 }
3133                                 break;
3134                         case RVE_MOD_OP:
3135                                 if (i==0){
3136                                         if (ct_rve==rve->left.rve){
3137                                                 /* 0 % $v -> 0 */
3138                                                 if (rve_replace_with_ct_rv(rve, rv)<0)
3139                                                         goto error;
3140                                                 ret=1;
3141                                         }else{
3142                                                 /* $v % 0 */
3143                                                 ERR("RVE modulo by 0 at %d,%d\n",
3144                                                                 ct_rve->fpos.s_line, ct_rve->fpos.s_col);
3145                                         }
3146                                 }
3147                                 /* $v % 1 -> 0 ? */
3148                                 break;
3149                         case RVE_MINUS_OP:
3150                                 if (i==0){
3151                                         if (ct_rve==rve->right.rve){
3152                                                 /* $v - 0 -> $v */
3153                                                 rve_destroy(ct_rve);
3154                                                 replace_int_rve(rve, v_rve);
3155                                                 ret=1;
3156                                         }
3157                                         /* ? 0 - $v -> -($v)  ? */
3158                                 }
3159                                 break;
3160                         case RVE_BAND_OP:
3161                                 if (i==0){
3162                                         /* $v &  0 -> 0
3163                                          *  0 & $v -> 0 */
3164                                         if (rve_replace_with_ct_rv(rve, rv)<0)
3165                                                 goto error;
3166                                         ret=1;
3167                                 }
3168                                 /* no 0xffffff optimization for now (haven't decided on
3169                                    the number of bits ) */
3170                                 break;
3171                         case RVE_BOR_OP:
3172                                 if (i==0){
3173                                         /* $v |  0 -> (int)$v
3174                                          *  0 | $v -> (int)$v */
3175                                         rve_destroy(ct_rve);
3176                                         replace_int_rve(rve, v_rve);
3177                                         ret=1;
3178                                 }
3179                                 break;
3180                         case RVE_LAND_OP:
3181                                 if (i==0){
3182                                         /* $v &&  0 -> 0
3183                                          *  0 && $v -> 0 */
3184                                         if (rve_replace_with_ct_rv(rve, rv)<0)
3185                                                 goto error;
3186                                         ret=1;
3187                                 }else if (i==1){
3188                                         /* $v &&  1 -> (int)$v
3189                                          *  1 && $v -> (int)$v */
3190                                         rve_destroy(ct_rve);
3191                                         replace_int_rve(rve, v_rve);
3192                                         ret=1;
3193                                 }
3194                                 break;
3195                         case RVE_LOR_OP:
3196                                 if (i==1){
3197                                         /* $v ||  1 -> 1
3198                                          *  1 || $v -> 1 */
3199                                         if (rve_replace_with_ct_rv(rve, rv)<0)
3200                                                 goto error;
3201                                         ret=1;
3202                                 }else if (i==0){
3203                                         /* $v ||  0 -> (int)$v
3204                                          *  0 && $v -> (int)$v */
3205                                         rve_destroy(ct_rve);
3206                                         replace_int_rve(rve, v_rve);
3207                                         ret=1;
3208                                 }
3209                                 break;
3210                         case RVE_PLUS_OP:
3211                         case RVE_IPLUS_OP:
3212                                 /* we must make sure that this is an int PLUS
3213                                    (because "foo"+0 is valid => "foo0") =>
3214                                    check if it's an IPLUS or the result is an integer
3215                                    (which generally means unoptimized <int> + <something>).
3216                                  */
3217                                 if ((i==0) && ((op==RVE_IPLUS_OP) || (rve_type==RV_INT))){
3218                                         /* $v +  0 -> (int)$v
3219                                          *  0 + $v -> (int)$v */
3220                                         rve_destroy(ct_rve);
3221                                         replace_int_rve(rve, v_rve);
3222                                         ret=1;
3223                                 }
3224                                 break;
3225                         default:
3226                                 /* do nothing */
3227                                 break;
3228                 }
3229                 /* debugging messages */
3230                 if (ret==1 && dbg){
3231                         if (right){
3232                                 if (rve->op==RVE_RVAL_OP){
3233                                         if (rve->left.rval.type==RV_INT)
3234                                                 DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
3235                                                                 " op%d($v, %d) -> %d\n", 
3236                                                                 rve->fpos.s_line, rve->fpos.s_col,
3237                                                                 rve->fpos.e_line, rve->fpos.e_col,
3238                                                                 op, i, (int)rve->left.rval.v.l);
3239                                         else
3240                                                 DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
3241                                                                 " op%d($v, %d) -> $v (rval)\n",
3242                                                                 rve->fpos.s_line, rve->fpos.s_col,
3243                                                                 rve->fpos.e_line, rve->fpos.e_col,
3244                                                                 op, i);
3245                                 }else if (rve->op==RVE_INT_OP){
3246                                         if (rve->left.rve->op==RVE_RVAL_OP &&
3247                                                         rve->left.rve->left.rval.type==RV_INT)
3248                                                 DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
3249                                                                 " op%d($v, %d) -> (int)%d\n", 
3250                                                                 rve->fpos.s_line, rve->fpos.s_col,
3251                                                                 rve->fpos.e_line, rve->fpos.e_col,
3252