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