Changing color of console text

Hi,
This seems like a silly question, but I have been unable to find
anything in the help files or in this newsgroup about it.
What I need to figure out is how can I change the color, or the type, of
text that I am printing out to a console? For example, I want that when
my program prints error messages I print them in red, so as to pop out
to the user something is wrong. Something like how when I run cc the
original text is grey, but the data passed to wpp is bold (or maybe just
white)
Can anyone point me in the right direction as to where to look for this
information?
Thanks in advance,

Chris Nasr
cnasr[at]mechtronix[dot]ca
Mechtronix Systems Inc.
Software Engineering.

Chris Nasr <cnasr@mechtronix.ca> wrote:

Hi,
This seems like a silly question, but I have been unable to find
anything in the help files or in this newsgroup about it.
What I need to figure out is how can I change the color, or the type, of
text that I am printing out to a console? For example, I want that when
my program prints error messages I print them in red, so as to pop out
to the user something is wrong. Something like how when I run cc the
original text is grey, but the data passed to wpp is bold (or maybe just
white)
Can anyone point me in the right direction as to where to look for this
information?

There are escape codes you can embed in the stream to change the
colour. Depending on whether you are in QNX or ANSI mode, they
will be different, though.

The documentation for Dev.con and Dev.ansi has fairly extensive notes
on all of the escape codes supported.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

David,
Thanks for the help. I have tried using the escape codes provided in the help
for Dev.con, both in QNX mode and ansi mode, but nothing appears to happen.
i.e. I do the following
printf("\x1b4017");
which should change the foreground colour to red and the background colour to
white, but everything else I print is grey and black. Is there something I am
missing, what am I doing wrong? Can this be done in a pterm, or do these codes
only work in a normal console?

David Gibbs wrote:

Chris Nasr <> cnasr@mechtronix.ca> > wrote:
Hi,
This seems like a silly question, but I have been unable to find
anything in the help files or in this newsgroup about it.
What I need to figure out is how can I change the color, or the type, of
text that I am printing out to a console? For example, I want that when
my program prints error messages I print them in red, so as to pop out
to the user something is wrong. Something like how when I run cc the
original text is grey, but the data passed to wpp is bold (or maybe just
white)
Can anyone point me in the right direction as to where to look for this
information?

There are escape codes you can embed in the stream to change the
colour. Depending on whether you are in QNX or ANSI mode, they
will be different, though.

The documentation for Dev.con and Dev.ansi has fairly extensive notes
on all of the escape codes supported.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

Chris Nasr <cnasr@mechtronix.ca> wrote:

David,
Thanks for the help. I have tried using the escape codes provided in the help
for Dev.con, both in QNX mode and ansi mode, but nothing appears to happen.
i.e. I do the following
printf("\x1b4017");

The following worked for me in QNX mode in a pterm:

#include <stdio.h>
int main()
{
printf("%c%c40", 0x1b, 0x40);
}

If you wish to do it all in \x escape sequences:

printf("\x1b\x40\x34\x30");

would do it.

Compiling your test program, then piping the output to hd can be
very helpful for seeing what output bytes are actually generated
by your printf string.

e.g. I called my program col.c and compiled it to col:

col | hd

gave:

0000000 1b 40 34 30 .@40
0000003

Which is what I wanted.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

David,
Thanks enourmously, that works, I think perhaps printf was misunderstanding my
string and formatting it wrong, doing each letter one at a time as in your second
example (or the first one) fixed everything.

David Gibbs wrote:

Chris Nasr <> cnasr@mechtronix.ca> > wrote:
David,
Thanks for the help. I have tried using the escape codes provided in the help
for Dev.con, both in QNX mode and ansi mode, but nothing appears to happen.
i.e. I do the following
printf("\x1b4017");

The following worked for me in QNX mode in a pterm:

#include <stdio.h
int main()
{
printf("%c%c40", 0x1b, 0x40);
}

If you wish to do it all in \x escape sequences:

printf("\x1b\x40\x34\x30");

would do it.

Compiling your test program, then piping the output to hd can be
very helpful for seeing what output bytes are actually generated
by your printf string.

e.g. I called my program col.c and compiled it to col:

col | hd

gave:

0000000 1b 40 34 30 .@40
0000003

Which is what I wanted.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

Chris Nasr <cnasr@mechtronix.ca> wrote:

David,
Thanks enourmously, that works, I think perhaps printf was misunderstanding my
string and formatting it wrong, doing each letter one at a time as in your second
example (or the first one) fixed everything.

The \x escapes are handled by the C compiler, not by printf – printf
never sees them.

THe problem with \x is it is that it is a multi-character escape,
and you gave: \x1b4017 – which digit is the last of the hex
digits? K&R says, “There is no limit to the number of digits,
but the behaviour is undefined if the resulting character value
exceeds that of the largest character.”

Undefined land is not a good place to go.

-David

David Gibbs wrote:

Chris Nasr <> cnasr@mechtronix.ca> > wrote:
David,
Thanks for the help. I have tried using the escape codes provided in the help
for Dev.con, both in QNX mode and ansi mode, but nothing appears to happen.
i.e. I do the following
printf("\x1b4017");

The following worked for me in QNX mode in a pterm:

#include <stdio.h
int main()
{
printf("%c%c40", 0x1b, 0x40);
}

If you wish to do it all in \x escape sequences:

printf("\x1b\x40\x34\x30");

would do it.

Compiling your test program, then piping the output to hd can be
very helpful for seeing what output bytes are actually generated
by your printf string.

e.g. I called my program col.c and compiled it to col:

col | hd

gave:

0000000 1b 40 34 30 .@40
0000003

