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)
64 case=size:upper/uplow;
70 compression=compress:on/off;
72 decimal=dec:dot/comma;
76 headers=headers:no/yes/blank;
84 lowres=lores:auto/on/off;
86 menus=menus:standard/extended;
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;
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;
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);
153 if (cmd.n_mxerrs[0] >= 1)
154 settings_set_max_messages (MSG_S_ERROR, cmd.n_mxerrs[0]);
156 msg (SE, _("%s must be at least 1."), "MXERRS");
160 if (cmd.n_mxloops[0] >= 1)
161 settings_set_mxloops (cmd.n_mxloops[0]);
163 msg (SE, _("%s must be at least 1."), "MXLOOPS");
167 if (cmd.n_mxwarns[0] >= 0)
168 settings_set_max_messages (MSG_S_WARNING, cmd.n_mxwarns[0]);
170 msg (SE, _("%s must not be negative."), "MXWARNS");
173 settings_set_input_integer_format (stc_to_integer_format (cmd.rib));
175 settings_set_input_float_format (stc_to_float_format (cmd.rrb));
177 settings_set_safer_mode ();
178 if (cmd.sbc_scompression)
179 settings_set_scompression (cmd.scompress == STC_ON);
180 if (cmd.sbc_undefined)
181 settings_set_undefined (cmd.undef == STC_WARN);
183 settings_set_output_integer_format (stc_to_integer_format (cmd.wib));
185 settings_set_output_float_format (stc_to_float_format (cmd.wrb));
186 if (cmd.sbc_workspace)
188 if ( cmd.n_workspace[0] < 1024 && ! settings_get_testing_mode ())
189 msg (SE, _("WORKSPACE must be at least 1MB"));
190 else if (cmd.n_workspace[0] <= 0)
191 msg (SE, _("WORKSPACE must be positive"));
193 settings_set_workspace (cmd.n_workspace[0] * 1024L);
197 msg (SW, _("%s is obsolete."), "BLOCK");
198 if (cmd.sbc_boxstring)
199 msg (SW, _("%s is obsolete."), "BOXSTRING");
201 msg (SW, _("%s is obsolete."), "CPI");
202 if (cmd.sbc_histogram)
203 msg (SW, _("%s is obsolete."), "HISTOGRAM");
205 msg (SW, _("%s is obsolete."), "LPI");
207 msg (SW, _("%s is obsolete."), "MENUS");
209 msg (SW, _("%s is obsolete."), "XSORT");
210 if (cmd.sbc_mxmemory)
211 msg (SE, _("%s is obsolete."), "MXMEMORY");
212 if (cmd.sbc_scripttab)
213 msg (SE, _("%s is obsolete."), "SCRIPTTAB");
215 msg (SW, _("%s is obsolete."), "TBFONTS");
216 if (cmd.sbc_tb1 && cmd.s_tb1)
217 msg (SW, _("%s is obsolete."), "TB1");
220 msg (SW, _("%s is not yet implemented."), "CASE");
222 if (cmd.sbc_compression)
223 msg (SW, _("Active file compression is not implemented."));
230 /* Returns the integer_format value corresponding to STC,
231 which should be the value of cmd.rib or cmd.wib. */
232 static enum integer_format
233 stc_to_integer_format (int stc)
235 return (stc == STC_MSBFIRST ? INTEGER_MSB_FIRST
236 : stc == STC_LSBFIRST ? INTEGER_LSB_FIRST
237 : stc == STC_VAX ? INTEGER_VAX
241 /* Returns the float_format value corresponding to STC,
242 which should be the value of cmd.rrb or cmd.wrb. */
243 static enum float_format
244 stc_to_float_format (int stc)
249 return FLOAT_NATIVE_DOUBLE;
252 return FLOAT_IEEE_SINGLE_LE;
254 return FLOAT_IEEE_SINGLE_BE;
256 return FLOAT_IEEE_DOUBLE_LE;
258 return FLOAT_IEEE_DOUBLE_BE;
268 return FLOAT_Z_SHORT;
277 set_output_routing (struct lexer *lexer, enum settings_output_type type)
279 enum settings_output_devices devices;
281 lex_match (lexer, T_EQUALS);
282 if (lex_match_id (lexer, "ON") || lex_match_id (lexer, "BOTH"))
283 devices = SETTINGS_DEVICE_LISTING | SETTINGS_DEVICE_TERMINAL;
284 else if (lex_match_id (lexer, "TERMINAL"))
285 devices = SETTINGS_DEVICE_TERMINAL;
286 else if (lex_match_id (lexer, "LISTING"))
287 devices = SETTINGS_DEVICE_LISTING;
288 else if (lex_match_id (lexer, "OFF") || lex_match_id (lexer, "NONE"))
292 lex_error (lexer, NULL);
296 settings_set_output_routing (type, devices);
301 /* Parses the BLANKS subcommand, which controls the value that
302 completely blank fields in numeric data imply. X, Wnd: Syntax is
303 SYSMIS or a numeric value. */
305 stc_custom_blanks (struct lexer *lexer,
306 struct dataset *ds UNUSED,
307 struct cmd_set *cmd UNUSED, void *aux UNUSED)
309 lex_match (lexer, T_EQUALS);
310 if (lex_match_id (lexer, "SYSMIS"))
313 settings_set_blanks (SYSMIS);
317 if (!lex_force_num (lexer))
319 settings_set_blanks (lex_number (lexer));
326 stc_custom_tnumbers (struct lexer *lexer,
327 struct dataset *ds UNUSED,
328 struct cmd_set *cmd UNUSED, void *aux UNUSED)
330 lex_match (lexer, T_EQUALS);
332 if (lex_match_id (lexer, "VALUES"))
334 settings_set_value_style (SETTINGS_VAL_STYLE_VALUES);
336 else if (lex_match_id (lexer, "LABELS"))
338 settings_set_value_style (SETTINGS_VAL_STYLE_LABELS);
340 else if (lex_match_id (lexer, "BOTH"))
342 settings_set_value_style (SETTINGS_VAL_STYLE_BOTH);
346 lex_error_expecting (lexer, "VALUES", "LABELS", "BOTH", NULL_SENTINEL);
354 /* Parses the EPOCH subcommand, which controls the epoch used for
355 parsing 2-digit years. */
357 stc_custom_epoch (struct lexer *lexer,
358 struct dataset *ds UNUSED,
359 struct cmd_set *cmd UNUSED, void *aux UNUSED)
361 lex_match (lexer, T_EQUALS);
362 if (lex_match_id (lexer, "AUTOMATIC"))
363 settings_set_epoch (-1);
364 else if (lex_is_integer (lexer))
366 int new_epoch = lex_integer (lexer);
368 if (new_epoch < 1500)
370 msg (SE, _("EPOCH must be 1500 or later."));
373 settings_set_epoch (new_epoch);
377 lex_error (lexer, _("expecting AUTOMATIC or year"));
385 stc_custom_errors (struct lexer *lexer, struct dataset *ds UNUSED,
386 struct cmd_set *cmd UNUSED, void *aux UNUSED)
388 return set_output_routing (lexer, SETTINGS_OUTPUT_ERROR);
392 stc_custom_length (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
396 lex_match (lexer, T_EQUALS);
397 if (lex_match_id (lexer, "NONE"))
401 if (!lex_force_int (lexer))
403 if (lex_integer (lexer) < 1)
405 msg (SE, _("LENGTH must be at least 1."));
408 page_length = lex_integer (lexer);
412 if (page_length != -1)
413 settings_set_viewlength (page_length);
419 stc_custom_locale (struct lexer *lexer, struct dataset *ds UNUSED,
420 struct cmd_set *cmd UNUSED, void *aux UNUSED)
424 lex_match (lexer, T_EQUALS);
426 if ( !lex_force_string (lexer))
429 s = lex_tokcstr (lexer);
431 /* First try this string as an encoding name */
432 if ( valid_encoding (s))
433 set_default_encoding (s);
435 /* Now try as a locale name (or alias) */
436 else if (set_encoding_from_locale (s))
441 msg (ME, _("%s is not a recognized encoding or locale name"), s);
451 stc_custom_messages (struct lexer *lexer, struct dataset *ds UNUSED,
452 struct cmd_set *cmd UNUSED, void *aux UNUSED)
454 return set_output_routing (lexer, SETTINGS_OUTPUT_NOTE);
458 stc_custom_printback (struct lexer *lexer, struct dataset *ds UNUSED,
459 struct cmd_set *cmd UNUSED, void *aux UNUSED)
461 return set_output_routing (lexer, SETTINGS_OUTPUT_SYNTAX);
465 stc_custom_results (struct lexer *lexer, struct dataset *ds UNUSED,
466 struct cmd_set *cmd UNUSED, void *aux UNUSED)
468 return set_output_routing (lexer, SETTINGS_OUTPUT_RESULT);
472 stc_custom_seed (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
474 lex_match (lexer, T_EQUALS);
475 if (lex_match_id (lexer, "RANDOM"))
479 if (!lex_force_num (lexer))
481 set_rng (lex_number (lexer));
489 stc_custom_width (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
491 lex_match (lexer, T_EQUALS);
492 if (lex_match_id (lexer, "NARROW"))
493 settings_set_viewwidth (79);
494 else if (lex_match_id (lexer, "WIDE"))
495 settings_set_viewwidth (131);
498 if (!lex_force_int (lexer))
500 if (lex_integer (lexer) < 40)
502 msg (SE, _("WIDTH must be at least 40."));
505 settings_set_viewwidth (lex_integer (lexer));
512 /* Parses FORMAT subcommand, which consists of a numeric format
515 stc_custom_format (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
519 lex_match (lexer, T_EQUALS);
520 if (!parse_format_specifier (lexer, &fmt))
523 if (!fmt_check_output (&fmt))
526 if (fmt_is_string (fmt.type))
528 char str[FMT_STRING_LEN_MAX + 1];
529 msg (SE, _("FORMAT requires numeric output format as an argument. "
530 "Specified format %s is of type string."),
531 fmt_to_string (&fmt, str));
535 settings_set_format (&fmt);
540 stc_custom_journal (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
542 lex_match (lexer, T_EQUALS);
543 if (lex_match_id (lexer, "ON") || lex_match_id (lexer, "YES"))
545 else if (lex_match_id (lexer, "OFF") || lex_match_id (lexer, "NO"))
547 else if (lex_is_string (lexer) || lex_token (lexer) == T_ID)
549 char *filename = utf8_to_filename (lex_tokcstr (lexer));
550 journal_set_file_name (filename);
557 lex_error (lexer, NULL);
564 stc_custom_log (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
566 return stc_custom_journal (lexer, ds, cmd, aux);
570 show_output_routing (enum settings_output_type type)
572 enum settings_output_devices devices;
575 devices = settings_get_output_routing (type);
576 if (devices & SETTINGS_DEVICE_LISTING)
577 s = devices & SETTINGS_DEVICE_TERMINAL ? "BOTH" : "LISTING";
578 else if (devices & SETTINGS_DEVICE_TERMINAL)
587 show_blanks (const struct dataset *ds UNUSED)
589 return (settings_get_blanks () == SYSMIS
591 : xasprintf ("%g", settings_get_blanks ()));
595 format_cc (struct string *out, const char *in, char grouping)
600 if (c == grouping || c == '\'')
601 ds_put_byte (out, '\'');
603 ds_put_byte (out, '"');
604 ds_put_byte (out, c);
609 show_cc (enum fmt_type type)
611 const struct fmt_number_style *cc = settings_get_style (type);
614 ds_init_empty (&out);
615 format_cc (&out, cc->neg_prefix.s, cc->grouping);
616 ds_put_byte (&out, cc->grouping);
617 format_cc (&out, cc->prefix.s, cc->grouping);
618 ds_put_byte (&out, cc->grouping);
619 format_cc (&out, cc->suffix.s, cc->grouping);
620 ds_put_byte (&out, cc->grouping);
621 format_cc (&out, cc->neg_suffix.s, cc->grouping);
623 return ds_cstr (&out);
627 show_cca (const struct dataset *ds UNUSED)
629 return show_cc (FMT_CCA);
633 show_ccb (const struct dataset *ds UNUSED)
635 return show_cc (FMT_CCB);
639 show_ccc (const struct dataset *ds UNUSED)
641 return show_cc (FMT_CCC);
645 show_ccd (const struct dataset *ds UNUSED)
647 return show_cc (FMT_CCD);
651 show_cce (const struct dataset *ds UNUSED)
653 return show_cc (FMT_CCE);
657 show_decimals (const struct dataset *ds UNUSED)
659 return xasprintf ("`%c'", settings_get_decimal_char (FMT_F));
663 show_errors (const struct dataset *ds UNUSED)
665 return show_output_routing (SETTINGS_OUTPUT_ERROR);
669 show_format (const struct dataset *ds UNUSED)
671 char str[FMT_STRING_LEN_MAX + 1];
672 return xstrdup (fmt_to_string (settings_get_format (), str));
676 show_length (const struct dataset *ds UNUSED)
678 return xasprintf ("%d", settings_get_viewlength ());
682 show_locale (const struct dataset *ds UNUSED)
684 return xstrdup (get_default_encoding ());
688 show_messages (const struct dataset *ds UNUSED)
690 return show_output_routing (SETTINGS_OUTPUT_NOTE);
694 show_printback (const struct dataset *ds UNUSED)
696 return show_output_routing (SETTINGS_OUTPUT_SYNTAX);
700 show_results (const struct dataset *ds UNUSED)
702 return show_output_routing (SETTINGS_OUTPUT_RESULT);
706 show_mxerrs (const struct dataset *ds UNUSED)
708 return xasprintf ("%d", settings_get_max_messages (MSG_S_ERROR));
712 show_mxloops (const struct dataset *ds UNUSED)
714 return xasprintf ("%d", settings_get_mxloops ());
718 show_mxwarns (const struct dataset *ds UNUSED)
720 return xasprintf ("%d", settings_get_max_messages (MSG_S_WARNING));
723 /* Returns a name for the given INTEGER_FORMAT value. */
725 show_integer_format (enum integer_format integer_format)
727 return xasprintf ("%s (%s)",
728 (integer_format == INTEGER_MSB_FIRST ? "MSBFIRST"
729 : integer_format == INTEGER_LSB_FIRST ? "LSBFIRST"
731 integer_format == INTEGER_NATIVE ? "NATIVE" : "nonnative");
734 /* Returns a name for the given FLOAT_FORMAT value. */
736 show_float_format (enum float_format float_format)
738 const char *format_name = "";
740 switch (float_format)
742 case FLOAT_IEEE_SINGLE_LE:
743 format_name = _("ISL (32-bit IEEE 754 single, little-endian)");
745 case FLOAT_IEEE_SINGLE_BE:
746 format_name = _("ISB (32-bit IEEE 754 single, big-endian)");
748 case FLOAT_IEEE_DOUBLE_LE:
749 format_name = _("IDL (64-bit IEEE 754 double, little-endian)");
751 case FLOAT_IEEE_DOUBLE_BE:
752 format_name = _("IDB (64-bit IEEE 754 double, big-endian)");
756 format_name = _("VF (32-bit VAX F, VAX-endian)");
759 format_name = _("VD (64-bit VAX D, VAX-endian)");
762 format_name = _("VG (64-bit VAX G, VAX-endian)");
766 format_name = _("ZS (32-bit IBM Z hexadecimal short, big-endian)");
769 format_name = _("ZL (64-bit IBM Z hexadecimal long, big-endian)");
777 return xasprintf ("%s (%s)", format_name,
778 (float_format == FLOAT_NATIVE_DOUBLE
779 ? "NATIVE" : "nonnative"));
783 show_rib (const struct dataset *ds UNUSED)
785 return show_integer_format (settings_get_input_integer_format ());
789 show_rrb (const struct dataset *ds UNUSED)
791 return show_float_format (settings_get_input_float_format ());
795 show_scompression (const struct dataset *ds UNUSED)
797 return xstrdup (settings_get_scompression () ? "ON" : "OFF");
801 show_undefined (const struct dataset *ds UNUSED)
803 return xstrdup (settings_get_undefined () ? "WARN" : "NOWARN");
807 show_weight (const struct dataset *ds)
809 const struct variable *var = dict_get_weight (dataset_dict (ds));
810 return xstrdup (var != NULL ? var_get_name (var) : "OFF");
814 show_wib (const struct dataset *ds UNUSED)
816 return show_integer_format (settings_get_output_integer_format ());
820 show_wrb (const struct dataset *ds UNUSED)
822 return show_float_format (settings_get_output_float_format ());
826 show_width (const struct dataset *ds UNUSED)
828 return xasprintf ("%d", settings_get_viewwidth ());
834 char *(*function) (const struct dataset *);
837 const struct show_sbc show_table[] =
839 {"BLANKS", show_blanks},
845 {"DECIMALS", show_decimals},
846 {"ERRORS", show_errors},
847 {"FORMAT", show_format},
848 {"LENGTH", show_length},
849 {"LOCALE", show_locale},
850 {"MESSAGES", show_messages},
851 {"MXERRS", show_mxerrs},
852 {"MXLOOPS", show_mxloops},
853 {"MXWARNS", show_mxwarns},
854 {"PRINTBACk", show_printback},
855 {"RESULTS", show_results},
858 {"SCOMPRESSION", show_scompression},
859 {"UNDEFINED", show_undefined},
860 {"WEIGHT", show_weight},
863 {"WIDTH", show_width},
867 do_show (const struct dataset *ds, const struct show_sbc *sbc)
869 char *value = sbc->function (ds);
870 msg (SN, _("%s is %s."), sbc->name, value);
875 show_all (const struct dataset *ds)
879 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
880 do_show (ds, &show_table[i]);
884 show_all_cc (const struct dataset *ds)
888 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
890 const struct show_sbc *sbc = &show_table[i];
891 if (!strncmp (sbc->name, "CC", 2))
897 show_warranty (const struct dataset *ds UNUSED)
899 fputs (lack_of_warranty, stdout);
903 show_copying (const struct dataset *ds UNUSED)
905 fputs (copyleft, stdout);
909 cmd_show (struct lexer *lexer, struct dataset *ds)
911 if (lex_token (lexer) == T_ENDCMD)
919 if (lex_match (lexer, T_ALL))
921 else if (lex_match_id (lexer, "CC"))
923 else if (lex_match_id (lexer, "WARRANTY"))
925 else if (lex_match_id (lexer, "COPYING"))
927 else if (lex_token (lexer) == T_ID)
931 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
933 const struct show_sbc *sbc = &show_table[i];
934 if (lex_match_id (lexer, sbc->name))
940 lex_error (lexer, NULL);
947 lex_error (lexer, NULL);
951 lex_match (lexer, T_SLASH);
953 while (lex_token (lexer) != T_ENDCMD);
958 #define MAX_SAVED_SETTINGS 5
960 static struct settings *saved_settings[MAX_SAVED_SETTINGS];
961 static int n_saved_settings;
964 cmd_preserve (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
966 if (n_saved_settings < MAX_SAVED_SETTINGS)
968 saved_settings[n_saved_settings++] = settings_get ();
973 msg (SE, _("Too many PRESERVE commands without a RESTORE: at most "
974 "%d levels of saved settings are allowed."),
976 return CMD_CASCADING_FAILURE;
981 cmd_restore (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
983 if (n_saved_settings > 0)
985 struct settings *s = saved_settings[--n_saved_settings];
987 settings_destroy (s);
992 msg (SE, _("RESTORE without matching PRESERVE."));