Re: [PATCH 1/1] Teach for-each-ref about a little language called Tcl.
- From
Sergey Vlasov <vsu@altlinux.ru>
- Date
- Jan 28, 2007, 09:49 UTC
- Message-ID
- <20070128094909.GA3740@procyon.home>
- In-Reply-To
- <20070128073913.GA9915@spearce.org>
On Sun, Jan 28, 2007 at 02:39:13AM -0500, Shawn O. Pearce wrote:
Show 8 quoted lines
> Love it or hate it, some people actually still program in Tcl. Some > of those programs are meant for interfacing with Git. Programs such as > gitk and git-gui. It may be useful to have Tcl-safe output available > from for-each-ref, just like shell, Perl and Python already enjoy. > > Thanks to Sergey Vlasov for pointing out the horrible flaws in the > first version of this patch, and steering me in the right direction > for Tcl value quoting.
[...]
Show 35 quoted lines
> +void tcl_quote_print(FILE *stream, const char *src)
> +{
> + char c;
> +
> + fputc('"', stream);
> + while ((c = *src++)) {
> + switch (c) {
> + 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);
> +}This is better; however, wrapping this format inside { ... } (which you may want to do in the template for some uses) won't work if the string contains unmatched braces. Quoting '{' and '}' characters with backslashes should fix this.
BTW, escaping newline characters as done here is not strictly required for a double-quoted string, but is very useful, because you may read the output line by line with "gets" and get fields from each line with "lindex"; without this escaping you will need to read the whole output before trying to parse it as a single huge list.