[Asterisk-code-review] config: Allow filters when appending to a category (asterisk[13])

George Joseph asteriskteam at digium.com
Sun Mar 27 23:53:41 CDT 2016


George Joseph has uploaded a new change for review.

  https://gerrit.asterisk.org/2487

Change subject: config:  Allow filters when appending to a category
......................................................................

config:  Allow filters when appending to a category

In sorcery based config files where there are multiple categories with the same
name, you can't use the (+) operator to reliably append to a category because
config.c stops looking when it finds the first one with the same name.

Example:

[1000]
type = endpoint

[1000]
type = aor

[1000](+)
authenticate_qualify = yes

This config will fail because config.c appends authenticate_qualify to the
first category it finds, the endpoint, and that's not valid for endpoint.

Solution:

The capability to find a category that contains a certain variable already
exists so the only real change was to parse anything after the '+' that's not a
comma, as a filter string.

[1000]
type = endpoint

[1000]
type = aor

[1000](+type=aor)
authenticate_qualify = yes

This now works as expected.

Although the following example doesn't make any sense for pjsip, you can even
specify multiple filters:

[1000](+type=aor&qualify_frequency=10)

ASTERISK-25868 #close
Reported-by: Nick Repin

Change-Id: I10773da4c79db36fbf1993961992af63d3441580
---
M include/asterisk/strings.h
M main/config.c
M main/utils.c
3 files changed, 36 insertions(+), 6 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/87/2487/1

diff --git a/include/asterisk/strings.h b/include/asterisk/strings.h
index 3701b53..b5302e3 100644
--- a/include/asterisk/strings.h
+++ b/include/asterisk/strings.h
@@ -304,6 +304,24 @@
 char *ast_strsep(char **s, const char sep, uint32_t flags);
 
 /*!
+  \brief Like ast_strsep but allows multiple separators
+  \since 13.9.0
+
+  \param s Pointer to address of the the string to be processed.
+    Will be modified and can't be constant.
+  \param sep A string of delimiters
+  \param flags Controls post-processing of the result.
+    AST_STRSEP_TRIM trims all leading and trailing whitespace from the result.
+    AST_STRSEP_STRIP does a trim then strips the outermost quotes.  You may want
+      to trim again after the strip.  Just OR both the TRIM and STRIP flags.
+    AST_STRSEP_UNESCAPE unescapes '\' sequences.
+    AST_STRSEP_ALL does all of the above processing.
+  \return The next token or NULL if done or if there are more than 8 levels of
+  nested quotes.
+*/
+char *ast_strsep2(char **s, const char *sep, uint32_t flags);
+
+/*!
   \brief Strip backslash for "escaped" semicolons, 
 	the string to be stripped (will be modified).
   \return The stripped string.
diff --git a/main/config.c b/main/config.c
index 74f42cf..1961cf9 100644
--- a/main/config.c
+++ b/main/config.c
@@ -793,7 +793,7 @@
 
 	dupmatch = ast_strdupa(match);
 
-	while ((nvp = ast_strsep(&dupmatch, ',', AST_STRSEP_STRIP))) {
+	while ((nvp = ast_strsep2(&dupmatch, ",&", AST_STRSEP_STRIP))) {
 		struct ast_variable *v;
 		char *match_name;
 		char *match_value = NULL;
@@ -1687,8 +1687,12 @@
 			while ((cur = strsep(&c, ","))) {
 				if (!strcasecmp(cur, "!")) {
 					(*cat)->ignored = 1;
-				} else if (!strcasecmp(cur, "+")) {
-					*cat = ast_category_get(cfg, catname, NULL);
+				} else if (cur[0] == '+') {
+					char *filter = NULL;
+					if (cur[1] != ',') {
+						filter = &cur[1];
+					}
+					*cat = ast_category_get(cfg, catname, filter);
 					if (!(*cat)) {
 						if (newcat) {
 							ast_category_destroy(newcat);
diff --git a/main/utils.c b/main/utils.c
index 686af6b..62e8f36 100644
--- a/main/utils.c
+++ b/main/utils.c
@@ -1516,7 +1516,7 @@
 	return s;
 }
 
-char *ast_strsep(char **iss, const char sep, uint32_t flags)
+char *ast_strsep2(char **iss, const char *sep, uint32_t flags)
 {
 	char *st = *iss;
 	char *is;
@@ -1524,7 +1524,7 @@
 	int found = 0;
 	char stack[8];
 
-	if (iss == NULL || *iss == '\0') {
+	if (!iss || *iss == '\0' || !sep) {
 		return NULL;
 	}
 
@@ -1550,7 +1550,7 @@
 			}
 		}
 
-		if (*is == sep && !inquote) {
+		if (strchr(sep, *is) && !inquote) {
 			*is = '\0';
 			found = 1;
 			*iss = is + 1;
@@ -1576,6 +1576,14 @@
 	return st;
 }
 
+char *ast_strsep(char **iss, const char sep, uint32_t flags)
+{
+	const char string_sep[] = { sep, '\0' };
+
+	return ast_strsep2(iss, string_sep, flags);
+}
+
+
 char *ast_unescape_semicolon(char *s)
 {
 	char *e;

-- 
To view, visit https://gerrit.asterisk.org/2487
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I10773da4c79db36fbf1993961992af63d3441580
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Owner: George Joseph <george.joseph at fairview5.com>



More information about the asterisk-code-review mailing list