Adopt use of gnulib for portability.
[pspp-builds.git] / src / matrix-data.c
index b47112a1c451eef4d55b0a55c00f1a951c17854e..e90b2ac68e8ea818ca2f4d4c9384a9c1fc44f83b 100644 (file)
 
    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., 59 Temple Place - Suite 330, Boston, MA
-   02111-1307, USA. */
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA. */
 
 #include <config.h>
-#include <assert.h>
+#include "error.h"
 #include <stdlib.h>
 #include <ctype.h>
 #include <float.h>
 #include "algorithm.h"
 #include "alloc.h"
+#include "case.h"
 #include "command.h"
 #include "data-in.h"
-#include "dfm.h"
+#include "dfm-read.h"
+#include "dictionary.h"
 #include "error.h"
 #include "file-handle.h"
 #include "lexer.h"
 #include "var.h"
 #include "vfm.h"
 
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+
 #include "debug-print.h"
 
 /* FIXME: /N subcommand not implemented.  It should be pretty simple,
    too. */
 
+/* Different types of variables for MATRIX DATA procedure.  Order is
+   important: these are used for sort keys. */
+enum
+  {
+    MXD_SPLIT,                 /* SPLIT FILE variables. */
+    MXD_ROWTYPE,               /* ROWTYPE_. */
+    MXD_FACTOR,                        /* Factor variables. */
+    MXD_VARNAME,               /* VARNAME_. */
+    MXD_CONTINUOUS,            /* Continuous variables. */
+
+    MXD_COUNT
+  };
+
 /* Format type enums. */
 enum format_type
   {
@@ -101,7 +119,7 @@ static const char *content_names[PROX + 1] =
 struct matrix_data_pgm 
   {
     struct pool *container;     /* Arena used for all allocations. */
-    struct file_handle *data_file; /* The data file to be read. */
+    struct dfm_reader *reader;  /* Data file to read. */
 
     /* Format. */
     enum format_type fmt;      /* LIST or FREE. */
@@ -126,42 +144,44 @@ struct matrix_data_pgm
     int contents[EOC * 3 + 1];  /* Contents. */
     int n_contents;             /* Number of entries. */
 
-    
+    /* Continuous variables. */
     int n_continuous;           /* Number of continuous variables. */
     int first_continuous;       /* Index into default_dict.var of
                                    first continuous variable. */
   };
 
+/* Auxiliary data attached to MATRIX DATA variables. */
+struct mxd_var 
+  {
+    int var_type;              /* Variable type. */
+    int sub_type;              /* Subtype. */
+  };
+
 static const struct case_source_class matrix_data_with_rowtype_source_class;
 static const struct case_source_class matrix_data_without_rowtype_source_class;
 
-static int compare_variables_by_mxd_vartype (const void *pa,
+static int compare_variables_by_mxd_var_type (const void *pa,
                                             const void *pb);
 static void read_matrices_without_rowtype (struct matrix_data_pgm *);
 static void read_matrices_with_rowtype (struct matrix_data_pgm *);
 static int string_to_content_type (char *, int *);
-
-#if DEBUGGING
-static void debug_print (void);
-#endif
+static void attach_mxd_aux (struct variable *, int var_type, int sub_type);
 
 int
 cmd_matrix_data (void)
 {
   struct pool *pool;
   struct matrix_data_pgm *mx;
-  
+  struct file_handle *fh = NULL;
+    
   unsigned seen = 0;
   
-  lex_match_id ("MATRIX");
-  lex_match_id ("DATA");
-
   discard_variables ();
 
   pool = pool_create ();
   mx = pool_alloc (pool, sizeof *mx);
   mx->container = pool;
-  mx->data_file = inline_file;
+  mx->reader = NULL;
   mx->fmt = LIST;
   mx->section = LOWER;
   mx->diag = DIAGONAL;
@@ -201,7 +221,7 @@ cmd_matrix_data (void)
            int i;
 
            for (i = 0; i < nv; i++)
-             if (!strcmp (v[i], "VARNAME_"))
+             if (!strcasecmp (v[i], "VARNAME_"))
                {
                  msg (SE, _("VARNAME_ cannot be explicitly specified on "
                             "VARIABLES."));
@@ -219,12 +239,11 @@ cmd_matrix_data (void)
              {
                struct variable *new_var;
                
-               if (strcmp (v[i], "ROWTYPE_"))
+               if (strcasecmp (v[i], "ROWTYPE_"))
                  {
                    new_var = dict_create_var_assert (default_dict, v[i], 0);
-                   new_var->p.mxd.vartype = MXD_CONTINUOUS;
-                   new_var->p.mxd.subtype = i;
-                 }
+                    attach_mxd_aux (new_var, MXD_CONTINUOUS, i);
+                  }
                else
                  mx->explicit_rowtype = 1;
                free (v[i]);
@@ -232,18 +251,15 @@ cmd_matrix_data (void)
            free (v);
          }
          
-         {
-           mx->rowtype_ = dict_create_var_assert (default_dict,
-                                                   "ROWTYPE_", 8);
-           mx->rowtype_->p.mxd.vartype = MXD_ROWTYPE;
-           mx->rowtype_->p.mxd.subtype = 0;
-         }
+          mx->rowtype_ = dict_create_var_assert (default_dict,
+                                                 "ROWTYPE_", 8);
+          attach_mxd_aux (mx->rowtype_, MXD_ROWTYPE, 0);
        }
       else if (lex_match_id ("FILE"))
        {
          lex_match ('=');
-         mx->data_file = fh_parse_file_handle ();
-         if (mx->data_file == NULL)
+         fh = fh_parse ();
+         if (fh == NULL)
            goto lossage;
        }
       else if (lex_match_id ("FORMAT"))
@@ -293,7 +309,8 @@ cmd_matrix_data (void)
          if (dict_lookup_var (default_dict, tokid) == NULL
              && (lex_look_ahead () == '.' || lex_look_ahead () == '/'))
            {
-             if (!strcmp (tokid, "ROWTYPE_") || !strcmp (tokid, "VARNAME_"))
+             if (!strcasecmp (tokid, "ROWTYPE_")
+                  || !strcasecmp (tokid, "VARNAME_"))
                {
                  msg (SE, _("Split variable may not be named ROWTYPE_ "
                             "or VARNAME_."));
@@ -302,10 +319,9 @@ cmd_matrix_data (void)
 
              mx->single_split = dict_create_var_assert (default_dict,
                                                          tokid, 0);
+              attach_mxd_aux (mx->single_split, MXD_CONTINUOUS, 0);
              lex_get ();
 
-             mx->single_split->p.mxd.vartype = MXD_CONTINUOUS;
-
               dict_set_split_vars (default_dict, &mx->single_split, 1);
            }
          else
@@ -326,14 +342,16 @@ cmd_matrix_data (void)
 
             for (i = 0; i < split_cnt; i++)
               {
-               if (split[i]->p.mxd.vartype != MXD_CONTINUOUS)
+                struct mxd_var *mv = split[i]->aux;
+                assert (mv != NULL);
+               if (mv->var_type != MXD_CONTINUOUS)
                  {
                    msg (SE, _("Split variable %s is already another type."),
                         tokid);
                    goto lossage;
                  }
-               split[i]->p.mxd.vartype = MXD_SPLIT;
-               split[i]->p.mxd.subtype = i;
+                var_clear_aux (split[i]);
+                attach_mxd_aux (split[i], MXD_SPLIT, i);
               }
          }
        }
@@ -356,14 +374,17 @@ cmd_matrix_data (void)
            
            for (i = 0; i < mx->n_factors; i++)
              {
-               if (mx->factors[i]->p.mxd.vartype != MXD_CONTINUOUS)
+                struct variable *v = mx->factors[i];
+                struct mxd_var *mv = v->aux;
+                assert (mv != NULL);
+               if (mv->var_type != MXD_CONTINUOUS)
                  {
                    msg (SE, _("Factor variable %s is already another type."),
                         tokid);
                    goto lossage;
                  }
-               mx->factors[i]->p.mxd.vartype = MXD_FACTOR;
-               mx->factors[i]->p.mxd.subtype = i;
+                var_clear_aux (v);
+                attach_mxd_aux (v, MXD_FACTOR, i);
              }
          }
        }
