posix-xprintf-functions.patch from patch #6230.
[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/casereader.h>
23 #include <data/dictionary.h>
24 #include <data/file-handle-def.h>
25 #include <data/format.h>
26 #include <data/missing-values.h>
27 #include <data/procedure.h>
28 #include <data/sys-file-reader.h>
29 #include <data/value-labels.h>
30 #include <data/variable.h>
31 #include <data/vector.h>
32 #include <language/command.h>
33 #include <language/data-io/file-handle.h>
34 #include <language/lexer/lexer.h>
35 #include <language/lexer/variable-parser.h>
36 #include <libpspp/array.h>
37 #include <libpspp/hash.h>
38 #include <libpspp/message.h>
39 #include <libpspp/message.h>
40 #include <libpspp/misc.h>
41 #include <output/manager.h>
42 #include <output/output.h>
43 #include <output/table.h>
44
45 #include "minmax.h"
46 #include "xalloc.h"
47
48 #include "gettext.h"
49 #define _(msgid) gettext (msgid)
50
51 /* Constants for DISPLAY utility. */
52 enum
53   {
54     AS_NAMES = 0,
55     AS_INDEX,
56     AS_VARIABLES,
57     AS_LABELS,
58     AS_DICTIONARY,
59     AS_SCRATCH,
60     AS_VECTOR
61   };
62
63 static int describe_variable (const struct variable *v, struct tab_table *t, int r, int as);
64
65 /* Sets the widths of all the columns and heights of all the rows in
66    table T for driver D. */
67 static void
68 sysfile_info_dim (struct tab_table *t, struct outp_driver *d)
69 {
70   static const int max[] = {20, 5, 35, 3, 0};
71   const int *p;
72   int i;
73
74   for (p = max; *p; p++)
75     t->w[p - max] = MIN (tab_natural_width (t, d, p - max),
76                          *p * d->prop_em_width);
77   for (i = 0; i < t->nr; i++)
78     t->h[i] = tab_natural_height (t, d, i);
79 }
80
81 /* SYSFILE INFO utility. */
82 int
83 cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED)
84 {
85   struct file_handle *h;
86   struct dictionary *d;
87   struct tab_table *t;
88   struct casereader *reader;
89   struct sfm_read_info info;
90   int r, nr;
91   int i;
92
93   lex_match_id (lexer, "FILE");
94   lex_match (lexer, '=');
95
96   h = fh_parse (lexer, FH_REF_FILE);
97   if (!h)
98     return CMD_FAILURE;
99
100   reader = sfm_open_reader (h, &d, &info);
101   if (!reader)
102     return CMD_FAILURE;
103   casereader_destroy (reader);
104
105   t = tab_create (2, 10, 0);
106   tab_vline (t, TAL_GAP, 1, 0, 8);
107   tab_text (t, 0, 0, TAB_LEFT, _("File:"));
108   tab_text (t, 1, 0, TAB_LEFT, fh_get_file_name (h));
109   tab_text (t, 0, 1, TAB_LEFT, _("Label:"));
110   {
111     const char *label = dict_get_label (d);
112     if (label == NULL)
113       label = _("No label.");
114     tab_text (t, 1, 1, TAB_LEFT, label);
115   }
116   tab_text (t, 0, 2, TAB_LEFT, _("Created:"));
117   tab_text (t, 1, 2, TAB_LEFT | TAT_PRINTF, "%s %s by %s",
118                 info.creation_date, info.creation_time, info.product);
119   tab_text (t, 0, 3, TAB_LEFT, _("Integer Format:"));
120   tab_text (t, 1, 3, TAB_LEFT,
121             info.integer_format == INTEGER_MSB_FIRST ? _("Big Endian.")
122             : info.integer_format == INTEGER_LSB_FIRST ? _("Little Endian.")
123             : _("Unknown."));
124   tab_text (t, 0, 4, TAB_LEFT, _("Real Format:"));
125   tab_text (t, 1, 4, TAB_LEFT,
126             info.float_format == FLOAT_IEEE_DOUBLE_LE ? _("IEEE 754 LE.")
127             : info.float_format == FLOAT_IEEE_DOUBLE_BE ? _("IEEE 754 BE.")
128             : info.float_format == FLOAT_VAX_D ? _("VAX D.")
129             : info.float_format == FLOAT_VAX_G ? _("VAX G.")
130             : info.float_format == FLOAT_Z_LONG ? _("IBM 390 Hex Long.")
131             : _("Unknown."));
132   tab_text (t, 0, 5, TAB_LEFT, _("Variables:"));
133   tab_text (t, 1, 5, TAB_LEFT | TAT_PRINTF, "%zu", dict_get_var_cnt (d));
134   tab_text (t, 0, 6, TAB_LEFT, _("Cases:"));
135   tab_text (t, 1, 6, TAB_LEFT | TAT_PRINTF,
136             info.case_cnt == -1 ? _("Unknown") : "%ld",
137             (long int) info.case_cnt);
138   tab_text (t, 0, 7, TAB_LEFT, _("Type:"));
139   tab_text (t, 1, 7, TAB_LEFT, _("System File."));
140   tab_text (t, 0, 8, TAB_LEFT, _("Weight:"));
141   {
142     struct variable *weight_var = dict_get_weight (d);
143     tab_text (t, 1, 8, TAB_LEFT,
144               (weight_var != NULL
145                ? var_get_name (weight_var) : _("Not weighted.")));
146   }
147   tab_text (t, 0, 9, TAB_LEFT, _("Mode:"));
148   tab_text (t, 1, 9, TAB_LEFT | TAT_PRINTF,
149                 _("Compression %s."), info.compressed ? _("on") : _("off"));
150   tab_dim (t, tab_natural_dimensions);
151   tab_submit (t);
152
153   nr = 1 + 2 * dict_get_var_cnt (d);
154
155   t = tab_create (4, nr, 1);
156   tab_dim (t, sysfile_info_dim);
157   tab_headers (t, 0, 0, 1, 0);
158   tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
159   tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
160   tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
161   tab_hline (t, TAL_2, 0, 3, 1);
162   for (r = 1, i = 0; i < dict_get_var_cnt (d); i++)
163     {
164       struct variable *v = dict_get_var (d, i);
165       const int nvl = val_labs_count (var_get_value_labels (v));
166
167       if (r + 13 + nvl > nr)
168         {
169           nr = MAX (nr * dict_get_var_cnt (d) / (i + 1), nr);
170           nr += 10 + nvl;
171           tab_realloc (t, 4, nr);
172         }
173
174       r = describe_variable (v, t, r, AS_DICTIONARY);
175     }
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   return lex_end_of_command (lexer);
188 }
189 \f
190 /* DISPLAY utility. */
191
192 static void display_macros (void);
193 static void display_documents (const struct dictionary *dict);
194 static void display_variables (const struct variable **, size_t, int);
195 static void display_vectors (const struct dictionary *dict, int sorted);
196
197 int
198 cmd_display (struct lexer *lexer, struct dataset *ds)
199 {
200   /* Whether to sort the list of variables alphabetically. */
201   int sorted;
202
203   /* Variables to display. */
204   size_t n;
205   const struct variable **vl;
206
207   if (lex_match_id (lexer, "MACROS"))
208     display_macros ();
209   else if (lex_match_id (lexer, "DOCUMENTS"))
210     display_documents (dataset_dict (ds));
211   else if (lex_match_id (lexer, "FILE"))
212     {
213       som_blank_line ();
214       if (!lex_force_match_id (lexer, "LABEL"))
215         return CMD_FAILURE;
216       if (dict_get_label (dataset_dict (ds)) == NULL)
217         tab_output_text (TAB_LEFT,
218                          _("The active file does not have a file label."));
219       else
220         {
221           tab_output_text (TAB_LEFT | TAT_TITLE, _("File label:"));
222           tab_output_text (TAB_LEFT | TAB_FIX, dict_get_label (dataset_dict (ds)));
223         }
224     }
225   else
226     {
227       static const char *sbc[] =
228         {"NAMES", "INDEX", "VARIABLES", "LABELS",
229          "DICTIONARY", "SCRATCH", "VECTORS", NULL};
230       const char **cp;
231       int as;
232
233       sorted = lex_match_id (lexer, "SORTED");
234
235       for (cp = sbc; *cp; cp++)
236         if (lex_token (lexer) == T_ID
237             && lex_id_match (ss_cstr (*cp), ss_cstr (lex_tokid (lexer))))
238           {
239             lex_get (lexer);
240             break;
241           }
242       as = cp - sbc;
243
244       if (*cp == NULL)
245         as = AS_NAMES;
246
247       if (as == AS_VECTOR)
248         {
249           display_vectors (dataset_dict(ds), sorted);
250           return CMD_SUCCESS;
251         }
252
253       lex_match (lexer, '/');
254       lex_match_id (lexer, "VARIABLES");
255       lex_match (lexer, '=');
256
257       if (lex_token (lexer) != '.')
258         {
259           if (!parse_variables_const (lexer, dataset_dict (ds), &vl, &n, PV_NONE))
260             {
261               free (vl);
262               return CMD_FAILURE;
263             }
264           as = AS_DICTIONARY;
265         }
266       else
267         dict_get_vars (dataset_dict (ds), &vl, &n, 0);
268
269       if (as == AS_SCRATCH)
270         {
271           size_t i, m;
272           for (i = 0, m = n; i < n; i++)
273             if (dict_class_from_id (var_get_name (vl[i])) != DC_SCRATCH)
274               {
275                 vl[i] = NULL;
276                 m--;
277               }
278           as = AS_NAMES;
279           n = m;
280         }
281
282       if (n == 0)
283         {
284           msg (SW, _("No variables to display."));
285           return CMD_FAILURE;
286         }
287
288       if (sorted)
289         sort (vl, n, sizeof *vl, compare_var_ptrs_by_name, NULL);
290
291       display_variables (vl, n, as);
292
293       free (vl);
294     }
295
296   return lex_end_of_command (lexer);
297 }
298
299 static void
300 display_macros (void)
301 {
302   som_blank_line ();
303   tab_output_text (TAB_LEFT, _("Macros not supported."));
304 }
305
306 static void
307 display_documents (const struct dictionary *dict)
308 {
309   const char *documents = dict_get_documents (dict);
310
311   som_blank_line ();
312   if (documents == NULL)
313     tab_output_text (TAB_LEFT, _("The active file dictionary does not "
314                                  "contain any documents."));
315   else
316     {
317       struct string line = DS_EMPTY_INITIALIZER;
318       size_t i;
319
320       tab_output_text (TAB_LEFT | TAT_TITLE,
321                        _("Documents in the active file:"));
322       som_blank_line ();
323       for (i = 0; i < dict_get_document_line_cnt (dict); i++)
324         {
325           dict_get_document_line (dict, i, &line);
326           tab_output_text (TAB_LEFT | TAB_FIX | TAT_NOWRAP, ds_cstr (&line));
327         }
328       ds_destroy (&line);
329     }
330 }
331
332 static int _as;
333
334 /* Sets the widths of all the columns and heights of all the rows in
335    table T for driver D. */
336 static void
337 variables_dim (struct tab_table *t, struct outp_driver *d)
338 {
339   int pc;
340   int i;
341
342   t->w[0] = tab_natural_width (t, d, 0);
343   if (_as == AS_DICTIONARY || _as == AS_VARIABLES || _as == AS_LABELS)
344     {
345       t->w[1] = MAX (tab_natural_width (t, d, 1), d->prop_em_width * 5);
346       t->w[2] = MAX (tab_natural_width (t, d, 2), d->prop_em_width * 35);
347       pc = 3;
348     }
349   else pc = 1;
350   if (_as != AS_NAMES)
351     t->w[pc] = tab_natural_width (t, d, pc);
352
353   for (i = 0; i < t->nr; i++)
354     t->h[i] = tab_natural_height (t, d, i);
355 }
356
357 static void
358 display_variables (const struct variable **vl, size_t n, int as)
359 {
360   const struct variable **vp = vl;      /* Variable pointer. */
361   struct tab_table *t;
362   int nc;                       /* Number of columns. */
363   int nr;                       /* Number of rows. */
364   int pc;                       /* `Position column' */
365   int r;                        /* Current row. */
366   size_t i;
367
368   _as = as;
369   switch (as)
370     {
371     case AS_INDEX:
372       nc = 2;
373       break;
374     case AS_NAMES:
375       nc = 1;
376       break;
377     default:
378       nc = 4;
379       break;
380     }
381
382   t = tab_create (nc, n + 5, 1);
383   tab_headers (t, 0, 0, 1, 0);
384   nr = n + 5;
385   tab_hline (t, TAL_2, 0, nc - 1, 1);
386   tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
387   pc = (as == AS_INDEX ? 1 : 3);
388   if (as != AS_NAMES)
389     tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
390   if (as == AS_DICTIONARY || as == AS_VARIABLES)
391     tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
392   else if (as == AS_LABELS)
393     tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Label"));
394   tab_dim (t, variables_dim);
395
396   for (i = r = 1; i <= n; i++)
397     {
398       const struct variable *v;
399
400       while (*vp == NULL)
401         vp++;
402       v = *vp++;
403
404       if (as == AS_DICTIONARY || as == AS_VARIABLES)
405         {
406           int nvl = val_labs_count (var_get_value_labels (v));
407
408           if (r + 13 + nvl > nr)
409             {
410               nr = MAX (nr * n / (i + 1), nr);
411               nr += 10 + nvl;
412               tab_realloc (t, nc, nr);
413             }
414
415           r = describe_variable (v, t, r, as);
416         } else {
417           tab_text (t, 0, r, TAB_LEFT, var_get_name (v));
418           if (as == AS_LABELS)
419             {
420               const char *label = var_get_label (v);
421               tab_joint_text (t, 1, r, 2, r, TAB_LEFT,
422                               label != NULL ? "(no label)" : label);
423             }
424           if (as != AS_NAMES)
425             {
426               tab_text (t, pc, r, TAT_PRINTF, "%zu",
427                         var_get_dict_index (v) + 1);
428               tab_hline (t, TAL_1, 0, nc - 1, r);
429             }
430           r++;
431         }
432     }
433   tab_hline (t, as == AS_NAMES ? TAL_1 : TAL_2, 0, nc - 1, 1);
434   if (as != AS_NAMES)
435     {
436       tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, nc - 1, r - 1);
437       tab_vline (t, TAL_1, 1, 0, r - 1);
438     }
439   else
440     tab_flags (t, SOMF_NO_TITLE);
441   if (as == AS_DICTIONARY || as == AS_VARIABLES || as == AS_LABELS)
442     tab_vline (t, TAL_1, 3, 0, r - 1);
443   tab_resize (t, -1, r);
444   tab_columns (t, TAB_COL_DOWN, 1);
445   tab_submit (t);
446 }
447 \f
448 /* Puts a description of variable V into table T starting at row R.
449    The variable will be described in the format AS.  Returns the next
450    row available for use in the table. */
451 static int
452 describe_variable (const struct variable *v, struct tab_table *t, int r, int as)
453 {
454   const struct fmt_spec *print = var_get_print_format (v);
455   const struct fmt_spec *write = var_get_write_format (v);
456   enum measure m = var_get_measure (v);
457   enum alignment a = var_get_alignment (v);
458
459   /* Put the name, var label, and position into the first row. */
460   tab_text (t, 0, r, TAB_LEFT, var_get_name (v));
461   tab_text (t, 3, r, TAT_PRINTF, "%zu", var_get_dict_index (v) + 1);
462
463   if (as == AS_DICTIONARY && var_has_label (v))
464     {
465       tab_joint_text (t, 1, r, 2, r, TAB_LEFT, var_get_label (v));
466       r++;
467     }
468
469   /* Print/write format, or print and write formats. */
470   if (fmt_equal (print, write))
471     {
472       char str[FMT_STRING_LEN_MAX + 1];
473       tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF, _("Format: %s"),
474                       fmt_to_string (print, str));
475       r++;
476     }
477   else
478     {
479       char str[FMT_STRING_LEN_MAX + 1];
480       tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
481                       _("Print Format: %s"), fmt_to_string (print, str));
482       r++;
483       tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
484                       _("Write Format: %s"), fmt_to_string (write, str));
485       r++;
486     }
487
488   /* Measurement level, display width, alignment. */
489   tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
490                   _("Measure: %s"),
491                   m == MEASURE_NOMINAL ? _("Nominal")
492                   : m == MEASURE_ORDINAL ? _("Ordinal")
493                   : _("Scale"));
494   r++;
495   tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
496                   _("Display Alignment: %s"),
497                   a == ALIGN_LEFT ? _("Left")
498                   : a == ALIGN_CENTRE ? _("Center")
499                   : _("Right"));
500   r++;
501   tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
502                   _("Display Width: %d"), var_get_display_width (v));
503   r++;
504
505   /* Missing values if any. */
506   if (var_has_missing_values (v))
507     {
508       char buf[128];
509       char *cp;
510       struct missing_values mv;
511       int cnt = 0;
512
513       cp = stpcpy (buf, _("Missing Values: "));
514
515       mv_copy (&mv, var_get_missing_values (v));
516       if (mv_has_range (&mv))
517         {
518           double x, y;
519           mv_pop_range (&mv, &x, &y);
520           if (x == LOWEST)
521             cp += sprintf (cp, "LOWEST THRU %g", y);
522           else if (y == HIGHEST)
523             cp += sprintf (cp, "%g THRU HIGHEST", x);
524           else
525             cp += sprintf (cp, "%g THRU %g", x, y);
526           cnt++;
527         }
528       while (mv_has_value (&mv))
529         {
530           union value value;
531           mv_pop_value (&mv, &value);
532           if (cnt++ > 0)
533             cp += sprintf (cp, "; ");
534           if (var_is_numeric (v))
535             cp += sprintf (cp, "%g", value.f);
536           else
537             {
538               *cp++ = '"';
539               memcpy (cp, value.s, var_get_width (v));
540               cp += var_get_width (v);
541               *cp++ = '"';
542               *cp = '\0';
543             }
544         }
545
546       tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
547       r++;
548     }
549
550   /* Value labels. */
551   if (as == AS_DICTIONARY && var_has_value_labels (v))
552     {
553       const struct val_labs *val_labs = var_get_value_labels (v);
554       struct val_labs_iterator *i;
555       struct val_lab *vl;
556       int orig_r = r;
557
558 #if 0
559       tab_text (t, 1, r, TAB_LEFT, _("Value"));
560       tab_text (t, 2, r, TAB_LEFT, _("Label"));
561       r++;
562 #endif
563
564       tab_hline (t, TAL_1, 1, 2, r);
565       for (vl = val_labs_first_sorted (val_labs, &i); vl != NULL;
566            vl = val_labs_next (val_labs, &i))
567         {
568           char buf[128];
569
570           if (var_is_alpha (v))
571             {
572               memcpy (buf, vl->value.s, var_get_width (v));
573               buf[var_get_width (v)] = 0;
574             }
575           else
576             sprintf (buf, "%g", vl->value.f);
577
578           tab_text (t, 1, r, TAB_NONE, buf);
579           tab_text (t, 2, r, TAB_LEFT, vl->label);
580           r++;
581         }
582
583       tab_vline (t, TAL_1, 2, orig_r, r - 1);
584     }
585
586   /* Draw a line below the last row of information on this variable. */
587   tab_hline (t, TAL_1, 0, 3, r);
588
589   return r;
590 }
591
592 /* Display a list of vectors.  If SORTED is nonzero then they are
593    sorted alphabetically. */
594 static void
595 display_vectors (const struct dictionary *dict, int sorted)
596 {
597   const struct vector **vl;
598   int i;
599   struct tab_table *t;
600   size_t nvec;
601   size_t nrow;
602   size_t row;
603
604   nvec = dict_get_vector_cnt (dict);
605   if (nvec == 0)
606     {
607       msg (SW, _("No vectors defined."));
608       return;
609     }
610
611   vl = xnmalloc (nvec, sizeof *vl);
612   nrow = 0;
613   for (i = 0; i < nvec; i++)
614     {
615       vl[i] = dict_get_vector (dict, i);
616       nrow += vector_get_var_cnt (vl[i]);
617     }
618   if (sorted)
619     qsort (vl, nvec, sizeof *vl, compare_vector_ptrs_by_name);
620
621   t = tab_create (4, nrow + 1, 0);
622   tab_headers (t, 0, 0, 1, 0);
623   tab_columns (t, TAB_COL_DOWN, 1);
624   tab_dim (t, tab_natural_dimensions);
625   tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, nrow);
626   tab_box (t, -1, -1, -1, TAL_1, 0, 0, 3, nrow);
627   tab_hline (t, TAL_2, 0, 3, 1);
628   tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
629   tab_text (t, 1, 0, TAT_TITLE | TAB_LEFT, _("Position"));
630   tab_text (t, 2, 0, TAT_TITLE | TAB_LEFT, _("Variable"));
631   tab_text (t, 3, 0, TAT_TITLE | TAB_LEFT, _("Print Format"));
632   tab_flags (t, SOMF_NO_TITLE);
633
634   row = 1;
635   for (i = 0; i < nvec; i++)
636     {
637       const struct vector *vec = vl[i];
638       size_t j;
639
640       tab_joint_text (t, 0, row, 0, row + vector_get_var_cnt (vec) - 1,
641                       TAB_LEFT, vector_get_name (vl[i]));
642
643       for (j = 0; j < vector_get_var_cnt (vec); j++)
644         {
645           struct variable *var = vector_get_var (vec, j);
646           char fmt_string[FMT_STRING_LEN_MAX + 1];
647           fmt_to_string (var_get_print_format (var), fmt_string);
648
649           tab_text (t, 1, row, TAB_RIGHT | TAT_PRINTF, "%zu", j + 1);
650           tab_text (t, 2, row, TAB_LEFT, var_get_name (var));
651           tab_text (t, 3, row, TAB_LEFT, fmt_string);
652           row++;
653         }
654       tab_hline (t, TAL_1, 0, 3, row);
655     }
656
657   tab_submit (t);
658
659   free (vl);
660 }