1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2007, 2009, 2010, 2011, 2015 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/>. */
19 #include "data/settings.h"
25 #include "data/case.h"
26 #include "data/format.h"
27 #include "data/value.h"
28 #include "libpspp/i18n.h"
29 #include "libpspp/integer-format.h"
30 #include "libpspp/message.h"
32 #include "gl/minmax.h"
33 #include "gl/xalloc.h"
36 #define _(msgid) gettext (msgid)
40 /* Integer format used for IB and PIB input. */
41 enum integer_format input_integer_format;
43 /* Floating-point format used for RB and RBHEX input. */
44 enum float_format input_float_format;
46 /* Format of integers in output (SET WIB). */
47 enum integer_format output_integer_format;
49 /* Format of reals in output (SET WRB). */
50 enum float_format output_float_format;
57 bool route_errors_to_terminal;
58 bool route_errors_to_listing;
62 int max_messages[MSG_N_SEVERITIES];
67 struct fmt_spec default_format;
75 struct fmt_settings styles;
77 enum settings_output_devices output_routing[SETTINGS_N_OUTPUT_TYPES];
79 enum settings_value_show show_values;
80 enum settings_value_show show_variables;
83 static struct settings the_settings = {
84 INTEGER_NATIVE, /* input_integer_format */
85 FLOAT_NATIVE_DOUBLE, /* input_float_format */
86 INTEGER_NATIVE, /* output_integer_format */
87 FLOAT_NATIVE_DOUBLE, /* output_float_format */
90 false, /* safer_mode */
93 true, /* route_errors_to_terminal */
94 true, /* route_errors_to_listing */
101 100, /* MSG_S_ERROR */
102 100, /* MSG_S_WARNING */
106 true, /* printback */
109 64L * 1024 * 1024, /* workspace */
110 {FMT_F, 8, 2}, /* default_format */
111 false, /* testing_mode */
113 ENHANCED, /* cmd_algorithm */
114 ENHANCED, /* global_algorithm */
115 ENHANCED, /* syntax */
116 FMT_SETTINGS_INIT, /* styles */
119 {SETTINGS_DEVICE_LISTING | SETTINGS_DEVICE_TERMINAL,
120 SETTINGS_DEVICE_LISTING | SETTINGS_DEVICE_TERMINAL,
122 SETTINGS_DEVICE_LISTING | SETTINGS_DEVICE_TERMINAL},
124 SETTINGS_VALUE_SHOW_LABEL,
125 SETTINGS_VALUE_SHOW_LABEL
128 /* Initializes the settings module. */
132 settings_set_epoch (-1);
133 settings_set_decimal_char (get_system_decimal ());
136 /* Cleans up the settings module. */
140 settings_destroy (&the_settings);
144 settings_copy (struct settings *dst, const struct settings *src)
147 fmt_settings_copy (&dst->styles, &src->styles);
150 /* Returns a copy of the current settings. */
154 struct settings *s = xmalloc (sizeof *s);
155 settings_copy (s, &the_settings);
159 /* Replaces the current settings by those in S. The caller retains ownership
162 settings_set (const struct settings *s)
164 settings_destroy (&the_settings);
165 settings_copy (&the_settings, s);
170 settings_destroy (struct settings *s)
174 fmt_settings_uninit (&s->styles);
175 if (s != &the_settings)
180 /* Returns the floating-point format used for RB and RBHEX
183 settings_get_input_float_format (void)
185 return the_settings.input_float_format;
188 /* Sets the floating-point format used for RB and RBHEX input to
191 settings_set_input_float_format (enum float_format format)
193 the_settings.input_float_format = format;
196 /* Returns the integer format used for IB and PIB input. */
198 settings_get_input_integer_format (void)
200 return the_settings.input_integer_format;
203 /* Sets the integer format used for IB and PIB input to
206 settings_set_input_integer_format (enum integer_format format)
208 the_settings.input_integer_format = format;
211 /* Returns the current output integer format. */
213 settings_get_output_integer_format (void)
215 return the_settings.output_integer_format;
218 /* Sets the output integer format to INTEGER_FORMAT. */
220 settings_set_output_integer_format (
221 enum integer_format integer_format)
223 the_settings.output_integer_format = integer_format;
226 /* Returns the current output float format. */
228 settings_get_output_float_format (void)
230 return the_settings.output_float_format;
233 /* Sets the output float format to FLOAT_FORMAT. */
235 settings_set_output_float_format (enum float_format float_format)
237 the_settings.output_float_format = float_format;
240 /* Screen length in lines. */
242 settings_get_viewlength (void)
244 return the_settings.viewlength;
247 /* Sets the view length. */
249 settings_set_viewlength (int viewlength_)
251 the_settings.viewlength = viewlength_;
256 settings_get_viewwidth(void)
258 return the_settings.viewwidth;
261 /* Sets the screen width. */
263 settings_set_viewwidth (int viewwidth_)
265 the_settings.viewwidth = viewwidth_;
268 /* Whether PSPP can erase and overwrite files. */
270 settings_get_safer_mode (void)
272 return the_settings.safer_mode;
275 /* Set safer mode. */
277 settings_set_safer_mode (void)
279 the_settings.safer_mode = true;
282 /* If echo is on, whether commands from include files are echoed. */
284 settings_get_include (void)
286 return the_settings.include;
289 /* Set include file echo. */
291 settings_set_include (bool include)
293 the_settings.include = include;
296 /* What year to use as the start of the epoch. */
298 settings_get_epoch (void)
300 assert (the_settings.epoch >= 0);
302 return the_settings.epoch;
305 /* Sets the year that starts the epoch. */
307 settings_set_epoch (int epoch)
312 struct tm *tm = localtime (&t);
313 epoch = (tm != NULL ? tm->tm_year + 1900 : 2000) - 69;
316 the_settings.epoch = epoch;
317 assert (the_settings.epoch >= 0);
320 /* Compress system files by default? */
322 settings_get_scompression (void)
324 return the_settings.scompress;
327 /* Set system file default compression. */
329 settings_set_scompression (bool scompress)
331 the_settings.scompress = scompress;
334 /* Whether to warn on undefined values in numeric data. */
336 settings_get_undefined (void)
338 return the_settings.undefined;
341 /* Set whether to warn on undefined values. */
343 settings_set_undefined (bool undefined)
345 the_settings.undefined = undefined;
348 /* The value that blank numeric fields are set to when read in. */
350 settings_get_blanks (void)
352 return the_settings.blanks;
355 /* Set the value that blank numeric fields are set to when read
358 settings_set_blanks (double blanks)
360 the_settings.blanks = blanks;
363 /* Returns the maximum number of messages to show of the given SEVERITY before
364 aborting. (The value for MSG_S_WARNING is interpreted as maximum number of
365 warnings and errors combined.) */
367 settings_get_max_messages (enum msg_severity severity)
369 assert (severity < MSG_N_SEVERITIES);
370 return the_settings.max_messages[severity];
373 /* Sets the maximum number of messages to show of the given SEVERITY before
374 aborting to MAX. (The value for MSG_S_WARNING is interpreted as maximum
375 number of warnings and errors combined.) In addition, in the case of
376 warnings the special value of zero indicates that no warnings are to be
380 settings_set_max_messages (enum msg_severity severity, int max)
382 assert (severity < MSG_N_SEVERITIES);
384 if (severity == MSG_S_WARNING)
389 _("MXWARNS set to zero. No further warnings will be given even when potentially problematic situations are encountered."));
390 msg_ui_disable_warnings (true);
392 else if (the_settings.max_messages [MSG_S_WARNING] == 0)
394 msg_ui_disable_warnings (false);
395 the_settings.max_messages[MSG_S_WARNING] = max;
396 msg (MW, _("Warnings re-enabled. %d warnings will be issued before aborting syntax processing."), max);
400 the_settings.max_messages[severity] = max;
403 /* Independent of get_printback, controls whether the commands
404 generated by macro invocations are displayed. */
406 settings_get_mprint (void)
408 return the_settings.mprint;
411 /* Sets whether the commands generated by macro invocations are
414 settings_set_mprint (bool mprint)
416 the_settings.mprint = mprint;
419 /* Implied limit of unbounded loop. */
421 settings_get_mxloops (void)
423 return the_settings.mxloops;
426 /* Set implied limit of unbounded loop. */
428 settings_set_mxloops (int mxloops)
430 the_settings.mxloops = mxloops;
433 /* Approximate maximum amount of memory to use for cases, in
436 settings_get_workspace (void)
438 return the_settings.workspace;
441 /* Approximate maximum number of cases to allocate in-core, given
442 that each case has the format given in PROTO. */
444 settings_get_workspace_cases (const struct caseproto *proto)
446 size_t n_cases = settings_get_workspace () / case_get_cost (proto);
447 return MAX (n_cases, 4);
450 /* Set approximate maximum amount of memory to use for cases, in
454 settings_set_workspace (size_t workspace)
456 the_settings.workspace = workspace;
459 /* Default format for variables created by transformations and by
460 DATA LIST {FREE,LIST}. */
461 const struct fmt_spec *
462 settings_get_format (void)
464 return &the_settings.default_format;
467 /* Set default format for variables created by transformations
468 and by DATA LIST {FREE,LIST}. */
470 settings_set_format (const struct fmt_spec *default_format)
472 the_settings.default_format = *default_format;
475 /* Are we in testing mode? (e.g. --testing-mode command line
478 settings_get_testing_mode (void)
480 return the_settings.testing_mode;
483 /* Set testing mode. */
485 settings_set_testing_mode (bool testing_mode)
487 the_settings.testing_mode = testing_mode;
491 settings_get_fuzzbits (void)
493 return the_settings.fuzzbits;
497 settings_set_fuzzbits (int fuzzbits)
499 the_settings.fuzzbits = fuzzbits;
502 /* Return the current algorithm setting */
504 settings_get_algorithm (void)
506 return the_settings.cmd_algorithm;
509 /* Set the algorithm option globally. */
511 settings_set_algorithm (enum behavior_mode mode)
513 the_settings.global_algorithm = the_settings.cmd_algorithm = mode;
516 /* Set the algorithm option for this command only */
518 settings_set_cmd_algorithm (enum behavior_mode mode)
520 the_settings.cmd_algorithm = mode;
523 /* Unset the algorithm option for this command */
525 unset_cmd_algorithm (void)
527 the_settings.cmd_algorithm = the_settings.global_algorithm;
530 /* Get the current syntax setting */
532 settings_get_syntax (void)
534 return the_settings.syntax;
537 /* Set the syntax option */
539 settings_set_syntax (enum behavior_mode mode)
541 the_settings.syntax = mode;
545 /* Sets custom currency specifier CC having name CC_NAME ('A' through
546 'E') to correspond to the settings in CC_STRING. */
548 settings_set_cc (const char *cc_string, enum fmt_type type)
550 struct fmt_number_style *style = fmt_number_style_from_string (cc_string);
553 msg (SE, _("%s: Custom currency string `%s' does not contain "
554 "exactly three periods or commas (or it contains both)."),
555 fmt_name (type), cc_string);
559 fmt_settings_set_cc (&the_settings.styles, type, style);
563 /* Returns the decimal point character for TYPE. */
565 settings_get_decimal_char (enum fmt_type type)
567 return fmt_settings_get_style (&the_settings.styles, type)->decimal;
571 settings_set_decimal_char (char decimal)
573 the_settings.styles.decimal = decimal;
576 /* Returns the number formatting style associated with the given
578 const struct fmt_number_style *
579 settings_get_style (enum fmt_type type)
581 assert (is_fmt_type (type));
582 return fmt_settings_get_style (&the_settings.styles, type);
585 /* Returns a string of the form "$#,###.##" according to FMT,
586 which must be of type FMT_DOLLAR. The caller must free the
589 settings_dollar_template (const struct fmt_spec *fmt)
591 struct string str = DS_EMPTY_INITIALIZER;
593 const struct fmt_number_style *fns ;
595 assert (fmt->type == FMT_DOLLAR);
597 fns = fmt_settings_get_style (&the_settings.styles, fmt->type);
599 ds_put_byte (&str, '$');
600 for (c = MAX (fmt->w - fmt->d - 1, 0); c > 0;)
602 ds_put_byte (&str, '#');
603 if (--c % 4 == 0 && c > 0)
605 ds_put_byte (&str, fns->grouping);
611 ds_put_byte (&str, fns->decimal);
612 ds_put_byte_multiple (&str, '#', fmt->d);
615 return ds_cstr (&str);
619 settings_set_output_routing (enum settings_output_type type,
620 enum settings_output_devices devices)
622 assert (type < SETTINGS_N_OUTPUT_TYPES);
623 the_settings.output_routing[type] = devices;
626 enum settings_output_devices
627 settings_get_output_routing (enum settings_output_type type)
629 assert (type < SETTINGS_N_OUTPUT_TYPES);
630 return the_settings.output_routing[type] | SETTINGS_DEVICE_UNFILTERED;
633 enum settings_value_show
634 settings_get_show_values (void)
636 return the_settings.show_values;
640 settings_set_show_values (enum settings_value_show s)
642 the_settings.show_values = s;
646 enum settings_value_show
647 settings_get_show_variables (void)
649 return the_settings.show_variables;
653 settings_set_show_variables (enum settings_value_show s)
655 the_settings.show_variables = s;