1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006 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 <language/data-io/file-handle.h>
20 #include <libpspp/message.h>
23 #include <data/file-name.h>
24 #include <language/command.h>
25 #include <language/lexer/lexer.h>
26 #include <libpspp/assertion.h>
27 #include <libpspp/message.h>
28 #include <libpspp/str.h>
29 #include <data/variable.h>
30 #include <data/file-handle-def.h>
35 #define _(msgid) gettext (msgid)
44 tabwidth=integer "x>=0" "%s must be nonnegative";
45 mode=mode:!character/binary/image/360/scratch;
46 recform=recform:fixed/f/variable/v/spanned/vs.
52 cmd_file_handle (struct lexer *lexer, struct dataset *ds)
54 char handle_name[VAR_NAME_LEN + 1];
55 struct cmd_file_handle cmd;
56 struct file_handle *handle;
58 if (!lex_force_id (lexer))
59 return CMD_CASCADING_FAILURE;
60 str_copy_trunc (handle_name, sizeof handle_name, lex_tokid (lexer));
62 handle = fh_from_id (handle_name);
65 msg (SE, _("File handle %s is already defined. "
66 "Use CLOSE FILE HANDLE before redefining a file handle."),
68 return CMD_CASCADING_FAILURE;
72 if (!lex_force_match (lexer, '/'))
73 return CMD_CASCADING_FAILURE;
75 if (!parse_file_handle (lexer, ds, &cmd, NULL))
76 return CMD_CASCADING_FAILURE;
78 if (lex_end_of_command (lexer) != CMD_SUCCESS)
81 if (cmd.mode != FH_SCRATCH)
83 struct fh_properties properties = *fh_default_properties ();
85 if (cmd.s_name == NULL)
87 lex_sbc_missing (lexer, "NAME");
94 properties.mode = FH_MODE_TEXT;
96 properties.tab_width = cmd.n_tabwidth[0];
99 properties.mode = FH_MODE_FIXED;
102 properties.mode = FH_MODE_VARIABLE;
105 properties.encoding = LEGACY_EBCDIC;
106 if (cmd.recform == FH_FIXED || cmd.recform == FH_F)
107 properties.mode = FH_MODE_FIXED;
108 else if (cmd.recform == FH_VARIABLE || cmd.recform == FH_V)
110 properties.mode = FH_MODE_360_VARIABLE;
111 properties.record_width = 8192;
113 else if (cmd.recform == FH_SPANNED || cmd.recform == FH_VS)
115 properties.mode = FH_MODE_360_SPANNED;
116 properties.record_width = 8192;
120 msg (SE, _("RECFORM must be specified with MODE=360."));
128 if (properties.mode == FH_MODE_FIXED || cmd.n_lrecl[0] != LONG_MIN)
130 if (cmd.n_lrecl[0] == LONG_MIN)
131 msg (SE, _("The specified file mode requires LRECL. "
132 "Assuming %d-character records."),
133 properties.record_width);
134 else if (cmd.n_lrecl[0] < 1 || cmd.n_lrecl[0] >= (1UL << 31))
135 msg (SE, _("Record length (%ld) must be between 1 and %lu bytes. "
136 "Assuming %d-character records."),
137 cmd.n_lrecl[0], (1UL << 31) - 1, properties.record_width);
139 properties.record_width = cmd.n_lrecl[0];
142 fh_create_file (handle_name, cmd.s_name, &properties);
145 fh_create_scratch (handle_name);
147 free_file_handle (&cmd);
151 free_file_handle (&cmd);
152 return CMD_CASCADING_FAILURE;
156 cmd_close_file_handle (struct lexer *lexer, struct dataset *ds UNUSED)
158 struct file_handle *handle;
160 if (!lex_force_id (lexer))
161 return CMD_CASCADING_FAILURE;
162 handle = fh_from_id (lex_tokid (lexer));
164 return CMD_CASCADING_FAILURE;
170 /* Returns the name for REFERENT. */
172 referent_name (enum fh_referent referent)
179 return _("inline file");
181 return _("scratch file");
187 /* Parses a file handle name, which may be a file name as a string
188 or a file handle name as an identifier. The allowed types of
189 file handle are restricted to those in REFERENT_MASK. Returns
190 the file handle when successful, a null pointer on failure.
192 The caller is responsible for fh_unref()'ing the returned
193 file handle when it is no longer needed. */
195 fh_parse (struct lexer *lexer, enum fh_referent referent_mask)
197 struct file_handle *handle;
199 if (lex_match_id (lexer, "INLINE"))
200 handle = fh_inline_file ();
203 if (lex_token (lexer) != T_ID && lex_token (lexer) != T_STRING)
205 lex_error (lexer, _("expecting a file name or handle name"));
210 if (lex_token (lexer) == T_ID)
211 handle = fh_from_id (lex_tokid (lexer));
214 if (lex_token (lexer) != T_ID || lex_tokid (lexer)[0] != '#' || settings_get_syntax () != ENHANCED)
215 handle = fh_create_file (NULL, ds_cstr (lex_tokstr (lexer)),
216 fh_default_properties ());
218 handle = fh_create_scratch (lex_tokid (lexer));
223 if (!(fh_get_referent (handle) & referent_mask))
225 msg (SE, _("Handle for %s not allowed here."),
226 referent_name (fh_get_referent (handle)));