do some cleanup

This commit is contained in:
Bill Currie 2002-11-12 02:30:08 +00:00
parent f561d25a84
commit 13c5216373
3 changed files with 120 additions and 70 deletions

View file

@ -40,8 +40,6 @@ static const char rcsid[] =
#include <QF/progs.h>
#include <QF/zone.h>
int *read_result; //FIXME: eww
static void
bi_print (progs_t *pr)
{
@ -91,6 +89,7 @@ bi_read (progs_t *pr)
{
int handle = P_INT (pr, 0);
int count = P_INT (pr, 1);
int *read_result = (int *)P_POINTER (pr, 2);
int res;
char *buffer;
@ -98,7 +97,7 @@ bi_read (progs_t *pr)
if (!buffer)
PR_Error (pr, "%s: couldn't allocate %d bytes", "bi_read", count);
res = read (handle, buffer, count);
if (res != -1) // FIXME: this just won't work :/
if (res != -1)
RETURN_STRING (pr, buffer);
*read_result = res;
}
@ -172,18 +171,18 @@ bi_printf (progs_t *pr)
}
void
BI_Init (progs_t *progs)
BI_Init (progs_t *pr)
{
PR_AddBuiltin (progs, "print", bi_print, 1);
PR_AddBuiltin (progs, "GarbageCollect", bi_GarbageCollect, 2);
PR_AddBuiltin (progs, "errno", bi_errno, 3);
PR_AddBuiltin (progs, "strerror", bi_strerror, 4);
PR_AddBuiltin (progs, "open", bi_open, 5);
PR_AddBuiltin (progs, "close", bi_close, 6);
PR_AddBuiltin (progs, "read", bi_read, 7);
PR_AddBuiltin (progs, "write", bi_write, 8);
PR_AddBuiltin (progs, "seek", bi_seek, 9);
PR_AddBuiltin (progs, "traceon", bi_traceon, 10);
PR_AddBuiltin (progs, "traceoff", bi_traceoff, 11);
PR_AddBuiltin (progs, "printf", bi_printf, 12);
PR_AddBuiltin (pr, "print", bi_print, 1);
PR_AddBuiltin (pr, "GarbageCollect", bi_GarbageCollect, 2);
PR_AddBuiltin (pr, "errno", bi_errno, 3);
PR_AddBuiltin (pr, "strerror", bi_strerror, 4);
PR_AddBuiltin (pr, "open", bi_open, 5);
PR_AddBuiltin (pr, "close", bi_close, 6);
PR_AddBuiltin (pr, "read", bi_read, 7);
PR_AddBuiltin (pr, "write", bi_write, 8);
PR_AddBuiltin (pr, "seek", bi_seek, 9);
PR_AddBuiltin (pr, "traceon", bi_traceon, 10);
PR_AddBuiltin (pr, "traceoff", bi_traceoff, 11);
PR_AddBuiltin (pr, "printf", bi_printf, 12);
}

View file

@ -4,7 +4,7 @@ integer () errno = #3;
string (integer err) strerror = #4;
integer (...) open = #5; // string path, float flags[, float mode]
integer (integer handle) close = #6;
string (integer handle, integer count) read = #7; // FIXME: cf read_result
string (integer handle, integer count, integer []result) read = #7;
integer (integer handle, string buffer, integer count) write = #8;
integer (integer handle, integer pos, integer whence) seek = #9;
@ -16,7 +16,6 @@ void (...) printf = #12;
float time;
entity self;
integer read_result; // FIXME: hacky (cf read)
.float nextthink;
.void() think;

View file

