1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011 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/>. */
25 #include "gl/vasnprintf.h"
27 #include "data/casereader.h"
28 #include "data/data-in.h"
29 #include "data/data-out.h"
30 #include "data/dataset.h"
31 #include "data/dictionary.h"
32 #include "data/format.h"
33 #include "data/settings.h"
34 #include "data/value.h"
35 #include "data/variable.h"
36 #include "language/command.h"
37 #include "language/lexer/format-parser.h"
38 #include "language/lexer/lexer.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"
54 #else /* !HAVE_TERMCAP_H */
55 int tgetent (char *, const char *);
56 int tgetnum (const char *);
57 #endif /* !HAVE_TERMCAP_H */
58 #endif /* !HAVE_LIBTERMCAP */
63 #define _(msgid) gettext (msgid)
70 case=size:upper/uplow;
76 compression=compress:on/off;
78 decimal=dec:dot/comma;
82 headers=headers:no/yes/blank;
90 lowres=lores:auto/on/off;
92 menus=menus:standard/extended;
104 rib=rib:msbfirst/lsbfirst/vax/native;
105 rrb=rrb:native/isl/isb/idl/idb/vf/vd/vg/zs/zl;
107 scompression=scompress:on/off;
113 undefined=undef:warn/nowarn;
114 wib=wib:msbfirst/lsbfirst/vax/native;
115 wrb=wrb:native/isl/isb/idl/idb/vf/vd/vg/zs/zl;
127 static enum integer_format stc_to_integer_format (int stc);
128 static enum float_format stc_to_float_format (int stc);
131 cmd_set (struct lexer *lexer, struct dataset *ds)
135 if (!parse_set (lexer, ds, &cmd, NULL))
142 settings_set_cc ( cmd.s_cca, FMT_CCA);
144 settings_set_cc ( cmd.s_ccb, FMT_CCB);
146 settings_set_cc ( cmd.s_ccc, FMT_CCC);
148 settings_set_cc ( cmd.s_ccd, FMT_CCD);
150 settings_set_cc ( cmd.s_cce, FMT_CCE);
153 settings_set_decimal_char (cmd.dec == STC_DOT ? '.' : ',');
156 settings_set_include (cmd.inc == STC_ON);
159 if (cmd.n_mxerrs[0] >= 1)
160 settings_set_max_messages (MSG_S_ERROR, cmd.n_mxerrs[0]);
162 msg (SE, _("%s must be at least 1."), "MXERRS");
166 if (cmd.n_mxloops[0] >= 1)
167 settings_set_mxloops (cmd.n_mxloops[0]);
169 msg (SE, _("%s must be at least 1."), "MXLOOPS");
173 if (cmd.n_mxwarns[0] >= 0)
174 settings_set_max_messages (MSG_S_WARNING, cmd.n_mxwarns[0]);
176 msg (SE, _("%s must not be negative."), "MXWARNS");
179 settings_set_input_integer_format (stc_to_integer_format (cmd.rib));
181 settings_set_input_float_format (stc_to_float_format (cmd.rrb));
183 settings_set_safer_mode ();
184 if (cmd.sbc_scompression)
185 settings_set_scompression (cmd.scompress == STC_ON);
186 if (cmd.sbc_undefined)
187 settings_set_undefined (cmd.undef == STC_WARN);
189 settings_set_output_integer_format (stc_to_integer_format (cmd.wib));
191 settings_set_output_float_format (stc_to_float_format (cmd.wrb));
192 if (cmd.sbc_workspace)
194 if ( cmd.n_workspace[0] < 1024 && ! settings_get_testing_mode ())
195 msg (SE, _("WORKSPACE must be at least 1MB"));
196 else if (cmd.n_workspace[0] <= 0)
197 msg (SE, _("WORKSPACE must be positive"));
199 settings_set_workspace (cmd.n_workspace[0] * 1024L);
203 msg (SW, _("%s is obsolete."), "BLOCK");
204 if (cmd.sbc_boxstring)
205 msg (SW, _("%s is obsolete."), "BOXSTRING");
207 msg (SW, _("%s is obsolete."), "CPI");
208 if (cmd.sbc_histogram)
209 msg (SW, _("%s is obsolete."), "HISTOGRAM");
211 msg (SW, _("%s is obsolete."), "LPI");
213 msg (SW, _("%s is obsolete."), "MENUS");
215 msg (SW, _("%s is obsolete."), "XSORT");
216 if (cmd.sbc_mxmemory)
217 msg (SE, _("%s is obsolete."), "MXMEMORY");
218 if (cmd.sbc_scripttab)
219 msg (SE, _("%s is obsolete."), "SCRIPTTAB");
221 msg (SW, _("%s is obsolete."), "TBFONTS");
222 if (cmd.sbc_tb1 && cmd.s_tb1)
223 msg (SW, _("%s is obsolete."), "TB1");
226 msg (SW, _("%s is not yet implemented."), "CASE");
228 if (cmd.sbc_compression)
229 msg (SW, _("Active file compression is not implemented."));
236 /* Returns the integer_format value corresponding to STC,
237 which should be the value of cmd.rib or cmd.wib. */
238 static enum integer_format
239 stc_to_integer_format (int stc)
241 return (stc == STC_MSBFIRST ? INTEGER_MSB_FIRST
242 : stc == STC_LSBFIRST ? INTEGER_LSB_FIRST
243 : stc == STC_VAX ? INTEGER_VAX
247 /* Returns the float_format value corresponding to STC,
248 which should be the value of cmd.rrb or cmd.wrb. */
249 static enum float_format
250 stc_to_float_format (int stc)
255 return FLOAT_NATIVE_DOUBLE;
258 return FLOAT_IEEE_SINGLE_LE;
260 return FLOAT_IEEE_SINGLE_BE;
262 return FLOAT_IEEE_DOUBLE_LE;
264 return FLOAT_IEEE_DOUBLE_BE;
274 return FLOAT_Z_SHORT;
283 set_output_routing (struct lexer *lexer, enum settings_output_type type)
285 enum settings_output_devices devices;
287 lex_match (lexer, T_EQUALS);
288 if (lex_match_id (lexer, "ON") || lex_match_id (lexer, "BOTH"))
289 devices = SETTINGS_DEVICE_LISTING | SETTINGS_DEVICE_TERMINAL;
290 else if (lex_match_id (lexer, "TERMINAL"))
291 devices = SETTINGS_DEVICE_TERMINAL;
292 else if (lex_match_id (lexer, "LISTING"))
293 devices = SETTINGS_DEVICE_LISTING;
294 else if (lex_match_id (lexer, "OFF") || lex_match_id (lexer, "NONE"))
298 lex_error (lexer, NULL);
302 settings_set_output_routing (type, devices);
307 /* Parses the BLANKS subcommand, which controls the value that
308 completely blank fields in numeric data imply. X, Wnd: Syntax is
309 SYSMIS or a numeric value. */
311 stc_custom_blanks (struct lexer *lexer,
312 struct dataset *ds UNUSED,
313 struct cmd_set *cmd UNUSED, void *aux UNUSED)
315 lex_match (lexer, T_EQUALS);
316 if (lex_match_id (lexer, "SYSMIS"))
319 settings_set_blanks (SYSMIS);
323 if (!lex_force_num (lexer))
325 settings_set_blanks (lex_number (lexer));
332 stc_custom_tnumbers (struct lexer *lexer,
333 struct dataset *ds UNUSED,
334 struct cmd_set *cmd UNUSED, void *aux UNUSED)
336 lex_match (lexer, T_EQUALS);
338 if (lex_match_id (lexer, "VALUES"))
340 settings_set_value_style (SETTINGS_VAL_STYLE_VALUES);
342 else if (lex_match_id (lexer, "LABELS"))
344 settings_set_value_style (SETTINGS_VAL_STYLE_LABELS);
346 else if (lex_match_id (lexer, "BOTH"))
348 settings_set_value_style (SETTINGS_VAL_STYLE_BOTH);
352 lex_error_expecting (lexer, "VALUES", "LABELS", "BOTH", NULL_SENTINEL);
360 /* Parses the EPOCH subcommand, which controls the epoch used for
361 parsing 2-digit years. */
363 stc_custom_epoch (struct lexer *lexer,
364 struct dataset *ds UNUSED,
365 struct cmd_set *cmd UNUSED, void *aux UNUSED)
367 lex_match (lexer, T_EQUALS);
368 if (lex_match_id (lexer, "AUTOMATIC"))
369 settings_set_epoch (-1);
370 else if (lex_is_integer (lexer))
372 int new_epoch = lex_integer (lexer);
374 if (new_epoch < 1500)
376 msg (SE, _("EPOCH must be 1500 or later."));
379 settings_set_epoch (new_epoch);
383 lex_error (lexer, _("expecting AUTOMATIC or year"));
391 stc_custom_errors (struct lexer *lexer, struct dataset *ds UNUSED,
392 struct cmd_set *cmd UNUSED, void *aux UNUSED)
394 return set_output_routing (lexer, SETTINGS_OUTPUT_ERROR);
398 stc_custom_length (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
402 lex_match (lexer, T_EQUALS);
403 if (lex_match_id (lexer, "NONE"))
407 if (!lex_force_int (lexer))
409 if (lex_integer (lexer) < 1)
411 msg (SE, _("LENGTH must be at least 1."));
414 page_length = lex_integer (lexer);
418 if (page_length != -1)
419 settings_set_viewlength (page_length);
425 stc_custom_locale (struct lexer *lexer, struct dataset *ds UNUSED,
426 struct cmd_set *cmd UNUSED, void *aux UNUSED)
430 lex_match (lexer, T_EQUALS);
432 if ( !lex_force_string (lexer))
435 s = lex_tokcstr (lexer);
437 /* First try this string as an encoding name */
438 if ( valid_encoding (s))
439 set_default_encoding (s);
441 /* Now try as a locale name (or alias) */
442 else if (set_encoding_from_locale (s))
447 msg (ME, _("%s is not a recognized encoding or locale name"), s);
457 stc_custom_messages (struct lexer *lexer, struct dataset *ds UNUSED,
458 struct cmd_set *cmd UNUSED, void *aux UNUSED)
460 return set_output_routing (lexer, SETTINGS_OUTPUT_NOTE);
464 stc_custom_printback (struct lexer *lexer, struct dataset *ds UNUSED,
465 struct cmd_set *cmd UNUSED, void *aux UNUSED)
467 return set_output_routing (lexer, SETTINGS_OUTPUT_SYNTAX);
471 stc_custom_results (struct lexer *lexer, struct dataset *ds UNUSED,
472 struct cmd_set *cmd UNUSED, void *aux UNUSED)
474 return set_output_routing (lexer, SETTINGS_OUTPUT_RESULT);
478 stc_custom_seed (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
480 lex_match (lexer, T_EQUALS);
481 if (lex_match_id (lexer, "RANDOM"))
485 if (!lex_force_num (lexer))
487 set_rng (lex_number (lexer));
495 stc_custom_width (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
497 lex_match (lexer, T_EQUALS);
498 if (lex_match_id (lexer, "NARROW"))
499 settings_set_viewwidth (79);
500 else if (lex_match_id (lexer, "WIDE"))
501 settings_set_viewwidth (131);
504 if (!lex_force_int (lexer))
506 if (lex_integer (lexer) < 40)
508 msg (SE, _("WIDTH must be at least 40."));
511 settings_set_viewwidth (lex_integer (lexer));
518 /* Parses FORMAT subcommand, which consists of a numeric format
521 stc_custom_format (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
525 lex_match (lexer, T_EQUALS);
526 if (!parse_format_specifier (lexer, &fmt))
529 if (!fmt_check_output (&fmt))
532 if (fmt_is_string (fmt.type))
534 char str[FMT_STRING_LEN_MAX + 1];
535 msg (SE, _("FORMAT requires numeric output format as an argument. "
536 "Specified format %s is of type string."),
537 fmt_to_string (&fmt, str));
541 settings_set_format (&fmt);
546 stc_custom_journal (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
548 lex_match (lexer, T_EQUALS);
549 if (lex_match_id (lexer, "ON") || lex_match_id (lexer, "YES"))
551 else if (lex_match_id (lexer, "OFF") || lex_match_id (lexer, "NO"))
553 else if (lex_is_string (lexer) || lex_token (lexer) == T_ID)
555 char *filename = utf8_to_filename (lex_tokcstr (lexer));
556 journal_set_file_name (filename);
563 lex_error (lexer, NULL);
570 stc_custom_log (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
572 return stc_custom_journal (lexer, ds, cmd, aux);
576 show_output_routing (enum settings_output_type type)
578 enum settings_output_devices devices;
581 devices = settings_get_output_routing (type);
582 if (devices & SETTINGS_DEVICE_LISTING)
583 s = devices & SETTINGS_DEVICE_TERMINAL ? "BOTH" : "LISTING";
584 else if (devices & SETTINGS_DEVICE_TERMINAL)
593 show_blanks (const struct dataset *ds UNUSED)
595 return (settings_get_blanks () == SYSMIS
597 : xasprintf ("%g", settings_get_blanks ()));
601 format_cc (struct string *out, const char *in, char grouping)
606 if (c == grouping || c == '\'')
607 ds_put_byte (out, '\'');
609 ds_put_byte (out, '"');
610 ds_put_byte (out, c);
615 show_cc (enum fmt_type type)
617 const struct fmt_number_style *cc = settings_get_style (type);
620 ds_init_empty (&out);
621 format_cc (&out, cc->neg_prefix.s, cc->grouping);
622 ds_put_byte (&out, cc->grouping);
623 format_cc (&out, cc->prefix.s, cc->grouping);
624 ds_put_byte (&out, cc->grouping);
625 format_cc (&out, cc->suffix.s, cc->grouping);
626 ds_put_byte (&out, cc->grouping);
627 format_cc (&out, cc->neg_suffix.s, cc->grouping);
629 return ds_cstr (&out);
633 show_cca (const struct dataset *ds UNUSED)
635 return show_cc (FMT_CCA);
639 show_ccb (const struct dataset *ds UNUSED)
641 return show_cc (FMT_CCB);
645 show_ccc (const struct dataset *ds UNUSED)
647 return show_cc (FMT_CCC);
651 show_ccd (const struct dataset *ds UNUSED)
653 return show_cc (FMT_CCD);
657 show_cce (const struct dataset *ds UNUSED)
659 return show_cc (FMT_CCE);
663 show_decimals (const struct dataset *ds UNUSED)
665 return xasprintf ("`%c'", settings_get_decimal_char (FMT_F));
669 show_errors (const struct dataset *ds UNUSED)
671 return show_output_routing (SETTINGS_OUTPUT_ERROR);
675 show_format (const struct dataset *ds UNUSED)
677 char str[FMT_STRING_LEN_MAX + 1];
678 return xstrdup (fmt_to_string (settings_get_format (), str));
682 show_length (const struct dataset *ds UNUSED)
684 return xasprintf ("%d", settings_get_viewlength ());
688 show_locale (const struct dataset *ds UNUSED)
690 return xstrdup (get_default_encoding ());
694 show_messages (const struct dataset *ds UNUSED)
696 return show_output_routing (SETTINGS_OUTPUT_NOTE);
700 show_printback (const struct dataset *ds UNUSED)
702 return show_output_routing (SETTINGS_OUTPUT_SYNTAX);
706 show_results (const struct dataset *ds UNUSED)
708 return show_output_routing (SETTINGS_OUTPUT_RESULT);
712 show_mxerrs (const struct dataset *ds UNUSED)
714 return xasprintf ("%d", settings_get_max_messages (MSG_S_ERROR));
718 show_mxloops (const struct dataset *ds UNUSED)
720 return xasprintf ("%d", settings_get_mxloops ());
724 show_mxwarns (const struct dataset *ds UNUSED)
726 return xasprintf ("%d", settings_get_max_messages (MSG_S_WARNING));
729 /* Returns a name for the given INTEGER_FORMAT value. */
731 show_integer_format (enum integer_format integer_format)
733 return xasprintf ("%s (%s)",
734 (integer_format == INTEGER_MSB_FIRST ? "MSBFIRST"
735 : integer_format == INTEGER_LSB_FIRST ? "LSBFIRST"
737 integer_format == INTEGER_NATIVE ? "NATIVE" : "nonnative");
740 /* Returns a name for the given FLOAT_FORMAT value. */
742 show_float_format (enum float_format float_format)
744 const char *format_name = "";
746 switch (float_format)
748 case FLOAT_IEEE_SINGLE_LE:
749 format_name = _("ISL (32-bit IEEE 754 single, little-endian)");
751 case FLOAT_IEEE_SINGLE_BE:
752 format_name = _("ISB (32-bit IEEE 754 single, big-endian)");
754 case FLOAT_IEEE_DOUBLE_LE:
755 format_name = _("IDL (64-bit IEEE 754 double, little-endian)");
757 case FLOAT_IEEE_DOUBLE_BE:
758 format_name = _("IDB (64-bit IEEE 754 double, big-endian)");
762 format_name = _("VF (32-bit VAX F, VAX-endian)");
765 format_name = _("VD (64-bit VAX D, VAX-endian)");
768 format_name = _("VG (64-bit VAX G, VAX-endian)");
772 format_name = _("ZS (32-bit IBM Z hexadecimal short, big-endian)");
775 format_name = _("ZL (64-bit IBM Z hexadecimal long, big-endian)");
783 return xasprintf ("%s (%s)", format_name,
784 (float_format == FLOAT_NATIVE_DOUBLE
785 ? "NATIVE" : "nonnative"));
789 show_rib (const struct dataset *ds UNUSED)
791 return show_integer_format (settings_get_input_integer_format ());
795 show_rrb (const struct dataset *ds UNUSED)
797 return show_float_format (settings_get_input_float_format ());
801 show_scompression (const struct dataset *ds UNUSED)
803 return xstrdup (settings_get_scompression () ? "ON" : "OFF");
807 show_undefined (const struct dataset *ds UNUSED)
809 return xstrdup (settings_get_undefined () ? "WARN" : "NOWARN");
813 show_weight (const struct dataset *ds)
815 const struct variable *var = dict_get_weight (dataset_dict (ds));
816 return xstrdup (var != NULL ? var_get_name (var) : "OFF");
820 show_wib (const struct dataset *ds UNUSED)
822 return show_integer_format (settings_get_output_integer_format ());
826 show_wrb (const struct dataset *ds UNUSED)
828 return show_float_format (settings_get_output_float_format ());
832 show_width (const struct dataset *ds UNUSED)
834 return xasprintf ("%d", settings_get_viewwidth ());
838 show_current_directory (const struct dataset *ds UNUSED)
847 buf = xrealloc (buf, len);
849 while (NULL == (wd = getcwd (buf, len)));
855 show_tempdir (const struct dataset *ds UNUSED)
857 return strdup (temp_dir_name ());
861 show_version (const struct dataset *ds UNUSED)
863 return strdup (version);
867 show_system (const struct dataset *ds UNUSED)
869 return strdup (host_system);
873 show_n (const struct dataset *ds)
878 const struct casereader *reader = dataset_source (ds);
881 return strdup (_("Unknown"));
883 n = casereader_count_cases (reader);
885 return asnprintf (NULL, &l, "%ld", n);
892 char *(*function) (const struct dataset *);
895 const struct show_sbc show_table[] =
897 {"BLANKS", show_blanks},
903 {"DECIMALS", show_decimals},
904 {"DIRECTORY", show_current_directory},
905 {"ENVIRONMENT", show_system},
906 {"ERRORS", show_errors},
907 {"FORMAT", show_format},
908 {"LENGTH", show_length},
909 {"LOCALE", show_locale},
910 {"MESSAGES", show_messages},
911 {"MXERRS", show_mxerrs},
912 {"MXLOOPS", show_mxloops},
913 {"MXWARNS", show_mxwarns},
915 {"PRINTBACk", show_printback},
916 {"RESULTS", show_results},
919 {"SCOMPRESSION", show_scompression},
920 {"TEMPDIR", show_tempdir},
921 {"UNDEFINED", show_undefined},
922 {"VERSION", show_version},
923 {"WEIGHT", show_weight},
926 {"WIDTH", show_width},
930 do_show (const struct dataset *ds, const struct show_sbc *sbc)
932 char *value = sbc->function (ds);
933 msg (SN, _("%s is %s."), sbc->name, value);
938 show_all (const struct dataset *ds)
942 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
943 do_show (ds, &show_table[i]);
947 show_all_cc (const struct dataset *ds)
951 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
953 const struct show_sbc *sbc = &show_table[i];
954 if (!strncmp (sbc->name, "CC", 2))
960 show_warranty (const struct dataset *ds UNUSED)
962 fputs (lack_of_warranty, stdout);
966 show_copying (const struct dataset *ds UNUSED)
968 fputs (copyleft, stdout);
973 cmd_show (struct lexer *lexer, struct dataset *ds)
975 if (lex_token (lexer) == T_ENDCMD)
983 if (lex_match (lexer, T_ALL))
985 else if (lex_match_id (lexer, "CC"))
987 else if (lex_match_id (lexer, "WARRANTY"))
989 else if (lex_match_id (lexer, "COPYING") || lex_match_id (lexer, "LICENSE"))
991 else if (lex_token (lexer) == T_ID)
995 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
997 const struct show_sbc *sbc = &show_table[i];
998 if (lex_match_id (lexer, sbc->name))
1004 lex_error (lexer, NULL);
1011 lex_error (lexer, NULL);
1015 lex_match (lexer, T_SLASH);
1017 while (lex_token (lexer) != T_ENDCMD);
1022 #define MAX_SAVED_SETTINGS 5
1024 static struct settings *saved_settings[MAX_SAVED_SETTINGS];
1025 static int n_saved_settings;
1028 cmd_preserve (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
1030 if (n_saved_settings < MAX_SAVED_SETTINGS)
1032 saved_settings[n_saved_settings++] = settings_get ();
1037 msg (SE, _("Too many PRESERVE commands without a RESTORE: at most "
1038 "%d levels of saved settings are allowed."),
1039 MAX_SAVED_SETTINGS);
1040 return CMD_CASCADING_FAILURE;
1045 cmd_restore (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
1047 if (n_saved_settings > 0)
1049 struct settings *s = saved_settings[--n_saved_settings];
1051 settings_destroy (s);
1056 msg (SE, _("RESTORE without matching PRESERVE."));