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