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