1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009, 2010 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/dictionary.h>
27 #include <data/format.h>
28 #include <data/procedure.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/integer-format.h>
39 #include <libpspp/message.h>
40 #include <libpspp/i18n.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";
104 tb1=string "x==3 || x==11" "3 or 11 characters long";
106 undefined=undef:warn/nowarn;
107 wib=wib:msbfirst/lsbfirst/vax/native;
108 wrb=wrb:native/isl/isb/idl/idb/vf/vd/vg/zs/zl;
110 workspace=integer "x>0" "%s must be positive";
120 static enum integer_format stc_to_integer_format (int stc);
121 static enum float_format stc_to_float_format (int stc);
124 cmd_set (struct lexer *lexer, struct dataset *ds)
128 if (!parse_set (lexer, ds, &cmd, NULL))
135 settings_set_cc ( cmd.s_cca, FMT_CCA);
137 settings_set_cc ( cmd.s_ccb, FMT_CCB);
139 settings_set_cc ( cmd.s_ccc, FMT_CCC);
141 settings_set_cc ( cmd.s_ccd, FMT_CCD);
143 settings_set_cc ( cmd.s_cce, FMT_CCE);
146 settings_set_decimal_char (cmd.dec == STC_DOT ? '.' : ',');
149 settings_set_include (cmd.inc == STC_ON);
151 settings_set_max_messages (MSG_S_ERROR, cmd.n_mxerrs[0]);
153 settings_set_max_messages (MSG_S_WARNING, cmd.n_mxwarns[0]);
155 settings_set_input_integer_format (stc_to_integer_format (cmd.rib));
157 settings_set_input_float_format (stc_to_float_format (cmd.rrb));
159 settings_set_safer_mode ();
160 if (cmd.sbc_scompression)
161 settings_set_scompression (cmd.scompress == STC_ON);
162 if (cmd.sbc_undefined)
163 settings_set_undefined (cmd.undef == STC_WARN);
165 settings_set_output_integer_format (stc_to_integer_format (cmd.wib));
167 settings_set_output_float_format (stc_to_float_format (cmd.wrb));
168 if (cmd.sbc_workspace)
170 if ( cmd.n_workspace[0] < 1024 && ! settings_get_testing_mode ())
171 msg (SE, _("WORKSPACE must be at least 1MB"));
173 settings_set_workspace (cmd.n_workspace[0] * 1024L);
177 msg (SW, _("%s is obsolete."), "BLOCK");
178 if (cmd.sbc_boxstring)
179 msg (SW, _("%s is obsolete."), "BOXSTRING");
180 if (cmd.sbc_histogram)
181 msg (SW, _("%s is obsolete."), "HISTOGRAM");
183 msg (SW, _("%s is obsolete."), "MENUS");
185 msg (SW, _("%s is obsolete."), "XSORT");
186 if (cmd.sbc_mxmemory)
187 msg (SE, _("%s is obsolete."), "MXMEMORY");
188 if (cmd.sbc_scripttab)
189 msg (SE, _("%s is obsolete."), "SCRIPTTAB");
191 msg (SW, _("%s is obsolete."), "TBFONTS");
192 if (cmd.sbc_tb1 && cmd.s_tb1)
193 msg (SW, _("%s is obsolete."), "TB1");
196 msg (SW, _("%s is not yet implemented."), "CASE");
198 if (cmd.sbc_compression)
199 msg (SW, _("Active file compression is not implemented."));
206 /* Returns the integer_format value corresponding to STC,
207 which should be the value of cmd.rib or cmd.wib. */
208 static enum integer_format
209 stc_to_integer_format (int stc)
211 return (stc == STC_MSBFIRST ? INTEGER_MSB_FIRST
212 : stc == STC_LSBFIRST ? INTEGER_LSB_FIRST
213 : stc == STC_VAX ? INTEGER_VAX
217 /* Returns the float_format value corresponding to STC,
218 which should be the value of cmd.rrb or cmd.wrb. */
219 static enum float_format
220 stc_to_float_format (int stc)
225 return FLOAT_NATIVE_DOUBLE;
228 return FLOAT_IEEE_SINGLE_LE;
230 return FLOAT_IEEE_SINGLE_BE;
232 return FLOAT_IEEE_DOUBLE_LE;
234 return FLOAT_IEEE_DOUBLE_BE;
244 return FLOAT_Z_SHORT;
253 set_output_routing (struct lexer *lexer, enum settings_output_type type)
255 enum settings_output_devices devices;
257 lex_match (lexer, T_EQUALS);
258 if (lex_match_id (lexer, "ON") || lex_match_id (lexer, "BOTH"))
259 devices = SETTINGS_DEVICE_LISTING | SETTINGS_DEVICE_TERMINAL;
260 else if (lex_match_id (lexer, "TERMINAL"))
261 devices = SETTINGS_DEVICE_TERMINAL;
262 else if (lex_match_id (lexer, "LISTING"))
263 devices = SETTINGS_DEVICE_LISTING;
264 else if (lex_match_id (lexer, "OFF") || lex_match_id (lexer, "NONE"))
268 lex_error (lexer, NULL);
272 settings_set_output_routing (type, devices);
277 /* Parses the BLANKS subcommand, which controls the value that
278 completely blank fields in numeric data imply. X, Wnd: Syntax is
279 SYSMIS or a numeric value. */
281 stc_custom_blanks (struct lexer *lexer,
282 struct dataset *ds UNUSED,
283 struct cmd_set *cmd UNUSED, void *aux UNUSED)
285 lex_match (lexer, T_EQUALS);
286 if (lex_match_id (lexer, "SYSMIS"))
289 settings_set_blanks (SYSMIS);
293 if (!lex_force_num (lexer))
295 settings_set_blanks (lex_number (lexer));
301 /* Parses the EPOCH subcommand, which controls the epoch used for
302 parsing 2-digit years. */
304 stc_custom_epoch (struct lexer *lexer,
305 struct dataset *ds UNUSED,
306 struct cmd_set *cmd UNUSED, void *aux UNUSED)
308 lex_match (lexer, T_EQUALS);
309 if (lex_match_id (lexer, "AUTOMATIC"))
310 settings_set_epoch (-1);
311 else if (lex_is_integer (lexer))
313 int new_epoch = lex_integer (lexer);
315 if (new_epoch < 1500)
317 msg (SE, _("EPOCH must be 1500 or later."));
320 settings_set_epoch (new_epoch);
324 lex_error (lexer, _("expecting AUTOMATIC or year"));
332 stc_custom_errors (struct lexer *lexer, struct dataset *ds UNUSED,
333 struct cmd_set *cmd UNUSED, void *aux UNUSED)
335 return set_output_routing (lexer, SETTINGS_OUTPUT_ERROR);
339 stc_custom_length (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
343 lex_match (lexer, T_EQUALS);
344 if (lex_match_id (lexer, "NONE"))
348 if (!lex_force_int (lexer))
350 if (lex_integer (lexer) < 1)
352 msg (SE, _("LENGTH must be at least 1."));
355 page_length = lex_integer (lexer);
359 if (page_length != -1)
360 settings_set_viewlength (page_length);
366 stc_custom_locale (struct lexer *lexer, struct dataset *ds UNUSED,
367 struct cmd_set *cmd UNUSED, void *aux UNUSED)
371 lex_match (lexer, T_EQUALS);
373 if ( !lex_force_string (lexer))
376 s = lex_tokcstr (lexer);
378 /* First try this string as an encoding name */
379 if ( valid_encoding (s))
380 set_default_encoding (s);
382 /* Now try as a locale name (or alias) */
383 else if (set_encoding_from_locale (s))
388 msg (ME, _("%s is not a recognized encoding or locale name"), s);
398 stc_custom_messages (struct lexer *lexer, struct dataset *ds UNUSED,
399 struct cmd_set *cmd UNUSED, void *aux UNUSED)
401 return set_output_routing (lexer, SETTINGS_OUTPUT_NOTE);
405 stc_custom_printback (struct lexer *lexer, struct dataset *ds UNUSED,
406 struct cmd_set *cmd UNUSED, void *aux UNUSED)
408 return set_output_routing (lexer, SETTINGS_OUTPUT_SYNTAX);
412 stc_custom_results (struct lexer *lexer, struct dataset *ds UNUSED,
413 struct cmd_set *cmd UNUSED, void *aux UNUSED)
415 return set_output_routing (lexer, SETTINGS_OUTPUT_RESULT);
419 stc_custom_seed (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
421 lex_match (lexer, T_EQUALS);
422 if (lex_match_id (lexer, "RANDOM"))
426 if (!lex_force_num (lexer))
428 set_rng (lex_number (lexer));
436 stc_custom_width (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
438 lex_match (lexer, T_EQUALS);
439 if (lex_match_id (lexer, "NARROW"))
440 settings_set_viewwidth (79);
441 else if (lex_match_id (lexer, "WIDE"))
442 settings_set_viewwidth (131);
445 if (!lex_force_int (lexer))
447 if (lex_integer (lexer) < 40)
449 msg (SE, _("WIDTH must be at least 40."));
452 settings_set_viewwidth (lex_integer (lexer));
459 /* Parses FORMAT subcommand, which consists of a numeric format
462 stc_custom_format (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
466 lex_match (lexer, T_EQUALS);
467 if (!parse_format_specifier (lexer, &fmt))
470 if (!fmt_check_output (&fmt))
473 if (fmt_is_string (fmt.type))
475 char str[FMT_STRING_LEN_MAX + 1];
476 msg (SE, _("FORMAT requires numeric output format as an argument. "
477 "Specified format %s is of type string."),
478 fmt_to_string (&fmt, str));
482 settings_set_format (&fmt);
487 stc_custom_journal (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
489 lex_match (lexer, T_EQUALS);
490 if (lex_match_id (lexer, "ON") || lex_match_id (lexer, "YES"))
492 else if (lex_match_id (lexer, "OFF") || lex_match_id (lexer, "NO"))
494 else if (lex_is_string (lexer) || lex_token (lexer) == T_ID)
496 journal_set_file_name (lex_tokcstr (lexer));
501 lex_error (lexer, NULL);
508 stc_custom_log (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
510 return stc_custom_journal (lexer, ds, cmd, aux);
514 show_output_routing (enum settings_output_type type)
516 enum settings_output_devices devices;
519 devices = settings_get_output_routing (type);
520 if (devices & SETTINGS_DEVICE_LISTING)
521 s = devices & SETTINGS_DEVICE_TERMINAL ? "BOTH" : "LISTING";
522 else if (devices & SETTINGS_DEVICE_TERMINAL)
531 show_blanks (const struct dataset *ds UNUSED)
533 return (settings_get_blanks () == SYSMIS
535 : xasprintf ("%g", settings_get_blanks ()));
539 format_cc (struct string *out, struct substring in, char grouping)
541 while (!ss_is_empty (in))
543 char c = ss_get_byte (&in);
544 if (c == grouping || c == '\'')
545 ds_put_byte (out, '\'');
547 ds_put_byte (out, '"');
548 ds_put_byte (out, c);
553 show_cc (enum fmt_type type)
555 const struct fmt_number_style *cc = settings_get_style (type);
558 ds_init_empty (&out);
559 format_cc (&out, cc->neg_prefix, cc->grouping);
560 ds_put_byte (&out, cc->grouping);
561 format_cc (&out, cc->prefix, cc->grouping);
562 ds_put_byte (&out, cc->grouping);
563 format_cc (&out, cc->suffix, cc->grouping);
564 ds_put_byte (&out, cc->grouping);
565 format_cc (&out, cc->neg_suffix, cc->grouping);
567 return ds_cstr (&out);
571 show_cca (const struct dataset *ds UNUSED)
573 return show_cc (FMT_CCA);
577 show_ccb (const struct dataset *ds UNUSED)
579 return show_cc (FMT_CCB);
583 show_ccc (const struct dataset *ds UNUSED)
585 return show_cc (FMT_CCC);
589 show_ccd (const struct dataset *ds UNUSED)
591 return show_cc (FMT_CCD);
595 show_cce (const struct dataset *ds UNUSED)
597 return show_cc (FMT_CCE);
601 show_decimals (const struct dataset *ds UNUSED)
603 return xasprintf ("`%c'", settings_get_decimal_char (FMT_F));
607 show_errors (const struct dataset *ds UNUSED)
609 return show_output_routing (SETTINGS_OUTPUT_ERROR);
613 show_format (const struct dataset *ds UNUSED)
615 char str[FMT_STRING_LEN_MAX + 1];
616 return xstrdup (fmt_to_string (settings_get_format (), str));
620 show_length (const struct dataset *ds UNUSED)
622 return xasprintf ("%d", settings_get_viewlength ());
626 show_locale (const struct dataset *ds UNUSED)
628 return xstrdup (get_default_encoding ());
632 show_messages (const struct dataset *ds UNUSED)
634 return show_output_routing (SETTINGS_OUTPUT_NOTE);
638 show_printback (const struct dataset *ds UNUSED)
640 return show_output_routing (SETTINGS_OUTPUT_SYNTAX);
644 show_results (const struct dataset *ds UNUSED)
646 return show_output_routing (SETTINGS_OUTPUT_RESULT);
650 show_mxerrs (const struct dataset *ds UNUSED)
652 return xasprintf ("%d", settings_get_max_messages (MSG_S_ERROR));
656 show_mxloops (const struct dataset *ds UNUSED)
658 return xasprintf ("%d", settings_get_mxloops ());
662 show_mxwarns (const struct dataset *ds UNUSED)
664 return xasprintf ("%d", settings_get_max_messages (MSG_S_WARNING));
667 /* Returns a name for the given INTEGER_FORMAT value. */
669 show_integer_format (enum integer_format integer_format)
671 return xasprintf ("%s (%s)",
672 (integer_format == INTEGER_MSB_FIRST ? "MSBFIRST"
673 : integer_format == INTEGER_LSB_FIRST ? "LSBFIRST"
675 integer_format == INTEGER_NATIVE ? "NATIVE" : "nonnative");
678 /* Returns a name for the given FLOAT_FORMAT value. */
680 show_float_format (enum float_format float_format)
682 const char *format_name = "";
684 switch (float_format)
686 case FLOAT_IEEE_SINGLE_LE:
687 format_name = _("ISL (32-bit IEEE 754 single, little-endian)");
689 case FLOAT_IEEE_SINGLE_BE:
690 format_name = _("ISB (32-bit IEEE 754 single, big-endian)");
692 case FLOAT_IEEE_DOUBLE_LE:
693 format_name = _("IDL (64-bit IEEE 754 double, little-endian)");
695 case FLOAT_IEEE_DOUBLE_BE:
696 format_name = _("IDB (64-bit IEEE 754 double, big-endian)");
700 format_name = _("VF (32-bit VAX F, VAX-endian)");
703 format_name = _("VD (64-bit VAX D, VAX-endian)");
706 format_name = _("VG (64-bit VAX G, VAX-endian)");
710 format_name = _("ZS (32-bit IBM Z hexadecimal short, big-endian)");
713 format_name = _("ZL (64-bit IBM Z hexadecimal long, big-endian)");
721 return xasprintf ("%s (%s)", format_name,
722 (float_format == FLOAT_NATIVE_DOUBLE
723 ? "NATIVE" : "nonnative"));
727 show_rib (const struct dataset *ds UNUSED)
729 return show_integer_format (settings_get_input_integer_format ());
733 show_rrb (const struct dataset *ds UNUSED)
735 return show_float_format (settings_get_input_float_format ());
739 show_scompression (const struct dataset *ds UNUSED)
741 return xstrdup (settings_get_scompression () ? "ON" : "OFF");
745 show_undefined (const struct dataset *ds UNUSED)
747 return xstrdup (settings_get_undefined () ? "WARN" : "NOWARN");
751 show_weight (const struct dataset *ds)
753 const struct variable *var = dict_get_weight (dataset_dict (ds));
754 return xstrdup (var != NULL ? var_get_name (var) : "OFF");
758 show_wib (const struct dataset *ds UNUSED)
760 return show_integer_format (settings_get_output_integer_format ());
764 show_wrb (const struct dataset *ds UNUSED)
766 return show_float_format (settings_get_output_float_format ());
770 show_width (const struct dataset *ds UNUSED)
772 return xasprintf ("%d", settings_get_viewwidth ());
778 char *(*function) (const struct dataset *);
781 const struct show_sbc show_table[] =
783 {"BLANKS", show_blanks},
789 {"DECIMALS", show_decimals},
790 {"ERRORS", show_errors},
791 {"FORMAT", show_format},
792 {"LENGTH", show_length},
793 {"LOCALE", show_locale},
794 {"MESSAGES", show_messages},
795 {"MXERRS", show_mxerrs},
796 {"MXLOOPS", show_mxloops},
797 {"MXWARNS", show_mxwarns},
798 {"PRINTBACk", show_printback},
799 {"RESULTS", show_results},
802 {"SCOMPRESSION", show_scompression},
803 {"UNDEFINED", show_undefined},
804 {"WEIGHT", show_weight},
807 {"WIDTH", show_width},
811 do_show (const struct dataset *ds, const struct show_sbc *sbc)
813 char *value = sbc->function (ds);
814 msg (SN, _("%s is %s."), sbc->name, value);
819 show_all (const struct dataset *ds)
823 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
824 do_show (ds, &show_table[i]);
828 show_all_cc (const struct dataset *ds)
832 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
834 const struct show_sbc *sbc = &show_table[i];
835 if (!strncmp (sbc->name, "CC", 2))
841 show_warranty (const struct dataset *ds UNUSED)
843 fputs (lack_of_warranty, stdout);
847 show_copying (const struct dataset *ds UNUSED)
849 fputs (copyleft, stdout);
853 cmd_show (struct lexer *lexer, struct dataset *ds)
855 if (lex_token (lexer) == T_ENDCMD)
863 if (lex_match (lexer, T_ALL))
865 else if (lex_match_id (lexer, "CC"))
867 else if (lex_match_id (lexer, "WARRANTY"))
869 else if (lex_match_id (lexer, "COPYING"))
871 else if (lex_token (lexer) == T_ID)
875 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
877 const struct show_sbc *sbc = &show_table[i];
878 if (lex_match_id (lexer, sbc->name))
884 lex_error (lexer, NULL);
891 lex_error (lexer, NULL);
895 lex_match (lexer, T_SLASH);
897 while (lex_token (lexer) != T_ENDCMD);
902 #define MAX_SAVED_SETTINGS 5
904 static struct settings *saved_settings[MAX_SAVED_SETTINGS];
905 static int n_saved_settings;
908 cmd_preserve (struct lexer *lexer, struct dataset *ds UNUSED)
910 if (n_saved_settings < MAX_SAVED_SETTINGS)
912 saved_settings[n_saved_settings++] = settings_get ();
913 return lex_end_of_command (lexer);
917 msg (SE, _("Too many PRESERVE commands without a RESTORE: at most "
918 "%d levels of saved settings are allowed."),
920 return CMD_CASCADING_FAILURE;
925 cmd_restore (struct lexer *lexer, struct dataset *ds UNUSED)
927 if (n_saved_settings > 0)
929 struct settings *s = saved_settings[--n_saved_settings];
931 settings_destroy (s);
932 return lex_end_of_command (lexer);
936 msg (SE, _("RESTORE without matching PRESERVE."));