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