Implement MRSETS command.
[pspp-builds.git] / tests / dissect-sysfile.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <ctype.h>
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include <data/val-type.h>
26 #include <libpspp/compiler.h>
27 #include <libpspp/float-format.h>
28 #include <libpspp/integer-format.h>
29 #include <libpspp/misc.h>
30
31 #include "error.h"
32 #include "minmax.h"
33 #include "progname.h"
34 #include "xalloc.h"
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38
39 #define VAR_NAME_LEN 64
40
41 struct sfm_reader
42   {
43     const char *file_name;
44     FILE *file;
45
46     int n_variable_records, n_variables;
47
48     int *var_widths;
49     size_t n_var_widths, allocated_var_widths;
50
51     enum integer_format integer_format;
52     enum float_format float_format;
53
54     bool compressed;
55     double bias;
56   };
57
58 static void read_header (struct sfm_reader *);
59 static void read_variable_record (struct sfm_reader *);
60 static void read_value_label_record (struct sfm_reader *);
61 static void read_document_record (struct sfm_reader *);
62 static void read_extension_record (struct sfm_reader *);
63 static void read_machine_integer_info (struct sfm_reader *,
64                                        size_t size, size_t count);
65 static void read_machine_float_info (struct sfm_reader *,
66                                      size_t size, size_t count);
67 static void read_mrsets (struct sfm_reader *, size_t size, size_t count);
68 static void read_display_parameters (struct sfm_reader *,
69                                      size_t size, size_t count);
70 static void read_long_var_name_map (struct sfm_reader *r,
71                                     size_t size, size_t count);
72 static void read_long_string_map (struct sfm_reader *r,
73                                   size_t size, size_t count);
74 static void read_datafile_attributes (struct sfm_reader *r,
75                                       size_t size, size_t count);
76 static void read_variable_attributes (struct sfm_reader *r,
77                                       size_t size, size_t count);
78 static void read_ncases64 (struct sfm_reader *, size_t size, size_t count);
79 static void read_character_encoding (struct sfm_reader *r,
80                                        size_t size, size_t count);
81 static void read_long_string_value_labels (struct sfm_reader *r,
82                                            size_t size, size_t count);
83 static void read_unknown_extension (struct sfm_reader *,
84                                     size_t size, size_t count);
85 static void read_compressed_data (struct sfm_reader *);
86
87 static struct text_record *open_text_record (
88   struct sfm_reader *, size_t size);
89 static void close_text_record (struct text_record *);
90 static bool read_variable_to_value_pair (struct text_record *,
91                                          char **key, char **value);
92 static char *text_tokenize (struct text_record *, int delimiter);
93 static bool text_match (struct text_record *text, int c);
94 static const char *text_parse_counted_string (struct text_record *);
95 static size_t text_pos (const struct text_record *);
96
97 static void usage (int exit_code);
98 static void sys_warn (struct sfm_reader *, const char *, ...)
99      PRINTF_FORMAT (2, 3);
100 static void sys_error (struct sfm_reader *, const char *, ...)
101      PRINTF_FORMAT (2, 3)
102      NO_RETURN;
103
104 static void read_bytes (struct sfm_reader *, void *, size_t);
105 static int read_int (struct sfm_reader *);
106 static int64_t read_int64 (struct sfm_reader *);
107 static double read_float (struct sfm_reader *);
108 static void read_string (struct sfm_reader *, char *, size_t);
109 static void skip_bytes (struct sfm_reader *, size_t);
110 static void trim_spaces (char *);
111
112 int
113 main (int argc, char *argv[])
114 {
115   struct sfm_reader r;
116   int i;
117
118   set_program_name (argv[0]);
119   if (argc < 2)
120     usage (EXIT_FAILURE);
121
122   for (i = 1; i < argc; i++) 
123     {
124       int rec_type;
125
126       r.file_name = argv[i];
127       r.file = fopen (r.file_name, "rb");
128       if (r.file == NULL)
129         error (EXIT_FAILURE, errno, "error opening \"%s\"", r.file_name);
130       r.n_variable_records = 0;
131       r.n_variables = 0;
132       r.n_var_widths = 0;
133       r.allocated_var_widths = 0;
134       r.var_widths = 0;
135       r.compressed = false;
136
137       if (argc > 2)
138         printf ("Reading \"%s\":\n", r.file_name);
139       
140       read_header (&r);
141       while ((rec_type = read_int (&r)) != 999)
142         {
143           switch (rec_type)
144             {
145             case 2:
146               read_variable_record (&r);
147               break;
148
149             case 3:
150               read_value_label_record (&r);
151               break;
152
153             case 4:
154               sys_error (&r, _("Misplaced type 4 record."));
155
156             case 6:
157               read_document_record (&r);
158               break;
159
160             case 7:
161               read_extension_record (&r);
162               break;
163
164             default:
165               sys_error (&r, _("Unrecognized record type %d."), rec_type);
166             }
167         }
168       printf ("%08lx: end-of-dictionary record "
169               "(first byte of data at %08lx)\n",
170               ftell (r.file), ftell (r.file) + 4);
171
172       if (r.compressed)
173         read_compressed_data (&r);
174
175       fclose (r.file);
176     }
177   
178   return 0;
179 }
180
181 static void
182 read_header (struct sfm_reader *r)
183 {
184   char rec_type[5];
185   char eye_catcher[61];
186   uint8_t raw_layout_code[4];
187   int32_t layout_code;
188   int32_t nominal_case_size;
189   int32_t compressed;
190   int32_t weight_index;
191   int32_t ncases;
192   uint8_t raw_bias[8];
193   char creation_date[10];
194   char creation_time[9];
195   char file_label[65];
196
197   read_string (r, rec_type, sizeof rec_type);
198   read_string (r, eye_catcher, sizeof eye_catcher);
199
200   if (strcmp ("$FL2", rec_type) != 0)
201     sys_error (r, _("This is not an SPSS system file."));
202
203   /* Identify integer format. */
204   read_bytes (r, raw_layout_code, sizeof raw_layout_code);
205   if ((!integer_identify (2, raw_layout_code, sizeof raw_layout_code,
206                           &r->integer_format)
207        && !integer_identify (3, raw_layout_code, sizeof raw_layout_code,
208                              &r->integer_format))
209       || (r->integer_format != INTEGER_MSB_FIRST
210           && r->integer_format != INTEGER_LSB_FIRST))
211     sys_error (r, _("This is not an SPSS system file."));
212   layout_code = integer_get (r->integer_format,
213                              raw_layout_code, sizeof raw_layout_code);
214
215   nominal_case_size = read_int (r);
216   compressed = read_int (r);
217   weight_index = read_int (r);
218   ncases = read_int (r);
219
220   r->compressed = compressed != 0;
221
222   /* Identify floating-point format and obtain compression bias. */
223   read_bytes (r, raw_bias, sizeof raw_bias);
224   if (float_identify (100.0, raw_bias, sizeof raw_bias, &r->float_format) == 0)
225     {
226       sys_warn (r, _("Compression bias is not the usual "
227                      "value of 100, or system file uses unrecognized "
228                      "floating-point format."));
229       if (r->integer_format == INTEGER_MSB_FIRST)
230         r->float_format = FLOAT_IEEE_DOUBLE_BE;
231       else
232         r->float_format = FLOAT_IEEE_DOUBLE_LE;
233     }
234   r->bias = float_get_double (r->float_format, raw_bias);
235
236   read_string (r, creation_date, sizeof creation_date);
237   read_string (r, creation_time, sizeof creation_time);
238   read_string (r, file_label, sizeof file_label);
239   trim_spaces (file_label);
240   skip_bytes (r, 3);
241
242   printf ("File header record:\n");
243   printf ("\t%17s: %s\n", "Product name", eye_catcher);
244   printf ("\t%17s: %"PRId32"\n", "Layout code", layout_code);
245   printf ("\t%17s: %"PRId32"\n", "Compressed", compressed);
246   printf ("\t%17s: %"PRId32"\n", "Weight index", weight_index);
247   printf ("\t%17s: %"PRId32"\n", "Number of cases", ncases);
248   printf ("\t%17s: %g\n", "Compression bias", r->bias);
249   printf ("\t%17s: %s\n", "Creation date", creation_date);
250   printf ("\t%17s: %s\n", "Creation time", creation_time);
251   printf ("\t%17s: \"%s\"\n", "File label", file_label);
252 }
253
254 static const char *
255 format_name (int format)
256 {
257   switch ((format >> 16) & 0xff)
258     {
259     case 1: return "A";
260     case 2: return "AHEX";
261     case 3: return "COMMA";
262     case 4: return "DOLLAR";
263     case 5: return "F";
264     case 6: return "IB";
265     case 7: return "PIBHEX";
266     case 8: return "P";
267     case 9: return "PIB";
268     case 10: return "PK";
269     case 11: return "RB";
270     case 12: return "RBHEX";
271     case 15: return "Z";
272     case 16: return "N";
273     case 17: return "E";
274     case 20: return "DATE";
275     case 21: return "TIME";
276     case 22: return "DATETIME";
277     case 23: return "ADATE";
278     case 24: return "JDATE";
279     case 25: return "DTIME";
280     case 26: return "WKDAY";
281     case 27: return "MONTH";
282     case 28: return "MOYR";
283     case 29: return "QYR";
284     case 30: return "WKYR";
285     case 31: return "PCT";
286     case 32: return "DOT";
287     case 33: return "CCA";
288     case 34: return "CCB";
289     case 35: return "CCC";
290     case 36: return "CCD";
291     case 37: return "CCE";
292     case 38: return "EDATE";
293     case 39: return "SDATE";
294     default: return "invalid";
295     }
296 }
297
298 /* Reads a variable (type 2) record from R and adds the
299    corresponding variable to DICT.
300    Also skips past additional variable records for long string
301    variables. */
302 static void
303 read_variable_record (struct sfm_reader *r)
304 {
305   int width;
306   int has_variable_label;
307   int missing_value_code;
308   int print_format;
309   int write_format;
310   char name[9];
311
312   printf ("%08lx: variable record #%d\n",
313           ftell (r->file), r->n_variable_records++);
314
315   width = read_int (r);
316   has_variable_label = read_int (r);
317   missing_value_code = read_int (r);
318   print_format = read_int (r);
319   write_format = read_int (r);
320   read_string (r, name, sizeof name);
321   name[strcspn (name, " ")] = '\0';
322
323   if (width >= 0)
324     r->n_variables++;
325
326   if (r->n_var_widths >= r->allocated_var_widths)
327     r->var_widths = x2nrealloc (r->var_widths, &r->allocated_var_widths,
328                                 sizeof *r->var_widths);
329   r->var_widths[r->n_var_widths++] = width;
330
331   printf ("\tWidth: %d (%s)\n",
332           width,
333           width > 0 ? "string"
334           : width == 0 ? "numeric"
335           : "long string continuation record");
336   printf ("\tVariable label: %d\n", has_variable_label);
337   printf ("\tMissing values code: %d (%s)\n", missing_value_code,
338           (missing_value_code == 0 ? "no missing values"
339            : missing_value_code == 1 ? "one missing value"
340            : missing_value_code == 2 ? "two missing values"
341            : missing_value_code == 3 ? "three missing values"
342            : missing_value_code == -2 ? "one missing value range"
343            : missing_value_code == -3 ? "one missing value, one range"
344            : "bad value"));
345   printf ("\tPrint format: %06x (%s%d.%d)\n",
346           print_format, format_name (print_format),
347           (print_format >> 8) & 0xff, print_format & 0xff);
348   printf ("\tWrite format: %06x (%s%d.%d)\n",
349           write_format, format_name (write_format),
350           (write_format >> 8) & 0xff, write_format & 0xff);
351   printf ("\tName: %s\n", name);
352
353   /* Get variable label, if any. */
354   if (has_variable_label != 0 && has_variable_label != 1)
355     sys_error (r, _("Variable label indicator field is not 0 or 1."));
356   if (has_variable_label == 1)
357     {
358       long int offset = ftell (r->file);
359       size_t len;
360       char label[255 + 1];
361
362       len = read_int (r);
363       if (len >= sizeof label)
364         sys_error (r, _("Variable %s has label of invalid length %zu."),
365                    name, len);
366       read_string (r, label, len + 1);
367       printf("\t%08lx Variable label: \"%s\"\n", offset, label);
368
369       skip_bytes (r, ROUND_UP (len, 4) - len);
370     }
371
372   /* Set missing values. */
373   if (missing_value_code != 0)
374     {
375       int i;
376
377       printf ("\t%08lx Missing values:", ftell (r->file));
378       if (!width)
379         {
380           if (missing_value_code < -3 || missing_value_code > 3
381               || missing_value_code == -1)
382             sys_error (r, _("Numeric missing value indicator field is not "
383                             "-3, -2, 0, 1, 2, or 3."));
384           if (missing_value_code < 0)
385             {
386               double low = read_float (r);
387               double high = read_float (r);
388               printf (" %g...%g", low, high);
389               missing_value_code = -missing_value_code - 2;
390             }
391           for (i = 0; i < missing_value_code; i++)
392             printf (" %g", read_float (r));
393         }
394       else if (width > 0)
395         {
396           if (missing_value_code < 1 || missing_value_code > 3)
397             sys_error (r, _("String missing value indicator field is not "
398                             "0, 1, 2, or 3."));
399           for (i = 0; i < missing_value_code; i++)
400             {
401               char string[9];
402               read_string (r, string, sizeof string);
403               printf (" \"%s\"", string);
404             }
405         }
406       putchar ('\n');
407     }
408 }
409
410 static void
411 print_untyped_value (struct sfm_reader *r, char raw_value[8])
412 {
413   int n_printable;
414   double value;
415
416   value = float_get_double (r->float_format, raw_value);
417   for (n_printable = 0; n_printable < sizeof raw_value; n_printable++)
418     if (!isprint (raw_value[n_printable]))
419       break;
420
421   printf ("%g/\"%.*s\"", value, n_printable, raw_value);
422 }
423
424 /* Reads value labels from sysfile R and inserts them into the
425    associated dictionary. */
426 static void
427 read_value_label_record (struct sfm_reader *r)
428 {
429   int label_cnt, var_cnt;
430   int i;
431
432   printf ("%08lx: value labels record\n", ftell (r->file));
433
434   /* Read number of labels. */
435   label_cnt = read_int (r);
436   for (i = 0; i < label_cnt; i++)
437     {
438       char raw_value[8];
439       unsigned char label_len;
440       size_t padded_len;
441       char label[256];
442
443       read_bytes (r, raw_value, sizeof raw_value);
444
445       /* Read label length. */
446       read_bytes (r, &label_len, sizeof label_len);
447       padded_len = ROUND_UP (label_len + 1, 8);
448
449       /* Read label, padding. */
450       read_bytes (r, label, padded_len - 1);
451       label[label_len] = 0;
452
453       printf ("\t");
454       print_untyped_value (r, raw_value);
455       printf (": \"%s\"\n", label);
456     }
457
458   /* Now, read the type 4 record that has the list of variables
459      to which the value labels are to be applied. */
460
461   /* Read record type of type 4 record. */
462   if (read_int (r) != 4)
463     sys_error (r, _("Variable index record (type 4) does not immediately "
464                     "follow value label record (type 3) as it should."));
465
466   /* Read number of variables associated with value label from type 4
467      record. */
468   printf ("\t%08lx: apply to variables", ftell (r->file));
469   var_cnt = read_int (r);
470   for (i = 0; i < var_cnt; i++)
471     printf (" #%d", read_int (r));
472   putchar ('\n');
473 }
474
475 static void
476 read_document_record (struct sfm_reader *r)
477 {
478   int n_lines;
479   int i;
480
481   printf ("%08lx: document record\n", ftell (r->file));
482   n_lines = read_int (r);
483   printf ("\t%d lines of documents\n", n_lines);
484
485   for (i = 0; i < n_lines; i++)
486     {
487       char line[81];
488       printf ("\t%08lx: ", ftell (r->file));
489       read_string (r, line, sizeof line);
490       trim_spaces (line);
491       printf ("line %d: \"%s\"\n", i, line);
492     }
493 }
494
495 static void
496 read_extension_record (struct sfm_reader *r)
497 {
498   long int offset = ftell (r->file);
499   int subtype = read_int (r);
500   size_t size = read_int (r);
501   size_t count = read_int (r);
502   size_t bytes = size * count;
503
504   printf ("%08lx: Record 7, subtype %d, size=%zu, count=%zu\n",
505           offset, subtype, size, count);
506
507   switch (subtype)
508     {
509     case 3:
510       read_machine_integer_info (r, size, count);
511       return;
512
513     case 4:
514       read_machine_float_info (r, size, count);
515       return;
516
517     case 5:
518       /* Variable sets information.  We don't use these yet.
519          They only apply to GUIs; see VARSETS on the APPLY
520          DICTIONARY command in SPSS documentation. */
521       break;
522
523     case 6:
524       /* DATE variable information.  We don't use it yet, but we
525          should. */
526       break;
527
528     case 7:
529     case 19:
530       read_mrsets (r, size, count);
531       return;
532
533     case 11:
534       read_display_parameters (r, size, count);
535       return;
536
537     case 13:
538       read_long_var_name_map (r, size, count);
539       return;
540
541     case 14:
542       read_long_string_map (r, size, count);
543       return;
544
545     case 16:
546       read_ncases64 (r, size, count);
547       return;
548
549     case 17:
550       read_datafile_attributes (r, size, count);
551       return;
552
553     case 18:
554       read_variable_attributes (r, size, count);
555       return;
556
557     case 20:
558       read_character_encoding (r, size, count);
559       return;
560
561     case 21:
562       read_long_string_value_labels (r, size, count);
563       return;
564
565     default:
566       sys_warn (r, _("Unrecognized record type 7, subtype %d."), subtype);
567       read_unknown_extension (r, size, count);
568       return;
569     }
570
571   skip_bytes (r, bytes);
572 }
573
574 static void
575 read_machine_integer_info (struct sfm_reader *r, size_t size, size_t count)
576 {
577   long int offset = ftell (r->file);
578   int version_major = read_int (r);
579   int version_minor = read_int (r);
580   int version_revision = read_int (r);
581   int machine_code = read_int (r);
582   int float_representation = read_int (r);
583   int compression_code = read_int (r);
584   int integer_representation = read_int (r);
585   int character_code = read_int (r);
586
587   printf ("%08lx: machine integer info\n", offset);
588   if (size != 4 || count != 8)
589     sys_error (r, _("Bad size (%zu) or count (%zu) field on record type 7, "
590                     "subtype 3."),
591                 size, count);
592
593   printf ("\tVersion: %d.%d.%d\n",
594           version_major, version_minor, version_revision);
595   printf ("\tMachine code: %d\n", machine_code);
596   printf ("\tFloating point representation: %d (%s)\n",
597           float_representation,
598           float_representation == 1 ? "IEEE 754"
599           : float_representation == 2 ? "IBM 370"
600           : float_representation == 3 ? "DEC VAX"
601           : "unknown");
602   printf ("\tCompression code: %d\n", compression_code);
603   printf ("\tEndianness: %d (%s)\n", integer_representation,
604           integer_representation == 1 ? "big"
605           : integer_representation == 2 ? "little" : "unknown");
606   printf ("\tCharacter code: %d\n", character_code);
607 }
608
609 /* Read record type 7, subtype 4. */
610 static void
611 read_machine_float_info (struct sfm_reader *r, size_t size, size_t count)
612 {
613   long int offset = ftell (r->file);
614   double sysmis = read_float (r);
615   double highest = read_float (r);
616   double lowest = read_float (r);
617
618   printf ("%08lx: machine float info\n", offset);
619   if (size != 8 || count != 3)
620     sys_error (r, _("Bad size (%zu) or count (%zu) on extension 4."),
621                size, count);
622
623   printf ("\tsysmis: %g\n", sysmis);
624   if (sysmis != SYSMIS)
625     sys_warn (r, _("File specifies unexpected value %g as %s."),
626               sysmis, "SYSMIS");
627
628   printf ("\thighest: %g\n", highest);
629   if (highest != HIGHEST)
630     sys_warn (r, _("File specifies unexpected value %g as %s."),
631               highest, "HIGHEST");
632
633   printf ("\tlowest: %g\n", lowest);
634   if (lowest != LOWEST)
635     sys_warn (r, _("File specifies unexpected value %g as %s."),
636               lowest, "LOWEST");
637 }
638
639 /* Read record type 7, subtype 7. */
640 static void
641 read_mrsets (struct sfm_reader *r, size_t size, size_t count)
642 {
643   struct text_record *text;
644
645   printf ("%08lx: multiple response sets\n", ftell (r->file));
646   text = open_text_record (r, size * count);
647   for (;;)
648     {
649       const char *name;
650       enum { MRSET_MC, MRSET_MD } type;
651       bool cat_label_from_counted_values = false;
652       bool label_from_var_label = false;
653       const char *counted;
654       const char *label;
655       const char *variables;
656
657       name = text_tokenize (text, '=');
658       if (name == NULL)
659         break;
660
661       if (text_match (text, 'C'))
662         {
663           type = MRSET_MC;
664           counted = NULL;
665           if (!text_match (text, ' '))
666             {
667               sys_warn (r, "missing space following 'C' at offset %zu "
668                         "in mrsets record", text_pos (text));
669               break;
670             }
671         }
672       else if (text_match (text, 'D'))
673         {
674           type = MRSET_MD;
675         }
676       else if (text_match (text, 'E'))
677         {
678           char *number;
679
680           type = MRSET_MD;
681           cat_label_from_counted_values = true;
682
683           if (!text_match (text, ' '))
684             {
685               sys_warn (r, _("Missing space following 'E' at offset %zu "
686                              "in MRSETS record"), text_pos (text));
687               break;
688             }
689
690           number = text_tokenize (text, ' ');
691           if (!strcmp (number, "11"))
692             label_from_var_label = true;
693           else if (strcmp (number, "1"))
694             sys_warn (r, _("Unexpected label source value \"%s\" "
695                            "following 'E' at offset %zu in MRSETS record"),
696                       number, text_pos (text));
697
698         }
699       else
700         {
701           sys_warn (r, "missing 'C', 'D', or 'E' at offset %zu "
702                     "in mrsets record", text_pos (text));
703           break;
704         }
705
706       if (type == MRSET_MD)
707         {
708           counted = text_parse_counted_string (text);
709           if (counted == NULL)
710             break;
711         }
712
713       label = text_parse_counted_string (text);
714       if (label == NULL)
715         break;
716
717       variables = text_tokenize (text, '\n');
718       if (variables == NULL)
719         {
720           sys_warn (r, "missing variable names following label "
721                     "at offset %zu in mrsets record", text_pos (text));
722           break;
723         }
724
725       printf ("\t\"%s\": multiple %s set",
726               name, type == MRSET_MC ? "category" : "dichotomy");
727       if (counted != NULL)
728         printf (", counted value \"%s\"", counted);
729       if (cat_label_from_counted_values)
730         printf (", category labels from counted values");
731       if (label[0] != '\0')
732         printf (", label \"%s\"", label);
733       if (label_from_var_label)
734         printf (", label from variable label");
735       printf(", variables \"%s\"\n", variables);
736     }
737   close_text_record (text);
738 }
739
740 /* Read record type 7, subtype 11. */
741 static void
742 read_display_parameters (struct sfm_reader *r, size_t size, size_t count)
743 {
744   size_t n_vars;
745   bool includes_width;
746   size_t i;
747
748   printf ("%08lx: variable display parameters\n", ftell (r->file));
749   if (size != 4)
750     {
751       sys_warn (r, _("Bad size %zu on extension 11."), size);
752       skip_bytes (r, size * count);
753       return;
754     }
755
756   n_vars = r->n_variables;
757   if (count == 3 * n_vars)
758     includes_width = true;
759   else if (count == 2 * n_vars)
760     includes_width = false;
761   else
762     {
763       sys_warn (r, _("Extension 11 has bad count %zu (for %zu variables)."),
764                 count, n_vars);
765       skip_bytes (r, size * count);
766       return;
767     }
768
769   for (i = 0; i < n_vars; ++i)
770     {
771       int measure = read_int (r);
772       int width = includes_width ? read_int (r) : 0;
773       int align = read_int (r);
774
775       printf ("\tVar #%zu: measure=%d (%s)", i, measure,
776               (measure == 1 ? "nominal"
777                : measure == 2 ? "ordinal"
778                : measure == 3 ? "scale"
779                : "invalid"));
780       if (includes_width)
781         printf (", width=%d", width);
782       printf (", align=%d (%s)\n", align,
783               (align == 0 ? "left"
784                : align == 1 ? "right"
785                : align == 2 ? "centre"
786                : "invalid"));
787     }
788 }
789
790 /* Reads record type 7, subtype 13, which gives the long name
791    that corresponds to each short name.  */
792 static void
793 read_long_var_name_map (struct sfm_reader *r, size_t size, size_t count)
794 {
795   struct text_record *text;
796   char *var;
797   char *long_name;
798
799   printf ("%08lx: long variable names (short => long)\n", ftell (r->file));
800   text = open_text_record (r, size * count);
801   while (read_variable_to_value_pair (text, &var, &long_name))
802     printf ("\t%s => %s\n", var, long_name);
803   close_text_record (text);
804 }
805
806 /* Reads record type 7, subtype 14, which gives the real length
807    of each very long string.  Rearranges DICT accordingly. */
808 static void
809 read_long_string_map (struct sfm_reader *r, size_t size, size_t count)
810 {
811   struct text_record *text;
812   char *var;
813   char *length_s;
814
815   printf ("%08lx: very long strings (variable => length)\n", ftell (r->file));
816   text = open_text_record (r, size * count);
817   while (read_variable_to_value_pair (text, &var, &length_s))
818     printf ("\t%s => %d\n", var, atoi (length_s));
819   close_text_record (text);
820 }
821
822 static bool
823 read_attributes (struct sfm_reader *r, struct text_record *text,
824                  const char *variable)
825 {
826   const char *key;
827   int index;
828
829   for (;;) 
830     {
831       key = text_tokenize (text, '(');
832       if (key == NULL)
833         return true;
834   
835       for (index = 1; ; index++)
836         {
837           /* Parse the value. */
838           const char *value = text_tokenize (text, '\n');
839           if (value == NULL) 
840             {
841               sys_warn (r, _("%s: Error parsing attribute value %s[%d]"),
842                         variable, key, index);
843               return false;
844             }
845           if (strlen (value) < 2
846               || value[0] != '\'' || value[strlen (value) - 1] != '\'')
847             sys_warn (r, _("%s: Attribute value %s[%d] is not quoted: %s"),
848                       variable, key, index, value);
849           else
850             printf ("\t%s: %s[%d] = \"%.*s\"\n",
851                     variable, key, index, (int) strlen (value) - 2, value + 1);
852
853           /* Was this the last value for this attribute? */
854           if (text_match (text, ')'))
855             break;
856         }
857
858       if (text_match (text, '/'))
859         return true; 
860     }
861 }
862
863 /* Read extended number of cases record. */
864 static void
865 read_ncases64 (struct sfm_reader *r, size_t size, size_t count)
866 {
867   int64_t unknown, ncases64;
868
869   if (size != 8)
870     {
871       sys_warn (r, _("Bad size %zu for extended number of cases."), size);
872       skip_bytes (r, size * count);
873       return;
874     }
875   if (count != 2)
876     {
877       sys_warn (r, _("Bad count %zu for extended number of cases."), size);
878       skip_bytes (r, size * count);
879       return;
880     }
881   unknown = read_int64 (r);
882   ncases64 = read_int64 (r);
883   printf ("%08lx: extended number of cases: "
884           "unknown=%"PRId64", ncases64=%"PRId64"\n",
885           ftell (r->file), unknown, ncases64);
886 }
887
888 static void
889 read_datafile_attributes (struct sfm_reader *r, size_t size, size_t count) 
890 {
891   struct text_record *text;
892   
893   printf ("%08lx: datafile attributes\n", ftell (r->file));
894   text = open_text_record (r, size * count);
895   read_attributes (r, text, "datafile");
896   close_text_record (text);
897 }
898
899 static void
900 read_character_encoding (struct sfm_reader *r, size_t size, size_t count)
901 {
902   const unsigned long int posn =  ftell (r->file);
903   char *encoding = xcalloc (size, count + 1);
904   read_string (r, encoding, count + 1);
905
906   printf ("%08lx: Character Encoding: %s\n", posn, encoding);
907 }
908
909 static void
910 read_long_string_value_labels (struct sfm_reader *r, size_t size, size_t count)
911 {
912   const long start = ftell (r->file);
913
914   printf ("%08lx: long string value labels\n", start);
915   while (ftell (r->file) - start < size * count)
916     {
917       long posn = ftell (r->file);
918       char var_name[VAR_NAME_LEN + 1];
919       int var_name_len;
920       int n_values;
921       int width;
922       int i;
923
924       /* Read variable name. */
925       var_name_len = read_int (r);
926       if (var_name_len > VAR_NAME_LEN)
927         sys_error (r, _("Variable name length in long string value label "
928                         "record (%d) exceeds %d-byte limit."),
929                    var_name_len, VAR_NAME_LEN);
930       read_string (r, var_name, var_name_len + 1);
931
932       /* Read width, number of values. */
933       width = read_int (r);
934       n_values = read_int (r);
935
936       printf ("\t%08lx: %s, width %d, %d values\n",
937               posn, var_name, width, n_values);
938
939       /* Read values. */
940       for (i = 0; i < n_values; i++)
941         {
942           char *value;
943           int value_length;
944
945           char *label;
946           int label_length;
947
948           posn = ftell (r->file);
949
950           /* Read value. */
951           value_length = read_int (r);
952           value = xmalloc (value_length + 1);
953           read_string (r, value, value_length + 1);
954
955           /* Read label. */
956           label_length = read_int (r);
957           label = xmalloc (label_length + 1);
958           read_string (r, label, label_length + 1);
959
960           printf ("\t\t%08lx: \"%s\" (%d bytes) => \"%s\" (%d bytes)\n",
961                   posn, value, value_length, label, label_length);
962
963           free (value);
964           free (label);
965         }
966     }
967 }
968
969 static void
970 hex_dump (size_t offset, const void *buffer_, size_t buffer_size)
971 {
972   const uint8_t *buffer = buffer_;
973
974   while (buffer_size > 0)
975     {
976       size_t n = MIN (buffer_size, 16);
977       size_t i;
978
979       printf ("%04zx", offset);
980       for (i = 0; i < 16; i++)
981         {
982           if (i < n)
983             printf ("%c%02x", i == 8 ? '-' : ' ', buffer[i]);
984           else
985             printf ("   ");
986         }
987
988       printf (" |");
989       for (i = 0; i < 16; i++)
990         {
991           unsigned char c = i < n ? buffer[i] : ' ';
992           putchar (isprint (c) ? c : '.');
993         }
994       printf ("|\n");
995
996       offset += n;
997       buffer += n;
998       buffer_size -= n;
999     }
1000 }
1001
1002 /* Reads and prints any type 7 record that we don't understand. */
1003 static void
1004 read_unknown_extension (struct sfm_reader *r, size_t size, size_t count)
1005 {
1006   unsigned char *buffer;
1007   size_t i;
1008
1009   if (size == 0 || count > 65536 / size)
1010     skip_bytes (r, size * count);
1011   else if (size != 1)
1012     {
1013       buffer = xmalloc (size);
1014       for (i = 0; i < count; i++)
1015         {
1016           read_bytes (r, buffer, size);
1017           hex_dump (i * size, buffer, size);
1018         }
1019       free (buffer);
1020     }
1021   else
1022     {
1023       buffer = xmalloc (count);
1024       read_bytes (r, buffer, count);
1025       if (memchr (buffer, 0, count) == 0)
1026         for (i = 0; i < count; i++)
1027           {
1028             unsigned char c = buffer[i];
1029
1030             if (c == '\\')
1031               printf ("\\\\");
1032             else if (c == '\n' || isprint (c))
1033               putchar (c);
1034             else
1035               printf ("\\%02x", c);
1036           }
1037       else
1038         hex_dump (0, buffer, count);
1039       free (buffer);
1040     }
1041 }
1042
1043 static void
1044 read_variable_attributes (struct sfm_reader *r, size_t size, size_t count) 
1045 {
1046   struct text_record *text;
1047   
1048   printf ("%08lx: variable attributes\n", ftell (r->file));
1049   text = open_text_record (r, size * count);
1050   for (;;) 
1051     {
1052       const char *variable = text_tokenize (text, ':');
1053       if (variable == NULL || !read_attributes (r, text, variable))
1054         break; 
1055     }
1056   close_text_record (text);
1057 }
1058
1059 static void
1060 read_compressed_data (struct sfm_reader *r)
1061 {
1062   enum { N_OPCODES = 8 };
1063   uint8_t opcodes[N_OPCODES];
1064   long int opcode_ofs;
1065   int opcode_idx;
1066   int case_num;
1067   int i;
1068
1069   read_int (r);
1070   printf ("\n%08lx: compressed data:\n", ftell (r->file));
1071
1072   opcode_idx = N_OPCODES;
1073   case_num = 0;
1074   for (case_num = 0; ; case_num++)
1075     {
1076       printf ("%08lx: case %d's uncompressible data begins\n",
1077               ftell (r->file), case_num);
1078       for (i = 0; i < r->n_var_widths; i++)
1079         {
1080           int width = r->var_widths[i];
1081           char raw_value[8];
1082           int opcode;
1083
1084           if (opcode_idx >= N_OPCODES)
1085             {
1086               opcode_ofs = ftell (r->file);
1087               read_bytes (r, opcodes, 8);
1088               opcode_idx = 0;
1089             }
1090           opcode = opcodes[opcode_idx];
1091           printf ("%08lx: variable %d: opcode %d: ",
1092                   opcode_ofs + opcode_idx, i, opcode);
1093
1094           switch (opcode)
1095             {
1096             default:
1097               printf ("%g", opcode - r->bias);
1098               if (width != 0)
1099                 printf (", but this is a string variable (width=%d)", width);
1100               printf ("\n");
1101               break;
1102
1103             case 252:
1104               printf ("end of data\n");
1105               return;
1106
1107             case 253:
1108               read_bytes (r, raw_value, 8);
1109               printf ("uncompressible data: ");
1110               print_untyped_value (r, raw_value);
1111               printf ("\n");
1112               break;
1113
1114             case 254:
1115               printf ("spaces");
1116               if (width == 0)
1117                 printf (", but this is a numeric variable");
1118               printf ("\n");
1119               break;
1120
1121             case 255:
1122               printf ("SYSMIS");
1123               if (width != 0)
1124                 printf (", but this is a string variable (width=%d)", width);
1125
1126               printf ("\n");
1127               break;
1128             }
1129
1130           opcode_idx++;
1131         }
1132     }
1133 }
1134 \f
1135 /* Helpers for reading records that consist of structured text
1136    strings. */
1137
1138 /* State. */
1139 struct text_record
1140   {
1141     struct sfm_reader *reader;  /* Reader. */
1142     char *buffer;               /* Record contents. */
1143     size_t size;                /* Size of buffer. */
1144     size_t pos;                 /* Current position in buffer. */
1145   };
1146
1147 /* Reads SIZE bytes into a text record for R,
1148    and returns the new text record. */
1149 static struct text_record *
1150 open_text_record (struct sfm_reader *r, size_t size)
1151 {
1152   struct text_record *text = xmalloc (sizeof *text);
1153   char *buffer = xmalloc (size + 1);
1154   read_bytes (r, buffer, size);
1155   buffer[size] = '\0';
1156   text->reader = r;
1157   text->buffer = buffer;
1158   text->size = size;
1159   text->pos = 0;
1160   return text;
1161 }
1162
1163 /* Closes TEXT and frees its storage.
1164    Not really needed, because the pool will free the text record anyway,
1165    but can be used to free it earlier. */
1166 static void
1167 close_text_record (struct text_record *text)
1168 {
1169   free (text->buffer);
1170   free (text);
1171 }
1172
1173 static char *
1174 text_tokenize (struct text_record *text, int delimiter)
1175 {
1176   size_t start = text->pos;
1177   while (text->pos < text->size
1178          && text->buffer[text->pos] != delimiter
1179          && text->buffer[text->pos] != '\0')
1180     text->pos++;
1181   if (text->pos == text->size)
1182     return NULL;
1183   text->buffer[text->pos++] = '\0';
1184   return &text->buffer[start];
1185 }
1186
1187 static bool
1188 text_match (struct text_record *text, int c) 
1189 {
1190   if (text->pos < text->size && text->buffer[text->pos] == c) 
1191     {
1192       text->pos++;
1193       return true;
1194     }
1195   else
1196     return false;
1197 }
1198
1199 /* Reads a integer value expressed in decimal, then a space, then a string that
1200    consists of exactly as many bytes as specified by the integer, then a space,
1201    from TEXT.  Returns the string, null-terminated, as a subset of TEXT's
1202    buffer (so the caller should not free the string). */
1203 static const char *
1204 text_parse_counted_string (struct text_record *text)
1205 {
1206   size_t start;
1207   size_t n;
1208   char *s;
1209
1210   start = text->pos;
1211   n = 0;
1212   while (isdigit ((unsigned char) text->buffer[text->pos]))
1213     n = (n * 10) + (text->buffer[text->pos++] - '0');
1214   if (start == text->pos)
1215     {
1216       sys_error (text->reader, "expecting digit at offset %zu in record",
1217                  text->pos);
1218       return NULL;
1219     }
1220
1221   if (!text_match (text, ' '))
1222     {
1223       sys_error (text->reader, "expecting space at offset %zu in record",
1224                  text->pos);
1225       return NULL;
1226     }
1227
1228   if (text->pos + n > text->size)
1229     {
1230       sys_error (text->reader, "%zu-byte string starting at offset %zu "
1231                  "exceeds record length %zu", n, text->pos, text->size);
1232       return NULL;
1233     }
1234
1235   s = &text->buffer[text->pos];
1236   if (s[n] != ' ')
1237     {
1238       sys_error (text->reader, "expecting space at offset %zu following "
1239                  "%zu-byte string", text->pos + n, n);
1240       return NULL;
1241     }
1242   s[n] = '\0';
1243   text->pos += n + 1;
1244   return s;
1245 }
1246
1247 /* Reads a variable=value pair from TEXT.
1248    Looks up the variable in DICT and stores it into *VAR.
1249    Stores a null-terminated value into *VALUE. */
1250 static bool
1251 read_variable_to_value_pair (struct text_record *text,
1252                              char **key, char **value)
1253 {
1254   *key = text_tokenize (text, '=');
1255   *value = text_tokenize (text, '\t');
1256   if (!*key || !*value)
1257     return false;
1258
1259   while (text->pos < text->size
1260          && (text->buffer[text->pos] == '\t'
1261              || text->buffer[text->pos] == '\0'))
1262     text->pos++;
1263   return true;
1264 }
1265
1266 /* Returns the current byte offset inside the TEXT's string. */
1267 static size_t
1268 text_pos (const struct text_record *text)
1269 {
1270   return text->pos;
1271 }
1272 \f
1273 static void
1274 usage (int exit_code)
1275 {
1276   printf ("usage: %s SYSFILE...\n"
1277           "where each SYSFILE is the name of a system file\n",
1278           program_name);
1279   exit (exit_code);
1280 }
1281
1282 /* Displays a corruption message. */
1283 static void
1284 sys_msg (struct sfm_reader *r, const char *format, va_list args)
1285 {
1286   printf ("\"%s\" near offset 0x%lx: ",
1287           r->file_name, (unsigned long) ftell (r->file));
1288   vprintf (format, args);
1289   putchar ('\n');
1290 }
1291
1292 /* Displays a warning for the current file position. */
1293 static void
1294 sys_warn (struct sfm_reader *r, const char *format, ...)
1295 {
1296   va_list args;
1297
1298   va_start (args, format);
1299   sys_msg (r, format, args);
1300   va_end (args);
1301 }
1302
1303 /* Displays an error for the current file position,
1304    marks it as in an error state,
1305    and aborts reading it using longjmp. */
1306 static void
1307 sys_error (struct sfm_reader *r, const char *format, ...)
1308 {
1309   va_list args;
1310
1311   va_start (args, format);
1312   sys_msg (r, format, args);
1313   va_end (args);
1314
1315   exit (EXIT_FAILURE);
1316 }
1317 \f
1318 /* Reads BYTE_CNT bytes into BUF.
1319    Returns true if exactly BYTE_CNT bytes are successfully read.
1320    Aborts if an I/O error or a partial read occurs.
1321    If EOF_IS_OK, then an immediate end-of-file causes false to be
1322    returned; otherwise, immediate end-of-file causes an abort
1323    too. */
1324 static inline bool
1325 read_bytes_internal (struct sfm_reader *r, bool eof_is_ok,
1326                      void *buf, size_t byte_cnt)
1327 {
1328   size_t bytes_read = fread (buf, 1, byte_cnt, r->file);
1329   if (bytes_read == byte_cnt)
1330     return true;
1331   else if (ferror (r->file))
1332     sys_error (r, _("System error: %s."), strerror (errno));
1333   else if (!eof_is_ok || bytes_read != 0)
1334     sys_error (r, _("Unexpected end of file."));
1335   else
1336     return false;
1337 }
1338
1339 /* Reads BYTE_CNT into BUF.
1340    Aborts upon I/O error or if end-of-file is encountered. */
1341 static void
1342 read_bytes (struct sfm_reader *r, void *buf, size_t byte_cnt)
1343 {
1344   read_bytes_internal (r, false, buf, byte_cnt);
1345 }
1346
1347 /* Reads a 32-bit signed integer from R and returns its value in
1348    host format. */
1349 static int
1350 read_int (struct sfm_reader *r)
1351 {
1352   uint8_t integer[4];
1353   read_bytes (r, integer, sizeof integer);
1354   return integer_get (r->integer_format, integer, sizeof integer);
1355 }
1356
1357 /* Reads a 64-bit signed integer from R and returns its value in
1358    host format. */
1359 static int64_t
1360 read_int64 (struct sfm_reader *r)
1361 {
1362   uint8_t integer[8];
1363   read_bytes (r, integer, sizeof integer);
1364   return integer_get (r->integer_format, integer, sizeof integer);
1365 }
1366
1367 /* Reads a 64-bit floating-point number from R and returns its
1368    value in host format. */
1369 static double
1370 read_float (struct sfm_reader *r)
1371 {
1372   uint8_t number[8];
1373   read_bytes (r, number, sizeof number);
1374   return float_get_double (r->float_format, number);
1375 }
1376
1377 /* Reads exactly SIZE - 1 bytes into BUFFER
1378    and stores a null byte into BUFFER[SIZE - 1]. */
1379 static void
1380 read_string (struct sfm_reader *r, char *buffer, size_t size)
1381 {
1382   assert (size > 0);
1383   read_bytes (r, buffer, size - 1);
1384   buffer[size - 1] = '\0';
1385 }
1386
1387 /* Skips BYTES bytes forward in R. */
1388 static void
1389 skip_bytes (struct sfm_reader *r, size_t bytes)
1390 {
1391   while (bytes > 0)
1392     {
1393       char buffer[1024];
1394       size_t chunk = MIN (sizeof buffer, bytes);
1395       read_bytes (r, buffer, chunk);
1396       bytes -= chunk;
1397     }
1398 }
1399
1400 static void
1401 trim_spaces (char *s)
1402 {
1403   char *end = strchr (s, '\0');
1404   while (end > s && end[-1] == ' ')
1405     end--;
1406   *end = '\0';
1407 }