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