1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2010, 2011 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/>. */
23 #include "data/file-name.h"
24 #include "data/session.h"
25 #include "language/command.h"
26 #include "language/data-io/file-handle.h"
27 #include "language/lexer/lexer.h"
28 #include "libpspp/assertion.h"
29 #include "libpspp/message.h"
30 #include "libpspp/str.h"
31 #include "data/variable.h"
32 #include "data/file-handle-def.h"
34 #include "gl/xalloc.h"
37 #define _(msgid) gettext (msgid)
46 tabwidth=integer "x>=0" "%s must be nonnegative";
47 mode=mode:!character/binary/image/360;
48 recform=recform:fixed/f/variable/v/spanned/vs.
54 cmd_file_handle (struct lexer *lexer, struct dataset *ds)
56 struct fh_properties properties;
57 struct cmd_file_handle cmd;
58 struct file_handle *handle;
59 enum cmd_result result;
62 result = CMD_CASCADING_FAILURE;
63 if (!lex_force_id (lexer))
66 handle_name = xstrdup (lex_tokcstr (lexer));
67 handle = fh_from_id (handle_name);
70 msg (SE, _("File handle %s is already defined. "
71 "Use CLOSE FILE HANDLE before redefining a file handle."),
73 goto exit_free_handle_name;
77 if (!lex_force_match (lexer, T_SLASH))
78 goto exit_free_handle_name;
80 if (!parse_file_handle (lexer, ds, &cmd, NULL))
81 goto exit_free_handle_name;
83 if (lex_end_of_command (lexer) != CMD_SUCCESS)
86 properties = *fh_default_properties ();
87 if (cmd.s_name == NULL)
89 lex_sbc_missing (lexer, "NAME");
96 properties.mode = FH_MODE_TEXT;
98 properties.tab_width = cmd.n_tabwidth[0];
101 properties.mode = FH_MODE_FIXED;
104 properties.mode = FH_MODE_VARIABLE;
107 properties.encoding = "EBCDIC-US";
108 if (cmd.recform == FH_FIXED || cmd.recform == FH_F)
109 properties.mode = FH_MODE_FIXED;
110 else if (cmd.recform == FH_VARIABLE || cmd.recform == FH_V)
112 properties.mode = FH_MODE_360_VARIABLE;
113 properties.record_width = 8192;
115 else if (cmd.recform == FH_SPANNED || cmd.recform == FH_VS)
117 properties.mode = FH_MODE_360_SPANNED;
118 properties.record_width = 8192;
122 msg (SE, _("RECFORM must be specified with MODE=360."));
130 if (properties.mode == FH_MODE_FIXED || cmd.n_lrecl[0] != LONG_MIN)
132 if (cmd.n_lrecl[0] == LONG_MIN)
133 msg (SE, _("The specified file mode requires LRECL. "
134 "Assuming %zu-character records."),
135 properties.record_width);
136 else if (cmd.n_lrecl[0] < 1 || cmd.n_lrecl[0] >= (1UL << 31))
137 msg (SE, _("Record length (%ld) must be between 1 and %lu bytes. "
138 "Assuming %zu-character records."),
139 cmd.n_lrecl[0], (1UL << 31) - 1, properties.record_width);
141 properties.record_width = cmd.n_lrecl[0];
144 fh_create_file (handle_name, cmd.s_name, &properties);
146 result = CMD_SUCCESS;
149 free_file_handle (&cmd);
150 exit_free_handle_name:
157 cmd_close_file_handle (struct lexer *lexer, struct dataset *ds UNUSED)
159 struct file_handle *handle;
161 if (!lex_force_id (lexer))
162 return CMD_CASCADING_FAILURE;
163 handle = fh_from_id (lex_tokcstr (lexer));
165 return CMD_CASCADING_FAILURE;
171 /* Returns the name for REFERENT. */
173 referent_name (enum fh_referent referent)
180 return _("inline file");
188 /* Parses a file handle name:
190 - If SESSION is nonnull, then the parsed syntax may be the name of a
191 dataset within SESSION. Dataset names take precedence over file handle
194 - If REFERENT_MASK includes FH_REF_FILE, the parsed syntax may be a file
195 name as a string or a file handle name as an identifier.
197 - If REFERENT_MASK includes FH_REF_INLINE, the parsed syntax may be the
198 identifier INLINE to represent inline data.
200 Returns the file handle when successful, a null pointer on failure.
202 The caller is responsible for fh_unref()'ing the returned file handle when
203 it is no longer needed. */
205 fh_parse (struct lexer *lexer, enum fh_referent referent_mask,
206 struct session *session)
208 struct file_handle *handle;
210 if (session != NULL && lex_token (lexer) == T_ID)
214 ds = session_lookup_dataset (session, lex_tokcstr (lexer));
218 return fh_create_dataset (ds);
222 if (lex_match_id (lexer, "INLINE"))
223 handle = fh_inline_file ();
226 if (lex_token (lexer) != T_ID && !lex_is_string (lexer))
228 lex_error (lexer, _("expecting a file name or handle name"));
233 if (lex_token (lexer) == T_ID)
234 handle = fh_from_id (lex_tokcstr (lexer));
236 handle = fh_create_file (NULL, lex_tokcstr (lexer),
237 fh_default_properties ());
241 if (!(fh_get_referent (handle) & referent_mask))
243 msg (SE, _("Handle for %s not allowed here."),
244 referent_name (fh_get_referent (handle)));