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 dataset *ds)
134 if (!parse_set (ds, &cmd, NULL))
138 do_cc (cmd.s_cca, FMT_CCA);
140 do_cc (cmd.s_ccb, FMT_CCB);
142 do_cc (cmd.s_ccc, FMT_CCC);
144 do_cc (cmd.s_ccd, FMT_CCD);
146 do_cc (cmd.s_cce, FMT_CCE);
149 getl_set_prompt (GETL_PROMPT_FIRST, cmd.s_prompt);
151 getl_set_prompt (GETL_PROMPT_LATER, cmd.s_cprompt);
153 getl_set_prompt (GETL_PROMPT_DATA, cmd.s_dprompt);
156 fmt_set_decimal (cmd.dec == STC_DOT ? '.' : ',');
158 set_echo (cmd.echo == STC_ON);
160 set_endcmd (cmd.s_endcmd[0]);
161 if (cmd.sbc_errorbreak)
162 set_errorbreak (cmd.errbrk == STC_ON);
165 bool both = cmd.errors == STC_BOTH || cmd.errors == STC_ON;
166 set_error_routing_to_terminal (cmd.errors == STC_TERMINAL || both);
167 set_error_routing_to_listing (cmd.errors == STC_LISTING || both);
170 set_include (cmd.inc == STC_ON);
172 set_mxerrs (cmd.n_mxerrs[0]);
174 set_mxwarns (cmd.n_mxwarns[0]);
176 set_nulline (cmd.null == STC_ON);
179 if (cmd.sbc_scompression)
180 set_scompression (cmd.scompress == STC_ON);
181 if (cmd.sbc_undefined)
182 set_undefined (cmd.undef == STC_WARN);
184 data_out_set_integer_format (stc_to_integer_format (cmd.wib));
186 data_out_set_float_format (stc_to_float_format (cmd.wrb));
187 if (cmd.sbc_workspace)
188 set_workspace (cmd.n_workspace[0] * 1024L);
191 msg (SW, _("%s is obsolete."), "BLOCK");
192 if (cmd.sbc_boxstring)
193 msg (SW, _("%s is obsolete."), "BOXSTRING");
194 if (cmd.sbc_histogram)
195 msg (SW, _("%s is obsolete."), "HISTOGRAM");
197 msg (SW, _("%s is obsolete."), "MENUS");
199 msg (SW, _("%s is obsolete."), "XSORT");
200 if (cmd.sbc_mxmemory)
201 msg (SE, _("%s is obsolete."), "MXMEMORY");
202 if (cmd.sbc_scripttab)
203 msg (SE, _("%s is obsolete."), "SCRIPTTAB");
205 msg (SW, _("%s is obsolete."), "TBFONTS");
206 if (cmd.sbc_tb1 && cmd.s_tb1)
207 msg (SW, _("%s is obsolete."), "TB1");
210 msg (SW, _("%s is not implemented."), "CASE");
212 if (cmd.sbc_compression)
213 msg (SW, _("Active file compression is not implemented."));
218 /* Returns the integer_format value corresponding to STC,
219 which should be the value of cmd.rib or cmd.wib. */
220 static enum integer_format
221 stc_to_integer_format (int stc)
223 return (stc == STC_MSBFIRST ? INTEGER_MSB_FIRST
224 : stc == STC_LSBFIRST ? INTEGER_LSB_FIRST
225 : stc == STC_VAX ? INTEGER_VAX
229 /* Returns the float_format value corresponding to STC,
230 which should be the value of cmd.rrb or cmd.wrb. */
231 static enum float_format
232 stc_to_float_format (int stc)
237 return FLOAT_NATIVE_DOUBLE;
240 return FLOAT_IEEE_SINGLE_LE;
242 return FLOAT_IEEE_SINGLE_BE;
244 return FLOAT_IEEE_DOUBLE_LE;
246 return FLOAT_IEEE_DOUBLE_BE;
256 return FLOAT_Z_SHORT;
264 /* Find the grouping characters in CC_STRING and set CC's
265 grouping and decimal members appropriately. Returns true if
266 successful, false otherwise. */
268 find_cc_separators (const char *cc_string, struct fmt_number_style *cc)
271 int comma_cnt, dot_cnt;
273 /* Count commas and periods. There must be exactly three of
274 one or the other, except that an apostrophe escapes a
275 following comma or period. */
276 comma_cnt = dot_cnt = 0;
277 for (sp = cc_string; *sp; sp++)
282 else if (*sp == '\'' && (sp[1] == '.' || sp[1] == ',' || sp[1] == '\''))
285 if ((comma_cnt == 3) == (dot_cnt == 3))
301 /* Extracts a token from IN into a newly allocated AFFIX. Tokens
302 are delimited by GROUPING. The token is truncated to at most
303 FMT_STYLE_AFFIX_MAX characters. Returns the first character
304 following the token. */
306 extract_cc_token (const char *in, int grouping, struct substring *affix)
309 ss_alloc_uninit (affix, FMT_STYLE_AFFIX_MAX);
310 for (; *in != '\0' && *in != grouping; in++)
312 if (*in == '\'' && in[1] == grouping)
314 if (ofs < FMT_STYLE_AFFIX_MAX)
315 ss_data (*affix)[ofs++] = *in;
324 /* Sets custom currency specifier CC having name CC_NAME ('A' through
325 'E') to correspond to the settings in CC_STRING. */
327 do_cc (const char *cc_string, enum fmt_type type)
329 struct fmt_number_style *cc = fmt_number_style_create ();
331 /* Determine separators. */
332 if (!find_cc_separators (cc_string, cc))
334 fmt_number_style_destroy (cc);
335 msg (SE, _("%s: Custom currency string `%s' does not contain "
336 "exactly three periods or commas (or it contains both)."),
337 fmt_name (type), cc_string);
341 cc_string = extract_cc_token (cc_string, cc->grouping, &cc->neg_prefix);
342 cc_string = extract_cc_token (cc_string, cc->grouping, &cc->prefix);
343 cc_string = extract_cc_token (cc_string, cc->grouping, &cc->suffix);
344 cc_string = extract_cc_token (cc_string, cc->grouping, &cc->neg_suffix);
346 fmt_set_style (type, cc);
351 /* Parses the BLANKS subcommand, which controls the value that
352 completely blank fields in numeric data imply. X, Wnd: Syntax is
353 SYSMIS or a numeric value. */
355 stc_custom_blanks (struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
358 if ((token == T_ID && lex_id_match ("SYSMIS", tokid)))
365 if (!lex_force_num ())
367 set_blanks (lex_number ());
373 /* Parses the EPOCH subcommand, which controls the epoch used for
374 parsing 2-digit years. */
376 stc_custom_epoch (struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
379 if (lex_match_id ("AUTOMATIC"))
381 else if (lex_is_integer ())
383 int new_epoch = lex_integer ();
385 if (new_epoch < 1500)
387 msg (SE, _("EPOCH must be 1500 or later."));
390 set_epoch (new_epoch);
394 lex_error (_("expecting AUTOMATIC or year"));
402 stc_custom_length (struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
407 if (lex_match_id ("NONE"))
411 if (!lex_force_int ())
413 if (lex_integer () < 1)
415 msg (SE, _("LENGTH must be at least 1."));
418 page_length = lex_integer ();
422 if (page_length != -1)
423 set_viewlength (page_length);
429 stc_custom_seed (struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
432 if (lex_match_id ("RANDOM"))
436 if (!lex_force_num ())
438 set_rng (lex_number ());
446 stc_custom_width (struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
449 if (lex_match_id ("NARROW"))
451 else if (lex_match_id ("WIDE"))
455 if (!lex_force_int ())
457 if (lex_integer () < 40)
459 msg (SE, _("WIDTH must be at least 40."));
462 set_viewwidth (lex_integer ());
469 /* Parses FORMAT subcommand, which consists of a numeric format
472 stc_custom_format (struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
477 if (!parse_format_specifier (&fmt))
479 if (fmt_is_string (fmt.type))
481 char str[FMT_STRING_LEN_MAX + 1];
482 msg (SE, _("FORMAT requires numeric output format as an argument. "
483 "Specified format %s is of type string."),
484 fmt_to_string (&fmt, str));
493 stc_custom_journal (struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
496 if (!lex_match_id ("ON") && !lex_match_id ("OFF"))
498 if (token == T_STRING)
510 stc_custom_listing (struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
515 if (lex_match_id ("ON") || lex_match_id ("YES"))
517 else if (lex_match_id ("OFF") || lex_match_id ("NO"))
524 outp_enable_device (listing, OUTP_DEV_LISTING);
530 stc_custom_disk (struct dataset *ds, struct cmd_set *cmd UNUSED, void *aux)
532 return stc_custom_listing (ds, cmd, aux);
536 show_blanks (const struct dataset *ds UNUSED)
538 if (get_blanks () == SYSMIS)
539 msg (SN, _("BLANKS is SYSMIS."));
541 msg (SN, _("BLANKS is %g."), get_blanks ());
546 format_cc (struct substring in, char grouping, char *out)
548 while (!ss_is_empty (in))
550 char c = ss_get_char (&in);
551 if (c == grouping || c == '\'')
561 show_cc (enum fmt_type type)
563 const struct fmt_number_style *cc = fmt_get_style (type);
564 char cc_string[FMT_STYLE_AFFIX_MAX * 4 * 2 + 3 + 1];
567 out = format_cc (cc->neg_prefix, cc->grouping, cc_string);
568 *out++ = cc->grouping;
569 out = format_cc (cc->prefix, cc->grouping, out);
570 *out++ = cc->grouping;
571 out = format_cc (cc->suffix, cc->grouping, out);
572 *out++ = cc->grouping;
573 out = format_cc (cc->neg_suffix, cc->grouping, out);
576 msg (SN, _("%s is \"%s\"."), fmt_name (type), cc_string);
580 show_cca (const struct dataset *ds UNUSED)
586 show_ccb (const struct dataset *ds UNUSED)
592 show_ccc (const struct dataset *ds UNUSED)
598 show_ccd (const struct dataset *ds UNUSED)
604 show_cce (const struct dataset *ds UNUSED)
610 show_decimals (const struct dataset *ds UNUSED)
612 msg (SN, _("DECIMAL is \"%c\"."), fmt_decimal_char (FMT_F));
616 show_endcmd (const struct dataset *ds UNUSED)
618 msg (SN, _("ENDCMD is \"%c\"."), get_endcmd ());
622 show_errors (const struct dataset *ds UNUSED)
624 bool terminal = get_error_routing_to_terminal ();
625 bool listing = get_error_routing_to_listing ();
626 msg (SN, _("ERRORS is \"%s\"."),
627 terminal && listing ? "BOTH"
628 : terminal ? "TERMINAL"
629 : listing ? "LISTING"
634 show_format (const struct dataset *ds UNUSED)
636 char str[FMT_STRING_LEN_MAX + 1];
637 msg (SN, _("FORMAT is %s."), fmt_to_string (get_format (), str));
641 show_length (const struct dataset *ds UNUSED)
643 msg (SN, _("LENGTH is %d."), get_viewlength ());
647 show_mxerrs (const struct dataset *ds UNUSED)
649 msg (SN, _("MXERRS is %d."), get_mxerrs ());
653 show_mxloops (const struct dataset *ds UNUSED)
655 msg (SN, _("MXLOOPS is %d."), get_mxloops ());
659 show_mxwarns (const struct dataset *ds UNUSED)
661 msg (SN, _("MXWARNS is %d."), get_mxwarns ());
664 /* Outputs that SETTING has the given INTEGER_FORMAT value. */
666 show_integer_format (const char *setting, enum integer_format integer_format)
668 msg (SN, _("%s is %s (%s)."),
670 (integer_format == INTEGER_MSB_FIRST ? "MSBFIRST"
671 : integer_format == INTEGER_LSB_FIRST ? "LSBFIRST"
673 integer_format == INTEGER_NATIVE ? "NATIVE" : "nonnative");
676 /* Outputs that SETTING has the given FLOAT_FORMAT value. */
678 show_float_format (const char *setting, enum float_format float_format)
680 const char *format_name = "";
682 switch (float_format)
684 case FLOAT_IEEE_SINGLE_LE:
685 format_name = "ISL (32-bit IEEE 754 single, little-endian)";
687 case FLOAT_IEEE_SINGLE_BE:
688 format_name = "ISB (32-bit IEEE 754 single, big-endian)";
690 case FLOAT_IEEE_DOUBLE_LE:
691 format_name = "IDL (64-bit IEEE 754 double, little-endian)";
693 case FLOAT_IEEE_DOUBLE_BE:
694 format_name = "IDB (64-bit IEEE 754 double, big-endian)";
698 format_name = "VF (32-bit VAX F, VAX-endian)";
701 format_name = "VD (64-bit VAX D, VAX-endian)";
704 format_name = "VG (64-bit VAX G, VAX-endian)";
708 format_name = "ZS (32-bit IBM Z hexadecimal short, big-endian)";
711 format_name = "ZL (64-bit IBM Z hexadecimal long, big-endian)";
719 msg (SN, _("%s is %s (%s)."),
720 setting, format_name,
721 float_format == FLOAT_NATIVE_DOUBLE ? "NATIVE" : "nonnative");
725 show_scompression (const struct dataset *ds UNUSED)
727 if (get_scompression ())
728 msg (SN, _("SCOMPRESSION is ON."));
730 msg (SN, _("SCOMPRESSION is OFF."));
734 show_undefined (const struct dataset *ds UNUSED)
736 if (get_undefined ())
737 msg (SN, _("UNDEFINED is WARN."));
739 msg (SN, _("UNDEFINED is NOWARN."));
743 show_weight (const struct dataset *ds)
745 struct variable *var = dict_get_weight (dataset_dict (ds));
747 msg (SN, _("WEIGHT is off."));
749 msg (SN, _("WEIGHT is variable %s."), var->name);
753 show_wib (const struct dataset *ds UNUSED)
755 show_integer_format ("WIB", data_out_get_integer_format ());
759 show_wrb (const struct dataset *ds UNUSED)
761 show_float_format ("WRB", data_out_get_float_format ());
765 show_width (const struct dataset *ds UNUSED)
767 msg (SN, _("WIDTH is %d."), get_viewwidth ());
773 void (*function) (const struct dataset *);
776 const struct show_sbc show_table[] =
778 {"BLANKS", show_blanks},
784 {"DECIMALS", show_decimals},
785 {"ENDCMD", show_endcmd},
786 {"ERRORS", show_errors},
787 {"FORMAT", show_format},
788 {"LENGTH", show_length},
789 {"MXERRS", show_mxerrs},
790 {"MXLOOPS", show_mxloops},
791 {"MXWARNS", show_mxwarns},
792 {"SCOMPRESSION", show_scompression},
793 {"UNDEFINED", show_undefined},
794 {"WEIGHT", show_weight},
797 {"WIDTH", show_width},
801 show_all (const struct dataset *ds)
805 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
806 show_table[i].function (ds);
814 for (i = 0; i < 5; i++)
819 show_warranty (const struct dataset *ds UNUSED)
821 msg (MN, lack_of_warranty);
825 show_copying (const struct dataset *ds UNUSED)
831 cmd_show (struct dataset *ds)
841 if (lex_match (T_ALL))
843 else if (lex_match_id ("CC"))
845 else if (lex_match_id ("WARRANTY"))
847 else if (lex_match_id ("COPYING"))
849 else if (token == T_ID)
853 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
854 if (lex_match_id (show_table[i].name))
856 show_table[i].function (ds);
872 while (token != '.');