1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2010, 2011, 2012, 2016 Free Software Foundation, Inc.
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.
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.
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/>. */
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"
36 #include "gl/intprops.h"
37 #include "gl/xalloc.h"
40 #define _(msgid) gettext (msgid)
43 cmd_vector (struct lexer *lexer, struct dataset *ds)
45 struct dictionary *dict = dataset_dict (ds);
46 struct pool *pool = pool_create ();
50 /* Get the name(s) of the new vector(s). */
51 if (!lex_force_id (lexer))
54 int vectors_start = lex_ofs (lexer);
55 char **vectors = NULL;
57 size_t allocated_vectors = 0;
58 while (lex_token (lexer) == T_ID)
60 char *error = dict_id_is_valid__ (dict, lex_tokcstr (lexer));
63 lex_error (lexer, "%s", error);
68 if (dict_lookup_vector (dict, lex_tokcstr (lexer)))
70 lex_next_error (lexer, 0, 0,
71 _("A vector named %s already exists."),
76 for (size_t i = 0; i < n_vectors; i++)
77 if (!utf8_strcasecmp (vectors[i], lex_tokcstr (lexer)))
79 lex_ofs_error (lexer, vectors_start, lex_ofs (lexer),
80 _("Vector name %s is given twice."),
85 if (n_vectors == allocated_vectors)
86 vectors = pool_2nrealloc (pool, vectors, &allocated_vectors,
88 vectors[n_vectors++] = pool_strdup (pool, lex_tokcstr (lexer));
91 lex_match (lexer, T_COMMA);
94 /* Now that we have the names it's time to check for the short
96 if (lex_match (lexer, T_EQUALS))
100 lex_ofs_error (lexer, vectors_start, lex_ofs (lexer) - 1,
101 _("Only a single vector name may be specified "
102 "when a list of variables is given."));
108 if (!parse_variables_pool (lexer, pool, dict, &v, &nv,
109 PV_SAME_WIDTH | PV_DUPLICATE))
112 dict_create_vector (dict, vectors[0], v, nv);
114 else if (lex_match (lexer, T_LPAREN))
116 struct fmt_spec format = fmt_for_output (FMT_F, 8, 2);
117 bool seen_format = false;
119 int name_ofs = lex_ofs (lexer) - 2;
120 int lparen_ofs = lex_ofs (lexer) - 1;
121 while (!lex_match (lexer, T_RPAREN))
123 if (lex_is_integer (lexer))
127 lex_ofs_error (lexer, lparen_ofs, lex_ofs (lexer),
128 _("Vector length may only be specified "
132 if (!lex_force_int_range (lexer, NULL, 1, INT_MAX))
134 n_vars = lex_integer (lexer);
137 else if (lex_token (lexer) == T_ID)
141 lex_ofs_error (lexer, lparen_ofs, lex_ofs (lexer),
142 _("Only one format may be specified."));
146 if (!parse_format_specifier (lexer, &format))
148 char *error = fmt_check_output__ (format);
151 lex_next_error (lexer, -1, -1, "%s", error);
158 lex_error (lexer, _("Syntax error expecting vector length "
162 lex_match (lexer, T_COMMA);
164 int end_ofs = lex_ofs (lexer) - 1;
167 lex_ofs_error (lexer, lparen_ofs, end_ofs,
168 _("Vector length is required."));
172 /* Check that none of the variables exist and that their names are
173 not excessively long. */
174 for (size_t i = 0; i < n_vectors; i++)
175 for (size_t j = 0; j < n_vars; j++)
177 char *name = xasprintf ("%s%zu", vectors[i], j + 1);
178 char *error = dict_id_is_valid__ (dict, name);
181 lex_ofs_error (lexer, name_ofs, end_ofs, "%s", error);
186 if (dict_lookup_var (dict, name))
188 lex_ofs_error (lexer, name_ofs, end_ofs,
189 _("%s is an existing variable name."),
197 /* Finally create the variables and vectors. */
198 struct variable **vars = pool_nmalloc (pool, n_vars, sizeof *vars);
199 for (size_t i = 0; i < n_vectors; i++)
201 for (size_t j = 0; j < n_vars; j++)
203 char *name = xasprintf ("%s%zu", vectors[i], j + 1);
204 vars[j] = dict_create_var_assert (dict, name,
205 fmt_var_width (format));
206 var_set_both_formats (vars[j], format);
209 dict_create_vector_assert (dict, vectors[i], vars, n_vars);
214 lex_error_expecting (lexer, "`='", "`('");
218 while (lex_match (lexer, T_SLASH));