Rewrite and improve formatted output routines.
[pspp-builds.git] / src / language / utilities / set.q
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21
22 #include <stdio.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <time.h>
26
27 #include <data/data-out.h>
28 #include <data/dictionary.h>
29 #include <data/format.h>
30 #include <data/procedure.h>
31 #include <data/settings.h>
32 #include <data/variable.h>
33 #include <language/command.h>
34 #include <language/lexer/format-parser.h>
35 #include <language/lexer/lexer.h>
36 #include <language/line-buffer.h>
37 #include <libpspp/alloc.h>
38 #include <libpspp/compiler.h>
39 #include <libpspp/copyleft.h>
40 #include <libpspp/float-format.h>
41 #include <libpspp/integer-format.h>
42 #include <libpspp/magic.h>
43 #include <libpspp/message.h>
44 #include <math/random.h>
45 #include <output/output.h>
46
47 #if HAVE_LIBTERMCAP
48 #if HAVE_TERMCAP_H
49 #include <termcap.h>
50 #else /* !HAVE_TERMCAP_H */
51 int tgetent (char *, const char *);
52 int tgetnum (const char *);
53 #endif /* !HAVE_TERMCAP_H */
54 #endif /* !HAVE_LIBTERMCAP */
55
56 #include "gettext.h"
57 #define _(msgid) gettext (msgid)
58
59 /* (specification)
60    "SET" (stc_):
61      blanks=custom;
62      block=string "x==1" "one character long";
63      boxstring=string "x==3 || x==11" "3 or 11 characters long";
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 "x>0" "%s must be greater than 0";
72      cprompt=string;
73      decimal=dec:dot/comma;
74      disk=custom;
75      dprompt=string;
76      echo=echo:on/off;
77      endcmd=string "x==1" "one character long";
78      epoch=custom;
79      errorbreak=errbrk:on/off;
80      errors=errors:terminal/listing/both/on/none/off;
81      format=custom;
82      headers=headers:no/yes/blank;
83      highres=hires:on/off;
84      histogram=string "x==1" "one character long";
85      include=inc:on/off;
86      journal=custom;
87      length=custom;
88      listing=custom;
89      lowres=lores:auto/on/off;
90      lpi=integer "x>0" "%s must be greater than 0";
91      menus=menus:standard/extended;
92      messages=messages:on/off/terminal/listing/both/on/none/off;
93      mexpand=mexp:on/off;
94      miterate=integer "x>0" "%s must be greater than 0";
95      mnest=integer "x>0" "%s must be greater than 0";
96      mprint=mprint:on/off;
97      mxerrs=integer "x >= 1" "%s must be at least 1";
98      mxloops=integer "x >=1" "%s must be at least 1";
99      mxmemory=integer;
100      mxwarns=integer;
101      nulline=null:on/off;
102      printback=prtbck:on/off;
103      prompt=string;
104      results=res:on/off/terminal/listing/both/on/none/off;
105      safer=safe:on;
106      scompression=scompress:on/off;
107      scripttab=string "x==1" "one character long";
108      seed=custom;
109      tb1=string "x==3 || x==11" "3 or 11 characters long";
110      tbfonts=string;
111      undefined=undef:warn/nowarn;
112      wib=wib:msbfirst/lsbfirst/vax/native;
113      wrb=wrb:native/isl/isb/idl/idb/vf/vd/vg/zs/zl;
114      width=custom;
115      workspace=integer "x>=1024" "%s must be at least 1 MB";
116      xsort=xsort:yes/no.
117 */
118
119 /* (headers) */
120
121 /* (declarations) */
122
123 /* (functions) */
124
125 static bool do_cc (const char *cc_string, enum fmt_type);
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 dataset *ds)
131 {
132   struct cmd_set cmd;
133
134   if (!parse_set (ds, &cmd, NULL))
135     return CMD_FAILURE;
136
137   if (cmd.sbc_cca)
138     do_cc (cmd.s_cca, FMT_CCA);
139   if (cmd.sbc_ccb)
140     do_cc (cmd.s_ccb, FMT_CCB);
141   if (cmd.sbc_ccc)
142     do_cc (cmd.s_ccc, FMT_CCC);
143   if (cmd.sbc_ccd)
144     do_cc (cmd.s_ccd, FMT_CCD);
145   if (cmd.sbc_cce)
146     do_cc (cmd.s_cce, FMT_CCE);
147
148   if (cmd.sbc_prompt)
149     getl_set_prompt (GETL_PROMPT_FIRST, cmd.s_prompt);
150   if (cmd.sbc_cprompt)
151     getl_set_prompt (GETL_PROMPT_LATER, cmd.s_cprompt);
152   if (cmd.sbc_dprompt)
153     getl_set_prompt (GETL_PROMPT_DATA, cmd.s_dprompt);
154
155   if (cmd.sbc_decimal)
156     fmt_set_decimal (cmd.dec == STC_DOT ? '.' : ',');
157   if (cmd.sbc_echo)
158     set_echo (cmd.echo == STC_ON);
159   if (cmd.sbc_endcmd)
160     set_endcmd (cmd.s_endcmd[0]);
161   if (cmd.sbc_errorbreak)
162     set_errorbreak (cmd.errbrk == STC_ON);
163   if (cmd.sbc_errors)
164     {
165       bool both = cmd.errors == STC_BOTH || cmd.errors == STC_ON;
166       set_error_routing_to_terminal (cmd.errors == STC_TERMINAL || both);
167       set_error_routing_to_listing (cmd.errors == STC_LISTING || both);
168     }
169   if (cmd.sbc_include)
170     set_include (cmd.inc == STC_ON);
171   if (cmd.sbc_mxerrs)
172     set_mxerrs (cmd.n_mxerrs[0]);
173   if (cmd.sbc_mxwarns)
174     set_mxwarns (cmd.n_mxwarns[0]);
175   if (cmd.sbc_nulline)
176     set_nulline (cmd.null == STC_ON);
177   if (cmd.sbc_safer)
178     set_safer_mode ();
179   if (cmd.sbc_scompression)
180     set_scompression (cmd.scompress == STC_ON);
181   if (cmd.sbc_undefined)
182     set_undefined (cmd.undef == STC_WARN);
183   if (cmd.sbc_wib)
184     data_out_set_integer_format (stc_to_integer_format (cmd.wib));
185   if (cmd.sbc_wrb)
186     data_out_set_float_format (stc_to_float_format (cmd.wrb));
187   if (cmd.sbc_workspace)
188     set_workspace (cmd.n_workspace[0] * 1024L);
189
190   if (cmd.sbc_block)
191     msg (SW, _("%s is obsolete."), "BLOCK");
192   if (cmd.sbc_boxstring)
193     msg (SW, _("%s is obsolete."), "BOXSTRING");
194   if (cmd.sbc_histogram)
195     msg (SW, _("%s is obsolete."), "HISTOGRAM");
196   if (cmd.sbc_menus)
197     msg (SW, _("%s is obsolete."), "MENUS");
198   if (cmd.sbc_xsort)
199     msg (SW, _("%s is obsolete."), "XSORT");
200   if (cmd.sbc_mxmemory)
201     msg (SE, _("%s is obsolete."), "MXMEMORY");
202   if (cmd.sbc_scripttab)
203     msg (SE, _("%s is obsolete."), "SCRIPTTAB");
204   if (cmd.sbc_tbfonts)
205     msg (SW, _("%s is obsolete."), "TBFONTS");
206   if (cmd.sbc_tb1 && cmd.s_tb1)
207     msg (SW, _("%s is obsolete."), "TB1");
208
209   if (cmd.sbc_case)
210     msg (SW, _("%s is not implemented."), "CASE");
211
212   if (cmd.sbc_compression)
213     msg (SW, _("Active file compression is not implemented."));
214
215   return CMD_SUCCESS;
216 }
217
218 /* Returns the integer_format value corresponding to STC,
219    which should be the value of cmd.rib or cmd.wib. */
220 static enum integer_format
221 stc_to_integer_format (int stc) 
222 {
223   return (stc == STC_MSBFIRST ? INTEGER_MSB_FIRST
224           : stc == STC_LSBFIRST ? INTEGER_LSB_FIRST
225           : stc == STC_VAX ? INTEGER_VAX
226           : INTEGER_NATIVE);
227 }
228
229 /* Returns the float_format value corresponding to STC,
230    which should be the value of cmd.rrb or cmd.wrb. */
231 static enum float_format
232 stc_to_float_format (int stc) 
233 {
234   switch (stc) 
235     {
236     case STC_NATIVE:
237       return FLOAT_NATIVE_DOUBLE;
238
239     case STC_ISL:
240       return FLOAT_IEEE_SINGLE_LE;
241     case STC_ISB:
242       return FLOAT_IEEE_SINGLE_BE;
243     case STC_IDL:
244       return FLOAT_IEEE_DOUBLE_LE;
245     case STC_IDB:
246       return FLOAT_IEEE_DOUBLE_BE;
247
248     case STC_VF:
249       return FLOAT_VAX_F;
250     case STC_VD:
251       return FLOAT_VAX_D;
252     case STC_VG:
253       return FLOAT_VAX_G;
254
255     case STC_ZS:
256       return FLOAT_Z_SHORT;
257     case STC_ZL:
258       return FLOAT_Z_LONG;
259     }
260
261   NOT_REACHED ();
262 }
263
264 /* Find the grouping characters in CC_STRING and set CC's
265    grouping and decimal members appropriately.  Returns true if
266    successful, false otherwise. */
267 static bool
268 find_cc_separators (const char *cc_string, struct fmt_number_style *cc)
269 {
270   const char *sp;
271   int comma_cnt, dot_cnt;
272   
273   /* Count commas and periods.  There must be exactly three of
274      one or the other, except that an apostrophe escapes a
275      following comma or period. */
276   comma_cnt = dot_cnt = 0;
277   for (sp = cc_string; *sp; sp++)
278     if (*sp == ',')
279       comma_cnt++;
280     else if (*sp == '.')
281       dot_cnt++;
282     else if (*sp == '\'' && (sp[1] == '.' || sp[1] == ',' || sp[1] == '\''))
283       sp++;
284   
285   if ((comma_cnt == 3) == (dot_cnt == 3))
286     return false;
287
288   if (comma_cnt == 3)
289     {
290       cc->decimal = '.';
291       cc->grouping = ',';
292     }
293   else
294     {
295       cc->decimal = ',';
296       cc->grouping = '.';
297     }
298   return true;
299 }
300
301 /* Extracts a token from IN into a newly allocated AFFIX.  Tokens
302    are delimited by GROUPING.  The token is truncated to at most
303    FMT_STYLE_AFFIX_MAX characters.  Returns the first character
304    following the token. */
305 static const char *
306 extract_cc_token (const char *in, int grouping, struct substring *affix) 
307 {
308   size_t ofs = 0;
309   ss_alloc_uninit (affix, FMT_STYLE_AFFIX_MAX);
310   for (; *in != '\0' && *in != grouping; in++) 
311     {
312       if (*in == '\'' && in[1] == grouping)
313         in++;
314       if (ofs < FMT_STYLE_AFFIX_MAX) 
315         ss_data (*affix)[ofs++] = *in;
316     }
317   affix->length = ofs;
318
319   if (*in == grouping)
320     in++;
321   return in;
322 }
323
324 /* Sets custom currency specifier CC having name CC_NAME ('A' through
325    'E') to correspond to the settings in CC_STRING. */
326 static bool
327 do_cc (const char *cc_string, enum fmt_type type)
328 {
329   struct fmt_number_style *cc = fmt_number_style_create ();
330   
331   /* Determine separators. */
332   if (!find_cc_separators (cc_string, cc)) 
333     {
334       fmt_number_style_destroy (cc);
335       msg (SE, _("%s: Custom currency string `%s' does not contain "
336                  "exactly three periods or commas (or it contains both)."),
337            fmt_name (type), cc_string);
338       return false;
339     }
340   
341   cc_string = extract_cc_token (cc_string, cc->grouping, &cc->neg_prefix);
342   cc_string = extract_cc_token (cc_string, cc->grouping, &cc->prefix);
343   cc_string = extract_cc_token (cc_string, cc->grouping, &cc->suffix);
344   cc_string = extract_cc_token (cc_string, cc->grouping, &cc->neg_suffix);
345
346   fmt_set_style (type, cc);
347   
348   return true;
349 }
350
351 /* Parses the BLANKS subcommand, which controls the value that
352    completely blank fields in numeric data imply.  X, Wnd: Syntax is
353    SYSMIS or a numeric value. */
354 static int
355 stc_custom_blanks (struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
356 {
357   lex_match ('=');
358   if ((token == T_ID && lex_id_match ("SYSMIS", tokid)))
359     {
360       lex_get ();
361       set_blanks (SYSMIS);
362     }
363   else
364     {
365       if (!lex_force_num ())
366         return 0;
367       set_blanks (lex_number ());
368       lex_get ();
369     }
370   return 1;
371 }
372
373 /* Parses the EPOCH subcommand, which controls the epoch used for
374    parsing 2-digit years. */
375 static int
376 stc_custom_epoch (struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED) 
377 {
378   lex_match ('=');
379   if (lex_match_id ("AUTOMATIC"))
380     set_epoch (-1);
381   else if (lex_is_integer ()) 
382     {
383       int new_epoch = lex_integer ();
384       lex_get ();
385       if (new_epoch < 1500) 
386         {
387           msg (SE, _("EPOCH must be 1500 or later."));
388           return 0;
389         }
390       set_epoch (new_epoch);
391     }
392   else 
393     {
394       lex_error (_("expecting AUTOMATIC or year"));
395       return 0;
396     }
397
398   return 1;
399 }
400
401 static int
402 stc_custom_length (struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
403 {
404   int page_length;
405
406   lex_match ('=');
407   if (lex_match_id ("NONE"))
408     page_length = -1;
409   else
410     {
411       if (!lex_force_int ())
412         return 0;
413       if (lex_integer () < 1)
414         {
415           msg (SE, _("LENGTH must be at least 1."));
416           return 0;
417         }
418       page_length = lex_integer ();
419       lex_get ();
420     }
421
422   if (page_length != -1) 
423     set_viewlength (page_length);
424
425   return 1;
426 }
427
428 static int
429 stc_custom_seed (struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
430 {
431   lex_match ('=');
432   if (lex_match_id ("RANDOM"))
433     set_rng (time (0));
434   else
435     {
436       if (!lex_force_num ())
437         return 0;
438       set_rng (lex_number ());
439       lex_get ();
440     }
441
442   return 1;
443 }
444
445 static int
446 stc_custom_width (struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
447 {
448   lex_match ('=');
449   if (lex_match_id ("NARROW"))
450     set_viewwidth (79);
451   else if (lex_match_id ("WIDE"))
452     set_viewwidth (131);
453   else
454     {
455       if (!lex_force_int ())
456         return 0;
457       if (lex_integer () < 40)
458         {
459           msg (SE, _("WIDTH must be at least 40."));
460           return 0;
461         }
462       set_viewwidth (lex_integer ());
463       lex_get ();
464     }
465
466   return 1;
467 }
468
469 /* Parses FORMAT subcommand, which consists of a numeric format
470    specifier. */
471 static int
472 stc_custom_format (struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
473 {
474   struct fmt_spec fmt;
475
476   lex_match ('=');
477   if (!parse_format_specifier (&fmt))
478     return 0;
479   if (fmt_is_string (fmt.type))
480     {
481       char str[FMT_STRING_LEN_MAX + 1];
482       msg (SE, _("FORMAT requires numeric output format as an argument.  "
483                  "Specified format %s is of type string."),
484            fmt_to_string (&fmt, str));
485       return 0;
486     }
487
488   set_format (&fmt);
489   return 1;
490 }
491
492 static int
493 stc_custom_journal (struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
494 {
495   lex_match ('=');
496   if (!lex_match_id ("ON") && !lex_match_id ("OFF")) 
497     {
498       if (token == T_STRING)
499         lex_get ();
500       else
501         {
502           lex_error (NULL);
503           return 0;
504         }
505     }
506   return 1;
507 }
508
509 static int
510 stc_custom_listing (struct dataset *ds UNUSED, struct cmd_set *cmd UNUSED, void *aux UNUSED)
511 {
512   bool listing;
513
514   lex_match ('=');
515   if (lex_match_id ("ON") || lex_match_id ("YES"))
516     listing = true;
517   else if (lex_match_id ("OFF") || lex_match_id ("NO"))
518     listing = false;
519   else
520     {
521       /* FIXME */
522       return 0;
523     }
524   outp_enable_device (listing, OUTP_DEV_LISTING);
525
526   return 1;
527 }
528
529 static int
530 stc_custom_disk (struct dataset *ds, struct cmd_set *cmd UNUSED, void *aux)
531 {
532   return stc_custom_listing (ds, cmd, aux);
533 }
534 \f
535 static void
536 show_blanks (const struct dataset *ds UNUSED) 
537 {
538   if (get_blanks () == SYSMIS)
539     msg (SN, _("BLANKS is SYSMIS."));
540   else
541     msg (SN, _("BLANKS is %g."), get_blanks ());
542
543 }
544
545 static char *
546 format_cc (struct substring in, char grouping, char *out) 
547 {
548   while (!ss_is_empty (in)) 
549     {
550       char c = ss_get_char (&in);
551       if (c == grouping || c == '\'')
552         *out++ = '\'';
553       else if (c == '"')
554         *out++ = '"';
555       *out++ = c;
556     }
557   return out;
558 }
559
560 static void
561 show_cc (enum fmt_type type) 
562 {
563   const struct fmt_number_style *cc = fmt_get_style (type);
564   char cc_string[FMT_STYLE_AFFIX_MAX * 4 * 2 + 3 + 1];
565   char *out;
566
567   out = format_cc (cc->neg_prefix, cc->grouping, cc_string);
568   *out++ = cc->grouping;
569   out = format_cc (cc->prefix, cc->grouping, out);
570   *out++ = cc->grouping;
571   out = format_cc (cc->suffix, cc->grouping, out);
572   *out++ = cc->grouping;
573   out = format_cc (cc->neg_suffix, cc->grouping, out);
574   *out = '\0';
575   
576   msg (SN, _("%s is \"%s\"."), fmt_name (type), cc_string);
577 }
578
579 static void
580 show_cca (const struct dataset *ds UNUSED) 
581 {
582   show_cc (FMT_CCA);
583 }
584
585 static void
586 show_ccb (const struct dataset *ds UNUSED) 
587 {
588   show_cc (FMT_CCB);
589 }
590
591 static void
592 show_ccc (const struct dataset *ds UNUSED) 
593 {
594   show_cc (FMT_CCC);
595 }
596
597 static void
598 show_ccd (const struct dataset *ds UNUSED) 
599 {
600   show_cc (FMT_CCD);
601 }
602
603 static void
604 show_cce (const struct dataset *ds UNUSED) 
605 {
606   show_cc (FMT_CCE);
607 }
608
609 static void
610 show_decimals (const struct dataset *ds UNUSED) 
611 {
612   msg (SN, _("DECIMAL is \"%c\"."), fmt_decimal_char (FMT_F));
613 }
614
615 static void
616 show_endcmd (const struct dataset *ds UNUSED) 
617 {
618   msg (SN, _("ENDCMD is \"%c\"."), get_endcmd ());
619 }
620
621 static void
622 show_errors (const struct dataset *ds UNUSED) 
623 {
624   bool terminal = get_error_routing_to_terminal ();
625   bool listing = get_error_routing_to_listing ();
626   msg (SN, _("ERRORS is \"%s\"."),
627        terminal && listing ? "BOTH"
628        : terminal ? "TERMINAL"
629        : listing ? "LISTING"
630        : "NONE");
631 }
632
633 static void
634 show_format (const struct dataset *ds UNUSED) 
635 {
636   char str[FMT_STRING_LEN_MAX + 1];
637   msg (SN, _("FORMAT is %s."), fmt_to_string (get_format (), str));
638 }
639
640 static void
641 show_length (const struct dataset *ds UNUSED) 
642 {
643   msg (SN, _("LENGTH is %d."), get_viewlength ());
644 }
645
646 static void
647 show_mxerrs (const struct dataset *ds UNUSED) 
648 {
649   msg (SN, _("MXERRS is %d."), get_mxerrs ());
650 }
651
652 static void
653 show_mxloops (const struct dataset *ds UNUSED) 
654 {
655   msg (SN, _("MXLOOPS is %d."), get_mxloops ());
656 }
657
658 static void
659 show_mxwarns (const struct dataset *ds UNUSED) 
660 {
661   msg (SN, _("MXWARNS is %d."), get_mxwarns ());
662 }
663
664 /* Outputs that SETTING has the given INTEGER_FORMAT value. */
665 static void
666 show_integer_format (const char *setting, enum integer_format integer_format) 
667 {
668   msg (SN, _("%s is %s (%s)."),
669        setting,
670        (integer_format == INTEGER_MSB_FIRST ? "MSBFIRST"
671         : integer_format == INTEGER_LSB_FIRST ? "LSBFIRST"
672         : "VAX"),
673        integer_format == INTEGER_NATIVE ? "NATIVE" : "nonnative");
674 }
675
676 /* Outputs that SETTING has the given FLOAT_FORMAT value. */
677 static void
678 show_float_format (const char *setting, enum float_format float_format) 
679 {
680   const char *format_name = "";
681   
682   switch (float_format)
683     {
684     case FLOAT_IEEE_SINGLE_LE:
685       format_name = "ISL (32-bit IEEE 754 single, little-endian)";
686       break;
687     case FLOAT_IEEE_SINGLE_BE:
688       format_name = "ISB (32-bit IEEE 754 single, big-endian)";
689       break;
690     case FLOAT_IEEE_DOUBLE_LE:
691       format_name = "IDL (64-bit IEEE 754 double, little-endian)";
692       break;
693     case FLOAT_IEEE_DOUBLE_BE:
694       format_name = "IDB (64-bit IEEE 754 double, big-endian)";
695       break;
696
697     case FLOAT_VAX_F:
698       format_name = "VF (32-bit VAX F, VAX-endian)";
699       break;
700     case FLOAT_VAX_D:
701       format_name = "VD (64-bit VAX D, VAX-endian)";
702       break;
703     case FLOAT_VAX_G:
704       format_name = "VG (64-bit VAX G, VAX-endian)";
705       break;
706
707     case FLOAT_Z_SHORT:
708       format_name = "ZS (32-bit IBM Z hexadecimal short, big-endian)";
709       break;
710     case FLOAT_Z_LONG:
711       format_name = "ZL (64-bit IBM Z hexadecimal long, big-endian)";
712       break;
713
714     case FLOAT_FP:
715     case FLOAT_HEX:
716       NOT_REACHED ();
717     }
718
719   msg (SN, _("%s is %s (%s)."),
720        setting, format_name,
721        float_format == FLOAT_NATIVE_DOUBLE ? "NATIVE" : "nonnative");
722 }
723
724 static void
725 show_scompression (const struct dataset *ds UNUSED) 
726 {
727   if (get_scompression ())
728     msg (SN, _("SCOMPRESSION is ON."));
729   else
730     msg (SN, _("SCOMPRESSION is OFF."));
731 }
732
733 static void
734 show_undefined (const struct dataset *ds UNUSED) 
735 {
736   if (get_undefined ())
737     msg (SN, _("UNDEFINED is WARN."));
738   else
739     msg (SN, _("UNDEFINED is NOWARN."));
740 }
741
742 static void
743 show_weight (const struct dataset *ds) 
744 {
745   struct variable *var = dict_get_weight (dataset_dict (ds));
746   if (var == NULL)
747     msg (SN, _("WEIGHT is off."));
748   else
749     msg (SN, _("WEIGHT is variable %s."), var->name);
750 }
751
752 static void
753 show_wib (const struct dataset *ds UNUSED) 
754 {
755   show_integer_format ("WIB", data_out_get_integer_format ());
756 }
757
758 static void
759 show_wrb (const struct dataset *ds UNUSED) 
760 {
761   show_float_format ("WRB", data_out_get_float_format ());
762 }
763
764 static void
765 show_width (const struct dataset *ds UNUSED) 
766 {
767   msg (SN, _("WIDTH is %d."), get_viewwidth ());
768 }
769
770 struct show_sbc 
771   {
772     const char *name;
773     void (*function) (const struct dataset *);
774   };
775
776 const struct show_sbc show_table[] = 
777   {
778     {"BLANKS", show_blanks},
779     {"CCA", show_cca},
780     {"CCB", show_ccb},
781     {"CCC", show_ccc},
782     {"CCD", show_ccd},
783     {"CCE", show_cce},
784     {"DECIMALS", show_decimals},
785     {"ENDCMD", show_endcmd},
786     {"ERRORS", show_errors},      
787     {"FORMAT", show_format},
788     {"LENGTH", show_length},
789     {"MXERRS", show_mxerrs},
790     {"MXLOOPS", show_mxloops},
791     {"MXWARNS", show_mxwarns},
792     {"SCOMPRESSION", show_scompression},
793     {"UNDEFINED", show_undefined},
794     {"WEIGHT", show_weight},
795     {"WIB", show_wib},
796     {"WRB", show_wrb},
797     {"WIDTH", show_width},
798   };
799
800 static void
801 show_all (const struct dataset *ds) 
802 {
803   size_t i;
804   
805   for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
806     show_table[i].function (ds);
807 }
808
809 static void
810 show_all_cc (void) 
811 {
812   int i;
813
814   for (i = 0; i < 5; i++)
815     show_cc (i);
816 }
817
818 static void
819 show_warranty (const struct dataset *ds UNUSED) 
820 {
821   msg (MN, lack_of_warranty);
822 }
823
824 static void
825 show_copying (const struct dataset *ds UNUSED) 
826 {
827   msg (MN, copyleft);
828 }
829
830 int
831 cmd_show (struct dataset *ds) 
832 {
833   if (token == '.') 
834     {
835       show_all (ds);
836       return CMD_SUCCESS;
837     }
838
839   do 
840     {
841       if (lex_match (T_ALL))
842         show_all (ds);
843       else if (lex_match_id ("CC")) 
844         show_all_cc ();
845       else if (lex_match_id ("WARRANTY"))
846         show_warranty (ds);
847       else if (lex_match_id ("COPYING"))
848         show_copying (ds);
849       else if (token == T_ID)
850         {
851           int i;
852
853           for (i = 0; i < sizeof show_table / sizeof *show_table; i++)
854             if (lex_match_id (show_table[i].name)) 
855               {
856                 show_table[i].function (ds);
857                 goto found;
858               }
859           lex_error (NULL);
860           return CMD_FAILURE;
861
862         found: ;
863         }
864       else 
865         {
866           lex_error (NULL);
867           return CMD_FAILURE;
868         }
869
870       lex_match ('/');
871     }
872   while (token != '.');
873
874   return CMD_SUCCESS;
875 }
876
877 /*
878    Local Variables:
879    mode: c
880    End:
881 */