a2e630d79173b8fa763ab71d63822c637bc849b5
[pspp] / src / language / utilities / output.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2014 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 <ctype.h>
20 #include <stdlib.h>
21
22 #include "data/dataset.h"
23 #include "data/settings.h"
24 #include "data/format.h"
25 #include "language/command.h"
26 #include "language/lexer/lexer.h"
27 #include "language/lexer/format-parser.h"
28 #include "libpspp/message.h"
29 #include "libpspp/version.h"
30 #include "output/tab.h"
31
32 #include "gl/xalloc.h"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 struct thing
38 {
39   const char *identifier;
40   enum result_class rc;
41 };
42
43 extern struct fmt_spec ugly [n_RC];
44
45 static const struct thing things[] =
46   {
47     {"SIGNIFICANCE", RC_PVALUE},
48     {"COUNT" ,RC_WEIGHT}
49   };
50
51 #define N_THINGS (sizeof (things) / sizeof (struct thing))
52
53 struct output_spec
54 {
55   /* An array of classes */
56   enum result_class *rc;
57
58   int n_rc;
59
60   /* The format to be applied to these classes */
61   struct fmt_spec fmt;
62 };
63
64 int
65 cmd_output (struct lexer *lexer, struct dataset *ds UNUSED)
66 {
67   int j, i;
68   struct output_spec *output_specs = NULL;
69   int n_os = 0;
70
71   if (!lex_force_match_id (lexer, "MODIFY"))
72     {
73       lex_error (lexer, NULL);
74       goto error;
75     }
76
77   while (lex_token (lexer) != T_ENDCMD)
78     {
79       lex_match (lexer, T_SLASH);
80
81       if (lex_match_id (lexer, "SELECT"))
82         {
83           if (!lex_match_id (lexer, "TABLES"))
84             {
85               lex_error (lexer, NULL);
86               goto error;
87             }
88         }
89       else if (lex_match_id (lexer, "TABLECELLS"))
90         {
91           struct output_spec *os;
92           output_specs = xrealloc (output_specs, sizeof (*output_specs) * ++n_os);
93           os = &output_specs[n_os - 1];
94           os->n_rc = 0;
95           os->rc = NULL;
96           bool format = false;
97
98           while (lex_token (lexer) != T_SLASH &&
99                  lex_token (lexer) != T_ENDCMD)
100             {
101               if (lex_match_id (lexer, "SELECT"))
102                 {
103                   if (! lex_force_match (lexer, T_EQUALS))
104                     goto error;
105
106                   if (! lex_force_match (lexer, T_LBRACK))
107                     goto error;
108
109                   while (lex_token (lexer) != T_RBRACK &&
110                          lex_token (lexer) != T_ENDCMD)
111                     {
112                       int i;
113                       for (i = 0 ; i < N_THINGS; ++i)
114                         {
115                           if (lex_match_id (lexer, things[i].identifier))
116                             {
117                               os->rc = xrealloc (os->rc, sizeof (*os->rc) * ++os->n_rc);
118                               os->rc[os->n_rc - 1] = things[i].rc;
119                               break;
120                             }
121                         }
122                       if (i >= N_THINGS)
123                         {
124                           lex_error (lexer, _("Unknown TABLECELLS class"));
125                           goto error;
126                         }
127                     }
128                   if (! lex_force_match (lexer, T_RBRACK))
129                     goto error;
130                 }
131               else if (lex_match_id (lexer, "FORMAT"))
132                 {
133                   struct fmt_spec fmt;
134                   char type[FMT_TYPE_LEN_MAX + 1];
135                   int width = -1;
136                   int decimals = -1;
137
138                   if (! lex_force_match (lexer, T_EQUALS))
139                     goto error;
140                   if (! parse_abstract_format_specifier (lexer, type, &width, &decimals))
141                     {
142                       lex_error (lexer, NULL);
143                       goto error;
144                     }
145
146                   if (width <= 0)
147                     {
148                       const struct fmt_spec *dflt = settings_get_format ();
149                       width = dflt->w;
150                     }
151
152                   if (!fmt_from_name (type, &fmt.type))
153                     {
154                       lex_error (lexer, _("Unknown format type `%s'."), type);
155                       goto error;
156                     }
157
158                   fmt.w = width;
159                   fmt.d = decimals;
160
161                   os->fmt = fmt;
162                   format = true;
163                 }
164               else
165                 {
166                   lex_error (lexer, NULL);
167                   goto error;
168                 }
169             }
170           if (!format)
171             goto error;
172         }
173       else
174         {
175           lex_error (lexer, NULL);
176           goto error;
177         }
178     }
179
180   /* Populate the global table, with the values we parsed */
181   for (i = 0; i < n_os; ++i)
182     {
183       for (j = 0; j < output_specs[i].n_rc;  ++j)
184         {
185           ugly [output_specs[i].rc[j]] = output_specs[i].fmt;
186         }
187     }
188
189   for (j = 0; j < n_os;  ++j)
190     free (output_specs[j].rc);
191   free (output_specs);
192   return CMD_SUCCESS;
193  error:
194
195   for (j = 0; j < n_os;  ++j)
196     free (output_specs[j].rc);
197   free (output_specs);
198   return CMD_SUCCESS;
199 }