1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "data/attributes.h"
24 #include "data/casereader.h"
25 #include "data/dataset.h"
26 #include "data/dictionary.h"
27 #include "data/file-handle-def.h"
28 #include "data/format.h"
29 #include "data/missing-values.h"
30 #include "data/sys-file-reader.h"
31 #include "data/value-labels.h"
32 #include "data/variable.h"
33 #include "data/vector.h"
34 #include "language/command.h"
35 #include "language/data-io/file-handle.h"
36 #include "language/lexer/lexer.h"
37 #include "language/lexer/variable-parser.h"
38 #include "libpspp/array.h"
39 #include "libpspp/message.h"
40 #include "libpspp/misc.h"
41 #include "libpspp/string-array.h"
42 #include "output/tab.h"
44 #include "gl/minmax.h"
45 #include "gl/xalloc.h"
48 #define _(msgid) gettext (msgid)
50 /* Information to include in displaying a dictionary. */
53 DF_DICT_INDEX = 1 << 0,
55 DF_VALUE_LABELS = 1 << 2,
56 DF_VARIABLE_LABELS = 1 << 3,
57 DF_MISSING_VALUES = 1 << 4,
58 DF_AT_ATTRIBUTES = 1 << 5, /* Attributes whose names begin with @. */
59 DF_ATTRIBUTES = 1 << 6, /* All other attributes. */
64 static int describe_variable (const struct variable *v, struct tab_table *t,
65 int r, int pc, int flags);
67 /* SYSFILE INFO utility. */
69 cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED)
71 struct sfm_reader *sfm_reader;
72 struct file_handle *h;
75 struct casereader *reader;
76 struct sfm_read_info info;
84 lex_match (lexer, T_SLASH);
86 if (lex_match_id (lexer, "FILE") || lex_is_string (lexer))
88 lex_match (lexer, T_EQUALS);
91 h = fh_parse (lexer, FH_REF_FILE, NULL);
95 else if (lex_match_id (lexer, "ENCODING"))
97 lex_match (lexer, T_EQUALS);
99 if (!lex_force_string (lexer))
103 encoding = ss_xstrdup (lex_tokss (lexer));
113 lex_sbc_missing ("FILE");
117 sfm_reader = sfm_open (h);
118 if (sfm_reader == NULL)
121 reader = sfm_decode (sfm_reader, encoding, &d, &info);
125 casereader_destroy (reader);
127 t = tab_create (2, 11 + (info.product_ext != NULL));
129 tab_vline (t, TAL_GAP, 1, 0, 8);
131 tab_text (t, 0, r, TAB_LEFT, _("File:"));
132 tab_text (t, 1, r++, TAB_LEFT, fh_get_file_name (h));
134 tab_text (t, 0, r, TAB_LEFT, _("Label:"));
136 const char *label = dict_get_label (d);
138 label = _("No label.");
139 tab_text (t, 1, r++, TAB_LEFT, label);
142 tab_text (t, 0, r, TAB_LEFT, _("Created:"));
143 tab_text_format (t, 1, r++, TAB_LEFT, "%s %s by %s",
144 info.creation_date, info.creation_time, info.product);
146 if (info.product_ext)
148 tab_text (t, 0, r, TAB_LEFT, _("Product:"));
149 tab_text (t, 1, r++, TAB_LEFT, info.product_ext);
152 tab_text (t, 0, r, TAB_LEFT, _("Integer Format:"));
153 tab_text (t, 1, r++, TAB_LEFT,
154 info.integer_format == INTEGER_MSB_FIRST ? _("Big Endian")
155 : info.integer_format == INTEGER_LSB_FIRST ? _("Little Endian")
158 tab_text (t, 0, r, TAB_LEFT, _("Real Format:"));
159 tab_text (t, 1, r++, TAB_LEFT,
160 info.float_format == FLOAT_IEEE_DOUBLE_LE ? _("IEEE 754 LE.")
161 : info.float_format == FLOAT_IEEE_DOUBLE_BE ? _("IEEE 754 BE.")
162 : info.float_format == FLOAT_VAX_D ? _("VAX D.")
163 : info.float_format == FLOAT_VAX_G ? _("VAX G.")
164 : info.float_format == FLOAT_Z_LONG ? _("IBM 390 Hex Long.")
167 tab_text (t, 0, r, TAB_LEFT, _("Variables:"));
168 tab_text_format (t, 1, r++, TAB_LEFT, "%zu", dict_get_var_cnt (d));
170 tab_text (t, 0, r, TAB_LEFT, _("Cases:"));
171 if (info.case_cnt == -1)
172 tab_text (t, 1, r, TAB_LEFT, _("Unknown"));
174 tab_text_format (t, 1, r, TAB_LEFT, "%ld", (long int) info.case_cnt);
177 tab_text (t, 0, r, TAB_LEFT, _("Type:"));
178 tab_text (t, 1, r++, TAB_LEFT, _("System File"));
180 tab_text (t, 0, r, TAB_LEFT, _("Weight:"));
182 struct variable *weight_var = dict_get_weight (d);
183 tab_text (t, 1, r++, TAB_LEFT,
185 ? var_get_name (weight_var) : _("Not weighted.")));
188 tab_text (t, 0, r, TAB_LEFT, _("Compression:"));
189 tab_text_format (t, 1, r++, TAB_LEFT,
190 info.compression == SFM_COMP_NONE ? _("None")
191 : info.compression == SFM_COMP_SIMPLE ? "SAV"
194 tab_text (t, 0, r, TAB_LEFT, _("Charset:"));
195 tab_text (t, 1, r++, TAB_LEFT, dict_get_encoding (d));
199 t = tab_create (4, 1 + 2 * dict_get_var_cnt (d));
200 tab_headers (t, 0, 0, 1, 0);
201 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
202 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
203 tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
204 tab_hline (t, TAL_2, 0, 3, 1);
205 for (r = 1, i = 0; i < dict_get_var_cnt (d); i++)
206 r = describe_variable (dict_get_var (d, i), t, r, 3,
207 DF_ALL & ~DF_AT_ATTRIBUTES);
209 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, r);
210 tab_vline (t, TAL_1, 1, 0, r);
211 tab_vline (t, TAL_1, 3, 0, r);
213 tab_resize (t, -1, r);
219 sfm_read_info_destroy (&info);
228 /* DISPLAY utility. */
230 static void display_macros (void);
231 static void display_documents (const struct dictionary *dict);
232 static void display_variables (const struct variable **, size_t, int);
233 static void display_vectors (const struct dictionary *dict, int sorted);
234 static void display_data_file_attributes (struct attrset *, int flags);
237 cmd_display (struct lexer *lexer, struct dataset *ds)
239 /* Whether to sort the list of variables alphabetically. */
242 /* Variables to display. */
244 const struct variable **vl;
246 if (lex_match_id (lexer, "MACROS"))
248 else if (lex_match_id (lexer, "DOCUMENTS"))
249 display_documents (dataset_dict (ds));
250 else if (lex_match_id (lexer, "FILE"))
252 if (!lex_force_match_id (lexer, "LABEL"))
254 if (dict_get_label (dataset_dict (ds)) == NULL)
255 tab_output_text (TAB_LEFT,
256 _("The active dataset does not have a file label."));
258 tab_output_text_format (TAB_LEFT, _("File label: %s"),
259 dict_get_label (dataset_dict (ds)));
265 sorted = lex_match_id (lexer, "SORTED");
267 if (lex_match_id (lexer, "VECTORS"))
269 display_vectors (dataset_dict(ds), sorted);
272 else if (lex_match_id (lexer, "SCRATCH"))
274 dict_get_vars (dataset_dict (ds), &vl, &n, DC_ORDINARY);
284 static const struct subcommand subcommands[] =
286 {"@ATTRIBUTES", DF_ATTRIBUTES | DF_AT_ATTRIBUTES},
287 {"ATTRIBUTES", DF_ATTRIBUTES},
288 {"DICTIONARY", DF_ALL & ~DF_AT_ATTRIBUTES},
289 {"INDEX", DF_DICT_INDEX},
290 {"LABELS", DF_DICT_INDEX | DF_VARIABLE_LABELS},
293 DF_DICT_INDEX | DF_FORMATS | DF_MISSING_VALUES | DF_MISC},
296 const struct subcommand *sbc;
299 for (sbc = subcommands; sbc->name != NULL; sbc++)
300 if (lex_match_id (lexer, sbc->name))
306 lex_match (lexer, T_SLASH);
307 lex_match_id (lexer, "VARIABLES");
308 lex_match (lexer, T_EQUALS);
310 if (lex_token (lexer) != T_ENDCMD)
312 if (!parse_variables_const (lexer, dataset_dict (ds), &vl, &n,
320 dict_get_vars (dataset_dict (ds), &vl, &n, 0);
325 sort (vl, n, sizeof *vl,
327 ? compare_var_ptrs_by_name
328 : compare_var_ptrs_by_dict_index), NULL);
329 display_variables (vl, n, flags);
332 msg (SW, _("No variables to display."));
335 if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES))
336 display_data_file_attributes (dict_get_attributes (dataset_dict (ds)),
344 display_macros (void)
346 tab_output_text (TAB_LEFT, _("Macros not supported."));
350 display_documents (const struct dictionary *dict)
352 const struct string_array *documents = dict_get_documents (dict);
354 if (string_array_is_empty (documents))
355 tab_output_text (TAB_LEFT, _("The active dataset dictionary does not "
356 "contain any documents."));
361 tab_output_text (TAB_LEFT | TAT_TITLE,
362 _("Documents in the active dataset:"));
363 for (i = 0; i < dict_get_document_line_cnt (dict); i++)
364 tab_output_text (TAB_LEFT | TAB_FIX, dict_get_document_line (dict, i));
369 display_variables (const struct variable **vl, size_t n, int flags)
372 int nc; /* Number of columns. */
373 int pc; /* `Position column' */
374 int r; /* Current row. */
377 /* One column for the name,
378 two columns for general description,
379 one column for dictionary index. */
381 if (flags & ~DF_DICT_INDEX)
384 if (flags & DF_DICT_INDEX)
387 t = tab_create (nc, n + 5);
388 tab_headers (t, 0, 0, 1, 0);
389 tab_hline (t, TAL_2, 0, nc - 1, 1);
390 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
391 if (flags & ~DF_DICT_INDEX)
392 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE,
393 (flags & ~(DF_DICT_INDEX | DF_VARIABLE_LABELS)
394 ? _("Description") : _("Label")));
395 if (flags & DF_DICT_INDEX)
396 tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
399 for (i = 0; i < n; i++)
400 r = describe_variable (vl[i], t, r, pc, flags);
401 tab_hline (t, flags & ~DF_DICT_INDEX ? TAL_2 : TAL_1, 0, nc - 1, 1);
404 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, nc - 1, r - 1);
405 tab_vline (t, TAL_1, 1, 0, r - 1);
407 if (flags & ~DF_DICT_INDEX)
408 tab_vline (t, TAL_1, nc - 1, 0, r - 1);
409 tab_resize (t, -1, r);
414 is_at_name (const char *name)
416 return name[0] == '@' || (name[0] == '$' && name[1] == '@');
420 count_attributes (const struct attrset *set, int flags)
422 struct attrset_iterator i;
423 struct attribute *attr;
427 for (attr = attrset_first (set, &i); attr != NULL;
428 attr = attrset_next (set, &i))
429 if (flags & DF_AT_ATTRIBUTES || !is_at_name (attribute_get_name (attr)))
430 n_attrs += attribute_get_n_values (attr);
435 display_attributes (struct tab_table *t, const struct attrset *set, int flags,
438 struct attribute **attrs;
442 n_attrs = attrset_count (set);
443 attrs = attrset_sorted (set);
444 for (i = 0; i < n_attrs; i++)
446 const struct attribute *attr = attrs[i];
447 const char *name = attribute_get_name (attr);
451 if (!(flags & DF_AT_ATTRIBUTES) && is_at_name (name))
454 n_values = attribute_get_n_values (attr);
455 for (j = 0; j < n_values; j++)
458 tab_text_format (t, c, r, TAB_LEFT, "%s[%zu]", name, j + 1);
460 tab_text (t, c, r, TAB_LEFT, name);
461 tab_text (t, c + 1, r, TAB_LEFT, attribute_get_value (attr, j));
469 display_data_file_attributes (struct attrset *set, int flags)
474 n_attrs = count_attributes (set, flags);
478 t = tab_create (2, n_attrs + 1);
479 tab_headers (t, 0, 0, 1, 0);
480 tab_box (t, TAL_1, TAL_1, -1, TAL_1, 0, 0, tab_nc (t) - 1, tab_nr (t) - 1);
481 tab_hline (t, TAL_2, 0, 1, 1);
482 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Attribute"));
483 tab_text (t, 1, 0, TAB_LEFT | TAT_TITLE, _("Value"));
484 display_attributes (t, set, flags, 0, 1);
485 tab_title (t, "Custom data file attributes.");
489 /* Puts a description of variable V into table T starting at row
490 R. The variable will be described in the format given by
491 FLAGS. Returns the next row available for use in the
494 describe_variable (const struct variable *v, struct tab_table *t, int r,
500 /* Make sure that enough rows are allocated. */
502 if (flags & ~(DF_DICT_INDEX | DF_VARIABLE_LABELS))
504 if (flags & DF_VALUE_LABELS)
505 need_rows += val_labs_count (var_get_value_labels (v));
506 if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES))
508 n_attrs = count_attributes (var_get_attributes (v), flags);
509 need_rows += n_attrs;
511 if (r + need_rows > tab_nr (t))
513 int nr = MAX (r + need_rows, tab_nr (t) * 2);
514 tab_realloc (t, -1, nr);
517 /* Put the name, var label, and position into the first row. */
518 tab_text (t, 0, r, TAB_LEFT, var_get_name (v));
519 if (flags & DF_DICT_INDEX)
520 tab_text_format (t, pc, r, 0, "%zu", var_get_dict_index (v) + 1);
522 if (flags & DF_VARIABLE_LABELS && var_has_label (v))
524 if (flags & ~(DF_DICT_INDEX | DF_VARIABLE_LABELS))
525 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
526 _("Label: %s"), var_get_label (v));
528 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, var_get_label (v));
532 /* Print/write format, or print and write formats. */
533 if (flags & DF_FORMATS)
535 const struct fmt_spec *print = var_get_print_format (v);
536 const struct fmt_spec *write = var_get_write_format (v);
538 if (fmt_equal (print, write))
540 char str[FMT_STRING_LEN_MAX + 1];
541 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
542 _("Format: %s"), fmt_to_string (print, str));
547 char str[FMT_STRING_LEN_MAX + 1];
548 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
549 _("Print Format: %s"),
550 fmt_to_string (print, str));
552 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
553 _("Write Format: %s"),
554 fmt_to_string (write, str));
559 /* Measurement level, role, display width, alignment. */
562 enum var_role role = var_get_role (v);
564 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
566 measure_to_string (var_get_measure (v)));
569 if (role != ROLE_INPUT)
571 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
572 _("Role: %s"), var_role_to_string (role));
576 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
577 _("Display Alignment: %s"),
578 alignment_to_string (var_get_alignment (v)));
581 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
582 _("Display Width: %d"),
583 var_get_display_width (v));
587 /* Missing values if any. */
588 if (flags & DF_MISSING_VALUES && var_has_missing_values (v))
590 const struct missing_values *mv = var_get_missing_values (v);
596 cp = stpcpy (buf, _("Missing Values: "));
598 if (mv_has_range (mv))
601 mv_get_range (mv, &x, &y);
603 cp += sprintf (cp, "LOWEST THRU %.*g", DBL_DIG + 1, y);
604 else if (y == HIGHEST)
605 cp += sprintf (cp, "%.*g THRU HIGHEST", DBL_DIG + 1, x);
607 cp += sprintf (cp, "%.*g THRU %.*g",
612 for (i = 0; i < mv_n_values (mv); i++)
614 const union value *value = mv_get_value (mv, i);
616 cp += sprintf (cp, "; ");
617 if (var_is_numeric (v))
618 cp += sprintf (cp, "%.*g", DBL_DIG + 1, value->f);
621 int width = var_get_width (v);
622 int mv_width = MIN (width, MV_MAX_STRING);
625 memcpy (cp, value_str (value, width), mv_width);
632 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
637 if (flags & DF_VALUE_LABELS && var_has_value_labels (v))
639 const struct val_labs *val_labs = var_get_value_labels (v);
640 size_t n_labels = val_labs_count (val_labs);
641 const struct val_lab **labels;
646 tab_text (t, 1, r, TAB_LEFT, _("Value"));
647 tab_text (t, 2, r, TAB_LEFT, _("Label"));
651 tab_hline (t, TAL_1, 1, 2, r);
653 labels = val_labs_sorted (val_labs);
654 for (i = 0; i < n_labels; i++)
656 const struct val_lab *vl = labels[i];
658 tab_value (t, 1, r, TAB_NONE, &vl->value, v, NULL);
659 tab_text (t, 2, r, TAB_LEFT, val_lab_get_escaped_label (vl));
664 tab_vline (t, TAL_1, 2, orig_r, r - 1);
667 if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES) && n_attrs)
669 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, "Custom attributes:");
672 display_attributes (t, var_get_attributes (v), flags, 1, r);
676 /* Draw a line below the last row of information on this variable. */
677 tab_hline (t, TAL_1, 0, tab_nc (t) - 1, r);
682 /* Display a list of vectors. If SORTED is nonzero then they are
683 sorted alphabetically. */
685 display_vectors (const struct dictionary *dict, int sorted)
687 const struct vector **vl;
694 nvec = dict_get_vector_cnt (dict);
697 msg (SW, _("No vectors defined."));
701 vl = xnmalloc (nvec, sizeof *vl);
703 for (i = 0; i < nvec; i++)
705 vl[i] = dict_get_vector (dict, i);
706 nrow += vector_get_var_cnt (vl[i]);
709 qsort (vl, nvec, sizeof *vl, compare_vector_ptrs_by_name);
711 t = tab_create (4, nrow + 1);
712 tab_headers (t, 0, 0, 1, 0);
713 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, nrow);
714 tab_box (t, -1, -1, -1, TAL_1, 0, 0, 3, nrow);
715 tab_hline (t, TAL_2, 0, 3, 1);
716 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
717 tab_text (t, 1, 0, TAT_TITLE | TAB_LEFT, _("Position"));
718 tab_text (t, 2, 0, TAT_TITLE | TAB_LEFT, _("Variable"));
719 tab_text (t, 3, 0, TAT_TITLE | TAB_LEFT, _("Print Format"));
722 for (i = 0; i < nvec; i++)
724 const struct vector *vec = vl[i];
727 tab_joint_text (t, 0, row, 0, row + vector_get_var_cnt (vec) - 1,
728 TAB_LEFT, vector_get_name (vl[i]));
730 for (j = 0; j < vector_get_var_cnt (vec); j++)
732 struct variable *var = vector_get_var (vec, j);
733 char fmt_string[FMT_STRING_LEN_MAX + 1];
734 fmt_to_string (var_get_print_format (var), fmt_string);
736 tab_text_format (t, 1, row, TAB_RIGHT, "%zu", j + 1);
737 tab_text (t, 2, row, TAB_LEFT, var_get_name (var));
738 tab_text (t, 3, row, TAB_LEFT, fmt_string);
741 tab_hline (t, TAL_1, 0, 3, row);