/* Copyright 1989, 1990 by James Aspnes, David Applegate, and Bennet Yee */
/* See the file COPYING for distribution information */
#include <stdio.h>
#include <ctype.h>

#include "db.h"
#include "globals.h"
#include "bytecode.h"
#include "externs.h"

extern byte *compile (const char *);

datum me;
datum you;
datum text;
datum mtext;

int please_gc = 0;
int please_checkpoint = 0;
int shutdown_flag = 0;

int main (int argc, char **argv)
{
  FILE *f;
  char buf[2048];

  if (argc < 2) {
    fprintf (stderr, "Usage: %s db-file\n", argv[0]);
    return (1);
  }

  if ((f = fopen (argv[1], "rb")) == NULL) {
    perror ("Couldn't open database.\n");
    return (2);
  }

  if (db_read (f) < 0) {
    fprintf (stderr, "Couldn't read database from %s\n", argv[1]);
    return (3);
  } else {
    fclose (f);
  }

  while (!shutdown_flag && gets (buf)) {
    parse_command (1, buf);
    if (please_gc) {
      full_gc ();
      please_gc = 0;
    } else {
      incremental_gc ();
    }
  }

  db_write (stdout);
  return 0;
}

void notify (datum victim, const char *buf)
{
  printf ("%ld: %s\n", victim, buf);
}