From c6a9027b7cf0d35401cae346b67ca95c5f55b4a1 Mon Sep 17 00:00:00 2001
From: Tim Angus <tim@ngus.net>
Date: Sat, 3 Nov 2007 00:06:23 +0000
Subject: * Merge ioq3-r1204

---
 Makefile                 |  91 ++++++++++++++++++++++++++++---
 src/client/cl_keys.c     |  12 ++++
 src/qcommon/cm_local.h   |   2 +
 src/qcommon/cm_patch.c   |   2 +-
 src/qcommon/cm_test.c    |  41 +++++++++++++-
 src/qcommon/cm_trace.c   |   2 +-
 src/qcommon/common.c     | 128 ++++++++++++++++++++++++++++++-------------
 src/qcommon/cvar.c       |  14 ++---
 src/qcommon/q_platform.h |  29 ++++++++++
 src/qcommon/qcommon.h    |   3 +
 src/renderer/qgl.h       |   6 +-
 src/sdl/sdl_gamma.c      |   7 ++-
 src/sdl/sdl_glimp.c      |  12 +++-
 src/sdl/sdl_input.c      |   6 +-
 src/sdl/sdl_snd.c        |   6 +-
 src/sys/sys_main.c       | 139 ++++++++++++++++++++---------------------------
 src/sys/win_resource.rc  |   2 +
 src/tools/asm/cmdlib.c   | 101 ++++------------------------------
 src/tools/asm/cmdlib.h   |   8 ---
 src/tools/asm/q3asm.c    |  12 ++++
 src/ui/ui_main.c         |   2 +-
 21 files changed, 382 insertions(+), 243 deletions(-)

diff --git a/Makefile b/Makefile
index 5496a2be..62810a56 100644
--- a/Makefile
+++ b/Makefile
@@ -38,12 +38,12 @@ PLATFORM=$(COMPILE_PLATFORM)
 endif
 export PLATFORM
 
-ifndef ARCH
-ARCH=$(COMPILE_ARCH)
+ifeq ($(COMPILE_ARCH),powerpc)
+  COMPILE_ARCH=ppc
 endif
 
-ifeq ($(ARCH),powerpc)
-  ARCH=ppc
+ifndef ARCH
+ARCH=$(COMPILE_ARCH)
 endif
 export ARCH
 
@@ -253,6 +253,11 @@ ifeq ($(PLATFORM),linux)
     # linux32 make ...
     BASE_CFLAGS += -m32
     LDFLAGS+=-m32
+  else
+  ifeq ($(ARCH),ppc64)
+    BASE_CFLAGS += -m64
+    LDFLAGS += -m64
+  endif
   endif
 
   DEBUG_CFLAGS = $(BASE_CFLAGS) -g -O0
@@ -356,7 +361,12 @@ ifeq ($(PLATFORM),darwin)
     CLIENT_LDFLAGS += -lvorbisfile -lvorbis -logg
   endif
 
-  BASE_CFLAGS += -D_THREAD_SAFE=1 -I$(SDLHDIR)/include
+  BASE_CFLAGS += -D_THREAD_SAFE=1
+
+  ifeq ($(USE_LOCAL_HEADERS),1)
+    BASE_CFLAGS += -I$(SDLHDIR)/include
+  endif
+
   # We copy sdlmain before ranlib'ing it so that subversion doesn't think
   #  the file has been modified by each build.
   LIBSDLMAIN=$(B)/libSDLmain.a
@@ -398,7 +408,11 @@ endif
   ARCH=x86
 
   BASE_CFLAGS = -Wall -fno-strict-aliasing -Wimplicit -Wstrict-prototypes \
-    -DUSE_ICON -I$(SDLHDIR)/include
+    -DUSE_ICON
+
+  ifeq ($(USE_LOCAL_HEADERS),1)
+    BASE_CFLAGS += -I$(SDLHDIR)/include
+  endif
 
   ifeq ($(USE_OPENAL),1)
     BASE_CFLAGS += -DUSE_OPENAL=1 -DUSE_OPENAL_DLOPEN
@@ -528,6 +542,63 @@ ifeq ($(PLATFORM),freebsd)
 
 else # ifeq freebsd
 
