Fixed bug reporting the significance of paired value t-test.
[pspp-builds.git] / src / language / utilities / include.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007 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 #include <ctype.h>
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <language/command.h>
23 #include <libpspp/message.h>
24 #include <libpspp/getl.h>
25 #include <language/syntax-file.h>
26 #include <language/lexer/lexer.h>
27 #include <libpspp/str.h>
28 #include <data/file-name.h>
29
30 #include "dirname.h"
31 #include "canonicalize.h"
32 #include "xalloc.h"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 static int parse_insert (struct lexer *lexer, char **filename);
38
39
40 int
41 cmd_include (struct lexer *lexer, struct dataset *ds UNUSED)
42 {
43   char *filename = NULL;
44   int status = parse_insert (lexer, &filename);
45
46   if ( CMD_SUCCESS != status)
47     return status;
48
49   lex_get (lexer);
50
51   status = lex_end_of_command (lexer);
52
53   if ( status == CMD_SUCCESS)
54     {
55       struct source_stream *ss = lex_get_source_stream (lexer);
56
57       assert (filename);
58       getl_include_source (ss, create_syntax_file_source (filename),
59                            GETL_BATCH, ERRMODE_STOP);
60       free (filename);
61     }
62
63   return status;
64 }
65
66
67 int
68 cmd_insert (struct lexer *lexer, struct dataset *ds UNUSED)
69 {
70   enum syntax_mode syntax_mode = GETL_INTERACTIVE;
71   enum error_mode error_mode = ERRMODE_CONTINUE;
72   char *filename = NULL;
73   int status = parse_insert (lexer, &filename);
74   bool cd = false;
75
76   if ( CMD_SUCCESS != status)
77     return status;
78
79   lex_get (lexer);
80
81   while ( '.' != lex_token (lexer))
82     {
83       if (lex_match_id (lexer, "SYNTAX"))
84         {
85           lex_match (lexer, '=');
86           if ( lex_match_id (lexer, "INTERACTIVE") )
87             syntax_mode = GETL_INTERACTIVE;
88           else if ( lex_match_id (lexer, "BATCH"))
89             syntax_mode = GETL_BATCH;
90           else
91             {
92               lex_error(lexer,
93                         _("Expecting BATCH or INTERACTIVE after SYNTAX."));
94               return CMD_FAILURE;
95             }
96         }
97       else if (lex_match_id (lexer, "CD"))
98         {
99           lex_match (lexer, '=');
100           if ( lex_match_id (lexer, "YES") )
101             {
102               cd = true;
103             }
104           else if ( lex_match_id (lexer, "NO"))
105             {
106               cd = false;
107             }
108           else
109             {
110               lex_error (lexer, _("Expecting YES or NO after CD."));
111               return CMD_FAILURE;
112             }
113         }
114       else if (lex_match_id (lexer, "ERROR"))
115         {
116           lex_match (lexer, '=');
117           if ( lex_match_id (lexer, "CONTINUE") )
118             {
119               error_mode = ERRMODE_CONTINUE;
120             }
121           else if ( lex_match_id (lexer, "STOP"))
122             {
123               error_mode = ERRMODE_STOP;
124             }
125           else
126             {
127               lex_error (lexer, _("Expecting CONTINUE or STOP after ERROR."));
128               return CMD_FAILURE;
129             }
130         }
131
132       else
133         {
134           lex_error (lexer, _("Unexpected token: `%s'."),
135                      lex_token_representation (lexer));
136
137           return CMD_FAILURE;
138         }
139     }
140
141   status = lex_end_of_command (lexer);
142
143   if ( status == CMD_SUCCESS)
144     {
145       struct source_stream *ss = lex_get_source_stream (lexer);
146
147       assert (filename);
148       getl_include_source (ss, create_syntax_file_source (filename),
149                            syntax_mode,
150                            error_mode);
151
152       if ( cd )
153         {
154           char *directory = dir_name (filename);
155           chdir (directory);
156           free (directory);
157         }
158
159       free (filename);
160     }
161
162   return status;
163 }
164
165
166 static int
167 parse_insert (struct lexer *lexer, char **filename)
168 {
169   char *target_fn;
170   char *relative_filename;
171
172   /* Skip optional FILE=. */
173   if (lex_match_id (lexer, "FILE"))
174     lex_match (lexer, '=');
175
176   /* File name can be identifier or string. */
177   if (lex_token (lexer) != T_ID && lex_token (lexer) != T_STRING)
178     {
179       lex_error (lexer, _("expecting file name"));
180       return CMD_FAILURE;
181     }
182
183   target_fn = ds_cstr (lex_tokstr (lexer));
184
185   relative_filename =
186     fn_search_path (target_fn,
187                     getl_include_path (lex_get_source_stream (lexer)));
188
189   if ( ! relative_filename)
190     {
191       msg (SE, _("Can't find `%s' in include file search path."),
192          target_fn);
193       return CMD_FAILURE;
194     }
195
196   *filename = canonicalize_file_name (relative_filename);
197   if (*filename == NULL) 
198     {
199       msg (SE, _("Unable to open `%s': %s."),
200            relative_filename, strerror (errno));
201       free (relative_filename);
202       return CMD_FAILURE;
203     }
204   free (relative_filename);
205
206   return CMD_SUCCESS;
207 }