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