Browse code

core expr. eval: support for int & str casts

- new internal operators for force-casting to int or str
- fix int conversion failure for v1 str_concat v2

Andrei Pelinescu-Onciul authored on 05/05/2009 11:51:40
Showing 2 changed files
... ...
@@ -28,6 +28,7 @@
28 28
  *               (str)undef="", (int)""=0, (int)"123"=123, (int)"abc"=0
29 29
  *              handle undef == expr, in function of the UNDEF_EQ_* defines.
30 30
  *              (andrei)
31
+ *  2009-05-05  casts operator for int & string (andrei)
31 32
  */
32 33
 
33 34
 /* special defines:
... ...
@@ -480,11 +481,13 @@ enum rval_type rve_guess_type( struct rval_expr* rve)
480 481
 		case RVE_STRLEN_OP:
481 482
 		case RVE_STREMPTY_OP:
482 483
 		case RVE_DEFINED_OP:
484
+		case RVE_INT_OP:
483 485
 			return RV_INT;
484 486
 		case RVE_PLUS_OP:
485 487
 			/* '+' evaluates to the type of the left operand */
486 488
 			return rve_guess_type(rve->left.rve);
487 489
 		case RVE_CONCAT_OP:
490
+		case RVE_STR_OP:
488 491
 			return RV_STR;
489 492
 		case RVE_NONE_OP:
490 493
 			break;
... ...
@@ -522,6 +525,8 @@ int rve_is_constant(struct rval_expr* rve)
522 525
 		case RVE_STRLEN_OP:
523 526
 		case RVE_STREMPTY_OP:
524 527
 		case RVE_DEFINED_OP:
528
+		case RVE_INT_OP:
529
+		case RVE_STR_OP:
525 530
 			return rve_is_constant(rve->left.rve);
526 531
 		case RVE_MINUS_OP:
527 532
 		case RVE_MUL_OP:
... ...
@@ -579,6 +584,8 @@ static int rve_op_unary(enum rval_expr_op op)
579 584
 		case RVE_STRLEN_OP:
580 585
 		case RVE_STREMPTY_OP:
581 586
 		case RVE_DEFINED_OP:
587
+		case RVE_INT_OP:
588
+		case RVE_STR_OP:
582 589
 			return 1;
583 590
 		case RVE_MINUS_OP:
584 591
 		case RVE_MUL_OP:
... ...
@@ -781,6 +788,14 @@ int rve_check_type(enum rval_type* type, struct rval_expr* rve,
781 788
 				return 1;
782 789
 			}
783 790
 			break;
791
+		case RVE_INT_OP:
792
+			*type=RV_INT;
793
+			return 1;
794
+			break;
795
+		case RVE_STR_OP:
796
+			*type=RV_STR;
797
+			return 1;
798
+			break;
784 799
 		case RVE_NONE_OP:
785 800
 		default:
786 801
 			BUG("unexpected rve op %d\n", rve->op);
... ...
@@ -1726,6 +1741,9 @@ int rval_expr_eval_int( struct run_act_ctx* h, struct sip_msg* msg,
1726 1741
 				break;
1727 1742
 			ret=int_intop1(res, rve->op, i1);
1728 1743
 			break;
1744
+		case RVE_INT_OP:
1745
+			ret=rval_expr_eval_int(h, msg, res, rve->left.rve);
1746
+			break;
1729 1747
 		case RVE_MUL_OP:
1730 1748
 		case RVE_DIV_OP:
1731 1749
 		case RVE_MINUS_OP:
... ...
@@ -1857,27 +1875,49 @@ int rval_expr_eval_int( struct run_act_ctx* h, struct sip_msg* msg,
1857 1875
 				}
1858 1876
 			}
1859 1877
 			break;
