2001-06-06 20:05:08 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2001-06-01 21:57:59 +00:00
|
|
|
#include <QF/progs.h>
|
2001-06-06 20:05:08 +00:00
|
|
|
#include <QF/zone.h>
|
2001-06-01 21:57:59 +00:00
|
|
|
|
2001-07-23 01:31:22 +00:00
|
|
|
#define RETURN_EDICT(p, e) ((p)->pr_globals[OFS_RETURN].integer_var = EDICT_TO_PROG(p, e))
|
|
|
|
#define RETURN_STRING(p, s) ((p)->pr_globals[OFS_RETURN].integer_var = PR_SetString((p), s))
|
2001-06-01 21:57:59 +00:00
|
|
|
|
2001-07-23 01:31:22 +00:00
|
|
|
int *read_result; //FIXME: eww
|
2001-06-06 20:05:08 +00:00
|
|
|
|
|
|
|
static void
|
2001-06-01 21:57:59 +00:00
|
|
|
bi_print (progs_t *pr)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
str = G_STRING (pr, (OFS_PARM0));
|
|
|
|
fprintf (stdout, "%s", str);
|
|
|
|
}
|
|
|
|
|
2001-06-06 20:05:08 +00:00
|
|
|
static void
|
|
|
|
bi_GarbageCollect (progs_t *pr)
|
|
|
|
{
|
|
|
|
PR_GarbageCollect (pr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bi_errno (progs_t *pr)
|
|
|
|
{
|
2001-07-23 01:31:22 +00:00
|
|
|
G_INT (pr, OFS_RETURN) = errno;
|
2001-06-06 20:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-07-23 01:31:22 +00:00
|
|
|
bi_strerror (progs_t *pr)
|
2001-06-06 20:05:08 +00:00
|
|
|
{
|
2001-07-23 01:31:22 +00:00
|
|
|
int err = G_INT (pr, OFS_PARM0);
|
2001-06-06 20:05:08 +00:00
|
|
|
RETURN_STRING (pr, strerror (err));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-07-23 01:31:22 +00:00
|
|
|
bi_open (progs_t *pr)
|
2001-06-06 20:05:08 +00:00
|
|
|
{
|
|
|
|
char *path = G_STRING (pr, OFS_PARM0);
|
2001-07-23 01:31:22 +00:00
|
|
|
int flags = G_INT (pr, OFS_PARM1);
|
|
|
|
int mode = G_INT (pr, OFS_PARM2);
|
2001-06-06 20:05:08 +00:00
|
|
|
G_INT (pr, OFS_RETURN) = open (path, flags, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-07-23 01:31:22 +00:00
|
|
|
bi_close (progs_t *pr)
|
2001-06-06 20:05:08 +00:00
|
|
|
{
|
|
|
|
int handle = G_INT (pr, OFS_PARM0);
|
2001-07-23 01:31:22 +00:00
|
|
|
G_INT (pr, OFS_RETURN) = close (handle);
|
2001-06-06 20:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-07-23 01:31:22 +00:00
|
|
|
bi_read (progs_t *pr)
|
2001-06-06 20:05:08 +00:00
|
|
|
{
|
|
|
|
int handle = G_INT (pr, OFS_PARM0);
|
2001-07-23 01:31:22 +00:00
|
|
|
int count = G_INT (pr, OFS_PARM1);
|
2001-06-06 20:05:08 +00:00
|
|
|
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
|
2001-07-23 01:31:22 +00:00
|
|
|
bi_write (progs_t *pr)
|
2001-06-06 20:05:08 +00:00
|
|
|
{
|
|
|
|
int handle = G_INT (pr, OFS_PARM0);
|
|
|
|
char *buffer = G_STRING (pr, OFS_PARM1);
|
2001-07-23 01:31:22 +00:00
|
|
|
int count = G_INT (pr, OFS_PARM2);
|
2001-06-06 20:05:08 +00:00
|
|
|
|
2001-07-23 01:31:22 +00:00
|
|
|
G_INT (pr, OFS_RETURN) = write (handle, buffer, count);
|
2001-06-06 20:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-07-23 01:31:22 +00:00
|
|
|
bi_seek (progs_t *pr)
|
2001-06-06 20:05:08 +00:00
|
|
|
{
|
|
|
|
int handle = G_INT (pr, OFS_PARM0);
|
2001-07-23 01:31:22 +00:00
|
|
|
int pos = G_INT (pr, OFS_PARM1);
|
|
|
|
int whence = G_INT (pr, OFS_PARM2);
|
2001-06-06 20:05:08 +00:00
|
|
|
|
2001-07-23 01:31:22 +00:00
|
|
|
G_INT (pr, OFS_RETURN) = lseek (handle, pos, whence);
|
2001-06-06 20:05:08 +00:00
|
|
|
}
|
|
|
|
|
2001-06-27 22:56:52 +00:00
|
|
|
static void
|
|
|
|
bi_traceon (progs_t *pr)
|
|
|
|
{
|
|
|
|
pr->pr_trace = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bi_traceoff (progs_t *pr)
|
|
|
|
{
|
|
|
|
pr->pr_trace = false;
|
|
|
|
}
|
|
|
|
|
2001-07-25 21:48:20 +00:00
|
|
|
static void
|
|
|
|
bi_printf (progs_t *pr)
|
|
|
|
{
|
|
|
|
const char *fmt = G_STRING (pr, OFS_PARM0);
|
|
|
|
char c;
|
|
|
|
int count = 0;
|
|
|
|
float *v;
|
|
|
|
|
|
|
|
while ((c = *fmt++)) {
|
|
|
|
if (c == '%' && count < 7) {
|
|
|
|
switch (c = *fmt++) {
|
|
|
|
case 'i':
|
|
|
|
fprintf (stdout, "%i", G_INT (pr, OFS_PARM1 + count++ * 3));
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
fprintf (stdout, "%f", G_FLOAT (pr, OFS_PARM1 + count++ * 3));
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
fputs (G_STRING (pr, OFS_PARM1 + count++ * 3), stdout);
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
v = G_VECTOR (pr, OFS_PARM1 + count++ * 3);
|
|
|
|
fprintf (stdout, "'%f %f %f'", v[0], v[1], v[2]);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fputc ('%', stdout);
|
|
|
|
fputc (c, stdout);
|
|
|
|
count = 7;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fputc (c, stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-01 21:57:59 +00:00
|
|
|
void
|
|
|
|
BI_Init (progs_t *progs)
|
|
|
|
{
|
2001-08-03 06:40:28 +00:00
|
|
|
progs->builtins = 0;
|
|
|
|
progs->numbuiltins = 0;
|
|
|
|
|
|
|
|
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);
|
2001-06-01 21:57:59 +00:00
|
|
|
}
|