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