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