quakeforge/tools/qwaq/builtins.c

198 lines
3.9 KiB
C
Raw Normal View History

2001-09-28 07:09:38 +00:00
/*
#FILENAME#
#DESCRIPTION#
Copyright (C) 2001 #AUTHOR#
Author: #AUTHOR#
Date: #DATE#
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
static __attribute__ ((unused)) const char rcsid[] =
"$Id$";
#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/zone.h>
#include "qwaq.h"
static void
bi_print (progs_t *pr)
{
char *str;
str = P_GSTRING (pr, 0);
fprintf (stdout, "%s", str);
}
static void
bi_GarbageCollect (progs_t *pr)
{
PR_GarbageCollect (pr);
}
static void
bi_errno (progs_t *pr)
{
R_INT (pr) = errno;
}
static void
Initial integer type support. qfcc /is/ partially broken when it comes to integer constants and float function args/return values. pr_comp.h: o add the integer opcodes to pr_opcode_e pr_edict.c: o add "quaternion" and "integer" to type_name[] o support quatnernion and integers types when printing values o support the integer opcodes when bounds checking pr_exec.c o enable the integer opcodes pr_opcode: o add the integer opcodes to the opcode table o logical operators all result in an integer rather than a value expr.h: o rename int_val to integer_val qfcc.h: o kill another magic number expr.c: o move the opcode to string conversion out of type_mismatch and into get_op_string o rename int_val to integer_val o general integer type support. o generate an internal comipiler error for null opcodes rather than segging. pr_imm.c: o rename int_val to integer_val o support integer constants, converting to float when needed. pr_lex.c: o magic number death and support quaternions and integers in type_size[] qc-lex.l o rename int_val to integer_val o support quaternion and integer type keywords qc-parse.y: o rename int_val to integer_val o use binary_expr instead of new_binary_expr for local initialized variables builtins.c: o rename int_val to integer_val o fix most (all?) of the INT related FIXMEs defs.qc: o use integer instead of float where it makes sense main.c: o read_result is now integer rather than float main.qc: o float -> integer where appropriate o new test for int const to float arg
2001-07-23 01:31:22 +00:00
bi_strerror (progs_t *pr)
{
int err = P_INT (pr, 0);
RETURN_STRING (pr, strerror (err));
}
static void
Initial integer type support. qfcc /is/ partially broken when it comes to integer constants and float function args/return values. pr_comp.h: o add the integer opcodes to pr_opcode_e pr_edict.c: o add "quaternion" and "integer" to type_name[] o support quatnernion and integers types when printing values o support the integer opcodes when bounds checking pr_exec.c o enable the integer opcodes pr_opcode: o add the integer opcodes to the opcode table o logical operators all result in an integer rather than a value expr.h: o rename int_val to integer_val qfcc.h: o kill another magic number expr.c: o move the opcode to string conversion out of type_mismatch and into get_op_string o rename int_val to integer_val o general integer type support. o generate an internal comipiler error for null opcodes rather than segging. pr_imm.c: o rename int_val to integer_val o support integer constants, converting to float when needed. pr_lex.c: o magic number death and support quaternions and integers in type_size[] qc-lex.l o rename int_val to integer_val o support quaternion and integer type keywords qc-parse.y: o rename int_val to integer_val o use binary_expr instead of new_binary_expr for local initialized variables builtins.c: o rename int_val to integer_val o fix most (all?) of the INT related FIXMEs defs.qc: o use integer instead of float where it makes sense main.c: o read_result is now integer rather than float main.qc: o float -> integer where appropriate o new test for int const to float arg
2001-07-23 01:31:22 +00:00
bi_open (progs_t *pr)
{
char *path = P_GSTRING (pr, 0);
int flags = P_INT (pr, 1);
int mode = P_INT (pr, 2);
R_INT (pr) = open (path, flags, mode);
}
static void
Initial integer type support. qfcc /is/ partially broken when it comes to integer constants and float function args/return values. pr_comp.h: o add the integer opcodes to pr_opcode_e pr_edict.c: o add "quaternion" and "integer" to type_name[] o support quatnernion and integers types when printing values o support the integer opcodes when bounds checking pr_exec.c o enable the integer opcodes pr_opcode: o add the integer opcodes to the opcode table o logical operators all result in an integer rather than a value expr.h: o rename int_val to integer_val qfcc.h: o kill another magic number expr.c: o move the opcode to string conversion out of type_mismatch and into get_op_string o rename int_val to integer_val o general integer type support. o generate an internal comipiler error for null opcodes rather than segging. pr_imm.c: o rename int_val to integer_val o support integer constants, converting to float when needed. pr_lex.c: o magic number death and support quaternions and integers in type_size[] qc-lex.l o rename int_val to integer_val o support quaternion and integer type keywords qc-parse.y: o rename int_val to integer_val o use binary_expr instead of new_binary_expr for local initialized variables builtins.c: o rename int_val to integer_val o fix most (all?) of the INT related FIXMEs defs.qc: o use integer instead of float where it makes sense main.c: o read_result is now integer rather than float main.qc: o float -> integer where appropriate o new test for int const to float arg
2001-07-23 01:31:22 +00:00
bi_close (progs_t *pr)
{
int handle = P_INT (pr, 0);
R_INT (pr) = close (handle);
}
static void
Initial integer type support. qfcc /is/ partially broken when it comes to integer constants and float function args/return values. pr_comp.h: o add the integer opcodes to pr_opcode_e pr_edict.c: o add "quaternion" and "integer" to type_name[] o support quatnernion and integers types when printing values o support the integer opcodes when bounds checking pr_exec.c o enable the integer opcodes pr_opcode: o add the integer opcodes to the opcode table o logical operators all result in an integer rather than a value expr.h: o rename int_val to integer_val qfcc.h: o kill another magic number expr.c: o move the opcode to string conversion out of type_mismatch and into get_op_string o rename int_val to integer_val o general integer type support. o generate an internal comipiler error for null opcodes rather than segging. pr_imm.c: o rename int_val to integer_val o support integer constants, converting to float when needed. pr_lex.c: o magic number death and support quaternions and integers in type_size[] qc-lex.l o rename int_val to integer_val o support quaternion and integer type keywords qc-parse.y: o rename int_val to integer_val o use binary_expr instead of new_binary_expr for local initialized variables builtins.c: o rename int_val to integer_val o fix most (all?) of the INT related FIXMEs defs.qc: o use integer instead of float where it makes sense main.c: o read_result is now integer rather than float main.qc: o float -> integer where appropriate o new test for int const to float arg
2001-07-23 01:31:22 +00:00
bi_read (progs_t *pr)
{
int handle = P_INT (pr, 0);
int count = P_INT (pr, 1);
int *read_result = (int *)P_GPOINTER (pr, 2);
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);
2002-11-12 02:30:08 +00:00
if (res != -1)
RETURN_STRING (pr, buffer);
*read_result = res;
}
static void
Initial integer type support. qfcc /is/ partially broken when it comes to integer constants and float function args/return values. pr_comp.h: o add the integer opcodes to pr_opcode_e pr_edict.c: o add "quaternion" and "integer" to type_name[] o support quatnernion and integers types when printing values o support the integer opcodes when bounds checking pr_exec.c o enable the integer opcodes pr_opcode: o add the integer opcodes to the opcode table o logical operators all result in an integer rather than a value expr.h: o rename int_val to integer_val qfcc.h: o kill another magic number expr.c: o move the opcode to string conversion out of type_mismatch and into get_op_string o rename int_val to integer_val o general integer type support. o generate an internal comipiler error for null opcodes rather than segging. pr_imm.c: o rename int_val to integer_val o support integer constants, converting to float when needed. pr_lex.c: o magic number death and support quaternions and integers in type_size[] qc-lex.l o rename int_val to integer_val o support quaternion and integer type keywords qc-parse.y: o rename int_val to integer_val o use binary_expr instead of new_binary_expr for local initialized variables builtins.c: o rename int_val to integer_val o fix most (all?) of the INT related FIXMEs defs.qc: o use integer instead of float where it makes sense main.c: o read_result is now integer rather than float main.qc: o float -> integer where appropriate o new test for int const to float arg
2001-07-23 01:31:22 +00:00
bi_write (progs_t *pr)
{
int handle = P_INT (pr, 0);
char *buffer = P_GSTRING (pr, 1);
int count = P_INT (pr, 2);
R_INT (pr) = write (handle, buffer, count);
}
static void
Initial integer type support. qfcc /is/ partially broken when it comes to integer constants and float function args/return values. pr_comp.h: o add the integer opcodes to pr_opcode_e pr_edict.c: o add "quaternion" and "integer" to type_name[] o support quatnernion and integers types when printing values o support the integer opcodes when bounds checking pr_exec.c o enable the integer opcodes pr_opcode: o add the integer opcodes to the opcode table o logical operators all result in an integer rather than a value expr.h: o rename int_val to integer_val qfcc.h: o kill another magic number expr.c: o move the opcode to string conversion out of type_mismatch and into get_op_string o rename int_val to integer_val o general integer type support. o generate an internal comipiler error for null opcodes rather than segging. pr_imm.c: o rename int_val to integer_val o support integer constants, converting to float when needed. pr_lex.c: o magic number death and support quaternions and integers in type_size[] qc-lex.l o rename int_val to integer_val o support quaternion and integer type keywords qc-parse.y: o rename int_val to integer_val o use binary_expr instead of new_binary_expr for local initialized variables builtins.c: o rename int_val to integer_val o fix most (all?) of the INT related FIXMEs defs.qc: o use integer instead of float where it makes sense main.c: o read_result is now integer rather than float main.qc: o float -> integer where appropriate o new test for int const to float arg
2001-07-23 01:31:22 +00:00
bi_seek (progs_t *pr)
{
int handle = P_INT (pr, 0);
int pos = P_INT (pr, 1);
int whence = P_INT (pr, 2);
R_INT (pr) = lseek (handle, pos, whence);
}
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 = P_GSTRING (pr, 0);
2001-07-25 21:48:20 +00:00
char c;
int count = 0;
float *v;
while ((c = *fmt++)) {
if (c == '%' && count < 7) {
switch (c = *fmt++) {
case 'i':
fprintf (stdout, "%i",
P_INT (pr, 1 + count++ * pr->pr_param_size));
2001-07-25 21:48:20 +00:00
break;
case 'f':
fprintf (stdout, "%f",
P_FLOAT (pr, 1 + count++ * pr->pr_param_size));
2001-07-25 21:48:20 +00:00
break;
case 's':
fputs (P_GSTRING (pr, 1 + count++ * pr->pr_param_size),
stdout);
2001-07-25 21:48:20 +00:00
break;
case 'v':
v = P_VECTOR (pr, 1 + count++ * pr->pr_param_size);
2001-07-25 21:48:20 +00:00
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);
}
}
}
void
2002-11-12 02:30:08 +00:00
BI_Init (progs_t *pr)
{
2002-11-12 02:30:08 +00:00
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);
}