From fabe501768809de5b1f3f394cbdc9d54d204e334 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Thu, 10 Feb 2000 13:49:39 +0000 Subject: [PATCH] the merge continues. qw_common/common.c and uquake/common.c have /finally/ been merged with the command line args merged into common/qargs.c. Args rebuilding in qargs.c should now be totally safe with no limits. --- {qw_common => common}/common.c | 151 +++---- common/common.h | 5 +- common/host.c | 2 +- common/qargs.c | 42 +- common/qdefs.h | 2 - common/quakefs.h | 2 + uquake/Makefile.in | 2 +- uquake/common.c | 745 --------------------------------- 8 files changed, 118 insertions(+), 833 deletions(-) rename {qw_common => common}/common.c (95%) delete mode 100644 uquake/common.c diff --git a/qw_common/common.c b/common/common.c similarity index 95% rename from qw_common/common.c rename to common/common.c index bb9cf95..2d22d9a 100644 --- a/qw_common/common.c +++ b/common/common.c @@ -21,41 +21,27 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ - -#include -#include -#include -#include -#include -#include - -#ifdef SERVERONLY -#include -#else -#include -#endif - #include "config.h" #ifdef HAVE_STRING_H #include #endif - #ifdef HAVE_STRINGS_H #include #endif +#include +#include +#include +#include +#include +#include +#include +#include +#include -void COM_InitFilesystem (void); -void COM_Path_f (void); - - +char com_token[1024]; qboolean standard_quake = true, rogue, hipnotic; - -extern cvar_t developer; -//============================================================================ - - // ClearLink is used for new headnodes void ClearLink (link_t *l) { @@ -232,8 +218,6 @@ void MSG_WriteDeltaUsercmd (sizebuf_t *buf, usercmd_t *from, usercmd_t *cmd) MSG_WriteByte (buf, cmd->impulse); MSG_WriteByte (buf, cmd->msec); } - - // // reading functions // @@ -432,9 +416,26 @@ void MSG_ReadDeltaUsercmd (usercmd_t *from, usercmd_t *move) move->msec = MSG_ReadByte (); } - //=========================================================================== +void SZ_Alloc (sizebuf_t *buf, int startsize) +{ + if (startsize < 256) + startsize = 256; + buf->data = Hunk_AllocName (startsize, "sizebuf"); + buf->maxsize = startsize; + buf->cursize = 0; +} + + +void SZ_Free (sizebuf_t *buf) +{ +// Z_Free (buf->data); +// buf->data = NULL; +// buf->maxsize = 0; + buf->cursize = 0; +} + void SZ_Clear (sizebuf_t *buf) { buf->cursize = 0; @@ -480,11 +481,8 @@ void SZ_Print (sizebuf_t *buf, char *data) else Q_memcpy ((byte *)SZ_GetSpace(buf, len-1)-1,data,len); // write over trailing 0 } - - //============================================================================ - /* ============ COM_SkipPath @@ -492,7 +490,7 @@ COM_SkipPath */ char *COM_SkipPath (char *pathname) { - char *last; + char *last; last = pathname; while (*pathname) @@ -524,7 +522,7 @@ COM_FileExtension char *COM_FileExtension (char *in) { static char exten[8]; - int i; + int i; while (*in && *in != '.') in++; @@ -588,12 +586,6 @@ void COM_DefaultExtension (char *path, char *extension) strcat (path, extension); } - -//============================================================================ - -char com_token[1024]; - - /* ============== COM_Parse @@ -603,8 +595,8 @@ Parse a token out of a string */ char *COM_Parse (char *data) { - int c; - int len; + int c; + int len; len = 0; com_token[0] = 0; @@ -617,7 +609,7 @@ skipwhite: while ( (c = *data) <= ' ') { if (c == 0) - return NULL; // end of file; + return NULL; // end of file; data++; } @@ -647,6 +639,15 @@ skipwhite: } } +// parse single characters + if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':') + { + com_token[len] = c; + len++; + com_token[len] = 0; + return data+1; + } + // parse a regular word do { @@ -654,12 +655,46 @@ skipwhite: data++; len++; c = *data; + if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':') + break; } while (c>32); com_token[len] = 0; return data; } +/* +============ +va + +does a varargs printf into a temp buffer, so I don't need to have +varargs versions of all text functions. +FIXME: make this buffer size safe someday +============ +*/ +char *va(char *format, ...) +{ + va_list argptr; + static char string[1024]; + + va_start (argptr, format); + vsnprintf (string, sizeof(string), format, argptr); + va_end (argptr); + + return string; +} + +/// just for debugging +int memsearch (byte *start, int count, int search) +{ + int i; + + for (i=0 ; ibasedir); + COM_Init (); Host_InitLocal (); #endif diff --git a/common/qargs.c b/common/qargs.c index 74ed4b5..a1dbbc9 100644 --- a/common/qargs.c +++ b/common/qargs.c @@ -22,6 +22,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#include +#include #include #include #include @@ -33,13 +35,18 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. usercmd_t nullcmd; // guarenteed to be zero -static char *largv[MAX_NUM_ARGVS + NUM_SAFE_ARGVS + 1]; +static char **largv; static char *argvdummy = " "; -static char *safeargvs[NUM_SAFE_ARGVS] = - {"-stdvid", "-nolan", "-nosound", "-nocdaudio", "-nojoy", "-nomouse"}; +static char *safeargvs[] = + {"-stdvid", "-nolan", "-nosound", "-nocdaudio", "-nojoy", "-nomouse", "-dibonly"}; + +#define NUM_SAFE_ARGVS (sizeof(safeargvs)/sizeof(safeargvs[0])) + int com_argc; char **com_argv; +char *com_cmdline; +cvar_t cmdline = {"cmdline","0", false, true}; /* ================ @@ -72,18 +79,29 @@ COM_InitArgv void COM_InitArgv (int argc, char **argv) { qboolean safe; - int i; + int i, len; safe = false; - for (com_argc=0 ; (com_argc -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#define NUM_SAFE_ARGVS 7 - -static char *largv[MAX_NUM_ARGVS + NUM_SAFE_ARGVS + 1]; -static char *argvdummy = " "; - -static char *safeargvs[NUM_SAFE_ARGVS] = - {"-stdvid", "-nolan", "-nosound", "-nocdaudio", "-nojoy", "-nomouse", "-dibonly"}; - -cvar_t cmdline = {"cmdline","0", false, true}; - -qboolean com_modified; // set true if using non-id files - -qboolean proghack; - -qboolean msg_suppress_1 = 0; - -void COM_InitFilesystem (void); - -// if a packfile directory differs from this, it is assumed to be hacked -#define PAK0_COUNT 339 -#define PAK0_CRC 32981 - -char com_token[1024]; -int com_argc; -char **com_argv; - -#define CMDLINE_LENGTH 256 -char com_cmdline[CMDLINE_LENGTH]; - -qboolean standard_quake = true, rogue, hipnotic; - -extern cvar_t developer; - -//============================================================================ - - -// ClearLink is used for new headnodes -void ClearLink (link_t *l) -{ - l->prev = l->next = l; -} - -void RemoveLink (link_t *l) -{ - l->next->prev = l->prev; - l->prev->next = l->next; -} - -void InsertLinkBefore (link_t *l, link_t *before) -{ - l->next = before; - l->prev = before->prev; - l->prev->next = l; - l->next->prev = l; -} -void InsertLinkAfter (link_t *l, link_t *after) -{ - l->next = after->next; - l->prev = after; - l->prev->next = l; - l->next->prev = l; -} - -/* -============================================================================== - - MESSAGE IO FUNCTIONS - -Handles byte ordering and avoids alignment errors -============================================================================== -*/ - -// -// writing functions -// - -void MSG_WriteChar (sizebuf_t *sb, int c) -{ - byte *buf; - -#ifdef PARANOID - if (c < -128 || c > 127) - Sys_Error ("MSG_WriteChar: range error"); -#endif - - buf = SZ_GetSpace (sb, 1); - buf[0] = c; -} - -void MSG_WriteByte (sizebuf_t *sb, int c) -{ - byte *buf; - -#ifdef PARANOID - if (c < 0 || c > 255) - Sys_Error ("MSG_WriteByte: range error"); -#endif - - buf = SZ_GetSpace (sb, 1); - buf[0] = c; -} - -void MSG_WriteShort (sizebuf_t *sb, int c) -{ - byte *buf; - -#ifdef PARANOID - if (c < ((short)0x8000) || c > (short)0x7fff) - Sys_Error ("MSG_WriteShort: range error"); -#endif - - buf = SZ_GetSpace (sb, 2); - buf[0] = c&0xff; - buf[1] = c>>8; -} - -void MSG_WriteLong (sizebuf_t *sb, int c) -{ - byte *buf; - - buf = SZ_GetSpace (sb, 4); - buf[0] = c&0xff; - buf[1] = (c>>8)&0xff; - buf[2] = (c>>16)&0xff; - buf[3] = c>>24; -} - -void MSG_WriteFloat (sizebuf_t *sb, float f) -{ - union - { - float f; - int l; - } dat; - - - dat.f = f; - dat.l = LittleLong (dat.l); - - SZ_Write (sb, &dat.l, 4); -} - -void MSG_WriteString (sizebuf_t *sb, char *s) -{ - if (!s) - SZ_Write (sb, "", 1); - else - SZ_Write (sb, s, Q_strlen(s)+1); -} - -void MSG_WriteCoord (sizebuf_t *sb, float f) -{ - MSG_WriteShort (sb, (int)(f*8)); -} - -void MSG_WriteAngle (sizebuf_t *sb, float f) -{ - MSG_WriteByte (sb, ((int)f*256/360) & 255); -} - -// -// reading functions -// -int msg_readcount; -qboolean msg_badread; - -void MSG_BeginReading (void) -{ - msg_readcount = 0; - msg_badread = false; -} - -// returns -1 and sets msg_badread if no more characters are available -int MSG_ReadChar (void) -{ - int c; - - if (msg_readcount+1 > net_message.cursize) - { - msg_badread = true; - return -1; - } - - c = (signed char)net_message.data[msg_readcount]; - msg_readcount++; - - return c; -} - -int MSG_ReadByte (void) -{ - int c; - - if (msg_readcount+1 > net_message.cursize) - { - msg_badread = true; - return -1; - } - - c = (unsigned char)net_message.data[msg_readcount]; - msg_readcount++; - - return c; -} - -int MSG_ReadShort (void) -{ - int c; - - if (msg_readcount+2 > net_message.cursize) - { - msg_badread = true; - return -1; - } - - c = (short)(net_message.data[msg_readcount] - + (net_message.data[msg_readcount+1]<<8)); - - msg_readcount += 2; - - return c; -} - -int MSG_ReadLong (void) -{ - int c; - - if (msg_readcount+4 > net_message.cursize) - { - msg_badread = true; - return -1; - } - - c = net_message.data[msg_readcount] - + (net_message.data[msg_readcount+1]<<8) - + (net_message.data[msg_readcount+2]<<16) - + (net_message.data[msg_readcount+3]<<24); - - msg_readcount += 4; - - return c; -} - -float MSG_ReadFloat (void) -{ - union - { - byte b[4]; - float f; - int l; - } dat; - - dat.b[0] = net_message.data[msg_readcount]; - dat.b[1] = net_message.data[msg_readcount+1]; - dat.b[2] = net_message.data[msg_readcount+2]; - dat.b[3] = net_message.data[msg_readcount+3]; - msg_readcount += 4; - - dat.l = LittleLong (dat.l); - - return dat.f; -} - -char *MSG_ReadString (void) -{ - static char string[2048]; - int l,c; - - l = 0; - do - { - c = MSG_ReadChar (); - if (c == -1 || c == 0) - break; - string[l] = c; - l++; - } while (l < sizeof(string)-1); - - string[l] = 0; - - return string; -} - -float MSG_ReadCoord (void) -{ - return MSG_ReadShort() * (1.0/8); -} - -float MSG_ReadAngle (void) -{ - return MSG_ReadChar() * (360.0/256); -} - - - -//=========================================================================== - -void SZ_Alloc (sizebuf_t *buf, int startsize) -{ - if (startsize < 256) - startsize = 256; - buf->data = Hunk_AllocName (startsize, "sizebuf"); - buf->maxsize = startsize; - buf->cursize = 0; -} - - -void SZ_Free (sizebuf_t *buf) -{ -// Z_Free (buf->data); -// buf->data = NULL; -// buf->maxsize = 0; - buf->cursize = 0; -} - -void SZ_Clear (sizebuf_t *buf) -{ - buf->cursize = 0; -} - -void *SZ_GetSpace (sizebuf_t *buf, int length) -{ - void *data; - - if (buf->cursize + length > buf->maxsize) - { - if (!buf->allowoverflow) - Sys_Error ("SZ_GetSpace: overflow without allowoverflow set"); - - if (length > buf->maxsize) - Sys_Error ("SZ_GetSpace: %i is > full buffer size", length); - - buf->overflowed = true; - Con_Printf ("SZ_GetSpace: overflow"); - SZ_Clear (buf); - } - - data = buf->data + buf->cursize; - buf->cursize += length; - - return data; -} - -void SZ_Write (sizebuf_t *buf, void *data, int length) -{ - Q_memcpy (SZ_GetSpace(buf,length),data,length); -} - -void SZ_Print (sizebuf_t *buf, char *data) -{ - int len; - - len = Q_strlen(data)+1; - -// byte * cast to keep VC++ happy - if (buf->data[buf->cursize-1]) - Q_memcpy ((byte *)SZ_GetSpace(buf, len),data,len); // no trailing 0 - else - Q_memcpy ((byte *)SZ_GetSpace(buf, len-1)-1,data,len); // write over trailing 0 -} - - -//============================================================================ - - -/* -============ -COM_SkipPath -============ -*/ -char *COM_SkipPath (char *pathname) -{ - char *last; - - last = pathname; - while (*pathname) - { - if (*pathname=='/') - last = pathname+1; - pathname++; - } - return last; -} - -/* -============ -COM_StripExtension -============ -*/ -void COM_StripExtension (char *in, char *out) -{ - while (*in && *in != '.') - *out++ = *in++; - *out = 0; -} - -/* -============ -COM_FileExtension -============ -*/ -char *COM_FileExtension (char *in) -{ - static char exten[8]; - int i; - - while (*in && *in != '.') - in++; - if (!*in) - return ""; - in++; - for (i=0 ; i<7 && *in ; i++,in++) - exten[i] = *in; - exten[i] = 0; - return exten; -} - -/* -============ -COM_FileBase -============ -*/ -void COM_FileBase (char *in, char *out) -{ - char *s, *s2; - - s = in + strlen(in) - 1; - - while (s != in && *s != '.') - s--; - - for (s2 = s ; *s2 && *s2 != '/' ; s2--) - ; - - if (s-s2 < 2) - strcpy (out,"?model?"); - else - { - s--; - strncpy (out,s2+1, s-s2); - out[s-s2] = 0; - } -} - - -/* -================== -COM_DefaultExtension -================== -*/ -void COM_DefaultExtension (char *path, char *extension) -{ - char *src; -// -// if path doesn't have a .EXT, append extension -// (extension should include the .) -// - src = path + strlen(path) - 1; - - while (*src != '/' && src != path) - { - if (*src == '.') - return; // it has an extension - src--; - } - - strcat (path, extension); -} - - -/* -============== -COM_Parse - -Parse a token out of a string -============== -*/ -char *COM_Parse (char *data) -{ - int c; - int len; - - len = 0; - com_token[0] = 0; - - if (!data) - return NULL; - -// skip whitespace -skipwhite: - while ( (c = *data) <= ' ') - { - if (c == 0) - return NULL; // end of file; - data++; - } - -// skip // comments - if (c=='/' && data[1] == '/') - { - while (*data && *data != '\n') - data++; - goto skipwhite; - } - - -// handle quoted strings specially - if (c == '\"') - { - data++; - while (1) - { - c = *data++; - if (c=='\"' || !c) - { - com_token[len] = 0; - return data; - } - com_token[len] = c; - len++; - } - } - -// parse single characters - if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':') - { - com_token[len] = c; - len++; - com_token[len] = 0; - return data+1; - } - -// parse a regular word - do - { - com_token[len] = c; - data++; - len++; - c = *data; - if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':') - break; - } while (c>32); - - com_token[len] = 0; - return data; -} - - -/* -================ -COM_CheckParm - -Returns the position (1 to argc-1) in the program's argument list -where the given parameter apears, or 0 if not present -================ -*/ -int COM_CheckParm (char *parm) -{ - int i; - - for (i=1 ; i