ref: 6b2f2bdd67732bcd7703f2d9edc9c1d9f8ca2483
parent: b4c6a7ee10270801b4e26063ad3a1919e0d0f104
author: qwx <>
date: Wed Jun 20 19:11:41 EDT 2018
cleanse argument parsing crap
--- a/common.c
+++ b/common.c
@@ -5,14 +5,7 @@
#include "fns.h"
cvar_t registered = {"registered","0"};
-cvar_t cmdline = {"cmdline","0", false, true};
-char com_cmdline[256];
-#define NUM_SAFE_ARGVS 6
-
-static char *largv[MAX_NUM_ARGVS + NUM_SAFE_ARGVS + 1];
-static char *argvdummy = " ";
-
qboolean msg_suppress_1 = 0;
char com_token[1024];
@@ -19,10 +12,8 @@
int com_argc;
char **com_argv;
-
qboolean standard_quake = true, rogue, hipnotic;
-
/*
@@ -454,81 +445,6 @@
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(strcmp(parm, com_argv[i]) == 0)
- return i;
- }
-
- return 0;
-}
-
-/*
-================
-COM_InitArgv
-================
-*/
-void COM_InitArgv (int argc, char **argv)
-{
- 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 < (sizeof(com_cmdline) - 1)) && argv[j][i])
- {
- com_cmdline[n++] = argv[j][i++];
- }
-
- if (n < (sizeof(com_cmdline) - 1))
- com_cmdline[n++] = ' ';
- else
- break;
- }
-
- com_cmdline[n] = 0;
-
- for (com_argc=0 ; (com_argc<MAX_NUM_ARGVS) && (com_argc < argc) ;
- com_argc++)
- {
- largv[com_argc] = argv[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;
- }
}
/*
--- a/common.h
+++ b/common.h
@@ -34,10 +34,6 @@
//============================================================================
-#ifndef NULL
-#define NULL ((void *)0)
-#endif
-
#define Q_MAXCHAR ((char)0x7f)
#define Q_MAXSHORT ((short)0x7fff)
#define Q_MAXINT ((int)0x7fffffff)
@@ -90,25 +86,17 @@
extern qboolean com_eof;
char *COM_Parse (char *data);
-
-
extern int com_argc;
extern char **com_argv;
-extern char com_cmdline[];
-int COM_CheckParm (char *parm);
-void COM_InitArgv (int argc, char **argv);
-
char *va(char *format, ...);
// does a varargs printf into a temp buffer
-
//============================================================================
struct cache_user_s;
extern cvar_t registered;
-extern cvar_t cmdline;
extern qboolean standard_quake, rogue, hipnotic;
--- a/cvar.c
+++ b/cvar.c
@@ -19,8 +19,7 @@
for (var=cvar_vars ; var ; var=var->next)
if(strcmp(var_name, var->name) == 0)
return var;
-
- return NULL;
+ return nil;
}
/*
@@ -66,16 +65,13 @@
int len;
len = strlen(partial);
-
if (!len)
- return NULL;
-
+ return nil;
// check functions
for (cvar=cvar_vars ; cvar ; cvar=cvar->next)
if(strncmp(partial, cvar->name, len) == 0)
return cvar->name;
-
- return NULL;
+ return nil;
}
/*
--- a/d_surf.c
+++ b/d_surf.c
@@ -16,12 +16,6 @@
int D_SurfaceCacheForRes (int width, int height)
{
int size, pix;
-
- if (COM_CheckParm ("-surfcachesize"))
- {
- size = atoi(com_argv[COM_CheckParm("-surfcachesize")+1]) * 1024;
- return size;
- }
size = SURFCACHE_SIZE_AT_320X200;
--- a/dat.h
+++ b/dat.h
@@ -1,5 +1,6 @@
typedef struct Sfx Sfx;
+extern char *game;
extern uchar *membase;
extern int memsize;
@@ -20,6 +21,8 @@
extern char savs[][Nsavcm];
extern int savcanld[];
+
+extern int dedicated, listener;
enum{
Fpsmin = 10,
--- a/fs.c
+++ b/fs.c
@@ -845,38 +845,26 @@
static void
initns(void)
{
- int i;
- char d[Nfspath], *e, *home;
+ char *home;
- memset(d, 0, sizeof d);
- i = COM_CheckParm("-basedir");
- if(i != 0 && i < com_argc - 1){
- strncpy(d, com_argv[i+1], sizeof(d)-1);
- e = d + strlen(d) - 1;
- if(e > d && *e == '/')
- *e = 0;
- }else if(home = getenv("home"), home != nil){
- snprint(d, sizeof d, "%s/lib/quake", home);
+ pakdir("/sys/games/lib/quake/id1");
+ if(game != nil){
+ if(strcmp(game, "rogue") == 0){
+ rogue = 1;
+ standard_quake = 0;
+ }else if(strcmp(game, "hipnotic") == 0){
+ hipnotic = 1;
+ standard_quake = 0;
+ }else
+ notid1 = 1;
+ pakdir(va("/sys/games/lib/quake/%s", game));
+ }
+ if((home = getenv("home")) != nil){
+ pakdir(va("%s/lib/quake/id1", home));
+ if(game != nil)
+ pakdir(va("%s/lib/quake/%s", home, game));
free(home);
}
- if(strlen(d) == 0 || access(d, AREAD) < 0){
- strncpy(d, "/sys/games/lib/quake", sizeof(d)-1);
- if(access(d, AREAD) < 0){
- strncpy(d, "/sys/lib/quake", sizeof(d)-1);
- if(access(d, AREAD) < 0)
- fatal("no accessible game directory");
- }
- }
- pakdir(va("%s%s", d, "/id1"));
- if(COM_CheckParm("-rogue"))
- pakdir(va("%s%s", d, "/rogue"));
- if(COM_CheckParm("-hipnotic"))
- pakdir(va("%s%s", d, "/hipnotic"));
- i = COM_CheckParm("-game");
- if(i != 0 && i < com_argc - 1){
- notid1 = 1;
- pakdir(va("%s/%s", d, com_argv[i+1]));
- }
}
static void
@@ -978,6 +966,4 @@
initns();
chkreg();
Cmd_AddCommand("path", path);
- Cvar_RegisterVariable(&cmdline);
- setcvar("cmdline", com_cmdline);
}
--- a/host.c
+++ b/host.c
@@ -14,6 +14,7 @@
Memory is cleared / released when a server or client begins, not when they end.
*/
+int dedicated;
qboolean host_initialized; // true if into command execution
@@ -125,31 +126,12 @@
*/
void Host_FindMaxClients (void)
{
- int i;
-
svs.maxclients = 1;
-
- i = COM_CheckParm ("-dedicated");
- if (i)
- {
- cls.state = ca_dedicated;
- if(i != com_argc-1)
- svs.maxclients = atoi(com_argv[i+1]);
- else
- svs.maxclients = 8;
- }
- else
- cls.state = ca_disconnected;
-
- i = COM_CheckParm ("-listen");
- if (i)
- {
- if (cls.state == ca_dedicated)
- fatal ("Only one of -dedicated or -listen can be specified");
- if (i != (com_argc - 1))
- svs.maxclients = atoi(com_argv[i+1]);
- else
- svs.maxclients = 8;
+ cls.state = ca_disconnected;
+ if(listener){
+ if(dedicated)
+ cls.state = ca_dedicated;
+ svs.maxclients = MAX_SCOREBOARD;
}
if (svs.maxclients < 1)
svs.maxclients = 8;
--- a/net_dgrm.c
+++ b/net_dgrm.c
@@ -746,9 +746,6 @@
myDriverLevel = net_driverlevel;
Cmd_AddCommand ("net_stats", NET_Stats_f);
- if (COM_CheckParm("-nolan"))
- return -1;
-
for (i = 0; i < net_numlandrivers; i++)
{
csock = net_landrivers[i].Init ();
--- a/net_main.c
+++ b/net_main.c
@@ -23,7 +23,7 @@
void (*GetModemConfig) (int portNumber, char *dialType, char *clear, char *init, char *hangup);
void (*SetModemConfig) (int portNumber, char *dialType, char *clear, char *init, char *hangup);
-static qboolean listening = false;
+int listener;
qboolean slistInProgress = false;
qboolean slistSilent = false;
@@ -155,17 +155,17 @@
{
if (Cmd_Argc () != 2)
{
- Con_Printf ("\"listen\" is \"%ud\"\n", listening ? 1 : 0);
+ Con_Printf ("\"listen\" is \"%ud\"\n", listener ? 1 : 0);
return;
}
- listening = atoi(Cmd_Argv(1)) ? true : false;
+ listener = atoi(Cmd_Argv(1)) ? true : false;
for (net_driverlevel=0 ; net_driverlevel<net_numdrivers; net_driverlevel++)
{
if (net_drivers[net_driverlevel].initialized == false)
continue;
- dfunc.Listen (listening);
+ dfunc.Listen (listener);
}
}
@@ -195,10 +195,10 @@
Con_Printf ("\"maxplayers\" set to \"%ud\"\n", n);
}
- if ((n == 1) && listening)
+ if ((n == 1) && listener)
Cbuf_AddText ("listen 0\n");
- if ((n > 1) && (!listening))
+ if ((n > 1) && (!listener))
Cbuf_AddText ("listen 1\n");
svs.maxclients = n;
@@ -229,7 +229,7 @@
DEFAULTnet_hostport = n;
net_hostport = n;
- if (listening)
+ if (listener)
{
// force a change to the new port
Cbuf_AddText ("listen 0\n");
@@ -429,7 +429,7 @@
{
if (net_drivers[net_driverlevel].initialized == false)
continue;
- if (net_driverlevel && listening == false)
+ if (net_driverlevel && listener == false)
continue;
ret = dfunc.CheckNewConnections ();
if(ret)
@@ -689,23 +689,7 @@
int controlSocket;
qsocket_t *s;
- i = COM_CheckParm ("-port");
- if (!i)
- i = COM_CheckParm ("-udpport");
- if (!i)
- i = COM_CheckParm ("-ipxport");
-
- if (i)
- {
- if (i < com_argc-1)
- DEFAULTnet_hostport = atoi(com_argv[i+1]);
- else
- fatal ("NET_Init: you must specify a number after -port");
- }
net_hostport = DEFAULTnet_hostport;
-
- if (COM_CheckParm("-listen") || cls.state == ca_dedicated)
- listening = true;
net_numsockets = svs.maxclientslimit;
if (cls.state != ca_dedicated)
net_numsockets++;
@@ -750,7 +734,7 @@
continue;
net_drivers[net_driverlevel].initialized = true;
net_drivers[net_driverlevel].controlSock = controlSocket;
- if (listening)
+ if (listener)
net_drivers[net_driverlevel].Listen (true);
}
--- a/qk1.c
+++ b/qk1.c
@@ -8,10 +8,11 @@
mainstacksize = 512*1024;
uchar *membase;
int memsize;
+char *game;
enum{
KB = 1024*1024,
- Nmem = 8 * KB
+ Nmem = 12 * KB
};
static int debug;
@@ -104,22 +105,40 @@
noted(NDFLT);
}
+static void
+usage(void)
+{
+ fprint(2, "usage: %s [-dl] [-g game] [-m kB]\n", argv0);
+ exits("usage");
+}
+
void
-threadmain(int c, char **v)
+threadmain(int argc, char **argv)
{
- int n;
double t, t´, Δt;
- /* ignore fp exceptions: rendering shit assumes they are */
- setfcr(getfcr() & ~(FPOVFL|FPUNFL|FPINVAL|FPZDIV));
- notify(croak);
- COM_InitArgv(c, v);
- argv0 = *v;
memsize = Nmem;
- if(n = COM_CheckParm("-mem"))
- memsize = atoi(com_argv[n+1]) * KB;
+ ARGBEGIN{
+ case 'd':
+ dedicated = 1;
+ case 'l':
+ listener = 1;
+ break;
+ case 'g':
+ game = EARGF(usage());
+ break;
+ case 'm':
+ memsize = strtol(EARGF(usage()), nil, 0) * KB;
+ if(memsize <= 0)
+ sysfatal("invalid memsize");
+ break;
+ default: usage();
+ }ARGEND
membase = emalloc(memsize);
srand(getpid());
+ /* ignore fp exceptions: rendering shit assumes they are */
+ setfcr(getfcr() & ~(FPOVFL|FPUNFL|FPINVAL|FPZDIV));
+ notify(croak);
Host_Init();
t = dtime() - 1.0 / Fpsmax;
for(;;){
--- a/r_part.c
+++ b/r_part.c
@@ -24,15 +24,7 @@
void
R_InitParticles(void)
{
- int i;
-
- if((i = COM_CheckParm("-particles")) != 0){
- r_numparticles = atoi(com_argv[i+1]);
- if(r_numparticles < ABSOLUTE_MIN_PARTICLES)
- r_numparticles = ABSOLUTE_MIN_PARTICLES;
- }else
- r_numparticles = MAX_PARTICLES;
-
+ r_numparticles = MAX_PARTICLES;
particles = Hunk_AllocName(r_numparticles * sizeof *particles, "particles");
}
--- a/zone.c
+++ b/zone.c
@@ -848,7 +848,6 @@
void Memory_Init (void)
{
- int p;
int zonesize = DYNAMIC_SIZE;
hunk_low_used = 0;
@@ -855,14 +854,6 @@
hunk_high_used = 0;
Cache_Init ();
- p = COM_CheckParm ("-zone");
- if (p)
- {
- if (p < com_argc-1)
- zonesize = atoi(com_argv[p+1]) * 1024;
- else
- fatal ("Memory_Init: you must specify a size in KB after -zone");
- }
mainzone = Hunk_AllocName (zonesize, "zone" );
Z_ClearZone (mainzone, zonesize);
}