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