1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007 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/>. */
22 #include <language/command.h>
23 #include <libpspp/message.h>
24 #include <libpspp/getl.h>
25 #include <language/syntax-file.h>
26 #include <language/lexer/lexer.h>
27 #include <libpspp/str.h>
28 #include <data/file-name.h>
31 #include "canonicalize.h"
35 #define _(msgid) gettext (msgid)
37 static int parse_insert (struct lexer *lexer, char **filename);
41 cmd_include (struct lexer *lexer, struct dataset *ds UNUSED)
43 char *filename = NULL;
44 int status = parse_insert (lexer, &filename);
46 if ( CMD_SUCCESS != status)
51 status = lex_end_of_command (lexer);
53 if ( status == CMD_SUCCESS)
55 struct source_stream *ss = lex_get_source_stream (lexer);
58 getl_include_source (ss, create_syntax_file_source (filename),
59 GETL_BATCH, ERRMODE_STOP);
68 cmd_insert (struct lexer *lexer, struct dataset *ds UNUSED)
70 enum syntax_mode syntax_mode = GETL_INTERACTIVE;
71 enum error_mode error_mode = ERRMODE_CONTINUE;
72 char *filename = NULL;
73 int status = parse_insert (lexer, &filename);
76 if ( CMD_SUCCESS != status)
81 while ( '.' != lex_token (lexer))
83 if (lex_match_id (lexer, "SYNTAX"))
85 lex_match (lexer, '=');
86 if ( lex_match_id (lexer, "INTERACTIVE") )
87 syntax_mode = GETL_INTERACTIVE;
88 else if ( lex_match_id (lexer, "BATCH"))
89 syntax_mode = GETL_BATCH;
93 _("Expecting BATCH or INTERACTIVE after SYNTAX."));
97 else if (lex_match_id (lexer, "CD"))
99 lex_match (lexer, '=');
100 if ( lex_match_id (lexer, "YES") )
104 else if ( lex_match_id (lexer, "NO"))
110 lex_error (lexer, _("Expecting YES or NO after CD."));
114 else if (lex_match_id (lexer, "ERROR"))
116 lex_match (lexer, '=');
117 if ( lex_match_id (lexer, "CONTINUE") )
119 error_mode = ERRMODE_CONTINUE;
121 else if ( lex_match_id (lexer, "STOP"))
123 error_mode = ERRMODE_STOP;
127 lex_error (lexer, _("Expecting CONTINUE or STOP after ERROR."));
134 lex_error (lexer, _("Unexpected token: `%s'."),
135 lex_token_representation (lexer));
141 status = lex_end_of_command (lexer);
143 if ( status == CMD_SUCCESS)
145 struct source_stream *ss = lex_get_source_stream (lexer);
148 getl_include_source (ss, create_syntax_file_source (filename),
154 char *directory = dir_name (filename);
167 parse_insert (struct lexer *lexer, char **filename)
170 char *relative_filename;
172 /* Skip optional FILE=. */
173 if (lex_match_id (lexer, "FILE"))
174 lex_match (lexer, '=');
176 /* File name can be identifier or string. */
177 if (lex_token (lexer) != T_ID && lex_token (lexer) != T_STRING)
179 lex_error (lexer, _("expecting file name"));
183 target_fn = ds_cstr (lex_tokstr (lexer));
186 fn_search_path (target_fn,
187 getl_include_path (lex_get_source_stream (lexer)));
189 if ( ! relative_filename)
191 msg (SE, _("Can't find `%s' in include file search path."),
196 *filename = canonicalize_file_name (relative_filename);
197 if (*filename == NULL)
199 msg (SE, _("Unable to open `%s': %s."),
200 relative_filename, strerror (errno));
201 free (relative_filename);
204 free (relative_filename);