shithub: qk1

Download patch

ref: 9c69d793fb471dbbdff55431cbfdd585ff3e4deb
parent: 604d47937d45ebba3ee3b6053bd796b8513e6a3f
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Mon Nov 6 18:50:55 EST 2023

clean up a bunch; more work

diff: cannot open b/3rd/parg//null: file does not exist: 'b/3rd/parg//null' diff: cannot open b/3rd//null: file does not exist: 'b/3rd//null'
--- /dev/null
+++ b/.gitignore
@@ -1,0 +1,3 @@
+*.[o0125678vqki]
+[o0125678vqki].out
+qk1
--- /dev/null
+++ b/3rd/parg/COPYING
@@ -1,0 +1,113 @@
+CC0 1.0 Universal
+
+Statement of Purpose
+
+The laws of most jurisdictions throughout the world automatically confer
+exclusive Copyright and Related Rights (defined below) upon the creator
+and subsequent owner(s) (each and all, an "owner") of an original work of
+authorship and/or a database (each, a "Work").
+
+Certain owners wish to permanently relinquish those rights to a Work for
+the purpose of contributing to a commons of creative, cultural and
+scientific works ("Commons") that the public can reliably and without fear
+of later claims of infringement build upon, modify, incorporate in other
+works, reuse and redistribute as freely as possible in any form whatsoever
+and for any purposes, including without limitation commercial purposes.
+These owners may contribute to the Commons to promote the ideal of a free
+culture and the further production of creative, cultural and scientific
+works, or to gain reputation or greater distribution for their Work in
+part through the use and efforts of others.
+
+For these and/or other purposes and motivations, and without any
+expectation of additional consideration or compensation, the person
+associating CC0 with a Work (the "Affirmer"), to the extent that he or she
+is an owner of Copyright and Related Rights in the Work, voluntarily
+elects to apply CC0 to the Work and publicly distribute the Work under its
+terms, with knowledge of his or her Copyright and Related Rights in the
+Work and the meaning and intended legal effect of CC0 on those rights.
+
+1. Copyright and Related Rights. A Work made available under CC0 may be
+protected by copyright and related or neighboring rights ("Copyright and
+Related Rights"). Copyright and Related Rights include, but are not
+limited to, the following:
+
+  i. the right to reproduce, adapt, distribute, perform, display,
+     communicate, and translate a Work;
+ ii. moral rights retained by the original author(s) and/or performer(s);
+iii. publicity and privacy rights pertaining to a person's image or
+     likeness depicted in a Work;
+ iv. rights protecting against unfair competition in regards to a Work,
+     subject to the limitations in paragraph 4(a), below;
+  v. rights protecting the extraction, dissemination, use and reuse of data
+     in a Work;
+ vi. database rights (such as those arising under Directive 96/9/EC of the
+     European Parliament and of the Council of 11 March 1996 on the legal
+     protection of databases, and under any national implementation
+     thereof, including any amended or successor version of such
+     directive); and
+vii. other similar, equivalent or corresponding rights throughout the
+     world based on applicable law or treaty, and any national
+     implementations thereof.
+
+2. Waiver. To the greatest extent permitted by, but not in contravention
+of, applicable law, Affirmer hereby overtly, fully, permanently,
+irrevocably and unconditionally waives, abandons, and surrenders all of
+Affirmer's Copyright and Related Rights and associated claims and causes
+of action, whether now known or unknown (including existing as well as
+future claims and causes of action), in the Work (i) in all territories
+worldwide, (ii) for the maximum duration provided by applicable law or
+treaty (including future time extensions), (iii) in any current or future
+medium and for any number of copies, and (iv) for any purpose whatsoever,
+including without limitation commercial, advertising or promotional
+purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
+member of the public at large and to the detriment of Affirmer's heirs and
+successors, fully intending that such Waiver shall not be subject to
+revocation, rescission, cancellation, termination, or any other legal or
+equitable action to disrupt the quiet enjoyment of the Work by the public
+as contemplated by Affirmer's express Statement of Purpose.
+
+3. Public License Fallback. Should any part of the Waiver for any reason
+be judged legally invalid or ineffective under applicable law, then the
+Waiver shall be preserved to the maximum extent permitted taking into
+account Affirmer's express Statement of Purpose. In addition, to the
+extent the Waiver is so judged Affirmer hereby grants to each affected
+person a royalty-free, non transferable, non sublicensable, non exclusive,
+irrevocable and unconditional license to exercise Affirmer's Copyright and
+Related Rights in the Work (i) in all territories worldwide, (ii) for the
+maximum duration provided by applicable law or treaty (including future
+time extensions), (iii) in any current or future medium and for any number
+of copies, and (iv) for any purpose whatsoever, including without
+limitation commercial, advertising or promotional purposes (the
+"License"). The License shall be deemed effective as of the date CC0 was
+applied by Affirmer to the Work. Should any part of the License for any
+reason be judged legally invalid or ineffective under applicable law, such
+partial invalidity or ineffectiveness shall not invalidate the remainder
+of the License, and in such case Affirmer hereby affirms that he or she
+will not (i) exercise any of his or her remaining Copyright and Related
+Rights in the Work or (ii) assert any associated claims and causes of
+action with respect to the Work, in either case contrary to Affirmer's
+express Statement of Purpose.
+
+4. Limitations and Disclaimers.
+
+ a. No trademark or patent rights held by Affirmer are waived, abandoned,
+    surrendered, licensed or otherwise affected by this document.
+ b. Affirmer offers the Work as-is and makes no representations or
+    warranties of any kind concerning the Work, express, implied,
+    statutory or otherwise, including without limitation warranties of
+    title, merchantability, fitness for a particular purpose, non
+    infringement, or the absence of latent or other defects, accuracy, or
+    the present or absence of errors, whether or not discoverable, all to
+    the greatest extent permissible under applicable law.
+ c. Affirmer disclaims responsibility for clearing rights of other persons
+    that may apply to the Work or any use thereof, including without
+    limitation any person's Copyright and Related Rights in the Work.
+    Further, Affirmer disclaims responsibility for obtaining any necessary
+    consents, permissions or other rights required for any use of the
+    Work.
+ d. Affirmer understands and acknowledges that Creative Commons is not a
+    party to this document and has no duty or obligation with respect to
+    this CC0 or use of the Work.
+
+For more information, please see
+<http://creativecommons.org/publicdomain/zero/1.0/>
--- /dev/null
+++ b/3rd/parg/parg.c
@@ -1,0 +1,354 @@
+/*
+ * parg - parse argv
+ *
+ * Written in 2015-2016 by Joergen Ibsen
+ *
+ * To the extent possible under law, the author(s) have dedicated all
+ * copyright and related and neighboring rights to this software to the
+ * public domain worldwide. This software is distributed without any
+ * warranty. <http://creativecommons.org/publicdomain/zero/1.0/>
+ */
+
+#include "parg.h"
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+/*
+ * Check if state is at end of argv.
+ */
+static int
+is_argv_end(const struct parg_state *ps, int argc, char *const argv[])
+{
+	return ps->optind >= argc || argv[ps->optind] == NULL;
+}
+
+/*
+ * Match nextchar against optstring.
+ */
+static int
+match_short(struct parg_state *ps, int argc, char *const argv[],
+            const char *optstring)
+{
+	const char *p = strchr(optstring, *ps->nextchar);
+
+	if (p == NULL) {
+		ps->optopt = *ps->nextchar++;
+		return '?';
+	}
+
+	/* If no option argument, return option */
+	if (p[1] != ':') {
+		return *ps->nextchar++;
+	}
+
+	/* If more characters, return as option argument */
+	if (ps->nextchar[1] != '\0') {
+		ps->optarg = &ps->nextchar[1];
+		ps->nextchar = NULL;
+		return *p;
+	}
+
+	/* If option argument is optional, return option */
+	if (p[2] == ':') {
+		return *ps->nextchar++;
+	}
+
+	/* Option argument required, so return next argv element */
+	if (is_argv_end(ps, argc, argv)) {
+		ps->optopt = *ps->nextchar++;
+		return optstring[0] == ':' ? ':' : '?';
+	}
+
+	ps->optarg = argv[ps->optind++];
+	ps->nextchar = NULL;
+	return *p;
+}
+
+/*
+ * Match string at nextchar against longopts.
+ */
+static int
+match_long(struct parg_state *ps, int argc, char *const argv[],
+           const char *optstring,
+           const struct parg_option *longopts, int *longindex)
+{
+	size_t len;
+	int num_match = 0;
+	int match = -1;
+	int i;
+
+	len = strcspn(ps->nextchar, "=");
+
+	for (i = 0; longopts[i].name; ++i) {
+		if (strncmp(ps->nextchar, longopts[i].name, len) == 0) {
+			match = i;
+			num_match++;
+			/* Take if exact match */
+			if (longopts[i].name[len] == '\0') {
+				num_match = 1;
+				break;
+			}
+		}
+	}
+
+	/* Return '?' on no or ambiguous match */
+	if (num_match != 1) {
+		ps->optopt = 0;
+		ps->nextchar = NULL;
+		return '?';
+	}
+
+	assert(match != -1);
+
+	if (longindex) {
+		*longindex = match;
+	}
+
+	if (ps->nextchar[len] == '=') {
+		/* Option argument present, check if extraneous */
+		if (longopts[match].has_arg == PARG_NOARG) {
+			ps->optopt = longopts[match].flag ? 0 : longopts[match].val;
+			ps->nextchar = NULL;
+			return optstring[0] == ':' ? ':' : '?';
+		}
+		else {
+			ps->optarg = &ps->nextchar[len + 1];
+		}
+	}
+	else if (longopts[match].has_arg == PARG_REQARG) {
+		/* Option argument required, so return next argv element */
+		if (is_argv_end(ps, argc, argv)) {
+			ps->optopt = longopts[match].flag ? 0 : longopts[match].val;
+			ps->nextchar = NULL;
+			return optstring[0] == ':' ? ':' : '?';
+		}
+
+		ps->optarg = argv[ps->optind++];
+	}
+
+	ps->nextchar = NULL;
+
+	if (longopts[match].flag != NULL) {
+		*longopts[match].flag = longopts[match].val;
+		return 0;
+	}
+
+	return longopts[match].val;
+}
+
+void
+parg_init(struct parg_state *ps)
+{
+	ps->optarg = NULL;
+	ps->optind = 1;
+	ps->optopt = '?';
+	ps->nextchar = NULL;
+}
+
+int
+parg_getopt(struct parg_state *ps, int argc, char *const argv[],
+            const char *optstring)
+{
+	return parg_getopt_long(ps, argc, argv, optstring, NULL, NULL);
+}
+
+int
+parg_getopt_long(struct parg_state *ps, int argc, char *const argv[],
+                 const char *optstring,
+                 const struct parg_option *longopts, int *longindex)
+{
+	assert(ps != NULL);
+	assert(argv != NULL);
+	assert(optstring != NULL);
+
+	ps->optarg = NULL;
+
+	if (argc < 2) {
+		return -1;
+	}
+
+	/* Advance to next element if needed */
+	if (ps->nextchar == NULL || *ps->nextchar == '\0') {
+		if (is_argv_end(ps, argc, argv)) {
+			return -1;
+		}
+
+		ps->nextchar = argv[ps->optind++];
+
+		/* Check for nonoption element (including '-') */
+		if (ps->nextchar[0] != '-' || ps->nextchar[1] == '\0') {
+			ps->optarg = ps->nextchar;
+			ps->nextchar = NULL;
+			return 1;
+		}
+
+		/* Check for '--' */
+		if (ps->nextchar[1] == '-') {
+			if (ps->nextchar[2] == '\0') {
+				ps->nextchar = NULL;
+				return -1;
+			}
+
+			if (longopts != NULL) {
+				ps->nextchar += 2;
+
+				return match_long(ps, argc, argv, optstring,
+				                  longopts, longindex);
+			}
+		}
+
+		ps->nextchar++;
+	}
+
+	/* Match nextchar */
+	return match_short(ps, argc, argv, optstring);
+}
+
+/*
+ * Reverse elements of `v` from `i` to `j`.
+ */
+static void
+reverse(char *v[], int i, int j)
+{
+	while (j - i > 1) {
+		char *tmp = v[i];
+		v[i] = v[j - 1];
+		v[j - 1] = tmp;
+		++i;
+		--j;
+	}
+}
+
+/*
+ * Reorder elements of `argv` with no special cases.
+ *
+ * This function assumes there is no `--` element, and the last element
+ * is not an option missing a required argument.
+ *
+ * The algorithm is described here:
+ * http://hardtoc.com/2016/11/07/reordering-arguments.html
+ */
+static int
+parg_reorder_simple(int argc, char *argv[],
+                    const char *optstring,
+                    const struct parg_option *longopts)
+{
+	struct parg_state ps;
+	int change;
+	int l = 0;
+	int m = 0;
+	int r = 0;
+
+	if (argc < 2) {
+		return argc;
+	}
+
+	do {
+		int nextind;
+		int c;
+
+		parg_init(&ps);
+
+		nextind = ps.optind;
+
+		/* Parse until end of argument */
+		do {
+			c = parg_getopt_long(&ps, argc, argv, optstring, longopts, NULL);
+		} while (ps.nextchar != NULL && *ps.nextchar != '\0');
+
+		change = 0;
+
+		do {
+			/* Find next non-option */
+			for (l = nextind; c != 1 && c != -1;) {
+				l = ps.optind;
+
+				do {
+					c = parg_getopt_long(&ps, argc, argv, optstring, longopts, NULL);
+				} while (ps.nextchar != NULL && *ps.nextchar != '\0');
+			}
+
+			/* Find next option */
+			for (m = l; c == 1;) {
+				m = ps.optind;
+
+				do {
+					c = parg_getopt_long(&ps, argc, argv, optstring, longopts, NULL);
+				} while (ps.nextchar != NULL && *ps.nextchar != '\0');
+			}
+
+			/* Find next non-option */
+			for (r = m; c != 1 && c != -1;) {
+				r = ps.optind;
+
+				do {
+					c = parg_getopt_long(&ps, argc, argv, optstring, longopts, NULL);
+				} while (ps.nextchar != NULL && *ps.nextchar != '\0');
+			}
+
+			/* Find next option */
+			for (nextind = r; c == 1;) {
+				nextind = ps.optind;
+
+				do {
+					c = parg_getopt_long(&ps, argc, argv, optstring, longopts, NULL);
+				} while (ps.nextchar != NULL && *ps.nextchar != '\0');
+			}
+
+			if (m < r) {
+				change = 1;
+				reverse(argv, l, m);
+				reverse(argv, m, r);
+				reverse(argv, l, r);
+			}
+		} while (c != -1);
+	} while (change != 0);
+
+	return l + (r - m);
+}
+
+int
+parg_reorder(int argc, char *argv[],
+             const char *optstring,
+             const struct parg_option *longopts)
+{
+	struct parg_state ps;
+	int lastind;
+	int optend;
+	int c;
+
+	assert(argv != NULL);
+	assert(optstring != NULL);
+
+	if (argc < 2) {
+		return argc;
+	}
+
+	parg_init(&ps);
+
+	/* Find end of normal arguments */
+	do {
+		lastind = ps.optind;
+
+		c = parg_getopt_long(&ps, argc, argv, optstring, longopts, NULL);
+
+		/* Check for trailing option with error */
+		if ((c == '?' || c == ':') && is_argv_end(&ps, argc, argv)) {
+			lastind = ps.optind - 1;
+			break;
+		}
+	} while (c != -1);
+
+	optend = parg_reorder_simple(lastind, argv, optstring, longopts);
+
+	/* Rotate `--` or trailing option with error into position */
+	if (lastind < argc) {
+		reverse(argv, optend, lastind);
+		reverse(argv, optend, lastind + 1);
+		++optend;
+	}
+
+	return optend;
+}
--- /dev/null
+++ b/3rd/parg/parg.h
@@ -1,0 +1,192 @@
+/*
+ * parg - parse argv
+ *
+ * Written in 2015-2016 by Joergen Ibsen
+ *
+ * To the extent possible under law, the author(s) have dedicated all
+ * copyright and related and neighboring rights to this software to the
+ * public domain worldwide. This software is distributed without any
+ * warranty. <http://creativecommons.org/publicdomain/zero/1.0/>
+ */
+
+#ifndef PARG_H_INCLUDED
+#define PARG_H_INCLUDED
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define PARG_VER_MAJOR 1        /**< Major version number */
+#define PARG_VER_MINOR 0        /**< Minor version number */
+#define PARG_VER_PATCH 2        /**< Patch version number */
+#define PARG_VER_STRING "1.0.2" /**< Version number as a string */
+
+/**
+ * Structure containing state between calls to parser.
+ *
+ * @see parg_init
+ */
+struct parg_state {
+	const char *optarg;   /**< Pointer to option argument, if any */
+	int optind;           /**< Next index in argv to process */
+	int optopt;           /**< Option value resulting in error, if any */
+	const char *nextchar; /**< Next character to process */
+};
+
+/**
+ * Structure for supplying long options to `parg_getopt_long()`.
+ *
+ * @see parg_getopt_long
+ */
+struct parg_option {
+	const char *name; /**< Name of option */
+	int has_arg;      /**< Option argument status */
+	int *flag;        /**< Pointer to flag variable */
+	int val;          /**< Value of option */
+};
+
+/**
+ * Values for `has_arg` flag in `parg_option`.
+ *
+ * @see parg_option
+ */
+typedef enum {
+	PARG_NOARG,  /**< No argument */
+	PARG_REQARG, /**< Required argument */
+	PARG_OPTARG  /**< Optional argument */
+} parg_arg_num;
+
+/**
+ * Initialize `ps`.
+ *
+ * Must be called before using state with a parser.
+ *
+ * @see parg_state
+ *
+ * @param ps pointer to state
+ */
+void
+parg_init(struct parg_state *ps);
+
+/**
+ * Parse next short option in `argv`.
+ *
+ * Elements in `argv` that contain short options start with a single dash
+ * followed by one or more option characters, and optionally an option
+ * argument for the last option character. Examples are '`-d`', '`-ofile`',
+ * and '`-dofile`'.
+ *
+ * Consecutive calls to this function match the command-line arguments in
+ * `argv` against the short option characters in `optstring`.
+ *
+ * If an option character in `optstring` is followed by a colon, '`:`', the
+ * option requires an argument. If it is followed by two colons, the option
+ * may take an optional argument.
+ *
+ * If a match is found, `optarg` points to the option argument, if any, and
+ * the value of the option character is returned.
+ *
+ * If a match is found, but is missing a required option argument, `optopt`
+ * is set to the option character. If the first character in `optstring` is
+ * '`:`', then '`:`' is returned, otherwise '`?`' is returned.
+ *
+ * If no option character in `optstring` matches a short option, `optopt`
+ * is set to the option character, and '`?`' is returned.
+ *
+ * If an element of argv does not contain options (a nonoption element),
+ * `optarg` points to the element, and `1` is returned.
+ *
+ * An element consisting of a single dash, '`-`', is returned as a nonoption.
+ *
+ * Parsing stops and `-1` is returned, when the end of `argv` is reached, or
+ * if an element contains '`--`'.
+ *
+ * Works similarly to `getopt`, if `optstring` were prefixed by '`-`'.
+ *
+ * @param ps pointer to state
+ * @param argc number of elements in `argv`
+ * @param argv array of pointers to command-line arguments
+ * @param optstring string containing option characters
+ * @return option value on match, `1` on nonoption element, `-1` on end of
+ * arguments, '`?`' on unmatched option, '`?`' or '`:`' on option argument
+ * error
+ */
+int
+parg_getopt(struct parg_state *ps, int argc, char *const argv[],
+            const char *optstring);
+
+/**
+ * Parse next long or short option in `argv`.
+ *
+ * Elements in `argv` that contain a long option start with two dashes
+ * followed by a string, and optionally an equal sign and an option argument.
+ * Examples are '`--help`' and '`--size=5`'.
+ *
+ * If no exact match is found, an unambiguous prefix of a long option will
+ * match. For example, if '`foo`' and '`foobar`' are valid long options, then
+ * '`--fo`' is ambiguous and will not match, '`--foo`' matches exactly, and
+ * '`--foob`' is an unambiguous prefix and will match.
+ *
+ * If a long option match is found, and `flag` is `NULL`, `val` is returned.
+ *
+ * If a long option match is found, and `flag` is not `NULL`, `val` is stored
+ * in the variable `flag` points to, and `0` is returned.
+ *
+ * If a long option match is found, but is missing a required option argument,
+ * or has an option argument even though it takes none, `optopt` is set to
+ * `val` if `flag` is `NULL`, and `0` otherwise. If the first character in
+ * `optstring` is '`:`', then '`:`' is returned, otherwise '`?`' is returned.
+ *
+ * If `longindex` is not `NULL`, the index of the entry in `longopts` that
+ * matched is stored there.
+ *
+ * If no long option in `longopts` matches a long option, '`?`' is returned.
+ *
+ * Handling of nonoptions and short options is like `parg_getopt()`.
+ *
+ * If no short options are required, an empty string, `""`, should be passed
+ * as `optstring`.
+ *
+ * Works similarly to `getopt_long`, if `optstring` were prefixed by '`-`'.
+ *
+ * @see parg_getopt
+ *
+ * @param ps pointer to state
+ * @param argc number of elements in `argv`
+ * @param argv array of pointers to command-line arguments
+ * @param optstring string containing option characters
+ * @param longopts array of `parg_option` structures
+ * @param longindex pointer to variable to store index of matching option in
+ * @return option value on match, `0` for flag option, `1` on nonoption
+ * element, `-1` on end of arguments, '`?`' on unmatched or ambiguous option,
+ * '`?`' or '`:`' on option argument error
+ */
+int
+parg_getopt_long(struct parg_state *ps, int argc, char *const argv[],
+                 const char *optstring,
+                 const struct parg_option *longopts, int *longindex);
+
+/**
+ * Reorder elements of `argv` so options appear first.
+ *
+ * If there are no long options, `longopts` may be `NULL`.
+ *
+ * The return value can be used as `argc` parameter for `parg_getopt()` and
+ * `parg_getopt_long()`.
+ *
+ * @param argc number of elements in `argv`
+ * @param argv array of pointers to command-line arguments
+ * @param optstring string containing option characters
+ * @param longopts array of `parg_option` structures
+ * @return index of first nonoption in `argv` on success, `-1` on error
+ */
+int
+parg_reorder(int argc, char *argv[],
+             const char *optstring,
+             const struct parg_option *longopts);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* PARG_H_INCLUDED */
--- a/Makefile
+++ b/Makefile
@@ -5,12 +5,13 @@
 MAN=${DESTDIR}${PREFIX}/share/man/man1
 SDL2_CFLAGS=$$(pkg-config --cflags sdl2)
 SDL2_LDFLAGS=$$(pkg-config --libs sdl2)
-CFLAGS?=-O2 -pipe -g -Wall
-CFLAGS+=-fms-extensions -Iunix -I. ${SDL2_CFLAGS}
+CFLAGS?=-O2 -g -Wall -Wextra -Wno-unknown-pragmas -Wno-missing-field-initializers -Wno-implicit-fallthrough
+CFLAGS+=-fms-extensions -I3rd/parg -Iunix -I. ${SDL2_CFLAGS}
 LDFLAGS?=
 LDFLAGS+=-lm ${SDL2_LDFLAGS}
 
 OBJS=\
+	3rd/parg/parg.o\
 	chase.o\
 	cl_demo.o\
 	cl_input.o\
@@ -62,7 +63,6 @@
 	r_sky.o\
 	r_sprite.o\
 	r_surf.o\
-	r_vars.o\
 	sbar.o\
 	screen.o\
 	span.o\
--- a/bspfile.h
+++ b/bspfile.h
@@ -306,7 +306,6 @@
 extern	int			dsurfedges[MAX_MAP_SURFEDGES];
 
 
-void DecompressVis (byte *in, byte *decompressed);
 int CompressVis (byte *vis, byte *dest);
 
 void	LoadBSPFile (char *filename);
--- a/cd.c
+++ b/cd.c
@@ -1,10 +1,6 @@
-#include <u.h>
-#include <libc.h>
+#include "quakedef.h"
 #include <thread.h>
 #include <regexp.h>
-#include "dat.h"
-#include "quakedef.h"
-#include "fns.h"
 
 cvar_t bgmvolume = {"bgmvolume", "1", 1};
 
--- a/chase.c
+++ b/chase.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 cvar_t	chase_back = {"chase_back", "100"};
 cvar_t	chase_up = {"chase_up", "16"};
--- a/cl_demo.c
+++ b/cl_demo.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 static void
 timedm(void)
--- a/cl_input.c
+++ b/cl_input.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 /*
 ===============================================================================
--- a/cl_main.c
+++ b/cl_main.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 // we need to declare some mouse variables here, because the menu system
 // references them even when on a unix system.
--- a/cl_parse.c
+++ b/cl_parse.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 static char *svc_strings[] =
 {
--- a/cl_tent.c
+++ b/cl_tent.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 static int num_temp_entities;
 static entity_t cl_temp_entities[MAX_TEMP_ENTITIES];
--- a/cmd.c
+++ b/cmd.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 void Cmd_ForwardToServer (void);
 
--- a/common.c
+++ b/common.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 cvar_t  registered = {"registered","0"};
 
--- a/common.h
+++ b/common.h
@@ -115,6 +115,7 @@
 extern	int		com_argc;
 extern	char	**com_argv;
 
+#pragma varargck	argpos	va	1
 char	*va(char *format, ...);
 // does a varargs printf into a temp buffer
 
@@ -125,7 +126,3 @@
 extern cvar_t registered;
 
 extern qboolean		standard_quake, rogue, hipnotic;
-
-#ifdef __plan9__
-#pragma varargck	argpos	va	1
-#endif
--- a/console.c
+++ b/console.c
@@ -1,9 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include <stdio.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 static int con_linewidth;
 
--- a/console.h
+++ b/console.h
@@ -13,14 +13,11 @@
 void Con_CheckResize (void);
 void Con_Init (void);
 void Con_DrawConsole (int lines, qboolean drawinput);
+#pragma varargck	argpos	Con_Printf	1
 void Con_Printf (char *fmt, ...);
+#pragma varargck	argpos	Con_DPrintf	1
 void Con_DPrintf (char *fmt, ...);
 void Con_Clear_f (void);
 void Con_DrawNotify (void);
 void Con_ClearNotify (void);
 void Con_ToggleConsole_f (void);
-
-#ifdef __plan9__
-#pragma varargck	argpos	Con_Printf	1
-#pragma varargck	argpos	Con_DPrintf	1
-#endif
--- a/cvar.c
+++ b/cvar.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 cvar_t	*cvar_vars;
 char	*cvar_null_string = "";
--- a/d_alpha.c
+++ b/d_alpha.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 typedef struct Col Col;
 
--- a/d_edge.c
+++ b/d_edge.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 static int	miplevel;
 
--- a/d_fill.c
+++ b/d_fill.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 /*
 ================
--- a/d_init.c
+++ b/d_init.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 #define NUM_MIPS	4
 
--- a/d_modech.c
+++ b/d_modech.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 int	d_vrectx, d_vrecty, d_vrectright_particle, d_vrectbottom_particle;
 
--- a/d_part.c
+++ b/d_part.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 /*
 ==============
--- a/d_polyse.c
+++ b/d_polyse.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 // TODO: put in span spilling to shrink list size
 // !!! if this is changed, it must be changed in d_polysa.s too !!!
--- a/d_scan.c
+++ b/d_scan.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 unsigned char	*r_turb_pbase, *r_turb_pdest;
 fixed16_t		r_turb_s, r_turb_t, r_turb_sstep, r_turb_tstep;
--- a/d_sky.c
+++ b/d_sky.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 #define SKY_SPAN_SHIFT	5
 #define SKY_SPAN_MAX	(1 << SKY_SPAN_SHIFT)
--- a/d_sprite.c
+++ b/d_sprite.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 static int		sprite_height;
 static int		minindex, maxindex;
--- a/d_surf.c
+++ b/d_surf.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 float           surfscale;
 qboolean        r_cache_thrash;         // set if surface cache is thrashing
--- a/d_vars.c
+++ b/d_vars.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 // all global and static refresh variables are collected in a contiguous block
 // to avoid cache conflicts.
--- a/d_zpoint.c
+++ b/d_zpoint.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 /*
 =====================
--- a/dat.h
+++ b/dat.h
@@ -1,7 +1,3 @@
-typedef struct Sfx Sfx;
-
-extern char *game;
-
 enum{
 	Npath = 128,
 	Nfspath = 128,
@@ -14,6 +10,16 @@
 
 	Udpport = 26000,
 };
+
+typedef struct Sfx Sfx;
+
+struct Sfx {
+	char s[Npath];
+	int map;
+	mem_user_t cu;
+};
+
+extern char *game;
 
 extern char fsdir[];
 extern u16int crcn;
--- a/dotproduct.c
+++ b/dotproduct.c
@@ -1,5 +1,7 @@
+#include "quakedef.h"
+
 float
-DotProduct(const float v1[3], const float v2[3])
+DotProduct(const vec3_t v1, const vec3_t v2)
 {
 	return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
 }
--- a/draw.c
+++ b/draw.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 typedef struct {
 	vrect_t	rect;
--- a/fns.h
+++ b/fns.h
@@ -48,6 +48,7 @@
 int	loaddm(char*);
 int	opendm(char*, int);
 void	initfs(void);
+#pragma varargck	argpos	fatal	1
 void	fatal(char*, ...);
 void*	emalloc(ulong);
 vlong	flen(int);
@@ -54,7 +55,3 @@
 double	dtime(void);
 void	game_shutdown(void);
 uvlong	nanosec(void);
-
-#ifdef __plan9__
-#pragma varargck	argpos	fatal	1
-#endif
--- a/fs.c
+++ b/fs.c
@@ -1,9 +1,5 @@
-#include <u.h>
-#include <libc.h>
-#include <bio.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
+#include <bio.h>
 
 u16int crcn;
 char fsdir[Nfspath];
--- a/host.c
+++ b/host.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 /*
 
--- a/host_cmd.c
+++ b/host_cmd.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 int	current_skill;
 
@@ -357,7 +353,7 @@
 */
 void Host_Name_f (void)
 {
-	char	*newName;
+	char	newName[16];
 
 	if (Cmd_Argc () == 1)
 	{
@@ -365,10 +361,10 @@
 		return;
 	}
 	if (Cmd_Argc () == 2)
-		newName = Cmd_Argv(1);	
+		strncpy(newName, Cmd_Argv(1), sizeof(newName));
 	else
-		newName = Cmd_Args();
-	newName[15] = 0;
+		strncpy(newName, Cmd_Args(), sizeof(newName));
+	newName[sizeof(newName)-1] = 0;
 
 	if (cmd_source == src_command)
 	{
--- a/in.c
+++ b/in.c
@@ -1,13 +1,9 @@
-#include <u.h>
-#include <libc.h>
+#include "quakedef.h"
 #include <bio.h>
 #include <draw.h>
 #include <thread.h>
 #include <mouse.h>
 #include <keyboard.h>
-#include "dat.h"
-#include "quakedef.h"
-#include "fns.h"
 
 /* vid.c */
 extern int resized;
--- /dev/null
+++ b/isnanf.c
@@ -1,0 +1,13 @@
+#include <u.h>
+
+int
+isnanf(float f)
+{
+	union {
+		float f;
+		u32int u;
+	}x;
+
+	x.f = f;
+	return (x.u & (0xff<<23)) == (0xff<<23);
+}
--- a/keys.c
+++ b/keys.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 /*
 
@@ -191,8 +187,8 @@
 				&& !key_lines[history_line][1]);
 		if (history_line == edit_line)
 			history_line = (edit_line+1)&31;
-		strcpy(key_lines[edit_line], key_lines[history_line]);
-		key_linepos = strlen(key_lines[edit_line]);
+		key_linepos = strlen(key_lines[history_line]);
+		memmove(key_lines[edit_line], key_lines[history_line], key_linepos+1);
 		return;
 	}
 
@@ -212,8 +208,8 @@
 		}
 		else
 		{
-			strcpy(key_lines[edit_line], key_lines[history_line]);
-			key_linepos = strlen(key_lines[edit_line]);
+			key_linepos = strlen(key_lines[history_line]);
+			memmove(key_lines[edit_line], key_lines[history_line], key_linepos+1);
 		}
 		return;
 	}
--- a/mathlib.c
+++ b/mathlib.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 vec3_t vec3_origin = {0,0,0};
 
--- a/mathlib.h
+++ b/mathlib.h
@@ -12,6 +12,8 @@
 #define M_PI		3.14159265358979323846264338327950288
 #endif
 
+int isnanf(float f);
+
 struct mplane_s;
 
 extern vec3_t vec3_origin;
--- a/menu.c
+++ b/menu.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 char savs[Nsav][Nsavcm];
 int savcanld[Nsav];
--- a/mkfile
+++ b/mkfile
@@ -38,6 +38,7 @@
 	host.$O\
 	host_cmd.$O\
 	in.$O\
+	isnanf.$O\
 	keys.$O\
 	mathlib.$O\
 	menu.$O\
@@ -65,7 +66,6 @@
 	r_sky.$O\
 	r_sprite.$O\
 	r_surf.$O\
-	r_vars.$O\
 	screen.$O\
 	sbar.$O\
 	snd.$O\
--- a/model.c
+++ b/model.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 model_t	*loadmodel;
 char	loadname[32];	// for hunk tags
@@ -83,7 +79,7 @@
 Mod_DecompressVis
 ===================
 */
-byte *Mod_DecompressVis (byte *in, model_t *model)
+static byte *Mod_DecompressVis (byte *in, model_t *model, int *outsz)
 {
 	static byte	*decompressed;
 	static int decompressed_size;
@@ -91,12 +87,13 @@
 	byte	*out;
 	int		row;
 
-	row = (model->numleafs+7)/8;	
+	row = (model->numleafs+7)/8;
 	if(decompressed == nil || row > decompressed_size){
 		decompressed_size = row;
 		decompressed = realloc(decompressed, decompressed_size);
 	}
 	out = decompressed;
+	*outsz = row;
 
 	if(!in){ // no vis info, so make all visible
 		memset(out, 0xff, row);
@@ -120,13 +117,14 @@
 	return decompressed;
 }
 
-byte *Mod_LeafPVS (mleaf_t *leaf, model_t *model)
+byte *Mod_LeafPVS (mleaf_t *leaf, model_t *model, int *outsz)
 {
 	static byte *mod_novis;
 	static int mod_novis_size;
 	int sz;
 
-	sz = (model->numleafs+7)/8;
+	sz = ((model->numleafs+7)/8 + 3) & ~3;
+	*outsz = sz;
 	if (leaf == model->leafs) {
 		if(mod_novis == nil || mod_novis_size < sz){
 			mod_novis = realloc(mod_novis, sz);
@@ -135,7 +133,7 @@
 		memset(mod_novis, 0xff, mod_novis_size);
 		return mod_novis;
 	}
-	return Mod_DecompressVis (leaf->compressed_vis, model);
+	return Mod_DecompressVis (leaf->compressed_vis, model, outsz);
 }
 
 /*
--- a/model.h
+++ b/model.h
@@ -365,5 +365,5 @@
 void	Mod_TouchModel (char *name);
 
 mleaf_t *Mod_PointInLeaf (vec3_t p, model_t *model);
-byte	*Mod_LeafPVS (mleaf_t *leaf, model_t *model);
+byte	*Mod_LeafPVS (mleaf_t *leaf, model_t *model, int *sz);
 void	Mod_Print(void);
--- a/net_dgrm.c
+++ b/net_dgrm.c
@@ -1,9 +1,5 @@
-#include <u.h>
-#include <libc.h>
-#include <ip.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
+#include <ip.h>
 
 //#define DEBUG
 
--- a/net_loop.c
+++ b/net_loop.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 qboolean	localconnectpending = false;
 qsocket_t	*loop_client = nil;
--- a/net_main.c
+++ b/net_main.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 qsocket_t	*net_activeSockets = nil;
 qsocket_t	*net_freeSockets = nil;
--- a/net_udp.c
+++ b/net_udp.c
@@ -1,10 +1,6 @@
-#include <u.h>
-#include <libc.h>
+#include "quakedef.h"
 #include <ip.h>
 #include <thread.h>
-#include "dat.h"
-#include "quakedef.h"
-#include "fns.h"
 
 Addr myip;
 
--- a/pal.c
+++ b/pal.c
@@ -1,4 +1,4 @@
-#include <u.h>
+#include "quakedef.h"
 
 void
 pal2xrgb(int n, s32int *pal, u8int *s, u32int *d)
--- a/pr_cmds.c
+++ b/pr_cmds.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 #define	RETURN_EDICT(e) (((int *)pr_globals)[OFS_RETURN] = EDICT_TO_PROG(e))
 
@@ -684,8 +680,7 @@
 // get the PVS for the entity
 	VectorAdd (ent->v.origin, ent->v.view_ofs, org);
 	leaf = Mod_PointInLeaf (org, sv.worldmodel);
-	pvs = Mod_LeafPVS (leaf, sv.worldmodel);
-	size = (sv.worldmodel->numleafs+7)>>3;
+	pvs = Mod_LeafPVS (leaf, sv.worldmodel, &size);
 	if(checkpvs == nil || size > checkpvs_size){
 		checkpvs = realloc(checkpvs, size);
 		checkpvs_size = size;
--- a/pr_edict.c
+++ b/pr_edict.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 dprograms_t		*progs;
 dfunction_t		*pr_functions;
@@ -704,12 +700,11 @@
 */
 qboolean	ED_ParseEpair (void *base, ddef_t *key, char *s)
 {
-	int		i;
-	char	string[128];
 	ddef_t	*def;
 	char	*v, *w;
 	void	*d;
 	dfunction_t	*func;
+	int		i;
 	
 	d = (void *)((int *)base + key->ofs);
 	
@@ -716,25 +711,25 @@
 	switch (key->type & ~DEF_SAVEGLOBAL)
 	{
 	case ev_string:
-		*(string_t *)d = ED_NewString (s);
+		*(string_t *)d = ED_NewString(s);
 		break;
 		
 	case ev_float:
-		*(float *)d = atof (s);
+		*(float *)d = atof(s);
 		break;
 		
 	case ev_vector:
-		strcpy (string, s);
-		v = string;
-		w = string;
-		for (i=0 ; i<3 ; i++)
-		{
-			while (*v && *v != ' ')
+		memset(d, 0, sizeof(float)*3);
+		w = s;
+		for(i = 0; i < 3; i++, w = v){
+			((float*)d)[i] = strtod(w, &v);
+			if(w == v)
+				break;
+			while(*v == ',' || *v == ' ')
 				v++;
-			*v = 0;
-			((float *)d)[i] = atof (w);
-			w = v = v+1;
 		}
+		for(; i < 3; i++)
+			((float*)d)[i] = 0;
 		break;
 		
 	case ev_entity:
--- a/pr_exec.c
+++ b/pr_exec.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 typedef struct
 {
--- a/progs.h
+++ b/progs.h
@@ -108,6 +108,7 @@
 extern	dfunction_t	*pr_xfunction;
 extern	int			pr_xstatement;
 
+#pragma varargck	argpos	PR_RunError	1
 void PR_RunError (char *error, ...);
 
 void ED_PrintEdicts (void);
@@ -119,7 +120,3 @@
 void M_Keydown (int key);
 void M_ToggleMenu_f (void);
 void M_Draw (void);
-
-#ifdef __plan9__
-#pragma varargck	argpos	PR_RunError	1
-#endif
--- a/protocol.c
+++ b/protocol.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 static void
 MSG_WriteProtocolInfoNQ(sizebuf_t *sb, protocol_t *proto)
--- a/qk1.c
+++ b/qk1.c
@@ -1,9 +1,5 @@
-#include <u.h>
-#include <libc.h>
-#include <thread.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
+#include <thread.h>
 
 int mainstacksize = 1*1024*1024;
 char *netmtpt = "/net";
--- a/quakedef.h
+++ b/quakedef.h
@@ -1,11 +1,14 @@
+#pragma once
+
+#include <u.h>
+#include <libc.h>
+#include <stdio.h>
+
 #define	QUAKE_GAME			// as opposed to utilities
 #define	VERSION				1.09
 //#define	PARANOID			// speed sapping error checking
 #define	GAMENAME	"id1"		// directory to look in by default
 
-#define	VID_LockBuffer()
-#define	VID_UnlockBuffer()
-
 enum {
 	// !!! if this is changed, it must be changed in d_ifacea.h too !!!
 	CACHE_SIZE = 32, // used to align key data structures
@@ -113,10 +116,12 @@
 
 #include "cvar.h"
 #include "common.h"
-#include "bspfile.h"
-#include "vid.h"
 #include "zone.h"
+#include "dat.h"
 #include "mathlib.h"
+#include "fns.h"
+#include "bspfile.h"
+#include "vid.h"
 
 typedef struct
 {
@@ -138,12 +143,6 @@
 #include "cmd.h"
 #include "sbar.h"
 
-struct Sfx{
-	char s[Npath];
-	int map;
-	mem_user_t cu;
-};
-
 extern cvar_t bgmvolume;
 extern cvar_t volume;
 
@@ -207,8 +206,6 @@
 void Chase_Reset (void);
 void Chase_Update (void);
 
-#ifdef __plan9__
 #pragma varargck	argpos	Host_Error	1
 #pragma varargck	argpos	Host_EndGame	1
 #pragma varargck	argpos	Host_ClientCommands	1
-#endif
--- a/r_aclip.c
+++ b/r_aclip.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 static finalvert_t		fv[2][8];
 static auxvert_t		av[8];
@@ -215,6 +211,8 @@
 	int				i, k, pingpong;
 	mtriangle_t		mtri;
 	unsigned		clipflags;
+
+	memset(fv, 0, sizeof(fv));
 
 // copy vertexes and fix seam texture coordinates
 	if (ptri->facesfront)
--- a/r_alias.c
+++ b/r_alias.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 #define LIGHT_MIN	5		// lowest light value we'll allow, to avoid the
 							//  need for inner-loop light clamping
--- a/r_bsp.c
+++ b/r_bsp.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 //
 // current entity info
--- a/r_draw.c
+++ b/r_draw.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 #define MAXLEFTCLIPEDGES		100
 
--- a/r_edge.c
+++ b/r_edge.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 /* FIXME
 the complex cases add new polys on most lines, so dont optimize for keeping them the same
@@ -339,9 +335,6 @@
 // start edge yet)
 	if (--surf->spanstate == 0)
 	{
-		if (surf->insubmodel)
-			r_bmodelactive--;
-
 		if (surf == surfaces[1].next)
 		{
 		// emit a span (current top going away)
@@ -388,9 +381,6 @@
 	// end edge)
 		if (++surf->spanstate == 1)
 		{
-			if (surf->insubmodel)
-				r_bmodelactive++;
-
 			surf2 = surfaces[1].next;
 
 			if (surf->key < surf2->key)
@@ -503,8 +493,6 @@
 {
 	edge_t			*edge;
 	surf_t			*surf;
-
-	r_bmodelactive = 0;
 
 // clear active surfaces to just the background surface
 	surfaces[1].next = surfaces[1].prev = &surfaces[1];
--- a/r_efrag.c
+++ b/r_efrag.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 mnode_t	*r_pefragtopnode;
 
--- a/r_light.c
+++ b/r_light.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 int	r_dlightframecount;
 
--- a/r_main.c
+++ b/r_main.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 //define	PASSAGES
 
@@ -402,7 +398,7 @@
 {
 	byte	*vis;
 	mnode_t	*node;
-	int		i;
+	int		i, sz;
 
 	if (r_oldviewleaf == r_viewleaf)
 		return;
@@ -410,8 +406,8 @@
 	r_visframecount++;
 	r_oldviewleaf = r_viewleaf;
 
-	vis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
-		
+	vis = Mod_LeafPVS (r_viewleaf, cl.worldmodel, &sz);
+
 	for (i=0 ; i<cl.worldmodel->numleafs ; i++)
 	{
 		if (vis[i>>3] & (1<<(i&7)))
--- a/r_misc.c
+++ b/r_misc.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 /*
 ===============
--- a/r_part.c
+++ b/r_part.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 #define MAX_PARTICLES			4096	// default max # of particles at one
 										//  time
--- a/r_sky.c
+++ b/r_sky.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 int		iskyspeed = 8;
 int		iskyspeed2 = 2;
--- a/r_sprite.c
+++ b/r_sprite.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 static int				clip_current;
 static vec5_t			clip_verts[2][MAXWORKINGVERTS];
--- a/r_surf.c
+++ b/r_surf.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 drawsurf_t	r_drawsurf;
 
--- a/r_vars.c
+++ /dev/null
@@ -1,19 +1,0 @@
-// r_vars.c: global refresh variables
-
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
-#include "quakedef.h"
-#include "fns.h"
-
-// all global and static refresh variables are collected in a contiguous block
-// to avoid cache conflicts.
-
-//-------------------------------------------------------
-// global refresh variables
-//-------------------------------------------------------
-
-// FIXME: make into one big structure, like cl or sv
-// FIXME: do separately for refresh engine and driver
-
-int	r_bmodelactive;
--- a/sbar.c
+++ b/sbar.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 
 int			sb_updates;		// if >= vid.numpages, no update needed
--- a/screen.c
+++ b/screen.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 // only the refresh window will be updated unless these variables are flagged 
 int			scr_copytop;
--- a/server.h
+++ b/server.h
@@ -186,7 +186,9 @@
 void SV_ClientThink (void);
 void SV_AddClientToServer (struct qsocket_s	*ret);
 
+#pragma varargck	argpos	SV_ClientPrintf	1
 void SV_ClientPrintf (char *fmt, ...);
+#pragma varargck	argpos	SV_BroadcastPrintf	1
 void SV_BroadcastPrintf (char *fmt, ...);
 
 void SV_Physics (void);
@@ -202,8 +204,3 @@
 void SV_RunClients (void);
 void SV_SaveSpawnparms (void);
 void SV_SpawnServer (char *server);
-
-#ifdef __plan9__
-#pragma varargck	argpos	SV_ClientPrintf	1
-#pragma varargck	argpos	SV_BroadcastPrintf	1
-#endif
--- a/snd.c
+++ b/snd.c
@@ -1,9 +1,5 @@
-#include <u.h>
-#include <libc.h>
-#include <thread.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
+#include <thread.h>
 
 cvar_t volume = {"volume", "0.7", 1};
 
--- a/span.c
+++ b/span.c
@@ -1,6 +1,3 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
 
 #define P \
@@ -8,7 +5,7 @@
 		*pdest++ = pbase[(s >> 16) + (t >> 16) * cachewidth]; \
 		s += sstep; \
 		t += tstep; \
-	}while(0)
+	}while(0);
 
 void
 dospan(uchar *pdest, uchar *pbase, int s, int t, int sstep, int tstep, int spancount, int cachewidth)
--- a/span_alpha.c
+++ b/span_alpha.c
@@ -1,6 +1,3 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
 
 #define P \
--- a/sv_main.c
+++ b/sv_main.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 server_t		sv;
 server_static_t	svs;
@@ -376,7 +372,7 @@
 
 void SV_AddToFatPVS (vec3_t org, mnode_t *node, model_t *m)
 {
-	int		i;
+	int		i, sz;
 	byte	*pvs;
 	mplane_t	*plane;
 	float	d;
@@ -388,8 +384,8 @@
 		{
 			if (node->contents != CONTENTS_SOLID)
 			{
-				pvs = Mod_LeafPVS ( (mleaf_t *)node, m);
-				for (i=0 ; i<fatbytes ; i++)
+				pvs = Mod_LeafPVS ( (mleaf_t *)node, m, &sz);
+				for (i=0 ; i<fatbytes && i<sz ; i++)
 					fatpvs[i] |= pvs[i];
 			}
 			return;
--- a/sv_move.c
+++ b/sv_move.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 #define	STEPSIZE	18
 
--- a/sv_phys.c
+++ b/sv_phys.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 /*
 
--- a/sv_user.c
+++ b/sv_user.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 #define	ON_EPSILON		0.1			// point on plane side epsilon
 
--- a/unix/cd.c
+++ b/unix/cd.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 cvar_t bgmvolume = {"bgmvolume", "1", 1};
 
--- a/unix/fs.c
+++ b/unix/fs.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 u16int crcn;
 char fsdir[Nfspath];
--- a/unix/in.c
+++ b/unix/in.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 #include <SDL.h>
 
 /* vid.c */
--- a/unix/net_udp.c
+++ b/unix/net_udp.c
@@ -1,4 +1,4 @@
-#include <u.h>
+#include "quakedef.h"
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
@@ -7,9 +7,6 @@
 #include <sys/param.h>
 #include <sys/ioctl.h>
 #include <errno.h>
-#include "dat.h"
-#include "quakedef.h"
-#include "fns.h"
 
 extern cvar_t hostname;
 
--- a/unix/qk1.c
+++ b/unix/qk1.c
@@ -1,8 +1,6 @@
-#include <u.h>
-#include <time.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
+#include "parg.h"
+#include <time.h>
 
 char *game;
 int debug;
@@ -62,27 +60,42 @@
 main(int argc, char **argv)
 {
 	double t, t2, dt;
-/*
-	ARGBEGIN{
-	case 'D':
-		debug = 1;
-		break;
-	case 'd':
-		dedicated = 1;
-		break;
-	case 'g':
-		game = EARGF(usage());
-		break;
-	case 'x':
-		netmtpt = EARGF(usage());
-		break;
-	default: usage();
-	}ARGEND
-*/
+	struct parg_state ps;
+	int c, nargs;
 
-	game = "ad";
+	parg_init(&ps);
+	nargs = 0;
+	while((c = parg_getopt(&ps, argc, argv, "Ddg:")) >= 0){
+		switch(c){
+		case 1:
+			argv[nargs++] = (char*)ps.optarg;
+			break;
+		case 'D':
+			debug = 1;
+			break;
+		case 'd':
+			dedicated = 1;
+			break;
+		case 'g':
+			game = (char*)ps.optarg;
+			break;
+		case 'h':
+			fprintf(stderr, "usage: qk1 [-g game]\n");
+			return 0;
+			break;
+		case '?':
+			fprintf(stderr, "unknown option -%c\n", ps.optopt);
+			return 1;
+			break;
+		default:
+			fprintf(stderr, "unhandled option -%c\n", c);
+			return 1;
+			break;
+		}
+	}
+
 	srand(getpid());
-	Host_Init(argc, argv);
+	Host_Init(nargs, argv);
 	t = dtime() - 1.0 / Fpsmax;
 	for(;;){
 		t2 = dtime();
--- a/unix/snd.c
+++ b/unix/snd.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 cvar_t volume = {"volume", "0.7", 1};
 
--- a/unix/vid.c
+++ b/unix/vid.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 #include <SDL.h>
 
 int resized;
--- a/vid.c
+++ b/vid.c
@@ -1,10 +1,6 @@
-#include <u.h>
-#include <libc.h>
+#include "quakedef.h"
 #include <draw.h>
 #include <thread.h>
-#include "dat.h"
-#include "quakedef.h"
-#include "fns.h"
 
 viddef_t vid;		/* global video state */
 int resized;
--- a/view.c
+++ b/view.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 /*
 
--- a/wad.c
+++ b/wad.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 int			wad_numlumps;
 lumpinfo_t	*wad_lumps;
--- a/world.c
+++ b/world.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 /*
 
--- a/zone.c
+++ b/zone.c
@@ -1,8 +1,4 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
 #include "quakedef.h"
-#include "fns.h"
 
 typedef struct mem_t mem_t;