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