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/commands/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 struct file_handle *fh = fh_from_id (handle_name);
63 lex_error (lexer, _("File handle %s is already defined. "
64 "Use %s before redefining a file handle."),
65 handle_name, "CLOSE FILE HANDLE");
70 if (!lex_force_match (lexer, T_SLASH))
75 while (lex_token (lexer) != T_ENDCMD)
77 if (lex_match_id (lexer, "NAME"))
81 lex_sbc_only_once (lexer, "NAME");
85 lex_match (lexer, T_EQUALS);
86 if (!lex_force_string (lexer))
89 file_name = ss_xstrdup (lex_tokss (lexer));
92 else if (lex_match_id (lexer, "LRECL"))
96 lex_sbc_only_once (lexer, "LRECL");
100 lex_match (lexer, T_EQUALS);
101 if (!lex_force_int_range (lexer, "LRECL", 1, (1UL << 31) - 1))
103 lrecl = lex_integer (lexer);
106 else if (lex_match_id (lexer, "TABWIDTH"))
110 lex_sbc_only_once (lexer, "TABWIDTH");
113 lex_match (lexer, T_EQUALS);
115 if (!lex_force_int_range (lexer, "TABWIDTH", 1, INT_MAX))
117 tabwidth = lex_integer (lexer);
120 else if (lex_match_id (lexer, "MODE"))
122 if (mode != MODE_DEFAULT)
124 lex_sbc_only_once (lexer, "MODE");
127 mode_start = lex_ofs (lexer) - 1;
128 lex_match (lexer, T_EQUALS);
130 if (lex_match_id (lexer, "CHARACTER"))
131 mode = MODE_CHARACTER;
132 else if (lex_match_id (lexer, "BINARY"))
134 else if (lex_match_id (lexer, "IMAGE"))
136 else if (lex_match_int (lexer, 360))
140 lex_error_expecting (lexer, "CHARACTER", "BINARY",
144 mode_end = lex_ofs (lexer) - 1;
146 else if (lex_match_id (lexer, "ENDS"))
150 lex_sbc_only_once (lexer, "ENDS");
153 lex_match (lexer, T_EQUALS);
155 if (lex_match_id (lexer, "LF"))
157 else if (lex_match_id (lexer, "CRLF"))
161 lex_error_expecting (lexer, "LF", "CRLF");
165 else if (lex_match_id (lexer, "RECFORM"))
169 lex_sbc_only_once (lexer, "RECFORM");
172 lex_match (lexer, T_EQUALS);
173 if (lex_match_id (lexer, "FIXED") || lex_match_id (lexer, "F"))
174 recform = RECFORM_FIXED;
175 else if (lex_match_id (lexer, "VARIABLE")
176 || lex_match_id (lexer, "V"))
177 recform = RECFORM_VARIABLE;
178 else if (lex_match_id (lexer, "SPANNED")
179 || lex_match_id (lexer, "VS"))
180 recform = RECFORM_SPANNED;
183 lex_error_expecting (lexer, "FIXED", "VARIABLE", "SPANNED");
187 else if (lex_match_id (lexer, "ENCODING"))
191 lex_sbc_only_once (lexer, "ENCODING");
195 lex_match (lexer, T_EQUALS);
196 if (!lex_force_string (lexer))
199 encoding = ss_xstrdup (lex_tokss (lexer));
202 if (!lex_match (lexer, T_SLASH))
206 if (lex_end_of_command (lexer) != CMD_SUCCESS)
209 struct fh_properties properties = *fh_default_properties ();
210 if (file_name == NULL)
212 lex_sbc_missing (lexer, "NAME");
220 properties.mode = FH_MODE_TEXT;
222 properties.tab_width = tabwidth;
224 properties.line_ends = ends;
227 properties.mode = FH_MODE_FIXED;
230 properties.mode = FH_MODE_VARIABLE;
233 properties.encoding = CONST_CAST (char *, "EBCDIC-US");
234 if (recform == RECFORM_FIXED)
235 properties.mode = FH_MODE_FIXED;
236 else if (recform == RECFORM_VARIABLE)
238 properties.mode = FH_MODE_360_VARIABLE;
239 properties.record_width = 8192;
241 else if (recform == RECFORM_SPANNED)
243 properties.mode = FH_MODE_360_SPANNED;
244 properties.record_width = 8192;
248 lex_ofs_error (lexer, mode_start, mode_end,
249 _("%s must be specified with %s."),
250 "RECFORM", "MODE=360");
258 if (properties.mode == FH_MODE_FIXED || lrecl)
261 msg (SE, _("The specified file mode requires LRECL. "
262 "Assuming %zu-character records."),
263 properties.record_width);
265 properties.record_width = lrecl;
269 properties.encoding = encoding;
271 fh_create_file (handle_name, file_name, lex_get_encoding (lexer),
274 result = CMD_SUCCESS;
284 cmd_close_file_handle (struct lexer *lexer, struct dataset *ds UNUSED)
286 struct file_handle *handle;
288 if (!lex_force_id (lexer))
289 return CMD_CASCADING_FAILURE;
290 handle = fh_from_id (lex_tokcstr (lexer));
292 return CMD_CASCADING_FAILURE;
298 /* Returns the name for REFERENT. */
300 referent_name (enum fh_referent referent)
307 return _("inline file");
315 /* Parses a file handle name:
317 - If SESSION is nonnull, then the parsed syntax may be the name of a
318 dataset within SESSION. Dataset names take precedence over file handle
321 - If REFERENT_MASK includes FH_REF_FILE, the parsed syntax may be a file
322 name as a string or a file handle name as an identifier.
324 - If REFERENT_MASK includes FH_REF_INLINE, the parsed syntax may be the
325 identifier INLINE to represent inline data.
327 Returns the file handle when successful, a null pointer on failure.
329 The caller is responsible for fh_unref()'ing the returned file handle when
330 it is no longer needed. */
332 fh_parse (struct lexer *lexer, enum fh_referent referent_mask,
333 struct session *session)
335 if (session != NULL && lex_token (lexer) == T_ID)
339 ds = session_lookup_dataset (session, lex_tokcstr (lexer));
343 return fh_create_dataset (ds);
347 int start_ofs = lex_ofs (lexer);
348 struct file_handle *handle;
349 if (lex_match_id (lexer, "INLINE"))
350 handle = fh_inline_file ();
353 if (lex_token (lexer) != T_ID && !lex_is_string (lexer))
356 _("Syntax error expecting a file name or handle name."));
361 if (lex_token (lexer) == T_ID)
362 handle = fh_from_id (lex_tokcstr (lexer));
364 handle = fh_create_file (NULL, lex_tokcstr (lexer), lex_get_encoding (lexer),
365 fh_default_properties ());
369 if (!(fh_get_referent (handle) & referent_mask))
371 lex_ofs_error (lexer, start_ofs, lex_ofs (lexer) - 1,
372 _("Handle for %s not allowed here."),
373 referent_name (fh_get_referent (handle)));