1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
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.
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.
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/>. */
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>
37 #define _(msgid) gettext (msgid)
39 #define VAR_NAME_LEN 64
43 const char *file_name;
46 int n_variable_records, n_variables;
49 size_t n_var_widths, allocated_var_widths;
51 enum integer_format integer_format;
52 enum float_format float_format;
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_display_parameters (struct sfm_reader *,
68 size_t size, size_t count);
69 static void read_long_var_name_map (struct sfm_reader *r,
70 size_t size, size_t count);
71 static void read_long_string_map (struct sfm_reader *r,
72 size_t size, size_t count);
73 static void read_datafile_attributes (struct sfm_reader *r,
74 size_t size, size_t count);
75 static void read_variable_attributes (struct sfm_reader *r,
76 size_t size, size_t count);
77 static void read_ncases64 (struct sfm_reader *, size_t size, size_t count);
78 static void read_character_encoding (struct sfm_reader *r,
79 size_t size, size_t count);
80 static void read_long_string_value_labels (struct sfm_reader *r,
81 size_t size, size_t count);
82 static void read_unknown_extension (struct sfm_reader *,
83 size_t size, size_t count);
84 static void read_compressed_data (struct sfm_reader *);
86 static struct text_record *open_text_record (
87 struct sfm_reader *, size_t size);
88 static void close_text_record (struct text_record *);
89 static bool read_variable_to_value_pair (struct text_record *,
90 char **key, char **value);
91 static char *text_tokenize (struct text_record *, int delimiter);
92 static bool text_match (struct text_record *text, int c);
94 static void usage (int exit_code);
95 static void sys_warn (struct sfm_reader *, const char *, ...)
97 static void sys_error (struct sfm_reader *, const char *, ...)
101 static void read_bytes (struct sfm_reader *, void *, size_t);
102 static int read_int (struct sfm_reader *);
103 static int64_t read_int64 (struct sfm_reader *);
104 static double read_float (struct sfm_reader *);
105 static void read_string (struct sfm_reader *, char *, size_t);
106 static void skip_bytes (struct sfm_reader *, size_t);
107 static void trim_spaces (char *);
110 main (int argc, char *argv[])
115 set_program_name (argv[0]);
117 usage (EXIT_FAILURE);
119 for (i = 1; i < argc; i++)
123 r.file_name = argv[i];
124 r.file = fopen (r.file_name, "rb");
126 error (EXIT_FAILURE, errno, "error opening \"%s\"", r.file_name);
127 r.n_variable_records = 0;
130 r.allocated_var_widths = 0;
132 r.compressed = false;
135 printf ("Reading \"%s\":\n", r.file_name);
138 while ((rec_type = read_int (&r)) != 999)
143 read_variable_record (&r);
147 read_value_label_record (&r);
151 sys_error (&r, _("Misplaced type 4 record."));
154 read_document_record (&r);
158 read_extension_record (&r);
162 sys_error (&r, _("Unrecognized record type %d."), rec_type);
165 printf ("%08lx: end-of-dictionary record "
166 "(first byte of data at %08lx)\n",
167 ftell (r.file), ftell (r.file) + 4);
170 read_compressed_data (&r);
179 read_header (struct sfm_reader *r)
182 char eye_catcher[61];
183 uint8_t raw_layout_code[4];
185 int32_t nominal_case_size;
187 int32_t weight_index;
190 char creation_date[10];
191 char creation_time[9];
194 read_string (r, rec_type, sizeof rec_type);
195 read_string (r, eye_catcher, sizeof eye_catcher);
197 if (strcmp ("$FL2", rec_type) != 0)
198 sys_error (r, _("This is not an SPSS system file."));
200 /* Identify integer format. */
201 read_bytes (r, raw_layout_code, sizeof raw_layout_code);
202 if ((!integer_identify (2, raw_layout_code, sizeof raw_layout_code,
204 && !integer_identify (3, raw_layout_code, sizeof raw_layout_code,
206 || (r->integer_format != INTEGER_MSB_FIRST
207 && r->integer_format != INTEGER_LSB_FIRST))
208 sys_error (r, _("This is not an SPSS system file."));
209 layout_code = integer_get (r->integer_format,
210 raw_layout_code, sizeof raw_layout_code);
212 nominal_case_size = read_int (r);
213 compressed = read_int (r);
214 weight_index = read_int (r);
215 ncases = read_int (r);
217 r->compressed = compressed != 0;
219 /* Identify floating-point format and obtain compression bias. */
220 read_bytes (r, raw_bias, sizeof raw_bias);
221 if (float_identify (100.0, raw_bias, sizeof raw_bias, &r->float_format) == 0)
223 sys_warn (r, _("Compression bias is not the usual "
224 "value of 100, or system file uses unrecognized "
225 "floating-point format."));
226 if (r->integer_format == INTEGER_MSB_FIRST)
227 r->float_format = FLOAT_IEEE_DOUBLE_BE;
229 r->float_format = FLOAT_IEEE_DOUBLE_LE;
231 r->bias = float_get_double (r->float_format, raw_bias);
233 read_string (r, creation_date, sizeof creation_date);
234 read_string (r, creation_time, sizeof creation_time);
235 read_string (r, file_label, sizeof file_label);
236 trim_spaces (file_label);
239 printf ("File header record:\n");
240 printf ("\t%17s: %s\n", "Product name", eye_catcher);
241 printf ("\t%17s: %"PRId32"\n", "Layout code", layout_code);
242 printf ("\t%17s: %"PRId32"\n", "Compressed", compressed);
243 printf ("\t%17s: %"PRId32"\n", "Weight index", weight_index);
244 printf ("\t%17s: %"PRId32"\n", "Number of cases", ncases);
245 printf ("\t%17s: %g\n", "Compression bias", r->bias);
246 printf ("\t%17s: %s\n", "Creation date", creation_date);
247 printf ("\t%17s: %s\n", "Creation time", creation_time);
248 printf ("\t%17s: \"%s\"\n", "File label", file_label);
252 format_name (int format)
254 switch ((format >> 16) & 0xff)
257 case 2: return "AHEX";
258 case 3: return "COMMA";
259 case 4: return "DOLLAR";
262 case 7: return "PIBHEX";
264 case 9: return "PIB";
265 case 10: return "PK";
266 case 11: return "RB";
267 case 12: return "RBHEX";
271 case 20: return "DATE";
272 case 21: return "TIME";
273 case 22: return "DATETIME";
274 case 23: return "ADATE";
275 case 24: return "JDATE";
276 case 25: return "DTIME";
277 case 26: return "WKDAY";
278 case 27: return "MONTH";
279 case 28: return "MOYR";
280 case 29: return "QYR";
281 case 30: return "WKYR";
282 case 31: return "PCT";
283 case 32: return "DOT";
284 case 33: return "CCA";
285 case 34: return "CCB";
286 case 35: return "CCC";
287 case 36: return "CCD";
288 case 37: return "CCE";
289 case 38: return "EDATE";
290 case 39: return "SDATE";
291 default: return "invalid";
295 /* Reads a variable (type 2) record from R and adds the
296 corresponding variable to DICT.
297 Also skips past additional variable records for long string
300 read_variable_record (struct sfm_reader *r)
303 int has_variable_label;
304 int missing_value_code;
309 printf ("%08lx: variable record #%d\n",
310 ftell (r->file), r->n_variable_records++);
312 width = read_int (r);
313 has_variable_label = read_int (r);
314 missing_value_code = read_int (r);
315 print_format = read_int (r);
316 write_format = read_int (r);
317 read_string (r, name, sizeof name);
318 name[strcspn (name, " ")] = '\0';
323 if (r->n_var_widths >= r->allocated_var_widths)
324 r->var_widths = x2nrealloc (r->var_widths, &r->allocated_var_widths,
325 sizeof *r->var_widths);
326 r->var_widths[r->n_var_widths++] = width;
328 printf ("\tWidth: %d (%s)\n",
331 : width == 0 ? "numeric"
332 : "long string continuation record");
333 printf ("\tVariable label: %d\n", has_variable_label);
334 printf ("\tMissing values code: %d (%s)\n", missing_value_code,
335 (missing_value_code == 0 ? "no missing values"
336 : missing_value_code == 1 ? "one missing value"
337 : missing_value_code == 2 ? "two missing values"
338 : missing_value_code == 3 ? "three missing values"
339 : missing_value_code == -2 ? "one missing value range"
340 : missing_value_code == -3 ? "one missing value, one range"
342 printf ("\tPrint format: %06x (%s%d.%d)\n",
343 print_format, format_name (print_format),
344 (print_format >> 8) & 0xff, print_format & 0xff);
345 printf ("\tWrite format: %06x (%s%d.%d)\n",
346 write_format, format_name (write_format),
347 (write_format >> 8) & 0xff, write_format & 0xff);
348 printf ("\tName: %s\n", name);
350 /* Get variable label, if any. */
351 if (has_variable_label != 0 && has_variable_label != 1)
352 sys_error (r, _("Variable label indicator field is not 0 or 1."));
353 if (has_variable_label == 1)
355 long int offset = ftell (r->file);
360 if (len >= sizeof label)
361 sys_error (r, _("Variable %s has label of invalid length %zu."),
363 read_string (r, label, len + 1);
364 printf("\t%08lx Variable label: \"%s\"\n", offset, label);
366 skip_bytes (r, ROUND_UP (len, 4) - len);
369 /* Set missing values. */
370 if (missing_value_code != 0)
374 printf ("\t%08lx Missing values:", ftell (r->file));
377 if (missing_value_code < -3 || missing_value_code > 3
378 || missing_value_code == -1)
379 sys_error (r, _("Numeric missing value indicator field is not "
380 "-3, -2, 0, 1, 2, or 3."));
381 if (missing_value_code < 0)
383 double low = read_float (r);
384 double high = read_float (r);
385 printf (" %g...%g", low, high);
386 missing_value_code = -missing_value_code - 2;
388 for (i = 0; i < missing_value_code; i++)
389 printf (" %g", read_float (r));
393 if (missing_value_code < 1 || missing_value_code > 3)
394 sys_error (r, _("String missing value indicator field is not "
396 for (i = 0; i < missing_value_code; i++)
399 read_string (r, string, sizeof string);
400 printf (" \"%s\"", string);
408 print_untyped_value (struct sfm_reader *r, char raw_value[8])
413 value = float_get_double (r->float_format, raw_value);
414 for (n_printable = 0; n_printable < sizeof raw_value; n_printable++)
415 if (!isprint (raw_value[n_printable]))
418 printf ("%g/\"%.*s\"", value, n_printable, raw_value);
421 /* Reads value labels from sysfile R and inserts them into the
422 associated dictionary. */
424 read_value_label_record (struct sfm_reader *r)
426 int label_cnt, var_cnt;
429 printf ("%08lx: value labels record\n", ftell (r->file));
431 /* Read number of labels. */
432 label_cnt = read_int (r);
433 for (i = 0; i < label_cnt; i++)
436 unsigned char label_len;
440 read_bytes (r, raw_value, sizeof raw_value);
442 /* Read label length. */
443 read_bytes (r, &label_len, sizeof label_len);
444 padded_len = ROUND_UP (label_len + 1, 8);
446 /* Read label, padding. */
447 read_bytes (r, label, padded_len - 1);
448 label[label_len] = 0;
451 print_untyped_value (r, raw_value);
452 printf (": \"%s\"\n", label);
455 /* Now, read the type 4 record that has the list of variables
456 to which the value labels are to be applied. */
458 /* Read record type of type 4 record. */
459 if (read_int (r) != 4)
460 sys_error (r, _("Variable index record (type 4) does not immediately "
461 "follow value label record (type 3) as it should."));
463 /* Read number of variables associated with value label from type 4
465 printf ("\t%08lx: apply to variables", ftell (r->file));
466 var_cnt = read_int (r);
467 for (i = 0; i < var_cnt; i++)
468 printf (" #%d", read_int (r));
473 read_document_record (struct sfm_reader *r)
478 printf ("%08lx: document record\n", ftell (r->file));
479 n_lines = read_int (r);
480 printf ("\t%d lines of documents\n", n_lines);
482 for (i = 0; i < n_lines; i++)
485 printf ("\t%08lx: ", ftell (r->file));
486 read_string (r, line, sizeof line);
488 printf ("line %d: \"%s\"\n", i, line);
493 read_extension_record (struct sfm_reader *r)
495 long int offset = ftell (r->file);
496 int subtype = read_int (r);
497 size_t size = read_int (r);
498 size_t count = read_int (r);
499 size_t bytes = size * count;
501 printf ("%08lx: Record 7, subtype %d, size=%zu, count=%zu\n",
502 offset, subtype, size, count);
507 read_machine_integer_info (r, size, count);
511 read_machine_float_info (r, size, count);
515 /* Variable sets information. We don't use these yet.
516 They only apply to GUIs; see VARSETS on the APPLY
517 DICTIONARY command in SPSS documentation. */
521 /* DATE variable information. We don't use it yet, but we
526 /* Unknown purpose. */
530 read_display_parameters (r, size, count);
534 read_long_var_name_map (r, size, count);
538 read_long_string_map (r, size, count);
542 read_ncases64 (r, size, count);
546 read_datafile_attributes (r, size, count);
550 read_variable_attributes (r, size, count);
554 read_character_encoding (r, size, count);
558 read_long_string_value_labels (r, size, count);
562 sys_warn (r, _("Unrecognized record type 7, subtype %d."), subtype);
563 read_unknown_extension (r, size, count);
567 skip_bytes (r, bytes);
571 read_machine_integer_info (struct sfm_reader *r, size_t size, size_t count)
573 long int offset = ftell (r->file);
574 int version_major = read_int (r);
575 int version_minor = read_int (r);
576 int version_revision = read_int (r);
577 int machine_code = read_int (r);
578 int float_representation = read_int (r);
579 int compression_code = read_int (r);
580 int integer_representation = read_int (r);
581 int character_code = read_int (r);
583 printf ("%08lx: machine integer info\n", offset);
584 if (size != 4 || count != 8)
585 sys_error (r, _("Bad size (%zu) or count (%zu) field on record type 7, "
589 printf ("\tVersion: %d.%d.%d\n",
590 version_major, version_minor, version_revision);
591 printf ("\tMachine code: %d\n", machine_code);
592 printf ("\tFloating point representation: %d (%s)\n",
593 float_representation,
594 float_representation == 1 ? "IEEE 754"
595 : float_representation == 2 ? "IBM 370"
596 : float_representation == 3 ? "DEC VAX"
598 printf ("\tCompression code: %d\n", compression_code);
599 printf ("\tEndianness: %d (%s)\n", integer_representation,
600 integer_representation == 1 ? "big"
601 : integer_representation == 2 ? "little" : "unknown");
602 printf ("\tCharacter code: %d\n", character_code);
605 /* Read record type 7, subtype 4. */
607 read_machine_float_info (struct sfm_reader *r, size_t size, size_t count)
609 long int offset = ftell (r->file);
610 double sysmis = read_float (r);
611 double highest = read_float (r);
612 double lowest = read_float (r);
614 printf ("%08lx: machine float info\n", offset);
615 if (size != 8 || count != 3)
616 sys_error (r, _("Bad size (%zu) or count (%zu) on extension 4."),
619 printf ("\tsysmis: %g\n", sysmis);
620 if (sysmis != SYSMIS)
621 sys_warn (r, _("File specifies unexpected value %g as %s."),
624 printf ("\thighest: %g\n", highest);
625 if (highest != HIGHEST)
626 sys_warn (r, _("File specifies unexpected value %g as %s."),
629 printf ("\tlowest: %g\n", lowest);
630 if (lowest != LOWEST)
631 sys_warn (r, _("File specifies unexpected value %g as %s."),
635 /* Read record type 7, subtype 11. */
637 read_display_parameters (struct sfm_reader *r, size_t size, size_t count)
643 printf ("%08lx: variable display parameters\n", ftell (r->file));
646 sys_warn (r, _("Bad size %zu on extension 11."), size);
647 skip_bytes (r, size * count);
651 n_vars = r->n_variables;
652 if (count == 3 * n_vars)
653 includes_width = true;
654 else if (count == 2 * n_vars)
655 includes_width = false;
658 sys_warn (r, _("Extension 11 has bad count %zu (for %zu variables)."),
660 skip_bytes (r, size * count);
664 for (i = 0; i < n_vars; ++i)
666 int measure = read_int (r);
667 int width = includes_width ? read_int (r) : 0;
668 int align = read_int (r);
670 printf ("\tVar #%zu: measure=%d (%s)", i, measure,
671 (measure == 1 ? "nominal"
672 : measure == 2 ? "ordinal"
673 : measure == 3 ? "scale"
676 printf (", width=%d", width);
677 printf (", align=%d (%s)\n", align,
679 : align == 1 ? "right"
680 : align == 2 ? "centre"
685 /* Reads record type 7, subtype 13, which gives the long name
686 that corresponds to each short name. */
688 read_long_var_name_map (struct sfm_reader *r, size_t size, size_t count)
690 struct text_record *text;
694 printf ("%08lx: long variable names (short => long)\n", ftell (r->file));
695 text = open_text_record (r, size * count);
696 while (read_variable_to_value_pair (text, &var, &long_name))
697 printf ("\t%s => %s\n", var, long_name);
698 close_text_record (text);
701 /* Reads record type 7, subtype 14, which gives the real length
702 of each very long string. Rearranges DICT accordingly. */
704 read_long_string_map (struct sfm_reader *r, size_t size, size_t count)
706 struct text_record *text;
710 printf ("%08lx: very long strings (variable => length)\n", ftell (r->file));
711 text = open_text_record (r, size * count);
712 while (read_variable_to_value_pair (text, &var, &length_s))
713 printf ("\t%s => %d\n", var, atoi (length_s));
714 close_text_record (text);
718 read_attributes (struct sfm_reader *r, struct text_record *text,
719 const char *variable)
726 key = text_tokenize (text, '(');
730 for (index = 1; ; index++)
732 /* Parse the value. */
733 const char *value = text_tokenize (text, '\n');
736 sys_warn (r, _("%s: Error parsing attribute value %s[%d]"),
737 variable, key, index);
740 if (strlen (value) < 2
741 || value[0] != '\'' || value[strlen (value) - 1] != '\'')
742 sys_warn (r, _("%s: Attribute value %s[%d] is not quoted: %s"),
743 variable, key, index, value);
745 printf ("\t%s: %s[%d] = \"%.*s\"\n",
746 variable, key, index, (int) strlen (value) - 2, value + 1);
748 /* Was this the last value for this attribute? */
749 if (text_match (text, ')'))
753 if (text_match (text, '/'))
758 /* Read extended number of cases record. */
760 read_ncases64 (struct sfm_reader *r, size_t size, size_t count)
762 int64_t unknown, ncases64;
766 sys_warn (r, _("Bad size %zu for extended number of cases."), size);
767 skip_bytes (r, size * count);
772 sys_warn (r, _("Bad count %zu for extended number of cases."), size);
773 skip_bytes (r, size * count);
776 unknown = read_int64 (r);
777 ncases64 = read_int64 (r);
778 printf ("%08lx: extended number of cases: "
779 "unknown=%"PRId64", ncases64=%"PRId64"\n",
780 ftell (r->file), unknown, ncases64);
784 read_datafile_attributes (struct sfm_reader *r, size_t size, size_t count)
786 struct text_record *text;
788 printf ("%08lx: datafile attributes\n", ftell (r->file));
789 text = open_text_record (r, size * count);
790 read_attributes (r, text, "datafile");
791 close_text_record (text);
795 read_character_encoding (struct sfm_reader *r, size_t size, size_t count)
797 const unsigned long int posn = ftell (r->file);
798 char *encoding = xcalloc (size, count + 1);
799 read_string (r, encoding, count + 1);
801 printf ("%08lx: Character Encoding: %s\n", posn, encoding);
805 read_long_string_value_labels (struct sfm_reader *r, size_t size, size_t count)
807 const long start = ftell (r->file);
809 printf ("%08lx: long string value labels\n", start);
810 while (ftell (r->file) - start < size * count)
812 long posn = ftell (r->file);
813 char var_name[VAR_NAME_LEN + 1];
819 /* Read variable name. */
820 var_name_len = read_int (r);
821 if (var_name_len > VAR_NAME_LEN)
822 sys_error (r, _("Variable name length in long string value label "
823 "record (%d) exceeds %d-byte limit."),
824 var_name_len, VAR_NAME_LEN);
825 read_string (r, var_name, var_name_len + 1);
827 /* Read width, number of values. */
828 width = read_int (r);
829 n_values = read_int (r);
831 printf ("\t%08lx: %s, width %d, %d values\n",
832 posn, var_name, width, n_values);
835 for (i = 0; i < n_values; i++)
843 posn = ftell (r->file);
846 value_length = read_int (r);
847 value = xmalloc (value_length + 1);
848 read_string (r, value, value_length + 1);
851 label_length = read_int (r);
852 label = xmalloc (label_length + 1);
853 read_string (r, label, label_length + 1);
855 printf ("\t\t%08lx: \"%s\" (%d bytes) => \"%s\" (%d bytes)\n",
856 posn, value, value_length, label, label_length);
865 hex_dump (size_t offset, const void *buffer_, size_t buffer_size)
867 const uint8_t *buffer = buffer_;
869 while (buffer_size > 0)
871 size_t n = MIN (buffer_size, 16);
874 printf ("%04zx", offset);
875 for (i = 0; i < 16; i++)
878 printf ("%c%02x", i == 8 ? '-' : ' ', buffer[i]);
884 for (i = 0; i < 16; i++)
886 unsigned char c = i < n ? buffer[i] : ' ';
887 putchar (isprint (c) ? c : '.');
897 /* Reads and prints any type 7 record that we don't understand. */
899 read_unknown_extension (struct sfm_reader *r, size_t size, size_t count)
901 unsigned char *buffer;
904 if (size == 0 || count > 65536 / size)
905 skip_bytes (r, size * count);
908 buffer = xmalloc (size);
909 for (i = 0; i < count; i++)
911 read_bytes (r, buffer, size);
912 hex_dump (i * size, buffer, size);
918 buffer = xmalloc (count);
919 read_bytes (r, buffer, count);
920 if (memchr (buffer, 0, count) == 0)
921 for (i = 0; i < count; i++)
923 unsigned char c = buffer[i];
927 else if (c == '\n' || isprint (c))
930 printf ("\\%02x", c);
933 hex_dump (0, buffer, count);
939 read_variable_attributes (struct sfm_reader *r, size_t size, size_t count)
941 struct text_record *text;
943 printf ("%08lx: variable attributes\n", ftell (r->file));
944 text = open_text_record (r, size * count);
947 const char *variable = text_tokenize (text, ':');
948 if (variable == NULL || !read_attributes (r, text, variable))
951 close_text_record (text);
955 read_compressed_data (struct sfm_reader *r)
957 enum { N_OPCODES = 8 };
958 uint8_t opcodes[N_OPCODES];
965 printf ("\n%08lx: compressed data:\n", ftell (r->file));
967 opcode_idx = N_OPCODES;
969 for (case_num = 0; ; case_num++)
971 printf ("%08lx: case %d's uncompressible data begins\n",
972 ftell (r->file), case_num);
973 for (i = 0; i < r->n_var_widths; i++)
975 int width = r->var_widths[i];
979 if (opcode_idx >= N_OPCODES)
981 opcode_ofs = ftell (r->file);
982 read_bytes (r, opcodes, 8);
985 opcode = opcodes[opcode_idx];
986 printf ("%08lx: variable %d: opcode %d: ",
987 opcode_ofs + opcode_idx, i, opcode);
992 printf ("%g", opcode - r->bias);
994 printf (", but this is a string variable (width=%d)", width);
999 printf ("end of data\n");
1003 read_bytes (r, raw_value, 8);
1004 printf ("uncompressible data: ");
1005 print_untyped_value (r, raw_value);
1012 printf (", but this is a numeric variable");
1019 printf (", but this is a string variable (width=%d)", width);
1030 /* Helpers for reading records that consist of structured text
1036 char *buffer; /* Record contents. */
1037 size_t size; /* Size of buffer. */
1038 size_t pos; /* Current position in buffer. */
1041 /* Reads SIZE bytes into a text record for R,
1042 and returns the new text record. */
1043 static struct text_record *
1044 open_text_record (struct sfm_reader *r, size_t size)
1046 struct text_record *text = xmalloc (sizeof *text);
1047 char *buffer = xmalloc (size + 1);
1048 read_bytes (r, buffer, size);
1049 text->buffer = buffer;
1055 /* Closes TEXT and frees its storage.
1056 Not really needed, because the pool will free the text record anyway,
1057 but can be used to free it earlier. */
1059 close_text_record (struct text_record *text)
1061 free (text->buffer);
1066 text_tokenize (struct text_record *text, int delimiter)
1068 size_t start = text->pos;
1069 while (text->pos < text->size
1070 && text->buffer[text->pos] != delimiter
1071 && text->buffer[text->pos] != '\0')
1073 if (text->pos == text->size)
1075 text->buffer[text->pos++] = '\0';
1076 return &text->buffer[start];
1080 text_match (struct text_record *text, int c)
1082 if (text->pos < text->size && text->buffer[text->pos] == c)
1091 /* Reads a variable=value pair from TEXT.
1092 Looks up the variable in DICT and stores it into *VAR.
1093 Stores a null-terminated value into *VALUE. */
1095 read_variable_to_value_pair (struct text_record *text,
1096 char **key, char **value)
1098 *key = text_tokenize (text, '=');
1099 *value = text_tokenize (text, '\t');
1100 if (!*key || !*value)
1103 while (text->pos < text->size
1104 && (text->buffer[text->pos] == '\t'
1105 || text->buffer[text->pos] == '\0'))
1111 usage (int exit_code)
1113 printf ("usage: %s SYSFILE...\n"
1114 "where each SYSFILE is the name of a system file\n",
1119 /* Displays a corruption message. */
1121 sys_msg (struct sfm_reader *r, const char *format, va_list args)
1123 printf ("\"%s\" near offset 0x%lx: ",
1124 r->file_name, (unsigned long) ftell (r->file));
1125 vprintf (format, args);
1129 /* Displays a warning for the current file position. */
1131 sys_warn (struct sfm_reader *r, const char *format, ...)
1135 va_start (args, format);
1136 sys_msg (r, format, args);
1140 /* Displays an error for the current file position,
1141 marks it as in an error state,
1142 and aborts reading it using longjmp. */
1144 sys_error (struct sfm_reader *r, const char *format, ...)
1148 va_start (args, format);
1149 sys_msg (r, format, args);
1152 exit (EXIT_FAILURE);
1155 /* Reads BYTE_CNT bytes into BUF.
1156 Returns true if exactly BYTE_CNT bytes are successfully read.
1157 Aborts if an I/O error or a partial read occurs.
1158 If EOF_IS_OK, then an immediate end-of-file causes false to be
1159 returned; otherwise, immediate end-of-file causes an abort
1162 read_bytes_internal (struct sfm_reader *r, bool eof_is_ok,
1163 void *buf, size_t byte_cnt)
1165 size_t bytes_read = fread (buf, 1, byte_cnt, r->file);
1166 if (bytes_read == byte_cnt)
1168 else if (ferror (r->file))
1169 sys_error (r, _("System error: %s."), strerror (errno));
1170 else if (!eof_is_ok || bytes_read != 0)
1171 sys_error (r, _("Unexpected end of file."));
1176 /* Reads BYTE_CNT into BUF.
1177 Aborts upon I/O error or if end-of-file is encountered. */
1179 read_bytes (struct sfm_reader *r, void *buf, size_t byte_cnt)
1181 read_bytes_internal (r, false, buf, byte_cnt);
1184 /* Reads a 32-bit signed integer from R and returns its value in
1187 read_int (struct sfm_reader *r)
1190 read_bytes (r, integer, sizeof integer);
1191 return integer_get (r->integer_format, integer, sizeof integer);
1194 /* Reads a 64-bit signed integer from R and returns its value in
1197 read_int64 (struct sfm_reader *r)
1200 read_bytes (r, integer, sizeof integer);
1201 return integer_get (r->integer_format, integer, sizeof integer);
1204 /* Reads a 64-bit floating-point number from R and returns its
1205 value in host format. */
1207 read_float (struct sfm_reader *r)
1210 read_bytes (r, number, sizeof number);
1211 return float_get_double (r->float_format, number);
1214 /* Reads exactly SIZE - 1 bytes into BUFFER
1215 and stores a null byte into BUFFER[SIZE - 1]. */
1217 read_string (struct sfm_reader *r, char *buffer, size_t size)
1220 read_bytes (r, buffer, size - 1);
1221 buffer[size - 1] = '\0';
1224 /* Skips BYTES bytes forward in R. */
1226 skip_bytes (struct sfm_reader *r, size_t bytes)
1231 size_t chunk = MIN (sizeof buffer, bytes);
1232 read_bytes (r, buffer, chunk);
1238 trim_spaces (char *s)
1240 char *end = strchr (s, '\0');
1241 while (end > s && end[-1] == ' ')