1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2010, 2011 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/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"
35 #include "gl/dirname.h"
36 #include "gl/xalloc.h"
39 #define _(msgid) gettext (msgid)
48 do_insert (struct lexer *lexer, struct dataset *ds, enum variant variant)
50 enum lex_syntax_mode syntax_mode;
51 enum lex_error_mode error_mode;
58 /* Skip optional FILE=. */
59 if (lex_match_id (lexer, "FILE"))
60 lex_match (lexer, T_EQUALS);
62 /* File name can be identifier or string. */
63 if (lex_token (lexer) != T_ID && !lex_is_string (lexer))
65 lex_error (lexer, _("expecting file name"));
69 relative_name = utf8_to_filename (lex_tokcstr (lexer));
70 filename = include_path_search (relative_name);
75 msg (SE, _("Can't find `%s' in include file search path."),
81 syntax_mode = LEX_SYNTAX_INTERACTIVE;
82 error_mode = LEX_ERROR_CONTINUE;
85 encoding = xstrdup (session_get_default_syntax_encoding (
86 dataset_session (ds)));
87 while ( T_ENDCMD != lex_token (lexer))
89 if (lex_match_id (lexer, "ENCODING"))
91 lex_match (lexer, T_EQUALS);
92 if (!lex_force_string (lexer))
96 encoding = xstrdup (lex_tokcstr (lexer));
98 else if (variant == INSERT && lex_match_id (lexer, "SYNTAX"))
100 lex_match (lexer, T_EQUALS);
101 if ( lex_match_id (lexer, "INTERACTIVE") )
102 syntax_mode = LEX_SYNTAX_INTERACTIVE;
103 else if ( lex_match_id (lexer, "BATCH"))
104 syntax_mode = LEX_SYNTAX_BATCH;
105 else if ( lex_match_id (lexer, "AUTO"))
106 syntax_mode = LEX_SYNTAX_AUTO;
109 lex_error (lexer, _("expecting %s, %s, or %s after %s"),
110 "BATCH", "INTERACTIVE", "AUTO", "SYNTAX");
114 else if (variant == INSERT && lex_match_id (lexer, "CD"))
116 lex_match (lexer, T_EQUALS);
117 if ( lex_match_id (lexer, "YES") )
121 else if ( lex_match_id (lexer, "NO"))
127 lex_error (lexer, _("expecting %s or %s after %s"),
132 else if (variant == INSERT && lex_match_id (lexer, "ERROR"))
134 lex_match (lexer, T_EQUALS);
135 if ( lex_match_id (lexer, "CONTINUE") )
137 error_mode = LEX_ERROR_CONTINUE;
139 else if ( lex_match_id (lexer, "STOP"))
141 error_mode = LEX_ERROR_STOP;
145 lex_error (lexer, _("expecting %s or %s after %s"),
146 "CONTINUE", "STOP", "ERROR");
153 lex_error (lexer, NULL);
157 status = lex_end_of_command (lexer);
159 if ( status == CMD_SUCCESS)
161 struct lex_reader *reader;
163 reader = lex_reader_for_file (filename, encoding,
164 syntax_mode, error_mode);
167 lex_discard_rest_of_command (lexer);
168 lex_include (lexer, reader);
172 char *directory = dir_name (filename);
186 cmd_include (struct lexer *lexer, struct dataset *ds)
188 return do_insert (lexer, ds, INCLUDE);
192 cmd_insert (struct lexer *lexer, struct dataset *ds)
194 return do_insert (lexer, ds, INSERT);