checkin of 0.3.0
[pspp-builds.git] / src / matrix-data.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, USA. */
19
20 /* AIX requires this to be the first thing in the file.  */
21 #include <config.h>
22 #if __GNUC__
23 #define alloca __builtin_alloca
24 #else
25 #if HAVE_ALLOCA_H
26 #include <alloca.h>
27 #else
28 #ifdef _AIX
29 #pragma alloca
30 #else
31 #ifndef alloca                  /* predefined by HP cc +Olibcalls */
32 char *alloca ();
33 #endif
34 #endif
35 #endif
36 #endif
37
38 #include <assert.h>
39 #include <stdlib.h>
40 #include <ctype.h>
41 #include <float.h>
42 #include "alloc.h"
43 #include "command.h"
44 #include "data-in.h"
45 #include "dfm.h"
46 #include "error.h"
47 #include "file-handle.h"
48 #include "lexer.h"
49 #include "misc.h"
50 #include "pool.h"
51 #include "str.h"
52 #include "var.h"
53 #include "vfm.h"
54
55 #undef DEBUGGING
56 /*#define DEBUGGING 1*/
57 #include "debug-print.h"
58
59 /* FIXME: /N subcommand not implemented.  It should be pretty simple,
60    too. */
61
62 /* Format type enums. */
63 enum
64   {
65     LIST,
66     FREE
67   };
68
69 /* Matrix section enums. */
70 enum
71   {
72     LOWER,
73     UPPER,
74     FULL
75   };
76
77 /* Diagonal inclusion enums. */
78 enum
79   {
80     DIAGONAL,
81     NODIAGONAL
82   };
83
84 /* CONTENTS types. */
85 enum
86   {
87     N_VECTOR,
88     N_SCALAR,
89     N_MATRIX,
90     MEAN,
91     STDDEV,
92     COUNT,
93     MSE,
94     DFE,
95     MAT,
96     COV,
97     CORR,
98     PROX,
99     
100     LPAREN,
101     RPAREN,
102     EOC
103   };
104
105 /* 0=vector, 1=matrix, 2=scalar. */
106 static int content_type[PROX + 1] = 
107   {
108     0, 2, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1,
109   };
110
111 /* Name of each content type. */
112 static const char *content_names[PROX + 1] =
113   {
114     "N", "N", "N_MATRIX", "MEAN", "STDDEV", "COUNT", "MSE",
115     "DFE", "MAT", "COV", "CORR", "PROX",
116   };
117
118 /* The data file to be read. */
119 static struct file_handle *data_file;
120
121 /* Format type. */
122 static int fmt;                 /* LIST or FREE. */
123 static int section;             /* LOWER or UPPER or FULL. */
124 static int diag;                /* DIAGONAL or NODIAGONAL. */
125
126 /* Arena used for all the MATRIX DATA allocations. */
127 static struct pool *container;
128
129 /* ROWTYPE_ specified explicitly in data? */
130 static int explicit_rowtype;
131
132 /* ROWTYPE_, VARNAME_ variables. */
133 static struct variable *rowtype_, *varname_;
134
135 /* Is is per-factor data? */
136 int is_per_factor[PROX + 1];
137
138 /* Single SPLIT FILE variable. */
139 static struct variable *single_split;
140
141 /* Factor variables.  */
142 static int n_factors;
143 static struct variable **factors;
144
145 /* Number of cells, or -1 if none. */
146 static int cells;
147
148 /* Population N specified by user. */
149 static int pop_n;
150
151 /* CONTENTS subcommand. */
152 static int contents[EOC * 3 + 1];
153 static int n_contents;
154
155 /* Number of continuous variables. */
156 static int n_continuous;
157
158 /* Index into default_dict.var of first continuous variables. */
159 static int first_continuous;
160
161 static int compare_variables_by_mxd_vartype (const void *pa,
162                                              const void *pb);
163 static void read_matrices_without_rowtype (void);
164 static void read_matrices_with_rowtype (void);
165 static int string_to_content_type (char *, int *);
166
167 #if DEBUGGING
168 static void debug_print (void);
169 #endif
170
171 int
172 cmd_matrix_data (void)
173 {
174   unsigned seen = 0;
175   
176   lex_match_id ("MATRIX");
177   lex_match_id ("DATA");
178
179   container = pool_create ();
180
181   discard_variables ();
182
183   data_file = inline_file;
184   fmt = LIST;
185   section = LOWER;
186   diag = DIAGONAL;
187   single_split = NULL;
188   n_factors = 0;
189   factors = NULL;
190   cells = -1;
191   pop_n = -1;
192   n_contents = 0;
193   while (token != '.')
194     {
195       lex_match ('/');
196
197       if (lex_match_id ("VARIABLES"))
198         {
199           char **v;
200           int nv;
201
202           if (seen & 1)
203             {
204               msg (SE, _("VARIABLES subcommand multiply specified."));
205               goto lossage;
206             }
207           seen |= 1;
208           
209           lex_match ('=');
210           if (!parse_DATA_LIST_vars (&v, &nv, PV_NO_DUPLICATE))
211             goto lossage;
212           
213           {
214             int i;
215
216             for (i = 0; i < nv; i++)
217               if (!strcmp (v[i], "VARNAME_"))
218                 {
219                   msg (SE, _("VARNAME_ cannot be explicitly specified on "
220                              "VARIABLES."));
221                   for (i = 0; i < nv; i++)
222                     free (v[i]);
223                   free (v);
224                   goto lossage;
225                 }
226           }
227           
228           {
229             int i;
230
231             for (i = 0; i < nv; i++)
232               {
233                 struct variable *new_var;
234                 
235                 if (strcmp (v[i], "ROWTYPE_"))
236                   {
237                     new_var = force_create_variable (&default_dict, v[i],
238                                                      NUMERIC, 0);
239                     new_var->p.mxd.vartype = MXD_CONTINUOUS;
240                     new_var->p.mxd.subtype = i;
241                   }
242                 else
243                   explicit_rowtype = 1;
244                 free (v[i]);
245               }
246             free (v);
247           }
248           
249           {
250             rowtype_ = force_create_variable (&default_dict, "ROWTYPE_",
251                                               ALPHA, 8);
252             rowtype_->p.mxd.vartype = MXD_ROWTYPE;
253             rowtype_->p.mxd.subtype = 0;
254           }
255         }
256       else if (lex_match_id ("FILE"))
257         {
258           lex_match ('=');
259           data_file = fh_parse_file_handle ();
260           if (!data_file)
261             goto lossage;
262         }
263       else if (lex_match_id ("FORMAT"))
264         {
265           lex_match ('=');
266
267           while (token == T_ID)
268             {
269               if (lex_match_id ("LIST"))
270                 fmt = LIST;
271               else if (lex_match_id ("FREE"))
272                 fmt = FREE;
273               else if (lex_match_id ("LOWER"))
274                 section = LOWER;
275               else if (lex_match_id ("UPPER"))
276                 section = UPPER;
277               else if (lex_match_id ("FULL"))
278                 section = FULL;
279               else if (lex_match_id ("DIAGONAL"))
280                 diag = DIAGONAL;
281               else if (lex_match_id ("NODIAGONAL"))
282                 diag = NODIAGONAL;
283               else 
284                 {
285                   lex_error (_("in FORMAT subcommand"));
286                   goto lossage;
287                 }
288             }
289         }
290       else if (lex_match_id ("SPLIT"))
291         {
292           lex_match ('=');
293
294           if (seen & 2)
295             {
296               msg (SE, _("SPLIT subcommand multiply specified."));
297               goto lossage;
298             }
299           seen |= 2;
300           
301           if (token != T_ID)
302             {
303               lex_error (_("in SPLIT subcommand"));
304               goto lossage;
305             }
306           
307           if (!is_varname (tokid)
308               && (lex_look_ahead () == '.' || lex_look_ahead () == '/'))
309             {
310               if (!strcmp (tokid, "ROWTYPE_") || !strcmp (tokid, "VARNAME_"))
311                 {
312                   msg (SE, _("Split variable may not be named ROWTYPE_ "
313                              "or VARNAME_."));
314                   goto lossage;
315                 }
316
317               single_split = force_create_variable (&default_dict, tokid,
318                                                     NUMERIC, 0);
319               lex_get ();
320
321               single_split->p.mxd.vartype = MXD_CONTINUOUS;
322
323               default_dict.n_splits = 1;
324               default_dict.splits = xmalloc (2 * sizeof *default_dict.splits);
325               default_dict.splits[0] = single_split;
326               default_dict.splits[1] = NULL;
327             }
328           else
329             {
330               struct variable **v;
331               int n;
332
333               if (!parse_variables (NULL, &v, &n, PV_NO_DUPLICATE))
334                 goto lossage;
335
336               default_dict.n_splits = n;
337               default_dict.splits = v = xrealloc (v, sizeof *v * (n + 1));
338               v[n] = NULL;
339             }
340           
341           {
342             int i;
343             
344             for (i = 0; i < default_dict.n_splits; i++)
345               {
346                 if (default_dict.splits[i]->p.mxd.vartype != MXD_CONTINUOUS)
347                   {
348                     msg (SE, _("Split variable %s is already another type."),
349                          tokid);
350                     goto lossage;
351                   }
352                 default_dict.splits[i]->p.mxd.vartype = MXD_SPLIT;
353                 default_dict.splits[i]->p.mxd.subtype = i;
354               }
355           }
356         }
357       else if (lex_match_id ("FACTORS"))
358         {
359           lex_match ('=');
360           
361           if (seen & 4)
362             {
363               msg (SE, _("FACTORS subcommand multiply specified."));
364               goto lossage;
365             }
366           seen |= 4;
367
368           if (!parse_variables (NULL, &factors, &n_factors, PV_NONE))
369             goto lossage;
370           
371           {
372             int i;
373             
374             for (i = 0; i < n_factors; i++)
375               {
376                 if (factors[i]->p.mxd.vartype != MXD_CONTINUOUS)
377                   {
378                     msg (SE, _("Factor variable %s is already another type."),
379                          tokid);
380                     goto lossage;
381                   }
382                 factors[i]->p.mxd.vartype = MXD_FACTOR;
383                 factors[i]->p.mxd.subtype = i;
384               }
385           }
386         }
387       else if (lex_match_id ("CELLS"))
388         {
389           lex_match ('=');
390           
391           if (cells != -1)
392             {
393               msg (SE, _("CELLS subcommand multiply specified."));
394               goto lossage;
395             }
396
397           if (!lex_integer_p () || lex_integer () < 1)
398             {
399               lex_error (_("expecting positive integer"));
400               goto lossage;
401             }
402
403           cells = lex_integer ();
404           lex_get ();
405         }
406       else if (lex_match_id ("N"))
407         {
408           lex_match ('=');
409
410           if (pop_n != -1)
411             {
412               msg (SE, _("N subcommand multiply specified."));
413               goto lossage;
414             }
415
416           if (!lex_integer_p () || lex_integer () < 1)
417             {
418               lex_error (_("expecting positive integer"));
419               goto lossage;
420             }
421
422           pop_n = lex_integer ();
423           lex_get ();
424         }
425       else if (lex_match_id ("CONTENTS"))
426         {
427           int inside_parens = 0;
428           unsigned collide = 0;
429           int item;
430           
431           if (seen & 8)
432             {
433               msg (SE, _("CONTENTS subcommand multiply specified."));
434               goto lossage;
435             }
436           seen |= 8;
437
438           lex_match ('=');
439           
440           {
441             int i;
442             
443             for (i = 0; i <= PROX; i++)
444               is_per_factor[i] = 0;
445           }
446
447           for (;;)
448             {
449               if (lex_match ('('))
450                 {
451                   if (inside_parens)
452                     {
453                       msg (SE, _("Nested parentheses not allowed."));
454                       goto lossage;
455                     }
456                   inside_parens = 1;
457                   item = LPAREN;
458                 }
459               else if (lex_match (')'))
460                 {
461                   if (!inside_parens)
462                     {
463                       msg (SE, _("Mismatched right parenthesis (`(')."));
464                       goto lossage;
465                     }
466                   if (contents[n_contents - 1] == LPAREN)
467                     {
468                       msg (SE, _("Empty parentheses not allowed."));
469                       goto lossage;
470                     }
471                   inside_parens = 0;
472                   item = RPAREN;
473                 }
474               else 
475                 {
476                   int content_type;
477                   int collide_index;
478                   
479                   if (token != T_ID)
480                     {
481                       lex_error (_("in CONTENTS subcommand"));
482                       goto lossage;
483                     }
484
485                   content_type = string_to_content_type (tokid,
486                                                          &collide_index);
487                   if (content_type == -1)
488                     {
489                       lex_error (_("in CONTENTS subcommand"));
490                       goto lossage;
491                     }
492                   lex_get ();
493
494                   if (collide & (1 << collide_index))
495                     {
496                       msg (SE, _("Content multiply specified for %s."),
497                            content_names[content_type]);
498                       goto lossage;
499                     }
500                   collide |= (1 << collide_index);
501                   
502                   item = content_type;
503                   is_per_factor[item] = inside_parens;
504                 }
505               contents[n_contents++] = item;
506
507               if (token == '/' || token == '.')
508                 break;
509             }
510
511           if (inside_parens)
512             {
513               msg (SE, _("Missing right parenthesis."));
514               goto lossage;
515             }
516           contents[n_contents] = EOC;
517         }
518       else 
519         {
520           lex_error (NULL);
521           goto lossage;
522         }
523     }
524   
525   if (token != '.')
526     {
527       lex_error (_("expecting end of command"));
528       goto lossage;
529     }
530   
531   if (!(seen & 1))
532     {
533       msg (SE, _("Missing VARIABLES subcommand."));
534       goto lossage;
535     }
536   
537   if (!n_contents && !explicit_rowtype)
538     {
539       msg (SW, _("CONTENTS subcommand not specified: assuming file "
540                  "contains only CORR matrix."));
541
542       contents[0] = CORR;
543       contents[1] = EOC;
544       n_contents = 0;
545     }
546
547   if (n_factors && !explicit_rowtype && cells == -1)
548     {
549       msg (SE, _("Missing CELLS subcommand.  CELLS is required "
550                  "when ROWTYPE_ is not given in the data and "
551                  "factors are present."));
552       goto lossage;
553     }
554
555   if (explicit_rowtype && single_split)
556     {
557       msg (SE, _("Split file values must be present in the data when "
558                  "ROWTYPE_ is present."));
559       goto lossage;
560     }
561       
562   /* Create VARNAME_. */
563   {
564     varname_ = force_create_variable (&default_dict, "VARNAME_",
565                                       ALPHA, 8);
566     varname_->p.mxd.vartype = MXD_VARNAME;
567     varname_->p.mxd.subtype = 0;
568   }
569   
570   /* Sort the dictionary variables into the desired order for the
571      system file output. */
572   {
573     int i;
574     
575     qsort (default_dict.var, default_dict.nvar, sizeof *default_dict.var,
576            compare_variables_by_mxd_vartype);
577
578     for (i = 0; i < default_dict.nvar; i++)
579       default_dict.var[i]->index = i;
580   }
581
582   /* Set formats. */
583   {
584     static const struct fmt_spec fmt_tab[MXD_COUNT] =
585       {
586         {FMT_F, 4, 0},
587         {FMT_A, 8, 0},
588         {FMT_F, 4, 0},
589         {FMT_A, 8, 0},
590         {FMT_F, 10, 4},
591       };
592     
593     int i;
594
595     first_continuous = -1;
596     for (i = 0; i < default_dict.nvar; i++)
597       {
598         struct variable *v = default_dict.var[i];
599         int type = v->p.mxd.vartype;
600         
601         assert (type >= 0 && type < MXD_COUNT);
602         v->print = v->write = fmt_tab[type];
603
604         if (type == MXD_CONTINUOUS)
605           n_continuous++;
606         if (first_continuous == -1 && type == MXD_CONTINUOUS)
607           first_continuous = i;
608       }
609   }
610
611   if (n_continuous == 0)
612     {
613       msg (SE, _("No continuous variables specified."));
614       goto lossage;
615     }
616
617 #if DEBUGGING
618   debug_print ();
619 #endif
620
621   if (explicit_rowtype)
622     read_matrices_with_rowtype ();
623   else
624     read_matrices_without_rowtype ();
625
626   pool_destroy (container);
627
628   return CMD_SUCCESS;
629
630 lossage:
631   discard_variables ();
632   free (factors);
633   pool_destroy (container);
634   return CMD_FAILURE;
635 }
636
637 /* Look up string S as a content-type name and return the
638    corresponding enumerated value, or -1 if there is no match.  If
639    COLLIDE is non-NULL then *COLLIDE returns a value (suitable for use
640    as a bit-index) which can be used for determining whether a related
641    statistic has already been used. */
642 static int
643 string_to_content_type (char *s, int *collide)
644 {
645   static const struct
646     {
647       int value;
648       int collide;
649       const char *string;
650     }
651   *tp,
652   tab[] = 
653     {
654       {N_VECTOR, 0, "N_VECTOR"},
655       {N_VECTOR, 0, "N"},
656       {N_SCALAR, 0, "N_SCALAR"},
657       {N_MATRIX, 1, "N_MATRIX"},
658       {MEAN, 2, "MEAN"},
659       {STDDEV, 3, "STDDEV"},
660       {STDDEV, 3, "SD"},
661       {COUNT, 4, "COUNT"},
662       {MSE, 5, "MSE"},
663       {DFE, 6, "DFE"},
664       {MAT, 7, "MAT"},
665       {COV, 8, "COV"},
666       {CORR, 9, "CORR"},
667       {PROX, 10, "PROX"},
668       {-1, -1, NULL},
669     };
670
671   for (tp = tab; tp->value != -1; tp++)
672     if (!strcmp (s, tp->string))
673       {
674         if (collide)
675           *collide = tp->collide;
676         
677         return tp->value;
678       }
679   return -1;
680 }
681
682 /* Compare two variables using p.mxd.vartype and p.mxd.subtype
683    fields. */
684 static int
685 compare_variables_by_mxd_vartype (const void *pa, const void *pb)
686 {
687   struct matrix_data_proc *a = &(*((struct variable **) pa))->p.mxd;
688   struct matrix_data_proc *b = &(*((struct variable **) pb))->p.mxd;
689   
690   return (a->vartype != b->vartype
691           ? a->vartype - b->vartype
692           : a->subtype - b->subtype);
693 }
694
695 #if DEBUGGING
696 /* Print out the command as input. */
697 static void
698 debug_print (void)
699 {
700   printf ("MATRIX DATA\n\t/VARIABLES=");
701   
702   {
703     int i;
704     
705     for (i = 0; i < default_dict.nvar; i++)
706       printf ("%s ", default_dict.var[i]->name);
707   }
708   printf ("\n");
709
710   printf ("\t/FORMAT=");
711   if (fmt == LIST)
712     printf ("LIST");
713   else if (fmt == FREE)
714     printf ("FREE");
715   else
716     assert (0);
717   if (section == LOWER)
718     printf (" LOWER");
719   else if (section == UPPER)
720     printf (" UPPER");
721   else if (section == FULL)
722     printf (" FULL");
723   else
724     assert (0);
725   if (diag == DIAGONAL)
726     printf (" DIAGONAL\n");
727   else if (diag == NODIAGONAL)
728     printf (" NODIAGONAL\n");
729   else
730     assert (0);
731
732   if (default_dict.n_splits)
733     {
734       int i;
735
736       printf ("\t/SPLIT=");
737       for (i = 0; i < default_dict.n_splits; i++)
738         printf ("%s ", default_dict.splits[i]->name);
739       if (single_split)
740         printf ("\t/* single split");
741       printf ("\n");
742     }
743   
744   if (n_factors)
745     {
746       int i;
747
748       printf ("\t/FACTORS=");
749       for (i = 0; i < n_factors; i++)
750         printf ("%s ", factors[i]->name);
751       printf ("\n");
752     }
753
754   if (cells != -1)
755     printf ("\t/CELLS=%d\n", cells);
756
757   if (pop_n != -1)
758     printf ("\t/N=%d\n", pop_n);
759
760   if (n_contents)
761     {
762       int i;
763       int space = 0;
764       
765       printf ("\t/CONTENTS=");
766       for (i = 0; i < n_contents; i++)
767         {
768           if (contents[i] == LPAREN)
769             {
770               if (space)
771                 printf (" ");
772               printf ("(");
773               space = 0;
774             }
775           else if (contents[i] == RPAREN)
776             {
777               printf (")");
778               space = 1;
779             }
780           else 
781             {
782
783               assert (contents[i] >= 0 && contents[i] <= PROX);
784               if (space)
785                 printf (" ");
786               printf ("%s", content_names[contents[i]]);
787               space = 1;
788             }
789         }
790       printf ("\n");
791     }
792 }
793 #endif /* DEBUGGING */
794 \f
795 /* Matrix tokenizer. */
796
797 /* Matrix token types. */
798 enum
799   {
800     MNULL,              /* No token. */
801     MNUM,               /* Number. */
802     MSTR,               /* String. */
803     MSTOP               /* End of file. */
804   };
805
806 /* Current matrix token. */
807 static int mtoken;
808
809 /* Token string if applicable; not null-terminated. */
810 static char *mtokstr;
811
812 /* Length of mtokstr in characters. */
813 static int mtoklen;
814
815 /* Token value if applicable. */
816 static double mtokval;
817
818 static int mget_token (void);
819
820 #if DEBUGGING
821 #define mget_token() mget_token_dump()
822
823 static int
824 mget_token_dump (void)
825 {
826   int result = (mget_token) ();
827   mdump_token ();
828   return result;
829 }
830
831 static void
832 mdump_token (void)
833 {
834   switch (mtoken)
835     {
836     case MNULL:
837       printf (" <NULLTOK>");
838       break;
839     case MNUM:
840       printf (" #%g", mtokval);
841       break;
842     case MSTR:
843       printf (" #'%.*s'", mtoklen, mtokstr);
844       break;
845     case MSTOP:
846       printf (" <STOP>");
847       break;
848     default:
849       assert (0);
850     }
851   fflush (stdout);
852 }
853 #endif
854
855 /* Return the current position in the data file. */
856 static const char *
857 context (void)
858 {
859   static char buf[32];
860   int len;
861   char *p = dfm_get_record (data_file, &len);
862   
863   if (!p || !len)
864     strcpy (buf, "at end of line");
865   else
866     {
867       char *cp = buf;
868       int n_copy = min (10, len);
869       cp = stpcpy (buf, "before `");
870       while (n_copy && isspace ((unsigned char) *p))
871         p++, n_copy++;
872       while (n_copy && !isspace ((unsigned char) *p))
873         *cp++ = *p++, n_copy--;
874       *cp++ = '\'';
875       *cp = 0;
876     }
877   
878   return buf;
879 }
880
881 /* Is there at least one token left in the data file? */
882 static int
883 another_token (void)
884 {
885   char *cp, *ep;
886   int len;
887
888   if (mtoken == MSTOP)
889     return 0;
890   
891   for (;;)
892     {
893       cp = dfm_get_record (data_file, &len);
894       if (!cp)
895         return 0;
896
897       ep = cp + len;
898       while (isspace ((unsigned char) *cp) && cp < ep)
899         cp++;
900
901       if (cp < ep)
902         break;
903
904       dfm_fwd_record (data_file);
905     }
906   
907   dfm_set_record (data_file, cp);
908
909   return 1;
910 }
911
912 /* Parse a MATRIX DATA token from data_file into mtok*. */
913 static int
914 (mget_token) (void)
915 {
916   char *cp, *ep;
917   int len;
918   int first_column;
919     
920   for (;;)
921     {
922       cp = dfm_get_record (data_file, &len);
923       if (!cp)
924         {
925           if (mtoken == MSTOP)
926             return 0;
927           mtoken = MSTOP;
928           return 1;
929         }
930
931       ep = cp + len;
932       while (isspace ((unsigned char) *cp) && cp < ep)
933         cp++;
934
935       if (cp < ep)
936         break;
937
938       dfm_fwd_record (data_file);
939     }
940   
941   dfm_set_record (data_file, cp);
942   first_column = dfm_get_cur_col (data_file) + 1;
943
944   /* Three types of fields: quoted with ', quoted with ", unquoted. */
945   if (*cp == '\'' || *cp == '"')
946     {
947       int quote = *cp;
948
949       mtoken = MSTR;
950       mtokstr = ++cp;
951       while (cp < ep && *cp != quote)
952         cp++;
953       mtoklen = cp - mtokstr;
954       if (cp < ep)
955         cp++;
956       else
957         msg (SW, _("Scope of string exceeds line."));
958     }
959   else
960     {
961       int is_num = isdigit ((unsigned char) *cp) || *cp == '.';
962
963       mtokstr = cp++;
964       while (cp < ep && !isspace ((unsigned char) *cp) && *cp != ','
965              && *cp != '-' && *cp != '+')
966         {
967           if (isdigit ((unsigned char) *cp))
968             is_num = 1;
969           
970           if ((tolower ((unsigned char) *cp) == 'd'
971                || tolower ((unsigned char) *cp) == 'e')
972               && (cp[1] == '+' || cp[1] == '-'))
973             cp += 2;
974           else
975             cp++;
976         }
977       
978       mtoklen = cp - mtokstr;
979       assert (mtoklen);
980
981       if (is_num)
982         {
983           struct data_in di;
984
985           di.s = mtokstr;
986           di.e = mtokstr + mtoklen;
987           di.v = (union value *) &mtokval;
988           di.f1 = first_column;
989           di.format.type = FMT_F;
990           di.format.w = mtoklen;
991           di.format.d = 0;
992
993           if (!data_in (&di))
994             return 0;
995         }
996       else
997         mtoken = MSTR;
998     }
999
1000   dfm_set_record (data_file, cp);
1001     
1002   return 1;
1003 }
1004
1005 /* Forcibly skip the end of a line for content type CONTENT in
1006    data_file. */
1007 static int
1008 force_eol (const char *content)
1009 {
1010   char *cp;
1011   int len;
1012   
1013   if (fmt == FREE)
1014     return 1;
1015
1016   cp = dfm_get_record (data_file, &len);
1017   if (!cp)
1018     return 0;
1019   while (len && isspace (*cp))
1020     cp++, len--;
1021   
1022   if (len)
1023     {
1024       msg (SE, _("End of line expected %s while reading %s."),
1025            context (), content);
1026       return 0;
1027     }
1028   
1029   dfm_fwd_record (data_file);
1030   
1031   return 1;
1032 }
1033 \f
1034 /* Back end, omitting ROWTYPE_. */
1035
1036 /* MATRIX DATA data. */
1037 static double ***nr_data;
1038
1039 /* Factor values. */
1040 static double *nr_factor_values;
1041
1042 /* Largest-numbered cell that we have read in thus far, plus one. */
1043 static int max_cell_index;
1044
1045 /* SPLIT FILE variable values. */
1046 static double *split_values;
1047
1048 static int nr_read_splits (int compare);
1049 static int nr_read_factors (int cell);
1050 static void nr_output_data (void);
1051 static int matrix_data_read_without_rowtype (void);
1052
1053 /* Read from the data file and write it to the active file. */
1054 static void
1055 read_matrices_without_rowtype (void)
1056 {
1057   if (cells == -1)
1058     cells = 1;
1059   
1060   mtoken = MNULL;
1061   split_values = xmalloc (sizeof *split_values * default_dict.n_splits);
1062   nr_factor_values = xmalloc (sizeof *nr_factor_values * n_factors * cells);
1063   max_cell_index = 0;
1064
1065   matrix_data_source.read = (void (*)(void)) matrix_data_read_without_rowtype;
1066   vfm_source = &matrix_data_source;
1067   
1068   procedure (NULL, NULL, NULL);
1069
1070   free (split_values);
1071   free (nr_factor_values);
1072
1073   fh_close_handle (data_file);
1074 }
1075
1076 /* Mirror data across the diagonal of matrix CP which contains
1077    CONTENT type data. */
1078 static void
1079 fill_matrix (int content, double *cp)
1080 {
1081   int type = content_type[content];
1082
1083   if (type == 1 && section != FULL)
1084     {
1085       if (diag == NODIAGONAL)
1086         {
1087           const double fill = content == CORR ? 1.0 : SYSMIS;
1088           int i;
1089
1090           for (i = 0; i < n_continuous; i++)
1091             cp[i * (1 + n_continuous)] = fill;
1092         }
1093       
1094       {
1095         int c, r;
1096         
1097         if (section == LOWER)
1098           {
1099             int n_lines = n_continuous;
1100             if (section != FULL && diag == NODIAGONAL)
1101               n_lines--;
1102             
1103             for (r = 1; r < n_lines; r++)
1104               for (c = 0; c < r; c++)
1105                 cp[r + c * n_continuous] = cp[c + r * n_continuous];
1106           }
1107         else 
1108           {
1109             assert (section == UPPER);
1110             for (r = 1; r < n_continuous; r++)
1111               for (c = 0; c < r; c++)
1112                 cp[c + r * n_continuous] = cp[r + c * n_continuous];
1113           }
1114       }
1115     }
1116   else if (type == 2)
1117     {
1118       int c;
1119
1120       for (c = 1; c < n_continuous; c++)
1121         cp[c] = cp[0];
1122     }
1123 }
1124
1125 /* Read data lines for content type CONTENT from the data file.  If
1126    PER_FACTOR is nonzero, then factor information is read from the
1127    data file.  Data is for cell number CELL. */
1128 static int
1129 nr_read_data_lines (int per_factor, int cell, int content, int compare)
1130 {
1131   /* Content type. */
1132   const int type = content_type[content];
1133   
1134   /* Number of lines that must be parsed from the data file for this
1135      content type. */
1136   int n_lines;
1137   
1138   /* Current position in vector or matrix. */
1139   double *cp;
1140
1141   /* Counter. */
1142   int i;
1143
1144   if (type != 1)
1145     n_lines = 1;
1146   else
1147     {
1148       n_lines = n_continuous;
1149       if (section != FULL && diag == NODIAGONAL)
1150         n_lines--;
1151     }
1152
1153   cp = nr_data[content][cell];
1154   if (type == 1 && section == LOWER && diag == NODIAGONAL)
1155     cp += n_continuous;
1156
1157   for (i = 0; i < n_lines; i++)
1158     {
1159       int n_cols;
1160       
1161       if (!nr_read_splits (1))
1162         return 0;
1163       if (per_factor && !nr_read_factors (cell))
1164         return 0;
1165       compare = 1;
1166
1167       switch (type)
1168         {
1169         case 0:
1170           n_cols = n_continuous;
1171           break;
1172         case 1:
1173           switch (section)
1174             {
1175             case LOWER:
1176               n_cols = i + 1;
1177               break;
1178             case UPPER:
1179               cp += i;
1180               n_cols = n_continuous - i;
1181               if (diag == NODIAGONAL)
1182                 {
1183                   n_cols--;
1184                   cp++;
1185                 }
1186               break;
1187             case FULL:
1188               n_cols = n_continuous;
1189               break;
1190             default:
1191               assert (0);
1192             }
1193           break;
1194         case 2:
1195           n_cols = 1;
1196           break;
1197         default:
1198           assert (0);
1199         }
1200
1201       {
1202         int j;
1203         
1204         for (j = 0; j < n_cols; j++)
1205           {
1206             if (!mget_token ())
1207               return 0;
1208             if (mtoken != MNUM)
1209               {
1210                 msg (SE, _("expecting value for %s %s"),
1211                      default_dict.var[j]->name, context ());
1212                 return 0;
1213               }
1214
1215             *cp++ = mtokval;
1216           }
1217         if (!force_eol (content_names[content]))
1218           return 0;
1219         debug_printf (("\n"));
1220       }
1221
1222       if (section == LOWER)
1223         cp += n_continuous - n_cols;
1224     }
1225
1226   fill_matrix (content, nr_data[content][cell]);
1227
1228   return 1;
1229 }
1230
1231 /* When ROWTYPE_ does not appear in the data, reads the matrices and
1232    writes them to the output file.  Returns success. */
1233 static int
1234 matrix_data_read_without_rowtype (void)
1235 {
1236   {
1237     int *cp;
1238
1239     nr_data = pool_alloc (container, (PROX + 1) * sizeof *nr_data);
1240     
1241     {
1242       int i;
1243
1244       for (i = 0; i <= PROX; i++)
1245         nr_data[i] = NULL;
1246     }
1247     
1248     for (cp = contents; *cp != EOC; cp++)
1249       if (*cp != LPAREN && *cp != RPAREN)
1250         {
1251           int per_factor = is_per_factor[*cp];
1252           int n_entries;
1253           
1254           n_entries = n_continuous;
1255           if (content_type[*cp] == 1)
1256             n_entries *= n_continuous;
1257           
1258           {
1259             int n_vectors = per_factor ? cells : 1;
1260             int i;
1261             
1262             nr_data[*cp] = pool_alloc (container,
1263                                        n_vectors * sizeof **nr_data);
1264             
1265             for (i = 0; i < n_vectors; i++)
1266               nr_data[*cp][i] = pool_alloc (container,
1267                                             n_entries * sizeof ***nr_data);
1268           }
1269         }
1270   }
1271   
1272   for (;;)
1273     {
1274       int *bp, *ep, *np;
1275       
1276       if (!nr_read_splits (0))
1277         return 0;
1278       
1279       for (bp = contents; *bp != EOC; bp = np)
1280         {
1281           int per_factor;
1282
1283           /* Trap the CONTENTS that we should parse in this pass
1284              between bp and ep.  Set np to the starting bp for next
1285              iteration. */
1286           if (*bp == LPAREN)
1287             {
1288               ep = ++bp;
1289               while (*ep != RPAREN)
1290                 ep++;
1291               np = &ep[1];
1292               per_factor = 1;
1293             }
1294           else
1295             {
1296               ep = &bp[1];
1297               while (*ep != EOC && *ep != LPAREN)
1298                 ep++;
1299               np = ep;
1300               per_factor = 0;
1301             }
1302           
1303           {
1304             int i;
1305               
1306             for (i = 0; i < (per_factor ? cells : 1); i++)
1307               {
1308                 int *cp;
1309
1310                 for (cp = bp; cp < ep; cp++) 
1311                   if (!nr_read_data_lines (per_factor, i, *cp, cp != bp))
1312                     return 0;
1313               }
1314           }
1315         }
1316
1317       nr_output_data ();
1318
1319       if (default_dict.n_splits == 0 || !another_token ())
1320         return 1;
1321     }
1322 }
1323
1324 /* Read the split file variables.  If COMPARE is 1, compares the
1325    values read to the last values read and returns 1 if they're equal,
1326    0 otherwise. */
1327 static int
1328 nr_read_splits (int compare)
1329 {
1330   static int just_read = 0;
1331
1332   if (compare && just_read)
1333     {
1334       just_read = 0;
1335       return 1;
1336     }
1337   
1338   if (default_dict.n_splits == 0)
1339     return 1;
1340
1341   if (single_split)
1342     {
1343       if (!compare)
1344         split_values[0] = ++default_dict.splits[0]->p.mxd.subtype;
1345       return 1;
1346     }
1347
1348   if (!compare)
1349     just_read = 1;
1350   
1351   {
1352     int i;
1353     
1354     for (i = 0; i < default_dict.n_splits; i++)
1355       {
1356         if (!mget_token ())
1357           return 0;
1358         if (mtoken != MNUM)
1359           {
1360             msg (SE, _("Syntax error expecting SPLIT FILE value %s."),
1361                  context ());
1362             return 0;
1363           }
1364
1365         if (!compare)
1366           split_values[i] = mtokval;
1367         else if (split_values[i] != mtokval)
1368           {
1369             msg (SE, _("Expecting value %g for %s."),
1370                   split_values[i], default_dict.splits[i]->name);
1371             return 0;
1372           }
1373       }
1374   }
1375
1376   return 1;
1377 }
1378
1379 /* Read the factors for cell CELL.  If COMPARE is 1, compares the
1380    values read to the last values read and returns 1 if they're equal,
1381    0 otherwise. */
1382 static int
1383 nr_read_factors (int cell)
1384 {
1385   int compare;
1386   
1387   if (n_factors == 0)
1388     return 1;
1389
1390   assert (max_cell_index >= cell);
1391   if (cell != max_cell_index)
1392     compare = 1;
1393   else
1394     {
1395       compare = 0;
1396       max_cell_index++;
1397     }
1398       
1399   {
1400     int i;
1401     
1402     for (i = 0; i < n_factors; i++)
1403       {
1404         if (!mget_token ())
1405           return 0;
1406         if (mtoken != MNUM)
1407           {
1408             msg (SE, _("Syntax error expecting factor value %s."),
1409                  context ());
1410             return 0;
1411           }
1412         
1413         if (!compare)
1414           nr_factor_values[i + n_factors * cell] = mtokval;
1415         else if (nr_factor_values[i + n_factors * cell] != mtokval)
1416           {
1417             msg (SE, _("Syntax error expecting value %g for %s %s."),
1418                  nr_factor_values[i + n_factors * cell],
1419                  factors[i]->name, context ());
1420             return 0;
1421           }
1422       }
1423   }
1424
1425   return 1;
1426 }
1427
1428 /* Write the contents of a cell having content type CONTENT and data
1429    CP to the active file. */
1430 static void
1431 dump_cell_content (int content, double *cp)
1432 {
1433   int type = content_type[content];
1434
1435   {
1436     st_bare_pad_copy (temp_case->data[rowtype_->fv].s,
1437                       content_names[content], 8);
1438     
1439     if (type != 1)
1440       memset (&temp_case->data[varname_->fv].s, ' ', 8);
1441   }
1442
1443   {
1444     int n_lines = (type == 1) ? n_continuous : 1;
1445     int i;
1446                 
1447     for (i = 0; i < n_lines; i++)
1448       {
1449         int j;
1450
1451         for (j = 0; j < n_continuous; j++)
1452           {
1453             temp_case->data[(default_dict.var
1454                              [first_continuous + j]->fv)].f = *cp;
1455             debug_printf (("c:%s(%g) ",
1456                            default_dict.var[first_continuous + j]->name,
1457                            *cp));
1458             cp++;
1459           }
1460         if (type == 1)
1461           st_bare_pad_copy (temp_case->data[varname_->fv].s,
1462                             default_dict.var[first_continuous + i]->name,
1463                             8);
1464         debug_printf (("\n"));
1465         write_case ();
1466       }
1467   }
1468 }
1469
1470 /* Finally dump out everything from nr_data[] to the output file. */
1471 static void
1472 nr_output_data (void)
1473 {
1474   {
1475     int i;
1476
1477     for (i = 0; i < default_dict.n_splits; i++)
1478       temp_case->data[default_dict.splits[i]->fv].f = split_values[i];
1479   }
1480
1481   if (n_factors)
1482     {
1483       int cell;
1484
1485       for (cell = 0; cell < cells; cell++)
1486         {
1487           {
1488             int factor;
1489
1490             for (factor = 0; factor < n_factors; factor++)
1491               {
1492                 temp_case->data[factors[factor]->fv].f
1493                   = nr_factor_values[factor + cell * n_factors];
1494                 debug_printf (("f:%s ", factors[factor]->name));
1495               }
1496           }
1497           
1498           {
1499             int content;
1500             
1501             for (content = 0; content <= PROX; content++)
1502               if (is_per_factor[content])
1503                 {
1504                   assert (nr_data[content] != NULL
1505                           && nr_data[content][cell] != NULL);
1506
1507                   dump_cell_content (content, nr_data[content][cell]);
1508                 }
1509           }
1510         }
1511     }
1512
1513   {
1514     int content;
1515     
1516     {
1517       int factor;
1518
1519       for (factor = 0; factor < n_factors; factor++)
1520         temp_case->data[factors[factor]->fv].f = SYSMIS;
1521     }
1522     
1523     for (content = 0; content <= PROX; content++)
1524       if (!is_per_factor[content] && nr_data[content] != NULL)
1525         dump_cell_content (content, nr_data[content][0]);
1526   }
1527 }
1528 \f
1529 /* Back end, with ROWTYPE_. */
1530
1531 /* Type of current row. */
1532 static int wr_content;
1533
1534 /* All the data for one set of factor values. */
1535 struct factor_data
1536   {
1537     double *factors;
1538     int n_rows[PROX + 1];
1539     double *data[PROX + 1];
1540     struct factor_data *next;
1541   };
1542
1543 /* All the data, period. */
1544 struct factor_data *wr_data;
1545
1546 /* Current factor. */
1547 struct factor_data *wr_current;
1548
1549 static int wr_read_splits (void);
1550 static int wr_output_data (void);
1551 static int wr_read_rowtype (void);
1552 static int wr_read_factors (void);
1553 static int wr_read_indeps (void);
1554 static int matrix_data_read_with_rowtype (void);
1555
1556 /* When ROWTYPE_ appears in the data, reads the matrices and writes
1557    them to the output file. */
1558 static void
1559 read_matrices_with_rowtype (void)
1560 {
1561   mtoken = MNULL;
1562   wr_data = wr_current = NULL;
1563   split_values = NULL;
1564   cells = 0;
1565
1566   matrix_data_source.read = (void (*)(void)) matrix_data_read_with_rowtype;
1567   vfm_source = &matrix_data_source;
1568   
1569   procedure (NULL, NULL, NULL);
1570
1571   free (split_values);
1572   fh_close_handle (data_file);
1573 }
1574
1575 /* Read from the data file and write it to the active file. */
1576 static int
1577 matrix_data_read_with_rowtype (void)
1578 {
1579   do
1580     {
1581       if (!wr_read_splits ())
1582         return 0;
1583
1584       if (!wr_read_factors ())
1585         return 0;
1586
1587       if (!wr_read_indeps ())
1588         return 0;
1589     }
1590   while (another_token ());
1591
1592   wr_output_data ();
1593   return 1;
1594 }
1595
1596 /* Read the split file variables.  If they differ from the previous
1597    set of split variables then output the data.  Returns success. */
1598 static int 
1599 wr_read_splits (void)
1600 {
1601   int compare;
1602   
1603   if (default_dict.n_splits == 0)
1604     return 1;
1605
1606   if (split_values)
1607     compare = 1;
1608   else
1609     {
1610       compare = 0;
1611       split_values = xmalloc (sizeof *split_values * default_dict.n_splits);
1612     }
1613   
1614   {
1615     int different = 0;
1616     int i;
1617     
1618     for (i = 0; i < default_dict.n_splits; i++)
1619       {
1620         if (!mget_token ())
1621           return 0;
1622         if (mtoken != MNUM)
1623           {
1624             msg (SE, _("Syntax error %s expecting SPLIT FILE value."),
1625                  context ());
1626             return 0;
1627           }
1628
1629         if (compare && split_values[i] != mtokval && !different)
1630           {
1631             if (!wr_output_data ())
1632               return 0;
1633             different = 1;
1634             cells = 0;
1635           }
1636         split_values[i] = mtokval;
1637       }
1638   }
1639
1640   return 1;
1641 }
1642
1643 /* Return strcmp()-type comparison of the n_factors factors at _A and
1644    _B.  Sort missing values toward the end. */
1645 static int
1646 compare_factors (const void *pa, const void *pb)
1647 {
1648   const double *a = (*(struct factor_data **) pa)->factors;
1649   const double *b = (*(struct factor_data **) pb)->factors;
1650   int i;
1651
1652   for (i = 0; i < n_factors; i++, a++, b++)
1653     {
1654       if (*a == *b)
1655         continue;
1656       
1657       if (*a == SYSMIS)
1658         return 1;
1659       else if (*b == SYSMIS)
1660         return -1;
1661       else
1662         return *a - *b < 0 ? -1 : 1;
1663     }
1664
1665   return 0;
1666 }
1667
1668 /* Write out the data for the current split file to the active
1669    file. */
1670 static int 
1671 wr_output_data (void)
1672 {
1673   {
1674     int i;
1675
1676     for (i = 0; i < default_dict.n_splits; i++)
1677       temp_case->data[default_dict.splits[i]->fv].f = split_values[i];
1678   }
1679
1680   /* Sort the wr_data list. */
1681   {
1682     struct factor_data **factors;
1683     struct factor_data *iter;
1684     int i;
1685
1686     factors = xmalloc (sizeof *factors * cells);
1687
1688     for (i = 0, iter = wr_data; iter; iter = iter->next, i++)
1689       factors[i] = iter;
1690
1691     qsort (factors, cells, sizeof *factors, compare_factors);
1692
1693     wr_data = factors[0];
1694     for (i = 0; i < cells - 1; i++)
1695       factors[i]->next = factors[i + 1];
1696     factors[cells - 1]->next = NULL;
1697
1698     free (factors);
1699   }
1700
1701   /* Write out records for every set of factor values. */
1702   {
1703     struct factor_data *iter;
1704     
1705     for (iter = wr_data; iter; iter = iter->next)
1706       {
1707         {
1708           int factor;
1709
1710           for (factor = 0; factor < n_factors; factor++)
1711             {
1712               temp_case->data[factors[factor]->fv].f
1713                 = iter->factors[factor];
1714               debug_printf (("f:%s ", factors[factor]->name));
1715             }
1716         }
1717         
1718         {
1719           int content;
1720
1721           for (content = 0; content <= PROX; content++)
1722             {
1723               if (!iter->n_rows[content])
1724                 continue;
1725               
1726               {
1727                 int type = content_type[content];
1728                 int n_lines = (type == 1
1729                                ? (n_continuous
1730                                   - (section != FULL && diag == NODIAGONAL))
1731                                : 1);
1732                 
1733                 if (n_lines != iter->n_rows[content])
1734                   {
1735                     msg (SE, _("Expected %d lines of data for %s content; "
1736                                "actually saw %d lines.  No data will be "
1737                                "output for this content."),
1738                          n_lines, content_names[content],
1739                          iter->n_rows[content]);
1740                     continue;
1741                   }
1742               }
1743
1744               fill_matrix (content, iter->data[content]);
1745
1746               dump_cell_content (content, iter->data[content]);
1747             }
1748         }
1749       }
1750   }
1751   
1752   pool_destroy (container);
1753   container = pool_create ();
1754   
1755   wr_data = wr_current = NULL;
1756   
1757   return 1;
1758 }
1759
1760 /* Read ROWTYPE_ from the data file.  Return success. */
1761 static int 
1762 wr_read_rowtype (void)
1763 {
1764   if (wr_content != -1)
1765     {
1766       msg (SE, _("Multiply specified ROWTYPE_ %s."), context ());
1767       return 0;
1768     }
1769   if (mtoken != MSTR)
1770     {
1771       msg (SE, _("Syntax error %s expecting ROWTYPE_ string."), context ());
1772       return 0;
1773     }
1774   
1775   {
1776     char s[16];
1777     char *cp;
1778     
1779     memcpy (s, mtokstr, min (15, mtoklen));
1780     s[min (15, mtoklen)] = 0;
1781
1782     for (cp = s; *cp; cp++)
1783       *cp = toupper ((unsigned char) *cp);
1784
1785     wr_content = string_to_content_type (s, NULL);
1786   }
1787
1788   if (wr_content == -1)
1789     {
1790       msg (SE, _("Syntax error %s."), context ());
1791       return 0;
1792     }
1793
1794   return 1;
1795 }
1796
1797 /* Read the factors for the current row.  Select a set of factors and
1798    point wr_current to it. */
1799 static int 
1800 wr_read_factors (void)
1801 {
1802   double *factor_values = local_alloc (sizeof *factor_values * n_factors);
1803
1804   wr_content = -1;
1805   {
1806     int i;
1807   
1808     for (i = 0; i < n_factors; i++)
1809       {
1810         if (!mget_token ())
1811           goto lossage;
1812         if (mtoken == MSTR)
1813           {
1814             if (!wr_read_rowtype ())
1815               goto lossage;
1816             if (!mget_token ())
1817               goto lossage;
1818           }
1819         if (mtoken != MNUM)
1820           {
1821             msg (SE, _("Syntax error expecting factor value %s."),
1822                  context ());
1823             goto lossage;
1824           }
1825         
1826         factor_values[i] = mtokval;
1827       }
1828   }
1829   if (wr_content == -1)
1830     {
1831       if (!mget_token ())
1832         goto lossage;
1833       if (!wr_read_rowtype ())
1834         goto lossage;
1835     }
1836   
1837   /* Try the most recent factor first as a simple caching
1838      mechanism. */
1839   if (wr_current)
1840     {
1841       int i;
1842       
1843       for (i = 0; i < n_factors; i++)
1844         if (factor_values[i] != wr_current->factors[i])
1845           goto cache_miss;
1846       goto winnage;
1847     }
1848
1849   /* Linear search through the list. */
1850 cache_miss:
1851   {
1852     struct factor_data *iter;
1853
1854     for (iter = wr_data; iter; iter = iter->next)
1855       {
1856         int i;
1857
1858         for (i = 0; i < n_factors; i++)
1859           if (factor_values[i] != iter->factors[i])
1860             goto next_item;
1861         
1862         wr_current = iter;
1863         goto winnage;
1864         
1865       next_item: ;
1866       }
1867   }
1868
1869   /* Not found.  Make a new item. */
1870   {
1871     struct factor_data *new = pool_alloc (container, sizeof *new);
1872
1873     new->factors = pool_alloc (container, sizeof *new->factors * n_factors);
1874     
1875     {
1876       int i;
1877
1878       for (i = 0; i < n_factors; i++)
1879         new->factors[i] = factor_values[i];
1880     }
1881     
1882     {
1883       int i;
1884
1885       for (i = 0; i <= PROX; i++)
1886         {
1887           new->n_rows[i] = 0;
1888           new->data[i] = NULL;
1889         }
1890     }
1891
1892     new->next = wr_data;
1893     wr_data = wr_current = new;
1894     cells++;
1895   }
1896
1897 winnage:
1898   local_free (factor_values);
1899   return 1;
1900
1901 lossage:
1902   local_free (factor_values);
1903   return 0;
1904 }
1905
1906 /* Read the independent variables into wr_current. */
1907 static int 
1908 wr_read_indeps (void)
1909 {
1910   struct factor_data *c = wr_current;
1911   const int type = content_type[wr_content];
1912   const int n_rows = c->n_rows[wr_content];
1913   double *cp;
1914   int n_cols;
1915
1916   /* Allocate room for data if necessary. */
1917   if (c->data[wr_content] == NULL)
1918     {
1919       int n_items = n_continuous;
1920       if (type == 1)
1921         n_items *= n_continuous;
1922       
1923       c->data[wr_content] = pool_alloc (container,
1924                                         sizeof **c->data * n_items);
1925     }
1926
1927   cp = &c->data[wr_content][n_rows * n_continuous];
1928
1929   /* Figure out how much to read from this line. */
1930   switch (type)
1931     {
1932     case 0:
1933     case 2:
1934       if (n_rows > 0)
1935         {
1936           msg (SE, _("Duplicate specification for %s."),
1937                content_names[wr_content]);
1938           return 0;
1939         }
1940       if (type == 0)
1941         n_cols = n_continuous;
1942       else
1943         n_cols = 1;
1944       break;
1945     case 1:
1946       if (n_rows >= n_continuous - (section != FULL && diag == NODIAGONAL))
1947         {
1948           msg (SE, _("Too many rows of matrix data for %s."),
1949                content_names[wr_content]);
1950           return 0;
1951         }
1952       
1953       switch (section)
1954         {
1955         case LOWER:
1956           n_cols = n_rows + 1;
1957           if (diag == NODIAGONAL)
1958             cp += n_continuous;
1959           break;
1960         case UPPER:
1961           cp += n_rows;
1962           n_cols = n_continuous - n_rows;
1963           if (diag == NODIAGONAL)
1964             {
1965               n_cols--;
1966               cp++;
1967             }
1968           break;
1969         case FULL:
1970           n_cols = n_continuous;
1971           break;
1972         default:
1973           assert (0);
1974         }
1975       break;
1976     default:
1977       assert (0);
1978     }
1979   c->n_rows[wr_content]++;
1980
1981   debug_printf ((" (c=%p,r=%d,n=%d)", c, n_rows + 1, n_cols));
1982
1983   /* Read N_COLS items at CP. */
1984   {
1985     int j;
1986         
1987     for (j = 0; j < n_cols; j++)
1988       {
1989         if (!mget_token ())
1990           return 0;
1991         if (mtoken != MNUM)
1992           {
1993             msg (SE, _("Syntax error expecting value for %s %s."),
1994                  default_dict.var[first_continuous + j]->name, context ());
1995             return 0;
1996           }
1997
1998         *cp++ = mtokval;
1999       }
2000     if (!force_eol (content_names[wr_content]))
2001       return 0;
2002     debug_printf (("\n"));
2003   }
2004
2005   return 1;
2006 }
2007 \f
2008 /* Matrix source. */
2009
2010 struct case_stream matrix_data_source = 
2011   {
2012     NULL,
2013     NULL,
2014     NULL,
2015     NULL,
2016     NULL,
2017     NULL,
2018     "MATRIX DATA",
2019   };
2020