1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009 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/journal.h>
44 #include <output/output.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;
78 endcmd=string "x==1" "one character long";
80 errorbreak=errbrk:on/off;
81 errors=errors:terminal/listing/both/on/none/off;
83 headers=headers:no/yes/blank;
85 histogram=string "x==1" "one character long";
92 lowres=lores:auto/on/off;
93 lpi=integer "x>0" "%s must be greater than 0";
94 menus=menus:standard/extended;
95 messages=messages:on/off/terminal/listing/both/on/none/off;
97 miterate=integer "x>0" "%s must be greater than 0";
98 mnest=integer "x>0" "%s must be greater than 0";
100 mxerrs=integer "x >= 1" "%s must be at least 1";
101 mxloops=integer "x >=1" "%s must be at least 1";
105 printback=prtbck:on/off;
107 results=res:on/off/terminal/listing/both/on/none/off;
108 rib=rib:msbfirst/lsbfirst/vax/native;
109 rrb=rrb:native/isl/isb/idl/idb/vf/vd/vg/zs/zl;
111 scompression=scompress:on/off;
112 scripttab=string "x==1" "one character long";
114 tb1=string "x==3 || x==11" "3 or 11 characters long";
116 undefined=undef:warn/nowarn;
117 wib=wib:msbfirst/lsbfirst/vax/native;
118 wrb=wrb:native/isl/isb/idl/idb/vf/vd/vg/zs/zl;
120 workspace=integer "x>0" "%s must be positive";
130 static enum integer_format stc_to_integer_format (int stc);
131 static enum float_format stc_to_float_format (int stc);
134 cmd_set (struct lexer *lexer, struct dataset *ds)
138 if (!parse_set (lexer, ds, &cmd, NULL))
145 settings_set_cc ( cmd.s_cca, FMT_CCA);
147 settings_set_cc ( cmd.s_ccb, FMT_CCB);
149 settings_set_cc ( cmd.s_ccc, FMT_CCC);
151 settings_set_cc ( cmd.s_ccd, FMT_CCD);
153 settings_set_cc ( cmd.s_cce, FMT_CCE);
156 prompt_set (PROMPT_FIRST, cmd.s_prompt);
158 prompt_set (PROMPT_LATER, cmd.s_cprompt);
160 prompt_set (PROMPT_DATA, cmd.s_dprompt);
163 settings_set_decimal_char (cmd.dec == STC_DOT ? '.' : ',');
166 settings_set_echo (cmd.echo == STC_ON);
168 settings_set_endcmd (cmd.s_endcmd[0]);
169 if (cmd.sbc_errorbreak)
170 settings_set_errorbreak (cmd.errbrk == STC_ON);
173 bool both = cmd.errors == STC_BOTH || cmd.errors == STC_ON;
174 settings_set_error_routing_to_terminal (cmd.errors == STC_TERMINAL || both);
175 settings_set_error_routing_to_listing (cmd.errors == STC_LISTING || both);
178 settings_set_include (cmd.inc == STC_ON);
180 settings_set_mxerrs (cmd.n_mxerrs[0]);
182 settings_set_mxwarns (cmd.n_mxwarns[0]);
184 settings_set_nulline (cmd.null == STC_ON);
186 settings_set_input_integer_format (stc_to_integer_format (cmd.rib));
188 settings_set_input_float_format (stc_to_float_format (cmd.rrb));
190 settings_set_safer_mode ();
191 if (cmd.sbc_scompression)
192 settings_set_scompression (cmd.scompress == STC_ON);
193 if (cmd.sbc_undefined)
194 settings_set_undefined (cmd.undef == STC_WARN);
196 settings_set_output_integer_format (stc_to_integer_format (cmd.wib));
198 settings_set_output_float_format (stc_to_float_format (cmd.wrb));
199 if (cmd.sbc_workspace)
201 if ( cmd.n_workspace[0] < 1024 && ! settings_get_testing_mode ())
202 msg (SE, _("WORKSPACE must be at least 1MB"));
204 settings_set_workspace (cmd.n_workspace[0] * 1024L);
208 msg (SW, _("%s is obsolete."), "BLOCK");
209 if (cmd.sbc_boxstring)
210 msg (SW, _("%s is obsolete."), "BOXSTRING");
211 if (cmd.sbc_histogram)
212 msg (SW, _("%s is obsolete."), "HISTOGRAM");
214 msg (SW, _("%s is obsolete."), "MENUS");
216 msg (SW, _("%s is obsolete."), "XSORT");
217 if (cmd.sbc_mxmemory)
218 msg (SE, _("%s is obsolete."), "MXMEMORY");
219 if (cmd.sbc_scripttab)
220 msg (SE, _("%s is obsolete."), "SCRIPTTAB");
222 msg (SW, _("%s is obsolete."), "TBFONTS");
223 if (cmd.sbc_tb1 && cmd.s_tb1)
224 msg (SW, _("%s is obsolete."), "TB1");
227 msg (SW, _("%s is not implemented."), "CASE");
229 if (cmd.sbc_compression)
230 msg (SW, _("Active file compression is not implemented."));
237 /* Returns the integer_format value corresponding to STC,
238 which should be the value of cmd.rib or cmd.wib. */
239 static enum integer_format
240 stc_to_integer_format (int stc)
242 return (stc == STC_MSBFIRST ? INTEGER_MSB_FIRST
243 : stc == STC_LSBFIRST ? INTEGER_LSB_FIRST
244 : stc == STC_VAX ? INTEGER_VAX
248 /* Returns the float_format value corresponding to STC,
249 which should be the value of cmd.rrb or cmd.wrb. */
250 static enum float_format
251 stc_to_float_format (int stc)
256 return FLOAT_NATIVE_DOUBLE;
259 return FLOAT_IEEE_SINGLE_LE;
261 return FLOAT_IEEE_SINGLE_BE;
263 return FLOAT_IEEE_DOUBLE_LE;
265 return FLOAT_IEEE_DOUBLE_BE;
275 return FLOAT_Z_SHORT;
285 /* Parses the BLANKS subcommand, which controls the value that
286 completely blank fields in numeric data imply. X, Wnd: Syntax is
287 SYSMIS or a numeric value. */
289 stc_custom_blanks (struct lexer *lexer,
290 struct dataset *ds UNUSED,
291 struct cmd_set *cmd UNUSED, void *aux UNUSED)
293 lex_match (lexer, '=');
294 if (lex_match_id (lexer, "SYSMIS"))
297 settings_set_blanks (SYSMIS);
301 if (!lex_force_num (lexer))
303 settings_set_blanks (lex_number (lexer));
309 /* Parses the EPOCH subcommand, which controls the epoch used for
310 parsing 2-digit years. */
312 stc_custom_epoch (struct lexer *lexer,
313 struct dataset *ds UNUSED,
314 struct cmd_set *cmd UNUSED, void *aux UNUSED)
316 lex_match (lexer, '=');
317 if (lex_match_id (lexer, "AUTOMATIC"))
318 settings_set_epoch (-1);
319 else if (lex_is_integer (lexer))
321 int new_epoch = lex_integer (lexer);
323 if (new_epoch < 1500)
325 msg (SE, _("EPOCH must be 1500 or later."));
328 settings_set_epoch (new_epoch);
332 lex_error (lexer, _("expecting AUTOMATIC or year"));
340 stc_custom_length (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
344 lex_match (lexer, '=');
345 if (lex_match_id (lexer, "NONE"))
349 if (!lex_force_int (lexer))
351 if (lex_integer (lexer) < 1)
353 msg (SE, _("LENGTH must be at least 1."));
356 page_length = lex_integer (lexer);
360 if (page_length != -1)
361 settings_set_viewlength (page_length);
367 stc_custom_locale (struct lexer *lexer, struct dataset *ds UNUSED,
368 struct cmd_set *cmd UNUSED, void *aux UNUSED)
370 const struct string *s;
372 lex_match (lexer, '=');
374 if ( !lex_force_string (lexer))
377 s = lex_tokstr (lexer);
379 /* First try this string as an encoding name */
380 if ( valid_encoding (ds_cstr (s)))
381 set_default_encoding (ds_cstr (s));
383 /* Now try as a locale name (or alias) */
384 else if (set_encoding_from_locale (ds_cstr (s)))
389 msg (ME, _("%s is not a recognised encoding or locale name"),
402 stc_custom_seed (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
404 lex_match (lexer, '=');
405 if (lex_match_id (lexer, "RANDOM"))
409 if (!lex_force_num (lexer))
411 set_rng (lex_number (lexer));
419 stc_custom_width (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
421 lex_match (lexer, '=');
422 if (lex_match_id (lexer, "NARROW"))
423 settings_set_viewwidth (79);
424 else if (lex_match_id (lexer, "WIDE"))
425 settings_set_viewwidth (131);
428 if (!lex_force_int (lexer))
430 if (lex_integer (lexer) < 40)
432 msg (SE, _("WIDTH must be at least 40."));
435 settings_set_viewwidth (lex_integer (lexer));
442 /* Parses FORMAT subcommand, which consists of a numeric format
445 stc_custom_format (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
449 lex_match (lexer, '=');
450 if (!parse_format_specifier (lexer, &fmt))
452 if (fmt_is_string (fmt.type))
454 char str[FMT_STRING_LEN_MAX + 1];
455 msg (SE, _("FORMAT requires numeric output format as an argument. "
456 "Specified format %s is of type string."),
457 fmt_to_string (&fmt, str));
461 settings_set_format (&fmt);
466 stc_custom_journal (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
468 lex_match (lexer, '=');
469 if (lex_match_id (lexer, "ON") || lex_match_id (lexer, "YES"))
471 else if (lex_match_id (lexer, "OFF") || lex_match_id (lexer, "NO"))
473 else if (lex_token (lexer) == T_STRING || lex_token (lexer) == T_ID)
475 journal_set_file_name (ds_cstr (lex_tokstr (lexer)));
480 lex_error (lexer, NULL);
487 stc_custom_log (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
489 return stc_custom_journal (lexer, ds, cmd, aux);
493 stc_custom_listing (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
497 lex_match (lexer, '=');
498 if (lex_match_id (lexer, "ON") || lex_match_id (lexer, "YES"))
500 else if (lex_match_id (lexer, "OFF") || lex_match_id (lexer, "NO"))
507 outp_enable_device (listing, OUTP_DEV_LISTING);
513 stc_custom_disk (struct lexer *lexer, struct dataset *ds, struct cmd_set *cmd UNUSED, void *aux)
515 return stc_custom_listing (lexer, ds, cmd, aux);
519 show_blanks (const struct dataset *ds UNUSED)
521 if (settings_get_blanks () == SYSMIS)
522 msg (SN, _("BLANKS is SYSMIS."));
524 msg (SN, _("BLANKS is %g."), settings_get_blanks ());
529 format_cc (struct substring in, char grouping, char *out)
531 while (!ss_is_empty (in))
533 char c = ss_get_char (&in);
534 if (c == grouping || c == '\'')
544 show_cc (enum fmt_type type)
546 const struct fmt_number_style *cc = settings_get_style (type);
547 char cc_string[FMT_STYLE_AFFIX_MAX * 4 * 2 + 3 + 1];
550 out = format_cc (cc->neg_prefix, cc->grouping, cc_string);
551 *out++ = cc->grouping;
552 out = format_cc (cc->prefix, cc->grouping, out);
553 *out++ = cc->grouping;
554 out = format_cc (cc->suffix, cc->grouping, out);
555 *out++ = cc->grouping;
556 out = format_cc (cc->neg_suffix, cc->grouping, out);
559 msg (SN, _("%s is \"%s\"."), fmt_name (type), cc_string);
563 show_cca (const struct dataset *ds UNUSED)
569 show_ccb (const struct dataset *ds UNUSED)
575 show_ccc (const struct dataset *ds UNUSED)
581 show_ccd (const struct dataset *ds UNUSED)
587 show_cce (const struct dataset *ds UNUSED)
593 show_decimals (const struct dataset *ds UNUSED)
595 msg (SN, _("DECIMAL is \"%c\"."), settings_get_decimal_char (FMT_F));
599 show_endcmd (const struct dataset *ds UNUSED)
601 msg (SN, _("ENDCMD is \"%c\"."), settings_get_endcmd ());
605 show_errors (const struct dataset *ds UNUSED)
607 bool terminal = settings_get_error_routing_to_terminal ();
608 bool listing = settings_get_error_routing_to_listing ();
609 msg (SN, _("ERRORS is \"%s\"."),
610 terminal && listing ? "BOTH"
611 : terminal ? "TERMINAL"
612 : listing ? "LISTING"
617 show_format (const struct dataset *ds UNUSED)
619 char str[FMT_STRING_LEN_MAX + 1];
620 msg (SN, _("FORMAT is %s."), fmt_to_string (settings_get_format (), str));
624 show_length (const struct dataset *ds UNUSED)
626 msg (SN, _("LENGTH is %d."), settings_get_viewlength ());
630 show_locale (const struct dataset *ds UNUSED)
632 msg (SN, _("LOCALE is %s"), get_default_encoding ());
636 show_mxerrs (const struct dataset *ds UNUSED)
638 msg (SN, _("MXERRS is %d."), settings_get_mxerrs ());
642 show_mxloops (const struct dataset *ds UNUSED)
644 msg (SN, _("MXLOOPS is %d."), settings_get_mxloops ());
648 show_mxwarns (const struct dataset *ds UNUSED)
650 msg (SN, _("MXWARNS is %d."), settings_get_mxwarns ());
653 /* Outputs that SETTING has the given INTEGER_FORMAT value. */
655 show_integer_format (const char *setting, enum integer_format integer_format)
657 msg (SN, _("%s is %s (%s)."),
659 (integer_format == INTEGER_MSB_FIRST ? "MSBFIRST"
660 : integer_format == INTEGER_LSB_FIRST ? "LSBFIRST"
662 integer_format == INTEGER_NATIVE ? "NATIVE" : "nonnative");
665 /* Outputs that SETTING has the given FLOAT_FORMAT value. */
667 show_float_format (const char *setting, enum float_format float_format)
669 const char *format_name = "";
671 switch (float_format)
673 case FLOAT_IEEE_SINGLE_LE:
674 format_name = "ISL (32-bit IEEE 754 single, little-endian)";
676 case FLOAT_IEEE_SINGLE_BE:
677 format_name = "ISB (32-bit IEEE 754 single, big-endian)";
679 case FLOAT_IEEE_DOUBLE_LE:
680 format_name = "IDL (64-bit IEEE 754 double, little-endian)";
682 case FLOAT_IEEE_DOUBLE_BE:
683 format_name = "IDB (64-bit IEEE 754 double, big-endian)";
687 format_name = "VF (32-bit VAX F, VAX-endian)";
690 format_name = "VD (64-bit VAX D, VAX-endian)";
693 format_name = "VG (64-bit VAX G, VAX-endian)";
697 format_name = "ZS (32-bit IBM Z hexadecimal short, big-endian)";
700 format_name = "ZL (64-bit IBM Z hexadecimal long, big-endian)";
708 msg (SN, _("%s is %s (%s)."),
709 setting, format_name,
710 float_format == FLOAT_NATIVE_DOUBLE ? "NATIVE" : "nonnative");
714 show_rib (const struct dataset *ds UNUSED)
716 show_integer_format ("RIB", settings_get_input_integer_format ());
720 show_rrb (const struct dataset *ds UNUSED)
722 show_float_format ("RRB", settings_get_input_float_format ());
726 show_scompression (const struct dataset *ds UNUSED)
728 if (settings_get_scompression ())
729 msg (SN, _("SCOMPRESSION is ON."));
731 msg (SN, _("SCOMPRESSION is OFF."));
735 show_undefined (const struct dataset *ds UNUSED)
737 if (settings_get_undefined ())
738 msg (SN, _("UNDEFINED is WARN."));
740 msg (SN, _("UNDEFINED is NOWARN."));
744 show_weight (const struct dataset *ds)
746 const struct variable *var = dict_get_weight (dataset_dict (ds));
748 msg (SN, _("WEIGHT is off."));
750 msg (SN, _("WEIGHT is variable %s."), var_get_name (var));
754 show_wib (const struct dataset *ds UNUSED)
756 show_integer_format ("WIB", settings_get_output_integer_format ());
760 show_wrb (const struct dataset *ds UNUSED)
762 show_float_format ("WRB", settings_get_output_float_format ());
766 show_width (const struct dataset *ds UNUSED)
768 msg (SN, _("WIDTH is %d."), settings_get_viewwidth ());
774 void (*function) (const struct dataset *);
777 const struct show_sbc show_table[] =
779 {"BLANKS", show_blanks},
785 {"DECIMALS", show_decimals},
786 {"ENDCMD", show_endcmd},
787 {"ERRORS", show_errors},
788 {"FORMAT", show_format},
789 {"LENGTH", show_length},
790 {"LOCALE", show_locale},
791 {"MXERRS", show_mxerrs},
792 {"MXLOOPS", show_mxloops},
793 {"MXWARNS", show_mxwarns},
796 {"SCOMPRESSION", show_scompression},
797 {"UNDEFINED", show_undefined},
798 {"WEIGHT", show_weight},
801 {"WIDTH", show_width},
805 show_all (const struct dataset *ds)
809 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
810 show_table[i].function (ds);
818 for (i = 0; i < 5; i++)
823 show_warranty (const struct dataset *ds UNUSED)
825 fputs (lack_of_warranty, stdout);
829 show_copying (const struct dataset *ds UNUSED)
831 fputs (copyleft, stdout);
835 cmd_show (struct lexer *lexer, struct dataset *ds)
837 if (lex_token (lexer) == '.')
845 if (lex_match (lexer, T_ALL))
847 else if (lex_match_id (lexer, "CC"))
849 else if (lex_match_id (lexer, "WARRANTY"))
851 else if (lex_match_id (lexer, "COPYING"))
853 else if (lex_token (lexer) == T_ID)
857 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
858 if (lex_match_id (lexer, show_table[i].name))
860 show_table[i].function (ds);
863 lex_error (lexer, NULL);
870 lex_error (lexer, NULL);
874 lex_match (lexer, '/');
876 while (lex_token (lexer) != '.');