lexer: New function lex_error_expecting().
[pspp-builds.git] / src / language / utilities / include.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007, 2010, 2011 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
19 #include <ctype.h>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include "data/dataset.h"
26 #include "data/file-name.h"
27 #include "data/session.h"
28 #include "language/command.h"
29 #include "language/lexer/include-path.h"
30 #include "language/lexer/lexer.h"
31 #include "libpspp/i18n.h"
32 #include "libpspp/message.h"
33 #include "libpspp/str.h"
34
35 #include "gl/dirname.h"
36 #include "gl/xalloc.h"
37
38 #include "gettext.h"
39 #define _(msgid) gettext (msgid)
40
41 enum variant
42   {
43     INSERT,
44     INCLUDE
45   };
46
47 static int
48 do_insert (struct lexer *lexer, struct dataset *ds, enum variant variant)
49 {
50   enum lex_syntax_mode syntax_mode;
51   enum lex_error_mode error_mode;
52   char *relative_name;
53   char *filename;
54   char *encoding;
55   int status;
56   bool cd;
57
58   /* Skip optional FILE=. */
59   if (lex_match_id (lexer, "FILE"))
60     lex_match (lexer, T_EQUALS);
61
62   /* File name can be identifier or string. */
63   if (lex_token (lexer) != T_ID && !lex_is_string (lexer))
64     {
65       lex_error (lexer, _("expecting file name"));
66       return CMD_FAILURE;
67     }
68
69   relative_name = utf8_to_filename (lex_tokcstr (lexer)); 
70   filename = include_path_search (relative_name);
71   free (relative_name);
72
73   if ( ! filename)
74     {
75       msg (SE, _("Can't find `%s' in include file search path."),
76            lex_tokcstr (lexer));
77       return CMD_FAILURE;
78     }
79   lex_get (lexer);
80
81   syntax_mode = LEX_SYNTAX_INTERACTIVE;
82   error_mode = LEX_ERROR_CONTINUE;
83   cd = false;
84   status = CMD_FAILURE;
85   encoding = xstrdup (session_get_default_syntax_encoding (
86                         dataset_session (ds)));
87   while ( T_ENDCMD != lex_token (lexer))
88     {
89       if (lex_match_id (lexer, "ENCODING"))
90         {
91           lex_match (lexer, T_EQUALS);
92           if (!lex_force_string (lexer))
93             goto exit;
94
95           free (encoding);
96           encoding = xstrdup (lex_tokcstr (lexer));
97         }
98       else if (variant == INSERT && lex_match_id (lexer, "SYNTAX"))
99         {
100           lex_match (lexer, T_EQUALS);
101           if ( lex_match_id (lexer, "INTERACTIVE") )
102             syntax_mode = LEX_SYNTAX_INTERACTIVE;
103           else if ( lex_match_id (lexer, "BATCH"))
104             syntax_mode = LEX_SYNTAX_BATCH;
105           else if ( lex_match_id (lexer, "AUTO"))
106             syntax_mode = LEX_SYNTAX_AUTO;
107           else
108             {
109               lex_error_expecting (lexer, "BATCH", "INTERACTIVE", "AUTO",
110                                    NULL_SENTINEL);
111               goto exit;
112             }
113         }
114       else if (variant == INSERT && lex_match_id (lexer, "CD"))
115         {
116           lex_match (lexer, T_EQUALS);
117           if ( lex_match_id (lexer, "YES") )
118             {
119               cd = true;
120             }
121           else if ( lex_match_id (lexer, "NO"))
122             {
123               cd = false;
124             }
125           else
126             {
127               lex_error_expecting (lexer, "YES", "NO", NULL_SENTINEL);
128               goto exit;
129             }
130         }
131       else if (variant == INSERT && lex_match_id (lexer, "ERROR"))
132         {
133           lex_match (lexer, T_EQUALS);
134           if ( lex_match_id (lexer, "CONTINUE") )
135             {
136               error_mode = LEX_ERROR_CONTINUE;
137             }
138           else if ( lex_match_id (lexer, "STOP"))
139             {
140               error_mode = LEX_ERROR_STOP;
141             }
142           else
143             {
144               lex_error_expecting (lexer, "CONTINUE", "STOP", NULL_SENTINEL);
145               goto exit;
146             }
147         }
148
149       else
150         {
151           lex_error (lexer, NULL);
152           goto exit;
153         }
154     }
155   status = lex_end_of_command (lexer);
156
157   if ( status == CMD_SUCCESS)
158     {
159       struct lex_reader *reader;
160
161       reader = lex_reader_for_file (filename, encoding,
162                                     syntax_mode, error_mode);
163       if (reader != NULL)
164         {
165           lex_discard_rest_of_command (lexer);
166           lex_include (lexer, reader);
167
168           if ( cd )
169             {
170               char *directory = dir_name (filename);
171               chdir (directory);
172               free (directory);
173             }
174         }
175     }
176
177 exit:
178   free (encoding);
179   free (filename);
180   return status;
181 }
182
183 int
184 cmd_include (struct lexer *lexer, struct dataset *ds)
185 {
186   return do_insert (lexer, ds, INCLUDE);
187 }
188
189 int
190 cmd_insert (struct lexer *lexer, struct dataset *ds)
191 {
192   return do_insert (lexer, ds, INSERT);
193 }
194