1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-2000, 2006, 2010-2013, 2016 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/>. */
19 #include "data/file-handle-def.h"
25 #include "data/file-name.h"
26 #include "data/session.h"
27 #include "data/variable.h"
28 #include "language/command.h"
29 #include "language/data-io/file-handle.h"
30 #include "language/lexer/lexer.h"
31 #include "libpspp/assertion.h"
32 #include "libpspp/cast.h"
33 #include "libpspp/message.h"
34 #include "libpspp/str.h"
36 #include "gl/xalloc.h"
39 #define _(msgid) gettext (msgid)
42 cmd_file_handle (struct lexer *lexer, struct dataset *ds UNUSED)
44 enum cmd_result result = CMD_CASCADING_FAILURE;
45 char *handle_name = NULL;
46 char *file_name = NULL;
49 enum { MODE_DEFAULT, MODE_CHARACTER, MODE_BINARY, MODE_IMAGE, MODE_360 }
52 enum { RECFORM_FIXED = 1, RECFORM_VARIABLE, RECFORM_SPANNED } recform = 0;
53 char *encoding = NULL;
55 if (!lex_force_id (lexer))
58 handle_name = xstrdup (lex_tokcstr (lexer));
59 if (fh_from_id (handle_name))
61 msg (SE, _("File handle %s is already defined. "
62 "Use %s before redefining a file handle."),
63 handle_name, "CLOSE FILE HANDLE");
68 if (!lex_force_match (lexer, T_SLASH))
71 while (lex_token (lexer) != T_ENDCMD)
73 if (lex_match_id (lexer, "NAME"))
77 lex_sbc_only_once ("NAME");
81 lex_match (lexer, T_EQUALS);
82 if (!lex_force_string (lexer))
85 file_name = ss_xstrdup (lex_tokss (lexer));
88 else if (lex_match_id (lexer, "LRECL"))
92 lex_sbc_only_once ("LRECL");
96 lex_match (lexer, T_EQUALS);
97 if (!lex_force_int (lexer))
99 lrecl = lex_integer (lexer);
102 else if (lex_match_id (lexer, "TABWIDTH"))
106 lex_sbc_only_once ("TABWIDTH");
109 lex_match (lexer, T_EQUALS);
111 if (!lex_force_int (lexer))
113 tabwidth = lex_integer (lexer);
116 else if (lex_match_id (lexer, "MODE"))
118 if (mode != MODE_DEFAULT)
120 lex_sbc_only_once ("MODE");
123 lex_match (lexer, T_EQUALS);
125 if (lex_match_id (lexer, "CHARACTER"))
126 mode = MODE_CHARACTER;
127 else if (lex_match_id (lexer, "BINARY"))
129 else if (lex_match_id (lexer, "IMAGE"))
131 else if (lex_match_int (lexer, 360))
135 lex_error (lexer, NULL);
139 else if (lex_match_id (lexer, "ENDS"))
143 lex_sbc_only_once ("ENDS");
146 lex_match (lexer, T_EQUALS);
148 if (lex_match_id (lexer, "LF"))
150 else if (lex_match_id (lexer, "CRLF"))
154 lex_error (lexer, NULL);
158 else if (lex_match_id (lexer, "RECFORM"))
162 lex_sbc_only_once ("RECFORM");
165 lex_match (lexer, T_EQUALS);
166 if (lex_match_id (lexer, "FIXED") || lex_match_id (lexer, "F"))
167 recform = RECFORM_FIXED;
168 else if (lex_match_id (lexer, "VARIABLE")
169 || lex_match_id (lexer, "V"))
170 recform = RECFORM_VARIABLE;
171 else if (lex_match_id (lexer, "SPANNED")
172 || lex_match_id (lexer, "VS"))
173 recform = RECFORM_SPANNED;
176 lex_error (lexer, NULL);
180 else if (lex_match_id (lexer, "ENCODING"))
184 lex_sbc_only_once ("ENCODING");
188 lex_match (lexer, T_EQUALS);
189 if (!lex_force_string (lexer))
192 encoding = ss_xstrdup (lex_tokss (lexer));
195 if (!lex_match (lexer, T_SLASH))
199 if (lex_end_of_command (lexer) != CMD_SUCCESS)
202 struct fh_properties properties = *fh_default_properties ();
203 if (file_name == NULL)
205 lex_sbc_missing ("NAME");
213 properties.mode = FH_MODE_TEXT;
215 properties.tab_width = tabwidth;
217 properties.line_ends = ends;
220 properties.mode = FH_MODE_FIXED;
223 properties.mode = FH_MODE_VARIABLE;
226 properties.encoding = CONST_CAST (char *, "EBCDIC-US");
227 if (recform == RECFORM_FIXED)
228 properties.mode = FH_MODE_FIXED;
229 else if (recform == RECFORM_VARIABLE)
231 properties.mode = FH_MODE_360_VARIABLE;
232 properties.record_width = 8192;
234 else if (recform == RECFORM_SPANNED)
236 properties.mode = FH_MODE_360_SPANNED;
237 properties.record_width = 8192;
241 msg (SE, _("%s must be specified with %s."), "RECFORM", "MODE=360");
249 if (properties.mode == FH_MODE_FIXED || lrecl)
252 msg (SE, _("The specified file mode requires LRECL. "
253 "Assuming %zu-character records."),
254 properties.record_width);
255 else if (lrecl < 1 || lrecl >= (1UL << 31))
256 msg (SE, _("Record length (%d) must be between 1 and %lu bytes. "
257 "Assuming %zu-character records."),
258 lrecl, (1UL << 31) - 1, properties.record_width);
260 properties.record_width = lrecl;
264 properties.encoding = encoding;
266 fh_create_file (handle_name, file_name, lex_get_encoding (lexer),
269 result = CMD_SUCCESS;
279 cmd_close_file_handle (struct lexer *lexer, struct dataset *ds UNUSED)
281 struct file_handle *handle;
283 if (!lex_force_id (lexer))
284 return CMD_CASCADING_FAILURE;
285 handle = fh_from_id (lex_tokcstr (lexer));
287 return CMD_CASCADING_FAILURE;
293 /* Returns the name for REFERENT. */
295 referent_name (enum fh_referent referent)
302 return _("inline file");
310 /* Parses a file handle name:
312 - If SESSION is nonnull, then the parsed syntax may be the name of a
313 dataset within SESSION. Dataset names take precedence over file handle
316 - If REFERENT_MASK includes FH_REF_FILE, the parsed syntax may be a file
317 name as a string or a file handle name as an identifier.
319 - If REFERENT_MASK includes FH_REF_INLINE, the parsed syntax may be the
320 identifier INLINE to represent inline data.
322 Returns the file handle when successful, a null pointer on failure.
324 The caller is responsible for fh_unref()'ing the returned file handle when
325 it is no longer needed. */
327 fh_parse (struct lexer *lexer, enum fh_referent referent_mask,
328 struct session *session)
330 struct file_handle *handle;
332 if (session != NULL && lex_token (lexer) == T_ID)
336 ds = session_lookup_dataset (session, lex_tokcstr (lexer));
340 return fh_create_dataset (ds);
344 if (lex_match_id (lexer, "INLINE"))
345 handle = fh_inline_file ();
348 if (lex_token (lexer) != T_ID && !lex_is_string (lexer))
350 lex_error (lexer, _("expecting a file name or handle name"));
355 if (lex_token (lexer) == T_ID)
356 handle = fh_from_id (lex_tokcstr (lexer));
358 handle = fh_create_file (NULL, lex_tokcstr (lexer), lex_get_encoding (lexer),
359 fh_default_properties ());
363 if (!(fh_get_referent (handle) & referent_mask))
365 msg (SE, _("Handle for %s not allowed here."),
366 referent_name (fh_get_referent (handle)));