Adopt use of gnulib for portability.
[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., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include "error.h"
22 #include <stdlib.h>
23 #include "alloc.h"
24 #include "command.h"
25 #include "dictionary.h"
26 #include "error.h"
27 #include "lexer.h"
28 #include "misc.h"
29 #include "str.h"
30 #include "var.h"
31
32 #include "gettext.h"
33 #define _(msgid) gettext (msgid)
34
35 int
36 cmd_vector (void)
37 {
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". */
42   char *vecnames;
43
44   /* vecnames iterators. */
45   char *cp, *cp2;
46
47   /* Maximum allocated position for vecnames, plus one position. */
48   char *endp = NULL;
49
50   cp = vecnames = xmalloc (256);
51   endp = &vecnames[256];
52   do
53     {
54       /* Get the name(s) of the new vector(s). */
55       if (!lex_force_id ())
56         return CMD_FAILURE;
57       while (token == T_ID)
58         {
59           if (cp + 16 > endp)
60             {
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;
65             }
66
67           for (cp2 = cp; cp2 < cp; cp2 += strlen (cp))
68             if (!strcasecmp (cp2, tokid))
69               {
70                 msg (SE, _("Vector name %s is given twice."), tokid);
71                 goto fail;
72               }
73
74           if (dict_lookup_vector (default_dict, tokid))
75             {
76               msg (SE, _("There is already a vector with name %s."), tokid);
77               goto fail;
78             }
79
80           cp = stpcpy (cp, tokid) + 1;
81           lex_get ();
82           lex_match (',');
83         }
84       *cp++ = 0;
85
86       /* Now that we have the names it's time to check for the short
87          or long forms. */
88       if (lex_match ('='))
89         {
90           /* Long form. */
91           struct variable **v;
92           int nv;
93
94           if (strchr (vecnames, '\0')[1])
95             {
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."));
100               goto fail;
101             }
102
103           if (!parse_variables (default_dict, &v, &nv,
104                                 PV_SAME_TYPE | PV_DUPLICATE))
105             goto fail;
106
107           dict_create_vector (default_dict, vecnames, v, nv);
108           free (v);
109         }
110       else if (lex_match ('('))
111         {
112           int i;
113
114           /* Maximum number of digits in a number to add to the base
115              vecname. */
116           int ndig;
117
118           /* Name of an individual variable to be created. */
119           char name[SHORT_NAME_LEN + 1];
120
121           /* Vector variables. */
122           struct variable **v;
123           int nv;
124
125           if (!lex_force_int ())
126             return CMD_FAILURE;
127           nv = lex_integer ();
128           lex_get ();
129           if (nv <= 0)
130             {
131               msg (SE, _("Vectors must have at least one element."));
132               goto fail;
133             }
134           if (!lex_force_match (')'))
135             goto fail;
136
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;)
141             {
142               int len = strlen (cp);
143               if (len + ndig > LONG_NAME_LEN)
144                 {
145                   msg (SE, _("%s%d is too long for a variable name."), cp, nv);
146                   goto fail;
147                 }
148               cp += len + 1;
149             }
150
151           /* Next check that none of the variables exist. */
152           for (cp = vecnames; *cp;)
153             {
154               for (i = 0; i < nv; i++)
155                 {
156                   sprintf (name, "%s%d", cp, i + 1);
157                   if (dict_lookup_var (default_dict, name))
158                     {
159                       msg (SE, _("There is already a variable named %s."),
160                            name);
161                       goto fail;
162                     }
163                 }
164               cp += strlen (cp) + 1;
165             }
166
167           /* Finally create the variables and vectors. */
168           v = xmalloc (nv * sizeof *v);
169           for (cp = vecnames; *cp;)
170             {
171               for (i = 0; i < nv; i++)
172                 {
173                   sprintf (name, "%s%d", cp, i + 1);
174                   v[i] = dict_create_var_assert (default_dict, name, 0);
175                 }
176               if (!dict_create_vector (default_dict, cp, v, nv))
177                 assert (0);
178               cp += strlen (cp) + 1;
179             }
180           free (v);
181         }
182       else
183         {
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."));
187           goto fail;
188         }
189
190       free (vecnames);
191       vecnames = NULL;
192     }
193   while (lex_match ('/'));
194
195   if (token != '.')
196     {
197       lex_error (_("expecting end of command"));
198       goto fail;
199     }
200   return CMD_SUCCESS;
201
202 fail:
203   free (vecnames);
204   return CMD_PART_SUCCESS_MAYBE;
205 }