diff --git a/tools/qwaq/builtins.c b/tools/qwaq/builtins.c index 8aa6970db..aadd8d70b 100644 --- a/tools/qwaq/builtins.c +++ b/tools/qwaq/builtins.c @@ -1,15 +1,25 @@ +#include +#include +#include +#include +#include +#include + #include +#include #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)) -void +float *read_result; //FIXME: eww + +static void bi_fixme (progs_t *pr) { PR_Error (pr, "unimplemented function\n"); } -void +static void bi_print (progs_t *pr) { char *str; @@ -18,9 +28,89 @@ bi_print (progs_t *pr) 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[] = { bi_fixme, bi_print, + bi_GarbageCollect, + bi_errno, + bi_strerror, + bi_open, + bi_close, + bi_read, + bi_write, + bi_seek, }; void diff --git a/tools/qwaq/defs.qc b/tools/qwaq/defs.qc index 974aeaaf6..c62d82a97 100644 --- a/tools/qwaq/defs.qc +++ b/tools/qwaq/defs.qc @@ -1,7 +1,16 @@ 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; entity self; +float read_result; // FIXME: hacky (cf read) .float nextthink; .float think; diff --git a/tools/qwaq/main.c b/tools/qwaq/main.c index 00ec32e62..4678c4cdd 100644 --- a/tools/qwaq/main.c +++ b/tools/qwaq/main.c @@ -20,13 +20,15 @@ void BI_Init (progs_t *progs); extern char *type_name[]; +extern float *read_result; //FIXME: eww + int main () { func_t main_func; FILE *f; int len; - int i; + //int i; Cvar_Init_Hash (); Cmd_Init_Hash (); @@ -43,6 +45,7 @@ main () progs.edicts = &edicts; progs.num_edicts = &num_edicts; progs.reserved_edicts = &reserved_edicts; + progs.no_exec_limit = 1; f = fopen ("qwaq.dat", "rb"); if (f) { @@ -59,7 +62,7 @@ main () Sys_Error ("couldn't load %s\n", "qwaq.dat"); *progs.edicts = PR_InitEdicts (&progs, MAX_EDICTS); - +#if 0 for (i = 0; i < progs.progs->numstatements; i++) PR_PrintStatement (&progs, &progs.pr_statements[i]); 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 ("\n"); +#endif + read_result = (float*)PR_GetGlobalPointer (&progs, "read_result"); main_func = PR_GetFunctionIndex (&progs, "main"); PR_ExecuteProgram (&progs, main_func); return 0; diff --git a/tools/qwaq/main.qc b/tools/qwaq/main.qc index b1675a86e..6f85efbf8 100644 --- a/tools/qwaq/main.qc +++ b/tools/qwaq/main.qc @@ -1,30 +1,21 @@ -string hello = "hello"; -string world = "world"; float () main = { - local string s; + local float handle; + local string buffer; - print (hello + " " + world + "\n"); - if (hello < world) - print (hello); - if (world > hello) - print (world); - print ("\n"); - s = hello + world; - s = s + "\n"; - print (s); -}; - -float () CheckExistence = -{ - return 1; -}; - -void () test = -{ - if (CheckExistence() == 0) { - return; + handle = open ("builtins.c", 0); + if (handle == -1) { + print (strerror (errno ()) + "\n"); + return 1; } - local entity p; - p = self; + do { + buffer = read (handle, 1024); + if (read_result == -1) { + print (strerror (errno ()) + "\n"); + return 1; + } + print (buffer); + } while (read_result == 1024); + close (handle); + return 0; };