Move var_set and variable parsing declarations into new header.
[pspp-builds.git] / src / language / stats / correlations.q
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/file-handle-def.h>
26 #include <data/procedure.h>
27 #include <data/variable.h>
28 #include <language/command.h>
29 #include <language/data-io/file-handle.h>
30 #include <language/lexer/lexer.h>
31 #include <language/lexer/variable-parser.h>
32 #include <libpspp/alloc.h>
33 #include <libpspp/compiler.h>
34
35 /* (headers) */
36
37 struct cor_set
38   {
39     struct cor_set *next;
40     struct variable **v1, **v2;
41     size_t nv1, nv2;
42   };
43
44 struct cor_set *cor_list, *cor_last;
45
46 struct file_handle *matrix_file;
47
48 static void free_correlations_state (void);
49 static int internal_cmd_correlations (void);
50
51 int
52 cmd_correlations (void)
53 {
54   int result = internal_cmd_correlations ();
55   free_correlations_state ();
56   return result;
57 }
58
59 /* (specification)
60    "CORRELATIONS" (cor_):
61      *variables=custom;
62      +missing=miss:!pairwise/listwise,
63               inc:include/exclude;
64      +print=tail:!twotail/onetail,
65             sig:!sig/nosig;
66      +format=fmt:!matrix/serial;
67      +matrix=custom;
68      +statistics[st_]=descriptives,xprod,all.
69 */
70 /* (declarations) */
71 /* (functions) */
72
73 int
74 internal_cmd_correlations (void)
75 {
76   struct cmd_correlations cmd;
77
78   cor_list = cor_last = NULL;
79   matrix_file = NULL;
80
81   if (!parse_correlations (&cmd))
82     return CMD_FAILURE;
83   free_correlations (&cmd);
84
85   return CMD_SUCCESS;
86 }
87
88 static int
89 cor_custom_variables (struct cmd_correlations *cmd UNUSED)
90 {
91   struct variable **v1, **v2;
92   size_t nv1, nv2;
93   struct cor_set *cor;
94
95   /* Ensure that this is a VARIABLES subcommand. */
96   if (!lex_match_id ("VARIABLES")
97       && (token != T_ID || dict_lookup_var (default_dict, tokid) != NULL)
98       && token != T_ALL)
99     return 2;
100   lex_match ('=');
101
102   if (!parse_variables (default_dict, &v1, &nv1,
103                         PV_NO_DUPLICATE | PV_NUMERIC))
104     return 0;
105   
106   if (lex_match (T_WITH))
107     {
108       if (!parse_variables (default_dict, &v2, &nv2,
109                             PV_NO_DUPLICATE | PV_NUMERIC))
110         {
111           free (v1);
112           return 0;
113         }
114     }
115   else
116     {
117       nv2 = nv1;
118       v2 = v1;
119     }
120
121   cor = xmalloc (sizeof *cor);
122   cor->next = NULL;
123   cor->v1 = v1;
124   cor->v2 = v2;
125   cor->nv1 = nv1;
126   cor->nv2 = nv2;
127   if (cor_list)
128     cor_last = cor_last->next = cor;
129   else
130     cor_list = cor_last = cor;
131   
132   return 1;
133 }
134
135 static int
136 cor_custom_matrix (struct cmd_correlations *cmd UNUSED)
137 {
138   if (!lex_force_match ('('))
139     return 0;
140   
141   if (lex_match ('*'))
142     matrix_file = NULL;
143   else 
144     {
145       matrix_file = fh_parse (FH_REF_FILE);
146       if (matrix_file == NULL)
147         return 0; 
148     }
149
150   if (!lex_force_match (')'))
151     return 0;
152
153   return 1;
154 }
155
156 static void
157 free_correlations_state (void)
158 {
159   struct cor_set *cor, *next;
160
161   for (cor = cor_list; cor != NULL; cor = next)
162     {
163       next = cor->next;
164       if (cor->v1 != cor->v2)
165         free (cor->v2);
166       free (cor->v1);
167       free (cor);
168     }
169 }
170
171 /*
172   Local Variables:
173   mode: c
174   End:
175 */