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