| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 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 |
|
| 57 |
static sc_bool scr_is_verbose = FALSE; |
| 58 |
static sc_game scr_game = NULL; |
| 59 |
static sx_script scr_script = NULL; |
| 60 |
|
| 61 |
|
| 62 |
static sc_int scr_line_number = 0; |
| 63 |
static sc_int scr_errors = 0; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
static sc_char *scr_expectation = NULL; |
| 70 |
static sc_char *scr_game_output = NULL; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
void |
| 79 |
scr_set_verbose (sc_bool flag) |
| 80 |
{ |
| 81 |
scr_is_verbose = flag; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 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 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 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 |
|
| 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 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 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 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
static sc_char * |
| 206 |
scr_get_next_line (sx_script script) |
| 207 |
{ |
| 208 |
sc_char *buffer, *line = NULL; |
| 209 |
|
| 210 |
|
| 211 |
buffer = sx_malloc (LINE_BUFFER_SIZE); |
| 212 |
|
| 213 |
|
| 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 |
|
| 225 |
if (!line) |
| 226 |
sx_free (buffer); |
| 227 |
|
| 228 |
return line; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
static sc_char * |
| 240 |
scr_concatenate (sc_char *string, const sc_char *buffer) |
| 241 |
{ |
| 242 |
|
| 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 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 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 |
|
| 277 |
first_line = other_lines = NULL; |
| 278 |
|
| 279 |
|
| 280 |
scr_save_location (script, &location); |
| 281 |
line = scr_get_next_line (script); |
| 282 |
while (line) |
| 283 |
{ |
| 284 |
|
| 285 |
if (first_line) |
| 286 |
{ |
| 287 |
|
| 288 |
|
| 289 |
|
| 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 |
|
| 306 |
scr_save_location (script, &location); |
| 307 |
line = scr_get_next_line (script); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 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 |
|
| 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 |
|
| 328 |
return FALSE; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
static void |
| 343 |
scr_expect (sc_char *expectation) |
| 344 |
{ |
| 345 |
|
| 346 |
|
| 347 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
static void |
| 399 |
scr_execute_debugger_command (const sc_char *command, sc_char *expectation) |
| 400 |
{ |
| 401 |
sc_bool status; |
| 402 |
|
| 403 |
|
| 404 |
scr_expect (expectation); |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 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 |
|
| 420 |
scr_verify_expectation (); |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 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 |
|
| 440 |
scr_verify_expectation (); |
| 441 |
|
| 442 |
|
| 443 |
if (scr_get_next_section (scr_script, &command, &expectation)) |
| 444 |
{ |
| 445 |
if (scr_is_line_debug_command (command)) |
| 446 |
{ |
| 447 |
|
| 448 |
scr_execute_debugger_command (command, expectation); |
| 449 |
sx_free (command); |
| 450 |
sx_free (expectation); |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 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 |
|
| 464 |
scr_expect (expectation); |
| 465 |
|
| 466 |
|
| 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 |
|
| 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 |
|
| 483 |
sc_set_game_debugger_enabled (scr_game, FALSE); |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
assert (length > 4); |
| 491 |
strcpy (buffer, "quit"); |
| 492 |
return TRUE; |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 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 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 547 |
|
| 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 |
|
| 558 |
scr_expect (expectation); |
| 559 |
sx_free (command); |
| 560 |
sx_free (expectation); |
| 561 |
break; |
| 562 |
} |
| 563 |
else |
| 564 |
{ |
| 565 |
|
| 566 |
scr_execute_debugger_command (command, expectation); |
| 567 |
sx_free (command); |
| 568 |
sx_free (expectation); |
| 569 |
} |
| 570 |
} |
| 571 |
else |
| 572 |
{ |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
scr_restore_location (scr_script, &location); |
| 578 |
sx_free (command); |
| 579 |
sx_free (expectation); |
| 580 |
break; |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
scr_save_location (scr_script, &location); |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
sc_set_game_debugger_enabled (scr_game, FALSE); |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
sc_int |
| 600 |
scr_finalize_script (void) |
| 601 |
{ |
| 602 |
sc_char *command, *expectation; |
| 603 |
sc_int errors; |
| 604 |
|
| 605 |
|
| 606 |
scr_verify_expectation (); |
| 607 |
|
| 608 |
|
| 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 |
|
| 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 |
|
| 628 |
sc_set_game_debugger_enabled (scr_game, FALSE); |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
stub_detach_handlers (); |
| 635 |
file_cleanup (); |
| 636 |
|
| 637 |
|
| 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 |
} |