static char *sq_dequote_step(char *arg, char **next) { char *dst = arg; char *src = arg; char c; if (*src != '\'') return NULL; for (;;) { c = *++src; if (!c) return NULL; if (c != '\'') { *dst++ = c; continue; } /* We stepped out of sq */ switch (*++src) { case '\0': *dst = 0; if (next) *next = NULL; return arg; case '\\': /* * Allow backslashed characters outside of * single-quotes only if they need escaping, * and only if we resume the single-quoted part * afterward. */ if (need_bs_quote(src[1]) && src[2] == '\'') { *dst++ = src[1]; src += 2; continue; } /* Fallthrough */ default: if (!next || !isspace(*src)) return NULL; do { c = *++src; } while (isspace(c)); *dst = 0; *next = src; return arg; } } }
void sq_quote_print(FILE *stream, const char *src) { char c; fputc('\'', stream); while ((c = *src++)) { if (need_bs_quote(c)) { fputs("'\\", stream); fputc(c, stream); fputc('\'', stream); } else { fputc(c, stream); } } fputc('\'', stream); }
static char * sq_dequote_step(char *arg, char **next) { char *dst = arg; char *src = arg; char c; if (*src != '\'') return NULL; for (;;) { c = *++src; if (!c) return NULL; if (c != '\'') { *dst++ = c; continue; } /* We stepped out of sq */ switch (*++src) { case '\0': *dst = 0; if (next) *next = NULL; return arg; case '\\': c = *++src; if (need_bs_quote(c) && *++src == '\'') { *dst++ = c; continue; } /* Fallthrough */ default: if (!next || !isspace(*src)) return NULL; do { c = *++src; } while (isspace(c)); *dst = 0; *next = src; return arg; } } }
/* Help to copy the thing properly quoted for the shell safety. * any single quote is replaced with '\'', any exclamation point * is replaced with '\!', and the whole thing is enclosed in a * single quote pair. * * E.g. * original sq_quote result * name ==> name ==> 'name' * a b ==> a b ==> 'a b' * a'b ==> a'\''b ==> 'a'\''b' * a!b ==> a'\!'b ==> 'a'\!'b' */ void sq_quote_buf(struct strbuf *dst, const char *src) { char *to_free = NULL; if (dst->buf == src) to_free = strbuf_detach(dst, NULL); strbuf_addch(dst, '\''); while (*src) { size_t len = strcspn(src, "'!"); strbuf_add(dst, src, len); src += len; while (need_bs_quote(*src)) { strbuf_addstr(dst, "'\\"); strbuf_addch(dst, *src++); strbuf_addch(dst, '\''); } } strbuf_addch(dst, '\''); free(to_free); }
static int sq_quote_buf(struct strbuf *dst, const char *src) { char *to_free = NULL; int ret; if (dst->buf == src) to_free = strbuf_detach(dst, NULL); ret = strbuf_addch(dst, '\''); while (!ret && *src) { size_t len = strcspn(src, "'!"); ret = strbuf_add(dst, src, len); src += len; while (!ret && need_bs_quote(*src)) ret = strbuf_addf(dst, "'\\%c\'", *src++); } if (!ret) ret = strbuf_addch(dst, '\''); free(to_free); return ret; }