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-11-30 initial version (andrei)
26 * 2009-04-28 added string and interger versions for the EQ and DIFF
28 * 2009-05-05 casts operator for int & string (andrei)
39 #include "parser/msg_parser.h"
43 RV_NONE, RV_INT, RV_STR, /* basic types */
44 RV_BEXPR, RV_ACTION_ST, /* special values */
45 RV_SEL, RV_AVP, RV_PVAR
49 RVE_NONE_OP, /* uninit / empty */
50 RVE_RVAL_OP, /* special op, means that the expr. is in fact a rval */
51 RVE_UMINUS_OP, /* one member expression, returns -(val) */
52 RVE_BOOL_OP, /* one member evaluate as bool. : (val!=0)*/
53 RVE_LNOT_OP, /* one member evaluate as bool. : (!val)*/
54 RVE_MUL_OP, /* 2 members, returns left * right */
55 RVE_DIV_OP, /* 2 members, returns left / right */
56 RVE_MOD_OP, /* 2 members, returns left % right */
57 RVE_MINUS_OP, /* 2 members, returns left - right */
58 RVE_BAND_OP, /* 2 members, returns left | right */
59 RVE_BOR_OP, /* 2 members, returns left & right */
60 RVE_LAND_OP, /* 2 members, returns left && right */
61 RVE_LOR_OP, /* 2 members, returns left || right */
62 RVE_GT_OP, /* 2 members, returns left > right */
63 RVE_GTE_OP, /* 2 members, returns left >= right */
64 RVE_LT_OP, /* 2 members, returns left < right */
65 RVE_LTE_OP, /* 2 members, returns left <= right */
66 RVE_IEQ_OP, /* 2 members, int == version, returns left == right */
67 RVE_IDIFF_OP,/* 2 members, int != version, returns left != right */
68 RVE_IPLUS_OP, /* 2 members, integer +, returns int(a)+int(b) */
69 /* common int & str */
70 RVE_PLUS_OP, /* generic plus (int or str) returns left + right */
71 RVE_EQ_OP, /* 2 members, returns left == right (int)*/
72 RVE_DIFF_OP, /* 2 members, returns left != right (int)*/
74 RVE_CONCAT_OP,/* 2 members, string concat, returns left . right (str)*/
75 RVE_STRLEN_OP, /* one member, string length:, returns strlen(val) (int)*/
76 RVE_STREMPTY_OP, /* one member, returns val=="" (bool) */
77 RVE_STREQ_OP, /* 2 members, string == , returns left == right (bool)*/
78 RVE_STRDIFF_OP,/* 2 members, string != , returns left != right (bool)*/
79 RVE_MATCH_OP, /* 2 members, string ~), returns left matches re(right) */
80 /* avp, pvars a.s.o */
81 RVE_DEFINED_OP, /* one member, returns is_defined(val) (bool) */
82 RVE_INT_OP, /* one member, returns (int)val (int) */
83 RVE_STR_OP /* one member, returns (str)val (str) */
99 struct action* action;
107 int refcnt; /**< refcnt, on 0 the structure is destroyed */
109 int bsize; /**< extra data size */
111 char buf[1]; /**< extra data, like string contents can be stored here */
116 #define RV_CNT_ALLOCED_F 1 /* free contents (pkg mem allocated) */
117 #define RV_RV_ALLOCED_F 2 /* free rv itself (pkg_free(rv)) */
118 #define RV_ALL_ALLOCED_F (RV_CNT_ALLOCED|RV_RV_ALLOCED)
119 #define RV_RE_F 4 /* string is a RE with a valid v->re member */
120 #define RV_RE_ALLOCED_F 8 /* v->re.regex must be freed */
124 enum rval_expr_op op;
126 struct rval_expr* rve;
130 struct rval_expr* rve;
137 enum rval_cache_type{
144 /** value cache for a rvalue struct.
145 * Used to optimize functions that would need to
146 * get the value repeatedly (e.g. rval_get_btype() and then rval_get_int())
149 enum rval_cache_type cache_type;
150 enum rval_type val_type;
152 int_str avp_val; /**< avp value */
153 pv_value_t pval; /**< pvar value */
159 /** allocates a new rval (should be freed by rval_destroy()). */
160 struct rvalue* rval_new_empty(int extra_size);
161 struct rvalue* rval_new_str(str* s, int extra_size);
162 struct rvalue* rval_new(enum rval_type t, union rval_val* v, int extra_size);
164 /** inits a rvalue structure- */
165 void rval_init(struct rvalue* rv, enum rval_type t, union rval_val* v,
167 /** frees a rval_new(), rval_convert() or rval_expr_eval() returned rval. */
168 void rval_destroy(struct rvalue* rv);
170 /** frees a rval contents */
171 void rval_clean(struct rvalue* rv);
173 /** init a rval_cache struct */
174 #define rval_cache_init(rvc) \
175 do{ (rvc)->cache_type=RV_CACHE_EMPTY; (rvc)->val_type=RV_NONE; }while(0)
177 /** destroy a rval_cache struct contents */
178 void rval_cache_clean(struct rval_cache* rvc);
181 /** convert a rvalue to another type. */
182 struct rvalue* rval_convert(struct run_act_ctx* h, struct sip_msg* msg,
183 enum rval_type type, struct rvalue* v,
184 struct rval_cache* c);
186 /** get the integer value of an rvalue. */
187 int rval_get_int(struct run_act_ctx* h, struct sip_msg* msg, int* i,
188 struct rvalue* rv, struct rval_cache* cache);
189 /** get the string value of an rv. */
190 int rval_get_str(struct run_act_ctx* h, struct sip_msg* msg,
191 str* s, struct rvalue* rv,
192 struct rval_cache* cache);
193 /** get the string value of an rv in a tmp variable */
194 int rval_get_tmp_str(struct run_act_ctx* h, struct sip_msg* msg,
195 str* tmpv, struct rvalue* rv,
196 struct rval_cache* cache,
197 struct rval_cache* tmp_cache);
199 /** evals an integer expr to an int. */
200 int rval_expr_eval_int( struct run_act_ctx* h, struct sip_msg* msg,
201 int* res, struct rval_expr* rve);
202 /** evals a rval expr.. */
203 struct rvalue* rval_expr_eval(struct run_act_ctx* h, struct sip_msg* msg,
204 struct rval_expr* rve);
205 /** evals an integer expr to an int or rvalue. */
206 int rval_expr_eval_rvint( struct run_act_ctx* h, struct sip_msg* msg,
207 struct rvalue** rv_res, int* i_res,
208 struct rval_expr* rve, struct rval_cache* cache);
211 /** guess the type of an expression. */
212 enum rval_type rve_guess_type(struct rval_expr* rve);
213 /** returns true if expression is constant. */
214 int rve_is_constant(struct rval_expr* rve);
215 /** returns true if the expression can have side-effect */
216 int rve_has_side_effects(struct rval_expr* rve);
217 /** returns 1 if expression is valid (type-wise).*/
218 int rve_check_type(enum rval_type* type, struct rval_expr* rve,
219 struct rval_expr** bad_rve, enum rval_type* bad_type,
220 enum rval_type* exp_type);
221 /** returns a string name for type (debugging).*/
222 char* rval_type_name(enum rval_type type);
224 /** create a RVE_RVAL_OP rval_expr, containing a single rval of the given type
226 struct rval_expr* mk_rval_expr_v(enum rval_type rv_type, void* val,
227 struct cfg_pos* pos);
228 /** create a unary op. rval_expr.. */
229 struct rval_expr* mk_rval_expr1(enum rval_expr_op op, struct rval_expr* rve1,
230 struct cfg_pos* pos);
231 /** create a rval_expr. from 2 other rval exprs, using op. */
232 struct rval_expr* mk_rval_expr2(enum rval_expr_op op, struct rval_expr* rve1,
233 struct rval_expr* rve2,
234 struct cfg_pos* pos);
235 /** destroys a pkg_malloc'ed rve. */
236 void rve_destroy(struct rval_expr* rve);
238 /** fix a rval_expr. */
239 int fix_rval_expr(void** p);
240 #endif /* _rvalue_h */