Fixed crash in MODIFY OUTPUT when no valid FORMAT clause was specified.
[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                   lex_force_match (lexer, T_EQUALS);
104                   lex_force_match (lexer, T_LBRACK);
105                   
106                   while (lex_token (lexer) != T_RBRACK &&
107                          lex_token (lexer) != T_ENDCMD)
108                     {
109                       int i;
110                       for (i = 0 ; i < N_THINGS; ++i)
111                         {
112                           if (lex_match_id (lexer, things[i].identifier))
113                             {
114                               os->rc = xrealloc (os->rc, sizeof (*os->rc) * ++os->n_rc);
115                               os->rc[os->n_rc - 1] = things[i].rc;
116                               break;
117                             }
118                         }
119                       if (i >= N_THINGS)
120                         {
121                           lex_error (lexer, _("Unknown TABLECELLS class"));
122                           goto error;
123                         }
124                     }
125                   lex_force_match (lexer, T_RBRACK);
126                 }
127               else if (lex_match_id (lexer, "FORMAT"))
128                 {
129                   struct fmt_spec fmt;    
130                   char type[FMT_TYPE_LEN_MAX + 1];
131                   int width = -1;
132                   int decimals = -1;
133
134                   lex_force_match (lexer, T_EQUALS);
135                   if (! parse_abstract_format_specifier (lexer, type, &width, &decimals))
136                     {
137                       lex_error (lexer, NULL);
138                       goto error;
139                     }
140
141                   if (width <= 0)
142                     {
143                       const struct fmt_spec *dflt = settings_get_format ();
144                       width = dflt->w;
145                     }
146
147                   if (!fmt_from_name (type, &fmt.type))
148                     {
149                       lex_error (lexer, _("Unknown format type `%s'."), type);
150                       goto error;
151                     }
152
153                   fmt.w = width;
154                   fmt.d = decimals;
155
156                   os->fmt = fmt;
157                   format = true;
158                 }
159               else 
160                 {
161                   lex_error (lexer, NULL);
162                   goto error;
163                 }
164             }
165           if (!format)
166             goto error;
167         }
168       else 
169         {
170           lex_error (lexer, NULL);
171           goto error;   
172         }
173     }
174
175   /* Populate the global table, with the values we parsed */
176   for (i = 0; i < n_os; ++i)
177     {
178       for (j = 0; j < output_specs[i].n_rc;  ++j)
179         {
180           ugly [output_specs[i].rc[j]] = output_specs[i].fmt;
181         }
182     }
183   
184   for (j = 0; j < n_os;  ++j)
185     free (output_specs[j].rc);
186   free (output_specs);
187   return CMD_SUCCESS;
188  error:
189
190   for (j = 0; j < n_os;  ++j)
191     free (output_specs[j].rc);
192   free (output_specs);
193   return CMD_SUCCESS;
194 }