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