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;
75 enum settings_output_devices output_routing[SETTINGS_N_OUTPUT_TYPES];
77 enum settings_value_show show_values;
78 enum settings_value_show show_variables;
81 static struct settings the_settings = {
82 INTEGER_NATIVE, /* input_integer_format */
83 FLOAT_NATIVE_DOUBLE, /* input_float_format */
84 INTEGER_NATIVE, /* output_integer_format */
85 FLOAT_NATIVE_DOUBLE, /* output_float_format */
88 false, /* safer_mode */
90 true, /* route_errors_to_terminal */
91 true, /* route_errors_to_listing */
98 100, /* MSG_S_ERROR */
99 100, /* MSG_S_WARNING */
103 true, /* printback */
106 64L * 1024 * 1024, /* workspace */
107 {FMT_F, 8, 2}, /* default_format */
108 false, /* testing_mode */
110 ENHANCED, /* cmd_algorithm */
111 ENHANCED, /* global_algorithm */
112 ENHANCED, /* syntax */
113 FMT_SETTINGS_INIT, /* styles */
116 {SETTINGS_DEVICE_LISTING | SETTINGS_DEVICE_TERMINAL,
117 SETTINGS_DEVICE_LISTING | SETTINGS_DEVICE_TERMINAL,
119 SETTINGS_DEVICE_LISTING | SETTINGS_DEVICE_TERMINAL},
121 SETTINGS_VALUE_SHOW_LABEL,
122 SETTINGS_VALUE_SHOW_LABEL
125 /* Initializes the settings module. */
129 settings_set_decimal_char (get_system_decimal ());
132 /* Cleans up the settings module. */
136 settings_destroy (&the_settings);
140 settings_copy (struct settings *dst, const struct settings *src)
143 dst->styles = fmt_settings_copy (&src->styles);
146 /* Returns a copy of the current settings. */
150 struct settings *s = xmalloc (sizeof *s);
151 settings_copy (s, &the_settings);
155 /* Replaces the current settings by those in S. The caller retains ownership
158 settings_set (const struct settings *s)
160 settings_destroy (&the_settings);
161 settings_copy (&the_settings, s);
166 settings_destroy (struct settings *s)
170 fmt_settings_uninit (&s->styles);
171 if (s != &the_settings)
176 /* Returns the floating-point format used for RB and RBHEX
179 settings_get_input_float_format (void)
181 return the_settings.input_float_format;
184 /* Sets the floating-point format used for RB and RBHEX input to
187 settings_set_input_float_format (enum float_format format)
189 the_settings.input_float_format = format;
192 /* Returns the integer format used for IB and PIB input. */
194 settings_get_input_integer_format (void)
196 return the_settings.input_integer_format;
199 /* Sets the integer format used for IB and PIB input to
202 settings_set_input_integer_format (enum integer_format format)
204 the_settings.input_integer_format = format;
207 /* Returns the current output integer format. */
209 settings_get_output_integer_format (void)
211 return the_settings.output_integer_format;
214 /* Sets the output integer format to INTEGER_FORMAT. */
216 settings_set_output_integer_format (
217 enum integer_format integer_format)
219 the_settings.output_integer_format = integer_format;
222 /* Returns the current output float format. */
224 settings_get_output_float_format (void)
226 return the_settings.output_float_format;
229 /* Sets the output float format to FLOAT_FORMAT. */
231 settings_set_output_float_format (enum float_format float_format)
233 the_settings.output_float_format = float_format;
236 /* Screen length in lines. */
238 settings_get_viewlength (void)
240 return the_settings.viewlength;
243 /* Sets the view length. */
245 settings_set_viewlength (int viewlength_)
247 the_settings.viewlength = viewlength_;
252 settings_get_viewwidth(void)
254 return the_settings.viewwidth;
257 /* Sets the screen width. */
259 settings_set_viewwidth (int viewwidth_)
261 the_settings.viewwidth = viewwidth_;
264 /* Whether PSPP can erase and overwrite files. */
266 settings_get_safer_mode (void)
268 return the_settings.safer_mode;
271 /* Set safer mode. */
273 settings_set_safer_mode (void)
275 the_settings.safer_mode = true;
278 /* If echo is on, whether commands from include files are echoed. */
280 settings_get_include (void)
282 return the_settings.include;
285 /* Set include file echo. */
287 settings_set_include (bool include)
289 the_settings.include = include;
292 /* Sets the year that starts the epoch. */
294 settings_set_epoch (int epoch)
296 the_settings.styles.epoch = epoch;
299 /* Compress system files by default? */
301 settings_get_scompression (void)
303 return the_settings.scompress;
306 /* Set system file default compression. */
308 settings_set_scompression (bool scompress)
310 the_settings.scompress = scompress;
313 /* Whether to warn on undefined values in numeric data. */
315 settings_get_undefined (void)
317 return the_settings.undefined;
320 /* Set whether to warn on undefined values. */
322 settings_set_undefined (bool undefined)
324 the_settings.undefined = undefined;
327 /* The value that blank numeric fields are set to when read in. */
329 settings_get_blanks (void)
331 return the_settings.blanks;
334 /* Set the value that blank numeric fields are set to when read
337 settings_set_blanks (double blanks)
339 the_settings.blanks = blanks;
342 /* Returns the maximum number of messages to show of the given SEVERITY before
343 aborting. (The value for MSG_S_WARNING is interpreted as maximum number of
344 warnings and errors combined.) */
346 settings_get_max_messages (enum msg_severity severity)
348 assert (severity < MSG_N_SEVERITIES);
349 return the_settings.max_messages[severity];
352 /* Sets the maximum number of messages to show of the given SEVERITY before
353 aborting to MAX. (The value for MSG_S_WARNING is interpreted as maximum
354 number of warnings and errors combined.) In addition, in the case of
355 warnings the special value of zero indicates that no warnings are to be
359 settings_set_max_messages (enum msg_severity severity, int max)
361 assert (severity < MSG_N_SEVERITIES);
363 if (severity == MSG_S_WARNING)
368 _("MXWARNS set to zero. No further warnings will be given even when potentially problematic situations are encountered."));
369 msg_ui_disable_warnings (true);
371 else if (the_settings.max_messages [MSG_S_WARNING] == 0)
373 msg_ui_disable_warnings (false);
374 the_settings.max_messages[MSG_S_WARNING] = max;
375 msg (MW, _("Warnings re-enabled. %d warnings will be issued before aborting syntax processing."), max);
379 the_settings.max_messages[severity] = max;
382 /* Independent of get_printback, controls whether the commands
383 generated by macro invocations are displayed. */
385 settings_get_mprint (void)
387 return the_settings.mprint;
390 /* Sets whether the commands generated by macro invocations are
393 settings_set_mprint (bool mprint)
395 the_settings.mprint = mprint;
398 /* Implied limit of unbounded loop. */
400 settings_get_mxloops (void)
402 return the_settings.mxloops;
405 /* Set implied limit of unbounded loop. */
407 settings_set_mxloops (int mxloops)
409 the_settings.mxloops = mxloops;
412 /* Approximate maximum amount of memory to use for cases, in
415 settings_get_workspace (void)
417 return the_settings.workspace;
420 /* Approximate maximum number of cases to allocate in-core, given
421 that each case has the format given in PROTO. */
423 settings_get_workspace_cases (const struct caseproto *proto)
425 size_t n_cases = settings_get_workspace () / case_get_cost (proto);
426 return MAX (n_cases, 4);
429 /* Set approximate maximum amount of memory to use for cases, in
433 settings_set_workspace (size_t workspace)
435 the_settings.workspace = workspace;
438 /* Default format for variables created by transformations and by
439 DATA LIST {FREE,LIST}. */
440 const struct fmt_spec *
441 settings_get_format (void)
443 return &the_settings.default_format;
446 /* Set default format for variables created by transformations
447 and by DATA LIST {FREE,LIST}. */
449 settings_set_format (const struct fmt_spec *default_format)
451 the_settings.default_format = *default_format;
454 /* Are we in testing mode? (e.g. --testing-mode command line
457 settings_get_testing_mode (void)
459 return the_settings.testing_mode;
462 /* Set testing mode. */
464 settings_set_testing_mode (bool testing_mode)
466 the_settings.testing_mode = testing_mode;
470 settings_get_fuzzbits (void)
472 return the_settings.fuzzbits;
476 settings_set_fuzzbits (int fuzzbits)
478 the_settings.fuzzbits = fuzzbits;
481 /* Return the current algorithm setting */
483 settings_get_algorithm (void)
485 return the_settings.cmd_algorithm;
488 /* Set the algorithm option globally. */
490 settings_set_algorithm (enum behavior_mode mode)
492 the_settings.global_algorithm = the_settings.cmd_algorithm = mode;
495 /* Set the algorithm option for this command only */
497 settings_set_cmd_algorithm (enum behavior_mode mode)
499 the_settings.cmd_algorithm = mode;
502 /* Unset the algorithm option for this command */
504 unset_cmd_algorithm (void)
506 the_settings.cmd_algorithm = the_settings.global_algorithm;
509 /* Get the current syntax setting */
511 settings_get_syntax (void)
513 return the_settings.syntax;
516 /* Set the syntax option */
518 settings_set_syntax (enum behavior_mode mode)
520 the_settings.syntax = mode;
524 /* Sets custom currency specifier CC having name CC_NAME ('A' through
525 'E') to correspond to the settings in CC_STRING. */
527 settings_set_cc (const char *cc_string, enum fmt_type type)
529 struct fmt_number_style *style = fmt_number_style_from_string (cc_string);
532 msg (SE, _("%s: Custom currency string `%s' does not contain "
533 "exactly three periods or commas (or it contains both)."),
534 fmt_name (type), cc_string);
538 fmt_settings_set_cc (&the_settings.styles, type, style);
543 settings_set_decimal_char (char decimal)
545 the_settings.styles.decimal = decimal;
548 const struct fmt_settings *
549 settings_get_fmt_settings (void)
551 return &the_settings.styles;
554 /* Returns a string of the form "$#,###.##" according to FMT,
555 which must be of type FMT_DOLLAR. The caller must free the
558 settings_dollar_template (const struct fmt_spec *fmt)
560 struct string str = DS_EMPTY_INITIALIZER;
562 const struct fmt_number_style *fns ;
564 assert (fmt->type == FMT_DOLLAR);
566 fns = fmt_settings_get_style (&the_settings.styles, fmt->type);
568 ds_put_byte (&str, '$');
569 for (c = MAX (fmt->w - fmt->d - 1, 0); c > 0;)
571 ds_put_byte (&str, '#');
572 if (--c % 4 == 0 && c > 0)
574 ds_put_byte (&str, fns->grouping);
580 ds_put_byte (&str, fns->decimal);
581 ds_put_byte_multiple (&str, '#', fmt->d);
584 return ds_cstr (&str);
588 settings_set_output_routing (enum settings_output_type type,
589 enum settings_output_devices devices)
591 assert (type < SETTINGS_N_OUTPUT_TYPES);
592 the_settings.output_routing[type] = devices;
595 enum settings_output_devices
596 settings_get_output_routing (enum settings_output_type type)
598 assert (type < SETTINGS_N_OUTPUT_TYPES);
599 return the_settings.output_routing[type] | SETTINGS_DEVICE_UNFILTERED;
602 enum settings_value_show
603 settings_get_show_values (void)
605 return the_settings.show_values;
609 settings_set_show_values (enum settings_value_show s)
611 the_settings.show_values = s;
615 enum settings_value_show
616 settings_get_show_variables (void)
618 return the_settings.show_variables;
622 settings_set_show_variables (enum settings_value_show s)
624 the_settings.show_variables = s;