Skip to content

Commit ac2c544

Browse files
committed
VT in Windows/Wine : follow-up to preceding commit b3f4eca, to work around a Wine bug.
In Wine, a carriage return/line feed are added every N characters, where N = screen width in columns. Escape sequences are included in the count. The bug is described at https://bugs.winehq.org/show_bug.cgi?id=58665 The count is reset if the program itself emits a carriage return and line feed. So when running in Wine, each cursor move is preceded by "move to upper left corner, emit CR/LF, and _then_ move to the desired location" (see PDC_gotoyx()). Also (again only in Wine) we insert a gratuitous PDC_gotoyx() if we've emitted enough characters in PDC_transform_line() that we might be close to the limit.
1 parent b3f4eca commit ac2c544

2 files changed

Lines changed: 30 additions & 13 deletions

File tree

vt/pdcdisp.c

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#ifdef _WIN32
1414
#include <io.h>
1515
#include <fcntl.h>
16+
17+
extern int PDC_wine_version;
1618
#else
1719
#include <unistd.h>
1820
#endif
@@ -55,21 +57,21 @@ int PDC_get_terminal_fd( void)
5557

5658
#define TBUFF_SIZE 512
5759

58-
static void put_to_stdout( const char *buff, size_t bytes_out)
60+
static size_t put_to_stdout( const char *buff, size_t bytes_out)
5961
{
6062
static char *tbuff = NULL;
6163
static size_t bytes_cached;
6264
int stdout_fd;
6365

6466
if( !buff && !tbuff)
65-
return;
67+
return( 0);
6668

6769
if( !buff && bytes_out == 1) /* release memory at shutdown */
6870
{
6971
free( tbuff);
7072
tbuff = NULL;
7173
bytes_cached = 0;
72-
return;
74+
return( 0);
7375
}
7476

7577
if( buff && !tbuff)
@@ -92,33 +94,40 @@ static void put_to_stdout( const char *buff, size_t bytes_out)
9294
while( bytes_cached)
9395
{
9496
#ifdef _WIN32
95-
const size_t bytes_written = _write( stdout_fd, tbuff,
97+
size_t bytes_written;
98+
99+
if( PDC_wine_version <= 0)
100+
bytes_written = _write( stdout_fd, tbuff,
96101
(unsigned int)bytes_cached);
102+
else
103+
bytes_written = fwrite( tbuff, 1,
104+
(unsigned int)bytes_cached, stdout);
97105
#else
98106
const size_t bytes_written = write( stdout_fd, tbuff, bytes_cached);
99107
#endif
100-
101108
bytes_cached -= bytes_written;
102109
if( bytes_cached)
103110
memmove( tbuff, tbuff + bytes_written, bytes_cached);
104111
}
105112
}
113+
return( bytes_cached);
106114
}
107115

108-
void PDC_puts_to_stdout( const char *buff)
116+
size_t PDC_puts_to_stdout( const char *buff)
109117
{
110-
put_to_stdout( buff, (buff ? strlen( buff) : 1));
118+
return( put_to_stdout( buff, (buff ? strlen( buff) : 1)));
111119
}
112120

113121
void PDC_gotoyx(int y, int x)
114122
{
115123
char tbuff[50];
116124

117-
#ifdef HAVE_SNPRINTF
118-
snprintf( tbuff, sizeof( tbuff), CSI "%d;%dH", y + 1, x + 1);
119-
#else
120-
sprintf( tbuff, CSI "%d;%dH", y + 1, x + 1);
125+
*tbuff = '\0';
126+
#ifdef _WIN32
127+
if( PDC_wine_version > 0)
128+
strcpy( tbuff, CSI "1;1H\r\n");
121129
#endif
130+
sprintf( tbuff + strlen( tbuff), CSI "%d;%dH", y + 1, x + 1);
122131
PDC_puts_to_stdout( tbuff);
123132
PDC_doupdate( );
124133
}
@@ -341,7 +350,12 @@ void PDC_transform_line(int lineno, int x, int len, const chtype *srcp)
341350
if( changes & (A_COLOR | A_STANDOUT | A_BLINK | A_REVERSE))
342351
reset_color( obuff + strlen( obuff), *srcp);
343352
if( *obuff)
353+
#ifdef _WIN32
354+
if( PDC_puts_to_stdout( obuff) > 50 && PDC_wine_version > 0)
355+
PDC_gotoyx( lineno, x);
356+
#else
344357
PDC_puts_to_stdout( obuff);
358+
#endif
345359
#ifdef USING_COMBINING_CHARACTER_SCHEME
346360
if( ch > (int)MAX_UNICODE) /* combining char sequence */
347361
{
@@ -378,7 +392,7 @@ void PDC_transform_line(int lineno, int x, int len, const chtype *srcp)
378392
#endif
379393
if( _is_altcharset( srcp[count]))
380394
ch = (int)acs_map[ch & 0x7f];
381-
#ifdef DOS
395+
#if !defined( PDC_WIDE)
382396
obuff[bytes_out++] = (char)ch;
383397
#else
384398
if( ch < (int)' ' || (ch >= 0x80 && ch <= 0x9f))
@@ -398,6 +412,9 @@ void PDC_transform_line(int lineno, int x, int len, const chtype *srcp)
398412
prev_ch = *srcp;
399413
srcp += count;
400414
len -= count;
415+
#ifdef _WIN32
416+
x += count;
417+
#endif
401418
}
402419
}
403420

vt/pdcvt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ extern int PDC_is_ansi;
2222
#define CSI "\x1b["
2323
#define OSC "\x1b]"
2424

25-
void PDC_puts_to_stdout( const char *buff); /* pdcdisp.c */
25+
size_t PDC_puts_to_stdout( const char *buff); /* pdcdisp.c */

0 commit comments

Comments
 (0)