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