#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
  char master[1024], new[1024];
  char *s, *test;
  FILE *skeezix = fopen("SKEEZIX.TRACE", "r");
  int i;

  /* File param is skeezix's trace of a tailgunner run */
  /* stdin is my trace.  Read from both in synch until */
  /* a difference is found.  Print context and exit.   */
  /* Take care in places where our formats don't match */
  /* Such as shifts and WAI instructions.              */

  for (i = 0; ; i++) {
    for (;;) {
      /* Get a master line */

      /* Heading:
      Retrocade - Version 1.2b2, Copyright (C) 1998 by the Retrocade group
      UNIX Version

      (null)(0): Can't open file cabinet.ini
      Loading... [Reg=Ok] [Games=OK] [CPU=OK] [CTRL=OK] Done.
      CCPU JMI Set: No.
      CCPU Address Space: 8k
      CCPU Monitor: bi-level-display
      Rest:
      0000: 57    : nopb        : A=000 B=000 I=000Z J=000 P=0 A0=00 00 N0 O0   B 
      */

      strcpy(master, "                                                             ");
      s = fgets(master, 1023, skeezix);
      if (s == NULL) {
        fprintf(stdout, "Success!  You got to the end of the listing!\n");
        exit(0);
      }
      s = strchr(master, '\n'); if (s != NULL) *s = '\0';
      /* Discard junk for now */
      if (strncmp(master+14, "nopa ", 5) == 0) continue;
      if (strncmp(master+14, "asr ", 4) == 0) continue;
      if (strncmp(master+14, "lsr ", 4) == 0) continue;
      if (strncmp(master+14, "lsl ", 4) == 0) continue;
      if (strncmp(master+14, "vdr ", 4) == 0) continue;
      if (master[26] == ':') break;
    }
    /* Get corresponding line from mine. */

    for (;;) {
      /* Get a line from the new test stream */

      /* First line:
      A=000 B=000 I=00 J=000 P=0 a=0 C=0 N=000 O=000 c=00000000
      Rest:
      A=000 B=000 I=00 J=000 P=0 a=0 C=0 N=000 O=000 c=00000000 | 0000: 57    USB        : A=000 B=000 I=00 J=000 P=0 a=0 C=0 N=000 O=000 c=00000002
      */

      s = fgets(new, 1023, stdin);
      if (s == NULL) {
        fprintf(stdout, "Error?  Premature close on stdin?\n");
        exit(1);
      }
      s = strchr(new, '\n'); if (s != NULL) *s = '\0';

      if (strncmp(new, "Error", 4) == 0) {
        fprintf(stdout, "%s\n", new);
        exit(1);
      }

      /* Discard junk for now */
      if (strncmp(new+60+14, "NOP ", 4) == 0) continue;
      if (strncmp(new+60+14, "WAI ", 4) == 0) continue;
      if (strncmp(new+60+14, "ASR ", 4) == 0) continue;
      if (strncmp(new+60+14, "LSR ", 4) == 0) continue;
      if (strncmp(new+60+14, "LSL ", 4) == 0) continue;
      if (strncmp(new+60+14, "VDR ", 4) == 0) continue;
      if (new[58] == '|') break;
    }
    /* Compare */
    test = new+60;
    fprintf(stdout, "Correct: %s\nTest   : %s\n\n", master, test);
    if (strncmp(master, test, 4) != 0) {
      exit(1);
    }
  }

  exit(0);
}

