f02291e5e72bed4f13b4465941e3f32ca07f6d6c
[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   /* Skip optional FILE=. */
50   if (lex_match_id (lexer, "FILE"))
51     lex_match (lexer, T_EQUALS);
52
53   if (!lex_force_string_or_id (lexer))
54     return CMD_FAILURE;
55
56   char *relative_name = utf8_to_filename (lex_tokcstr (lexer));
57   char *filename = include_path_search (relative_name);
58   free (relative_name);
59
60   if (!filename)
61     {
62       msg (SE, _("Can't find `%s' in include file search path."),
63            lex_tokcstr (lexer));
64       return CMD_FAILURE;
65     }
66   lex_get (lexer);
67
68   enum segmenter_mode syntax_mode = SEG_MODE_INTERACTIVE;
69   enum lex_error_mode error_mode = LEX_ERROR_CONTINUE;
70   bool cd = false;
71   int status = CMD_FAILURE;
72   char *encoding = xstrdup (session_get_default_syntax_encoding (
73                               dataset_session (ds)));
74   while (T_ENDCMD != lex_token (lexer))
75     {
76       if (lex_match_id (lexer, "ENCODING"))
77         {
78           lex_match (lexer, T_EQUALS);
79           if (!lex_force_string (lexer))
80             goto exit;
81
82           free (encoding);
83           encoding = xstrdup (lex_tokcstr (lexer));
84           lex_get (lexer);
85         }
86       else if (variant == INSERT && lex_match_id (lexer, "SYNTAX"))
87         {
88           lex_match (lexer, T_EQUALS);
89           if (lex_match_id (lexer, "INTERACTIVE"))
90             syntax_mode = SEG_MODE_INTERACTIVE;
91           else if (lex_match_id (lexer, "BATCH"))
92             syntax_mode = SEG_MODE_BATCH;
93           else if (lex_match_id (lexer, "AUTO"))
94             syntax_mode = SEG_MODE_AUTO;
95           else
96             {
97               lex_error_expecting (lexer, "BATCH", "INTERACTIVE", "AUTO");
98               goto exit;
99             }
100         }
101       else if (variant == INSERT && lex_match_id (lexer, "CD"))
102         {
103           lex_match (lexer, T_EQUALS);
104           if (lex_match_id (lexer, "YES"))
105             cd = true;
106           else if (lex_match_id (lexer, "NO"))
107             cd = false;
108           else
109             {
110               lex_error_expecting (lexer, "YES", "NO");
111               goto exit;
112             }
113         }
114       else if (variant == INSERT && lex_match_id (lexer, "ERROR"))
115         {
116           lex_match (lexer, T_EQUALS);
117           if (lex_match_id (lexer, "CONTINUE"))
118             error_mode = LEX_ERROR_CONTINUE;
119           else if (lex_match_id (lexer, "STOP"))
120             error_mode = LEX_ERROR_STOP;
121           else if (settings_get_testing_mode ()
122                    && lex_match_id (lexer, "IGNORE"))
123             error_mode = LEX_ERROR_IGNORE;
124           else
125             {
126               lex_error_expecting (lexer, "CONTINUE", "STOP");
127               goto exit;
128             }
129         }
130       else
131         {
132           if (variant == INSERT)
133             lex_error_expecting (lexer, "ENCODING", "SYNTAX", "CD", "ERROR");
134           else
135             lex_error_expecting (lexer, "ENCODING");
136           goto exit;
137         }
138     }
139   status = lex_end_of_command (lexer);
140
141   if (status == CMD_SUCCESS)
142     {
143       struct lex_reader *reader = lex_reader_for_file (filename, encoding,
144                                                        syntax_mode, error_mode);
145       if (reader != NULL)
146         {
147           lex_discard_rest_of_command (lexer);
148           lex_include (lexer, reader);
149
150           if (cd)
151             {
152               char *directory = dir_name (filename);
153               if (chdir (directory))
154                 {
155                   int err = errno;
156                   msg (SE, _("Cannot change directory to %s: %s"), directory,
157                        strerror (err));
158                   status = CMD_FAILURE;
159                 }
160
161               free (directory);
162             }
163         }
164     }
165
166 exit:
167   free (encoding);
168   free (filename);
169   return status;
170 }
171
172 int
173 cmd_include (struct lexer *lexer, struct dataset *ds)
174 {
175   return do_insert (lexer, ds, INCLUDE);
176 }
177
178 int
179 cmd_insert (struct lexer *lexer, struct dataset *ds)
180 {
181   return do_insert (lexer, ds, INSERT);
182 }
183