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/>. */
24 #include "data/data-in.h"
25 #include "data/data-out.h"
26 #include "data/dataset.h"
27 #include "data/dictionary.h"
28 #include "data/format.h"
29 #include "data/settings.h"
30 #include "data/value.h"
31 #include "data/variable.h"
32 #include "language/command.h"
33 #include "language/lexer/format-parser.h"
34 #include "language/lexer/lexer.h"
35 #include "libpspp/compiler.h"
36 #include "libpspp/copyleft.h"
37 #include "libpspp/float-format.h"
38 #include "libpspp/i18n.h"
39 #include "libpspp/integer-format.h"
40 #include "libpspp/message.h"
41 #include "math/random.h"
42 #include "output/driver.h"
43 #include "output/journal.h"
48 #else /* !HAVE_TERMCAP_H */
49 int tgetent (char *, const char *);
50 int tgetnum (const char *);
51 #endif /* !HAVE_TERMCAP_H */
52 #endif /* !HAVE_LIBTERMCAP */
57 #define _(msgid) gettext (msgid)
62 block=string "x==1" "one character long";
63 boxstring=string "x==3 || x==11" "3 or 11 characters long";
64 case=size:upper/uplow;
70 compression=compress:on/off;
71 cpi=integer "x>0" "%s must be greater than 0";
72 decimal=dec:dot/comma;
76 headers=headers:no/yes/blank;
78 histogram=string "x==1" "one character long";
84 lowres=lores:auto/on/off;
85 lpi=integer "x>0" "%s must be greater than 0";
86 menus=menus:standard/extended;
89 miterate=integer "x>0" "%s must be greater than 0";
90 mnest=integer "x>0" "%s must be greater than 0";
92 mxerrs=integer "x >= 1" "%s must be at least 1";
93 mxloops=integer "x >=1" "%s must be at least 1";
98 rib=rib:msbfirst/lsbfirst/vax/native;
99 rrb=rrb:native/isl/isb/idl/idb/vf/vd/vg/zs/zl;
101 scompression=scompress:on/off;
102 scripttab=string "x==1" "one character long";
105 tb1=string "x==3 || x==11" "3 or 11 characters long";
107 undefined=undef:warn/nowarn;
108 wib=wib:msbfirst/lsbfirst/vax/native;
109 wrb=wrb:native/isl/isb/idl/idb/vf/vd/vg/zs/zl;
111 workspace=integer "x>0" "%s must be positive";
121 static enum integer_format stc_to_integer_format (int stc);
122 static enum float_format stc_to_float_format (int stc);
125 cmd_set (struct lexer *lexer, struct dataset *ds)
129 if (!parse_set (lexer, ds, &cmd, NULL))
136 settings_set_cc ( cmd.s_cca, FMT_CCA);
138 settings_set_cc ( cmd.s_ccb, FMT_CCB);
140 settings_set_cc ( cmd.s_ccc, FMT_CCC);
142 settings_set_cc ( cmd.s_ccd, FMT_CCD);
144 settings_set_cc ( cmd.s_cce, FMT_CCE);
147 settings_set_decimal_char (cmd.dec == STC_DOT ? '.' : ',');
150 settings_set_include (cmd.inc == STC_ON);
152 settings_set_max_messages (MSG_S_ERROR, cmd.n_mxerrs[0]);
154 settings_set_max_messages (MSG_S_WARNING, cmd.n_mxwarns[0]);
156 settings_set_input_integer_format (stc_to_integer_format (cmd.rib));
158 settings_set_input_float_format (stc_to_float_format (cmd.rrb));
160 settings_set_safer_mode ();
161 if (cmd.sbc_scompression)
162 settings_set_scompression (cmd.scompress == STC_ON);
163 if (cmd.sbc_undefined)
164 settings_set_undefined (cmd.undef == STC_WARN);
166 settings_set_output_integer_format (stc_to_integer_format (cmd.wib));
168 settings_set_output_float_format (stc_to_float_format (cmd.wrb));
169 if (cmd.sbc_workspace)
171 if ( cmd.n_workspace[0] < 1024 && ! settings_get_testing_mode ())
172 msg (SE, _("WORKSPACE must be at least 1MB"));
174 settings_set_workspace (cmd.n_workspace[0] * 1024L);
178 msg (SW, _("%s is obsolete."), "BLOCK");
179 if (cmd.sbc_boxstring)
180 msg (SW, _("%s is obsolete."), "BOXSTRING");
181 if (cmd.sbc_histogram)
182 msg (SW, _("%s is obsolete."), "HISTOGRAM");
184 msg (SW, _("%s is obsolete."), "MENUS");
186 msg (SW, _("%s is obsolete."), "XSORT");
187 if (cmd.sbc_mxmemory)
188 msg (SE, _("%s is obsolete."), "MXMEMORY");
189 if (cmd.sbc_scripttab)
190 msg (SE, _("%s is obsolete."), "SCRIPTTAB");
192 msg (SW, _("%s is obsolete."), "TBFONTS");
193 if (cmd.sbc_tb1 && cmd.s_tb1)
194 msg (SW, _("%s is obsolete."), "TB1");
197 msg (SW, _("%s is not yet implemented."), "CASE");
199 if (cmd.sbc_compression)
200 msg (SW, _("Active file compression is not implemented."));
207 /* Returns the integer_format value corresponding to STC,
208 which should be the value of cmd.rib or cmd.wib. */
209 static enum integer_format
210 stc_to_integer_format (int stc)
212 return (stc == STC_MSBFIRST ? INTEGER_MSB_FIRST
213 : stc == STC_LSBFIRST ? INTEGER_LSB_FIRST
214 : stc == STC_VAX ? INTEGER_VAX
218 /* Returns the float_format value corresponding to STC,
219 which should be the value of cmd.rrb or cmd.wrb. */
220 static enum float_format
221 stc_to_float_format (int stc)
226 return FLOAT_NATIVE_DOUBLE;
229 return FLOAT_IEEE_SINGLE_LE;
231 return FLOAT_IEEE_SINGLE_BE;
233 return FLOAT_IEEE_DOUBLE_LE;
235 return FLOAT_IEEE_DOUBLE_BE;
245 return FLOAT_Z_SHORT;
254 set_output_routing (struct lexer *lexer, enum settings_output_type type)
256 enum settings_output_devices devices;
258 lex_match (lexer, T_EQUALS);
259 if (lex_match_id (lexer, "ON") || lex_match_id (lexer, "BOTH"))
260 devices = SETTINGS_DEVICE_LISTING | SETTINGS_DEVICE_TERMINAL;
261 else if (lex_match_id (lexer, "TERMINAL"))
262 devices = SETTINGS_DEVICE_TERMINAL;
263 else if (lex_match_id (lexer, "LISTING"))
264 devices = SETTINGS_DEVICE_LISTING;
265 else if (lex_match_id (lexer, "OFF") || lex_match_id (lexer, "NONE"))
269 lex_error (lexer, NULL);
273 settings_set_output_routing (type, devices);
278 /* Parses the BLANKS subcommand, which controls the value that
279 completely blank fields in numeric data imply. X, Wnd: Syntax is
280 SYSMIS or a numeric value. */
282 stc_custom_blanks (struct lexer *lexer,
283 struct dataset *ds UNUSED,
284 struct cmd_set *cmd UNUSED, void *aux UNUSED)
286 lex_match (lexer, T_EQUALS);
287 if (lex_match_id (lexer, "SYSMIS"))
290 settings_set_blanks (SYSMIS);
294 if (!lex_force_num (lexer))
296 settings_set_blanks (lex_number (lexer));
303 stc_custom_tnumbers (struct lexer *lexer,
304 struct dataset *ds UNUSED,
305 struct cmd_set *cmd UNUSED, void *aux UNUSED)
307 lex_match (lexer, T_EQUALS);
309 if (lex_match_id (lexer, "VALUES"))
311 settings_set_value_style (SETTINGS_VAL_STYLE_VALUES);
313 else if (lex_match_id (lexer, "LABELS"))
315 settings_set_value_style (SETTINGS_VAL_STYLE_LABELS);
317 else if (lex_match_id (lexer, "BOTH"))
319 settings_set_value_style (SETTINGS_VAL_STYLE_BOTH);
323 lex_error (lexer, _("expecting VALUES, LABELS or BOTH"));
331 /* Parses the EPOCH subcommand, which controls the epoch used for
332 parsing 2-digit years. */
334 stc_custom_epoch (struct lexer *lexer,
335 struct dataset *ds UNUSED,
336 struct cmd_set *cmd UNUSED, void *aux UNUSED)
338 lex_match (lexer, T_EQUALS);
339 if (lex_match_id (lexer, "AUTOMATIC"))
340 settings_set_epoch (-1);
341 else if (lex_is_integer (lexer))
343 int new_epoch = lex_integer (lexer);
345 if (new_epoch < 1500)
347 msg (SE, _("EPOCH must be 1500 or later."));
350 settings_set_epoch (new_epoch);
354 lex_error (lexer, _("expecting AUTOMATIC or year"));
362 stc_custom_errors (struct lexer *lexer, struct dataset *ds UNUSED,
363 struct cmd_set *cmd UNUSED, void *aux UNUSED)
365 return set_output_routing (lexer, SETTINGS_OUTPUT_ERROR);
369 stc_custom_length (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
373 lex_match (lexer, T_EQUALS);
374 if (lex_match_id (lexer, "NONE"))
378 if (!lex_force_int (lexer))
380 if (lex_integer (lexer) < 1)
382 msg (SE, _("LENGTH must be at least 1."));
385 page_length = lex_integer (lexer);
389 if (page_length != -1)
390 settings_set_viewlength (page_length);
396 stc_custom_locale (struct lexer *lexer, struct dataset *ds UNUSED,
397 struct cmd_set *cmd UNUSED, void *aux UNUSED)
401 lex_match (lexer, T_EQUALS);
403 if ( !lex_force_string (lexer))
406 s = lex_tokcstr (lexer);
408 /* First try this string as an encoding name */
409 if ( valid_encoding (s))
410 set_default_encoding (s);
412 /* Now try as a locale name (or alias) */
413 else if (set_encoding_from_locale (s))
418 msg (ME, _("%s is not a recognized encoding or locale name"), s);
428 stc_custom_messages (struct lexer *lexer, struct dataset *ds UNUSED,
429 struct cmd_set *cmd UNUSED, void *aux UNUSED)
431 return set_output_routing (lexer, SETTINGS_OUTPUT_NOTE);
435 stc_custom_printback (struct lexer *lexer, struct dataset *ds UNUSED,
436 struct cmd_set *cmd UNUSED, void *aux UNUSED)
438 return set_output_routing (lexer, SETTINGS_OUTPUT_SYNTAX);
442 stc_custom_results (struct lexer *lexer, struct dataset *ds UNUSED,
443 struct cmd_set *cmd UNUSED, void *aux UNUSED)
445 return set_output_routing (lexer, SETTINGS_OUTPUT_RESULT);
449 stc_custom_seed (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
451 lex_match (lexer, T_EQUALS);
452 if (lex_match_id (lexer, "RANDOM"))
456 if (!lex_force_num (lexer))
458 set_rng (lex_number (lexer));
466 stc_custom_width (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
468 lex_match (lexer, T_EQUALS);
469 if (lex_match_id (lexer, "NARROW"))
470 settings_set_viewwidth (79);
471 else if (lex_match_id (lexer, "WIDE"))
472 settings_set_viewwidth (131);
475 if (!lex_force_int (lexer))
477 if (lex_integer (lexer) < 40)
479 msg (SE, _("WIDTH must be at least 40."));
482 settings_set_viewwidth (lex_integer (lexer));
489 /* Parses FORMAT subcommand, which consists of a numeric format
492 stc_custom_format (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
496 lex_match (lexer, T_EQUALS);
497 if (!parse_format_specifier (lexer, &fmt))
500 if (!fmt_check_output (&fmt))
503 if (fmt_is_string (fmt.type))
505 char str[FMT_STRING_LEN_MAX + 1];
506 msg (SE, _("FORMAT requires numeric output format as an argument. "
507 "Specified format %s is of type string."),
508 fmt_to_string (&fmt, str));
512 settings_set_format (&fmt);
517 stc_custom_journal (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
519 lex_match (lexer, T_EQUALS);
520 if (lex_match_id (lexer, "ON") || lex_match_id (lexer, "YES"))
522 else if (lex_match_id (lexer, "OFF") || lex_match_id (lexer, "NO"))
524 else if (lex_is_string (lexer) || lex_token (lexer) == T_ID)
526 char *filename = utf8_to_filename (lex_tokcstr (lexer));
527 journal_set_file_name (filename);
534 lex_error (lexer, NULL);
541 stc_custom_log (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
543 return stc_custom_journal (lexer, ds, cmd, aux);
547 show_output_routing (enum settings_output_type type)
549 enum settings_output_devices devices;
552 devices = settings_get_output_routing (type);
553 if (devices & SETTINGS_DEVICE_LISTING)
554 s = devices & SETTINGS_DEVICE_TERMINAL ? "BOTH" : "LISTING";
555 else if (devices & SETTINGS_DEVICE_TERMINAL)
564 show_blanks (const struct dataset *ds UNUSED)
566 return (settings_get_blanks () == SYSMIS
568 : xasprintf ("%g", settings_get_blanks ()));
572 format_cc (struct string *out, const char *in, char grouping)
577 if (c == grouping || c == '\'')
578 ds_put_byte (out, '\'');
580 ds_put_byte (out, '"');
581 ds_put_byte (out, c);
586 show_cc (enum fmt_type type)
588 const struct fmt_number_style *cc = settings_get_style (type);
591 ds_init_empty (&out);
592 format_cc (&out, cc->neg_prefix.s, cc->grouping);
593 ds_put_byte (&out, cc->grouping);
594 format_cc (&out, cc->prefix.s, cc->grouping);
595 ds_put_byte (&out, cc->grouping);
596 format_cc (&out, cc->suffix.s, cc->grouping);
597 ds_put_byte (&out, cc->grouping);
598 format_cc (&out, cc->neg_suffix.s, cc->grouping);
600 return ds_cstr (&out);
604 show_cca (const struct dataset *ds UNUSED)
606 return show_cc (FMT_CCA);
610 show_ccb (const struct dataset *ds UNUSED)
612 return show_cc (FMT_CCB);
616 show_ccc (const struct dataset *ds UNUSED)
618 return show_cc (FMT_CCC);
622 show_ccd (const struct dataset *ds UNUSED)
624 return show_cc (FMT_CCD);
628 show_cce (const struct dataset *ds UNUSED)
630 return show_cc (FMT_CCE);
634 show_decimals (const struct dataset *ds UNUSED)
636 return xasprintf ("`%c'", settings_get_decimal_char (FMT_F));
640 show_errors (const struct dataset *ds UNUSED)
642 return show_output_routing (SETTINGS_OUTPUT_ERROR);
646 show_format (const struct dataset *ds UNUSED)
648 char str[FMT_STRING_LEN_MAX + 1];
649 return xstrdup (fmt_to_string (settings_get_format (), str));
653 show_length (const struct dataset *ds UNUSED)
655 return xasprintf ("%d", settings_get_viewlength ());
659 show_locale (const struct dataset *ds UNUSED)
661 return xstrdup (get_default_encoding ());
665 show_messages (const struct dataset *ds UNUSED)
667 return show_output_routing (SETTINGS_OUTPUT_NOTE);
671 show_printback (const struct dataset *ds UNUSED)
673 return show_output_routing (SETTINGS_OUTPUT_SYNTAX);
677 show_results (const struct dataset *ds UNUSED)
679 return show_output_routing (SETTINGS_OUTPUT_RESULT);
683 show_mxerrs (const struct dataset *ds UNUSED)
685 return xasprintf ("%d", settings_get_max_messages (MSG_S_ERROR));
689 show_mxloops (const struct dataset *ds UNUSED)
691 return xasprintf ("%d", settings_get_mxloops ());
695 show_mxwarns (const struct dataset *ds UNUSED)
697 return xasprintf ("%d", settings_get_max_messages (MSG_S_WARNING));
700 /* Returns a name for the given INTEGER_FORMAT value. */
702 show_integer_format (enum integer_format integer_format)
704 return xasprintf ("%s (%s)",
705 (integer_format == INTEGER_MSB_FIRST ? "MSBFIRST"
706 : integer_format == INTEGER_LSB_FIRST ? "LSBFIRST"
708 integer_format == INTEGER_NATIVE ? "NATIVE" : "nonnative");
711 /* Returns a name for the given FLOAT_FORMAT value. */
713 show_float_format (enum float_format float_format)
715 const char *format_name = "";
717 switch (float_format)
719 case FLOAT_IEEE_SINGLE_LE:
720 format_name = _("ISL (32-bit IEEE 754 single, little-endian)");
722 case FLOAT_IEEE_SINGLE_BE:
723 format_name = _("ISB (32-bit IEEE 754 single, big-endian)");
725 case FLOAT_IEEE_DOUBLE_LE:
726 format_name = _("IDL (64-bit IEEE 754 double, little-endian)");
728 case FLOAT_IEEE_DOUBLE_BE:
729 format_name = _("IDB (64-bit IEEE 754 double, big-endian)");
733 format_name = _("VF (32-bit VAX F, VAX-endian)");
736 format_name = _("VD (64-bit VAX D, VAX-endian)");
739 format_name = _("VG (64-bit VAX G, VAX-endian)");
743 format_name = _("ZS (32-bit IBM Z hexadecimal short, big-endian)");
746 format_name = _("ZL (64-bit IBM Z hexadecimal long, big-endian)");
754 return xasprintf ("%s (%s)", format_name,
755 (float_format == FLOAT_NATIVE_DOUBLE
756 ? "NATIVE" : "nonnative"));
760 show_rib (const struct dataset *ds UNUSED)
762 return show_integer_format (settings_get_input_integer_format ());
766 show_rrb (const struct dataset *ds UNUSED)
768 return show_float_format (settings_get_input_float_format ());
772 show_scompression (const struct dataset *ds UNUSED)
774 return xstrdup (settings_get_scompression () ? "ON" : "OFF");
778 show_undefined (const struct dataset *ds UNUSED)
780 return xstrdup (settings_get_undefined () ? "WARN" : "NOWARN");
784 show_weight (const struct dataset *ds)
786 const struct variable *var = dict_get_weight (dataset_dict (ds));
787 return xstrdup (var != NULL ? var_get_name (var) : "OFF");
791 show_wib (const struct dataset *ds UNUSED)
793 return show_integer_format (settings_get_output_integer_format ());
797 show_wrb (const struct dataset *ds UNUSED)
799 return show_float_format (settings_get_output_float_format ());
803 show_width (const struct dataset *ds UNUSED)
805 return xasprintf ("%d", settings_get_viewwidth ());
811 char *(*function) (const struct dataset *);
814 const struct show_sbc show_table[] =
816 {"BLANKS", show_blanks},
822 {"DECIMALS", show_decimals},
823 {"ERRORS", show_errors},
824 {"FORMAT", show_format},
825 {"LENGTH", show_length},
826 {"LOCALE", show_locale},
827 {"MESSAGES", show_messages},
828 {"MXERRS", show_mxerrs},
829 {"MXLOOPS", show_mxloops},
830 {"MXWARNS", show_mxwarns},
831 {"PRINTBACk", show_printback},
832 {"RESULTS", show_results},
835 {"SCOMPRESSION", show_scompression},
836 {"UNDEFINED", show_undefined},
837 {"WEIGHT", show_weight},
840 {"WIDTH", show_width},
844 do_show (const struct dataset *ds, const struct show_sbc *sbc)
846 char *value = sbc->function (ds);
847 msg (SN, _("%s is %s."), sbc->name, value);
852 show_all (const struct dataset *ds)
856 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
857 do_show (ds, &show_table[i]);
861 show_all_cc (const struct dataset *ds)
865 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
867 const struct show_sbc *sbc = &show_table[i];
868 if (!strncmp (sbc->name, "CC", 2))
874 show_warranty (const struct dataset *ds UNUSED)
876 fputs (lack_of_warranty, stdout);
880 show_copying (const struct dataset *ds UNUSED)
882 fputs (copyleft, stdout);
886 cmd_show (struct lexer *lexer, struct dataset *ds)
888 if (lex_token (lexer) == T_ENDCMD)
896 if (lex_match (lexer, T_ALL))
898 else if (lex_match_id (lexer, "CC"))
900 else if (lex_match_id (lexer, "WARRANTY"))
902 else if (lex_match_id (lexer, "COPYING"))
904 else if (lex_token (lexer) == T_ID)
908 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
910 const struct show_sbc *sbc = &show_table[i];
911 if (lex_match_id (lexer, sbc->name))
917 lex_error (lexer, NULL);
924 lex_error (lexer, NULL);
928 lex_match (lexer, T_SLASH);
930 while (lex_token (lexer) != T_ENDCMD);
935 #define MAX_SAVED_SETTINGS 5
937 static struct settings *saved_settings[MAX_SAVED_SETTINGS];
938 static int n_saved_settings;
941 cmd_preserve (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
943 if (n_saved_settings < MAX_SAVED_SETTINGS)
945 saved_settings[n_saved_settings++] = settings_get ();
950 msg (SE, _("Too many PRESERVE commands without a RESTORE: at most "
951 "%d levels of saved settings are allowed."),
953 return CMD_CASCADING_FAILURE;
958 cmd_restore (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
960 if (n_saved_settings > 0)
962 struct settings *s = saved_settings[--n_saved_settings];
964 settings_destroy (s);
969 msg (SE, _("RESTORE without matching PRESERVE."));