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