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