90f245c85f4b84b9ca54490667fa7fd751aeadd9
[pspp-builds.git] / src / language / dictionary / vector.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
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.
9
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.
14
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
18    02110-1301, USA. */
19
20 #include <config.h>
21
22 #include <stdlib.h>
23
24 #include <procedure.h>
25 #include <data/dictionary.h>
26 #include <data/variable.h>
27 #include <language/command.h>
28 #include <language/lexer/lexer.h>
29 #include <libpspp/alloc.h>
30 #include <libpspp/message.h>
31 #include <libpspp/misc.h>
32 #include <libpspp/str.h>
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 int
38 cmd_vector (void)
39 {
40   /* Just to be different, points to a set of null terminated strings
41      containing the names of the vectors to be created.  The list
42      itself is terminated by a empty string.  So a list of three
43      elements, A B C, would look like this: "A\0B\0C\0\0". */
44   char *vecnames;
45
46   /* vecnames iterators. */
47   char *cp, *cp2;
48
49   /* Maximum allocated position for vecnames, plus one position. */
50   char *endp = NULL;
51
52   cp = vecnames = xmalloc (256);
53   endp = &vecnames[256];
54   do
55     {
56       /* Get the name(s) of the new vector(s). */
57       if (!lex_force_id ())
58         return CMD_CASCADING_FAILURE;
59       while (token == T_ID)
60         {
61           if (cp + 16 > endp)
62             {
63               char *old_vecnames = vecnames;
64               vecnames = xrealloc (vecnames, endp - vecnames + 256);
65               cp = (cp - old_vecnames) + vecnames;
66               endp = (endp - old_vecnames) + vecnames + 256;
67             }
68
69           for (cp2 = cp; cp2 < cp; cp2 += strlen (cp))
70             if (!strcasecmp (cp2, tokid))
71               {
72                 msg (SE, _("Vector name %s is given twice."), tokid);
73                 goto fail;
74               }
75
76           if (dict_lookup_vector (default_dict, tokid))
77             {
78               msg (SE, _("There is already a vector with name %s."), tokid);
79               goto fail;
80             }
81
82           cp = stpcpy (cp, tokid) + 1;
83           lex_get ();
84           lex_match (',');
85         }
86       *cp++ = 0;
87
88       /* Now that we have the names it's time to check for the short
89          or long forms. */
90       if (lex_match ('='))
91         {
92           /* Long form. */
93           struct variable **v;
94           size_t nv;
95
96           if (strchr (vecnames, '\0')[1])
97             {
98               /* There's more than one vector name. */
99               msg (SE, _("A slash must be used to separate each vector "
100                          "specification when using the long form.  Commands "
101                          "such as VECTOR A,B=Q1 TO Q20 are not supported."));
102               goto fail;
103             }
104
105           if (!parse_variables (default_dict, &v, &nv,
106                                 PV_SAME_TYPE | PV_DUPLICATE))
107             goto fail;
108
109           dict_create_vector (default_dict, vecnames, v, nv);
110           free (v);
111         }
112       else if (lex_match ('('))
113         {
114           int i;
115
116           /* Maximum number of digits in a number to add to the base
117              vecname. */
118           int ndig;
119
120           /* Name of an individual variable to be created. */
121           char name[SHORT_NAME_LEN + 1];
122
123           /* Vector variables. */
124           struct variable **v;
125           int nv;
126
127           if (!lex_force_int ())
128             return CMD_CASCADING_FAILURE;
129           nv = lex_integer ();
130           lex_get ();
131           if (nv <= 0)
132             {
133               msg (SE, _("Vectors must have at least one element."));
134               goto fail;
135             }
136           if (!lex_force_match (')'))
137             goto fail;
138
139           /* First check that all the generated variable names
140              are LONG_NAME_LEN characters or shorter. */
141           ndig = intlog10 (nv);
142           for (cp = vecnames; *cp;)
143             {
144               int len = strlen (cp);
145               if (len + ndig > LONG_NAME_LEN)
146                 {
147                   msg (SE, _("%s%d is too long for a variable name."), cp, nv);
148                   goto fail;
149                 }
150               cp += len + 1;
151             }
152
153           /* Next check that none of the variables exist. */
154           for (cp = vecnames; *cp;)
155             {
156               for (i = 0; i < nv; i++)
157                 {
158                   sprintf (name, "%s%d", cp, i + 1);
159                   if (dict_lookup_var (default_dict, name))
160                     {
161                       msg (SE, _("There is already a variable named %s."),
162                            name);
163                       goto fail;
164                     }
165                 }
166               cp += strlen (cp) + 1;
167             }
168
169           /* Finally create the variables and vectors. */
170           v = xmalloc (nv * sizeof *v);
171           for (cp = vecnames; *cp;)
172             {
173               for (i = 0; i < nv; i++)
174                 {
175                   sprintf (name, "%s%d", cp, i + 1);
176                   v[i] = dict_create_var_assert (default_dict, name, 0);
177                 }
178               if (!dict_create_vector (default_dict, cp, v, nv))
179                 assert (0);
180               cp += strlen (cp) + 1;
181             }
182           free (v);
183         }
184       else
185         {
186           msg (SE, _("The syntax for this command does not match "
187                "the expected syntax for either the long form "
188                "or the short form of VECTOR."));
189           goto fail;
190         }
191
192       free (vecnames);
193       vecnames = NULL;
194     }
195   while (lex_match ('/'));
196
197   if (token != '.')
198     {
199       lex_error (_("expecting end of command"));
200       goto fail;
201     }
202   return CMD_SUCCESS;
203
204 fail:
205   free (vecnames);
206   return CMD_FAILURE;
207 }