improve and document function get_in_brackets(), introducing

a helper function find_closing_quote() of more general use.



git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@44651 65c4cc65-6c06-0410-ace0-fbb531ad65f3
1.6.0
Luigi Rizzo 19 years ago
parent 6466c2ebf9
commit e691246b6e

@ -2138,47 +2138,65 @@ static int send_request(struct sip_pvt *p, struct sip_request *req, enum xmittyp
return res; return res;
} }
/*! \brief Locate closing quote in a string, skipping escaped quotes.
* optionally with a limit on the search.
* start must be past the first quote.
*/
static const char *find_closing_quote(const char *start, const char *lim)
{
char last_char = '\0';
const char *s;
for (s = start; *s && s != lim; last_char = *s++) {
if (*s == '"' && last_char != '\\')
break;
}
return s;
}
/*! \brief Pick out text in brackets from character string /*! \brief Pick out text in brackets from character string
\return pointer to terminated stripped string \return pointer to terminated stripped string
\param tmp input string that will be modified */ \param tmp input string that will be modified
Examples:
"foo" <bar> valid input, returns bar
foo returns the whole string
< "foo ... > returns the string between brackets
< "foo... bogus (missing closing bracket), returns the whole string
XXX maybe should still skip the opening bracket
*/
static char *get_in_brackets(char *tmp) static char *get_in_brackets(char *tmp)
{ {
char *parse; const char *parse = tmp;
char *first_quote;
char *first_bracket; char *first_bracket;
char *second_bracket;
char last_char;
parse = tmp; /*
for (;;) { * Skip any quoted text until we find the part in brackets.
first_quote = strchr(parse, '"'); * On any error give up and return the full string.
first_bracket = strchr(parse, '<'); */
if (first_quote && first_bracket && (first_quote < first_bracket)) { while ( (first_bracket = strchr(parse, '<')) ) {
last_char = '\0'; char *first_quote = strchr(parse, '"');
for (parse = first_quote + 1; *parse; parse++) {
if ((*parse == '"') && (last_char != '\\')) if (!first_quote || first_quote > first_bracket)
break; break; /* no need to look at quoted part */
last_char = *parse; /* the bracket is within quotes, so ignore it */
} parse = find_closing_quote(first_quote + 1, NULL);
if (!*parse) { if (!*parse) { /* not found, return full string ? */
ast_log(LOG_WARNING, "No closing quote found in '%s'\n", tmp); /* XXX or be robust and return in-bracket part ? */
return tmp; ast_log(LOG_WARNING, "No closing quote found in '%s'\n", tmp);
} break;
parse++;
continue;
} }
if (first_bracket) { parse++;
second_bracket = strchr(first_bracket + 1, '>'); }
if (second_bracket) { if (first_bracket) {
*second_bracket = '\0'; char *second_bracket = strchr(first_bracket + 1, '>');
return first_bracket + 1; if (second_bracket) {
} else { *second_bracket = '\0';
ast_log(LOG_WARNING, "No closing bracket found in '%s'\n", tmp); tmp = first_bracket + 1;
return tmp; } else {
} ast_log(LOG_WARNING, "No closing bracket found in '%s'\n", tmp);
} }
return tmp;
} }
return tmp;
} }
/*! \brief Send SIP MESSAGE text within a call /*! \brief Send SIP MESSAGE text within a call

Loading…
Cancel
Save