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