3da12a6153bcbcc12f3ccf201324972268a5ef78
[pspp-builds.git] / src / language / utilities / set.q
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <stdio.h>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <time.h>
23
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/i18n.h"
39 #include "libpspp/integer-format.h"
40 #include "libpspp/message.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       char *filename = utf8_to_filename (lex_tokcstr (lexer));
497       journal_set_file_name (filename);
498       free (filename);
499
500       lex_get (lexer);
501     }
502   else
503     {
504       lex_error (lexer, NULL);
505       return 0;
506     }
507   return 1;
508 }
509
510 static int
511 stc_custom_log (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
512 {
513   return stc_custom_journal (lexer, ds, cmd, aux);
514 }
515 \f
516 static char *
517 show_output_routing (enum settings_output_type type)
518 {
519   enum settings_output_devices devices;
520   const char *s;
521
522   devices = settings_get_output_routing (type);
523   if (devices & SETTINGS_DEVICE_LISTING)
524     s = devices & SETTINGS_DEVICE_TERMINAL ? "BOTH" : "LISTING";
525   else if (devices & SETTINGS_DEVICE_TERMINAL)
526     s = "TERMINAL";
527   else
528     s = "NONE";
529
530   return xstrdup (s);
531 }
532
533 static char *
534 show_blanks (const struct dataset *ds UNUSED)
535 {
536   return (settings_get_blanks () == SYSMIS
537           ? xstrdup ("SYSMIS")
538           : xasprintf ("%g", settings_get_blanks ()));
539 }
540
541 static void
542 format_cc (struct string *out, const char *in, char grouping)
543 {
544   while (*in != '\0')
545     {
546       char c = *in++;
547       if (c == grouping || c == '\'')
548         ds_put_byte (out, '\'');
549       else if (c == '"')
550         ds_put_byte (out, '"');
551       ds_put_byte (out, c);
552     }
553 }
554
555 static char *
556 show_cc (enum fmt_type type)
557 {
558   const struct fmt_number_style *cc = settings_get_style (type);
559   struct string out;
560
561   ds_init_empty (&out);
562   format_cc (&out, cc->neg_prefix.s, cc->grouping);
563   ds_put_byte (&out, cc->grouping);
564   format_cc (&out, cc->prefix.s, cc->grouping);
565   ds_put_byte (&out, cc->grouping);
566   format_cc (&out, cc->suffix.s, cc->grouping);
567   ds_put_byte (&out, cc->grouping);
568   format_cc (&out, cc->neg_suffix.s, cc->grouping);
569
570   return ds_cstr (&out);
571 }
572
573 static char *
574 show_cca (const struct dataset *ds UNUSED)
575 {
576   return show_cc (FMT_CCA);
577 }
578
579 static char *
580 show_ccb (const struct dataset *ds UNUSED)
581 {
582   return show_cc (FMT_CCB);
583 }
584
585 static char *
586 show_ccc (const struct dataset *ds UNUSED)
587 {
588   return show_cc (FMT_CCC);
589 }
590
591 static char *
592 show_ccd (const struct dataset *ds UNUSED)
593 {
594   return show_cc (FMT_CCD);
595 }
596
597 static char *
598 show_cce (const struct dataset *ds UNUSED)
599 {
600   return show_cc (FMT_CCE);
601 }
602
603 static char *
604 show_decimals (const struct dataset *ds UNUSED)
605 {
606   return xasprintf ("`%c'", settings_get_decimal_char (FMT_F));
607 }
608
609 static char *
610 show_errors (const struct dataset *ds UNUSED)
611 {
612   return show_output_routing (SETTINGS_OUTPUT_ERROR);
613 }
614
615 static char *
616 show_format (const struct dataset *ds UNUSED)
617 {
618   char str[FMT_STRING_LEN_MAX + 1];
619   return xstrdup (fmt_to_string (settings_get_format (), str));
620 }
621
622 static char *
623 show_length (const struct dataset *ds UNUSED)
624 {
625   return xasprintf ("%d", settings_get_viewlength ());
626 }
627
628 static char *
629 show_locale (const struct dataset *ds UNUSED)
630 {
631   return xstrdup (get_default_encoding ());
632 }
633
634 static char *
635 show_messages (const struct dataset *ds UNUSED)
636 {
637   return show_output_routing (SETTINGS_OUTPUT_NOTE);
638 }
639
640 static char *
641 show_printback (const struct dataset *ds UNUSED)
642 {
643   return show_output_routing (SETTINGS_OUTPUT_SYNTAX);
644 }
645
646 static char *
647 show_results (const struct dataset *ds UNUSED)
648 {
649   return show_output_routing (SETTINGS_OUTPUT_RESULT);
650 }
651
652 static char *
653 show_mxerrs (const struct dataset *ds UNUSED)
654 {
655   return xasprintf ("%d", settings_get_max_messages (MSG_S_ERROR));
656 }
657
658 static char *
659 show_mxloops (const struct dataset *ds UNUSED)
660 {
661   return xasprintf ("%d", settings_get_mxloops ());
662 }
663
664 static char *
665 show_mxwarns (const struct dataset *ds UNUSED)
666 {
667   return xasprintf ("%d", settings_get_max_messages (MSG_S_WARNING));
668 }
669
670 /* Returns a name for the given INTEGER_FORMAT value. */
671 static char *
672 show_integer_format (enum integer_format integer_format)
673 {
674   return xasprintf ("%s (%s)",
675                     (integer_format == INTEGER_MSB_FIRST ? "MSBFIRST"
676                      : integer_format == INTEGER_LSB_FIRST ? "LSBFIRST"
677                      : "VAX"),
678                     integer_format == INTEGER_NATIVE ? "NATIVE" : "nonnative");
679 }
680
681 /* Returns a name for the given FLOAT_FORMAT value. */
682 static char *
683 show_float_format (enum float_format float_format)
684 {
685   const char *format_name = "";
686
687   switch (float_format)
688     {
689     case FLOAT_IEEE_SINGLE_LE:
690       format_name = _("ISL (32-bit IEEE 754 single, little-endian)");
691       break;
692     case FLOAT_IEEE_SINGLE_BE:
693       format_name = _("ISB (32-bit IEEE 754 single, big-endian)");
694       break;
695     case FLOAT_IEEE_DOUBLE_LE:
696       format_name = _("IDL (64-bit IEEE 754 double, little-endian)");
697       break;
698     case FLOAT_IEEE_DOUBLE_BE:
699       format_name = _("IDB (64-bit IEEE 754 double, big-endian)");
700       break;
701
702     case FLOAT_VAX_F:
703       format_name = _("VF (32-bit VAX F, VAX-endian)");
704       break;
705     case FLOAT_VAX_D:
706       format_name = _("VD (64-bit VAX D, VAX-endian)");
707       break;
708     case FLOAT_VAX_G:
709       format_name = _("VG (64-bit VAX G, VAX-endian)");
710       break;
711
712     case FLOAT_Z_SHORT:
713       format_name = _("ZS (32-bit IBM Z hexadecimal short, big-endian)");
714       break;
715     case FLOAT_Z_LONG:
716       format_name = _("ZL (64-bit IBM Z hexadecimal long, big-endian)");
717       break;
718
719     case FLOAT_FP:
720     case FLOAT_HEX:
721       NOT_REACHED ();
722     }
723
724   return xasprintf ("%s (%s)", format_name,
725                     (float_format == FLOAT_NATIVE_DOUBLE
726                      ? "NATIVE" : "nonnative"));
727 }
728
729 static char *
730 show_rib (const struct dataset *ds UNUSED)
731 {
732   return show_integer_format (settings_get_input_integer_format ());
733 }
734
735 static char *
736 show_rrb (const struct dataset *ds UNUSED)
737 {
738   return show_float_format (settings_get_input_float_format ());
739 }
740
741 static char *
742 show_scompression (const struct dataset *ds UNUSED)
743 {
744   return xstrdup (settings_get_scompression () ? "ON" : "OFF");
745 }
746
747 static char *
748 show_undefined (const struct dataset *ds UNUSED)
749 {
750   return xstrdup (settings_get_undefined () ? "WARN" : "NOWARN");
751 }
752
753 static char *
754 show_weight (const struct dataset *ds)
755 {
756   const struct variable *var = dict_get_weight (dataset_dict (ds));
757   return xstrdup (var != NULL ? var_get_name (var) : "OFF");
758 }
759
760 static char *
761 show_wib (const struct dataset *ds UNUSED)
762 {
763   return show_integer_format (settings_get_output_integer_format ());
764 }
765
766 static char *
767 show_wrb (const struct dataset *ds UNUSED)
768 {
769   return show_float_format (settings_get_output_float_format ());
770 }
771
772 static char *
773 show_width (const struct dataset *ds UNUSED)
774 {
775   return xasprintf ("%d", settings_get_viewwidth ());
776 }
777
778 struct show_sbc
779   {
780     const char *name;
781     char *(*function) (const struct dataset *);
782   };
783
784 const struct show_sbc show_table[] =
785   {
786     {"BLANKS", show_blanks},
787     {"CCA", show_cca},
788     {"CCB", show_ccb},
789     {"CCC", show_ccc},
790     {"CCD", show_ccd},
791     {"CCE", show_cce},
792     {"DECIMALS", show_decimals},
793     {"ERRORS", show_errors},
794     {"FORMAT", show_format},
795     {"LENGTH", show_length},
796     {"LOCALE", show_locale},
797     {"MESSAGES", show_messages},
798     {"MXERRS", show_mxerrs},
799     {"MXLOOPS", show_mxloops},
800     {"MXWARNS", show_mxwarns},
801     {"PRINTBACk", show_printback},
802     {"RESULTS", show_results},
803     {"RIB", show_rib},
804     {"RRB", show_rrb},
805     {"SCOMPRESSION", show_scompression},
806     {"UNDEFINED", show_undefined},
807     {"WEIGHT", show_weight},
808     {"WIB", show_wib},
809     {"WRB", show_wrb},
810     {"WIDTH", show_width},
811   };
812
813 static void
814 do_show (const struct dataset *ds, const struct show_sbc *sbc)
815 {
816   char *value = sbc->function (ds);
817   msg (SN, _("%s is %s."), sbc->name, value);
818   free (value);
819 }
820
821 static void
822 show_all (const struct dataset *ds)
823 {
824   size_t i;
825
826   for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
827     do_show (ds, &show_table[i]);
828 }
829
830 static void
831 show_all_cc (const struct dataset *ds)
832 {
833   int i;
834
835   for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
836     {
837       const struct show_sbc *sbc = &show_table[i];
838       if (!strncmp (sbc->name, "CC", 2))
839         do_show (ds, sbc);
840     }
841 }
842
843 static void
844 show_warranty (const struct dataset *ds UNUSED)
845 {
846   fputs (lack_of_warranty, stdout);
847 }
848
849 static void
850 show_copying (const struct dataset *ds UNUSED)
851 {
852   fputs (copyleft, stdout);
853 }
854
855 int
856 cmd_show (struct lexer *lexer, struct dataset *ds)
857 {
858   if (lex_token (lexer) == T_ENDCMD)
859     {
860       show_all (ds);
861       return CMD_SUCCESS;
862     }
863
864   do
865     {
866       if (lex_match (lexer, T_ALL))
867         show_all (ds);
868       else if (lex_match_id (lexer, "CC"))
869         show_all_cc (ds);
870       else if (lex_match_id (lexer, "WARRANTY"))
871         show_warranty (ds);
872       else if (lex_match_id (lexer, "COPYING"))
873         show_copying (ds);
874       else if (lex_token (lexer) == T_ID)
875         {
876           int i;
877
878           for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
879             {
880               const struct show_sbc *sbc = &show_table[i];
881               if (lex_match_id (lexer, sbc->name))
882                 {
883                   do_show (ds, sbc);
884                   goto found;
885                 }
886               }
887           lex_error (lexer, NULL);
888           return CMD_FAILURE;
889
890         found: ;
891         }
892       else
893         {
894           lex_error (lexer, NULL);
895           return CMD_FAILURE;
896         }
897
898       lex_match (lexer, T_SLASH);
899     }
900   while (lex_token (lexer) != T_ENDCMD);
901
902   return CMD_SUCCESS;
903 }
904 \f
905 #define MAX_SAVED_SETTINGS 5
906
907 static struct settings *saved_settings[MAX_SAVED_SETTINGS];
908 static int n_saved_settings;
909
910 int
911 cmd_preserve (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
912 {
913   if (n_saved_settings < MAX_SAVED_SETTINGS)
914     {
915       saved_settings[n_saved_settings++] = settings_get ();
916       return CMD_SUCCESS;
917     }
918   else
919     {
920       msg (SE, _("Too many PRESERVE commands without a RESTORE: at most "
921                  "%d levels of saved settings are allowed."),
922            MAX_SAVED_SETTINGS);
923       return CMD_CASCADING_FAILURE;
924     }
925 }
926
927 int
928 cmd_restore (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
929 {
930   if (n_saved_settings > 0)
931     {
932       struct settings *s = saved_settings[--n_saved_settings];
933       settings_set (s);
934       settings_destroy (s);
935       return CMD_SUCCESS;
936     }
937   else
938     {
939       msg (SE, _("RESTORE without matching PRESERVE."));
940       return CMD_FAILURE;
941     }
942 }
943
944 /*
945    Local Variables:
946    mode: c
947    End:
948 */