lexer: Change the functions for retrieving token strings.
[pspp-builds.git] / 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, T_EQUALS);
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, T_EQUALS);
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, T_EQUALS);
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, T_EQUALS);
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 char *s;
387
388   lex_match (lexer, T_EQUALS);
389
390   if ( !lex_force_string (lexer))
391     return 0;
392
393   s = lex_tokcstr (lexer);
394
395   /* First try this string as an encoding name */
396   if ( valid_encoding (s))
397     set_default_encoding (s);
398
399   /* Now try as a locale name (or alias) */
400   else if (set_encoding_from_locale (s))
401     {
402     }
403   else
404     {
405       msg (ME, _("%s is not a recognized encoding or locale name"), s);
406       return 0;
407     }
408
409   lex_get (lexer);
410
411   return 1;
412 }
413
414 static int
415 stc_custom_messages (struct lexer *lexer, struct dataset *ds UNUSED,
416                    struct cmd_set *cmd UNUSED, void *aux UNUSED)
417 {
418   return set_output_routing (lexer, SETTINGS_OUTPUT_NOTE);
419 }
420
421 static int
422 stc_custom_printback (struct lexer *lexer, struct dataset *ds UNUSED,
423                       struct cmd_set *cmd UNUSED, void *aux UNUSED)
424 {
425   return set_output_routing (lexer, SETTINGS_OUTPUT_SYNTAX);
426 }
427
428 static int
429 stc_custom_results (struct lexer *lexer, struct dataset *ds UNUSED,
430                     struct cmd_set *cmd UNUSED, void *aux UNUSED)
431 {
432   return set_output_routing (lexer, SETTINGS_OUTPUT_RESULT);
433 }
434
435 static int
436 stc_custom_seed (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
437 {
438   lex_match (lexer, T_EQUALS);
439   if (lex_match_id (lexer, "RANDOM"))
440     set_rng (time (0));
441   else
442     {
443       if (!lex_force_num (lexer))
444         return 0;
445       set_rng (lex_number (lexer));
446       lex_get (lexer);
447     }
448
449   return 1;
450 }
451
452 static int
453 stc_custom_width (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, "NARROW"))
457     settings_set_viewwidth (79);
458   else if (lex_match_id (lexer, "WIDE"))
459     settings_set_viewwidth (131);
460   else
461     {
462       if (!lex_force_int (lexer))
463         return 0;
464       if (lex_integer (lexer) < 40)
465         {
466           msg (SE, _("WIDTH must be at least 40."));
467           return 0;
468         }
469       settings_set_viewwidth (lex_integer (lexer));
470       lex_get (lexer);
471     }
472
473   return 1;
474 }
475
476 /* Parses FORMAT subcommand, which consists of a numeric format
477    specifier. */
478 static int
479 stc_custom_format (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
480 {
481   struct fmt_spec fmt;
482
483   lex_match (lexer, T_EQUALS);
484   if (!parse_format_specifier (lexer, &fmt))
485     return 0;
486
487   if (!fmt_check_output (&fmt))
488     return 0;
489   
490   if (fmt_is_string (fmt.type))
491     {
492       char str[FMT_STRING_LEN_MAX + 1];
493       msg (SE, _("FORMAT requires numeric output format as an argument.  "
494                  "Specified format %s is of type string."),
495            fmt_to_string (&fmt, str));
496       return 0;
497     }
498
499   settings_set_format (&fmt);
500   return 1;
501 }
502
503 static int
504 stc_custom_journal (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
505 {
506   lex_match (lexer, T_EQUALS);
507   if (lex_match_id (lexer, "ON") || lex_match_id (lexer, "YES"))
508     journal_enable ();
509   else if (lex_match_id (lexer, "OFF") || lex_match_id (lexer, "NO"))
510     journal_disable ();
511   else if (lex_is_string (lexer) || lex_token (lexer) == T_ID)
512     {
513       journal_set_file_name (lex_tokcstr (lexer));
514       lex_get (lexer);
515     }
516   else
517     {
518       lex_error (lexer, NULL);
519       return 0;
520     }
521   return 1;
522 }
523
524 static int
525 stc_custom_log (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
526 {
527   return stc_custom_journal (lexer, ds, cmd, aux);
528 }
529 \f
530 static char *
531 show_output_routing (enum settings_output_type type)
532 {
533   enum settings_output_devices devices;
534   const char *s;
535
536   devices = settings_get_output_routing (type);
537   if (devices & SETTINGS_DEVICE_LISTING)
538     s = devices & SETTINGS_DEVICE_TERMINAL ? "BOTH" : "LISTING";
539   else if (devices & SETTINGS_DEVICE_TERMINAL)
540     s = "TERMINAL";
541   else
542     s = "NONE";
543
544   return xstrdup (s);
545 }
546
547 static char *
548 show_blanks (const struct dataset *ds UNUSED)
549 {
550   return (settings_get_blanks () == SYSMIS
551           ? xstrdup ("SYSMIS")
552           : xasprintf ("%g", settings_get_blanks ()));
553 }
554
555 static void
556 format_cc (struct string *out, struct substring in, char grouping)
557 {
558   while (!ss_is_empty (in))
559     {
560       char c = ss_get_byte (&in);
561       if (c == grouping || c == '\'')
562         ds_put_byte (out, '\'');
563       else if (c == '"')
564         ds_put_byte (out, '"');
565       ds_put_byte (out, c);
566     }
567 }
568
569 static char *
570 show_cc (enum fmt_type type)
571 {
572   const struct fmt_number_style *cc = settings_get_style (type);
573   struct string out;
574
575   ds_init_empty (&out);
576   format_cc (&out, cc->neg_prefix, cc->grouping);
577   ds_put_byte (&out, cc->grouping);
578   format_cc (&out, cc->prefix, cc->grouping);
579   ds_put_byte (&out, cc->grouping);
580   format_cc (&out, cc->suffix, cc->grouping);
581   ds_put_byte (&out, cc->grouping);
582   format_cc (&out, cc->neg_suffix, cc->grouping);
583
584   return ds_cstr (&out);
585 }
586
587 static char *
588 show_cca (const struct dataset *ds UNUSED)
589 {
590   return show_cc (FMT_CCA);
591 }
592
593 static char *
594 show_ccb (const struct dataset *ds UNUSED)
595 {
596   return show_cc (FMT_CCB);
597 }
598
599 static char *
600 show_ccc (const struct dataset *ds UNUSED)
601 {
602   return show_cc (FMT_CCC);
603 }
604
605 static char *
606 show_ccd (const struct dataset *ds UNUSED)
607 {
608   return show_cc (FMT_CCD);
609 }
610
611 static char *
612 show_cce (const struct dataset *ds UNUSED)
613 {
614   return show_cc (FMT_CCE);
615 }
616
617 static char *
618 show_decimals (const struct dataset *ds UNUSED)
619 {
620   return xasprintf ("`%c'", settings_get_decimal_char (FMT_F));
621 }
622
623 static char *
624 show_endcmd (const struct dataset *ds UNUSED)
625 {
626   return xasprintf ("`%c'", settings_get_endcmd ());
627 }
628
629 static char *
630 show_errors (const struct dataset *ds UNUSED)
631 {
632   return show_output_routing (SETTINGS_OUTPUT_ERROR);
633 }
634
635 static char *
636 show_format (const struct dataset *ds UNUSED)
637 {
638   char str[FMT_STRING_LEN_MAX + 1];
639   return xstrdup (fmt_to_string (settings_get_format (), str));
640 }
641
642 static char *
643 show_length (const struct dataset *ds UNUSED)
644 {
645   return xasprintf ("%d", settings_get_viewlength ());
646 }
647
648 static char *
649 show_locale (const struct dataset *ds UNUSED)
650 {
651   return xstrdup (get_default_encoding ());
652 }
653
654 static char *
655 show_messages (const struct dataset *ds UNUSED)
656 {
657   return show_output_routing (SETTINGS_OUTPUT_NOTE);
658 }
659
660 static char *
661 show_printback (const struct dataset *ds UNUSED)
662 {
663   return show_output_routing (SETTINGS_OUTPUT_SYNTAX);
664 }
665
666 static char *
667 show_results (const struct dataset *ds UNUSED)
668 {
669   return show_output_routing (SETTINGS_OUTPUT_RESULT);
670 }
671
672 static char *
673 show_mxerrs (const struct dataset *ds UNUSED)
674 {
675   return xasprintf ("%d", settings_get_max_messages (MSG_S_ERROR));
676 }
677
678 static char *
679 show_mxloops (const struct dataset *ds UNUSED)
680 {
681   return xasprintf ("%d", settings_get_mxloops ());
682 }
683
684 static char *
685 show_mxwarns (const struct dataset *ds UNUSED)
686 {
687   return xasprintf ("%d", settings_get_max_messages (MSG_S_WARNING));
688 }
689
690 /* Returns a name for the given INTEGER_FORMAT value. */
691 static char *
692 show_integer_format (enum integer_format integer_format)
693 {
694   return xasprintf ("%s (%s)",
695                     (integer_format == INTEGER_MSB_FIRST ? "MSBFIRST"
696                      : integer_format == INTEGER_LSB_FIRST ? "LSBFIRST"
697                      : "VAX"),
698                     integer_format == INTEGER_NATIVE ? "NATIVE" : "nonnative");
699 }
700
701 /* Returns a name for the given FLOAT_FORMAT value. */
702 static char *
703 show_float_format (enum float_format float_format)
704 {
705   const char *format_name = "";
706
707   switch (float_format)
708     {
709     case FLOAT_IEEE_SINGLE_LE:
710       format_name = _("ISL (32-bit IEEE 754 single, little-endian)");
711       break;
712     case FLOAT_IEEE_SINGLE_BE:
713       format_name = _("ISB (32-bit IEEE 754 single, big-endian)");
714       break;
715     case FLOAT_IEEE_DOUBLE_LE:
716       format_name = _("IDL (64-bit IEEE 754 double, little-endian)");
717       break;
718     case FLOAT_IEEE_DOUBLE_BE:
719       format_name = _("IDB (64-bit IEEE 754 double, big-endian)");
720       break;
721
722     case FLOAT_VAX_F:
723       format_name = _("VF (32-bit VAX F, VAX-endian)");
724       break;
725     case FLOAT_VAX_D:
726       format_name = _("VD (64-bit VAX D, VAX-endian)");
727       break;
728     case FLOAT_VAX_G:
729       format_name = _("VG (64-bit VAX G, VAX-endian)");
730       break;
731
732     case FLOAT_Z_SHORT:
733       format_name = _("ZS (32-bit IBM Z hexadecimal short, big-endian)");
734       break;
735     case FLOAT_Z_LONG:
736       format_name = _("ZL (64-bit IBM Z hexadecimal long, big-endian)");
737       break;
738
739     case FLOAT_FP:
740     case FLOAT_HEX:
741       NOT_REACHED ();
742     }
743
744   return xasprintf ("%s (%s)", format_name,
745                     (float_format == FLOAT_NATIVE_DOUBLE
746                      ? "NATIVE" : "nonnative"));
747 }
748
749 static char *
750 show_rib (const struct dataset *ds UNUSED)
751 {
752   return show_integer_format (settings_get_input_integer_format ());
753 }
754
755 static char *
756 show_rrb (const struct dataset *ds UNUSED)
757 {
758   return show_float_format (settings_get_input_float_format ());
759 }
760
761 static char *
762 show_scompression (const struct dataset *ds UNUSED)
763 {
764   return xstrdup (settings_get_scompression () ? "ON" : "OFF");
765 }
766
767 static char *
768 show_undefined (const struct dataset *ds UNUSED)
769 {
770   return xstrdup (settings_get_undefined () ? "WARN" : "NOWARN");
771 }
772
773 static char *
774 show_weight (const struct dataset *ds)
775 {
776   const struct variable *var = dict_get_weight (dataset_dict (ds));
777   return xstrdup (var != NULL ? var_get_name (var) : "OFF");
778 }
779
780 static char *
781 show_wib (const struct dataset *ds UNUSED)
782 {
783   return show_integer_format (settings_get_output_integer_format ());
784 }
785
786 static char *
787 show_wrb (const struct dataset *ds UNUSED)
788 {
789   return show_float_format (settings_get_output_float_format ());
790 }
791
792 static char *
793 show_width (const struct dataset *ds UNUSED)
794 {
795   return xasprintf ("%d", settings_get_viewwidth ());
796 }
797
798 struct show_sbc
799   {
800     const char *name;
801     char *(*function) (const struct dataset *);
802   };
803
804 const struct show_sbc show_table[] =
805   {
806     {"BLANKS", show_blanks},
807     {"CCA", show_cca},
808     {"CCB", show_ccb},
809     {"CCC", show_ccc},
810     {"CCD", show_ccd},
811     {"CCE", show_cce},
812     {"DECIMALS", show_decimals},
813     {"ENDCMD", show_endcmd},
814     {"ERRORS", show_errors},
815     {"FORMAT", show_format},
816     {"LENGTH", show_length},
817     {"LOCALE", show_locale},
818     {"MESSAGES", show_messages},
819     {"MXERRS", show_mxerrs},
820     {"MXLOOPS", show_mxloops},
821     {"MXWARNS", show_mxwarns},
822     {"PRINTBACk", show_printback},
823     {"RESULTS", show_results},
824     {"RIB", show_rib},
825     {"RRB", show_rrb},
826     {"SCOMPRESSION", show_scompression},
827     {"UNDEFINED", show_undefined},
828     {"WEIGHT", show_weight},
829     {"WIB", show_wib},
830     {"WRB", show_wrb},
831     {"WIDTH", show_width},
832   };
833
834 static void
835 do_show (const struct dataset *ds, const struct show_sbc *sbc)
836 {
837   char *value = sbc->function (ds);
838   msg (SN, _("%s is %s."), sbc->name, value);
839   free (value);
840 }
841
842 static void
843 show_all (const struct dataset *ds)
844 {
845   size_t i;
846
847   for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
848     do_show (ds, &show_table[i]);
849 }
850
851 static void
852 show_all_cc (const struct dataset *ds)
853 {
854   int i;
855
856   for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
857     {
858       const struct show_sbc *sbc = &show_table[i];
859       if (!strncmp (sbc->name, "CC", 2))
860         do_show (ds, sbc);
861     }
862 }
863
864 static void
865 show_warranty (const struct dataset *ds UNUSED)
866 {
867   fputs (lack_of_warranty, stdout);
868 }
869
870 static void
871 show_copying (const struct dataset *ds UNUSED)
872 {
873   fputs (copyleft, stdout);
874 }
875
876 int
877 cmd_show (struct lexer *lexer, struct dataset *ds)
878 {
879   if (lex_token (lexer) == T_ENDCMD)
880     {
881       show_all (ds);
882       return CMD_SUCCESS;
883     }
884
885   do
886     {
887       if (lex_match (lexer, T_ALL))
888         show_all (ds);
889       else if (lex_match_id (lexer, "CC"))
890         show_all_cc (ds);
891       else if (lex_match_id (lexer, "WARRANTY"))
892         show_warranty (ds);
893       else if (lex_match_id (lexer, "COPYING"))
894         show_copying (ds);
895       else if (lex_token (lexer) == T_ID)
896         {
897           int i;
898
899           for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
900             {
901               const struct show_sbc *sbc = &show_table[i];
902               if (lex_match_id (lexer, sbc->name))
903                 {
904                   do_show (ds, sbc);
905                   goto found;
906                 }
907               }
908           lex_error (lexer, NULL);
909           return CMD_FAILURE;
910
911         found: ;
912         }
913       else
914         {
915           lex_error (lexer, NULL);
916           return CMD_FAILURE;
917         }
918
919       lex_match (lexer, T_SLASH);
920     }
921   while (lex_token (lexer) != T_ENDCMD);
922
923   return CMD_SUCCESS;
924 }
925 \f
926 #define MAX_SAVED_SETTINGS 5
927
928 static struct settings *saved_settings[MAX_SAVED_SETTINGS];
929 static int n_saved_settings;
930
931 int
932 cmd_preserve (struct lexer *lexer, struct dataset *ds UNUSED)
933 {
934   if (n_saved_settings < MAX_SAVED_SETTINGS)
935     {
936       saved_settings[n_saved_settings++] = settings_get ();
937       return lex_end_of_command (lexer);
938     }
939   else
940     {
941       msg (SE, _("Too many PRESERVE commands without a RESTORE: at most "
942                  "%d levels of saved settings are allowed."),
943            MAX_SAVED_SETTINGS);
944       return CMD_CASCADING_FAILURE;
945     }
946 }
947
948 int
949 cmd_restore (struct lexer *lexer, struct dataset *ds UNUSED)
950 {
951   if (n_saved_settings > 0)
952     {
953       struct settings *s = saved_settings[--n_saved_settings];
954       settings_set (s);
955       settings_destroy (s);
956       return lex_end_of_command (lexer);
957     }
958   else
959     {
960       msg (SE, _("RESTORE without matching PRESERVE."));
961       return CMD_FAILURE;
962     }
963 }
964
965 /*
966    Local Variables:
967    mode: c
968    End:
969 */