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