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 "language/command.h"
25 #include "language/data-io/file-handle.h"
26 #include "language/lexer/lexer.h"
27 #include "libpspp/assertion.h"
28 #include "libpspp/message.h"
29 #include "libpspp/str.h"
30 #include "data/variable.h"
31 #include "data/file-handle-def.h"
33 #include "gl/xalloc.h"
36 #define _(msgid) gettext (msgid)
45 tabwidth=integer "x>=0" "%s must be nonnegative";
46 mode=mode:!character/binary/image/360/scratch;
47 recform=recform:fixed/f/variable/v/spanned/vs.
53 cmd_file_handle (struct lexer *lexer, struct dataset *ds)
55 struct cmd_file_handle cmd;
56 struct file_handle *handle;
57 enum cmd_result result;
60 result = CMD_CASCADING_FAILURE;
61 if (!lex_force_id (lexer))
64 handle_name = xstrdup (lex_tokcstr (lexer));
65 handle = fh_from_id (handle_name);
68 msg (SE, _("File handle %s is already defined. "
69 "Use CLOSE FILE HANDLE before redefining a file handle."),
71 goto exit_free_handle_name;
75 if (!lex_force_match (lexer, T_SLASH))
76 goto exit_free_handle_name;
78 if (!parse_file_handle (lexer, ds, &cmd, NULL))
79 goto exit_free_handle_name;
81 if (lex_end_of_command (lexer) != CMD_SUCCESS)
84 if (cmd.mode != FH_SCRATCH)
86 struct fh_properties properties = *fh_default_properties ();
88 if (cmd.s_name == NULL)
90 lex_sbc_missing (lexer, "NAME");
97 properties.mode = FH_MODE_TEXT;
99 properties.tab_width = cmd.n_tabwidth[0];
102 properties.mode = FH_MODE_FIXED;
105 properties.mode = FH_MODE_VARIABLE;
108 properties.encoding = "EBCDIC-US";
109 if (cmd.recform == FH_FIXED || cmd.recform == FH_F)
110 properties.mode = FH_MODE_FIXED;
111 else if (cmd.recform == FH_VARIABLE || cmd.recform == FH_V)
113 properties.mode = FH_MODE_360_VARIABLE;
114 properties.record_width = 8192;
116 else if (cmd.recform == FH_SPANNED || cmd.recform == FH_VS)
118 properties.mode = FH_MODE_360_SPANNED;
119 properties.record_width = 8192;
123 msg (SE, _("RECFORM must be specified with MODE=360."));
131 if (properties.mode == FH_MODE_FIXED || cmd.n_lrecl[0] != LONG_MIN)
133 if (cmd.n_lrecl[0] == LONG_MIN)
134 msg (SE, _("The specified file mode requires LRECL. "
135 "Assuming %zu-character records."),
136 properties.record_width);
137 else if (cmd.n_lrecl[0] < 1 || cmd.n_lrecl[0] >= (1UL << 31))
138 msg (SE, _("Record length (%ld) must be between 1 and %lu bytes. "
139 "Assuming %d-character records."),
140 cmd.n_lrecl[0], (1UL << 31) - 1, properties.record_width);
142 properties.record_width = cmd.n_lrecl[0];
145 fh_create_file (handle_name, cmd.s_name, &properties);
148 fh_create_scratch (handle_name);
150 result = CMD_SUCCESS;
153 free_file_handle (&cmd);
154 exit_free_handle_name:
161 cmd_close_file_handle (struct lexer *lexer, struct dataset *ds UNUSED)
163 struct file_handle *handle;
165 if (!lex_force_id (lexer))
166 return CMD_CASCADING_FAILURE;
167 handle = fh_from_id (lex_tokcstr (lexer));
169 return CMD_CASCADING_FAILURE;
175 /* Returns the name for REFERENT. */
177 referent_name (enum fh_referent referent)
184 return _("inline file");
186 return _("scratch file");
192 /* Parses a file handle name, which may be a file name as a string
193 or a file handle name as an identifier. The allowed types of
194 file handle are restricted to those in REFERENT_MASK. Returns
195 the file handle when successful, a null pointer on failure.
197 The caller is responsible for fh_unref()'ing the returned
198 file handle when it is no longer needed. */
200 fh_parse (struct lexer *lexer, enum fh_referent referent_mask)
202 struct file_handle *handle;
204 if (lex_match_id (lexer, "INLINE"))
205 handle = fh_inline_file ();
208 if (lex_token (lexer) != T_ID && !lex_is_string (lexer))
210 lex_error (lexer, _("expecting a file name or handle name"));
215 if (lex_token (lexer) == T_ID)
216 handle = fh_from_id (lex_tokcstr (lexer));
219 if (lex_token (lexer) != T_ID || lex_tokcstr (lexer)[0] != '#'
220 || settings_get_syntax () != ENHANCED)
221 handle = fh_create_file (NULL, lex_tokcstr (lexer),
222 fh_default_properties ());
224 handle = fh_create_scratch (lex_tokcstr (lexer));
229 if (!(fh_get_referent (handle) & referent_mask))
231 msg (SE, _("Handle for %s not allowed here."),
232 referent_name (fh_get_referent (handle)));