1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006 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/alloc.h>
37 #include <libpspp/compiler.h>
38 #include <libpspp/copyleft.h>
39 #include <libpspp/float-format.h>
40 #include <libpspp/integer-format.h>
41 #include <libpspp/magic.h>
42 #include <libpspp/message.h>
43 #include <math/random.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 */
56 #define _(msgid) gettext (msgid)
61 block=string "x==1" "one character long";
62 boxstring=string "x==3 || x==11" "3 or 11 characters long";
63 case=size:upper/uplow;
69 compression=compress:on/off;
70 cpi=integer "x>0" "%s must be greater than 0";
72 decimal=dec:dot/comma;
76 endcmd=string "x==1" "one character long";
78 errorbreak=errbrk:on/off;
79 errors=errors:terminal/listing/both/on/none/off;
81 headers=headers:no/yes/blank;
83 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;
91 messages=messages:on/off/terminal/listing/both/on/none/off;
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";
101 printback=prtbck:on/off;
103 results=res:on/off/terminal/listing/both/on/none/off;
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>=1024" "%s must be at least 1 MB";
126 static bool do_cc (const char *cc_string, enum fmt_type);
127 static enum integer_format stc_to_integer_format (int stc);
128 static enum float_format stc_to_float_format (int stc);
131 cmd_set (struct lexer *lexer, struct dataset *ds)
135 if (!parse_set (lexer, ds, &cmd, NULL))
142 do_cc (cmd.s_cca, FMT_CCA);
144 do_cc (cmd.s_ccb, FMT_CCB);
146 do_cc (cmd.s_ccc, FMT_CCC);
148 do_cc (cmd.s_ccd, FMT_CCD);
150 do_cc (cmd.s_cce, FMT_CCE);
153 prompt_set (PROMPT_FIRST, cmd.s_prompt);
155 prompt_set (PROMPT_LATER, cmd.s_cprompt);
157 prompt_set (PROMPT_DATA, cmd.s_dprompt);
160 fmt_set_decimal (cmd.dec == STC_DOT ? '.' : ',');
162 set_echo (cmd.echo == STC_ON);
164 set_endcmd (cmd.s_endcmd[0]);
165 if (cmd.sbc_errorbreak)
166 set_errorbreak (cmd.errbrk == STC_ON);
169 bool both = cmd.errors == STC_BOTH || cmd.errors == STC_ON;
170 set_error_routing_to_terminal (cmd.errors == STC_TERMINAL || both);
171 set_error_routing_to_listing (cmd.errors == STC_LISTING || both);
174 set_include (cmd.inc == STC_ON);
176 set_mxerrs (cmd.n_mxerrs[0]);
178 set_mxwarns (cmd.n_mxwarns[0]);
180 set_nulline (cmd.null == STC_ON);
182 data_in_set_integer_format (stc_to_integer_format (cmd.rib));
184 data_in_set_float_format (stc_to_float_format (cmd.rrb));
187 if (cmd.sbc_scompression)
188 set_scompression (cmd.scompress == STC_ON);
189 if (cmd.sbc_undefined)
190 set_undefined (cmd.undef == STC_WARN);
192 data_out_set_integer_format (stc_to_integer_format (cmd.wib));
194 data_out_set_float_format (stc_to_float_format (cmd.wrb));
195 if (cmd.sbc_workspace)
196 set_workspace (cmd.n_workspace[0] * 1024L);
199 msg (SW, _("%s is obsolete."), "BLOCK");
200 if (cmd.sbc_boxstring)
201 msg (SW, _("%s is obsolete."), "BOXSTRING");
202 if (cmd.sbc_histogram)
203 msg (SW, _("%s is obsolete."), "HISTOGRAM");
205 msg (SW, _("%s is obsolete."), "MENUS");
207 msg (SW, _("%s is obsolete."), "XSORT");
208 if (cmd.sbc_mxmemory)
209 msg (SE, _("%s is obsolete."), "MXMEMORY");
210 if (cmd.sbc_scripttab)
211 msg (SE, _("%s is obsolete."), "SCRIPTTAB");
213 msg (SW, _("%s is obsolete."), "TBFONTS");
214 if (cmd.sbc_tb1 && cmd.s_tb1)
215 msg (SW, _("%s is obsolete."), "TB1");
218 msg (SW, _("%s is not implemented."), "CASE");
220 if (cmd.sbc_compression)
221 msg (SW, _("Active file compression is not implemented."));
228 /* Returns the integer_format value corresponding to STC,
229 which should be the value of cmd.rib or cmd.wib. */
230 static enum integer_format
231 stc_to_integer_format (int stc)
233 return (stc == STC_MSBFIRST ? INTEGER_MSB_FIRST
234 : stc == STC_LSBFIRST ? INTEGER_LSB_FIRST
235 : stc == STC_VAX ? INTEGER_VAX
239 /* Returns the float_format value corresponding to STC,
240 which should be the value of cmd.rrb or cmd.wrb. */
241 static enum float_format
242 stc_to_float_format (int stc)
247 return FLOAT_NATIVE_DOUBLE;
250 return FLOAT_IEEE_SINGLE_LE;
252 return FLOAT_IEEE_SINGLE_BE;
254 return FLOAT_IEEE_DOUBLE_LE;
256 return FLOAT_IEEE_DOUBLE_BE;
266 return FLOAT_Z_SHORT;
274 /* Find the grouping characters in CC_STRING and set CC's
275 grouping and decimal members appropriately. Returns true if
276 successful, false otherwise. */
278 find_cc_separators (const char *cc_string, struct fmt_number_style *cc)
281 int comma_cnt, dot_cnt;
283 /* Count commas and periods. There must be exactly three of
284 one or the other, except that an apostrophe escapes a
285 following comma or period. */
286 comma_cnt = dot_cnt = 0;
287 for (sp = cc_string; *sp; sp++)
292 else if (*sp == '\'' && (sp[1] == '.' || sp[1] == ',' || sp[1] == '\''))
295 if ((comma_cnt == 3) == (dot_cnt == 3))
311 /* Extracts a token from IN into a newly allocated AFFIX. Tokens
312 are delimited by GROUPING. The token is truncated to at most
313 FMT_STYLE_AFFIX_MAX characters. Returns the first character
314 following the token. */
316 extract_cc_token (const char *in, int grouping, struct substring *affix)
319 ss_alloc_uninit (affix, FMT_STYLE_AFFIX_MAX);
320 for (; *in != '\0' && *in != grouping; in++)
322 if (*in == '\'' && in[1] == grouping)
324 if (ofs < FMT_STYLE_AFFIX_MAX)
325 ss_data (*affix)[ofs++] = *in;
334 /* Sets custom currency specifier CC having name CC_NAME ('A' through
335 'E') to correspond to the settings in CC_STRING. */
337 do_cc (const char *cc_string, enum fmt_type type)
339 struct fmt_number_style *cc = fmt_number_style_create ();
341 /* Determine separators. */
342 if (!find_cc_separators (cc_string, cc))
344 fmt_number_style_destroy (cc);
345 msg (SE, _("%s: Custom currency string `%s' does not contain "
346 "exactly three periods or commas (or it contains both)."),
347 fmt_name (type), cc_string);
351 cc_string = extract_cc_token (cc_string, cc->grouping, &cc->neg_prefix);
352 cc_string = extract_cc_token (cc_string, cc->grouping, &cc->prefix);
353 cc_string = extract_cc_token (cc_string, cc->grouping, &cc->suffix);
354 cc_string = extract_cc_token (cc_string, cc->grouping, &cc->neg_suffix);
356 fmt_set_style (type, cc);
361 /* Parses the BLANKS subcommand, which controls the value that
362 completely blank fields in numeric data imply. X, Wnd: Syntax is
363 SYSMIS or a numeric value. */
365 stc_custom_blanks (struct lexer *lexer,
366 struct dataset *ds UNUSED,
367 struct cmd_set *cmd UNUSED, void *aux UNUSED)
369 lex_match (lexer, '=');
370 if (lex_match_id (lexer, "SYSMIS"))
377 if (!lex_force_num (lexer))
379 set_blanks (lex_number (lexer));
385 /* Parses the EPOCH subcommand, which controls the epoch used for
386 parsing 2-digit years. */
388 stc_custom_epoch (struct lexer *lexer,
389 struct dataset *ds UNUSED,
390 struct cmd_set *cmd UNUSED, void *aux UNUSED)
392 lex_match (lexer, '=');
393 if (lex_match_id (lexer, "AUTOMATIC"))
395 else if (lex_is_integer (lexer))
397 int new_epoch = lex_integer (lexer);
399 if (new_epoch < 1500)
401 msg (SE, _("EPOCH must be 1500 or later."));
404 set_epoch (new_epoch);
408 lex_error (lexer, _("expecting AUTOMATIC or year"));
416 stc_custom_length (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
420 lex_match (lexer, '=');
421 if (lex_match_id (lexer, "NONE"))
425 if (!lex_force_int (lexer))
427 if (lex_integer (lexer) < 1)
429 msg (SE, _("LENGTH must be at least 1."));
432 page_length = lex_integer (lexer);
436 if (page_length != -1)
437 set_viewlength (page_length);
443 stc_custom_seed (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
445 lex_match (lexer, '=');
446 if (lex_match_id (lexer, "RANDOM"))
450 if (!lex_force_num (lexer))
452 set_rng (lex_number (lexer));
460 stc_custom_width (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
462 lex_match (lexer, '=');
463 if (lex_match_id (lexer, "NARROW"))
465 else if (lex_match_id (lexer, "WIDE"))
469 if (!lex_force_int (lexer))
471 if (lex_integer (lexer) < 40)
473 msg (SE, _("WIDTH must be at least 40."));
476 set_viewwidth (lex_integer (lexer));
483 /* Parses FORMAT subcommand, which consists of a numeric format
486 stc_custom_format (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
490 lex_match (lexer, '=');
491 if (!parse_format_specifier (lexer, &fmt))
493 if (fmt_is_string (fmt.type))
495 char str[FMT_STRING_LEN_MAX + 1];
496 msg (SE, _("FORMAT requires numeric output format as an argument. "
497 "Specified format %s is of type string."),
498 fmt_to_string (&fmt, str));
507 stc_custom_journal (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
509 lex_match (lexer, '=');
510 if (!lex_match_id (lexer, "ON") && !lex_match_id (lexer, "OFF"))
512 if (lex_token (lexer) == T_STRING)
516 lex_error (lexer, NULL);
524 stc_custom_listing (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
528 lex_match (lexer, '=');
529 if (lex_match_id (lexer, "ON") || lex_match_id (lexer, "YES"))
531 else if (lex_match_id (lexer, "OFF") || lex_match_id (lexer, "NO"))
538 outp_enable_device (listing, OUTP_DEV_LISTING);
544 stc_custom_disk (struct lexer *lexer, struct dataset *ds, struct cmd_set *cmd UNUSED, void *aux)
546 return stc_custom_listing (lexer, ds, cmd, aux);
550 show_blanks (const struct dataset *ds UNUSED)
552 if (get_blanks () == SYSMIS)
553 msg (SN, _("BLANKS is SYSMIS."));
555 msg (SN, _("BLANKS is %g."), get_blanks ());
560 format_cc (struct substring in, char grouping, char *out)
562 while (!ss_is_empty (in))
564 char c = ss_get_char (&in);
565 if (c == grouping || c == '\'')
575 show_cc (enum fmt_type type)
577 const struct fmt_number_style *cc = fmt_get_style (type);
578 char cc_string[FMT_STYLE_AFFIX_MAX * 4 * 2 + 3 + 1];
581 out = format_cc (cc->neg_prefix, cc->grouping, cc_string);
582 *out++ = cc->grouping;
583 out = format_cc (cc->prefix, cc->grouping, out);
584 *out++ = cc->grouping;
585 out = format_cc (cc->suffix, cc->grouping, out);
586 *out++ = cc->grouping;
587 out = format_cc (cc->neg_suffix, cc->grouping, out);
590 msg (SN, _("%s is \"%s\"."), fmt_name (type), cc_string);
594 show_cca (const struct dataset *ds UNUSED)
600 show_ccb (const struct dataset *ds UNUSED)
606 show_ccc (const struct dataset *ds UNUSED)
612 show_ccd (const struct dataset *ds UNUSED)
618 show_cce (const struct dataset *ds UNUSED)
624 show_decimals (const struct dataset *ds UNUSED)
626 msg (SN, _("DECIMAL is \"%c\"."), fmt_decimal_char (FMT_F));
630 show_endcmd (const struct dataset *ds UNUSED)
632 msg (SN, _("ENDCMD is \"%c\"."), get_endcmd ());
636 show_errors (const struct dataset *ds UNUSED)
638 bool terminal = get_error_routing_to_terminal ();
639 bool listing = get_error_routing_to_listing ();
640 msg (SN, _("ERRORS is \"%s\"."),
641 terminal && listing ? "BOTH"
642 : terminal ? "TERMINAL"
643 : listing ? "LISTING"
648 show_format (const struct dataset *ds UNUSED)
650 char str[FMT_STRING_LEN_MAX + 1];
651 msg (SN, _("FORMAT is %s."), fmt_to_string (get_format (), str));
655 show_length (const struct dataset *ds UNUSED)
657 msg (SN, _("LENGTH is %d."), get_viewlength ());
661 show_mxerrs (const struct dataset *ds UNUSED)
663 msg (SN, _("MXERRS is %d."), get_mxerrs ());
667 show_mxloops (const struct dataset *ds UNUSED)
669 msg (SN, _("MXLOOPS is %d."), get_mxloops ());
673 show_mxwarns (const struct dataset *ds UNUSED)
675 msg (SN, _("MXWARNS is %d."), get_mxwarns ());
678 /* Outputs that SETTING has the given INTEGER_FORMAT value. */
680 show_integer_format (const char *setting, enum integer_format integer_format)
682 msg (SN, _("%s is %s (%s)."),
684 (integer_format == INTEGER_MSB_FIRST ? "MSBFIRST"
685 : integer_format == INTEGER_LSB_FIRST ? "LSBFIRST"
687 integer_format == INTEGER_NATIVE ? "NATIVE" : "nonnative");
690 /* Outputs that SETTING has the given FLOAT_FORMAT value. */
692 show_float_format (const char *setting, enum float_format float_format)
694 const char *format_name = "";
696 switch (float_format)
698 case FLOAT_IEEE_SINGLE_LE:
699 format_name = "ISL (32-bit IEEE 754 single, little-endian)";
701 case FLOAT_IEEE_SINGLE_BE:
702 format_name = "ISB (32-bit IEEE 754 single, big-endian)";
704 case FLOAT_IEEE_DOUBLE_LE:
705 format_name = "IDL (64-bit IEEE 754 double, little-endian)";
707 case FLOAT_IEEE_DOUBLE_BE:
708 format_name = "IDB (64-bit IEEE 754 double, big-endian)";
712 format_name = "VF (32-bit VAX F, VAX-endian)";
715 format_name = "VD (64-bit VAX D, VAX-endian)";
718 format_name = "VG (64-bit VAX G, VAX-endian)";
722 format_name = "ZS (32-bit IBM Z hexadecimal short, big-endian)";
725 format_name = "ZL (64-bit IBM Z hexadecimal long, big-endian)";
733 msg (SN, _("%s is %s (%s)."),
734 setting, format_name,
735 float_format == FLOAT_NATIVE_DOUBLE ? "NATIVE" : "nonnative");
739 show_rib (const struct dataset *ds UNUSED)
741 show_integer_format ("RIB", data_in_get_integer_format ());
745 show_rrb (const struct dataset *ds UNUSED)
747 show_float_format ("RRB", data_in_get_float_format ());
751 show_scompression (const struct dataset *ds UNUSED)
753 if (get_scompression ())
754 msg (SN, _("SCOMPRESSION is ON."));
756 msg (SN, _("SCOMPRESSION is OFF."));
760 show_undefined (const struct dataset *ds UNUSED)
762 if (get_undefined ())
763 msg (SN, _("UNDEFINED is WARN."));
765 msg (SN, _("UNDEFINED is NOWARN."));
769 show_weight (const struct dataset *ds)
771 struct variable *var = dict_get_weight (dataset_dict (ds));
773 msg (SN, _("WEIGHT is off."));
775 msg (SN, _("WEIGHT is variable %s."), var_get_name (var));
779 show_wib (const struct dataset *ds UNUSED)
781 show_integer_format ("WIB", data_out_get_integer_format ());
785 show_wrb (const struct dataset *ds UNUSED)
787 show_float_format ("WRB", data_out_get_float_format ());
791 show_width (const struct dataset *ds UNUSED)
793 msg (SN, _("WIDTH is %d."), get_viewwidth ());
799 void (*function) (const struct dataset *);
802 const struct show_sbc show_table[] =
804 {"BLANKS", show_blanks},
810 {"DECIMALS", show_decimals},
811 {"ENDCMD", show_endcmd},
812 {"ERRORS", show_errors},
813 {"FORMAT", show_format},
814 {"LENGTH", show_length},
815 {"MXERRS", show_mxerrs},
816 {"MXLOOPS", show_mxloops},
817 {"MXWARNS", show_mxwarns},
820 {"SCOMPRESSION", show_scompression},
821 {"UNDEFINED", show_undefined},
822 {"WEIGHT", show_weight},
825 {"WIDTH", show_width},
829 show_all (const struct dataset *ds)
833 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
834 show_table[i].function (ds);
842 for (i = 0; i < 5; i++)
847 show_warranty (const struct dataset *ds UNUSED)
849 msg (MN, lack_of_warranty);
853 show_copying (const struct dataset *ds UNUSED)
859 cmd_show (struct lexer *lexer, struct dataset *ds)
861 if (lex_token (lexer) == '.')
869 if (lex_match (lexer, T_ALL))
871 else if (lex_match_id (lexer, "CC"))
873 else if (lex_match_id (lexer, "WARRANTY"))
875 else if (lex_match_id (lexer, "COPYING"))
877 else if (lex_token (lexer) == T_ID)
881 for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
882 if (lex_match_id (lexer, show_table[i].name))
884 show_table[i].function (ds);
887 lex_error (lexer, NULL);
894 lex_error (lexer, NULL);
898 lex_match (lexer, '/');
900 while (lex_token (lexer) != '.');