Browse code

core: core pvars added for $?/$retcode/$rc

- added core pvars (registered before script parsing) for the
return code ($?, $retcode, $rc). This solves script conflicts
between the different return code handling (kamailio - pvars and
ser built-in expr. elem).
- removed ser expr. elem for retcode (not needed anymore)
- added '?' as a valid pvar char (needed so that $? will be
accepted)

Andrei Pelinescu-Onciul authored on 07/10/2009 21:57:45
Showing 6 changed files
... ...
@@ -78,7 +78,8 @@
78 78
  *  2008-11-28  added support for kamailio pvars and avp/pvar guessing (andrei)
79 79
  *  2008-12-11  added support for "string1" "string2" (andrei)
80 80
  *  2009-03-10  added SET_USERPHONE action (Miklos)
81
- *  2009-04-24  addd strlen, strempty and defined operators (andrei)
81
+ *  2009-04-24  add strlen, strempty and defined operators (andrei)
82
+ *  2009-03-07  RETCODE, it's now  a core pvar (andrei)
82 83
 */
83 84
 
84 85
 
... ...
@@ -256,7 +257,7 @@ PROTO	proto
256 256
 AF		af
257 257
 MYSELF	myself
258 258
 MSGLEN			"msg:len"
259
-RETCODE	\$\?|\$retcode
259
+RETCODE	\$\?|\$retcode|\$rc
260 260
 /* operators */
261 261
 EQUAL	=
262 262
 EQUAL_T	==
... ...
@@ -535,7 +536,6 @@ EAT_ABLE	[\ \t\b\r]
535 535
 <INITIAL>{ISAVPFLAGSET}	{ count(); yylval.strval=yytext; return ISAVPFLAGSET; }
536 536
 <INITIAL>{AVPFLAGS_DECL}	{ count(); yylval.strval=yytext; return AVPFLAGS_DECL; }
537 537
 <INITIAL>{MSGLEN}	{ count(); yylval.strval=yytext; return MSGLEN; }
538
-<INITIAL>{RETCODE}	{ count(); yylval.strval=yytext; return RETCODE; }
539 538
 <INITIAL>{ROUTE}	{ count(); yylval.strval=yytext; return ROUTE; }
540 539
 <INITIAL>{ROUTE_ONREPLY}	{ count(); yylval.strval=yytext;
541 540
 								return ROUTE_ONREPLY; }
... ...
@@ -969,6 +969,8 @@ EAT_ABLE	[\ \t\b\r]
969 969
 								return PVAR;
970 970
 							}
971 971
 
972
+	/* if found retcode => it's a built-in pvar */
973
+<INITIAL>{RETCODE}			{ count(); yylval.strval=yytext; return PVAR; }
972 974
 