+#############################################################################
+# SETUP AND BUILD -- OPENBSD
+#############################################################################
+
+ifeq ($(PLATFORM),openbsd)
+
+  #default to i386, no tests done on anything else
+  ARCH=i386
+
+
+  BASE_CFLAGS = -Wall -fno-strict-aliasing -Wimplicit -Wstrict-prototypes \
+    -DUSE_ICON $(shell sdl-config --cflags)
+
+  ifeq ($(USE_OPENAL),1)
+    BASE_CFLAGS += -DUSE_OPENAL
+    ifeq ($(USE_OPENAL_DLOPEN),1)
+      BASE_CFLAGS += -DUSE_OPENAL_DLOPEN
+    endif
+  endif
+
+  ifeq ($(USE_CODEC_VORBIS),1)
+    BASE_CFLAGS += -DUSE_CODEC_VORBIS
+  endif
+
+  BASE_CFLAGS += -DNO_VM_COMPILED -I/usr/X11R6/include -I/usr/local/include
+  RELEASE_CFLAGS=$(BASE_CFLAGS) -DNDEBUG -O3 \
+    -march=pentium -fomit-frame-pointer -pipe -ffast-math \
+    -falign-loops=2 -falign-jumps=2 -falign-functions=2 \
+    -funroll-loops -fstrength-reduce
+  HAVE_VM_COMPILED=false
+
+  DEBUG_CFLAGS=$(BASE_CFLAGS) -g
+
+  SHLIBEXT=so
+  SHLIBCFLAGS=-fPIC
+  SHLIBLDFLAGS=-shared $(LDFLAGS)
+
+  THREAD_LDFLAGS=-lpthread
+  LDFLAGS=-lm
+
+  CLIENT_LDFLAGS =
+
+  CLIENT_LDFLAGS += $(shell sdl-config --libs) -lGL
+
+  ifeq ($(USE_OPENAL),1)
+    ifneq ($(USE_OPENAL_DLOPEN),1)
+      CLIENT_LDFLAGS += $(THREAD_LDFLAGS) -lopenal
+    endif
+  endif
+
+  ifeq ($(USE_CODEC_VORBIS),1)
+    CLIENT_LDFLAGS += -lvorbisfile -lvorbis -logg
+  endif
+
+
+else # ifeq openbsd
+
 #############################################################################
 # SETUP AND BUILD -- NETBSD
 #############################################################################
@@ -661,6 +732,7 @@ endif #Linux
 endif #darwin
 endif #mingw32
 endif #FreeBSD
+endif #OpenBSD
 endif #NetBSD
 endif #IRIX
 endif #SunOS
@@ -830,12 +902,17 @@ makedirs:
 # QVM BUILD TOOLS
 #############################################################################
 
-TOOLS_CFLAGS = -O2 -Wall -fno-strict-aliasing -MMD \
+TOOLS_OPTIMIZE = -g -O2 -Wall -fno-strict-aliasing
+TOOLS_CFLAGS = $(TOOLS_OPTIMIZE) \
                -DTEMPDIR=\"$(TEMPDIR)\" -DSYSTEM=\"\" \
                -I$(Q3LCCSRCDIR) \
                -I$(LBURGDIR)
 TOOLS_LDFLAGS =
 
+ifeq ($(GENERATE_DEPENDENCIES),1)
+	TOOLS_CFLAGS += -MMD
+endif
+
 define DO_TOOLS_CC
 $(echo_cmd) "TOOLS_CC $<"
 $(Q)$(CC) $(TOOLS_CFLAGS) -o $@ -c $<
diff --git a/src/client/cl_keys.c b/src/client/cl_keys.c
index 66597427..d9fc9946 100644
--- a/src/client/cl_keys.c
+++ b/src/client/cl_keys.c
@@ -1014,6 +1014,18 @@ void Key_Bindlist_f( void ) {
 	}
 }
 
