f0d9efb5da24f4d4f5242d2b9d74289949708c4e
[pspp] / src / language / utilities / include.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007, 2010, 2011, 2020 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 segmenter_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   if (NULL == relative_name)
66     return CMD_FAILURE;
67
68   filename = include_path_search (relative_name);
69   free (relative_name);
70
71   if (! filename)
72     {
73       msg (SE, _("Can't find `%s' in include file search path."),
74            lex_tokcstr (lexer));
75       return CMD_FAILURE;
76     }
77   lex_get (lexer);
78
79   syntax_mode = SEG_MODE_INTERACTIVE;
80   error_mode = LEX_ERROR_CONTINUE;
81   cd = false;
82   status = CMD_FAILURE;
83   encoding = xstrdup (session_get_default_syntax_encoding (
84                         dataset_session (ds)));
85   while (T_ENDCMD != lex_token (lexer))
86     {
87       if (lex_match_id (lexer, "ENCODING"))
88         {
89           lex_match (lexer, T_EQUALS);
90           if (!lex_force_string (lexer))
91             goto exit;
92
93           free (encoding);
94           encoding = xstrdup (lex_tokcstr (lexer));
95           lex_get (lexer);
96         }
97       else if (variant == INSERT && lex_match_id (lexer, "SYNTAX"))
98         {
99           lex_match (lexer, T_EQUALS);
100           if (lex_match_id (lexer, "INTERACTIVE"))
101             syntax_mode = SEG_MODE_INTERACTIVE;
102           else if (lex_match_id (lexer, "BATCH"))
103             syntax_mode = SEG_MODE_BATCH;
104           else if (lex_match_id (lexer, "AUTO"))
105             syntax_mode = SEG_MODE_AUTO;
106           else
107             {
108               lex_error_expecting (lexer, "BATCH", "INTERACTIVE", "AUTO");
109               goto exit;
110             }
111         }
112       else if (variant == INSERT && lex_match_id (lexer, "CD"))
113         {
114           lex_match (lexer, T_EQUALS);
115           if (lex_match_id (lexer, "YES"))
116             {
117               cd = true;
118             }
119           else if (lex_match_id (lexer, "NO"))
120             {
121               cd = false;
122             }
123           else
124             {
125               lex_error_expecting (lexer, "YES", "NO");
126               goto exit;
127             }
128         }
129       else if (variant == INSERT && lex_match_id (lexer, "ERROR"))
130         {
131           lex_match (lexer, T_EQUALS);
132           if (lex_match_id (lexer, "CONTINUE"))
133             error_mode = LEX_ERROR_CONTINUE;
134           else if (lex_match_id (lexer, "STOP"))
135             error_mode = LEX_ERROR_STOP;
136           else if (settings_get_testing_mode ()
137                    && lex_match_id (lexer, "IGNORE"))
138             error_mode = LEX_ERROR_IGNORE;
139           else
140             {
141               lex_error_expecting (lexer, "CONTINUE", "STOP");
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               int ret = chdir (directory);
169               if (0 != ret)
170                 {
171                   int err = errno;
172                   msg (SE, _("Cannot change directory to %s: %s"), directory,
173                        strerror (err));
174                   status = CMD_FAILURE;
175                 }
176
177               free (directory);
178             }
179         }
180     }
181
182 exit:
183   free (encoding);
184   free (filename);
185   return status;
186 }
187
188 int
189 cmd_include (struct lexer *lexer, struct dataset *ds)
190 {
191   return do_insert (lexer, ds, INCLUDE);
192 }
193
194 int
195 cmd_insert (struct lexer *lexer, struct dataset *ds)
196 {
197   return do_insert (lexer, ds, INSERT);
198 }
199