82b58c0240f633a33cfa4da34e1109641606ebbc
[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 <language/lexer/variable-parser.h>
30 #include <libpspp/assertion.h>
31 #include <libpspp/message.h>
32 #include <libpspp/str.h>
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 /* Parses the NUMERIC command. */
38 int
39 cmd_numeric (void)
40 {
41   size_t i;
42
43   /* Names of variables to create. */
44   char **v;
45   size_t nv;
46
47   /* Format spec for variables to create.  f.type==-1 if default is to
48      be used. */
49   struct fmt_spec f;
50
51   do
52     {
53       if (!parse_DATA_LIST_vars (&v, &nv, PV_NONE))
54         return CMD_FAILURE;
55
56       /* Get the optional format specification. */
57       if (lex_match ('('))
58         {
59           if (!parse_format_specifier (&f, 0))
60             goto fail;
61           if (formats[f.type].cat & FCAT_STRING)
62             {
63               msg (SE, _("Format type %s may not be used with a numeric "
64                    "variable."), fmt_to_string (&f));
65               goto fail;
66             }
67
68           if (!lex_match (')'))
69             {
70               msg (SE, _("`)' expected after output format."));
71               goto fail;
72             }
73         }
74       else
75         f.type = -1;
76
77       /* Create each variable. */
78       for (i = 0; i < nv; i++)
79         {
80           struct variable *new_var = dict_create_var (default_dict, v[i], 0);
81           if (!new_var)
82             msg (SE, _("There is already a variable named %s."), v[i]);
83           else
84             {
85               if (f.type != -1)
86                 new_var->print = new_var->write = f;
87             }
88         }
89
90       /* Clean up. */
91       for (i = 0; i < nv; i++)
92         free (v[i]);
93       free (v);
94     }
95   while (lex_match ('/'));
96
97   return lex_end_of_command ();
98
99   /* If we have an error at a point where cleanup is required,
100      flow-of-control comes here. */
101 fail:
102   for (i = 0; i < nv; i++)
103     free (v[i]);
104   free (v);
105   return CMD_FAILURE;
106 }
107
108 /* Parses the STRING command. */
109 int
110 cmd_string (void)
111 {
112   size_t i;
113
114   /* Names of variables to create. */
115   char **v;
116   size_t nv;
117
118   /* Format spec for variables to create. */
119   struct fmt_spec f;
120
121   /* Width of variables to create. */
122   int width;
123
124   do
125     {
126       if (!parse_DATA_LIST_vars (&v, &nv, PV_NONE))
127         return CMD_FAILURE;
128
129       if (!lex_force_match ('(')
130           || !parse_format_specifier (&f, 0))
131         goto fail;
132       if (!(formats[f.type].cat & FCAT_STRING))
133         {
134           msg (SE, _("Format type %s may not be used with a string "
135                "variable."), fmt_to_string (&f));
136           goto fail;
137         }
138
139       if (!lex_match (')'))
140         {
141           msg (SE, _("`)' expected after output format."));
142           goto fail;
143         }
144
145       switch (f.type)
146         {
147         case FMT_A:
148           width = f.w;
149           break;
150         case FMT_AHEX:
151           width = f.w / 2;
152           break;
153         default:
154           NOT_REACHED ();
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 }