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