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