+/*
+============
+Key_KeynameCompletion
+============
+*/
+void Key_KeynameCompletion( void(*callback)(const char *s) ) {
+	int		i;
+
+	for( i = 0; keynames[ i ].name != NULL; i++ )
+		callback( keynames[ i ].name );
+}
+
 /*
 ===================
 CL_InitKeyCommands
diff --git a/src/qcommon/cm_local.h b/src/qcommon/cm_local.h
index cc69482b..4b30052d 100644
--- a/src/qcommon/cm_local.h
+++ b/src/qcommon/cm_local.h
@@ -205,6 +205,8 @@ void CM_StoreBrushes( leafList_t *ll, int nodenum );
 void CM_BoxLeafnums_r( leafList_t *ll, int nodenum );
 
 cmodel_t	*CM_ClipHandleToModel( clipHandle_t handle );
+qboolean CM_BoundsIntersect( const vec3_t mins, const vec3_t maxs, const vec3_t mins2, const vec3_t maxs2 );
+qboolean CM_BoundsIntersectPoint( const vec3_t mins, const vec3_t maxs, const vec3_t point );
 
 // cm_patch.c
 
diff --git a/src/qcommon/cm_patch.c b/src/qcommon/cm_patch.c
index 38b7d5cc..98724644 100644
--- a/src/qcommon/cm_patch.c
+++ b/src/qcommon/cm_patch.c
@@ -1387,7 +1387,7 @@ void CM_TraceThroughPatchCollide( traceWork_t *tw, const struct patchCollide_s *
 	static cvar_t *cv;
 #endif //BSPC
 
-	if ( !BoundsIntersect( tw->bounds[0], tw->bounds[1],
+	if ( !CM_BoundsIntersect( tw->bounds[0], tw->bounds[1],
 				pc->bounds[0], pc->bounds[1] ) ) {
 		return;
 	}
diff --git a/src/qcommon/cm_test.c b/src/qcommon/cm_test.c
index 485facc2..420a91f2 100644
--- a/src/qcommon/cm_test.c
+++ b/src/qcommon/cm_test.c
@@ -251,7 +251,7 @@ int CM_PointContents( const vec3_t p, clipHandle_t model ) {
 		brushnum = cm.leafbrushes[leaf->firstLeafBrush+k];
 		b = &cm.brushes[brushnum];
 
-		if ( !BoundsIntersectPoint( b->bounds[0], b->bounds[1], p ) ) {
+		if ( !CM_BoundsIntersectPoint( b->bounds[0], b->bounds[1], p ) ) {
 			continue;
 		}
 
@@ -481,3 +481,42 @@ int CM_WriteAreaBits (byte *buffer, int area)
 	return bytes;
 }
 
+/*
+====================
+CM_BoundsIntersect
+====================
+*/
+qboolean CM_BoundsIntersect( const vec3_t mins, const vec3_t maxs, const vec3_t mins2, const vec3_t maxs2 )
+{
+	if (maxs[0] < mins2[0] - SURFACE_CLIP_EPSILON ||
+		maxs[1] < mins2[1] - SURFACE_CLIP_EPSILON ||
+		maxs[2] < mins2[2] - SURFACE_CLIP_EPSILON ||
+		mins[0] > maxs2[0] + SURFACE_CLIP_EPSILON ||
+		mins[1] > maxs2[1] + SURFACE_CLIP_EPSILON ||
+		mins[2] > maxs2[2] + SURFACE_CLIP_EPSILON)
+	{
+		return qfalse;
+	}
+
+	return qtrue;
+}
+
+/*
+====================
+CM_BoundsIntersectPoint
+====================
+*/
+qboolean CM_BoundsIntersectPoint( const vec3_t mins, const vec3_t maxs, const vec3_t point )
+{
+	if (maxs[0] < point[0] - SURFACE_CLIP_EPSILON ||
+		maxs[1] < point[1] - SURFACE_CLIP_EPSILON ||
+		maxs[2] < point[2] - SURFACE_CLIP_EPSILON ||
+		mins[0] > point[0] + SURFACE_CLIP_EPSILON ||
+		mins[1] > point[1] + SURFACE_CLIP_EPSILON ||
+		mins[2] > point[2] + SURFACE_CLIP_EPSILON)
+	{
+		return qfalse;
+	}
+
+	return qtrue;
+}
diff --git a/src/qcommon/cm_trace.c b/src/qcommon/cm_trace.c
index bd8d4bd5..3be22d43 100644
--- a/src/qcommon/cm_trace.c
+++ b/src/qcommon/cm_trace.c
@@ -847,7 +847,7 @@ void CM_TraceThroughLeaf( traceWork_t *tw, cLeaf_t *leaf ) {
 
 		b->collided = qfalse;
 
-		if ( !BoundsIntersect( tw->bounds[0], tw->bounds[1],
+		if ( !CM_BoundsIntersect( tw->bounds[0], tw->bounds[1],
 					b->bounds[0], b->bounds[1] ) ) {
 			continue;
 		}
diff --git a/src/qcommon/common.c b/src/qcommon/common.c
index 083a7d18..ff742803 100644
--- a/src/qcommon/common.c
+++ b/src/qcommon/common.c
@@ -2947,6 +2947,40 @@ static char *Field_FindFirstSeparator( char *s )
 	return NULL;
 }
 
+#ifndef DEDICATED
+/*
+===============
+Field_CompleteKeyname
+===============
+*/
+static void Field_CompleteKeyname( void )
+{
+	matchCount = 0;
+	shortestMatch[ 0 ] = 0;
+
+	Key_KeynameCompletion( FindMatches );
+
+	if( matchCount == 0 )
+		return;
+
+	Q_strncpyz( &completionField->buffer[ strlen( completionField->buffer ) -
+		strlen( completionString ) ], shortestMatch,
+		sizeof( completionField->buffer ) );
+	completionField->cursor = strlen( completionField->buffer );
+
+	if( matchCount == 1 )
+	{
+		Q_strcat( completionField->buffer, sizeof( completionField->buffer ), " " );
+		completionField->cursor++;
+		return;
+	}
+
+	Com_Printf( "]%s\n", completionField->buffer );
+	
+	Key_KeynameCompletion( PrintMatches );
+}
+#endif
+
 /*
 ===============
 Field_CompleteFilename
@@ -2963,8 +2997,9 @@ static void Field_CompleteFilename( const char *dir,
 	if( matchCount == 0 )
 		return;
 
-	Q_strcat( completionField->buffer, sizeof( completionField->buffer ),
-			shortestMatch + strlen( completionString ) );
+	Q_strncpyz( &completionField->buffer[ strlen( completionField->buffer ) -
+		strlen( completionString ) ], shortestMatch,
+		sizeof( completionField->buffer ) );
 	completionField->cursor = strlen( completionField->buffer );
 
 	if( matchCount == 1 )
@@ -3005,20 +3040,36 @@ static void Field_CompleteCommand( char *cmd,
 	else
 		completionString = Cmd_Argv( completionArgument - 1 );
 
+#ifndef DEDICATED
+	// Unconditionally add a '\' to the start of the buffer
+	if( completionField->buffer[ 0 ] &&
+			completionField->buffer[ 0 ] != '\\' )
+	{
+		if( completionField->buffer[ 0 ] != '/' )
+		{
+			// Buffer is full, refuse to complete
+			if( strlen( completionField->buffer ) + 1 >=
+				sizeof( completionField->buffer ) )
+				return;
+
+			memmove( &completionField->buffer[ 1 ],
+				&completionField->buffer[ 0 ],
+				strlen( completionField->buffer ) + 1 );
+			completionField->cursor++;
+		}
+
+		completionField->buffer[ 0 ] = '\\';
+	}
+#endif
+
 	if( completionArgument > 1 )
 	{
 		const char *baseCmd = Cmd_Argv( 0 );
 
 #ifndef DEDICATED
-		// If the very first token does not have a leading \ or /,
-		// refuse to autocomplete
-		if( cmd == completionField->buffer )
-		{
-			if( baseCmd[ 0 ] != '\\' && baseCmd[ 0 ] != '/' )
-				return;
-
+		// This should always be true
+		if( baseCmd[ 0 ] == '\\' || baseCmd[ 0 ] == '/' )
 			baseCmd++;
-		}
 #endif
 
 		if( ( p = Field_FindFirstSeparator( cmd ) ) )
@@ -3049,13 +3100,6 @@ static void Field_CompleteCommand( char *cmd,
 			{
 				Field_CompleteFilename( "", "txt", qfalse );
 			}
-			else if( !Q_stricmp( baseCmd, "demo" ) && completionArgument == 2 )
-			{
-				char demoExt[ 16 ];
-
-				Com_sprintf( demoExt, sizeof( demoExt ), ".dm_%d", PROTOCOL_VERSION );
-				Field_CompleteFilename( "demos", demoExt, qtrue );
-			}
 			else if( ( !Q_stricmp( baseCmd, "toggle" ) ||
 						!Q_stricmp( baseCmd, "vstr" ) ||
 						!Q_stricmp( baseCmd, "set" ) ||
@@ -3070,6 +3114,14 @@ static void Field_CompleteCommand( char *cmd,
 				if( p > cmd )
 					Field_CompleteCommand( p, qfalse, qtrue );
 			}
+#ifndef DEDICATED
+			else if( !Q_stricmp( baseCmd, "demo" ) && completionArgument == 2 )
+			{
+				char demoExt[ 16 ];
+
+				Com_sprintf( demoExt, sizeof( demoExt ), ".dm_%d", PROTOCOL_VERSION );
+				Field_CompleteFilename( "demos", demoExt, qtrue );
+			}
 			else if( !Q_stricmp( baseCmd, "rcon" ) && completionArgument == 2 )
 			{
 				// Skip "rcon "
@@ -3078,14 +3130,26 @@ static void Field_CompleteCommand( char *cmd,
 				if( p > cmd )
 					Field_CompleteCommand( p, qtrue, qtrue );
 			}
-			else if( !Q_stricmp( baseCmd, "bind" ) && completionArgument >= 3 )
+			else if( !Q_stricmp( baseCmd, "bind" ) )
 			{
-				// Skip "bind <key> "
-				p = Com_SkipTokens( cmd, 2, " " );
+				if( completionArgument == 2 )
+				{
+					// Skip "bind "
+					p = Com_SkipTokens( cmd, 1, " " );
 
-				if( p > cmd )
-					Field_CompleteCommand( p, qtrue, qtrue );
+					if( p > cmd )
+						Field_CompleteKeyname( );
+				}
+				else if( completionArgument >= 3 )
+				{
+					// Skip "bind <key> "
+					p = Com_SkipTokens( cmd, 2, " " );
+
+					if( p > cmd )
+						Field_CompleteCommand( p, qtrue, qtrue );
+				}
 			}
+#endif
 		}
 	}
 	else
@@ -3106,23 +3170,11 @@ static void Field_CompleteCommand( char *cmd,
 			Cvar_CommandCompletion( FindMatches );
 
 		if( matchCount == 0 )
-			return;	// no matches
+			return; // no matches
 
-		if( cmd == completionField->buffer )
-		{
-#ifndef DEDICATED
-			Com_sprintf( completionField->buffer,
-					sizeof( completionField->buffer ), "\\%s", shortestMatch );
-#else
-			Com_sprintf( completionField->buffer,
-					sizeof( completionField->buffer ), "%s", shortestMatch );
-#endif
-		}
-		else
-		{
-			Q_strcat( completionField->buffer, sizeof( completionField->buffer ),
-					shortestMatch + strlen( completionString ) );
-		}
+		Q_strncpyz( &completionField->buffer[ strlen( completionField->buffer ) -
+			strlen( completionString ) ], shortestMatch,
+			sizeof( completionField->buffer ) );
 
 		completionField->cursor = strlen( completionField->buffer );
 
diff --git a/src/qcommon/cvar.c b/src/qcommon/cvar.c
index d39f134e..5503bcc1 100644
--- a/src/qcommon/cvar.c
+++ b/src/qcommon/cvar.c
@@ -498,9 +498,6 @@ Handles variable inspection and changing from the console
 */
 qboolean Cvar_Command( void ) {
 	cvar_t	*v;
-	char		string[ TRUNCATE_LENGTH ];
-	char		resetString[ TRUNCATE_LENGTH ];
-	char		latchedString[ TRUNCATE_LENGTH ];
 
 	// check variables
 	v = Cvar_FindVar (Cmd_Argv(0));
@@ -510,25 +507,22 @@ qboolean Cvar_Command( void ) {
 
 	// perform a variable print or set
 	if ( Cmd_Argc() == 1 ) {
-		Com_TruncateLongString( string, v->string );
-		Com_TruncateLongString( resetString, v->resetString );
 		Com_Printf ("\"%s\" is:\"%s" S_COLOR_WHITE "\"",
-				v->name, string );
+				v->name, v->string );
 
 		if ( !( v->flags & CVAR_ROM ) ) {
-			if ( !Q_stricmp( string, resetString ) ) {
+			if ( !Q_stricmp( v->string, v->resetString ) ) {
 				Com_Printf (", the default" );
 			} else {
 				Com_Printf (" default:\"%s" S_COLOR_WHITE "\"",
-						resetString );
+						v->resetString );
 			}
 		}
 
 		Com_Printf ("\n");
 
 		if ( v->latchedString ) {
-			Com_TruncateLongString( latchedString, v->latchedString );
-			Com_Printf( "latched: \"%s\"\n", latchedString );
+			Com_Printf( "latched: \"%s\"\n", v->latchedString );
 		}
 		return qtrue;
 	}
diff --git a/src/qcommon/q_platform.h b/src/qcommon/q_platform.h
index 361526dc..9d0d6dd3 100644
--- a/src/qcommon/q_platform.h
+++ b/src/qcommon/q_platform.h
@@ -123,6 +123,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
 #ifdef __linux__
 
+#include <endian.h>
+
 #define OS_STRING "linux"
 #define ID_INLINE inline
 #define PATH_SEP '/'
@@ -171,6 +173,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
 #ifdef __FreeBSD__
 
+#include <sys/types.h>
 #include <machine/endian.h>
 
 #define OS_STRING "freebsd"
@@ -193,11 +196,37 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
 #endif
 
+//=============================================================== OpenBSD ===
+
+#ifdef __OpenBSD__
+
+#include <sys/types.h>
+#include <machine/endian.h>
+
+#define OS_STRING "openbsd"
+#define ID_INLINE inline
+#define PATH_SEP '/'
+
+#ifdef __i386__
+#define ARCH_STRING "i386"
+#endif
+
+#if BYTE_ORDER == BIG_ENDIAN
+#define Q3_BIG_ENDIAN
+#else
+#define Q3_LITTLE_ENDIAN
+#endif
+
+#define DLL_EXT ".so"
+
+#endif
+
 //================================================================ NetBSD ===
 
 // This is very much like the FreeBSD one and can probably be merged
 #ifdef __NetBSD__
 
+#include <sys/types.h>
 #include <machine/endian.h>
 
 #define OS_STRING "netbsd"
diff --git a/src/qcommon/qcommon.h b/src/qcommon/qcommon.h
index 59332068..d6273d8a 100644
--- a/src/qcommon/qcommon.h
+++ b/src/qcommon/qcommon.h
@@ -916,6 +916,9 @@ void CL_FlushMemory( void );
 void CL_StartHunkUsers( qboolean rendererOnly );
 // start all the client stuff using the hunk
 
+void Key_KeynameCompletion( void(*callback)(const char *s) );
+// for keyname autocompletion
+
 void Key_WriteBindings( fileHandle_t f );
 // for writing the config files
 
diff --git a/src/renderer/qgl.h b/src/renderer/qgl.h
index f83eaf31..b8544b15 100644
--- a/src/renderer/qgl.h
+++ b/src/renderer/qgl.h
@@ -27,7 +27,11 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #ifndef __QGL_H__
 #define __QGL_H__
 
-#include "SDL_opengl.h"
+#ifdef USE_LOCAL_HEADERS
+#	include "SDL_opengl.h"
+#else
+#	include <SDL_opengl.h>
+#endif
 
 extern void (APIENTRYP qglActiveTextureARB) (GLenum texture);
 extern void (APIENTRYP qglClientActiveTextureARB) (GLenum texture);
diff --git a/src/sdl/sdl_gamma.c b/src/sdl/sdl_gamma.c
index 0b63b85a..346bcfd2 100644
--- a/src/sdl/sdl_gamma.c
+++ b/src/sdl/sdl_gamma.c
@@ -20,7 +20,12 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 ===========================================================================
 */
 
-#include "SDL.h"
+#ifdef USE_LOCAL_HEADERS
+#	include "SDL.h"
+#else
+#	include <SDL.h>
+#endif
+
 #include "../renderer/tr_local.h"
 #include "../qcommon/qcommon.h"
 
diff --git a/src/sdl/sdl_glimp.c b/src/sdl/sdl_glimp.c
index 1f5c3434..cb3e8e0d 100644
--- a/src/sdl/sdl_glimp.c
+++ b/src/sdl/sdl_glimp.c
@@ -20,7 +20,11 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 ===========================================================================
 */
 
-#include "SDL.h"
+#ifdef USE_LOCAL_HEADERS
+#	include "SDL.h"
+#else
+#	include <SDL.h>
+#endif
 
 #if !SDL_VERSION_ATLEAST(1, 2, 10)
 #define SDL_GL_ACCELERATED_VISUAL 15
@@ -30,7 +34,11 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #endif
 
 #ifdef SMP
-#include "SDL_thread.h"
+#	ifdef USE_LOCAL_HEADERS
+#		include "SDL_thread.h"
+#	else
+#		include <SDL_thread.h>
+#	endif
 #endif
 
 #include <stdarg.h>
diff --git a/src/sdl/sdl_input.c b/src/sdl/sdl_input.c
index 1e2ca988..d65c3504 100644
--- a/src/sdl/sdl_input.c
+++ b/src/sdl/sdl_input.c
@@ -20,7 +20,11 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 ===========================================================================
 */
 
-#include "SDL.h"
+#ifdef USE_LOCAL_HEADERS
+#	include "SDL.h"
+#else
+#	include <SDL.h>
+#endif
 
 #include <stdarg.h>
 #include <stdio.h>
diff --git a/src/sdl/sdl_snd.c b/src/sdl/sdl_snd.c
index ea7927ee..4a4c78d1 100644
--- a/src/sdl/sdl_snd.c
+++ b/src/sdl/sdl_snd.c
@@ -23,7 +23,11 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <stdlib.h>
 #include <stdio.h>
 
-#include "SDL.h"
+#ifdef USE_LOCAL_HEADERS
+#	include "SDL.h"
+#else
+#	include <SDL.h>
+#endif
 
 #include "../qcommon/q_shared.h"
 #include "../client/snd_local.h"
diff --git a/src/sys/sys_main.c b/src/sys/sys_main.c
index 3f3bd7d5..32c55e37 100644
--- a/src/sys/sys_main.c
+++ b/src/sys/sys_main.c
@@ -19,13 +19,11 @@ along with Tremulous; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 ===========================================================================
 */
-#include <unistd.h>
+
 #include <signal.h>
 #include <stdlib.h>
 #include <limits.h>
-#include <sys/time.h>
 #include <sys/types.h>
-#include <unistd.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <sys/stat.h>
@@ -34,8 +32,13 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <errno.h>
 
 #ifndef DEDICATED
-#include "SDL.h"
-#include "SDL_cpuinfo.h"
+#ifdef USE_LOCAL_HEADERS
+#	include "SDL.h"
+#	include "SDL_cpuinfo.h"
+#else
+#	include <SDL.h>
+#	include <SDL_cpuinfo.h>
+#endif
 #endif
 
 #include "sys_local.h"
@@ -163,10 +166,7 @@ void Sys_Exit( int ex )
 #endif
 
 #ifdef NDEBUG
-	// _exit is called instead of exit since there are rumours of
-	// GL libraries installing atexit calls that we don't want to call
-	// FIXME: get some testing done with plain exit
-	_exit(ex);
+	exit(ex);
 #else
 	// Cause a backtrace on error exits
 	assert( ex == 0 );
@@ -220,86 +220,73 @@ void Sys_Init(void)
 	Cvar_Set( "username", Sys_GetCurrentUser( ) );
 }
 
-static struct Q3ToAnsiColorTable_s
-{
-	char Q3color;
-	char *ANSIcolor;
-} CON_colorTable[ ] =
-{
-	{ COLOR_BLACK,    "30" },
-	{ COLOR_RED,      "31" },
-	{ COLOR_GREEN,    "32" },
-	{ COLOR_YELLOW,   "33" },
-	{ COLOR_BLUE,     "34" },
-	{ COLOR_CYAN,     "36" },
-	{ COLOR_MAGENTA,  "35" },
-	{ COLOR_WHITE,    "0" }
-};
-
-static int CON_colorTableSize =
-	sizeof( CON_colorTable ) / sizeof( CON_colorTable[ 0 ] );
-
 /*
 =================
-Sys_ANSIColorify
+Sys_AnsiColorPrint
 
 Transform Q3 colour codes to ANSI escape sequences
 =================
 */
-static void Sys_ANSIColorify( const char *msg, char *buffer, int bufferSize )
+static void Sys_AnsiColorPrint( const char *msg )
 {
-	int   msgLength, pos;
-	int   i, j;
-	char  *escapeCode;
-	char  tempBuffer[ 7 ];
-
-	if( !msg || !buffer )
-		return;
-
-	msgLength = strlen( msg );
-	pos = 0;
-	i = 0;
-	buffer[ 0 ] = '\0';
-
-	while( i < msgLength )
+	static char buffer[ MAXPRINTMSG ];
+	int         length = 0;
+	static int  q3ToAnsi[ 8 ] =
 	{
-		if( msg[ i ] == '\n' )
-		{
-			Com_sprintf( tempBuffer, 7, "%c[0m\n", 0x1B );
-			strncat( buffer, tempBuffer, bufferSize );
-			i++;
-		}
-		else if( msg[ i ] == Q_COLOR_ESCAPE )
+		30, // COLOR_BLACK
+		31, // COLOR_RED
+		32, // COLOR_GREEN
+		33, // COLOR_YELLOW
+		34, // COLOR_BLUE
+		36, // COLOR_CYAN
+		35, // COLOR_MAGENTA
+		0   // COLOR_WHITE
+	};
+
+	while( *msg )
+	{
+		if( Q_IsColorString( msg ) || *msg == '\n' )
 		{
-			i++;
+			// First empty the buffer
+			if( length > 0 )
+			{
+				buffer[ length ] = '\0';
+				fputs( buffer, stderr );
+				length = 0;
+			}
 
-			if( i < msgLength )
+			if( *msg == '\n' )
+			{
+				// Issue a reset and then the newline
+				fputs( "\033[0m\n", stderr );
+				msg++;
+			}
+			else
 			{
-				escapeCode = NULL;
-				for( j = 0; j < CON_colorTableSize; j++ )
-				{
-					if( msg[ i ] == CON_colorTable[ j ].Q3color )
-					{
-						escapeCode = CON_colorTable[ j ].ANSIcolor;
-						break;
-					}
-				}
-
-				if( escapeCode )
-				{
-					Com_sprintf( tempBuffer, 7, "%c[%sm", 0x1B, escapeCode );
-					strncat( buffer, tempBuffer, bufferSize );
-				}
-
-				i++;
+				// Print the color code
+				Com_sprintf( buffer, sizeof( buffer ), "\033[%dm",
+						q3ToAnsi[ ColorIndex( *( msg + 1 ) ) ] );
+				fputs( buffer, stderr );
+				msg += 2;
 			}
 		}
 		else
 		{
-			Com_sprintf( tempBuffer, 7, "%c", msg[ i++ ] );
-			strncat( buffer, tempBuffer, bufferSize );
+			if( length >= MAXPRINTMSG - 1 )
+				break;
+
+			buffer[ length ] = *msg;
+			length++;
+			msg++;
 		}
 	}
+
+	// Empty anything still left in the buffer
+	if( length > 0 )
+	{
+		buffer[ length ] = '\0';
+		fputs( buffer, stderr );
+	}
 }
 
 /*
@@ -314,11 +301,7 @@ void Sys_Print( const char *msg )
 #endif
 
 	if( com_ansiColor && com_ansiColor->integer )
-	{
-		char ansiColorString[ MAXPRINTMSG ];
-		Sys_ANSIColorify( msg, ansiColorString, MAXPRINTMSG );
-		fputs( ansiColorString, stderr );
-	}
+		Sys_AnsiColorPrint( msg );
 	else
 		fputs(msg, stderr);
 
@@ -453,7 +436,6 @@ void *Sys_LoadDll( const char *name, char *fqpath ,
 {
 	void  *libHandle;
 	void  (*dllEntry)( intptr_t (*syscallptr)(intptr_t, ...) );
-	char  curpath[MAX_OSPATH];
 	char  fname[MAX_OSPATH];
 	char  *basepath;
 	char  *homepath;
@@ -462,7 +444,6 @@ void *Sys_LoadDll( const char *name, char *fqpath ,
 
 	assert( name );
 
-	getcwd(curpath, sizeof(curpath));
 	Q_snprintf (fname, sizeof(fname), "%s" ARCH_STRING DLL_EXT, name);
 
 	// TODO: use fs_searchpaths from files.c
diff --git a/src/sys/win_resource.rc b/src/sys/win_resource.rc
index 4b50adc2..2527af2f 100644
--- a/src/sys/win_resource.rc
+++ b/src/sys/win_resource.rc
@@ -58,6 +58,8 @@ END
 // Icon with lowest ID value placed first to ensure application icon
 // remains consistent on all systems.
 IDI_ICON1               ICON    DISCARDABLE     "misc/tremulous.ico"
+#endif
+
 
 /////////////////////////////////////////////////////////////////////////////
 //
diff --git a/src/tools/asm/cmdlib.c b/src/tools/asm/cmdlib.c
index 69ce3ffd..ac088675 100644
--- a/src/tools/asm/cmdlib.c
+++ b/src/tools/asm/cmdlib.c
@@ -976,13 +976,7 @@ int ParseNum (const char *str)
 ============================================================================
 */
 
-#ifdef _SGI_SOURCE
-#define	__BIG_ENDIAN__
-#endif
-
-#ifdef __BIG_ENDIAN__
-
-short   LittleShort (short l)
+short   ShortSwap (short l)
 {
 	byte    b1,b2;
 
@@ -992,13 +986,7 @@ short   LittleShort (short l)
 	return (b1<<8) + b2;
 }
 
-short   BigShort (short l)
-{
-	return l;
-}
-
-
-int    LittleLong (int l)
+int    LongSwap (int l)
 {
 	byte    b1,b2,b3,b4;
 
@@ -1010,89 +998,20 @@ int    LittleLong (int l)
 	return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
 }
 
-int    BigLong (int l)
-{
-	return l;
-}
-
-
-float	LittleFloat (float l)
-{
-	union {byte b[4]; float f;} in, out;
-	
-	in.f = l;
-	out.b[0] = in.b[3];
-	out.b[1] = in.b[2];
-	out.b[2] = in.b[1];
-	out.b[3] = in.b[0];
-	
-	return out.f;
-}
-
-float	BigFloat (float l)
-{
-	return l;
-}
-
-
-#else
-
-
-short   BigShort (short l)
-{
-	byte    b1,b2;
-
-	b1 = l&255;
-	b2 = (l>>8)&255;
-
-	return (b1<<8) + b2;
-}
-
-short   LittleShort (short l)
-{
-	return l;
-}
+typedef union {
+    float	f;
+    unsigned int i;
+} _FloatByteUnion;
 
+float FloatSwap (const float *f) {
+	_FloatByteUnion out;
 
-int    BigLong (int l)
-{
-	byte    b1,b2,b3,b4;
+	out.f = *f;
+	out.i = LongSwap(out.i);
 
-	b1 = l&255;
-	b2 = (l>>8)&255;
-	b3 = (l>>16)&255;
-	b4 = (l>>24)&255;
-
-	return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
-}
-
-int    LittleLong (int l)
-{
-	return l;
-}
-
-float	BigFloat (float l)
-{
-	union {byte b[4]; float f;} in, out;
-	
-	in.f = l;
-	out.b[0] = in.b[3];
-	out.b[1] = in.b[2];
-	out.b[2] = in.b[1];
-	out.b[3] = in.b[0];
-	
 	return out.f;
 }
 
-float	LittleFloat (float l)
-{
-	return l;
-}
-
-
-#endif
-
-
 //=======================================================
 
 
diff --git a/src/tools/asm/cmdlib.h b/src/tools/asm/cmdlib.h
index 36a5dfc0..3b2f2db4 100644
--- a/src/tools/asm/cmdlib.h
+++ b/src/tools/asm/cmdlib.h
@@ -116,14 +116,6 @@ void	ExtractFileExtension( const char *path, char *dest );
 
 int 	ParseNum (const char *str);
 
-short	BigShort (short l);
-short	LittleShort (short l);
-int		BigLong (int l);
-int		LittleLong (int l);
-float	BigFloat (float l);
-float	LittleFloat (float l);
-
-
 char *COM_Parse (char *data);
 
 extern	char		com_token[1024];
diff --git a/src/tools/asm/q3asm.c b/src/tools/asm/q3asm.c
index 4f4c9a37..1ac2939e 100644
--- a/src/tools/asm/q3asm.c
+++ b/src/tools/asm/q3asm.c
@@ -21,6 +21,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 ===========================================================================
 */
 
+#include "../../qcommon/q_platform.h"
 #include "cmdlib.h"
 #include "mathlib.h"
 #include "../../qcommon/qfiles.h"
@@ -1401,6 +1402,17 @@ void WriteVmFile( void ) {
 
 	report( "Writing to %s\n", imageName );
 
+#ifdef Q3_BIG_ENDIAN
+	{
+		int i;
+
+		// byte swap the header
+		for ( i = 0 ; i < sizeof( vmHeader_t ) / 4 ; i++ ) {
+			((int *)&header)[i] = LittleLong( ((int *)&header)[i] );
+		}
+	}
+#endif
+
 	CreatePath( imageName );
 	f = SafeOpenWrite( imageName );
 	SafeWrite( f, &header, headerSize );
diff --git a/src/ui/ui_main.c b/src/ui/ui_main.c
index 39e6c1b5..7618c211 100644
--- a/src/ui/ui_main.c
+++ b/src/ui/ui_main.c
@@ -5535,7 +5535,7 @@ static void UI_DisplayDownloadInfo( const char *downloadName, float centerPoint,
   Text_PaintCenter(centerPoint, yStart + 248, scale, colorWhite, xferText, 0);
 
   if (downloadSize > 0) {
-    s = va( "%s (%d%%)", downloadName, downloadCount * 100 / downloadSize );
+    s = va( "%s (%d%%)", downloadName, (int)( (float)downloadCount * 100.0f / downloadSize ) );
   } else {
     s = downloadName;
   }
-- 
cgit