[Scummvm-cvs-logs] SF.net SVN: scummvm:[38485] scummvm/trunk/engines/sci/engine

sev at users.sourceforge.net sev at users.sourceforge.net
Wed Feb 18 16:05:01 CET 2009


Revision: 38485
          http://scummvm.svn.sourceforge.net/scummvm/?rev=38485&view=rev
Author:   sev
Date:     2009-02-18 15:05:00 +0000 (Wed, 18 Feb 2009)

Log Message:
-----------
restore removed files

Added Paths:
-----------
    scummvm/trunk/engines/sci/engine/cfsml.pl
    scummvm/trunk/engines/sci/engine/said.y
    scummvm/trunk/engines/sci/engine/savegame.cfsml

Copied: scummvm/trunk/engines/sci/engine/cfsml.pl (from rev 38466, scummvm/trunk/engines/sci/engine/cfsml.pl)
===================================================================
--- scummvm/trunk/engines/sci/engine/cfsml.pl	                        (rev 0)
+++ scummvm/trunk/engines/sci/engine/cfsml.pl	2009-02-18 15:05:00 UTC (rev 38485)
@@ -0,0 +1,1183 @@
+#! /usr/bin/env perl
+# The C File Storage Meta Language "reference" implementation
+# This implementation is supposed to conform to version
+$version = "0.8.2";
+# of the spec. Please contact the maintainer if it doesn't.
+#
+# cfsml.pl Copyright (C) 1999, 2000, 2001 Christoph Reichenbach
+#
+#
+# This program may be modified and copied freely according to the terms of
+# the GNU general public license (GPL), as long as the above copyright
+# notice and the licensing information contained herein are preserved.
+#
+# Please refer to www.gnu.org for licensing details.
+#
+# This work is provided AS IS, without warranty of any kind, expressed or
+# implied, including but not limited to the warranties of merchantibility,
+# noninfringement, and fitness for a specific purpose. The author will not
+# be held liable for any damage caused by this work or derivatives of it.
+#
+# By using this source code, you agree to the licensing terms as stated
+# above.
+#
+#
+# Please contact the maintainer for bug reports or inquiries.
+#
+# Current Maintainer:
+#
+#    Christoph Reichenbach (CJR) [jameson at linuxgames.com]
+#
+#
+# Warning: This is still a "bit" messy. Sorry for that.
+#
+
+#$debug = 1;
+
+$write_lines = "true";
+$source_file = "CFSML source file";
+$type_integer = "integer";
+$type_string = "string";
+$type_record = "RECORD";
+$type_pointer = "POINTER";
+$type_abspointer = "ABSPOINTER";
+
+%types;      # Contains all type bindings
+%records;    # Contains all record bindings
+
+$mode = undef;
+while ($op = shift @ARGV) {
+    if ($mode eq undef) {
+	if ($op eq "-f") {
+	    $mode = "fname";
+	} elsif ($op eq "-l") {
+	    $write_lines = undef;
+	} elsif ($op eq "-v") {
+	    print "cfsml.pl, the CFSML code generator, version $version\n";
+	    print "This program is provided WITHOUT WARRANTY of any kind. It may be\n";
+	    print "copied and modified freely according to the terms of the GNU\n";
+	    print "General Public License.\n";
+	    exit(0);
+	} elsif ($op eq "-h") {
+	    print "CFSML help:\n";
+	    print "Usage: cat source | cfsml.pl [-v] [-h] [-l] [-f <filename>] > dest\n";
+	    print "  -h : help\n";
+	    print "  -v : print version\n";
+	    print "  -l : disable line number printing in dest file\n";
+	    print "  -f : specify file name for line number printing\n";
+	    exit(0);
+	} else {
+	    die "Unknown option '$op'\n";
+	}
+    } elsif ($mode eq "fname") {
+	$source_file = $op;
+	$mode = 0;
+    } else {
+	die "Invalid internal state '$mode'\n";
+    }
+}
+
+sub write_line_pp
+# write_line_pp(int line_nr, bool input_file?)
+{
+    my $line_nr = shift;
+    my $_file = shift;
+    my $filename = "cfsml.pl";
+
+    if (_file) {
+	$filename = $source_file;
+    }
+
+    if ($write_lines) {
+	print "#line $line_nr \"$filename\"\n";
+    }
+}
+
+sub create_string_functions
+  {
+    $firstline = __LINE__;
+    $firstline += 4;
+    write_line_pp($firstline, 0);
+    print <<'EOF';
+
+#include <stdarg.h> /* We need va_lists */
+#include "sci/include/sci_memory.h"
+
+#ifdef CFSML_DEBUG_MALLOC
+/*
+#define free(p)        dbg_sci_free(p)
+#define malloc(s)      dbg_sci_malloc(s)
+#define calloc(n, s)   dbg_sci_calloc(n, s)
+#define realloc(p, s)  dbg_sci_realloc(p, s)
+*/
+#define free        dbg_sci_free
+#define malloc      dbg_sci_malloc
+#define calloc      dbg_sci_calloc
+#define realloc     dbg_sci_realloc
+#endif
+
+static void
+_cfsml_error(const char *fmt, ...)
+{
+  va_list argp;
+
+  fprintf(stderr, "Error: ");
+  va_start(argp, fmt);
+  vfprintf(stderr, fmt, argp);
+  va_end(argp);
+
+}
+
+
+static struct _cfsml_pointer_refstruct {
+    struct _cfsml_pointer_refstruct *next;
+    void *ptr;
+} *_cfsml_pointer_references = NULL;
+
+static struct _cfsml_pointer_refstruct **_cfsml_pointer_references_current = &_cfsml_pointer_references;
+
+static char *_cfsml_last_value_retrieved = NULL;
+static char *_cfsml_last_identifier_retrieved = NULL;
+
+static void
+_cfsml_free_pointer_references_recursively(struct _cfsml_pointer_refstruct *refs, int free_pointers)
+{
+    if (!refs)
+	return;
+    #ifdef CFSML_DEBUG_MALLOC
+    SCI_MEMTEST;
+    #endif
+
+    _cfsml_free_pointer_references_recursively(refs->next, free_pointers);
+    #ifdef CFSML_DEBUG_MALLOC
+    SCI_MEMTEST;
+
+    fprintf(stderr,"Freeing ptrref %p [%p] %s\n", refs->ptr, refs, free_pointers?
+	    "ALL": "cleanup only");
+    #endif
+
+    if (free_pointers)
+	free(refs->ptr);
+
+    #ifdef CFSML_DEBUG_MALLOC
+    SCI_MEMTEST;
+    #endif
+    free(refs);
+    #ifdef CFSML_DEBUG_MALLOC
+    SCI_MEMTEST;
+    #endif
+}
+
+static void
+_cfsml_free_pointer_references(struct _cfsml_pointer_refstruct **meta_ref, int free_pointers)
+{
+    _cfsml_free_pointer_references_recursively(*meta_ref, free_pointers);
+    *meta_ref = NULL;
+    _cfsml_pointer_references_current = meta_ref;
+}
+
+static struct _cfsml_pointer_refstruct **
+_cfsml_get_current_refpointer()
+{
+    return _cfsml_pointer_references_current;
+}
+
+static void _cfsml_register_pointer(void *ptr)
+{
+    struct _cfsml_pointer_refstruct *newref = (struct _cfsml_pointer_refstruct*)sci_malloc(sizeof (struct _cfsml_pointer_refstruct));
+    #ifdef CFSML_DEBUG_MALLOC
+    SCI_MEMTEST;
+    fprintf(stderr,"Registering ptrref %p [%p]\n", ptr, newref);
+    #endif
+    newref->next = *_cfsml_pointer_references_current;
+    newref->ptr = ptr;
+    *_cfsml_pointer_references_current = newref;
+}
+
+
+static char *
+_cfsml_mangle_string(const char *s)
+{
+  const char *source = s;
+  char c;
+  char *target = (char *) sci_malloc(1 + strlen(s) * 2); /* We will probably need less than that */
+  char *writer = target;
+
+  while ((c = *source++)) {
+
+    if (c < 32) { /* Special character? */
+      *writer++ = '\\'; /* Escape... */
+      c += ('a' - 1);
+    } else if (c == '\\' || c == '"')
+      *writer++ = '\\'; /* Escape, but do not change */
+    *writer++ = c;
+
+  }
+  *writer = 0; /* Terminate string */
+
+  return (char *) sci_realloc(target, strlen(target) + 1);
+}
+
+
+static char *
+_cfsml_unmangle_string(const char *s, unsigned int length)
+{
+  char *target = (char *) sci_malloc(1 + strlen(s));
+  char *writer = target;
+  const char *source = s;
+  const char *end = s + length;
+  char c;
+
+  while ((source != end) && (c = *source++) && (c > 31)) {
+    if (c == '\\') { /* Escaped character? */
+      c = *source++;
+      if ((c != '\\') && (c != '"')) /* Un-escape 0-31 only */
+	c -= ('a' - 1);
+    }
+    *writer++ = c;
+  }
+  *writer = 0; /* Terminate string */
+
+  return (char *) sci_realloc(target, strlen(target) + 1);
+}
+
+
+static char *
+_cfsml_get_identifier(FILE *fd, int *line, int *hiteof, int *assignment)
+{
+  int c;
+  int mem = 32;
+  int pos = 0;
+  int done = 0;
+  char *retval = (char *) sci_malloc(mem);
+
+  if (_cfsml_last_identifier_retrieved) {
+      free(_cfsml_last_identifier_retrieved);
+      _cfsml_last_identifier_retrieved = NULL;
+  }
+
+  while (isspace(c = fgetc(fd)) && (c != EOF));
+  if (c == EOF) {
+    _cfsml_error("Unexpected end of file at line %d\n", *line);
+    free(retval);
+    *hiteof = 1;
+    return NULL;
+  }
+
+  ungetc(c, fd);
+
+  while (((c = fgetc(fd)) != EOF) && ((pos == 0) || (c != '\n')) && (c != '=')) {
+
+     if (pos == mem - 1) /* Need more memory? */
+       retval = (char *) sci_realloc(retval, mem *= 2);
+
+     if (!isspace(c)) {
+        if (done) {
+           _cfsml_error("Single word identifier expected at line %d\n", *line);
+           free(retval);
+           return NULL;
+        }
+        retval[pos++] = c;
+     } else
+        if (pos != 0)
+           done = 1; /* Finished the variable name */
+        else if (c == '\n')
+           ++(*line);
+  }
+
+  if (c == EOF) {
+    _cfsml_error("Unexpected end of file at line %d\n", *line);
+    free(retval);
+    *hiteof = 1;
+    return NULL;
+  }
+
+  if (c == '\n') {
+    ++(*line);
+    if (assignment)
+      *assignment = 0;
+  } else
+    if (assignment)
+      *assignment = 1;
+
+  if (pos == 0) {
+    _cfsml_error("Missing identifier in assignment at line %d\n", *line);
+    free(retval);
+    return NULL;
+  }
+
+  if (pos == mem - 1) /* Need more memory? */
+     retval = (char *) sci_realloc(retval, mem += 1);
+
+  retval[pos] = 0; /* Terminate string */
+EOF
+
+if ($debug) {
+    print "  printf(\"identifier is '%s'\\n\", retval);\n";
+}
+
+  $firstline = __LINE__;
+  $firstline += 4;
+  write_line_pp($firstline, 0);
+  print <<'EOF2';
+
+  return _cfsml_last_identifier_retrieved = retval;
+}
+
+
+static char *
+_cfsml_get_value(FILE *fd, int *line, int *hiteof)
+{
+  int c;
+  int mem = 64;
+  int pos = 0;
+  char *retval = (char *) sci_malloc(mem);
+
+  if (_cfsml_last_value_retrieved) {
+      free(_cfsml_last_value_retrieved);
+      _cfsml_last_value_retrieved = NULL;
+  }
+
+  while (((c = fgetc(fd)) != EOF) && (c != '\n')) {
+
+     if (pos == mem - 1) /* Need more memory? */
+       retval = (char *) sci_realloc(retval, mem *= 2);
+
+     if (pos || (!isspace(c)))
+        retval[pos++] = c;
+
+  }
+
+  while ((pos > 0) && (isspace(retval[pos - 1])))
+     --pos; /* Strip trailing whitespace */
+
+  if (c == EOF)
+    *hiteof = 1;
+
+  if (pos == 0) {
+    _cfsml_error("Missing value in assignment at line %d\n", *line);
+    free(retval);
+    return NULL;
+  }
+
+  if (c == '\n')
+     ++(*line);
+
+  if (pos == mem - 1) /* Need more memory? */
+    retval = (char *) sci_realloc(retval, mem += 1);
+
+  retval[pos] = 0; /* Terminate string */
+EOF2
+
+    if ($debug) {
+	print "  printf(\"value is '%s'\\n\", retval);\n";
+    }
+
+    $firstline = __LINE__;
+    $firstline += 4;
+    write_line_pp($firstline, 0);
+  print <<'EOF3';
+  return (_cfsml_last_value_retrieved = (char *) sci_realloc(retval, strlen(retval) + 1));
+  /* Re-allocate; this value might be used for quite some while (if we are
+  ** restoring a string)
+  */
+}
+EOF3
+  }
+
+
+# Call with $expression as a simple expression, like "tos + 1".
+# Returns (in this case) ("tos", "-1").
+sub lvaluize
+  {
+    my @retval;
+#    print "//DEBUG: $expression [";
+    my @tokens = split (/([+-\/\*])/, $expression);
+#    print join(",", @tokens);
+    $retval[0] = $tokens[0];
+
+    my $rightvalue = "";
+    for ($i = 1; $tokens[$i]; $i++) {
+
+      if ($tokens[$i] eq "+") {
+	$rightvalue .= "-";
+      } elsif ($tokens[$i] eq "-") {
+	$rightvalue .= "+";
+      } elsif ($tokens[$i] eq "/") {
+	$rightvalue .= "*";
+      } elsif ($tokens[$i] eq "*") {
+	$rightvalue .= "/";
+      } else {
+	$rightvalue .= $tokens[$i];
+      }
+    }
+
+    $retval[1] = $rightvalue;
+
+#   print "] => ($retval[0];$retval[1])\n";
+
+    return @retval;
+  }
+
+
+
+sub create_declaration
+  {
+    $typename = $type;
+    $ctype = $types{$type}->{'ctype'};
+
+    if (not $types{$type}->{'external'}) {
+      $types{$type}{'writer'} = "_cfsml_write_" . $typename;
+      $types{$type}{'reader'} = "_cfsml_read_" . $typename;
+      write_line_pp(__LINE__, 0);
+      print "static void\n$types{$type}{'writer'}(FILE *fh, $ctype* save_struc);\n";
+      print "static int\n$types{$type}{'reader'}(FILE *fh, $ctype* save_struc, const char *lastval,".
+	" int *line, int *hiteof);\n\n";
+    };
+
+  }
+
+sub create_writer
+  {
+    $typename = $type;
+    $ctype = $types{$type}{'ctype'};
+
+    write_line_pp(__LINE__, 0);
+    print "static void\n_cfsml_write_$typename(FILE *fh, $ctype* save_struc)\n{\n";
+    if ($types{$type}{'type'} eq $type_record) {
+        print "  int min, max, i;\n\n";
+    }
+
+    if ($types{$type}{'type'} eq $type_integer) {
+      print "  fprintf(fh, \"%li\", (long) *save_struc);\n";
+    }
+    elsif ($types{$type}{'type'} eq $type_string) {
+	write_line_pp(__LINE__, 0);
+	print "  if (!(*save_struc))\n";
+	print "    fprintf(fh, \"\\\\null\\\\\");\n";
+	print "  else {\n";
+	print "    char *token = _cfsml_mangle_string((const char *) *save_struc);\n";
+	print "    fprintf(fh, \"\\\"%s\\\"\", token);\n";
+	print "    free(token);\n";
+	print "  }\n";
+    }
+    elsif ($types{$type}{'type'} eq $type_record) {
+	write_line_pp(__LINE__, 0);
+	print "  fprintf(fh, \"{\\n\");\n";
+
+	for $n (@{$records{$type}}) {
+
+	    print "  fprintf(fh, \"$n->{'name'} = \");\n";
+
+	    if ($n->{'array'}) { # Check for arrays
+
+		if ($n->{'array'} eq 'static' or $n->{'size'} * 2) { # fixed integer value?
+		    print "    min = max = $n->{'size'};\n";
+		}
+		else { # No, a variable
+		    print "    min = max = save_struc->$n->{'size'};\n";
+		}
+
+		if ($n->{'maxwrite'}) { # A write limit?
+		    print "    if (save_struc->$n->{'maxwrite'} < min)\n";
+		    print "       min = save_struc->$n->{'maxwrite'};\n";
+		}
+
+		if ($n->{'array'} eq 'dynamic') {
+		    print "    if (!save_struc->$n->{'name'})\n";
+		    print "       min = max = 0; /* Don't write if it points to NULL */\n";
+		}
+
+		write_line_pp(__LINE__, 0);
+		print "    fprintf(fh, \"[%d][\\n\", max);\n";
+		print "    for (i = 0; i < min; i++) {\n";
+		print "      $types{$n->{'type'}}{'writer'}";
+		my $subscribstr = "[i]"; # To avoid perl interpolation problems
+		print "(fh, &(save_struc->$n->{'name'}$subscribstr));\n";
+		print "      fprintf(fh, \"\\n\");\n";
+		print "    }\n";
+		print "    fprintf(fh, \"]\");\n";
+
+	} elsif ($n->{'type'} eq $type_pointer) { # Relative pointer
+
+	  print "    fprintf(fh, \"%d\", save_struc->$n->{'name'} - save_struc->$n->{'anchor'});" .
+	    " /* Relative pointer */\n";
+
+      } elsif ($n->{'type'} eq $type_abspointer) { # Absolute pointer
+
+	  print "    if (!save_struc->$n->{'name'})\n";
+	  print "      fprintf(fh, \"\\\\null\\\\\");\n";
+	  print "    else \n";
+	  print "      $types{$n->{'reftype'}}{'writer'}";
+	  print "(fh, save_struc->$n->{'name'});\n";
+
+	} else { # Normal record entry
+
+	  print "    $types{$n->{'type'}}{'writer'}";
+	  print "(fh, ($types{$n->{'type'}}{'ctype'}*) &(save_struc->$n->{'name'}));\n";
+
+	}
+
+	print "    fprintf(fh, \"\\n\");\n";
+      }
+
+      print "  fprintf(fh, \"}\");\n";
+    }
+    else {
+      print STDERR "Warning: Attempt to create_writer for invalid type '$types{$type}{'type'}'\n";
+    }
+    print "}\n\n";
+
+  }
+
+
+sub create_reader
+  {
+    $typename = $type;
+    $ctype = $types{$type}{'ctype'};
+
+    write_line_pp(__LINE__, 0);
+    print "static int\n_cfsml_read_$typename";
+    print "(FILE *fh, $ctype* save_struc, const char *lastval, int *line, int *hiteof)\n{\n";
+
+    print "  char *token;\n";
+    if ($types{$type}{'type'} eq $type_record) {
+      print "int min, max, i;\n";
+    }
+    my $reladdress_nr = 0; # Number of relative addresses needed
+    my $reladdress = 0; # Current relative address number
+    my $reladdress_resolver = ""; # Relative addresses are resolved after the main while block
+
+    if ($types{$type}{'type'} eq $type_record) {
+
+      foreach $n (@{$records{$type}}) { # Count relative addresses we need
+	if ($n->{'type'} eq $type_pointer) {
+	  ++$reladdress_nr;
+	}
+      }
+
+      if ($reladdress_nr) { # Allocate stack space for all relative addresses needed
+	print "  int reladdresses[$reladdress_nr] = {0};\n";
+      }
+    }
+
+    if ($types{$type}{'type'} eq $type_integer) {
+	write_line_pp(__LINE__, 0);
+	print "\n  *save_struc = strtol(lastval, &token, 0);\n";
+	print "  if ( (*save_struc == 0) && (token == lastval) ) {\n";
+	print "     _cfsml_error(\"strtol failed at line %d\\n\", *line);\n";
+	print "     return CFSML_FAILURE;\n";
+	print "  }\n";
+	print "  if (*token != 0) {\n";
+	print "     _cfsml_error(\"Non-integer encountered while parsing int value at line %d\\n\",";
+	print " *line);\n";
+	print "     return CFSML_FAILURE;\n";
+	print "  }\n";
+	print "  return CFSML_SUCCESS;\n";
+    } elsif ($types{$type}{'type'} eq $type_string) {
+	write_line_pp(__LINE__, 0);
+	print "\n";
+	print "  if (strcmp(lastval, \"\\\\null\\\\\")) { /* null pointer? */\n";
+    print "    unsigned int length = strlen(lastval);\n";
+	print "    if (*lastval == '\"') { /* Quoted string? */\n";
+	print "      while (lastval[length] != '\"')\n";
+	print "        --length;\n\n";
+	print "      if (!length) { /* No matching double-quotes? */\n";
+	print "        _cfsml_error(\"Unbalanced quotes at line %d\\n\", *line);\n";
+	print "        return CFSML_FAILURE;\n";
+	print "      }\n\n";
+	print "      lastval++; /* ...and skip the opening quotes locally */\n";
+	print "      length--;\n";
+	print "    }\n";
+	print "    *save_struc = _cfsml_unmangle_string(lastval, length);\n";
+	print "    _cfsml_register_pointer(*save_struc);\n";
+	print "    return CFSML_SUCCESS;\n";
+	print "  } else {\n";
+	print "    *save_struc = NULL;\n";
+	print "    return CFSML_SUCCESS;\n";
+	print "  }\n";
+    } elsif ($types{$type}{'type'} eq $type_record) {
+	write_line_pp(__LINE__, 0);
+	print "  int assignment, closed, done;\n\n";
+	print "  if (strcmp(lastval, \"{\")) {\n";
+	print "     _cfsml_error(\"Reading record $type; expected opening braces in line %d, got \\\"%s\\\"\\n\",";
+	print "*line, lastval);\n";
+	print "     return CFSML_FAILURE;\n";
+	print "  };\n";
+	print "  closed = 0;\n";
+	print "  do {\n";
+	print "    const char *value;\n";
+	print "    token = _cfsml_get_identifier(fh, line, hiteof, &assignment);\n\n";
+	print "    if (!token) {\n";
+	print "       _cfsml_error(\"Expected token at line %d\\n\", *line);\n";
+	print "       return CFSML_FAILURE;\n";
+	print "    }\n";
+	print "    if (!assignment) {\n";
+	print "      if (!strcmp(token, \"}\")) \n";
+	print "         closed = 1;\n";
+	print "      else {\n";
+	print "        _cfsml_error(\"Expected assignment or closing braces in line %d\\n\", *line);\n";
+	print "        return CFSML_FAILURE;\n";
+	print "      }\n";
+	print "    } else {\n";
+	print "      value = \"\";\n";
+	print "      while (!value || !strcmp(value, \"\"))\n";
+	print "        value = _cfsml_get_value(fh, line, hiteof);\n";
+	print "      if (!value) {\n";
+	print "        _cfsml_error(\"Expected token at line %d\\n\", *line);\n";
+	print "        return CFSML_FAILURE;\n";
+	print "      }\n";
+#	print "    }\n";
+
+
+      foreach $n (@{$records{$type}}) { # Now take care of all record elements
+
+	my $type = $n->{'type'};
+	my $reference = undef;
+	if ($type eq $type_abspointer) {
+	    $reference = 1;
+	    $type = $n->{'reftype'};
+	}
+	my $name = $n->{'name'};
+	my $reader = $types{$type}{'reader'};
+	my $size = $n->{'size'};
+
+	print "      if (!strcmp(token, \"$name\")) {\n";
+
+	if ($type eq $type_pointer) { # A relative pointer
+
+	  $reader = $types{'int'}{'reader'}; # Read relpointer as int
+
+	  write_line_pp(__LINE__, 0);
+	  print "         if ($reader(fh, &(reladdresses[$reladdress]), value, line, hiteof)) {\n";
+	  print "            _cfsml_error(\"Expected token at line %d\\n\", *line);\n";
+	  print "            return CFSML_FAILURE;\n";
+	  print "         }\n";
+
+	  # Make sure that the resulting variable is interpreted correctly
+	  $reladdress_resolver .= "  save_struc->$n->{'name'} =".
+	    " save_struc->$n->{'anchor'} + reladdresses[$reladdress];\n";
+
+	  ++$reladdress; # Prepare reladdress for next element
+
+	} elsif ($n->{'array'}) { # Is it an array?
+	    write_line_pp(__LINE__, 0);
+	    print "         if ((value[0] != '[') || (value[strlen(value) - 1] != '[')) {\n";
+	    # The value must end with [, since we're starting array data, and it must also
+	    # begin with [, since this is either the only character in the line, or it starts
+	    # the "amount of memory to allocate" block
+	    print "            _cfsml_error(\"Opening brackets expected at line %d\\n\", *line);\n";
+	    print "            return CFSML_FAILURE;\n";
+	    print "         }\n";
+
+	  if ($n->{'array'} eq 'dynamic') {
+	      write_line_pp(__LINE__, 0);
+	    # We need to allocate the array first
+	    print "         /* Prepare to restore dynamic array */\n";
+	    # Read amount of memory to allocate
+	    print "         max = strtol(value + 1, NULL, 0);\n";
+	    print "         if (max < 0) {\n";
+	    print "            _cfsml_error(\"Invalid number of elements to allocate for dynamic ";
+	    print "array '%s' at line %d\\n\", token, *line);\n";
+	    print "            return CFSML_FAILURE;\n";
+	    print "         }\n\n";
+
+	    print "         if (max) {\n";
+	    print "           save_struc->$name = ($n->{'type'} *) sci_malloc(max * sizeof($type));\n";
+	    print "#ifdef SATISFY_PURIFY\n";
+            print "           memset(save_struc->$name, 0, max * sizeof($type));\n";
+	    print "#endif\n";
+	    print "           _cfsml_register_pointer(save_struc->$name);\n";
+	    print "         }\n";
+	    print "         else\n";
+	    print "           save_struc->$name = NULL;\n"
+
+	    } else { # static array
+		print "         /* Prepare to restore static array */\n";
+		print "         max = $size;\n";
+	    }
+
+	    write_line_pp(__LINE__, 0);
+	    print "         done = i = 0;\n";
+	    print "         do {\n";
+	    if ($type eq $type_record) {
+		print "           if (!(value = _cfsml_get_value(fh, line, hiteof))) {\n";
+	    } else {
+		print "           if (!(value = _cfsml_get_identifier(fh, line, hiteof, NULL))) {\n";
+	    }
+	    write_line_pp(__LINE__, 0);
+	    
+	    print "              _cfsml_error(\"Token expected at line %d\\n\", *line);\n";
+	    print "              return 1;\n";
+	    print "           }\n";
+	    print "           if (strcmp(value, \"]\")) {\n";
+	    print "             if (i == max) {\n";
+	    print "               _cfsml_error(\"More elements than space available (%d) in '%s' at ";
+	    print "line %d\\n\", max, token, *line);\n";
+	    print "               return CFSML_FAILURE;\n";
+	    print "             }\n";
+	    my $helper = "[i++]";
+	    print "             if ($reader(fh, &(save_struc->$name$helper), value, line, hiteof)) {\n";
+	    print "                _cfsml_error(\"Token expected by $reader() for $name$helper at line %d\\n\", *line);\n";
+	    print "                return CFSML_FAILURE;\n";
+	    print "             }\n";
+	    print "           } else done = 1;\n";
+	    print "         } while (!done);\n";
+
+	    if ($n->{'array'} eq "dynamic") {
+		my @xpr = lvaluize($expression = $n->{'size'});
+		print "         save_struc->$xpr[0] = max $xpr[1]; /* Set array size accordingly */\n";
+	    }
+
+	    if ($n->{'maxwrite'}) {
+		my @xpr = lvaluize($expression = $n->{'maxwrite'});
+		print "         save_struc->$xpr[0] = i $xpr[1]; /* Set number of elements */\n";
+	    }
+
+	}
+	elsif ($reference) {
+	    write_line_pp(__LINE__, 0);
+	    print "        if (strcmp(value, \"\\\\null\\\\\")) { /* null pointer? */\n";
+	    print "           save_struc->$name = sci_malloc(sizeof ($type));\n";
+	    print "           _cfsml_register_pointer(save_struc->$name);\n";
+	    print "           if ($reader(fh, save_struc->$name, value, line, hiteof)) {\n";
+	    print "              _cfsml_error(\"Token expected by $reader() for $name at line %d\\n\", *line);\n";
+	    print "              return CFSML_FAILURE;\n";
+	    print "           }\n";
+	    print "        } else save_struc->$name = NULL;\n";
+	}
+	else { # It's a simple variable or a struct
+	    write_line_pp(__LINE__, 0);
+	    print "         if ($reader(fh, ($types{$type}{'ctype'}*) &(save_struc->$name), value, line, hiteof)) {\n";
+	    print "            _cfsml_error(\"Token expected by $reader() for $name at line %d\\n\", *line);\n";
+	    print "            return CFSML_FAILURE;\n";
+	    print "         }\n";
+	}
+	print "      } else\n";
+
+      }
+	write_line_pp(__LINE__, 0);
+	print "       {\n";
+	print "          _cfsml_error(\"$type: Assignment to invalid identifier '%s' in line %d\\n\",";
+	print " token, *line);\n";
+	print "          return CFSML_FAILURE;\n";
+	print "       }\n";
+	print "     }\n";
+
+      print "  } while (!closed); /* Until closing braces are hit */\n";
+
+      print $reladdress_resolver; # Resolves any relative addresses
+
+      print "  return CFSML_SUCCESS;\n";
+    } else {
+      print STDERR "Warning: Attempt to create_reader for invalid type '$types{$type}{'type'}'\n";
+    }
+
+    print "}\n\n";
+  }
+
+# Built-in types
+
+%types = (
+	  'int' => {
+		    'type' => $type_integer,
+		    'ctype' => "int",
+		   },
+
+	  'string' => {
+		       'type' => $type_string,
+		       'ctype' => "char *",
+		      },
+	 );
+
+
+
+sub create_function_block {
+  print "\n/* Auto-generated CFSML declaration and function block */\n\n";
+  write_line_pp(__LINE__, 0);
+  print "#define CFSML_SUCCESS 0\n";
+  print "#define CFSML_FAILURE 1\n\n";
+  create_string_functions;
+
+  foreach $n ( keys %types ) {
+    create_declaration($type = $n);
+  }
+
+  foreach $n ( keys %types ) {
+    if (not $types{$n}->{'external'}) {
+      create_writer($type = $n);
+      create_reader($type = $n);
+    }
+  }
+  print "\n/* Auto-generated CFSML declaration and function block ends here */\n";
+  print "/* Auto-generation performed by cfsml.pl $version */\n";
+}
+
+
+# Gnerates code to read a data type
+# Parameters: $type: Type to read
+#             $datap: Pointer to the write destination
+#             $fh: Existing filehandle of an open file to use
+#             $eofvar: Variable to store _cfsml_eof into
+sub insert_reader_code {
+  print "/* Auto-generated CFSML data reader code */\n";
+  write_line_pp(__LINE__, 0);
+  print "  {\n";
+  if (!$linecounter) {
+      write_line_pp(__LINE__, 0);
+      print "    int _cfsml_line_ctr = 0;\n";
+      $linecounter = '_cfsml_line_ctr';
+  }
+  if ($atomic) {
+      write_line_pp(__LINE__, 0);
+      print "    struct _cfsml_pointer_refstruct **_cfsml_myptrrefptr = _cfsml_get_current_refpointer();\n";
+  }
+  write_line_pp(__LINE__, 0);
+  print "    int _cfsml_eof = 0, _cfsml_error;\n";
+  print "    int dummy;\n";
+
+  if ($firsttoken) {
+      write_line_pp(__LINE__, 0);
+      print "    const char *_cfsml_inp = $firsttoken;\n";
+  } else {
+      write_line_pp(__LINE__, 0);
+      print "    const char *_cfsml_inp =".
+	  " _cfsml_get_identifier($fh, &($linecounter), &_cfsml_eof, &dummy);\n\n";
+  }
+
+  write_line_pp(__LINE__, 0);
+  print "    _cfsml_error =".
+      " $types{$type}{'reader'}($fh, $datap, _cfsml_inp, &($linecounter), &_cfsml_eof);\n";
+
+  if ($eofvar) {
+      write_line_pp(__LINE__, 0);
+      print "    $eofvar = _cfsml_error;\n";
+  }
+  if ($atomic) {
+      write_line_pp(__LINE__, 0);
+      print "     _cfsml_free_pointer_references(_cfsml_myptrrefptr, _cfsml_error);\n";
+  }
+  write_line_pp(__LINE__, 0);
+  print "     if (_cfsml_last_value_retrieved) {\n";
+  print "       free(_cfsml_last_value_retrieved);\n";
+  print "       _cfsml_last_value_retrieved = NULL;\n";
+  print "     }\n";
+  print "     if (_cfsml_last_identifier_retrieved) {\n";
+  print "       free(_cfsml_last_identifier_retrieved);\n";
+  print "       _cfsml_last_identifier_retrieved = NULL;\n";
+  print "     }\n";
+  print "  }\n";
+  print "/* End of auto-generated CFSML data reader code */\n";
+}
+
+# Generates code to write a data type
+# Parameters: $type: Type to write
+#             $datap: Pointer to the write destination
+#             $fh: Existing filehandle of an open file to use
+sub insert_writer_code {
+    write_line_pp(__LINE__, 0);
+    print "/* Auto-generated CFSML data writer code */\n";
+    print "  $types{$type}{'writer'}($fh, $datap);\n";
+    print "  fprintf($fh, \"\\n\");\n";
+    print "/* End of auto-generated CFSML data writer code */\n";
+}
+
+
+################
+# Main program #
+################
+
+$parsing = 0;
+$struct = undef; # Not working on a struct
+$commentmode = undef;
+$line = 0;
+
+while (<STDIN>) {
+
+  $line++;
+
+  if ($parsing) {
+    ($data) = split "#"; # Remove shell-style comments
+    @_ = ($data);
+
+    s/\/\*.*\*\///g; # Remove C-style one-line comments
+
+    ($data) = split "\/\/"; # Remove C++-style comments
+    @_ = ($data);
+
+    if ($commentmode) {
+
+      if (grep /\*\//, $_) {
+	($empty, $_) = split /\*\//;
+      } else {
+	@_ = (); # Empty line
+      }
+
+    } else {
+      if (grep /\/\*/, $_) {
+	$commentmode = 1;
+	($_) = split /\/\*/;
+      }
+    }
+
+
+    # Now tokenize:
+    s/;//;
+    split /(\".*\"|[,\[\]\(\)\{\}])|\s+/;
+
+    @items = @_;
+
+    @tokens = ();
+
+    $tokens_nr = 0;
+    for ($n = 0; $n < scalar @items; $n++) { # Get rid of all undefs
+      if ($_[$n]) {
+	$_ = $items[$n];
+	s/\"//g;
+	$tokens[$tokens_nr++] = $_;
+      }
+    }
+
+    # Now all tokens are in @tokens, and we have $tokens_nr of them.
+
+#    print "//DEBUG: " . join ("|", @tokens) . "\n";
+
+    if ($tokens_nr) {
+      if ($tokens_nr == 2 && $tokens[0] eq "%END" && $tokens[1] eq "CFSML") {
+
+	$struct && die "Record $struct needs closing braces in intput file (line $line).";
+
+	$parsing = 0;
+	create_function_block;
+	my $linep = $line + 1;
+	write_line_pp($linep, 1);
+      } elsif ($struct) { # Parsing struct
+	if ($tokens_nr == 1) {
+	  if ($tokens[0] eq "}") {
+	    $struct = undef;
+	  } else { die "Invalid declaration of $token[0] in input file (line $line)\n";};
+	} else { # Must be a member declaration
+
+	  my @structrecs = (@{$records{$struct}});
+	  my $newidx = (scalar @structrecs) or "0";
+	  my %member = ();
+	  $member{'name'} = $tokens[1];
+	  $member{'type'} = $tokens[0];
+
+	  if ($tokens_nr == 3 && $tokens[1] == "*") {
+	      $tokens_nr = 2;
+	      $member{'name'} = $tokens[2];
+	      $member{'reftype'} = $tokens[0];
+	      $member{'type'} = $type_abspointer;
+	  }
+
+	  if ($tokens_nr == 4 and $tokens[0] eq $type_pointer) { # Relative pointer
+
+	    if (not $tokens[2] eq "RELATIVETO") {
+	      die "Invalid relative pointer declaration in input file (line $line)\n";
+	    }
+
+	    $member{'anchor'} = $tokens[3]; # RelPointer anchor
+
+	  } else { # Non-pointer
+
+	    if (not $types{$tokens[0]}) {
+	      die "Unknown type $tokens[0] used in input file (line $line)\n";
+	    }
+
+	    if ($tokens_nr > 2) { # Array
+
+	      if ($tokens[2] ne "[") {
+		die "Invalid token '$tokens[2]' in input file (line $line)\n";
+	      }
+
+	      $member{'array'} = "static";
+
+	      if ($tokens[$tokens_nr - 1] ne "]") {
+		die "Array declaration incorrectly terminated in input file (line $line)\n";
+	      }
+
+	      $parsepos = 3;
+
+	      while ($parsepos < $tokens_nr) {
+
+		if ($tokens[$parsepos] eq ",") {
+
+		  $parsepos++;
+
+		} elsif ($tokens[$parsepos] eq "STATIC") {
+
+		  $member{'array'} = "static";
+		  $parsepos++;
+
+		} elsif ($tokens[$parsepos] eq "DYNAMIC") {
+
+		  $member{'array'} = "dynamic";
+		  $parsepos++;
+
+		} elsif ($tokens[$parsepos] eq "MAXWRITE") {
+
+		  $member{'maxwrite'} = $tokens[$parsepos + 1];
+		  $parsepos += 2;
+
+		} elsif ($tokens[$parsepos] eq "]") {
+
+		  $parsepos++;
+		  if ($parsepos != $tokens_nr) {
+		    die "Error: Invalid tokens after array declaration in input file (line $line)\n";
+
+		  }
+		} else {
+
+		  if ($member{'size'}) {
+		    die "Attempt to use more than one array size in input file (line $line)\n" .
+		      "(Original size was \"$member->{'size'}\", new size is \"$tokens[$parsepos]\"\n";
+		  }
+
+		  $member{'size'} = $tokens[$parsepos];
+		  $parsepos++;
+		}
+	      }
+
+
+	      unless ($member{'size'}) {
+		die "Array declaration without size in input file (line $line)\n";
+	      }
+	    }
+	  }
+
+	  @{$records{$struct}}->[$newidx] = \%member;
+	}
+      } else { # not parsing struct; normal operation.
+
+	if ($tokens[0] eq "TYPE") { # Simple type declaration
+
+	  my $newtype = $tokens[1];
+
+	  $types{$newtype}->{'ctype'} = $tokens[2];
+
+	  if ($tokens_nr == 5) { # must be ...LIKE...
+
+	    unless ($tokens[3] eq "LIKE") {
+	      die "Invalid TYPE declaration in input file (line $line)\n";
+	    }
+
+	    $types{$newtype}->{'type'} = $types{$tokens[4]}->{'type'};
+	    $types{$newtype}->{'reader'} = $types{$tokens[4]}->{'reader'};
+	    $types{$newtype}->{'writer'} = $types{$tokens[4]}->{'writer'};
+
+	  } elsif ($tokens_nr == 6) { # must be ...USING...
+
+	    unless ($tokens[3] eq "USING") {
+	      die "Invalid TYPE declaration in input file (line $line)\n";
+	    }
+
+	    $types{$newtype}->{'writer'} = $tokens[4];
+	    $types{$newtype}->{'reader'} = $tokens[5];
+	    $types{$newtype}->{'external'} = 'T';
+
+	  } else {
+	    die "Invalid TYPE declaration in input file (line $line)\n";
+	  }
+
+	} elsif ($tokens[0] eq "RECORD") {
+
+	  $struct = $tokens[1];
+	  if ($types{$struct}) {
+	    die "Attempt to re-define existing type $struct as a struct in input file (line $line)";
+	  }
+	  $types{$struct}{'type'} = $type_record;
+	  if ($tokens_nr < 3 or $tokens_nr > 6 or $tokens[$tokens_nr - 1] ne "{") {
+	    die "Invalid record declaration in input file (line $line)";
+	  }
+
+	  my $extoffset = 2;
+
+	  if ($tokens_nr > 3) {
+	      if ($tokens[2] ne "EXTENDS") { # Record declaration with explicit c type
+		  $types{$struct}{'ctype'} = $tokens[2];
+		  $extoffset = 3;
+	      } else { # Record name is the same as the c type name
+		  $types{$struct}{'ctype'} = $struct;
+	      }
+	  } elsif ($tokens_nr == 3) {
+		  $types{$struct}{'ctype'} = $struct;
+	  }
+
+	  if (($tokens_nr > $extoffset + 1) && ($extoffset + 1 <= $tokens_nr)) {
+	      if ($tokens[$extoffset] ne "EXTENDS") {
+		  die "Invalid or improper keyword \"$tokens[$extoffset]\" in input file (line $line)";
+	      }
+	      if ($extoffset + 2 >= $tokens_nr) {
+		  die "RECORD \"$struct\" extends on unspecified type in input file (line $line)";
+	      }
+	      my $ext_type = $tokens[$extoffset + 1];
+
+	      if (!($types{$ext_type}{type} eq $type_record)) {
+		  print "$types{$ext_type}{type}";
+		  die "RECORD \"$struct\" attempts to extend non-existing or non-record type \"$ext_type\" in input file (line $line)";
+	      }
+
+	      (@{$records{$struct}}) = (@{$records{$ext_type}}); # Copy type information from super type
+	  }
+
+	} else {
+	  die "Invalid declaration \"$tokens[0]\" in line $line";
+	}
+      }
+    }
+
+
+  } else {
+
+    ($subtoken) = split ";"; # Get rid of trailing ;s
+    $tokens_nr = @tokens = split " ", $subtoken;
+
+    if ($tokens_nr == 1 && $tokens[0] eq "%CFSML") {
+
+      $parsing = 1;
+
+    } elsif ($tokens[0] eq "%CFSMLWRITE" and $tokens[3] eq "INTO" and $tokens_nr >= 5) {
+
+      insert_writer_code($type = $tokens[1], $datap = $tokens[2], $fh = $tokens[4]);
+      my $templine = $line + 1;
+      write_line_pp($templine, 1); # Yes, this sucks.
+
+    } elsif (($tokens[0] eq "%CFSMLREAD") or ($tokens[0] eq "%CFSMLREAD-ATOMIC") and $tokens[3] eq "FROM" and $tokens_nr >= 5) {
+
+      my $myeofvar = 0;
+      my $myfirsttoken = 0;
+      my $mylinecounter = 0;
+
+      my $idcounter = 5;
+
+      while ($idcounter < $tokens_nr) {
+	if ($tokens[$idcounter] eq "ERRVAR" and $tokens_nr >= $idcounter + 2) {
+	  $myeofvar = $tokens[$idcounter + 1];
+	  $idcounter += 2;
+	} elsif ($tokens[$idcounter] eq "FIRSTTOKEN" and $tokens_nr >= $idcounter + 2) {
+	  $myfirsttoken = $tokens[$idcounter + 1];
+	  $idcounter += 2;
+	} elsif ($tokens[$idcounter] eq "LINECOUNTER" and $tokens_nr >= $idcounter + 2) {
+	  $mylinecounter = $tokens[$idcounter + 1];
+	  $idcounter += 2;
+	} else {
+	  die "Unknown %CFSMLREAD operational token: $tokens[$idcounter]\n";
+	}
+      }
+      insert_reader_code($type = $tokens[1], $datap = $tokens[2],
+			 $fh = $tokens[4], $eofvar = $myeofvar, $firsttoken = $myfirsttoken,
+			$linecounter = $mylinecounter, $atomic = ($tokens[0] eq "%CFSMLREAD-ATOMIC"));
+      my $templine = $line + 1;
+      write_line_pp($templine, 1); # Yes, this sucks, too.
+
+    } else {
+      print;
+    }
+  }
+
+}
+
+if ($parsing) {
+  print <STDERR>, "Warning: Missing %END CFSML\n";
+}

Copied: scummvm/trunk/engines/sci/engine/said.y (from rev 38467, scummvm/trunk/engines/sci/engine/said.y)
===================================================================
--- scummvm/trunk/engines/sci/engine/said.y	                        (rev 0)
+++ scummvm/trunk/engines/sci/engine/said.y	2009-02-18 15:05:00 UTC (rev 38485)
@@ -0,0 +1,948 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $URL$
+ * $Id$
+ *
+ */
+
+%{
+
+#include "sci/include/engine.h"
+
+#define SAID_BRANCH_NULL 0
+
+#define MAX_SAID_TOKENS 128
+
+/* Maximum number of words to be expected in a parsed sentence */
+#define AUGMENT_MAX_WORDS 64
+
+
+#define ANYWORD 0xfff
+
+#define WORD_TYPE_BASE 0x141
+#define WORD_TYPE_REF 0x144
+#define WORD_TYPE_SYNTACTIC_SUGAR 0x145
+
+#define AUGMENT_SENTENCE_PART_BRACKETS 0x152
+
+/* Minor numbers */
+#define AUGMENT_SENTENCE_MINOR_MATCH_PHRASE 0x14c
+#define AUGMENT_SENTENCE_MINOR_MATCH_WORD 0x153
+#define AUGMENT_SENTENCE_MINOR_RECURSE 0x144
+#define AUGMENT_SENTENCE_MINOR_PARENTHESES 0x14f
+
+
+#undef YYDEBUG /*1*/
+/*#define SAID_DEBUG*/
+/*#define SCI_DEBUG_PARSE_TREE_AUGMENTATION*/ /* uncomment to debug parse tree augmentation*/
+
+
+#ifdef SCI_DEBUG_PARSE_TREE_AUGMENTATION
+#define scidprintf sciprintf
+#else
+#define scidprintf if (0) sciprintf
+#endif
+
+
+static char *said_parse_error;
+
+static int said_token;
+static int said_tokens_nr;
+static int said_tokens[MAX_SAID_TOKENS];
+
+static int said_blessed;  /* increminated by said_top_branch */
+
+static int said_tree_pos; /* Set to 0 if we're out of space */
+#define SAID_TREE_START 4; /* Reserve space for the 4 top nodes */
+
+#define VALUE_IGNORE -424242
+
+static parse_tree_node_t said_tree[VOCAB_TREE_NODES];
+
+typedef int wgroup_t;
+typedef int tree_t;
+typedef int said_spec_t;
+
+static tree_t
+said_aug_branch(int, int, tree_t, tree_t);
+
+static tree_t
+said_attach_branch(tree_t, tree_t);
+/*
+static tree_t
+said_wgroup_branch(wgroup_t);
+*/
+static said_spec_t
+said_top_branch(tree_t);
+
+static tree_t
+said_paren(tree_t, tree_t);
+
+static tree_t
+said_value(int, tree_t);
+
+static tree_t
+said_terminal(int);
+
+
+static int
+yylex(void);
+
+static int
+yyerror(char *s)
+{
+	said_parse_error = sci_strdup(s);
+	return 1; /* Abort */
+}
+
+%}
+
+%token WGROUP /* Word group */
+%token YY_COMMA     /* 0xf0 */
+%token YY_AMP       /* 0xf1 */
+%token YY_SLASH     /* 0xf2 */
+%token YY_PARENO    /* 0xf3 */
+%token YY_PARENC    /* 0xf4 */
+%token YY_BRACKETSO /* 0xf5 */
+%token YY_BRACKETSC /* 0xf6 */
+%token YY_HASH      /* 0xf7 */
+%token YY_LT        /* 0xf8 */
+%token YY_GT        /* 0xf9 */
+%token YY_BRACKETSO_LT /* special token used to imitate LR(2) behaviour */
+%token YY_BRACKETSO_SLASH /* special token used to imitate LR(2) behaviour */
+%token YY_LT_BRACKETSO /* special token used to imitate LR(2) behaviour */
+%token YY_LT_PARENO /* special token used to imitate LR(2) behaviour */
+
+%%
+
+saidspec :	  leftspec optcont 
+			{ $$ = said_top_branch(said_attach_branch($1, $2)); } 
+		| leftspec midspec optcont
+			{ $$ = said_top_branch(said_attach_branch($1, said_attach_branch($2, $3))); }
+		| leftspec midspec rightspec optcont
+			{ $$ = said_top_branch(said_attach_branch($1, said_attach_branch($2, said_attach_branch($3, $4)))); }
+		;
+
+
+optcont :	 /* empty */
+			{ $$ = SAID_BRANCH_NULL; }
+		| YY_GT
+			{ $$ = said_paren(said_value(0x14b, said_value(0xf900, said_terminal(0xf900))), SAID_BRANCH_NULL); }
+		;
+
+
+
+leftspec :	/* empty */
+			{ $$ = SAID_BRANCH_NULL; }
+		| expr
+			{ $$ = said_paren(said_value(0x141, said_value(0x149, $1)), SAID_BRANCH_NULL); }
+		;
+
+
+
+midspec :	 YY_SLASH expr
+			{ $$ = said_aug_branch(0x142, 0x14a, $2, SAID_BRANCH_NULL); }
+		| YY_BRACKETSO_SLASH YY_SLASH expr YY_BRACKETSC
+			{ $$ = said_aug_branch(0x152, 0x142, said_aug_branch(0x142, 0x14a, $3, SAID_BRANCH_NULL), SAID_BRANCH_NULL); }
+		| YY_SLASH
+			{ $$ = SAID_BRANCH_NULL; }
+		;
+
+
+
+rightspec :	 YY_SLASH expr
+			{ $$ = said_aug_branch(0x143, 0x14a, $2, SAID_BRANCH_NULL); }
+		| YY_BRACKETSO_SLASH YY_SLASH expr YY_BRACKETSC
+			{ $$ = said_aug_branch(0x152, 0x143, said_aug_branch(0x143, 0x14a, $3, SAID_BRANCH_NULL), SAID_BRANCH_NULL); }
+		| YY_SLASH
+			{ $$ = SAID_BRANCH_NULL; }
+		;
+
+
+
+word :		 WGROUP
+			{ $$ = said_paren(said_value(0x141, said_value(0x153, said_terminal($1))), SAID_BRANCH_NULL); }
+		;
+
+
+cwordset :	wordset
+			{ $$ = said_aug_branch(0x141, 0x14f, $1, SAID_BRANCH_NULL); }
+		| YY_BRACKETSO wordset YY_BRACKETSC
+			{ $$ = said_aug_branch(0x141, 0x14f, said_aug_branch(0x152, 0x14c, said_aug_branch(0x141, 0x14f, $2, SAID_BRANCH_NULL), SAID_BRANCH_NULL), SAID_BRANCH_NULL); }
+		;
+
+
+wordset :	 word
+			{ $$ = $1; }
+		| YY_PARENO expr YY_PARENC
+			{ $$ = $1; }
+		| wordset YY_COMMA wordset
+			{ $$ = said_attach_branch($1, $3); }
+                | wordset YY_BRACKETSO_LT wordrefset YY_BRACKETSC
+			{ $$ = said_attach_branch($1, $3); }
+		| wordset YY_COMMA YY_BRACKETSO wordset YY_BRACKETSC
+			{ $$ = said_attach_branch($1, $3); }
+		;
+
+
+
+expr :		 cwordset cwordrefset
+			{ $$ = said_attach_branch($1, $2); }
+		| cwordset
+			{ $$ = $1; }
+		| cwordrefset
+			{ $$ = $1; }
+		;
+
+
+
+cwordrefset :	 wordrefset
+			{ $$ = $1; }
+		| YY_BRACKETSO_LT wordrefset YY_BRACKETSC
+			{ $$ = said_aug_branch(0x152, 0x144, $2, SAID_BRANCH_NULL); }
+		| wordrefset YY_BRACKETSO_LT wordrefset YY_BRACKETSC
+			{ $$ = said_attach_branch($1, said_aug_branch(0x152, 0x144, $3, SAID_BRANCH_NULL)); }
+		;
+
+
+
+wordrefset :	YY_LT word recref
+			{ $$ = said_aug_branch(0x144, 0x14f, $2, $3); }
+		| YY_LT_PARENO YY_PARENO expr YY_PARENC
+		{ $$ = said_aug_branch(0x144, 0x14f, said_aug_branch(0x141, 0x144, $2, SAID_BRANCH_NULL), SAID_BRANCH_NULL); }
+		| YY_LT wordset
+			{ $$ = said_aug_branch(0x144, 0x14f, $2, SAID_BRANCH_NULL); }
+		| YY_LT_BRACKETSO YY_BRACKETSO wordset YY_BRACKETSC
+			{ $$ = said_aug_branch(0x152, 0x144, said_aug_branch(0x144, 0x14f, $3, SAID_BRANCH_NULL), SAID_BRANCH_NULL); }
+		;
+
+
+
+recref :	YY_LT wordset recref
+			{ $$ = said_aug_branch(0x141, 0x144, said_aug_branch(0x144, 0x14f, $2, SAID_BRANCH_NULL), $3); }
+		| YY_LT wordset
+			{ $$ = said_aug_branch(0x141, 0x144, said_aug_branch(0x144, 0x14f, $2, SAID_BRANCH_NULL), SAID_BRANCH_NULL); }
+		| YY_LT_PARENO YY_PARENO expr YY_PARENC
+		{ $$ = said_aug_branch(0x141, 0x14c, $2, SAID_BRANCH_NULL); }
+		;
+
+
+
+%%
+
+
+int
+parse_yy_token_lookup[] = {YY_COMMA, YY_AMP, YY_SLASH, YY_PARENO, YY_PARENC, YY_BRACKETSO, YY_BRACKETSC,
+			   YY_HASH, YY_LT, YY_GT};
+
+static int
+yylex(void)
+{
+	int retval = said_tokens[said_token++];
+
+	if (retval < SAID_LONG(SAID_FIRST)) {
+		yylval = retval;
+		retval = WGROUP;
+	} else {
+		retval >>= 8;
+
+		if (retval == SAID_TERM)
+			retval = 0;
+		else {
+			assert(retval >= SAID_FIRST);
+			retval = parse_yy_token_lookup[retval - SAID_FIRST];
+			if (retval == YY_BRACKETSO) {
+				if ((said_tokens[said_token] >> 8) == SAID_LT)
+					retval = YY_BRACKETSO_LT;
+				else
+					if ((said_tokens[said_token] >> 8) == SAID_SLASH)
+						retval = YY_BRACKETSO_SLASH;
+			} else if (retval == YY_LT && (said_tokens[said_token] >> 8) == SAID_BRACKO) {
+				retval = YY_LT_BRACKETSO;
+			} else if (retval == YY_LT && (said_tokens[said_token] >> 8) == SAID_PARENO) {
+				retval = YY_LT_PARENO;
+			}
+		}
+	}
+
+	return retval;
+}
+
+#define SAID_NEXT_NODE ((said_tree_pos == 0) || (said_tree_pos >= VOCAB_TREE_NODES))? said_tree_pos = 0 : said_tree_pos++
+
+static inline int
+said_leaf_node(tree_t pos, int value)
+{
+	said_tree[pos].type = PARSE_TREE_NODE_LEAF;
+
+	if (value != VALUE_IGNORE)
+		said_tree[pos].content.value = value;
+
+	return pos;
+}
+
+static inline int
+said_branch_node(tree_t pos, int left, int right)
+{
+	said_tree[pos].type = PARSE_TREE_NODE_BRANCH;
+
+	if (left != VALUE_IGNORE)
+		said_tree[pos].content.branches[0] = left;
+
+	if (right != VALUE_IGNORE)
+		said_tree[pos].content.branches[1] = right;
+
+	return pos;
+}
+
+
+static tree_t
+said_paren(tree_t t1, tree_t t2)
+{
+	if (t1)
+		return said_branch_node(SAID_NEXT_NODE,
+					t1,
+					t2
+					);
+	else
+		return t2;
+}
+
+static tree_t
+said_value(int val, tree_t t)
+{
+	return said_branch_node(SAID_NEXT_NODE,
+				said_leaf_node(SAID_NEXT_NODE, val),
+				t
+				);
+			  
+}
+
+static tree_t
+said_terminal(int val)
+{
+	return said_leaf_node(SAID_NEXT_NODE, val);
+}
+
+
+static tree_t
+said_aug_branch(int n1, int n2, tree_t t1, tree_t t2)
+{
+	int retval;
+
+	retval = said_branch_node(SAID_NEXT_NODE,
+				  said_branch_node(SAID_NEXT_NODE,
+						   said_leaf_node(SAID_NEXT_NODE, n1),
+						   said_branch_node(SAID_NEXT_NODE,
+								    said_leaf_node(SAID_NEXT_NODE, n2),
+								    t1
+								    )
+						   ),
+				  t2
+				  );
+
+#ifdef SAID_DEBUG
+	fprintf(stderr,"AUG(0x%x, 0x%x, [%04x], [%04x]) = [%04x]\n", n1, n2, t1, t2, retval);
+#endif
+
+	return retval;
+}
+
+static tree_t
+said_attach_branch(tree_t base, tree_t attacheant)
+{
+#ifdef SAID_DEBUG
+	fprintf(stderr,"ATT2([%04x], [%04x]) = [%04x]\n", base, attacheant, base);
+#endif
+
+	if (!attacheant)
+		return base;
+	if (!base)
+		return attacheant;
+
+	if (!base)
+		return 0; /* Happens if we're out of space */
+
+	said_branch_node(base, VALUE_IGNORE, attacheant);
+
+	return base;
+}
+
+static said_spec_t
+said_top_branch(tree_t first)
+{
+#ifdef SAID_DEBUG
+	fprintf(stderr, "TOP([%04x])\n", first);
+#endif
+	said_branch_node(0, 1, 2);
+	said_leaf_node(1, 0x141); /* Magic number #1 */
+	said_branch_node(2, 3, first);
+	said_leaf_node(3, 0x13f); /* Magic number #2 */
+
+	++said_blessed;
+
+	return 0;
+}
+
+
+int
+said_parse_spec(state_t *s, byte *spec)
+{
+	int nextitem;
+
+	said_parse_error = NULL;
+	said_token = 0;
+	said_tokens_nr = 0;
+	said_blessed = 0;
+
+	said_tree_pos = SAID_TREE_START;
+
+	do {
+		nextitem = *spec++;
+		if (nextitem < SAID_FIRST)
+			said_tokens[said_tokens_nr++] = nextitem << 8 | *spec++;
+		else
+			said_tokens[said_tokens_nr++] = SAID_LONG(nextitem);
+
+	} while ((nextitem != SAID_TERM) && (said_tokens_nr < MAX_SAID_TOKENS));
+
+	if (nextitem == SAID_TERM)
+		yyparse();
+	else {
+		sciprintf("Error: SAID spec is too long\n");
+		return 1;
+	}
+
+	if (said_parse_error) {
+		sciprintf("Error while parsing SAID spec: %s\n", said_parse_error);
+		free(said_parse_error);
+		return 1;
+	}
+
+	if (said_tree_pos == 0) {
+		sciprintf("Error: Out of tree space while parsing SAID spec\n");
+		return 1;
+	}
+
+	if (said_blessed != 1) {
+		sciprintf("Error: Found %d top branches\n");
+		return 1;
+	}
+
+	return 0;
+}
+
+/**********************/
+/**** Augmentation ****/
+/**********************/
+
+
+/** primitive functions **/
+
+#define AUG_READ_BRANCH(a, br, p) \
+  if (tree[p].type != PARSE_TREE_NODE_BRANCH) \
+    return 0; \
+  a = tree[p].content.branches[br];
+
+#define AUG_READ_VALUE(a, p) \
+  if (tree[p].type != PARSE_TREE_NODE_LEAF) \
+    return 0; \
+  a = tree[p].content.value;
+
+#define AUG_ASSERT(i) \
+  if (!i) return 0;
+
+static int
+aug_get_next_sibling(parse_tree_node_t *tree, int pos, int *first, int *second)
+     /* Returns the next sibling relative to the specified position in 'tree',
+     ** sets *first and *second to its augment node values, returns the new position
+     ** or 0 if there was no next sibling
+     */
+{
+	int seek, valpos;
+
+	AUG_READ_BRANCH(pos, 1, pos);
+	AUG_ASSERT(pos);
+	AUG_READ_BRANCH(seek, 0, pos);
+	AUG_ASSERT(seek);
+
+	/* Now retreive first value */
+	AUG_READ_BRANCH(valpos, 0, seek);
+	AUG_ASSERT(valpos);
+	AUG_READ_VALUE(*first, valpos);
+
+	/* Get second value */
+	AUG_READ_BRANCH(seek, 1, seek);
+	AUG_ASSERT(seek);
+	AUG_READ_BRANCH(valpos, 0, seek);
+	AUG_ASSERT(valpos);
+	AUG_READ_VALUE(*second, valpos);
+
+	return pos;
+}
+
+
+static int
+aug_get_wgroup(parse_tree_node_t *tree, int pos)
+     /* Returns 0 if pos in tree is not the root of a 3-element list, otherwise
+     ** it returns the last element (which, in practice, is the word group
+     */
+{
+	int val;
+
+	AUG_READ_BRANCH(pos, 0, pos);
+	AUG_ASSERT(pos);
+	AUG_READ_BRANCH(pos, 1, pos);
+	AUG_ASSERT(pos);
+	AUG_READ_BRANCH(pos, 1, pos);
+	AUG_ASSERT(pos);
+	AUG_READ_VALUE(val, pos);
+
+	return val;
+}
+
+
+static int
+aug_get_base_node(parse_tree_node_t *tree)
+{
+	int startpos = 0;
+	AUG_READ_BRANCH(startpos, 1, startpos);
+	return startpos;
+}
+
+
+/** semi-primitive functions **/
+
+
+static int
+aug_get_first_child(parse_tree_node_t *tree, int pos, int *first, int *second)
+     /* like aug_get_next_sibling, except that it recurses into the tree and
+     ** finds the first child (usually *not* Ayanami Rei) of the current branch
+     ** rather than its next sibling.
+     */
+{
+	AUG_READ_BRANCH(pos, 0, pos);
+	AUG_ASSERT(pos);
+	AUG_READ_BRANCH(pos, 1, pos);
+	AUG_ASSERT(pos);
+
+	return aug_get_next_sibling(tree, pos, first, second);
+}
+
+static void
+aug_find_words_recursively(parse_tree_node_t *tree, int startpos,
+			   int *base_words, int *base_words_nr,
+			   int *ref_words, int *ref_words_nr,
+			   int maxwords, int refbranch)
+     /* Finds and lists all base (141) and reference (144) words */
+{
+	int major, minor;
+	int word;
+	int pos = aug_get_first_child(tree, startpos, &major, &minor);
+
+	/*	if (major == WORD_TYPE_REF)
+		refbranch = 1;*/
+
+	while (pos) {
+		if ((word = aug_get_wgroup(tree, pos))) { /* found a word */
+
+			if (!refbranch && major == WORD_TYPE_BASE) {	
+				if ((*base_words_nr) == maxwords) {
+					sciprintf("Out of regular words\n");
+					return; /* return gracefully */
+				}
+
+				base_words[*base_words_nr] = word; /* register word */
+				++(*base_words_nr);
+
+			}
+			if (major == WORD_TYPE_REF || refbranch) {
+				if ((*ref_words_nr) == maxwords) {
+					sciprintf("Out of reference words\n");
+					return; /* return gracefully */
+				}
+
+				ref_words[*ref_words_nr] = word; /* register word */
+				++(*ref_words_nr);
+
+			}
+			if (major != WORD_TYPE_SYNTACTIC_SUGAR && major != WORD_TYPE_BASE && major != WORD_TYPE_REF)
+				sciprintf("aug_find_words_recursively(): Unknown word type %03x\n", major);
+    
+		} else /* Did NOT find a word group: Attempt to recurse */
+			aug_find_words_recursively(tree, pos, base_words, base_words_nr,
+						   ref_words, ref_words_nr, maxwords, refbranch || major == WORD_TYPE_REF);
+
+		pos = aug_get_next_sibling(tree, pos, &major, &minor);
+	}
+}
+
+
+static void
+aug_find_words(parse_tree_node_t *tree, int startpos,
+	       int *base_words, int *base_words_nr,
+	       int *ref_words, int *ref_words_nr,
+	       int maxwords)
+     /* initializing wrapper for aug_find_words_recursively() */
+{
+	*base_words_nr = 0;
+	*ref_words_nr = 0;
+
+	aug_find_words_recursively(tree, startpos, base_words, base_words_nr, ref_words, ref_words_nr, maxwords, 0);
+}
+
+
+static inline int
+aug_contains_word(int *list, int length, int word)
+{
+	int i;
+	if (word == ANYWORD)
+		return (length);
+
+	for (i = 0; i < length; i++)
+		if (list[i] == word)
+			return 1;
+
+	return 0;
+}
+
+
+static int
+augment_sentence_expression(parse_tree_node_t *saidt, int augment_pos,
+			    parse_tree_node_t *parset, int parse_branch,
+			    int major, int minor,
+			    int *base_words, int base_words_nr,
+			    int *ref_words, int ref_words_nr);
+
+static int
+augment_match_expression_p(parse_tree_node_t *saidt, int augment_pos,
+			   parse_tree_node_t *parset, int parse_basepos,
+			   int major, int minor,
+			   int *base_words, int base_words_nr,
+			   int *ref_words, int ref_words_nr)
+{
+	int cmajor, cminor, cpos;
+	cpos = aug_get_first_child(saidt, augment_pos, &cmajor, &cminor);
+	if (!cpos) {
+		sciprintf("augment_match_expression_p(): Empty condition\n");
+		return 1;
+	}
+
+	scidprintf("Attempting to match (%03x %03x (%03x %03x\n", major, minor, cmajor, cminor);
+
+	if ((major == WORD_TYPE_BASE) && (minor == AUGMENT_SENTENCE_MINOR_RECURSE))
+		return augment_match_expression_p(saidt, cpos,
+						  parset, parse_basepos,
+						  cmajor, cminor,
+						  base_words, base_words_nr,
+						  ref_words, ref_words_nr);
+
+
+	switch (major) {
+
+	case WORD_TYPE_BASE:
+		while (cpos) {
+			if (cminor == AUGMENT_SENTENCE_MINOR_MATCH_WORD) {
+				int word = aug_get_wgroup(saidt, cpos);
+				scidprintf("Looking for word %03x\n", word);
+
+				if (aug_contains_word(base_words, base_words_nr, word))
+					return 1;
+			} else if (cminor == AUGMENT_SENTENCE_MINOR_MATCH_PHRASE) {
+				if (augment_sentence_expression(saidt, cpos,
+								parset, parse_basepos,
+								cmajor, cminor,
+								base_words, base_words_nr,
+								ref_words, ref_words_nr))
+					return 1;
+			} else if (cminor == AUGMENT_SENTENCE_MINOR_PARENTHESES) {
+				int gc_major, gc_minor;
+				int gchild = aug_get_first_child(saidt, cpos, &gc_major, &gc_minor);
+
+				while (gchild) {
+					if (augment_match_expression_p(saidt, cpos,
+								       parset, parse_basepos,
+								       major, minor,
+								       base_words, base_words_nr,
+								       ref_words, ref_words_nr))
+						return 1;
+					gchild = aug_get_next_sibling(saidt, gchild, &gc_major, &gc_minor);
+				}
+			} else
+				sciprintf("augment_match_expression_p(): Unknown type 141 minor number %3x\n", cminor);
+
+			cpos = aug_get_next_sibling(saidt, cpos, &cmajor, &cminor);
+
+		}
+		break;
+
+	case WORD_TYPE_REF:
+		while (cpos) {
+			if (cminor == AUGMENT_SENTENCE_MINOR_MATCH_WORD) {
+				int word = aug_get_wgroup(saidt, cpos);
+				scidprintf("Looking for refword %03x\n", word);
+
+				if (aug_contains_word(ref_words, ref_words_nr, word))
+					return 1;
+			} else if (cminor == AUGMENT_SENTENCE_MINOR_MATCH_PHRASE) {
+				if (augment_match_expression_p(saidt, cpos,
+							       parset, parse_basepos,
+							       cmajor, cminor,
+							       base_words, base_words_nr,
+							       ref_words, ref_words_nr))
+					return 1;
+			} else if (cminor == AUGMENT_SENTENCE_MINOR_PARENTHESES) {
+				int gc_major, gc_minor;
+				int gchild = aug_get_first_child(saidt, cpos, &gc_major, &gc_minor);
+
+				while (gchild) {
+					if (augment_match_expression_p(saidt, cpos,
+								       parset, parse_basepos,
+								       major, minor,
+								       base_words, base_words_nr,
+								       ref_words, ref_words_nr))
+						return 1;
+					gchild = aug_get_next_sibling(saidt, gchild, &gc_major, &gc_minor);
+				}
+			} else
+				sciprintf("augment_match_expression_p(): Unknown type 144 minor number %3x\n", cminor);
+
+			cpos = aug_get_next_sibling(saidt, cpos, &cmajor, &cminor);
+
+		}
+		break;
+
+	case AUGMENT_SENTENCE_PART_BRACKETS:
+		if (augment_match_expression_p(saidt, cpos,
+					       parset, parse_basepos,
+					       cmajor, cminor,
+					       base_words, base_words_nr,
+					       ref_words, ref_words_nr))
+			return 1;
+
+		scidprintf("Didn't match subexpression; checking sub-bracked predicate %03x\n", cmajor);
+
+		switch (cmajor) {
+		case WORD_TYPE_BASE:
+			if (!base_words_nr)
+				return 1;
+			break;
+
+		case WORD_TYPE_REF:
+			if (!ref_words_nr)
+				return 1;
+			break;
+
+		default:
+			sciprintf("augment_match_expression_p(): (subp1) Unkonwn sub-bracket predicate %03x\n", cmajor);
+		}
+
+		break;
+
+	default:
+		sciprintf("augment_match_expression_p(): Unknown predicate %03x\n", major);
+
+	}
+
+	scidprintf("Generic failure\n");
+	return 0;
+}
+
+static int
+augment_sentence_expression(parse_tree_node_t *saidt, int augment_pos,
+			    parse_tree_node_t *parset, int parse_branch,
+			    int major, int minor,
+			    int *base_words, int base_words_nr,
+			    int *ref_words, int ref_words_nr)
+{
+	int check_major, check_minor;
+	int check_pos = aug_get_first_child(saidt, augment_pos, &check_major, &check_minor);
+	do {
+		if (!(augment_match_expression_p(saidt, check_pos, parset, parse_branch,
+						 check_major, check_minor, base_words, base_words_nr,
+						 ref_words, ref_words_nr)))
+			return 0;
+	} while ((check_pos = aug_get_next_sibling(saidt, check_pos, &check_major, &check_minor)));
+	return 1;
+}
+
+
+
+static int
+augment_sentence_part(parse_tree_node_t *saidt, int augment_pos,
+		      parse_tree_node_t *parset, int parse_basepos,
+		      int major, int minor)
+{
+	int pmajor, pminor;
+	int parse_branch = parse_basepos;
+	int optional = 0;
+	int foundwords = 0;
+
+	scidprintf("Augmenting (%03x %03x\n", major, minor);
+
+	if (major == AUGMENT_SENTENCE_PART_BRACKETS) { /* '[/ foo]' is true if '/foo' or if there
+						       ** exists no x for which '/x' is true
+						       */
+		if ((augment_pos = aug_get_first_child(saidt, augment_pos, &major, &minor))) {
+			scidprintf("Optional part: Now augmenting (%03x %03x\n", major, minor);
+			optional = 1;
+		} else {
+			scidprintf("Matched empty optional expression\n");
+			return 1;
+		}
+	}
+
+	if ((major < 0x141)
+	    || (major > 0x143)) {
+		scidprintf("augment_sentence_part(): Unexpected sentence part major number %03x\n", major);
+		return 0;
+	}
+
+	while ((parse_branch = aug_get_next_sibling(parset, parse_branch, &pmajor, &pminor)))
+		if (pmajor == major) { /* found matching sentence part */
+			int success;
+			int base_words_nr;
+			int ref_words_nr;
+			int base_words[AUGMENT_MAX_WORDS];
+			int ref_words[AUGMENT_MAX_WORDS];
+#ifdef SCI_DEBUG_PARSE_TREE_AUGMENTATION
+			int i;
+#endif
+
+			scidprintf("Found match with pminor = %03x\n", pminor);
+			aug_find_words(parset, parse_branch, base_words, &base_words_nr,
+				       ref_words, &ref_words_nr, AUGMENT_MAX_WORDS);
+			foundwords |= (ref_words_nr | base_words_nr);
+#ifdef SCI_DEBUG_PARSE_TREE_AUGMENTATION
+			sciprintf("%d base words:", base_words_nr);
+			for (i = 0; i < base_words_nr; i++)
+				sciprintf(" %03x", base_words[i]);
+			sciprintf("\n%d reference words:", ref_words_nr);
+			for (i = 0; i < ref_words_nr; i++)
+				sciprintf(" %03x", ref_words[i]);
+			sciprintf("\n");
+#endif
+
+			success = augment_sentence_expression(saidt, augment_pos,
+							      parset, parse_basepos, major, minor,
+							      base_words, base_words_nr,
+							      ref_words, ref_words_nr);
+
+			if (success) {
+				scidprintf("SUCCESS on augmenting (%03x %03x\n", major, minor);
+				return 1;
+			}
+		}
+
+	if (optional && (foundwords == 0)) {
+		scidprintf("Found no words and optional branch => SUCCESS on augmenting (%03x %03x\n", major, minor);
+		return 1;
+	}
+	scidprintf("FAILURE on augmenting (%03x %03x\n", major, minor);
+	return 0;
+}
+
+static int
+augment_parse_nodes(parse_tree_node_t *parset, parse_tree_node_t *saidt)
+{
+	int augment_basepos = 0;
+	int parse_basepos;
+	int major, minor;
+	int dontclaim = 0;
+
+	parse_basepos = aug_get_base_node(parset);
+	if (!parse_basepos) {
+		sciprintf("augment_parse_nodes(): Parse tree is corrupt\n");
+		return 0;
+	}
+  
+	augment_basepos = aug_get_base_node(saidt);
+	if (!augment_basepos) {
+		sciprintf("augment_parse_nodes(): Said tree is corrupt\n");
+		return 0;
+	}
+	while ((augment_basepos = aug_get_next_sibling(saidt, augment_basepos, &major, &minor))) {
+
+		if ((major == 0x14b)
+		    && (minor == SAID_LONG(SAID_GT)))
+			dontclaim = 1; /* special case */
+		else /* normal sentence part */
+			if (!(augment_sentence_part(saidt, augment_basepos, parset, parse_basepos, major, minor))) {
+				scidprintf("Returning failure\n");
+				return 0; /* fail */
+			}
+	}
+
+	scidprintf("Returning success with dontclaim=%d\n", dontclaim);
+
+	if (dontclaim)
+		return SAID_PARTIAL_MATCH;
+	else return 1; /* full match */
+}
+
+
+/*******************/
+/**** Main code ****/
+/*******************/
+
+int
+said(state_t *s, byte *spec, int verbose)
+{
+	int retval;
+
+	parse_tree_node_t *parse_tree_ptr = s->parser_nodes;
+
+	if (s->parser_valid) {
+
+		if (said_parse_spec(s, spec)) {
+			sciprintf("Offending spec was: ");
+			vocab_decypher_said_block(s, spec);
+			return SAID_NO_MATCH;
+		}
+
+		if (verbose)
+			vocab_dump_parse_tree("Said-tree", said_tree); /* Nothing better to do yet */
+		retval = augment_parse_nodes(parse_tree_ptr, &(said_tree[0]));
+
+		if (!retval)
+			return SAID_NO_MATCH;
+		else if (retval != SAID_PARTIAL_MATCH)
+			return SAID_FULL_MATCH;
+		else return SAID_PARTIAL_MATCH;
+	}
+
+	return SAID_NO_MATCH;
+}
+
+
+
+#ifdef SAID_DEBUG_PROGRAM
+int
+main (int argc, char *argv)
+{
+	byte block[] = {0x01, 0x00, 0xf8, 0xf5, 0x02, 0x01, 0xf6, 0xf2, 0x02, 0x01, 0xf2, 0x01, 0x03, 0xff};
+	state_t s;
+	con_passthrough = 1;
+
+	s.parser_valid = 1;
+	said(&s, block);
+}
+#endif

Copied: scummvm/trunk/engines/sci/engine/savegame.cfsml (from rev 38467, scummvm/trunk/engines/sci/engine/savegame.cfsml)
===================================================================
--- scummvm/trunk/engines/sci/engine/savegame.cfsml	                        (rev 0)
+++ scummvm/trunk/engines/sci/engine/savegame.cfsml	2009-02-18 15:05:00 UTC (rev 38485)
@@ -0,0 +1,1150 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $URL$
+ * $Id$
+ *
+ */
+
+/* Savegame handling for state_t structs. Makes heavy use of cfsml magic. */
+/* DON'T EDIT savegame.c ! Only modify savegame.cfsml, if something needs
+** to be changed. Refer to freesci/docs/misc/cfsml.spec if you don't understand
+** savegame.cfsml. If this doesn't solve your problem, contact the maintainer.
+*/
+
+#include "sci/include/sci_memory.h"
+#include "sci/include/gfx_operations.h"
+#include "sci/include/sfx_engine.h"
+#include "sci/include/engine.h"
+#include "sci/engine/heap.h"
+
+#ifdef _MSC_VER
+#include <direct.h>
+#endif
+
+#ifdef _WIN32
+#pragma warning( disable : 4101 )
+#endif
+
+#define HUNK_TYPE_GFX_SNAPSHOT_STRING "g\n"
+
+/* Missing:
+** - SFXdriver
+** - File input/output state (this is likely not to happen)
+*/
+
+static state_t *_global_save_state;
+/* Needed for some graphical stuff. */
+#define FILE_VERSION _global_save_state->savegame_version
+
+
+void
+write_reg_t(FILE *fh, reg_t *foo) {
+	fprintf(fh, PREG, PRINT_REG(*foo));
+}
+
+int
+read_reg_t(FILE *fh, reg_t *foo, const char *lastval, int *line, int *hiteof) {
+	int segment, offset;
+
+	if (sscanf(lastval, PREG, &segment, &offset)<2) {
+		sciprintf("Error parsing reg_t on line %d\n", *line);
+		return 1;
+	}
+
+	*foo = make_reg(segment, offset);
+	return 0;
+}
+
+void
+write_sci_version(FILE *fh, sci_version_t *foo) {
+	fprintf(fh, "%d.%03d.%03d", SCI_VERSION_MAJOR(*foo), SCI_VERSION_MINOR(*foo),
+		SCI_VERSION_PATCHLEVEL(*foo));
+}
+
+int
+read_sci_version(FILE *fh, sci_version_t *foo, const char *lastval, int *line, int *hiteof) {
+	return version_parse(lastval, foo);
+}
+
+void
+write_PTN(FILE *fh, parse_tree_node_t *foo) {
+	if (foo->type == PARSE_TREE_NODE_LEAF)
+		fprintf(fh, "L%d", foo->content.value);
+	else
+		fprintf(fh, "B(%d,%d)", foo->content.branches[0], foo->content.branches[1]);
+}
+
+int
+read_PTN(FILE *fh, parse_tree_node_t *foo, const char *lastval, int *line, int *hiteof) {
+	if (lastval[0] == 'L') {
+		const char *c = lastval + 1;
+		char *strend;
+
+		while (*c && isspace(*c))
+			++c;
+
+		if (!*c)
+			return 1;
+
+		foo->content.value = strtol(c, &strend, 0);
+
+		return (strend == c); /* Error if nothing could be read */
+
+		return 0;
+	} else if (lastval[0] == 'B') {
+		const char *c = lastval + 1;
+		char *strend;
+
+		while (*c && isspace(*c)) ++c;
+		if (*c++ != '(') return 1;
+		while (*c && isspace(*c)) ++c;
+
+		foo->content.branches[0] = strtol(c, &strend, 0);
+		if (strend == c)
+			return 1;
+		c = strend;
+
+		while (*c && isspace(*c)) ++c;
+		if (*c++ != ',')
+			return 1;
+
+		while (*c && isspace(*c)) ++c;
+
+		foo->content.branches[1] = strtol(c, &strend, 0);
+		if (strend == c)
+			return 1;
+		c = strend;
+
+		while (*c && isspace(*c)) ++c;
+		if (*c++ != ')') return 1;
+
+		return 0;
+	} else return 1; /* failure to parse anything */
+}
+
+
+void
+write_menubar_tp(FILE *fh, menubar_t **foo);
+int
+read_menubar_tp(FILE *fh, menubar_t **foo, const char *lastval, int *line, int *hiteof);
+
+void
+write_mem_obj_tp(FILE *fh, mem_obj_t **foo);
+int
+read_mem_obj_tp(FILE *fh, mem_obj_t **foo, const char *lastval, int *line, int *hiteof);
+
+void
+write_int_hash_map_tp(FILE *fh, int_hash_map_t **foo);
+int
+read_int_hash_map_tp(FILE *fh, int_hash_map_t **foo, const char *lastval, int *line, int *hiteof);
+
+void
+write_songlib_t(FILE *fh, songlib_t *foo);
+int
+read_songlib_t(FILE *fh, songlib_t *foo, const char *lastval, int *line, int *hiteof);
+
+void
+write_int_hash_map_node_tp(FILE *fh, int_hash_map_t::node_t **foo);
+int
+read_int_hash_map_node_tp(FILE *fh, int_hash_map_t::node_t **foo, const char *lastval, int *line, int *hiteof);
+
+int
+read_song_tp(FILE *fh, song_t **foo, const char *lastval, int *line, int *hiteof);
+
+typedef mem_obj_t *mem_obj_ptr;
+
+%CFSML
+
+TYPE byte "byte" LIKE int;
+TYPE long "long" LIKE int;
+TYPE gint16 "gint16" LIKE int;
+TYPE seg_id_t "seg_id_t" LIKE int;
+TYPE sci_version_t "sci_version_t" USING write_sci_version read_sci_version;
+TYPE menubar_tp "menubar_t *" USING write_menubar_tp read_menubar_tp;
+TYPE mem_obj_t "mem_obj_t" USING write_mem_obj_t read_mem_obj_t;
+TYPE mem_obj_ptr "mem_obj_t *" USING write_mem_obj_tp read_mem_obj_tp;
+TYPE reg_t "reg_t" USING write_reg_t read_reg_t;
+TYPE size_t "size_t" LIKE int;
+TYPE int_hash_map_tp "int_hash_map_t *" USING write_int_hash_map_tp read_int_hash_map_tp;
+TYPE int_hash_map_node_tp "int_hash_map_t::node_t *" USING write_int_hash_map_node_tp read_int_hash_map_node_tp;
+TYPE songlib_t "songlib_t" USING write_songlib_t read_songlib_t;
+TYPE song_tp "song_t *" USING write_song_tp read_song_tp;
+TYPE song_iterator_t "song_iterator_t" USING write_song_iterator_t read_song_iterator_t;
+TYPE song_handle_t "song_handle_t" LIKE int;
+
+RECORD song_t "song_t" {
+	song_handle_t handle;
+	int resource_num;
+	int priority;
+	int status;
+	int restore_behavior;
+	int restore_time;
+	int loops;
+	int hold;
+}
+	
+RECORD int_hash_map_t "int_hash_map_t" {
+	int base_value;
+	int_hash_map_node_tp nodes[STATIC DCS_INT_HASH_MAX+1];
+}
+
+RECORD menu_item_t "menu_item_t" {
+	int type;
+	string keytext;
+	int keytext_size;
+
+	int flags;
+	byte said[STATIC MENU_SAID_SPEC_SIZE];
+	reg_t said_pos;
+	string text;
+	reg_t text_pos;
+	int modifiers;
+	int key;
+	int enabled;
+	int tag;
+}
+
+RECORD menu_t "menu_t" {
+	string title;
+	int title_width;
+	int width;
+
+	menu_item_t items[DYNAMIC items_nr];
+}
+
+RECORD menubar_t "menubar_t" {
+	menu_t menus[DYNAMIC menus_nr];
+}
+
+RECORD synonym_t "synonym_t" {
+	int replaceant;
+	int replacement;
+}
+
+
+RECORD seg_manager_t "seg_manager_t" {
+	int_hash_map_tp id_seg_map;
+	mem_obj_ptr heap[DYNAMIC heap_size];
+	int heap_size;
+	int reserved_id;
+	int exports_wide;
+	int sci1_1;
+	int gc_mark_bits;
+	size_t mem_allocated;
+	seg_id_t clones_seg_id;
+	seg_id_t lists_seg_id;
+	seg_id_t nodes_seg_id;
+}
+
+RECORD class_t "class_t" {
+	int script;
+	reg_t reg;
+}
+
+RECORD sfx_state_t "sfx_state_t" {
+	songlib_t songlib;
+}
+
+RECORD state_t "state_t" {
+	int savegame_version;
+
+	string game_version;
+	sci_version_t version;
+	menubar_tp menubar;
+	int status_bar_foreground;
+	int status_bar_background;
+	seg_manager_t seg_manager;
+	int classtable_size;
+	class_t classtable[DYNAMIC classtable_size];
+	sfx_state_t sound;
+}
+
+RECORD local_variables_t "local_variables_t" {
+	int script_id;
+	int nr;
+	reg_t locals[DYNAMIC nr];
+}
+
+RECORD object_t "object_t" {
+	int flags;
+	reg_t pos;
+	int variables_nr;
+	int variable_names_nr;
+	int methods_nr;
+	reg_t variables[DYNAMIC variables_nr];
+}
+
+RECORD clone_t "clone_t" {
+	int flags;
+	reg_t pos;
+	int variables_nr;
+	int variable_names_nr;
+	int methods_nr;
+	reg_t variables[DYNAMIC variables_nr];
+}
+
+RECORD list_t "list_t" {
+	reg_t first;
+	reg_t last;
+}
+
+RECORD node_t "node_t" {
+	reg_t pred;
+	reg_t succ;
+	reg_t key;
+	reg_t value;
+}
+
+RECORD clone_entry_t "clone_entry_t" {
+	int next_free;
+	clone_t entry;
+}
+
+RECORD clone_table_t "clone_table_t" {
+	int entries_nr;
+	int first_free;
+	int entries_used;
+	int max_entry;
+	clone_entry_t table[DYNAMIC entries_nr];
+}
+
+RECORD list_entry_t "list_entry_t" {
+	int next_free;
+	list_t entry;
+}
+
+RECORD list_table_t "list_table_t" {
+	int entries_nr;
+	int first_free;
+	int entries_used;
+	int max_entry;
+	list_entry_t table[DYNAMIC entries_nr];
+}
+
+RECORD node_entry_t "node_entry_t" {
+	int next_free;
+	node_t entry;
+}
+
+RECORD node_table_t "node_table_t" {
+	int entries_nr;
+	int first_free;
+	int entries_used;
+	int max_entry;
+	node_entry_t table[DYNAMIC entries_nr];
+}
+
+RECORD script_t "script_t" {
+	int nr;
+
+	size_t buf_size;
+	size_t script_size;
+	size_t heap_size;
+
+	int_hash_map_tp obj_indices;
+	int exports_nr;
+	int synonyms_nr;
+	int lockers;
+	int objects_allocated;
+	int objects_nr;
+	object_t objects[DYNAMIC objects_allocated];
+
+	int locals_offset;
+	int locals_segment;
+
+	int marked_as_deleted;
+}
+
+RECORD sys_string_t "sys_string_t" {
+	string name;
+	int max_size;
+	string value;
+}
+
+RECORD sys_strings_t "sys_strings_t" {
+	sys_string_t strings[STATIC SYS_STRINGS_MAX];
+}
+
+RECORD dynmem_t "dynmem_t" {
+	int size;
+	string description;
+	byte buf[DYNAMIC size];
+}
+
+%END CFSML
+
+void 
+write_songlib_t(FILE *fh, songlib_t *songlib) {
+	song_t *seeker = *(songlib->lib);
+	int songcount = song_lib_count(*songlib);
+
+	fprintf(fh, "{\n");
+	fprintf(fh, "songcount = %d\n", songcount);
+	fprintf(fh, "list = \n");
+	fprintf(fh, "[\n");
+	while (seeker) {
+		seeker->restore_time = seeker->it->get_timepos(seeker->it);
+		%CFSMLWRITE song_t seeker INTO fh;
+		seeker = seeker->next;
+	}
+	fprintf(fh, "]\n");
+	fprintf(fh, "}\n");
+}
+
+int read_songlib_t(FILE *fh, songlib_t *songlib, const char *lastval, int *line, int *hiteof) {
+	int songcount;
+	int i;
+	song_t *newsong;
+	int oldstatus;
+
+	fscanf(fh, "{\n");
+	fscanf(fh, "songcount = %d\n", &songcount);
+	fscanf(fh, "list = \n");
+	fscanf(fh, "[\n");
+	*line += 4;
+	song_lib_init(songlib);
+	for (i = 0; i < songcount; i++) {
+		%CFSMLREAD song_tp &newsong FROM fh ERRVAR *hiteof FIRSTTOKEN lastval LINECOUNTER *line;
+		song_lib_add(*songlib, newsong);
+	}  
+	fscanf(fh, "]\n");
+	fscanf(fh, "}\n");;
+	*line += 2;
+	return 0;
+}
+
+struct {
+	int type;
+	const char *name;
+} mem_obj_string_names[] = {
+	{MEM_OBJ_INVALID, "INVALID"},
+	{MEM_OBJ_SCRIPT, "SCRIPT"},
+	{MEM_OBJ_CLONES, "CLONES"},
+	{MEM_OBJ_LOCALS, "LOCALS"},
+	{MEM_OBJ_STACK, "STACK"},
+	{MEM_OBJ_SYS_STRINGS,"SYS_STRINGS"},
+	{MEM_OBJ_LISTS,"LISTS"},
+	{MEM_OBJ_NODES,"NODES"},
+	{MEM_OBJ_HUNK,"HUNK"},
+	{MEM_OBJ_DYNMEM,"DYNMEM"}};
+
+int mem_obj_string_to_enum(const char *str) {
+	int i;
+
+	for (i = 0; i <= MEM_OBJ_MAX; i++) {
+		if (!scumm_stricmp(mem_obj_string_names[i].name, str))
+			return i;
+	}
+
+	return -1;
+}
+
+static int bucket_length;
+
+void write_int_hash_map_tp(FILE *fh, int_hash_map_t **foo) {
+	%CFSMLWRITE int_hash_map_t *foo INTO fh;
+}
+
+void write_song_tp(FILE *fh, song_t **foo) {
+	%CFSMLWRITE song_t *foo INTO fh;
+}
+
+song_iterator_t *build_iterator(state_t *s, int song_nr, int type, songit_id_t id);
+
+int read_song_tp(FILE *fh, song_t **foo, const char *lastval, int *line, int *hiteof) {
+  char *token;
+  int assignment;
+  *foo = (song_t*) malloc(sizeof(song_t));
+  token = _cfsml_get_identifier(fh, line, hiteof, &assignment);
+  %CFSMLREAD song_t (*foo) FROM fh ERRVAR *hiteof FIRSTTOKEN token LINECOUNTER *line;
+  (*foo)->delay = 0;
+  (*foo)->it = NULL;
+  (*foo)->next_playing = (*foo)->next_stopping = (*foo)->next = NULL;
+  return 0;
+}
+int
+read_int_hash_map_tp(FILE *fh, int_hash_map_t **foo, const char *lastval, int *line, int *hiteof) {
+	*foo = (int_hash_map_t*)malloc(sizeof(int_hash_map_t));
+	%CFSMLREAD int_hash_map_t (*foo) FROM fh ERRVAR *hiteof FIRSTTOKEN lastval LINECOUNTER *line;
+	(*foo)->holes = NULL;
+	return 0;
+}
+
+void
+write_int_hash_map_node_tp(FILE *fh, int_hash_map_t::node_t **foo) {
+	if (!(*foo)) {
+		fputs("\\null", fh);
+	} else {
+		fprintf(fh,"[\n%d=>%d\n", (*foo)->name, (*foo)->value);
+		if ((*foo)->next) {
+			%CFSMLWRITE int_hash_map_node_tp &((*foo)->next) INTO fh;
+		} else fputc('L', fh);
+		fputs("]", fh);
+	}
+}
+
+int
+read_int_hash_map_node_tp(FILE *fh, int_hash_map_t::node_t **foo, const char *lastval, int *line, int *hiteof) {
+	static char buffer[80];
+
+	if (lastval[0] == '\\') {
+		*foo = NULL; /* No hash map node */
+	} else {
+		*foo = (int_hash_map_t::node_t*)malloc(sizeof(int_hash_map_t::node_t));
+		if (lastval[0] != '[') {
+			sciprintf("Expected opening bracket in hash_map_node_t on line %d\n", *line);
+			return 1;
+		}
+		
+		do {
+			(*line)++;
+			fgets(buffer, 80, fh);
+			if (buffer[0] == 'L') {
+				(*foo)->next = NULL;
+				buffer[0] = buffer[1];
+			} /* HACK: deliberately no else clause here */
+			if (buffer[0] == ']')  {
+				break;
+			}
+			else if (buffer[0] == '[') {
+				if (read_int_hash_map_node_tp(fh, &((*foo)->next), buffer, line, hiteof))
+					return 1;
+			}
+			else if (sscanf(buffer, "%d=>%d", &((*foo)->name), &((*foo)->value))<2) {
+				sciprintf("Error parsing hash_map_node_t on line %d\n", *line);
+				return 1;
+			}
+		} while (1);
+	}
+
+	return 0;
+}
+
+void
+write_menubar_tp(FILE *fh, menubar_t **foo) {
+	if (*foo) {
+
+		%CFSMLWRITE menubar_t (*foo) INTO fh;
+
+	} else { /* Nothing to write */
+		fputs("\\null\\", fh);
+	}
+}
+
+
+int
+read_menubar_tp(FILE *fh, menubar_t **foo, const char *lastval, int *line, int *hiteof) {
+
+	if (lastval[0] == '\\') {
+		*foo = NULL; /* No menu bar */
+	} else {
+
+		*foo = (menubar_t *) sci_malloc(sizeof(menubar_t));
+		%CFSMLREAD menubar_t (*foo) FROM fh ERRVAR *hiteof FIRSTTOKEN lastval LINECOUNTER *line;
+
+	}
+	return *hiteof;
+}
+
+void
+write_mem_obj_t(FILE *fh, mem_obj_t *foo) {
+	fprintf(fh, "%s\n", mem_obj_string_names[foo->type].name);	
+	%CFSMLWRITE int &foo->segmgr_id INTO fh;
+	switch (foo->type) {
+	case MEM_OBJ_SCRIPT:
+		%CFSMLWRITE script_t &foo->data.script INTO fh;
+	break;
+	case MEM_OBJ_CLONES:
+		%CFSMLWRITE clone_table_t &foo->data.clones INTO fh;
+	break;
+	case MEM_OBJ_LOCALS:
+		%CFSMLWRITE local_variables_t &foo->data.locals INTO fh;
+	break;
+	case MEM_OBJ_SYS_STRINGS:
+		%CFSMLWRITE sys_strings_t &foo->data.sys_strings INTO fh;
+	break;
+	case MEM_OBJ_STACK:
+		%CFSMLWRITE int &foo->data.stack.nr INTO fh;
+	break;
+	case MEM_OBJ_HUNK:
+		break;
+	case MEM_OBJ_LISTS:	
+		%CFSMLWRITE list_table_t &foo->data.lists INTO fh;
+	break;
+	case MEM_OBJ_NODES:	
+		%CFSMLWRITE node_table_t &foo->data.nodes INTO fh;
+	break;
+	case MEM_OBJ_DYNMEM:
+		%CFSMLWRITE dynmem_t  &foo->data.dynmem INTO fh;
+	break;
+	}
+}
+
+int
+read_mem_obj_t(FILE *fh, mem_obj_t *foo, const char *lastval, int *line, int *hiteof) {
+	char buffer[80];
+	foo->type = mem_obj_string_to_enum(lastval);
+	if (foo->type < 0) {
+		sciprintf("Unknown mem_obj_t type %s on line %d\n", lastval, *line);
+		return 1;
+	}
+
+	%CFSMLREAD int &foo->segmgr_id FROM fh ERRVAR *hiteof LINECOUNTER *line;
+	switch (foo->type) {
+	case MEM_OBJ_SCRIPT:
+		%CFSMLREAD script_t &foo->data.script FROM fh ERRVAR *hiteof LINECOUNTER *line;
+	break;
+	case MEM_OBJ_CLONES:
+		%CFSMLREAD clone_table_t &foo->data.clones FROM fh ERRVAR *hiteof LINECOUNTER *line;
+	break;
+	case MEM_OBJ_LOCALS:
+		%CFSMLREAD local_variables_t &foo->data.locals FROM fh ERRVAR *hiteof LINECOUNTER *line;
+	break;
+	case MEM_OBJ_SYS_STRINGS:
+		%CFSMLREAD sys_strings_t &foo->data.sys_strings FROM fh ERRVAR *hiteof LINECOUNTER *line;
+	break;
+	case MEM_OBJ_LISTS:
+		%CFSMLREAD list_table_t &foo->data.lists FROM fh ERRVAR *hiteof LINECOUNTER *line;
+	break;
+	case MEM_OBJ_NODES:
+		%CFSMLREAD node_table_t &foo->data.nodes FROM fh ERRVAR *hiteof LINECOUNTER *line;
+	break;
+	case MEM_OBJ_STACK:
+		%CFSMLREAD int &foo->data.stack.nr FROM fh ERRVAR *hiteof LINECOUNTER *line;
+	foo->data.stack.entries = (reg_t *)sci_calloc(foo->data.stack.nr, sizeof(reg_t));
+	break;
+	case MEM_OBJ_HUNK:
+		init_hunk_table(&foo->data.hunks);
+		break;
+	case MEM_OBJ_DYNMEM:
+		%CFSMLREAD dynmem_t  &foo->data.dynmem FROM fh ERRVAR *hiteof LINECOUNTER *line;
+	break;
+	}
+
+	return *hiteof;
+}
+
+void
+write_mem_obj_tp(FILE *fh, mem_obj_t **foo) {
+	if (*foo) {
+
+		%CFSMLWRITE mem_obj_t (*foo) INTO fh;
+
+	} else { /* Nothing to write */
+		fputs("\\null\\", fh);
+	}
+}
+
+int
+read_mem_obj_tp(FILE *fh, mem_obj_t **foo, const char *lastval, int *line, int *hiteof) {
+
+	if (lastval[0] == '\\') {
+		*foo = NULL; /* No menu bar */
+	} else {
+		*foo = (mem_obj_t *) sci_malloc(sizeof(mem_obj_t));
+		%CFSMLREAD mem_obj_t (*foo) FROM fh ERRVAR *hiteof FIRSTTOKEN lastval LINECOUNTER *line;
+		return *hiteof;
+	}
+	return 0;
+}
+
+
+
+/* This function is called to undo some strange stuff done in preparation
+** to writing a gamestate to disk
+*/
+void
+_gamestate_unfrob(state_t *s) {
+}
+
+
+int
+gamestate_save(state_t *s, char *dirname) {
+	FILE *fh;
+	sci_dir_t dir;
+	char *filename;
+	int fd;
+
+	_global_save_state = s;
+	s->savegame_version = FREESCI_CURRENT_SAVEGAME_VERSION;
+	s->dyn_views_list_serial = (s->dyn_views)? s->dyn_views->serial : -2;
+	s->drop_views_list_serial = (s->drop_views)? s->drop_views->serial : -2;
+	s->port_serial = (s->port)? s->port->serial : -2;
+
+	if (s->execution_stack_base) {
+		sciprintf("Cannot save from below kernel function\n");
+		return 1;
+	}
+
+	scimkdir (dirname, 0700);
+
+	if (chdir(dirname)) {
+		sciprintf("Could not enter directory '%s'\n", dirname);
+		return 1;
+	}
+
+	sci_init_dir(&dir);
+	filename = sci_find_first(&dir, "*");
+	while (filename) {
+		if (strcmp(filename, "..") && strcmp(filename, "."))
+			unlink(filename); /* Delete all files in directory */
+		filename = sci_find_next(&dir);
+	}
+	sci_finish_find(&dir);
+
+/*
+	if (s->sound_server) {
+		if ((s->sound_server->save)(s, dirname)) {
+			sciprintf("Saving failed for the sound subsystem\n");
+			chdir("..");
+			return 1;
+		}
+	}
+*/
+	fh = fopen("state", "w" FO_TEXT);
+
+	/* Calculate the time spent with this game */
+	s->game_time = time(NULL) - s->game_start_time.tv_sec;
+
+SCI_MEMTEST;
+	%CFSMLWRITE state_t s INTO fh;
+SCI_MEMTEST;
+
+	fclose(fh);
+
+	_gamestate_unfrob(s);
+
+
+	chdir("..");
+	return 0;
+}
+
+static seg_id_t
+find_unique_seg_by_type(seg_manager_t *self, int type) {
+	int i;
+
+	for (i = 0; i < self->heap_size; i++)
+		if (self->heap[i] &&
+		    self->heap[i]->type == type)
+			return i;
+	return -1;
+}
+
+static byte *
+find_unique_script_block(state_t *s, byte *buf, int type) {
+	int magic_pos_adder = s->version >= SCI_VERSION_FTU_NEW_SCRIPT_HEADER ? 0 : 2;
+
+	buf += magic_pos_adder;
+	do {
+		int seeker_type = getUInt16(buf);
+		int seeker_size;
+
+		if (seeker_type == 0) break;
+		if (seeker_type == type) return buf;
+
+		seeker_size = getUInt16(buf + 2);
+		buf += seeker_size;
+	} while(1);
+
+	return NULL;
+}
+
+static
+void reconstruct_stack(state_t *retval) {
+	seg_id_t stack_seg = find_unique_seg_by_type(&retval->seg_manager, MEM_OBJ_STACK);
+	dstack_t *stack = &(retval->seg_manager.heap[stack_seg]->data.stack);
+
+	retval->stack_segment = stack_seg;
+	retval->stack_base = stack->entries;
+	retval->stack_top = retval->stack_base + VM_STACK_SIZE;
+}
+
+static
+int clone_entry_used(clone_table_t *table, int n) {
+	int backup;
+	int seeker = table->first_free;
+	clone_entry_t *entries = table->table;
+
+	if (seeker == HEAPENTRY_INVALID) return 1;
+
+	do {
+		if (seeker == n) return 0;
+		backup = seeker;
+		seeker = entries[seeker].next_free;
+	} while (entries[backup].next_free != HEAPENTRY_INVALID);
+
+	return 1;
+}
+
+static
+void load_script(state_t *s, seg_id_t seg) {
+	resource_t *script, *heap = NULL;
+	script_t *scr = &(s->seg_manager.heap[seg]->data.script);
+
+	scr->buf = (byte *) malloc(scr->buf_size);
+
+	script = scir_find_resource(s->resmgr, sci_script, scr->nr, 0);
+	if (s->version >= SCI_VERSION(1,001,000))
+		heap = scir_find_resource(s->resmgr, sci_heap, scr->nr, 0);
+
+	switch (s->seg_manager.sci1_1) {
+	case 0 :
+		sm_mcpy_in_out( &s->seg_manager, 0, script->data, script->size, seg, SEG_ID);
+		break;
+	case 1 :
+		sm_mcpy_in_out( &s->seg_manager, 0, script->data, script->size, seg, SEG_ID);
+		sm_mcpy_in_out( &s->seg_manager, scr->script_size, heap->data, heap->size, seg, SEG_ID);
+		break;
+	}
+}
+
+static
+void reconstruct_scripts(state_t *s, seg_manager_t *self) {
+	int i;
+	mem_obj_t *mobj;
+	object_t **objects;
+	int *objects_nr;
+	for (i = 0; i < self->heap_size; i++)
+		if (self->heap[i]) {
+			mobj = self->heap[i];
+			switch (mobj->type)  {
+			case MEM_OBJ_SCRIPT: {
+				int j;
+				script_t *scr = &mobj->data.script;
+
+				load_script(s, i);
+				scr->locals_block = scr->locals_segment == 0 ? NULL :
+					&s->seg_manager.heap[scr->locals_segment]->data.locals;
+				scr->export_table = (guint16 *) find_unique_script_block(s, scr->buf, sci_obj_exports);
+				scr->synonyms = find_unique_script_block(s, scr->buf, sci_obj_synonyms);
+				scr->code = NULL;
+				scr->code_blocks_nr = 0;
+				scr->code_blocks_allocated = 0;
+
+				if (!self->sci1_1)
+					scr->export_table += 3;
+				
+				for (j = 0; j < scr->objects_nr; j++) {
+					byte *data = scr->buf + scr->objects[j].pos.offset;
+					scr->objects[j].base = scr->buf;
+					scr->objects[j].base_obj = data;
+				}
+
+			}
+			}
+		}
+
+	for (i = 0; i < self->heap_size; i++)
+		if (self->heap[i]) {
+			mobj = self->heap[i];
+			switch (mobj->type)  {
+			case MEM_OBJ_SCRIPT: {
+				int j;
+				script_t *scr = &mobj->data.script;
+
+				for (j = 0; j < scr->objects_nr; j++) {
+					byte *data = scr->buf + scr->objects[j].pos.offset;
+
+					if (self->sci1_1) {
+						guint16 *funct_area = (guint16 *) (scr->buf + getUInt16( data + 6 ));
+						guint16 *prop_area = (guint16 *) (scr->buf + getUInt16( data + 4 ));
+
+						scr->objects[j].base_method = funct_area;
+						scr->objects[j].base_vars = prop_area;
+					} else {
+						int funct_area = getUInt16( data + SCRIPT_FUNCTAREAPTR_OFFSET );
+						object_t *base_obj;
+
+						base_obj = obj_get(s, scr->objects[j].variables[SCRIPT_SPECIES_SELECTOR]);
+
+						if (!base_obj) {
+							sciprintf("Object without a base class: Script %d, index %d (reg address "PREG"\n",
+								  scr->nr, j, PRINT_REG(scr->objects[j].variables[SCRIPT_SPECIES_SELECTOR]));
+							continue;
+						}
+						scr->objects[j].variable_names_nr = base_obj->variables_nr;
+						scr->objects[j].base_obj = base_obj->base_obj;
+
+						scr->objects[j].base_method = (guint16 *) (data + funct_area);
+						scr->objects[j].base_vars = (guint16 *) (data + scr->objects[j].variable_names_nr * 2 + SCRIPT_SELECTOR_OFFSET);
+					}
+				}
+			}
+			}
+		}
+}
+
+void
+reconstruct_clones(state_t *s, seg_manager_t *self) {
+	int i;
+	mem_obj_t *mobj;
+
+	for (i = 0; i < self->heap_size; i++)
+		if (self->heap[i]) {
+			mobj = self->heap[i];
+			switch (mobj->type) {
+			case MEM_OBJ_CLONES: {
+				int j;
+				clone_entry_t *seeker = mobj->data.clones.table;
+				
+				sciprintf("Free list: ");
+				for (j = mobj->data.clones.first_free;
+				     j != HEAPENTRY_INVALID;
+				     j = mobj->data.clones.table[j].next_free) {
+					sciprintf("%d ", j);
+				}
+				sciprintf("\n");
+
+				sciprintf("Entries w/zero vars: ");
+				for (j = 0; j < mobj->data.clones.max_entry; j++) {
+					if (mobj->data.clones.table[j].entry.variables == NULL)
+						sciprintf("%d ", j);
+				}
+				sciprintf("\n");
+
+				for (j = 0; j < mobj->data.clones.max_entry; j++) {
+ 					object_t *base_obj;
+
+					if (!clone_entry_used(&mobj->data.clones, j)) {
+						seeker++;
+						continue;
+					}
+					base_obj = obj_get(s, seeker->entry.variables[SCRIPT_SPECIES_SELECTOR]);
+					if (!base_obj) {
+						sciprintf("Clone entry without a base class: %d\n", j);
+						seeker->entry.base = seeker->entry.base_obj = NULL;
+						seeker->entry.base_vars = seeker->entry.base_method = NULL;
+						continue;
+					}
+					seeker->entry.base = base_obj->base;
+					seeker->entry.base_obj = base_obj->base_obj;
+					seeker->entry.base_vars = base_obj->base_vars;
+					seeker->entry.base_method = base_obj->base_method;
+
+					seeker++;
+				}
+
+				break;
+			}
+			}
+		}
+}
+
+int
+_reset_graphics_input(state_t *s);
+
+song_iterator_t *
+new_fast_forward_iterator(song_iterator_t *it, int delta);
+
+static
+void reconstruct_sounds(state_t *s) {
+  song_t *seeker;
+  int it_type = s->resmgr->sci_version >= SCI_VERSION_01 ?
+    SCI_SONG_ITERATOR_TYPE_SCI1
+    : SCI_SONG_ITERATOR_TYPE_SCI0;
+
+  if (s->sound.songlib.lib)
+    seeker = *(s->sound.songlib.lib);
+  else {
+      song_lib_init(&s->sound.songlib);
+      seeker = NULL;
+    }
+  while (seeker) {
+      song_iterator_t *base, *ff;
+      int oldstatus;
+      song_iterator_message_t msg;
+
+      base = ff = build_iterator(s, seeker->resource_num, it_type, seeker->handle);
+      if (seeker->restore_behavior == RESTORE_BEHAVIOR_CONTINUE)
+	  ff = (song_iterator_t *) new_fast_forward_iterator(base, seeker->restore_time);
+      ff->init(ff);
+
+      msg = songit_make_message(seeker->handle, SIMSG_SET_LOOPS(seeker->loops));
+      songit_handle_message(&ff, msg);
+      msg = songit_make_message(seeker->handle, SIMSG_SET_HOLD(seeker->hold));
+      songit_handle_message(&ff, msg);
+
+
+      oldstatus = seeker->status;
+      seeker->status = SOUND_STATUS_STOPPED;
+      seeker->it = ff;
+      sfx_song_set_status(&s->sound, seeker->handle, oldstatus);
+      seeker = seeker->next;
+    }
+
+}
+
+state_t *
+gamestate_restore(state_t *s, char *dirname) {
+	FILE *fh;
+	int fd;
+	int i;
+	int read_eof = 0;
+	state_t *retval;
+	songlib_t temp;
+
+	if (chdir(dirname)) {
+		sciprintf("Game state '%s' does not exist\n", dirname);
+		return NULL;
+	}
+
+/*
+	if (s->sound_server) {
+		if ((s->sound_server->restore)(s, dirname)) {
+			sciprintf("Restoring failed for the sound subsystem\n");
+			return NULL;
+		}
+	}
+*/
+
+	retval = (state_t *) sci_malloc(sizeof(state_t));
+
+	memset(retval, 0, sizeof(state_t));
+
+	retval->savegame_version = -1;
+	_global_save_state = retval;
+	retval->gfx_state = s->gfx_state;
+
+	fh = fopen("state", "r" FO_TEXT);
+	if (!fh) {
+		free(retval);
+		return NULL;
+	}
+
+	/* Backwards compatibility settings */
+	retval->dyn_views = NULL;
+	retval->drop_views = NULL;
+	retval->port = NULL;
+	retval->save_dir_copy_buf = NULL;
+
+	retval->sound_mute = s->sound_mute;
+	retval->sound_volume = s->sound_volume;
+
+	%CFSMLREAD-ATOMIC state_t retval FROM fh ERRVAR read_eof;
+
+	fclose(fh);
+
+	if ((retval->savegame_version < FREESCI_MINIMUM_SAVEGAME_VERSION) ||
+	    (retval->savegame_version > FREESCI_CURRENT_SAVEGAME_VERSION)) {
+
+		if (retval->savegame_version < FREESCI_MINIMUM_SAVEGAME_VERSION)
+			sciprintf("Old savegame version detected- can't load\n");
+		else
+			sciprintf("Savegame version is %d- maximum supported is %0d\n", retval->savegame_version, FREESCI_CURRENT_SAVEGAME_VERSION);
+
+		chdir("..");
+		free(retval);
+		return NULL;
+	}
+
+	sfx_exit(&s->sound);
+	_gamestate_unfrob(retval);
+
+	/* Set exec stack base to zero */
+	retval->execution_stack_base = 0;
+	retval->execution_stack_pos = 0;
+
+	/* Now copy all current state information */
+	/* Graphics and input state: */
+	retval->animation_delay = s->animation_delay;
+	retval->animation_granularity = s->animation_granularity;
+	retval->gfx_state = s->gfx_state;
+
+	retval->resmgr = s->resmgr;
+
+	temp = retval->sound.songlib;
+	sfx_init(&retval->sound, retval->resmgr, s->sfx_init_flags);
+	retval->sfx_init_flags = s->sfx_init_flags;
+	song_lib_free(retval->sound.songlib);
+	retval->sound.songlib = temp;
+
+	_reset_graphics_input(retval);
+	reconstruct_stack(retval);
+	reconstruct_scripts(retval, &retval->seg_manager);
+	reconstruct_clones(retval, &retval->seg_manager);
+	retval->game_obj = s->game_obj;
+	retval->script_000 = &retval->seg_manager.heap[script_get_segment(s, 0, SCRIPT_GET_DONT_LOAD)]->data.script;
+	retval->gc_countdown = GC_INTERVAL - 1;
+	retval->save_dir_copy = make_reg(s->sys_strings_segment, SYS_STRING_SAVEDIR);
+	retval->save_dir_edit_offset = 0;
+	retval->sys_strings_segment = find_unique_seg_by_type(&retval->seg_manager, MEM_OBJ_SYS_STRINGS);
+	retval->sys_strings = &(((mem_obj_t *)(GET_SEGMENT(retval->seg_manager, retval->sys_strings_segment, MEM_OBJ_SYS_STRINGS)))->data.sys_strings);
+	sys_strings_restore(retval->sys_strings, s->sys_strings);
+
+	/* Time state: */
+	sci_get_current_time(&(retval->last_wait_time));
+	retval->game_start_time.tv_sec = time(NULL) - retval->game_time;
+	retval->game_start_time.tv_usec = 0;
+
+	/* File IO state: */
+	retval->file_handles_nr = 2;
+	retval->file_handles = (FILE **)sci_calloc(2, sizeof(FILE *));
+
+	/* static parser information: */
+	retval->parser_rules = s->parser_rules;
+	retval->parser_words_nr = s->parser_words_nr;
+	retval->parser_words = s->parser_words;
+	retval->parser_suffices_nr = s->parser_suffices_nr;
+	retval->parser_suffices = s->parser_suffices;
+	retval->parser_branches_nr = s->parser_branches_nr;
+	retval->parser_branches = s->parser_branches;
+
+	/* static VM/Kernel information: */
+	retval->selector_names_nr = s->selector_names_nr;
+	retval->selector_names = s->selector_names;
+	retval->kernel_names_nr = s->kernel_names_nr;
+	retval->kernel_names = s->kernel_names;
+	retval->kfunct_table = s->kfunct_table;
+	retval->kfunct_nr = s->kfunct_nr;
+	retval->opcodes = s->opcodes;
+
+	memcpy(&(retval->selector_map), &(s->selector_map), sizeof(selector_map_t));
+
+	retval->max_version = retval->version;
+	retval->min_version = retval->version;
+	retval->parser_base = make_reg(s->sys_strings_segment, SYS_STRING_PARSER_BASE);
+
+	/* Copy breakpoint information from current game instance */
+	retval->have_bp = s->have_bp;
+	retval->bp_list = s->bp_list;
+
+	retval->debug_mode = s->debug_mode;
+
+	retval->resource_dir = s->resource_dir;
+	retval->work_dir = s->work_dir;
+	retval->kernel_opt_flags = 0;
+	retval->have_mouse_flag = s->have_mouse_flag;
+
+	retval->successor = NULL;
+	retval->pic_priority_table = (int*)gfxop_get_pic_metainfo(retval->gfx_state);
+	retval->game_name = sci_strdup(obj_get_name(retval, retval->game_obj));
+
+	retval->sound.it = NULL;
+	retval->sound.flags = s->sound.flags;
+	retval->sound.song = NULL;
+	retval->sound.suspended = s->sound.suspended;
+	retval->sound.debug = s->sound.debug;
+	reconstruct_sounds(retval);
+
+	chdir ("..");
+
+	return retval;
+}


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list