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