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