git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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.

Previous: Shawn O. PearceNext: Junio C Hamano
Message 2 of 4 in “Teach for-each-ref about a little language called Tcl.”
  1. 1/1 Teach for-each-ref about a little language called Tcl.Shawn O. Pearce, Jan 28, 2007
  2. Sergey VlasovJan 28, 2007
  3. Junio C HamanoJan 28, 2007
  4. Sergey VlasovJan 29, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.