1 /*
2 	#FILENAME#
3 
4 	#DESCRIPTION#
5 
6 	Copyright (C) 2001 #AUTHOR#
7 
8 	Author: #AUTHOR#
9 	Date: #DATE#
10 
11 	This program is free software; you can redistribute it and/or
12 	modify it under the terms of the GNU General Public License
13 	as published by the Free Software Foundation; either version 2
14 	of the License, or (at your option) any later version.
15 
16 	This program is distributed in the hope that it will be useful,
17 	but WITHOUT ANY WARRANTY; without even the implied warranty of
18 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 
20 	See the GNU General Public License for more details.
21 
22 	You should have received a copy of the GNU General Public License
23 	along with this program; if not, write to:
24 
25 		Free Software Foundation, Inc.
26 		59 Temple Place - Suite 330
27 		Boston, MA  02111-1307, USA
28 
29 */
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33 
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <string.h>
40 
41 #include <QF/dstring.h>
42 #include <QF/progs.h>
43 #include <QF/zone.h>
44 
45 #include "qwaq.h"
46 
47 static void
bi_print(progs_t * pr)48 bi_print (progs_t *pr)
49 {
50 	const char *str;
51 
52 	str = P_GSTRING (pr, 0);
53 	fprintf (stdout, "%s", str);
54 }
55 
56 static void
bi_errno(progs_t * pr)57 bi_errno (progs_t *pr)
58 {
59 	R_INT (pr) = errno;
60 }
61 
62 static void
bi_strerror(progs_t * pr)63 bi_strerror (progs_t *pr)
64 {
65 	int err = P_INT (pr, 0);
66 	RETURN_STRING (pr, strerror (err));
67 }
68 
69 static void
bi_open(progs_t * pr)70 bi_open (progs_t *pr)
71 {
72 	const char *path = P_GSTRING (pr, 0);
73 	int flags = P_INT (pr, 1);
74 	int mode = P_INT (pr, 2);
75 	R_INT (pr) = open (path, flags, mode);
76 }
77 
78 static void
bi_close(progs_t * pr)79 bi_close (progs_t *pr)
80 {
81 	int handle = P_INT (pr, 0);
82 	R_INT (pr) = close (handle);
83 }
84 
85 static void
bi_read(progs_t * pr)86 bi_read (progs_t *pr)
87 {
88 	int handle = P_INT (pr, 0);
89 	int count = P_INT (pr, 1);
90 	int *read_result = (int *)P_GPOINTER (pr, 2);
91 	int res;
92 	char *buffer;
93 
94 	buffer = Hunk_TempAlloc (count);
95 	if (!buffer)
96 		PR_Error (pr, "%s: couldn't allocate %d bytes", "bi_read", count);
97 	res = read (handle, buffer, count);
98 	if (res != -1)
99 		RETURN_STRING (pr, buffer);
100 	*read_result = res;
101 }
102 
103 static void
bi_write(progs_t * pr)104 bi_write (progs_t *pr)
105 {
106 	int handle = P_INT (pr, 0);
107 	const char *buffer = P_GSTRING (pr, 1);
108 	int count = P_INT (pr, 2);
109 
110 	R_INT (pr) = write (handle, buffer, count);
111 }
112 
113 static void
bi_seek(progs_t * pr)114 bi_seek (progs_t *pr)
115 {
116 	int handle = P_INT (pr, 0);
117 	int pos = P_INT (pr, 1);
118 	int whence = P_INT (pr, 2);
119 
120 	R_INT (pr) = lseek (handle, pos, whence);
121 }
122 
123 /*
124 static void
125 bi_traceon (progs_t *pr)
126 {
127 	pr->pr_trace = true;
128 	pr->pr_trace_depth = pr->pr_depth;
129 }
130 
131 static void
132 bi_traceoff (progs_t *pr)
133 {
134 	pr->pr_trace = false;
135 }
136 */
137 static void
bi_printf(progs_t * pr)138 bi_printf (progs_t *pr)
139 {
140 	const char *fmt = P_GSTRING (pr, 0);
141 	int         count = pr->pr_argc - 1;
142 	pr_type_t **args = pr->pr_params + 1;
143 	dstring_t  *dstr = dstring_new ();
144 
145 	PR_Sprintf (pr, dstr, "bi_printf", fmt, count, args);
146 	if (dstr->str)
147 		fputs (dstr->str, stdout);
148 	dstring_delete (dstr);
149 }
150 
151 static builtin_t builtins[] = {
152 	{"print",		bi_print,		-1},
153 	{"errno",		bi_errno,		-1},
154 	{"strerror",	bi_strerror,	-1},
155 	{"open",		bi_open,		-1},
156 	{"close",		bi_close,		-1},
157 	{"read",		bi_read,		-1},
158 	{"write",		bi_write,		-1},
159 	{"seek",		bi_seek,		-1},
160 //	{"traceon",		bi_traceon,		-1},
161 //	{"traceoff",	bi_traceoff,	-1},
162 	{"printf",		bi_printf,		-1},
163 	{0}
164 };
165 
166 void
BI_Init(progs_t * pr)167 BI_Init (progs_t *pr)
168 {
169 	PR_RegisterBuiltins (pr, builtins);
170 }
171