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