sys-file-reader: Break reading a system file into two stages.
[pspp] / src / language / dictionary / sys-file-info.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2012, 2013, 2014 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 <float.h>
21 #include <stdlib.h>
22
23 #include "data/attributes.h"
24 #include "data/casereader.h"
25 #include "data/dataset.h"
26 #include "data/dictionary.h"
27 #include "data/file-handle-def.h"
28 #include "data/format.h"
29 #include "data/missing-values.h"
30 #include "data/sys-file-reader.h"
31 #include "data/value-labels.h"
32 #include "data/variable.h"
33 #include "data/vector.h"
34 #include "language/command.h"
35 #include "language/data-io/file-handle.h"
36 #include "language/lexer/lexer.h"
37 #include "language/lexer/variable-parser.h"
38 #include "libpspp/array.h"
39 #include "libpspp/message.h"
40 #include "libpspp/misc.h"
41 #include "libpspp/string-array.h"
42 #include "output/tab.h"
43
44 #include "gl/minmax.h"
45 #include "gl/xalloc.h"
46
47 #include "gettext.h"
48 #define _(msgid) gettext (msgid)
49
50 /* Information to include in displaying a dictionary. */
51 enum 
52   {
53     DF_DICT_INDEX       = 1 << 0,
54     DF_FORMATS          = 1 << 1,
55     DF_VALUE_LABELS     = 1 << 2,
56     DF_VARIABLE_LABELS  = 1 << 3,
57     DF_MISSING_VALUES   = 1 << 4,
58     DF_AT_ATTRIBUTES    = 1 << 5, /* Attributes whose names begin with @. */
59     DF_ATTRIBUTES       = 1 << 6, /* All other attributes. */
60     DF_MISC             = 1 << 7,
61     DF_ALL              = (1 << 8) - 1
62   };
63
64 static int describe_variable (const struct variable *v, struct tab_table *t,
65                               int r, int pc, int flags);
66
67 /* SYSFILE INFO utility. */
68 int
69 cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED)
70 {
71   struct sfm_reader *sfm_reader;
72   struct file_handle *h;
73   struct dictionary *d;
74   struct tab_table *t;
75   struct casereader *reader;
76   struct sfm_read_info info;
77   char *encoding;
78   int r, i;
79
80   h = NULL;
81   encoding = NULL;
82   for (;;)
83     {
84       lex_match (lexer, T_SLASH);
85
86       if (lex_match_id (lexer, "FILE") || lex_is_string (lexer))
87         {
88           lex_match (lexer, T_EQUALS);
89
90           fh_unref (h);
91           h = fh_parse (lexer, FH_REF_FILE, NULL);
92           if (h == NULL)
93             goto error;
94         }
95       else if (lex_match_id (lexer, "ENCODING"))
96         {
97           lex_match (lexer, T_EQUALS);
98
99           if (!lex_force_string (lexer))
100             goto error;
101
102           free (encoding);
103           encoding = ss_xstrdup (lex_tokss (lexer));
104
105           lex_get (lexer);
106         }
107       else
108         break;
109     }
110
111   if (h == NULL)
112     {
113       lex_sbc_missing ("FILE");
114       goto error;
115     }
116
117   sfm_reader = sfm_open (h);
118   if (sfm_reader == NULL)
119     goto error;
120
121   reader = sfm_decode (sfm_reader, encoding, &d, &info);
122   if (!reader)
123     goto error;
124
125   casereader_destroy (reader);
126
127   t = tab_create (2, 11 + (info.product_ext != NULL));
128   r = 0;
129   tab_vline (t, TAL_GAP, 1, 0, 8);
130
131   tab_text (t, 0, r, TAB_LEFT, _("File:"));
132   tab_text (t, 1, r++, TAB_LEFT, fh_get_file_name (h));
133
134   tab_text (t, 0, r, TAB_LEFT, _("Label:"));
135   {
136     const char *label = dict_get_label (d);
137     if (label == NULL)
138       label = _("No label.");
139     tab_text (t, 1, r++, TAB_LEFT, label);
140   }
141
142   tab_text (t, 0, r, TAB_LEFT, _("Created:"));
143   tab_text_format (t, 1, r++, TAB_LEFT, "%s %s by %s",
144                    info.creation_date, info.creation_time, info.product);
145
146   if (info.product_ext)
147     {
148       tab_text (t, 0, r, TAB_LEFT, _("Product:"));
149       tab_text (t, 1, r++, TAB_LEFT, info.product_ext);
150     }
151
152   tab_text (t, 0, r, TAB_LEFT, _("Integer Format:"));
153   tab_text (t, 1, r++, TAB_LEFT,
154             info.integer_format == INTEGER_MSB_FIRST ? _("Big Endian")
155             : info.integer_format == INTEGER_LSB_FIRST ? _("Little Endian")
156             : _("Unknown"));
157
158   tab_text (t, 0, r, TAB_LEFT, _("Real Format:"));
159   tab_text (t, 1, r++, TAB_LEFT,
160             info.float_format == FLOAT_IEEE_DOUBLE_LE ? _("IEEE 754 LE.")
161             : info.float_format == FLOAT_IEEE_DOUBLE_BE ? _("IEEE 754 BE.")
162             : info.float_format == FLOAT_VAX_D ? _("VAX D.")
163             : info.float_format == FLOAT_VAX_G ? _("VAX G.")
164             : info.float_format == FLOAT_Z_LONG ? _("IBM 390 Hex Long.")
165             : _("Unknown"));
166
167   tab_text (t, 0, r, TAB_LEFT, _("Variables:"));
168   tab_text_format (t, 1, r++, TAB_LEFT, "%zu", dict_get_var_cnt (d));
169
170   tab_text (t, 0, r, TAB_LEFT, _("Cases:"));
171   if (info.case_cnt == -1)
172     tab_text (t, 1, r, TAB_LEFT, _("Unknown"));
173   else
174     tab_text_format (t, 1, r, TAB_LEFT, "%ld", (long int) info.case_cnt);
175   r++;
176
177   tab_text (t, 0, r, TAB_LEFT, _("Type:"));
178   tab_text (t, 1, r++, TAB_LEFT, _("System File"));
179
180   tab_text (t, 0, r, TAB_LEFT, _("Weight:"));
181   {
182     struct variable *weight_var = dict_get_weight (d);
183     tab_text (t, 1, r++, TAB_LEFT,
184               (weight_var != NULL
185                ? var_get_name (weight_var) : _("Not weighted.")));
186   }
187
188   tab_text (t, 0, r, TAB_LEFT, _("Compression:"));
189   tab_text_format (t, 1, r++, TAB_LEFT,
190                    info.compression == SFM_COMP_NONE ? _("None")
191                    : info.compression == SFM_COMP_SIMPLE ? "SAV"
192                    : "ZSAV");
193
194   tab_text (t, 0, r, TAB_LEFT, _("Charset:"));
195   tab_text (t, 1, r++, TAB_LEFT, dict_get_encoding (d));
196
197   tab_submit (t);
198
199   t = tab_create (4, 1 + 2 * dict_get_var_cnt (d));
200   tab_headers (t, 0, 0, 1, 0);
201   tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
202   tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
203   tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
204   tab_hline (t, TAL_2, 0, 3, 1);
205   for (r = 1, i = 0; i < dict_get_var_cnt (d); i++)
206     r = describe_variable (dict_get_var (d, i), t, r, 3,
207                            DF_ALL & ~DF_AT_ATTRIBUTES);
208
209   tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, r);
210   tab_vline (t, TAL_1, 1, 0, r);
211   tab_vline (t, TAL_1, 3, 0, r);
212
213   tab_resize (t, -1, r);
214   tab_submit (t);
215
216   dict_destroy (d);
217
218   fh_unref (h);
219   sfm_read_info_destroy (&info);
220   return CMD_SUCCESS;
221
222 error:
223   fh_unref (h);
224   free (encoding);
225   return CMD_FAILURE;
226 }
227 \f
228 /* DISPLAY utility. */
229
230 static void display_macros (void);
231 static void display_documents (const struct dictionary *dict);
232 static void display_variables (const struct variable **, size_t, int);
233 static void display_vectors (const struct dictionary *dict, int sorted);
234 static void display_data_file_attributes (struct attrset *, int flags);
235
236 int
237 cmd_display (struct lexer *lexer, struct dataset *ds)
238 {
239   /* Whether to sort the list of variables alphabetically. */
240   int sorted;
241
242   /* Variables to display. */
243   size_t n;
244   const struct variable **vl;
245
246   if (lex_match_id (lexer, "MACROS"))
247     display_macros ();
248   else if (lex_match_id (lexer, "DOCUMENTS"))
249     display_documents (dataset_dict (ds));
250   else if (lex_match_id (lexer, "FILE"))
251     {
252       if (!lex_force_match_id (lexer, "LABEL"))
253         return CMD_FAILURE;
254       if (dict_get_label (dataset_dict (ds)) == NULL)
255         tab_output_text (TAB_LEFT,
256                          _("The active dataset does not have a file label."));
257       else
258         tab_output_text_format (TAB_LEFT, _("File label: %s"),
259                                 dict_get_label (dataset_dict (ds)));
260     }
261   else
262     {
263       int flags;
264
265       sorted = lex_match_id (lexer, "SORTED");
266
267       if (lex_match_id (lexer, "VECTORS"))
268         {
269           display_vectors (dataset_dict(ds), sorted);
270           return CMD_SUCCESS;
271         }
272       else if (lex_match_id (lexer, "SCRATCH")) 
273         {
274           dict_get_vars (dataset_dict (ds), &vl, &n, DC_ORDINARY);
275           flags = 0;
276         }
277       else 
278         {
279           struct subcommand 
280             {
281               const char *name;
282               int flags;
283             };
284           static const struct subcommand subcommands[] = 
285             {
286               {"@ATTRIBUTES", DF_ATTRIBUTES | DF_AT_ATTRIBUTES},
287               {"ATTRIBUTES", DF_ATTRIBUTES},
288               {"DICTIONARY", DF_ALL & ~DF_AT_ATTRIBUTES},
289               {"INDEX", DF_DICT_INDEX},
290               {"LABELS", DF_DICT_INDEX | DF_VARIABLE_LABELS},
291               {"NAMES", 0},
292               {"VARIABLES",
293                DF_DICT_INDEX | DF_FORMATS | DF_MISSING_VALUES | DF_MISC},
294               {NULL, 0},
295             };
296           const struct subcommand *sbc;
297
298           flags = 0;
299           for (sbc = subcommands; sbc->name != NULL; sbc++)
300             if (lex_match_id (lexer, sbc->name))
301               {
302                 flags = sbc->flags;
303                 break;
304               }
305
306           lex_match (lexer, T_SLASH);
307           lex_match_id (lexer, "VARIABLES");
308           lex_match (lexer, T_EQUALS);
309
310           if (lex_token (lexer) != T_ENDCMD)
311             {
312               if (!parse_variables_const (lexer, dataset_dict (ds), &vl, &n,
313                                           PV_NONE))
314                 {
315                   free (vl);
316                   return CMD_FAILURE;
317                 }
318             }
319           else
320             dict_get_vars (dataset_dict (ds), &vl, &n, 0);
321         }
322
323       if (n > 0) 
324         {
325           sort (vl, n, sizeof *vl,
326                 (sorted
327                  ? compare_var_ptrs_by_name
328                  : compare_var_ptrs_by_dict_index), NULL);
329           display_variables (vl, n, flags);
330         }
331       else
332         msg (SW, _("No variables to display."));
333       free (vl);
334
335       if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES))
336         display_data_file_attributes (dict_get_attributes (dataset_dict (ds)),
337                                       flags);
338     }
339
340   return CMD_SUCCESS;
341 }
342
343 static void
344 display_macros (void)
345 {
346   tab_output_text (TAB_LEFT, _("Macros not supported."));
347 }
348
349 static void
350 display_documents (const struct dictionary *dict)
351 {
352   const struct string_array *documents = dict_get_documents (dict);
353
354   if (string_array_is_empty (documents))
355     tab_output_text (TAB_LEFT, _("The active dataset dictionary does not "
356                                  "contain any documents."));
357   else
358     {
359       size_t i;
360
361       tab_output_text (TAB_LEFT | TAT_TITLE,
362                        _("Documents in the active dataset:"));
363       for (i = 0; i < dict_get_document_line_cnt (dict); i++)
364         tab_output_text (TAB_LEFT | TAB_FIX, dict_get_document_line (dict, i));
365     }
366 }
367
368 static void
369 display_variables (const struct variable **vl, size_t n, int flags)
370 {
371   struct tab_table *t;
372   int nc;                       /* Number of columns. */
373   int pc;                       /* `Position column' */
374   int r;                        /* Current row. */
375   size_t i;
376
377   /* One column for the name,
378      two columns for general description,
379      one column for dictionary index. */
380   nc = 1;
381   if (flags & ~DF_DICT_INDEX)
382     nc += 2;
383   pc = nc;
384   if (flags & DF_DICT_INDEX)
385     nc++;
386
387   t = tab_create (nc, n + 5);
388   tab_headers (t, 0, 0, 1, 0);
389   tab_hline (t, TAL_2, 0, nc - 1, 1);
390   tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
391   if (flags & ~DF_DICT_INDEX) 
392     tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE,
393                     (flags & ~(DF_DICT_INDEX | DF_VARIABLE_LABELS)
394                      ? _("Description") : _("Label")));
395   if (flags & DF_DICT_INDEX)
396     tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
397
398   r = 1;
399   for (i = 0; i < n; i++)
400     r = describe_variable (vl[i], t, r, pc, flags);
401   tab_hline (t, flags & ~DF_DICT_INDEX ? TAL_2 : TAL_1, 0, nc - 1, 1);
402   if (flags)
403     {
404       tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, nc - 1, r - 1);
405       tab_vline (t, TAL_1, 1, 0, r - 1);
406     }
407   if (flags & ~DF_DICT_INDEX)
408     tab_vline (t, TAL_1, nc - 1, 0, r - 1);
409   tab_resize (t, -1, r);
410   tab_submit (t);
411 }
412 \f
413 static bool
414 is_at_name (const char *name) 
415 {
416   return name[0] == '@' || (name[0] == '$' && name[1] == '@');
417 }
418
419 static size_t
420 count_attributes (const struct attrset *set, int flags) 
421 {
422   struct attrset_iterator i;
423   struct attribute *attr;
424   size_t n_attrs;
425   
426   n_attrs = 0;
427   for (attr = attrset_first (set, &i); attr != NULL;
428        attr = attrset_next (set, &i)) 
429     if (flags & DF_AT_ATTRIBUTES || !is_at_name (attribute_get_name (attr)))
430       n_attrs += attribute_get_n_values (attr);
431   return n_attrs;
432 }
433
434 static void
435 display_attributes (struct tab_table *t, const struct attrset *set, int flags,
436                     int c, int r)
437 {
438   struct attribute **attrs;
439   size_t n_attrs;
440   size_t i;
441
442   n_attrs = attrset_count (set);
443   attrs = attrset_sorted (set);
444   for (i = 0; i < n_attrs; i++)
445     {
446       const struct attribute *attr = attrs[i];
447       const char *name = attribute_get_name (attr);
448       size_t n_values;
449       size_t j;
450
451       if (!(flags & DF_AT_ATTRIBUTES) && is_at_name (name))
452         continue;
453
454       n_values = attribute_get_n_values (attr);
455       for (j = 0; j < n_values; j++)
456         {
457           if (n_values > 1)
458             tab_text_format (t, c, r, TAB_LEFT, "%s[%zu]", name, j + 1);
459           else
460             tab_text (t, c, r, TAB_LEFT, name);
461           tab_text (t, c + 1, r, TAB_LEFT, attribute_get_value (attr, j));
462           r++;
463         }
464     }
465   free (attrs);
466 }
467
468 static void
469 display_data_file_attributes (struct attrset *set, int flags) 
470 {
471   struct tab_table *t;
472   size_t n_attrs;
473
474   n_attrs = count_attributes (set, flags);
475   if (!n_attrs)
476     return;
477
478   t = tab_create (2, n_attrs + 1);
479   tab_headers (t, 0, 0, 1, 0);
480   tab_box (t, TAL_1, TAL_1, -1, TAL_1, 0, 0, tab_nc (t) - 1, tab_nr (t) - 1);
481   tab_hline (t, TAL_2, 0, 1, 1); 
482   tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Attribute"));
483   tab_text (t, 1, 0, TAB_LEFT | TAT_TITLE, _("Value"));
484   display_attributes (t, set, flags, 0, 1);
485   tab_title (t, "Custom data file attributes.");
486   tab_submit (t);
487 }
488
489 /* Puts a description of variable V into table T starting at row
490    R.  The variable will be described in the format given by
491    FLAGS.  Returns the next row available for use in the
492    table. */
493 static int
494 describe_variable (const struct variable *v, struct tab_table *t, int r,
495                    int pc, int flags)
496 {
497   size_t n_attrs = 0;
498   int need_rows;
499
500   /* Make sure that enough rows are allocated. */
501   need_rows = 1;
502   if (flags & ~(DF_DICT_INDEX | DF_VARIABLE_LABELS))
503     need_rows += 16;
504   if (flags & DF_VALUE_LABELS)
505     need_rows += val_labs_count (var_get_value_labels (v));
506   if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES))
507     {
508       n_attrs = count_attributes (var_get_attributes (v), flags);
509       need_rows += n_attrs; 
510     }
511   if (r + need_rows > tab_nr (t))
512     {
513       int nr = MAX (r + need_rows, tab_nr (t) * 2);
514       tab_realloc (t, -1, nr);
515     }
516
517   /* Put the name, var label, and position into the first row. */
518   tab_text (t, 0, r, TAB_LEFT, var_get_name (v));
519   if (flags & DF_DICT_INDEX)
520     tab_text_format (t, pc, r, 0, "%zu", var_get_dict_index (v) + 1);
521
522   if (flags & DF_VARIABLE_LABELS && var_has_label (v))
523     {
524       if (flags & ~(DF_DICT_INDEX | DF_VARIABLE_LABELS))
525         tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
526                                _("Label: %s"), var_get_label (v));
527       else
528         tab_joint_text (t, 1, r, 2, r, TAB_LEFT, var_get_label (v));
529       r++;
530     }
531
532   /* Print/write format, or print and write formats. */
533   if (flags & DF_FORMATS) 
534     {
535       const struct fmt_spec *print = var_get_print_format (v);
536       const struct fmt_spec *write = var_get_write_format (v);
537
538       if (fmt_equal (print, write))
539         {
540           char str[FMT_STRING_LEN_MAX + 1];
541           tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
542                                  _("Format: %s"), fmt_to_string (print, str));
543           r++;
544         }
545       else
546         {
547           char str[FMT_STRING_LEN_MAX + 1];
548           tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
549                                  _("Print Format: %s"),
550                                  fmt_to_string (print, str));
551           r++;
552           tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
553                                  _("Write Format: %s"),
554                                  fmt_to_string (write, str));
555           r++;
556         }
557     }
558   
559   /* Measurement level, role, display width, alignment. */
560   if (flags & DF_MISC) 
561     {
562       enum var_role role = var_get_role (v);
563
564       tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
565                              _("Measure: %s"),
566                              measure_to_string (var_get_measure (v)));
567       r++;
568
569       if (role != ROLE_INPUT)
570         {
571           tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
572                                  _("Role: %s"), var_role_to_string (role));
573           r++;
574         }
575
576       tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
577                              _("Display Alignment: %s"),
578                              alignment_to_string (var_get_alignment (v)));
579       r++;
580
581       tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
582                              _("Display Width: %d"),
583                              var_get_display_width (v));
584       r++;
585     }
586   
587   /* Missing values if any. */
588   if (flags & DF_MISSING_VALUES && var_has_missing_values (v))
589     {
590       const struct missing_values *mv = var_get_missing_values (v);
591       char buf[128];
592       char *cp;
593       int cnt = 0;
594       int i;
595
596       cp = stpcpy (buf, _("Missing Values: "));
597
598       if (mv_has_range (mv))
599         {
600           double x, y;
601           mv_get_range (mv, &x, &y);
602           if (x == LOWEST)
603             cp += sprintf (cp, "LOWEST THRU %.*g", DBL_DIG + 1, y);
604           else if (y == HIGHEST)
605             cp += sprintf (cp, "%.*g THRU HIGHEST", DBL_DIG + 1, x);
606           else
607             cp += sprintf (cp, "%.*g THRU %.*g",
608                            DBL_DIG + 1, x,
609                            DBL_DIG + 1, y);
610           cnt++;
611         }
612       for (i = 0; i < mv_n_values (mv); i++)
613         {
614           const union value *value = mv_get_value (mv, i);
615           if (cnt++ > 0)
616             cp += sprintf (cp, "; ");
617           if (var_is_numeric (v))
618             cp += sprintf (cp, "%.*g", DBL_DIG + 1, value->f);
619           else
620             {
621               int width = var_get_width (v);
622               int mv_width = MIN (width, MV_MAX_STRING);
623
624               *cp++ = '"';
625               memcpy (cp, value_str (value, width), mv_width);
626               cp += mv_width;
627               *cp++ = '"';
628               *cp = '\0';
629             }
630         }
631
632       tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
633       r++;
634     }
635
636   /* Value labels. */
637   if (flags & DF_VALUE_LABELS && var_has_value_labels (v))
638     {
639       const struct val_labs *val_labs = var_get_value_labels (v);
640       size_t n_labels = val_labs_count (val_labs);
641       const struct val_lab **labels;
642       int orig_r = r;
643       size_t i;
644
645 #if 0
646       tab_text (t, 1, r, TAB_LEFT, _("Value"));
647       tab_text (t, 2, r, TAB_LEFT, _("Label"));
648       r++;
649 #endif
650
651       tab_hline (t, TAL_1, 1, 2, r);
652
653       labels = val_labs_sorted (val_labs);
654       for (i = 0; i < n_labels; i++)
655         {
656           const struct val_lab *vl = labels[i];
657
658           tab_value (t, 1, r, TAB_NONE, &vl->value, v, NULL);
659           tab_text (t, 2, r, TAB_LEFT, val_lab_get_escaped_label (vl));
660           r++;
661         }
662       free (labels);
663
664       tab_vline (t, TAL_1, 2, orig_r, r - 1);
665     }
666
667   if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES) && n_attrs)
668     {
669       tab_joint_text (t, 1, r, 2, r, TAB_LEFT, "Custom attributes:");
670       r++;
671
672       display_attributes (t, var_get_attributes (v), flags, 1, r);
673       r += n_attrs;
674     }
675
676   /* Draw a line below the last row of information on this variable. */
677   tab_hline (t, TAL_1, 0, tab_nc (t) - 1, r);
678
679   return r;
680 }
681
682 /* Display a list of vectors.  If SORTED is nonzero then they are
683    sorted alphabetically. */
684 static void
685 display_vectors (const struct dictionary *dict, int sorted)
686 {
687   const struct vector **vl;
688   int i;
689   struct tab_table *t;
690   size_t nvec;
691   size_t nrow;
692   size_t row;
693
694   nvec = dict_get_vector_cnt (dict);
695   if (nvec == 0)
696     {
697       msg (SW, _("No vectors defined."));
698       return;
699     }
700
701   vl = xnmalloc (nvec, sizeof *vl);
702   nrow = 0;
703   for (i = 0; i < nvec; i++)
704     {
705       vl[i] = dict_get_vector (dict, i);
706       nrow += vector_get_var_cnt (vl[i]);
707     }
708   if (sorted)
709     qsort (vl, nvec, sizeof *vl, compare_vector_ptrs_by_name);
710
711   t = tab_create (4, nrow + 1);
712   tab_headers (t, 0, 0, 1, 0);
713   tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, nrow);
714   tab_box (t, -1, -1, -1, TAL_1, 0, 0, 3, nrow);
715   tab_hline (t, TAL_2, 0, 3, 1);
716   tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
717   tab_text (t, 1, 0, TAT_TITLE | TAB_LEFT, _("Position"));
718   tab_text (t, 2, 0, TAT_TITLE | TAB_LEFT, _("Variable"));
719   tab_text (t, 3, 0, TAT_TITLE | TAB_LEFT, _("Print Format"));
720
721   row = 1;
722   for (i = 0; i < nvec; i++)
723     {
724       const struct vector *vec = vl[i];
725       size_t j;
726
727       tab_joint_text (t, 0, row, 0, row + vector_get_var_cnt (vec) - 1,
728                       TAB_LEFT, vector_get_name (vl[i]));
729
730       for (j = 0; j < vector_get_var_cnt (vec); j++)
731         {
732           struct variable *var = vector_get_var (vec, j);
733           char fmt_string[FMT_STRING_LEN_MAX + 1];
734           fmt_to_string (var_get_print_format (var), fmt_string);
735
736           tab_text_format (t, 1, row, TAB_RIGHT, "%zu", j + 1);
737           tab_text (t, 2, row, TAB_LEFT, var_get_name (var));
738           tab_text (t, 3, row, TAB_LEFT, fmt_string);
739           row++;
740         }
741       tab_hline (t, TAL_1, 0, 3, row);
742     }
743
744   tab_submit (t);
745
746   free (vl);
747 }