1860
-#if 0
1861
-		case RVE_MATCH_OP:
1862
-				if (unlikely((rv1=rval_expr_eval(h, msg, rve->left.rve))==0)){
1863
-					ret=-1;
1864
-					break;
1865
-				}
1866
-				if (unlikely((rv2=rval_expr_eval(h, msg,
1867
-													rve->right.rve))==0)){
1868
-					rval_destroy(rv1);
1869
-					ret=-1;
1870
-					break;
1871
-				}
1872
-				ret=rval_str_lop2(res, rve->op, rv1, 0, rv2, 0);
1878
+		case RVE_CONCAT_OP:
1879
+			/* eval expression => string */
1880
+			if (unlikely((rv1=rval_expr_eval(h, msg, rve))==0)){
1881
+				ret=-1;
1882
+				break;
1883
+			}
1884
+			/* conver to int */
1885
+			ret=rval_get_int(h, msg, res, rv1, 0); /* convert to int */
1886
+			rval_destroy(rv1);
1887
+			break;
1888
+		case RVE_STR_OP:
1889
+			/* (str)expr => eval expression */
1890
+			rval_cache_init(&c1);
1891
+			if (unlikely((ret=rval_expr_eval_rvint(h, msg, &rv1, res,
1892
+													rve->left.rve, &c1))<0)){
1893
+				/* error */
1894
+				rval_cache_clean(&c1);
1895
+				break;
1896
+			}
1897
+			if (unlikely(rv1)){
1898
+				/* expr evaluated to string => (int)(str)v == (int)v */
1899
+				ret=rval_get_int(h, msg, res, rv1, &c1); /* convert to int */
1873 1900
 				rval_destroy(rv1);
1874
-				rval_destroy(rv2);
1901
+				rval_cache_clean(&c1);
1902
+			} /* else (rv1==0)
1903
+				 => expr evaluated to int => 
1904
+				 return (int)(str)v == (int)v => do nothing */
1875 1905
 			break;
1876
-#endif
1877
-		case RVE_CONCAT_OP:
1878
-			*res=0;
1879
-			ret=-1;
1906
+
1907
+#if 0
1908
+			/* same thing as above, but in a not optimized, easier to
1909
+			   understand way */
1910
+			/* 1. (str) expr => eval expr */
1911
+			if (unlikely((rv1=rval_expr_eval(h, msg, rve->left.rve))==0)){
1912
+				ret=-1;
1913
+				break;
1914
+			}
1915
+			/* 2. convert to str and then convert to int
1916
+			   but since (int)(str)v == (int)v skip over (str)v */
1917
+			ret=rval_get_int(h, msg, res, rv1, 0); /* convert to int */
1918
+			rval_destroy(rv1);
1880 1919
 			break;
1920
+#endif
1881 1921
 		case RVE_DEFINED_OP:
1882 1922
 			ret=int_rve_defined(h, msg, res, rve->left.rve);
1883 1923
 			break;
