Skip to content

Commit

Permalink
VT : made the pointer to the terminal (usually stdout; stderr if stdo…
Browse files Browse the repository at this point in the history
…ut has been redirected) accessible via the new PDC_get_terminal_fd( ) function. This needs to be accessible so that the resizing handler is set to that file (see next commit).
  • Loading branch information
Bill-Gray committed Jan 7, 2023
1 parent 1917dcd commit a6618d9
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions vt/pdcdisp.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,30 @@
#include "../common/acs_defs.h"
#include "../common/pdccolor.h"

int PDC_get_terminal_fd( void)
{
static int stdout_fd = -1;

if( stdout_fd == -1)
{
#ifdef _WIN32
// if( FILE_TYPE_CHAR == GetFileType( GetStdHandle( STD_OUTPUT_HANDLE)))
stdout_fd = 2;
#else
if( isatty( STDOUT_FILENO))
stdout_fd = STDOUT_FILENO; /* stdout hasn't been redirected; use it */
else if( isatty( STDERR_FILENO))
stdout_fd = STDERR_FILENO; /* stdout is redirected to a file; use stderr */
else
{
fprintf(stderr, "No output device found\n");
exit( -1);
}
#endif
}
return( stdout_fd);
}

/* Rarely, writes to stdout fail if a signal handler is
called. In which case we just try to write out the
remainder of the buffer until success happens. */
Expand All @@ -29,7 +53,7 @@ static void put_to_stdout( const char *buff, size_t bytes_out)
{
static char *tbuff = NULL;
static size_t bytes_cached;
static int stdout_fd = -1;
int stdout_fd;

if( !buff && !tbuff)
return;
Expand All @@ -42,26 +66,9 @@ static void put_to_stdout( const char *buff, size_t bytes_out)
return;
}

if( stdout_fd == -1)
{
#ifdef _WIN32
// if( FILE_TYPE_CHAR == GetFileType( GetStdHandle( STD_OUTPUT_HANDLE)))
stdout_fd = 2;
#else
if( isatty( STDOUT_FILENO))
stdout_fd = STDOUT_FILENO; /* stdout hasn't been redirected; use it */
else if( isatty( STDERR_FILENO))
stdout_fd = STDERR_FILENO; /* stdout is redirected to a file; use stderr */
else
{
fprintf(stderr, "No output device found\n");
exit( -1);
}
#endif
}

if( buff && !tbuff)
tbuff = (char *)malloc( TBUFF_SIZE);
stdout_fd = PDC_get_terminal_fd( );
while( bytes_out || (!buff && bytes_cached))
{
if( buff)
Expand Down

0 comments on commit a6618d9

Please sign in to comment.