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