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