Skip to contentSkip to conversation

\e in "echo" and "printf" builtins

PATCH In review Adam Borowski 5 participants 8 messages Jun 28, 2014 – Jul 23, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 \e in "echo" and "printf" builtins

Hi!

I'm not sure what's your policy towards extensions, but \e as \033 is something ubiquitous in the Unix world. C compilers (gcc, clang, icc and tcc -- but not MSVC), perl, shells (bash and zsh -- but not dash), etc.

What about supporting it in dash as well?
Patch attached.

Signature · 2 lines
Gnome 3, Windows 8, Slashdot Beta, now Firefox Ribbon^WAustralis.  WTF is going
on with replacing usable interfaces with tabletized ones?

From a6e6e7b6f3a725b4ca0514f22e9ee4cfe2c225e2 Mon Sep 17 00:00:00 2001
From: Adam Borowski <[email protected]>
Date: Sat, 28 Jun 2014 06:29:56 +0200
Subject: [PATCH] Support \e in "echo" and "printf" builtins.

---
 src/bltin/printf.c | 1 +
 src/dash.1         | 4 ++++
 2 files changed, 5 insertions(+)
src/bltin/printf.c +1 -0
index 893295c..98d954c 100644
@@ -300,6 +300,7 @@ conv_escape(char *str, int *conv_ch)
 	case '\\':	value = '\\';	break;	/* backslash */
 	case 'a':	value = '\a';	break;	/* alert */
 	case 'b':	value = '\b';	break;	/* backspace */
+	case 'e':	value = '\e';	break;  /* escape */
 	case 'f':	value = '\f';	break;	/* form-feed */
 	case 'n':	value = '\n';	break;	/* newline */
 	case 'r':	value = '\r';	break;	/* carriage-return */
src/dash.1 +4 -0
index 3847d98..7107faa 100644
@@ -1199,6 +1199,8 @@ Subsequent output is suppressed.  This is normally used at the end of the
 last argument to suppress the trailing newline that
 .Ic echo
 would otherwise output.
+.It Li \ee
+Outputs an escape character (ESC).
 .It Li \ef
 Output a form feed.
 .It Li \en
@@ -1573,6 +1575,8 @@ The characters and their meanings are as follows:
 Write a \*[Lt]bell\*[Gt] character.
 .It Cm \eb
 Write a \*[Lt]backspace\*[Gt] character.
+.It Cm \ee
+Write an \*[Lt]escape\*[Gt] (ESC) character.
 .It Cm \ef
 Write a \*[Lt]form-feed\*[Gt] character.
 .It Cm \en
Signature · 1 line
2.0.0
#1 lore raw
Quoted 8 lines · Adam Borowski

Hi!

I'm not sure what's your policy towards extensions, but \e as \033 is something ubiquitous in the Unix world. C compilers (gcc, clang, icc and tcc -- but not MSVC), perl, shells (bash and zsh -- but not dash), etc.

What about supporting it in dash as well?
Patch attached.

Hi,

No comment on whether dash itself should accept \e, but you already found a compiler that doesn't support it at all, and many of the ones that do support it also (optionally) issue a warning for it. Should the C code perhaps be using \033 instead of \e?

Cheers,
Harald van Dijk

#2 lore raw
Quoted 9 lines · Harald van Dijk
Quoted 3 lines · Adam Borowski

I'm not sure what's your policy towards extensions, but \e as \033 is something ubiquitous in the Unix world. C compilers (gcc, clang, icc and tcc -- but not MSVC), perl, shells (bash and zsh -- but not dash), etc.

No comment on whether dash itself should accept \e, but you already found a compiler that doesn't support it at all, and many of the ones that do support it also (optionally) issue a warning for it. Should the C code perhaps be using \033 instead of \e?

I don't think anyone is going to ever port dash to MSVC, but you have a
point: some other new compiler can appear.

A version of the patch with \033 attached.

Signature · 2 lines
Gnome 3, Windows 8, Slashdot Beta, now Firefox Ribbon^WAustralis.  WTF is going
on with replacing usable interfaces with tabletized ones?

From a6e6e7b6f3a725b4ca0514f22e9ee4cfe2c225e2 Mon Sep 17 00:00:00 2001
From: Adam Borowski <[email protected]>
Date: Sat, 28 Jun 2014 06:29:56 +0200
Subject: [PATCH] Support \e in "echo" and "printf" builtins.

