X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;ds=sidebyside;f=quote.c;h=d88bf7515932bba96c694478c3b51c85549fa92a;hb=967506bbbdc38e75263d6e6a90e3b8dbba6cec0f;hp=e3a4d4aef3478148ff89aebeba23e1cbf4202ba1;hpb=f5961572a02ef08324f297fe93b704ff2137e5a6;p=git.git diff --git a/quote.c b/quote.c index e3a4d4aef..d88bf7515 100644 --- a/quote.c +++ b/quote.c @@ -20,7 +20,7 @@ static inline int need_bs_quote(char c) return (c == '\'' || c == '!'); } -size_t sq_quote_buf(char *dst, size_t n, const char *src) +static size_t sq_quote_buf(char *dst, size_t n, const char *src) { char c; char *bp = dst; @@ -62,18 +62,6 @@ void sq_quote_print(FILE *stream, const char *src) fputc('\'', stream); } -char *sq_quote(const char *src) -{ - char *buf; - size_t cnt; - - cnt = sq_quote_buf(NULL, 0, src) + 1; - buf = xmalloc(cnt); - sq_quote_buf(buf, cnt, src); - - return buf; -} - char *sq_quote_argv(const char** argv, int count) { char *buf, *to; @@ -200,7 +188,8 @@ static int quote_c_style_counted(const char *name, int namelen, #define EMITQ() EMIT('\\') const char *sp; - int ch, count = 0, needquote = 0; + unsigned char ch; + int count = 0, needquote = 0; if (!no_dq) EMIT('"'); @@ -209,7 +198,7 @@ static int quote_c_style_counted(const char *name, int namelen, if (!ch) break; if ((ch < ' ') || (ch == '"') || (ch == '\\') || - (ch == 0177)) { + (quote_path_fully && (ch >= 0177))) { needquote = 1; switch (ch) { case '\a': EMITQ(); ch = 'a'; break; @@ -349,3 +338,75 @@ void write_name_quoted(const char *prefix, int prefix_len, else goto no_quote; } + +/* quoting as a string literal for other languages */ + +void perl_quote_print(FILE *stream, const char *src) +{ + const char sq = '\''; + const char bq = '\\'; + char c; + + fputc(sq, stream); + while ((c = *src++)) { + if (c == sq || c == bq) + fputc(bq, stream); + fputc(c, stream); + } + fputc(sq, stream); +} + +void python_quote_print(FILE *stream, const char *src) +{ + const char sq = '\''; + const char bq = '\\'; + const char nl = '\n'; + char c; + + fputc(sq, stream); + while ((c = *src++)) { + if (c == nl) { + fputc(bq, stream); + fputc('n', stream); + continue; + } + if (c == sq || c == bq) + fputc(bq, stream); + fputc(c, stream); + } + fputc(sq, stream); +} + +void tcl_quote_print(FILE *stream, const char *src) +{ + char c; + + fputc('"', stream); + while ((c = *src++)) { + switch (c) { + case '[': case ']': + case '{': case '}': + case '$': case '\\': case '"': + fputc('\\', stream); + default: + fputc(c, stream); + break; + case '\f': + fputs("\\f", stream); + break; + case '\r': + fputs("\\r", stream); + break; + case '\n': + fputs("\\n", stream); + break; + case '\t': + fputs("\\t", stream); + break; + case '\v': + fputs("\\v", stream); + break; + } + } + fputc('"', stream); +}