Remove unnecessary #include directives
[pspp] / src / language / utilities / include.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007, 2010, 2011 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 <errno.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include "data/dataset.h"
26 #include "data/session.h"
27 #include "language/command.h"
28 #include "language/lexer/include-path.h"
29 #include "language/lexer/lexer.h"
30 #include "libpspp/i18n.h"
31 #include "libpspp/message.h"
32 #include "libpspp/str.h"
33
34 #include "gl/dirname.h"
35 #include "gl/xalloc.h"
36
37 #include "gettext.h"
38 #define _(msgid) gettext (msgid)
39
40 enum variant
41   {
42     INSERT,
43     INCLUDE
44   };
45
46 static int
47 do_insert (struct lexer *lexer, struct dataset *ds, enum variant variant)
48 {
49   enum lex_syntax_mode syntax_mode;
50   enum lex_error_mode error_mode;
51   char *relative_name;
52   char *filename;
53   char *encoding;
54   int status;
55   bool cd;
56
57   /* Skip optional FILE=. */
58   if (lex_match_id (lexer, "FILE"))
59     lex_match (lexer, T_EQUALS);
60
61   if (!lex_force_string_or_id (lexer))
62     return CMD_FAILURE;
63
64   relative_name = utf8_to_filename (lex_tokcstr (lexer)); 
65   filename = include_path_search (relative_name);
66   free (relative_name);
67
68   if ( ! filename)
69     {
70       msg (SE, _("Can't find `%s' in include file search path."),
71            lex_tokcstr (lexer));
72       return CMD_FAILURE;
73     }
74   lex_get (lexer);
75
76   syntax_mode = LEX_SYNTAX_INTERACTIVE;
77   error_mode = LEX_ERROR_CONTINUE;
78   cd = false;
79   status = CMD_FAILURE;
80   encoding = xstrdup (session_get_default_syntax_encoding (
81                         dataset_session (ds)));
82   while ( T_ENDCMD != lex_token (lexer))
83     {
84       if (lex_match_id (lexer, "ENCODING"))
85         {
86           lex_match (lexer, T_EQUALS);
87           if (!lex_force_string (lexer))
88             goto exit;
89
90           free (encoding);
91           encoding = xstrdup (lex_tokcstr (lexer));
92           lex_get (lexer);
93         }
94       else if (variant == INSERT && lex_match_id (lexer, "SYNTAX"))
95         {
96           lex_match (lexer, T_EQUALS);
97           if ( lex_match_id (lexer, "INTERACTIVE") )
98             syntax_mode = LEX_SYNTAX_INTERACTIVE;
99           else if ( lex_match_id (lexer, "BATCH"))
100             syntax_mode = LEX_SYNTAX_BATCH;
101           else if ( lex_match_id (lexer, "AUTO"))
102             syntax_mode = LEX_SYNTAX_AUTO;
103           else
104             {
105               lex_error_expecting (lexer, "BATCH", "INTERACTIVE", "AUTO",
106                                    NULL_SENTINEL);
107               goto exit;
108             }
109         }
110       else if (variant == INSERT && lex_match_id (lexer, "CD"))
111         {
112           lex_match (lexer, T_EQUALS);
113           if ( lex_match_id (lexer, "YES") )
114             {
115               cd = true;
116             }
117           else if ( lex_match_id (lexer, "NO"))
118             {
119               cd = false;
120             }
121           else
122             {
123               lex_error_expecting (lexer, "YES", "NO", NULL_SENTINEL);
124               goto exit;
125             }
126         }
127       else if (variant == INSERT && lex_match_id (lexer, "ERROR"))
128         {
129           lex_match (lexer, T_EQUALS);
130           if ( lex_match_id (lexer, "CONTINUE") )
131             {
132               error_mode = LEX_ERROR_CONTINUE;
133             }
134           else if ( lex_match_id (lexer, "STOP"))
135             {
136               error_mode = LEX_ERROR_STOP;
137             }
138           else
139             {
140               lex_error_expecting (lexer, "CONTINUE", "STOP", NULL_SENTINEL);
141               goto exit;
142             }
143         }
144
145       else
146         {
147           lex_error (lexer, NULL);
148           goto exit;
149         }
150     }
151   status = lex_end_of_command (lexer);
152
153   if ( status == CMD_SUCCESS)
154     {
155       struct lex_reader *reader;
156
157       reader = lex_reader_for_file (filename, encoding,
158                                     syntax_mode, error_mode);
159       if (reader != NULL)
160         {
161           lex_discard_rest_of_command (lexer);
162           lex_include (lexer, reader);
163
164           if ( cd )
165             {
166               char *directory = dir_name (filename);
167               chdir (directory);
168               free (directory);
169             }
170         }
171     }
172
173 exit:
174   free (encoding);
175   free (filename);
176   return status;
177 }
178
179 int
180 cmd_include (struct lexer *lexer, struct dataset *ds)
181 {
182   return do_insert (lexer, ds, INCLUDE);
183 }
184
185 int
186 cmd_insert (struct lexer *lexer, struct dataset *ds)
187 {
188   return do_insert (lexer, ds, INSERT);
189 }
190