Debian Codegraph @

terps/scare/sxstubs.c

raw symbols SCIP
1 /* vi: set ts=2 shiftwidth=2 expandtab:
2 *
3 * Copyright (C) 2003-2008 Simon Baldwin and Mark J. Tilford
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 * USA
18 */
19
20 /*
21 * Module notes:
22 *
23 * o Better, or perhaps less strace-like, tracing might be more beneficial
24 * for game script debugging.
25 */
26
27 #include <assert.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include "scare.h"
32 #include "sxprotos.h"
33
34
35 /* Stubs trace flag. */
36 static sc_bool stub_trace = FALSE;
37
38 /*
39 * Input/output handler functions. If assigned, calls to os_* functions are
40 * routed here to allow the script runner to catch interpeter i/o.
41 */
42 static sc_bool (*stub_read_line) (sc_char*, sc_int) = NULL;
43 static void (*stub_print_string) (const sc_char*) = NULL;
44 static void *(*stub_open_file) (sc_bool) = NULL;
45 static sc_int (*stub_read_file) (void*, sc_byte*, sc_int) = NULL;
46 static void (*stub_write_file) (void*, const sc_byte*, sc_int) = NULL;
47 static void (*stub_close_file) (void*) = NULL;
48
49 /* Flags for whether to report tags and resources via stub_print_string(). */
50 static sc_int stub_show_resources = 0;
51 static sc_int stub_show_tags = 0;
52
53
54 /*
55 * stub_attach_handlers()
56 * stub_detach_handlers()
57 *
58 * Attach input/output handler functions, and reset to NULLs.
59 */
60 void
61 stub_attach_handlers (sc_bool (*read_line) (sc_char*, sc_int),
62 void (*print_string) (const sc_char*),
63 void *(*open_file) (sc_bool),
64 sc_int (*read_file) (void*, sc_byte*, sc_int),
65 void (*write_file) (void*, const sc_byte*, sc_int),
66 void (*close_file) (void*))
67 {
68 stub_read_line = read_line;
69 stub_print_string = print_string;
70 stub_open_file = open_file;
71 stub_read_file = read_file;
72 stub_write_file = write_file;
73 stub_close_file = close_file;
74
75 stub_show_resources = 0;
76 stub_show_tags = 0;
77 }
78
79 void
80 stub_detach_handlers (void)
81 {
82 stub_read_line = NULL;
83 stub_print_string = NULL;
84 stub_open_file = NULL;
85 stub_read_file = NULL;
86 stub_write_file = NULL;
87 stub_close_file = NULL;
88
89 stub_show_resources = 0;
90 stub_show_tags = 0;
91 }
92
93
94 /*
95 * stub_adjust_test_control()
96 * stub_catch_test_control()
97 *
98 * Trap testing control tags from the game, incrementing or decrementing
99 * stub_show_resources or stub_show_tags if a testing control tag arrives.
100 * Returns TRUE if the tag trapped was one of our testing ones.
101 */
102 static void
103 stub_adjust_test_control (sc_int *control, sc_bool is_begin)
104 {
105 *control += is_begin ? 1 : (*control > 0 ? -1 : 0);
106 }
107
108 static sc_bool
109 stub_catch_test_control (sc_int tag, const sc_char *argument)
110 {
111 if (tag == SC_TAG_UNKNOWN && argument)
112 {
113 sc_bool is_begin;
114 const sc_char *name;
115
116 is_begin = !(argument[0] == '/');
117 name = is_begin ? argument : argument + 1;
118
119 if (sc_strcasecmp (name, "sxshowresources") == 0)
120 {
121 stub_adjust_test_control (&stub_show_resources, is_begin);
122 return TRUE;
123 }
124 else if (sc_strcasecmp (name, "sxshowtags") == 0)
125 {
126 stub_adjust_test_control (&stub_show_tags, is_begin);
127 return TRUE;
128 }
129 }
130
131 return FALSE;
132 }
133
134
135 /*
136 * stub_notnull()
137 *
138 * Returns the string address passed in, or "(nil)" if NULL, for printf
139 * safety. Most libc's handle this themselves, but it's not defined by ANSI.
140 */
141 static const sc_char *
142 stub_notnull (const sc_char *string)
143 {
144 return string ? string : "(nil)";
145 }
146
147
148 /*
149 * os_*()
150 *
151 * Stub functions called by the interpreter core.
152 */
153 void
154 os_print_tag (sc_int tag, const sc_char *argument)
155 {
156 if (stub_trace)
157 sx_trace ("os_print_tag (%ld, \"%s\")\n", tag, stub_notnull (argument));
158
159 if (!stub_catch_test_control (tag, argument))
160 {
161 if (stub_print_string)
162 {
163 if (stub_show_tags > 0)
164 {
165 sc_char buffer[32];
166
167 stub_print_string ("<<Tag: id=");
168 sprintf (buffer, "%ld", tag);
169 stub_print_string (buffer);
170 stub_print_string (", argument=\"");
171 stub_print_string (stub_notnull (argument));
172 stub_print_string ("\">>");
173 }
174 else if (tag == SC_TAG_WAITKEY || tag == SC_TAG_CLS)
175 stub_print_string (" ");
176 }
177 }
178 }
179
180 void
181 os_print_string (const sc_char *string)
182 {
183 if (stub_trace)
184 sx_trace ("os_print_string (\"%s\")\n", stub_notnull (string));
185
186 if (stub_print_string)
187 stub_print_string (string);
188 }
189
190 void
191 os_print_string_debug (const sc_char *string)
192 {
193 if (stub_trace)
194 sx_trace ("os_print_string_debug (\"%s\")\n", stub_notnull (string));
195
196 if (stub_print_string)
197 stub_print_string (string);
198 }
199
200 void
201 os_play_sound (const sc_char *filepath,
202 sc_int offset, sc_int length, sc_bool is_looping)
203 {
204 if (stub_trace)
205 sx_trace ("os_play_sound (\"%s\", %ld, %ld, %s)\n",
206 stub_notnull (filepath), offset, length,
207 is_looping ? "true" : "false");
208
209 if (stub_print_string && stub_show_resources > 0)
210 {
211 sc_char buffer[32];
212
213 stub_print_string ("<<Sound: id=\"");
214 stub_print_string (stub_notnull (filepath));
215 stub_print_string ("\", offset=");
216 sprintf (buffer, "%ld", offset);
217 stub_print_string (buffer);
218 stub_print_string (", length=");
219 sprintf (buffer, "%ld", length);
220 stub_print_string (buffer);
221 stub_print_string (", looping=");
222 stub_print_string (is_looping ? "true" : "false");
223 stub_print_string (">>");
224 }
225 }
226
227 void
228 os_stop_sound (void)
229 {
230 if (stub_trace)
231 sx_trace ("os_stop_sound ()\n");
232
233 if (stub_print_string && stub_show_resources > 0)
234 stub_print_string ("<<Sound: stop>>");
235 }
236
237 void
238 os_show_graphic (const sc_char *filepath, sc_int offset, sc_int length)
239 {
240 if (stub_trace)
241 sx_trace ("os_show_graphic (\"%s\", %ld, %ld)\n",
242 stub_notnull (filepath), offset, length);
243
244 if (stub_print_string && stub_show_resources > 0)
245 {
246 sc_char buffer[32];
247
248 stub_print_string ("<<Graphic: id=\"");
249 stub_print_string (stub_notnull (filepath));
250 stub_print_string ("\", offset=");
251 sprintf (buffer, "%ld", offset);
252 stub_print_string (buffer);
253 stub_print_string (", length=");
254 sprintf (buffer, "%ld", length);
255 stub_print_string (buffer);
256 stub_print_string (">>");
257 }
258 }
259
260 sc_bool
261 os_read_line (sc_char *buffer, sc_int length)
262 {
263 sc_bool status;
264
265 if (stub_read_line)
266 status = stub_read_line (buffer, length);
267 else
268 {
269 assert (buffer && length > 4);
270 sprintf (buffer, "%s", "quit");
271 status = TRUE;
272 }
273
274 if (stub_trace)
275 {
276 if (status)
277 sx_trace ("os_read_line (\"%s\", %ld) -> true\n",
278 stub_notnull (buffer), length);
279 else
280 sx_trace ("os_read_line (\"...\", %ld) -> false\n", length);
281 }
282 return status;
283 }
284
285 sc_bool
286 os_read_line_debug (sc_char *buffer, sc_int length)
287 {
288 assert (buffer && length > 8);
289 sprintf (buffer, "%s", "continue");
290
291 if (stub_trace)
292 sx_trace ("os_read_line_debug (\"%s\", %ld) -> true\n", buffer, length);
293 return TRUE;
294 }
295
296 sc_bool
297 os_confirm (sc_int type)
298 {
299 if (stub_trace)
300 sx_trace ("os_confirm (%ld) -> true\n", type);
301 return TRUE;
302 }
303
304 void *
305 os_open_file (sc_bool is_save)
306 {
307 void *opaque;
308
309 if (stub_open_file)
310 opaque = stub_open_file (is_save);
311 else
312 opaque = NULL;
313
314 if (stub_trace)
315 {
316 if (opaque)
317 sx_trace ("os_open_file (%s) -> %p\n",
318 is_save ? "true" : "false", opaque);
319 else
320 sx_trace ("os_open_file (%s) -> null\n", is_save ? "true" : "false");
321 }
322 return opaque;
323 }
324
325 sc_int
326 os_read_file (void *opaque, sc_byte *buffer, sc_int length)
327 {
328 sc_int bytes;
329
330 if (stub_read_file)
331 bytes = stub_read_file (opaque, buffer, length);
332 else
333 bytes = 0;
334
335 if (stub_trace)
336 sx_trace ("os_read_file (%p, %p, %ld) -> %ld\n",
337 opaque, buffer, length, bytes);
338 return bytes;
339 }
340
341 void
342 os_write_file (void *opaque, const sc_byte *buffer, sc_int length)
343 {
344 if (stub_write_file)
345 stub_write_file (opaque, buffer, length);
346
347 if (stub_trace)
348 sx_trace ("os_write_file (%p, %p, %ld)\n", opaque, buffer, length);
349 }
350
351 void
352 os_close_file (void *opaque)
353 {
354 if (stub_close_file)
355 stub_close_file (opaque);
356
357 if (stub_trace)
358 sx_trace ("os_close_file (%p)\n", opaque);
359 }
360
361 void
362 os_display_hints (sc_game game)
363 {
364 if (stub_trace)
365 sx_trace ("os_display_hints (%p)\n", game);
366
367 if (stub_print_string)
368 {
369 sc_game_hint hint;
370
371 for (hint = sc_get_first_game_hint (game);
372 hint; hint = sc_get_next_game_hint (game, hint))
373 {
374 const sc_char *hint_text;
375
376 stub_print_string (sc_get_game_hint_question (game, hint));
377 stub_print_string ("\n");
378
379 hint_text = sc_get_game_subtle_hint (game, hint);
380 if (hint_text)
381 {
382 stub_print_string ("- ");
383 stub_print_string (hint_text);
384 stub_print_string ("\n");
385 }
386
387 hint_text = sc_get_game_unsubtle_hint (game, hint);
388 if (hint_text)
389 {
390 stub_print_string ("- ");
391 stub_print_string (hint_text);
392 stub_print_string ("\n");
393 }
394 }
395 }
396 }
397
398
399 /*
400 * stub_debug_trace()
401 *
402 * Set stubs tracing on/off.
403 */
404 void
405 stub_debug_trace (sc_bool flag)
406 {
407 stub_trace = flag;
408 }