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