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.
This commit is contained in:
Bill Currie 2000-02-10 13:49:39 +00:00
parent c4797f95fe
commit fabe501768
8 changed files with 118 additions and 833 deletions

View file

@ -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 <ctype.h>
#include <common.h>
#include <crc.h>
#include <sys.h>
#include <cmd.h>
#include <console.h>
#ifdef SERVERONLY
#include <qwsvdef.h>
#else
#include <quakedef.h>
#endif
#include "config.h"
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <stdarg.h>
#include <ctype.h>
#include <common.h>
#include <protocol.h>
#include <zone.h>
#include <sys.h>
#include <crc.h>
#include <console.h>
#include <cmd.h>
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 ; i<count ; i++)
if (start[i] == search)
return i;
return -1;
}
/*
================
COM_Init
@ -683,45 +718,13 @@ void COM_Init (void)
LittleFloat = FloatNoSwap;
#endif
Cvar_RegisterVariable (&cmdline);
Cmd_AddCommand ("path", COM_Path_f);
COM_InitFilesystem ();
register_check ();
}
/*
============
va
does a varargs printf into a temp buffer, so I don't need to have
varargs versions of all text functions.
============
*/
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 ; i<count ; i++)
if (start[i] == search)
return i;
return -1;
}
/*
=====================================================================

View file

@ -155,14 +155,11 @@ char *COM_Parse (char *data);
extern int com_argc;
extern char **com_argv;
extern cvar_t cmdline;
int COM_CheckParm (char *parm);
#ifdef QUAKEWORLD
void COM_AddParm (char *parm);
void COM_Init (void);
#else
void COM_Init (char *path);
#endif
void COM_InitArgv (int argc, char **argv);
char *COM_SkipPath (char *pathname);

View file

@ -543,7 +543,7 @@ Host_Init ( quakeparms_t *parms)
Chase_Init ();
Host_InitVCR (parms);
COM_Init (parms->basedir);
COM_Init ();
Host_InitLocal ();
#endif

View file

@ -22,6 +22,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <malloc.h>
#include <string.h>
#include <ctype.h>
#include <qtypes.h>
#include <common.h>
@ -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<MAX_NUM_ARGVS) && (com_argc < argc) ;
com_argc++)
largv = (char**)malloc ((argc + NUM_SAFE_ARGVS + 1) * sizeof (char**));
for (com_argc=0, len=0 ; com_argc < argc ; com_argc++)
{
largv[com_argc] = argv[com_argc];
if (!Q_strcmp ("-safe", argv[com_argc]))
safe = true;
len += strlen (argv[com_argc]) + 1;
}
com_cmdline = (char*)malloc (len); // len is strlen(com_cmdline)+1 already
com_cmdline[0] = 0;
for (i=0; i < argc; i++)
{
strncat (com_cmdline, argv[i], len);
strncat (com_cmdline, " ", len);
}
com_cmdline[len] = 0;
if (safe)
{
// force all the safe-mode switches. Note that we reserved extra space in
@ -97,6 +115,18 @@ void COM_InitArgv (int argc, char **argv)
largv[com_argc] = argvdummy;
com_argv = largv;
if (COM_CheckParm ("-rogue"))
{
rogue = true;
standard_quake = false;
}
if (COM_CheckParm ("-hipnotic"))
{
hipnotic = true;
standard_quake = false;
}
}
/*

View file

@ -31,8 +31,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#define MAX_MODELS 256
#define MAX_SOUNDS 256
#define MAX_SCOREBOARDNAME 32
#define MAX_NUM_ARGVS 50
#define NUM_SAFE_ARGVS 7
#define MAX_STYLESTRING 64
#define MAX_EDICTS 768
#define MAX_LIGHTSTYLES 64

View file

@ -46,5 +46,7 @@ byte *COM_LoadHunkFile (char *path);
void COM_LoadCacheFile (char *path, struct cache_user_s *cu);
void COM_CreatePath (char *path);
void COM_Gamedir (char *dir);
void COM_InitFilesystem (void);
void COM_Path_f (void);
#endif // _QUAKEFS_H

View file

@ -157,7 +157,7 @@ UQ_NET_SRC = net_dgrm.c net_loop.c net_main.c net_vcr.c $(NET_SRC)
MISC_SRC = common.c crc.c cvar.c cmd.c mathlib.c register_check.c \
wad.c zone.c cvars.c lib_replace.c qendian.c quakefs.c \
quakeio.c
quakeio.c qargs.c
# GL renderer source

View file

@ -1,745 +0,0 @@
/*
Copyright (C) 1996-1997 Id Software, Inc.
Portions Copyright (C) 1999,2000 Nelson Rush.
Copyright (C) 1999,2000 contributors of the QuakeForge project
Please see the file "AUTHORS" for a list of contributors
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// common.c -- misc functions used in client and server
#include <string.h>
#include <qtypes.h>
#include <qdefs.h>
#include <cvar.h>
#include <quakefs.h>
#include <common.h>
#include <net.h>
#include <zone.h>
#include <sys.h>
#include <qendian.h>
#include <console.h>
#include <cmd.h>
#include <stdarg.h>
#include <lib_replace.h>
#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<com_argc ; i++)
{
if (!com_argv[i])
continue; // NEXTSTEP sometimes clears appkit vars.
if (!Q_strcmp (parm,com_argv[i]))
return i;
}
return 0;
}
void COM_Path_f (void);
/*
================
COM_InitArgv
================
*/
void COM_InitArgv (int argc, char **argv)
{
qboolean safe;
int i, j, n;
// reconstitute the command line for the cmdline externally visible cvar
n = 0;
for (j=0 ; (j<MAX_NUM_ARGVS) && (j< argc) ; j++)
{
i = 0;
while ((n < (CMDLINE_LENGTH - 1)) && argv[j][i])
{
com_cmdline[n++] = argv[j][i++];
}
if (n < (CMDLINE_LENGTH - 1))
com_cmdline[n++] = ' ';
else
break;
}
com_cmdline[n] = 0;
safe = false;
for (com_argc=0 ; (com_argc<MAX_NUM_ARGVS) && (com_argc < argc) ;
com_argc++)
{
largv[com_argc] = argv[com_argc];
if (!Q_strcmp ("-safe", argv[com_argc]))
safe = true;
}
if (safe)
{
// force all the safe-mode switches. Note that we reserved extra space in
// case we need to add these, so we don't need an overflow check
for (i=0 ; i<NUM_SAFE_ARGVS ; i++)
{
largv[com_argc] = safeargvs[i];
com_argc++;
}
}
largv[com_argc] = argvdummy;
com_argv = largv;
if (COM_CheckParm ("-rogue"))
{
rogue = true;
standard_quake = false;
}
if (COM_CheckParm ("-hipnotic"))
{
hipnotic = true;
standard_quake = false;
}
}
/*
================
COM_Init
================
*/
void COM_Init (char *basedir)
{
#ifdef WORDS_BIGENDIAN
BigShort = ShortNoSwap;
LittleShort = ShortSwap;
BigLong = LongNoSwap;
LittleLong = LongSwap;
BigFloat = FloatNoSwap;
LittleFloat = FloatSwap;
#else
BigShort = ShortSwap;
LittleShort = ShortNoSwap;
BigLong = LongSwap;
LittleLong = LongNoSwap;
BigFloat = FloatSwap;
LittleFloat = FloatNoSwap;
#endif
Cvar_RegisterVariable (&cmdline);
Cmd_AddCommand ("path", COM_Path_f);
COM_InitFilesystem ();
register_check ();
}
/*
============
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 ; i<count ; i++)
if (start[i] == search)
return i;
return -1;
}