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