... ...
@@ -1918,8 +1958,19 @@ int rval_expr_eval_int( struct run_act_ctx* h, struct sip_msg* msg,
1918 1958
 
1919 1959
 /** evals a rval expr. into an int or another rv(str).
1920 1960
  * WARNING: rv result (rv_res) must be rval_destroy()'ed if non-null
1921
- * (it might be a reference to another rval). The result can be 
1961
+ * (it might be a reference to another rval). The result can be
1922 1962
  * modified only if rv_chg_in_place() returns true.
1963
+ * @param res_rv - pointer to rvalue result, if non-null it means the 
1964
+ *                 expression evaluated to a non-int (str), which will be
1965
+ *                 stored here.
1966
+ * @param res_i  - pointer to int result, if res_rv==0 and the function
1967
+ *                 returns success => the result is an int which will be 
1968
+ *                 stored here.
1969
+ * @param rve    - expression that will be evaluated.
1970
+ * @param cache  - write-only value cache, it might be filled if non-null and
1971
+ *                 empty (rval_cache_init()). If non-null, it _must_ be 
1972
+ *                 rval_cache_clean()'ed when done. 
1973
+ *
1923 1974
  * @result  0 on success, -1 on error,  sets *res_rv or *res_i.
1924 1975
  */
1925 1976
 int rval_expr_eval_rvint(			   struct run_act_ctx* h,
... ...
@@ -1986,6 +2037,7 @@ int rval_expr_eval_rvint(			   struct run_act_ctx* h,
1986 2037
 		case RVE_STRLEN_OP:
1987 2038
 		case RVE_STREMPTY_OP:
1988 2039
 		case RVE_DEFINED_OP:
2040
+		case RVE_INT_OP:
1989 2041
 			/* operator forces integer type */
1990 2042
 			ret=rval_expr_eval_int(h, msg, res_i, rve);
1991 2043
 			*res_rv=0;
... ...
@@ -2020,6 +2072,7 @@ int rval_expr_eval_rvint(			   struct run_act_ctx* h,
2020 2072
 			rval_cache_clean(&c1);
2021 2073
 			break;
2022 2074
 		case RVE_CONCAT_OP:
2075
+		case RVE_STR_OP:
2023 2076
 			*res_rv=rval_expr_eval(h, msg, rve);
2024 2077
 			ret=-(*res_rv==0);
2025 2078
 			break;
... ...
@@ -2089,6 +2142,7 @@ struct rvalue* rval_expr_eval(struct run_act_ctx* h, struct sip_msg* msg,
2089 2142
 		case RVE_STRLEN_OP:
2090 2143
 		case RVE_STREMPTY_OP:
2091 2144
 		case RVE_DEFINED_OP:
2145
+		case RVE_INT_OP:
2092 2146
 			/* operator forces integer type */
2093 2147
 			r=rval_expr_eval_int(h, msg, &i, rve);
2094 2148
 			if (likely(r==0)){
... ...
@@ -2168,6 +2222,14 @@ struct rvalue* rval_expr_eval(struct run_act_ctx* h, struct sip_msg* msg,
2168 2222
 			}
2169 2223
 			ret=rval_str_add2(h, msg, rv1, 0, rv2, 0);
2170 2224
 			break;
2225
+		case RVE_STR_OP:
2226
+			rv1=rval_expr_eval(h, msg, rve->left.rve);
2227
+			if (unlikely(rv1==0)){
2228
+				ERR("rval expression evaluation failed\n");
2229
+				goto error;
2230
+			}
2231
+			ret=rval_convert(h, msg, RV_STR, rv1, 0);
2232
+			break;
2171 2233
 		case RVE_NONE_OP:
2172 2234
 		/*default:*/
2173 2235
 			BUG("invalid rval expression operation %d\n", rve->op);
... ...
@@ -2292,6 +2354,8 @@ struct rval_expr* mk_rval_expr1(enum rval_expr_op op, struct rval_expr* rve1,
2292 2354
 		case RVE_STRLEN_OP:
2293 2355
 		case RVE_STREMPTY_OP:
2294 2356
 		case RVE_DEFINED_OP:
2357
+		case RVE_INT_OP:
2358
+		case RVE_STR_OP:
2295 2359
 			break;
2296 2360
 		default:
2297 2361
 			BUG("unsupported unary operator %d\n", op);
... ...
@@ -2374,6 +2438,8 @@ static int rve_op_is_assoc(enum rval_expr_op op)
2374 2438
 		case RVE_STRLEN_OP:
2375 2439
 		case RVE_STREMPTY_OP:
2376 2440
 		case RVE_DEFINED_OP:
2441
+		case RVE_INT_OP:
2442
+		case RVE_STR_OP:
2377 2443
 			/* one operand expression => cannot be assoc. */
2378 2444
 			return 0;
2379 2445
 		case RVE_DIV_OP:
... ...
@@ -2422,6 +2488,8 @@ static int rve_op_is_commutative(enum rval_expr_op op)
2422 2488
 		case RVE_STRLEN_OP:
2423 2489
 		case RVE_STREMPTY_OP:
2424 2490
 		case RVE_DEFINED_OP:
2491
+		case RVE_INT_OP:
2492
+		case RVE_STR_OP:
2425 2493
 			/* one operand expression => cannot be commut. */
2426 2494
 			return 0;
2427 2495
 		case RVE_DIV_OP:
... ...
@@ -3291,6 +3359,8 @@ int fix_rval_expr(void** p)
3291 3359
 		case RVE_STRLEN_OP:
3292 3360
 		case RVE_STREMPTY_OP:
3293 3361
 		case RVE_DEFINED_OP:
3362
+		case RVE_INT_OP:
3363
+		case RVE_STR_OP:
3294 3364
 			ret=fix_rval_expr((void**)&rve->left.rve);
3295 3365
 			if (ret<0) return ret;
3296 3366
 			break;
... ...
@@ -25,6 +25,7 @@
25 25
  *  2008-11-30  initial version (andrei)
26 26
  *  2009-04-28  added string and interger versions for the EQ and DIFF
27 27
  *              operators (andrei)
28
+ *  2009-05-05  casts operator for int & string (andrei)
28 29
  */
29 30
 
30 31
 #ifndef _rvalue_h_
... ...
@@ -77,6 +78,8 @@ enum rval_expr_op{
77 78
 	RVE_MATCH_OP,  /* 2 members, string ~),  returns left matches re(right) */
78 79
 	/* avp, pvars a.s.o */
79 80
 	RVE_DEFINED_OP, /* one member, returns is_defined(val) (bool) */
81
+	RVE_INT_OP,   /* one member, returns (int)val  (int) */
82
+	RVE_STR_OP    /* one member, returns (str)val  (str) */
80 83
 };
81 84
 
82 85