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