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 <language/prompt.h>
36 #include <libpspp/compiler.h>
37 #include <libpspp/copyleft.h>
38 #include <libpspp/float-format.h>
39 #include <libpspp/integer-format.h>
40 #include <libpspp/message.h>
41 #include <libpspp/i18n.h>
42 #include <math/random.h>
43 #include <output/driver.h>
44 #include <output/journal.h>
49 #else /* !HAVE_TERMCAP_H */
50 int tgetent (char *, const char *);
51 int tgetnum (const char *);
52 #endif /* !HAVE_TERMCAP_H */
53 #endif /* !HAVE_LIBTERMCAP */
58 #define _(msgid) gettext (msgid)
63 block=string "x==1" "one character long";
64 boxstring=string "x==3 || x==11" "3 or 11 characters long";
65 case=size:upper/uplow;
71 compression=compress:on/off;
72 cpi=integer "x>0" "%s must be greater than 0";
74 decimal=dec:dot/comma;
76 endcmd=string "x==1" "one character long";
80 headers=headers:no/yes/blank;
82 histogram=string "x==1" "one character long";
88 lowres=lores:auto/on/off;
89 lpi=integer "x>0" "%s must be greater than 0";
90 menus=menus:standard/extended;
93 miterate=integer "x>0" "%s must be greater than 0";
94 mnest=integer "x>0" "%s must be greater than 0";
96 mxerrs=integer "x >= 1" "%s must be at least 1";
97 mxloops=integer "x >=1" "%s must be at least 1";
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;
108 scripttab=string "x==1" "one character long";
110 tb1=string "x==3 || x==11" "3 or 11 characters long";
112 undefined=undef:warn/nowarn;
113 wib=wib:msbfirst/lsbfirst/vax/native;
114 wrb=wrb:native/isl/isb/idl/idb/vf/vd/vg/zs/zl;
116 workspace=integer "x>0" "%s must be positive";
126 static enum integer_format stc_to_integer_format (int stc);
127 static enum float_format stc_to_float_format (int stc);
130 cmd_set (struct lexer *lexer, struct dataset *ds)
134 if (!parse_set (lexer, ds, &cmd, NULL))
141 settings_set_cc ( cmd.s_cca, FMT_CCA);
143 settings_set_cc ( cmd.s_ccb, FMT_CCB);
145 settings_set_cc ( cmd.s_ccc, FMT_CCC);
147 settings_set_cc ( cmd.s_ccd, FMT_CCD);
149 settings_set_cc ( cmd.s_cce, FMT_CCE);
152 prompt_set (PROMPT_FIRST, cmd.s_prompt);
154 prompt_set (PROMPT_LATER, cmd.s_cprompt);
156 prompt_set (PROMPT_DATA, cmd.s_dprompt);
159 settings_set_decimal_char (cmd.dec == STC_DOT ? '.' : ',');
162 settings_set_endcmd (cmd.s_endcmd[0]);
164 settings_set_include (cmd.inc == STC_ON);
166 settings_set_max_messages (MSG_S_ERROR, cmd.n_mxerrs[0]);
168 settings_set_max_messages (MSG_S_WARNING, cmd.n_mxwarns[0]);
170 settings_set_nulline (cmd.null == STC_ON);
172 settings_set_input_integer_format (stc_to_integer_format (cmd.rib));
174 settings_set_input_float_format (stc_to_float_format (cmd.rrb));
176 settings_set_safer_mode ();
177 if (cmd.sbc_scompression)
178 settings_set_scompression (cmd.scompress == STC_ON);
179 if (cmd.sbc_undefined)
180 settings_set_undefined (cmd.undef == STC_WARN);
182 settings_set_output_integer_format (stc_to_integer_format (cmd.wib));
184 settings_set_output_float_format (stc_to_float_format (cmd.wrb));
185 if (cmd.sbc_workspace)
187 if ( cmd.n_workspace[0] < 1024 && ! settings_get_testing_mode ())
188 msg (SE, _("WORKSPACE must be at least 1MB"));
190 settings_set_workspace (cmd.n_workspace[0] * 1024L);
194 msg (SW, _("%s is obsolete."), "BLOCK");
195 if (cmd.sbc_boxstring)
196 msg (SW, _("%s is obsolete."), "BOXSTRING");
197 if (cmd.sbc_histogram)
198 msg (SW, _("%s is obsolete."), "HISTOGRAM");
200 msg (SW, _("%s is obsolete."), "MENUS");
202 msg (SW, _("%s is obsolete."), "XSORT");
203 if (cmd.sbc_mxmemory)
204 msg (SE, _("%s is obsolete."), "MXMEMORY");
205 if (cmd.sbc_scripttab)
206 msg (SE, _("%s is obsolete."), "SCRIPTTAB");
208 msg (SW, _("%s is obsolete."), "TBFONTS");
209 if (cmd.sbc_tb1 && cmd.s_tb1)
210 msg (SW, _("%s is obsolete."), "TB1");
213 msg (SW, _("%s is not yet implemented."), "CASE");
215 if (cmd.sbc_compression)
216 msg (SW, _("Active file compression is not implemented."));
223 /* Returns the integer_format value corresponding to STC,
224 which should be the value of cmd.rib or cmd.wib. */
225 static enum integer_format
226 stc_to_integer_format (int stc)
228 return (stc == STC_MSBFIRST ? INTEGER_MSB_FIRST
229 : stc == STC_LSBFIRST ? INTEGER_LSB_FIRST
230 : stc == STC_VAX ? INTEGER_VAX
234 /* Returns the float_format value corresponding to STC,
235 which should be the value of cmd.rrb or cmd.wrb. */
236 static enum float_format
237 stc_to_float_format (int stc)
242 return FLOAT_NATIVE_DOUBLE;
245 return FLOAT_IEEE_SINGLE_LE;
247 return FLOAT_IEEE_SINGLE_BE;
249 return FLOAT_IEEE_DOUBLE_LE;
251 return FLOAT_IEEE_DOUBLE_BE;
261 return FLOAT_Z_SHORT;
270 set_output_routing (struct lexer *lexer, enum settings_output_type type)
272 enum settings_output_devices devices;
274 lex_match (lexer, T_EQUALS);
275 if (lex_match_id (lexer, "ON") || lex_match_id (lexer, "BOTH"))
276 devices = SETTINGS_DEVICE_LISTING | SETTINGS_DEVICE_TERMINAL;
277 else if (lex_match_id (lexer, "TERMINAL"))
278 devices = SETTINGS_DEVICE_TERMINAL;
279 else if (lex_match_id (lexer, "LISTING"))
280 devices = SETTINGS_DEVICE_LISTING;
281 else if (lex_match_id (lexer, "OFF") || lex_match_id (lexer, "NONE"))
285 lex_error (lexer, NULL);
289 settings_set_output_routing (type, devices);
294 /* Parses the BLANKS subcommand, which controls the value that
295 completely blank fields in numeric data imply. X, Wnd: Syntax is
296 SYSMIS or a numeric value. */
298 stc_custom_blanks (struct lexer *lexer,
299 struct dataset *ds UNUSED,
300 struct cmd_set *cmd UNUSED, void *aux UNUSED)
302 lex_match (lexer, T_EQUALS);
303 if (lex_match_id (lexer, "SYSMIS"))
306 settings_set_blanks (SYSMIS);
310 if (!lex_force_num (lexer))
312 settings_set_blanks (lex_number (lexer));
318 /* Parses the EPOCH subcommand, which controls the epoch used for
319 parsing 2-digit years. */
321 stc_custom_epoch (struct lexer *lexer,
322 struct dataset *ds UNUSED,
323 struct cmd_set *cmd UNUSED, void *aux UNUSED)
325 lex_match (lexer, T_EQUALS);
326 if (lex_match_id (lexer, "AUTOMATIC"))
327 settings_set_epoch (-1);
328 else if (lex_is_integer (lexer))
330 int new_epoch = lex_integer (lexer);
332 if (new_epoch < 1500)
334 msg (SE, _("EPOCH must be 1500 or later."));
337 settings_set_epoch (new_epoch);
341 lex_error (lexer, _("expecting AUTOMATIC or year"));
349 stc_custom_errors (struct lexer *lexer, struct dataset *ds UNUSED,
350 struct cmd_set *cmd UNUSED, void *aux UNUSED)
352 return set_output_routing (lexer, SETTINGS_OUTPUT_ERROR);
356 stc_custom_length (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
360 lex_match (lexer, T_EQUALS);
361 if (lex_match_id (lexer, "NONE"))
365 if (!lex_force_int (lexer))
367 if (lex_integer (lexer) < 1)
369 msg (SE, _("LENGTH must be at least 1."));
372 page_length = lex_integer (lexer);
376 if (page_length != -1)
377 settings_set_viewlength (page_length);
383 stc_custom_locale (struct lexer *lexer, struct dataset *ds UNUSED,
384 struct cmd_set *cmd UNUSED, void *aux UNUSED)
388 lex_match (lexer, T_EQUALS);
390 if ( !lex_force_string (lexer))
393 s = lex_tokcstr (lexer);
395 /* First try this string as an encoding name */
396 if ( valid_encoding (s))
397 set_default_encoding (s);
399 /* Now try as a locale name (or alias) */
400 else if (set_encoding_from_locale (s))
405 msg (ME, _("%s is not a recognized encoding or locale name"), s);
415 stc_custom_messages (struct lexer *lexer, struct dataset *ds UNUSED,
416 struct cmd_set *cmd UNUSED, void *aux UNUSED)
418 return set_output_routing (lexer, SETTINGS_OUTPUT_NOTE);
422 stc_custom_printback (struct lexer *lexer, struct dataset *ds UNUSED,
423 struct cmd_set *cmd UNUSED, void *aux UNUSED)
425 return set_output_routing (lexer, SETTINGS_OUTPUT_SYNTAX);
429 stc_custom_results (struct lexer *lexer, struct dataset *ds UNUSED,
430 struct cmd_set *cmd UNUSED, void *aux UNUSED)
432 return set_output_routing (lexer, SETTINGS_OUTPUT_RESULT);
436 stc_custom_seed (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, "RANDOM"))
443 if (!lex_force_num (lexer))
445 set_rng (lex_number (lexer));
453 stc_custom_width (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
455 lex_match (lexer, T_EQUALS);
456 if (lex_match_id (lexer, "NARROW"))
457 settings_set_viewwidth (79);
458 else if (lex_match_id (lexer, "WIDE"))
459 settings_set_viewwidth (131);
462 if (!lex_force_int (lexer))
464 if (lex_integer (lexer) < 40)
466 msg (SE, _("WIDTH must be at least 40."));
469 settings_set_viewwidth (lex_integer (lexer));
476 /* Parses FORMAT subcommand, which consists of a numeric format
479 stc_custom_format (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
483 lex_match (lexer, T_EQUALS);
484 if (!parse_format_specifier (lexer, &fmt))
487 if (!fmt_check_output (&fmt))
490 if (fmt_is_string (fmt.type))
492 char str[FMT_STRING_LEN_MAX + 1];
493 msg (SE, _("FORMAT requires numeric output format as an argument. "
494 "Specified format %s is of type string."),
495 fmt_to_string (&fmt, str));
499 settings_set_format (&fmt);
504 stc_custom_journal (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
506 lex_match (lexer, T_EQUALS);
507 if (lex_match_id (lexer, "ON") || lex_match_id (lexer, "YES"))
509 else if (lex_match_id (lexer, "OFF") || lex_match_id (lexer, "NO"))
511 else if (lex_is_string (lexer) || lex_token (lexer) == T_ID)
513 journal_set_file_name (lex_tokcstr (lexer));
518 lex_error (lexer, NULL);
525 stc_custom_log (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
527 return stc_custom_journal (lexer, ds, cmd, aux);
531 show_output_routing (enum settings_output_type type)
533 enum settings_output_devices devices;
536 devices = settings_get_output_routing (type);
537 if (devices & SETTINGS_DEVICE_LISTING)
538 s = devices & SETTINGS_DEVICE_TERMINAL ? "BOTH" : "LISTING";
539 else if (devices & SETTINGS_DEVICE_TERMINAL)
548 show_blanks (const struct dataset *ds UNUSED)
550 return (settings_get_blanks () == SYSMIS
552 : xasprintf ("%g", settings_get_blanks ()));
556 format_cc (struct string *out, struct substring in, char grouping)
558 while (!ss_is_empty (in))
560 char c = ss_get_byte (&in);
561 if (c == grouping || c == '\'')
562 ds_put_byte (out, '\'');
564 ds_put_byte (out, '"');
565 ds_put_byte (out, c);
570 show_cc (enum fmt_type type)
572 const struct fmt_number_style *cc = settings_get_style (type);
575 ds_init_empty (&out);
576 format_cc (&out, cc->neg_prefix, cc->grouping);
577 ds_put_byte (&out, cc->grouping);
578 format_cc (&out, cc->prefix, cc->grouping);
579 ds_put_byte (&out, cc->grouping);
580 format_cc (&out, cc->suffix, cc->grouping);
581 ds_put_byte (&out, cc->grouping);
582 format_cc (&out, cc->neg_suffix, cc->grouping);
584 return ds_cstr (&out);
588 show_cca (const struct dataset *ds UNUSED)
590 return show_cc (FMT_CCA);
594 show_ccb (const struct dataset *ds UNUSED)
596 return show_cc (FMT_CCB);
600 show_ccc (const struct dataset *ds UNUSED)
602 return show_cc (FMT_CCC);
606 show_ccd (const struct dataset *ds UNUSED)
608 return show_cc (FMT_CCD);
612 show_cce (const struct dataset *ds UNUSED)
614 return show_cc (FMT_CCE);
618 show_decimals (const struct dataset *ds UNUSED)
620 return xasprintf ("`%c'", settings_get_decimal_char (FMT_F));
624 show_endcmd (const struct dataset *ds UNUSED)
626 return xasprintf ("`%c'", settings_get_endcmd ());
630 show_errors (const struct dataset *ds UNUSED)
632 return show_output_routing (SETTINGS_OUTPUT_ERROR);
636 show_format (const struct dataset *ds UNUSED)
638 char str[FMT_STRING_LEN_MAX + 1];
639 return xstrdup (fmt_to_string (settings_get_format (), str));
643 show_length (const struct dataset *ds UNUSED)
645 return xasprintf ("%d", settings_get_viewlength ());
649 show_locale (const struct dataset *ds UNUSED)
651 return xstrdup (get_default_encoding ());
655 show_messages (const struct dataset *ds UNUSED)
657 return show_output_routing (SETTINGS_OUTPUT_NOTE);
661 show_printback (const struct dataset *ds UNUSED)
663 return show_output_routing (SETTINGS_OUTPUT_SYNTAX);
667 show_results (const struct dataset *ds UNUSED)
669 return show_output_routing (SETTINGS_OUTPUT_RESULT);
673 show_mxerrs (const struct dataset *ds UNUSED)
675 return xasprintf ("%d", settings_get_max_messages (MSG_S_ERROR));
679 show_mxloops (const struct dataset *ds UNUSED)
681 return xasprintf ("%d", settings_get_mxloops ());
685 show_mxwarns (const struct dataset *ds UNUSED)
687 return xasprintf ("%d", settings_get_max_messages (MSG_S_WARNING));
690 /* Returns a name for the given INTEGER_FORMAT value. */
692 show_integer_format (enum integer_format integer_format)
694 return xasprintf ("%s (%s)",
695 (integer_format == INTEGER_MSB_FIRST ? "MSBFIRST"
696 : integer_format == INTEGER_LSB_FIRST ? "LSBFIRST"
698 integer_format == INTEGER_NATIVE ? "NATIVE" : "nonnative");
701 /* Returns a name for the given FLOAT_FORMAT value. */
703 show_float_format (enum float_format float_format)
705 const char *format_name = "";
707 switch (float_format)
709 case FLOAT_IEEE_SINGLE_LE:
710 format_name = _("ISL (32-bit IEEE 754 single, little-endian)");
712 case FLOAT_IEEE_SINGLE_BE:
713 format_name = _("ISB (32-bit IEEE 754 single, big-endian)");
715 case FLOAT_IEEE_DOUBLE_LE:
716 format_name = _("IDL (64-bit IEEE 754 double, little-endian)");
718 case FLOAT_IEEE_DOUBLE_BE:
719 format_name = _("IDB (64-bit IEEE 754 double, big-endian)");
723 format_name = _("VF (32-bit VAX F, VAX-endian)");
726 format_name = _("VD (64-bit VAX D, VAX-endian)");
729 format_name = _("VG (64-bit VAX G, VAX-endian)");
733 format_name = _("ZS (32-bit IBM Z hexadecimal short, big-endian)");
736 format_name = _("ZL (64-bit IBM Z hexadecimal long, big-endian)");
744 return xasprintf ("%s (%s)", format_name,
745 (float_format == FLOAT_NATIVE_DOUBLE
746 ? "NATIVE" : "nonnative"));
750 show_rib (const struct dataset *ds UNUSED)
752 return show_integer_format (settings_get_input_integer_format ());
756 show_rrb (const struct dataset *ds UNUSED)
758 return show_float_format (settings_get_input_float_format ());
762 show_scompression (const struct dataset *ds UNUSED)
764 return xstrdup (settings_get_scompression () ? "ON" : "OFF");
768 show_undefined (const struct dataset *ds UNUSED)
770 return xstrdup (settings_get_undefined () ? "WARN" : "NOWARN");
774 show_weight (const struct dataset *ds)
776 const struct variable *var = dict_get_weight (dataset_dict (ds));
777 return xstrdup (var != NULL ? var_get_name (var) : "OFF");
781 show_wib (const struct dataset *ds UNUSED)
783 return show_integer_format (settings_get_output_integer_format ());
787 show_wrb (const struct dataset *ds UNUSED)
789 return show_float_format (settings_get_output_float_format ());
793 show_width (const struct dataset *ds UNUSED)
795 return xasprintf ("%d", settings_get_viewwidth ());
801 char *(*function) (const struct dataset *);
804 const struct show_sbc show_table[] =
806 {"BLANKS", show_blanks},
812 {"DECIMALS", show_decimals},
813 {"ENDCMD", show_endcmd},
814 {"ERRORS", show_errors},
815 {"FORMAT", show_format},
816 {"LENGTH", show_length},
817 {"LOCALE", show_locale},
818 {"MESSAGES", show_messages},
819 {"MXERRS", show_mxerrs},
820 {"MXLOOPS", show_mxloops},
821 {"MXWARNS", show_mxwarns},
822 {"PRINTBACk", show_printback},
823 {"RESULTS", show_results},
826 {"SCOMPRESSION", show_scompression},
827 {"UNDEFINED", show_undefined},
828 {"WEIGHT", show_weight},
831 {"WIDTH", show_width},
835 do_show (const struct dataset *ds, const struct show_sbc *sbc)
837 char *value = sbc->function (ds);
838 msg (SN, _("%s is %s."), sbc->name, value);
843 show_all (const struct dataset *ds)
847 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
848 do_show (ds, &show_table[i]);
852 show_all_cc (const struct dataset *ds)
856 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
858 const struct show_sbc *sbc = &show_table[i];
859 if (!strncmp (sbc->name, "CC", 2))
865 show_warranty (const struct dataset *ds UNUSED)
867 fputs (lack_of_warranty, stdout);
871 show_copying (const struct dataset *ds UNUSED)
873 fputs (copyleft, stdout);
877 cmd_show (struct lexer *lexer, struct dataset *ds)
879 if (lex_token (lexer) == T_ENDCMD)
887 if (lex_match (lexer, T_ALL))
889 else if (lex_match_id (lexer, "CC"))
891 else if (lex_match_id (lexer, "WARRANTY"))
893 else if (lex_match_id (lexer, "COPYING"))
895 else if (lex_token (lexer) == T_ID)
899 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
901 const struct show_sbc *sbc = &show_table[i];
902 if (lex_match_id (lexer, sbc->name))
908 lex_error (lexer, NULL);
915 lex_error (lexer, NULL);
919 lex_match (lexer, T_SLASH);
921 while (lex_token (lexer) != T_ENDCMD);
926 #define MAX_SAVED_SETTINGS 5
928 static struct settings *saved_settings[MAX_SAVED_SETTINGS];
929 static int n_saved_settings;
932 cmd_preserve (struct lexer *lexer, struct dataset *ds UNUSED)
934 if (n_saved_settings < MAX_SAVED_SETTINGS)
936 saved_settings[n_saved_settings++] = settings_get ();
937 return lex_end_of_command (lexer);
941 msg (SE, _("Too many PRESERVE commands without a RESTORE: at most "
942 "%d levels of saved settings are allowed."),
944 return CMD_CASCADING_FAILURE;
949 cmd_restore (struct lexer *lexer, struct dataset *ds UNUSED)
951 if (n_saved_settings > 0)
953 struct settings *s = saved_settings[--n_saved_settings];
955 settings_destroy (s);
956 return lex_end_of_command (lexer);
960 msg (SE, _("RESTORE without matching PRESERVE."));