1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include <libpspp/message.h>
23 #include <libpspp/alloc.h>
24 #include <language/command.h>
25 #include <data/dictionary.h>
26 #include <libpspp/message.h>
27 #include <language/lexer/lexer.h>
28 #include <libpspp/misc.h>
29 #include <libpspp/str.h>
30 #include <data/variable.h>
33 #define _(msgid) gettext (msgid)
38 /* Just to be different, points to a set of null terminated strings
39 containing the names of the vectors to be created. The list
40 itself is terminated by a empty string. So a list of three
41 elements, A B C, would look like this: "A\0B\0C\0\0". */
44 /* vecnames iterators. */
47 /* Maximum allocated position for vecnames, plus one position. */
50 cp = vecnames = xmalloc (256);
51 endp = &vecnames[256];
54 /* Get the name(s) of the new vector(s). */
56 return CMD_CASCADING_FAILURE;
61 char *old_vecnames = vecnames;
62 vecnames = xrealloc (vecnames, endp - vecnames + 256);
63 cp = (cp - old_vecnames) + vecnames;
64 endp = (endp - old_vecnames) + vecnames + 256;
67 for (cp2 = cp; cp2 < cp; cp2 += strlen (cp))
68 if (!strcasecmp (cp2, tokid))
70 msg (SE, _("Vector name %s is given twice."), tokid);
74 if (dict_lookup_vector (default_dict, tokid))
76 msg (SE, _("There is already a vector with name %s."), tokid);
80 cp = stpcpy (cp, tokid) + 1;
86 /* Now that we have the names it's time to check for the short
94 if (strchr (vecnames, '\0')[1])
96 /* There's more than one vector name. */
97 msg (SE, _("A slash must be used to separate each vector "
98 "specification when using the long form. Commands "
99 "such as VECTOR A,B=Q1 TO Q20 are not supported."));
103 if (!parse_variables (default_dict, &v, &nv,
104 PV_SAME_TYPE | PV_DUPLICATE))
107 dict_create_vector (default_dict, vecnames, v, nv);
110 else if (lex_match ('('))
114 /* Maximum number of digits in a number to add to the base
118 /* Name of an individual variable to be created. */
119 char name[SHORT_NAME_LEN + 1];
121 /* Vector variables. */
125 if (!lex_force_int ())
126 return CMD_CASCADING_FAILURE;
131 msg (SE, _("Vectors must have at least one element."));
134 if (!lex_force_match (')'))
137 /* First check that all the generated variable names
138 are LONG_NAME_LEN characters or shorter. */
139 ndig = intlog10 (nv);
140 for (cp = vecnames; *cp;)
142 int len = strlen (cp);
143 if (len + ndig > LONG_NAME_LEN)
145 msg (SE, _("%s%d is too long for a variable name."), cp, nv);
151 /* Next check that none of the variables exist. */
152 for (cp = vecnames; *cp;)
154 for (i = 0; i < nv; i++)
156 sprintf (name, "%s%d", cp, i + 1);
157 if (dict_lookup_var (default_dict, name))
159 msg (SE, _("There is already a variable named %s."),
164 cp += strlen (cp) + 1;
167 /* Finally create the variables and vectors. */
168 v = xmalloc (nv * sizeof *v);
169 for (cp = vecnames; *cp;)
171 for (i = 0; i < nv; i++)
173 sprintf (name, "%s%d", cp, i + 1);
174 v[i] = dict_create_var_assert (default_dict, name, 0);
176 if (!dict_create_vector (default_dict, cp, v, nv))
178 cp += strlen (cp) + 1;
184 msg (SE, _("The syntax for this command does not match "
185 "the expected syntax for either the long form "
186 "or the short form of VECTOR."));
193 while (lex_match ('/'));
197 lex_error (_("expecting end of command"));
204 return CMD_PART_SUCCESS_MAYBE;