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"
24 #include "data/case.h"
25 #include "data/format.h"
26 #include "data/value.h"
27 #include "libpspp/i18n.h"
28 #include "libpspp/integer-format.h"
29 #include "libpspp/message.h"
31 #include "gl/minmax.h"
32 #include "gl/xalloc.h"
35 #define _(msgid) gettext (msgid)
39 /* Integer format used for IB and PIB input. */
40 enum integer_format input_integer_format;
42 /* Floating-point format used for RB and RBHEX input. */
43 enum float_format input_float_format;
45 /* Format of integers in output (SET WIB). */
46 enum integer_format output_integer_format;
48 /* Format of reals in output (SET WRB). */
49 enum float_format output_float_format;
55 bool route_errors_to_terminal;
56 bool route_errors_to_listing;
60 int max_messages[MSG_N_SEVERITIES];
65 struct fmt_spec default_format;
73 struct fmt_settings styles;
76 enum settings_output_devices output_routing[SETTINGS_N_OUTPUT_TYPES];
78 enum settings_value_show show_values;
79 enum settings_value_show show_variables;
82 static struct settings the_settings = {
83 INTEGER_NATIVE, /* input_integer_format */
84 FLOAT_NATIVE_DOUBLE, /* input_float_format */
85 INTEGER_NATIVE, /* output_integer_format */
86 FLOAT_NATIVE_DOUBLE, /* output_float_format */
89 false, /* safer_mode */
91 true, /* route_errors_to_terminal */
92 true, /* route_errors_to_listing */
99 100, /* MSG_S_ERROR */
100 100, /* MSG_S_WARNING */
104 true, /* printback */
107 64L * 1024 * 1024, /* workspace */
108 { .type = FMT_F, .w = 8, .d = 2 }, /* default_format */
109 false, /* testing_mode */
111 ENHANCED, /* cmd_algorithm */
112 ENHANCED, /* global_algorithm */
113 ENHANCED, /* syntax */
114 FMT_SETTINGS_INIT, /* styles */
118 {SETTINGS_DEVICE_LISTING | SETTINGS_DEVICE_TERMINAL,
119 SETTINGS_DEVICE_LISTING | SETTINGS_DEVICE_TERMINAL,
121 SETTINGS_DEVICE_LISTING | SETTINGS_DEVICE_TERMINAL},
123 SETTINGS_VALUE_SHOW_LABEL,
124 SETTINGS_VALUE_SHOW_LABEL
127 /* Initializes the settings module. */
131 settings_set_decimal_char (get_system_decimal ());
134 /* Cleans up the settings module. */
138 settings_destroy (&the_settings);
142 settings_copy (struct settings *dst, const struct settings *src)
145 dst->styles = fmt_settings_copy (&src->styles);
148 /* Returns a copy of the current settings. */
152 struct settings *s = xmalloc (sizeof *s);
153 settings_copy (s, &the_settings);
157 /* Replaces the current settings by those in S. The caller retains ownership
160 settings_set (const struct settings *s)
162 settings_destroy (&the_settings);
163 settings_copy (&the_settings, s);
168 settings_destroy (struct settings *s)
172 fmt_settings_uninit (&s->styles);
173 if (s != &the_settings)
178 /* Returns the floating-point format used for RB and RBHEX
181 settings_get_input_float_format (void)
183 return the_settings.input_float_format;
186 /* Sets the floating-point format used for RB and RBHEX input to
189 settings_set_input_float_format (enum float_format format)
191 the_settings.input_float_format = format;
194 /* Returns the integer format used for IB and PIB input. */
196 settings_get_input_integer_format (void)
198 return the_settings.input_integer_format;
201 /* Sets the integer format used for IB and PIB input to
204 settings_set_input_integer_format (enum integer_format format)
206 the_settings.input_integer_format = format;
209 /* Returns the current output integer format. */
211 settings_get_output_integer_format (void)
213 return the_settings.output_integer_format;
216 /* Sets the output integer format to INTEGER_FORMAT. */
218 settings_set_output_integer_format (
219 enum integer_format integer_format)
221 the_settings.output_integer_format = integer_format;
224 /* Returns the current output float format. */
226 settings_get_output_float_format (void)
228 return the_settings.output_float_format;
231 /* Sets the output float format to FLOAT_FORMAT. */
233 settings_set_output_float_format (enum float_format float_format)
235 the_settings.output_float_format = float_format;
238 /* Screen length in lines. */
240 settings_get_viewlength (void)
242 return the_settings.viewlength;
245 /* Sets the view length. */
247 settings_set_viewlength (int viewlength_)
249 the_settings.viewlength = viewlength_;
254 settings_get_viewwidth(void)
256 return the_settings.viewwidth;
259 /* Sets the screen width. */
261 settings_set_viewwidth (int viewwidth_)
263 the_settings.viewwidth = viewwidth_;
266 /* Whether PSPP can erase and overwrite files. */
268 settings_get_safer_mode (void)
270 return the_settings.safer_mode;
273 /* Set safer mode. */
275 settings_set_safer_mode (void)
277 the_settings.safer_mode = true;
280 /* If echo is on, whether commands from include files are echoed. */
282 settings_get_include (void)
284 return the_settings.include;
287 /* Set include file echo. */
289 settings_set_include (bool include)
291 the_settings.include = include;
294 /* Sets the year that starts the epoch. */
296 settings_set_epoch (int epoch)
298 the_settings.styles.epoch = epoch;
301 /* Compress system files by default? */
303 settings_get_scompression (void)
305 return the_settings.scompress;
308 /* Set system file default compression. */
310 settings_set_scompression (bool scompress)
312 the_settings.scompress = scompress;
315 /* Whether to warn on undefined values in numeric data. */
317 settings_get_undefined (void)
319 return the_settings.undefined;
322 /* Set whether to warn on undefined values. */
324 settings_set_undefined (bool undefined)
326 the_settings.undefined = undefined;
329 /* The value that blank numeric fields are set to when read in. */
331 settings_get_blanks (void)
333 return the_settings.blanks;
336 /* Set the value that blank numeric fields are set to when read
339 settings_set_blanks (double blanks)
341 the_settings.blanks = blanks;
344 /* Returns the maximum number of messages to show of the given SEVERITY before
345 aborting. (The value for MSG_S_WARNING is interpreted as maximum number of
346 warnings and errors combined.) */
348 settings_get_max_messages (enum msg_severity severity)
350 assert (severity < MSG_N_SEVERITIES);
351 return the_settings.max_messages[severity];
354 /* Sets the maximum number of messages to show of the given SEVERITY before
355 aborting to MAX. (The value for MSG_S_WARNING is interpreted as maximum
356 number of warnings and errors combined.) In addition, in the case of
357 warnings the special value of zero indicates that no warnings are to be
361 settings_set_max_messages (enum msg_severity severity, int max)
363 assert (severity < MSG_N_SEVERITIES);
365 if (severity == MSG_S_WARNING)
370 _("MXWARNS set to zero. No further warnings will be given even when potentially problematic situations are encountered."));
371 msg_ui_disable_warnings (true);
373 else if (the_settings.max_messages [MSG_S_WARNING] == 0)
375 msg_ui_disable_warnings (false);
376 the_settings.max_messages[MSG_S_WARNING] = max;
377 msg (MW, _("Warnings re-enabled. %d warnings will be issued before aborting syntax processing."), max);
381 the_settings.max_messages[severity] = max;
384 /* Independent of get_printback, controls whether the commands
385 generated by macro invocations are displayed. */
387 settings_get_mprint (void)
389 return the_settings.mprint;
392 /* Sets whether the commands generated by macro invocations are
395 settings_set_mprint (bool mprint)
397 the_settings.mprint = mprint;
400 /* Implied limit of unbounded loop. */
402 settings_get_mxloops (void)
404 return the_settings.mxloops;
407 /* Set implied limit of unbounded loop. */
409 settings_set_mxloops (int mxloops)
411 the_settings.mxloops = mxloops;
414 /* Approximate maximum amount of memory to use for cases, in
417 settings_get_workspace (void)
419 return the_settings.workspace;
422 /* Approximate maximum number of cases to allocate in-core, given
423 that each case has the format given in PROTO. */
425 settings_get_workspace_cases (const struct caseproto *proto)
427 size_t n_cases = settings_get_workspace () / case_get_cost (proto);
428 return MAX (n_cases, 4);
431 /* Set approximate maximum amount of memory to use for cases, in
435 settings_set_workspace (size_t workspace)
437 the_settings.workspace = workspace;
440 /* Default format for variables created by transformations and by
441 DATA LIST {FREE,LIST}. */
442 const struct fmt_spec *
443 settings_get_format (void)
445 return &the_settings.default_format;
448 /* Set default format for variables created by transformations
449 and by DATA LIST {FREE,LIST}. */
451 settings_set_format (const struct fmt_spec *default_format)
453 the_settings.default_format = *default_format;
456 /* Are we in testing mode? (e.g. --testing-mode command line
459 settings_get_testing_mode (void)
461 return the_settings.testing_mode;
464 /* Set testing mode. */
466 settings_set_testing_mode (bool testing_mode)
468 the_settings.testing_mode = testing_mode;
472 settings_get_fuzzbits (void)
474 return the_settings.fuzzbits;
478 settings_set_fuzzbits (int fuzzbits)
480 the_settings.fuzzbits = fuzzbits;
483 /* Return the current algorithm setting */
485 settings_get_algorithm (void)
487 return the_settings.cmd_algorithm;
490 /* Set the algorithm option globally. */
492 settings_set_algorithm (enum behavior_mode mode)
494 the_settings.global_algorithm = the_settings.cmd_algorithm = mode;
497 /* Set the algorithm option for this command only */
499 settings_set_cmd_algorithm (enum behavior_mode mode)
501 the_settings.cmd_algorithm = mode;
504 /* Unset the algorithm option for this command */
506 unset_cmd_algorithm (void)
508 the_settings.cmd_algorithm = the_settings.global_algorithm;
511 /* Get the current syntax setting */
513 settings_get_syntax (void)
515 return the_settings.syntax;
518 /* Set the syntax option */
520 settings_set_syntax (enum behavior_mode mode)
522 the_settings.syntax = mode;
526 /* Sets custom currency specifier CC having name CC_NAME ('A' through
527 'E') to correspond to the settings in CC_STRING. */
529 settings_set_cc (const char *cc_string, enum fmt_type type)
531 struct fmt_number_style *style = fmt_number_style_from_string (cc_string);
534 msg (SE, _("%s: Custom currency string `%s' does not contain "
535 "exactly three periods or commas (or it contains both)."),
536 fmt_name (type), cc_string);
540 fmt_settings_set_cc (&the_settings.styles, type, style);
545 settings_set_decimal_char (char decimal)
547 the_settings.styles.decimal = decimal;
550 const struct fmt_settings *
551 settings_get_fmt_settings (void)
553 return &the_settings.styles;
557 settings_get_small (void)
559 return the_settings.small;
563 settings_set_small (double small)
565 the_settings.small = small;
568 /* Returns a string of the form "$#,###.##" according to FMT,
569 which must be of type FMT_DOLLAR. The caller must free the
572 settings_dollar_template (const struct fmt_spec *fmt)
574 struct string str = DS_EMPTY_INITIALIZER;
576 const struct fmt_number_style *fns ;
578 assert (fmt->type == FMT_DOLLAR);
580 fns = fmt_settings_get_style (&the_settings.styles, fmt->type);
582 ds_put_byte (&str, '$');
583 for (c = MAX (fmt->w - fmt->d - 1, 0); c > 0;)
585 ds_put_byte (&str, '#');
586 if (--c % 4 == 0 && c > 0)
588 ds_put_byte (&str, fns->grouping);
594 ds_put_byte (&str, fns->decimal);
595 ds_put_byte_multiple (&str, '#', fmt->d);
598 return ds_cstr (&str);
602 settings_set_output_routing (enum settings_output_type type,
603 enum settings_output_devices devices)
605 assert (type < SETTINGS_N_OUTPUT_TYPES);
606 the_settings.output_routing[type] = devices;
609 enum settings_output_devices
610 settings_get_output_routing (enum settings_output_type type)
612 assert (type < SETTINGS_N_OUTPUT_TYPES);
613 return the_settings.output_routing[type] | SETTINGS_DEVICE_UNFILTERED;
616 enum settings_value_show
617 settings_get_show_values (void)
619 return the_settings.show_values;
623 settings_set_show_values (enum settings_value_show s)
625 the_settings.show_values = s;
629 enum settings_value_show
630 settings_get_show_variables (void)
632 return the_settings.show_variables;
636 settings_set_show_variables (enum settings_value_show s)
638 the_settings.show_variables = s;