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