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