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