John's original code for patch #6210.
[pspp-builds.git] / src / data / settings.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2007 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18 #include "settings.h"
19 #include <assert.h>
20 #include <stdlib.h>
21 #include <time.h>
22 #include "format.h"
23 #include "value.h"
24 #include "xalloc.h"
25 #include <libpspp/i18n.h>
26
27 #include "error.h"
28
29 #include "gettext.h"
30 #define _(msgid) gettext (msgid)
31
32 static int *viewlength = NULL;
33 static int *viewwidth = NULL;
34 static bool long_view = false;
35
36 static bool safer_mode = false;
37
38 static bool do_echo = false;
39 static bool include = true;
40
41 static int epoch = -1;
42
43 static bool errorbreak = false;
44
45 static bool route_errors_to_terminal = true;
46 static bool route_errors_to_listing = true;
47
48 static bool scompress = true;
49
50 static bool undefined = true;
51 static double blanks = SYSMIS;
52
53 static int mxwarns = 100;
54 static int mxerrs = 100;
55
56 static bool printback = true;
57 static bool mprint = true;
58
59 static int mxloops = 1;
60
61 static bool nulline = true;
62
63 static char endcmd = '.';
64
65 static size_t workspace = 4L * 1024 * 1024;
66
67 static struct fmt_spec default_format = {FMT_F, 8, 2};
68
69 static bool testing_mode = false;
70
71 static int global_algorithm = ENHANCED;
72 static int cmd_algorithm = ENHANCED;
73 static int *algorithm = &global_algorithm;
74
75 static int syntax = ENHANCED;
76
77 static void init_viewport (int *, int *);
78
79 void
80 settings_init (int *width, int *length)
81 {
82   init_viewport (width, length);
83   i18n_init ();
84 }
85
86 void
87 settings_done (void)
88 {
89   i18n_done ();
90 }
91
92 /* Screen length in lines. */
93 int
94 get_viewlength (void)
95 {
96   return *viewlength;
97 }
98
99 /* Sets the view length. */
100 void
101 set_viewlength (int viewlength_)
102 {
103   *viewlength = viewlength_;
104 }
105
106 /* Set view width to a very long value, and prevent it from ever
107    changing. */
108 void
109 force_long_view (void)
110 {
111   long_view = true;
112 }
113
114 /* Screen width. */
115 int
116 get_viewwidth(void)
117 {
118   if (long_view)
119     return 9999;
120
121   return *viewwidth;
122 }
123
124 /* Sets the screen width. */
125 void
126 set_viewwidth (int viewwidth_)
127 {
128   *viewwidth = viewwidth_;
129 }
130
131 static void
132 init_viewport (int  *width, int *length)
133 {
134   viewwidth = width;
135   viewlength = length;
136
137   if (long_view)
138     return;
139
140 }
141
142 /* Whether PSPP can erase and overwrite files. */
143 bool
144 get_safer_mode (void)
145 {
146   return safer_mode;
147 }
148
149 /* Set safer mode. */
150 void
151 set_safer_mode (void)
152 {
153   safer_mode = true;
154 }
155
156 /* Echo commands to the listing file/printer? */
157 bool
158 get_echo (void)
159 {
160   return do_echo;
161 }
162
163 /* Set echo. */
164 void
165 set_echo (bool echo_)
166 {
167   do_echo = echo_;
168 }
169
170 /* If echo is on, whether commands from include files are echoed. */
171 bool
172 get_include (void)
173 {
174   return include;
175 }
176
177 /* Set include file echo. */
178 void
179 set_include (bool include_)
180 {
181   include = include_;
182 }
183
184 /* What year to use as the start of the epoch. */
185 int
186 get_epoch (void)
187 {
188   if (epoch < 0)
189     {
190       time_t t = time (0);
191       struct tm *tm = localtime (&t);
192       epoch = (tm != NULL ? tm->tm_year + 1900 : 2000) - 69;
193     }
194
195   return epoch;
196 }
197
198 /* Sets the year that starts the epoch. */
199 void
200 set_epoch (int epoch_)
201 {
202   epoch = epoch_;
203 }
204
205 /* Does an error stop execution? */
206 bool
207 get_errorbreak (void)
208 {
209   return errorbreak;
210 }
211
212 /* Sets whether an error stops execution. */
213 void
214 set_errorbreak (bool errorbreak_)
215 {
216   errorbreak = errorbreak_;
217 }
218
219 /* Route error messages to terminal? */
220 bool
221 get_error_routing_to_terminal (void)
222 {
223   return route_errors_to_terminal;
224 }
225
226 /* Sets whether error messages should be routed to the
227    terminal. */
228 void
229 set_error_routing_to_terminal (bool route_to_terminal)
230 {
231   route_errors_to_terminal = route_to_terminal;
232 }
233
234 /* Route error messages to listing file? */
235 bool
236 get_error_routing_to_listing (void)
237 {
238   return route_errors_to_listing;
239 }
240
241 /* Sets whether error messages should be routed to the
242    listing file. */
243 void
244 set_error_routing_to_listing (bool route_to_listing)
245 {
246   route_errors_to_listing = route_to_listing;
247 }
248
249 /* Compress system files by default? */
250 bool
251 get_scompression (void)
252 {
253   return scompress;
254 }
255
256 /* Set system file default compression. */
257 void
258 set_scompression (bool scompress_)
259 {
260   scompress = scompress_;
261 }
262
263 /* Whether to warn on undefined values in numeric data. */
264 bool
265 get_undefined (void)
266 {
267   return undefined;
268 }
269
270 /* Set whether to warn on undefined values. */
271 void
272 set_undefined (bool undefined_)
273 {
274   undefined = undefined_;
275 }
276
277 /* The value that blank numeric fields are set to when read in. */
278 double
279 get_blanks (void)
280 {
281   return blanks;
282 }
283
284 /* Set the value that blank numeric fields are set to when read
285    in. */
286 void
287 set_blanks (double blanks_)
288 {
289   blanks = blanks_;
290 }
291
292 /* Maximum number of warnings + errors. */
293 int
294 get_mxwarns (void)
295 {
296   return mxwarns;
297 }
298
299 /* Sets maximum number of warnings + errors. */
300 void
301 set_mxwarns (int mxwarns_)
302 {
303   mxwarns = mxwarns_;
304 }
305
306 /* Maximum number of errors. */
307 int
308 get_mxerrs (void)
309 {
310   return mxerrs;
311 }
312
313 /* Sets maximum number of errors. */
314 void
315 set_mxerrs (int mxerrs_)
316 {
317   mxerrs = mxerrs_;
318 }
319
320 /* Whether commands are written to the display. */
321 bool
322 get_printback (void)
323 {
324   return printback;
325 }
326
327 /* Sets whether commands are written to the display. */
328 void
329 set_printback (bool printback_)
330 {
331   printback = printback_;
332 }
333
334 /* Independent of get_printback, controls whether the commands
335    generated by macro invocations are displayed. */
336 bool
337 get_mprint (void)
338 {
339   return mprint;
340 }
341
342 /* Sets whether the commands generated by macro invocations are
343    displayed. */
344 void
345 set_mprint (bool mprint_)
346 {
347   mprint = mprint_;
348 }
349
350 /* Implied limit of unbounded loop. */
351 int
352 get_mxloops (void)
353 {
354   return mxloops;
355 }
356
357 /* Set implied limit of unbounded loop. */
358 void
359 set_mxloops (int mxloops_)
360 {
361   mxloops = mxloops_;
362 }
363
364 /* Whether a blank line is a command terminator. */
365 bool
366 get_nulline (void)
367 {
368   return nulline;
369 }
370
371 /* Set whether a blank line is a command terminator. */
372 void
373 set_nulline (bool nulline_)
374 {
375   nulline = nulline_;
376 }
377
378 /* The character used to terminate commands. */
379 char
380 get_endcmd (void)
381 {
382   return endcmd;
383 }
384
385 /* Set the character used to terminate commands. */
386 void
387 set_endcmd (char endcmd_)
388 {
389   endcmd = endcmd_;
390 }
391
392 /* Approximate maximum amount of memory to use for cases, in
393    bytes. */
394 size_t
395 get_workspace (void)
396 {
397   return workspace;
398 }
399
400 /* Approximate maximum number of cases to allocate in-core, given
401    that each case contains VALUE_CNT values. */
402 size_t
403 get_workspace_cases (size_t value_cnt)
404 {
405   size_t case_size = sizeof (union value) * value_cnt + 4 * sizeof (void *);
406   size_t case_cnt = MAX (get_workspace () / case_size, 4);
407   return case_cnt;
408 }
409
410 /* Set approximate maximum amount of memory to use for cases, in
411    bytes. */
412
413 void
414 set_workspace (size_t workspace_)
415 {
416   workspace = workspace_;
417 }
418
419 /* Default format for variables created by transformations and by
420    DATA LIST {FREE,LIST}. */
421 const struct fmt_spec *
422 get_format (void)
423 {
424   return &default_format;
425 }
426
427 /* Set default format for variables created by transformations
428    and by DATA LIST {FREE,LIST}. */
429 void
430 set_format (const struct fmt_spec *default_format_)
431 {
432   default_format = *default_format_;
433 }
434
435 /* Are we in testing mode?  (e.g. --testing-mode command line
436    option) */
437 bool
438 get_testing_mode (void)
439 {
440   return testing_mode;
441 }
442
443 /* Set testing mode. */
444 void
445 set_testing_mode (bool testing_mode_)
446 {
447   testing_mode = testing_mode_;
448 }
449
450 /* Return the current algorithm setting */
451 enum behavior_mode
452 get_algorithm (void)
453 {
454   return *algorithm;
455 }
456
457 /* Set the algorithm option globally. */
458 void
459 set_algorithm (enum behavior_mode mode)
460 {
461   global_algorithm = mode;
462 }
463
464 /* Set the algorithm option for this command only */
465 void
466 set_cmd_algorithm (enum behavior_mode mode)
467 {
468   cmd_algorithm = mode;
469   algorithm = &cmd_algorithm;
470 }
471
472 /* Unset the algorithm option for this command */
473 void
474 unset_cmd_algorithm (void)
475 {
476   algorithm = &global_algorithm;
477 }
478
479 /* Get the current syntax setting */
480 enum behavior_mode
481 get_syntax (void)
482 {
483   return syntax;
484 }
485
486 /* Set the syntax option */
487 void
488 set_syntax (enum behavior_mode mode)
489 {
490   syntax = mode;
491 }