Debian Codegraph @

terps/scare/sxscript.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 The script file format is as follows. Lines beginning '#' are comments
24 * and empty lines are ignored, otherwise the file is composed of sections.
25 * The first section line is one that starts with either '>' or '~'. This
26 * is the next command. The following lines, up to the next '>' or '~'
27 * section start, are concatenated into the expectation for the command.
28 * Expectations are glob patterns. Commands starting with '>' are sent to
29 * the game; those starting with '~' are sent to the SCARE debugger. Before
30 * the game is running, debugger commands are valid. The first non-debugger
31 * command starts the game running. An empty debugger command ('~') that
32 * follows any introductory debugger commands both starts the game and sets
33 * an expectation for the game's introductory text. After the game has
34 * completed (or quit), only debugger commands are valid; others are ignored.
35 *
36 * o The script file structure is intentionally simple, but might be too
37 * simple for some purposes.
38 */
39
40 #include <assert.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 #include "scare.h"
46 #include "sxprotos.h"
47
48
49 /* Assorted definitions and constants. */
50 static const sc_int LINE_BUFFER_SIZE = 256;
51 static const sc_char NUL = '\0';
52 static const sc_char SCRIPT_COMMENT = '#';
53 static const sc_char GAME_COMMAND = '>';
54 static const sc_char DEBUG_COMMAND = '~';
55
56 /* Verbosity, and references to the game and script being processed. */
57 static sc_bool scr_is_verbose = FALSE;
58 static sc_game scr_game = NULL;
59 static sx_script scr_script = NULL;
60
61 /* Script line number, and count of errors registered for the script. */
62 static sc_int scr_line_number = 0;
63 static sc_int scr_errors = 0;
64
65 /*
66 * Current expected output, and game accumulated output, used by the
67 * expectation checking function.
68 */
69 static sc_char *scr_expectation = NULL;
70 static sc_char *scr_game_output = NULL;
71
72
73 /*
74 * scr_set_verbose()
75 *
76 * Set error reporting for expectation errors detected in the script.
77 */
78 void
79 scr_set_verbose (sc_bool flag)
80 {
81 scr_is_verbose = flag;
82 }
83
84
85 /*
86 * scr_test_message()
87 * scr_test_failed()
88 *
89 * Simple common message and test case failure handling functions. The second
90 * is used by the serialization helper, so is not static.
91 */
92 static void
93 scr_test_message (const sc_char *format, const sc_char *string)
94 {
95 if (scr_is_verbose)
96 {
97 sx_trace ("--- ");
98 sx_trace (format, string);
99 sx_trace ("\n");
100 }
101 }
102
103 void
104 scr_test_failed (const sc_char *format, const sc_char *string)
105 {
106 assert (format && string);
107
108 if (scr_is_verbose)
109 {
110 if (scr_line_number > 0)
111 sx_trace ("--- Near line %ld: ", scr_line_number);
112 else
113 sx_trace ("--- ");
114 sx_trace (format, string);
115 sx_trace ("\n");
116 }
117 scr_errors++;
118 }
119
120
121 /*
122 * scr_is_line_type()
123 * scr_is_line_comment_or_empty()
124 * scr_is_line_game_command()
125 * scr_is_line_debug_command()
126 * scr_is_line_command()
127 * scr_is_line_empty_debug_command()
128 *
129 * Line classifiers, return TRUE if line has the given type.
130 */
131 static sc_bool
132 scr_is_line_type (const sc_char *line, sc_char type)
133 {
134 return line[0] == type;
135 }
136
137 static sc_bool
138 scr_is_line_comment_or_empty (const sc_char *line)
139 {
140 return scr_is_line_type (line, SCRIPT_COMMENT)
141 || strspn (line, "\t\n\v\f\r ") == strlen (line);
142 }
143
144 static sc_bool
145 scr_is_line_game_command (const sc_char *line)
146 {
147 return scr_is_line_type (line, GAME_COMMAND);
148 }
149
150 static sc_bool
151 scr_is_line_debug_command (const sc_char *line)
152 {
153 return scr_is_line_type (line, DEBUG_COMMAND);
154 }
155
156 static sc_bool
157 scr_is_line_command (const sc_char *line)
158 {
159 return scr_is_line_game_command (line) || scr_is_line_debug_command (line);
160 }
161
162 static sc_bool
163 scr_is_line_empty_debug_command (const sc_char *line)
164 {
165 return scr_is_line_type (line, DEBUG_COMMAND) && line[1] == NUL;
166 }
167
168
169 /* Script location, a pair holding the file location and the line number. */
170 typedef struct
171 {
172 fpos_t position;
173 sc_int line_number;
174 } sx_scr_location_t;
175 typedef sx_scr_location_t *sx_scr_locationref_t;
176
177 /*
178 * scr_save_location()
179 * scr_restore_location()
180 *
181 * Save and restore the script location in the given structure.
182 */
183 static void
184 scr_save_location (sx_script script, sx_scr_locationref_t location)
185 {
186 fgetpos (script, &location->position);
187 location->line_number = scr_line_number;
188 }
189
190 static void
191 scr_restore_location (sx_script script, sx_scr_locationref_t location)
192 {
193 fsetpos (script, &location->position);
194 scr_line_number = location->line_number;
195 }
196
197
198 /*
199 * scr_get_next_line()
200 *
201 * Helper for scr_get_next_section(). Returns the next non-comment, non-empty
202 * line from the script. Returns NULL if no more lines, or on file error. The
203 * return string is allocated, and it's the caller's responsibility to free it.
204 */
205 static sc_char *
206 scr_get_next_line (sx_script script)
207 {
208 sc_char *buffer, *line = NULL;
209
210 /* Allocate a buffer for line reads. */
211 buffer = sx_malloc (LINE_BUFFER_SIZE);
212
213 /* Read until a significant line is found, or end of file or error. */
214 while (fgets (buffer, LINE_BUFFER_SIZE, script))
215 {
216 scr_line_number++;
217 if (!scr_is_line_comment_or_empty (buffer))
218 {
219 line = buffer;
220 break;
221 }
222 }
223
224 /* If no significant line read, free the read buffer. */
225 if (!line)
226 sx_free (buffer);
227
228 return line;
229 }
230
231
232 /*
233 * scr_concatenate()
234 *
235 * Helper for scr_get_next_section(). Builds a string formed by concatenating
236 * the second argument to the first. If the first is NULL, acts as strdup()
237 * instead.
238 */
239 static sc_char *
240 scr_concatenate (sc_char *string, const sc_char *buffer)
241 {
242 /* If string is not null, concatenate buffer, otherwise duplicate. */
243 if (string)
244 {
245 string = sx_realloc (string,
246 strlen (string) + 1 + strlen (buffer) + 1);
247 strcat (string, " ");
248 strcat (string, buffer);
249 }
250 else
251 {
252 string = sx_malloc (strlen (buffer) + 1);
253 strcpy (string, buffer);
254 }
255
256 return string;
257 }
258
259
260 /*
261 * scr_get_next_section()
262 *
263 * Retrieve the next command and any expectation from the script file.
264 * Returns TRUE if a line is returned, FALSE at end-of-file. Expectation may
265 * be NULL if this paragraph doesn't have one; command may not be (if TRUE is
266 * returned). Command and expectation are allocated, and the caller needs to
267 * free them.
268 */
269 static sc_bool
270 scr_get_next_section (sx_script script,
271 sc_char **command, sc_char **expectation)
272 {
273 sc_char *line, *first_line, *other_lines;
274 sx_scr_location_t location;
275
276 /* Clear initial line accumulation. */
277 first_line = other_lines = NULL;
278
279 /* Read the next significant line from the script. */
280 scr_save_location (script, &location);
281 line = scr_get_next_line (script);
282 while (line)
283 {
284 /* If already a first line, this is other lines or section end. */
285 if (first_line)
286 {
287 /*
288 * If we found the start of the next section, reset the script
289 * location that saved on the line read, and we're done.
290 */
291 if (scr_is_line_command (line))
292 {
293 scr_restore_location (script, &location);
294 sx_free (line);
295 break;
296 }
297 else
298 other_lines = scr_concatenate (other_lines, line);
299 }
300 else
301 first_line = scr_concatenate (first_line, line);
302
303 sx_free (line);
304
305 /* Read the next significant line from the script. */
306 scr_save_location (script, &location);
307 line = scr_get_next_line (script);
308 }
309
310 /* Clean up and return nothing on file error. */
311 if (ferror (script))
312 {
313 scr_test_failed ("Script error: Failed reading script input file", "");
314 sx_free (first_line);
315 sx_free (other_lines);
316 return FALSE;
317 }
318
319 /* Return the command and the matching expectation string, if any. */
320 if (first_line)
321 {
322 *command = sx_normalize_string (first_line);
323 *expectation = other_lines ? sx_normalize_string (other_lines) : NULL;
324 return TRUE;
325 }
326
327 /* End of file, no command section read. */
328 return FALSE;
329 }
330
331
332 /*
333 * scr_expect()
334 * scr_verify_expectation()
335 *
336 * Set an expectation, and compare the expectation, if any, with the
337 * accumulated game output, using glob matching. scr_verify_expectation()
338 * increments the error count if the expectation isn't met, and reports the
339 * error if required. It then frees both the expectation and accumulated
340 * input.
341 */
342 static void
343 scr_expect (sc_char *expectation)
344 {
345 /*
346 * Save the expectation, and set up collection of game output if needed.
347 * And if not needed, ensure expectation and game output are cleared.
348 */
349 if (expectation)
350 {
351 scr_expectation = sx_malloc (strlen (expectation) + 1);
352 strcpy (scr_expectation, expectation);
353 scr_game_output = sx_malloc (1);
354 strcpy (scr_game_output, "");
355 }
356 else
357 {
358 sx_free (scr_expectation);
359 scr_expectation = NULL;
360 sx_free (scr_game_output);
361 scr_game_output = NULL;
362 }
363 }
364
365 static void
366 scr_verify_expectation (void)
367 {
368 /* Compare expected with actual, and handle any error detected. */
369 if (scr_expectation && scr_game_output)
370 {
371 scr_game_output = sx_normalize_string (scr_game_output);
372 if (!glob_match (scr_expectation, scr_game_output))
373 {
374 scr_test_failed ("Expectation error:", "");
375 scr_test_message (" Expected: \"%s\"", scr_expectation);
376 scr_test_message (" Received: \"%s\"", scr_game_output);
377 }
378 }
379
380 /* Dispose of the expectation and accumulated game output. */
381 sx_free (scr_expectation);
382 scr_expectation = NULL;
383 sx_free (scr_game_output);
384 scr_game_output = NULL;
385 }
386
387
388 /*
389 * scr_execute_debugger_command()
390 *
391 * Convenience interface for immediate execution of debugger commands. This
392 * function directly calls the debugger interface, and because it's immediate,
393 * can also verify the expectation before returning to the caller.
394 *
395 * Also, it turns on the game debugger, and it's the caller's responsibility
396 * to turn it off when it's no longer needed.
397 */
398 static void
399 scr_execute_debugger_command (const sc_char *command, sc_char *expectation)
400 {
401 sc_bool status;
402
403 /* Set up the expectation. */
404 scr_expect (expectation);
405
406 /*
407 * Execute the command via the debugger interface. The "+1" on command
408 * skips the leading '~' read in from the game script.
409 */
410 sc_set_game_debugger_enabled (scr_game, TRUE);
411 status = sc_run_game_debugger_command (scr_game, command + 1);
412
413 if (!status)
414 {
415 scr_test_failed ("Script error:"
416 " Debug command \"%s\" is not valid", command);
417 }
418
419 /* Check expectations immediately. */
420 scr_verify_expectation ();
421 }
422
423
424 /*
425 * scr_read_line_callback()
426 *
427 * Check any expectations set for the last line. Consult the script for the
428 * next line to feed to the game, and any expectation for the game output
429 * for that line. If there is an expectation, save it and set scr_game_output
430 * to "" so that accumulation begins. Then pass the next line of data back
431 * to the game.
432 */
433 static sc_bool
434 scr_read_line_callback (sc_char *buffer, sc_int length)
435 {
436 sc_char *command, *expectation;
437 assert (buffer && length > 0);
438
439 /* Check pending expectation, and clear settings for the next line. */
440 scr_verify_expectation ();
441
442 /* Get the next line-expectation pair from the script stream. */
443 if (scr_get_next_section (scr_script, &command, &expectation))
444 {
445 if (scr_is_line_debug_command (command))
446 {
447 /* The debugger persists where debug commands are adjacent. */
448 scr_execute_debugger_command (command, expectation);
449 sx_free (command);
450 sx_free (expectation);
451
452 /*
453 * Returning FALSE here causes the game to re-prompt. We could
454 * loop (or tail recurse) ourselves, but returning is simpler.
455 */
456 return FALSE;
457 }
458 else
459 sc_set_game_debugger_enabled (scr_game, FALSE);
460
461 if (scr_is_line_game_command (command))
462 {
463 /* Set up the expectation. */
464 scr_expect (expectation);
465
466 /* Copy out the line to the return buffer, and free the line. */
467 strncpy (buffer, command + 1, length);
468 buffer[length - 1] = NUL;
469 sx_free (command);
470 sx_free (expectation);
471 return TRUE;
472 }
473
474 /* Neither a '~' nor a '>' command. */
475 scr_test_failed ("Script error:"
476 " Command \"%s\" is not valid, ignored", command);
477 sx_free (command);
478 sx_free (expectation);
479 return FALSE;
480 }
481
482 /* Ensure the game debugger is off after this section. */
483 sc_set_game_debugger_enabled (scr_game, FALSE);
484
485 /*
486 * We reached the end of the script without finding a "quit" command.
487 * Supply one here, then. In the unlikely even that this does not quit
488 * the game, we'll iterate on this.
489 */
490 assert (length > 4);
491 strcpy (buffer, "quit");
492 return TRUE;
493 }
494
495
496 /*
497 * scr_print_string_callback()
498 *
499 * Handler function for game output. Accumulates strings received from the
500 * game into scr_game_output, unless no expectation is set, in which case
501 * the current game output will be NULL, and we can simply save the effort.
502 */
503 static void
504 scr_print_string_callback (const sc_char *string)
505 {
506 assert (string);
507
508 if (scr_game_output)
509 {
510 scr_game_output = sx_realloc (scr_game_output,
511 strlen (scr_game_output)
512 + strlen (string) + 1);
513 strcat (scr_game_output, string);
514 }
515 }
516
517
518 /*
519 * scr_start_script()
520 *
521 * Set up game monitoring so that each request for a line from the game
522 * enters this module. For each request, we grab the next "send" and
523 * "expect" pair from the script, satisfy the request with the send data,
524 * and match against the expectations on next request or on finalization.
525 */
526 void
527 scr_start_script (sc_game game, sx_script script)
528 {
529 sc_char *command, *expectation;
530 sx_scr_location_t location;
531 assert (game && script);
532
533 /* Save the game and stream, and clear the line number and errors count. */
534 assert (!scr_game && !scr_script);
535 scr_game = game;
536 scr_script = script;
537 scr_line_number = 0;
538 scr_errors = 0;
539
540 /* Set up our callback functions to catch game i/o. */
541 stub_attach_handlers (scr_read_line_callback, scr_print_string_callback,
542 file_open_file_callback, file_read_file_callback,
543 file_write_file_callback, file_close_file_callback);
544
545 /*
546 * Handle any initial debugging commands, terminating on either a non-
547 * debugging one or an expectation for the game intro.
548 */
549 rewind (scr_script);
550 scr_save_location (scr_script, &location);
551 while (scr_get_next_section (scr_script, &command, &expectation))
552 {
553 if (scr_is_line_debug_command (command))
554 {
555 if (scr_is_line_empty_debug_command (command))
556 {
557 /* It's an intro expectation - set and break loop. */
558 scr_expect (expectation);
559 sx_free (command);
560 sx_free (expectation);
561 break;
562 }
563 else
564 {
565 /* It's a full debug command - execute it as one. */
566 scr_execute_debugger_command (command, expectation);
567 sx_free (command);
568 sx_free (expectation);
569 }
570 }
571 else
572 {
573 /*
574 * It's an ordinary section - rewind so that it's the first one
575 * handled in the callback, and break loop.
576 */
577 scr_restore_location (scr_script, &location);
578 sx_free (command);
579 sx_free (expectation);
580 break;
581 }
582
583 /* Note script position before reading the next section. */
584 scr_save_location (scr_script, &location);
585 }
586
587 /* Ensure the game debugger is off after this section. */
588 sc_set_game_debugger_enabled (scr_game, FALSE);
589 }
590
591
592 /*
593 * scr_finalize_script()
594 *
595 * Match any final received string against a possible expectation, and then
596 * clear local records of the game, stream, and error count. Returns the
597 * count of errors detected during the script.
598 */
599 sc_int
600 scr_finalize_script (void)
601 {
602 sc_char *command, *expectation;
603 sc_int errors;
604
605 /* Check pending expectation, and clear settings. */
606 scr_verify_expectation ();
607
608 /* Drain the remainder of the script, ignoring non-debugging commands. */
609 while (scr_get_next_section (scr_script, &command, &expectation))
610 {
611 if (scr_is_line_debug_command (command))
612 {
613 scr_execute_debugger_command (command, expectation);
614 sx_free (command);
615 sx_free (expectation);
616 }
617 else
618 {
619 /* Complain about script entries ignored because the game ended. */
620 scr_test_failed ("Script error:"
621 " Game completed, command \"%s\" ignored", command);
622 sx_free (command);
623 sx_free (expectation);
624 }
625 }
626
627 /* Ensure the game debugger is off after this section. */
628 sc_set_game_debugger_enabled (scr_game, FALSE);
629
630 /*
631 * Remove our callback functions from the stubs, and "close" any retained
632 * stream data from game save/load tests.
633 */
634 stub_detach_handlers ();
635 file_cleanup ();
636
637 /* Clear local records of game stream, line number, and errors count. */
638 errors = scr_errors;
639 scr_game = NULL;
640 scr_script = NULL;
641 scr_line_number = 0;
642 scr_errors = 0;
643
644 return errors;
645 }