1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2010, 2011, 2012, 2013 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)
49 mode=mode:!character/binary/image/360;
51 recform=recform:fixed/f/variable/v/spanned/vs;
58 cmd_file_handle (struct lexer *lexer, struct dataset *ds)
60 struct fh_properties properties;
61 struct cmd_file_handle cmd;
62 struct file_handle *handle;
63 enum cmd_result result;
66 result = CMD_CASCADING_FAILURE;
67 if (!lex_force_id (lexer))
70 handle_name = xstrdup (lex_tokcstr (lexer));
71 handle = fh_from_id (handle_name);
74 msg (SE, _("File handle %s is already defined. "
75 "Use CLOSE FILE HANDLE before redefining a file handle."),
77 goto exit_free_handle_name;
81 if (!lex_force_match (lexer, T_SLASH))
82 goto exit_free_handle_name;
84 if (!parse_file_handle (lexer, ds, &cmd, NULL))
85 goto exit_free_handle_name;
87 if (lex_end_of_command (lexer) != CMD_SUCCESS)
90 properties = *fh_default_properties ();
91 if (cmd.s_name == NULL)
93 lex_sbc_missing ("NAME");
100 properties.mode = FH_MODE_TEXT;
101 if (cmd.sbc_tabwidth)
103 if (cmd.n_tabwidth[0] >= 0)
104 properties.tab_width = cmd.n_tabwidth[0];
106 msg (SE, _("%s must not be negative."), "TABWIDTH");
108 if (cmd.ends == FH_LF)
109 properties.line_ends = FH_END_LF;
110 else if (cmd.ends == FH_CRLF)
111 properties.line_ends = FH_END_CRLF;
114 properties.mode = FH_MODE_FIXED;
117 properties.mode = FH_MODE_VARIABLE;
120 properties.encoding = CONST_CAST (char *, "EBCDIC-US");
121 if (cmd.recform == FH_FIXED || cmd.recform == FH_F)
122 properties.mode = FH_MODE_FIXED;
123 else if (cmd.recform == FH_VARIABLE || cmd.recform == FH_V)
125 properties.mode = FH_MODE_360_VARIABLE;
126 properties.record_width = 8192;
128 else if (cmd.recform == FH_SPANNED || cmd.recform == FH_VS)
130 properties.mode = FH_MODE_360_SPANNED;
131 properties.record_width = 8192;
135 msg (SE, _("RECFORM must be specified with MODE=360."));
143 if (properties.mode == FH_MODE_FIXED || cmd.n_lrecl[0] != LONG_MIN)
145 if (cmd.n_lrecl[0] == LONG_MIN)
146 msg (SE, _("The specified file mode requires LRECL. "
147 "Assuming %zu-character records."),
148 properties.record_width);
149 else if (cmd.n_lrecl[0] < 1 || cmd.n_lrecl[0] >= (1UL << 31))
150 msg (SE, _("Record length (%ld) must be between 1 and %lu bytes. "
151 "Assuming %zu-character records."),
152 cmd.n_lrecl[0], (1UL << 31) - 1, properties.record_width);
154 properties.record_width = cmd.n_lrecl[0];
157 if (cmd.s_encoding != NULL)
158 properties.encoding = cmd.s_encoding;
160 fh_create_file (handle_name, cmd.s_name, &properties);
162 result = CMD_SUCCESS;
165 free_file_handle (&cmd);
166 exit_free_handle_name:
173 cmd_close_file_handle (struct lexer *lexer, struct dataset *ds UNUSED)
175 struct file_handle *handle;
177 if (!lex_force_id (lexer))
178 return CMD_CASCADING_FAILURE;
179 handle = fh_from_id (lex_tokcstr (lexer));
181 return CMD_CASCADING_FAILURE;
187 /* Returns the name for REFERENT. */
189 referent_name (enum fh_referent referent)
196 return _("inline file");
204 /* Parses a file handle name:
206 - If SESSION is nonnull, then the parsed syntax may be the name of a
207 dataset within SESSION. Dataset names take precedence over file handle
210 - If REFERENT_MASK includes FH_REF_FILE, the parsed syntax may be a file
211 name as a string or a file handle name as an identifier.
213 - If REFERENT_MASK includes FH_REF_INLINE, the parsed syntax may be the
214 identifier INLINE to represent inline data.
216 Returns the file handle when successful, a null pointer on failure.
218 The caller is responsible for fh_unref()'ing the returned file handle when
219 it is no longer needed. */
221 fh_parse (struct lexer *lexer, enum fh_referent referent_mask,
222 struct session *session)
224 struct file_handle *handle;
226 if (session != NULL && lex_token (lexer) == T_ID)
230 ds = session_lookup_dataset (session, lex_tokcstr (lexer));
234 return fh_create_dataset (ds);
238 if (lex_match_id (lexer, "INLINE"))
239 handle = fh_inline_file ();
242 if (lex_token (lexer) != T_ID && !lex_is_string (lexer))
244 lex_error (lexer, _("expecting a file name or handle name"));
249 if (lex_token (lexer) == T_ID)
250 handle = fh_from_id (lex_tokcstr (lexer));
252 handle = fh_create_file (NULL, lex_tokcstr (lexer),
253 fh_default_properties ());
257 if (!(fh_get_referent (handle) & referent_mask))
259 msg (SE, _("Handle for %s not allowed here."),
260 referent_name (fh_get_referent (handle)));