Actually implement the new procedure code and adapt all of its clients
[pspp-builds.git] / src / language / dictionary / sys-file-info.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    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, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include <ctype.h>
22 #include <stdlib.h>
23
24 #include <data/casereader.h>
25 #include <data/dictionary.h>
26 #include <data/file-handle-def.h>
27 #include <data/format.h>
28 #include <data/missing-values.h>
29 #include <data/procedure.h>
30 #include <data/sys-file-reader.h>
31 #include <data/value-labels.h>
32 #include <data/variable.h>
33 #include <data/vector.h>
34 #include <language/command.h>
35 #include <language/data-io/file-handle.h>
36 #include <language/lexer/lexer.h>
37 #include <language/lexer/variable-parser.h>
38 #include <libpspp/alloc.h>
39 #include <libpspp/array.h>
40 #include <libpspp/hash.h>
41 #include <libpspp/magic.h>
42 #include <libpspp/message.h>
43 #include <libpspp/message.h>
44 #include <libpspp/misc.h>
45 #include <output/manager.h>
46 #include <output/output.h>
47 #include <output/table.h>
48
49 #include "minmax.h"
50
51 #include "gettext.h"
52 #define _(msgid) gettext (msgid)
53
54 /* Constants for DISPLAY utility. */
55 enum
56   {
57     AS_NAMES = 0,
58     AS_INDEX,
59     AS_VARIABLES,
60     AS_LABELS,
61     AS_DICTIONARY,
62     AS_SCRATCH,
63     AS_VECTOR
64   };
65
66 static int describe_variable (const struct variable *v, struct tab_table *t, int r, int as);
67      
68 /* Sets the widths of all the columns and heights of all the rows in
69    table T for driver D. */
70 static void
71 sysfile_info_dim (struct tab_table *t, struct outp_driver *d)
72 {
73   static const int max[] = {20, 5, 35, 3, 0};
74   const int *p;
75   int i;
76
77   for (p = max; *p; p++)
78     t->w[p - max] = MIN (tab_natural_width (t, d, p - max),
79                          *p * d->prop_em_width);
80   for (i = 0; i < t->nr; i++)
81     t->h[i] = tab_natural_height (t, d, i);
82 }
83
84 /* SYSFILE INFO utility. */
85 int
86 cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED)
87 {
88   struct file_handle *h;
89   struct dictionary *d;
90   struct tab_table *t;
91   struct casereader *reader;
92   struct sfm_read_info info;
93   int r, nr;
94   int 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     return CMD_FAILURE;
106   casereader_destroy (reader);
107
108   t = tab_create (2, 10, 0);
109   tab_vline (t, TAL_GAP, 1, 0, 8);
110   tab_text (t, 0, 0, TAB_LEFT, _("File:"));
111   tab_text (t, 1, 0, TAB_LEFT, fh_get_file_name (h));
112   tab_text (t, 0, 1, TAB_LEFT, _("Label:"));
113   {
114     const char *label = dict_get_label (d);
115     if (label == NULL)
116       label = _("No label.");
117     tab_text (t, 1, 1, TAB_LEFT, label);
118   }
119   tab_text (t, 0, 2, TAB_LEFT, _("Created:"));
120   tab_text (t, 1, 2, TAB_LEFT | TAT_PRINTF, "%s %s by %s",
121                 info.creation_date, info.creation_time, info.product);
122   tab_text (t, 0, 3, TAB_LEFT, _("Integer Format:"));
123   tab_text (t, 1, 3, TAB_LEFT,
124             info.integer_format == INTEGER_MSB_FIRST ? _("Big Endian.")
125             : info.integer_format == INTEGER_LSB_FIRST ? _("Little Endian.")
126             : _("Unknown."));
127   tab_text (t, 0, 4, TAB_LEFT, _("Real Format:"));
128   tab_text (t, 1, 4, TAB_LEFT,
129             info.float_format == FLOAT_IEEE_DOUBLE_LE ? _("IEEE 754 LE.")
130             : info.float_format == FLOAT_IEEE_DOUBLE_BE ? _("IEEE 754 BE.")
131             : info.float_format == FLOAT_VAX_D ? _("VAX D.")
132             : info.float_format == FLOAT_VAX_G ? _("VAX G.")
133             : info.float_format == FLOAT_Z_LONG ? _("IBM 390 Hex Long.")
134             : _("Unknown."));
135   tab_text (t, 0, 5, TAB_LEFT, _("Variables:"));
136   tab_text (t, 1, 5, TAB_LEFT | TAT_PRINTF, "%u",
137             (unsigned int) dict_get_var_cnt (d));
138   tab_text (t, 0, 6, TAB_LEFT, _("Cases:"));
139   tab_text (t, 1, 6, TAB_LEFT | TAT_PRINTF,
140                 info.case_cnt == -1 ? _("Unknown") : "%d", info.case_cnt);
141   tab_text (t, 0, 7, TAB_LEFT, _("Type:"));
142   tab_text (t, 1, 7, TAB_LEFT, _("System File."));
143   tab_text (t, 0, 8, TAB_LEFT, _("Weight:"));
144   {
145     struct variable *weight_var = dict_get_weight (d);
146     tab_text (t, 1, 8, TAB_LEFT,
147               (weight_var != NULL
148                ? var_get_name (weight_var) : _("Not weighted."))); 
149   }
150   tab_text (t, 0, 9, TAB_LEFT, _("Mode:"));
151   tab_text (t, 1, 9, TAB_LEFT | TAT_PRINTF,
152                 _("Compression %s."), info.compressed ? _("on") : _("off"));
153   tab_dim (t, tab_natural_dimensions);
154   tab_submit (t);
155
156   nr = 1 + 2 * dict_get_var_cnt (d);
157
158   t = tab_create (4, nr, 1);
159   tab_dim (t, sysfile_info_dim);
160   tab_headers (t, 0, 0, 1, 0);
161   tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
162   tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
163   tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
164   tab_hline (t, TAL_2, 0, 3, 1);
165   for (r = 1, i = 0; i < dict_get_var_cnt (d); i++)
166     {
167       struct variable *v = dict_get_var (d, i);
168       const int nvl = val_labs_count (var_get_value_labels (v));
169       
170       if (r + 10 + nvl > nr)
171         {
172           nr = MAX (nr * dict_get_var_cnt (d) / (i + 1), nr);
173           nr += 10 + nvl;
174           tab_realloc (t, 4, nr);
175         }
176
177       r = describe_variable (v, t, r, AS_DICTIONARY);
178     }
179
180   tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, r);
181   tab_vline (t, TAL_1, 1, 0, r);
182   tab_vline (t, TAL_1, 3, 0, r);
183
184   tab_resize (t, -1, r);
185   tab_flags (t, SOMF_NO_TITLE);
186   tab_submit (t);
187
188   dict_destroy (d);
189   
190   return lex_end_of_command (lexer);
191 }
192 \f
193 /* DISPLAY utility. */
194
195 static void display_macros (void);
196 static void display_documents (const struct dictionary *dict);
197 static void display_variables (const struct variable **, size_t, int);
198 static void display_vectors (const struct dictionary *dict, int sorted);
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       static const char *sbc[] =
231         {"NAMES", "INDEX", "VARIABLES", "LABELS",
232          "DICTIONARY", "SCRATCH", "VECTORS", NULL};
233       const char **cp;
234       int as;
235
236       sorted = lex_match_id (lexer, "SORTED");
237
238       for (cp = sbc; *cp; cp++)
239         if (lex_token (lexer) == T_ID
240             && lex_id_match (ss_cstr (*cp), ss_cstr (lex_tokid (lexer))))
241           {
242             lex_get (lexer);
243             break;
244           }
245       as = cp - sbc;
246
247       if (*cp == NULL)
248         as = AS_NAMES;
249
250       if (as == AS_VECTOR)
251         {
252           display_vectors (dataset_dict(ds), sorted);
253           return CMD_SUCCESS;
254         }
255
256       lex_match (lexer, '/');
257       lex_match_id (lexer, "VARIABLES");
258       lex_match (lexer, '=');
259
260       if (lex_token (lexer) != '.')
261         {
262           if (!parse_variables_const (lexer, dataset_dict (ds), &vl, &n, PV_NONE))
263             {
264               free (vl);
265               return CMD_FAILURE;
266             }
267           as = AS_DICTIONARY;
268         }
269       else
270         dict_get_vars (dataset_dict (ds), &vl, &n, 0);
271
272       if (as == AS_SCRATCH)
273         {
274           size_t i, m;
275           for (i = 0, m = n; i < n; i++)
276             if (dict_class_from_id (var_get_name (vl[i])) != DC_SCRATCH)
277               {
278                 vl[i] = NULL;
279                 m--;
280               }
281           as = AS_NAMES;
282           n = m;
283         }
284
285       if (n == 0)
286         {
287           msg (SW, _("No variables to display."));
288           return CMD_FAILURE;
289         }
290
291       if (sorted)
292         sort (vl, n, sizeof *vl, compare_var_ptrs_by_name, NULL);
293
294       display_variables (vl, n, as);
295
296       free (vl);
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 _as;
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 (_as == AS_DICTIONARY || _as == AS_VARIABLES || _as == AS_LABELS)
347     {
348       t->w[1] = MAX (tab_natural_width (t, d, 1), d->prop_em_width * 5);
349       t->w[2] = MAX (tab_natural_width (t, d, 2), d->prop_em_width * 35);
350       pc = 3;
351     }
352   else pc = 1;
353   if (_as != AS_NAMES)
354     t->w[pc] = tab_natural_width (t, d, pc);
355
356   for (i = 0; i < t->nr; i++)
357     t->h[i] = tab_natural_height (t, d, i);
358 }
359   
360 static void
361 display_variables (const struct variable **vl, size_t n, int as)
362 {
363   const struct variable **vp = vl;      /* Variable pointer. */
364   struct tab_table *t;
365   int nc;                       /* Number of columns. */
366   int nr;                       /* Number of rows. */
367   int pc;                       /* `Position column' */
368   int r;                        /* Current row. */
369   size_t i;
370
371   _as = as;
372   switch (as)
373     {
374     case AS_INDEX:
375       nc = 2;
376       break;
377     case AS_NAMES:
378       nc = 1;
379       break;
380     default:
381       nc = 4;
382       break;
383     }
384
385   t = tab_create (nc, n + 5, 1);
386   tab_headers (t, 0, 0, 1, 0);
387   nr = n + 5;
388   tab_hline (t, TAL_2, 0, nc - 1, 1);
389   tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
390   pc = (as == AS_INDEX ? 1 : 3);
391   if (as != AS_NAMES)
392     tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
393   if (as == AS_DICTIONARY || as == AS_VARIABLES)
394     tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
395   else if (as == AS_LABELS)
396     tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Label"));
397   tab_dim (t, variables_dim);
398     
399   for (i = r = 1; i <= n; i++)
400     {
401       const struct variable *v;
402
403       while (*vp == NULL)
404         vp++;
405       v = *vp++;
406
407       if (as == AS_DICTIONARY || as == AS_VARIABLES)
408         {
409           int nvl = val_labs_count (var_get_value_labels (v));
410       
411           if (r + 10 + nvl > nr)
412             {
413               nr = MAX (nr * n / (i + 1), nr);
414               nr += 10 + nvl;
415               tab_realloc (t, nc, nr);
416             }
417
418           r = describe_variable (v, t, r, as);
419         } else {
420           tab_text (t, 0, r, TAB_LEFT, var_get_name (v));
421           if (as == AS_LABELS) 
422             {
423               const char *label = var_get_label (v);
424               tab_joint_text (t, 1, r, 2, r, TAB_LEFT,
425                               label != NULL ? "(no label)" : label); 
426             }
427           if (as != AS_NAMES)
428             {
429               tab_text (t, pc, r, TAT_PRINTF, "%d",
430                         (int) var_get_dict_index (v) + 1);
431               tab_hline (t, TAL_1, 0, nc - 1, r);
432             }
433           r++;
434         }
435     }
436   tab_hline (t, as == AS_NAMES ? TAL_1 : TAL_2, 0, nc - 1, 1);
437   if (as != AS_NAMES)
438     {
439       tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, nc - 1, r - 1);
440       tab_vline (t, TAL_1, 1, 0, r - 1);
441     }
442   else
443     tab_flags (t, SOMF_NO_TITLE);
444   if (as == AS_DICTIONARY || as == AS_VARIABLES || as == AS_LABELS)
445     tab_vline (t, TAL_1, 3, 0, r - 1);
446   tab_resize (t, -1, r);
447   tab_columns (t, TAB_COL_DOWN, 1);
448   tab_submit (t);
449 }
450 \f
451 /* Puts a description of variable V into table T starting at row R.
452    The variable will be described in the format AS.  Returns the next
453    row available for use in the table. */
454 static int 
455 describe_variable (const struct variable *v, struct tab_table *t, int r, int as)
456 {
457   const struct fmt_spec *print = var_get_print_format (v);
458   const struct fmt_spec *write = var_get_write_format (v);
459
460   /* Put the name, var label, and position into the first row. */
461   tab_text (t, 0, r, TAB_LEFT, var_get_name (v));
462   tab_text (t, 3, r, TAT_PRINTF, "%d", (int) var_get_dict_index (v) + 1);
463
464   if (as == AS_DICTIONARY && var_has_label (v)) 
465     {
466       tab_joint_text (t, 1, r, 2, r, TAB_LEFT, var_get_label (v));
467       r++;
468     }
469   
470   /* Print/write format, or print and write formats. */
471   if (fmt_equal (print, write))
472     {
473       char str[FMT_STRING_LEN_MAX + 1];
474       tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF, _("Format: %s"),
475                       fmt_to_string (print, str));
476       r++;
477     }
478   else
479     {
480       char str[FMT_STRING_LEN_MAX + 1];
481       tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
482                       _("Print Format: %s"), fmt_to_string (print, str));
483       r++;
484       tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
485                       _("Write Format: %s"), fmt_to_string (write, str));
486       r++;
487     }
488
489   /* Missing values if any. */
490   if (var_has_missing_values (v))
491     {
492       char buf[128];
493       char *cp;
494       struct missing_values mv;
495       int cnt = 0;
496       
497       cp = stpcpy (buf, _("Missing Values: "));
498       
499       mv_copy (&mv, var_get_missing_values (v));
500       if (mv_has_range (&mv)) 
501         {
502           double x, y;
503           mv_pop_range (&mv, &x, &y);
504           if (x == LOWEST)
505             cp += sprintf (cp, "LOWEST THRU %g", y);
506           else if (y == HIGHEST)
507             cp += sprintf (cp, "%g THRU HIGHEST", x);
508           else
509             cp += sprintf (cp, "%g THRU %g", x, y);
510           cnt++;
511         }
512       while (mv_has_value (&mv)) 
513         {
514           union value value;
515           mv_pop_value (&mv, &value);
516           if (cnt++ > 0)
517             cp += sprintf (cp, "; ");
518           if (var_is_numeric (v))
519             cp += sprintf (cp, "%g", value.f);
520           else 
521             {
522               *cp++ = '"';
523               memcpy (cp, value.s, var_get_width (v));
524               cp += var_get_width (v);
525               *cp++ = '"';
526               *cp = '\0';
527             }
528         }
529
530       tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
531       r++;
532     }
533
534   /* Value labels. */
535   if (as == AS_DICTIONARY && var_has_value_labels (v))
536     {
537       const struct val_labs *val_labs = var_get_value_labels (v);
538       struct val_labs_iterator *i;
539       struct val_lab *vl;
540       int orig_r = r;
541
542 #if 0
543       tab_text (t, 1, r, TAB_LEFT, _("Value"));
544       tab_text (t, 2, r, TAB_LEFT, _("Label"));
545       r++;
546 #endif
547
548       tab_hline (t, TAL_1, 1, 2, r);
549       for (vl = val_labs_first_sorted (val_labs, &i); vl != NULL;
550            vl = val_labs_next (val_labs, &i))
551         {
552           char buf[128];
553
554           if (var_is_alpha (v))
555             {
556               memcpy (buf, vl->value.s, var_get_width (v));
557               buf[var_get_width (v)] = 0;
558             }
559           else
560             sprintf (buf, "%g", vl->value.f);
561
562           tab_text (t, 1, r, TAB_NONE, buf);
563           tab_text (t, 2, r, TAB_LEFT, vl->label);
564           r++;
565         }
566
567       tab_vline (t, TAL_1, 2, orig_r, r - 1);
568     }
569
570   /* Draw a line below the last row of information on this variable. */
571   tab_hline (t, TAL_1, 0, 3, r);
572
573   return r;
574 }
575
576 /* Display a list of vectors.  If SORTED is nonzero then they are
577    sorted alphabetically. */
578 static void
579 display_vectors (const struct dictionary *dict, int sorted)
580 {
581   const struct vector **vl;
582   int i;
583   struct tab_table *t;
584   size_t nvec;
585   size_t nrow;
586   size_t row;
587   
588   nvec = dict_get_vector_cnt (dict);
589   if (nvec == 0)
590     {
591       msg (SW, _("No vectors defined."));
592       return;
593     }
594
595   vl = xnmalloc (nvec, sizeof *vl);
596   nrow = 0;
597   for (i = 0; i < nvec; i++) 
598     {
599       vl[i] = dict_get_vector (dict, i);
600       nrow += vector_get_var_cnt (vl[i]); 
601     }
602   if (sorted)
603     qsort (vl, nvec, sizeof *vl, compare_vector_ptrs_by_name);
604
605   t = tab_create (4, nrow + 1, 0);
606   tab_headers (t, 0, 0, 1, 0);
607   tab_columns (t, TAB_COL_DOWN, 1);
608   tab_dim (t, tab_natural_dimensions);
609   tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, nrow);
610   tab_box (t, -1, -1, -1, TAL_1, 0, 0, 3, nrow);
611   tab_hline (t, TAL_2, 0, 3, 1);
612   tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
613   tab_text (t, 1, 0, TAT_TITLE | TAB_LEFT, _("Position"));
614   tab_text (t, 2, 0, TAT_TITLE | TAB_LEFT, _("Variable"));
615   tab_text (t, 3, 0, TAT_TITLE | TAB_LEFT, _("Print Format"));
616   tab_flags (t, SOMF_NO_TITLE);
617
618   row = 1;
619   for (i = 0; i < nvec; i++) 
620     {
621       const struct vector *vec = vl[i];
622       size_t j;
623       
624       tab_joint_text (t, 0, row, 0, row + vector_get_var_cnt (vec) - 1,
625                       TAB_LEFT, vector_get_name (vl[i]));
626
627       for (j = 0; j < vector_get_var_cnt (vec); j++)
628         {
629           struct variable *var = vector_get_var (vec, j);
630           char fmt_string[FMT_STRING_LEN_MAX + 1];
631           fmt_to_string (var_get_print_format (var), fmt_string);
632           
633           tab_text (t, 1, row, TAB_RIGHT | TAT_PRINTF, "%d", (int) j + 1);
634           tab_text (t, 2, row, TAB_LEFT, var_get_name (var));
635           tab_text (t, 3, row, TAB_LEFT, fmt_string);
636           row++;
637         }
638       tab_hline (t, TAL_1, 0, 3, row);
639     }
640
641   tab_submit (t);
642
643   free (vl);
644 }