Skip to content

Commit

Permalink
publish skip_string API for quoted strings
Browse files Browse the repository at this point in the history
  • Loading branch information
alandekok committed Apr 6, 2020
1 parent eaf6092 commit 568cc1b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
35 changes: 2 additions & 33 deletions src/lib/unlang/xlat_tokenize.c
Original file line number Diff line number Diff line change
Expand Up @@ -738,37 +738,6 @@ static ssize_t xlat_tokenize_literal(TALLOC_CTX *ctx, xlat_exp_t **head, char co
return p - in;
}

static ssize_t skip_string(char const *start, char const *end)
{
char const *p = start;
char quote;

quote = *(p++);

while (p < end) {
if (*p == quote) {
p++;
return p - start;
}

if (*p == '\\') {
if (((p + 1) >= end) || !p[1]) {
break;
}

p += 2;
continue;
}

p++;
}

/*
* Unexpected end of string.
*/
fr_strerror_printf("Unexpected end of string");
return -(p - start);
}

/** skip xlat expansions, included embedded strings and nested xlats.
*
Expand All @@ -785,7 +754,7 @@ static ssize_t skip_xlat(char const *start, char const *end)
}

if ((*p == '"') || (*p == '\'')) {
slen = skip_string(p, end);
slen = fr_skip_string(p, end);
if (slen < 0) return slen - (p - start);

p += slen;
Expand Down Expand Up @@ -879,7 +848,7 @@ ssize_t xlat_tokenize_argv(TALLOC_CTX *ctx, xlat_exp_t **head, char const *in, s
* Look for quoted strings.
*/
if ((*p == '"') || (*p == '\'')) {
slen = skip_string(p, end);
slen = fr_skip_string(p, end);
if (slen < 0) return slen - (p - in);

} else {
Expand Down
33 changes: 33 additions & 0 deletions src/lib/util/token.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,3 +487,36 @@ char const *fr_token_name(int token)
{
return fr_table_str_by_value(fr_tokens_table, token, "???");
}


ssize_t fr_skip_string(char const *start, char const *end)
{
char const *p = start;
char quote;

quote = *(p++);

while (p < end) {
if (*p == quote) {
p++;
return p - start;
}

if (*p == '\\') {
if (((p + 1) >= end) || !p[1]) {
break;
}

p += 2;
continue;
}

p++;
}

/*
* Unexpected end of string.
*/
fr_strerror_printf("Unexpected end of string");
return -(p - start);
}
1 change: 1 addition & 0 deletions src/lib/util/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ FR_TOKEN gettoken(char const **ptr, char *buf, int buflen, bool unescape);
FR_TOKEN getop(char const **ptr);
FR_TOKEN getstring(char const **ptr, char *buf, int buflen, bool unescape);
char const *fr_token_name(int);
ssize_t fr_skip_string(char const *start, char const *end);

#ifdef __cplusplus
}
Expand Down

0 comments on commit 568cc1b

Please sign in to comment.