Module: sems
Branch: master
Commit: dea38d2dcdcccfbc9b46501be0fd0febb6cdc096
URL:    
http://git.sip-router.org/cgi-bin/gitweb.cgi/sems/?a=commit;h=dea38d2dcdcccfbc9b46501be0fd0febb6cdc096

Author: Stefan Sayer <[email protected]>
Committer: Stefan Sayer <[email protected]>
Date:   Mon Jul 21 13:53:28 2014 +0200

dsm:mod_regex: add substring selection

 $regex.match[n] is set to nth substring match, starting with 1
 ($regex.match[1] is set to first substring match...)

---

 apps/dsm/mods/mod_regex/ModRegex.cpp |   30 +++++++++++++++++++++++++++---
 apps/dsm/mods/mod_regex/ModRegex.h   |    2 +-
 doc/dsm/mods/Readme.mod_regex.txt    |    7 ++++---
 3 files changed, 32 insertions(+), 7 deletions(-)

diff --git a/apps/dsm/mods/mod_regex/ModRegex.cpp 
b/apps/dsm/mods/mod_regex/ModRegex.cpp
index e0a3b7a..642bb5d 100644
--- a/apps/dsm/mods/mod_regex/ModRegex.cpp
+++ b/apps/dsm/mods/mod_regex/ModRegex.cpp
@@ -74,7 +74,7 @@ int MOD_CLS_NAME::preload() {
 }
 
 int MOD_CLS_NAME::add_regex(const string& r_name, const string& r_reg) {
-  if (regexes[r_name].regcomp(r_reg.c_str(), REG_NOSUB | REG_EXTENDED)) {
+  if (regexes[r_name].regcomp(r_reg.c_str(), /* REG_NOSUB | */ REG_EXTENDED)) {
     ERROR("compiling '%s' for regex '%s'\n", r_reg.c_str(), r_name.c_str());
     regexes.erase(r_name);
     return -1;
@@ -97,8 +97,18 @@ MATCH_CONDITION_START(SCExecRegexCondition) {
     return false;
   }
 
-  int res = it->second.regexec(val.c_str(), 1, NULL, 0);
+  regmatch_t matches[it->second.get_nsub()+1];
+  int res = it->second.regexec(val.c_str(), it->second.get_nsub(), matches, 0);
   // res==0 -> match
+
+  if (!res) {
+    for (size_t i=1;i<it->second.get_nsub()+1;i++) {
+      if (matches[i].rm_so < 0) continue;
+      sc_sess->var["regex.match["+int2str((unsigned int)i)+"]"] =
+       val.substr(matches[i].rm_so, matches[i].rm_eo - matches[i].rm_so);
+    }
+  }
+
   DBG("regex did %smatch\n", res==0?"":"not ");
   if (inv) {
     return res != 0;
@@ -131,7 +141,17 @@ EXEC_ACTION_START(SCExecRegexAction) {
     EXEC_ACTION_STOP;
   }
 
-  int res = it->second.regexec(val.c_str(), 1, NULL, 0);
+  regmatch_t matches[it->second.get_nsub()+1];
+  int res = it->second.regexec(val.c_str(), it->second.get_nsub()+1, matches, 
0);
+
+  if (!res) {
+    for (size_t i=1;i<it->second.get_nsub()+1;i++) {
+      if (matches[i].rm_so < 0) continue;
+      sc_sess->var["regex.match["+int2str((unsigned int)i)+"]"] =
+       val.substr(matches[i].rm_so, matches[i].rm_eo - matches[i].rm_so);
+    }
+  }
+
   if (!res) {
     // yeah side effects
     sc_sess->var["regex.match"] = "1";
@@ -179,3 +199,7 @@ int TsRegex::regexec(const char *_string, size_t nmatch,
   m.unlock();
   return res;
 }
+
+size_t TsRegex::get_nsub() {
+  return i ? reg.re_nsub : 0;
+}
diff --git a/apps/dsm/mods/mod_regex/ModRegex.h 
b/apps/dsm/mods/mod_regex/ModRegex.h
index f553389..216b31a 100644
--- a/apps/dsm/mods/mod_regex/ModRegex.h
+++ b/apps/dsm/mods/mod_regex/ModRegex.h
@@ -46,7 +46,7 @@ class TsRegex {
   ~TsRegex();
   int regcomp(const char *regex, int cflags);
   int regexec(const char *_string, size_t nmatch, regmatch_t pmatch[], int 
eflags);
-
+  size_t get_nsub();
 };
 
 DECLARE_MODULE_BEGIN(MOD_CLS_NAME);
diff --git a/doc/dsm/mods/Readme.mod_regex.txt 
b/doc/dsm/mods/Readme.mod_regex.txt
index bf74068..af7caba 100644
--- a/doc/dsm/mods/Readme.mod_regex.txt
+++ b/doc/dsm/mods/Readme.mod_regex.txt
@@ -15,6 +15,8 @@ regex.compile(name, reg_ex)
 regex.match(name, match_string)
  Match match_string on regex referenced by name.
  $regex.match is set to 1 if matched, 0 if not matched.
+ $regex.match[n] is set to nth substring match, starting with 1
+ ($regex.match[1] is set to first substring match...)
 
 regex.clear(name)
  Clear the regex referenced by name.
@@ -22,9 +24,8 @@ regex.clear(name)
 Conditions:
  regex.match(name, match_string)
   Match match_string on regex referenced by name.
-
-
+  $regex.match[n] is set to nth substring match, starting with 1
+  ($regex.match[1] is set to first substring match...)
 
 TODO:
- - implement substring adressing
  - find a better way for $regex.match side-effect

_______________________________________________
Semsdev mailing list
[email protected]
http://lists.iptel.org/mailman/listinfo/semsdev

Reply via email to