7bb2e55733aad4f29fb21da08dcefdc3f510a82d
[pspp-builds.git] / src / language / utilities / include.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <libpspp/alloc.h>
24 #include <language/command.h>
25 #include <libpspp/message.h>
26 #include <libpspp/getl.h>
27 #include <language/syntax-file.h>
28 #include <language/lexer/lexer.h>
29 #include <libpspp/str.h>
30 #include <data/file-name.h>
31
32
33 #include "gettext.h"
34 #define _(msgid) gettext (msgid)
35
36 int
37 cmd_include (struct lexer *lexer, struct dataset *ds UNUSED)
38 {
39   struct source_stream *ss;
40   char *found_fn;
41   char *target_fn;
42
43   /* Skip optional FILE=. */
44   if (lex_match_id (lexer, "FILE"))
45     lex_match (lexer, '=');
46
47   /* File name can be identifier or string. */
48   if (lex_token (lexer) != T_ID && lex_token (lexer) != T_STRING) 
49     {
50       lex_error (lexer, _("expecting file name")); 
51       return CMD_CASCADING_FAILURE;
52     }
53
54   target_fn = ds_cstr (lex_tokstr (lexer));
55
56   ss = lex_get_source_stream (lexer);
57   found_fn = fn_search_path (target_fn, getl_include_path ( ss ));
58
59   if (found_fn != NULL) 
60     {
61       getl_include_source (ss, create_syntax_file_source (found_fn));
62       free (found_fn); 
63     }
64   else
65     msg (SE, _("Can't find `%s' in include file search path."), 
66          target_fn);
67
68   lex_get (lexer);
69   return lex_end_of_command (lexer);
70 }