delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/26/04:13:23

Date: Mon, 26 Jan 1998 11:13:10 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: Ragnar Kellari <kellari AT swipnet DOT se>
cc: djgpp AT delorie DOT com
Subject: Re: Redirecting cprintf
In-Reply-To: <34CC25D9.61F6@swipnet.se>
Message-ID: <Pine.SUN.3.91.980126111253.8951E-100000@is>
MIME-Version: 1.0

On Sun, 25 Jan 1998, Ragnar Kellari wrote:

> When I use printf the output goes to stdout and may then be redirected to 
> a file but the only way I can use colors with printf is by sending 
> escape-sequences to ansi.sys. Then when the output is redirected to a 
> file the escape-sequence goes there aswell.
> With cprintf, redirection is not possible as the output goes directly to 
> the screen.

What you need is to catch the output of your program before it goes to
DOS and redirect it to a conio call such as cprintf if stdout is a
terminal.

I think the best way to do that is by using the DJGPP Filesystem
Extension feature.  Basically, you call a special library function to
install your function as a handler for file I/O, and after that
low-level library functions will call that handler just before they
are about to call DOS to do the actual I/O.  Your handler then has an
option to either do the I/O (by writing to the screen directly using
conio) or return an indication that the library function should
proceed with calling DOS as usual.

A notable advantage of this technique is that even the messages
printed by library functions on which you have no control are caught
and colorized as you want them.

For an example of a program that uses this method, look in the source
distribution of the DJGPP port of GNU Fileutils (v2gnu/fil316s.zip),
file src/ls-msdos.c.  The ported `ls' program uses this to implement
colorization of different file types without requiring ANSI.SYS to be
installed.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019