core: fix selects in optimized rvalue expressions
[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 to a temporary string or inside
1066   * new_cache. new_cache must be non zero, initialized previously,
1067   * and it _must_ be rval_cache_clean(...)'ed when done.
1068   * WARNING: it's not intended for general use, it might return a pointer
1069   * to a static buffer (int2str) so use the result a.s.a.p, make a copy.
1070   * or use rval_get_str() instead.
1071   * @param h - script context handle
1072   * @param msg - sip msg
1073   * @param tmpv - str return value (pointer to a str struct that will be
1074   *               be filled with the conversion result)
1075   * @param rv   - rvalue to be converted
1076   * @param cache - cached rv value (read-only), can be 0
1077   * @param tmp_cache - used for temporary storage (so that tmpv will not
1078   *                 point to possible freed data), it must be non-null,
1079   *                 initialized and cleaned afterwards.
1080   * @return 0 on success, <0 on error and EXPR_DROP on drop
1081  */
1082 int rval_get_tmp_str(struct run_act_ctx* h, struct sip_msg* msg,
1083                                                                 str* tmpv, struct rvalue* rv,
1084                                                                 struct rval_cache* cache,
1085                                                                 struct rval_cache* tmp_cache)
1086 {
1087         avp_t* r_avp;
1088         int i;
1089         
1090         switch(rv->type){
1091                 case RV_INT:
1092                         tmpv->s=int2str(rv->v.l, &tmpv->len);
1093                         break;
1094                 case RV_STR:
1095                         *tmpv=rv->v.s;
1096                         break;
1097                 case RV_ACTION_ST:
1098                         if (rv->v.action)
1099                                 i=(run_actions(h, rv->v.action, msg)>0);
1100                         else
1101                                 i=0;
1102                         tmpv->s=int2str(i, &tmpv->len);
1103                         break;
1104                 case RV_BEXPR:
1105                         i=eval_expr(h, rv->v.bexpr, msg);
1106                         if (i==EXPR_DROP){
1107                                 i=0; /* false */
1108                                 tmpv->s=int2str(i, &tmpv->len);
1109                                 return EXPR_DROP;
1110                         }
1111                         tmpv->s=int2str(i, &tmpv->len);
1112                         break;
1113                 case RV_SEL:
1114                         i=run_select(tmpv, &rv->v.sel, msg);
1115                         if (unlikely(i!=0)){
1116                                 if (i<0){
1117                                         goto eval_error;
1118                                 }else { /* i>0  => undefined */
1119                                         goto undef;
1120                                 }
1121                         }
1122                         break;
1123                 case RV_AVP:
1124                         if (likely(cache && cache->cache_type==RV_CACHE_AVP)){
1125                                 if (likely(cache->val_type==RV_STR)){
1126                                         *tmpv=cache->c.avp_val.s;
1127                                 }else if (cache->val_type==RV_INT){
1128                                         i=cache->c.avp_val.n;
1129                                         tmpv->s=int2str(i, &tmpv->len);
1130                                 }else if (cache->val_type==RV_NONE){
1131                                         goto undef;
1132                                 }else goto error_cache;
1133                         }else{
1134                                 r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
1135                                                                                         &tmp_cache->c.avp_val,
1136                                                                                         rv->v.avps.index);
1137                                 if (likely(r_avp)){
1138                                         if (likely(r_avp->flags & AVP_VAL_STR)){
1139                                                 tmp_cache->cache_type=RV_CACHE_AVP;
1140                                                 tmp_cache->val_type=RV_STR;
1141                                                 *tmpv=tmp_cache->c.avp_val.s;
1142                                         }else{
1143                                                 i=tmp_cache->c.avp_val.n;
1144                                                 tmpv->s=int2str(i, &tmpv->len);
1145                                         }
1146                                 }else goto undef;
1147                         }
1148                         break;
1149                 case RV_PVAR:
1150                         if (likely(cache && cache->cache_type==RV_CACHE_PVAR)){
1151                                 if (likely(cache->val_type==RV_STR)){
1152                                         *tmpv=cache->c.pval.rs;
1153                                 }else if (cache->val_type==RV_INT){
1154                                         i=cache->c.pval.ri;
1155                                         tmpv->s=int2str(i, &tmpv->len);
1156                                 }else if (cache->val_type==RV_NONE){
1157                                         goto undef;
1158                                 }else goto error_cache;
1159                         }else{
1160                                 memset(&tmp_cache->c.pval, 0, sizeof(tmp_cache->c.pval));
1161                                 if (likely(pv_get_spec_value(msg, &rv->v.pvs,
1162                                                                                                 &tmp_cache->c.pval)==0)){
1163                                         if (likely(tmp_cache->c.pval.flags & PV_VAL_STR)){
1164                                                 /*  the value is not destroyed, but saved instead
1165                                                         in tmp_cache so that it can be destroyed later
1166                                                         when no longer needed */
1167                                                 tmp_cache->cache_type=RV_CACHE_PVAR;
1168                                                 tmp_cache->val_type=RV_STR;
1169                                                 *tmpv=tmp_cache->c.pval.rs;
1170                                         }else if (likely(tmp_cache->c.pval.flags & PV_VAL_INT)){
1171                                                 i=tmp_cache->c.pval.ri;
1172                                                 pv_value_destroy(&tmp_cache->c.pval);
1173                                                 tmpv->s=int2str(i, &tmpv->len);
1174                                         }else{
1175                                                 /* no PV_VAL_STR and no PV_VAL_INT => undef
1176                                                    (PV_VAL_NULL) */
1177                                                 pv_value_destroy(&tmp_cache->c.pval);
1178                                                 goto undef;
1179                                         }
1180                                 }else{
1181                                         goto eval_error;
1182                                 }
1183                         }
1184                         break;
1185                 default:
1186                         BUG("rv type %d not handled\n", rv->type);
1187                         goto error;
1188         }
1189         return 0;
1190 undef:
1191 eval_error: /* same as undefined */
1192         /* handle undefined => result "", return success */
1193         tmpv->s="";
1194         tmpv->len=0;
1195         return 0;
1196 error_cache:
1197         BUG("invalid cached value:cache type %d, value type %d\n",
1198                         cache?cache->cache_type:0, cache?cache->val_type:0);
1199 error:
1200         tmpv->s="";
1201         tmpv->len=0;
1202         return -1;
1203 }
1204
1205
1206
1207 /** get the string value of an rv.
1208   * *s=(str)rv
1209   * The result is pkg malloc'ed (so it should be pkg_free()'ed when finished.
1210   * @return 0 on success, <0 on error and EXPR_DROP on drop
1211  */
1212 int rval_get_str(struct run_act_ctx* h, struct sip_msg* msg,
1213                                                                 str* s, struct rvalue* rv,
1214                                                                 struct rval_cache* cache)
1215 {
1216         str tmp;
1217         struct rval_cache tmp_cache;
1218         
1219         rval_cache_init(&tmp_cache);
1220         if (unlikely(rval_get_tmp_str(h, msg, &tmp, rv, cache, &tmp_cache)<0))
1221                 goto error;
1222         s->s=pkg_malloc(tmp.len+1/* 0 term */);
1223         if (unlikely(s->s==0)){
1224                 ERR("memory allocation error\n");
1225                 goto error;
1226         }
1227         s->len=tmp.len;
1228         memcpy(s->s, tmp.s, tmp.len);
1229         s->s[tmp.len]=0; /* 0 term */
1230         rval_cache_clean(&tmp_cache);
1231         return 0;
1232 error:
1233         rval_cache_clean(&tmp_cache);
1234         return -1;
1235 }
1236
1237
1238
1239 /** convert a rvalue to another rvalue, of a specific type.
1240  *
1241  * The result is read-only in most cases (can be a reference
1242  * to another rvalue, can be checked by using rv_chg_in_place()) and
1243  * _must_ be rval_destroy()'ed.
1244  *
1245  * @param type - type to convert to
1246  * @param v - rvalue to convert
1247  * @param c - rval_cache (cached v value if known/filled by another
1248  *            function), can be 0 (unknown/not needed)
1249  * @return pointer to a rvalue (reference to an existing one or a new
1250  *   one, @see rv_chg_in_place() and the above comment) or 0 on error.
1251  */
1252 struct rvalue* rval_convert(struct run_act_ctx* h, struct sip_msg* msg,
1253                                                         enum rval_type type, struct rvalue* v,
1254                                                         struct rval_cache* c)
1255 {
1256         int i;
1257         struct rval_cache tmp_cache;
1258         str tmp;
1259         struct rvalue* ret;
1260         union rval_val val;
1261         
1262         if (v->type==type){
1263                 rv_ref(v);
1264                 return v;
1265         }
1266         switch(type){
1267                 case RV_INT:
1268                         if (unlikely(rval_get_int(h, msg, &i, v, c) < 0))
1269                                 return 0;
1270                         val.l=i;
1271                         return rval_new(RV_INT, &val, 0);
1272                 case RV_STR:
1273                         rval_cache_init(&tmp_cache);
1274                         if (unlikely(rval_get_tmp_str(h, msg, &tmp, v, c, &tmp_cache) < 0))
1275                         {
1276                                 rval_cache_clean(&tmp_cache);
1277                                 return 0;
1278                         }
1279                         ret=rval_new_str(&tmp, RV_STR_EXTRA);
1280                         rval_cache_clean(&tmp_cache);
1281                         return ret;
1282                 case RV_NONE:
1283                 default:
1284                         BUG("unsupported conversion to type %d\n", type);
1285                         return 0;
1286         }
1287         return 0;
1288 }
1289
1290
1291
1292 /** integer operation: *res= op v.
1293   * @return 0 on succes, \<0 on error
1294   */
1295 inline static int int_intop1(int* res, enum rval_expr_op op, int v)
1296 {
1297         switch(op){
1298                 case RVE_UMINUS_OP:
1299                         *res=-v;
1300                         break;
1301                 case RVE_BOOL_OP:
1302                         *res=!!v;
1303                         break;
1304                 case RVE_LNOT_OP:
1305                         *res=!v;
1306                         break;
1307                 default:
1308                         BUG("rv unsupported intop1 %d\n", op);
1309                         return -1;
1310         }
1311         return 0;
1312 }
1313
1314
1315
1316 /** integer operation: *res= v1 op v2
1317   * @return 0 on succes, \<0 on error
1318   */
1319 inline static int int_intop2(int* res, enum rval_expr_op op, int v1, int v2)
1320 {
1321         switch(op){
1322                 case RVE_PLUS_OP:
1323                 case RVE_IPLUS_OP:
1324                         *res=v1+v2;
1325                         break;
1326                 case RVE_MINUS_OP:
1327                         *res=v1-v2;
1328                         break;
1329                 case RVE_MUL_OP:
1330                         *res=v1*v2;
1331                         break;
1332                 case RVE_DIV_OP:
1333                         if (unlikely(v2==0)){
1334                                 ERR("rv div by 0\n");
1335                                 return -1;
1336                         }
1337                         *res=v1/v2;
1338                         break;
1339                 case RVE_MOD_OP:
1340                         if (unlikely(v2==0)){
1341                                 ERR("rv mod by 0\n");
1342                                 return -1;
1343                         }
1344                         *res=v1%v2;
1345                         break;
1346                 case RVE_BOR_OP:
1347                         *res=v1|v2;
1348                         break;
1349                 case RVE_BAND_OP:
1350                         *res=v1&v2;
1351                         break;
1352                 case RVE_LAND_OP:
1353                         *res=v1 && v2;
1354                         break;
1355                 case RVE_LOR_OP:
1356                         *res=v1 || v2;
1357                         break;
1358                 case RVE_GT_OP:
1359                         *res=v1 > v2;
1360                         break;
1361                 case RVE_GTE_OP:
1362                         *res=v1 >= v2;
1363                         break;
1364                 case RVE_LT_OP:
1365                         *res=v1 < v2;
1366                         break;
1367                 case RVE_LTE_OP:
1368                         *res=v1 <= v2;
1369                         break;
1370                 case RVE_EQ_OP:
1371                 case RVE_IEQ_OP:
1372                         *res=v1 == v2;
1373                         break;
1374                 case RVE_DIFF_OP:
1375                 case RVE_IDIFF_OP:
1376                         *res=v1 != v2;
1377                         break;
1378                 case RVE_CONCAT_OP:
1379                         *res=0;
1380                         /* invalid operand for int */
1381                         return -1;
1382                 default:
1383                         BUG("rv unsupported intop %d\n", op);
1384                         return -1;
1385         }
1386         return 0;
1387 }
1388
1389
1390
1391 /** internal helper: compare 2 RV_STR RVs.
1392   * Warning: rv1 & rv2 must be RV_STR
1393   * @return 0 on success, -1 on error
1394   */
1395 inline static int bool_rvstrop2( enum rval_expr_op op, int* res,
1396                                                                 struct rvalue* rv1, struct rvalue* rv2)
1397 {
1398         str* s1;
1399         str* s2;
1400         regex_t tmp_re;
1401         
1402         s1=&rv1->v.s;
1403         s2=&rv2->v.s;
1404         switch(op){
1405                 case RVE_EQ_OP:
1406                 case RVE_STREQ_OP:
1407                         *res= (s1->len==s2->len) && (memcmp(s1->s, s2->s, s1->len)==0);
1408                         break;
1409                 case RVE_DIFF_OP:
1410                 case RVE_STRDIFF_OP:
1411                         *res= (s1->len!=s2->len) || (memcmp(s1->s, s2->s, s1->len)!=0);
1412                         break;
1413                 case RVE_MATCH_OP:
1414                         if (likely(rv2->flags & RV_RE_F)){
1415                                 *res=(regexec(rv2->v.re.regex, rv1->v.s.s, 0, 0, 0)==0);
1416                         }else{
1417                                 /* we need to compile the RE on the fly */
1418                                 if (unlikely(regcomp(&tmp_re, s2->s,
1419                                                                                 REG_EXTENDED|REG_NOSUB|REG_ICASE))){
1420                                         /* error */
1421                                         ERR("Bad regular expression \"%s\"\n", s2->s);
1422                                         goto error;
1423                                 }
1424                                 *res=(regexec(&tmp_re, s1->s, 0, 0, 0)==0);
1425                                 regfree(&tmp_re);
1426                         }
1427                         break;
1428                 default:
1429                         BUG("rv unsupported intop %d\n", op);
1430                         goto error;
1431         }
1432         return 0;
1433 error:
1434         *res=0; /* false */
1435         return -1;
1436 }
1437
1438
1439
1440 /** integer returning operation on string: *res= op str (returns integer)
1441   * @return 0 on succes, \<0 on error
1442   */
1443 inline static int int_strop1(int* res, enum rval_expr_op op, str* s1)
1444 {
1445         switch(op){
1446                 case RVE_STRLEN_OP:
1447                         *res=s1->len;
1448                         break;
1449                 case RVE_STREMPTY_OP:
1450                         *res=(s1->len==0);
1451                         break;
1452                 default:
1453                         BUG("rv unsupported int_strop1 %d\n", op);
1454                         *res=0;
1455                         return -1;
1456         }
1457         return 0;
1458 }
1459
1460
1461
1462 /** integer operation: ret= op v (returns a rvalue).
1463  * @return rvalue on success, 0 on error
1464  */
1465 inline static struct rvalue* rval_intop1(struct run_act_ctx* h,
1466                                                                                         struct sip_msg* msg,
1467                                                                                         enum rval_expr_op op,
1468                                                                                         struct rvalue* v)
1469 {
1470         struct rvalue* rv2;
1471         struct rvalue* ret;
1472         int i;
1473         
1474         i=0;
1475         rv2=rval_convert(h, msg, RV_INT, v, 0);
1476         if (unlikely(rv2==0)){
1477                 ERR("rval int conversion failed\n");
1478                 goto error;
1479         }
1480         if (unlikely(int_intop1(&i, op, rv2->v.l)<0))
1481                 goto error;
1482         if (rv_chg_in_place(rv2)){
1483                 ret=rv2;
1484                 rv_ref(ret);
1485         }else if (rv_chg_in_place(v)){
1486                 ret=v;
1487                 rv_ref(ret);
1488         }else{
1489                 ret=rval_new(RV_INT, &rv2->v, 0);
1490                 if (unlikely(ret==0)){
1491                         ERR("eval out of memory\n");
1492                         goto error;
1493                 }
1494         }
1495         rval_destroy(rv2);
1496         ret->v.l=i;
1497         return ret;
1498 error:
1499         rval_destroy(rv2);
1500         return 0;
1501 }
1502
1503
1504
1505 /** integer operation: ret= l op r (returns a rvalue).
1506  * @return rvalue on success, 0 on error
1507  */
1508 inline static struct rvalue* rval_intop2(struct run_act_ctx* h,
1509                                                                                         struct sip_msg* msg,
1510                                                                                         enum rval_expr_op op,
1511                                                                                         struct rvalue* l,
1512                                                                                         struct rvalue* r)
1513 {
1514         struct rvalue* rv1;
1515         struct rvalue* rv2;
1516         struct rvalue* ret;
1517         int i;
1518
1519         rv2=rv1=0;
1520         ret=0;
1521         if ((rv1=rval_convert(h, msg, RV_INT, l, 0))==0)
1522                 goto error;
1523         if ((rv2=rval_convert(h, msg, RV_INT, r, 0))==0)
1524                 goto error;
1525         if (unlikely(int_intop2(&i, op, rv1->v.l, rv2->v.l)<0))
1526                 goto error;
1527         if (rv_chg_in_place(rv1)){
1528                 /* try reusing rv1 */
1529                 ret=rv1;
1530                 rv_ref(ret);
1531         }else if (rv_chg_in_place(rv2)){
1532                 /* try reusing rv2 */
1533                 ret=rv2;
1534                 rv_ref(ret);
1535         }else if ((l->type==RV_INT) && (rv_chg_in_place(l))){
1536                 ret=l;
1537                 rv_ref(ret);
1538         } else if ((r->type==RV_INT) && (rv_chg_in_place(r))){
1539                 ret=r;
1540                 rv_ref(ret);
1541         }else{
1542                 ret=rval_new(RV_INT, &rv1->v, 0);
1543                 if (unlikely(ret==0)){
1544                         ERR("rv eval out of memory\n");
1545                         goto error;
1546                 }
1547         }
1548         rval_destroy(rv1); 
1549         rval_destroy(rv2); 
1550         ret->v.l=i;
1551         return ret;
1552 error:
1553         rval_destroy(rv1); 
1554         rval_destroy(rv2); 
1555         return 0;
1556 }
1557
1558
1559
1560 /** string add operation: ret= l . r (returns a rvalue).
1561  * Can use cached rvalues (c1 & c2).
1562  * @return rvalue on success, 0 on error
1563  */
1564 inline static struct rvalue* rval_str_add2(struct run_act_ctx* h,
1565                                                                                         struct sip_msg* msg,
1566                                                                                         struct rvalue* l,
1567                                                                                         struct rval_cache* c1,
1568                                                                                         struct rvalue* r,
1569                                                                                         struct rval_cache* c2
1570                                                                                         )
1571 {
1572         struct rvalue* rv1;
1573         struct rvalue* rv2;
1574         struct rvalue* ret;
1575         str* s1;
1576         str* s2;
1577         str tmp;
1578         short flags;
1579         int len;
1580         
1581         rv2=rv1=0;
1582         ret=0;
1583         flags=0;
1584         s1=0;
1585         s2=0;
1586         if ((rv1=rval_convert(h, msg, RV_STR, l, c1))==0)
1587                 goto error;
1588         if ((rv2=rval_convert(h, msg, RV_STR, r, c2))==0)
1589                 goto error;
1590         
1591         len=rv1->v.s.len + rv2->v.s.len + 1 /* 0 */;
1592         
1593         if (rv_chg_in_place(rv1) && (rv1->bsize>=len)){
1594                 /* try reusing rv1 */
1595                 ret=rv1;
1596                 rv_ref(ret);
1597                 s2=&rv2->v.s;
1598                 if (ret->v.s.s == &ret->buf[0]) s1=0;
1599                 else{
1600                         tmp=ret->v.s;
1601                         flags=ret->flags;
1602                         ret->flags &= ~RV_CNT_ALLOCED_F;
1603                         ret->v.s.s=&ret->buf[0];
1604                         ret->v.s.len=0;
1605                         s1=&tmp;
1606                 }
1607         }else if (rv_chg_in_place(rv2) && (rv2->bsize>=len)){
1608                 /* try reusing rv2 */
1609                 ret=rv2;
1610                 rv_ref(ret);
1611                 s1=&rv1->v.s;
1612                 if (ret->v.s.s == &ret->buf[0]) 
1613                         s2=&ret->v.s;
1614                 else{
1615                         tmp=ret->v.s;
1616                         flags=ret->flags;
1617                         ret->flags &= ~RV_CNT_ALLOCED_F;
1618                         ret->v.s.s=&ret->buf[0];
1619                         ret->v.s.len=0;
1620                         s2=&tmp;
1621                 }
1622         }else if ((l->type==RV_STR) && (rv_chg_in_place(l)) && (l->bsize>=len)){
1623                 ret=l;
1624                 rv_ref(ret);
1625                 s2=&rv2->v.s;
1626                 if (ret->v.s.s == &ret->buf[0]) s1=0;
1627                 else{
1628                         tmp=ret->v.s;
1629                         flags=ret->flags;
1630                         ret->flags &= ~RV_CNT_ALLOCED_F;
1631                         ret->v.s.s=&ret->buf[0];
1632                         ret->v.s.len=0;
1633                         s1=&tmp;
1634                 }
1635         } else if ((r->type==RV_STR) && (rv_chg_in_place(r) && (r->bsize>=len))){
1636                 ret=r;
1637                 rv_ref(ret);
1638                 s1=&rv1->v.s;
1639                 if (ret->v.s.s == &ret->buf[0]) 
1640                         s2=&ret->v.s;
1641                 else{
1642                         tmp=ret->v.s;
1643                         flags=ret->flags;
1644                         ret->flags &= ~RV_CNT_ALLOCED_F;
1645                         ret->v.s.s=&ret->buf[0];
1646                         ret->v.s.len=0;
1647                         s2=&tmp;
1648                 }
1649         }else{
1650                 ret=rval_new(RV_STR, &rv1->v, len + RV_STR_EXTRA);
1651                 if (unlikely(ret==0)){
1652                         ERR("rv eval out of memory\n");
1653                         goto error;
1654                 }
1655                 s1=0;
1656                 s2=&rv2->v.s;
1657         }
1658         /* do the actual copy */
1659         memmove(ret->buf+rv1->v.s.len, s2->s, s2->len);
1660         if (s1){
1661                 memcpy(ret->buf, s1->s, s1->len);
1662         }
1663         ret->v.s.len=rv1->v.s.len+s2->len;
1664         ret->v.s.s[ret->v.s.len]=0;
1665         /* cleanup if needed */
1666         if (flags & RV_CNT_ALLOCED_F)
1667                 pkg_free(tmp.s);
1668         rval_destroy(rv1); 
1669         rval_destroy(rv2); 
1670         return ret;
1671 error:
1672         rval_destroy(rv1); 
1673         rval_destroy(rv2); 
1674         return 0;
1675 }
1676
1677
1678
1679 /** bool operation on rval evaluated as strings.
1680  * Can use cached rvalues (c1 & c2).
1681  * @return 0 success, -1 on error
1682  */
1683 inline static int rval_str_lop2(struct run_act_ctx* h,
1684                                                  struct sip_msg* msg,
1685                                                  int* res,
1686                                                  enum rval_expr_op op,
1687                                                  struct rvalue* l,
1688                                                  struct rval_cache* c1,
1689                                                  struct rvalue* r,
1690                                                  struct rval_cache* c2)
1691 {
1692         struct rvalue* rv1;
1693         struct rvalue* rv2;
1694         int ret;
1695         
1696         rv2=rv1=0;
1697         ret=0;
1698         if ((rv1=rval_convert(h, msg, RV_STR, l, c1))==0)
1699                 goto error;
1700         if ((rv2=rval_convert(h, msg, RV_STR, r, c2))==0)
1701                 goto error;
1702         ret=bool_rvstrop2(op, res, rv1, rv2);
1703         rval_destroy(rv1); 
1704         rval_destroy(rv2); 
1705         return ret;
1706 error:
1707         rval_destroy(rv1); 
1708         rval_destroy(rv2); 
1709         return 0;
1710 }
1711
1712
1713
1714 /** integer operation on rval evaluated as string.
1715  * Can use cached rvalues (c1 & c2).
1716  * @param res - will be set to the result
1717  * @return 0 success, -1 on error
1718  */
1719 inline static int rval_int_strop1(struct run_act_ctx* h,
1720                                                  struct sip_msg* msg,
1721                                                  int* res,
1722                                                  enum rval_expr_op op,
1723                                                  struct rvalue* l,
1724                                                  struct rval_cache* c1)
1725 {
1726         struct rvalue* rv1;
1727         int ret;
1728         
1729         rv1=0;
1730         ret=0;
1731         if ((rv1=rval_convert(h, msg, RV_STR, l, c1))==0)
1732                 goto error;
1733         ret=int_strop1(res, op, &rv1->v.s);
1734         rval_destroy(rv1); 
1735         return ret;
1736 error:
1737         *res=0;
1738         rval_destroy(rv1); 
1739         return -1;
1740 }
1741
1742
1743
1744 /** checks if rv is defined.
1745  * @param res - set to the result 1 - defined, 0 not defined
1746  * @return 0 on success, -1 on error
1747  * Can use cached rvalues (cache).
1748  * Note: a rv can be undefined if it's an undefined avp or pvar or select or
1749  * if it's NONE
1750  * Note2: an error in the avp, pvar or select search is equivalent to 
1751  *  undefined (and it's not reported)
1752  */
1753 inline static int rv_defined(struct run_act_ctx* h,
1754                                                  struct sip_msg* msg, int* res,
1755                                                  struct rvalue* rv, struct rval_cache* cache)
1756 {
1757         avp_t* r_avp;
1758         int_str avp_val;
1759         pv_value_t pval;
1760         str tmp;
1761         
1762         *res=1;
1763         switch(rv->type){
1764                 case RV_SEL:
1765                         if (unlikely(cache && cache->cache_type==RV_CACHE_SELECT)){
1766                                 *res=(cache->val_type!=RV_NONE);
1767                         }else
1768                                 /* run select returns 0 on success, -1 on error and >0 on 
1769                                    undefined. error is considered undefined */
1770                                 *res=(run_select(&tmp, &rv->v.sel, msg)==0);
1771                         break;
1772                 case RV_AVP:
1773                         if (unlikely(cache && cache->cache_type==RV_CACHE_AVP)){
1774                                 *res=(cache->val_type!=RV_NONE);
1775                         }else{
1776                                 r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
1777                                                                                         &avp_val, rv->v.avps.index);
1778                                 if (unlikely(r_avp==0)){
1779                                         *res=0;
1780                                 }
1781                         }
1782                         break;
1783                 case RV_PVAR:
1784                         /* PV_VAL_NULL or pv_get_spec_value error => undef */
1785                         if (unlikely(cache && cache->cache_type==RV_CACHE_PVAR)){
1786                                 *res=(cache->val_type!=RV_NONE);
1787                         }else{
1788                                 memset(&pval, 0, sizeof(pval));
1789                                 if (likely(pv_get_spec_value(msg, &rv->v.pvs, &pval)==0)){
1790                                         if ((pval.flags & PV_VAL_NULL) &&
1791                                                         ! (pval.flags & (PV_VAL_INT|PV_VAL_STR))){
1792                                                 *res=0;
1793                                         }
1794                                         pv_value_destroy(&pval);
1795                                 }else{
1796                                         *res=0; /* in case of error, consider it undef */
1797                                 }
1798                         }
1799                         break;
1800                 case RV_NONE:
1801                         *res=0;
1802                         break;
1803                 default:
1804                         break;
1805         }
1806         return 0;
1807 }
1808
1809
1810 /** defined (integer) operation on rve.
1811  * @param res - set to  1 defined, 0 not defined
1812  * @return - 0 on success, -1 on error
1813  */
1814 inline static int int_rve_defined(struct run_act_ctx* h,
1815                                                  struct sip_msg* msg, int* res,
1816                                                  struct rval_expr* rve)
1817 {
1818         /* only a rval can be undefined, any expression consisting on more
1819            then one rval => defined */
1820         if (likely(rve->op==RVE_RVAL_OP))
1821                 return rv_defined(h, msg, res, &rve->left.rval, 0);
1822         *res=1;
1823         return 0;
1824 }
1825
1826
1827
1828 /** evals an integer expr  to an int.
1829  * 
1830  *  *res=(int)eval(rve)
1831  *  @return 0 on success, \<0 on error
1832  */
1833 int rval_expr_eval_int( struct run_act_ctx* h, struct sip_msg* msg,
1834                                                 int* res, struct rval_expr* rve)
1835 {
1836         int i1, i2, ret;
1837         struct rval_cache c1, c2;
1838         struct rvalue* rv1;
1839         struct rvalue* rv2;
1840         
1841         ret=-1;
1842         switch(rve->op){
1843                 case RVE_RVAL_OP:
1844                         ret=rval_get_int(h, msg, res,  &rve->left.rval, 0);
1845                         rval_get_int_handle_ret(ret, "rval expression conversion to int"
1846                                                                                 " failed", rve);
1847                         break;
1848                 case RVE_UMINUS_OP:
1849                 case RVE_BOOL_OP:
1850                 case RVE_LNOT_OP:
1851                         if (unlikely(
1852                                         (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1853                                 break;
1854                         ret=int_intop1(res, rve->op, i1);
1855                         break;
1856                 case RVE_INT_OP:
1857                         ret=rval_expr_eval_int(h, msg, res, rve->left.rve);
1858                         break;
1859                 case RVE_MUL_OP:
1860                 case RVE_DIV_OP:
1861                 case RVE_MOD_OP:
1862                 case RVE_MINUS_OP:
1863                 case RVE_PLUS_OP:
1864                 case RVE_IPLUS_OP:
1865                 case RVE_BOR_OP:
1866                 case RVE_BAND_OP:
1867                 case RVE_GT_OP:
1868                 case RVE_GTE_OP:
1869                 case RVE_LT_OP:
1870                 case RVE_LTE_OP:
1871                 case RVE_IEQ_OP:
1872                 case RVE_IDIFF_OP:
1873                         if (unlikely(
1874                                         (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1875                                 break;
1876                         if (unlikely(
1877                                         (ret=rval_expr_eval_int(h, msg, &i2, rve->right.rve)) <0) )
1878                                 break;
1879                         ret=int_intop2(res, rve->op, i1, i2);
1880                         break;
1881                 case RVE_LAND_OP:
1882                         if (unlikely(
1883                                         (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1884                                 break;
1885                         if (i1==0){
1886                                 *res=0;
1887                         }else{
1888                                 if (unlikely( (ret=rval_expr_eval_int(h, msg, &i2,
1889                                                                                 rve->right.rve)) <0) )
1890                                         break;
1891                                 *res=i1 && i2;
1892                         }
1893                         ret=0;
1894                         break;
1895                 case RVE_LOR_OP:
1896                         if (unlikely(
1897                                         (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1898                                 break;
1899                         if (i1){
1900                                 *res=1;
1901                         }else{
1902                                 if (unlikely( (ret=rval_expr_eval_int(h, msg, &i2,
1903                                                                                 rve->right.rve)) <0) )
1904                                         break;
1905                                 *res=i1 || i2;
1906                         }
1907                         ret=0;
1908                         break;
1909                 case RVE_EQ_OP:
1910                 case RVE_DIFF_OP:
1911                         /* if left is string, eval left & right as string and
1912                          *   use string diff.
1913                          * if left is int eval as int using int diff
1914                          * if left is undef, look at right and convert to right type
1915                          */
1916                         rval_cache_init(&c1);
1917                         if (unlikely( (ret=rval_expr_eval_rvint(h, msg, &rv1, &i1,
1918                                                                                                         rve->left.rve, &c1))<0)){
1919                                 /* error */
1920                                 rval_cache_clean(&c1);
1921                                 break;
1922                         }
1923                         if (likely(rv1==0)){
1924                                 /* int */
1925                                 rval_cache_clean(&c1);
1926                                 if (unlikely( (ret=rval_expr_eval_int(h, msg, &i2,
1927                                                                                                                 rve->right.rve)) <0) )
1928                                         break;  /* error */
1929                                 ret=int_intop2(res, rve->op, i1, i2);
1930                         }else{
1931                                 /* not int => str or undef */
1932                                 /* check for undefined left operand */
1933                                 if (unlikely( c1.cache_type!=RV_CACHE_EMPTY &&
1934                                                                 c1.val_type==RV_NONE)){
1935 #ifdef UNDEF_EQ_ALWAYS_FALSE
1936                                         /* undef == something  always false
1937                                            undef != something  always true*/
1938                                         ret=(rve->op==RVE_DIFF_OP);
1939 #elif defined UNDEF_EQ_UNDEF_TRUE
1940                                         /* undef == something defined always false
1941                                            undef == undef true */
1942                                         if (int_rve_defined(h, msg, &i2, rve->right.rve)<0){
1943                                                 /* error */
1944                                                 rval_cache_clean(&c1);
1945                                                 rval_destroy(rv1);
1946                                                 break;
1947                                         }
1948                                         ret=(!i2) ^ (rve->op==RVE_DIFF_OP);
1949 #else  /* ! UNDEF_EQ_* */
1950                                         /*  undef == val
1951                                          *  => convert to (type_of(val)) (undef) == val */
1952                                         rval_cache_init(&c2);
1953                                         if (unlikely( (ret=rval_expr_eval_rvint(h, msg, &rv2, &i2,
1954                                                                                                         rve->right.rve, &c2))<0)){
1955                                                 /* error */
1956                                                 rval_cache_clean(&c1);
1957                                                 rval_cache_clean(&c2);
1958                                                 rval_destroy(rv1);
1959                                                 break;
1960                                         }
1961                                         if (rv2==0){
1962                                                 /* int */
1963                                                 ret=int_intop2(res, rve->op, 0 /* undef */, i2);
1964                                         }else{
1965                                                 /* str or undef */
1966                                                 ret=rval_str_lop2(h, msg, res, rve->op, rv1, &c1,
1967                                                                                         rv2, &c2);
1968                                                 rval_cache_clean(&c2);
1969                                                 rval_destroy(rv2);
1970                                         }
1971 #endif /* UNDEF_EQ_* */
1972                                         rval_cache_clean(&c1);
1973                                         rval_destroy(rv1);
1974                                 }else{
1975                                         /* left value == defined and != int => str
1976                                          * => lval == (str) val */
1977                                         if (unlikely((rv2=rval_expr_eval(h, msg,
1978                                                                                                                 rve->right.rve))==0)){
1979                                                 /* error */
1980                                                 rval_destroy(rv1);
1981                                                 rval_cache_clean(&c1);
1982                                                 break;
1983                                         }
1984                                         ret=rval_str_lop2(h, msg, res, rve->op, rv1, &c1, rv2, 0);
1985                                         rval_cache_clean(&c1);
1986                                         rval_destroy(rv1);
1987                                         rval_destroy(rv2);
1988                                 }
1989                         }
1990                         break;
1991                 case RVE_CONCAT_OP:
1992                         /* eval expression => string */
1993                         if (unlikely((rv1=rval_expr_eval(h, msg, rve))==0)){
1994                                 ret=-1;
1995                                 break;
1996                         }
1997                         /* convert to int */
1998                         ret=rval_get_int(h, msg, res, rv1, 0); /* convert to int */
1999                         rval_get_int_handle_ret(ret, "rval expression conversion to int"
2000                                                                                 " failed", rve);
2001                         rval_destroy(rv1);
2002                         break;
2003                 case RVE_STR_OP:
2004                         /* (str)expr => eval expression */
2005                         rval_cache_init(&c1);
2006                         if (unlikely((ret=rval_expr_eval_rvint(h, msg, &rv1, res,
2007                                                                                                         rve->left.rve, &c1))<0)){
2008                                 /* error */
2009                                 rval_cache_clean(&c1);
2010                                 break;
2011                         }
2012                         if (unlikely(rv1)){
2013                                 /* expr evaluated to string => (int)(str)v == (int)v */
2014                                 ret=rval_get_int(h, msg, res, rv1, &c1); /* convert to int */
2015                                 rval_get_int_handle_ret(ret, "rval expression conversion"
2016                                                                                                 " to int failed", rve);
2017                                 rval_destroy(rv1);
2018                                 rval_cache_clean(&c1);
2019                         } /* else (rv1==0)
2020                                  => expr evaluated to int => 
2021                                  return (int)(str)v == (int)v => do nothing */
2022                         break;
2023
2024 #if 0
2025                         /* same thing as above, but in a not optimized, easier to
2026                            understand way */
2027                         /* 1. (str) expr => eval expr */
2028                         if (unlikely((rv1=rval_expr_eval(h, msg, rve->left.rve))==0)){
2029                                 ret=-1;
2030                                 break;
2031                         }
2032                         /* 2. convert to str and then convert to int
2033                            but since (int)(str)v == (int)v skip over (str)v */
2034                         ret=rval_get_int(h, msg, res, rv1, 0); /* convert to int */
2035                         rval_destroy(rv1);
2036                         break;
2037 #endif
2038                 case RVE_DEFINED_OP:
2039                         ret=int_rve_defined(h, msg, res, rve->left.rve);
2040                         break;
2041                 case RVE_STREQ_OP:
2042                 case RVE_STRDIFF_OP:
2043                 case RVE_MATCH_OP:
2044                         if (unlikely((rv1=rval_expr_eval(h, msg, rve->left.rve))==0)){
2045                                 ret=-1;
2046                                 break;
2047                         }
2048                         if (unlikely((rv2=rval_expr_eval(h, msg, rve->right.rve))==0)){
2049                                 rval_destroy(rv1);
2050                                 ret=-1;
2051                                 break;
2052                         }
2053                         ret=rval_str_lop2(h, msg, res, rve->op, rv1, 0, rv2, 0);
2054                         rval_destroy(rv1);
2055                         rval_destroy(rv2);
2056                         break;
2057                 case RVE_STRLEN_OP:
2058                 case RVE_STREMPTY_OP:
2059                         if (unlikely((rv1=rval_expr_eval(h, msg, rve->left.rve))==0)){
2060                                         ret=-1;
2061                                         break;
2062                         }
2063                         ret=rval_int_strop1(h, msg, res, rve->op, rv1, 0);
2064                         rval_destroy(rv1);
2065                         break;
2066                 case RVE_NONE_OP:
2067                 /*default:*/
2068                         BUG("invalid rval int expression operation %d (%d,%d-%d,%d)\n",
2069                                         rve->op, rve->fpos.s_line, rve->fpos.s_col,
2070                                         rve->fpos.e_line, rve->fpos.e_col);
2071                         ret=-1;
2072         };
2073         return ret;
2074 }
2075
2076
2077
2078 /** evals a rval expr. into an int or another rv(str).
2079  * WARNING: rv result (rv_res) must be rval_destroy()'ed if non-null
2080  * (it might be a reference to another rval). The result can be
2081  * modified only if rv_chg_in_place() returns true.
2082  * @param res_rv - pointer to rvalue result, if non-null it means the 
2083  *                 expression evaluated to a non-int (str), which will be
2084  *                 stored here.
2085  * @param res_i  - pointer to int result, if res_rv==0 and the function
2086  *                 returns success => the result is an int which will be 
2087  *                 stored here.
2088  * @param rve    - expression that will be evaluated.
2089  * @param cache  - write-only value cache, it might be filled if non-null and
2090  *                 empty (rval_cache_init()). If non-null, it _must_ be 
2091  *                 rval_cache_clean()'ed when done. 
2092  *
2093  * @result  0 on success, -1 on error,  sets *res_rv or *res_i.
2094  */
2095 int rval_expr_eval_rvint(                          struct run_act_ctx* h,
2096                                                                            struct sip_msg* msg,
2097                                                                            struct rvalue** res_rv,
2098                                                                            int* res_i,
2099                                                                            struct rval_expr* rve,
2100                                                                            struct rval_cache* cache
2101                                                                            )
2102 {
2103         struct rvalue* rv1;
2104         struct rvalue* rv2;
2105         struct rval_cache c1; /* local cache */
2106         int ret;
2107         int r, i, j;
2108         enum rval_type type;
2109         
2110         rv1=0;
2111         rv2=0;
2112         ret=-1;
2113         switch(rve->op){
2114                 case RVE_RVAL_OP:
2115                         rv1=&rve->left.rval;
2116                         rv_ref(rv1);
2117                         type=rval_get_btype(h, msg, rv1, cache);
2118                         if (type==RV_INT){
2119                                         r=rval_get_int(h, msg, res_i, rv1, cache);
2120                                         rval_get_int_handle_ret(r, "rval expression conversion"
2121                                                                                                 " to int failed", rve);
2122                                         *res_rv=0;
2123                                         ret=r; /* equiv. to if (r<0) goto error */
2124                         }else{
2125                                 /* RV_STR, RV_PVAR, RV_AVP a.s.o => return rv1 and the 
2126                                    cached resolved value in cache*/
2127                                         *res_rv=rv1;
2128                                         rv_ref(rv1);
2129                                         ret=0;
2130                         }
2131                         break;
2132                 case RVE_UMINUS_OP:
2133                 case RVE_BOOL_OP:
2134                 case RVE_LNOT_OP:
2135                 case RVE_MINUS_OP:
2136                 case RVE_MUL_OP:
2137                 case RVE_DIV_OP:
2138                 case RVE_MOD_OP:
2139                 case RVE_BOR_OP:
2140                 case RVE_BAND_OP:
2141                 case RVE_LAND_OP:
2142                 case RVE_LOR_OP:
2143                 case RVE_GT_OP:
2144                 case RVE_GTE_OP:
2145                 case RVE_LT_OP:
2146                 case RVE_LTE_OP:
2147                 case RVE_EQ_OP:
2148                 case RVE_DIFF_OP:
2149                 case RVE_IEQ_OP:
2150                 case RVE_IDIFF_OP:
2151                 case RVE_IPLUS_OP:
2152                 case RVE_STREQ_OP:
2153                 case RVE_STRDIFF_OP:
2154                 case RVE_MATCH_OP:
2155                 case RVE_STRLEN_OP:
2156                 case RVE_STREMPTY_OP:
2157                 case RVE_DEFINED_OP:
2158                 case RVE_INT_OP:
2159                         /* operator forces integer type */
2160                         ret=rval_expr_eval_int(h, msg, res_i, rve);
2161                         *res_rv=0;
2162                         break;
2163                 case RVE_PLUS_OP:
2164                         rval_cache_init(&c1);
2165                         r=rval_expr_eval_rvint(h, msg, &rv1, &i, rve->left.rve, &c1);
2166                         if (unlikely(r<0)){
2167                                 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2168                                                 rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
2169                                                 rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col
2170                                         );
2171                                 rval_cache_clean(&c1);
2172                                 goto error;
2173                         }
2174                         if (rv1==0){
2175                                 if (unlikely((r=rval_expr_eval_int(h, msg, &j,
2176                                                                                                                 rve->right.rve))<0)){
2177                                                 ERR("rval expression evaluation failed (%d,%d-%d,%d)"
2178                                                                 "\n", rve->right.rve->fpos.s_line,
2179                                                                 rve->right.rve->fpos.s_col,
2180                                                                 rve->right.rve->fpos.e_line,
2181                                                                 rve->right.rve->fpos.e_col);
2182                                                 rval_cache_clean(&c1);
2183                                                 goto error;
2184                                 }
2185                                 ret=int_intop2(res_i, rve->op, i, j);
2186                                 *res_rv=0;
2187                         }else{
2188                                 rv2=rval_expr_eval(h, msg, rve->right.rve);
2189                                 if (unlikely(rv2==0)){
2190                                         ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2191                                                                 rve->right.rve->fpos.s_line,
2192                                                                 rve->right.rve->fpos.s_col,
2193                                                                 rve->right.rve->fpos.e_line,
2194                                                                 rve->right.rve->fpos.e_col);
2195                                         rval_cache_clean(&c1);
2196                                         goto error;
2197                                 }
2198                                 *res_rv=rval_str_add2(h, msg, rv1, &c1, rv2, 0);
2199                                 ret=-(*res_rv==0);
2200                         }
2201                         rval_cache_clean(&c1);
2202                         break;
2203                 case RVE_CONCAT_OP:
2204                 case RVE_STR_OP:
2205                         *res_rv=rval_expr_eval(h, msg, rve);
2206                         ret=-(*res_rv==0);
2207                         break;
2208                 case RVE_NONE_OP:
2209                 /*default:*/
2210                         BUG("invalid rval expression operation %d (%d,%d-%d,%d)\n",
2211                                         rve->op, rve->fpos.s_line, rve->fpos.s_col,
2212                                         rve->fpos.e_line, rve->fpos.e_col);
2213                         goto error;
2214         };
2215         rval_destroy(rv1);
2216         rval_destroy(rv2);
2217         return ret;
2218 error:
2219         rval_destroy(rv1);
2220         rval_destroy(rv2);
2221         return -1;
2222 }
2223
2224
2225
2226 /** evals a rval expr..
2227  * WARNING: result must be rval_destroy()'ed if non-null (it might be
2228  * a reference to another rval). The result can be modified only
2229  * if rv_chg_in_place() returns true.
2230  * @return rvalue on success, 0 on error
2231  */
2232 struct rvalue* rval_expr_eval(struct run_act_ctx* h, struct sip_msg* msg,
2233                                                                 struct rval_expr* rve)
2234 {
2235         struct rvalue* rv1;
2236         struct rvalue* rv2;
2237         struct rvalue* ret;
2238         struct rval_cache c1;
2239         union rval_val v;
2240         int r, i, j;
2241         enum rval_type type;
2242         
2243         rv1=0;
2244         rv2=0;
2245         ret=0;
2246         switch(rve->op){
2247                 case RVE_RVAL_OP:
2248                         rv_ref(&rve->left.rval);
2249                         return &rve->left.rval;
2250                         break;
2251                 case RVE_UMINUS_OP:
2252                 case RVE_BOOL_OP:
2253                 case RVE_LNOT_OP:
2254                 case RVE_MINUS_OP:
2255                 case RVE_MUL_OP:
2256                 case RVE_DIV_OP:
2257                 case RVE_MOD_OP:
2258                 case RVE_BOR_OP:
2259                 case RVE_BAND_OP:
2260                 case RVE_LAND_OP:
2261                 case RVE_LOR_OP:
2262                 case RVE_GT_OP:
2263                 case RVE_GTE_OP:
2264                 case RVE_LT_OP:
2265                 case RVE_LTE_OP:
2266                 case RVE_EQ_OP:
2267                 case RVE_DIFF_OP:
2268                 case RVE_IEQ_OP:
2269                 case RVE_IDIFF_OP:
2270                 case RVE_IPLUS_OP:
2271                 case RVE_STREQ_OP:
2272                 case RVE_STRDIFF_OP:
2273                 case RVE_MATCH_OP:
2274                 case RVE_STRLEN_OP:
2275                 case RVE_STREMPTY_OP:
2276                 case RVE_DEFINED_OP:
2277                 case RVE_INT_OP:
2278                         /* operator forces integer type */
2279                         r=rval_expr_eval_int(h, msg, &i, rve);
2280                         if (likely(r==0)){
2281                                 v.l=i;
2282                                 ret=rval_new(RV_INT, &v, 0);
2283                                 if (unlikely(ret==0)){
2284                                         ERR("rv eval int expression: out of memory\n");
2285                                         goto error;
2286                                 }
2287                                 return ret;
2288                         }else{
2289                                 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2290                                                 rve->fpos.s_line, rve->fpos.s_col,
2291                                                 rve->fpos.e_line, rve->fpos.e_col);
2292                                 goto error;
2293                         }
2294                         break;
2295                 case RVE_PLUS_OP:
2296                         rv1=rval_expr_eval(h, msg, rve->left.rve);
2297                         if (unlikely(rv1==0)){
2298                                 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2299                                                 rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
2300                                                 rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col);
2301                                 goto error;
2302                         }
2303                         rval_cache_init(&c1);
2304                         type=rval_get_btype(h, msg, rv1, &c1);
2305                         switch(type){
2306                                 case RV_INT:
2307                                         r=rval_get_int(h, msg, &i, rv1, &c1);
2308                                         rval_get_int_handle_ret(r, "rval expression left side "
2309                                                                                                 "conversion to int failed",
2310                                                                                         rve);
2311                                         if (unlikely(r<0)){
2312                                                 rval_cache_clean(&c1);
2313                                                 goto error;
2314                                         }
2315                                         if (unlikely((r=rval_expr_eval_int(h, msg, &j,
2316                                                                                                                 rve->right.rve))<0)){
2317                                                 rval_cache_clean(&c1);
2318                                                 ERR("rval expression evaluation failed (%d,%d-%d,%d):"
2319                                                                 " could not evaluate right side to int\n",
2320                                                                 rve->fpos.s_line, rve->fpos.s_col,
2321                                                                 rve->fpos.e_line, rve->fpos.e_col);
2322                                                 goto error;
2323                                         }
2324                                         int_intop2(&r, rve->op, i, j);
2325                                         if (rv_chg_in_place(rv1)){
2326                                                 rv1->v.l=r;
2327                                                 ret=rv1;
2328                                                 rv_ref(ret);
2329                                         }else{
2330                                                 v.l=r;
2331                                                 ret=rval_new(RV_INT, &v, 0);
2332                                                 if (unlikely(ret==0)){
2333                                                         rval_cache_clean(&c1);
2334                                                         ERR("rv eval int expression: out of memory\n");
2335                                                         goto error;
2336                                                 }
2337                                         }
2338                                         break;
2339                                 case RV_STR:
2340                                 case RV_NONE:
2341                                         rv2=rval_expr_eval(h, msg, rve->right.rve);
2342                                         if (unlikely(rv2==0)){
2343                                                 ERR("rval expression evaluation failed (%d,%d-%d,%d)"
2344                                                                 "\n", rve->right.rve->fpos.s_line,
2345                                                                 rve->right.rve->fpos.s_col,
2346                                                                 rve->right.rve->fpos.e_line,
2347                                                                 rve->right.rve->fpos.e_col);
2348                                                 rval_cache_clean(&c1);
2349                                                 goto error;
2350                                         }
2351                                         ret=rval_str_add2(h, msg, rv1, &c1, rv2, 0);
2352                                         break;
2353                                 default:
2354                                         BUG("rv unsupported basic type %d (%d,%d-%d,%d)\n", type,
2355                                                         rve->fpos.s_line, rve->fpos.s_col,
2356                                                         rve->fpos.e_line, rve->fpos.e_col);
2357                         }
2358                         rval_cache_clean(&c1);
2359                         break;
2360                 case RVE_CONCAT_OP:
2361                         rv1=rval_expr_eval(h, msg, rve->left.rve);
2362                         if (unlikely(rv1==0)){
2363                                 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2364                                                 rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
2365                                                 rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col);
2366                                 goto error;
2367                         }
2368                         rv2=rval_expr_eval(h, msg, rve->right.rve);
2369                         if (unlikely(rv2==0)){
2370                                 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2371                                                 rve->right.rve->fpos.s_line,
2372                                                 rve->right.rve->fpos.s_col,
2373                                                 rve->right.rve->fpos.e_line,
2374                                                 rve->right.rve->fpos.e_col);
2375                                 goto error;
2376                         }
2377                         ret=rval_str_add2(h, msg, rv1, 0, rv2, 0);
2378                         break;
2379                 case RVE_STR_OP:
2380                         rv1=rval_expr_eval(h, msg, rve->left.rve);
2381                         if (unlikely(rv1==0)){
2382                                 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2383                                                 rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
2384                                                 rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col);
2385                                 goto error;
2386                         }
2387                         ret=rval_convert(h, msg, RV_STR, rv1, 0);
2388                         break;
2389                 case RVE_NONE_OP:
2390                 /*default:*/
2391                         BUG("invalid rval expression operation %d (%d,%d-%d,%d)\n",
2392                                         rve->op, rve->fpos.s_line, rve->fpos.s_col,
2393                                         rve->fpos.e_line, rve->fpos.e_col);
2394                         goto error;
2395         };
2396         rval_destroy(rv1);
2397         rval_destroy(rv2);
2398         return ret;
2399 error:
2400         rval_destroy(rv1);
2401         rval_destroy(rv2);
2402         return 0;
2403 }
2404
2405
2406
2407 /** evals a rval expr and always returns a new rval.
2408  * like rval_expr_eval, but always returns a new rvalue (never a reference
2409  * to an exisiting one).
2410  * WARNING: result must be rval_destroy()'ed if non-null (it might be
2411  * a reference to another rval). The result can be modified only
2412  * if rv_chg_in_place() returns true.
2413  * @result rvalue on success, 0 on error
2414  */
2415 struct rvalue* rval_expr_eval_new(struct run_act_ctx* h, struct sip_msg* msg,
2416                                                                 struct rval_expr* rve)
2417 {
2418         struct rvalue* ret;
2419         struct rvalue* rv;
2420         
2421         ret=rval_expr_eval(h, msg, rve);
2422         if (ret && !rv_chg_in_place(ret)){
2423                 rv=ret;
2424                 /* create a new rv */
2425                 ret=rval_new(rv->type, &rv->v, 0);
2426                 rval_destroy(rv);
2427         }
2428         return ret;
2429 }
2430
2431
2432
2433 /** create a RVE_RVAL_OP rval_expr, containing a single rval of the given type.
2434  *
2435  * @param rv_type - rval type
2436  * @param val     - rval value
2437  * @param pos     - config position
2438  * @return new pkg_malloc'ed rval_expr or 0 on error.
2439  */
2440 struct rval_expr* mk_rval_expr_v(enum rval_type rv_type, void* val, 
2441                                                                         struct cfg_pos* pos)
2442 {
2443         struct rval_expr* rve;
2444         union rval_val v;
2445         str* s;
2446         int flags;
2447         
2448         rve=pkg_malloc(sizeof(*rve));
2449         if (rve==0) 
2450                 return 0;
2451         memset(rve, 0, sizeof(*rve));
2452         flags=0;
2453         switch(rv_type){
2454                 case RV_INT:
2455                         v.l=(long)val;
2456                         break;
2457                 case RV_STR:
2458                         s=(str*)val;
2459                         v.s.s=pkg_malloc(s->len+1 /*0*/);
2460                         if (v.s.s==0){
2461                                 ERR("memory allocation failure\n");
2462                                 return 0;
2463                         }
2464                         v.s.len=s->len;
2465                         memcpy(v.s.s, s->s, s->len);
2466                         v.s.s[s->len]=0;
2467                         flags=RV_CNT_ALLOCED_F;
2468                         break;
2469                 case RV_AVP:
2470                         v.avps=*(avp_spec_t*)val;
2471                         break;
2472                 case RV_PVAR:
2473                         v.pvs=*(pv_spec_t*)val;
2474                         break;
2475                 case RV_SEL:
2476                         v.sel=*(select_t*)val;
2477                         break;
2478                 case RV_BEXPR:
2479                         v.bexpr=(struct expr*)val;
2480                         break;
2481                 case RV_ACTION_ST:
2482                         v.action=(struct action*)val;
2483                         break;
2484                 default:
2485                         BUG("unsupported rv type %d\n", rv_type);
2486                         pkg_free(rve);
2487                         return 0;
2488         }
2489         rval_init(&rve->left.rval, rv_type, &v, flags);
2490         rve->op=RVE_RVAL_OP;
2491         if (pos) rve->fpos=*pos;
2492         return rve;
2493 }
2494
2495
2496
2497 /** create a unary op. rval_expr..
2498  * ret= op rve1
2499  * @param op   - rval expr. unary operator
2500  * @param rve1 - rval expr. on which the operator will act.
2501  * @return new pkg_malloc'ed rval_expr or 0 on error.
2502  */
2503 struct rval_expr* mk_rval_expr1(enum rval_expr_op op, struct rval_expr* rve1,
2504                                                                 struct cfg_pos* pos)
2505 {
2506         struct rval_expr* ret;
2507         
2508         switch(op){
2509                 case RVE_UMINUS_OP:
2510                 case RVE_BOOL_OP:
2511                 case RVE_LNOT_OP:
2512                 case RVE_STRLEN_OP:
2513                 case RVE_STREMPTY_OP:
2514                 case RVE_DEFINED_OP:
2515                 case RVE_INT_OP:
2516                 case RVE_STR_OP:
2517                         break;
2518                 default:
2519                         BUG("unsupported unary operator %d\n", op);
2520                         return 0;
2521         }
2522         ret=pkg_malloc(sizeof(*ret));
2523         if (ret==0) 
2524                 return 0;
2525         memset(ret, 0, sizeof(*ret));
2526         ret->op=op;
2527         ret->left.rve=rve1;
2528         if (pos) ret->fpos=*pos;
2529         return ret;
2530 }
2531
2532
2533
2534 /** create a rval_expr. from 2 other rval exprs, using op.
2535  * ret = rve1 op rve2
2536  * @param op   - rval expr. operator
2537  * @param rve1 - rval expr. on which the operator will act.
2538  * @param rve2 - rval expr. on which the operator will act.
2539  * @return new pkg_malloc'ed rval_expr or 0 on error.
2540  */
2541 struct rval_expr* mk_rval_expr2(enum rval_expr_op op, struct rval_expr* rve1,
2542                                                                                                           struct rval_expr* rve2,
2543                                                                                                           struct cfg_pos* pos)
2544 {
2545         struct rval_expr* ret;
2546         
2547         switch(op){
2548                 case RVE_MUL_OP:
2549                 case RVE_DIV_OP:
2550                 case RVE_MOD_OP:
2551                 case RVE_MINUS_OP:
2552                 case RVE_BOR_OP:
2553                 case RVE_BAND_OP:
2554                 case RVE_LAND_OP:
2555                 case RVE_LOR_OP:
2556                 case RVE_GT_OP:
2557                 case RVE_GTE_OP:
2558                 case RVE_LT_OP:
2559                 case RVE_LTE_OP:
2560                 case RVE_PLUS_OP:
2561                 case RVE_IPLUS_OP:
2562                 case RVE_EQ_OP:
2563                 case RVE_DIFF_OP:
2564                 case RVE_IEQ_OP:
2565                 case RVE_IDIFF_OP:
2566                 case RVE_STREQ_OP:
2567                 case RVE_STRDIFF_OP:
2568                 case RVE_MATCH_OP:
2569                 case RVE_CONCAT_OP:
2570                         break;
2571                 default:
2572                         BUG("unsupported operator %d\n", op);
2573                         return 0;
2574         }
2575         ret=pkg_malloc(sizeof(*ret));
2576         if (ret==0) 
2577                 return 0;
2578         memset(ret, 0, sizeof(*ret));
2579         ret->op=op;
2580         ret->left.rve=rve1;
2581         ret->right.rve=rve2;
2582         if (pos) ret->fpos=*pos;
2583         return ret;
2584 }
2585
2586
2587
2588 /** returns true if the operator is associative. */
2589 static int rve_op_is_assoc(enum rval_expr_op op)
2590 {
2591         switch(op){
2592                 case RVE_NONE_OP:
2593                 case RVE_RVAL_OP:
2594                 case RVE_UMINUS_OP:
2595                 case RVE_BOOL_OP:
2596                 case RVE_LNOT_OP:
2597                 case RVE_STRLEN_OP:
2598                 case RVE_STREMPTY_OP:
2599                 case RVE_DEFINED_OP:
2600                 case RVE_INT_OP:
2601                 case RVE_STR_OP:
2602                         /* one operand expression => cannot be assoc. */
2603                         return 0;
2604                 case RVE_DIV_OP:
2605                 case RVE_MOD_OP:
2606                 case RVE_MINUS_OP:
2607                         return 0;
2608                 case RVE_PLUS_OP:
2609                         /* the generic plus is not assoc, e.g.
2610                            "a" + 1 + "2" => "a12" in one case and "a3" in the other */
2611                         return 0;
2612                 case RVE_IPLUS_OP:
2613                 case RVE_CONCAT_OP:
2614                 case RVE_MUL_OP:
2615                 case RVE_BAND_OP:
2616                 case RVE_BOR_OP:
2617                         return 1;
2618                 case RVE_LAND_OP:
2619                 case RVE_LOR_OP:
2620                         return 1;
2621                 case RVE_GT_OP:
2622                 case RVE_GTE_OP:
2623                 case RVE_LT_OP:
2624                 case RVE_LTE_OP:
2625                 case RVE_EQ_OP:
2626                 case RVE_DIFF_OP:
2627                 case RVE_IEQ_OP:
2628                 case RVE_IDIFF_OP:
2629                 case RVE_STREQ_OP:
2630                 case RVE_STRDIFF_OP:
2631                 case RVE_MATCH_OP:
2632                         return 0;
2633         }
2634         return 0;
2635 }
2636
2637
2638
2639 /** returns true if the operator is commutative. */
2640 static int rve_op_is_commutative(enum rval_expr_op op)
2641 {
2642         switch(op){
2643                 case RVE_NONE_OP:
2644                 case RVE_RVAL_OP:
2645                 case RVE_UMINUS_OP:
2646                 case RVE_BOOL_OP:
2647                 case RVE_LNOT_OP:
2648                 case RVE_STRLEN_OP:
2649                 case RVE_STREMPTY_OP:
2650                 case RVE_DEFINED_OP:
2651                 case RVE_INT_OP:
2652                 case RVE_STR_OP:
2653                         /* one operand expression => cannot be commut. */
2654                         return 0;
2655                 case RVE_DIV_OP:
2656                 case RVE_MOD_OP:
2657                 case RVE_MINUS_OP:
2658                         return 0;
2659                 case RVE_PLUS_OP:
2660                         /* non commut. when diff. type 
2661                            (e.g 1 + "2" != "2" + 1 ) => non commut. in general
2662                            (specific same type versions are covered by IPLUS & CONCAT) */
2663                         return 0;
2664                 case RVE_IPLUS_OP:
2665                 case RVE_MUL_OP:
2666                 case RVE_BAND_OP:
2667                 case RVE_BOR_OP:
2668                 case RVE_LAND_OP:
2669                 case RVE_LOR_OP:
2670                 case RVE_IEQ_OP:
2671                 case RVE_IDIFF_OP:
2672                 case RVE_STREQ_OP:
2673                 case RVE_STRDIFF_OP:
2674                         return 1;
2675                 case RVE_GT_OP:
2676                 case RVE_GTE_OP:
2677                 case RVE_LT_OP:
2678                 case RVE_LTE_OP:
2679                 case RVE_CONCAT_OP:
2680                 case RVE_MATCH_OP:
2681                         return 0;
2682                 case RVE_DIFF_OP:
2683                 case RVE_EQ_OP:
2684                         /* non. commut. in general, only for same type e.g.:
2685                            "" == 0  diff. 0 == "" ( "" == "0" and 0 == 0)
2686                            same type versions are covered by IEQ, IDIFF, STREQ, STRDIFF
2687                          */
2688                         return 0 /* asymmetrical undef handling */;
2689         }
2690         return 0;
2691 }
2692
2693
2694 #if 0
2695 /** returns true if the rval expr can be optimized to an int.
2696  *  (if left & right are leafs (RVE_RVAL_OP) and both of them are
2697  *   ints return true, else false)
2698  *  @return 0 or 1
2699  */
2700 static int rve_can_optimize_int(struct rval_expr* rve)
2701 {
2702         if (scr_opt_lev<1)
2703                 return 0;
2704         if (rve->op == RVE_RVAL_OP)
2705                 return 0;
2706         if (rve->left.rve->op != RVE_RVAL_OP)
2707                 return 0;
2708         if (rve->left.rve->left.rval.type!=RV_INT)
2709                 return 0;
2710         if (rve->right.rve){
2711                 if  (rve->right.rve->op != RVE_RVAL_OP)
2712                         return 0;
2713                 if (rve->right.rve->left.rval.type!=RV_INT)
2714                         return 0;
2715         }
2716         DBG("rve_can_optimize_int: left %d, right %d\n", 
2717                         rve->left.rve->op, rve->right.rve?rve->right.rve->op:0);
2718         return 1;
2719 }
2720
2721
2722
2723 /** returns true if the rval expr can be optimized to a str.
2724  *  (if left & right are leafs (RVE_RVAL_OP) and both of them are
2725  *   str or left is str and right is int return true, else false)
2726  *  @return 0 or 1
2727  */
2728 static int rve_can_optimize_str(struct rval_expr* rve)
2729 {
2730         if (scr_opt_lev<1)
2731                 return 0;
2732         if (rve->op == RVE_RVAL_OP)
2733                 return 0;
2734         DBG("rve_can_optimize_str: left %d, right %d\n", 
2735                         rve->left.rve->op, rve->right.rve?rve->right.rve->op:0);
2736         if (rve->left.rve->op != RVE_RVAL_OP)
2737                 return 0;
2738         if (rve->left.rve->left.rval.type!=RV_STR)
2739                 return 0;
2740         if (rve->right.rve){
2741                 if  (rve->right.rve->op != RVE_RVAL_OP)
2742                         return 0;
2743                 if ((rve->right.rve->left.rval.type!=RV_STR) && 
2744                                 (rve->right.rve->left.rval.type!=RV_INT))
2745                         return 0;
2746         }
2747         return 1;
2748 }
2749 #endif
2750
2751
2752
2753 static int fix_rval(struct rvalue* rv)
2754 {
2755         DBG("RV fixing type %d\n", rv->type);
2756         switch(rv->type){
2757                 case RV_INT:
2758                         /*nothing to do*/
2759                         DBG("RV is int: %d\n", (int)rv->v.l);
2760                         return 0;
2761                 case RV_STR:
2762                         /*nothing to do*/
2763                         DBG("RV is str: \"%s\"\n", rv->v.s.s);
2764                         return 0;
2765                 case RV_BEXPR:
2766                         return fix_expr(rv->v.bexpr);
2767                 case RV_ACTION_ST:
2768                         return fix_actions(rv->v.action);
2769                 case RV_SEL:
2770                         if (resolve_select(&rv->v.sel)<0){
2771                                 ERR("Unable to resolve select\n");
2772                                 print_select(&rv->v.sel);
2773                         }
2774                         return 0;
2775                 case RV_AVP:
2776                         /* nothing to do, resolved at runtime */
2777                         return 0;
2778                 case RV_PVAR:
2779                         /* nothing to do, resolved at parsing time */
2780                         return 0;
2781                 case RV_NONE:
2782                         BUG("uninitialized rvalue\n");
2783                         return -1;
2784         }
2785         BUG("unknown rvalue type %d\n", rv->type);
2786         return -1;
2787 }
2788
2789
2790
2791 /** helper function: replace a rve (in-place) with a constant rval_val.
2792  * WARNING: since it replaces in-place, one should make sure that if
2793  * rve is in fact a rval (rve->op==RVE_RVAL_OP), no reference is kept
2794  * to the rval!
2795  * @param rve - expression to be replaced (in-place)
2796  * @param v   - pointer to a rval_val union containing the replacement
2797  *              value.
2798  * @param flags - value flags (how it was alloc'ed, e.g.: RV_CNT_ALLOCED_F)
2799  * @return 0 on success, -1 on error */
2800 static int rve_replace_with_val(struct rval_expr* rve, enum rval_type type,
2801                                                                 union rval_val* v, int flags)
2802 {
2803         int refcnt;
2804         
2805         refcnt=1; /* replaced-in-place rval refcnt */
2806         if (rve->op!=RVE_RVAL_OP){
2807                 rve_destroy(rve->left.rve);
2808                 if (rve_op_unary(rve->op)==0)
2809                         rve_destroy(rve->right.rve);
2810         }else{
2811                 if (rve->left.rval.refcnt!=1){
2812                         BUG("trying to replace a referenced rval! (refcnt=%d)\n",
2813                                         rve->left.rval.refcnt);
2814                         /* try to recover */
2815                         refcnt=rve->left.rval.refcnt;
2816                         abort(); /* find bugs quicker -- andrei */
2817                 }
2818                 rval_destroy(&rve->left.rval);
2819         }
2820         rval_init(&rve->left.rval, type, v, flags);
2821         rve->left.rval.refcnt=refcnt;
2822         rval_init(&rve->right.rval, RV_NONE, 0, 0);
2823         rve->op=RVE_RVAL_OP;
2824         return 0;
2825 }
2826
2827
2828
2829 /** helper function: replace a rve (in-place) with a constant rvalue.
2830  * @param rve - expression to be replaced (in-place)
2831  * @param rv   - pointer to the replacement _constant_ rvalue structure
2832  * @return 0 on success, -1 on error */
2833 static int rve_replace_with_ct_rv(struct rval_expr* rve, struct rvalue* rv)
2834 {
2835         enum rval_type type;
2836         int flags;
2837         int i;
2838         union rval_val v;
2839         
2840         type=rv->type;
2841         flags=0;
2842         if (rv->type==RV_INT){
2843                 if (rval_get_int(0, 0, &i, rv, 0)!=0){
2844                         BUG("unexpected int evaluation failure (%d,%d-%d,%d)\n",
2845                                         rve->fpos.s_line, rve->fpos.s_col,
2846                                         rve->fpos.e_line, rve->fpos.e_col);
2847                         return -1;
2848                 }
2849                 v.l=i;
2850         }else if(rv->type==RV_STR){
2851                 if (rval_get_str(0, 0, &v.s, rv, 0)<0){
2852                         BUG("unexpected str evaluation failure(%d,%d-%d,%d)\n",
2853                                         rve->fpos.s_line, rve->fpos.s_col,
2854                                         rve->fpos.e_line, rve->fpos.e_col);
2855                         return -1;
2856                 }
2857                 flags|=RV_CNT_ALLOCED_F;
2858         }else{
2859                 BUG("unknown constant expression type %d (%d,%d-%d,%d)\n", rv->type,
2860                                 rve->fpos.s_line, rve->fpos.s_col,
2861                                 rve->fpos.e_line, rve->fpos.e_col);
2862                 return -1;
2863         }
2864         return rve_replace_with_val(rve, type, &v, flags);
2865 }
2866
2867
2868
2869 /** try to replace the right side of the rve with a compiled regex.
2870   * @return 0 on success and -1 on error.
2871  */
2872 static int fix_match_rve(struct rval_expr* rve)
2873 {
2874         struct rvalue* rv;
2875         regex_t* re;
2876         union rval_val v;
2877         int flags;
2878         int ret;
2879
2880         rv=0;
2881         v.s.s=0;
2882         v.re.regex=0;
2883         /* normal fix-up for the  left side */
2884         ret=fix_rval_expr((void**)&rve->left.rve);
2885         if (ret<0) return ret;
2886         
2887         /* fixup the right side (RE) */
2888         if (rve_is_constant(rve->right.rve)){
2889                 if ((rve_guess_type(rve->right.rve)!=RV_STR)){
2890                         ERR("fixup failure(%d,%d-%d,%d): left side of  =~ is not string"
2891                                         " (%d,%d)\n",   rve->fpos.s_line, rve->fpos.s_col,
2892                                                                         rve->fpos.e_line, rve->fpos.e_col,
2893                                                                         rve->right.rve->fpos.s_line,
2894                                                                         rve->right.rve->fpos.s_col);
2895                         goto error;
2896                 }
2897                 if ((rv=rval_expr_eval(0, 0, rve->right.rve))==0){
2898                         ERR("fixup failure(%d,%d-%d,%d):  bad RE expression\n",
2899                                         rve->right.rve->fpos.s_line, rve->right.rve->fpos.s_col,
2900                                         rve->right.rve->fpos.e_line, rve->right.rve->fpos.e_col);
2901                         goto error;
2902                 }
2903                 if (rval_get_str(0, 0, &v.s, rv, 0)<0){
2904                         BUG("fixup unexpected failure (%d,%d-%d,%d)\n",