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