---
 src/bltin/printf.c | 1 +
 src/dash.1         | 4 ++++
 2 files changed, 5 insertions(+)
src/bltin/printf.c +1 -0
index 893295c..98d954c 100644
@@ -300,6 +300,7 @@ conv_escape(char *str, int *conv_ch)
 	case '\\':	value = '\\';	break;	/* backslash */
 	case 'a':	value = '\a';	break;	/* alert */
 	case 'b':	value = '\b';	break;	/* backspace */
+	case 'e':	value = '\033';	break;  /* escape */
 	case 'f':	value = '\f';	break;	/* form-feed */
 	case 'n':	value = '\n';	break;	/* newline */
 	case 'r':	value = '\r';	break;	/* carriage-return */
src/dash.1 +4 -0
index 3847d98..7107faa 100644
@@ -1199,6 +1199,8 @@ Subsequent output is suppressed.  This is normally used at the end of the
 last argument to suppress the trailing newline that
 .Ic echo
 would otherwise output.
+.It Li \ee
+Outputs an escape character (ESC).
 .It Li \ef
 Output a form feed.
 .It Li \en
@@ -1573,6 +1575,8 @@ The characters and their meanings are as follows:
 Write a \*[Lt]bell\*[Gt] character.
 .It Cm \eb
 Write a \*[Lt]backspace\*[Gt] character.
+.It Cm \ee
+Write an \*[Lt]escape\*[Gt] (ESC) character.
 .It Cm \ef
 Write a \*[Lt]form-feed\*[Gt] character.
 .It Cm \en
Signature · 1 line
2.0.0
#3 lore raw
Quoted 7 lines · Adam Borowski
Quoted 6 lines · Harald van Dijk
Quoted 3 lines · Adam Borowski

I'm not sure what's your policy towards extensions, but \e as \033 is something ubiquitous in the Unix world. C compilers (gcc, clang, icc and tcc -- but not MSVC), perl, shells (bash and zsh -- but not dash), etc.

No comment on whether dash itself should accept \e, but [...]

So... can I has an answer whether dash should indeed accept \e ? Being told "no" or "go away, we hate you" is fine, I just dislike having patches rot forever. And not having this shorthand is annoying if you like using colour for highlights -- it works in perl and bash, then suddenly Oops! not in /bin/sh = dash.

Meow!

Signature · 4 lines
// If you believe in so-called "intellectual property", please immediately
// cease using counterfeit alphabets.  Instead, contact the nearest temple
// of Amon, whose priests will provide you with scribal services for all
// your writing needs, for Reasonable and Non-Discriminatory prices.

From a6e6e7b6f3a725b4ca0514f22e9ee4cfe2c225e2 Mon Sep 17 00:00:00 2001
From: Adam Borowski <[email protected]>
Date: Sat, 28 Jun 2014 06:29:56 +0200
Subject: [PATCH] Support \e in "echo" and "printf" builtins.

---
 src/bltin/printf.c | 1 +
 src/dash.1         | 4 ++++
 2 files changed, 5 insertions(+)
src/bltin/printf.c +1 -0
index 893295c..98d954c 100644
@@ -300,6 +300,7 @@ conv_escape(char *str, int *conv_ch)
 	case '\\':	value = '\\';	break;	/* backslash */
 	case 'a':	value = '\a';	break;	/* alert */
 	case 'b':	value = '\b';	break;	/* backspace */
+	case 'e':	value = '\033';	break;  /* escape */
 	case 'f':	value = '\f';	break;	/* form-feed */
 	case 'n':	value = '\n';	break;	/* newline */
 	case 'r':	value = '\r';	break;	/* carriage-return */
src/dash.1 +4 -0
index 3847d98..7107faa 100644
@@ -1199,6 +1199,8 @@ Subsequent output is suppressed.  This is normally used at the end of the
 last argument to suppress the trailing newline that
 .Ic echo
 would otherwise output.
+.It Li \ee
+Outputs an escape character (ESC).
 .It Li \ef
 Output a form feed.
 .It Li \en
@@ -1573,6 +1575,8 @@ The characters and their meanings are as follows:
 Write a \*[Lt]bell\*[Gt] character.
 .It Cm \eb
 Write a \*[Lt]backspace\*[Gt] character.