@@ -377,7 +398,7 @@ cmd_matrix_data (void)
              goto lossage;
            }
 
-         if (!lex_integer_p () || lex_integer () < 1)
+         if (!lex_is_integer () || lex_integer () < 1)
            {
              lex_error (_("expecting positive integer"));
              goto lossage;
@@ -396,7 +417,7 @@ cmd_matrix_data (void)
              goto lossage;
            }
 
-         if (!lex_integer_p () || lex_integer () < 1)
+         if (!lex_is_integer () || lex_integer () < 1)
            {
              lex_error (_("expecting positive integer"));
              goto lossage;
@@ -543,11 +564,8 @@ cmd_matrix_data (void)
     }
       
   /* Create VARNAME_. */
-  {
-    mx->varname_ = dict_create_var_assert (default_dict, "VARNAME_", 8);
-    mx->varname_->p.mxd.vartype = MXD_VARNAME;
-    mx->varname_->p.mxd.subtype = 0;
-  }
+  mx->varname_ = dict_create_var_assert (default_dict, "VARNAME_", 8);
+  attach_mxd_aux (mx->varname_, MXD_VARNAME, 0);
   
   /* Sort the dictionary variables into the desired order for the
      system file output. */
@@ -556,7 +574,7 @@ cmd_matrix_data (void)
     size_t nv;
 
     dict_get_vars (default_dict, &v, &nv, 0);
-    qsort (v, nv, sizeof *v, compare_variables_by_mxd_vartype);
+    qsort (v, nv, sizeof *v, compare_variables_by_mxd_var_type);
     dict_reorder_vars (default_dict, v, nv);
     free (v);
   }
@@ -578,7 +596,8 @@ cmd_matrix_data (void)
     for (i = 0; i < dict_get_var_cnt (default_dict); i++)
       {
        struct variable *v = dict_get_var (default_dict, i);
-       int type = v->p.mxd.vartype;
+        struct mxd_var *mv = v->aux;
+       int type = mv->var_type;
        
        assert (type >= 0 && type < MXD_COUNT);
        v->print = v->write = fmt_tab[type];
@@ -596,15 +615,17 @@ cmd_matrix_data (void)
       goto lossage;
     }
 
-#if DEBUGGING
-  debug_print ();
-#endif
+  mx->reader = dfm_open_reader (fh);
+  if (mx->reader == NULL)
+    goto lossage;
 
   if (mx->explicit_rowtype)
     read_matrices_with_rowtype (mx);
   else
     read_matrices_without_rowtype (mx);
 
+  dfm_close_reader (mx->reader);
+
   pool_destroy (mx->container);
 
   return CMD_SUCCESS;
@@ -651,7 +672,7 @@ string_to_content_type (char *s, int *collide)
     };
 
   for (tp = tab; tp->value != -1; tp++)
-    if (!strcmp (s, tp->string))
+    if (!strcasecmp (s, tp->string))
       {
        if (collide)
          *collide = tp->collide;
@@ -661,121 +682,35 @@ string_to_content_type (char *s, int *collide)
   return -1;
 }
 
-/* Compare two variables using p.mxd.vartype and p.mxd.subtype
+/* Compare two variables using p.mxd.var_type and p.mxd.sub_type
    fields. */
 static int
-compare_variables_by_mxd_vartype (const void *a_, const void *b_)
+compare_variables_by_mxd_var_type (const void *a_, const void *b_)
 {
   struct variable *const *pa = a_;
   struct variable *const *pb = b_;
-  const struct matrix_data_proc *a = &(*pa)->p.mxd;
-  const struct matrix_data_proc *b = &(*pb)->p.mxd;
-
-  if (a->vartype != b->vartype)
-    return a->vartype > b->vartype ? 1 : -1;
+  const struct mxd_var *a = (*pa)->aux;
+  const struct mxd_var *b = (*pb)->aux;
+  
+  if (a->var_type != b->var_type)
+    return a->var_type > b->var_type ? 1 : -1;
   else
-    return a->subtype < b->subtype ? -1 : a->subtype > b->subtype;
+    return a->sub_type < b->sub_type ? -1 : a->sub_type > b->sub_type;
 }
 
-#if DEBUGGING
-/* Print out the command as input. */
+/* Attaches a struct mxd_var with the specific member values to
+   V. */
 static void
-debug_print (void)
+attach_mxd_aux (struct variable *v, int var_type, int sub_type) 
 {
-  printf ("MATRIX DATA\n\t/VARIABLES=");
+  struct mxd_var *mv;
   
-  {
-    int i;
-    
-    for (i = 0; i < default_dict.nvar; i++)
-      printf ("%s ", default_dict.var[i]->name);
-  }
-  printf ("\n");
-
-  printf ("\t/FORMAT=");
-  if (fmt == LIST)
-    printf ("LIST");
-  else if (fmt == FREE)
-    printf ("FREE");
-  else
-    assert (0);
-  if (section == LOWER)
-    printf (" LOWER");
-  else if (section == UPPER)
-    printf (" UPPER");
-  else if (section == FULL)
-    printf (" FULL");
-  else
-    assert (0);
-  if (diag == DIAGONAL)
-    printf (" DIAGONAL\n");
-  else if (diag == NODIAGONAL)
-    printf (" NODIAGONAL\n");
-  else
-    assert (0);
-
-  if (dict_get_split_cnt (default_dict) != 0)
-    {
-      int i;
-
-      printf ("\t/SPLIT=");
-      for (i = 0; i < dict_get_split_cnt (default_dict); i++)
-       printf ("%s ", dict_get_split_vars (default_dict)[i]->name);
-      if (single_split)
-       printf ("\t/* single split");
-      printf ("\n");
-    }
-  
-  if (n_factors)
-    {
-      int i;
-
-      printf ("\t/FACTORS=");
-      for (i = 0; i < n_factors; i++)
-       printf ("%s ", factors[i]->name);
-      printf ("\n");
-    }
-
-  if (cells != -1)
-    printf ("\t/CELLS=%d\n", cells);
-
-  if (mx->pop_n != -1)
-    printf ("\t/N=%d\n", mx->pop_n);
-
-  if (mx->n_contents)
-    {
-      int i;
-      int space = 0;
-      
-      printf ("\t/CONTENTS=");
-      for (i = 0; i < mx->n_contents; i++)
-       {
-         if (mx->contents[i] == LPAREN)
-           {
-             if (space)
-               printf (" ");
-             printf ("(");
-             space = 0;
-           }
-         else if (mx->contents[i] == RPAREN)
-           {
-             printf (")");
-             space = 1;
-           }
-         else 
-           {
-
-             assert (mx->contents[i] >= 0 && mx->contents[i] <= PROX);
-             if (space)
-               printf (" ");
-             printf ("%s", content_names[mx->contents[i]]);
-             space = 1;
-           }
-       }
-      printf ("\n");
-    }
+  assert (v->aux == NULL);
+  mv = xmalloc (sizeof *mv);
+  mv->var_type = var_type;
+  mv->sub_type = sub_type;
+  var_attach_aux (v, mv, var_dtor_free);
 }
