4 * Copyright (C) 2008 iptelorg GmbH
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.
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.
20 * @brief rvalue expressions
25 * 2008-12-01 initial version (andrei)
26 * 2009-04-24 added support for defined, strempty, strlen (andrei)
27 * 2009-04-28 int and str automatic conversions: (int)undef=0,
28 * (str)undef="", (int)""=0, (int)"123"=123, (int)"abc"=0
29 * handle undef == expr, in function of the UNDEF_EQ_* defines.
31 * 2009-05-05 casts operator for int & string (andrei)
36 * UNDEF_EQ_* - how to behave when undef is on the right side of a generic
38 * UNDEF_EQ_ALWAYS_FALSE: undef == something is always false
39 * UNDEF_EQ_UNDEF_TRUE : undef == something false except for undef==undef
41 * no UNDEF_EQ* define : undef == expr => convert undef to typeof(expr)
42 * and perform normal ==. undef == undef will be
43 * converted to string and it will be true
45 * NOTE: expr == undef, with defined(expr) is always evaluated this way:
46 expr == (type_of(expr))undef
47 * RV_STR2INT_VERBOSE_ERR - if a string conversion to int fails, log (L_WARN)
48 * the string that caused it (only the string, not
49 * the expression position).
50 * RV_STR2INT_ERR - if a string conversion to int fails, don't ignore
51 * the error (return error).
52 * RVAL_GET_INT_ERR_WARN - if a conversion to int fails, log a warning with
53 * the expression position.
54 * Depends on RV_STR2INT_ERR.
55 * RVAL_GET_INT_ERR_IGN - if a conversion to int fails, ignore the error
56 * (the result will be 0). Can be combined with
57 * RVAL_GET_INT_ERR_WARN.
58 * Depends on RV_STR2INT_ERR.
63 * \brief SIP-router core ::
70 #include <stdlib.h> /* abort() */
72 /* if defined warn when str2int conversions fail */
73 #define RV_STR2INT_VERBOSE_ERR
75 /* if defined rval_get_int will fail if str2int conversion fail
76 (else convert to 0) */
77 #define RV_STR2INT_ERR
79 /* if a rval_get_int fails (conversion to int), warn
80 Depends on RV_STR2INT_ERR.
82 #define RVAL_GET_INT_ERR_WARN
84 /* if a rval_get_int fails, ignore it (expression evaluation will not fail,
85 the int conversion will result in 0).
86 Can be combined with RVAL_GET_INT_ERR_WARN.
87 Depends on RV_STR2INT_ERR.
89 #define RVAL_GET_INT_ERR_IGN
91 /* minimum size alloc'ed for STR RVs (to accomodate
92 * strops without reallocs) */
93 #define RV_STR_EXTRA 80
95 #define rv_ref(rv) ((rv)->refcnt++)
97 /** unref rv and returns true if 0 */
98 #define rv_unref(rv) ((--(rv)->refcnt)==0)
101 inline static void rval_force_clean(struct rvalue* rv)
103 if (rv->flags & RV_CNT_ALLOCED_F){
111 BUG("RV_CNT_ALLOCED_F not supported for type %d\n", rv->type);
113 rv->flags&=~RV_CNT_ALLOCED_F;
115 if (rv->flags & RV_RE_ALLOCED_F){
117 if (unlikely(rv->type!=RV_STR || !(rv->flags & RV_RE_F))){
118 BUG("RV_RE_ALLOCED_F not supported for type %d or "
119 "bad flags %x\n", rv->type, rv->flags);
121 regfree(rv->v.re.regex);
122 pkg_free(rv->v.re.regex);
125 rv->flags&=~(RV_RE_ALLOCED_F|RV_RE_F);
131 /** frees a rval returned by rval_new(), rval_convert() or rval_expr_eval().
132 * Note: it will be freed only when refcnt reaches 0
134 void rval_destroy(struct rvalue* rv)
136 if (rv && rv_unref(rv)){
137 rval_force_clean(rv);
138 /* still an un-regfreed RE ? */
139 if ((rv->flags & RV_RE_F) && rv->v.re.regex){
140 if (unlikely(rv->type!=RV_STR))
141 BUG("RV_RE_F not supported for type %d\n", rv->type);
142 regfree(rv->v.re.regex);
144 if (rv->flags & RV_RV_ALLOCED_F){
152 void rval_clean(struct rvalue* rv)
155 rval_force_clean(rv);
160 void rve_destroy(struct rval_expr* rve)
163 if (rve->op==RVE_RVAL_OP){
164 if (rve->left.rval.refcnt){
165 if (rve->left.rval.refcnt==1)
166 rval_destroy(&rve->left.rval);
168 BUG("rval expr rval with invalid refcnt: %d (%d,%d-%d,%d)"
169 "\n", rve->left.rval.refcnt,
170 rve->fpos.s_line, rve->fpos.s_col,
171 rve->fpos.e_line, rve->fpos.e_col);
173 if (rve->right.rval.refcnt){
174 if (rve->right.rval.refcnt==1)
175 rval_destroy(&rve->right.rval);
177 BUG("rval expr rval with invalid refcnt: %d (%d,%d-%d,%d)"
178 "\n", rve->right.rval.refcnt,
179 rve->fpos.s_line, rve->fpos.s_col,
180 rve->fpos.e_line, rve->fpos.e_col);
184 rve_destroy(rve->left.rve);
186 rve_destroy(rve->right.rve);
194 void rval_cache_clean(struct rval_cache* rvc)
196 if ((rvc->cache_type==RV_CACHE_PVAR) && (rvc->val_type!=RV_NONE)){
197 pv_value_destroy(&rvc->c.pval);
199 rvc->cache_type=RV_CACHE_EMPTY;
200 rvc->val_type=RV_NONE;
204 #define rv_chg_in_place(rv) ((rv)->refcnt==1)
208 /** init a rvalue structure.
209 * Note: not needed if the structure is allocate with one of the
210 * rval_new* functions
212 void rval_init(struct rvalue* rv, enum rval_type t, union rval_val* v,
221 memset (&rv->v, 0, sizeof(rv->v));
227 /** create a new pk_malloc'ed empty rvalue.
229 * @param extra_size - extra space to allocate
230 * (e.g.: so that future string operation can reuse
232 * @return new rv or 0 on error
234 struct rvalue* rval_new_empty(int extra_size)
237 int size; /* extra size at the end */
239 size=ROUND_LONG(sizeof(*rv)-sizeof(rv->buf)+extra_size); /* round up */
242 rv->bsize=size-sizeof(*rv)-sizeof(rv->buf); /* remaining size->buffer*/
243 rv->flags=RV_RV_ALLOCED_F;
252 /** create a new pk_malloc'ed rv from a str.
254 * @param s - pointer to str, must be non-null
255 * @param extra_size - extra space to allocate
256 * (so that future string operation can reuse
258 * @return new rv or 0 on error
260 struct rvalue* rval_new_str(str* s, int extra_size)
264 rv=rval_new_empty(extra_size+s->len+1/* 0 term */);
267 rv->v.s.s=&rv->buf[0];
269 memcpy(rv->v.s.s, s->s, s->len);
277 /** create a new pk_malloc'ed RE rv from a str re.
278 * It acts as rval_new_str, but also compiles a RE from the str
279 * and sets v->re.regex.
280 * @param s - pointer to str, must be non-null, zero-term'ed and a valid RE.
281 * @return new rv or 0 on error
283 struct rvalue* rval_new_re(str* s)
288 offs=(long)&((struct rvalue*)0)->buf[0]; /* offset of the buf. member */
289 /* make sure we reserve enough space so that we can satisfy any regex_t
290 alignment requirement (pointer) */
291 rv=rval_new_empty(ROUND_POINTER(offs)-offs+sizeof(*rv->v.re.regex)+
295 /* make sure regex points to a properly aligned address
296 (use max./pointer alignment to be sure ) */
297 rv->v.re.regex=(regex_t*)((char*)&rv->buf[0]+ROUND_POINTER(offs)-offs);
298 rv->v.s.s=(char*)rv->v.re.regex+sizeof(*rv->v.re.regex);
300 memcpy(rv->v.s.s, s->s, s->len);
302 /* compile the regex */
303 /* same flags as for expr. =~ (fix_expr()) */
304 if (unlikely(regcomp(rv->v.re.regex, s->s,
305 REG_EXTENDED|REG_NOSUB|REG_ICASE))){
317 /** get string name for a type.
319 * @return - null terminated name of the type
321 char* rval_type_name(enum rval_type type)
342 return "error_unkown_type";
347 /** create a new pk_malloc'ed rvalue from a rval_val union.
349 * @param s - pointer to str, must be non-null
350 * @param extra_size - extra space to allocate
351 * (so that future string operation can reuse
353 * @return new rv or 0 on error
355 struct rvalue* rval_new(enum rval_type t, union rval_val* v, int extra_size)
359 if (t==RV_STR && v && v->s.s)
360 return rval_new_str(&v->s, extra_size);
361 rv=rval_new_empty(extra_size);
364 if (likely(v && t!=RV_STR)){
366 }else if (t==RV_STR){
367 rv->v.s.s=&rv->buf[0];
369 if (likely(extra_size)) rv->v.s.s[0]=0;
371 memset (&rv->v, 0, sizeof(rv->v));
378 /** get rvalue basic type (RV_INT or RV_STR).
380 * Given a rvalue it tries to determinte its basic type.
381 * Fills val_cache if non-null and empty (can be used in other rval*
382 * function calls, to avoid re-resolving avps or pvars). It must be
383 * rval_cache_clean()'en when no longer needed.
385 * @param rv - target rvalue
386 * @param val_cache - write-only: value cache, might be filled if non-null,
387 * it _must_ be rval_cache_clean()'en when done.
388 * @return - basic type or RV_NONE on error
390 inline static enum rval_type rval_get_btype(struct run_act_ctx* h,
393 struct rval_cache* val_cache)
401 enum rval_type* ptype;
411 if (likely(val_cache && val_cache->cache_type==RV_CACHE_EMPTY)){
412 pv=&val_cache->c.pval;
413 val_cache->cache_type=RV_CACHE_PVAR;
418 memset(pv, 0, sizeof(tmp_pval));
419 if (likely(pv_get_spec_value(msg, &rv->v.pvs, pv)==0)){
420 if (pv->flags & PV_TYPE_INT){
421 if (likely(val_cache!=0))
422 val_cache->val_type=RV_INT;
424 pv_value_destroy(pv);
426 }else if (pv->flags & PV_VAL_STR){
427 if (likely(val_cache!=0))
428 val_cache->val_type=RV_STR;
430 pv_value_destroy(pv);
433 pv_value_destroy(pv);
434 if (likely(val_cache!=0))
435 val_cache->val_type=RV_NONE; /* undefined */
439 if (likely(val_cache!=0))
440 val_cache->val_type=RV_NONE; /* undefined */
445 if (likely(val_cache && val_cache->cache_type==RV_CACHE_EMPTY)){
446 ptype=&val_cache->val_type;
447 avpv=&val_cache->c.avp_val;
448 val_cache->cache_type=RV_CACHE_AVP;
453 r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
454 avpv, rv->v.avps.index);
456 if (r_avp->flags & AVP_VAL_STR){
471 BUG("rv type %d not handled\n", rv->type);
479 /** guess the type of an expression.
480 * @return RV_INT, RV_STR or RV_NONE (when type could not be found,
483 enum rval_type rve_guess_type( struct rval_expr* rve)
487 switch(rve->left.rval.type){
525 case RVE_STREMPTY_OP:
530 /* '+' evaluates to the type of the left operand */
531 return rve_guess_type(rve->left.rve);
543 /** returns true if expression is constant.
547 int rve_is_constant(struct rval_expr* rve)
551 switch(rve->left.rval.type){
569 case RVE_STREMPTY_OP:
573 return rve_is_constant(rve->left.rve);
596 return rve_is_constant(rve->left.rve) &&
597 rve_is_constant(rve->right.rve);
606 /** returns true if the expression has side-effects.
607 * @return 1 for possible side-effects, 0 for no side-effects
608 * TODO: add better checks
610 int rve_has_side_effects(struct rval_expr* rve)
612 return !rve_is_constant(rve);
617 /** returns true if operator is unary (takes only 1 arg).
620 static int rve_op_unary(enum rval_expr_op op)
623 case RVE_RVAL_OP: /* not realy an operator */
629 case RVE_STREMPTY_OP:
666 /** returns 1 if expression is valid (type-wise).
667 * @param type - filled with the type of the expression (RV_INT, RV_STR or
668 * RV_NONE if it's dynamic)
669 * @param rve - checked expression
670 * @param bad_rve - set on failure to the subexpression for which the
672 * @param bad_type - set on failure to the type of the bad subexpression
673 * @param exp_type - set on failure to the expected type for the bad
675 * @return 0 or 1 and sets *type to the resulting type
676 * (RV_INT, RV_STR or RV_NONE if it can be found only at runtime)
678 int rve_check_type(enum rval_type* type, struct rval_expr* rve,
679 struct rval_expr** bad_rve,
680 enum rval_type* bad_t,
681 enum rval_type* exp_t)
683 enum rval_type type1, type2;
687 *type=rve_guess_type(rve);
693 if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
695 if (bad_rve) *bad_rve=rve->left.rve;
696 if (bad_t) *bad_t=type1;
697 if (exp_t) *exp_t=RV_INT;
720 if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
722 if (bad_rve) *bad_rve=rve->left.rve;
723 if (bad_t) *bad_t=type1;
724 if (exp_t) *exp_t=RV_INT;
727 if (rve_check_type(&type2, rve->right.rve, bad_rve,
730 if (bad_rve) *bad_rve=rve->right.rve;
731 if (bad_t) *bad_t=type2;
732 if (exp_t) *exp_t=RV_INT;
742 if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
743 if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
745 if ((type2!=type1) && (type1!=RV_NONE) &&
747 !(type1==RV_STR && type2==RV_INT)){
748 if (bad_rve) *bad_rve=rve->right.rve;
749 if (bad_t) *bad_t=type2;
750 if (exp_t) *exp_t=type1;
759 if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
760 if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
762 if ((type2!=type1) && (type1!=RV_NONE) &&
764 !(type1==RV_STR && type2==RV_INT)){
765 if (bad_rve) *bad_rve=rve->right.rve;
766 if (bad_t) *bad_t=type2;
767 if (exp_t) *exp_t=type1;
777 if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
778 if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
780 if ((type2!=type1) && (type1!=RV_NONE) &&
782 !(type1==RV_STR && type2==RV_INT)){
783 if (bad_rve) *bad_rve=rve->right.rve;
784 if (bad_t) *bad_t=type2;
785 if (exp_t) *exp_t=type1;
789 if (bad_rve) *bad_rve=rve->left.rve;
790 if (bad_t) *bad_t=type1;
791 if (exp_t) *exp_t=RV_STR;
802 if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
803 if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
805 if ((type2!=type1) && (type1!=RV_NONE) &&
807 !(type1==RV_STR && type2==RV_INT)){
808 if (bad_rve) *bad_rve=rve->right.rve;
809 if (bad_t) *bad_t=type2;
810 if (exp_t) *exp_t=type1;
814 if (bad_rve) *bad_rve=rve->left.rve;
815 if (bad_t) *bad_t=type1;
816 if (exp_t) *exp_t=RV_STR;
824 case RVE_STREMPTY_OP:
827 if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
829 if (bad_rve) *bad_rve=rve->left.rve;
830 if (bad_t) *bad_t=type1;
831 if (exp_t) *exp_t=RV_STR;
847 BUG("unexpected rve op %d (%d,%d-%d,%d)\n", rve->op,
848 rve->fpos.s_line, rve->fpos.s_col,
849 rve->fpos.e_line, rve->fpos.e_col);
850 if (bad_rve) *bad_rve=rve;
851 if (bad_t) *bad_t=RV_NONE;
852 if (exp_t) *exp_t=RV_STR;
860 /** get the integer value of an rvalue.
862 * if rv == undefined select, avp or pvar, return 0.
863 * if an error occurs while evaluating a select, avp or pvar, behave as
864 * for the undefined case (and return success).
865 * @param h - script context handle
866 * @param msg - sip msg
867 * @param i - pointer to int, where the conversion result will be stored
868 * @param rv - rvalue to be converted
869 * @param cache - cached rv value (read-only), can be 0
871 * @return 0 on success, \<0 on error and EXPR_DROP on drop
873 int rval_get_int(struct run_act_ctx* h, struct sip_msg* msg,
874 int* i, struct rvalue* rv,
875 struct rval_cache* cache)
896 *i=eval_expr(h, rv->v.bexpr, msg);
904 *i=(run_actions(h, rv->v.action, msg)>0);
909 r=run_select(&tmp, &rv->v.sel, msg);
913 else /* i>0 => undefined */
919 if (unlikely(cache && cache->cache_type==RV_CACHE_AVP)){
920 if (likely(cache->val_type==RV_INT)){
921 *i=cache->c.avp_val.n;
922 }else if (cache->val_type==RV_STR){
923 s=&cache->c.avp_val.s;
925 }else if (cache->val_type==RV_NONE)
927 else goto error_cache;
929 r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
930 &avp_val, rv->v.avps.index);
932 if (unlikely(r_avp->flags & AVP_VAL_STR)){
944 if (unlikely(cache && cache->cache_type==RV_CACHE_PVAR)){
945 if (likely((cache->val_type==RV_INT) ||
946 (cache->c.pval.flags & PV_VAL_INT))){
948 }else if (cache->val_type==RV_STR){
951 }else if (cache->val_type==RV_NONE)
953 else goto error_cache;
955 memset(&pval, 0, sizeof(pval));
956 if (likely(pv_get_spec_value(msg, &rv->v.pvs, &pval)==0)){
957 if (likely(pval.flags & PV_VAL_INT)){
959 pv_value_destroy(&pval);
960 }else if (likely(pval.flags & PV_VAL_STR)){
961 destroy_pval=1; /* we must pv_value_destroy() later*/
965 /* no PV_VAL_STR and no PV_VAL_INT => undef
967 pv_value_destroy(&pval);
976 BUG("rv type %d not handled\n", rv->type);
981 eval_error: /* same as undefined */
982 /* handle undefined => result 0, return success */
986 /* rv is of string type => try to convert it to int */
987 /* if "" => 0 (most likely case) */
988 if (likely(s->len==0)) *i=0;
989 else if (unlikely(str2sint(s, i)!=0)){
990 /* error converting to int => non numeric => 0 */
992 #ifdef RV_STR2INT_VERBOSE_ERR
993 WARN("automatic string to int conversion for \"%.*s\" failed\n",
995 /* return an error code */
997 #ifdef RV_STR2INT_ERR
1002 pv_value_destroy(&pval);
1005 BUG("invalid cached value:cache type %d, value type %d\n",
1006 cache?cache->cache_type:0, cache?cache->val_type:0);
1009 pv_value_destroy(&pval);
1016 /** log a message, appending rve position and a '\n'.*/
1017 #define RVE_LOG(lev, rve, txt) \
1018 LOG((lev), txt " (%d,%d-%d,%d)\n", \
1019 (rve)->fpos.s_line, rve->fpos.s_col, \
1020 (rve)->fpos.e_line, rve->fpos.e_col )
1023 /** macro for checking and handling rval_get_int() retcode.
1024 * check if the return code is an rval_get_int error and if so
1025 * handle the error (e.g. print a log message, ignore the error by
1026 * setting ret to 0 a.s.o.)
1027 * @param ret - retcode as returned by rval_get_int() (might be changed)
1028 * @param txt - warning message txt (no pointer allowed)
1029 * @param rve - rval_expr, used to access the config. pos
1031 #if defined RVAL_GET_INT_ERR_WARN && defined RVAL_GET_INT_ERR_IGN
1032 #define rval_get_int_handle_ret(ret, txt, rve) \
1034 if (unlikely((ret)<0)) { \
1035 RVE_LOG(L_WARN, rve, txt); \
1039 #elif defined RVAL_GET_INT_ERR_WARN
1040 #define rval_get_int_handle_ret(ret, txt, rve) \
1042 if (unlikely((ret)<0)) \
1043 RVE_LOG(L_WARN, rve, txt); \
1045 #elif defined RVAL_GET_INT_ERR_IGN
1046 #define rval_get_int_handle_ret(ret, txt, rve) \
1048 if (unlikely((ret)<0)) \
1052 #define rval_get_int_handle_ret(ret, txt, rve) /* do nothing */
1060 /** get the string value of an rv in a tmp variable
1062 * if rv == undefined select, avp or pvar, return "".
1063 * if an error occurs while evaluating a select, avp or pvar, behave as
1064 * for the undefined case (and return success).
1065 * The result points either to a temporary string or inside
1066 * new_cache. new_cache must be non zero, initialized previously,
1067 * and it _must_ be rval_cache_clean(...)'ed when done.
1068 * WARNING: it's not intended for general use, it might return a pointer
1069 * to a static buffer (int2str) so use the result a.s.a.p, make a copy.
1070 * or use rval_get_str() instead.
1071 * @param h - script context handle
1072 * @param msg - sip msg
1073 * @param tmpv - str return value (pointer to a str struct that will be
1074 * be filled with the conversion result)
1075 * @param rv - rvalue to be converted
1076 * @param cache - cached rv value (read-only), can be 0
1077 * @param tmp_cache - used for temporary storage (so that tmpv will not
1078 * point to possible freed data), it must be non-null,
1079 * initialized and cleaned afterwards.
1080 * @return 0 on success, <0 on error and EXPR_DROP on drop
1082 int rval_get_tmp_str(struct run_act_ctx* h, struct sip_msg* msg,
1083 str* tmpv, struct rvalue* rv,
1084 struct rval_cache* cache,
1085 struct rval_cache* tmp_cache)
1092 tmpv->s=int2str(rv->v.l, &tmpv->len);
1099 i=(run_actions(h, rv->v.action, msg)>0);
1102 tmpv->s=int2str(i, &tmpv->len);
1105 i=eval_expr(h, rv->v.bexpr, msg);
1108 tmpv->s=int2str(i, &tmpv->len);
1111 tmpv->s=int2str(i, &tmpv->len);
1114 i=run_select(tmpv, &rv->v.sel, msg);
1115 if (unlikely(i!=0)){
1118 }else { /* i>0 => undefined */
1124 if (likely(cache && cache->cache_type==RV_CACHE_AVP)){
1125 if (likely(cache->val_type==RV_STR)){
1126 *tmpv=cache->c.avp_val.s;
1127 }else if (cache->val_type==RV_INT){
1128 i=cache->c.avp_val.n;
1129 tmpv->s=int2str(i, &tmpv->len);
1130 }else if (cache->val_type==RV_NONE){
1132 }else goto error_cache;
1134 r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
1135 &tmp_cache->c.avp_val,
1138 if (likely(r_avp->flags & AVP_VAL_STR)){
1139 tmp_cache->cache_type=RV_CACHE_AVP;
1140 tmp_cache->val_type=RV_STR;
1141 *tmpv=tmp_cache->c.avp_val.s;
1143 i=tmp_cache->c.avp_val.n;
1144 tmpv->s=int2str(i, &tmpv->len);
1150 if (likely(cache && cache->cache_type==RV_CACHE_PVAR)){
1151 if (likely(cache->val_type==RV_STR)){
1152 *tmpv=cache->c.pval.rs;
1153 }else if (cache->val_type==RV_INT){
1155 tmpv->s=int2str(i, &tmpv->len);
1156 }else if (cache->val_type==RV_NONE){
1158 }else goto error_cache;
1160 memset(&tmp_cache->c.pval, 0, sizeof(tmp_cache->c.pval));
1161 if (likely(pv_get_spec_value(msg, &rv->v.pvs,
1162 &tmp_cache->c.pval)==0)){
1163 if (likely(tmp_cache->c.pval.flags & PV_VAL_STR)){
1164 /* the value is not destroyed, but saved instead
1165 in tmp_cache so that it can be destroyed later
1166 when no longer needed */
1167 tmp_cache->cache_type=RV_CACHE_PVAR;
1168 tmp_cache->val_type=RV_STR;
1169 *tmpv=tmp_cache->c.pval.rs;
1170 }else if (likely(tmp_cache->c.pval.flags & PV_VAL_INT)){
1171 i=tmp_cache->c.pval.ri;
1172 pv_value_destroy(&tmp_cache->c.pval);
1173 tmpv->s=int2str(i, &tmpv->len);
1175 /* no PV_VAL_STR and no PV_VAL_INT => undef
1177 pv_value_destroy(&tmp_cache->c.pval);
1186 BUG("rv type %d not handled\n", rv->type);
1191 eval_error: /* same as undefined */
1192 /* handle undefined => result "", return success */
1197 BUG("invalid cached value:cache type %d, value type %d\n",
1198 cache?cache->cache_type:0, cache?cache->val_type:0);
1207 /** get the string value of an rv.
1209 * The result is pkg malloc'ed (so it should be pkg_free()'ed when finished.
1210 * @return 0 on success, <0 on error and EXPR_DROP on drop
1212 int rval_get_str(struct run_act_ctx* h, struct sip_msg* msg,
1213 str* s, struct rvalue* rv,
1214 struct rval_cache* cache)
1217 struct rval_cache tmp_cache;
1219 rval_cache_init(&tmp_cache);
1220 if (unlikely(rval_get_tmp_str(h, msg, &tmp, rv, cache, &tmp_cache)<0))
1222 s->s=pkg_malloc(tmp.len+1/* 0 term */);
1223 if (unlikely(s->s==0)){
1224 ERR("memory allocation error\n");
1228 memcpy(s->s, tmp.s, tmp.len);
1229 s->s[tmp.len]=0; /* 0 term */
1230 rval_cache_clean(&tmp_cache);
1233 rval_cache_clean(&tmp_cache);
1239 /** convert a rvalue to another rvalue, of a specific type.
1241 * The result is read-only in most cases (can be a reference
1242 * to another rvalue, can be checked by using rv_chg_in_place()) and
1243 * _must_ be rval_destroy()'ed.
1245 * @param type - type to convert to
1246 * @param v - rvalue to convert
1247 * @param c - rval_cache (cached v value if known/filled by another
1248 * function), can be 0 (unknown/not needed)
1249 * @return pointer to a rvalue (reference to an existing one or a new
1250 * one, @see rv_chg_in_place() and the above comment) or 0 on error.
1252 struct rvalue* rval_convert(struct run_act_ctx* h, struct sip_msg* msg,
1253 enum rval_type type, struct rvalue* v,
1254 struct rval_cache* c)
1257 struct rval_cache tmp_cache;
1268 if (unlikely(rval_get_int(h, msg, &i, v, c) < 0))
1271 return rval_new(RV_INT, &val, 0);
1273 rval_cache_init(&tmp_cache);
1274 if (unlikely(rval_get_tmp_str(h, msg, &tmp, v, c, &tmp_cache) < 0))
1276 rval_cache_clean(&tmp_cache);
1279 ret=rval_new_str(&tmp, RV_STR_EXTRA);
1280 rval_cache_clean(&tmp_cache);
1284 BUG("unsupported conversion to type %d\n", type);
1292 /** integer operation: *res= op v.
1293 * @return 0 on succes, \<0 on error
1295 inline static int int_intop1(int* res, enum rval_expr_op op, int v)
1308 BUG("rv unsupported intop1 %d\n", op);
1316 /** integer operation: *res= v1 op v2
1317 * @return 0 on succes, \<0 on error
1319 inline static int int_intop2(int* res, enum rval_expr_op op, int v1, int v2)
1333 if (unlikely(v2==0)){
1334 ERR("rv div by 0\n");
1340 if (unlikely(v2==0)){
1341 ERR("rv mod by 0\n");
1380 /* invalid operand for int */
1383 BUG("rv unsupported intop %d\n", op);
1391 /** internal helper: compare 2 RV_STR RVs.
1392 * Warning: rv1 & rv2 must be RV_STR
1393 * @return 0 on success, -1 on error
1395 inline static int bool_rvstrop2( enum rval_expr_op op, int* res,
1396 struct rvalue* rv1, struct rvalue* rv2)
1407 *res= (s1->len==s2->len) && (memcmp(s1->s, s2->s, s1->len)==0);
1410 case RVE_STRDIFF_OP:
1411 *res= (s1->len!=s2->len) || (memcmp(s1->s, s2->s, s1->len)!=0);
1414 if (likely(rv2->flags & RV_RE_F)){
1415 *res=(regexec(rv2->v.re.regex, rv1->v.s.s, 0, 0, 0)==0);
1417 /* we need to compile the RE on the fly */
1418 if (unlikely(regcomp(&tmp_re, s2->s,
1419 REG_EXTENDED|REG_NOSUB|REG_ICASE))){
1421 ERR("Bad regular expression \"%s\"\n", s2->s);
1424 *res=(regexec(&tmp_re, s1->s, 0, 0, 0)==0);
1429 BUG("rv unsupported intop %d\n", op);
1440 /** integer returning operation on string: *res= op str (returns integer)
1441 * @return 0 on succes, \<0 on error
1443 inline static int int_strop1(int* res, enum rval_expr_op op, str* s1)
1449 case RVE_STREMPTY_OP:
1453 BUG("rv unsupported int_strop1 %d\n", op);
1462 /** integer operation: ret= op v (returns a rvalue).
1463 * @return rvalue on success, 0 on error
1465 inline static struct rvalue* rval_intop1(struct run_act_ctx* h,
1466 struct sip_msg* msg,
1467 enum rval_expr_op op,
1475 rv2=rval_convert(h, msg, RV_INT, v, 0);
1476 if (unlikely(rv2==0)){
1477 ERR("rval int conversion failed\n");
1480 if (unlikely(int_intop1(&i, op, rv2->v.l)<0))
1482 if (rv_chg_in_place(rv2)){
1485 }else if (rv_chg_in_place(v)){
1489 ret=rval_new(RV_INT, &rv2->v, 0);
1490 if (unlikely(ret==0)){
1491 ERR("eval out of memory\n");
1505 /** integer operation: ret= l op r (returns a rvalue).
1506 * @return rvalue on success, 0 on error
1508 inline static struct rvalue* rval_intop2(struct run_act_ctx* h,
1509 struct sip_msg* msg,
1510 enum rval_expr_op op,
1521 if ((rv1=rval_convert(h, msg, RV_INT, l, 0))==0)
1523 if ((rv2=rval_convert(h, msg, RV_INT, r, 0))==0)
1525 if (unlikely(int_intop2(&i, op, rv1->v.l, rv2->v.l)<0))
1527 if (rv_chg_in_place(rv1)){
1528 /* try reusing rv1 */
1531 }else if (rv_chg_in_place(rv2)){
1532 /* try reusing rv2 */
1535 }else if ((l->type==RV_INT) && (rv_chg_in_place(l))){
1538 } else if ((r->type==RV_INT) && (rv_chg_in_place(r))){
1542 ret=rval_new(RV_INT, &rv1->v, 0);
1543 if (unlikely(ret==0)){
1544 ERR("rv eval out of memory\n");
1560 /** string add operation: ret= l . r (returns a rvalue).
1561 * Can use cached rvalues (c1 & c2).
1562 * @return rvalue on success, 0 on error
1564 inline static struct rvalue* rval_str_add2(struct run_act_ctx* h,
1565 struct sip_msg* msg,
1567 struct rval_cache* c1,
1569 struct rval_cache* c2
1586 if ((rv1=rval_convert(h, msg, RV_STR, l, c1))==0)
1588 if ((rv2=rval_convert(h, msg, RV_STR, r, c2))==0)
1591 len=rv1->v.s.len + rv2->v.s.len + 1 /* 0 */;
1593 if (rv_chg_in_place(rv1) && (rv1->bsize>=len)){
1594 /* try reusing rv1 */
1598 if (ret->v.s.s == &ret->buf[0]) s1=0;
1602 ret->flags &= ~RV_CNT_ALLOCED_F;
1603 ret->v.s.s=&ret->buf[0];
1607 }else if (rv_chg_in_place(rv2) && (rv2->bsize>=len)){
1608 /* try reusing rv2 */
1612 if (ret->v.s.s == &ret->buf[0])
1617 ret->flags &= ~RV_CNT_ALLOCED_F;
1618 ret->v.s.s=&ret->buf[0];
1622 }else if ((l->type==RV_STR) && (rv_chg_in_place(l)) && (l->bsize>=len)){
1626 if (ret->v.s.s == &ret->buf[0]) s1=0;
1630 ret->flags &= ~RV_CNT_ALLOCED_F;
1631 ret->v.s.s=&ret->buf[0];
1635 } else if ((r->type==RV_STR) && (rv_chg_in_place(r) && (r->bsize>=len))){
1639 if (ret->v.s.s == &ret->buf[0])
1644 ret->flags &= ~RV_CNT_ALLOCED_F;
1645 ret->v.s.s=&ret->buf[0];
1650 ret=rval_new(RV_STR, &rv1->v, len + RV_STR_EXTRA);
1651 if (unlikely(ret==0)){
1652 ERR("rv eval out of memory\n");
1658 /* do the actual copy */
1659 memmove(ret->buf+rv1->v.s.len, s2->s, s2->len);
1661 memcpy(ret->buf, s1->s, s1->len);
1663 ret->v.s.len=rv1->v.s.len+s2->len;
1664 ret->v.s.s[ret->v.s.len]=0;
1665 /* cleanup if needed */
1666 if (flags & RV_CNT_ALLOCED_F)
1679 /** bool operation on rval evaluated as strings.
1680 * Can use cached rvalues (c1 & c2).
1681 * @return 0 success, -1 on error
1683 inline static int rval_str_lop2(struct run_act_ctx* h,
1684 struct sip_msg* msg,
1686 enum rval_expr_op op,
1688 struct rval_cache* c1,
1690 struct rval_cache* c2)
1698 if ((rv1=rval_convert(h, msg, RV_STR, l, c1))==0)
1700 if ((rv2=rval_convert(h, msg, RV_STR, r, c2))==0)
1702 ret=bool_rvstrop2(op, res, rv1, rv2);
1714 /** integer operation on rval evaluated as string.
1715 * Can use cached rvalues (c1 & c2).
1716 * @param res - will be set to the result
1717 * @return 0 success, -1 on error
1719 inline static int rval_int_strop1(struct run_act_ctx* h,
1720 struct sip_msg* msg,
1722 enum rval_expr_op op,
1724 struct rval_cache* c1)
1731 if ((rv1=rval_convert(h, msg, RV_STR, l, c1))==0)
1733 ret=int_strop1(res, op, &rv1->v.s);
1744 /** checks if rv is defined.
1745 * @param res - set to the result 1 - defined, 0 not defined
1746 * @return 0 on success, -1 on error
1747 * Can use cached rvalues (cache).
1748 * Note: a rv can be undefined if it's an undefined avp or pvar or select or
1750 * Note2: an error in the avp, pvar or select search is equivalent to
1751 * undefined (and it's not reported)
1753 inline static int rv_defined(struct run_act_ctx* h,
1754 struct sip_msg* msg, int* res,
1755 struct rvalue* rv, struct rval_cache* cache)
1765 if (unlikely(cache && cache->cache_type==RV_CACHE_SELECT)){
1766 *res=(cache->val_type!=RV_NONE);
1768 /* run select returns 0 on success, -1 on error and >0 on
1769 undefined. error is considered undefined */
1770 *res=(run_select(&tmp, &rv->v.sel, msg)==0);
1773 if (unlikely(cache && cache->cache_type==RV_CACHE_AVP)){
1774 *res=(cache->val_type!=RV_NONE);
1776 r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
1777 &avp_val, rv->v.avps.index);
1778 if (unlikely(r_avp==0)){
1784 /* PV_VAL_NULL or pv_get_spec_value error => undef */
1785 if (unlikely(cache && cache->cache_type==RV_CACHE_PVAR)){
1786 *res=(cache->val_type!=RV_NONE);
1788 memset(&pval, 0, sizeof(pval));
1789 if (likely(pv_get_spec_value(msg, &rv->v.pvs, &pval)==0)){
1790 if ((pval.flags & PV_VAL_NULL) &&
1791 ! (pval.flags & (PV_VAL_INT|PV_VAL_STR))){
1794 pv_value_destroy(&pval);
1796 *res=0; /* in case of error, consider it undef */
1810 /** defined (integer) operation on rve.
1811 * @param res - set to 1 defined, 0 not defined
1812 * @return - 0 on success, -1 on error
1814 inline static int int_rve_defined(struct run_act_ctx* h,
1815 struct sip_msg* msg, int* res,
1816 struct rval_expr* rve)
1818 /* only a rval can be undefined, any expression consisting on more
1819 then one rval => defined */
1820 if (likely(rve->op==RVE_RVAL_OP))
1821 return rv_defined(h, msg, res, &rve->left.rval, 0);
1828 /** evals an integer expr to an int.
1830 * *res=(int)eval(rve)
1831 * @return 0 on success, \<0 on error
1833 int rval_expr_eval_int( struct run_act_ctx* h, struct sip_msg* msg,
1834 int* res, struct rval_expr* rve)
1837 struct rval_cache c1, c2;
1844 ret=rval_get_int(h, msg, res, &rve->left.rval, 0);
1845 rval_get_int_handle_ret(ret, "rval expression conversion to int"
1852 (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1854 ret=int_intop1(res, rve->op, i1);
1857 ret=rval_expr_eval_int(h, msg, res, rve->left.rve);
1874 (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1877 (ret=rval_expr_eval_int(h, msg, &i2, rve->right.rve)) <0) )
1879 ret=int_intop2(res, rve->op, i1, i2);
1883 (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1888 if (unlikely( (ret=rval_expr_eval_int(h, msg, &i2,
1889 rve->right.rve)) <0) )
1897 (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1902 if (unlikely( (ret=rval_expr_eval_int(h, msg, &i2,
1903 rve->right.rve)) <0) )
1911 /* if left is string, eval left & right as string and
1913 * if left is int eval as int using int diff
1914 * if left is undef, look at right and convert to right type
1916 rval_cache_init(&c1);
1917 if (unlikely( (ret=rval_expr_eval_rvint(h, msg, &rv1, &i1,
1918 rve->left.rve, &c1))<0)){
1920 rval_cache_clean(&c1);
1923 if (likely(rv1==0)){
1925 rval_cache_clean(&c1);
1926 if (unlikely( (ret=rval_expr_eval_int(h, msg, &i2,
1927 rve->right.rve)) <0) )
1929 ret=int_intop2(res, rve->op, i1, i2);
1931 /* not int => str or undef */
1932 /* check for undefined left operand */
1933 if (unlikely( c1.cache_type!=RV_CACHE_EMPTY &&
1934 c1.val_type==RV_NONE)){
1935 #ifdef UNDEF_EQ_ALWAYS_FALSE
1936 /* undef == something always false
1937 undef != something always true*/
1938 ret=(rve->op==RVE_DIFF_OP);
1939 #elif defined UNDEF_EQ_UNDEF_TRUE
1940 /* undef == something defined always false
1941 undef == undef true */
1942 if (int_rve_defined(h, msg, &i2, rve->right.rve)<0){
1944 rval_cache_clean(&c1);
1948 ret=(!i2) ^ (rve->op==RVE_DIFF_OP);
1949 #else /* ! UNDEF_EQ_* */
1951 * => convert to (type_of(val)) (undef) == val */
1952 rval_cache_init(&c2);
1953 if (unlikely( (ret=rval_expr_eval_rvint(h, msg, &rv2, &i2,
1954 rve->right.rve, &c2))<0)){
1956 rval_cache_clean(&c1);
1957 rval_cache_clean(&c2);
1963 ret=int_intop2(res, rve->op, 0 /* undef */, i2);
1966 ret=rval_str_lop2(h, msg, res, rve->op, rv1, &c1,
1968 rval_cache_clean(&c2);
1971 #endif /* UNDEF_EQ_* */
1972 rval_cache_clean(&c1);
1975 /* left value == defined and != int => str
1976 * => lval == (str) val */
1977 if (unlikely((rv2=rval_expr_eval(h, msg,
1978 rve->right.rve))==0)){
1981 rval_cache_clean(&c1);
1984 ret=rval_str_lop2(h, msg, res, rve->op, rv1, &c1, rv2, 0);
1985 rval_cache_clean(&c1);
1992 /* eval expression => string */
1993 if (unlikely((rv1=rval_expr_eval(h, msg, rve))==0)){
1997 /* convert to int */
1998 ret=rval_get_int(h, msg, res, rv1, 0); /* convert to int */
1999 rval_get_int_handle_ret(ret, "rval expression conversion to int"
2004 /* (str)expr => eval expression */
2005 rval_cache_init(&c1);
2006 if (unlikely((ret=rval_expr_eval_rvint(h, msg, &rv1, res,
2007 rve->left.rve, &c1))<0)){
2009 rval_cache_clean(&c1);
2013 /* expr evaluated to string => (int)(str)v == (int)v */
2014 ret=rval_get_int(h, msg, res, rv1, &c1); /* convert to int */
2015 rval_get_int_handle_ret(ret, "rval expression conversion"
2016 " to int failed", rve);
2018 rval_cache_clean(&c1);
2020 => expr evaluated to int =>
2021 return (int)(str)v == (int)v => do nothing */
2025 /* same thing as above, but in a not optimized, easier to
2027 /* 1. (str) expr => eval expr */
2028 if (unlikely((rv1=rval_expr_eval(h, msg, rve->left.rve))==0)){
2032 /* 2. convert to str and then convert to int
2033 but since (int)(str)v == (int)v skip over (str)v */
2034 ret=rval_get_int(h, msg, res, rv1, 0); /* convert to int */
2038 case RVE_DEFINED_OP:
2039 ret=int_rve_defined(h, msg, res, rve->left.rve);
2042 case RVE_STRDIFF_OP:
2044 if (unlikely((rv1=rval_expr_eval(h, msg, rve->left.rve))==0)){
2048 if (unlikely((rv2=rval_expr_eval(h, msg, rve->right.rve))==0)){
2053 ret=rval_str_lop2(h, msg, res, rve->op, rv1, 0, rv2, 0);
2058 case RVE_STREMPTY_OP:
2059 if (unlikely((rv1=rval_expr_eval(h, msg, rve->left.rve))==0)){
2063 ret=rval_int_strop1(h, msg, res, rve->op, rv1, 0);
2068 BUG("invalid rval int expression operation %d (%d,%d-%d,%d)\n",
2069 rve->op, rve->fpos.s_line, rve->fpos.s_col,
2070 rve->fpos.e_line, rve->fpos.e_col);
2078 /** evals a rval expr. into an int or another rv(str).
2079 * WARNING: rv result (rv_res) must be rval_destroy()'ed if non-null
2080 * (it might be a reference to another rval). The result can be
2081 * modified only if rv_chg_in_place() returns true.
2082 * @param res_rv - pointer to rvalue result, if non-null it means the
2083 * expression evaluated to a non-int (str), which will be
2085 * @param res_i - pointer to int result, if res_rv==0 and the function
2086 * returns success => the result is an int which will be
2088 * @param rve - expression that will be evaluated.
2089 * @param cache - write-only value cache, it might be filled if non-null and
2090 * empty (rval_cache_init()). If non-null, it _must_ be
2091 * rval_cache_clean()'ed when done.
2093 * @result 0 on success, -1 on error, sets *res_rv or *res_i.
2095 int rval_expr_eval_rvint( struct run_act_ctx* h,
2096 struct sip_msg* msg,
2097 struct rvalue** res_rv,
2099 struct rval_expr* rve,
2100 struct rval_cache* cache
2105 struct rval_cache c1; /* local cache */
2108 enum rval_type type;
2115 rv1=&rve->left.rval;
2117 type=rval_get_btype(h, msg, rv1, cache);
2119 r=rval_get_int(h, msg, res_i, rv1, cache);
2120 rval_get_int_handle_ret(r, "rval expression conversion"
2121 " to int failed", rve);
2123 ret=r; /* equiv. to if (r<0) goto error */
2125 /* RV_STR, RV_PVAR, RV_AVP a.s.o => return rv1 and the
2126 cached resolved value in cache*/
2153 case RVE_STRDIFF_OP:
2156 case RVE_STREMPTY_OP:
2157 case RVE_DEFINED_OP:
2159 /* operator forces integer type */
2160 ret=rval_expr_eval_int(h, msg, res_i, rve);
2164 rval_cache_init(&c1);
2165 r=rval_expr_eval_rvint(h, msg, &rv1, &i, rve->left.rve, &c1);
2167 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2168 rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
2169 rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col
2171 rval_cache_clean(&c1);
2175 if (unlikely((r=rval_expr_eval_int(h, msg, &j,
2176 rve->right.rve))<0)){
2177 ERR("rval expression evaluation failed (%d,%d-%d,%d)"
2178 "\n", rve->right.rve->fpos.s_line,
2179 rve->right.rve->fpos.s_col,
2180 rve->right.rve->fpos.e_line,
2181 rve->right.rve->fpos.e_col);
2182 rval_cache_clean(&c1);
2185 ret=int_intop2(res_i, rve->op, i, j);
2188 rv2=rval_expr_eval(h, msg, rve->right.rve);
2189 if (unlikely(rv2==0)){
2190 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2191 rve->right.rve->fpos.s_line,
2192 rve->right.rve->fpos.s_col,
2193 rve->right.rve->fpos.e_line,
2194 rve->right.rve->fpos.e_col);
2195 rval_cache_clean(&c1);
2198 *res_rv=rval_str_add2(h, msg, rv1, &c1, rv2, 0);
2201 rval_cache_clean(&c1);
2205 *res_rv=rval_expr_eval(h, msg, rve);
2210 BUG("invalid rval expression operation %d (%d,%d-%d,%d)\n",
2211 rve->op, rve->fpos.s_line, rve->fpos.s_col,
2212 rve->fpos.e_line, rve->fpos.e_col);
2226 /** evals a rval expr..
2227 * WARNING: result must be rval_destroy()'ed if non-null (it might be
2228 * a reference to another rval). The result can be modified only
2229 * if rv_chg_in_place() returns true.
2230 * @return rvalue on success, 0 on error
2232 struct rvalue* rval_expr_eval(struct run_act_ctx* h, struct sip_msg* msg,
2233 struct rval_expr* rve)
2238 struct rval_cache c1;
2241 enum rval_type type;
2248 rv_ref(&rve->left.rval);
2249 return &rve->left.rval;
2272 case RVE_STRDIFF_OP:
2275 case RVE_STREMPTY_OP:
2276 case RVE_DEFINED_OP:
2278 /* operator forces integer type */
2279 r=rval_expr_eval_int(h, msg, &i, rve);
2282 ret=rval_new(RV_INT, &v, 0);
2283 if (unlikely(ret==0)){
2284 ERR("rv eval int expression: out of memory\n");
2289 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2290 rve->fpos.s_line, rve->fpos.s_col,
2291 rve->fpos.e_line, rve->fpos.e_col);
2296 rv1=rval_expr_eval(h, msg, rve->left.rve);
2297 if (unlikely(rv1==0)){
2298 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2299 rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
2300 rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col);
2303 rval_cache_init(&c1);
2304 type=rval_get_btype(h, msg, rv1, &c1);
2307 r=rval_get_int(h, msg, &i, rv1, &c1);
2308 rval_get_int_handle_ret(r, "rval expression left side "
2309 "conversion to int failed",
2312 rval_cache_clean(&c1);
2315 if (unlikely((r=rval_expr_eval_int(h, msg, &j,
2316 rve->right.rve))<0)){
2317 rval_cache_clean(&c1);
2318 ERR("rval expression evaluation failed (%d,%d-%d,%d):"
2319 " could not evaluate right side to int\n",
2320 rve->fpos.s_line, rve->fpos.s_col,
2321 rve->fpos.e_line, rve->fpos.e_col);
2324 int_intop2(&r, rve->op, i, j);
2325 if (rv_chg_in_place(rv1)){
2331 ret=rval_new(RV_INT, &v, 0);
2332 if (unlikely(ret==0)){
2333 rval_cache_clean(&c1);
2334 ERR("rv eval int expression: out of memory\n");
2341 rv2=rval_expr_eval(h, msg, rve->right.rve);
2342 if (unlikely(rv2==0)){
2343 ERR("rval expression evaluation failed (%d,%d-%d,%d)"
2344 "\n", rve->right.rve->fpos.s_line,
2345 rve->right.rve->fpos.s_col,
2346 rve->right.rve->fpos.e_line,
2347 rve->right.rve->fpos.e_col);
2348 rval_cache_clean(&c1);
2351 ret=rval_str_add2(h, msg, rv1, &c1, rv2, 0);
2354 BUG("rv unsupported basic type %d (%d,%d-%d,%d)\n", type,
2355 rve->fpos.s_line, rve->fpos.s_col,
2356 rve->fpos.e_line, rve->fpos.e_col);
2358 rval_cache_clean(&c1);
2361 rv1=rval_expr_eval(h, msg, rve->left.rve);
2362 if (unlikely(rv1==0)){
2363 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2364 rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
2365 rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col);
2368 rv2=rval_expr_eval(h, msg, rve->right.rve);
2369 if (unlikely(rv2==0)){
2370 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2371 rve->right.rve->fpos.s_line,
2372 rve->right.rve->fpos.s_col,
2373 rve->right.rve->fpos.e_line,
2374 rve->right.rve->fpos.e_col);
2377 ret=rval_str_add2(h, msg, rv1, 0, rv2, 0);
2380 rv1=rval_expr_eval(h, msg, rve->left.rve);
2381 if (unlikely(rv1==0)){
2382 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2383 rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
2384 rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col);
2387 ret=rval_convert(h, msg, RV_STR, rv1, 0);
2391 BUG("invalid rval expression operation %d (%d,%d-%d,%d)\n",
2392 rve->op, rve->fpos.s_line, rve->fpos.s_col,
2393 rve->fpos.e_line, rve->fpos.e_col);
2407 /** evals a rval expr and always returns a new rval.
2408 * like rval_expr_eval, but always returns a new rvalue (never a reference
2409 * to an exisiting one).
2410 * WARNING: result must be rval_destroy()'ed if non-null (it might be
2411 * a reference to another rval). The result can be modified only
2412 * if rv_chg_in_place() returns true.
2413 * @result rvalue on success, 0 on error
2415 struct rvalue* rval_expr_eval_new(struct run_act_ctx* h, struct sip_msg* msg,
2416 struct rval_expr* rve)
2421 ret=rval_expr_eval(h, msg, rve);
2422 if (ret && !rv_chg_in_place(ret)){
2424 /* create a new rv */
2425 ret=rval_new(rv->type, &rv->v, 0);
2433 /** create a RVE_RVAL_OP rval_expr, containing a single rval of the given type.
2435 * @param rv_type - rval type
2436 * @param val - rval value
2437 * @param pos - config position
2438 * @return new pkg_malloc'ed rval_expr or 0 on error.
2440 struct rval_expr* mk_rval_expr_v(enum rval_type rv_type, void* val,
2441 struct cfg_pos* pos)
2443 struct rval_expr* rve;
2448 rve=pkg_malloc(sizeof(*rve));
2451 memset(rve, sizeof(*rve), 0);
2459 v.s.s=pkg_malloc(s->len+1 /*0*/);
2461 ERR("memory allocation failure\n");
2465 memcpy(v.s.s, s->s, s->len);
2467 flags=RV_CNT_ALLOCED_F;
2470 v.avps=*(avp_spec_t*)val;
2473 v.pvs=*(pv_spec_t*)val;
2476 v.sel=*(select_t*)val;
2479 v.bexpr=(struct expr*)val;
2482 v.action=(struct action*)val;
2485 BUG("unsupported rv type %d\n", rv_type);
2489 rval_init(&rve->left.rval, rv_type, &v, flags);
2490 rve->op=RVE_RVAL_OP;
2491 if (pos) rve->fpos=*pos;
2497 /** create a unary op. rval_expr..
2499 * @param op - rval expr. unary operator
2500 * @param rve1 - rval expr. on which the operator will act.
2501 * @return new pkg_malloc'ed rval_expr or 0 on error.
2503 struct rval_expr* mk_rval_expr1(enum rval_expr_op op, struct rval_expr* rve1,
2504 struct cfg_pos* pos)
2506 struct rval_expr* ret;
2513 case RVE_STREMPTY_OP:
2514 case RVE_DEFINED_OP:
2519 BUG("unsupported unary operator %d\n", op);
2522 ret=pkg_malloc(sizeof(*ret));
2525 memset(ret, sizeof(*ret), 0);
2528 if (pos) ret->fpos=*pos;
2534 /** create a rval_expr. from 2 other rval exprs, using op.
2535 * ret = rve1 op rve2
2536 * @param op - rval expr. operator
2537 * @param rve1 - rval expr. on which the operator will act.
2538 * @param rve2 - rval expr. on which the operator will act.
2539 * @return new pkg_malloc'ed rval_expr or 0 on error.
2541 struct rval_expr* mk_rval_expr2(enum rval_expr_op op, struct rval_expr* rve1,
2542 struct rval_expr* rve2,
2543 struct cfg_pos* pos)
2545 struct rval_expr* ret;
2567 case RVE_STRDIFF_OP:
2572 BUG("unsupported operator %d\n", op);
2575 ret=pkg_malloc(sizeof(*ret));
2578 memset(ret, sizeof(*ret), 0);
2581 ret->right.rve=rve2;
2582 if (pos) ret->fpos=*pos;
2588 /** returns true if the operator is associative. */
2589 static int rve_op_is_assoc(enum rval_expr_op op)
2598 case RVE_STREMPTY_OP:
2599 case RVE_DEFINED_OP:
2602 /* one operand expression => cannot be assoc. */
2609 /* the generic plus is not assoc, e.g.
2610 "a" + 1 + "2" => "a12" in one case and "a3" in the other */
2630 case RVE_STRDIFF_OP:
2639 /** returns true if the operator is commutative. */
2640 static int rve_op_is_commutative(enum rval_expr_op op)
2649 case RVE_STREMPTY_OP:
2650 case RVE_DEFINED_OP:
2653 /* one operand expression => cannot be commut. */
2660 /* non commut. when diff. type
2661 (e.g 1 + "2" != "2" + 1 ) => non commut. in general
2662 (specific same type versions are covered by IPLUS & CONCAT) */
2673 case RVE_STRDIFF_OP:
2684 /* non. commut. in general, only for same type e.g.:
2685 "" == 0 diff. 0 == "" ( "" == "0" and 0 == 0)
2686 same type versions are covered by IEQ, IDIFF, STREQ, STRDIFF
2688 return 0 /* asymmetrical undef handling */;
2695 /** returns true if the rval expr can be optimized to an int.
2696 * (if left & right are leafs (RVE_RVAL_OP) and both of them are
2697 * ints return true, else false)
2700 static int rve_can_optimize_int(struct rval_expr* rve)
2704 if (rve->op == RVE_RVAL_OP)
2706 if (rve->left.rve->op != RVE_RVAL_OP)
2708 if (rve->left.rve->left.rval.type!=RV_INT)
2710 if (rve->right.rve){
2711 if (rve->right.rve->op != RVE_RVAL_OP)
2713 if (rve->right.rve->left.rval.type!=RV_INT)
2716 DBG("rve_can_optimize_int: left %d, right %d\n",
2717 rve->left.rve->op, rve->right.rve?rve->right.rve->op:0);
2723 /** returns true if the rval expr can be optimized to a str.
2724 * (if left & right are leafs (RVE_RVAL_OP) and both of them are
2725 * str or left is str and right is int return true, else false)
2728 static int rve_can_optimize_str(struct rval_expr* rve)
2732 if (rve->op == RVE_RVAL_OP)
2734 DBG("rve_can_optimize_str: left %d, right %d\n",
2735 rve->left.rve->op, rve->right.rve?rve->right.rve->op:0);
2736 if (rve->left.rve->op != RVE_RVAL_OP)
2738 if (rve->left.rve->left.rval.type!=RV_STR)
2740 if (rve->right.rve){
2741 if (rve->right.rve->op != RVE_RVAL_OP)
2743 if ((rve->right.rve->left.rval.type!=RV_STR) &&
2744 (rve->right.rve->left.rval.type!=RV_INT))
2753 static int fix_rval(struct rvalue* rv)
2755 DBG("RV fixing type %d\n", rv->type);
2759 DBG("RV is int: %d\n", (int)rv->v.l);
2763 DBG("RV is str: \"%s\"\n", rv->v.s.s);
2766 return fix_expr(rv->v.bexpr);
2768 return fix_actions(rv->v.action);
2770 if (resolve_select(&rv->v.sel)<0){
2771 BUG("Unable to resolve select\n");
2772 print_select(&rv->v.sel);
2776 /* nothing to do, resolved at runtime */
2779 /* nothing to do, resolved at parsing time */
2782 BUG("uninitialized rvalue\n");
2785 BUG("unknown rvalue type %d\n", rv->type);
2791 /** helper function: replace a rve (in-place) with a constant rval_val.
2792 * WARNING: since it replaces in-place, one should make sure that if
2793 * rve is in fact a rval (rve->op==RVE_RVAL_OP), no reference is kept
2795 * @param rve - expression to be replaced (in-place)
2796 * @param v - pointer to a rval_val union containing the replacement
2798 * @param flags - value flags (how it was alloc'ed, e.g.: RV_CNT_ALLOCED_F)
2799 * @return 0 on success, -1 on error */
2800 static int rve_replace_with_val(struct rval_expr* rve, enum rval_type type,
2801 union rval_val* v, int flags)
2805 refcnt=1; /* replaced-in-place rval refcnt */
2806 if (rve->op!=RVE_RVAL_OP){
2807 rve_destroy(rve->left.rve);
2808 if (rve_op_unary(rve->op)==0)
2809 rve_destroy(rve->right.rve);
2811 if (rve->left.rval.refcnt!=1){
2812 BUG("trying to replace a referenced rval! (refcnt=%d)\n",
2813 rve->left.rval.refcnt);
2814 /* try to recover */
2815 refcnt=rve->left.rval.refcnt;
2816 abort(); /* find bugs quicker -- andrei */
2818 rval_destroy(&rve->left.rval);
2820 rval_init(&rve->left.rval, type, v, flags);
2821 rve->left.rval.refcnt=refcnt;
2822 rval_init(&rve->right.rval, RV_NONE, 0, 0);
2823 rve->op=RVE_RVAL_OP;
2829 /** helper function: replace a rve (in-place) with a constant rvalue.
2830 * @param rve - expression to be replaced (in-place)
2831 * @param rv - pointer to the replacement _constant_ rvalue structure
2832 * @return 0 on success, -1 on error */
2833 static int rve_replace_with_ct_rv(struct rval_expr* rve, struct rvalue* rv)
2835 enum rval_type type;
2842 if (rv->type==RV_INT){
2843 if (rval_get_int(0, 0, &i, rv, 0)!=0){
2844 BUG("unexpected int evaluation failure (%d,%d-%d,%d)\n",
2845 rve->fpos.s_line, rve->fpos.s_col,
2846 rve->fpos.e_line, rve->fpos.e_col);
2850 }else if(rv->type==RV_STR){
2851 if (rval_get_str(0, 0, &v.s, rv, 0)<0){
2852 BUG("unexpected str evaluation failure(%d,%d-%d,%d)\n",
2853 rve->fpos.s_line, rve->fpos.s_col,
2854 rve->fpos.e_line, rve->fpos.e_col);
2857 flags|=RV_CNT_ALLOCED_F;
2859 BUG("unknown constant expression type %d (%d,%d-%d,%d)\n", rv->type,
2860 rve->fpos.s_line, rve->fpos.s_col,
2861 rve->fpos.e_line, rve->fpos.e_col);
2864 return rve_replace_with_val(rve, type, &v, flags);
2869 /** try to replace the right side of the rve with a compiled regex.
2870 * @return 0 on success and -1 on error.
2872 static int fix_match_rve(struct rval_expr* rve)
2883 /* normal fix-up for the left side */
2884 ret=fix_rval_expr((void**)&rve->left.rve);
2885 if (ret<0) return ret;
2887 /* fixup the right side (RE) */
2888 if (rve_is_constant(rve->right.rve)){
2889 if ((rve_guess_type(rve->right.rve)!=RV_STR)){
2890 ERR("fixup failure(%d,%d-%d,%d): left side of =~ is not string"
2891 " (%d,%d)\n", rve->fpos.s_line, rve->fpos.s_col,
2892 rve->fpos.e_line, rve->fpos.e_col,
2893 rve->right.rve->fpos.s_line,
2894 rve->right.rve->fpos.s_col);
2897 if ((rv=rval_expr_eval(0, 0, rve->right.rve))==0){
2898 ERR("fixup failure(%d,%d-%d,%d): bad RE expression\n",
2899 rve->right.rve->fpos.s_line, rve->right.rve->fpos.s_col,
2900 rve->right.rve->fpos.e_line, rve->right.rve->fpos.e_col);
2903 if (rval_get_str(0, 0, &v.s, rv, 0)<0){
2904 BUG("fixup unexpected failure (%d,%d-%d,%d)\n",
2905 rve->fpos.s_line, rve->fpos.s_col,
2906 rve->fpos.e_line, rve->fpos.e_col);
2909 /* we have the str, we don't need the rv anymore */
2912 re=pkg_malloc(sizeof(*re));
2914 ERR("out of memory\n");
2917 /* same flags as for expr. =~ (fix_expr()) */
2918 if (regcomp(re, v.s.s, REG_EXTENDED|REG_NOSUB|REG_ICASE)){
2920 ERR("Bad regular expression \"%s\"(%d,%d-%d,%d)\n", v.s.s,
2921 rve->right.rve->fpos.s_line, rve->right.rve->fpos.s_col,
2922 rve->right.rve->fpos.e_line, rve->right.rve->fpos.e_col);
2926 flags=RV_RE_F|RV_RE_ALLOCED_F|RV_CNT_ALLOCED_F;
2927 if (rve_replace_with_val(rve->right.rve, RV_STR, &v, flags)<0)
2930 /* right side is not constant => normal fixup */
2931 return fix_rval_expr((void**)&rve->right.rve);
2935 if (rv) rval_destroy(rv);
2936 if (v.s.s) pkg_free(v.s.s);
2938 regfree(v.re.regex);
2939 pkg_free(v.re.regex);
2946 /** optimize op($v, 0) or op($v, 1).
2947 * Note: internal use only from rve_optimize
2948 * It should be called after ct optimization, for non-contant
2949 * expressions (the left or right side is not constant).
2950 * @return 1 on success (rve was changed), 0 on failure and -1 on error
2952 static int rve_opt_01(struct rval_expr* rve, enum rval_type rve_type)
2955 struct rval_expr* ct_rve;
2956 struct rval_expr* v_rve;
2959 enum rval_expr_op op;
2961 int right; /* debugging msg */
2962 int dbg; /* debugging msg on/off */
2964 /* helper macro: replace in-place a <ctype> type rve with v (another rve).
2965 * if type_of(v)== <ctype> => rve:=*v (copy v contents into rve and free v)
2966 * else if type_of(v)!=<ctype> => rve:= (ctype) v (casts v to <ctype>)
2968 * ctype can be INT or STR
2969 * WARNING: - v might be pkg_free()'d
2970 * - rve members _are_ _not_ freed or destroyed
2972 #define replace_rve_type_cast(e, v, ctype) \
2974 if ( rve_guess_type((v)) == RV_##ctype ){\
2975 /* if type_of($v)==int we don't need to add an \
2976 int cast operator => replace with v */\
2978 *(e)=*(v); /* replace e with v (in-place) */ \
2980 pkg_free((v)); /* rve_destroy(v_rve) would free everything*/ \
2982 /* unknown type or str => (int) $v */ \
2983 (e)->op=RVE_##ctype##_OP; \
2984 (e)->left.rve=(v); \
2989 /* helper macro: replace in-place an int type rve with v (another rve).*/
2990 #define replace_int_rve(e, v) replace_rve_type_cast(e, v, INT)
2991 /* helper macro: replace in-place a str type rve with v (another rve).*/
2992 #define replace_str_rve(e, v) replace_rve_type_cast(e, v, STR)
3000 if (rve_is_constant(rve->right.rve)){
3001 ct_rve=rve->right.rve;
3002 v_rve=rve->left.rve;
3004 }else if (rve_is_constant(rve->left.rve)){
3005 ct_rve=rve->left.rve;
3006 v_rve=rve->right.rve;
3009 return 0; /* op($v, $w) */
3011 /* rval_expr_eval_new() instead of rval_expr_eval() to avoid
3012 referencing a ct_rve->left.rval if ct_rve is a rval, which
3013 would prevent rve_destroy(ct_rve) from working */
3014 if ((rv=rval_expr_eval_new(0, 0, ct_rve))==0){
3015 ERR("optimization failure, bad expression (%d,%d-%d,%d)\n",
3016 ct_rve->fpos.s_line, ct_rve->fpos.s_col,
3017 ct_rve->fpos.e_line, ct_rve->fpos.e_col);
3021 if (rv->type==RV_INT){
3028 if (rve_replace_with_ct_rv(rve, rv)<0)
3032 /* $v * 1 -> (int)$v
3033 * 1 * $v -> (int)$v */
3034 rve_destroy(ct_rve);
3035 replace_int_rve(rve, v_rve);
3041 if (ct_rve==rve->left.rve){
3043 if (rve_replace_with_ct_rv(rve, rv)<0)
3048 ERR("RVE divide by 0 at %d,%d\n",
3049 ct_rve->fpos.s_line, ct_rve->fpos.s_col);
3052 if (ct_rve==rve->right.rve){
3053 /* $v / 1 -> (int)$v */
3054 rve_destroy(ct_rve);
3055 replace_int_rve(rve, v_rve);
3062 if (ct_rve==rve->left.rve){
3064 if (rve_replace_with_ct_rv(rve, rv)<0)
3069 ERR("RVE modulo by 0 at %d,%d\n",
3070 ct_rve->fpos.s_line, ct_rve->fpos.s_col);
3077 if (ct_rve==rve->right.rve){
3079 rve_destroy(ct_rve);
3080 replace_int_rve(rve, v_rve);
3083 /* ? 0 - $v -> -($v) ? */
3090 if (rve_replace_with_ct_rv(rve, rv)<0)
3094 /* no 0xffffff optimization for now (haven't decided on
3095 the number of bits ) */
3099 /* $v | 0 -> (int)$v
3100 * 0 | $v -> (int)$v */
3101 rve_destroy(ct_rve);
3102 replace_int_rve(rve, v_rve);
3110 if (rve_replace_with_ct_rv(rve, rv)<0)
3114 /* $v && 1 -> (int)$v
3115 * 1 && $v -> (int)$v */
3116 rve_destroy(ct_rve);
3117 replace_int_rve(rve, v_rve);
3125 if (rve_replace_with_ct_rv(rve, rv)<0)
3129 /* $v || 0 -> (int)$v
3130 * 0 && $v -> (int)$v */
3131 rve_destroy(ct_rve);
3132 replace_int_rve(rve, v_rve);
3138 /* we must make sure that this is an int PLUS
3139 (because "foo"+0 is valid => "foo0") =>
3140 check if it's an IPLUS or the result is an integer
3141 (which generally means unoptimized <int> + <something>).
3143 if ((i==0) && ((op==RVE_IPLUS_OP) || (rve_type==RV_INT))){
3144 /* $v + 0 -> (int)$v
3145 * 0 + $v -> (int)$v */
3146 rve_destroy(ct_rve);
3147 replace_int_rve(rve, v_rve);
3155 /* debugging messages */
3158 if (rve->op==RVE_RVAL_OP){
3159 if (rve->left.rval.type==RV_INT)
3160 DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
3161 " op%d($v, %d) -> %d\n",
3162 rve->fpos.s_line, rve->fpos.s_col,
3163 rve->fpos.e_line, rve->fpos.e_col,
3164 op, i, (int)rve->left.rval.v.l);
3166 DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
3167 " op%d($v, %d) -> $v (rval)\n",
3168 rve->fpos.s_line, rve->fpos.s_col,
3169 rve->fpos.e_line, rve->fpos.e_col,
3171 }else if (rve->op==RVE_INT_OP){
3172 if (rve->left.rve->op==RVE_RVAL_OP &&
3173 rve->left.rve->left.rval.type==RV_INT)
3174 DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
3175 " op%d($v, %d) -> (int)%d\n",
3176 rve->fpos.s_line, rve->fpos.s_col,
3177 rve->fpos.e_line, rve->fpos.e_col,
3178 op, i, (int)rve->left.rve->left.rval.v.l);
3180 DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
3181 " op%d($v, %d) -> (int)$v\n",
3182 rve->fpos.s_line, rve->fpos.s_col,
3183 rve->fpos.e_line, rve->fpos.e_col,
3186 DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
3187 " op%d($v, %d) -> $v\n",
3188 rve->fpos.s_line, rve->fpos.s_col,
3189 rve->fpos.e_line, rve->fpos.e_col,
3193 if (rve->op==RVE_RVAL_OP){
3194 if (rve->left.rval.type==RV_INT)
3195 DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
3196 " op%d(%d, $v) -> %d\n",
3197 rve->fpos.s_line, rve->fpos.s_col,
3198 rve->fpos.e_line, rve->fpos.e_col,
3199 op, i, (int)rve->left.rval.v.l);
3201 DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
3202 " op%d(%d, $v) -> $v (rval)\n",
3203 rve->fpos.s_line, rve->fpos.s_col,
3204 rve->fpos.e_line, rve->fpos.e_col,
3206 }else if (rve->op==RVE_INT_OP){
3207 if (rve->left.rve->op==RVE_RVAL_OP &&
3208 rve->left.rve->left.rval.type==RV_INT)
3209 DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
3210 " op%d(%d, $v) -> (int)%d\n",
3211 rve->fpos.s_line, rve->fpos.s_col,
3212 rve->fpos.e_line, rve->fpos.e_col,
3213 op, i, (int)rve->left.rve->left.rval.v.l);
3215 DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
3216 " op%d(%d, $v) -> (int)$v\n",
3217 rve->fpos.s_line, rve->fpos.s_col,
3218 rve->fpos.e_line, rve->fpos.e_col,
3221 DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
3222 " op%d(%d, $v) -> $v\n",
3223 rve->fpos.s_line, rve->fpos.s_col,
3224 rve->fpos.e_line, rve->fpos.e_col,
3229 }else if (rv->type==RV_STR){