1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #include <data/procedure.h>
24 #include <data/dictionary.h>
25 #include <data/variable.h>
26 #include <language/command.h>
27 #include <language/lexer/lexer.h>
28 #include <language/lexer/variable-parser.h>
29 #include <libpspp/alloc.h>
30 #include <libpspp/assertion.h>
31 #include <libpspp/message.h>
32 #include <libpspp/misc.h>
33 #include <libpspp/str.h>
36 #define _(msgid) gettext (msgid)
39 cmd_vector (struct lexer *lexer, struct dataset *ds)
41 /* Just to be different, points to a set of null terminated strings
42 containing the names of the vectors to be created. The list
43 itself is terminated by a empty string. So a list of three
44 elements, A B C, would look like this: "A\0B\0C\0\0". */
47 /* vecnames iterators. */
50 /* Maximum allocated position for vecnames, plus one position. */
53 struct dictionary *dict = dataset_dict (ds);
55 cp = vecnames = xmalloc (256);
56 endp = &vecnames[256];
59 /* Get the name(s) of the new vector(s). */
60 if (!lex_force_id (lexer))
61 return CMD_CASCADING_FAILURE;
62 while (lex_token (lexer) == T_ID)
66 char *old_vecnames = vecnames;
67 vecnames = xrealloc (vecnames, endp - vecnames + 256);
68 cp = (cp - old_vecnames) + vecnames;
69 endp = (endp - old_vecnames) + vecnames + 256;
72 for (cp2 = cp; cp2 < cp; cp2 += strlen (cp))
73 if (!strcasecmp (cp2, lex_tokid (lexer)))
75 msg (SE, _("Vector name %s is given twice."), lex_tokid (lexer));
79 if (dict_lookup_vector (dict, lex_tokid (lexer)))
81 msg (SE, _("There is already a vector with name %s."), lex_tokid (lexer));
85 cp = stpcpy (cp, lex_tokid (lexer)) + 1;
87 lex_match (lexer, ',');
91 /* Now that we have the names it's time to check for the short
93 if (lex_match (lexer, '='))
99 if (strchr (vecnames, '\0')[1])
101 /* There's more than one vector name. */
102 msg (SE, _("A slash must be used to separate each vector "
103 "specification when using the long form. Commands "
104 "such as VECTOR A,B=Q1 TO Q20 are not supported."));
108 if (!parse_variables (lexer, dict, &v, &nv,
109 PV_SAME_WIDTH | PV_DUPLICATE))
112 dict_create_vector (dict, vecnames, v, nv);
115 else if (lex_match (lexer, '('))
119 /* Maximum number of digits in a number to add to the base
123 /* Name of an individual variable to be created. */
124 char name[SHORT_NAME_LEN + 1];
126 /* Vector variables. */
130 if (!lex_force_int (lexer))
131 return CMD_CASCADING_FAILURE;
132 nv = lex_integer (lexer);
136 msg (SE, _("Vectors must have at least one element."));
139 if (!lex_force_match (lexer, ')'))
142 /* First check that all the generated variable names
143 are LONG_NAME_LEN characters or shorter. */
144 ndig = intlog10 (nv);
145 for (cp = vecnames; *cp;)
147 int len = strlen (cp);
148 if (len + ndig > LONG_NAME_LEN)
150 msg (SE, _("%s%d is too long for a variable name."), cp, nv);
156 /* Next check that none of the variables exist. */
157 for (cp = vecnames; *cp;)
159 for (i = 0; i < nv; i++)
161 sprintf (name, "%s%d", cp, i + 1);
162 if (dict_lookup_var (dict, name))
164 msg (SE, _("There is already a variable named %s."),
169 cp += strlen (cp) + 1;
172 /* Finally create the variables and vectors. */
173 v = xmalloc (nv * sizeof *v);
174 for (cp = vecnames; *cp;)
176 for (i = 0; i < nv; i++)
178 sprintf (name, "%s%d", cp, i + 1);
179 v[i] = dict_create_var_assert (dict, name, 0);
181 if (!dict_create_vector (dict, cp, v, nv))
183 cp += strlen (cp) + 1;
189 msg (SE, _("The syntax for this command does not match "
190 "the expected syntax for either the long form "
191 "or the short form of VECTOR."));
198 while (lex_match (lexer, '/'));
200 if (lex_token (lexer) != '.')
202 lex_error (lexer, _("expecting end of command"));