Browse code

- sed like subst re support (see textops subst('/re/repl/flags')) (don't use yet I think I have still some fixes to do)

Andrei Pelinescu-Onciul authored on 19/08/2003 17:53:46
Showing 4 changed files
... ...
@@ -272,8 +272,7 @@ DEFS+= $(extra_defs) \
272 272
 	 -DUSE_IPV6 \
273 273
 	 -DUSE_TCP \
274 274
 	 -DDISABLE_NAGLE \
275
-	 -DDBG_QM_MALLOC \
276
-	# -DF_MALLOC \
275
+	 -DF_MALLOC \
277 276
 	# -DDBG_F_MALLOC \
278 277
 	# -DDBG_QM_MALLOC \
279 278
 	 #-DF_MALLOC \
... ...
@@ -2,6 +2,7 @@ $Id$
2 2
 
3 3
 ( - todo, x - done)
4 4
 
5
+- backport tcp conn id fix to stable
5 6
 - regex subst on uris?
6 7
 - port receive.c pre_script_cb fix from stable
7 8
 - extend alias to include port numbers :
... ...
@@ -35,41 +35,9 @@
35 35
 
36 36
 #include "dprint.h"
37 37
 #include "mem/mem.h"
38
-#include "str.h"
39
-#include "parser/msg_parser.h"
38
+#include "re.h"
40 39
 
41 40
 #include <string.h>
42
-#include <sys/types.h> /* for regex */
43
-#include <regex.h>
44
-
45
-
46
-enum replace_special { REPLACE_NMATCH, REPLACE_CHAR, REPLACE_URI };
47
-
48
-struct replace_with{
49
-	int offset; /* offset in string */
50
-	int size;   /* size of replace "anchor" in string */
51
-	enum replace_special type;
52
-	union{
53
-		int nmatch;
54
-		char c;
55
-	};
56
-};
57
-
58
-struct subst_expr{
59
-	regex_t* re;
60
-	str replacement;
61
-	int replace_all; 
62
-	int n_escapes; /* escapes number (replace[] size) */
63
-	int max_pmatch ; /* highest () referenced */
64
-	struct replace_with replace[1]; /* 0 does not work on all compilers */
65
-};
66
-
67
-struct replace_lst{
68
-	int offset;
69
-	int size;   /* at offset, delete size bytes and replace them with rpl */;
70
-	str rpl;
71
-	struct replace_lst *next;
72
-};
73 41
 
74 42
 
75 43
 
... ...
@@ -82,7 +50,7 @@ void subst_expr_free(struct subst_expr* se)
82 82
 
83 83
 
84 84
 
85
-/* frees the entire least, head (l) too */
85
+/* frees the entire list, head (l) too */
86 86
 void replace_lst_free(struct replace_lst* l)
87 87
 {
88 88
 	struct replace_lst* t;
... ...
@@ -289,6 +257,7 @@ found_repl:
289 289
 	se->n_escapes=rw_no;
290 290
 	se->max_pmatch=max_pmatch;
291 291
 	for (r=0; r<rw_no; r++) se->replace[r]=rw[r];
292
+	DBG("subst_parser: ok, se is %p\n", se);
292 293
 	return se;
293 294
 	
294 295
 error:
... ...
@@ -416,7 +385,7 @@ error:
416 416
 
417 417
 
418 418
 /* WARNING: input must be 0 terminated! */
419
-struct replace_lst* run_subst(struct subst_expr* se, char* input,
419
+struct replace_lst* subst_run(struct subst_expr* se, char* input,
420 420
 								struct sip_msg* msg)
421 421
 {
422 422
 	struct replace_lst *head;
... ...
@@ -435,21 +404,22 @@ struct replace_lst* run_subst(struct subst_expr* se, char* input,
435 435
 	/* no of () referenced + 1 for the whole string: pmatch[0] */
436 436
 	pmatch=pkg_malloc(nmatch*sizeof(regmatch_t));
437 437
 	if (pmatch==0){
438
-		LOG(L_ERR, "ERROR: run_subst_ out of mem. (pmatch)\n");
438
+		LOG(L_ERR, "ERROR: subst_run_ out of mem. (pmatch)\n");
439 439
 		goto error;
440 440
 	}
441 441
 	do{
442 442
 		r=regexec(se->re, p, nmatch, pmatch, 0);
443
+		DBG("subst_run: running. r=%d\n", r);
443 444
 		/* subst */
444
-		if (r){
445
+		if (r==0){ /* != REG_NOMATCH */
445 446
 			*crt=pkg_malloc(sizeof(struct replace_lst));
446 447
 			if (*crt==0){
447
-				LOG(L_ERR, "ERROR: run_subst: out of mem (crt)\n");
448
+				LOG(L_ERR, "ERROR: subst_run: out of mem (crt)\n");
448 449
 				goto error;
449 450
 			}
450 451
 			memset(*crt, sizeof(struct replace_lst), 0);
451 452
 			if (pmatch[0].rm_so==-1){
452
-				LOG(L_ERR, "ERROR: run_subst: unknown offset?\n");
453
+				LOG(L_ERR, "ERROR: subst_run: unknown offset?\n");
453 454
 				goto error;
454 455
 			}
455 456
 			(*crt)->offset=pmatch[0].rm_so+(int)(p-input);
... ...
@@ -462,7 +432,7 @@ struct replace_lst* run_subst(struct subst_expr* se, char* input,
462 462
 			crt=&((*crt)->next);
463 463
 			p+=pmatch[0].rm_eo;
464 464
 		}
465
-	}while(r && se->replace_all);
465
+	}while((r==0) && se->replace_all);
466 466
 	pkg_free(pmatch);
467 467
 	return head;
468 468
 error:
... ...
@@ -489,7 +459,7 @@ str* subst_str(char *input, struct sip_msg* msg, struct subst_expr* se)
489 489
 	/* compute the len */
490 490
 	len=strlen(input);
491 491
 	end=input+len;
492
-	lst=run_subst(se, input, msg);
492
+	lst=subst_run(se, input, msg);
493 493
 	for (l=lst; l; l=l->next)
494 494
 		len+=(int)(l->rpl.len)-l->size;
495 495
 	res=pkg_malloc(sizeof(str));
496 496
new file mode 100644
... ...
@@ -0,0 +1,83 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * regexp and regexp substitutions implementations
4
+ * 
5
+ * Copyright (C) 2001-2003 Fhg Fokus
6
+ *
7
+ * This file is part of ser, a free SIP server.
8
+ *
9
+ * ser is free software; you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation; either version 2 of the License, or
12
+ * (at your option) any later version
13
+ *
14
+ * For a license to use the ser software under conditions
15
+ * other than those described here, or to purchase support for this
16
+ * software, please contact iptel.org by e-mail at the following addresses:
17
+ *    info@iptel.org
18
+ *
19
+ * ser is distributed in the hope that it will be useful,
20
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
+ * GNU General Public License for more details.
23
+ *
24
+ * You should have received a copy of the GNU General Public License 
25
+ * along with this program; if not, write to the Free Software 
26
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
+ *
28
+ *
29
+ * History:
30
+ * --------
31
+ *   2003-08-04  created by andrei
32
+ */
33
+
34
+#ifndef _re_h
35
+#define _re_h
36
+
37
+#include "str.h"
38
+#include "parser/msg_parser.h"
39
+#include <sys/types.h> /* for regex */
40
+#include <regex.h>
41
+
42
+enum replace_special { REPLACE_NMATCH, REPLACE_CHAR, REPLACE_URI };
43
+
44
+struct replace_with{
45
+	int offset; /* offset in string */
46
+	int size;   /* size of replace "anchor" in string */
47
+	enum replace_special type;
48
+	union{
49
+		int nmatch;
50
+		char c;
51
+	};
52
+};
53
+
54
+struct subst_expr{
55
+	regex_t* re;
56
+	str replacement;
57
+	int replace_all; 
58
+	int n_escapes; /* escapes number (replace[] size) */
59
+	int max_pmatch ; /* highest () referenced */
60
+	struct replace_with replace[1]; /* 0 does not work on all compilers */
61
+};
62
+
63
+struct replace_lst{
64
+	int offset;
65
+	int size;   /* at offset, delete size bytes and replace them with rpl */;
66
+	str rpl;
67
+	struct replace_lst *next;
68
+};
69
+
70
+
71
+
72
+void subst_expr_free(struct subst_expr* se);
73
+void replace_lst_free(struct replace_lst* l);
74
+struct subst_expr*  subst_parser(str* subst);
75
+struct replace_lst* subst_run( struct subst_expr* se, char* input, 
76
+		                       struct sip_msg* msg);
77
+str* subst_str(char* input, struct sip_msg* msg, struct subst_expr* se);
78
+
79
+
80
+
81
+#endif
82
+