Mercurial > hg > dhcpcd
changeset 4429:85da28e7b6e7 draft
options: improve string parsing some more
| author | Roy Marples <roy@marples.name> |
|---|---|
| date | Thu, 11 Apr 2019 17:18:29 +0100 |
| parents | 27de5fd1012a |
| children | 4d90f9a458cc |
| files | src/if-options.c |
| diffstat | 1 files changed, 21 insertions(+), 21 deletions(-) [+] |
line wrap: on
line diff
--- a/src/if-options.c Wed Apr 10 22:58:36 2019 +0100 +++ b/src/if-options.c Thu Apr 11 17:18:29 2019 +0100 @@ -280,22 +280,27 @@ return newlist[i]; } -#define parse_string(buf, len, arg) parse_string_hwaddr(buf, len, arg, 0) +#define PARSE_STRING 0 +#define PARSE_STRING_NULL 1 +#define PARSE_HWADDR 2 +#define parse_string(a, b, c) parse_str((a), (b), (c), PARSE_STRING) +#define parse_hwaddr(a, b, c) parse_str((a), (b), (c), PARSE_HWADDR) static ssize_t -parse_string_hwaddr(char *sbuf, size_t slen, const char *str, int clid) +parse_str(char *sbuf, size_t slen, const char *str, int flags) { size_t l; - const char *p; - int i, punt_last = 0; + const char *p, *end; + int i; char c[4], cmd; + end = str + strlen(str); /* If surrounded by quotes then it's a string */ if (*str == '"') { - str++; - l = strlen(str); - p = str + l - 1; - if (*p == '"') - punt_last = 1; + p = end - 1; + if (*p == '"') { + str++; + end = p; + } } else { l = (size_t)hwaddr_aton(NULL, str); if ((ssize_t) l != -1 && l > 1) { @@ -312,13 +317,13 @@ l = 0; /* If processing a string on the clientid, first byte should be * 0 to indicate a non hardware type */ - if (clid && *str) { + if (flags == PARSE_HWADDR && *str) { if (sbuf) *sbuf++ = 0; l++; } c[3] = '\0'; - while (*str) { + while (str < end) { if (++l > slen && sbuf) { errno = ENOBUFS; return -1; @@ -386,12 +391,7 @@ str++; } } - if (punt_last) { - if (sbuf) - --sbuf; - l--; - } - if (sbuf) + if (flags == PARSE_STRING_NULL && sbuf) *sbuf = '\0'; return (ssize_t)l; } @@ -713,7 +713,7 @@ ARG_REQUIRED; if (ifo->script != default_script) free(ifo->script); - s = parse_string(NULL, 0, arg); + s = parse_str(NULL, 0, arg, PARSE_STRING_NULL); if (s == 0) { ifo->script = NULL; break; @@ -724,7 +724,7 @@ logerr(__func__); return -1; } - parse_string(ifo->script, dl, arg); + parse_str(ifo->script, dl, arg, PARSE_STRING_NULL); if (ifo->script[0] == '\0' || strcmp(ifo->script, "/dev/null") == 0) { @@ -1025,8 +1025,8 @@ /* Strings have a type of 0 */; ifo->clientid[1] = 0; if (arg) - s = parse_string_hwaddr((char *)ifo->clientid + 1, - CLIENTID_MAX_LEN, arg, 1); + s = parse_hwaddr((char *)ifo->clientid + 1, + CLIENTID_MAX_LEN, arg); else s = 0; if (s == -1) {
