Improve error messages for format specifiers.
[pspp] / src / language / dictionary / vector.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2010, 2011, 2012, 2016 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/format.h"
23 #include "data/dictionary.h"
24 #include "data/variable.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/i18n.h"
31 #include "libpspp/message.h"
32 #include "libpspp/misc.h"
33 #include "libpspp/pool.h"
34 #include "libpspp/str.h"
35
36 #include "gl/intprops.h"
37 #include "gl/xalloc.h"
38
39 #include "gettext.h"
40 #define _(msgid) gettext (msgid)
41
42 int
43 cmd_vector (struct lexer *lexer, struct dataset *ds)
44 {
45   struct dictionary *dict = dataset_dict (ds);
46   struct pool *pool = pool_create ();
47
48   do
49     {
50       char **vectors;
51       size_t n_vectors, allocated_vectors;
52
53       /* Get the name(s) of the new vector(s). */
54       if (!lex_force_id (lexer))
55         return CMD_CASCADING_FAILURE;
56       char *error = dict_id_is_valid__ (dict, lex_tokcstr (lexer));
57       if (error)
58         {
59           lex_error (lexer, "%s", error);
60           free (error);
61           return CMD_CASCADING_FAILURE;
62         }
63
64       vectors = NULL;
65       n_vectors = allocated_vectors = 0;
66       while (lex_token (lexer) == T_ID)
67         {
68           size_t i;
69
70           if (dict_lookup_vector (dict, lex_tokcstr (lexer)))
71             {
72               lex_next_error (lexer, 0, 0,
73                               _("A vector named %s already exists."),
74                               lex_tokcstr (lexer));
75               goto fail;
76             }
77
78           for (i = 0; i < n_vectors; i++)
79             if (!utf8_strcasecmp (vectors[i], lex_tokcstr (lexer)))
80               {
81                 lex_next_error (lexer, 0, 0,
82                                 _("Vector name %s is given twice."),
83                                 lex_tokcstr (lexer));
84                 goto fail;
85               }
86
87           if (n_vectors == allocated_vectors)
88             vectors = pool_2nrealloc (pool, vectors, &allocated_vectors,
89                                       sizeof *vectors);
90           vectors[n_vectors++] = pool_strdup (pool, lex_tokcstr (lexer));
91
92           lex_get (lexer);
93           lex_match (lexer, T_COMMA);
94         }
95
96       /* Now that we have the names it's time to check for the short
97          or long forms. */
98       if (lex_match (lexer, T_EQUALS))
99         {
100           /* Long form. */
101           struct variable **v;
102           size_t nv;
103
104           if (n_vectors > 1)
105             {
106               lex_error (lexer, _("A slash must separate each vector "
107                                   "specification in VECTOR's long form."));
108               goto fail;
109             }
110
111           if (!parse_variables_pool (lexer, pool, dict, &v, &nv,
112                                      PV_SAME_WIDTH | PV_DUPLICATE))
113             goto fail;
114
115           dict_create_vector (dict, vectors[0], v, nv);
116         }
117       else if (lex_match (lexer, T_LPAREN))
118         {
119           /* Short form. */
120           struct fmt_spec format = fmt_for_output (FMT_F, 8, 2);
121           bool seen_format = false;
122           size_t n_vars = 0;
123           int start_ofs = lex_ofs (lexer) - 2;
124           while (!lex_match (lexer, T_RPAREN))
125             {
126               if (lex_is_integer (lexer) && n_vars == 0)
127                 {
128                   if (!lex_force_int_range (lexer, NULL, 1, INT_MAX))
129                     goto fail;
130                   n_vars = lex_integer (lexer);
131                   lex_get (lexer);
132                 }
133               else if (lex_token (lexer) == T_ID && !seen_format)
134                 {
135                   seen_format = true;
136                   if (!parse_format_specifier (lexer, &format))
137                     goto fail;
138                   char *error = fmt_check_output__ (&format);
139                   if (error)
140                     {
141                       lex_next_error (lexer, -1, -1, "%s", error);
142                       free (error);
143                       goto fail;
144                     }
145                 }
146               else
147                 {
148                   lex_error (lexer, NULL);
149                   goto fail;
150                 }
151               lex_match (lexer, T_COMMA);
152             }
153           int end_ofs = lex_ofs (lexer) - 1;
154           if (n_vars == 0)
155             {
156               lex_error (lexer, _("Syntax error expecting vector length."));
157               goto fail;
158             }
159
160           /* Check that none of the variables exist and that their names are
161              not excessively long. */
162           for (size_t i = 0; i < n_vectors; i++)
163             {
164               int j;
165               for (j = 0; j < n_vars; j++)
166                 {
167                   char *name = xasprintf ("%s%d", vectors[i], j + 1);
168                   char *error = dict_id_is_valid__ (dict, name);
169                   if (error)
170                     {
171                       lex_ofs_error (lexer, start_ofs, end_ofs, "%s", error);
172                       free (error);
173                       free (name);
174                       goto fail;
175                     }
176                   if (dict_lookup_var (dict, name))
177                     {
178                       lex_ofs_error (lexer, start_ofs, end_ofs,
179                                      _("%s is an existing variable name."),
180                                      name);
181                       free (name);
182                       goto fail;
183                     }
184                   free (name);
185                 }
186             }
187
188           /* Finally create the variables and vectors. */
189           struct variable **vars = pool_nmalloc (pool, n_vars, sizeof *vars);
190           for (size_t i = 0; i < n_vectors; i++)
191             {
192               for (size_t j = 0; j < n_vars; j++)
193                 {
194                   char *name = xasprintf ("%s%zu", vectors[i], j + 1);
195                   vars[j] = dict_create_var_assert (dict, name,
196                                                     fmt_var_width (&format));
197                   var_set_both_formats (vars[j], &format);
198                   free (name);
199                 }
200               dict_create_vector_assert (dict, vectors[i], vars, n_vars);
201             }
202         }
203       else
204         {
205           lex_error (lexer, NULL);
206           goto fail;
207         }
208     }
209   while (lex_match (lexer, T_SLASH));
210
211   pool_destroy (pool);
212   return CMD_SUCCESS;
213
214 fail:
215   pool_destroy (pool);
216   return CMD_FAILURE;
217 }