1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2010, 2011, 2020 Free Software Foundation, Inc.
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.
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.
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/>. */
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"
34 #include "gl/dirname.h"
35 #include "gl/xalloc.h"
38 #define _(msgid) gettext (msgid)
47 do_insert (struct lexer *lexer, struct dataset *ds, enum variant variant)
49 enum segmenter_mode syntax_mode;
50 enum lex_error_mode error_mode;
57 /* Skip optional FILE=. */
58 if (lex_match_id (lexer, "FILE"))
59 lex_match (lexer, T_EQUALS);
61 if (!lex_force_string_or_id (lexer))
64 relative_name = utf8_to_filename (lex_tokcstr (lexer));
65 if (NULL == relative_name)
68 filename = include_path_search (relative_name);
73 msg (SE, _("Can't find `%s' in include file search path."),
79 syntax_mode = SEG_MODE_INTERACTIVE;
80 error_mode = LEX_ERROR_CONTINUE;
83 encoding = xstrdup (session_get_default_syntax_encoding (
84 dataset_session (ds)));
85 while (T_ENDCMD != lex_token (lexer))
87 if (lex_match_id (lexer, "ENCODING"))
89 lex_match (lexer, T_EQUALS);
90 if (!lex_force_string (lexer))
94 encoding = xstrdup (lex_tokcstr (lexer));
97 else if (variant == INSERT && lex_match_id (lexer, "SYNTAX"))
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;
108 lex_error_expecting (lexer, "BATCH", "INTERACTIVE", "AUTO");
112 else if (variant == INSERT && lex_match_id (lexer, "CD"))
114 lex_match (lexer, T_EQUALS);
115 if (lex_match_id (lexer, "YES"))
119 else if (lex_match_id (lexer, "NO"))
125 lex_error_expecting (lexer, "YES", "NO");
129 else if (variant == INSERT && lex_match_id (lexer, "ERROR"))
131 lex_match (lexer, T_EQUALS);
132 if (lex_match_id (lexer, "CONTINUE"))
134 error_mode = LEX_ERROR_CONTINUE;
136 else if (lex_match_id (lexer, "STOP"))
138 error_mode = LEX_ERROR_STOP;
142 lex_error_expecting (lexer, "CONTINUE", "STOP");
149 lex_error (lexer, NULL);
153 status = lex_end_of_command (lexer);
155 if (status == CMD_SUCCESS)
157 struct lex_reader *reader;
159 reader = lex_reader_for_file (filename, encoding,
160 syntax_mode, error_mode);
163 lex_discard_rest_of_command (lexer);
164 lex_include (lexer, reader);
168 char *directory = dir_name (filename);
169 int ret = chdir (directory);
173 msg (SE, _("Cannot change directory to %s: %s"), directory,
175 status = CMD_FAILURE;
190 cmd_include (struct lexer *lexer, struct dataset *ds)
192 return do_insert (lexer, ds, INCLUDE);
196 cmd_insert (struct lexer *lexer, struct dataset *ds)
198 return do_insert (lexer, ds, INSERT);