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, '=');
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, '=');
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, '=');
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, '=');
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)
386 const struct string *s;
388 lex_match (lexer, '=');
390 if ( !lex_force_string (lexer))
393 s = lex_tokstr (lexer);
395 /* First try this string as an encoding name */
396 if ( valid_encoding (ds_cstr (s)))
397 set_default_encoding (ds_cstr (s));
399 /* Now try as a locale name (or alias) */
400 else if (set_encoding_from_locale (ds_cstr (s)))
405 msg (ME, _("%s is not a recognised encoding or locale name"),
416 stc_custom_messages (struct lexer *lexer, struct dataset *ds UNUSED,
417 struct cmd_set *cmd UNUSED, void *aux UNUSED)
419 return set_output_routing (lexer, SETTINGS_OUTPUT_NOTE);
423 stc_custom_printback (struct lexer *lexer, struct dataset *ds UNUSED,
424 struct cmd_set *cmd UNUSED, void *aux UNUSED)
426 return set_output_routing (lexer, SETTINGS_OUTPUT_SYNTAX);
430 stc_custom_results (struct lexer *lexer, struct dataset *ds UNUSED,
431 struct cmd_set *cmd UNUSED, void *aux UNUSED)
433 return set_output_routing (lexer, SETTINGS_OUTPUT_RESULT);
437 stc_custom_seed (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
439 lex_match (lexer, '=');
440 if (lex_match_id (lexer, "RANDOM"))
444 if (!lex_force_num (lexer))
446 set_rng (lex_number (lexer));
454 stc_custom_width (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
456 lex_match (lexer, '=');
457 if (lex_match_id (lexer, "NARROW"))
458 settings_set_viewwidth (79);
459 else if (lex_match_id (lexer, "WIDE"))
460 settings_set_viewwidth (131);
463 if (!lex_force_int (lexer))
465 if (lex_integer (lexer) < 40)
467 msg (SE, _("WIDTH must be at least 40."));
470 settings_set_viewwidth (lex_integer (lexer));
477 /* Parses FORMAT subcommand, which consists of a numeric format
480 stc_custom_format (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
484 lex_match (lexer, '=');
485 if (!parse_format_specifier (lexer, &fmt))
487 if (fmt_is_string (fmt.type))
489 char str[FMT_STRING_LEN_MAX + 1];
490 msg (SE, _("FORMAT requires numeric output format as an argument. "
491 "Specified format %s is of type string."),
492 fmt_to_string (&fmt, str));
496 settings_set_format (&fmt);
501 stc_custom_journal (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
503 lex_match (lexer, '=');
504 if (lex_match_id (lexer, "ON") || lex_match_id (lexer, "YES"))
506 else if (lex_match_id (lexer, "OFF") || lex_match_id (lexer, "NO"))
508 else if (lex_token (lexer) == T_STRING || lex_token (lexer) == T_ID)
510 journal_set_file_name (ds_cstr (lex_tokstr (lexer)));
515 lex_error (lexer, NULL);
522 stc_custom_log (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
524 return stc_custom_journal (lexer, ds, cmd, aux);
528 show_output_routing (enum settings_output_type type)
530 enum settings_output_devices devices;
533 devices = settings_get_output_routing (type);
534 if (devices & SETTINGS_DEVICE_LISTING)
535 s = devices & SETTINGS_DEVICE_TERMINAL ? "BOTH" : "LISTING";
536 else if (devices & SETTINGS_DEVICE_TERMINAL)
545 show_blanks (const struct dataset *ds UNUSED)
547 return (settings_get_blanks () == SYSMIS
549 : xasprintf ("%g", settings_get_blanks ()));
553 format_cc (struct string *out, struct substring in, char grouping)
555 while (!ss_is_empty (in))
557 char c = ss_get_char (&in);
558 if (c == grouping || c == '\'')
559 ds_put_char (out, '\'');
561 ds_put_char (out, '"');
562 ds_put_char (out, c);
567 show_cc (enum fmt_type type)
569 const struct fmt_number_style *cc = settings_get_style (type);
572 ds_init_empty (&out);
573 format_cc (&out, cc->neg_prefix, cc->grouping);
574 ds_put_char (&out, cc->grouping);
575 format_cc (&out, cc->prefix, cc->grouping);
576 ds_put_char (&out, cc->grouping);
577 format_cc (&out, cc->suffix, cc->grouping);
578 ds_put_char (&out, cc->grouping);
579 format_cc (&out, cc->neg_suffix, cc->grouping);
581 return ds_cstr (&out);
585 show_cca (const struct dataset *ds UNUSED)
587 return show_cc (FMT_CCA);
591 show_ccb (const struct dataset *ds UNUSED)
593 return show_cc (FMT_CCB);
597 show_ccc (const struct dataset *ds UNUSED)
599 return show_cc (FMT_CCC);
603 show_ccd (const struct dataset *ds UNUSED)
605 return show_cc (FMT_CCD);
609 show_cce (const struct dataset *ds UNUSED)
611 return show_cc (FMT_CCE);
615 show_decimals (const struct dataset *ds UNUSED)
617 return xasprintf ("\"%c\"", settings_get_decimal_char (FMT_F));
621 show_endcmd (const struct dataset *ds UNUSED)
623 return xasprintf ("\"%c\"", settings_get_endcmd ());
627 show_errors (const struct dataset *ds UNUSED)
629 return show_output_routing (SETTINGS_OUTPUT_ERROR);
633 show_format (const struct dataset *ds UNUSED)
635 char str[FMT_STRING_LEN_MAX + 1];
636 return xstrdup (fmt_to_string (settings_get_format (), str));
640 show_length (const struct dataset *ds UNUSED)
642 return xasprintf ("%d", settings_get_viewlength ());
646 show_locale (const struct dataset *ds UNUSED)
648 return xstrdup (get_default_encoding ());
652 show_messages (const struct dataset *ds UNUSED)
654 return show_output_routing (SETTINGS_OUTPUT_NOTE);
658 show_printback (const struct dataset *ds UNUSED)
660 return show_output_routing (SETTINGS_OUTPUT_SYNTAX);
664 show_results (const struct dataset *ds UNUSED)
666 return show_output_routing (SETTINGS_OUTPUT_RESULT);
670 show_mxerrs (const struct dataset *ds UNUSED)
672 return xasprintf ("%d", settings_get_max_messages (MSG_S_ERROR));
676 show_mxloops (const struct dataset *ds UNUSED)
678 return xasprintf ("%d", settings_get_mxloops ());
682 show_mxwarns (const struct dataset *ds UNUSED)
684 return xasprintf ("%d", settings_get_max_messages (MSG_S_WARNING));
687 /* Returns a name for the given INTEGER_FORMAT value. */
689 show_integer_format (enum integer_format integer_format)
691 return xasprintf ("%s (%s)",
692 (integer_format == INTEGER_MSB_FIRST ? "MSBFIRST"
693 : integer_format == INTEGER_LSB_FIRST ? "LSBFIRST"
695 integer_format == INTEGER_NATIVE ? "NATIVE" : "nonnative");
698 /* Returns a name for the given FLOAT_FORMAT value. */
700 show_float_format (enum float_format float_format)
702 const char *format_name = "";
704 switch (float_format)
706 case FLOAT_IEEE_SINGLE_LE:
707 format_name = _("ISL (32-bit IEEE 754 single, little-endian)");
709 case FLOAT_IEEE_SINGLE_BE:
710 format_name = _("ISB (32-bit IEEE 754 single, big-endian)");
712 case FLOAT_IEEE_DOUBLE_LE:
713 format_name = _("IDL (64-bit IEEE 754 double, little-endian)");
715 case FLOAT_IEEE_DOUBLE_BE:
716 format_name = _("IDB (64-bit IEEE 754 double, big-endian)");
720 format_name = _("VF (32-bit VAX F, VAX-endian)");
723 format_name = _("VD (64-bit VAX D, VAX-endian)");
726 format_name = _("VG (64-bit VAX G, VAX-endian)");
730 format_name = _("ZS (32-bit IBM Z hexadecimal short, big-endian)");
733 format_name = _("ZL (64-bit IBM Z hexadecimal long, big-endian)");
741 return xasprintf ("%s (%s)", format_name,
742 (float_format == FLOAT_NATIVE_DOUBLE
743 ? "NATIVE" : "nonnative"));
747 show_rib (const struct dataset *ds UNUSED)
749 return show_integer_format (settings_get_input_integer_format ());
753 show_rrb (const struct dataset *ds UNUSED)
755 return show_float_format (settings_get_input_float_format ());
759 show_scompression (const struct dataset *ds UNUSED)
761 return xstrdup (settings_get_scompression () ? "ON" : "OFF");
765 show_undefined (const struct dataset *ds UNUSED)
767 return xstrdup (settings_get_undefined () ? "WARN" : "NOWARN");
771 show_weight (const struct dataset *ds)
773 const struct variable *var = dict_get_weight (dataset_dict (ds));
774 return xstrdup (var != NULL ? var_get_name (var) : "OFF");
778 show_wib (const struct dataset *ds UNUSED)
780 return show_integer_format (settings_get_output_integer_format ());
784 show_wrb (const struct dataset *ds UNUSED)
786 return show_float_format (settings_get_output_float_format ());
790 show_width (const struct dataset *ds UNUSED)
792 return xasprintf ("%d", settings_get_viewwidth ());
798 char *(*function) (const struct dataset *);
801 const struct show_sbc show_table[] =
803 {"BLANKS", show_blanks},
809 {"DECIMALS", show_decimals},
810 {"ENDCMD", show_endcmd},
811 {"ERRORS", show_errors},
812 {"FORMAT", show_format},
813 {"LENGTH", show_length},
814 {"LOCALE", show_locale},
815 {"MESSAGES", show_messages},
816 {"MXERRS", show_mxerrs},
817 {"MXLOOPS", show_mxloops},
818 {"MXWARNS", show_mxwarns},
819 {"PRINTBACk", show_printback},
820 {"RESULTS", show_results},
823 {"SCOMPRESSION", show_scompression},
824 {"UNDEFINED", show_undefined},
825 {"WEIGHT", show_weight},
828 {"WIDTH", show_width},
832 do_show (const struct dataset *ds, const struct show_sbc *sbc)
834 char *value = sbc->function (ds);
835 msg (SN, _("%s is %s."), sbc->name, value);
840 show_all (const struct dataset *ds)
844 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
845 do_show (ds, &show_table[i]);
849 show_all_cc (const struct dataset *ds)
853 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
855 const struct show_sbc *sbc = &show_table[i];
856 if (!strncmp (sbc->name, "CC", 2))
862 show_warranty (const struct dataset *ds UNUSED)
864 fputs (lack_of_warranty, stdout);
868 show_copying (const struct dataset *ds UNUSED)
870 fputs (copyleft, stdout);
874 cmd_show (struct lexer *lexer, struct dataset *ds)
876 if (lex_token (lexer) == '.')
884 if (lex_match (lexer, T_ALL))
886 else if (lex_match_id (lexer, "CC"))
888 else if (lex_match_id (lexer, "WARRANTY"))
890 else if (lex_match_id (lexer, "COPYING"))
892 else if (lex_token (lexer) == T_ID)
896 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
898 const struct show_sbc *sbc = &show_table[i];
899 if (lex_match_id (lexer, sbc->name))
905 lex_error (lexer, NULL);
912 lex_error (lexer, NULL);
916 lex_match (lexer, '/');
918 while (lex_token (lexer) != '.');