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>
34 #define _(msgid) gettext (msgid)
36 static int parse_insert (struct lexer *lexer, char **filename);
40 cmd_include (struct lexer *lexer, struct dataset *ds UNUSED)
42 char *filename = NULL;
43 int status = parse_insert (lexer, &filename);
45 if ( CMD_SUCCESS != status)
50 status = lex_end_of_command (lexer);
52 if ( status == CMD_SUCCESS)
54 struct source_stream *ss = lex_get_source_stream (lexer);
57 getl_include_source (ss, create_syntax_file_source (filename),
58 GETL_BATCH, ERRMODE_STOP);
67 cmd_insert (struct lexer *lexer, struct dataset *ds UNUSED)
69 enum syntax_mode syntax_mode = GETL_INTERACTIVE;
70 enum error_mode error_mode = ERRMODE_CONTINUE;
71 char *filename = NULL;
72 int status = parse_insert (lexer, &filename);
75 if ( CMD_SUCCESS != status)
80 while ( '.' != lex_token (lexer))
82 if (lex_match_id (lexer, "SYNTAX"))
84 lex_match (lexer, '=');
85 if ( lex_match_id (lexer, "INTERACTIVE") )
86 syntax_mode = GETL_INTERACTIVE;
87 else if ( lex_match_id (lexer, "BATCH"))
88 syntax_mode = GETL_BATCH;
92 _("Expecting BATCH or INTERACTIVE after SYNTAX."));
96 else if (lex_match_id (lexer, "CD"))
98 lex_match (lexer, '=');
99 if ( lex_match_id (lexer, "YES") )
103 else if ( lex_match_id (lexer, "NO"))
109 lex_error (lexer, _("Expecting YES or NO after CD."));
113 else if (lex_match_id (lexer, "ERROR"))
115 lex_match (lexer, '=');
116 if ( lex_match_id (lexer, "CONTINUE") )
118 error_mode = ERRMODE_CONTINUE;
120 else if ( lex_match_id (lexer, "STOP"))
122 error_mode = ERRMODE_STOP;
126 lex_error (lexer, _("Expecting CONTINUE or STOP after ERROR."));
133 lex_error (lexer, _("Unexpected token: `%s'."),
134 lex_token_representation (lexer));
140 status = lex_end_of_command (lexer);
142 if ( status == CMD_SUCCESS)
144 struct source_stream *ss = lex_get_source_stream (lexer);
147 getl_include_source (ss, create_syntax_file_source (filename),
153 char *directory = dir_name (filename);
166 parse_insert (struct lexer *lexer, char **filename)
169 char *relative_filename;
171 /* Skip optional FILE=. */
172 if (lex_match_id (lexer, "FILE"))
173 lex_match (lexer, '=');
175 /* File name can be identifier or string. */
176 if (lex_token (lexer) != T_ID && lex_token (lexer) != T_STRING)
178 lex_error (lexer, _("expecting file name"));
182 target_fn = ds_cstr (lex_tokstr (lexer));
185 fn_search_path (target_fn,
186 getl_include_path (lex_get_source_stream (lexer)));
188 if ( ! relative_filename)
190 msg (SE, _("Can't find `%s' in include file search path."),
195 *filename = relative_filename;
196 if (*filename == NULL)
198 msg (SE, _("Unable to open `%s': %s."),
199 relative_filename, strerror (errno));
200 free (relative_filename);