973 975
 <INITIAL>{VAR_MARK}			{
974 976
 								switch(sr_cfg_compat){
... ...
@@ -354,7 +354,6 @@ extern char *finame;
354 354
 %token AF
355 355
 %token MYSELF
356 356
 %token MSGLEN
357
-%token RETCODE
358 357
 %token UDP
359 358
 %token TCP
360 359
 %token TLS
... ...
@@ -1824,7 +1823,6 @@ eint_op:	SRCPORT		{ $$=SRCPORT_O; }
1824 1824
 		|	DSTPORT		{ $$=DSTPORT_O; }
1825 1825
 		|	AF			{ $$=AF_O; }
1826 1826
 		|	MSGLEN		{ $$=MSGLEN_O; }
1827
-		|	RETCODE		{ $$=RETCODE_O; }
1828 1827
 		| eint_op_onsend
1829 1828
 	;
1830 1829
 
... ...
@@ -2418,7 +2416,7 @@ pvar:	PVAR {
2418 2418
 			memset(pv_spec, 0, sizeof(*pv_spec));
2419 2419
 			s_tmp.s=$1; s_tmp.len=strlen($1);
2420 2420
 			if (pv_parse_spec(&s_tmp, pv_spec)==0){
2421
-				yyerror("unknown script pseudo variable");
2421
+				yyerror("unknown script pseudo variable %s", $1 );
2422 2422
 				pkg_free(pv_spec);
2423 2423
 				pv_spec=0;
2424 2424
 				YYABORT;
... ...
@@ -172,6 +172,7 @@
172 172
 #include "cfg_core.h"
173 173
 #include "endianness.h" /* init */
174 174
 #include "basex.h" /* init */
175
+#include "pv_core.h" /* register core pvars */
175 176
 
176 177
 #ifdef DEBUG_DMALLOC
177 178
 #include <dmalloc.h>
... ...
@@ -1760,6 +1761,7 @@ int main(int argc, char** argv)
1760 1760
 	if (init_script_cb()<0) goto error;
1761 1761
 	if (init_rpcs()<0) goto error;
1762 1762
 	if (register_core_rpcs()!=0) goto error;
1763
+	if (pv_register_core_vars()!=0) goto error;
1763 1764
 
1764 1765
 	/* Fix the value of cfg_file variable.*/
1765 1766
 	if (fix_cfg_file() < 0) goto error;
1766 1767
new file mode 100644
... ...
@@ -0,0 +1,63 @@
0
+/* 
1
+ * $Id$
2
+ * 
3
+ * Copyright (C) 2009 iptelorg GmbH
4
+ *
5
+ * Permission to use, copy, modify, and distribute this software for any
6
+ * purpose with or without fee is hereby granted, provided that the above
7
+ * copyright notice and this permission notice appear in all copies.
8
+ *
9
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
+ */
17
+/*
18
+ * pv_core.c - pvars needed in the core, e.g. $?, $retcode
19
+ *
20
+ * Note: in general please avoid adding pvars directly to the core, unless
21
+ * absolutely necessary (use/create a new module instead).
22
+ */
23
+
24
+#include "pv_core.h"
25
+#include "pvar.h"
26
+#include "str.h"
27
+
28
+static int pv_get_retcode(struct sip_msg*, pv_param_t*, pv_value_t*);
29
+
30
+static pv_export_t core_pvs[] = {
31
+	/* return code, various synonims */
32
+	{ STR_STATIC_INIT("?"), PVT_OTHER, pv_get_retcode, 0, 0, 0, 0, 0 },
33
+	{ STR_STATIC_INIT("rc"), PVT_OTHER, pv_get_retcode, 0, 0, 0, 0, 0 },
34
+	{ STR_STATIC_INIT("retcode"), PVT_OTHER, pv_get_retcode, 0, 0, 0, 0, 0 },
35
+	
36
+	{ {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
37
+};
38
+
39
+
40
+/* ugly hack to get the return code, needed because the PVs do not know (yet)
41
+   about the script context */
42
+extern int _last_returned_code;
43
+
44
+static int pv_get_retcode(struct sip_msg* msg, pv_param_t* p, pv_value_t* res)
45
+{
46
+	/* FIXME: as soon as PVs support script context, use it instead of the
47
+	          return in global variable hack */
48
+	return pv_get_sintval(msg, p, res, _last_returned_code);
49
+}
50
+
51
+
52
+
53
+/** register built-in core pvars.
54
+ * should be called before parsing the config script.
55
+ * @return 0 on success 
56
+ */
57
+int pv_register_core_vars(void)
58
+{
59
+	return register_pvars_mod("core", core_pvs);
60
+}
61
+
62
+/* vi: set ts=4 sw=4 tw=79:ai:cindent: */
0 63
new file mode 100644
... ...
@@ -0,0 +1,30 @@
0
+/* 
1
+ * $Id$
2
+ * 
3
+ * Copyright (C) 2009 iptelorg GmbH
4
+ *
5
+ * Permission to use, copy, modify, and distribute this software for any
6
+ * purpose with or without fee is hereby granted, provided that the above
7
+ * copyright notice and this permission notice appear in all copies.
8
+ *
9
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
+ */
17
+/*
18
+ * pv_core.h
19
+ */
20
+
21
+#ifndef __pv_core_h
22
+#define __pv_core_h
23
+
24
+/* register core pvars */
25
+int pv_register_core_vars(void);
26
+
27
+#endif /*__pv_core_h*/
28
+
29
+/* vi: set ts=4 sw=4 tw=79:ai:cindent: */
... ...
@@ -73,7 +73,7 @@ void pv_init_table(void)
73 73
 static int is_pv_valid_char(char c)
74 74
 {
75 75
 	if((c>='0' && c<='9') || (c>='a' && c<='z') || (c>='A' && c<='Z')
76
-			|| (c=='_') || (c=='.'))
76
+			|| (c=='_') || (c=='.') || (c=='?') /* ser $? */)
77 77
 		return 1;
78 78
 	return 0;
79 79
 }