Add OUTPUT MODIFY command
[pspp] / src / language / utilities / output.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2014 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <ctype.h>
20 #include <stdlib.h>
21
22 #include "data/dataset.h"
23 #include "data/settings.h"
24 #include "data/format.h"
25 #include "language/command.h"
26 #include "language/lexer/lexer.h"
27 #include "language/lexer/format-parser.h"
28 #include "libpspp/message.h"
29 #include "libpspp/version.h"
30 #include "output/tab.h"
31
32 #include "gl/xalloc.h"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 struct thing
38 {
39   const char *identifier;
40   enum result_class rc;
41 };
42
43 extern struct fmt_spec ugly [n_RC];
44
45 static const struct thing things[] = 
46   {
47     {"SIGNIFICANCE", RC_PVALUE},
48     {"COUNT" ,RC_WEIGHT}
49   };
50
51 #define N_THINGS (sizeof (things) / sizeof (struct thing))
52
53 struct output_spec
54 {
55   /* An array of classes */
56   enum result_class *rc;
57
58   int n_rc;
59
60   /* The format to be applied to these classes */
61   struct fmt_spec fmt;
62 };
63
64 int
65 cmd_output (struct lexer *lexer, struct dataset *ds UNUSED)
66 {
67   int j, i;
68   struct output_spec *output_specs = NULL;
69   int n_os = 0;
70   
71   if (!lex_force_match_id (lexer, "MODIFY"))
72     {
73       lex_error (lexer, NULL);
74       goto error;
75     }
76
77   while (lex_token (lexer) != T_ENDCMD)
78     {
79       lex_match (lexer, T_SLASH);
80
81       if (lex_match_id (lexer, "SELECT"))
82         {
83           if (!lex_match_id (lexer, "TABLES"))
84             {
85               lex_error (lexer, NULL);
86               goto error;
87             }
88         }
89       else if (lex_match_id (lexer, "TABLECELLS"))
90         {
91           struct output_spec *os;
92           output_specs = xrealloc (output_specs, sizeof (*output_specs) * ++n_os);
93           os = &output_specs[n_os - 1];
94           os->n_rc = 0;
95           os->rc = NULL;
96           
97           while (lex_token (lexer) != T_SLASH && 
98                  lex_token (lexer) != T_ENDCMD)
99             {
100               if (lex_match_id (lexer, "SELECT"))
101                 {
102                   lex_force_match (lexer, T_EQUALS);
103                   lex_force_match (lexer, T_LBRACK);
104                   
105                   while (lex_token (lexer) != T_RBRACK &&
106                          lex_token (lexer) != T_ENDCMD)
107                     {
108                       int i;
109                       for (i = 0 ; i < N_THINGS; ++i)
110                         {
111                           if (lex_match_id (lexer, things[i].identifier))
112                             {
113                               os->rc = xrealloc (os->rc, sizeof (*os->rc) * ++os->n_rc);
114                               os->rc[os->n_rc - 1] = things[i].rc;
115                               break;
116                             }
117                         }
118                       if (i >= N_THINGS)
119                         {
120                           lex_error (lexer, _("Unknown TABLECELLS class"));
121                           goto error;
122                         }
123                     }
124                   lex_force_match (lexer, T_RBRACK);
125                 }
126               else if (lex_match_id (lexer, "FORMAT"))
127                 {
128                   struct fmt_spec fmt;    
129                   char type[FMT_TYPE_LEN_MAX + 1];
130                   int width = -1;
131                   int decimals = -1;
132
133                   lex_force_match (lexer, T_EQUALS);
134                   if (! parse_abstract_format_specifier (lexer, type, &width, &decimals))
135                     {
136                       lex_error (lexer, NULL);
137                       goto error;
138                     }
139
140                   if (width <= 0)
141                     {
142                       const struct fmt_spec *dflt = settings_get_format ();
143                       width = dflt->w;
144                     }
145
146                   if (!fmt_from_name (type, &fmt.type))
147                     {
148                       lex_error (lexer, _("Unknown format type `%s'."), type);
149                       goto error;
150                     }
151
152                   fmt.w = width;
153                   fmt.d = decimals;
154
155                   os->fmt = fmt;
156                 }
157               else 
158                 {
159                   lex_error (lexer, NULL);
160                   goto error;
161             
162                 }
163             }
164         }
165       else 
166         {
167           lex_error (lexer, NULL);
168           goto error;   
169
170         }
171     }
172
173   /* Populate the global table, with the values we parsed */
174   for (i = 0; i < n_os; ++i)
175     {
176       for (j = 0; j < output_specs[i].n_rc;  ++j)
177         {
178           ugly [output_specs[i].rc[j]] = output_specs[i].fmt;
179         }
180     }
181   
182   for (j = 0; j < n_os;  ++j)
183     free (output_specs[j].rc);
184   free (output_specs);
185   return CMD_SUCCESS;
186  error:
187
188   for (j = 0; j < n_os;  ++j)
189     free (output_specs[j].rc);
190   free (output_specs);
191   return CMD_SUCCESS;
192 }