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