f90e3e14de3d8e675b7988879a79c3a2cca83b67
[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, _("%s must be at least 1MB"), "WORKSPACE");
198       else if (cmd.n_workspace[0] <= 0)
199         msg (SE, _("%s must be positive"), "WORKSPACE");
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, _("%s must be 1500 or later."), "EPOCH");
408           return 0;
409         }
410       settings_set_epoch (new_epoch);
411     }
412   else
413     {
414       lex_error (lexer, _("expecting %s or year"), "AUTOMATIC");
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, _("%s must be at least %d."), "LENGTH", 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, _("%s must be at least %d."), "WIDTH", 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, _("%s requires numeric output format as an argument.  "
567                  "Specified format %s is of type string."),
568            "FORMAT",
569            fmt_to_string (&fmt, str));
570       return 0;
571     }
572
573   settings_set_format (&fmt);
574   return 1;
575 }
576
577 static int
578 stc_custom_journal (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
579 {
580   lex_match (lexer, T_EQUALS);
581   if (lex_match_id (lexer, "ON") || lex_match_id (lexer, "YES"))
582     journal_enable ();
583   else if (lex_match_id (lexer, "OFF") || lex_match_id (lexer, "NO"))
584     journal_disable ();
585   else if (lex_is_string (lexer) || lex_token (lexer) == T_ID)
586     {
587       char *filename = utf8_to_filename (lex_tokcstr (lexer));
588       journal_set_file_name (filename);
589       free (filename);
590
591       lex_get (lexer);
592     }
593   else
594     {
595       lex_error (lexer, NULL);
596       return 0;
597     }
598   return 1;
599 }
600
601 static int
602 stc_custom_log (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
603 {
604   return stc_custom_journal (lexer, ds, cmd, aux);
605 }
606 \f
607 static char *
608 show_output_routing (enum settings_output_type type)
609 {
610   enum settings_output_devices devices;
611   const char *s;
612
613   devices = settings_get_output_routing (type);
614   if (devices & SETTINGS_DEVICE_LISTING)
615     s = devices & SETTINGS_DEVICE_TERMINAL ? "BOTH" : "LISTING";
616   else if (devices & SETTINGS_DEVICE_TERMINAL)
617     s = "TERMINAL";
618   else
619     s = "NONE";
620
621   return xstrdup (s);
622 }
623
624 static char *
625 show_blanks (const struct dataset *ds UNUSED)
626 {
627   return (settings_get_blanks () == SYSMIS
628           ? xstrdup ("SYSMIS")
629           : xasprintf ("%.*g", DBL_DIG + 1, settings_get_blanks ()));
630 }
631
632 static void
633 format_cc (struct string *out, const char *in, char grouping)
634 {
635   while (*in != '\0')
636     {
637       char c = *in++;
638       if (c == grouping || c == '\'')
639         ds_put_byte (out, '\'');
640       else if (c == '"')
641         ds_put_byte (out, '"');
642       ds_put_byte (out, c);
643     }
644 }
645
646 static char *
647 show_cc (enum fmt_type type)
648 {
649   const struct fmt_number_style *cc = settings_get_style (type);
650   struct string out;
651
652   ds_init_empty (&out);
653   format_cc (&out, cc->neg_prefix.s, cc->grouping);
654   ds_put_byte (&out, cc->grouping);
655   format_cc (&out, cc->prefix.s, cc->grouping);
656   ds_put_byte (&out, cc->grouping);
657   format_cc (&out, cc->suffix.s, cc->grouping);
658   ds_put_byte (&out, cc->grouping);
659   format_cc (&out, cc->neg_suffix.s, cc->grouping);
660
661   return ds_cstr (&out);
662 }
663
664 static char *
665 show_cca (const struct dataset *ds UNUSED)
666 {
667   return show_cc (FMT_CCA);
668 }
669
670 static char *
671 show_ccb (const struct dataset *ds UNUSED)
672 {
673   return show_cc (FMT_CCB);
674 }
675
676 static char *
677 show_ccc (const struct dataset *ds UNUSED)
678 {
679   return show_cc (FMT_CCC);
680 }
681
682 static char *
683 show_ccd (const struct dataset *ds UNUSED)
684 {
685   return show_cc (FMT_CCD);
686 }
687
688 static char *
689 show_cce (const struct dataset *ds UNUSED)
690 {
691   return show_cc (FMT_CCE);
692 }
693
694 static char *
695 show_decimals (const struct dataset *ds UNUSED)
696 {
697   return xasprintf ("`%c'", settings_get_decimal_char (FMT_F));
698 }
699
700 static char *
701 show_errors (const struct dataset *ds UNUSED)
702 {
703   return show_output_routing (SETTINGS_OUTPUT_ERROR);
704 }
705
706 static char *
707 show_format (const struct dataset *ds UNUSED)
708 {
709   char str[FMT_STRING_LEN_MAX + 1];
710   return xstrdup (fmt_to_string (settings_get_format (), str));
711 }
712
713 static char *
714 show_journal (const struct dataset *ds UNUSED)
715 {
716   return (journal_is_enabled ()
717           ? xasprintf ("\"%s\"", journal_get_file_name ())
718           : xstrdup ("disabled"));
719 }
720
721 static char *
722 show_length (const struct dataset *ds UNUSED)
723 {
724   return xasprintf ("%d", settings_get_viewlength ());
725 }
726
727 static char *
728 show_locale (const struct dataset *ds UNUSED)
729 {
730   return xstrdup (get_default_encoding ());
731 }
732
733 static char *
734 show_messages (const struct dataset *ds UNUSED)
735 {
736   return show_output_routing (SETTINGS_OUTPUT_NOTE);
737 }
738
739 static char *
740 show_printback (const struct dataset *ds UNUSED)
741 {
742   return show_output_routing (SETTINGS_OUTPUT_SYNTAX);
743 }
744
745 static char *
746 show_results (const struct dataset *ds UNUSED)
747 {
748   return show_output_routing (SETTINGS_OUTPUT_RESULT);
749 }
750
751 static char *
752 show_mxerrs (const struct dataset *ds UNUSED)
753 {
754   return xasprintf ("%d", settings_get_max_messages (MSG_S_ERROR));
755 }
756
757 static char *
758 show_mxloops (const struct dataset *ds UNUSED)
759 {
760   return xasprintf ("%d", settings_get_mxloops ());
761 }
762
763 static char *
764 show_mxwarns (const struct dataset *ds UNUSED)
765 {
766   return xasprintf ("%d", settings_get_max_messages (MSG_S_WARNING));
767 }
768
769 /* Returns a name for the given INTEGER_FORMAT value. */
770 static char *
771 show_integer_format (enum integer_format integer_format)
772 {
773   return xasprintf ("%s (%s)",
774                     (integer_format == INTEGER_MSB_FIRST ? "MSBFIRST"
775                      : integer_format == INTEGER_LSB_FIRST ? "LSBFIRST"
776                      : "VAX"),
777                     integer_format == INTEGER_NATIVE ? "NATIVE" : "nonnative");
778 }
779
780 /* Returns a name for the given FLOAT_FORMAT value. */
781 static char *
782 show_float_format (enum float_format float_format)
783 {
784   const char *format_name = "";
785
786   switch (float_format)
787     {
788     case FLOAT_IEEE_SINGLE_LE:
789       format_name = _("ISL (32-bit IEEE 754 single, little-endian)");
790       break;
791     case FLOAT_IEEE_SINGLE_BE:
792       format_name = _("ISB (32-bit IEEE 754 single, big-endian)");
793       break;
794     case FLOAT_IEEE_DOUBLE_LE:
795       format_name = _("IDL (64-bit IEEE 754 double, little-endian)");
796       break;
797     case FLOAT_IEEE_DOUBLE_BE:
798       format_name = _("IDB (64-bit IEEE 754 double, big-endian)");
799       break;
800
801     case FLOAT_VAX_F:
802       format_name = _("VF (32-bit VAX F, VAX-endian)");
803       break;
804     case FLOAT_VAX_D:
805       format_name = _("VD (64-bit VAX D, VAX-endian)");
806       break;
807     case FLOAT_VAX_G:
808       format_name = _("VG (64-bit VAX G, VAX-endian)");
809       break;
810
811     case FLOAT_Z_SHORT:
812       format_name = _("ZS (32-bit IBM Z hexadecimal short, big-endian)");
813       break;
814     case FLOAT_Z_LONG:
815       format_name = _("ZL (64-bit IBM Z hexadecimal long, big-endian)");
816       break;
817
818     case FLOAT_FP:
819     case FLOAT_HEX:
820       NOT_REACHED ();
821     }
822
823   return xasprintf ("%s (%s)", format_name,
824                     (float_format == FLOAT_NATIVE_DOUBLE
825                      ? "NATIVE" : "nonnative"));
826 }
827
828 static char *
829 show_rib (const struct dataset *ds UNUSED)
830 {
831   return show_integer_format (settings_get_input_integer_format ());
832 }
833
834 static char *
835 show_rrb (const struct dataset *ds UNUSED)
836 {
837   return show_float_format (settings_get_input_float_format ());
838 }
839
840 static char *
841 show_scompression (const struct dataset *ds UNUSED)
842 {
843   return xstrdup (settings_get_scompression () ? "ON" : "OFF");
844 }
845
846 static char *
847 show_undefined (const struct dataset *ds UNUSED)
848 {
849   return xstrdup (settings_get_undefined () ? "WARN" : "NOWARN");
850 }
851
852 static char *
853 show_weight (const struct dataset *ds)
854 {
855   const struct variable *var = dict_get_weight (dataset_dict (ds));
856   return xstrdup (var != NULL ? var_get_name (var) : "OFF");
857 }
858
859 static char *
860 show_wib (const struct dataset *ds UNUSED)
861 {
862   return show_integer_format (settings_get_output_integer_format ());
863 }
864
865 static char *
866 show_wrb (const struct dataset *ds UNUSED)
867 {
868   return show_float_format (settings_get_output_float_format ());
869 }
870
871 static char *
872 show_width (const struct dataset *ds UNUSED)
873 {
874   return xasprintf ("%d", settings_get_viewwidth ());
875 }
876
877 static char *
878 show_workspace (const struct dataset *ds UNUSED)
879 {
880   size_t ws = settings_get_workspace () / 1024L;
881   return xasprintf ("%zu", ws);
882 }
883
884 static char *
885 show_current_directory (const struct dataset *ds UNUSED)
886 {
887   char *buf = NULL;
888   char *wd = NULL;
889   size_t len = 256;
890
891   do
892     {
893       len <<= 1;
894       buf = xrealloc (buf, len);
895     } 
896   while (NULL == (wd = getcwd (buf, len)));
897
898   return wd;
899 }
900
901 static char *
902 show_tempdir (const struct dataset *ds UNUSED)
903 {
904   return strdup (temp_dir_name ());
905 }
906
907 static char *
908 show_version (const struct dataset *ds UNUSED)
909 {
910   return strdup (version);
911 }
912
913 static char *
914 show_system (const struct dataset *ds UNUSED)
915 {
916   return strdup (host_system);
917 }
918
919 static char *
920 show_n (const struct dataset *ds)
921 {
922   casenumber n;
923   size_t l;
924
925   const struct casereader *reader = dataset_source (ds);
926
927   if (reader == NULL)
928     return strdup (_("Unknown"));
929
930   n =  casereader_count_cases (reader);
931
932   return  asnprintf (NULL, &l, "%ld", n);
933 }
934
935
936 struct show_sbc
937   {
938     const char *name;
939     char *(*function) (const struct dataset *);
940   };
941
942 const struct show_sbc show_table[] =
943   {
944     {"BLANKS", show_blanks},
945     {"CCA", show_cca},
946     {"CCB", show_ccb},
947     {"CCC", show_ccc},
948     {"CCD", show_ccd},
949     {"CCE", show_cce},
950     {"DECIMALS", show_decimals},
951     {"DIRECTORY", show_current_directory},
952     {"ENVIRONMENT", show_system},
953     {"ERRORS", show_errors},
954     {"FORMAT", show_format},
955     {"JOURNAL", show_journal},
956     {"LENGTH", show_length},
957     {"LOCALE", show_locale},
958     {"MESSAGES", show_messages},
959     {"MXERRS", show_mxerrs},
960     {"MXLOOPS", show_mxloops},
961     {"MXWARNS", show_mxwarns},
962     {"N", show_n},
963     {"PRINTBACk", show_printback},
964     {"RESULTS", show_results},
965     {"RIB", show_rib},
966     {"RRB", show_rrb},
967     {"SCOMPRESSION", show_scompression},
968     {"TEMPDIR", show_tempdir},
969     {"UNDEFINED", show_undefined},
970     {"VERSION", show_version},
971     {"WEIGHT", show_weight},
972     {"WIB", show_wib},
973     {"WRB", show_wrb},
974     {"WIDTH", show_width},
975     {"WORKSPACE", show_workspace},
976   };
977
978 static void
979 do_show (const struct dataset *ds, const struct show_sbc *sbc)
980 {
981   char *value = sbc->function (ds);
982   msg (SN, _("%s is %s."), sbc->name, value);
983   free (value);
984 }
985
986 static void
987 show_all (const struct dataset *ds)
988 {
989   size_t i;
990
991   for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
992     do_show (ds, &show_table[i]);
993 }
994
995 static void
996 show_all_cc (const struct dataset *ds)
997 {
998   int i;
999
1000   for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
1001     {
1002       const struct show_sbc *sbc = &show_table[i];
1003       if (!strncmp (sbc->name, "CC", 2))
1004         do_show (ds, sbc);
1005     }
1006 }
1007
1008 static void
1009 show_warranty (const struct dataset *ds UNUSED)
1010 {
1011   fputs (lack_of_warranty, stdout);
1012 }
1013
1014 static void
1015 show_copying (const struct dataset *ds UNUSED)
1016 {
1017   fputs (copyleft, stdout);
1018 }
1019
1020
1021 int
1022 cmd_show (struct lexer *lexer, struct dataset *ds)
1023 {
1024   if (lex_token (lexer) == T_ENDCMD)
1025     {
1026       show_all (ds);
1027       return CMD_SUCCESS;
1028     }
1029
1030   do
1031     {
1032       if (lex_match (lexer, T_ALL))
1033         show_all (ds);
1034       else if (lex_match_id (lexer, "CC"))
1035         show_all_cc (ds);
1036       else if (lex_match_id (lexer, "WARRANTY"))
1037         show_warranty (ds);
1038       else if (lex_match_id (lexer, "COPYING") || lex_match_id (lexer, "LICENSE"))
1039         show_copying (ds);
1040       else if (lex_token (lexer) == T_ID)
1041         {
1042           int i;
1043
1044           for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
1045             {
1046               const struct show_sbc *sbc = &show_table[i];
1047               if (lex_match_id (lexer, sbc->name))
1048                 {
1049                   do_show (ds, sbc);
1050                   goto found;
1051                 }
1052               }
1053           lex_error (lexer, NULL);
1054           return CMD_FAILURE;
1055
1056         found: ;
1057         }
1058       else
1059         {
1060           lex_error (lexer, NULL);
1061           return CMD_FAILURE;
1062         }
1063
1064       lex_match (lexer, T_SLASH);
1065     }
1066   while (lex_token (lexer) != T_ENDCMD);
1067
1068   return CMD_SUCCESS;
1069 }
1070 \f
1071 #define MAX_SAVED_SETTINGS 5
1072
1073 static struct settings *saved_settings[MAX_SAVED_SETTINGS];
1074 static int n_saved_settings;
1075
1076 int
1077 cmd_preserve (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
1078 {
1079   if (n_saved_settings < MAX_SAVED_SETTINGS)
1080     {
1081       saved_settings[n_saved_settings++] = settings_get ();
1082       return CMD_SUCCESS;
1083     }
1084   else
1085     {
1086       msg (SE, _("Too many %s commands without a %s: at most "
1087                  "%d levels of saved settings are allowed."),
1088            "PRESERVE", "RESTORE",
1089            MAX_SAVED_SETTINGS);
1090       return CMD_CASCADING_FAILURE;
1091     }
1092 }
1093
1094 int
1095 cmd_restore (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
1096 {
1097   if (n_saved_settings > 0)
1098     {
1099       struct settings *s = saved_settings[--n_saved_settings];
1100       settings_set (s);
1101       settings_destroy (s);
1102       return CMD_SUCCESS;
1103     }
1104   else
1105     {
1106       msg (SE, _("%s without matching %s."), "RESTORE", "PRESERVE");
1107       return CMD_FAILURE;
1108     }
1109 }
1110
1111 /*
1112    Local Variables:
1113    mode: c
1114    End:
1115 */