Rename procedure.[ch] to dataset.[ch].
[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 "language/command.h"
28 #include "language/lexer/include-path.h"
29 #include "language/lexer/lexer.h"
30 #include "libpspp/i18n.h"
31 #include "libpspp/message.h"
32 #include "libpspp/str.h"
33
34 #include "gl/dirname.h"
35 #include "gl/xalloc.h"
36
37 #include "gettext.h"
38 #define _(msgid) gettext (msgid)
39
40 enum variant
41   {
42     INSERT,
43     INCLUDE
44   };
45
46 static int
47 do_insert (struct lexer *lexer, struct dataset *ds, enum variant variant)
48 {
49   enum lex_syntax_mode syntax_mode;
50   enum lex_error_mode error_mode;
51   char *relative_name;
52   char *filename;
53   char *encoding;
54   int status;
55   bool cd;
56
57   /* Skip optional FILE=. */
58   if (lex_match_id (lexer, "FILE"))
59     lex_match (lexer, T_EQUALS);
60
61   /* File name can be identifier or string. */
62   if (lex_token (lexer) != T_ID && !lex_is_string (lexer))
63     {
64       lex_error (lexer, _("expecting file name"));
65       return CMD_FAILURE;
66     }
67
68   relative_name = utf8_to_filename (lex_tokcstr (lexer)); 
69   filename = include_path_search (relative_name);
70   free (relative_name);
71
72   if ( ! filename)
73     {
74       msg (SE, _("Can't find `%s' in include file search path."),
75            lex_tokcstr (lexer));
76       return CMD_FAILURE;
77     }
78   lex_get (lexer);
79
80   syntax_mode = LEX_SYNTAX_INTERACTIVE;
81   error_mode = LEX_ERROR_CONTINUE;
82   cd = false;
83   status = CMD_FAILURE;
84   encoding = xstrdup (dataset_get_default_syntax_encoding (ds));
85   while ( T_ENDCMD != lex_token (lexer))
86     {
87       if (lex_match_id (lexer, "ENCODING"))
88         {
89           lex_match (lexer, T_EQUALS);
90           if (!lex_force_string (lexer))
91             goto exit;
92
93           free (encoding);
94           encoding = xstrdup (lex_tokcstr (lexer));
95         }
96       else if (variant == INSERT && lex_match_id (lexer, "SYNTAX"))
97         {
98           lex_match (lexer, T_EQUALS);
99           if ( lex_match_id (lexer, "INTERACTIVE") )
100             syntax_mode = LEX_SYNTAX_INTERACTIVE;
101           else if ( lex_match_id (lexer, "BATCH"))
102             syntax_mode = LEX_SYNTAX_BATCH;
103           else if ( lex_match_id (lexer, "AUTO"))
104             syntax_mode = LEX_SYNTAX_AUTO;
105           else
106             {
107               lex_error (lexer, _("expecting %s, %s, or %s after %s"),
108                          "BATCH", "INTERACTIVE", "AUTO", "SYNTAX");
109               goto exit;
110             }
111         }
112       else if (variant == INSERT && lex_match_id (lexer, "CD"))
113         {
114           lex_match (lexer, T_EQUALS);
115           if ( lex_match_id (lexer, "YES") )
116             {
117               cd = true;
118             }
119           else if ( lex_match_id (lexer, "NO"))
120             {
121               cd = false;
122             }
123           else
124             {
125               lex_error (lexer, _("expecting %s or %s after %s"),
126                          "YES", "NO", "CD");
127               goto exit;
128             }
129         }
130       else if (variant == INSERT && lex_match_id (lexer, "ERROR"))
131         {
132           lex_match (lexer, T_EQUALS);
133           if ( lex_match_id (lexer, "CONTINUE") )
134             {
135               error_mode = LEX_ERROR_CONTINUE;
136             }
137           else if ( lex_match_id (lexer, "STOP"))
138             {
139               error_mode = LEX_ERROR_STOP;
140             }
141           else
142             {
143               lex_error (lexer, _("expecting %s or %s after %s"),
144                          "CONTINUE", "STOP", "ERROR");
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