1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2010 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/file-name.h>
26 #include <language/command.h>
27 #include <language/lexer/lexer.h>
28 #include <language/syntax-file.h>
29 #include <libpspp/getl.h>
30 #include <libpspp/message.h>
31 #include <libpspp/str.h>
37 #define _(msgid) gettext (msgid)
39 static int parse_insert (struct lexer *lexer, char **filename);
43 cmd_include (struct lexer *lexer, struct dataset *ds UNUSED)
45 char *filename = NULL;
46 int status = parse_insert (lexer, &filename);
48 if ( CMD_SUCCESS != status)
53 status = lex_end_of_command (lexer);
55 if ( status == CMD_SUCCESS)
57 struct source_stream *ss = lex_get_source_stream (lexer);
60 getl_include_source (ss, create_syntax_file_source (filename),
61 GETL_BATCH, ERRMODE_STOP);
70 cmd_insert (struct lexer *lexer, struct dataset *ds UNUSED)
72 enum syntax_mode syntax_mode = GETL_INTERACTIVE;
73 enum error_mode error_mode = ERRMODE_CONTINUE;
74 char *filename = NULL;
75 int status = parse_insert (lexer, &filename);
78 if ( CMD_SUCCESS != status)
83 while ( T_ENDCMD != lex_token (lexer))
85 if (lex_match_id (lexer, "SYNTAX"))
87 lex_match (lexer, T_EQUALS);
88 if ( lex_match_id (lexer, "INTERACTIVE") )
89 syntax_mode = GETL_INTERACTIVE;
90 else if ( lex_match_id (lexer, "BATCH"))
91 syntax_mode = GETL_BATCH;
94 lex_error (lexer, _("expecting %s or %s after %s"),
95 "BATCH", "INTERACTIVE", "SYNTAX");
99 else if (lex_match_id (lexer, "CD"))
101 lex_match (lexer, T_EQUALS);
102 if ( lex_match_id (lexer, "YES") )
106 else if ( lex_match_id (lexer, "NO"))
112 lex_error (lexer, _("expecting %s or %s after %s"),
117 else if (lex_match_id (lexer, "ERROR"))
119 lex_match (lexer, T_EQUALS);
120 if ( lex_match_id (lexer, "CONTINUE") )
122 error_mode = ERRMODE_CONTINUE;
124 else if ( lex_match_id (lexer, "STOP"))
126 error_mode = ERRMODE_STOP;
130 lex_error (lexer, _("expecting %s or %s after %s"),
131 "CONTINUE", "STOP", "ERROR");
138 lex_error (lexer, _("Unexpected token: `%s'."),
139 lex_token_representation (lexer));
145 status = lex_end_of_command (lexer);
147 if ( status == CMD_SUCCESS)
149 struct source_stream *ss = lex_get_source_stream (lexer);
152 getl_include_source (ss, create_syntax_file_source (filename),
158 char *directory = dir_name (filename);
171 parse_insert (struct lexer *lexer, char **filename)
173 const char *target_fn;
174 char *relative_filename;
176 /* Skip optional FILE=. */
177 if (lex_match_id (lexer, "FILE"))
178 lex_match (lexer, T_EQUALS);
180 /* File name can be identifier or string. */
181 if (lex_token (lexer) != T_ID && !lex_is_string (lexer))
183 lex_error (lexer, _("expecting file name"));
187 target_fn = lex_tokcstr (lexer);
190 fn_search_path (target_fn,
191 getl_include_path (lex_get_source_stream (lexer)));
193 if ( ! relative_filename)
195 msg (SE, _("Can't find `%s' in include file search path."),
200 *filename = relative_filename;
201 if (*filename == NULL)
203 msg (SE, _("Unable to open `%s': %s."),
204 relative_filename, strerror (errno));
205 free (relative_filename);