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 file_handle *h;
74 struct casereader *reader;
75 struct sfm_read_info info;
83 lex_match (lexer, T_SLASH);
85 if (lex_match_id (lexer, "FILE") || lex_is_string (lexer))
87 lex_match (lexer, T_EQUALS);
90 h = fh_parse (lexer, FH_REF_FILE, NULL);
94 else if (lex_match_id (lexer, "ENCODING"))
96 lex_match (lexer, T_EQUALS);
98 if (!lex_force_string (lexer))
102 encoding = ss_xstrdup (lex_tokss (lexer));
112 lex_sbc_missing ("FILE");
116 reader = sfm_open_reader (h, encoding, &d, &info);
120 casereader_destroy (reader);
122 t = tab_create (2, 11 + (info.product_ext != NULL));
124 tab_vline (t, TAL_GAP, 1, 0, 8);
126 tab_text (t, 0, r, TAB_LEFT, _("File:"));
127 tab_text (t, 1, r++, TAB_LEFT, fh_get_file_name (h));
129 tab_text (t, 0, r, TAB_LEFT, _("Label:"));
131 const char *label = dict_get_label (d);
133 label = _("No label.");
134 tab_text (t, 1, r++, TAB_LEFT, label);
137 tab_text (t, 0, r, TAB_LEFT, _("Created:"));
138 tab_text_format (t, 1, r++, TAB_LEFT, "%s %s by %s",
139 info.creation_date, info.creation_time, info.product);
141 if (info.product_ext)
143 tab_text (t, 0, r, TAB_LEFT, _("Product:"));
144 tab_text (t, 1, r++, TAB_LEFT, info.product_ext);
147 tab_text (t, 0, r, TAB_LEFT, _("Integer Format:"));
148 tab_text (t, 1, r++, TAB_LEFT,
149 info.integer_format == INTEGER_MSB_FIRST ? _("Big Endian")
150 : info.integer_format == INTEGER_LSB_FIRST ? _("Little Endian")
153 tab_text (t, 0, r, TAB_LEFT, _("Real Format:"));
154 tab_text (t, 1, r++, TAB_LEFT,
155 info.float_format == FLOAT_IEEE_DOUBLE_LE ? _("IEEE 754 LE.")
156 : info.float_format == FLOAT_IEEE_DOUBLE_BE ? _("IEEE 754 BE.")
157 : info.float_format == FLOAT_VAX_D ? _("VAX D.")
158 : info.float_format == FLOAT_VAX_G ? _("VAX G.")
159 : info.float_format == FLOAT_Z_LONG ? _("IBM 390 Hex Long.")
162 tab_text (t, 0, r, TAB_LEFT, _("Variables:"));
163 tab_text_format (t, 1, r++, TAB_LEFT, "%zu", dict_get_var_cnt (d));
165 tab_text (t, 0, r, TAB_LEFT, _("Cases:"));
166 if (info.case_cnt == -1)
167 tab_text (t, 1, r, TAB_LEFT, _("Unknown"));
169 tab_text_format (t, 1, r, TAB_LEFT, "%ld", (long int) info.case_cnt);
172 tab_text (t, 0, r, TAB_LEFT, _("Type:"));
173 tab_text (t, 1, r++, TAB_LEFT, _("System File"));
175 tab_text (t, 0, r, TAB_LEFT, _("Weight:"));
177 struct variable *weight_var = dict_get_weight (d);
178 tab_text (t, 1, r++, TAB_LEFT,
180 ? var_get_name (weight_var) : _("Not weighted.")));
183 tab_text (t, 0, r, TAB_LEFT, _("Compression:"));
184 tab_text_format (t, 1, r++, TAB_LEFT,
185 info.compression == SFM_COMP_NONE ? _("None")
186 : info.compression == SFM_COMP_SIMPLE ? "SAV"
189 tab_text (t, 0, r, TAB_LEFT, _("Charset:"));
190 tab_text (t, 1, r++, TAB_LEFT, dict_get_encoding (d));
194 t = tab_create (4, 1 + 2 * dict_get_var_cnt (d));
195 tab_headers (t, 0, 0, 1, 0);
196 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
197 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
198 tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
199 tab_hline (t, TAL_2, 0, 3, 1);
200 for (r = 1, i = 0; i < dict_get_var_cnt (d); i++)
201 r = describe_variable (dict_get_var (d, i), t, r, 3,
202 DF_ALL & ~DF_AT_ATTRIBUTES);
204 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, r);
205 tab_vline (t, TAL_1, 1, 0, r);
206 tab_vline (t, TAL_1, 3, 0, r);
208 tab_resize (t, -1, r);
214 sfm_read_info_destroy (&info);
223 /* DISPLAY utility. */
225 static void display_macros (void);
226 static void display_documents (const struct dictionary *dict);
227 static void display_variables (const struct variable **, size_t, int);
228 static void display_vectors (const struct dictionary *dict, int sorted);
229 static void display_data_file_attributes (struct attrset *, int flags);
232 cmd_display (struct lexer *lexer, struct dataset *ds)
234 /* Whether to sort the list of variables alphabetically. */
237 /* Variables to display. */
239 const struct variable **vl;
241 if (lex_match_id (lexer, "MACROS"))
243 else if (lex_match_id (lexer, "DOCUMENTS"))
244 display_documents (dataset_dict (ds));
245 else if (lex_match_id (lexer, "FILE"))
247 if (!lex_force_match_id (lexer, "LABEL"))
249 if (dict_get_label (dataset_dict (ds)) == NULL)
250 tab_output_text (TAB_LEFT,
251 _("The active dataset does not have a file label."));
253 tab_output_text_format (TAB_LEFT, _("File label: %s"),
254 dict_get_label (dataset_dict (ds)));
260 sorted = lex_match_id (lexer, "SORTED");
262 if (lex_match_id (lexer, "VECTORS"))
264 display_vectors (dataset_dict(ds), sorted);
267 else if (lex_match_id (lexer, "SCRATCH"))
269 dict_get_vars (dataset_dict (ds), &vl, &n, DC_ORDINARY);
279 static const struct subcommand subcommands[] =
281 {"@ATTRIBUTES", DF_ATTRIBUTES | DF_AT_ATTRIBUTES},
282 {"ATTRIBUTES", DF_ATTRIBUTES},
283 {"DICTIONARY", DF_ALL & ~DF_AT_ATTRIBUTES},
284 {"INDEX", DF_DICT_INDEX},
285 {"LABELS", DF_DICT_INDEX | DF_VARIABLE_LABELS},
288 DF_DICT_INDEX | DF_FORMATS | DF_MISSING_VALUES | DF_MISC},
291 const struct subcommand *sbc;
294 for (sbc = subcommands; sbc->name != NULL; sbc++)
295 if (lex_match_id (lexer, sbc->name))
301 lex_match (lexer, T_SLASH);
302 lex_match_id (lexer, "VARIABLES");
303 lex_match (lexer, T_EQUALS);
305 if (lex_token (lexer) != T_ENDCMD)
307 if (!parse_variables_const (lexer, dataset_dict (ds), &vl, &n,
315 dict_get_vars (dataset_dict (ds), &vl, &n, 0);
320 sort (vl, n, sizeof *vl,
322 ? compare_var_ptrs_by_name
323 : compare_var_ptrs_by_dict_index), NULL);
324 display_variables (vl, n, flags);
327 msg (SW, _("No variables to display."));
330 if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES))
331 display_data_file_attributes (dict_get_attributes (dataset_dict (ds)),
339 display_macros (void)
341 tab_output_text (TAB_LEFT, _("Macros not supported."));
345 display_documents (const struct dictionary *dict)
347 const struct string_array *documents = dict_get_documents (dict);
349 if (string_array_is_empty (documents))
350 tab_output_text (TAB_LEFT, _("The active dataset dictionary does not "
351 "contain any documents."));
356 tab_output_text (TAB_LEFT | TAT_TITLE,
357 _("Documents in the active dataset:"));
358 for (i = 0; i < dict_get_document_line_cnt (dict); i++)
359 tab_output_text (TAB_LEFT | TAB_FIX, dict_get_document_line (dict, i));
364 display_variables (const struct variable **vl, size_t n, int flags)
367 int nc; /* Number of columns. */
368 int pc; /* `Position column' */
369 int r; /* Current row. */
372 /* One column for the name,
373 two columns for general description,
374 one column for dictionary index. */
376 if (flags & ~DF_DICT_INDEX)
379 if (flags & DF_DICT_INDEX)
382 t = tab_create (nc, n + 5);
383 tab_headers (t, 0, 0, 1, 0);
384 tab_hline (t, TAL_2, 0, nc - 1, 1);
385 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
386 if (flags & ~DF_DICT_INDEX)
387 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE,
388 (flags & ~(DF_DICT_INDEX | DF_VARIABLE_LABELS)
389 ? _("Description") : _("Label")));
390 if (flags & DF_DICT_INDEX)
391 tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
394 for (i = 0; i < n; i++)
395 r = describe_variable (vl[i], t, r, pc, flags);
396 tab_hline (t, flags & ~DF_DICT_INDEX ? TAL_2 : TAL_1, 0, nc - 1, 1);
399 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, nc - 1, r - 1);
400 tab_vline (t, TAL_1, 1, 0, r - 1);
402 if (flags & ~DF_DICT_INDEX)
403 tab_vline (t, TAL_1, nc - 1, 0, r - 1);
404 tab_resize (t, -1, r);
409 is_at_name (const char *name)
411 return name[0] == '@' || (name[0] == '$' && name[1] == '@');
415 count_attributes (const struct attrset *set, int flags)
417 struct attrset_iterator i;
418 struct attribute *attr;
422 for (attr = attrset_first (set, &i); attr != NULL;
423 attr = attrset_next (set, &i))
424 if (flags & DF_AT_ATTRIBUTES || !is_at_name (attribute_get_name (attr)))
425 n_attrs += attribute_get_n_values (attr);
430 display_attributes (struct tab_table *t, const struct attrset *set, int flags,
433 struct attribute **attrs;
437 n_attrs = attrset_count (set);
438 attrs = attrset_sorted (set);
439 for (i = 0; i < n_attrs; i++)
441 const struct attribute *attr = attrs[i];
442 const char *name = attribute_get_name (attr);
446 if (!(flags & DF_AT_ATTRIBUTES) && is_at_name (name))
449 n_values = attribute_get_n_values (attr);
450 for (j = 0; j < n_values; j++)
453 tab_text_format (t, c, r, TAB_LEFT, "%s[%zu]", name, j + 1);
455 tab_text (t, c, r, TAB_LEFT, name);
456 tab_text (t, c + 1, r, TAB_LEFT, attribute_get_value (attr, j));
464 display_data_file_attributes (struct attrset *set, int flags)
469 n_attrs = count_attributes (set, flags);
473 t = tab_create (2, n_attrs + 1);
474 tab_headers (t, 0, 0, 1, 0);
475 tab_box (t, TAL_1, TAL_1, -1, TAL_1, 0, 0, tab_nc (t) - 1, tab_nr (t) - 1);
476 tab_hline (t, TAL_2, 0, 1, 1);
477 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Attribute"));
478 tab_text (t, 1, 0, TAB_LEFT | TAT_TITLE, _("Value"));
479 display_attributes (t, set, flags, 0, 1);
480 tab_title (t, "Custom data file attributes.");
484 /* Puts a description of variable V into table T starting at row
485 R. The variable will be described in the format given by
486 FLAGS. Returns the next row available for use in the
489 describe_variable (const struct variable *v, struct tab_table *t, int r,
495 /* Make sure that enough rows are allocated. */
497 if (flags & ~(DF_DICT_INDEX | DF_VARIABLE_LABELS))
499 if (flags & DF_VALUE_LABELS)
500 need_rows += val_labs_count (var_get_value_labels (v));
501 if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES))
503 n_attrs = count_attributes (var_get_attributes (v), flags);
504 need_rows += n_attrs;
506 if (r + need_rows > tab_nr (t))
508 int nr = MAX (r + need_rows, tab_nr (t) * 2);
509 tab_realloc (t, -1, nr);
512 /* Put the name, var label, and position into the first row. */
513 tab_text (t, 0, r, TAB_LEFT, var_get_name (v));
514 if (flags & DF_DICT_INDEX)
515 tab_text_format (t, pc, r, 0, "%zu", var_get_dict_index (v) + 1);
517 if (flags & DF_VARIABLE_LABELS && var_has_label (v))
519 if (flags & ~(DF_DICT_INDEX | DF_VARIABLE_LABELS))
520 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
521 _("Label: %s"), var_get_label (v));
523 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, var_get_label (v));
527 /* Print/write format, or print and write formats. */
528 if (flags & DF_FORMATS)
530 const struct fmt_spec *print = var_get_print_format (v);
531 const struct fmt_spec *write = var_get_write_format (v);
533 if (fmt_equal (print, write))
535 char str[FMT_STRING_LEN_MAX + 1];
536 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
537 _("Format: %s"), fmt_to_string (print, str));
542 char str[FMT_STRING_LEN_MAX + 1];
543 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
544 _("Print Format: %s"),
545 fmt_to_string (print, str));
547 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
548 _("Write Format: %s"),
549 fmt_to_string (write, str));
554 /* Measurement level, role, display width, alignment. */
557 enum var_role role = var_get_role (v);
559 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
561 measure_to_string (var_get_measure (v)));
564 if (role != ROLE_INPUT)
566 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
567 _("Role: %s"), var_role_to_string (role));
571 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
572 _("Display Alignment: %s"),
573 alignment_to_string (var_get_alignment (v)));
576 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
577 _("Display Width: %d"),
578 var_get_display_width (v));
582 /* Missing values if any. */
583 if (flags & DF_MISSING_VALUES && var_has_missing_values (v))
585 const struct missing_values *mv = var_get_missing_values (v);
591 cp = stpcpy (buf, _("Missing Values: "));
593 if (mv_has_range (mv))
596 mv_get_range (mv, &x, &y);
598 cp += sprintf (cp, "LOWEST THRU %.*g", DBL_DIG + 1, y);
599 else if (y == HIGHEST)
600 cp += sprintf (cp, "%.*g THRU HIGHEST", DBL_DIG + 1, x);
602 cp += sprintf (cp, "%.*g THRU %.*g",
607 for (i = 0; i < mv_n_values (mv); i++)
609 const union value *value = mv_get_value (mv, i);
611 cp += sprintf (cp, "; ");
612 if (var_is_numeric (v))
613 cp += sprintf (cp, "%.*g", DBL_DIG + 1, value->f);
616 int width = var_get_width (v);
617 int mv_width = MIN (width, MV_MAX_STRING);
620 memcpy (cp, value_str (value, width), mv_width);
627 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
632 if (flags & DF_VALUE_LABELS && var_has_value_labels (v))
634 const struct val_labs *val_labs = var_get_value_labels (v);
635 size_t n_labels = val_labs_count (val_labs);
636 const struct val_lab **labels;
641 tab_text (t, 1, r, TAB_LEFT, _("Value"));
642 tab_text (t, 2, r, TAB_LEFT, _("Label"));
646 tab_hline (t, TAL_1, 1, 2, r);
648 labels = val_labs_sorted (val_labs);
649 for (i = 0; i < n_labels; i++)
651 const struct val_lab *vl = labels[i];
653 tab_value (t, 1, r, TAB_NONE, &vl->value, v, NULL);
654 tab_text (t, 2, r, TAB_LEFT, val_lab_get_escaped_label (vl));
659 tab_vline (t, TAL_1, 2, orig_r, r - 1);
662 if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES) && n_attrs)
664 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, "Custom attributes:");
667 display_attributes (t, var_get_attributes (v), flags, 1, r);
671 /* Draw a line below the last row of information on this variable. */
672 tab_hline (t, TAL_1, 0, tab_nc (t) - 1, r);
677 /* Display a list of vectors. If SORTED is nonzero then they are
678 sorted alphabetically. */
680 display_vectors (const struct dictionary *dict, int sorted)
682 const struct vector **vl;
689 nvec = dict_get_vector_cnt (dict);
692 msg (SW, _("No vectors defined."));
696 vl = xnmalloc (nvec, sizeof *vl);
698 for (i = 0; i < nvec; i++)
700 vl[i] = dict_get_vector (dict, i);
701 nrow += vector_get_var_cnt (vl[i]);
704 qsort (vl, nvec, sizeof *vl, compare_vector_ptrs_by_name);
706 t = tab_create (4, nrow + 1);
707 tab_headers (t, 0, 0, 1, 0);
708 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, nrow);
709 tab_box (t, -1, -1, -1, TAL_1, 0, 0, 3, nrow);
710 tab_hline (t, TAL_2, 0, 3, 1);
711 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
712 tab_text (t, 1, 0, TAT_TITLE | TAB_LEFT, _("Position"));
713 tab_text (t, 2, 0, TAT_TITLE | TAB_LEFT, _("Variable"));
714 tab_text (t, 3, 0, TAT_TITLE | TAB_LEFT, _("Print Format"));
717 for (i = 0; i < nvec; i++)
719 const struct vector *vec = vl[i];
722 tab_joint_text (t, 0, row, 0, row + vector_get_var_cnt (vec) - 1,
723 TAB_LEFT, vector_get_name (vl[i]));
725 for (j = 0; j < vector_get_var_cnt (vec); j++)
727 struct variable *var = vector_get_var (vec, j);
728 char fmt_string[FMT_STRING_LEN_MAX + 1];
729 fmt_to_string (var_get_print_format (var), fmt_string);
731 tab_text_format (t, 1, row, TAB_RIGHT, "%zu", j + 1);
732 tab_text (t, 2, row, TAB_LEFT, var_get_name (var));
733 tab_text (t, 3, row, TAB_LEFT, fmt_string);
736 tab_hline (t, TAL_1, 0, 3, row);