584db1430da30c212f7681ac3464ad05e2892beb
[pspp-builds.git] / src / sysfile-info.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, USA. */
19
20 #include <config.h>
21 #include <assert.h>
22 #include <ctype.h>
23 #include <stdlib.h>
24 #include "algorithm.h"
25 #include "alloc.h"
26 #include "command.h"
27 #include "error.h"
28 #include "file-handle.h"
29 #include "hash.h"
30 #include "lexer.h"
31 #include "misc.h"
32 #include "output.h"
33 #include "sfm.h"
34 #include "som.h"
35 #include "tab.h"
36 #include "value-labels.h"
37 #include "var.h"
38 #include "vector.h"
39
40 /* Constants for DISPLAY utility. */
41 enum
42   {
43     AS_NAMES = 0,
44     AS_INDEX,
45     AS_VARIABLES,
46     AS_LABELS,
47     AS_DICTIONARY,
48     AS_SCRATCH,
49     AS_VECTOR
50   };
51
52 int describe_variable (struct variable *v, struct tab_table *t, int r, int as);
53      
54 /* Sets the widths of all the columns and heights of all the rows in
55    table T for driver D. */
56 static void
57 sysfile_info_dim (struct tab_table *t, struct outp_driver *d)
58 {
59   static const int max[] = {20, 5, 35, 3, 0};
60   const int *p;
61   int i;
62
63   for (p = max; *p; p++)
64     t->w[p - max] = min (tab_natural_width (t, d, p - max),
65                          *p * d->prop_em_width);
66   for (i = 0; i < t->nr; i++)
67     t->h[i] = tab_natural_height (t, d, i);
68 }
69
70 /* SYSFILE INFO utility. */
71 int
72 cmd_sysfile_info (void)
73 {
74   struct file_handle *h;
75   struct dictionary *d;
76   struct tab_table *t;
77   struct sfm_read_info inf;
78   int r, nr;
79   int i;
80
81   lex_match_id ("SYSFILE");
82   lex_match_id ("INFO");
83
84   lex_match_id ("FILE");
85   lex_match ('=');
86
87   h = fh_parse_file_handle ();
88   if (!h)
89     return CMD_FAILURE;
90
91   d = sfm_read_dictionary (h, &inf);
92   fh_close_handle (h);
93   if (!d)
94     return CMD_FAILURE;
95
96   t = tab_create (2, 9, 0);
97   tab_vline (t, TAL_1 | TAL_SPACING, 1, 0, 8);
98   tab_text (t, 0, 0, TAB_LEFT, _("File:"));
99   tab_text (t, 1, 0, TAB_LEFT, fh_handle_filename (h));
100   tab_text (t, 0, 1, TAB_LEFT, _("Label:"));
101   tab_text (t, 1, 1, TAB_LEFT,
102                 d->label ? d->label : _("No label."));
103   tab_text (t, 0, 2, TAB_LEFT, _("Created:"));
104   tab_text (t, 1, 2, TAB_LEFT | TAT_PRINTF, "%s %s by %s",
105                 inf.creation_date, inf.creation_time, inf.product);
106   tab_text (t, 0, 3, TAB_LEFT, _("Endian:"));
107   tab_text (t, 1, 3, TAB_LEFT, inf.bigendian ? _("Big.") : _("Little."));
108   tab_text (t, 0, 4, TAB_LEFT, _("Variables:"));
109   tab_text (t, 1, 4, TAB_LEFT | TAT_PRINTF, "%d",
110                 d->nvar);
111   tab_text (t, 0, 5, TAB_LEFT, _("Cases:"));
112   tab_text (t, 1, 5, TAB_LEFT | TAT_PRINTF,
113                 inf.ncases == -1 ? _("Unknown") : "%d", inf.ncases);
114   tab_text (t, 0, 6, TAB_LEFT, _("Type:"));
115   tab_text (t, 1, 6, TAB_LEFT, _("System File."));
116   tab_text (t, 0, 7, TAB_LEFT, _("Weight:"));
117   tab_text (t, 1, 7, TAB_LEFT,
118                 d->weight_var[0] ? d->weight_var : _("Not weighted."));
119   tab_text (t, 0, 8, TAB_LEFT, _("Mode:"));
120   tab_text (t, 1, 8, TAB_LEFT | TAT_PRINTF,
121                 _("Compression %s."), inf.compressed ? _("on") : _("off"));
122   tab_dim (t, tab_natural_dimensions);
123   tab_submit (t);
124
125   nr = 1 + 2 * d->nvar;
126   t = tab_create (4, nr, 1);
127   tab_dim (t, sysfile_info_dim);
128   tab_headers (t, 0, 0, 1, 0);
129   tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
130   tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
131   tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
132   tab_hline (t, TAL_2, 0, 3, 1);
133   for (r = 1, i = 0; i < d->nvar; i++)
134     {
135       int nvl = val_labs_count (d->var[i]->val_labs);
136       
137       if (r + 10 + nvl > nr)
138         {
139           nr = max (nr * d->nvar / (i + 1), nr);
140           nr += 10 + nvl;
141           tab_realloc (t, 4, nr);
142         }
143
144       r = describe_variable (d->var[i], t, r, AS_DICTIONARY);
145     }
146   tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, r);
147   tab_vline (t, TAL_1, 0, 0, r);
148   tab_vline (t, TAL_1, 1, 0, r);
149   tab_vline (t, TAL_1, 3, 0, r);
150   tab_resize (t, -1, r);
151   tab_flags (t, SOMF_NO_TITLE);
152   tab_submit (t);
153
154   free_dictionary (d);
155   
156   return lex_end_of_command ();
157 }
158 \f
159 /* DISPLAY utility. */
160
161 static void display_macros (void);
162 static void display_documents (void);
163 static void display_variables (struct variable **, int, int);
164 static void display_vectors (int sorted);
165
166 int
167 cmd_display (void)
168 {
169   /* Whether to sort the list of variables alphabetically. */
170   int sorted;
171
172   /* Variables to display. */
173   int n;
174   struct variable **vl;
175
176   lex_match_id ("DISPLAY");
177
178   if (lex_match_id ("MACROS"))
179     display_macros ();
180   else if (lex_match_id ("DOCUMENTS"))
181     display_documents ();
182   else if (lex_match_id ("FILE"))
183     {
184       som_blank_line ();
185       if (!lex_force_match_id ("LABEL"))
186         return CMD_FAILURE;
187       if (default_dict.label == NULL)
188         tab_output_text (TAB_LEFT,
189                          _("The active file does not have a file label."));
190       else
191         {
192           tab_output_text (TAB_LEFT | TAT_TITLE, _("File label:"));
193           tab_output_text (TAB_LEFT | TAT_FIX, default_dict.label);
194         }
195     }
196   else
197     {
198       static const char *sbc[] =
199         {"NAMES", "INDEX", "VARIABLES", "LABELS",
200          "DICTIONARY", "SCRATCH", "VECTORS", NULL};
201       const char **cp;
202       int as;
203
204       sorted = lex_match_id ("SORTED");
205
206       for (cp = sbc; *cp; cp++)
207         if (token == T_ID && lex_id_match (*cp, tokid))
208           {
209             lex_get ();
210             break;
211           }
212       as = cp - sbc;
213
214       if (*cp == NULL)
215         as = AS_NAMES;
216
217       if (as == AS_VECTOR)
218         {
219           display_vectors (sorted);
220           return CMD_SUCCESS;
221         }
222
223       lex_match ('/');
224       lex_match_id ("VARIABLES");
225       lex_match ('=');
226
227       if (token != '.')
228         {
229           if (!parse_variables (NULL, &vl, &n, PV_NONE))
230             {
231               free (vl);
232               return CMD_FAILURE;
233             }
234           as = AS_DICTIONARY;
235         }
236       else
237         fill_all_vars (&vl, &n, FV_NONE);
238
239       if (as == AS_SCRATCH)
240         {
241           int i, m;
242           for (i = 0, m = n; i < n; i++)
243             if (vl[i]->name[0] != '#')
244               {
245                 vl[i] = NULL;
246                 m--;
247               }
248           as = AS_NAMES;
249           n = m;
250         }
251
252       if (n == 0)
253         {
254           msg (SW, _("No variables to display."));
255           return CMD_FAILURE;
256         }
257
258       if (sorted)
259         sort (vl, n, sizeof *vl, compare_variables, NULL);
260
261       display_variables (vl, n, as);
262
263       free (vl);
264     }
265
266   return lex_end_of_command ();
267 }
268
269 static void
270 display_macros (void)
271 {
272   som_blank_line ();
273   tab_output_text (TAB_LEFT, _("Macros not supported."));
274 }
275
276 static void
277 display_documents (void)
278 {
279   som_blank_line ();
280   if (default_dict.n_documents == 0)
281     tab_output_text (TAB_LEFT, _("The active file dictionary does not "
282                      "contain any documents."));
283   else
284     {
285       char buf[81];
286       int i;
287
288       tab_output_text (TAB_LEFT | TAT_TITLE,
289                        _("Documents in the active file:"));
290       som_blank_line ();
291       buf[80] = 0;
292       for (i = 0; i < default_dict.n_documents; i++)
293         {
294           int len = 79;
295
296           memcpy (buf, &default_dict.documents[i * 80], 80);
297           while ((isspace ((unsigned char) buf[len]) || buf[len] == 0)
298                  && len > 0)
299             len--;
300           buf[len + 1] = 0;
301           tab_output_text (TAB_LEFT | TAT_FIX | TAT_NOWRAP, buf);
302         }
303     }
304 }
305
306 static int _as;
307
308 /* Sets the widths of all the columns and heights of all the rows in
309    table T for driver D. */
310 static void
311 variables_dim (struct tab_table *t, struct outp_driver *d)
312 {
313   int pc;
314   int i;
315   
316   t->w[0] = tab_natural_width (t, d, 0);
317   if (_as == AS_DICTIONARY || _as == AS_VARIABLES || _as == AS_LABELS)
318     {
319       t->w[1] = max (tab_natural_width (t, d, 1), d->prop_em_width * 5);
320       t->w[2] = max (tab_natural_width (t, d, 2), d->prop_em_width * 35);
321       pc = 3;
322     }
323   else pc = 1;
324   if (_as != AS_NAMES)
325     t->w[pc] = tab_natural_width (t, d, pc);
326
327   for (i = 0; i < t->nr; i++)
328     t->h[i] = tab_natural_height (t, d, i);
329 }
330   
331 static void
332 display_variables (struct variable **vl, int n, int as)
333 {
334   struct variable **vp = vl;            /* Variable pointer. */
335   struct tab_table *t;
336   int nc;                       /* Number of columns. */
337   int nr;                       /* Number of rows. */
338   int pc;                       /* `Position column' */
339   int r;                        /* Current row. */
340   int i;
341
342   _as = as;
343   switch (as)
344     {
345     case AS_INDEX:
346       nc = 2;
347       break;
348     case AS_NAMES:
349       nc = 1;
350       break;
351     default:
352       nc = 4;
353       break;
354     }
355
356   t = tab_create (nc, n + 5, 1);
357   tab_headers (t, 0, 0, 1, 0);
358   nr = n + 5;
359   tab_hline (t, TAL_2, 0, nc - 1, 1);
360   tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
361   if (as != AS_NAMES)
362     {
363       pc = (as == AS_INDEX ? 1 : 3);
364       tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
365     }
366   if (as == AS_DICTIONARY || as == AS_VARIABLES)
367     tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
368   else if (as == AS_LABELS)
369     tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Label"));
370   tab_dim (t, variables_dim);
371     
372   for (i = r = 1; i <= n; i++)
373     {
374       struct variable *v;
375
376       while (*vp == NULL)
377         vp++;
378       v = *vp++;
379
380       if (as == AS_DICTIONARY || as == AS_VARIABLES)
381         {
382           int nvl = val_labs_count (v->val_labs);
383       
384           if (r + 10 + nvl > nr)
385             {
386               nr = max (nr * n / (i + 1), nr);
387               nr += 10 + nvl;
388               tab_realloc (t, nc, nr);
389             }
390
391           r = describe_variable (v, t, r, as);
392         } else {
393           tab_text (t, 0, r, TAB_LEFT, v->name);
394           if (as == AS_LABELS)
395             tab_joint_text (t, 1, r, 2, r, TAB_LEFT,
396                             v->label == NULL ? "(no label)" : v->label);
397           if (as != AS_NAMES)
398             {
399               tab_text (t, pc, r, TAT_PRINTF, "%d", v->index + 1);
400               tab_hline (t, TAL_1, 0, nc - 1, r);
401             }
402           r++;
403         }
404     }
405   tab_hline (t, as == AS_NAMES ? TAL_1 : TAL_2, 0, nc - 1, 1);
406   if (as != AS_NAMES)
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 (as == AS_DICTIONARY || as == AS_VARIABLES || as == AS_LABELS)
414     tab_vline (t, TAL_1, 3, 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 /* Puts a description of variable V into table T starting at row R.
421    The variable will be described in the format AS.  Returns the next
422    row available for use in the table. */
423 int 
424 describe_variable (struct variable *v, struct tab_table *t, int r, int as)
425 {
426   /* Put the name, var label, and position into the first row. */
427   tab_text (t, 0, r, TAB_LEFT, v->name);
428   tab_text (t, 3, r, TAT_PRINTF, "%d", v->index + 1);
429
430   if (as == AS_DICTIONARY && v->label)
431     {
432       tab_joint_text (t, 1, r, 2, r, TAB_LEFT, v->label);
433       r++;
434     }
435   
436   /* Print/write format, or print and write formats. */
437   if (v->print.type == v->write.type
438       && v->print.w == v->write.w
439       && v->print.d == v->write.d)
440     {
441       tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF, _("Format: %s"),
442                       fmt_to_string (&v->print));
443       r++;
444     }
445   else
446     {
447       tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
448                       _("Print Format: %s"), fmt_to_string (&v->print));
449       r++;
450       tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
451                       _("Write Format: %s"), fmt_to_string (&v->write));
452       r++;
453     }
454
455   /* Missing values if any. */
456   if (v->miss_type != MISSING_NONE)
457     {
458       char buf[80];
459       char *cp = stpcpy (buf, _("Missing Values: "));
460
461       if (v->type == NUMERIC)
462         switch (v->miss_type)
463           {
464           case MISSING_1:
465             sprintf (cp, "%g", v->missing[0].f);
466             break;
467           case MISSING_2:
468             sprintf (cp, "%g; %g", v->missing[0].f, v->missing[1].f);
469             break;
470           case MISSING_3:
471             sprintf (cp, "%g; %g; %g", v->missing[0].f,
472                      v->missing[1].f, v->missing[2].f);
473             break;
474           case MISSING_RANGE:
475             sprintf (cp, "%g THRU %g", v->missing[0].f, v->missing[1].f);
476             break;
477           case MISSING_LOW:
478             sprintf (cp, "LOWEST THRU %g", v->missing[0].f);
479             break;
480           case MISSING_HIGH:
481             sprintf (cp, "%g THRU HIGHEST", v->missing[0].f);
482             break;
483           case MISSING_RANGE_1:
484             sprintf (cp, "%g THRU %g; %g",
485                      v->missing[0].f, v->missing[1].f, v->missing[2].f);
486             break;
487           case MISSING_LOW_1:
488             sprintf (cp, "LOWEST THRU %g; %g",
489                      v->missing[0].f, v->missing[1].f);
490             break;
491           case MISSING_HIGH_1:
492             sprintf (cp, "%g THRU HIGHEST; %g",
493                      v->missing[0].f, v->missing[1].f);
494             break;
495           default:
496             assert (0);
497           }
498       else
499         {
500           int i;
501
502           for (i = 0; i < v->miss_type; i++)
503             {
504               if (i != 0)
505                 cp = stpcpy (cp, "; ");
506               *cp++ = '"';
507               memcpy (cp, v->missing[i].s, v->width);
508               cp += v->width;
509               *cp++ = '"';
510             }
511           *cp = 0;
512         }
513
514       tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
515       r++;
516     }
517
518   /* Value labels. */
519   if (as == AS_DICTIONARY && val_labs_count (v->val_labs))
520     {
521       struct val_labs_iterator *i;
522       struct val_lab *vl;
523       int orig_r = r;
524
525 #if 0
526       tab_text (t, 1, r, TAB_LEFT, _("Value"));
527       tab_text (t, 2, r, TAB_LEFT, _("Label"));
528       r++;
529 #endif
530
531       tab_hline (t, TAL_1, 1, 2, r);
532       for (vl = val_labs_first_sorted (v->val_labs, &i); vl != NULL;
533            vl = val_labs_next (v->val_labs, &i))
534         {
535           char buf[128];
536
537           if (v->type == ALPHA)
538             {
539               memcpy (buf, vl->value.s, v->width);
540               buf[v->width] = 0;
541             }
542           else
543             sprintf (buf, "%g", vl->value.f);
544
545           tab_text (t, 1, r, TAB_NONE, buf);
546           tab_text (t, 2, r, TAB_LEFT, vl->label);
547           r++;
548         }
549
550       tab_vline (t, TAL_1, 2, orig_r, r - 1);
551     }
552
553   /* Draw a line below the last row of information on this variable. */
554   tab_hline (t, TAL_1, 0, 3, r);
555
556   return r;
557 }
558
559 static int
560 compare_vectors_by_name (const void *a_, const void *b_)
561 {
562   struct vector *const *pa = a_;
563   struct vector *const *pb = b_;
564   struct vector *a = *pa;
565   struct vector *b = *pb;
566   
567   return strcmp (a->name, b->name);
568 }
569
570 /* Display a list of vectors.  If SORTED is nonzero then they are
571    sorted alphabetically. */
572 static void
573 display_vectors (int sorted)
574 {
575   struct vector **vl;
576   int i;
577   struct tab_table *t;
578
579   if (nvec == 0)
580     {
581       msg (SW, _("No vectors defined."));
582       return;
583     }
584
585   vl = xmalloc (sizeof *vl * nvec);
586   for (i = 0; i < nvec; i++)
587     vl[i] = &vec[i];
588   if (sorted)
589     qsort (vl, nvec, sizeof *vl, compare_vectors_by_name);
590
591   t = tab_create (1, nvec + 1, 0);
592   tab_headers (t, 0, 0, 1, 0);
593   tab_columns (t, TAB_COL_DOWN, 1);
594   tab_dim (t, tab_natural_dimensions);
595   tab_hline (t, TAL_1, 0, 0, 1);
596   tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
597   tab_flags (t, SOMF_NO_TITLE);
598   for (i = 0; i < nvec; i++)
599     tab_text (t, 0, i + 1, TAB_LEFT, vl[i]->name);
600   tab_submit (t);
601
602   free (vl);
603 }