threads / patch / 6568

patchTeach for-each-ref about a little language called Tcl.

Subject: [PATCH 1/1] Teach for-each-ref about a little language called Tcl.

## tl;dr

4 messages between Jan 28, 2007 and Jan 29, 2007. Diffs are folded; open one to read it.

replies: 3people: 3as markdown or json

Shawn O. Pearce· Jan 28, 2007, 07:39 UTC · lore

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.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 This replaces my prior patch.  As Sergey pointed out, it was
 horribly broken.
 Documentation/git-for-each-ref.txt |    4 ++--
 builtin-for-each-ref.c             |   10 ++++++++++
 quote.c                            |   36 ++++++++++++++++++++++++++++++++++++
 quote.h                            |    1 +
 4 files changed, 49 insertions(+), 2 deletions(-)
Show changes to 4 files +49 −1

Documentation/git-for-each-ref.txt, builtin-for-each-ref.c, quote.c, quote.h

diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt
index 06e7ab1..da52eba 100644
--- a/Documentation/git-for-each-ref.txt
+++ b/Documentation/git-for-each-ref.txt
@@ -7,7 +7,7 @@ git-for-each-ref - Output information on each ref
 
 SYNOPSIS
 --------
-'git-for-each-ref' [--count=<count>]\* [--shell|--perl|--python] [--sort=<key>]\* [--format=<format>] [<pattern>]
+'git-for-each-ref' [--count=<count>]\* [--shell|--perl|--python|--tcl] [--sort=<key>]\* [--format=<format>] [<pattern>]
 
 DESCRIPTION
 -----------
@@ -49,7 +49,7 @@ OPTIONS
 	using fnmatch(3).  Refs that do not match the pattern
 	are not shown.
 
---shell, --perl, --python::
+--shell, --perl, --python, --tcl::
 	If given, strings that substitute `%(fieldname)`
 	placeholders are quoted as string literals suitable for
 	the specified host language.  This is meant to produce
diff --git a/builtin-for-each-ref.c b/builtin-for-each-ref.c
index af72a12..9d5f266 100644
--- a/builtin-for-each-ref.c
+++ b/builtin-for-each-ref.c
@@ -12,6 +12,7 @@
 #define QUOTE_SHELL 1
 #define QUOTE_PERL 2
 #define QUOTE_PYTHON 3
+#define QUOTE_TCL 4
 
 typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
 
@@ -723,6 +724,9 @@ static void print_value(struct refinfo *ref, int atom, int quote_style)
 	case QUOTE_PYTHON:
 		python_quote_print(stdout, v->s);
 		break;
+	case QUOTE_TCL:
+		tcl_quote_print(stdout, v->s);
+		break;
 	}
 }
 
@@ -834,6 +838,12 @@ int cmd_for_each_ref(int ac, const char **av, char *prefix)
 			quote_style = QUOTE_PYTHON;
 			continue;
 		}
+		if (!strcmp(arg, "--tcl") ) {
+			if (0 <= quote_style)
+				die("more than one quoting style?");
+			quote_style = QUOTE_TCL;
+			continue;
+		}
 		if (!strncmp(arg, "--count=", 8)) {
 			if (maxcount)
 				die("more than one --count?");
diff --git a/quote.c b/quote.c
index a418a0f..01db5e2 100644
--- a/quote.c
+++ b/quote.c
@@ -387,3 +387,39 @@ void python_quote_print(FILE *stream, const char *src)
 	}
 	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 '"':
+			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);
+}
diff --git a/quote.h b/quote.h
index b55e699..bdc3610 100644
--- a/quote.h
+++ b/quote.h
@@ -55,5 +55,6 @@ extern void write_name_quoted(const char *prefix, int prefix_len,
 /* quoting as a string literal for other languages */
 extern void perl_quote_print(FILE *stream, const char *src);
 extern void python_quote_print(FILE *stream, const char *src);
+extern void tcl_quote_print(FILE *stream, const char *src);
 
 #endif
-- 
1.5.0.rc2.gf127
Sergey Vlasov· Jan 28, 2007, 09:49 UTC · re: Shawn O. Pearce · lore

Re: [PATCH 1/1] Teach for-each-ref about a little language called Tcl.

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.

Junio C Hamano· Jan 28, 2007, 20:58 UTC · re: Sergey Vlasov · lore

Re: [PATCH 1/1] Teach for-each-ref about a little language called Tcl.

Sergey Vlasov <vsu@altlinux.ru> writes:
> 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.
Then I'd commit after applying this on top of Shawn's.  Ok?
Show changes to quote.c +3 −5
diff --git a/quote.c b/quote.c
index 01db5e2..fb9e4ca 100644
--- a/quote.c
+++ b/quote.c
@@ -395,11 +395,9 @@ void tcl_quote_print(FILE *stream, const char *src)
 	fputc('"', stream);
 	while ((c = *src++)) {
 		switch (c) {
-		case '[':
-		case ']':
-		case '$':
-		case '\\':
-		case '"':
+		case '[': case ']':
+		case '{': case '}':
+		case '$': case '\\': case '"':
 			fputc('\\', stream);
 		default:
 			fputc(c, stream);
Sergey Vlasov· Jan 29, 2007, 09:51 UTC · re: Junio C Hamano · lore

Re: [PATCH 1/1] Teach for-each-ref about a little language called Tcl.

On Sun, Jan 28, 2007 at 12:58:51PM -0800, Junio C Hamano wrote:
Show 21 quoted lines
> Then I'd commit after applying this on top of Shawn's.  Ok?
> 
> diff --git a/quote.c b/quote.c
> index 01db5e2..fb9e4ca 100644
> --- a/quote.c
> +++ b/quote.c
> @@ -395,11 +395,9 @@ void tcl_quote_print(FILE *stream, const char *src)
>  	fputc('"', stream);
>  	while ((c = *src++)) {
>  		switch (c) {
> -		case '[':
> -		case ']':
> -		case '$':
> -		case '\\':
> -		case '"':
> +		case '[': case ']':
> +		case '{': case '}':
> +		case '$': case '\\': case '"':
>  			fputc('\\', stream);
>  		default:
>  			fputc(c, stream);
Looks OK (still didn't have time to actually test this thing).

← back to recent threads