Merge commit 'origin/stable'
[pspp-builds.git] / src / language / utilities / include.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18 #include <ctype.h>
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <string.h>
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>
29
30 #include "dirname.h"
31 #include "xalloc.h"
32
33 #include "gettext.h"
34 #define _(msgid) gettext (msgid)
35
36 static int parse_insert (struct lexer *lexer, char **filename);
37
38
39 int
40 cmd_include (struct lexer *lexer, struct dataset *ds UNUSED)
41 {
42   char *filename = NULL;
43   int status = parse_insert (lexer, &filename);
44
45   if ( CMD_SUCCESS != status)
46     return status;
47
48   lex_get (lexer);
49
50   status = lex_end_of_command (lexer);
51
52   if ( status == CMD_SUCCESS)
53     {
54       struct source_stream *ss = lex_get_source_stream (lexer);
55
56       assert (filename);
57       getl_include_source (ss, create_syntax_file_source (filename),
58                            GETL_BATCH, ERRMODE_STOP);
59       free (filename);
60     }
61
62   return status;
63 }
64
65
66 int
67 cmd_insert (struct lexer *lexer, struct dataset *ds UNUSED)
68 {
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);
73   bool cd = false;
74
75   if ( CMD_SUCCESS != status)
76     return status;
77
78   lex_get (lexer);
79
80   while ( '.' != lex_token (lexer))
81     {
82       if (lex_match_id (lexer, "SYNTAX"))
83         {
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;
89           else
90             {
91               lex_error(lexer,
92                         _("Expecting BATCH or INTERACTIVE after SYNTAX."));
93               return CMD_FAILURE;
94             }
95         }
96       else if (lex_match_id (lexer, "CD"))
97         {
98           lex_match (lexer, '=');
99           if ( lex_match_id (lexer, "YES") )
100             {
101               cd = true;
102             }
103           else if ( lex_match_id (lexer, "NO"))
104             {
105               cd = false;
106             }
107           else
108             {
109               lex_error (lexer, _("Expecting YES or NO after CD."));
110               return CMD_FAILURE;
111             }
112         }
113       else if (lex_match_id (lexer, "ERROR"))
114         {
115           lex_match (lexer, '=');
116           if ( lex_match_id (lexer, "CONTINUE") )
117             {
118               error_mode = ERRMODE_CONTINUE;
119             }
120           else if ( lex_match_id (lexer, "STOP"))
121             {
122               error_mode = ERRMODE_STOP;
123             }
124           else
125             {
126               lex_error (lexer, _("Expecting CONTINUE or STOP after ERROR."));
127               return CMD_FAILURE;
128             }
129         }
130
131       else
132         {
133           lex_error (lexer, _("Unexpected token: `%s'."),
134                      lex_token_representation (lexer));
135
136           return CMD_FAILURE;
137         }
138     }
139
140   status = lex_end_of_command (lexer);
141
142   if ( status == CMD_SUCCESS)
143     {
144       struct source_stream *ss = lex_get_source_stream (lexer);
145
146       assert (filename);
147       getl_include_source (ss, create_syntax_file_source (filename),
148                            syntax_mode,
149                            error_mode);
150
151       if ( cd )
152         {
153           char *directory = dir_name (filename);
154           chdir (directory);
155           free (directory);
156         }
157
158       free (filename);
159     }
160
161   return status;
162 }
163
164
165 static int
166 parse_insert (struct lexer *lexer, char **filename)
167 {
168   char *target_fn;
169   char *relative_filename;
170
171   /* Skip optional FILE=. */
172   if (lex_match_id (lexer, "FILE"))
173     lex_match (lexer, '=');
174
175   /* File name can be identifier or string. */
176   if (lex_token (lexer) != T_ID && lex_token (lexer) != T_STRING)
177     {
178       lex_error (lexer, _("expecting file name"));
179       return CMD_FAILURE;
180     }
181
182   target_fn = ds_cstr (lex_tokstr (lexer));
183
184   relative_filename =
185     fn_search_path (target_fn,
186                     getl_include_path (lex_get_source_stream (lexer)));
187
188   if ( ! relative_filename)
189     {
190       msg (SE, _("Can't find `%s' in include file search path."),
191          target_fn);
192       return CMD_FAILURE;
193     }
194
195   *filename = relative_filename;
196   if (*filename == NULL) 
197     {
198       msg (SE, _("Unable to open `%s': %s."),
199            relative_filename, strerror (errno));
200       free (relative_filename);
201       return CMD_FAILURE;
202     }
203
204   return CMD_SUCCESS;
205 }