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 char *error = settings_set_cc (lex_tokcstr (lexer), ccx);
321 lex_error (lexer, "%s", error);
331 parse_BASETEXTDIRECTION (struct lexer *lexer)
333 return parse_unimplemented (lexer, "BASETEXTDIRECTION");
337 parse_BLANKS (struct lexer *lexer)
339 if (lex_match_id (lexer, "SYSMIS"))
340 settings_set_blanks (SYSMIS);
343 if (!lex_force_num (lexer))
345 settings_set_blanks (lex_number (lexer));
352 show_BLANKS (const struct dataset *ds UNUSED)
354 return (settings_get_blanks () == SYSMIS
356 : xasprintf ("%.*g", DBL_DIG + 1, settings_get_blanks ()));
360 parse_BLOCK (struct lexer *lexer)
362 return parse_unimplemented (lexer, "BLOCK");
366 parse_BOX (struct lexer *lexer)
368 return parse_unimplemented (lexer, "BOX");
372 parse_CACHE (struct lexer *lexer)
374 return parse_unimplemented (lexer, "CACHE");
378 parse_CCA (struct lexer *lexer)
380 return parse_ccx (lexer, FMT_CCA);
384 parse_CCB (struct lexer *lexer)
386 return parse_ccx (lexer, FMT_CCB);
390 parse_CCC (struct lexer *lexer)
392 return parse_ccx (lexer, FMT_CCC);
396 parse_CCD (struct lexer *lexer)
398 return parse_ccx (lexer, FMT_CCD);
402 parse_CCE (struct lexer *lexer)
404 return parse_ccx (lexer, FMT_CCE);
408 show_cc (enum fmt_type type)
410 return fmt_number_style_to_string (fmt_settings_get_style (
411 settings_get_fmt_settings (), type));
415 show_CCA (const struct dataset *ds UNUSED)
417 return show_cc (FMT_CCA);
421 show_CCB (const struct dataset *ds UNUSED)
423 return show_cc (FMT_CCB);
427 show_CCC (const struct dataset *ds UNUSED)
429 return show_cc (FMT_CCC);
433 show_CCD (const struct dataset *ds UNUSED)
435 return show_cc (FMT_CCD);
439 show_CCE (const struct dataset *ds UNUSED)
441 return show_cc (FMT_CCE);
445 parse_CELLSBREAK (struct lexer *lexer)
447 return parse_unimplemented (lexer, "CELLSBREAK");
451 parse_CMPTRANS (struct lexer *lexer)
453 return parse_unimplemented (lexer, "CMPTRANS");
457 parse_COMPRESSION (struct lexer *lexer)
459 return parse_unimplemented (lexer, "COMPRESSION");
463 parse_CTEMPLATE (struct lexer *lexer)
465 return parse_unimplemented (lexer, "CTEMPLATE");
469 parse_DECIMAL (struct lexer *lexer)
471 int decimal_char = force_parse_enum (lexer,
474 if (decimal_char != -1)
475 settings_set_decimal_char (decimal_char);
476 return decimal_char != -1;
480 show_DECIMAL (const struct dataset *ds UNUSED)
482 return xasprintf ("`%c'", settings_get_fmt_settings ()->decimal);
486 parse_EPOCH (struct lexer *lexer)
488 if (lex_match_id (lexer, "AUTOMATIC"))
489 settings_set_epoch (-1);
490 else if (lex_is_integer (lexer))
492 if (!lex_force_int_range (lexer, "EPOCH", 1500, INT_MAX))
494 settings_set_epoch (lex_integer (lexer));
499 lex_error (lexer, _("Syntax error expecting %s or year."), "AUTOMATIC");
507 show_EPOCH (const struct dataset *ds UNUSED)
509 return xasprintf ("%d", settings_get_epoch ());
513 parse_ERRORS (struct lexer *lexer)
515 return parse_output_routing (lexer, SETTINGS_OUTPUT_ERROR);
519 show_ERRORS (const struct dataset *ds UNUSED)
521 return show_output_routing (SETTINGS_OUTPUT_ERROR);
525 parse_FORMAT (struct lexer *lexer)
527 int start = subcommand_start_ofs (lexer);
530 if (!parse_format_specifier (lexer, &fmt))
533 char *error = fmt_check_output__ (&fmt);
536 lex_next_error (lexer, -1, -1, "%s", error);
541 int end = lex_ofs (lexer) - 1;
542 if (fmt_is_string (fmt.type))
544 char str[FMT_STRING_LEN_MAX + 1];
545 lex_ofs_error (lexer, start, end,
546 _("%s requires numeric output format as an argument. "
547 "Specified format %s is of type string."),
548 "FORMAT", fmt_to_string (&fmt, str));
552 settings_set_format (&fmt);
557 show_FORMAT (const struct dataset *ds UNUSED)
559 char str[FMT_STRING_LEN_MAX + 1];
560 return xstrdup (fmt_to_string (settings_get_format (), str));
564 parse_FUZZBITS (struct lexer *lexer)
566 if (!lex_force_int_range (lexer, "FUZZITS", 0, 20))
568 settings_set_fuzzbits (lex_integer (lexer));
574 show_FUZZBITS (const struct dataset *ds UNUSED)
576 return xasprintf ("%d", settings_get_fuzzbits ());
580 parse_HEADER (struct lexer *lexer)
582 return parse_unimplemented (lexer, "HEADER");
586 parse_INCLUDE (struct lexer *lexer)
588 int include = force_parse_bool (lexer);
590 settings_set_include (include);
591 return include != -1;
595 show_INCLUDE (const struct dataset *ds UNUSED)
597 return xstrdup (settings_get_include () ? "ON" : "OFF");
601 parse_JOURNAL (struct lexer *lexer)
603 int b = parse_bool (lexer);
608 else if (lex_is_string (lexer) || lex_token (lexer) == T_ID)
610 char *filename = utf8_to_filename (lex_tokcstr (lexer));
611 journal_set_file_name (filename);
618 lex_error (lexer, NULL);
625 show_JOURNAL (const struct dataset *ds UNUSED)
627 const char *enabled = journal_is_enabled () ? "ON" : "OFF";
628 const char *file_name = journal_get_file_name ();
630 ? xasprintf ("%s (%s)", enabled, file_name)
631 : xstrdup (enabled));
635 parse_LEADZERO (struct lexer *lexer)
637 int leadzero = force_parse_bool (lexer);
639 settings_set_include_leading_zero (leadzero);
640 return leadzero != -1;
644 show_LEADZERO (const struct dataset *ds UNUSED)
646 bool leadzero = settings_get_fmt_settings ()->include_leading_zero;
647 return xstrdup (leadzero ? "ON" : "OFF");
651 parse_LENGTH (struct lexer *lexer)
655 if (lex_match_id (lexer, "NONE"))
659 if (!lex_force_int_range (lexer, "LENGTH", 1, INT_MAX))
661 page_length = lex_integer (lexer);
665 if (page_length != -1)
666 settings_set_viewlength (page_length);
672 show_LENGTH (const struct dataset *ds UNUSED)
674 return xasprintf ("%d", settings_get_viewlength ());
678 parse_LOCALE (struct lexer *lexer)
680 if (!lex_force_string (lexer))
683 /* Try the argument as an encoding name, then as a locale name or alias. */
684 const char *s = lex_tokcstr (lexer);
685 if (valid_encoding (s))
686 set_default_encoding (s);
687 else if (!set_encoding_from_locale (s))
689 lex_error (lexer, _("%s is not a recognized encoding or locale name"), s);
698 show_LOCALE (const struct dataset *ds UNUSED)
700 return xstrdup (get_default_encoding ());
704 parse_MDISPLAY (struct lexer *lexer)
706 int mdisplay = force_parse_enum (lexer,
707 "TEXT", SETTINGS_MDISPLAY_TEXT,
708 "TABLES", SETTINGS_MDISPLAY_TABLES);
710 settings_set_mdisplay (mdisplay);
711 return mdisplay >= 0;
715 show_MDISPLAY (const struct dataset *ds UNUSED)
717 return xstrdup (settings_get_mdisplay () == SETTINGS_MDISPLAY_TEXT
718 ? "TEXT" : "TABLES");
722 parse_MESSAGES (struct lexer *lexer)
724 return parse_output_routing (lexer, SETTINGS_OUTPUT_NOTE);
728 show_MESSAGES (const struct dataset *ds UNUSED)
730 return show_output_routing (SETTINGS_OUTPUT_NOTE);
734 parse_MEXPAND (struct lexer *lexer)
736 int mexpand = force_parse_bool (lexer);
738 settings_set_mexpand (mexpand);
739 return mexpand != -1;
743 show_MEXPAND (const struct dataset *ds UNUSED)
745 return xstrdup (settings_get_mexpand () ? "ON" : "OFF");
749 parse_MITERATE (struct lexer *lexer)
751 if (!lex_force_int_range (lexer, "MITERATE", 1, INT_MAX))
753 settings_set_miterate (lex_integer (lexer));
759 show_MITERATE (const struct dataset *ds UNUSED)
761 return xasprintf ("%d", settings_get_miterate ());
765 parse_MNEST (struct lexer *lexer)
767 if (!lex_force_int_range (lexer, "MNEST", 1, INT_MAX))
769 settings_set_mnest (lex_integer (lexer));
775 show_MNEST (const struct dataset *ds UNUSED)
777 return xasprintf ("%d", settings_get_mnest ());
781 parse_MPRINT (struct lexer *lexer)
783 int mprint = force_parse_bool (lexer);
785 settings_set_mprint (mprint);
790 show_MPRINT (const struct dataset *ds UNUSED)
792 return xstrdup (settings_get_mprint () ? "ON" : "OFF");
796 parse_MXERRS (struct lexer *lexer)
798 if (!lex_force_int_range (lexer, "MXERRS", 1, INT_MAX))
800 settings_set_max_messages (MSG_S_ERROR, lex_integer (lexer));
806 show_MXERRS (const struct dataset *ds UNUSED)
808 return xasprintf ("%d", settings_get_max_messages (MSG_S_ERROR));
812 parse_MXLOOPS (struct lexer *lexer)
814 if (!lex_force_int_range (lexer, "MXLOOPS", 1, INT_MAX))
816 settings_set_mxloops (lex_integer (lexer));
822 show_MXLOOPS (const struct dataset *ds UNUSED)
824 return xasprintf ("%d", settings_get_mxloops ());
828 parse_MXWARNS (struct lexer *lexer)
830 if (!lex_force_int_range (lexer, "MXWARNS", 0, INT_MAX))
832 settings_set_max_messages (MSG_S_WARNING, lex_integer (lexer));
838 show_MXWARNS (const struct dataset *ds UNUSED)
840 return xasprintf ("%d", settings_get_max_messages (MSG_S_WARNING));
844 parse_PRINTBACK (struct lexer *lexer)
846 return parse_output_routing (lexer, SETTINGS_OUTPUT_SYNTAX);
850 show_PRINTBACK (const struct dataset *ds UNUSED)
852 return show_output_routing (SETTINGS_OUTPUT_SYNTAX);
856 parse_RESULTS (struct lexer *lexer)
858 return parse_output_routing (lexer, SETTINGS_OUTPUT_RESULT);
862 show_RESULTS (const struct dataset *ds UNUSED)
864 return show_output_routing (SETTINGS_OUTPUT_RESULT);
868 parse_RIB (struct lexer *lexer)
870 return parse_integer_format (lexer, settings_set_input_integer_format);
874 show_RIB (const struct dataset *ds UNUSED)
876 return show_integer_format (settings_get_input_integer_format ());
880 parse_RRB (struct lexer *lexer)
882 return parse_real_format (lexer, settings_set_input_float_format);
886 show_RRB (const struct dataset *ds UNUSED)
888 return show_real_format (settings_get_input_float_format ());
892 parse_SAFER (struct lexer *lexer)
894 bool ok = force_parse_enum (lexer, "ON", true, "YES", true) != -1;
896 settings_set_safer_mode ();
901 show_SAFER (const struct dataset *ds UNUSED)
903 return xstrdup (settings_get_safer_mode () ? "ON" : "OFF");
907 parse_SCOMPRESSION (struct lexer *lexer)
909 int value = force_parse_bool (lexer);
911 settings_set_scompression (value);
916 show_SCOMPRESSION (const struct dataset *ds UNUSED)
918 return xstrdup (settings_get_scompression () ? "ON" : "OFF");
922 parse_SEED (struct lexer *lexer)
924 if (lex_match_id (lexer, "RANDOM"))
928 if (!lex_force_num (lexer))
930 set_rng (lex_number (lexer));
938 parse_SMALL (struct lexer *lexer)
940 if (!lex_force_num (lexer))
942 settings_set_small (lex_number (lexer));
948 show_SMALL (const struct dataset *ds UNUSED)
950 char buf[DBL_BUFSIZE_BOUND];
951 if (dtoastr (buf, sizeof buf, 0, 0, settings_get_small ()) < 0)
953 return xstrdup (buf);
957 show_SUBTITLE (const struct dataset *ds UNUSED)
959 return xstrdup (output_get_subtitle ());
963 show_TEMPDIR (const struct dataset *ds UNUSED)
965 return xstrdup (temp_dir_name ());
969 show_TITLE (const struct dataset *ds UNUSED)
971 return xstrdup (output_get_title ());
975 parse_TNUMBERS (struct lexer *lexer)
977 int value = force_parse_enum (lexer,
978 "LABELS", SETTINGS_VALUE_SHOW_LABEL,
979 "VALUES", SETTINGS_VALUE_SHOW_VALUE,
980 "BOTH", SETTINGS_VALUE_SHOW_BOTH);
982 settings_set_show_values (value);
987 show_TNUMBERS (const struct dataset *ds UNUSED)
989 enum settings_value_show tnumbers = settings_get_show_values ();
990 return xstrdup (tnumbers == SETTINGS_VALUE_SHOW_LABEL ? "LABELS"
991 : tnumbers == SETTINGS_VALUE_SHOW_VALUE ? "VALUES"
996 parse_TVARS (struct lexer *lexer)
998 int value = force_parse_enum (lexer,
999 "LABELS", SETTINGS_VALUE_SHOW_LABEL,
1000 "NAMES", SETTINGS_VALUE_SHOW_VALUE,
1001 "BOTH", SETTINGS_VALUE_SHOW_BOTH);
1003 settings_set_show_variables (value);
1008 show_TVARS (const struct dataset *ds UNUSED)
1010 enum settings_value_show tvars = settings_get_show_variables ();
1011 return xstrdup (tvars == SETTINGS_VALUE_SHOW_LABEL ? "LABELS"
1012 : tvars == SETTINGS_VALUE_SHOW_VALUE ? "NAMES"
1017 parse_TLOOK (struct lexer *lexer)
1019 if (lex_match_id (lexer, "NONE"))
1020 pivot_table_look_set_default (pivot_table_look_builtin_default ());
1021 else if (lex_is_string (lexer))
1023 struct pivot_table_look *look;
1024 char *error = pivot_table_look_read (lex_tokcstr (lexer), &look);
1029 msg (SE, "%s", error);
1034 pivot_table_look_set_default (look);
1035 pivot_table_look_unref (look);
1042 parse_UNDEFINED (struct lexer *lexer)
1044 int value = force_parse_enum (lexer,
1048 settings_set_undefined (value);
1053 show_UNDEFINED (const struct dataset *ds UNUSED)
1055 return xstrdup (settings_get_undefined () ? "WARN" : "NOWARN");
1059 show_VERSION (const struct dataset *ds UNUSED)
1061 return strdup (announced_version);
1065 show_WEIGHT (const struct dataset *ds)
1067 const struct variable *var = dict_get_weight (dataset_dict (ds));
1068 return xstrdup (var != NULL ? var_get_name (var) : "OFF");
1072 parse_WIB (struct lexer *lexer)
1074 return parse_integer_format (lexer, settings_set_output_integer_format);
1078 show_WIB (const struct dataset *ds UNUSED)
1080 return show_integer_format (settings_get_output_integer_format ());
1084 parse_WRB (struct lexer *lexer)
1086 return parse_real_format (lexer, settings_set_output_float_format);
1090 show_WRB (const struct dataset *ds UNUSED)
1092 return show_real_format (settings_get_output_float_format ());
1096 parse_WIDTH (struct lexer *lexer)
1098 if (lex_match_id (lexer, "NARROW"))
1099 settings_set_viewwidth (79);
1100 else if (lex_match_id (lexer, "WIDE"))
1101 settings_set_viewwidth (131);
1104 if (!lex_force_int_range (lexer, "WIDTH", 40, INT_MAX))
1106 settings_set_viewwidth (lex_integer (lexer));
1114 show_WIDTH (const struct dataset *ds UNUSED)
1116 return xasprintf ("%d", settings_get_viewwidth ());
1120 parse_WORKSPACE (struct lexer *lexer)
1122 if (!lex_force_int_range (lexer, "WORKSPACE",
1123 settings_get_testing_mode () ? 1 : 1024,
1126 int workspace = lex_integer (lexer);
1128 settings_set_workspace (MIN (workspace, INT_MAX / 1024) * 1024);
1133 show_WORKSPACE (const struct dataset *ds UNUSED)
1135 size_t ws = settings_get_workspace () / 1024L;
1136 return xasprintf ("%zu", ws);
1140 show_DIRECTORY (const struct dataset *ds UNUSED)
1149 buf = xrealloc (buf, len);
1151 while (NULL == (wd = getcwd (buf, len)));
1157 show_N (const struct dataset *ds)
1159 const struct casereader *reader = dataset_source (ds);
1161 ? xasprintf ("%lld", (long long int) casereader_count_cases (reader))
1162 : xstrdup (_("Unknown")));
1166 do_show (const struct dataset *ds, const struct setting *s,
1167 struct pivot_table **ptp)
1169 struct pivot_table *pt = *ptp;
1172 pt = *ptp = pivot_table_create (N_("Settings"));
1173 pivot_dimension_create (pt, PIVOT_AXIS_ROW, N_("Setting"));
1176 struct pivot_value *name = pivot_value_new_user_text (s->name, SIZE_MAX);
1177 char *text = s->show (ds);
1179 text = xstrdup("empty");
1180 struct pivot_value *value = pivot_value_new_user_text_nocopy (text);
1182 int row = pivot_category_create_leaf (pt->dimensions[0]->root, name);
1183 pivot_table_put1 (pt, row, value);
1187 show_warranty (const struct dataset *ds UNUSED)
1189 fputs (lack_of_warranty, stdout);
1193 show_copying (const struct dataset *ds UNUSED)
1195 fputs (copyleft, stdout);
1199 add_row (struct pivot_table *table, const char *attribute,
1202 int row = pivot_category_create_leaf (table->dimensions[0]->root,
1203 pivot_value_new_text (attribute));
1205 pivot_table_put1 (table, row, pivot_value_new_user_text (value, -1));
1209 show_system (const struct dataset *ds UNUSED)
1211 struct pivot_table *table = pivot_table_create (N_("System Information"));
1212 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Attribute"));
1214 add_row (table, N_("Version"), version);
1215 add_row (table, N_("Host System"), host_system);
1216 add_row (table, N_("Build System"), build_system);
1217 add_row (table, N_("Locale Directory"), relocate (locale_dir));
1218 add_row (table, N_("Compiler Version"),
1225 pivot_table_submit (table);
1228 static const struct setting settings[] = {
1229 { "BASETEXTDIRECTION", parse_BASETEXTDIRECTION, NULL },
1230 { "BLANKS", parse_BLANKS, show_BLANKS },
1231 { "BLOCK", parse_BLOCK, NULL },
1232 { "BOX", parse_BOX, NULL },
1233 { "CACHE", parse_CACHE, NULL },
1234 { "CCA", parse_CCA, show_CCA },
1235 { "CCB", parse_CCB, show_CCB },
1236 { "CCC", parse_CCC, show_CCC },
1237 { "CCD", parse_CCD, show_CCD },
1238 { "CCE", parse_CCE, show_CCE },
1239 { "CELLSBREAK", parse_CELLSBREAK, NULL },
1240 { "CMPTRANS", parse_CMPTRANS, NULL },
1241 { "COMPRESSION", parse_COMPRESSION, NULL },
1242 { "CTEMPLATE", parse_CTEMPLATE, NULL },
1243 { "DECIMAL", parse_DECIMAL, show_DECIMAL },
1244 { "DIRECTORY", NULL, show_DIRECTORY },
1245 { "EPOCH", parse_EPOCH, show_EPOCH },
1246 { "ERRORS", parse_ERRORS, show_ERRORS },
1247 { "FORMAT", parse_FORMAT, show_FORMAT },
1248 { "FUZZBITS", parse_FUZZBITS, show_FUZZBITS },
1249 { "HEADER", parse_HEADER, NULL },
1250 { "INCLUDE", parse_INCLUDE, show_INCLUDE },
1251 { "JOURNAL", parse_JOURNAL, show_JOURNAL },
1252 { "LEADZERO", parse_LEADZERO, show_LEADZERO },
1253 { "LENGTH", parse_LENGTH, show_LENGTH },
1254 { "LOCALE", parse_LOCALE, show_LOCALE },
1255 { "MDISPLAY", parse_MDISPLAY, show_MDISPLAY },
1256 { "MESSAGES", parse_MESSAGES, show_MESSAGES },
1257 { "MEXPAND", parse_MEXPAND, show_MEXPAND },
1258 { "MITERATE", parse_MITERATE, show_MITERATE },
1259 { "MNEST", parse_MNEST, show_MNEST },
1260 { "MPRINT", parse_MPRINT, show_MPRINT },
1261 { "MXERRS", parse_MXERRS, show_MXERRS },
1262 { "MXLOOPS", parse_MXLOOPS, show_MXLOOPS },
1263 { "MXWARNS", parse_MXWARNS, show_MXWARNS },
1264 { "N", NULL, show_N },
1265 { "PRINTBACK", parse_PRINTBACK, show_PRINTBACK },
1266 { "RESULTS", parse_RESULTS, show_RESULTS },
1267 { "RIB", parse_RIB, show_RIB },
1268 { "RRB", parse_RRB, show_RRB },
1269 { "SAFER", parse_SAFER, show_SAFER },
1270 { "SCOMPRESSION", parse_SCOMPRESSION, show_SCOMPRESSION },
1271 { "SEED", parse_SEED, NULL },
1272 { "SMALL", parse_SMALL, show_SMALL },
1273 { "TEMPDIR", NULL, show_TEMPDIR },
1274 { "TNUMBERS", parse_TNUMBERS, show_TNUMBERS },
1275 { "TVARS", parse_TVARS, show_TVARS },
1276 { "TLOOK", parse_TLOOK, NULL },
1277 { "UNDEFINED", parse_UNDEFINED, show_UNDEFINED },
1278 { "VERSION", NULL, show_VERSION },
1279 { "WEIGHT", NULL, show_WEIGHT },
1280 { "WIB", parse_WIB, show_WIB },
1281 { "WRB", parse_WRB, show_WRB },
1282 { "WIDTH", parse_WIDTH, show_WIDTH },
1283 { "WORKSPACE", parse_WORKSPACE, show_WORKSPACE },
1285 enum { N_SETTINGS = sizeof settings / sizeof *settings };
1288 parse_setting (struct lexer *lexer)
1290 for (size_t i = 0; i < N_SETTINGS; i++)
1291 if (settings[i].set && match_subcommand (lexer, settings[i].name))
1292 return settings[i].set (lexer);
1294 lex_error (lexer, NULL);
1299 cmd_set (struct lexer *lexer, struct dataset *ds UNUSED)
1303 lex_match (lexer, T_SLASH);
1304 if (lex_token (lexer) == T_ENDCMD)
1307 if (!parse_setting (lexer))
1315 show_all (const struct dataset *ds, struct pivot_table **ptp)
1317 for (size_t i = 0; i < sizeof settings / sizeof *settings; i++)
1318 if (settings[i].show)
1319 do_show (ds, &settings[i], ptp);
1323 show_all_cc (const struct dataset *ds, struct pivot_table **ptp)
1325 for (size_t i = 0; i < sizeof settings / sizeof *settings; i++)
1327 const struct setting *s = &settings[i];
1328 if (s->show && !strncmp (s->name, "CC", 2))
1329 do_show (ds, s, ptp);
1334 cmd_show (struct lexer *lexer, struct dataset *ds)
1336 struct pivot_table *pt = NULL;
1337 if (lex_token (lexer) == T_ENDCMD)
1340 pivot_table_submit (pt);
1346 if (lex_match (lexer, T_ALL))
1348 else if (lex_match_id (lexer, "CC"))
1349 show_all_cc (ds, &pt);
1350 else if (lex_match_id (lexer, "WARRANTY"))
1352 else if (lex_match_id (lexer, "COPYING") || lex_match_id (lexer, "LICENSE"))
1354 else if (lex_match_id (lexer, "SYSTEM"))
1356 else if (lex_match_id (lexer, "TITLE"))
1358 struct setting s = { .name = "TITLE", .show = show_TITLE };
1359 do_show (ds, &s, &pt);
1361 else if (lex_match_id (lexer, "SUBTITLE"))
1363 struct setting s = { .name = "SUBTITLE", .show = show_SUBTITLE };
1364 do_show (ds, &s, &pt);
1366 else if (lex_token (lexer) == T_ID)
1368 for (size_t i = 0; i < sizeof settings / sizeof *settings; i++)
1370 const struct setting *s = &settings[i];
1371 if (s->show && lex_match_id (lexer, s->name))
1373 do_show (ds, s, &pt);
1377 lex_error (lexer, NULL);
1384 lex_error (lexer, NULL);
1388 lex_match (lexer, T_SLASH);
1390 while (lex_token (lexer) != T_ENDCMD);
1393 pivot_table_submit (pt);
1398 #define MAX_SAVED_SETTINGS 5
1400 static struct settings *saved_settings[MAX_SAVED_SETTINGS];
1401 static int n_saved_settings;
1404 cmd_preserve (struct lexer *lexer, struct dataset *ds UNUSED)
1406 if (n_saved_settings < MAX_SAVED_SETTINGS)
1408 saved_settings[n_saved_settings++] = settings_get ();
1413 lex_next_error (lexer, -1, -1,
1414 _("Too many %s commands without a %s: at most "
1415 "%d levels of saved settings are allowed."),
1416 "PRESERVE", "RESTORE",
1417 MAX_SAVED_SETTINGS);
1418 return CMD_CASCADING_FAILURE;
1423 cmd_restore (struct lexer *lexer, struct dataset *ds UNUSED)
1425 if (n_saved_settings > 0)
1427 struct settings *s = saved_settings[--n_saved_settings];
1429 settings_destroy (s);
1434 lex_next_error (lexer, -1, -1,
1435 _("%s without matching %s."), "RESTORE", "PRESERVE");