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