@ -37,79 +37,131 @@ static const char rcsid[] =
#include <QF/progs.h>
#include <QF/quakefs.h>
#include <QF/sys.h>
#include "QF/va.h"
#include <QF/zone.h>
#define MAX_EDICTS 1024
progs_t progs;
void *membase;
int memsize = 16*1024*1024;
static edict_t *edicts;
static int num_edicts;
static int reserved_edicts;
static progs_t pr;
static void *membase;
static int memsize = 16*1024*1024;
edict_t *edicts;
int num_edicts;
int reserved_edicts;
void BI_Init (progs_t *pr);
void BI_Init (progs_t *progs);
extern int *read_result; //FIXME: eww
int
main ()
static QFile *
open_file (const char *path, int *len)
{
func_t main_func;
QFile *f;
int len;
QFile *file = Qopen (path, "rbz");
if (!file) {
perror (path);
return 0;
}
*len = Qfilesize (file);
return file;
}
static void *
load_file (progs_t *pr, const char *name)
{
QFile *file;
int size;
void *sym;
file = open_file (name, &size);
if (!file) {
file = open_file (va ("%s.gz", name), &size);
if (!file) {
return 0;
}
}
sym = malloc (size);
Qread (file, sym, size);
return sym;
}
static void *
allocate_progs_mem (progs_t *pr, int size)
{
return malloc (size);
}
static void
free_progs_mem (progs_t *pr, void *mem)
{
free (mem);
}
static void
init_qf (void)
{
Cvar_Init_Hash ();
Cmd_Init_Hash ();
membase = malloc (memsize);
SYS_CHECKMEM (membase);
Memory_Init (membase, memsize);
Cvar_Init ();
Sys_Init_Cvars ();
Cmd_Init ();
membase = malloc (memsize);
Memory_Init (membase, memsize);
Cvar_Get ("pr_debug", "1", 0, 0, 0);
Cvar_Get ("pr_boundscheck", "0", 0, 0, 0);
Cvar_Get ("fs_basegame", ".", 0, 0, 0);
Cvar_Get ("fs_userpath", ".", 0, 0, 0);
Cvar_Get ("fs_sharepath", ".", 0, 0, 0);
developer = Cvar_Get ("developer", "1", 0, 0, 0);
pr.edicts = &edicts;
pr.num_edicts = &num_edicts;
pr.reserved_edicts = &reserved_edicts;
pr.load_file = load_file;
pr.allocate_progs_mem = allocate_progs_mem;
pr.free_progs_mem = free_progs_mem;
PR_Init_Cvars ();
COM_Filesystem_Init_Cvars ();
COM_Filesystem_Init ();
PR_Init ();
PR_Obj_Progs_Init (&progs);
BI_Init (&progs);
PR_Obj_Progs_Init (&pr);
BI_Init (&pr);
}
progs.edicts = &edicts;
progs.num_edicts = &num_edicts;
progs.reserved_edicts = &reserved_edicts;
progs.no_exec_limit = 1;
progs.progs_name = "qwaq.dat";
static int
load_progs (const char *name)
{
QFile *file;
int size;
f = Qopen (progs.progs_name, "rb");
if (f) {
Qseek (f, 0, SEEK_END);
len = Qtell (f);
Qseek (f, 0, SEEK_SET);
com_filesize = len;
PR_LoadProgsFile (&progs, f, len, 1, 1024 * 1024);
Qclose (f);
file = open_file (name, &size);
if (!file) {
perror (name);
return 0;
}
if (!progs.progs)
pr.progs_name = name;
PR_LoadProgsFile (&pr, file, size, 1, 1024 * 1024);
Qclose (file);
if (!PR_ResolveGlobals (&pr))
PR_Error (&pr, "unable to load %s", pr.progs_name);
PR_LoadStrings (&pr);
PR_LoadDebug (&pr);
PR_Check_Opcodes (&pr);
PR_RelocateBuiltins (&pr);
PR_InitRuntime (&pr);
return 1;
}
int
main (int argc, char **argv)
{
func_t main_func;
const char *name = "qwaq.dat";
init_qf ();
if (argc > 1)
name = argv[1];
if (!load_progs (name))
Sys_Error ("couldn't load %s", "qwaq.dat");
if (!PR_ResolveGlobals (&progs))
PR_Error (&progs, "unable to load %s", progs.progs_name);
PR_LoadStrings (&progs);
PR_LoadDebug (&progs);
PR_Check_Opcodes (&progs);
PR_RelocateBuiltins (&progs);
PR_InitRuntime (&progs);
read_result = (int*)PR_GetGlobalPointer (&progs, "read_result");
main_func = PR_GetFunctionIndex (&progs, "main");
PR_ExecuteProgram (&progs, main_func);
return G_FLOAT (&progs, OFS_RETURN);
main_func = PR_GetFunctionIndex (&pr, "main");
PR_ExecuteProgram (&pr, main_func);
return G_INT (&pr, OFS_RETURN);
}