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