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