implement file io for qwaq (and cat in qc }:> )

This commit is contained in:
Bill Currie 2001-06-06 20:05:08 +00:00
parent 2bc94ea66f
commit e349ed3013
4 changed files with 124 additions and 29 deletions

View file

@ -1,15 +1,25 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <QF/progs.h> #include <QF/progs.h>
#include <QF/zone.h>
#define RETURN_EDICT(p, e) ((p)->pr_globals[OFS_RETURN].int_var = EDICT_TO_PROG(p, e)) #define RETURN_EDICT(p, e) ((p)->pr_globals[OFS_RETURN].int_var = EDICT_TO_PROG(p, e))
#define RETURN_STRING(p, s) ((p)->pr_globals[OFS_RETURN].int_var = PR_SetString((p), s)) #define RETURN_STRING(p, s) ((p)->pr_globals[OFS_RETURN].int_var = PR_SetString((p), s))
void float *read_result; //FIXME: eww
static void
bi_fixme (progs_t *pr) bi_fixme (progs_t *pr)
{ {
PR_Error (pr, "unimplemented function\n"); PR_Error (pr, "unimplemented function\n");
} }
void static void
bi_print (progs_t *pr) bi_print (progs_t *pr)
{ {
char *str; char *str;
@ -18,9 +28,89 @@ bi_print (progs_t *pr)
fprintf (stdout, "%s", str); fprintf (stdout, "%s", str);
} }
static void
bi_GarbageCollect (progs_t *pr)
{
PR_GarbageCollect (pr);
}
static void
bi_errno (progs_t *pr)
{
G_FLOAT (pr, OFS_RETURN) = errno;
}
static void
bi_strerror (progs_t *pr) // FIXME: make INT when qc int
{
int err = G_FLOAT (pr, OFS_PARM0);
RETURN_STRING (pr, strerror (err));
}
static void
bi_open (progs_t *pr) // FIXME: make INT when qc int
{
char *path = G_STRING (pr, OFS_PARM0);
int flags = G_FLOAT (pr, OFS_PARM1);
int mode = G_FLOAT (pr, OFS_PARM2);
G_INT (pr, OFS_RETURN) = open (path, flags, mode);
}
static void
bi_close (progs_t *pr) // FIXME: make INT when qc int
{
int handle = G_INT (pr, OFS_PARM0);
G_FLOAT (pr, OFS_RETURN) = close (handle);
}
static void
bi_read (progs_t *pr) // FIXME: make INT when qc int
{
int handle = G_INT (pr, OFS_PARM0);
int count = G_FLOAT (pr, OFS_PARM1);
int res;
char *buffer;
buffer = Hunk_TempAlloc (count);
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 :/
RETURN_STRING (pr, buffer);
*read_result = res;
}
static void
bi_write (progs_t *pr) // FIXME: make INT when qc int
{
int handle = G_INT (pr, OFS_PARM0);
char *buffer = G_STRING (pr, OFS_PARM1);
int count = G_FLOAT (pr, OFS_PARM2);
G_FLOAT (pr, OFS_RETURN) = write (handle, buffer, count);
}
static void
bi_seek (progs_t *pr) // FIXME: make INT when qc int
{
int handle = G_INT (pr, OFS_PARM0);
int pos = G_FLOAT (pr, OFS_PARM1);
int whence = G_FLOAT (pr, OFS_PARM2);
G_FLOAT (pr, OFS_RETURN) = lseek (handle, pos, whence);
}
builtin_t builtins[] = { builtin_t builtins[] = {
bi_fixme, bi_fixme,
bi_print, bi_print,
bi_GarbageCollect,
bi_errno,
bi_strerror,
bi_open,
bi_close,
bi_read,
bi_write,
bi_seek,
}; };
void void

View file

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

View file

@ -20,13 +20,15 @@ void BI_Init (progs_t *progs);
extern char *type_name[]; extern char *type_name[];
extern float *read_result; //FIXME: eww
int int
main () main ()
{ {
func_t main_func; func_t main_func;
FILE *f; FILE *f;
int len; int len;
int i; //int i;
Cvar_Init_Hash (); Cvar_Init_Hash ();
Cmd_Init_Hash (); Cmd_Init_Hash ();
@ -43,6 +45,7 @@ main ()
progs.edicts = &edicts; progs.edicts = &edicts;
progs.num_edicts = &num_edicts; progs.num_edicts = &num_edicts;
progs.reserved_edicts = &reserved_edicts; progs.reserved_edicts = &reserved_edicts;
progs.no_exec_limit = 1;
f = fopen ("qwaq.dat", "rb"); f = fopen ("qwaq.dat", "rb");
if (f) { if (f) {
@ -59,7 +62,7 @@ main ()
Sys_Error ("couldn't load %s\n", "qwaq.dat"); Sys_Error ("couldn't load %s\n", "qwaq.dat");
*progs.edicts = PR_InitEdicts (&progs, MAX_EDICTS); *progs.edicts = PR_InitEdicts (&progs, MAX_EDICTS);
#if 0
for (i = 0; i < progs.progs->numstatements; i++) for (i = 0; i < progs.progs->numstatements; i++)
PR_PrintStatement (&progs, &progs.pr_statements[i]); PR_PrintStatement (&progs, &progs.pr_statements[i]);
printf ("\n"); printf ("\n");
@ -85,6 +88,8 @@ main ()
printf ("%s %d %d %s\n", type_name[def->type & ~DEF_SAVEGLOBAL], (def->type & DEF_SAVEGLOBAL) != 0, def->ofs, PR_GetString (&progs, def->s_name)); printf ("%s %d %d %s\n", type_name[def->type & ~DEF_SAVEGLOBAL], (def->type & DEF_SAVEGLOBAL) != 0, def->ofs, PR_GetString (&progs, def->s_name));
} }
printf ("\n"); printf ("\n");
#endif
read_result = (float*)PR_GetGlobalPointer (&progs, "read_result");
main_func = PR_GetFunctionIndex (&progs, "main"); main_func = PR_GetFunctionIndex (&progs, "main");
PR_ExecuteProgram (&progs, main_func); PR_ExecuteProgram (&progs, main_func);
return 0; return 0;

View file

@ -1,30 +1,21 @@
string hello = "hello";
string world = "world";
float () main = float () main =
{ {
local string s; local float handle;
local string buffer;
print (hello + " " + world + "\n"); handle = open ("builtins.c", 0);
if (hello < world) if (handle == -1) {
print (hello); print (strerror (errno ()) + "\n");
if (world > hello)
print (world);
print ("\n");
s = hello + world;
s = s + "\n";
print (s);
};
float () CheckExistence =
{
return 1; return 1;
};
void () test =
{
if (CheckExistence() == 0) {
return;
} }
local entity p; do {
p = self; buffer = read (handle, 1024);
if (read_result == -1) {
print (strerror (errno ()) + "\n");
return 1;
}
print (buffer);
} while (read_result == 1024);
close (handle);
return 0;
}; };