Fix numerous memory leaks.
[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/string-set.h"
30 #include "libpspp/version.h"
31 #include "output/pivot-table.h"
32
33 #include "gl/xalloc.h"
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37
38 int
39 cmd_output (struct lexer *lexer, struct dataset *ds UNUSED)
40 {
41   struct string_set rc_names = STRING_SET_INITIALIZER (rc_names);
42
43   if (!lex_force_match_id (lexer, "MODIFY"))
44     {
45       lex_error (lexer, NULL);
46       goto error;
47     }
48
49   while (lex_token (lexer) != T_ENDCMD)
50     {
51       lex_match (lexer, T_SLASH);
52
53       if (lex_match_id (lexer, "SELECT"))
54         {
55           if (!lex_match_id (lexer, "TABLES"))
56             {
57               lex_error (lexer, NULL);
58               goto error;
59             }
60         }
61       else if (lex_match_id (lexer, "TABLECELLS"))
62         {
63           string_set_clear (&rc_names);
64           struct fmt_spec fmt = { 0, 0, 0 };
65
66           while (lex_token (lexer) != T_SLASH &&
67                  lex_token (lexer) != T_ENDCMD)
68             {
69               if (lex_match_id (lexer, "SELECT"))
70                 {
71                   if (! lex_force_match (lexer, T_EQUALS))
72                     goto error;
73
74                   if (! lex_force_match (lexer, T_LBRACK))
75                     goto error;
76
77                   while (lex_token (lexer) == T_ID)
78                     {
79                       string_set_insert (&rc_names, lex_tokcstr (lexer));
80                       lex_get (lexer);
81                     }
82
83                   if (! lex_force_match (lexer, T_RBRACK))
84                     goto error;
85                 }
86               else if (lex_match_id (lexer, "FORMAT"))
87                 {
88                   char type[FMT_TYPE_LEN_MAX + 1];
89                   int width = -1;
90                   int decimals = -1;
91
92                   if (! lex_force_match (lexer, T_EQUALS))
93                     goto error;
94                   if (! parse_abstract_format_specifier (lexer, type, &width, &decimals))
95                     {
96                       lex_error (lexer, NULL);
97                       goto error;
98                     }
99
100                   if (width <= 0)
101                     {
102                       const struct fmt_spec *dflt = settings_get_format ();
103                       width = dflt->w;
104                     }
105
106                   if (!fmt_from_name (type, &fmt.type))
107                     {
108                       lex_error (lexer, _("Unknown format type `%s'."), type);
109                       goto error;
110                     }
111
112                   fmt.w = width;
113                   fmt.d = decimals;
114                 }
115               else
116                 {
117                   lex_error (lexer, NULL);
118                   goto error;
119                 }
120             }
121
122           if (fmt.w)
123             {
124               const struct string_set_node *node;
125               const char *s;
126               STRING_SET_FOR_EACH (s, node, &rc_names)
127                 if (!pivot_result_class_change (s, &fmt))
128                   lex_error (lexer, _("Unknown cell class %s."), s);
129             }
130         }
131       else
132         {
133           lex_error (lexer, NULL);
134           goto error;
135         }
136     }
137
138   string_set_destroy (&rc_names);
139   return CMD_SUCCESS;
140
141  error:
142   string_set_destroy (&rc_names);
143   return CMD_SUCCESS;
144 }