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