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