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