PRINT: Improve error messages.
[pspp] / src / language / dictionary / numeric.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2010, 2011, 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 <stdlib.h>
20
21 #include "data/dataset.h"
22 #include "data/dictionary.h"
23 #include "data/variable.h"
24 #include "data/format.h"
25 #include "language/command.h"
26 #include "language/lexer/format-parser.h"
27 #include "language/lexer/lexer.h"
28 #include "language/lexer/variable-parser.h"
29 #include "libpspp/assertion.h"
30 #include "libpspp/message.h"
31 #include "libpspp/str.h"
32
33 #include "gettext.h"
34 #define _(msgid) gettext (msgid)
35
36 /* Parses the NUMERIC command. */
37 int
38 cmd_numeric (struct lexer *lexer, struct dataset *ds)
39 {
40   size_t i;
41
42   /* Names of variables to create. */
43   char **v;
44   size_t nv;
45
46   do
47     {
48       /* Format spec for variables to create. */
49       struct fmt_spec f;
50
51       if (!parse_DATA_LIST_vars (lexer, dataset_dict (ds),
52                                  &v, &nv, PV_NO_DUPLICATE))
53         return CMD_FAILURE;
54
55       /* Get the optional format specification. */
56       if (lex_match (lexer, T_LPAREN))
57         {
58           if (!parse_format_specifier (lexer, &f))
59             goto fail;
60
61           char *error = fmt_check_output__ (&f);
62           if (error)
63             {
64               lex_next_error (lexer, -1, -1, "%s", error);
65               free (error);
66               goto fail;
67             }
68
69           if (fmt_is_string (f.type))
70             {
71               char str[FMT_STRING_LEN_MAX + 1];
72               lex_next_error (lexer, -1, -1,
73                               _("Format type %s may not be used with a numeric "
74                                 "variable."), fmt_to_string (&f, str));
75               goto fail;
76             }
77
78           if (!lex_match (lexer, T_RPAREN))
79             {
80               lex_error_expecting (lexer, "`)'");
81               goto fail;
82             }
83         }
84       else
85         f = var_default_formats (0);
86
87       /* Create each variable. */
88       for (i = 0; i < nv; i++)
89         {
90           struct variable *new_var = dict_create_var (dataset_dict (ds), v[i], 0);
91           if (!new_var)
92             msg (SE, _("There is already a variable named %s."), v[i]);
93           else
94             var_set_both_formats (new_var, &f);
95         }
96
97       /* Clean up. */
98       for (i = 0; i < nv; i++)
99         free (v[i]);
100       free (v);
101     }
102   while (lex_match (lexer, T_SLASH));
103
104   return CMD_SUCCESS;
105
106   /* If we have an error at a point where cleanup is required,
107      flow-of-control comes here. */
108 fail:
109   for (i = 0; i < nv; i++)
110     free (v[i]);
111   free (v);
112   return CMD_FAILURE;
113 }
114
115 /* Parses the STRING command. */
116 int
117 cmd_string (struct lexer *lexer, struct dataset *ds)
118 {
119   size_t i;
120
121   /* Names of variables to create. */
122   char **v;
123   size_t nv;
124
125   /* Format spec for variables to create. */
126   struct fmt_spec f;
127
128   /* Width of variables to create. */
129   int width;
130
131   do
132     {
133       if (!parse_DATA_LIST_vars (lexer, dataset_dict (ds),
134                                  &v, &nv, PV_NO_DUPLICATE))
135         return CMD_FAILURE;
136
137       if (!lex_force_match (lexer, T_LPAREN)
138           || !parse_format_specifier (lexer, &f))
139         goto fail;
140
141       char *error = fmt_check_type_compat__ (&f, NULL, VAL_STRING);
142       if (!error)
143         error = fmt_check_output__ (&f);
144       if (error)
145         {
146           lex_next_error (lexer, -2, -2, "%s", error);
147           free (error);
148           goto fail;
149         }
150
151       if (!lex_force_match (lexer, T_RPAREN))
152         goto fail;
153
154       width = fmt_var_width (&f);
155
156       /* Create each variable. */
157       for (i = 0; i < nv; i++)
158         {
159           struct variable *new_var = dict_create_var (dataset_dict (ds), v[i],
160                                                       width);
161           if (!new_var)
162             msg (SE, _("There is already a variable named %s."), v[i]);
163           else
164             var_set_both_formats (new_var, &f);
165         }
166
167       /* Clean up. */
168       for (i = 0; i < nv; i++)
169         free (v[i]);
170       free (v);
171     }
172   while (lex_match (lexer, T_SLASH));
173
174   return CMD_SUCCESS;
175
176   /* If we have an error at a point where cleanup is required,
177      flow-of-control comes here. */
178 fail:
179   for (i = 0; i < nv; i++)
180     free (v[i]);
181   free (v);
182   return CMD_FAILURE;
183 }
184
185 /* Parses the LEAVE command. */
186 int
187 cmd_leave (struct lexer *lexer, struct dataset *ds)
188 {
189   struct variable **v;
190   size_t nv;
191
192   size_t i;
193
194   if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
195     return CMD_CASCADING_FAILURE;
196   for (i = 0; i < nv; i++)
197     var_set_leave (v[i], true);
198   free (v);
199
200   return CMD_SUCCESS;
201 }