-#endif /* DEBUGGING */
 \f
 /* Matrix tokenizer. */
 
@@ -786,6 +721,7 @@ enum matrix_token_type
     MSTR               /* String. */
   };
 
+/* A MATRIX DATA parsing token. */
 struct matrix_token
   {
     enum matrix_token_type type; 
@@ -794,10 +730,10 @@ struct matrix_token
     int length;          /* MSTR: tokstr length. */
   };
 
-static int mget_token (struct matrix_token *, struct file_handle *);
+static int mget_token (struct matrix_token *, struct dfm_reader *);
 
 #if DEBUGGING
-#define mget_token(TOKEN, HANDLE) mget_token_dump(TOKEN, HANDLE)
+#define mget_token(TOKEN, READER) mget_token_dump(TOKEN, READER)
 
 static void
 mdump_token (const struct matrix_token *token)
@@ -817,35 +753,47 @@ mdump_token (const struct matrix_token *token)
 }
 
 static int
-mget_token_dump (struct matrix_token *token, struct file_handle *data_file)
+mget_token_dump (struct matrix_token *token, struct dfm_reader *reader)
 {
-  int result = (mget_token) (token, data_file);
+  int result = (mget_token) (token, reader);
   mdump_token (token);
   return result;
 }
 #endif
 
-/* Return the current position in DATA_FILE. */
+/* Return the current position in READER. */
 static const char *
