1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 #include <data/data-out.h>
28 #include <data/dictionary.h>
29 #include <data/format.h>
30 #include <data/procedure.h>
31 #include <data/settings.h>
32 #include <data/variable.h>
33 #include <language/command.h>
34 #include <language/lexer/format-parser.h>
35 #include <language/lexer/lexer.h>
36 #include <language/line-buffer.h>
37 #include <libpspp/alloc.h>
38 #include <libpspp/compiler.h>
39 #include <libpspp/copyleft.h>
40 #include <libpspp/float-format.h>
41 #include <libpspp/integer-format.h>
42 #include <libpspp/magic.h>
43 #include <libpspp/message.h>
44 #include <math/random.h>
45 #include <output/output.h>
50 #else /* !HAVE_TERMCAP_H */
51 int tgetent (char *, const char *);
52 int tgetnum (const char *);
53 #endif /* !HAVE_TERMCAP_H */
54 #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";
73 decimal=dec:dot/comma;
77 endcmd=string "x==1" "one character long";
79 errorbreak=errbrk:on/off;
80 errors=errors:terminal/listing/both/on/none/off;
82 headers=headers:no/yes/blank;
84 histogram=string "x==1" "one character long";
89 lowres=lores:auto/on/off;
90 lpi=integer "x>0" "%s must be greater than 0";
91 menus=menus:standard/extended;
92 messages=messages:on/off/terminal/listing/both/on/none/off;
94 miterate=integer "x>0" "%s must be greater than 0";
95 mnest=integer "x>0" "%s must be greater than 0";
97 mxerrs=integer "x >= 1" "%s must be at least 1";
98 mxloops=integer "x >=1" "%s must be at least 1";
102 printback=prtbck:on/off;
104 results=res:on/off/terminal/listing/both/on/none/off;
106 scompression=scompress:on/off;
107 scripttab=string "x==1" "one character long";
109 tb1=string "x==3 || x==11" "3 or 11 characters long";
111 undefined=undef:warn/nowarn;
112 wib=wib:msbfirst/lsbfirst/vax/native;
113 wrb=wrb:native/isl/isb/idl/idb/vf/vd/vg/zs/zl;
115 workspace=integer "x>=1024" "%s must be at least 1 MB";
125 static bool do_cc (const char *cc_string, enum fmt_type);
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 do_cc (cmd.s_cca, FMT_CCA);
143 do_cc (cmd.s_ccb, FMT_CCB);
145 do_cc (cmd.s_ccc, FMT_CCC);
147 do_cc (cmd.s_ccd, FMT_CCD);
149 do_cc (cmd.s_cce, FMT_CCE);
152 getl_set_prompt (GETL_PROMPT_FIRST, cmd.s_prompt);
154 getl_set_prompt (GETL_PROMPT_LATER, cmd.s_cprompt);
156 getl_set_prompt (GETL_PROMPT_DATA, cmd.s_dprompt);
159 fmt_set_decimal (cmd.dec == STC_DOT ? '.' : ',');
161 set_echo (cmd.echo == STC_ON);
163 set_endcmd (cmd.s_endcmd[0]);
164 if (cmd.sbc_errorbreak)
165 set_errorbreak (cmd.errbrk == STC_ON);
168 bool both = cmd.errors == STC_BOTH || cmd.errors == STC_ON;
169 set_error_routing_to_terminal (cmd.errors == STC_TERMINAL || both);
170 set_error_routing_to_listing (cmd.errors == STC_LISTING || both);
173 set_include (cmd.inc == STC_ON);
175 set_mxerrs (cmd.n_mxerrs[0]);
177 set_mxwarns (cmd.n_mxwarns[0]);
179 set_nulline (cmd.null == STC_ON);
182 if (cmd.sbc_scompression)
183 set_scompression (cmd.scompress == STC_ON);
184 if (cmd.sbc_undefined)
185 set_undefined (cmd.undef == STC_WARN);
187 data_out_set_integer_format (stc_to_integer_format (cmd.wib));
189 data_out_set_float_format (stc_to_float_format (cmd.wrb));
190 if (cmd.sbc_workspace)
191 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 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;
269 /* Find the grouping characters in CC_STRING and set CC's
270 grouping and decimal members appropriately. Returns true if
271 successful, false otherwise. */
273 find_cc_separators (const char *cc_string, struct fmt_number_style *cc)
276 int comma_cnt, dot_cnt;
278 /* Count commas and periods. There must be exactly three of
279 one or the other, except that an apostrophe escapes a
280 following comma or period. */
281 comma_cnt = dot_cnt = 0;
282 for (sp = cc_string; *sp; sp++)
287 else if (*sp == '\'' && (sp[1] == '.' || sp[1] == ',' || sp[1] == '\''))
290 if ((comma_cnt == 3) == (dot_cnt == 3))
306 /* Extracts a token from IN into a newly allocated AFFIX. Tokens
307 are delimited by GROUPING. The token is truncated to at most
308 FMT_STYLE_AFFIX_MAX characters. Returns the first character
309 following the token. */
311 extract_cc_token (const char *in, int grouping, struct substring *affix)
314 ss_alloc_uninit (affix, FMT_STYLE_AFFIX_MAX);
315 for (; *in != '\0' && *in != grouping; in++)
317 if (*in == '\'' && in[1] == grouping)
319 if (ofs < FMT_STYLE_AFFIX_MAX)
320 ss_data (*affix)[ofs++] = *in;
329 /* Sets custom currency specifier CC having name CC_NAME ('A' through
330 'E') to correspond to the settings in CC_STRING. */
332 do_cc (const char *cc_string, enum fmt_type type)
334 struct fmt_number_style *cc = fmt_number_style_create ();
336 /* Determine separators. */
337 if (!find_cc_separators (cc_string, cc))
339 fmt_number_style_destroy (cc);
340 msg (SE, _("%s: Custom currency string `%s' does not contain "
341 "exactly three periods or commas (or it contains both)."),
342 fmt_name (type), cc_string);
346 cc_string = extract_cc_token (cc_string, cc->grouping, &cc->neg_prefix);
347 cc_string = extract_cc_token (cc_string, cc->grouping, &cc->prefix);
348 cc_string = extract_cc_token (cc_string, cc->grouping, &cc->suffix);
349 cc_string = extract_cc_token (cc_string, cc->grouping, &cc->neg_suffix);
351 fmt_set_style (type, cc);
356 /* Parses the BLANKS subcommand, which controls the value that
357 completely blank fields in numeric data imply. X, Wnd: Syntax is
358 SYSMIS or a numeric value. */
360 stc_custom_blanks (struct lexer *lexer,
361 struct dataset *ds UNUSED,
362 struct cmd_set *cmd UNUSED, void *aux UNUSED)
364 lex_match (lexer, '=');
365 if ((lex_token (lexer) == T_ID && lex_id_match ("SYSMIS", lex_tokid (lexer))))
372 if (!lex_force_num (lexer))
374 set_blanks (lex_number (lexer));
380 /* Parses the EPOCH subcommand, which controls the epoch used for
381 parsing 2-digit years. */
383 stc_custom_epoch (struct lexer *lexer,
384 struct dataset *ds UNUSED,
385 struct cmd_set *cmd UNUSED, void *aux UNUSED)
387 lex_match (lexer, '=');
388 if (lex_match_id (lexer, "AUTOMATIC"))
390 else if (lex_is_integer (lexer))
392 int new_epoch = lex_integer (lexer);
394 if (new_epoch < 1500)
396 msg (SE, _("EPOCH must be 1500 or later."));
399 set_epoch (new_epoch);
403 lex_error (lexer, _("expecting AUTOMATIC or year"));
411 stc_custom_length (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
415 lex_match (lexer, '=');
416 if (lex_match_id (lexer, "NONE"))
420 if (!lex_force_int (lexer))
422 if (lex_integer (lexer) < 1)
424 msg (SE, _("LENGTH must be at least 1."));
427 page_length = lex_integer (lexer);
431 if (page_length != -1)
432 set_viewlength (page_length);
438 stc_custom_seed (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
440 lex_match (lexer, '=');
441 if (lex_match_id (lexer, "RANDOM"))
445 if (!lex_force_num (lexer))
447 set_rng (lex_number (lexer));
455 stc_custom_width (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
457 lex_match (lexer, '=');
458 if (lex_match_id (lexer, "NARROW"))
460 else if (lex_match_id (lexer, "WIDE"))
464 if (!lex_force_int (lexer))
466 if (lex_integer (lexer) < 40)
468 msg (SE, _("WIDTH must be at least 40."));
471 set_viewwidth (lex_integer (lexer));
478 /* Parses FORMAT subcommand, which consists of a numeric format
481 stc_custom_format (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
485 lex_match (lexer, '=');
486 if (!parse_format_specifier (lexer, &fmt))
488 if (fmt_is_string (fmt.type))
490 char str[FMT_STRING_LEN_MAX + 1];
491 msg (SE, _("FORMAT requires numeric output format as an argument. "
492 "Specified format %s is of type string."),
493 fmt_to_string (&fmt, str));
502 stc_custom_journal (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
504 lex_match (lexer, '=');
505 if (!lex_match_id (lexer, "ON") && !lex_match_id (lexer, "OFF"))
507 if (lex_token (lexer) == T_STRING)
511 lex_error (lexer, NULL);
519 stc_custom_listing (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
523 lex_match (lexer, '=');
524 if (lex_match_id (lexer, "ON") || lex_match_id (lexer, "YES"))
526 else if (lex_match_id (lexer, "OFF") || lex_match_id (lexer, "NO"))
533 outp_enable_device (listing, OUTP_DEV_LISTING);
539 stc_custom_disk (struct lexer *lexer, struct dataset *ds, struct cmd_set *cmd UNUSED, void *aux)
541 return stc_custom_listing (lexer, ds, cmd, aux);
545 show_blanks (const struct dataset *ds UNUSED)
547 if (get_blanks () == SYSMIS)
548 msg (SN, _("BLANKS is SYSMIS."));
550 msg (SN, _("BLANKS is %g."), get_blanks ());
555 format_cc (struct substring in, char grouping, char *out)
557 while (!ss_is_empty (in))
559 char c = ss_get_char (&in);
560 if (c == grouping || c == '\'')
570 show_cc (enum fmt_type type)
572 const struct fmt_number_style *cc = fmt_get_style (type);
573 char cc_string[FMT_STYLE_AFFIX_MAX * 4 * 2 + 3 + 1];
576 out = format_cc (cc->neg_prefix, cc->grouping, cc_string);
577 *out++ = cc->grouping;
578 out = format_cc (cc->prefix, cc->grouping, out);
579 *out++ = cc->grouping;
580 out = format_cc (cc->suffix, cc->grouping, out);
581 *out++ = cc->grouping;
582 out = format_cc (cc->neg_suffix, cc->grouping, out);
585 msg (SN, _("%s is \"%s\"."), fmt_name (type), cc_string);
589 show_cca (const struct dataset *ds UNUSED)
595 show_ccb (const struct dataset *ds UNUSED)
601 show_ccc (const struct dataset *ds UNUSED)
607 show_ccd (const struct dataset *ds UNUSED)
613 show_cce (const struct dataset *ds UNUSED)
619 show_decimals (const struct dataset *ds UNUSED)
621 msg (SN, _("DECIMAL is \"%c\"."), fmt_decimal_char (FMT_F));
625 show_endcmd (const struct dataset *ds UNUSED)
627 msg (SN, _("ENDCMD is \"%c\"."), get_endcmd ());
631 show_errors (const struct dataset *ds UNUSED)
633 bool terminal = get_error_routing_to_terminal ();
634 bool listing = get_error_routing_to_listing ();
635 msg (SN, _("ERRORS is \"%s\"."),
636 terminal && listing ? "BOTH"
637 : terminal ? "TERMINAL"
638 : listing ? "LISTING"
643 show_format (const struct dataset *ds UNUSED)
645 char str[FMT_STRING_LEN_MAX + 1];
646 msg (SN, _("FORMAT is %s."), fmt_to_string (get_format (), str));
650 show_length (const struct dataset *ds UNUSED)
652 msg (SN, _("LENGTH is %d."), get_viewlength ());
656 show_mxerrs (const struct dataset *ds UNUSED)
658 msg (SN, _("MXERRS is %d."), get_mxerrs ());
662 show_mxloops (const struct dataset *ds UNUSED)
664 msg (SN, _("MXLOOPS is %d."), get_mxloops ());
668 show_mxwarns (const struct dataset *ds UNUSED)
670 msg (SN, _("MXWARNS is %d."), get_mxwarns ());
673 /* Outputs that SETTING has the given INTEGER_FORMAT value. */
675 show_integer_format (const char *setting, enum integer_format integer_format)
677 msg (SN, _("%s is %s (%s)."),
679 (integer_format == INTEGER_MSB_FIRST ? "MSBFIRST"
680 : integer_format == INTEGER_LSB_FIRST ? "LSBFIRST"
682 integer_format == INTEGER_NATIVE ? "NATIVE" : "nonnative");
685 /* Outputs that SETTING has the given FLOAT_FORMAT value. */
687 show_float_format (const char *setting, enum float_format float_format)
689 const char *format_name = "";
691 switch (float_format)
693 case FLOAT_IEEE_SINGLE_LE:
694 format_name = "ISL (32-bit IEEE 754 single, little-endian)";
696 case FLOAT_IEEE_SINGLE_BE:
697 format_name = "ISB (32-bit IEEE 754 single, big-endian)";
699 case FLOAT_IEEE_DOUBLE_LE:
700 format_name = "IDL (64-bit IEEE 754 double, little-endian)";
702 case FLOAT_IEEE_DOUBLE_BE:
703 format_name = "IDB (64-bit IEEE 754 double, big-endian)";
707 format_name = "VF (32-bit VAX F, VAX-endian)";
710 format_name = "VD (64-bit VAX D, VAX-endian)";
713 format_name = "VG (64-bit VAX G, VAX-endian)";
717 format_name = "ZS (32-bit IBM Z hexadecimal short, big-endian)";
720 format_name = "ZL (64-bit IBM Z hexadecimal long, big-endian)";
728 msg (SN, _("%s is %s (%s)."),
729 setting, format_name,
730 float_format == FLOAT_NATIVE_DOUBLE ? "NATIVE" : "nonnative");
734 show_scompression (const struct dataset *ds UNUSED)
736 if (get_scompression ())
737 msg (SN, _("SCOMPRESSION is ON."));
739 msg (SN, _("SCOMPRESSION is OFF."));
743 show_undefined (const struct dataset *ds UNUSED)
745 if (get_undefined ())
746 msg (SN, _("UNDEFINED is WARN."));
748 msg (SN, _("UNDEFINED is NOWARN."));
752 show_weight (const struct dataset *ds)
754 struct variable *var = dict_get_weight (dataset_dict (ds));
756 msg (SN, _("WEIGHT is off."));
758 msg (SN, _("WEIGHT is variable %s."), var->name);
762 show_wib (const struct dataset *ds UNUSED)
764 show_integer_format ("WIB", data_out_get_integer_format ());
768 show_wrb (const struct dataset *ds UNUSED)
770 show_float_format ("WRB", data_out_get_float_format ());
774 show_width (const struct dataset *ds UNUSED)
776 msg (SN, _("WIDTH is %d."), get_viewwidth ());
782 void (*function) (const struct dataset *);
785 const struct show_sbc show_table[] =
787 {"BLANKS", show_blanks},
793 {"DECIMALS", show_decimals},
794 {"ENDCMD", show_endcmd},
795 {"ERRORS", show_errors},
796 {"FORMAT", show_format},
797 {"LENGTH", show_length},
798 {"MXERRS", show_mxerrs},
799 {"MXLOOPS", show_mxloops},
800 {"MXWARNS", show_mxwarns},
801 {"SCOMPRESSION", show_scompression},
802 {"UNDEFINED", show_undefined},
803 {"WEIGHT", show_weight},
806 {"WIDTH", show_width},
810 show_all (const struct dataset *ds)
814 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
815 show_table[i].function (ds);
823 for (i = 0; i < 5; i++)
828 show_warranty (const struct dataset *ds UNUSED)
830 msg (MN, lack_of_warranty);
834 show_copying (const struct dataset *ds UNUSED)
840 cmd_show (struct lexer *lexer, struct dataset *ds)
842 if (lex_token (lexer) == '.')
850 if (lex_match (lexer, T_ALL))
852 else if (lex_match_id (lexer, "CC"))
854 else if (lex_match_id (lexer, "WARRANTY"))
856 else if (lex_match_id (lexer, "COPYING"))
858 else if (lex_token (lexer) == T_ID)
862 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
863 if (lex_match_id (lexer, show_table[i].name))
865 show_table[i].function (ds);
868 lex_error (lexer, NULL);
875 lex_error (lexer, NULL);
879 lex_match (lexer, '/');
881 while (lex_token (lexer) != '.');