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