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_expecting (lexer, "ON", "BOTH", "TERMINAL", "LISTING",
186 settings_set_output_routing (type, devices);
192 show_output_routing (enum settings_output_type type)
194 enum settings_output_devices devices;
197 devices = settings_get_output_routing (type);
198 if (devices & SETTINGS_DEVICE_LISTING)
199 s = devices & SETTINGS_DEVICE_TERMINAL ? "BOTH" : "LISTING";
200 else if (devices & SETTINGS_DEVICE_TERMINAL)
209 parse_integer_format (struct lexer *lexer,
210 void (*set_format) (enum integer_format))
212 int value = force_parse_enum (lexer,
213 "MSBFIRST", INTEGER_MSB_FIRST,
214 "LSBFIRST", INTEGER_LSB_FIRST,
216 "NATIVE", INTEGER_NATIVE);
222 /* Returns a name for the given INTEGER_FORMAT value. */
224 show_integer_format (enum integer_format integer_format)
226 return xasprintf ("%s (%s)",
227 (integer_format == INTEGER_MSB_FIRST ? "MSBFIRST"
228 : integer_format == INTEGER_LSB_FIRST ? "LSBFIRST"
230 integer_format == INTEGER_NATIVE ? "NATIVE" : "nonnative");
234 parse_real_format (struct lexer *lexer,
235 void (*set_format) (enum float_format))
237 int value = force_parse_enum (lexer,
238 "NATIVE", FLOAT_NATIVE_DOUBLE,
239 "ISL", FLOAT_IEEE_SINGLE_LE,
240 "ISB", FLOAT_IEEE_SINGLE_BE,
241 "IDL", FLOAT_IEEE_DOUBLE_LE,
242 "IDB", FLOAT_IEEE_DOUBLE_BE,
253 /* Returns a name for the given FLOAT_FORMAT value. */
255 show_real_format (enum float_format float_format)
257 const char *format_name = "";
259 switch (float_format)
261 case FLOAT_IEEE_SINGLE_LE:
262 format_name = _("ISL (32-bit IEEE 754 single, little-endian)");
264 case FLOAT_IEEE_SINGLE_BE:
265 format_name = _("ISB (32-bit IEEE 754 single, big-endian)");
267 case FLOAT_IEEE_DOUBLE_LE:
268 format_name = _("IDL (64-bit IEEE 754 double, little-endian)");
270 case FLOAT_IEEE_DOUBLE_BE:
271 format_name = _("IDB (64-bit IEEE 754 double, big-endian)");
275 format_name = _("VF (32-bit VAX F, VAX-endian)");
278 format_name = _("VD (64-bit VAX D, VAX-endian)");
281 format_name = _("VG (64-bit VAX G, VAX-endian)");
285 format_name = _("ZS (32-bit IBM Z hexadecimal short, big-endian)");
288 format_name = _("ZL (64-bit IBM Z hexadecimal long, big-endian)");
296 return xasprintf ("%s (%s)", format_name,
297 (float_format == FLOAT_NATIVE_DOUBLE
298 ? "NATIVE" : "nonnative"));
302 parse_unimplemented (struct lexer *lexer, const char *name)
304 int start = subcommand_start_ofs (lexer);
305 if (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
307 int end = lex_ofs (lexer) - 1;
309 lex_ofs_msg (lexer, SW, start, end, _("%s is not yet implemented."), name);
314 parse_ccx (struct lexer *lexer, enum fmt_type ccx)
316 if (!lex_force_string (lexer))
319 char *error = settings_set_cc (lex_tokcstr (lexer), ccx);
322 lex_error (lexer, "%s", error);
332 parse_BASETEXTDIRECTION (struct lexer *lexer)
334 return parse_unimplemented (lexer, "BASETEXTDIRECTION");
338 parse_BLANKS (struct lexer *lexer)
340 if (lex_match_id (lexer, "SYSMIS"))
341 settings_set_blanks (SYSMIS);
344 if (!lex_force_num (lexer))
346 settings_set_blanks (lex_number (lexer));
353 show_BLANKS (const struct dataset *ds UNUSED)
355 return (settings_get_blanks () == SYSMIS
357 : xasprintf ("%.*g", DBL_DIG + 1, settings_get_blanks ()));
361 parse_BLOCK (struct lexer *lexer)
363 return parse_unimplemented (lexer, "BLOCK");
367 parse_BOX (struct lexer *lexer)
369 return parse_unimplemented (lexer, "BOX");
373 parse_CACHE (struct lexer *lexer)
375 return parse_unimplemented (lexer, "CACHE");
379 parse_CCA (struct lexer *lexer)
381 return parse_ccx (lexer, FMT_CCA);
385 parse_CCB (struct lexer *lexer)
387 return parse_ccx (lexer, FMT_CCB);
391 parse_CCC (struct lexer *lexer)
393 return parse_ccx (lexer, FMT_CCC);
397 parse_CCD (struct lexer *lexer)
399 return parse_ccx (lexer, FMT_CCD);
403 parse_CCE (struct lexer *lexer)
405 return parse_ccx (lexer, FMT_CCE);
409 show_cc (enum fmt_type type)
411 return fmt_number_style_to_string (fmt_settings_get_style (
412 settings_get_fmt_settings (), type));
416 show_CCA (const struct dataset *ds UNUSED)
418 return show_cc (FMT_CCA);
422 show_CCB (const struct dataset *ds UNUSED)
424 return show_cc (FMT_CCB);
428 show_CCC (const struct dataset *ds UNUSED)
430 return show_cc (FMT_CCC);
434 show_CCD (const struct dataset *ds UNUSED)
436 return show_cc (FMT_CCD);
440 show_CCE (const struct dataset *ds UNUSED)
442 return show_cc (FMT_CCE);
446 parse_CELLSBREAK (struct lexer *lexer)
448 return parse_unimplemented (lexer, "CELLSBREAK");
452 parse_CMPTRANS (struct lexer *lexer)
454 return parse_unimplemented (lexer, "CMPTRANS");
458 parse_COMPRESSION (struct lexer *lexer)
460 return parse_unimplemented (lexer, "COMPRESSION");
464 parse_CTEMPLATE (struct lexer *lexer)
466 return parse_unimplemented (lexer, "CTEMPLATE");
470 parse_DECIMAL (struct lexer *lexer)
472 int decimal_char = force_parse_enum (lexer,
475 if (decimal_char != -1)
476 settings_set_decimal_char (decimal_char);
477 return decimal_char != -1;
481 show_DECIMAL (const struct dataset *ds UNUSED)
483 return xasprintf ("`%c'", settings_get_fmt_settings ()->decimal);
487 parse_EPOCH (struct lexer *lexer)
489 if (lex_match_id (lexer, "AUTOMATIC"))
490 settings_set_epoch (-1);
491 else if (lex_is_integer (lexer))
493 if (!lex_force_int_range (lexer, "EPOCH", 1500, INT_MAX))
495 settings_set_epoch (lex_integer (lexer));
500 lex_error (lexer, _("Syntax error expecting %s or year."), "AUTOMATIC");
508 show_EPOCH (const struct dataset *ds UNUSED)
510 return xasprintf ("%d", settings_get_epoch ());
514 parse_ERRORS (struct lexer *lexer)
516 return parse_output_routing (lexer, SETTINGS_OUTPUT_ERROR);
520 show_ERRORS (const struct dataset *ds UNUSED)
522 return show_output_routing (SETTINGS_OUTPUT_ERROR);
526 parse_FORMAT (struct lexer *lexer)
528 int start = subcommand_start_ofs (lexer);
531 if (!parse_format_specifier (lexer, &fmt))
534 char *error = fmt_check_output__ (fmt);
537 lex_next_error (lexer, -1, -1, "%s", error);
542 int end = lex_ofs (lexer) - 1;
543 if (fmt_is_string (fmt.type))
545 char str[FMT_STRING_LEN_MAX + 1];
546 lex_ofs_error (lexer, start, end,
547 _("%s requires numeric output format as an argument. "
548 "Specified format %s is of type string."),
549 "FORMAT", fmt_to_string (fmt, str));
553 settings_set_format (fmt);
558 show_FORMAT (const struct dataset *ds UNUSED)
560 char str[FMT_STRING_LEN_MAX + 1];
561 return xstrdup (fmt_to_string (settings_get_format (), str));
565 parse_FUZZBITS (struct lexer *lexer)
567 if (!lex_force_int_range (lexer, "FUZZBITS", 0, 20))
569 settings_set_fuzzbits (lex_integer (lexer));
575 show_FUZZBITS (const struct dataset *ds UNUSED)
577 return xasprintf ("%d", settings_get_fuzzbits ());
581 parse_HEADER (struct lexer *lexer)
583 return parse_unimplemented (lexer, "HEADER");
587 parse_INCLUDE (struct lexer *lexer)
589 int include = force_parse_bool (lexer);
591 settings_set_include (include);
592 return include != -1;
596 show_INCLUDE (const struct dataset *ds UNUSED)
598 return xstrdup (settings_get_include () ? "ON" : "OFF");
602 parse_JOURNAL (struct lexer *lexer)
604 int b = parse_bool (lexer);
609 else if (lex_is_string (lexer) || lex_token (lexer) == T_ID)
611 char *filename = utf8_to_filename (lex_tokcstr (lexer));
612 journal_set_file_name (filename);
619 lex_error (lexer, _("Syntax error expecting ON or OFF or a file name."));
626 show_JOURNAL (const struct dataset *ds UNUSED)
628 const char *enabled = journal_is_enabled () ? "ON" : "OFF";
629 const char *file_name = journal_get_file_name ();
631 ? xasprintf ("%s (%s)", enabled, file_name)
632 : xstrdup (enabled));
636 parse_LEADZERO (struct lexer *lexer)
638 int leadzero = force_parse_bool (lexer);
640 settings_set_include_leading_zero (leadzero);
641 return leadzero != -1;
645 show_LEADZERO (const struct dataset *ds UNUSED)
647 bool leadzero = settings_get_fmt_settings ()->include_leading_zero;
648 return xstrdup (leadzero ? "ON" : "OFF");
652 parse_LENGTH (struct lexer *lexer)
656 if (lex_match_id (lexer, "NONE"))
660 if (!lex_force_int_range (lexer, "LENGTH", 1, INT_MAX))
662 page_length = lex_integer (lexer);
666 if (page_length != -1)
667 settings_set_viewlength (page_length);
673 show_LENGTH (const struct dataset *ds UNUSED)
675 return xasprintf ("%d", settings_get_viewlength ());
679 parse_LOCALE (struct lexer *lexer)
681 if (!lex_force_string (lexer))
684 /* Try the argument as an encoding name, then as a locale name or alias. */
685 const char *s = lex_tokcstr (lexer);
686 if (valid_encoding (s))
687 set_default_encoding (s);
688 else if (!set_encoding_from_locale (s))
690 lex_error (lexer, _("%s is not a recognized encoding or locale name"), s);
699 show_LOCALE (const struct dataset *ds UNUSED)
701 return xstrdup (get_default_encoding ());
705 parse_MDISPLAY (struct lexer *lexer)
707 int mdisplay = force_parse_enum (lexer,
708 "TEXT", SETTINGS_MDISPLAY_TEXT,
709 "TABLES", SETTINGS_MDISPLAY_TABLES);
711 settings_set_mdisplay (mdisplay);
712 return mdisplay >= 0;
716 show_MDISPLAY (const struct dataset *ds UNUSED)
718 return xstrdup (settings_get_mdisplay () == SETTINGS_MDISPLAY_TEXT
719 ? "TEXT" : "TABLES");
723 parse_MESSAGES (struct lexer *lexer)
725 return parse_output_routing (lexer, SETTINGS_OUTPUT_NOTE);
729 show_MESSAGES (const struct dataset *ds UNUSED)
731 return show_output_routing (SETTINGS_OUTPUT_NOTE);
735 parse_MEXPAND (struct lexer *lexer)
737 int mexpand = force_parse_bool (lexer);
739 settings_set_mexpand (mexpand);
740 return mexpand != -1;
744 show_MEXPAND (const struct dataset *ds UNUSED)
746 return xstrdup (settings_get_mexpand () ? "ON" : "OFF");
750 parse_MITERATE (struct lexer *lexer)
752 if (!lex_force_int_range (lexer, "MITERATE", 1, INT_MAX))
754 settings_set_miterate (lex_integer (lexer));
760 show_MITERATE (const struct dataset *ds UNUSED)
762 return xasprintf ("%d", settings_get_miterate ());
766 parse_MNEST (struct lexer *lexer)
768 if (!lex_force_int_range (lexer, "MNEST", 1, INT_MAX))
770 settings_set_mnest (lex_integer (lexer));
776 show_MNEST (const struct dataset *ds UNUSED)
778 return xasprintf ("%d", settings_get_mnest ());
782 parse_MPRINT (struct lexer *lexer)
784 int mprint = force_parse_bool (lexer);
786 settings_set_mprint (mprint);
791 show_MPRINT (const struct dataset *ds UNUSED)
793 return xstrdup (settings_get_mprint () ? "ON" : "OFF");
797 parse_MXERRS (struct lexer *lexer)
799 if (!lex_force_int_range (lexer, "MXERRS", 1, INT_MAX))
801 settings_set_max_messages (MSG_S_ERROR, lex_integer (lexer));
807 show_MXERRS (const struct dataset *ds UNUSED)
809 return xasprintf ("%d", settings_get_max_messages (MSG_S_ERROR));
813 parse_MXLOOPS (struct lexer *lexer)
815 if (!lex_force_int_range (lexer, "MXLOOPS", 1, INT_MAX))
817 settings_set_mxloops (lex_integer (lexer));
823 show_MXLOOPS (const struct dataset *ds UNUSED)
825 return xasprintf ("%d", settings_get_mxloops ());
829 parse_MXWARNS (struct lexer *lexer)
831 if (!lex_force_int_range (lexer, "MXWARNS", 0, INT_MAX))
833 settings_set_max_messages (MSG_S_WARNING, lex_integer (lexer));
839 show_MXWARNS (const struct dataset *ds UNUSED)
841 return xasprintf ("%d", settings_get_max_messages (MSG_S_WARNING));
845 parse_PRINTBACK (struct lexer *lexer)
847 return parse_output_routing (lexer, SETTINGS_OUTPUT_SYNTAX);
851 show_PRINTBACK (const struct dataset *ds UNUSED)
853 return show_output_routing (SETTINGS_OUTPUT_SYNTAX);
857 parse_RESULTS (struct lexer *lexer)
859 return parse_output_routing (lexer, SETTINGS_OUTPUT_RESULT);
863 show_RESULTS (const struct dataset *ds UNUSED)
865 return show_output_routing (SETTINGS_OUTPUT_RESULT);
869 parse_RIB (struct lexer *lexer)
871 return parse_integer_format (lexer, settings_set_input_integer_format);
875 show_RIB (const struct dataset *ds UNUSED)
877 return show_integer_format (settings_get_input_integer_format ());
881 parse_RRB (struct lexer *lexer)
883 return parse_real_format (lexer, settings_set_input_float_format);
887 show_RRB (const struct dataset *ds UNUSED)
889 return show_real_format (settings_get_input_float_format ());
893 parse_SAFER (struct lexer *lexer)
895 bool ok = force_parse_enum (lexer, "ON", true, "YES", true) != -1;
897 settings_set_safer_mode ();
902 show_SAFER (const struct dataset *ds UNUSED)
904 return xstrdup (settings_get_safer_mode () ? "ON" : "OFF");
908 parse_SCOMPRESSION (struct lexer *lexer)
910 int value = force_parse_bool (lexer);
912 settings_set_scompression (value);
917 show_SCOMPRESSION (const struct dataset *ds UNUSED)
919 return xstrdup (settings_get_scompression () ? "ON" : "OFF");
923 parse_SEED (struct lexer *lexer)
925 if (lex_match_id (lexer, "RANDOM"))
929 if (!lex_force_num (lexer))
931 set_rng (lex_number (lexer));
939 parse_SMALL (struct lexer *lexer)
941 if (!lex_force_num (lexer))
943 settings_set_small (lex_number (lexer));
949 show_SMALL (const struct dataset *ds UNUSED)
951 char buf[DBL_BUFSIZE_BOUND];
952 if (dtoastr (buf, sizeof buf, 0, 0, settings_get_small ()) < 0)
954 return xstrdup (buf);
958 show_SPLIT (const struct dataset *ds)
960 const struct dictionary *dict = dataset_dict (ds);
963 switch (dict_get_split_type (dict))
966 return xstrdup ("none");
980 struct string s = DS_EMPTY_INITIALIZER;
982 size_t n = dict_get_n_splits (dict);
983 const struct variable *const *vars = dict_get_split_vars (dict);
984 for (size_t i = 0; i < n; i++)
987 ds_put_cstr (&s, ", ");
988 ds_put_cstr (&s, var_get_name (vars[i]));
990 ds_put_format (&s, " (%s)", type);
991 return ds_steal_cstr (&s);
995 show_SUBTITLE (const struct dataset *ds UNUSED)
997 return xstrdup (output_get_subtitle ());
1001 show_TEMPDIR (const struct dataset *ds UNUSED)
1003 return xstrdup (temp_dir_name ());
1007 show_TITLE (const struct dataset *ds UNUSED)
1009 return xstrdup (output_get_title ());
1013 parse_TNUMBERS (struct lexer *lexer)
1015 int value = force_parse_enum (lexer,
1016 "LABELS", SETTINGS_VALUE_SHOW_LABEL,
1017 "VALUES", SETTINGS_VALUE_SHOW_VALUE,
1018 "BOTH", SETTINGS_VALUE_SHOW_BOTH);
1020 settings_set_show_values (value);
1025 show_TNUMBERS (const struct dataset *ds UNUSED)
1027 enum settings_value_show tnumbers = settings_get_show_values ();
1028 return xstrdup (tnumbers == SETTINGS_VALUE_SHOW_LABEL ? "LABELS"
1029 : tnumbers == SETTINGS_VALUE_SHOW_VALUE ? "VALUES"
1034 parse_TVARS (struct lexer *lexer)
1036 int value = force_parse_enum (lexer,
1037 "LABELS", SETTINGS_VALUE_SHOW_LABEL,
1038 "NAMES", SETTINGS_VALUE_SHOW_VALUE,
1039 "BOTH", SETTINGS_VALUE_SHOW_BOTH);
1041 settings_set_show_variables (value);
1046 show_TVARS (const struct dataset *ds UNUSED)
1048 enum settings_value_show tvars = settings_get_show_variables ();
1049 return xstrdup (tvars == SETTINGS_VALUE_SHOW_LABEL ? "LABELS"
1050 : tvars == SETTINGS_VALUE_SHOW_VALUE ? "NAMES"
1055 parse_TLOOK (struct lexer *lexer)
1057 if (lex_match_id (lexer, "NONE"))
1058 pivot_table_look_set_default (pivot_table_look_builtin_default ());
1059 else if (lex_is_string (lexer))
1061 struct pivot_table_look *look;
1062 char *error = pivot_table_look_read (lex_tokcstr (lexer), &look);
1067 msg (SE, "%s", error);
1072 pivot_table_look_set_default (look);
1073 pivot_table_look_unref (look);
1080 parse_UNDEFINED (struct lexer *lexer)
1082 int value = force_parse_enum (lexer,
1086 settings_set_undefined (value);
1091 show_UNDEFINED (const struct dataset *ds UNUSED)
1093 return xstrdup (settings_get_undefined () ? "WARN" : "NOWARN");
1097 show_VERSION (const struct dataset *ds UNUSED)
1099 return strdup (announced_version);
1103 show_WEIGHT (const struct dataset *ds)
1105 const struct variable *var = dict_get_weight (dataset_dict (ds));
1106 return xstrdup (var != NULL ? var_get_name (var) : "OFF");
1110 parse_WIB (struct lexer *lexer)
1112 return parse_integer_format (lexer, settings_set_output_integer_format);
1116 show_WIB (const struct dataset *ds UNUSED)
1118 return show_integer_format (settings_get_output_integer_format ());
1122 parse_WRB (struct lexer *lexer)
1124 return parse_real_format (lexer, settings_set_output_float_format);
1128 show_WRB (const struct dataset *ds UNUSED)
1130 return show_real_format (settings_get_output_float_format ());
1134 parse_WIDTH (struct lexer *lexer)
1136 if (lex_match_id (lexer, "NARROW"))
1137 settings_set_viewwidth (79);
1138 else if (lex_match_id (lexer, "WIDE"))
1139 settings_set_viewwidth (131);
1142 if (!lex_force_int_range (lexer, "WIDTH", 40, INT_MAX))
1144 settings_set_viewwidth (lex_integer (lexer));
1152 show_WIDTH (const struct dataset *ds UNUSED)
1154 return xasprintf ("%d", settings_get_viewwidth ());
1158 parse_WORKSPACE (struct lexer *lexer)
1160 if (!lex_force_int_range (lexer, "WORKSPACE",
1161 settings_get_testing_mode () ? 1 : 1024,
1164 int workspace = lex_integer (lexer);
1166 settings_set_workspace (MIN (workspace, INT_MAX / 1024) * 1024);
1171 show_WORKSPACE (const struct dataset *ds UNUSED)
1173 size_t ws = settings_get_workspace () / 1024L;
1174 return xasprintf ("%zu", ws);
1178 show_DIRECTORY (const struct dataset *ds UNUSED)
1187 buf = xrealloc (buf, len);
1189 while (NULL == (wd = getcwd (buf, len)));
1195 show_N (const struct dataset *ds)
1197 const struct casereader *reader = dataset_source (ds);
1199 ? xasprintf ("%lld", (long long int) casereader_count_cases (reader))
1200 : xstrdup (_("Unknown")));
1204 do_show (const struct dataset *ds, const struct setting *s,
1205 struct pivot_table **ptp)
1207 struct pivot_table *pt = *ptp;
1210 pt = *ptp = pivot_table_create (N_("Settings"));
1211 pivot_dimension_create (pt, PIVOT_AXIS_ROW, N_("Setting"));
1214 struct pivot_value *name = pivot_value_new_user_text (s->name, SIZE_MAX);
1215 char *text = s->show (ds);
1217 text = xstrdup("empty");
1218 struct pivot_value *value = pivot_value_new_user_text_nocopy (text);
1220 int row = pivot_category_create_leaf (pt->dimensions[0]->root, name);
1221 pivot_table_put1 (pt, row, value);
1225 show_warranty (const struct dataset *ds UNUSED)
1227 fputs (lack_of_warranty, stdout);
1231 show_copying (const struct dataset *ds UNUSED)
1233 fputs (copyleft, stdout);
1237 add_row (struct pivot_table *table, const char *attribute,
1240 int row = pivot_category_create_leaf (table->dimensions[0]->root,
1241 pivot_value_new_text (attribute));
1243 pivot_table_put1 (table, row, pivot_value_new_user_text (value, -1));
1247 show_system (const struct dataset *ds UNUSED)
1249 struct pivot_table *table = pivot_table_create (N_("System Information"));
1250 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Attribute"));
1252 add_row (table, N_("Version"), version);
1253 add_row (table, N_("Host System"), host_system);
1254 add_row (table, N_("Build System"), build_system);
1255 add_row (table, N_("Locale Directory"), relocate (locale_dir));
1256 add_row (table, N_("Compiler Version"),
1263 pivot_table_submit (table);
1266 static const struct setting settings[] = {
1267 { "BASETEXTDIRECTION", parse_BASETEXTDIRECTION, NULL },
1268 { "BLANKS", parse_BLANKS, show_BLANKS },
1269 { "BLOCK", parse_BLOCK, NULL },
1270 { "BOX", parse_BOX, NULL },
1271 { "CACHE", parse_CACHE, NULL },
1272 { "CCA", parse_CCA, show_CCA },
1273 { "CCB", parse_CCB, show_CCB },
1274 { "CCC", parse_CCC, show_CCC },
1275 { "CCD", parse_CCD, show_CCD },
1276 { "CCE", parse_CCE, show_CCE },
1277 { "CELLSBREAK", parse_CELLSBREAK, NULL },
1278 { "CMPTRANS", parse_CMPTRANS, NULL },
1279 { "COMPRESSION", parse_COMPRESSION, NULL },
1280 { "CTEMPLATE", parse_CTEMPLATE, NULL },
1281 { "DECIMAL", parse_DECIMAL, show_DECIMAL },
1282 { "DIRECTORY", NULL, show_DIRECTORY },
1283 { "EPOCH", parse_EPOCH, show_EPOCH },
1284 { "ERRORS", parse_ERRORS, show_ERRORS },
1285 { "FORMAT", parse_FORMAT, show_FORMAT },
1286 { "FUZZBITS", parse_FUZZBITS, show_FUZZBITS },
1287 { "HEADER", parse_HEADER, NULL },
1288 { "INCLUDE", parse_INCLUDE, show_INCLUDE },
1289 { "JOURNAL", parse_JOURNAL, show_JOURNAL },
1290 { "LEADZERO", parse_LEADZERO, show_LEADZERO },
1291 { "LENGTH", parse_LENGTH, show_LENGTH },
1292 { "LOCALE", parse_LOCALE, show_LOCALE },
1293 { "MDISPLAY", parse_MDISPLAY, show_MDISPLAY },
1294 { "MESSAGES", parse_MESSAGES, show_MESSAGES },
1295 { "MEXPAND", parse_MEXPAND, show_MEXPAND },
1296 { "MITERATE", parse_MITERATE, show_MITERATE },
1297 { "MNEST", parse_MNEST, show_MNEST },
1298 { "MPRINT", parse_MPRINT, show_MPRINT },
1299 { "MXERRS", parse_MXERRS, show_MXERRS },
1300 { "MXLOOPS", parse_MXLOOPS, show_MXLOOPS },
1301 { "MXWARNS", parse_MXWARNS, show_MXWARNS },
1302 { "N", NULL, show_N },
1303 { "PRINTBACK", parse_PRINTBACK, show_PRINTBACK },
1304 { "RESULTS", parse_RESULTS, show_RESULTS },
1305 { "RIB", parse_RIB, show_RIB },
1306 { "RRB", parse_RRB, show_RRB },
1307 { "SAFER", parse_SAFER, show_SAFER },
1308 { "SCOMPRESSION", parse_SCOMPRESSION, show_SCOMPRESSION },
1309 { "SEED", parse_SEED, NULL },
1310 { "SMALL", parse_SMALL, show_SMALL },
1311 { "SPLIT", NULL, show_SPLIT },
1312 { "TEMPDIR", NULL, show_TEMPDIR },
1313 { "TNUMBERS", parse_TNUMBERS, show_TNUMBERS },
1314 { "TVARS", parse_TVARS, show_TVARS },
1315 { "TLOOK", parse_TLOOK, NULL },
1316 { "UNDEFINED", parse_UNDEFINED, show_UNDEFINED },
1317 { "VERSION", NULL, show_VERSION },
1318 { "WEIGHT", NULL, show_WEIGHT },
1319 { "WIB", parse_WIB, show_WIB },
1320 { "WRB", parse_WRB, show_WRB },
1321 { "WIDTH", parse_WIDTH, show_WIDTH },
1322 { "WORKSPACE", parse_WORKSPACE, show_WORKSPACE },
1324 enum { N_SETTINGS = sizeof settings / sizeof *settings };
1327 parse_setting (struct lexer *lexer)
1329 for (size_t i = 0; i < N_SETTINGS; i++)
1330 if (settings[i].set && match_subcommand (lexer, settings[i].name))
1331 return settings[i].set (lexer);
1333 lex_error (lexer, _("Syntax error expecting the name of a setting."));
1338 cmd_set (struct lexer *lexer, struct dataset *ds UNUSED)
1342 lex_match (lexer, T_SLASH);
1343 if (lex_token (lexer) == T_ENDCMD)
1346 if (!parse_setting (lexer))
1354 show_all (const struct dataset *ds, struct pivot_table **ptp)
1356 for (size_t i = 0; i < sizeof settings / sizeof *settings; i++)
1357 if (settings[i].show)
1358 do_show (ds, &settings[i], ptp);
1362 show_all_cc (const struct dataset *ds, struct pivot_table **ptp)
1364 for (size_t i = 0; i < sizeof settings / sizeof *settings; i++)
1366 const struct setting *s = &settings[i];
1367 if (s->show && !strncmp (s->name, "CC", 2))
1368 do_show (ds, s, ptp);
1373 cmd_show (struct lexer *lexer, struct dataset *ds)
1375 struct pivot_table *pt = NULL;
1376 if (lex_token (lexer) == T_ENDCMD)
1379 pivot_table_submit (pt);
1385 if (lex_match (lexer, T_ALL))
1387 else if (lex_match_id (lexer, "CC"))
1388 show_all_cc (ds, &pt);
1389 else if (lex_match_id (lexer, "WARRANTY"))
1391 else if (lex_match_id (lexer, "COPYING") || lex_match_id (lexer, "LICENSE"))
1393 else if (lex_match_id (lexer, "SYSTEM"))
1395 else if (lex_match_id (lexer, "TITLE"))
1397 struct setting s = { .name = "TITLE", .show = show_TITLE };
1398 do_show (ds, &s, &pt);
1400 else if (lex_match_id (lexer, "SUBTITLE"))
1402 struct setting s = { .name = "SUBTITLE", .show = show_SUBTITLE };
1403 do_show (ds, &s, &pt);
1405 else if (lex_token (lexer) == T_ID)
1407 for (size_t i = 0; i < sizeof settings / sizeof *settings; i++)
1409 const struct setting *s = &settings[i];
1410 if (s->show && lex_match_id (lexer, s->name))
1412 do_show (ds, s, &pt);
1416 lex_error (lexer, _("Syntax error expecting the name of a setting."));
1423 lex_error (lexer, _("Syntax error expecting the name of a setting."));
1427 lex_match (lexer, T_SLASH);
1429 while (lex_token (lexer) != T_ENDCMD);
1432 pivot_table_submit (pt);
1437 #define MAX_SAVED_SETTINGS 5
1439 static struct settings *saved_settings[MAX_SAVED_SETTINGS];
1440 static int n_saved_settings;
1443 cmd_preserve (struct lexer *lexer, struct dataset *ds UNUSED)
1445 if (n_saved_settings < MAX_SAVED_SETTINGS)
1447 saved_settings[n_saved_settings++] = settings_get ();
1452 lex_next_error (lexer, -1, -1,
1453 _("Too many %s commands without a %s: at most "
1454 "%d levels of saved settings are allowed."),
1455 "PRESERVE", "RESTORE",
1456 MAX_SAVED_SETTINGS);
1457 return CMD_CASCADING_FAILURE;
1462 cmd_restore (struct lexer *lexer, struct dataset *ds UNUSED)
1464 if (n_saved_settings > 0)
1466 struct settings *s = saved_settings[--n_saved_settings];
1468 settings_destroy (s);
1473 lex_next_error (lexer, -1, -1,
1474 _("%s without matching %s."), "RESTORE", "PRESERVE");