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;
79 headers=headers:no/yes/blank;
81 histogram=string "x==1" "one character long";
87 lowres=lores:auto/on/off;
88 lpi=integer "x>0" "%s must be greater than 0";
89 menus=menus:standard/extended;
92 miterate=integer "x>0" "%s must be greater than 0";
93 mnest=integer "x>0" "%s must be greater than 0";
95 mxerrs=integer "x >= 1" "%s must be at least 1";
96 mxloops=integer "x >=1" "%s must be at least 1";
102 rib=rib:msbfirst/lsbfirst/vax/native;
103 rrb=rrb:native/isl/isb/idl/idb/vf/vd/vg/zs/zl;
105 scompression=scompress:on/off;
106 scripttab=string "x==1" "one character long";
108 tb1=string "x==3 || x==11" "3 or 11 characters long";
110 undefined=undef:warn/nowarn;
111 wib=wib:msbfirst/lsbfirst/vax/native;
112 wrb=wrb:native/isl/isb/idl/idb/vf/vd/vg/zs/zl;
114 workspace=integer "x>0" "%s must be positive";
124 static enum integer_format stc_to_integer_format (int stc);
125 static enum float_format stc_to_float_format (int stc);
128 cmd_set (struct lexer *lexer, struct dataset *ds)
132 if (!parse_set (lexer, ds, &cmd, NULL))
139 settings_set_cc ( cmd.s_cca, FMT_CCA);
141 settings_set_cc ( cmd.s_ccb, FMT_CCB);
143 settings_set_cc ( cmd.s_ccc, FMT_CCC);
145 settings_set_cc ( cmd.s_ccd, FMT_CCD);
147 settings_set_cc ( cmd.s_cce, FMT_CCE);
150 prompt_set (PROMPT_FIRST, cmd.s_prompt);
152 prompt_set (PROMPT_LATER, cmd.s_cprompt);
154 prompt_set (PROMPT_DATA, cmd.s_dprompt);
157 settings_set_decimal_char (cmd.dec == STC_DOT ? '.' : ',');
160 settings_set_include (cmd.inc == STC_ON);
162 settings_set_max_messages (MSG_S_ERROR, cmd.n_mxerrs[0]);
164 settings_set_max_messages (MSG_S_WARNING, cmd.n_mxwarns[0]);
166 settings_set_input_integer_format (stc_to_integer_format (cmd.rib));
168 settings_set_input_float_format (stc_to_float_format (cmd.rrb));
170 settings_set_safer_mode ();
171 if (cmd.sbc_scompression)
172 settings_set_scompression (cmd.scompress == STC_ON);
173 if (cmd.sbc_undefined)
174 settings_set_undefined (cmd.undef == STC_WARN);
176 settings_set_output_integer_format (stc_to_integer_format (cmd.wib));
178 settings_set_output_float_format (stc_to_float_format (cmd.wrb));
179 if (cmd.sbc_workspace)
181 if ( cmd.n_workspace[0] < 1024 && ! settings_get_testing_mode ())
182 msg (SE, _("WORKSPACE must be at least 1MB"));
184 settings_set_workspace (cmd.n_workspace[0] * 1024L);
188 msg (SW, _("%s is obsolete."), "BLOCK");
189 if (cmd.sbc_boxstring)
190 msg (SW, _("%s is obsolete."), "BOXSTRING");
191 if (cmd.sbc_histogram)
192 msg (SW, _("%s is obsolete."), "HISTOGRAM");
194 msg (SW, _("%s is obsolete."), "MENUS");
196 msg (SW, _("%s is obsolete."), "XSORT");
197 if (cmd.sbc_mxmemory)
198 msg (SE, _("%s is obsolete."), "MXMEMORY");
199 if (cmd.sbc_scripttab)
200 msg (SE, _("%s is obsolete."), "SCRIPTTAB");
202 msg (SW, _("%s is obsolete."), "TBFONTS");
203 if (cmd.sbc_tb1 && cmd.s_tb1)
204 msg (SW, _("%s is obsolete."), "TB1");
207 msg (SW, _("%s is not yet implemented."), "CASE");
209 if (cmd.sbc_compression)
210 msg (SW, _("Active file compression is not implemented."));
217 /* Returns the integer_format value corresponding to STC,
218 which should be the value of cmd.rib or cmd.wib. */
219 static enum integer_format
220 stc_to_integer_format (int stc)
222 return (stc == STC_MSBFIRST ? INTEGER_MSB_FIRST
223 : stc == STC_LSBFIRST ? INTEGER_LSB_FIRST
224 : stc == STC_VAX ? INTEGER_VAX
228 /* Returns the float_format value corresponding to STC,
229 which should be the value of cmd.rrb or cmd.wrb. */
230 static enum float_format
231 stc_to_float_format (int stc)
236 return FLOAT_NATIVE_DOUBLE;
239 return FLOAT_IEEE_SINGLE_LE;
241 return FLOAT_IEEE_SINGLE_BE;
243 return FLOAT_IEEE_DOUBLE_LE;
245 return FLOAT_IEEE_DOUBLE_BE;
255 return FLOAT_Z_SHORT;
264 set_output_routing (struct lexer *lexer, enum settings_output_type type)
266 enum settings_output_devices devices;
268 lex_match (lexer, T_EQUALS);
269 if (lex_match_id (lexer, "ON") || lex_match_id (lexer, "BOTH"))
270 devices = SETTINGS_DEVICE_LISTING | SETTINGS_DEVICE_TERMINAL;
271 else if (lex_match_id (lexer, "TERMINAL"))
272 devices = SETTINGS_DEVICE_TERMINAL;
273 else if (lex_match_id (lexer, "LISTING"))
274 devices = SETTINGS_DEVICE_LISTING;
275 else if (lex_match_id (lexer, "OFF") || lex_match_id (lexer, "NONE"))
279 lex_error (lexer, NULL);
283 settings_set_output_routing (type, devices);
288 /* Parses the BLANKS subcommand, which controls the value that
289 completely blank fields in numeric data imply. X, Wnd: Syntax is
290 SYSMIS or a numeric value. */
292 stc_custom_blanks (struct lexer *lexer,
293 struct dataset *ds UNUSED,
294 struct cmd_set *cmd UNUSED, void *aux UNUSED)
296 lex_match (lexer, T_EQUALS);
297 if (lex_match_id (lexer, "SYSMIS"))
300 settings_set_blanks (SYSMIS);
304 if (!lex_force_num (lexer))
306 settings_set_blanks (lex_number (lexer));
312 /* Parses the EPOCH subcommand, which controls the epoch used for
313 parsing 2-digit years. */
315 stc_custom_epoch (struct lexer *lexer,
316 struct dataset *ds UNUSED,
317 struct cmd_set *cmd UNUSED, void *aux UNUSED)
319 lex_match (lexer, T_EQUALS);
320 if (lex_match_id (lexer, "AUTOMATIC"))
321 settings_set_epoch (-1);
322 else if (lex_is_integer (lexer))
324 int new_epoch = lex_integer (lexer);
326 if (new_epoch < 1500)
328 msg (SE, _("EPOCH must be 1500 or later."));
331 settings_set_epoch (new_epoch);
335 lex_error (lexer, _("expecting AUTOMATIC or year"));
343 stc_custom_errors (struct lexer *lexer, struct dataset *ds UNUSED,
344 struct cmd_set *cmd UNUSED, void *aux UNUSED)
346 return set_output_routing (lexer, SETTINGS_OUTPUT_ERROR);
350 stc_custom_length (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
354 lex_match (lexer, T_EQUALS);
355 if (lex_match_id (lexer, "NONE"))
359 if (!lex_force_int (lexer))
361 if (lex_integer (lexer) < 1)
363 msg (SE, _("LENGTH must be at least 1."));
366 page_length = lex_integer (lexer);
370 if (page_length != -1)
371 settings_set_viewlength (page_length);
377 stc_custom_locale (struct lexer *lexer, struct dataset *ds UNUSED,
378 struct cmd_set *cmd UNUSED, void *aux UNUSED)
382 lex_match (lexer, T_EQUALS);
384 if ( !lex_force_string (lexer))
387 s = lex_tokcstr (lexer);
389 /* First try this string as an encoding name */
390 if ( valid_encoding (s))
391 set_default_encoding (s);
393 /* Now try as a locale name (or alias) */
394 else if (set_encoding_from_locale (s))
399 msg (ME, _("%s is not a recognized encoding or locale name"), s);
409 stc_custom_messages (struct lexer *lexer, struct dataset *ds UNUSED,
410 struct cmd_set *cmd UNUSED, void *aux UNUSED)
412 return set_output_routing (lexer, SETTINGS_OUTPUT_NOTE);
416 stc_custom_printback (struct lexer *lexer, struct dataset *ds UNUSED,
417 struct cmd_set *cmd UNUSED, void *aux UNUSED)
419 return set_output_routing (lexer, SETTINGS_OUTPUT_SYNTAX);
423 stc_custom_results (struct lexer *lexer, struct dataset *ds UNUSED,
424 struct cmd_set *cmd UNUSED, void *aux UNUSED)
426 return set_output_routing (lexer, SETTINGS_OUTPUT_RESULT);
430 stc_custom_seed (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
432 lex_match (lexer, T_EQUALS);
433 if (lex_match_id (lexer, "RANDOM"))
437 if (!lex_force_num (lexer))
439 set_rng (lex_number (lexer));
447 stc_custom_width (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
449 lex_match (lexer, T_EQUALS);
450 if (lex_match_id (lexer, "NARROW"))
451 settings_set_viewwidth (79);
452 else if (lex_match_id (lexer, "WIDE"))
453 settings_set_viewwidth (131);
456 if (!lex_force_int (lexer))
458 if (lex_integer (lexer) < 40)
460 msg (SE, _("WIDTH must be at least 40."));
463 settings_set_viewwidth (lex_integer (lexer));
470 /* Parses FORMAT subcommand, which consists of a numeric format
473 stc_custom_format (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
477 lex_match (lexer, T_EQUALS);
478 if (!parse_format_specifier (lexer, &fmt))
481 if (!fmt_check_output (&fmt))
484 if (fmt_is_string (fmt.type))
486 char str[FMT_STRING_LEN_MAX + 1];
487 msg (SE, _("FORMAT requires numeric output format as an argument. "
488 "Specified format %s is of type string."),
489 fmt_to_string (&fmt, str));
493 settings_set_format (&fmt);
498 stc_custom_journal (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
500 lex_match (lexer, T_EQUALS);
501 if (lex_match_id (lexer, "ON") || lex_match_id (lexer, "YES"))
503 else if (lex_match_id (lexer, "OFF") || lex_match_id (lexer, "NO"))
505 else if (lex_is_string (lexer) || lex_token (lexer) == T_ID)
507 journal_set_file_name (lex_tokcstr (lexer));
512 lex_error (lexer, NULL);
519 stc_custom_log (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
521 return stc_custom_journal (lexer, ds, cmd, aux);
525 show_output_routing (enum settings_output_type type)
527 enum settings_output_devices devices;
530 devices = settings_get_output_routing (type);
531 if (devices & SETTINGS_DEVICE_LISTING)
532 s = devices & SETTINGS_DEVICE_TERMINAL ? "BOTH" : "LISTING";
533 else if (devices & SETTINGS_DEVICE_TERMINAL)
542 show_blanks (const struct dataset *ds UNUSED)
544 return (settings_get_blanks () == SYSMIS
546 : xasprintf ("%g", settings_get_blanks ()));
550 format_cc (struct string *out, struct substring in, char grouping)
552 while (!ss_is_empty (in))
554 char c = ss_get_byte (&in);
555 if (c == grouping || c == '\'')
556 ds_put_byte (out, '\'');
558 ds_put_byte (out, '"');
559 ds_put_byte (out, c);
564 show_cc (enum fmt_type type)
566 const struct fmt_number_style *cc = settings_get_style (type);
569 ds_init_empty (&out);
570 format_cc (&out, cc->neg_prefix, cc->grouping);
571 ds_put_byte (&out, cc->grouping);
572 format_cc (&out, cc->prefix, cc->grouping);
573 ds_put_byte (&out, cc->grouping);
574 format_cc (&out, cc->suffix, cc->grouping);
575 ds_put_byte (&out, cc->grouping);
576 format_cc (&out, cc->neg_suffix, cc->grouping);
578 return ds_cstr (&out);
582 show_cca (const struct dataset *ds UNUSED)
584 return show_cc (FMT_CCA);
588 show_ccb (const struct dataset *ds UNUSED)
590 return show_cc (FMT_CCB);
594 show_ccc (const struct dataset *ds UNUSED)
596 return show_cc (FMT_CCC);
600 show_ccd (const struct dataset *ds UNUSED)
602 return show_cc (FMT_CCD);
606 show_cce (const struct dataset *ds UNUSED)
608 return show_cc (FMT_CCE);
612 show_decimals (const struct dataset *ds UNUSED)
614 return xasprintf ("`%c'", settings_get_decimal_char (FMT_F));
618 show_errors (const struct dataset *ds UNUSED)
620 return show_output_routing (SETTINGS_OUTPUT_ERROR);
624 show_format (const struct dataset *ds UNUSED)
626 char str[FMT_STRING_LEN_MAX + 1];
627 return xstrdup (fmt_to_string (settings_get_format (), str));
631 show_length (const struct dataset *ds UNUSED)
633 return xasprintf ("%d", settings_get_viewlength ());
637 show_locale (const struct dataset *ds UNUSED)
639 return xstrdup (get_default_encoding ());
643 show_messages (const struct dataset *ds UNUSED)
645 return show_output_routing (SETTINGS_OUTPUT_NOTE);
649 show_printback (const struct dataset *ds UNUSED)
651 return show_output_routing (SETTINGS_OUTPUT_SYNTAX);
655 show_results (const struct dataset *ds UNUSED)
657 return show_output_routing (SETTINGS_OUTPUT_RESULT);
661 show_mxerrs (const struct dataset *ds UNUSED)
663 return xasprintf ("%d", settings_get_max_messages (MSG_S_ERROR));
667 show_mxloops (const struct dataset *ds UNUSED)
669 return xasprintf ("%d", settings_get_mxloops ());
673 show_mxwarns (const struct dataset *ds UNUSED)
675 return xasprintf ("%d", settings_get_max_messages (MSG_S_WARNING));
678 /* Returns a name for the given INTEGER_FORMAT value. */
680 show_integer_format (enum integer_format integer_format)
682 return xasprintf ("%s (%s)",
683 (integer_format == INTEGER_MSB_FIRST ? "MSBFIRST"
684 : integer_format == INTEGER_LSB_FIRST ? "LSBFIRST"
686 integer_format == INTEGER_NATIVE ? "NATIVE" : "nonnative");
689 /* Returns a name for the given FLOAT_FORMAT value. */
691 show_float_format (enum float_format float_format)
693 const char *format_name = "";
695 switch (float_format)
697 case FLOAT_IEEE_SINGLE_LE:
698 format_name = _("ISL (32-bit IEEE 754 single, little-endian)");
700 case FLOAT_IEEE_SINGLE_BE:
701 format_name = _("ISB (32-bit IEEE 754 single, big-endian)");
703 case FLOAT_IEEE_DOUBLE_LE:
704 format_name = _("IDL (64-bit IEEE 754 double, little-endian)");
706 case FLOAT_IEEE_DOUBLE_BE:
707 format_name = _("IDB (64-bit IEEE 754 double, big-endian)");
711 format_name = _("VF (32-bit VAX F, VAX-endian)");
714 format_name = _("VD (64-bit VAX D, VAX-endian)");
717 format_name = _("VG (64-bit VAX G, VAX-endian)");
721 format_name = _("ZS (32-bit IBM Z hexadecimal short, big-endian)");
724 format_name = _("ZL (64-bit IBM Z hexadecimal long, big-endian)");
732 return xasprintf ("%s (%s)", format_name,
733 (float_format == FLOAT_NATIVE_DOUBLE
734 ? "NATIVE" : "nonnative"));
738 show_rib (const struct dataset *ds UNUSED)
740 return show_integer_format (settings_get_input_integer_format ());
744 show_rrb (const struct dataset *ds UNUSED)
746 return show_float_format (settings_get_input_float_format ());
750 show_scompression (const struct dataset *ds UNUSED)
752 return xstrdup (settings_get_scompression () ? "ON" : "OFF");
756 show_undefined (const struct dataset *ds UNUSED)
758 return xstrdup (settings_get_undefined () ? "WARN" : "NOWARN");
762 show_weight (const struct dataset *ds)
764 const struct variable *var = dict_get_weight (dataset_dict (ds));
765 return xstrdup (var != NULL ? var_get_name (var) : "OFF");
769 show_wib (const struct dataset *ds UNUSED)
771 return show_integer_format (settings_get_output_integer_format ());
775 show_wrb (const struct dataset *ds UNUSED)
777 return show_float_format (settings_get_output_float_format ());
781 show_width (const struct dataset *ds UNUSED)
783 return xasprintf ("%d", settings_get_viewwidth ());
789 char *(*function) (const struct dataset *);
792 const struct show_sbc show_table[] =
794 {"BLANKS", show_blanks},
800 {"DECIMALS", show_decimals},
801 {"ERRORS", show_errors},
802 {"FORMAT", show_format},
803 {"LENGTH", show_length},
804 {"LOCALE", show_locale},
805 {"MESSAGES", show_messages},
806 {"MXERRS", show_mxerrs},
807 {"MXLOOPS", show_mxloops},
808 {"MXWARNS", show_mxwarns},
809 {"PRINTBACk", show_printback},
810 {"RESULTS", show_results},
813 {"SCOMPRESSION", show_scompression},
814 {"UNDEFINED", show_undefined},
815 {"WEIGHT", show_weight},
818 {"WIDTH", show_width},
822 do_show (const struct dataset *ds, const struct show_sbc *sbc)
824 char *value = sbc->function (ds);
825 msg (SN, _("%s is %s."), sbc->name, value);
830 show_all (const struct dataset *ds)
834 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
835 do_show (ds, &show_table[i]);
839 show_all_cc (const struct dataset *ds)
843 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
845 const struct show_sbc *sbc = &show_table[i];
846 if (!strncmp (sbc->name, "CC", 2))
852 show_warranty (const struct dataset *ds UNUSED)
854 fputs (lack_of_warranty, stdout);
858 show_copying (const struct dataset *ds UNUSED)
860 fputs (copyleft, stdout);
864 cmd_show (struct lexer *lexer, struct dataset *ds)
866 if (lex_token (lexer) == T_ENDCMD)
874 if (lex_match (lexer, T_ALL))
876 else if (lex_match_id (lexer, "CC"))
878 else if (lex_match_id (lexer, "WARRANTY"))
880 else if (lex_match_id (lexer, "COPYING"))
882 else if (lex_token (lexer) == T_ID)
886 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
888 const struct show_sbc *sbc = &show_table[i];
889 if (lex_match_id (lexer, sbc->name))
895 lex_error (lexer, NULL);
902 lex_error (lexer, NULL);
906 lex_match (lexer, T_SLASH);
908 while (lex_token (lexer) != T_ENDCMD);
913 #define MAX_SAVED_SETTINGS 5
915 static struct settings *saved_settings[MAX_SAVED_SETTINGS];
916 static int n_saved_settings;
919 cmd_preserve (struct lexer *lexer, struct dataset *ds UNUSED)
921 if (n_saved_settings < MAX_SAVED_SETTINGS)
923 saved_settings[n_saved_settings++] = settings_get ();
924 return lex_end_of_command (lexer);
928 msg (SE, _("Too many PRESERVE commands without a RESTORE: at most "
929 "%d levels of saved settings are allowed."),
931 return CMD_CASCADING_FAILURE;
936 cmd_restore (struct lexer *lexer, struct dataset *ds UNUSED)
938 if (n_saved_settings > 0)
940 struct settings *s = saved_settings[--n_saved_settings];
942 settings_destroy (s);
943 return lex_end_of_command (lexer);
947 msg (SE, _("RESTORE without matching PRESERVE."));