diff options
Diffstat (limited to 'lib/dynamic_debug.c')
| -rw-r--r-- | lib/dynamic_debug.c | 53 |
1 files changed, 33 insertions, 20 deletions
diff --git a/lib/dynamic_debug.c b/lib/dynamic_debug.c index 9e123ae326bc..833139ce1e22 100644 --- a/lib/dynamic_debug.c +++ b/lib/dynamic_debug.c | |||
| @@ -196,32 +196,45 @@ static void ddebug_change(const struct ddebug_query *query, | |||
| 196 | } | 196 | } |
| 197 | 197 | ||
| 198 | /* | 198 | /* |
| 199 | * Wrapper around strsep() to collapse the multiple empty tokens | ||
| 200 | * that it returns when fed sequences of separator characters. | ||
| 201 | * Now, if we had strtok_r()... | ||
| 202 | */ | ||
| 203 | static inline char *nearly_strtok_r(char **p, const char *sep) | ||
| 204 | { | ||
| 205 | char *r; | ||
| 206 | |||
| 207 | while ((r = strsep(p, sep)) != NULL && *r == '\0') | ||
| 208 | ; | ||
| 209 | return r; | ||
| 210 | } | ||
| 211 | |||
| 212 | /* | ||
| 213 | * Split the buffer `buf' into space-separated words. | 199 | * Split the buffer `buf' into space-separated words. |
| 214 | * Return the number of such words or <0 on error. | 200 | * Handles simple " and ' quoting, i.e. without nested, |
| 201 | * embedded or escaped \". Return the number of words | ||
| 202 | * or <0 on error. | ||
| 215 | */ | 203 | */ |
| 216 | static int ddebug_tokenize(char *buf, char *words[], int maxwords) | 204 | static int ddebug_tokenize(char *buf, char *words[], int maxwords) |
| 217 | { | 205 | { |
| 218 | int nwords = 0; | 206 | int nwords = 0; |
| 219 | 207 | ||
| 220 | while (nwords < maxwords && | 208 | while (*buf) { |
| 221 | (words[nwords] = nearly_strtok_r(&buf, " \t\r\n")) != NULL) | 209 | char *end; |
| 222 | nwords++; | 210 | |
| 223 | if (buf) | 211 | /* Skip leading whitespace */ |
| 224 | return -EINVAL; /* ran out of words[] before bytes */ | 212 | while (*buf && isspace(*buf)) |
| 213 | buf++; | ||
| 214 | if (!*buf) | ||
| 215 | break; /* oh, it was trailing whitespace */ | ||
| 216 | |||
| 217 | /* Run `end' over a word, either whitespace separated or quoted */ | ||
| 218 | if (*buf == '"' || *buf == '\'') { | ||
| 219 | int quote = *buf++; | ||
| 220 | for (end = buf ; *end && *end != quote ; end++) | ||
| 221 | ; | ||
| 222 | if (!*end) | ||
| 223 | return -EINVAL; /* unclosed quote */ | ||
| 224 | } else { | ||
| 225 | for (end = buf ; *end && !isspace(*end) ; end++) | ||
| 226 | ; | ||
| 227 | BUG_ON(end == buf); | ||
| 228 | } | ||
| 229 | /* Here `buf' is the start of the word, `end' is one past the end */ | ||
| 230 | |||
| 231 | if (nwords == maxwords) | ||
| 232 | return -EINVAL; /* ran out of words[] before bytes */ | ||
| 233 | if (*end) | ||
| 234 | *end++ = '\0'; /* terminate the word */ | ||
| 235 | words[nwords++] = buf; | ||
| 236 | buf = end; | ||
| 237 | } | ||
| 225 | 238 | ||
| 226 | if (verbose) { | 239 | if (verbose) { |
| 227 | int i; | 240 | int i; |
