Add SET TVARS option. Closes bug #31566
[pspp] / src / language / utilities / set.q
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU 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, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <stdio.h>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <time.h>
23 #include <unistd.h>
24
25 #include "gl/vasnprintf.h"
26
27 #include "data/casereader.h"
28 #include "data/data-in.h"
29 #include "data/data-out.h"
30 #include "data/dataset.h"
31 #include "data/dictionary.h"
32 #include "data/format.h"
33 #include "data/settings.h"
34 #include "data/value.h"
35 #include "data/variable.h"
36 #include "language/command.h"
37 #include "language/lexer/format-parser.h"
38 #include "language/lexer/lexer.h"
39 #include "libpspp/compiler.h"
40 #include "libpspp/copyleft.h"
41 #include "libpspp/temp-file.h"
42 #include "libpspp/version.h"
43 #include "libpspp/float-format.h"
44 #include "libpspp/i18n.h"
45 #include "libpspp/integer-format.h"
46 #include "libpspp/message.h"
47 #include "math/random.h"
48 #include "output/driver.h"
49 #include "output/journal.h"
50
51 #if HAVE_LIBTERMCAP
52 #if HAVE_TERMCAP_H
53 #include <termcap.h>
54 #else /* !HAVE_TERMCAP_H */
55 int tgetent (char *, const char *);
56 int tgetnum (const char *);
57 #endif /* !HAVE_TERMCAP_H */
58 #endif /* !HAVE_LIBTERMCAP */
59
60 #include "xalloc.h"
61
62 #include "gettext.h"
63 #define _(msgid) gettext (msgid)
64
65 /* (specification)
66    "SET" (stc_):
67      blanks=custom;
68      block=string;
69      boxstring=string;
70      case=size:upper/uplow;
71      cca=string;
72      ccb=string;
73      ccc=string;
74      ccd=string;
75      cce=string;
76      compression=compress:on/off;
77      cpi=integer;
78      decimal=dec:dot/comma;
79      epoch=custom;
80      errors=custom;
81      format=custom;
82      headers=headers:no/yes/blank;
83      highres=hires:on/off;
84      histogram=string;
85      include=inc:on/off;
86      journal=custom;
87      log=custom;
88      length=custom;
89      locale=custom;
90      lowres=lores:auto/on/off;
91      lpi=integer;
92      menus=menus:standard/extended;
93      messages=custom;
94      mexpand=mexp:on/off;
95      miterate=integer;
96      mnest=integer;
97      mprint=mprint:on/off;
98      mxerrs=integer;
99      mxloops=integer;
100      mxmemory=integer;
101      mxwarns=integer;
102      printback=custom;
103      results=custom;
104      rib=rib:msbfirst/lsbfirst/vax/native;
105      rrb=rrb:native/isl/isb/idl/idb/vf/vd/vg/zs/zl;
106      safer=safe:on;
107      scompression=scompress:on/off;
108      scripttab=string;
109      seed=custom;
110      tnumbers=custom;
111      tvars=custom;
112      tb1=string;
113      tbfonts=string;
114      undefined=undef:warn/nowarn;
115      wib=wib:msbfirst/lsbfirst/vax/native;
116      wrb=wrb:native/isl/isb/idl/idb/vf/vd/vg/zs/zl;
117      width=custom;
118      workspace=integer;
119      xsort=xsort:yes/no.
120 */
121
122 /* (headers) */
123
124 /* (declarations) */
125
126 /* (functions) */
127
128 static enum integer_format stc_to_integer_format (int stc);
129 static enum float_format stc_to_float_format (int stc);
130
131 int
132 cmd_set (struct lexer *lexer, struct dataset *ds)
133 {
134   struct cmd_set cmd;
135
136   if (!parse_set (lexer, ds, &cmd, NULL))
137     {
138       free_set (&cmd);
139       return CMD_FAILURE;
140     }
141
142   if (cmd.sbc_cca)
143     settings_set_cc ( cmd.s_cca, FMT_CCA);
144   if (cmd.sbc_ccb)
145     settings_set_cc ( cmd.s_ccb, FMT_CCB);
146   if (cmd.sbc_ccc)
147     settings_set_cc ( cmd.s_ccc, FMT_CCC);
148   if (cmd.sbc_ccd)
149     settings_set_cc ( cmd.s_ccd, FMT_CCD);
150   if (cmd.sbc_cce)
151     settings_set_cc ( cmd.s_cce, FMT_CCE);
152
153   if (cmd.sbc_decimal)
154     settings_set_decimal_char (cmd.dec == STC_DOT ? '.' : ',');
155
156   if (cmd.sbc_include)
157     settings_set_include (cmd.inc == STC_ON);
158   if (cmd.sbc_mxerrs)
159     {
160       if (cmd.n_mxerrs[0] >= 1)
161         settings_set_max_messages (MSG_S_ERROR, cmd.n_mxerrs[0]);
162       else
163         msg (SE, _("%s must be at least 1."), "MXERRS");
164     }
165   if (cmd.sbc_mxloops)
166     {
167       if (cmd.n_mxloops[0] >= 1)
168         settings_set_mxloops (cmd.n_mxloops[0]);
169       else
170         msg (SE, _("%s must be at least 1."), "MXLOOPS");
171     }
172   if (cmd.sbc_mxwarns)
173     {
174       if (cmd.n_mxwarns[0] >= 0)
175         settings_set_max_messages (MSG_S_WARNING, cmd.n_mxwarns[0]);
176       else
177         msg (SE, _("%s must not be negative."), "MXWARNS");
178     }
179   if (cmd.sbc_rib)
180     settings_set_input_integer_format (stc_to_integer_format (cmd.rib));
181   if (cmd.sbc_rrb)
182     settings_set_input_float_format (stc_to_float_format (cmd.rrb));
183   if (cmd.sbc_safer)
184     settings_set_safer_mode ();
185   if (cmd.sbc_scompression)
186     settings_set_scompression (cmd.scompress == STC_ON);
187   if (cmd.sbc_undefined)
188     settings_set_undefined (cmd.undef == STC_WARN);
189   if (cmd.sbc_wib)
190     settings_set_output_integer_format (stc_to_integer_format (cmd.wib));
191   if (cmd.sbc_wrb)
192     settings_set_output_float_format (stc_to_float_format (cmd.wrb));
193   if (cmd.sbc_workspace)
194     {
195       if ( cmd.n_workspace[0] < 1024 && ! settings_get_testing_mode ())
196         msg (SE, _("WORKSPACE must be at least 1MB"));
197       else if (cmd.n_workspace[0] <= 0)
198         msg (SE, _("WORKSPACE must be positive"));
199       else
200         settings_set_workspace (cmd.n_workspace[0] * 1024L);
201     }
202
203   if (cmd.sbc_block)
204     msg (SW, _("%s is obsolete."), "BLOCK");
205   if (cmd.sbc_boxstring)
206     msg (SW, _("%s is obsolete."), "BOXSTRING");
207   if (cmd.sbc_cpi)
208     msg (SW, _("%s is obsolete."), "CPI");
209   if (cmd.sbc_histogram)
210     msg (SW, _("%s is obsolete."), "HISTOGRAM");
211   if (cmd.sbc_lpi)
212     msg (SW, _("%s is obsolete."), "LPI");
213   if (cmd.sbc_menus)
214     msg (SW, _("%s is obsolete."), "MENUS");
215   if (cmd.sbc_xsort)
216     msg (SW, _("%s is obsolete."), "XSORT");
217   if (cmd.sbc_mxmemory)
218     msg (SE, _("%s is obsolete."), "MXMEMORY");
219   if (cmd.sbc_scripttab)
220     msg (SE, _("%s is obsolete."), "SCRIPTTAB");
221   if (cmd.sbc_tbfonts)
222     msg (SW, _("%s is obsolete."), "TBFONTS");
223   if (cmd.sbc_tb1 && cmd.s_tb1)
224     msg (SW, _("%s is obsolete."), "TB1");
225
226   if (cmd.sbc_case)
227     msg (SW, _("%s is not yet implemented."), "CASE");
228
229   if (cmd.sbc_compression)
230     msg (SW, _("Active file compression is not implemented."));
231
232   free_set (&cmd);
233
234   return CMD_SUCCESS;
235 }
236
237 /* Returns the integer_format value corresponding to STC,
238    which should be the value of cmd.rib or cmd.wib. */
239 static enum integer_format
240 stc_to_integer_format (int stc)
241 {
242   return (stc == STC_MSBFIRST ? INTEGER_MSB_FIRST
243           : stc == STC_LSBFIRST ? INTEGER_LSB_FIRST
244           : stc == STC_VAX ? INTEGER_VAX
245           : INTEGER_NATIVE);
246 }
247
248 /* Returns the float_format value corresponding to STC,
249    which should be the value of cmd.rrb or cmd.wrb. */
250 static enum float_format
251 stc_to_float_format (int stc)
252 {
253   switch (stc)
254     {
255     case STC_NATIVE:
256       return FLOAT_NATIVE_DOUBLE;
257
258     case STC_ISL:
259       return FLOAT_IEEE_SINGLE_LE;
260     case STC_ISB:
261       return FLOAT_IEEE_SINGLE_BE;
262     case STC_IDL:
263       return FLOAT_IEEE_DOUBLE_LE;
264     case STC_IDB:
265       return FLOAT_IEEE_DOUBLE_BE;
266
267     case STC_VF:
268       return FLOAT_VAX_F;
269     case STC_VD:
270       return FLOAT_VAX_D;
271     case STC_VG:
272       return FLOAT_VAX_G;
273
274     case STC_ZS:
275       return FLOAT_Z_SHORT;
276     case STC_ZL:
277       return FLOAT_Z_LONG;
278     }
279
280   NOT_REACHED ();
281 }
282
283 static int
284 set_output_routing (struct lexer *lexer, enum settings_output_type type)
285 {
286   enum settings_output_devices devices;
287
288   lex_match (lexer, T_EQUALS);
289   if (lex_match_id (lexer, "ON") || lex_match_id (lexer, "BOTH"))
290     devices = SETTINGS_DEVICE_LISTING | SETTINGS_DEVICE_TERMINAL;
291   else if (lex_match_id (lexer, "TERMINAL"))
292     devices = SETTINGS_DEVICE_TERMINAL;
293   else if (lex_match_id (lexer, "LISTING"))
294     devices = SETTINGS_DEVICE_LISTING;
295   else if (lex_match_id (lexer, "OFF") || lex_match_id (lexer, "NONE"))
296     devices = 0;
297   else
298     {
299       lex_error (lexer, NULL);
300       return 0;
301     }
302
303   settings_set_output_routing (type, devices);
304
305   return 1;
306 }
307
308 /* Parses the BLANKS subcommand, which controls the value that
309    completely blank fields in numeric data imply.  X, Wnd: Syntax is
310    SYSMIS or a numeric value. */
311 static int
312 stc_custom_blanks (struct lexer *lexer,
313                    struct dataset *ds UNUSED,
314                    struct cmd_set *cmd UNUSED, void *aux UNUSED)
315 {
316   lex_match (lexer, T_EQUALS);
317   if (lex_match_id (lexer, "SYSMIS"))
318     {
319       lex_get (lexer);
320       settings_set_blanks (SYSMIS);
321     }
322   else
323     {
324       if (!lex_force_num (lexer))
325         return 0;
326       settings_set_blanks (lex_number (lexer));
327       lex_get (lexer);
328     }
329   return 1;
330 }
331
332 static int
333 stc_custom_tnumbers (struct lexer *lexer,
334                    struct dataset *ds UNUSED,
335                    struct cmd_set *cmd UNUSED, void *aux UNUSED)
336 {
337   lex_match (lexer, T_EQUALS);
338
339   if (lex_match_id (lexer, "VALUES"))
340     {
341       settings_set_value_style (SETTINGS_VAL_STYLE_VALUES);
342     }
343   else if (lex_match_id (lexer, "LABELS"))
344     {
345       settings_set_value_style (SETTINGS_VAL_STYLE_LABELS);
346     }
347   else if (lex_match_id (lexer, "BOTH"))
348     {
349       settings_set_value_style (SETTINGS_VAL_STYLE_BOTH);
350     }
351   else
352     {
353       lex_error_expecting (lexer, "VALUES", "LABELS", "BOTH", NULL_SENTINEL);
354       return 0;
355     }
356
357   return 1;
358 }
359
360
361 static int
362 stc_custom_tvars (struct lexer *lexer,
363                   struct dataset *ds UNUSED,
364                   struct cmd_set *cmd UNUSED, void *aux UNUSED)
365 {
366   lex_match (lexer, T_EQUALS);
367
368   if (lex_match_id (lexer, "NAMES"))
369     {
370       settings_set_var_style (SETTINGS_VAR_STYLE_NAMES);
371     }
372   else if (lex_match_id (lexer, "LABELS"))
373     {
374       settings_set_var_style (SETTINGS_VAR_STYLE_LABELS);
375     }
376   else if (lex_match_id (lexer, "BOTH"))
377     {
378       settings_set_var_style (SETTINGS_VAR_STYLE_BOTH);
379     }
380   else
381     {
382       lex_error_expecting (lexer, "NAMES", "LABELS", "BOTH", NULL_SENTINEL);
383       return 0;
384     }
385
386   return 1;
387 }
388
389
390 /* Parses the EPOCH subcommand, which controls the epoch used for
391    parsing 2-digit years. */
392 static int
393 stc_custom_epoch (struct lexer *lexer,
394                   struct dataset *ds UNUSED,
395                   struct cmd_set *cmd UNUSED, void *aux UNUSED)
396 {
397   lex_match (lexer, T_EQUALS);
398   if (lex_match_id (lexer, "AUTOMATIC"))
399     settings_set_epoch (-1);
400   else if (lex_is_integer (lexer))
401     {
402       int new_epoch = lex_integer (lexer);
403       lex_get (lexer);
404       if (new_epoch < 1500)
405         {
406           msg (SE, _("EPOCH must be 1500 or later."));
407           return 0;
408         }
409       settings_set_epoch (new_epoch);
410     }
411   else
412     {
413       lex_error (lexer, _("expecting AUTOMATIC or year"));
414       return 0;
415     }
416
417   return 1;
418 }
419
420 static int
421 stc_custom_errors (struct lexer *lexer, struct dataset *ds UNUSED,
422                    struct cmd_set *cmd UNUSED, void *aux UNUSED)
423 {
424   return set_output_routing (lexer, SETTINGS_OUTPUT_ERROR);
425 }
426
427 static int
428 stc_custom_length (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
429 {
430   int page_length;
431
432   lex_match (lexer, T_EQUALS);
433   if (lex_match_id (lexer, "NONE"))
434     page_length = -1;
435   else
436     {
437       if (!lex_force_int (lexer))
438         return 0;
439       if (lex_integer (lexer) < 1)
440         {
441           msg (SE, _("LENGTH must be at least 1."));
442           return 0;
443         }
444       page_length = lex_integer (lexer);
445       lex_get (lexer);
446     }
447
448   if (page_length != -1)
449     settings_set_viewlength (page_length);
450
451   return 1;
452 }
453
454 static int
455 stc_custom_locale (struct lexer *lexer, struct dataset *ds UNUSED,
456                    struct cmd_set *cmd UNUSED, void *aux UNUSED)
457 {
458   const char *s;
459
460   lex_match (lexer, T_EQUALS);
461
462   if ( !lex_force_string (lexer))
463     return 0;
464
465   s = lex_tokcstr (lexer);
466
467   /* First try this string as an encoding name */
468   if ( valid_encoding (s))
469     set_default_encoding (s);
470
471   /* Now try as a locale name (or alias) */
472   else if (set_encoding_from_locale (s))
473     {
474     }
475   else
476     {
477       msg (ME, _("%s is not a recognized encoding or locale name"), s);
478       return 0;
479     }
480
481   lex_get (lexer);
482
483   return 1;
484 }
485
486 static int
487 stc_custom_messages (struct lexer *lexer, struct dataset *ds UNUSED,
488                    struct cmd_set *cmd UNUSED, void *aux UNUSED)
489 {
490   return set_output_routing (lexer, SETTINGS_OUTPUT_NOTE);
491 }
492
493 static int
494 stc_custom_printback (struct lexer *lexer, struct dataset *ds UNUSED,
495                       struct cmd_set *cmd UNUSED, void *aux UNUSED)
496 {
497   return set_output_routing (lexer, SETTINGS_OUTPUT_SYNTAX);
498 }
499
500 static int
501 stc_custom_results (struct lexer *lexer, struct dataset *ds UNUSED,
502                     struct cmd_set *cmd UNUSED, void *aux UNUSED)
503 {
504   return set_output_routing (lexer, SETTINGS_OUTPUT_RESULT);
505 }
506
507 static int
508 stc_custom_seed (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
509 {
510   lex_match (lexer, T_EQUALS);
511   if (lex_match_id (lexer, "RANDOM"))
512     set_rng (time (0));
513   else
514     {
515       if (!lex_force_num (lexer))
516         return 0;
517       set_rng (lex_number (lexer));
518       lex_get (lexer);
519     }
520
521   return 1;
522 }
523
524 static int
525 stc_custom_width (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
526 {
527   lex_match (lexer, T_EQUALS);
528   if (lex_match_id (lexer, "NARROW"))
529     settings_set_viewwidth (79);
530   else if (lex_match_id (lexer, "WIDE"))
531     settings_set_viewwidth (131);
532   else
533     {
534       if (!lex_force_int (lexer))
535         return 0;
536       if (lex_integer (lexer) < 40)
537         {
538           msg (SE, _("WIDTH must be at least 40."));
539           return 0;
540         }
541       settings_set_viewwidth (lex_integer (lexer));
542       lex_get (lexer);
543     }
544
545   return 1;
546 }
547
548 /* Parses FORMAT subcommand, which consists of a numeric format
549    specifier. */
550 static int
551 stc_custom_format (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
552 {
553   struct fmt_spec fmt;
554
555   lex_match (lexer, T_EQUALS);
556   if (!parse_format_specifier (lexer, &fmt))
557     return 0;
558
559   if (!fmt_check_output (&fmt))
560     return 0;
561   
562   if (fmt_is_string (fmt.type))
563     {
564       char str[FMT_STRING_LEN_MAX + 1];
565       msg (SE, _("FORMAT requires numeric output format as an argument.  "
566                  "Specified format %s is of type string."),
567            fmt_to_string (&fmt, str));
568       return 0;
569     }
570
571   settings_set_format (&fmt);
572   return 1;
573 }
574
575 static int
576 stc_custom_journal (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
577 {
578   lex_match (lexer, T_EQUALS);
579   if (lex_match_id (lexer, "ON") || lex_match_id (lexer, "YES"))
580     journal_enable ();
581   else if (lex_match_id (lexer, "OFF") || lex_match_id (lexer, "NO"))
582     journal_disable ();
583   else if (lex_is_string (lexer) || lex_token (lexer) == T_ID)
584     {
585       char *filename = utf8_to_filename (lex_tokcstr (lexer));
586       journal_set_file_name (filename);
587       free (filename);
588
589       lex_get (lexer);
590     }
591   else
592     {
593       lex_error (lexer, NULL);
594       return 0;
595     }
596   return 1;
597 }
598
599 static int
600 stc_custom_log (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
601 {
602   return stc_custom_journal (lexer, ds, cmd, aux);
603 }
604 \f
605 static char *
606 show_output_routing (enum settings_output_type type)
607 {
608   enum settings_output_devices devices;
609   const char *s;
610
611   devices = settings_get_output_routing (type);
612   if (devices & SETTINGS_DEVICE_LISTING)
613     s = devices & SETTINGS_DEVICE_TERMINAL ? "BOTH" : "LISTING";
614   else if (devices & SETTINGS_DEVICE_TERMINAL)
615     s = "TERMINAL";
616   else
617     s = "NONE";
618
619   return xstrdup (s);
620 }
621
622 static char *
623 show_blanks (const struct dataset *ds UNUSED)
624 {
625   return (settings_get_blanks () == SYSMIS
626           ? xstrdup ("SYSMIS")
627           : xasprintf ("%g", settings_get_blanks ()));
628 }
629
630 static void
631 format_cc (struct string *out, const char *in, char grouping)
632 {
633   while (*in != '\0')
634     {
635       char c = *in++;
636       if (c == grouping || c == '\'')
637         ds_put_byte (out, '\'');
638       else if (c == '"')
639         ds_put_byte (out, '"');
640       ds_put_byte (out, c);
641     }
642 }
643
644 static char *
645 show_cc (enum fmt_type type)
646 {
647   const struct fmt_number_style *cc = settings_get_style (type);
648   struct string out;
649
650   ds_init_empty (&out);
651   format_cc (&out, cc->neg_prefix.s, cc->grouping);
652   ds_put_byte (&out, cc->grouping);
653   format_cc (&out, cc->prefix.s, cc->grouping);
654   ds_put_byte (&out, cc->grouping);
655   format_cc (&out, cc->suffix.s, cc->grouping);
656   ds_put_byte (&out, cc->grouping);
657   format_cc (&out, cc->neg_suffix.s, cc->grouping);
658
659   return ds_cstr (&out);
660 }
661
662 static char *
663 show_cca (const struct dataset *ds UNUSED)
664 {
665   return show_cc (FMT_CCA);
666 }
667
668 static char *
669 show_ccb (const struct dataset *ds UNUSED)
670 {
671   return show_cc (FMT_CCB);
672 }
673
674 static char *
675 show_ccc (const struct dataset *ds UNUSED)
676 {
677   return show_cc (FMT_CCC);
678 }
679
680 static char *
681 show_ccd (const struct dataset *ds UNUSED)
682 {
683   return show_cc (FMT_CCD);
684 }
685
686 static char *
687 show_cce (const struct dataset *ds UNUSED)
688 {
689   return show_cc (FMT_CCE);
690 }
691
692 static char *
693 show_decimals (const struct dataset *ds UNUSED)
694 {
695   return xasprintf ("`%c'", settings_get_decimal_char (FMT_F));
696 }
697
698 static char *
699 show_errors (const struct dataset *ds UNUSED)
700 {
701   return show_output_routing (SETTINGS_OUTPUT_ERROR);
702 }
703
704 static char *
705 show_format (const struct dataset *ds UNUSED)
706 {
707   char str[FMT_STRING_LEN_MAX + 1];
708   return xstrdup (fmt_to_string (settings_get_format (), str));
709 }
710
711 static char *
712 show_length (const struct dataset *ds UNUSED)
713 {
714   return xasprintf ("%d", settings_get_viewlength ());
715 }
716
717 static char *
718 show_locale (const struct dataset *ds UNUSED)
719 {
720   return xstrdup (get_default_encoding ());
721 }
722
723 static char *
724 show_messages (const struct dataset *ds UNUSED)
725 {
726   return show_output_routing (SETTINGS_OUTPUT_NOTE);
727 }
728
729 static char *
730 show_printback (const struct dataset *ds UNUSED)
731 {
732   return show_output_routing (SETTINGS_OUTPUT_SYNTAX);
733 }
734
735 static char *
736 show_results (const struct dataset *ds UNUSED)
737 {
738   return show_output_routing (SETTINGS_OUTPUT_RESULT);
739 }
740
741 static char *
742 show_mxerrs (const struct dataset *ds UNUSED)
743 {
744   return xasprintf ("%d", settings_get_max_messages (MSG_S_ERROR));
745 }
746
747 static char *
748 show_mxloops (const struct dataset *ds UNUSED)
749 {
750   return xasprintf ("%d", settings_get_mxloops ());
751 }
752
753 static char *
754 show_mxwarns (const struct dataset *ds UNUSED)
755 {
756   return xasprintf ("%d", settings_get_max_messages (MSG_S_WARNING));
757 }
758
759 /* Returns a name for the given INTEGER_FORMAT value. */
760 static char *
761 show_integer_format (enum integer_format integer_format)
762 {
763   return xasprintf ("%s (%s)",
764                     (integer_format == INTEGER_MSB_FIRST ? "MSBFIRST"
765                      : integer_format == INTEGER_LSB_FIRST ? "LSBFIRST"
766                      : "VAX"),
767                     integer_format == INTEGER_NATIVE ? "NATIVE" : "nonnative");
768 }
769
770 /* Returns a name for the given FLOAT_FORMAT value. */
771 static char *
772 show_float_format (enum float_format float_format)
773 {
774   const char *format_name = "";
775
776   switch (float_format)
777     {
778     case FLOAT_IEEE_SINGLE_LE:
779       format_name = _("ISL (32-bit IEEE 754 single, little-endian)");
780       break;
781     case FLOAT_IEEE_SINGLE_BE:
782       format_name = _("ISB (32-bit IEEE 754 single, big-endian)");
783       break;
784     case FLOAT_IEEE_DOUBLE_LE:
785       format_name = _("IDL (64-bit IEEE 754 double, little-endian)");
786       break;
787     case FLOAT_IEEE_DOUBLE_BE:
788       format_name = _("IDB (64-bit IEEE 754 double, big-endian)");
789       break;
790
791     case FLOAT_VAX_F:
792       format_name = _("VF (32-bit VAX F, VAX-endian)");
793       break;
794     case FLOAT_VAX_D:
795       format_name = _("VD (64-bit VAX D, VAX-endian)");
796       break;
797     case FLOAT_VAX_G:
798       format_name = _("VG (64-bit VAX G, VAX-endian)");
799       break;
800
801     case FLOAT_Z_SHORT:
802       format_name = _("ZS (32-bit IBM Z hexadecimal short, big-endian)");
803       break;
804     case FLOAT_Z_LONG:
805       format_name = _("ZL (64-bit IBM Z hexadecimal long, big-endian)");
806       break;
807
808     case FLOAT_FP:
809     case FLOAT_HEX:
810       NOT_REACHED ();
811     }
812
813   return xasprintf ("%s (%s)", format_name,
814                     (float_format == FLOAT_NATIVE_DOUBLE
815                      ? "NATIVE" : "nonnative"));
816 }
817
818 static char *
819 show_rib (const struct dataset *ds UNUSED)
820 {
821   return show_integer_format (settings_get_input_integer_format ());
822 }
823
824 static char *
825 show_rrb (const struct dataset *ds UNUSED)
826 {
827   return show_float_format (settings_get_input_float_format ());
828 }
829
830 static char *
831 show_scompression (const struct dataset *ds UNUSED)
832 {
833   return xstrdup (settings_get_scompression () ? "ON" : "OFF");
834 }
835
836 static char *
837 show_undefined (const struct dataset *ds UNUSED)
838 {
839   return xstrdup (settings_get_undefined () ? "WARN" : "NOWARN");
840 }
841
842 static char *
843 show_weight (const struct dataset *ds)
844 {
845   const struct variable *var = dict_get_weight (dataset_dict (ds));
846   return xstrdup (var != NULL ? var_get_name (var) : "OFF");
847 }
848
849 static char *
850 show_wib (const struct dataset *ds UNUSED)
851 {
852   return show_integer_format (settings_get_output_integer_format ());
853 }
854
855 static char *
856 show_wrb (const struct dataset *ds UNUSED)
857 {
858   return show_float_format (settings_get_output_float_format ());
859 }
860
861 static char *
862 show_width (const struct dataset *ds UNUSED)
863 {
864   return xasprintf ("%d", settings_get_viewwidth ());
865 }
866
867 static char *
868 show_current_directory (const struct dataset *ds UNUSED)
869 {
870   char *buf = NULL;
871   char *wd = NULL;
872   size_t len = 256;
873
874   do
875     {
876       len <<= 1;
877       buf = xrealloc (buf, len);
878     } 
879   while (NULL == (wd = getcwd (buf, len)));
880
881   return wd;
882 }
883
884 static char *
885 show_tempdir (const struct dataset *ds UNUSED)
886 {
887   return strdup (temp_dir_name ());
888 }
889
890 static char *
891 show_version (const struct dataset *ds UNUSED)
892 {
893   return strdup (version);
894 }
895
896 static char *
897 show_system (const struct dataset *ds UNUSED)
898 {
899   return strdup (host_system);
900 }
901
902 static char *
903 show_n (const struct dataset *ds)
904 {
905   casenumber n;
906   size_t l;
907
908   const struct casereader *reader = dataset_source (ds);
909
910   if (reader == NULL)
911     return strdup (_("Unknown"));
912
913   n =  casereader_count_cases (reader);
914
915   return  asnprintf (NULL, &l, "%ld", n);
916 }
917
918
919 struct show_sbc
920   {
921     const char *name;
922     char *(*function) (const struct dataset *);
923   };
924
925 const struct show_sbc show_table[] =
926   {
927     {"BLANKS", show_blanks},
928     {"CCA", show_cca},
929     {"CCB", show_ccb},
930     {"CCC", show_ccc},
931     {"CCD", show_ccd},
932     {"CCE", show_cce},
933     {"DECIMALS", show_decimals},
934     {"DIRECTORY", show_current_directory},
935     {"ENVIRONMENT", show_system},
936     {"ERRORS", show_errors},
937     {"FORMAT", show_format},
938     {"LENGTH", show_length},
939     {"LOCALE", show_locale},
940     {"MESSAGES", show_messages},
941     {"MXERRS", show_mxerrs},
942     {"MXLOOPS", show_mxloops},
943     {"MXWARNS", show_mxwarns},
944     {"N", show_n},
945     {"PRINTBACk", show_printback},
946     {"RESULTS", show_results},
947     {"RIB", show_rib},
948     {"RRB", show_rrb},
949     {"SCOMPRESSION", show_scompression},
950     {"TEMPDIR", show_tempdir},
951     {"UNDEFINED", show_undefined},
952     {"VERSION", show_version},
953     {"WEIGHT", show_weight},
954     {"WIB", show_wib},
955     {"WRB", show_wrb},
956     {"WIDTH", show_width},
957   };
958
959 static void
960 do_show (const struct dataset *ds, const struct show_sbc *sbc)
961 {
962   char *value = sbc->function (ds);
963   msg (SN, _("%s is %s."), sbc->name, value);
964   free (value);
965 }
966
967 static void
968 show_all (const struct dataset *ds)
969 {
970   size_t i;
971
972   for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
973     do_show (ds, &show_table[i]);
974 }
975
976 static void
977 show_all_cc (const struct dataset *ds)
978 {
979   int i;
980
981   for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
982     {
983       const struct show_sbc *sbc = &show_table[i];
984       if (!strncmp (sbc->name, "CC", 2))
985         do_show (ds, sbc);
986     }
987 }
988
989 static void
990 show_warranty (const struct dataset *ds UNUSED)
991 {
992   fputs (lack_of_warranty, stdout);
993 }
994
995 static void
996 show_copying (const struct dataset *ds UNUSED)
997 {
998   fputs (copyleft, stdout);
999 }
1000
1001
1002 int
1003 cmd_show (struct lexer *lexer, struct dataset *ds)
1004 {
1005   if (lex_token (lexer) == T_ENDCMD)
1006     {
1007       show_all (ds);
1008       return CMD_SUCCESS;
1009     }
1010
1011   do
1012     {
1013       if (lex_match (lexer, T_ALL))
1014         show_all (ds);
1015       else if (lex_match_id (lexer, "CC"))
1016         show_all_cc (ds);
1017       else if (lex_match_id (lexer, "WARRANTY"))
1018         show_warranty (ds);
1019       else if (lex_match_id (lexer, "COPYING") || lex_match_id (lexer, "LICENSE"))
1020         show_copying (ds);
1021       else if (lex_token (lexer) == T_ID)
1022         {
1023           int i;
1024
1025           for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
1026             {
1027               const struct show_sbc *sbc = &show_table[i];
1028               if (lex_match_id (lexer, sbc->name))
1029                 {
1030                   do_show (ds, sbc);
1031                   goto found;
1032                 }
1033               }
1034           lex_error (lexer, NULL);
1035           return CMD_FAILURE;
1036
1037         found: ;
1038         }
1039       else
1040         {
1041           lex_error (lexer, NULL);
1042           return CMD_FAILURE;
1043         }
1044
1045       lex_match (lexer, T_SLASH);
1046     }
1047   while (lex_token (lexer) != T_ENDCMD);
1048
1049   return CMD_SUCCESS;
1050 }
1051 \f
1052 #define MAX_SAVED_SETTINGS 5
1053
1054 static struct settings *saved_settings[MAX_SAVED_SETTINGS];
1055 static int n_saved_settings;
1056
1057 int
1058 cmd_preserve (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
1059 {
1060   if (n_saved_settings < MAX_SAVED_SETTINGS)
1061     {
1062       saved_settings[n_saved_settings++] = settings_get ();
1063       return CMD_SUCCESS;
1064     }
1065   else
1066     {
1067       msg (SE, _("Too many PRESERVE commands without a RESTORE: at most "
1068                  "%d levels of saved settings are allowed."),
1069            MAX_SAVED_SETTINGS);
1070       return CMD_CASCADING_FAILURE;
1071     }
1072 }
1073
1074 int
1075 cmd_restore (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
1076 {
1077   if (n_saved_settings > 0)
1078     {
1079       struct settings *s = saved_settings[--n_saved_settings];
1080       settings_set (s);
1081       settings_destroy (s);
1082       return CMD_SUCCESS;
1083     }
1084   else
1085     {
1086       msg (SE, _("RESTORE without matching PRESERVE."));
1087       return CMD_FAILURE;
1088     }
1089 }
1090
1091 /*
1092    Local Variables:
1093    mode: c
1094    End:
1095 */