Adopt use of gnulib for portability.
[pspp-builds.git] / src / numeric.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 "command.h"
24 #include "dictionary.h"
25 #include "error.h"
26 #include "lexer.h"
27 #include "str.h"
28 #include "var.h"
29
30 #include "gettext.h"
31 #define _(msgid) gettext (msgid)
32
33 #include "debug-print.h"
34
35 /* Parses the NUMERIC command. */
36 int
37 cmd_numeric (void)
38 {
39   int i;
40
41   /* Names of variables to create. */
42   char **v;
43   int nv;
44
45   /* Format spec for variables to create.  f.type==-1 if default is to
46      be used. */
47   struct fmt_spec f;
48
49   do
50     {
51       if (!parse_DATA_LIST_vars (&v, &nv, PV_NONE))
52         return CMD_PART_SUCCESS_MAYBE;
53
54       /* Get the optional format specification. */
55       if (lex_match ('('))
56         {
57           if (!parse_format_specifier (&f, 0))
58             goto fail;
59           if (formats[f.type].cat & FCAT_STRING)
60             {
61               msg (SE, _("Format type %s may not be used with a numeric "
62                    "variable."), fmt_to_string (&f));
63               goto fail;
64             }
65
66           if (!lex_match (')'))
67             {
68               msg (SE, _("`)' expected after output format."));
69               goto fail;
70             }
71         }
72       else
73         f.type = -1;
74
75       /* Create each variable. */
76       for (i = 0; i < nv; i++)
77         {
78           struct variable *new_var = dict_create_var (default_dict, v[i], 0);
79           if (!new_var)
80             msg (SE, _("There is already a variable named %s."), v[i]);
81           else
82             {
83               if (f.type != -1)
84                 new_var->print = new_var->write = f;
85             }
86         }
87
88       /* Clean up. */
89       for (i = 0; i < nv; i++)
90         free (v[i]);
91       free (v);
92     }
93   while (lex_match ('/'));
94
95   return lex_end_of_command ();
96
97   /* If we have an error at a point where cleanup is required,
98      flow-of-control comes here. */
99 fail:
100   for (i = 0; i < nv; i++)
101     free (v[i]);
102   free (v);
103   return CMD_PART_SUCCESS_MAYBE;
104 }
105
106 /* Parses the STRING command. */
107 int
108 cmd_string (void)
109 {
110   int i;
111
112   /* Names of variables to create. */
113   char **v;
114   int nv;
115
116   /* Format spec for variables to create. */
117   struct fmt_spec f;
118
119   /* Width of variables to create. */
120   int width;
121
122   do
123     {
124       if (!parse_DATA_LIST_vars (&v, &nv, PV_NONE))
125         return CMD_PART_SUCCESS_MAYBE;
126
127       if (!lex_force_match ('(')
128           || !parse_format_specifier (&f, 0))
129         goto fail;
130       if (!(formats[f.type].cat & FCAT_STRING))
131         {
132           msg (SE, _("Format type %s may not be used with a string "
133                "variable."), fmt_to_string (&f));
134           goto fail;
135         }
136
137       if (!lex_match (')'))
138         {
139           msg (SE, _("`)' expected after output format."));
140           goto fail;
141         }
142
143       switch (f.type)
144         {
145         case FMT_A:
146           width = f.w;
147           break;
148         case FMT_AHEX:
149           width = f.w / 2;
150           break;
151         default:
152           assert (0);
153           abort ();
154         }
155
156       /* Create each variable. */
157       for (i = 0; i < nv; i++)
158         {
159           struct variable *new_var = dict_create_var (default_dict, v[i],
160                                                       width);
161           if (!new_var)
162             msg (SE, _("There is already a variable named %s."), v[i]);
163           else
164             new_var->print = new_var->write = f;
165         }
166
167       /* Clean up. */
168       for (i = 0; i < nv; i++)
169         free (v[i]);
170       free (v);
171     }
172   while (lex_match ('/'));
173
174   return lex_end_of_command ();
175
176   /* If we have an error at a point where cleanup is required,
177      flow-of-control comes here. */
178 fail:
179   for (i = 0; i < nv; i++)
180     free (v[i]);
181   free (v);
182   return CMD_PART_SUCCESS_MAYBE;
183 }
184
185 /* Parses the LEAVE command. */
186 int
187 cmd_leave (void)
188 {
189   struct variable **v;
190   int nv;
191
192   int i;
193
194   if (!parse_variables (default_dict, &v, &nv, PV_NONE))
195     return CMD_FAILURE;
196   for (i = 0; i < nv; i++)
197     {
198       if (!v[i]->reinit)
199         continue;
200       v[i]->reinit = 0;
201       v[i]->init = 1;
202     }
203   free (v);
204
205   return lex_end_of_command ();
206 }