Which is what I wanted.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.


QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

David Gibbs <dagibbs@qnx.com> wrote:

Chris Nasr <> cnasr@mechtronix.ca> > wrote:
David,
Thanks enourmously, that works, I think perhaps printf was misunderstanding my
string and formatting it wrong, doing each letter one at a time as in your second
example (or the first one) fixed everything.

The \x escapes are handled by the C compiler, not by printf – printf
never sees them.

THe problem with \x is it is that it is a multi-character escape,
and you gave: \x1b4017 – which digit is the last of the hex
digits? K&R says, “There is no limit to the number of digits,
but the behaviour is undefined if the resulting character value
exceeds that of the largest character.”

Undefined land is not a good place to go.

Your kidding right? Undefined land is a great place! Everything there
speaks gibberish, so why try to make sense of it all? Just sit
back and let those silly little characters wave their hands in the air
and yell their funny little language, while you just laugh.

Sorry, the blinking lights in my cube finally got to me.

E.


-David

David Gibbs wrote:

Chris Nasr <> cnasr@mechtronix.ca> > wrote:
David,
Thanks for the help. I have tried using the escape codes provided in the help
for Dev.con, both in QNX mode and ansi mode, but nothing appears to happen.
i.e. I do the following
printf("\x1b4017");

The following worked for me in QNX mode in a pterm:

#include <stdio.h
int main()
{
printf("%c%c40", 0x1b, 0x40);
}

If you wish to do it all in \x escape sequences:

printf("\x1b\x40\x34\x30");

would do it.

Compiling your test program, then piping the output to hd can be
very helpful for seeing what output bytes are actually generated
by your printf string.

e.g. I called my program col.c and compiled it to col:

col | hd

gave:

0000000 1b 40 34 30 .@40
0000003

Which is what I wanted.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.


QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

Chris Nasr <cnasr@mechtronix.ca> wrote:

Thanks enourmously, that works, I think perhaps printf was misunderstanding my
string and formatting it wrong, doing each letter one at a time as in your second
example (or the first one) fixed everything.

It wasn’t printf, it was the compiler. C doesn’t parse “\x1b4017” as a
‘\x1b’ followed by a ‘4’, a ‘0’, a ‘1’, and a ‘7’. It is parsed as a
single character whose value is (char)0x1b4017. On most systems with
8-bit chars, it ends up being exactly the same as “\x17”.


Wojtek Lerch QNX Software Systems Ltd.

David Gibbs <dagibbs@qnx.com> wrote:

Chris Nasr <> cnasr@mechtronix.ca> > wrote:
David,
Thanks enourmously, that works, I think perhaps printf was misunderstanding my
string and formatting it wrong, doing each letter one at a time as in your second
example (or the first one) fixed everything.

The \x escapes are handled by the C compiler, not by printf – printf
never sees them.

THe problem with \x is it is that it is a multi-character escape,
and you gave: \x1b4017 – which digit is the last of the hex
digits? K&R says, “There is no limit to the number of digits,
but the behaviour is undefined if the resulting character value
exceeds that of the largest character.”

Undefined land is not a good place to go.

In C99, I think it’s implementation-defined, but it also is a constraint
violation that requires a diagnostic. In other words, it’s not
undefined behaviour, but the compiler is required to produce a warning
when an octal or hex escape sequence specifies a value greater than
UCHAR_MAX, and its documentation is required to explain what value is
produced.


Wojtek Lerch QNX Software Systems Ltd.

Wojtek Lerch <wojtek_l@yahoo.ca> wrote:

David Gibbs <> dagibbs@qnx.com> > wrote:
Chris Nasr <> cnasr@mechtronix.ca> > wrote:
David,
Thanks enourmously, that works, I think perhaps printf was misunderstanding my
string and formatting it wrong, doing each letter one at a time as in your second
example (or the first one) fixed everything.

The \x escapes are handled by the C compiler, not by printf – printf
never sees them.

THe problem with \x is it is that it is a multi-character escape,
and you gave: \x1b4017 – which digit is the last of the hex
digits? K&R says, “There is no limit to the number of digits,
but the behaviour is undefined if the resulting character value
exceeds that of the largest character.”

Undefined land is not a good place to go.

In C99, I think it’s implementation-defined, but it also is a constraint
violation that requires a diagnostic. In other words, it’s not
undefined behaviour, but the compiler is required to produce a warning
when an octal or hex escape sequence specifies a value greater than
UCHAR_MAX, and its documentation is required to explain what value is
produced.

Watcom doesn’t produce a warning, gcc (for QNX 6) does produce a
warning.

Hm… Watcom probably pre-dates C99, or at least, the widespread
acceptance of C99.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

David Gibbs <dagibbs@qnx.com> wrote:

Hm… Watcom probably pre-dates C99, or at least, the widespread
acceptance of C99.

I would say that anything that exists today pre-dates the widespread
acceptance of C99. Do you know of any compiler that claims conformance
to C99? :slight_smile:

\

Wojtek Lerch QNX Software Systems Ltd.

Wojtek Lerch <wojtek_l@yahoo.ca> wrote:

David Gibbs <> dagibbs@qnx.com> > wrote:
Hm… Watcom probably pre-dates C99, or at least, the widespread
acceptance of C99.

I would say that anything that exists today pre-dates the widespread
acceptance of C99. Do you know of any compiler that claims conformance
to C99? > :slight_smile:

No, but I don’t really track compilers, I’m generally happy if my
compiler does what my K&R (2nd edition) says it should. :slight_smile:

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

David,
certainly not :slight_smile: , thanks again for the help.

Undefined land is not a good place to go.