+.It Cm \ee
+Write an \*[Lt]escape\*[Gt] (ESC) character.
 .It Cm \ef
 Write a \*[Lt]form-feed\*[Gt] character.
 .It Cm \en
Signature · 1 line
2.0.0
#4 lore raw
Quoted 7 lines · Adam Borowski
Quoted 6 lines · Adam Borowski
Quoted 5 lines · Harald van Dijk
Quoted 3 lines · Adam Borowski

I'm not sure what's your policy towards extensions, but \e as \033 is something ubiquitous in the Unix world. C compilers (gcc, clang, icc and tcc -- but not MSVC), perl, shells (bash and zsh -- but not dash), etc.

Quoted 7 lines
Quoted 1 line
Quoted 1 line

No comment on whether dash itself should accept \e, but [...]

So... can I has an answer whether dash should indeed accept \e ? Being told "no" or "go away, we hate you" is fine, I just dislike having patches rot forever. And not having this shorthand is annoying if you like using colour for highlights -- it works in perl and bash, then suddenly Oops! not in /bin/sh = dash.

I agree with Erik Blake, IMO there is no reason for dash to support \e unless POSIX standardizes it first. Except if you want to encourage unportable scripts, of course. /bin/sh is not necessarily bash or dash.

$ /bin/sh
$ type echo
echo is a shell builtin
$ echo '\033[1m hello \033[0m'
 hello # (bold)
$ /bin/echo '\033[1m hello \033[0m'
\033[1m hello \033[0m
$ echo '\e[1m hello \e[0m'
\e[1m hello \e[0m
$ /bin/echo  '\e[1m hello \e[0m'
\e[1m hello \e[0m
$
Signature · 1 line
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE
#5 lore raw
In reply to Harald van Dijk
Quoted 9 lines · Harald van Dijk
Quoted 8 lines · Adam Borowski

I'm not sure what's your policy towards extensions, but \e as \033 is something ubiquitous in the Unix world. C compilers (gcc, clang, icc and tcc -- but not MSVC), perl, shells (bash and zsh -- but not dash), etc.

What about supporting it in dash as well?

As a mostly passive lurker, I'm inclined to say, "No. Dash doesn't want to become bash."

http://windtoons.com/images/Camel_-Head-Under-Tent-large.gif

OTOH, there's a POSIX requirement that builtins be indistinguishable (except in performance) from the corresponding executables.

Quoted 5 lines

No comment on whether dash itself should accept \e, but you already found a compiler that doesn't support it at all, and many of the ones that do support it also (optionally) issue a warning for it. Should the C code perhaps be using \033 instead of \e?

And here I put on my EBCDIC hat (crown of thorns) and say, "Be careful using numeric code points; they're not portable."

-- gil

#6 lore raw
Quoted 8 lines · Paul Gilmartin
Quoted 5 lines · Harald van Dijk

No comment on whether dash itself should accept \e, but you already found a compiler that doesn't support it at all, and many of the ones that do support it also (optionally) issue a warning for it. Should the C code perhaps be using \033 instead of \e?

And here I put on my EBCDIC hat (crown of thorns) and say, "Be careful using numeric code points; they're not portable."

The way GCC does this is to hard-code the values for ASCII, and to hard-code the values for EBCDIC, both hidden behind preprocessor macros. Taking that approach in dash would look something like

#if defined(ASCII)
#define ESCAPE '\033'
#elif defined(EBCDIC)
#define ESCAPE '\047'
#else
#error Unknown character set
#endif

and then use ESCAPE.

ASCII and EBCDIC would be detected (or specified) by the configure script.

But dash does have an assumption of ASCII already, which is why I didn't think it would hurt to add one more. At the very least, it uses hard-coded \033s in src/hetio.c, so that part of the code already cannot be used on EBCDIC systems.

Cheers,
Harald van Dijk

#7 lore raw
In reply to Paul Gilmartin
Quoted 3 lines · Paul Gilmartin

OTOH, there's a POSIX requirement that builtins be indistinguishable (except in performance) from the corresponding executables.

The POSIX requirement only applies to portable uses of the builtin - ie. those that are prescribed by POSIX. Since POSIX does not require \e, dash is not failing compliance, even if it differs from extensions provided by corresponding executables. I do not think dash needs to bloat for \e unless POSIX standardizes it first.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org