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