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 "libpspp/assertion.h"
39 #include "libpspp/compiler.h"
40 #include "libpspp/copyleft.h"
41 #include "libpspp/temp-file.h"
42 #include "libpspp/version.h"
43 #include "libpspp/float-format.h"
44 #include "libpspp/i18n.h"
45 #include "libpspp/integer-format.h"
46 #include "libpspp/message.h"
47 #include "math/random.h"
48 #include "output/driver.h"
49 #include "output/journal.h"
50 #include "output/pivot-table.h"
52 #include "gl/ftoastr.h"
53 #include "gl/minmax.h"
54 #include "gl/relocatable.h"
55 #include "gl/vasnprintf.h"
56 #include "gl/xalloc.h"
59 #define _(msgid) gettext (msgid)
60 #define N_(msgid) (msgid)
65 bool (*set) (struct lexer *);
66 char *(*show) (const struct dataset *);
70 match_subcommand (struct lexer *lexer, const char *name)
72 if (lex_match_id (lexer, name))
74 lex_match (lexer, T_EQUALS);
82 parse_enum_valist (struct lexer *lexer, va_list args)
86 const char *name = va_arg (args, char *);
89 int value = va_arg (args, int);
91 if (lex_match_id (lexer, name))
96 #define parse_enum(...) parse_enum (__VA_ARGS__, NULL_SENTINEL)
97 static int SENTINEL(0)
98 (parse_enum) (struct lexer *lexer, ...)
102 va_start (args, lexer);
103 int retval = parse_enum_valist (lexer, args);
109 #define force_parse_enum(...) force_parse_enum (__VA_ARGS__, NULL_SENTINEL)
110 static int SENTINEL(0)
111 (force_parse_enum) (struct lexer *lexer, ...)
115 va_start (args, lexer);
116 int retval = parse_enum_valist (lexer, args);
121 enum { MAX_OPTIONS = 9 };
122 const char *options[MAX_OPTIONS];
125 va_start (args, lexer);
126 while (n < MAX_OPTIONS)
128 const char *name = va_arg (args, char *);
137 lex_error_expecting_array (lexer, options, n);
144 parse_bool (struct lexer *lexer)
146 return parse_enum (lexer,
147 "ON", true, "YES", true,
148 "OFF", false, "NO", false);
152 force_parse_bool (struct lexer *lexer)
154 return force_parse_enum (lexer,
155 "ON", true, "YES", true,
156 "OFF", false, "NO", false);
160 force_parse_int (struct lexer *lexer, int *integerp)
162 if (!lex_force_int (lexer))
164 *integerp = lex_integer (lexer);
170 parse_output_routing (struct lexer *lexer, enum settings_output_type type)
172 enum settings_output_devices devices;
173 if (lex_match_id (lexer, "ON") || lex_match_id (lexer, "BOTH"))
174 devices = SETTINGS_DEVICE_LISTING | SETTINGS_DEVICE_TERMINAL;
175 else if (lex_match_id (lexer, "TERMINAL"))
176 devices = SETTINGS_DEVICE_TERMINAL;
177 else if (lex_match_id (lexer, "LISTING"))
178 devices = SETTINGS_DEVICE_LISTING;
179 else if (lex_match_id (lexer, "OFF") || lex_match_id (lexer, "NONE"))
183 lex_error (lexer, NULL);
187 settings_set_output_routing (type, devices);
193 show_output_routing (enum settings_output_type type)
195 enum settings_output_devices devices;
198 devices = settings_get_output_routing (type);
199 if (devices & SETTINGS_DEVICE_LISTING)
200 s = devices & SETTINGS_DEVICE_TERMINAL ? "BOTH" : "LISTING";
201 else if (devices & SETTINGS_DEVICE_TERMINAL)
210 parse_integer_format (struct lexer *lexer,
211 void (*set_format) (enum integer_format))
213 int value = force_parse_enum (lexer,
214 "MSBFIRST", INTEGER_MSB_FIRST,
215 "LSBFIRST", INTEGER_LSB_FIRST,
217 "NATIVE", INTEGER_NATIVE);
223 /* Returns a name for the given INTEGER_FORMAT value. */
225 show_integer_format (enum integer_format integer_format)
227 return xasprintf ("%s (%s)",
228 (integer_format == INTEGER_MSB_FIRST ? "MSBFIRST"
229 : integer_format == INTEGER_LSB_FIRST ? "LSBFIRST"
231 integer_format == INTEGER_NATIVE ? "NATIVE" : "nonnative");
235 parse_real_format (struct lexer *lexer,
236 void (*set_format) (enum float_format))
238 int value = force_parse_enum (lexer,
239 "NATIVE", FLOAT_NATIVE_DOUBLE,
240 "ISL", FLOAT_IEEE_SINGLE_LE,
241 "ISB", FLOAT_IEEE_SINGLE_BE,
242 "IDL", FLOAT_IEEE_DOUBLE_LE,
243 "IDB", FLOAT_IEEE_DOUBLE_BE,
254 /* Returns a name for the given FLOAT_FORMAT value. */
256 show_real_format (enum float_format float_format)
258 const char *format_name = "";
260 switch (float_format)
262 case FLOAT_IEEE_SINGLE_LE:
263 format_name = _("ISL (32-bit IEEE 754 single, little-endian)");
265 case FLOAT_IEEE_SINGLE_BE:
266 format_name = _("ISB (32-bit IEEE 754 single, big-endian)");
268 case FLOAT_IEEE_DOUBLE_LE:
269 format_name = _("IDL (64-bit IEEE 754 double, little-endian)");
271 case FLOAT_IEEE_DOUBLE_BE:
272 format_name = _("IDB (64-bit IEEE 754 double, big-endian)");
276 format_name = _("VF (32-bit VAX F, VAX-endian)");
279 format_name = _("VD (64-bit VAX D, VAX-endian)");
282 format_name = _("VG (64-bit VAX G, VAX-endian)");
286 format_name = _("ZS (32-bit IBM Z hexadecimal short, big-endian)");
289 format_name = _("ZL (64-bit IBM Z hexadecimal long, big-endian)");
297 return xasprintf ("%s (%s)", format_name,
298 (float_format == FLOAT_NATIVE_DOUBLE
299 ? "NATIVE" : "nonnative"));
303 parse_unimplemented (struct lexer *lexer, const char *name)
305 msg (SW, _("%s is not yet implemented."), name);
306 if (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
312 parse_ccx (struct lexer *lexer, enum fmt_type ccx)
314 if (!lex_force_string (lexer))
317 settings_set_cc (lex_tokcstr (lexer), ccx);
323 parse_BASETEXTDIRECTION (struct lexer *lexer)
325 return parse_unimplemented (lexer, "BASETEXTDIRECTION");
329 parse_BLANKS (struct lexer *lexer)
331 if (lex_match_id (lexer, "SYSMIS"))
332 settings_set_blanks (SYSMIS);
335 if (!lex_force_num (lexer))
337 settings_set_blanks (lex_number (lexer));
344 show_BLANKS (const struct dataset *ds UNUSED)
346 return (settings_get_blanks () == SYSMIS
348 : xasprintf ("%.*g", DBL_DIG + 1, settings_get_blanks ()));
352 parse_BLOCK (struct lexer *lexer)
354 return parse_unimplemented (lexer, "BLOCK");
358 parse_BOX (struct lexer *lexer)
360 return parse_unimplemented (lexer, "BOX");
364 parse_CACHE (struct lexer *lexer)
366 return parse_unimplemented (lexer, "CACHE");
370 parse_CCA (struct lexer *lexer)
372 return parse_ccx (lexer, FMT_CCA);
376 parse_CCB (struct lexer *lexer)
378 return parse_ccx (lexer, FMT_CCB);
382 parse_CCC (struct lexer *lexer)
384 return parse_ccx (lexer, FMT_CCC);
388 parse_CCD (struct lexer *lexer)
390 return parse_ccx (lexer, FMT_CCD);
394 parse_CCE (struct lexer *lexer)
396 return parse_ccx (lexer, FMT_CCE);
400 show_cc (enum fmt_type type)
402 return fmt_number_style_to_string (fmt_settings_get_style (
403 settings_get_fmt_settings (), type));
407 show_CCA (const struct dataset *ds UNUSED)
409 return show_cc (FMT_CCA);
413 show_CCB (const struct dataset *ds UNUSED)
415 return show_cc (FMT_CCB);
419 show_CCC (const struct dataset *ds UNUSED)
421 return show_cc (FMT_CCC);
425 show_CCD (const struct dataset *ds UNUSED)
427 return show_cc (FMT_CCD);
431 show_CCE (const struct dataset *ds UNUSED)
433 return show_cc (FMT_CCE);
437 parse_CELLSBREAK (struct lexer *lexer)
439 return parse_unimplemented (lexer, "CELLSBREAK");
443 parse_CMPTRANS (struct lexer *lexer)
445 return parse_unimplemented (lexer, "CMPTRANS");
449 parse_COMPRESSION (struct lexer *lexer)
451 return parse_unimplemented (lexer, "COMPRESSION");
455 parse_CTEMPLATE (struct lexer *lexer)
457 return parse_unimplemented (lexer, "CTEMPLATE");
461 parse_DECIMAL (struct lexer *lexer)
463 int decimal_char = force_parse_enum (lexer,
466 if (decimal_char != -1)
467 settings_set_decimal_char (decimal_char);
468 return decimal_char != -1;
472 show_DECIMAL (const struct dataset *ds UNUSED)
474 return xasprintf ("`%c'", settings_get_fmt_settings ()->decimal);
478 parse_EPOCH (struct lexer *lexer)
480 if (lex_match_id (lexer, "AUTOMATIC"))
481 settings_set_epoch (-1);
482 else if (lex_is_integer (lexer))
484 if (!lex_force_int_range (lexer, "EPOCH", 1500, INT_MAX))
486 settings_set_epoch (lex_integer (lexer));
491 lex_error (lexer, _("expecting %s or year"), "AUTOMATIC");
499 show_EPOCH (const struct dataset *ds UNUSED)
501 return xasprintf ("%d", settings_get_epoch ());
505 parse_ERRORS (struct lexer *lexer)
507 return parse_output_routing (lexer, SETTINGS_OUTPUT_ERROR);
511 show_ERRORS (const struct dataset *ds UNUSED)
513 return show_output_routing (SETTINGS_OUTPUT_ERROR);
517 parse_FORMAT (struct lexer *lexer)
521 lex_match (lexer, T_EQUALS);
522 if (!parse_format_specifier (lexer, &fmt))
525 if (!fmt_check_output (&fmt))
528 if (fmt_is_string (fmt.type))
530 char str[FMT_STRING_LEN_MAX + 1];
531 msg (SE, _("%s requires numeric output format as an argument. "
532 "Specified format %s is of type string."),
534 fmt_to_string (&fmt, str));
538 settings_set_format (&fmt);
543 show_FORMAT (const struct dataset *ds UNUSED)
545 char str[FMT_STRING_LEN_MAX + 1];
546 return xstrdup (fmt_to_string (settings_get_format (), str));
550 parse_FUZZBITS (struct lexer *lexer)
552 if (!lex_force_int_range (lexer, "FUZZITS", 0, 20))
554 settings_set_fuzzbits (lex_integer (lexer));
560 show_FUZZBITS (const struct dataset *ds UNUSED)
562 return xasprintf ("%d", settings_get_fuzzbits ());
566 parse_HEADER (struct lexer *lexer)
568 return parse_unimplemented (lexer, "HEADER");
572 parse_INCLUDE (struct lexer *lexer)
574 int include = force_parse_bool (lexer);
576 settings_set_include (include);
577 return include != -1;
581 show_INCLUDE (const struct dataset *ds UNUSED)
583 return xstrdup (settings_get_include () ? "ON" : "OFF");
587 parse_JOURNAL (struct lexer *lexer)
589 int b = parse_bool (lexer);
594 else if (lex_is_string (lexer) || lex_token (lexer) == T_ID)
596 char *filename = utf8_to_filename (lex_tokcstr (lexer));
597 journal_set_file_name (filename);
604 lex_error (lexer, NULL);
611 show_JOURNAL (const struct dataset *ds UNUSED)
613 const char *enabled = journal_is_enabled () ? "ON" : "OFF";
614 const char *file_name = journal_get_file_name ();
616 ? xasprintf ("%s (%s)", enabled, file_name)
617 : xstrdup (enabled));
621 parse_LEADZERO (struct lexer *lexer)
623 int leadzero = force_parse_bool (lexer);
625 settings_set_include_leading_zero (leadzero);
626 return leadzero != -1;
630 show_LEADZERO (const struct dataset *ds UNUSED)
632 bool leadzero = settings_get_fmt_settings ()->include_leading_zero;
633 return xstrdup (leadzero ? "ON" : "OFF");
637 parse_LENGTH (struct lexer *lexer)
641 if (lex_match_id (lexer, "NONE"))
645 if (!lex_force_int_range (lexer, "LENGTH", 1, INT_MAX))
647 page_length = lex_integer (lexer);
651 if (page_length != -1)
652 settings_set_viewlength (page_length);
658 show_LENGTH (const struct dataset *ds UNUSED)
660 return xasprintf ("%d", settings_get_viewlength ());
664 parse_LOCALE (struct lexer *lexer)
666 if (!lex_force_string (lexer))
669 /* Try the argument as an encoding name, then as a locale name or alias. */
670 const char *s = lex_tokcstr (lexer);
671 if (valid_encoding (s))
672 set_default_encoding (s);
673 else if (!set_encoding_from_locale (s))
675 msg (ME, _("%s is not a recognized encoding or locale name"), s);
684 show_LOCALE (const struct dataset *ds UNUSED)
686 return xstrdup (get_default_encoding ());
690 parse_MDISPLAY (struct lexer *lexer)
692 int mdisplay = force_parse_enum (lexer,
693 "TEXT", SETTINGS_MDISPLAY_TEXT,
694 "TABLES", SETTINGS_MDISPLAY_TABLES);
696 settings_set_mdisplay (mdisplay);
697 return mdisplay >= 0;
701 show_MDISPLAY (const struct dataset *ds UNUSED)
703 return xstrdup (settings_get_mdisplay () == SETTINGS_MDISPLAY_TEXT
704 ? "TEXT" : "TABLES");
708 parse_MESSAGES (struct lexer *lexer)
710 return parse_output_routing (lexer, SETTINGS_OUTPUT_NOTE);
714 show_MESSAGES (const struct dataset *ds UNUSED)
716 return show_output_routing (SETTINGS_OUTPUT_NOTE);
720 parse_MEXPAND (struct lexer *lexer)
722 int mexpand = force_parse_bool (lexer);
724 settings_set_mexpand (mexpand);
725 return mexpand != -1;
729 show_MEXPAND (const struct dataset *ds UNUSED)
731 return xstrdup (settings_get_mexpand () ? "ON" : "OFF");
735 parse_MITERATE (struct lexer *lexer)
737 if (!lex_force_int_range (lexer, "MITERATE", 1, INT_MAX))
739 settings_set_miterate (lex_integer (lexer));
745 show_MITERATE (const struct dataset *ds UNUSED)
747 return xasprintf ("%d", settings_get_miterate ());
751 parse_MNEST (struct lexer *lexer)
753 if (!lex_force_int_range (lexer, "MNEST", 1, INT_MAX))
755 settings_set_mnest (lex_integer (lexer));
761 show_MNEST (const struct dataset *ds UNUSED)
763 return xasprintf ("%d", settings_get_mnest ());
767 parse_MPRINT (struct lexer *lexer)
769 int mprint = force_parse_bool (lexer);
771 settings_set_mprint (mprint);
776 show_MPRINT (const struct dataset *ds UNUSED)
778 return xstrdup (settings_get_mprint () ? "ON" : "OFF");
782 parse_MXERRS (struct lexer *lexer)
785 if (!force_parse_int (lexer, &n))
789 settings_set_max_messages (MSG_S_ERROR, n);
791 msg (SE, _("%s must be at least 1."), "MXERRS");
796 show_MXERRS (const struct dataset *ds UNUSED)
798 return xasprintf ("%d", settings_get_max_messages (MSG_S_ERROR));
802 parse_MXLOOPS (struct lexer *lexer)
805 if (!force_parse_int (lexer, &n))
809 settings_set_mxloops (n);
811 msg (SE, _("%s must be at least 1."), "MXLOOPS");
816 show_MXLOOPS (const struct dataset *ds UNUSED)
818 return xasprintf ("%d", settings_get_mxloops ());
822 parse_MXWARNS (struct lexer *lexer)
825 if (!force_parse_int (lexer, &n))
829 settings_set_max_messages (MSG_S_WARNING, n);
831 msg (SE, _("%s must not be negative."), "MXWARNS");
836 show_MXWARNS (const struct dataset *ds UNUSED)
838 return xasprintf ("%d", settings_get_max_messages (MSG_S_WARNING));
842 parse_PRINTBACK (struct lexer *lexer)
844 return parse_output_routing (lexer, SETTINGS_OUTPUT_SYNTAX);
848 show_PRINTBACK (const struct dataset *ds UNUSED)
850 return show_output_routing (SETTINGS_OUTPUT_SYNTAX);
854 parse_RESULTS (struct lexer *lexer)
856 return parse_output_routing (lexer, SETTINGS_OUTPUT_RESULT);
860 show_RESULTS (const struct dataset *ds UNUSED)
862 return show_output_routing (SETTINGS_OUTPUT_RESULT);
866 parse_RIB (struct lexer *lexer)
868 return parse_integer_format (lexer, settings_set_input_integer_format);
872 show_RIB (const struct dataset *ds UNUSED)
874 return show_integer_format (settings_get_input_integer_format ());
878 parse_RRB (struct lexer *lexer)
880 return parse_real_format (lexer, settings_set_input_float_format);
884 show_RRB (const struct dataset *ds UNUSED)
886 return show_real_format (settings_get_input_float_format ());
890 parse_SAFER (struct lexer *lexer)
892 bool ok = force_parse_enum (lexer, "ON", true, "YES", true) != -1;
894 settings_set_safer_mode ();
899 show_SAFER (const struct dataset *ds UNUSED)
901 return xstrdup (settings_get_safer_mode () ? "ON" : "OFF");
905 parse_SCOMPRESSION (struct lexer *lexer)
907 int value = force_parse_bool (lexer);
909 settings_set_scompression (value);
914 show_SCOMPRESSION (const struct dataset *ds UNUSED)
916 return xstrdup (settings_get_scompression () ? "ON" : "OFF");
920 parse_SEED (struct lexer *lexer)
922 if (lex_match_id (lexer, "RANDOM"))
926 if (!lex_force_num (lexer))
928 set_rng (lex_number (lexer));
936 parse_SMALL (struct lexer *lexer)
938 if (!lex_force_num (lexer))
940 settings_set_small (lex_number (lexer));
946 show_SMALL (const struct dataset *ds UNUSED)
948 char buf[DBL_BUFSIZE_BOUND];
949 if (dtoastr (buf, sizeof buf, 0, 0, settings_get_small ()) < 0)
951 return xstrdup (buf);
955 show_SUBTITLE (const struct dataset *ds UNUSED)
957 return xstrdup (output_get_subtitle ());
961 show_TEMPDIR (const struct dataset *ds UNUSED)
963 return xstrdup (temp_dir_name ());
967 show_TITLE (const struct dataset *ds UNUSED)
969 return xstrdup (output_get_title ());
973 parse_TNUMBERS (struct lexer *lexer)
975 int value = force_parse_enum (lexer,
976 "LABELS", SETTINGS_VALUE_SHOW_LABEL,
977 "VALUES", SETTINGS_VALUE_SHOW_VALUE,
978 "BOTH", SETTINGS_VALUE_SHOW_BOTH);
980 settings_set_show_values (value);
985 show_TNUMBERS (const struct dataset *ds UNUSED)
987 enum settings_value_show tnumbers = settings_get_show_values ();
988 return xstrdup (tnumbers == SETTINGS_VALUE_SHOW_LABEL ? "LABELS"
989 : tnumbers == SETTINGS_VALUE_SHOW_VALUE ? "VALUES"
994 parse_TVARS (struct lexer *lexer)
996 int value = force_parse_enum (lexer,
997 "LABELS", SETTINGS_VALUE_SHOW_LABEL,
998 "NAMES", SETTINGS_VALUE_SHOW_VALUE,
999 "BOTH", SETTINGS_VALUE_SHOW_BOTH);
1001 settings_set_show_variables (value);
1006 show_TVARS (const struct dataset *ds UNUSED)
1008 enum settings_value_show tvars = settings_get_show_variables ();
1009 return xstrdup (tvars == SETTINGS_VALUE_SHOW_LABEL ? "LABELS"
1010 : tvars == SETTINGS_VALUE_SHOW_VALUE ? "NAMES"
1015 parse_TLOOK (struct lexer *lexer)
1017 if (lex_match_id (lexer, "NONE"))
1018 pivot_table_look_set_default (pivot_table_look_builtin_default ());
1019 else if (lex_is_string (lexer))
1021 struct pivot_table_look *look;
1022 char *error = pivot_table_look_read (lex_tokcstr (lexer), &look);
1027 msg (SE, "%s", error);
1032 pivot_table_look_set_default (look);
1033 pivot_table_look_unref (look);
1040 parse_UNDEFINED (struct lexer *lexer)
1042 int value = force_parse_enum (lexer,
1046 settings_set_undefined (value);
1051 show_UNDEFINED (const struct dataset *ds UNUSED)
1053 return xstrdup (settings_get_undefined () ? "WARN" : "NOWARN");
1057 show_VERSION (const struct dataset *ds UNUSED)
1059 return strdup (announced_version);
1063 show_WEIGHT (const struct dataset *ds)
1065 const struct variable *var = dict_get_weight (dataset_dict (ds));
1066 return xstrdup (var != NULL ? var_get_name (var) : "OFF");
1070 parse_WIB (struct lexer *lexer)
1072 return parse_integer_format (lexer, settings_set_output_integer_format);
1076 show_WIB (const struct dataset *ds UNUSED)
1078 return show_integer_format (settings_get_output_integer_format ());
1082 parse_WRB (struct lexer *lexer)
1084 return parse_real_format (lexer, settings_set_output_float_format);
1088 show_WRB (const struct dataset *ds UNUSED)
1090 return show_real_format (settings_get_output_float_format ());
1094 parse_WIDTH (struct lexer *lexer)
1096 if (lex_match_id (lexer, "NARROW"))
1097 settings_set_viewwidth (79);
1098 else if (lex_match_id (lexer, "WIDE"))
1099 settings_set_viewwidth (131);
1102 if (!lex_force_int_range (lexer, "WIDTH", 40, INT_MAX))
1104 settings_set_viewwidth (lex_integer (lexer));
1112 show_WIDTH (const struct dataset *ds UNUSED)
1114 return xasprintf ("%d", settings_get_viewwidth ());
1118 parse_WORKSPACE (struct lexer *lexer)
1120 if (!lex_force_int_range (lexer, "WORKSPACE",
1121 settings_get_testing_mode () ? 1 : 1024,
1124 int workspace = lex_integer (lexer);
1126 settings_set_workspace (MIN (workspace, INT_MAX / 1024) * 1024);
1131 show_WORKSPACE (const struct dataset *ds UNUSED)
1133 size_t ws = settings_get_workspace () / 1024L;
1134 return xasprintf ("%zu", ws);
1138 show_DIRECTORY (const struct dataset *ds UNUSED)
1147 buf = xrealloc (buf, len);
1149 while (NULL == (wd = getcwd (buf, len)));
1155 show_N (const struct dataset *ds)
1157 const struct casereader *reader = dataset_source (ds);
1159 ? xasprintf ("%lld", (long long int) casereader_count_cases (reader))
1160 : xstrdup (_("Unknown")));
1164 do_show (const struct dataset *ds, const struct setting *s)
1166 char *value = s->show (ds);
1167 msg (SN, _("%s is %s."), s->name, value ? value : _("empty"));
1172 show_warranty (const struct dataset *ds UNUSED)
1174 fputs (lack_of_warranty, stdout);
1178 show_copying (const struct dataset *ds UNUSED)
1180 fputs (copyleft, stdout);
1184 add_row (struct pivot_table *table, const char *attribute,
1187 int row = pivot_category_create_leaf (table->dimensions[0]->root,
1188 pivot_value_new_text (attribute));
1190 pivot_table_put1 (table, row, pivot_value_new_user_text (value, -1));
1194 show_system (const struct dataset *ds UNUSED)
1196 struct pivot_table *table = pivot_table_create (N_("System Information"));
1197 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Attribute"));
1199 add_row (table, N_("Version"), version);
1200 add_row (table, N_("Host System"), host_system);
1201 add_row (table, N_("Build System"), build_system);
1202 add_row (table, N_("Locale Directory"), relocate (locale_dir));
1203 add_row (table, N_("Compiler Version"),
1210 pivot_table_submit (table);
1213 static const struct setting settings[] = {
1214 { "BASETEXTDIRECTION", parse_BASETEXTDIRECTION, NULL },
1215 { "BLANKS", parse_BLANKS, show_BLANKS },
1216 { "BLOCK", parse_BLOCK, NULL },
1217 { "BOX", parse_BOX, NULL },
1218 { "CACHE", parse_CACHE, NULL },
1219 { "CCA", parse_CCA, show_CCA },
1220 { "CCB", parse_CCB, show_CCB },
1221 { "CCC", parse_CCC, show_CCC },
1222 { "CCD", parse_CCD, show_CCD },
1223 { "CCE", parse_CCE, show_CCE },
1224 { "CELLSBREAK", parse_CELLSBREAK, NULL },
1225 { "CMPTRANS", parse_CMPTRANS, NULL },
1226 { "COMPRESSION", parse_COMPRESSION, NULL },
1227 { "CTEMPLATE", parse_CTEMPLATE, NULL },
1228 { "DECIMAL", parse_DECIMAL, show_DECIMAL },
1229 { "DIRECTORY", NULL, show_DIRECTORY },
1230 { "EPOCH", parse_EPOCH, show_EPOCH },
1231 { "ERRORS", parse_ERRORS, show_ERRORS },
1232 { "FORMAT", parse_FORMAT, show_FORMAT },
1233 { "FUZZBITS", parse_FUZZBITS, show_FUZZBITS },
1234 { "HEADER", parse_HEADER, NULL },
1235 { "INCLUDE", parse_INCLUDE, show_INCLUDE },
1236 { "JOURNAL", parse_JOURNAL, show_JOURNAL },
1237 { "LEADZERO", parse_LEADZERO, show_LEADZERO },
1238 { "LENGTH", parse_LENGTH, show_LENGTH },
1239 { "LOCALE", parse_LOCALE, show_LOCALE },
1240 { "MDISPLAY", parse_MDISPLAY, show_MDISPLAY },
1241 { "MESSAGES", parse_MESSAGES, show_MESSAGES },
1242 { "MEXPAND", parse_MEXPAND, show_MEXPAND },
1243 { "MITERATE", parse_MITERATE, show_MITERATE },
1244 { "MNEST", parse_MNEST, show_MNEST },
1245 { "MPRINT", parse_MPRINT, show_MPRINT },
1246 { "MXERRS", parse_MXERRS, show_MXERRS },
1247 { "MXLOOPS", parse_MXLOOPS, show_MXLOOPS },
1248 { "MXWARNS", parse_MXWARNS, show_MXWARNS },
1249 { "N", NULL, show_N },
1250 { "PRINTBACK", parse_PRINTBACK, show_PRINTBACK },
1251 { "RESULTS", parse_RESULTS, show_RESULTS },
1252 { "RIB", parse_RIB, show_RIB },
1253 { "RRB", parse_RRB, show_RRB },
1254 { "SAFER", parse_SAFER, show_SAFER },
1255 { "SCOMPRESSION", parse_SCOMPRESSION, show_SCOMPRESSION },
1256 { "SEED", parse_SEED, NULL },
1257 { "SMALL", parse_SMALL, show_SMALL },
1258 { "TEMPDIR", NULL, show_TEMPDIR },
1259 { "TNUMBERS", parse_TNUMBERS, show_TNUMBERS },
1260 { "TVARS", parse_TVARS, show_TVARS },
1261 { "TLOOK", parse_TLOOK, NULL },
1262 { "UNDEFINED", parse_UNDEFINED, show_UNDEFINED },
1263 { "VERSION", NULL, show_VERSION },
1264 { "WEIGHT", NULL, show_WEIGHT },
1265 { "WIB", parse_WIB, show_WIB },
1266 { "WRB", parse_WRB, show_WRB },
1267 { "WIDTH", parse_WIDTH, show_WIDTH },
1268 { "WORKSPACE", parse_WORKSPACE, show_WORKSPACE },
1270 enum { N_SETTINGS = sizeof settings / sizeof *settings };
1273 parse_setting (struct lexer *lexer)
1275 for (size_t i = 0; i < N_SETTINGS; i++)
1276 if (settings[i].set && match_subcommand (lexer, settings[i].name))
1277 return settings[i].set (lexer);
1279 lex_error (lexer, NULL);
1284 cmd_set (struct lexer *lexer, struct dataset *ds UNUSED)
1288 lex_match (lexer, T_SLASH);
1289 if (lex_token (lexer) == T_ENDCMD)
1292 if (!parse_setting (lexer))
1300 show_all (const struct dataset *ds)
1302 for (size_t i = 0; i < sizeof settings / sizeof *settings; i++)
1303 if (settings[i].show)
1304 do_show (ds, &settings[i]);
1308 show_all_cc (const struct dataset *ds)
1310 for (size_t i = 0; i < sizeof settings / sizeof *settings; i++)
1312 const struct setting *s = &settings[i];
1313 if (s->show && !strncmp (s->name, "CC", 2))
1319 cmd_show (struct lexer *lexer, struct dataset *ds)
1321 if (lex_token (lexer) == T_ENDCMD)
1329 if (lex_match (lexer, T_ALL))
1331 else if (lex_match_id (lexer, "CC"))
1333 else if (lex_match_id (lexer, "WARRANTY"))
1335 else if (lex_match_id (lexer, "COPYING") || lex_match_id (lexer, "LICENSE"))
1337 else if (lex_match_id (lexer, "SYSTEM"))
1339 else if (lex_match_id (lexer, "TITLE"))
1341 struct setting s = { .name = "TITLE", .show = show_TITLE };
1344 else if (lex_match_id (lexer, "SUBTITLE"))
1346 struct setting s = { .name = "SUBTITLE", .show = show_SUBTITLE };
1349 else if (lex_token (lexer) == T_ID)
1353 for (i = 0; i < sizeof settings / sizeof *settings; i++)
1355 const struct setting *s = &settings[i];
1356 if (s->show && lex_match_id (lexer, s->name))
1362 lex_error (lexer, NULL);
1369 lex_error (lexer, NULL);
1373 lex_match (lexer, T_SLASH);
1375 while (lex_token (lexer) != T_ENDCMD);
1380 #define MAX_SAVED_SETTINGS 5
1382 static struct settings *saved_settings[MAX_SAVED_SETTINGS];
1383 static int n_saved_settings;
1386 cmd_preserve (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
1388 if (n_saved_settings < MAX_SAVED_SETTINGS)
1390 saved_settings[n_saved_settings++] = settings_get ();
1395 msg (SE, _("Too many %s commands without a %s: at most "
1396 "%d levels of saved settings are allowed."),
1397 "PRESERVE", "RESTORE",
1398 MAX_SAVED_SETTINGS);
1399 return CMD_CASCADING_FAILURE;
1404 cmd_restore (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
1406 if (n_saved_settings > 0)
1408 struct settings *s = saved_settings[--n_saved_settings];
1410 settings_destroy (s);
1415 msg (SE, _("%s without matching %s."), "RESTORE", "PRESERVE");