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