Selaa lähdekoodia

- 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 22 vuotta sitten
vanhempi
commit
cc087c642e
4 muutettua tiedostoa jossa 96 lisäystä ja 43 poistoa
  1. 1 2
      Makefile.defs
  2. 1 0
      TODO
  3. 11 41
      re.c
  4. 83 0
      re.h

+ 1 - 2
Makefile.defs

@@ -272,8 +272,7 @@ DEFS+= $(extra_defs) \
 	 -DUSE_IPV6 \
 	 -DUSE_TCP \
 	 -DDISABLE_NAGLE \
-	 -DDBG_QM_MALLOC \
-	# -DF_MALLOC \
+	 -DF_MALLOC \
 	# -DDBG_F_MALLOC \
 	# -DDBG_QM_MALLOC \
 	 #-DF_MALLOC \

+ 1 - 0
TODO

@@ -2,6 +2,7 @@ $Id$
 
 ( - todo, x - done)
 
+- backport tcp conn id fix to stable
 - regex subst on uris?
 - port receive.c pre_script_cb fix from stable
 - extend alias to include port numbers :

+ 11 - 41
re.c

@@ -35,41 +35,9 @@
 
 #include "dprint.h"
 #include "mem/mem.h"
-#include "str.h"
-#include "parser/msg_parser.h"
+#include "re.h"
 
 #include <string.h>
-#include <sys/types.h> /* for regex */
-#include <regex.h>
-
-
-enum replace_special { REPLACE_NMATCH, REPLACE_CHAR, REPLACE_URI };
-
-struct replace_with{
-	int offset; /* offset in string */
-	int size;   /* size of replace "anchor" in string */
-	enum replace_special type;
-	union{
-		int nmatch;
-		char c;
-	};
-};
-
-struct subst_expr{
-	regex_t* re;
-	str replacement;
-	int replace_all; 
-	int n_escapes; /* escapes number (replace[] size) */
-	int max_pmatch ; /* highest () referenced */
-	struct replace_with replace[1]; /* 0 does not work on all compilers */
-};
-
-struct replace_lst{
-	int offset;
-	int size;   /* at offset, delete size bytes and replace them with rpl */;
-	str rpl;
-	struct replace_lst *next;
-};
 
 
 
@@ -82,7 +50,7 @@ void subst_expr_free(struct subst_expr* se)
 
 
 
-/* frees the entire least, head (l) too */
+/* frees the entire list, head (l) too */
 void replace_lst_free(struct replace_lst* l)
 {
 	struct replace_lst* t;
@@ -289,6 +257,7 @@ found_repl:
 	se->n_escapes=rw_no;
 	se->max_pmatch=max_pmatch;
 	for (r=0; r<rw_no; r++) se->replace[r]=rw[r];
+	DBG("subst_parser: ok, se is %p\n", se);
 	return se;
 	
 error:
@@ -416,7 +385,7 @@ error:
 
 
 /* WARNING: input must be 0 terminated! */
-struct replace_lst* run_subst(struct subst_expr* se, char* input,
+struct replace_lst* subst_run(struct subst_expr* se, char* input,
 								struct sip_msg* msg)
 {
 	struct replace_lst *head;
@@ -435,21 +404,22 @@ struct replace_lst* run_subst(struct subst_expr* se, char* input,
 	/* no of () referenced + 1 for the whole string: pmatch[0] */
 	pmatch=pkg_malloc(nmatch*sizeof(regmatch_t));
 	if (pmatch==0){
-		LOG(L_ERR, "ERROR: run_subst_ out of mem. (pmatch)\n");
+		LOG(L_ERR, "ERROR: subst_run_ out of mem. (pmatch)\n");
 		goto error;
 	}
 	do{
 		r=regexec(se->re, p, nmatch, pmatch, 0);
+		DBG("subst_run: running. r=%d\n", r);
 		/* subst */
-		if (r){
+		if (r==0){ /* != REG_NOMATCH */
 			*crt=pkg_malloc(sizeof(struct replace_lst));
 			if (*crt==0){
-				LOG(L_ERR, "ERROR: run_subst: out of mem (crt)\n");
+				LOG(L_ERR, "ERROR: subst_run: out of mem (crt)\n");
 				goto error;
 			}
 			memset(*crt, sizeof(struct replace_lst), 0);
 			if (pmatch[0].rm_so==-1){
-				LOG(L_ERR, "ERROR: run_subst: unknown offset?\n");
+				LOG(L_ERR, "ERROR: subst_run: unknown offset?\n");
 				goto error;
 			}
 			(*crt)->offset=pmatch[0].rm_so+(int)(p-input);
@@ -462,7 +432,7 @@ struct replace_lst* run_subst(struct subst_expr* se, char* input,
 			crt=&((*crt)->next);
 			p+=pmatch[0].rm_eo;
 		}
-	}while(r && se->replace_all);
+	}while((r==0) && se->replace_all);
 	pkg_free(pmatch);
 	return head;
 error:
@@ -489,7 +459,7 @@ str* subst_str(char *input, struct sip_msg* msg, struct subst_expr* se)
 	/* compute the len */
 	len=strlen(input);
 	end=input+len;
-	lst=run_subst(se, input, msg);
+	lst=subst_run(se, input, msg);
 	for (l=lst; l; l=l->next)
 		len+=(int)(l->rpl.len)-l->size;
 	res=pkg_malloc(sizeof(str));

+ 83 - 0
re.h

@@ -0,0 +1,83 @@
+/*
+ * $Id$
+ *
+ * regexp and regexp substitutions implementations
+ * 
+ * Copyright (C) 2001-2003 Fhg Fokus
+ *
+ * This file is part of ser, a free SIP server.
+ *
+ * ser is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ * For a license to use the ser software under conditions
+ * other than those described here, or to purchase support for this
+ * software, please contact iptel.org by e-mail at the following addresses:
+ *    [email protected]
+ *
+ * ser is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License 
+ * along with this program; if not, write to the Free Software 
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ *
+ * History:
+ * --------
+ *   2003-08-04  created by andrei
+ */
+
+#ifndef _re_h
+#define _re_h
+
+#include "str.h"
+#include "parser/msg_parser.h"
+#include <sys/types.h> /* for regex */
+#include <regex.h>
+
+enum replace_special { REPLACE_NMATCH, REPLACE_CHAR, REPLACE_URI };
+
+struct replace_with{
+	int offset; /* offset in string */
+	int size;   /* size of replace "anchor" in string */
+	enum replace_special type;
+	union{
+		int nmatch;
+		char c;
+	};
+};
+
+struct subst_expr{
+	regex_t* re;
+	str replacement;
+	int replace_all; 
+	int n_escapes; /* escapes number (replace[] size) */
+	int max_pmatch ; /* highest () referenced */
+	struct replace_with replace[1]; /* 0 does not work on all compilers */
+};
+
+struct replace_lst{
+	int offset;
+	int size;   /* at offset, delete size bytes and replace them with rpl */;
+	str rpl;
+	struct replace_lst *next;
+};
+
+
+
+void subst_expr_free(struct subst_expr* se);
+void replace_lst_free(struct replace_lst* l);
+struct subst_expr*  subst_parser(str* subst);
+struct replace_lst* subst_run( struct subst_expr* se, char* input, 
+		                       struct sip_msg* msg);
+str* subst_str(char* input, struct sip_msg* msg, struct subst_expr* se);
+
+
+
+#endif
+