From 6731805c0c030d5ca08b51d489360d8e3deb5054 Mon Sep 17 00:00:00 2001 From: "Steffen (Daode) Nurpmeso" Date: Tue, 12 Sep 2017 19:44:25 +0200 Subject: [PATCH] Turn a_head_customhdr__sep() to n_strsep_esc() --- head.c | 53 +---------------------------------------------- nailfuns.h | 5 +++-- strings.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 64 insertions(+), 64 deletions(-) diff --git a/head.c b/head.c index 8ce9dcf8..88af3a50 100644 --- a/head.c +++ b/head.c @@ -116,9 +116,6 @@ static int charcount(char *str, int c); static char const * nexttoken(char const *cp); -/* TODO v15: change *customhdr* syntax and use shell tokens?! */ -static char *a_head_customhdr__sep(char **iolist); - static char const * _from__skipword(char const *wp) { @@ -922,54 +919,6 @@ nexttoken(char const *cp) return cp; } -static char * -a_head_customhdr__sep(char **iolist){ - char *cp, c, *base; - bool_t isesc, anyesc; - NYD2_ENTER; - - for(base = *iolist; base != NULL; base = *iolist){ - while((c = *base) != '\0' && blankspacechar(c)) - ++base; - - for(isesc = anyesc = FAL0, cp = base;; ++cp){ - if(n_UNLIKELY((c = *cp) == '\0')){ - *iolist = NULL; - break; - }else if(!isesc){ - if(c == ','){ - *iolist = cp + 1; - break; - } - isesc = (c == '\\'); - }else{ - isesc = FAL0; - anyesc |= (c == ','); - } - } - - while(cp > base && blankspacechar(cp[-1])) - --cp; - *cp = '\0'; - - if(*base != '\0'){ - if(anyesc){ - char *ins; - - for(ins = cp = base;; ++ins) - if((c = *cp) == '\\' && cp[1] == ','){ - *ins = ','; - cp += 2; - }else if((*ins = (++cp, c)) == '\0') - break; - } - break; - } - } - NYD2_LEAVE; - return base; -} - FL char const * myaddrs(struct header *hp) /* TODO */ { @@ -2554,7 +2503,7 @@ n_customhdr_query(void){ tail = &rv; buf = savestr(vp); jch_outer: - while((vp = a_head_customhdr__sep(&buf)) != NULL){ + while((vp = n_strsep_esc(&buf, ',', TRU1)) != NULL){ ui32_t nl, bl; char const *nstart, *cp; diff --git a/nailfuns.h b/nailfuns.h index 0df46a10..3a3eb91b 100644 --- a/nailfuns.h +++ b/nailfuns.h @@ -2157,9 +2157,10 @@ FL bool_t n_anyof_buf(char const *template, char const *dat, size_t len); * next entry, trimming surrounding whitespace, and point *iolist to the next * entry or to NULL if no more entries are contained. If ignore_empty is * set empty entries are started over. - * See n_shexp_parse_token() for the new way that supports sh(1) quoting. + * strsep_esc() is identical but allows reverse solidus escaping of sep, too. * Return NULL or an entry */ -FL char * n_strsep(char **iolist, char sep, bool_t ignore_empty); +FL char *n_strsep(char **iolist, char sep, bool_t ignore_empty); +FL char *n_strsep_esc(char **iolist, char sep, bool_t ignore_empty); /* Copy a string, lowercasing it as we go; *size* is buffer size of *dest*; * *dest* will always be terminated unless *size* is 0 */ diff --git a/strings.c b/strings.c index 9474953b..ef8648d5 100644 --- a/strings.c +++ b/strings.c @@ -205,25 +205,75 @@ n_anyof_buf(char const *template, char const *dat, size_t len){ } FL char * -n_strsep(char **iolist, char sep, bool_t ignore_empty) -{ +n_strsep(char **iolist, char sep, bool_t ignore_empty){ char *base, *cp; NYD2_ENTER; - for (base = *iolist; base != NULL; base = *iolist) { - while (*base != '\0' && blankspacechar(*base)) + for(base = *iolist; base != NULL; base = *iolist){ + while(*base != '\0' && blankspacechar(*base)) ++base; + cp = strchr(base, sep); - if (cp != NULL) - *iolist = cp + 1; - else { + if(cp != NULL) + *iolist = &cp[1]; + else{ *iolist = NULL; - cp = base + strlen(base); + cp = &base[strlen(base)]; + } + while(cp > base && blankspacechar(cp[-1])) + --cp; + *cp = '\0'; + if(*base != '\0' || !ignore_empty) + break; + } + NYD2_LEAVE; + return base; +} + +FL char * +n_strsep_esc(char **iolist, char sep, bool_t ignore_empty){ + char *cp, c, *base; + bool_t isesc, anyesc; + NYD2_ENTER; + + for(base = *iolist; base != NULL; base = *iolist){ + while((c = *base) != '\0' && blankspacechar(c)) + ++base; + + for(isesc = anyesc = FAL0, cp = base;; ++cp){ + if(n_UNLIKELY((c = *cp) == '\0')){ + *iolist = NULL; + break; + }else if(!isesc){ + if(c == sep){ + *iolist = &cp[1]; + break; + } + isesc = (c == '\\'); + }else{ + isesc = FAL0; + anyesc |= (c == sep); + } } - while (cp > base && blankspacechar(cp[-1])) + + while(cp > base && blankspacechar(cp[-1])) --cp; *cp = '\0'; - if (*base != '\0' || !ignore_empty) + + if(*base != '\0'){ + if(anyesc){ + char *ins; + + for(ins = cp = base;; ++ins) + if((c = *cp) == '\\' && cp[1] == sep){ + *ins = sep; + cp += 2; + }else if((*ins = (++cp, c)) == '\0') + break; + } + } + + if(*base != '\0' || !ignore_empty) break; } NYD2_LEAVE; -- 2.11.4.GIT