Completely rewrite src/data/format.[ch], to achieve better
[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 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 /* Compress system files by default? */
246 bool 
247 get_scompression (void)
248 {
249   return scompress;
250 }
251
252 /* Set system file default compression. */
253 void
254 set_scompression (bool scompress_) 
255 {
256   scompress = scompress_;
257 }
258
259 /* Whether to warn on undefined values in numeric data. */
260 bool
261 get_undefined (void)
262 {
263   return undefined;
264 }
265
266 /* Set whether to warn on undefined values. */
267 void
268 set_undefined (bool undefined_) 
269 {
270   undefined = undefined_;
271 }
272
273 /* The value that blank numeric fields are set to when read in. */
274 double
275 get_blanks (void)
276 {
277   return blanks;
278 }
279
280 /* Set the value that blank numeric fields are set to when read
281    in. */
282 void
283 set_blanks (double blanks_) 
284 {
285   blanks = blanks_;
286 }
287
288 /* Maximum number of warnings + errors. */
289 int
290 get_mxwarns (void)
291 {  
292   return mxwarns;
293 }
294
295 /* Sets maximum number of warnings + errors. */
296 void
297 set_mxwarns (int mxwarns_) 
298 {
299   mxwarns = mxwarns_;
300 }
301
302 /* Maximum number of errors. */
303 int
304 get_mxerrs (void)
305 {
306   return mxerrs;
307 }
308
309 /* Sets maximum number of errors. */
310 void
311 set_mxerrs (int mxerrs_) 
312 {
313   mxerrs = mxerrs_;
314 }
315
316 /* Whether commands are written to the display. */
317 bool
318 get_printback (void)
319 {
320   return printback;
321 }
322
323 /* Sets whether commands are written to the display. */
324 void
325 set_printback (bool printback_) 
326 {
327   printback = printback_;
328 }
329
330 /* Independent of get_printback, controls whether the commands
331    generated by macro invocations are displayed. */
332 bool
333 get_mprint (void)
334 {
335   return mprint;
336 }
337
338 /* Sets whether the commands generated by macro invocations are
339    displayed. */
340 void
341 set_mprint (bool mprint_) 
342 {
343   mprint = mprint_;
344 }
345
346 /* Implied limit of unbounded loop. */
347 int
348 get_mxloops (void)
349 {
350   return mxloops;
351 }
352
353 /* Set implied limit of unbounded loop. */
354 void
355 set_mxloops (int mxloops_) 
356 {
357   mxloops = mxloops_;
358 }
359
360 /* Whether a blank line is a command terminator. */
361 bool
362 get_nulline (void)
363 {
364   return nulline;
365 }
366
367 /* Set whether a blank line is a command terminator. */
368 void
369 set_nulline (bool nulline_)
370 {
371   nulline = nulline_;
372 }
373
374 /* The character used to terminate commands. */
375 char
376 get_endcmd (void)
377 {
378   return endcmd;
379 }
380
381 /* Set the character used to terminate commands. */
382 void
383 set_endcmd (char endcmd_) 
384 {
385   endcmd = endcmd_;
386 }
387
388 /* Approximate maximum amount of memory to use for cases, in
389    bytes. */
390 size_t
391 get_workspace (void)
392 {
393   return workspace;
394 }
395
396 /* Set approximate maximum amount of memory to use for cases, in
397    bytes. */
398
399 void
400 set_workspace (size_t workspace_) 
401 {
402   workspace = workspace_;
403 }
404
405 /* Default format for variables created by transformations and by
406    DATA LIST {FREE,LIST}. */
407 const struct fmt_spec *
408 get_format (void)
409
410   return &default_format;
411 }
412
413 /* Set default format for variables created by transformations
414    and by DATA LIST {FREE,LIST}. */
415 void
416 set_format (const struct fmt_spec *default_format_) 
417 {
418   default_format = *default_format_;
419 }
420
421 /* Are we in testing mode?  (e.g. --testing-mode command line
422    option) */
423 bool
424 get_testing_mode (void) 
425 {
426   return testing_mode;
427 }
428
429 /* Set testing mode. */
430 void
431 set_testing_mode (bool testing_mode_) 
432 {
433   testing_mode = testing_mode_;
434 }
435
436 /* Return the current algorithm setting */
437 enum behavior_mode
438 get_algorithm (void)
439 {
440   return *algorithm;
441 }
442
443 /* Set the algorithm option globally. */
444 void 
445 set_algorithm (enum behavior_mode mode)
446 {
447   global_algorithm = mode;
448 }
449
450 /* Set the algorithm option for this command only */
451 void 
452 set_cmd_algorithm (enum behavior_mode mode)
453 {
454   cmd_algorithm = mode; 
455   algorithm = &cmd_algorithm;
456 }
457
458 /* Unset the algorithm option for this command */
459 void
460 unset_cmd_algorithm (void)
461 {
462   algorithm = &global_algorithm;
463 }
464
465 /* Get the current syntax setting */
466 enum behavior_mode
467 get_syntax (void)
468 {
469   return syntax;
470 }
471
472 /* Set the syntax option */
473 void 
474 set_syntax (enum behavior_mode mode)
475 {
476   syntax = mode;
477 }