removing color codes before printing to the terminal

fixed Download_CleanUp calling closesocket on socket 0, messing with stdin
This commit is contained in:
myT 2017-10-10 19:54:57 +02:00
parent ac48bc3248
commit 484e9e7e40
3 changed files with 48 additions and 21 deletions

View file

@ -143,6 +143,8 @@ add: automatic dedicated server process restarts for crashes and timed reboots (
this means 2 CNQ3 processes run per server: a parent (fixed pid) and a child (new pid after each restart)
this behavior can be disabled by passing "nohardreboot" as a command-line argument
fix: color codes (e.g. "^1") don't get printed to the terminal anymore
fix: tty handling of the leading "]" character
fix: dedicated servers no longer insert a leading backslash when running auto-completion

View file

@ -61,6 +61,7 @@ struct mapDownload_t {
qbool lastErrorTimeOut; // qtrue if the last recv error was a timeout
int sourceIndex; // index into the cl_mapDLSources array
qbool exactMatch; // qtrue if an exact match is required
qbool cleared; // qtrue if Download_Clear was called at least once
};
@ -208,6 +209,7 @@ static void Download_Clear( mapDownload_t* dl )
dl->lastErrorTimeOut = qfalse;
dl->sourceIndex = 0;
dl->exactMatch = qfalse;
dl->cleared = qtrue;
}
@ -315,6 +317,9 @@ static qbool Download_Rename( mapDownload_t* dl )
static qbool Download_CleanUp( mapDownload_t* dl, qbool rename )
{
if (!cl_mapDL.cleared)
return qfalse;
if (dl->socket != INVALID_SOCKET) {
Q_closesocket(dl->socket);
dl->socket = INVALID_SOCKET;
@ -784,7 +789,7 @@ void CL_MapDownload_Init()
qbool CL_MapDownload_Active()
{
return cl_mapDL.socket != INVALID_SOCKET;
return cl_mapDL.cleared && cl_mapDL.socket != INVALID_SOCKET;
}

View file

@ -77,7 +77,6 @@ static struct termios tty_tc;
static field_t tty_con;
static cvar_t *ttycon_ansicolor = NULL;
static qboolean ttycon_color_on = qfalse;
static history_t tty_history;
@ -183,14 +182,14 @@ static void Sys_ConsoleInputInit()
{
if (isatty(STDIN_FILENO)!=1)
{
Com_Printf("stdin is not a tty, tty console mode failed\n");
Com_Printf("stdin is not a tty, tty console mode failed: %s\n", strerror(errno));
Cvar_Set("ttycon", "0");
ttycon_on = qfalse;
return;
}
Com_Printf("Started tty console (use +set ttycon 0 to disable)\n");
Field_Clear(&tty_con);
tcgetattr (0, &tty_tc);
tcgetattr (STDIN_FILENO, &tty_tc);
tty_erase = tty_tc.c_cc[VERASE];
tty_eof = tty_tc.c_cc[VEOF];
tc = tty_tc;
@ -210,19 +209,15 @@ static void Sys_ConsoleInputInit()
tc.c_iflag &= ~(ISTRIP | INPCK);
tc.c_cc[VMIN] = 1;
tc.c_cc[VTIME] = 0;
tcsetattr (0, TCSADRAIN, &tc);
tcsetattr (STDIN_FILENO, TCSADRAIN, &tc);
ttycon_on = qtrue;
ttycon_ansicolor = Cvar_Get( "ttycon_ansicolor", "0", CVAR_ARCHIVE );
if( ttycon_ansicolor && ttycon_ansicolor->value )
{
ttycon_color_on = qtrue;
}
} else
ttycon_on = qfalse;
// make stdin non-blocking
fcntl( 0, F_SETFL, fcntl(0, F_GETFL, 0) | FNDELAY );
fcntl( STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL, 0) | FNDELAY );
}
@ -236,7 +231,7 @@ const char* Sys_ConsoleInput()
if (ttycon && ttycon->value)
{
avail = read(0, &key, 1);
avail = read(STDIN_FILENO, &key, 1);
if (avail != -1)
{
// we have something
@ -449,15 +444,15 @@ const char* Sys_ConsoleInput()
return NULL;
FD_ZERO(&fdset);
FD_SET(0, &fdset); // stdin
FD_SET(STDIN_FILENO, &fdset);
timeout.tv_sec = 0;
timeout.tv_usec = 0;
if (select (1, &fdset, NULL, NULL, &timeout) == -1 || !FD_ISSET(0, &fdset))
if (select (1, &fdset, NULL, NULL, &timeout) == -1 || !FD_ISSET(STDIN_FILENO, &fdset))
{
return NULL;
}
len = read (0, text, sizeof(text));
len = read (STDIN_FILENO, text, sizeof(text));
if (len == 0)
{ // eof!
stdin_active = qfalse;
@ -479,12 +474,12 @@ void Sys_ConsoleInputShutdown()
if (ttycon_on)
{
tty_Back(); // delete the leading "]"
tcsetattr (0, TCSADRAIN, &tty_tc);
tcsetattr (STDIN_FILENO, TCSADRAIN, &tty_tc);
ttycon_on = qfalse;
}
// make stdin blocking
fcntl( 0, F_SETFL, fcntl(0, F_GETFL, 0) & ~FNDELAY );
fcntl( STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL, 0) & (~FNDELAY) );
}
@ -737,6 +732,7 @@ static struct Q3ToAnsiColorTable_s
static const int tty_colorTableSize = sizeof( tty_colorTable ) / sizeof( tty_colorTable[0] );
static void Sys_ANSIColorify( const char *msg, char *buffer, int bufferSize )
{
int msgLength, pos;
@ -793,21 +789,45 @@ static void Sys_ANSIColorify( const char *msg, char *buffer, int bufferSize )
}
}
static char* CleanTermStr( char* string )
{
char* s = string;
char* d = string;
char c;
while ((c = *s) != 0 ) {
if ( Q_IsColorString( s ) )
s++;
else
*d++ = c;
s++;
}
*d = '\0';
return string;
}
void Sys_Print( const char *msg )
{
static char finalMsg[ MAXPRINTMSG ];
if (ttycon_on)
{
tty_Hide();
}
if( ttycon_on && ttycon_color_on )
if( ttycon_on && ttycon_ansicolor && ttycon_ansicolor->integer )
{
char ansiColorString[ MAXPRINTMSG ];
Sys_ANSIColorify( msg, ansiColorString, MAXPRINTMSG );
fputs( ansiColorString, stderr );
Sys_ANSIColorify( msg, finalMsg, sizeof(finalMsg) );
}
else
fputs(msg, stderr);
{
Q_strncpyz( finalMsg, msg, sizeof(finalMsg) );
CleanTermStr( finalMsg );
}
fputs( finalMsg, stdout );
if (ttycon_on)
{