-context (struct file_handle *data_file)
+context (struct dfm_reader *reader)
 {
   static char buf[32];
-  int len;
-  char *p = dfm_get_record (data_file, &len);
-  
-  if (!p || !len)
-    strcpy (buf, "at end of line");
-  else
+
+  if (dfm_eof (reader))
+    strcpy (buf, "at end of file");
+  else 
     {
-      char *cp = buf;
-      int n_copy = min (10, len);
-      cp = stpcpy (buf, "before `");
-      while (n_copy && isspace ((unsigned char) *p))
-       p++, n_copy++;
-      while (n_copy && !isspace ((unsigned char) *p))
-       *cp++ = *p++, n_copy--;
-      *cp++ = '\'';
-      *cp = 0;
+      struct fixed_string line;
+      const char *sp;
+      
+      dfm_get_record (reader, &line);
+      sp = ls_c_str (&line);
+      while (sp < ls_end (&line) && isspace ((unsigned char) *sp))
+        sp++;
+      if (sp >= ls_end (&line))
+        strcpy (buf, "at end of line");
+      else
+        {
+          char *dp;
+          size_t copy_cnt = 0;
+
+          dp = stpcpy (buf, "before `");
+          while (sp < ls_end (&line) && !isspace ((unsigned char) *sp)
+                 && copy_cnt < 10) 
+            {
+              *dp++ = *sp++;
+              copy_cnt++; 
+            }
+          strcpy (dp, "'");
+        }
     }
   
   return buf;
@@ -853,70 +801,57 @@ context (struct file_handle *data_file)
 
 /* Is there at least one token left in the data file? */
 static int
-another_token (struct file_handle *data_file)
+another_token (struct dfm_reader *reader)
 {
-  char *cp, *ep;
-  int len;
-
   for (;;)
     {
-      cp = dfm_get_record (data_file, &len);
-      if (!cp)
-       return 0;
+      struct fixed_string line;
+      const char *cp;
+      
+      if (dfm_eof (reader))
+        return 0;
+      dfm_get_record (reader, &line);
 
-      ep = cp + len;
-      while (isspace ((unsigned char) *cp) && cp < ep)
+      cp = ls_c_str (&line);
+      while (isspace ((unsigned char) *cp) && cp < ls_end (&line))
        cp++;
 
-      if (cp < ep)
-       break;
+      if (cp < ls_end (&line)) 
+        {
+          dfm_forward_columns (reader, cp - ls_c_str (&line));
+          return 1;
+        }
 
-      dfm_fwd_record (data_file);
+      dfm_forward_record (reader);
     }
-  
-  dfm_set_record (data_file, cp);
-
-  return 1;
 }
 
-/* Parse a MATRIX DATA token from mx->data_file into TOKEN. */
+/* Parse a MATRIX DATA token from READER into TOKEN. */
 static int
-(mget_token) (struct matrix_token *token, struct file_handle *data_file)
+(mget_token) (struct matrix_token *token, struct dfm_reader *reader)
 {
-  char *cp, *ep;
-  int len;
+  struct fixed_string line;
   int first_column;
-    
-  for (;;)
-    {
-      cp = dfm_get_record (data_file, &len);
-      if (!cp)
-        return 0;
-
-      ep = cp + len;
-      while (isspace ((unsigned char) *cp) && cp < ep)
-       cp++;
+  char *cp;
 
-      if (cp < ep)
-       break;
+  if (!another_token (reader))
+    return 0;
 
-      dfm_fwd_record (data_file);
-    }
-  
-  dfm_set_record (data_file, cp);
-  first_column = dfm_get_cur_col (data_file) + 1;
+  dfm_get_record (reader, &line);
+  first_column = dfm_column_start (reader);
 
   /* Three types of fields: quoted with ', quoted with ", unquoted. */
+  cp = ls_c_str (&line);
   if (*cp == '\'' || *cp == '"')
     {
       int quote = *cp;
 
       token->type = MSTR;
       token->string = ++cp;
-      while (cp < ep && *cp != quote)
+      while (cp < ls_end (&line) && *cp != quote)
        cp++;
       token->length = cp - token->string;
-      if (cp < ep)
+      if (cp < ls_end (&line))
        cp++;
       else
        msg (SW, _("Scope of string exceeds line."));
@@ -926,7 +861,8 @@ static int
       int is_num = isdigit ((unsigned char) *cp) || *cp == '.';
 
       token->string = cp++;
-      while (cp < ep && !isspace ((unsigned char) *cp) && *cp != ','
+      while (cp < ls_end (&line)
+             && !isspace ((unsigned char) *cp) && *cp != ','
             && *cp != '-' && *cp != '+')
        {
          if (isdigit ((unsigned char) *cp))
@@ -951,9 +887,7 @@ static int
          di.e = token->string + token->length;
          di.v = (union value *) &token->number;
          di.f1 = first_column;
-         di.format.type = FMT_F;
-         di.format.w = token->length;
-         di.format.d = 0;
+         di.format = make_output_format (FMT_F, token->length, 0);
 
          if (!data_in (&di))
            return 0;
@@ -962,56 +896,56 @@ static int
        token->type = MSTR;
     }
 
-  dfm_set_record (data_file, cp);
+  dfm_forward_columns (reader, cp - ls_c_str (&line));
     
   return 1;
 }
 
 /* Forcibly skip the end of a line for content type CONTENT in
-   DATA_FILE. */
+   READER. */
 static int
-force_eol (struct file_handle *data_file, const char *content)
+force_eol (struct dfm_reader *reader, const char *content)
 {
-  char *cp;
-  int len;
-  
-  cp = dfm_get_record (data_file, &len);
-  if (!cp)
+  struct fixed_string line;
+  const char *cp;
+
+  if (dfm_eof (reader))
     return 0;
-  while (len && isspace (*cp))
-    cp++, len--;
+  dfm_get_record (reader, &line);
+
+  cp = ls_c_str (&line);
+  while (isspace ((unsigned char) *cp) && cp < ls_end (&line))
+    cp++;
   
-  if (len)
+  if (cp < ls_end (&line))
     {
       msg (SE, _("End of line expected %s while reading %s."),
-          context (data_file), content);
+          context (reader), content);
       return 0;
     }
   
-  dfm_fwd_record (data_file);
-  
+  dfm_forward_record (reader);
   return 1;
 }
 \f
 /* Back end, omitting ROWTYPE_. */
 
-/* MATRIX DATA data. */
-static double ***nr_data;
-
-/* Factor values. */
-static double *nr_factor_values;
-
-/* Largest-numbered cell that we have read in thus far, plus one. */
-static int max_cell_index;
-
-/* SPLIT FILE variable values. */
-static double *split_values;
+struct nr_aux_data 
+  {
+    struct matrix_data_pgm *mx; /* MATRIX DATA program. */
+    double ***data;             /* MATRIX DATA data. */
+    double *factor_values;      /* Factor values. */
+    int max_cell_idx;           /* Max-numbered cell that we have
+                                   read so far, plus one. */
+    double *split_values;       /* SPLIT FILE variable values. */
+  };
 
-static int nr_read_splits (struct matrix_data_pgm *, int compare);
-static int nr_read_factors (struct matrix_data_pgm *, int cell);
-static void nr_output_data (struct matrix_data_pgm *,
+static int nr_read_splits (struct nr_aux_data *, int compare);
+static int nr_read_factors (struct nr_aux_data *, int cell);
+static void nr_output_data (struct nr_aux_data *, struct ccase *,
                             write_case_func *, write_case_data);
 static void matrix_data_read_without_rowtype (struct case_source *source,
+                                              struct ccase *,
                                               write_case_func *,
                                               write_case_data);
 
@@ -1019,23 +953,24 @@ static void matrix_data_read_without_rowtype (struct case_source *source,
 static void
 read_matrices_without_rowtype (struct matrix_data_pgm *mx)
 {
+  struct nr_aux_data nr;
+  
   if (mx->cells == -1)
     mx->cells = 1;
-  
-  split_values = xmalloc (sizeof *split_values
-                          * dict_get_split_cnt (default_dict));
-  nr_factor_values = xmalloc (sizeof *nr_factor_values * mx->n_factors * mx->cells);
-  max_cell_index = 0;
 
-  vfm_source = create_case_source (&matrix_data_without_rowtype_source_class,
-                                   mx);
-  
-  procedure (NULL, NULL, NULL, NULL);
+  nr.mx = mx;
+  nr.data = NULL;
+  nr.factor_values = xmalloc (sizeof *nr.factor_values * mx->n_factors * mx->cells);
+  nr.max_cell_idx = 0;
+  nr.split_values = xmalloc (sizeof *nr.split_values
+                             * dict_get_split_cnt (default_dict));
 
-  free (split_values);
-  free (nr_factor_values);
+  vfm_source = create_case_source (&matrix_data_without_rowtype_source_class, &nr);
+  
+  procedure (NULL, NULL);
 
-  fh_close_handle (mx->data_file);
+  free (nr.split_values);
+  free (nr.factor_values);
 }
 
 /* Mirror data across the diagonal of matrix CP which contains
@@ -1091,20 +1026,13 @@ fill_matrix (struct matrix_data_pgm *mx, int content, double *cp)
    If PER_FACTOR is nonzero, then factor information is read from
    the data file.  Data is for cell number CELL. */
 static int
-nr_read_data_lines (struct matrix_data_pgm *mx,
+nr_read_data_lines (struct nr_aux_data *nr,
                     int per_factor, int cell, int content, int compare)
 {
-  /* Content type. */
-  const int type = content_type[content];
-  
-  /* Number of lines that must be parsed from the data file for this
-     content type. */
-  int n_lines;
-  
-  /* Current position in vector or matrix. */
-  double *cp;
-
-  /* Counter. */
+  struct matrix_data_pgm *mx = nr->mx;
+  const int type = content_type[content];               /* Content type. */
+  int n_lines; /* Number of lines to parse from data file for this type. */
+  double *cp;                   /* Current position in vector or matrix. */
   int i;
 
   if (type != 1)
@@ -1116,7 +1044,7 @@ nr_read_data_lines (struct matrix_data_pgm *mx,
        n_lines--;
     }
 
-  cp = nr_data[content][cell];
+  cp = nr->data[content][cell];
   if (type == 1 && mx->section == LOWER && mx->diag == NODIAGONAL)
     cp += mx->n_continuous;
 
@@ -1124,9 +1052,9 @@ nr_read_data_lines (struct matrix_data_pgm *mx,
     {
       int n_cols;
       
-      if (!nr_read_splits (mx, 1))
+      if (!nr_read_splits (nr, 1))
        return 0;
-      if (per_factor && !nr_read_factors (mx, cell))
+      if (per_factor && !nr_read_factors (nr, cell))
        return 0;
       compare = 1;
 
@@ -1155,6 +1083,7 @@ nr_read_data_lines (struct matrix_data_pgm *mx,
              break;
            default:
              assert (0);
+              abort ();
            }
          break;
        case 2:
@@ -1162,6 +1091,7 @@ nr_read_data_lines (struct matrix_data_pgm *mx,
          break;
        default:
          assert (0);
+          abort ();
        }
 
       {
@@ -1170,20 +1100,20 @@ nr_read_data_lines (struct matrix_data_pgm *mx,
        for (j = 0; j < n_cols; j++)
          {
             struct matrix_token token;
-           if (!mget_token (&token, mx->data_file))
+           if (!mget_token (&token, mx->reader))
              return 0;
            if (token.type != MNUM)
              {
                msg (SE, _("expecting value for %s %s"),
                     dict_get_var (default_dict, j)->name,
-                     context (mx->data_file));
+                     context (mx->reader));
                return 0;
              }
 
            *cp++ = token.number;
          }
        if (mx->fmt != FREE
-            && !force_eol (mx->data_file, content_names[content]))
+            && !force_eol (mx->reader, content_names[content]))
          return 0;
        debug_printf (("\n"));
       }
@@ -1192,7 +1122,7 @@ nr_read_data_lines (struct matrix_data_pgm *mx,
        cp += mx->n_continuous - n_cols;
     }
 
-  fill_matrix (mx, content, nr_data[content][cell]);
+  fill_matrix (mx, content, nr->data[content][cell]);
 
   return 1;
 }
@@ -1201,21 +1131,23 @@ nr_read_data_lines (struct matrix_data_pgm *mx,
    writes them to the output file.  Returns success. */
 static void
 matrix_data_read_without_rowtype (struct case_source *source,
+                                  struct ccase *c,
                                   write_case_func *write_case,
                                   write_case_data wc_data)
 {
-  struct matrix_data_pgm *mx = source->aux;
+  struct nr_aux_data *nr = source->aux;
+  struct matrix_data_pgm *mx = nr->mx;
 
   {
     int *cp;
 
-    nr_data = pool_alloc (mx->container, (PROX + 1) * sizeof *nr_data);
+    nr->data = pool_alloc (mx->container, (PROX + 1) * sizeof *nr->data);
     
     {
       int i;
 
       for (i = 0; i <= PROX; i++)
-       nr_data[i] = NULL;
+       nr->data[i] = NULL;
     }
     
     for (cp = mx->contents; *cp != EOC; cp++)
@@ -1232,12 +1164,12 @@ matrix_data_read_without_rowtype (struct case_source *source,
            int n_vectors = per_factor ? mx->cells : 1;
            int i;
            
-           nr_data[*cp] = pool_alloc (mx->container,
-                                      n_vectors * sizeof **nr_data);
+           nr->data[*cp] = pool_alloc (mx->container,
+                                      n_vectors * sizeof **nr->data);
            
            for (i = 0; i < n_vectors; i++)
-             nr_data[*cp][i] = pool_alloc (mx->container,
-                                           n_entries * sizeof ***nr_data);
+             nr->data[*cp][i] = pool_alloc (mx->container,
+                                           n_entries * sizeof ***nr->data);
          }
        }
   }
@@ -1246,7 +1178,7 @@ matrix_data_read_without_rowtype (struct case_source *source,
     {
       int *bp, *ep, *np;
       
-      if (!nr_read_splits (mx, 0))
+      if (!nr_read_splits (nr, 0))
        return;
       
       for (bp = mx->contents; *bp != EOC; bp = np)
@@ -1281,16 +1213,16 @@ matrix_data_read_without_rowtype (struct case_source *source,
                int *cp;
 
                for (cp = bp; cp < ep; cp++) 
-                 if (!nr_read_data_lines (mx, per_factor, i, *cp, cp != bp))
+                 if (!nr_read_data_lines (nr, per_factor, i, *cp, cp != bp))
                    return;
              }
          }
        }
 
-      nr_output_data (mx, write_case, wc_data);
+      nr_output_data (nr, c, write_case, wc_data);
 
       if (dict_get_split_cnt (default_dict) == 0
-          || !another_token (mx->data_file))
+          || !another_token (mx->reader))
        return;
     }
 }
@@ -1299,9 +1231,10 @@ matrix_data_read_without_rowtype (struct case_source *source,
    values read to the last values read and returns 1 if they're equal,
    0 otherwise. */
 static int
-nr_read_splits (struct matrix_data_pgm *mx, int compare)
+nr_read_splits (struct nr_aux_data *nr, int compare)
 {
-  static int just_read = 0;
+  struct matrix_data_pgm *mx = nr->mx;
+  static int just_read = 0; /* FIXME: WTF? */
   size_t split_cnt;
   size_t i;
 
@@ -1316,9 +1249,11 @@ nr_read_splits (struct matrix_data_pgm *mx, int compare)
 
   if (mx->single_split)
     {
-      if (!compare)
-       split_values[0]
-          = ++dict_get_split_vars (default_dict)[0]->p.mxd.subtype;
+      if (!compare) 
+        {
+          struct mxd_var *mv = dict_get_split_vars (default_dict)[0]->aux;
+          nr->split_values[0] = ++mv->sub_type; 
+        }
       return 1;
     }
 
@@ -1329,21 +1264,22 @@ nr_read_splits (struct matrix_data_pgm *mx, int compare)
   for (i = 0; i < split_cnt; i++) 
     {
       struct matrix_token token;
-      if (!mget_token (&token, mx->data_file))
+      if (!mget_token (&token, mx->reader))
         return 0;
       if (token.type != MNUM)
         {
           msg (SE, _("Syntax error expecting SPLIT FILE value %s."),
-               context (mx->data_file));
+               context (mx->reader));
           return 0;
         }
 
       if (!compare)
-        split_values[i] = token.number;
-      else if (split_values[i] != token.number)
+        nr->split_values[i] = token.number;
+      else if (nr->split_values[i] != token.number)
         {
           msg (SE, _("Expecting value %g for %s."),
-               split_values[i], dict_get_split_vars (default_dict)[i]->name);
+               nr->split_values[i],
+               dict_get_split_vars (default_dict)[i]->name);
           return 0;
         }
     }
@@ -1355,20 +1291,21 @@ nr_read_splits (struct matrix_data_pgm *mx, int compare)
    values read to the last values read and returns 1 if they're equal,
    0 otherwise. */
 static int
-nr_read_factors (struct matrix_data_pgm *mx, int cell)
+nr_read_factors (struct nr_aux_data *nr, int cell)
 {
+  struct matrix_data_pgm *mx = nr->mx;
   int compare;
   
   if (mx->n_factors == 0)
     return 1;
 
-  assert (max_cell_index >= cell);
-  if (cell != max_cell_index)
+  assert (nr->max_cell_idx >= cell);
+  if (cell != nr->max_cell_idx)
     compare = 1;
   else
     {
       compare = 0;
-      max_cell_index++;
+      nr->max_cell_idx++;
     }
       
   {
@@ -1377,22 +1314,22 @@ nr_read_factors (struct matrix_data_pgm *mx, int cell)
     for (i = 0; i < mx->n_factors; i++)
       {
         struct matrix_token token;
-       if (!mget_token (&token, mx->data_file))
+       if (!mget_token (&token, mx->reader))
          return 0;
        if (token.type != MNUM)
          {
            msg (SE, _("Syntax error expecting factor value %s."),
-                context (mx->data_file));
+                context (mx->reader));
            return 0;
          }
        
        if (!compare)
-         nr_factor_values[i + mx->n_factors * cell] = token.number;
-       else if (nr_factor_values[i + mx->n_factors * cell] != token.number)
+         nr->factor_values[i + mx->n_factors * cell] = token.number;
+       else if (nr->factor_values[i + mx->n_factors * cell] != token.number)
          {
            msg (SE, _("Syntax error expecting value %g for %s %s."),
-                nr_factor_values[i + mx->n_factors * cell],
-                mx->factors[i]->name, context (mx->data_file));
+                nr->factor_values[i + mx->n_factors * cell],
+                mx->factors[i]->name, context (mx->reader));
            return 0;
          }
       }
@@ -1405,16 +1342,17 @@ nr_read_factors (struct matrix_data_pgm *mx, int cell)
    CP to the active file. */
 static void
 dump_cell_content (struct matrix_data_pgm *mx, int content, double *cp,
+                   struct ccase *c,
                    write_case_func *write_case, write_case_data wc_data)
 {
   int type = content_type[content];
 
   {
-    st_bare_pad_copy (temp_case->data[mx->rowtype_->fv].s,
-                     content_names[content], 8);
+    buf_copy_str_rpad (case_data_rw (c, mx->rowtype_->fv)->s, 8,
+                       content_names[content]);
     
     if (type != 1)
-      memset (&temp_case->data[mx->varname_->fv].s, ' ', 8);
+      memset (case_data_rw (c, mx->varname_->fv)->s, ' ', 8);
   }
 
   {
@@ -1428,14 +1366,13 @@ dump_cell_content (struct matrix_data_pgm *mx, int content, double *cp,
        for (j = 0; j < mx->n_continuous; j++)
          {
             int fv = dict_get_var (default_dict, mx->first_continuous + j)->fv;
-           temp_case->data[fv].f = *cp;
+            case_data_rw (c, fv)->f = *cp;
            cp++;
          }
        if (type == 1)
-         st_bare_pad_copy (temp_case->data[mx->varname_->fv].s,
-                            dict_get_var (default_dict,
-                                          mx->first_continuous + i)->name,
-                           8);
+         buf_copy_str_rpad (case_data_rw (c, mx->varname_->fv)->s, 8,
+                             dict_get_var (default_dict,
+                                           mx->first_continuous + i)->name);
        write_case (wc_data);
       }
   }
@@ -1443,9 +1380,11 @@ dump_cell_content (struct matrix_data_pgm *mx, int content, double *cp,
 
 /* Finally dump out everything from nr_data[] to the output file. */
 static void
-nr_output_data (struct matrix_data_pgm *mx,
+nr_output_data (struct nr_aux_data *nr, struct ccase *c,
                 write_case_func *write_case, write_case_data wc_data)
 {
+  struct matrix_data_pgm *mx = nr->mx;
+  
   {
     struct variable *const *split;
     size_t split_cnt;
@@ -1454,7 +1393,7 @@ nr_output_data (struct matrix_data_pgm *mx,
     split_cnt = dict_get_split_cnt (default_dict);
     split = dict_get_split_vars (default_dict);
     for (i = 0; i < split_cnt; i++)
-      temp_case->data[split[i]->fv].f = split_values[i];
+      case_data_rw (c, split[i]->fv)->f = nr->split_values[i];
   }
 
   if (mx->n_factors)
@@ -1468,8 +1407,8 @@ nr_output_data (struct matrix_data_pgm *mx,
 
            for (factor = 0; factor < mx->n_factors; factor++)
              {
-               temp_case->data[mx->factors[factor]->fv].f
-                 = nr_factor_values[factor + cell * mx->n_factors];
+               case_data_rw (c, mx->factors[factor]->fv)->f
+                 = nr->factor_values[factor + cell * mx->n_factors];
                debug_printf (("f:%s ", mx->factors[factor]->name));
              }
          }
@@ -1480,11 +1419,11 @@ nr_output_data (struct matrix_data_pgm *mx,
            for (content = 0; content <= PROX; content++)
              if (mx->is_per_factor[content])
                {
-                 assert (nr_data[content] != NULL
-                         && nr_data[content][cell] != NULL);
+                 assert (nr->data[content] != NULL
+                         && nr->data[content][cell] != NULL);
 
-                 dump_cell_content (mx, content, nr_data[content][cell],
-                                     write_case, wc_data);
+                 dump_cell_content (mx, content, nr->data[content][cell],
+                                     c, write_case, wc_data);
                }
          }
        }
@@ -1497,21 +1436,18 @@ nr_output_data (struct matrix_data_pgm *mx,
       int factor;
 
       for (factor = 0; factor < mx->n_factors; factor++)
-       temp_case->data[mx->factors[factor]->fv].f = SYSMIS;
+       case_data_rw (c, mx->factors[factor]->fv)->f = SYSMIS;
     }
     
     for (content = 0; content <= PROX; content++)
-      if (!mx->is_per_factor[content] && nr_data[content] != NULL)
-       dump_cell_content (mx, content, nr_data[content][0],
-                           write_case, wc_data);
+      if (!mx->is_per_factor[content] && nr->data[content] != NULL)
+       dump_cell_content (mx, content, nr->data[content][0],
+                           c, write_case, wc_data);
   }
 }
 \f
 /* Back end, with ROWTYPE_. */
 
-/* Type of current row. */
-static int wr_content;
-
 /* All the data for one set of factor values. */
 struct factor_data
   {
@@ -1521,19 +1457,26 @@ struct factor_data
     struct factor_data *next;
   };
 
-/* All the data, period. */
-struct factor_data *wr_data;
-
-/* Current factor. */
-struct factor_data *wr_current;
+/* With ROWTYPE_ auxiliary data. */
+struct wr_aux_data 
+  {
+    struct matrix_data_pgm *mx;         /* MATRIX DATA program. */
+    int content;                        /* Type of current row. */
+    double *split_values;               /* SPLIT FILE variable values. */
+    struct factor_data *data;           /* All the data. */
+    struct factor_data *current;        /* Current factor. */
+  };
 
-static int wr_read_splits (struct matrix_data_pgm *,
+static int wr_read_splits (struct wr_aux_data *, struct ccase *,
                            write_case_func *, write_case_data);
-static int wr_output_data (struct matrix_data_pgm *, write_case_func *, write_case_data);
-static int wr_read_rowtype (const struct matrix_token *, struct file_handle *);
-static int wr_read_factors (struct matrix_data_pgm *);
-static int wr_read_indeps (struct matrix_data_pgm *);
+static int wr_output_data (struct wr_aux_data *, struct ccase *,
+                           write_case_func *, write_case_data);
+static int wr_read_rowtype (struct wr_aux_data *, 
+                            const struct matrix_token *, struct dfm_reader *);
+static int wr_read_factors (struct wr_aux_data *);
+static int wr_read_indeps (struct wr_aux_data *);
 static void matrix_data_read_with_rowtype (struct case_source *,
+                                           struct ccase *,
                                            write_case_func *,
                                            write_case_data);
 
@@ -1542,48 +1485,56 @@ static void matrix_data_read_with_rowtype (struct case_source *,
 static void
 read_matrices_with_rowtype (struct matrix_data_pgm *mx)
 {
-  wr_data = wr_current = NULL;
-  split_values = NULL;
+  struct wr_aux_data wr;
+
+  wr.mx = mx;
+  wr.content = -1;
+  wr.split_values = NULL;
+  wr.data = NULL;
+  wr.current = NULL;
   mx->cells = 0;
 
-  vfm_source = create_case_source (&matrix_data_with_rowtype_source_class, mx);
-  
-  procedure (NULL, NULL, NULL, NULL);
+  vfm_source = create_case_source (&matrix_data_with_rowtype_source_class,
+                                   &wr);
+  procedure (NULL, NULL);
 
-  free (split_values);
-  fh_close_handle (mx->data_file);
+  free (wr.split_values);
 }
 
 /* Read from the data file and write it to the active file. */
 static void
 matrix_data_read_with_rowtype (struct case_source *source,
+                               struct ccase *c,
                                write_case_func *write_case,
                                write_case_data wc_data)
 {
-  struct matrix_data_pgm *mx = source->aux;
+  struct wr_aux_data *wr = source->aux;
+  struct matrix_data_pgm *mx = wr->mx;
 
   do
     {
-      if (!wr_read_splits (mx, write_case, wc_data))
+      if (!wr_read_splits (wr, c, write_case, wc_data))
        return;
 
-      if (!wr_read_factors (mx))
+      if (!wr_read_factors (wr))
        return;
 
-      if (!wr_read_indeps (mx))
+      if (!wr_read_indeps (wr))
        return;
     }
-  while (another_token (mx->data_file));
+  while (another_token (mx->reader));
 
-  wr_output_data (mx, write_case, wc_data);
+  wr_output_data (wr, c, write_case, wc_data);
 }
 
 /* Read the split file variables.  If they differ from the previous
    set of split variables then output the data.  Returns success. */
 static int 
-wr_read_splits (struct matrix_data_pgm *mx,
+wr_read_splits (struct wr_aux_data *wr,
+                struct ccase *c,
                 write_case_func *write_case, write_case_data wc_data)
 {
+  struct matrix_data_pgm *mx = wr->mx;
   int compare;
   size_t split_cnt;
 
@@ -1591,12 +1542,12 @@ wr_read_splits (struct matrix_data_pgm *mx,
   if (split_cnt == 0)
     return 1;
 
-  if (split_values)
+  if (wr->split_values)
     compare = 1;
   else
     {
       compare = 0;
-      split_values = xmalloc (split_cnt * sizeof *split_values);
+      wr->split_values = xmalloc (split_cnt * sizeof *wr->split_values);
     }
   
   {
@@ -1606,23 +1557,23 @@ wr_read_splits (struct matrix_data_pgm *mx,
     for (i = 0; i < split_cnt; i++)
       {
         struct matrix_token token;
-       if (!mget_token (&token, mx->data_file))
+       if (!mget_token (&token, mx->reader))
          return 0;
        if (token.type != MNUM)
          {
            msg (SE, _("Syntax error %s expecting SPLIT FILE value."),
-                context (mx->data_file));
+                context (mx->reader));
            return 0;
          }
 
-       if (compare && split_values[i] != token.number && !different)
+       if (compare && wr->split_values[i] != token.number && !different)
          {
-           if (!wr_output_data (mx, write_case, wc_data))
+           if (!wr_output_data (wr, c, write_case, wc_data))
              return 0;
            different = 1;
            mx->cells = 0;
          }
-       split_values[i] = token.number;
+       wr->split_values[i] = token.number;
       }
   }
 
@@ -1668,9 +1619,12 @@ compare_factors (const void *a_, const void *b_, void *mx_)
 /* Write out the data for the current split file to the active
    file. */
 static int 
-wr_output_data (struct matrix_data_pgm *mx,
+wr_output_data (struct wr_aux_data *wr,
+                struct ccase *c,
                 write_case_func *write_case, write_case_data wc_data)
 {
+  struct matrix_data_pgm *mx = wr->mx;
+
   {
     struct variable *const *split;
     size_t split_cnt;
@@ -1679,10 +1633,10 @@ wr_output_data (struct matrix_data_pgm *mx,
     split_cnt = dict_get_split_cnt (default_dict);
     split = dict_get_split_vars (default_dict);
     for (i = 0; i < split_cnt; i++)
-      temp_case->data[split[i]->fv].f = split_values[i];
+      case_data_rw (c, split[i]->fv)->f = wr->split_values[i];
   }
 
-  /* Sort the wr_data list. */
+  /* Sort the wr->data list. */
   {
     struct factor_data **factors;
     struct factor_data *iter;
@@ -1690,12 +1644,12 @@ wr_output_data (struct matrix_data_pgm *mx,
 
     factors = xmalloc (sizeof *factors * mx->cells);
 
-    for (i = 0, iter = wr_data; iter; iter = iter->next, i++)
+    for (i = 0, iter = wr->data; iter; iter = iter->next, i++)
       factors[i] = iter;
 
     sort (factors, mx->cells, sizeof *factors, compare_factors, mx);
 
-    wr_data = factors[0];
+    wr->data = factors[0];
     for (i = 0; i < mx->cells - 1; i++)
       factors[i]->next = factors[i + 1];
     factors[mx->cells - 1]->next = NULL;
@@ -1707,17 +1661,14 @@ wr_output_data (struct matrix_data_pgm *mx,
   {
     struct factor_data *iter;
     
-    for (iter = wr_data; iter; iter = iter->next)
+    for (iter = wr->data; iter; iter = iter->next)
       {
        {
          int factor;
 
          for (factor = 0; factor < mx->n_factors; factor++)
-           {
-             temp_case->data[mx->factors[factor]->fv].f
-               = iter->factors[factor];
-             debug_printf (("f:%s ", factors[factor]->name));
-           }
+            case_data_rw (c, mx->factors[factor]->fv)->f
+              = iter->factors[factor];
        }
        
        {
@@ -1749,7 +1700,7 @@ wr_output_data (struct matrix_data_pgm *mx,
              fill_matrix (mx, content, iter->data[content]);
 
              dump_cell_content (mx, content, iter->data[content],
-                                 write_case, wc_data);
+                                 c, write_case, wc_data);
            }
        }
       }
@@ -1758,26 +1709,27 @@ wr_output_data (struct matrix_data_pgm *mx,
   pool_destroy (mx->container);
   mx->container = pool_create ();
   
-  wr_data = wr_current = NULL;
+  wr->data = wr->current = NULL;
   
   return 1;
 }
 
-/* Sets ROWTYPE_ based on the given TOKEN read from DATA_FILE.
+/* Sets ROWTYPE_ based on the given TOKEN read from READER.
    Return success. */
 static int 
-wr_read_rowtype (const struct matrix_token *token,
-                 struct file_handle *data_file)
+wr_read_rowtype (struct wr_aux_data *wr,
+                 const struct matrix_token *token,
+                 struct dfm_reader *reader)
 {
-  if (wr_content != -1)
+  if (wr->content != -1)
     {
-      msg (SE, _("Multiply specified ROWTYPE_ %s."), context (data_file));
+      msg (SE, _("Multiply specified ROWTYPE_ %s."), context (reader));
       return 0;
     }
   if (token->type != MSTR)
     {
       msg (SE, _("Syntax error %s expecting ROWTYPE_ string."),
-           context (data_file));
+           context (reader));
       return 0;
     }
   
@@ -1791,12 +1743,12 @@ wr_read_rowtype (const struct matrix_token *token,
     for (cp = s; *cp; cp++)
       *cp = toupper ((unsigned char) *cp);
 
-    wr_content = string_to_content_type (s, NULL);
+    wr->content = string_to_content_type (s, NULL);
   }
 
-  if (wr_content == -1)
+  if (wr->content == -1)
     {
-      msg (SE, _("Syntax error %s."), context (data_file));
+      msg (SE, _("Syntax error %s."), context (reader));
       return 0;
     }
 
@@ -1806,53 +1758,54 @@ wr_read_rowtype (const struct matrix_token *token,
 /* Read the factors for the current row.  Select a set of factors and
    point wr_current to it. */
 static int 
-wr_read_factors (struct matrix_data_pgm *mx)
+wr_read_factors (struct wr_aux_data *wr)
 {
+  struct matrix_data_pgm *mx = wr->mx;
   double *factor_values = local_alloc (sizeof *factor_values * mx->n_factors);
 
-  wr_content = -1;
+  wr->content = -1;
   {
     int i;
   
     for (i = 0; i < mx->n_factors; i++)
       {
         struct matrix_token token;
-       if (!mget_token (&token, mx->data_file))
+       if (!mget_token (&token, mx->reader))
          goto lossage;
        if (token.type == MSTR)
          {
-           if (!wr_read_rowtype (&token, mx->data_file))
+           if (!wr_read_rowtype (wr, &token, mx->reader))
              goto lossage;
-           if (!mget_token (&token, mx->data_file))
+           if (!mget_token (&token, mx->reader))
              goto lossage;
          }
        if (token.type != MNUM)
          {
            msg (SE, _("Syntax error expecting factor value %s."),
-                context (mx->data_file));
+                context (mx->reader));
            goto lossage;
          }
        
        factor_values[i] = token.number;
       }
   }
-  if (wr_content == -1)
+  if (wr->content == -1)
     {
       struct matrix_token token;
-      if (!mget_token (&token, mx->data_file))
+      if (!mget_token (&token, mx->reader))
        goto lossage;
-      if (!wr_read_rowtype (&token, mx->data_file))
+      if (!wr_read_rowtype (wr, &token, mx->reader))
        goto lossage;
     }
   
   /* Try the most recent factor first as a simple caching
      mechanism. */
-  if (wr_current)
+  if (wr->current)
     {
       int i;
       
       for (i = 0; i < mx->n_factors; i++)
-       if (factor_values[i] != wr_current->factors[i])
+       if (factor_values[i] != wr->current->factors[i])
          goto cache_miss;
       goto winnage;
     }
@@ -1862,7 +1815,7 @@ cache_miss:
   {
     struct factor_data *iter;
 
-    for (iter = wr_data; iter; iter = iter->next)
+    for (iter = wr->data; iter; iter = iter->next)
       {
        int i;
 
@@ -1870,7 +1823,7 @@ cache_miss:
          if (factor_values[i] != iter->factors[i])
            goto next_item;
        
-       wr_current = iter;
+       wr->current = iter;
        goto winnage;
        
       next_item: ;
@@ -1900,8 +1853,8 @@ cache_miss:
        }
     }
 
-    new->next = wr_data;
-    wr_data = wr_current = new;
+    new->next = wr->data;
+    wr->data = wr->current = new;
     mx->cells++;
   }
 
@@ -1914,28 +1867,29 @@ lossage:
   return 0;
 }
 
-/* Read the independent variables into wr_current. */
+/* Read the independent variables into wr->current. */
 static int 
-wr_read_indeps (struct matrix_data_pgm *mx)
+wr_read_indeps (struct wr_aux_data *wr)
 {
-  struct factor_data *c = wr_current;
-  const int type = content_type[wr_content];
-  const int n_rows = c->n_rows[wr_content];
+  struct matrix_data_pgm *mx = wr->mx;
+  struct factor_data *c = wr->current;
+  const int type = content_type[wr->content];
+  const int n_rows = c->n_rows[wr->content];
   double *cp;
   int n_cols;
 
   /* Allocate room for data if necessary. */
-  if (c->data[wr_content] == NULL)
+  if (c->data[wr->content] == NULL)
     {
       int n_items = mx->n_continuous;
       if (type == 1)
        n_items *= mx->n_continuous;
       
-      c->data[wr_content] = pool_alloc (mx->container,
+      c->data[wr->content] = pool_alloc (mx->container,
                                        sizeof **c->data * n_items);
     }
 
-  cp = &c->data[wr_content][n_rows * mx->n_continuous];
+  cp = &c->data[wr->content][n_rows * mx->n_continuous];
 
   /* Figure out how much to read from this line. */
   switch (type)
@@ -1945,7 +1899,7 @@ wr_read_indeps (struct matrix_data_pgm *mx)
       if (n_rows > 0)
        {
          msg (SE, _("Duplicate specification for %s."),
-              content_names[wr_content]);
+              content_names[wr->content]);
          return 0;
        }
       if (type == 0)
@@ -1957,7 +1911,7 @@ wr_read_indeps (struct matrix_data_pgm *mx)
       if (n_rows >= mx->n_continuous - (mx->section != FULL && mx->diag == NODIAGONAL))
        {
          msg (SE, _("Too many rows of matrix data for %s."),
-              content_names[wr_content]);
+              content_names[wr->content]);
          return 0;
        }
       
@@ -1982,12 +1936,14 @@ wr_read_indeps (struct matrix_data_pgm *mx)
          break;
        default:
          assert (0);
+          abort ();
        }
       break;
     default:
       assert (0);
+      abort ();
     }
-  c->n_rows[wr_content]++;
+  c->n_rows[wr->content]++;
 
   debug_printf ((" (c=%p,r=%d,n=%d)", c, n_rows + 1, n_cols));
 
@@ -1998,20 +1954,20 @@ wr_read_indeps (struct matrix_data_pgm *mx)
     for (j = 0; j < n_cols; j++)
       {
         struct matrix_token token;
-       if (!mget_token (&token, mx->data_file))
+       if (!mget_token (&token, mx->reader))
          return 0;
        if (token.type != MNUM)
          {
            msg (SE, _("Syntax error expecting value for %s %s."),
                  dict_get_var (default_dict, mx->first_continuous + j)->name,
-                 context (mx->data_file));
+                 context (mx->reader));
            return 0;
          }
 
        *cp++ = token.number;
       }
     if (mx->fmt != FREE
-        && !force_eol (mx->data_file, content_names[wr_content]))
+        && !force_eol (mx->reader, content_names[wr->content]))
       return 0;
     debug_printf (("\n"));
   }
@@ -2037,3 +1993,4 @@ matrix_data_without_rowtype_source_class =
     matrix_data_read_without_rowtype,
     NULL,
   };
+