1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2012, 2013, 2014, 2015 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/>. */
26 #include "data/casereader.h"
27 #include "data/data-in.h"
28 #include "data/data-out.h"
29 #include "data/dataset.h"
30 #include "data/dictionary.h"
31 #include "data/format.h"
32 #include "data/settings.h"
33 #include "data/value.h"
34 #include "data/variable.h"
35 #include "language/command.h"
36 #include "language/lexer/format-parser.h"
37 #include "language/lexer/lexer.h"
38 #include "language/lexer/token.h"
39 #include "libpspp/assertion.h"
40 #include "libpspp/compiler.h"
41 #include "libpspp/copyleft.h"
42 #include "libpspp/temp-file.h"
43 #include "libpspp/version.h"
44 #include "libpspp/float-format.h"
45 #include "libpspp/i18n.h"
46 #include "libpspp/integer-format.h"
47 #include "libpspp/message.h"
48 #include "math/random.h"
49 #include "output/driver.h"
50 #include "output/journal.h"
51 #include "output/pivot-table.h"
53 #include "gl/ftoastr.h"
54 #include "gl/minmax.h"
55 #include "gl/relocatable.h"
56 #include "gl/vasnprintf.h"
57 #include "gl/xalloc.h"
60 #define _(msgid) gettext (msgid)
61 #define N_(msgid) (msgid)
66 bool (*set) (struct lexer *);
67 char *(*show) (const struct dataset *);
71 match_subcommand (struct lexer *lexer, const char *name)
73 if (lex_match_id (lexer, name))
75 lex_match (lexer, T_EQUALS);
83 subcommand_start_ofs (struct lexer *lexer)
85 int ofs = lex_ofs (lexer) - 1;
86 return lex_ofs_token (lexer, ofs)->type == T_EQUALS ? ofs - 1 : ofs;
90 parse_enum_valist (struct lexer *lexer, va_list args)
94 const char *name = va_arg (args, char *);
97 int value = va_arg (args, int);
99 if (lex_match_id (lexer, name))
104 #define parse_enum(...) parse_enum (__VA_ARGS__, NULL_SENTINEL)
105 static int SENTINEL(0)
106 (parse_enum) (struct lexer *lexer, ...)
110 va_start (args, lexer);
111 int retval = parse_enum_valist (lexer, args);
117 #define force_parse_enum(...) force_parse_enum (__VA_ARGS__, NULL_SENTINEL)
118 static int SENTINEL(0)
119 (force_parse_enum) (struct lexer *lexer, ...)
123 va_start (args, lexer);
124 int retval = parse_enum_valist (lexer, args);
129 enum { MAX_OPTIONS = 9 };
130 const char *options[MAX_OPTIONS];
133 va_start (args, lexer);
134 while (n < MAX_OPTIONS)
136 const char *name = va_arg (args, char *);
145 lex_error_expecting_array (lexer, options, n);
152 parse_bool (struct lexer *lexer)
154 return parse_enum (lexer,
155 "ON", true, "YES", true,
156 "OFF", false, "NO", false);
160 force_parse_bool (struct lexer *lexer)
162 return force_parse_enum (lexer,
163 "ON", true, "YES", true,
164 "OFF", false, "NO", false);
168 parse_output_routing (struct lexer *lexer, enum settings_output_type type)
170 enum settings_output_devices devices;
171 if (lex_match_id (lexer, "ON") || lex_match_id (lexer, "BOTH"))
172 devices = SETTINGS_DEVICE_LISTING | SETTINGS_DEVICE_TERMINAL;
173 else if (lex_match_id (lexer, "TERMINAL"))
174 devices = SETTINGS_DEVICE_TERMINAL;
175 else if (lex_match_id (lexer, "LISTING"))
176 devices = SETTINGS_DEVICE_LISTING;
177 else if (lex_match_id (lexer, "OFF") || lex_match_id (lexer, "NONE"))
181 lex_error (lexer, NULL);
185 settings_set_output_routing (type, devices);
191 show_output_routing (enum settings_output_type type)
193 enum settings_output_devices devices;
196 devices = settings_get_output_routing (type);
197 if (devices & SETTINGS_DEVICE_LISTING)
198 s = devices & SETTINGS_DEVICE_TERMINAL ? "BOTH" : "LISTING";
199 else if (devices & SETTINGS_DEVICE_TERMINAL)
208 parse_integer_format (struct lexer *lexer,
209 void (*set_format) (enum integer_format))
211 int value = force_parse_enum (lexer,
212 "MSBFIRST", INTEGER_MSB_FIRST,
213 "LSBFIRST", INTEGER_LSB_FIRST,
215 "NATIVE", INTEGER_NATIVE);
221 /* Returns a name for the given INTEGER_FORMAT value. */
223 show_integer_format (enum integer_format integer_format)
225 return xasprintf ("%s (%s)",
226 (integer_format == INTEGER_MSB_FIRST ? "MSBFIRST"
227 : integer_format == INTEGER_LSB_FIRST ? "LSBFIRST"
229 integer_format == INTEGER_NATIVE ? "NATIVE" : "nonnative");
233 parse_real_format (struct lexer *lexer,
234 void (*set_format) (enum float_format))
236 int value = force_parse_enum (lexer,
237 "NATIVE", FLOAT_NATIVE_DOUBLE,
238 "ISL", FLOAT_IEEE_SINGLE_LE,
239 "ISB", FLOAT_IEEE_SINGLE_BE,
240 "IDL", FLOAT_IEEE_DOUBLE_LE,
241 "IDB", FLOAT_IEEE_DOUBLE_BE,
252 /* Returns a name for the given FLOAT_FORMAT value. */
254 show_real_format (enum float_format float_format)
256 const char *format_name = "";
258 switch (float_format)
260 case FLOAT_IEEE_SINGLE_LE:
261 format_name = _("ISL (32-bit IEEE 754 single, little-endian)");
263 case FLOAT_IEEE_SINGLE_BE:
264 format_name = _("ISB (32-bit IEEE 754 single, big-endian)");
266 case FLOAT_IEEE_DOUBLE_LE:
267 format_name = _("IDL (64-bit IEEE 754 double, little-endian)");
269 case FLOAT_IEEE_DOUBLE_BE:
270 format_name = _("IDB (64-bit IEEE 754 double, big-endian)");
274 format_name = _("VF (32-bit VAX F, VAX-endian)");
277 format_name = _("VD (64-bit VAX D, VAX-endian)");
280 format_name = _("VG (64-bit VAX G, VAX-endian)");
284 format_name = _("ZS (32-bit IBM Z hexadecimal short, big-endian)");
287 format_name = _("ZL (64-bit IBM Z hexadecimal long, big-endian)");
295 return xasprintf ("%s (%s)", format_name,
296 (float_format == FLOAT_NATIVE_DOUBLE
297 ? "NATIVE" : "nonnative"));
301 parse_unimplemented (struct lexer *lexer, const char *name)
303 int start = subcommand_start_ofs (lexer);
304 if (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
306 int end = lex_ofs (lexer) - 1;
308 lex_ofs_msg (lexer, SW, start, end, _("%s is not yet implemented."), name);
313 parse_ccx (struct lexer *lexer, enum fmt_type ccx)
315 if (!lex_force_string (lexer))
318 settings_set_cc (lex_tokcstr (lexer), ccx);
324 parse_BASETEXTDIRECTION (struct lexer *lexer)
326 return parse_unimplemented (lexer, "BASETEXTDIRECTION");
330 parse_BLANKS (struct lexer *lexer)
332 if (lex_match_id (lexer, "SYSMIS"))
333 settings_set_blanks (SYSMIS);
336 if (!lex_force_num (lexer))
338 settings_set_blanks (lex_number (lexer));
345 show_BLANKS (const struct dataset *ds UNUSED)
347 return (settings_get_blanks () == SYSMIS
349 : xasprintf ("%.*g", DBL_DIG + 1, settings_get_blanks ()));
353 parse_BLOCK (struct lexer *lexer)
355 return parse_unimplemented (lexer, "BLOCK");
359 parse_BOX (struct lexer *lexer)
361 return parse_unimplemented (lexer, "BOX");
365 parse_CACHE (struct lexer *lexer)
367 return parse_unimplemented (lexer, "CACHE");
371 parse_CCA (struct lexer *lexer)
373 return parse_ccx (lexer, FMT_CCA);
377 parse_CCB (struct lexer *lexer)
379 return parse_ccx (lexer, FMT_CCB);
383 parse_CCC (struct lexer *lexer)
385 return parse_ccx (lexer, FMT_CCC);
389 parse_CCD (struct lexer *lexer)
391 return parse_ccx (lexer, FMT_CCD);
395 parse_CCE (struct lexer *lexer)
397 return parse_ccx (lexer, FMT_CCE);
401 show_cc (enum fmt_type type)
403 return fmt_number_style_to_string (fmt_settings_get_style (
404 settings_get_fmt_settings (), type));
408 show_CCA (const struct dataset *ds UNUSED)
410 return show_cc (FMT_CCA);
414 show_CCB (const struct dataset *ds UNUSED)
416 return show_cc (FMT_CCB);
420 show_CCC (const struct dataset *ds UNUSED)
422 return show_cc (FMT_CCC);
426 show_CCD (const struct dataset *ds UNUSED)
428 return show_cc (FMT_CCD);
432 show_CCE (const struct dataset *ds UNUSED)
434 return show_cc (FMT_CCE);
438 parse_CELLSBREAK (struct lexer *lexer)
440 return parse_unimplemented (lexer, "CELLSBREAK");
444 parse_CMPTRANS (struct lexer *lexer)
446 return parse_unimplemented (lexer, "CMPTRANS");
450 parse_COMPRESSION (struct lexer *lexer)
452 return parse_unimplemented (lexer, "COMPRESSION");
456 parse_CTEMPLATE (struct lexer *lexer)
458 return parse_unimplemented (lexer, "CTEMPLATE");
462 parse_DECIMAL (struct lexer *lexer)
464 int decimal_char = force_parse_enum (lexer,
467 if (decimal_char != -1)
468 settings_set_decimal_char (decimal_char);
469 return decimal_char != -1;
473 show_DECIMAL (const struct dataset *ds UNUSED)
475 return xasprintf ("`%c'", settings_get_fmt_settings ()->decimal);
479 parse_EPOCH (struct lexer *lexer)
481 if (lex_match_id (lexer, "AUTOMATIC"))
482 settings_set_epoch (-1);
483 else if (lex_is_integer (lexer))
485 if (!lex_force_int_range (lexer, "EPOCH", 1500, INT_MAX))
487 settings_set_epoch (lex_integer (lexer));
492 lex_error (lexer, _("Syntax error expecting %s or year."), "AUTOMATIC");
500 show_EPOCH (const struct dataset *ds UNUSED)
502 return xasprintf ("%d", settings_get_epoch ());
506 parse_ERRORS (struct lexer *lexer)
508 return parse_output_routing (lexer, SETTINGS_OUTPUT_ERROR);
512 show_ERRORS (const struct dataset *ds UNUSED)
514 return show_output_routing (SETTINGS_OUTPUT_ERROR);
518 parse_FORMAT (struct lexer *lexer)
520 int start = subcommand_start_ofs (lexer);
523 if (!parse_format_specifier (lexer, &fmt))
526 if (!fmt_check_output (&fmt))
529 int end = lex_ofs (lexer) - 1;
530 if (fmt_is_string (fmt.type))
532 char str[FMT_STRING_LEN_MAX + 1];
533 lex_ofs_error (lexer, start, end,
534 _("%s requires numeric output format as an argument. "
535 "Specified format %s is of type string."),
536 "FORMAT", fmt_to_string (&fmt, str));
540 settings_set_format (&fmt);
545 show_FORMAT (const struct dataset *ds UNUSED)
547 char str[FMT_STRING_LEN_MAX + 1];
548 return xstrdup (fmt_to_string (settings_get_format (), str));
552 parse_FUZZBITS (struct lexer *lexer)
554 if (!lex_force_int_range (lexer, "FUZZITS", 0, 20))
556 settings_set_fuzzbits (lex_integer (lexer));
562 show_FUZZBITS (const struct dataset *ds UNUSED)
564 return xasprintf ("%d", settings_get_fuzzbits ());
568 parse_HEADER (struct lexer *lexer)
570 return parse_unimplemented (lexer, "HEADER");
574 parse_INCLUDE (struct lexer *lexer)
576 int include = force_parse_bool (lexer);
578 settings_set_include (include);
579 return include != -1;
583 show_INCLUDE (const struct dataset *ds UNUSED)
585 return xstrdup (settings_get_include () ? "ON" : "OFF");
589 parse_JOURNAL (struct lexer *lexer)
591 int b = parse_bool (lexer);
596 else if (lex_is_string (lexer) || lex_token (lexer) == T_ID)
598 char *filename = utf8_to_filename (lex_tokcstr (lexer));
599 journal_set_file_name (filename);
606 lex_error (lexer, NULL);
613 show_JOURNAL (const struct dataset *ds UNUSED)
615 const char *enabled = journal_is_enabled () ? "ON" : "OFF";
616 const char *file_name = journal_get_file_name ();
618 ? xasprintf ("%s (%s)", enabled, file_name)
619 : xstrdup (enabled));
623 parse_LEADZERO (struct lexer *lexer)
625 int leadzero = force_parse_bool (lexer);
627 settings_set_include_leading_zero (leadzero);
628 return leadzero != -1;
632 show_LEADZERO (const struct dataset *ds UNUSED)
634 bool leadzero = settings_get_fmt_settings ()->include_leading_zero;
635 return xstrdup (leadzero ? "ON" : "OFF");
639 parse_LENGTH (struct lexer *lexer)
643 if (lex_match_id (lexer, "NONE"))
647 if (!lex_force_int_range (lexer, "LENGTH", 1, INT_MAX))
649 page_length = lex_integer (lexer);
653 if (page_length != -1)
654 settings_set_viewlength (page_length);
660 show_LENGTH (const struct dataset *ds UNUSED)
662 return xasprintf ("%d", settings_get_viewlength ());
666 parse_LOCALE (struct lexer *lexer)
668 if (!lex_force_string (lexer))
671 /* Try the argument as an encoding name, then as a locale name or alias. */
672 const char *s = lex_tokcstr (lexer);
673 if (valid_encoding (s))
674 set_default_encoding (s);
675 else if (!set_encoding_from_locale (s))
677 lex_error (lexer, _("%s is not a recognized encoding or locale name"), s);
686 show_LOCALE (const struct dataset *ds UNUSED)
688 return xstrdup (get_default_encoding ());
692 parse_MDISPLAY (struct lexer *lexer)
694 int mdisplay = force_parse_enum (lexer,
695 "TEXT", SETTINGS_MDISPLAY_TEXT,
696 "TABLES", SETTINGS_MDISPLAY_TABLES);
698 settings_set_mdisplay (mdisplay);
699 return mdisplay >= 0;
703 show_MDISPLAY (const struct dataset *ds UNUSED)
705 return xstrdup (settings_get_mdisplay () == SETTINGS_MDISPLAY_TEXT
706 ? "TEXT" : "TABLES");
710 parse_MESSAGES (struct lexer *lexer)
712 return parse_output_routing (lexer, SETTINGS_OUTPUT_NOTE);
716 show_MESSAGES (const struct dataset *ds UNUSED)
718 return show_output_routing (SETTINGS_OUTPUT_NOTE);
722 parse_MEXPAND (struct lexer *lexer)
724 int mexpand = force_parse_bool (lexer);
726 settings_set_mexpand (mexpand);
727 return mexpand != -1;
731 show_MEXPAND (const struct dataset *ds UNUSED)
733 return xstrdup (settings_get_mexpand () ? "ON" : "OFF");
737 parse_MITERATE (struct lexer *lexer)
739 if (!lex_force_int_range (lexer, "MITERATE", 1, INT_MAX))
741 settings_set_miterate (lex_integer (lexer));
747 show_MITERATE (const struct dataset *ds UNUSED)
749 return xasprintf ("%d", settings_get_miterate ());
753 parse_MNEST (struct lexer *lexer)
755 if (!lex_force_int_range (lexer, "MNEST", 1, INT_MAX))
757 settings_set_mnest (lex_integer (lexer));
763 show_MNEST (const struct dataset *ds UNUSED)
765 return xasprintf ("%d", settings_get_mnest ());
769 parse_MPRINT (struct lexer *lexer)
771 int mprint = force_parse_bool (lexer);
773 settings_set_mprint (mprint);
778 show_MPRINT (const struct dataset *ds UNUSED)
780 return xstrdup (settings_get_mprint () ? "ON" : "OFF");
784 parse_MXERRS (struct lexer *lexer)
786 if (!lex_force_int_range (lexer, "MXERRS", 1, INT_MAX))
788 settings_set_max_messages (MSG_S_ERROR, lex_integer (lexer));
794 show_MXERRS (const struct dataset *ds UNUSED)
796 return xasprintf ("%d", settings_get_max_messages (MSG_S_ERROR));
800 parse_MXLOOPS (struct lexer *lexer)
802 if (!lex_force_int_range (lexer, "MXLOOPS", 1, INT_MAX))
804 settings_set_mxloops (lex_integer (lexer));
810 show_MXLOOPS (const struct dataset *ds UNUSED)
812 return xasprintf ("%d", settings_get_mxloops ());
816 parse_MXWARNS (struct lexer *lexer)
818 if (!lex_force_int_range (lexer, "MXWARNS", 0, INT_MAX))
820 settings_set_max_messages (MSG_S_WARNING, lex_integer (lexer));
826 show_MXWARNS (const struct dataset *ds UNUSED)
828 return xasprintf ("%d", settings_get_max_messages (MSG_S_WARNING));
832 parse_PRINTBACK (struct lexer *lexer)
834 return parse_output_routing (lexer, SETTINGS_OUTPUT_SYNTAX);
838 show_PRINTBACK (const struct dataset *ds UNUSED)
840 return show_output_routing (SETTINGS_OUTPUT_SYNTAX);
844 parse_RESULTS (struct lexer *lexer)
846 return parse_output_routing (lexer, SETTINGS_OUTPUT_RESULT);
850 show_RESULTS (const struct dataset *ds UNUSED)
852 return show_output_routing (SETTINGS_OUTPUT_RESULT);
856 parse_RIB (struct lexer *lexer)
858 return parse_integer_format (lexer, settings_set_input_integer_format);
862 show_RIB (const struct dataset *ds UNUSED)
864 return show_integer_format (settings_get_input_integer_format ());
868 parse_RRB (struct lexer *lexer)
870 return parse_real_format (lexer, settings_set_input_float_format);
874 show_RRB (const struct dataset *ds UNUSED)
876 return show_real_format (settings_get_input_float_format ());
880 parse_SAFER (struct lexer *lexer)
882 bool ok = force_parse_enum (lexer, "ON", true, "YES", true) != -1;
884 settings_set_safer_mode ();
889 show_SAFER (const struct dataset *ds UNUSED)
891 return xstrdup (settings_get_safer_mode () ? "ON" : "OFF");
895 parse_SCOMPRESSION (struct lexer *lexer)
897 int value = force_parse_bool (lexer);
899 settings_set_scompression (value);
904 show_SCOMPRESSION (const struct dataset *ds UNUSED)
906 return xstrdup (settings_get_scompression () ? "ON" : "OFF");
910 parse_SEED (struct lexer *lexer)
912 if (lex_match_id (lexer, "RANDOM"))
916 if (!lex_force_num (lexer))
918 set_rng (lex_number (lexer));
926 parse_SMALL (struct lexer *lexer)
928 if (!lex_force_num (lexer))
930 settings_set_small (lex_number (lexer));
936 show_SMALL (const struct dataset *ds UNUSED)
938 char buf[DBL_BUFSIZE_BOUND];
939 if (dtoastr (buf, sizeof buf, 0, 0, settings_get_small ()) < 0)
941 return xstrdup (buf);
945 show_SUBTITLE (const struct dataset *ds UNUSED)
947 return xstrdup (output_get_subtitle ());
951 show_TEMPDIR (const struct dataset *ds UNUSED)
953 return xstrdup (temp_dir_name ());
957 show_TITLE (const struct dataset *ds UNUSED)
959 return xstrdup (output_get_title ());
963 parse_TNUMBERS (struct lexer *lexer)
965 int value = force_parse_enum (lexer,
966 "LABELS", SETTINGS_VALUE_SHOW_LABEL,
967 "VALUES", SETTINGS_VALUE_SHOW_VALUE,
968 "BOTH", SETTINGS_VALUE_SHOW_BOTH);
970 settings_set_show_values (value);
975 show_TNUMBERS (const struct dataset *ds UNUSED)
977 enum settings_value_show tnumbers = settings_get_show_values ();
978 return xstrdup (tnumbers == SETTINGS_VALUE_SHOW_LABEL ? "LABELS"
979 : tnumbers == SETTINGS_VALUE_SHOW_VALUE ? "VALUES"
984 parse_TVARS (struct lexer *lexer)
986 int value = force_parse_enum (lexer,
987 "LABELS", SETTINGS_VALUE_SHOW_LABEL,
988 "NAMES", SETTINGS_VALUE_SHOW_VALUE,
989 "BOTH", SETTINGS_VALUE_SHOW_BOTH);
991 settings_set_show_variables (value);
996 show_TVARS (const struct dataset *ds UNUSED)
998 enum settings_value_show tvars = settings_get_show_variables ();
999 return xstrdup (tvars == SETTINGS_VALUE_SHOW_LABEL ? "LABELS"
1000 : tvars == SETTINGS_VALUE_SHOW_VALUE ? "NAMES"
1005 parse_TLOOK (struct lexer *lexer)
1007 if (lex_match_id (lexer, "NONE"))
1008 pivot_table_look_set_default (pivot_table_look_builtin_default ());
1009 else if (lex_is_string (lexer))
1011 struct pivot_table_look *look;
1012 char *error = pivot_table_look_read (lex_tokcstr (lexer), &look);
1017 msg (SE, "%s", error);
1022 pivot_table_look_set_default (look);
1023 pivot_table_look_unref (look);
1030 parse_UNDEFINED (struct lexer *lexer)
1032 int value = force_parse_enum (lexer,
1036 settings_set_undefined (value);
1041 show_UNDEFINED (const struct dataset *ds UNUSED)
1043 return xstrdup (settings_get_undefined () ? "WARN" : "NOWARN");
1047 show_VERSION (const struct dataset *ds UNUSED)
1049 return strdup (announced_version);
1053 show_WEIGHT (const struct dataset *ds)
1055 const struct variable *var = dict_get_weight (dataset_dict (ds));
1056 return xstrdup (var != NULL ? var_get_name (var) : "OFF");
1060 parse_WIB (struct lexer *lexer)
1062 return parse_integer_format (lexer, settings_set_output_integer_format);
1066 show_WIB (const struct dataset *ds UNUSED)
1068 return show_integer_format (settings_get_output_integer_format ());
1072 parse_WRB (struct lexer *lexer)
1074 return parse_real_format (lexer, settings_set_output_float_format);
1078 show_WRB (const struct dataset *ds UNUSED)
1080 return show_real_format (settings_get_output_float_format ());
1084 parse_WIDTH (struct lexer *lexer)
1086 if (lex_match_id (lexer, "NARROW"))
1087 settings_set_viewwidth (79);
1088 else if (lex_match_id (lexer, "WIDE"))
1089 settings_set_viewwidth (131);
1092 if (!lex_force_int_range (lexer, "WIDTH", 40, INT_MAX))
1094 settings_set_viewwidth (lex_integer (lexer));
1102 show_WIDTH (const struct dataset *ds UNUSED)
1104 return xasprintf ("%d", settings_get_viewwidth ());
1108 parse_WORKSPACE (struct lexer *lexer)
1110 if (!lex_force_int_range (lexer, "WORKSPACE",
1111 settings_get_testing_mode () ? 1 : 1024,
1114 int workspace = lex_integer (lexer);
1116 settings_set_workspace (MIN (workspace, INT_MAX / 1024) * 1024);
1121 show_WORKSPACE (const struct dataset *ds UNUSED)
1123 size_t ws = settings_get_workspace () / 1024L;
1124 return xasprintf ("%zu", ws);
1128 show_DIRECTORY (const struct dataset *ds UNUSED)
1137 buf = xrealloc (buf, len);
1139 while (NULL == (wd = getcwd (buf, len)));
1145 show_N (const struct dataset *ds)
1147 const struct casereader *reader = dataset_source (ds);
1149 ? xasprintf ("%lld", (long long int) casereader_count_cases (reader))
1150 : xstrdup (_("Unknown")));
1154 do_show (const struct dataset *ds, const struct setting *s)
1156 char *value = s->show (ds);
1157 msg (SN, _("%s is %s."), s->name, value ? value : _("empty"));
1162 show_warranty (const struct dataset *ds UNUSED)
1164 fputs (lack_of_warranty, stdout);
1168 show_copying (const struct dataset *ds UNUSED)
1170 fputs (copyleft, stdout);
1174 add_row (struct pivot_table *table, const char *attribute,
1177 int row = pivot_category_create_leaf (table->dimensions[0]->root,
1178 pivot_value_new_text (attribute));
1180 pivot_table_put1 (table, row, pivot_value_new_user_text (value, -1));
1184 show_system (const struct dataset *ds UNUSED)
1186 struct pivot_table *table = pivot_table_create (N_("System Information"));
1187 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Attribute"));
1189 add_row (table, N_("Version"), version);
1190 add_row (table, N_("Host System"), host_system);
1191 add_row (table, N_("Build System"), build_system);
1192 add_row (table, N_("Locale Directory"), relocate (locale_dir));
1193 add_row (table, N_("Compiler Version"),
1200 pivot_table_submit (table);
1203 static const struct setting settings[] = {
1204 { "BASETEXTDIRECTION", parse_BASETEXTDIRECTION, NULL },
1205 { "BLANKS", parse_BLANKS, show_BLANKS },
1206 { "BLOCK", parse_BLOCK, NULL },
1207 { "BOX", parse_BOX, NULL },
1208 { "CACHE", parse_CACHE, NULL },
1209 { "CCA", parse_CCA, show_CCA },
1210 { "CCB", parse_CCB, show_CCB },
1211 { "CCC", parse_CCC, show_CCC },
1212 { "CCD", parse_CCD, show_CCD },
1213 { "CCE", parse_CCE, show_CCE },
1214 { "CELLSBREAK", parse_CELLSBREAK, NULL },
1215 { "CMPTRANS", parse_CMPTRANS, NULL },
1216 { "COMPRESSION", parse_COMPRESSION, NULL },
1217 { "CTEMPLATE", parse_CTEMPLATE, NULL },
1218 { "DECIMAL", parse_DECIMAL, show_DECIMAL },
1219 { "DIRECTORY", NULL, show_DIRECTORY },
1220 { "EPOCH", parse_EPOCH, show_EPOCH },
1221 { "ERRORS", parse_ERRORS, show_ERRORS },
1222 { "FORMAT", parse_FORMAT, show_FORMAT },
1223 { "FUZZBITS", parse_FUZZBITS, show_FUZZBITS },
1224 { "HEADER", parse_HEADER, NULL },
1225 { "INCLUDE", parse_INCLUDE, show_INCLUDE },
1226 { "JOURNAL", parse_JOURNAL, show_JOURNAL },
1227 { "LEADZERO", parse_LEADZERO, show_LEADZERO },
1228 { "LENGTH", parse_LENGTH, show_LENGTH },
1229 { "LOCALE", parse_LOCALE, show_LOCALE },
1230 { "MDISPLAY", parse_MDISPLAY, show_MDISPLAY },
1231 { "MESSAGES", parse_MESSAGES, show_MESSAGES },
1232 { "MEXPAND", parse_MEXPAND, show_MEXPAND },
1233 { "MITERATE", parse_MITERATE, show_MITERATE },
1234 { "MNEST", parse_MNEST, show_MNEST },
1235 { "MPRINT", parse_MPRINT, show_MPRINT },
1236 { "MXERRS", parse_MXERRS, show_MXERRS },
1237 { "MXLOOPS", parse_MXLOOPS, show_MXLOOPS },
1238 { "MXWARNS", parse_MXWARNS, show_MXWARNS },
1239 { "N", NULL, show_N },
1240 { "PRINTBACK", parse_PRINTBACK, show_PRINTBACK },
1241 { "RESULTS", parse_RESULTS, show_RESULTS },
1242 { "RIB", parse_RIB, show_RIB },
1243 { "RRB", parse_RRB, show_RRB },
1244 { "SAFER", parse_SAFER, show_SAFER },
1245 { "SCOMPRESSION", parse_SCOMPRESSION, show_SCOMPRESSION },
1246 { "SEED", parse_SEED, NULL },
1247 { "SMALL", parse_SMALL, show_SMALL },
1248 { "TEMPDIR", NULL, show_TEMPDIR },
1249 { "TNUMBERS", parse_TNUMBERS, show_TNUMBERS },
1250 { "TVARS", parse_TVARS, show_TVARS },
1251 { "TLOOK", parse_TLOOK, NULL },
1252 { "UNDEFINED", parse_UNDEFINED, show_UNDEFINED },
1253 { "VERSION", NULL, show_VERSION },
1254 { "WEIGHT", NULL, show_WEIGHT },
1255 { "WIB", parse_WIB, show_WIB },
1256 { "WRB", parse_WRB, show_WRB },
1257 { "WIDTH", parse_WIDTH, show_WIDTH },
1258 { "WORKSPACE", parse_WORKSPACE, show_WORKSPACE },
1260 enum { N_SETTINGS = sizeof settings / sizeof *settings };
1263 parse_setting (struct lexer *lexer)
1265 for (size_t i = 0; i < N_SETTINGS; i++)
1266 if (settings[i].set && match_subcommand (lexer, settings[i].name))
1267 return settings[i].set (lexer);
1269 lex_error (lexer, NULL);
1274 cmd_set (struct lexer *lexer, struct dataset *ds UNUSED)
1278 lex_match (lexer, T_SLASH);
1279 if (lex_token (lexer) == T_ENDCMD)
1282 if (!parse_setting (lexer))
1290 show_all (const struct dataset *ds)
1292 for (size_t i = 0; i < sizeof settings / sizeof *settings; i++)
1293 if (settings[i].show)
1294 do_show (ds, &settings[i]);
1298 show_all_cc (const struct dataset *ds)
1300 for (size_t i = 0; i < sizeof settings / sizeof *settings; i++)
1302 const struct setting *s = &settings[i];
1303 if (s->show && !strncmp (s->name, "CC", 2))
1309 cmd_show (struct lexer *lexer, struct dataset *ds)
1311 if (lex_token (lexer) == T_ENDCMD)
1319 if (lex_match (lexer, T_ALL))
1321 else if (lex_match_id (lexer, "CC"))
1323 else if (lex_match_id (lexer, "WARRANTY"))
1325 else if (lex_match_id (lexer, "COPYING") || lex_match_id (lexer, "LICENSE"))
1327 else if (lex_match_id (lexer, "SYSTEM"))
1329 else if (lex_match_id (lexer, "TITLE"))
1331 struct setting s = { .name = "TITLE", .show = show_TITLE };
1334 else if (lex_match_id (lexer, "SUBTITLE"))
1336 struct setting s = { .name = "SUBTITLE", .show = show_SUBTITLE };
1339 else if (lex_token (lexer) == T_ID)
1343 for (i = 0; i < sizeof settings / sizeof *settings; i++)
1345 const struct setting *s = &settings[i];
1346 if (s->show && lex_match_id (lexer, s->name))
1352 lex_error (lexer, NULL);
1359 lex_error (lexer, NULL);
1363 lex_match (lexer, T_SLASH);
1365 while (lex_token (lexer) != T_ENDCMD);
1370 #define MAX_SAVED_SETTINGS 5
1372 static struct settings *saved_settings[MAX_SAVED_SETTINGS];
1373 static int n_saved_settings;
1376 cmd_preserve (struct lexer *lexer, struct dataset *ds UNUSED)
1378 if (n_saved_settings < MAX_SAVED_SETTINGS)
1380 saved_settings[n_saved_settings++] = settings_get ();
1385 lex_next_error (lexer, -1, -1,
1386 _("Too many %s commands without a %s: at most "
1387 "%d levels of saved settings are allowed."),
1388 "PRESERVE", "RESTORE",
1389 MAX_SAVED_SETTINGS);
1390 return CMD_CASCADING_FAILURE;
1395 cmd_restore (struct lexer *lexer, struct dataset *ds UNUSED)
1397 if (n_saved_settings > 0)
1399 struct settings *s = saved_settings[--n_saved_settings];
1401 settings_destroy (s);
1406 lex_next_error (lexer, -1, -1,
1407 _("%s without matching %s."), "RESTORE", "PRESERVE");