diff options
-rw-r--r-- | Doc/Zsh/expn.yo | 4 | ||||
-rw-r--r-- | Src/subst.c | 44 |
2 files changed, 38 insertions, 10 deletions
diff --git a/Doc/Zsh/expn.yo b/Doc/Zsh/expn.yo index 43562fbf3..da6b43cb8 100644 --- a/Doc/Zsh/expn.yo +++ b/Doc/Zsh/expn.yo @@ -582,7 +582,9 @@ of alphanumeric characters separated by non-alphanumerics, em(not) to words that result from field splitting. ) item(tt(q))( -Quote the resulting words with backslashes. +Quote the resulting words with backslashes. If this flag is given +twice, the resulting words are quoted in single quotes and if it is +given three times, the words are quoted in double quotes. ) item(tt(Q))( Remove one level of quotes from the resulting words. diff --git a/Src/subst.c b/Src/subst.c index f5d13b500..b0bb885c9 100644 --- a/Src/subst.c +++ b/Src/subst.c @@ -721,7 +721,7 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub) int flnum = 0; int sortit = 0, casind = 0; int casmod = 0; - int quotemod = 0, quoteerr = 0; + int quotemod = 0, quotetype = 0, quoteerr = 0; char *sep = NULL, *spsep = NULL; char *premul = NULL, *postmul = NULL, *preone = NULL, *postone = NULL; char *replstr = NULL; /* replacement string for /orig/repl */ @@ -826,7 +826,7 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub) break; case 'q': - quotemod++; + quotemod++, quotetype++; break; case 'Q': quotemod--; @@ -1617,6 +1617,8 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub) opts[PROMPTPERCENT] = opp; } if (quotemod) { + if (--quotetype > 2) + quotetype = 2; if (isarr) { char **ap; @@ -1624,10 +1626,23 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub) aval = arrdup(aval), copied = 1; ap = aval; - if (quotemod > 0) - for (; *ap; ap++) - *ap = bslashquote(*ap, NULL, 0); - else { + if (quotemod > 0) { + if (quotetype) { + int sl; + char *tmp; + + for (; *ap; ap++) { + tmp = bslashquote(*ap, NULL, quotetype); + sl = strlen(tmp); + *ap = (char *) zhalloc(sl + 3); + strcpy((*ap) + 1, tmp); + ap[0][0] = ap[0][sl + 1] = (quotetype == 1 ? '\'' : '"'); + ap[0][sl + 2] = '\0'; + } + } else + for (; *ap; ap++) + *ap = bslashquote(*ap, NULL, 0); + } else { int one = noerrs, oef = errflag, haserr = 0; if (!quoteerr) @@ -1648,9 +1663,20 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int ssub) } else { if (!copied) val = dupstring(val), copied = 1; - if (quotemod > 0) - val = bslashquote(val, NULL, 0); - else { + if (quotemod > 0) { + if (quotetype) { + int sl; + char *tmp; + + tmp = bslashquote(val, NULL, quotetype); + sl = strlen(tmp); + val = (char *) zhalloc(sl + 3); + strcpy(val + 1, tmp); + val[0] = val[sl + 1] = (quotetype == 1 ? '\'' : '"'); + val[sl + 2] = '\0'; + } else + val = bslashquote(val, NULL, 0); + } else { int one = noerrs, oef = errflag, haserr; if (!quoteerr) |