Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Are ANSI control sequences reliable constructs for terminals?

1 view
Skip to first unread message

Mark Hobley

unread,
Feb 20, 2009, 5:08:06 PM2/20/09
to
I notice as I am looking through code for various applications, that
ANSI control sequences are being used to control the terminal, rather
than using the tput interface.

I'm just wondering if this fairly reliable across most terminal types.
Do all the common real and virtual terminal types support ansi control
sequences?

TERM=linux,TERM=ansi,TERM=color,TERM=vt100,TERM=vt220,TERM=vt,TERM=rxvt,
TERM=xterm etc?

What about TERM=3270?

What terminals do not support ANSI sequences?

What does a monochrome terminal do if it receives a colour ansi sequence?

Mark.

--
Mark Hobley
Linux User: #370818 http://markhobley.yi.org/

David W. Hodgins

unread,
Feb 20, 2009, 5:58:10 PM2/20/09
to
On Fri, 20 Feb 2009 17:08:06 -0500, Mark Hobley <markh...@hotpop.donottypethisbit.com> wrote:

> Do all the common real and virtual terminal types support ansi control
> sequences?

Rather then using ANSI control sequences defined in the script/program, find
the correct ones using tput.

This should work, as long as the terminal is properly defined in the terminfo
database. See "man tput" and "man terminfo" The following script may help.

#!/bin/bash
for ((ForegroundColorCurrent=0; ForegroundColorCurrent < 10; ForegroundColorCurrent++)); do
for ((BackgroundColorCurrent=0; BackgroundColorCurrent < 10; BackgroundColorCurrent++)); do
tput setaf $ForegroundColorCurrent
tput setab $BackgroundColorCurrent
echo ForegroundColorCurrent="$ForegroundColorCurrent", "BackgroundColorCurrent=$BackgroundColorCurrent"
done
done

See the "Colors using tput" section of
http://bash-hackers.org/wiki/doku.php/scripting/terminalcodes

Regards, Dave Hodgins

--
Change nomail.afraid.org to ody.ca to reply by email.
(nomail.afraid.org has been set up specifically for
use in usenet. Feel free to use it yourself.)

Michael Paoli

unread,
Feb 22, 2009, 1:22:59 AM2/22/09
to
On Feb 20, 2:08 pm, markh...@hotpop.donottypethisbit.com (Mark

Hobley) wrote:
> I notice as I am looking through code for various applications, that
> ANSI control sequences are being used to control the terminal, rather
> than using the tput interface.
>
> I'm just wondering if this fairly reliable across most terminal types.
> Do all the common real and virtual terminal types support ansi control
> sequences?
>
> TERM=linux,TERM=ansi,TERM=color,TERM=vt100,TERM=vt220,TERM=vt,TERM=rxvt,
> TERM=xterm etc?
>
> What about TERM=3270?
>
> What terminals do not support ANSI sequences?
>
> What does a monochrome terminal do if it receives a colour ansi sequence?

That's absolutely the wrong way to do it at least in the land of
UNIX(/Linux/BSD/etc.), and reinvention of a problem that was well
solved
over 3 decades ago. I can also guarantee that that approach will
generally fail rather miserably on my on my Cromemco 3102* terminal
and
my Qume QVT-102 terminal - at least in most of its modes (so yes, it
would generally fail on every actual physical terminal that I own).
So, in general, it won't work, and won't work for a large number of
terminals/emulations.

The correct way to do it is to use terminfo (or termcap in some cases,
e.g. old systems with termcap, but not terminfo). From shell, that
would typically be making use of tput.

On the system at my fingertips, I find approximately 2498 distinct
supported terminal types and some of their various main/distinct modes
and aliases defined, and among them, I find 137 distinct primary/best
means to clear their screen (at least for those that support such a
capability ... and even have a screen for that matter (generally not
applicable for hardcopy terminal)).

Some examples**:

$ tput clear || {
> 1>&2 echo "Don't know how to clear screen for TERM=$TERM"
> fail
> }

$ if tput setaf 1 || tput setf 4; then
> echo RED
> tput op || { 2>&1 echo 'tput op failed - stuck in RED?'; false; }
> else
> 2>&1 echo "Can't get red via tput setaf or tput setf"
> false
> fi

$ TERM=ansi-m
$ tput setaf 1 || tput setf 4 || {
> 1>&2 echo "Can't do that for TERM=$TERM"
> false
> }
Can't do that for TERM=ansi-m
$

*and yes, one can crash a Cromemco 3102 terminal by sending it
inappropriate control/escape sequences (such as the one that
essentially
says interpret the following as hex data, load and execute it).

**for illustrative purposes only - no guarantees to be sufficiently
robust/complete for production purposes

Mark Hobley

unread,
Feb 22, 2009, 7:08:02 AM2/22/09
to
In comp.unix.programmer Michael Paoli <micha...@yahoo.com> wrote:

> That's absolutely the wrong way to do it at least in the land of
> UNIX(/Linux/BSD/etc.), and reinvention of a problem that was well
> solved over 3 decades ago.

Right, OK. I have encountered these on certain applications, so I will rip
these out and try to recode them.

Cheers,

Chris F.A. Johnson

unread,
Feb 22, 2009, 10:07:24 AM2/22/09
to
On 2009-02-20, David W. Hodgins wrote:
> On Fri, 20 Feb 2009 17:08:06 -0500, Mark Hobley <markh...@hotpop.donottypethisbit.com> wrote:
>
>> Do all the common real and virtual terminal types support ansi control
>> sequences?
>
> Rather then using ANSI control sequences defined in the script/program, find
> the correct ones using tput.

Unfortunately, tput is not consistent from one system to another.

The support of ANSI sequences is all but universal these days, so
you are likely to be more portable using ANSI sequences than
tput.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence

Spiros Bousbouras

unread,
Feb 24, 2009, 3:27:24 PM2/24/09
to
On 20 Feb, 22:08, markhob...@hotpop.donottypethisbit.com (Mark Hobley)
wrote:

> I notice as I am looking through code for various applications, that
> ANSI control sequences are being used to control the terminal, rather
> than using the tput interface.
>
> I'm just wondering if this fairly reliable across most terminal types.
> Do all the common real and virtual terminal types support ansi control
> sequences?
>
> TERM=linux,TERM=ansi,TERM=color,TERM=vt100,TERM=vt220,TERM=vt,TERM=rxvt,
> TERM=xterm etc?
>
> What about TERM=3270?
>
> What terminals do not support ANSI sequences?
>
> What does a monochrome terminal do if it receives a colour ansi sequence?

I don't have an answer to your questions but there is a
comp.terminals newsgroup and it sounds as if your
questions have a better chance of getting answered there.

0 new messages