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;
59 if (!lex_force_id (lexer))
61 handle_name = xstrdup (lex_tokcstr (lexer));
63 handle = fh_from_id (handle_name);
66 msg (SE, _("File handle %s is already defined. "
67 "Use CLOSE FILE HANDLE before redefining a file handle."),
73 if (!lex_force_match (lexer, T_SLASH))
74 goto error_free_handle_name;
76 if (!parse_file_handle (lexer, ds, &cmd, NULL))
77 goto error_free_handle_name;
79 if (lex_end_of_command (lexer) != CMD_SUCCESS)
82 if (cmd.mode != FH_SCRATCH)
84 struct fh_properties properties = *fh_default_properties ();
86 if (cmd.s_name == NULL)
88 lex_sbc_missing (lexer, "NAME");
95 properties.mode = FH_MODE_TEXT;
97 properties.tab_width = cmd.n_tabwidth[0];
100 properties.mode = FH_MODE_FIXED;
103 properties.mode = FH_MODE_VARIABLE;
106 properties.encoding = "EBCDIC-US";
107 if (cmd.recform == FH_FIXED || cmd.recform == FH_F)
108 properties.mode = FH_MODE_FIXED;
109 else if (cmd.recform == FH_VARIABLE || cmd.recform == FH_V)
111 properties.mode = FH_MODE_360_VARIABLE;
112 properties.record_width = 8192;
114 else if (cmd.recform == FH_SPANNED || cmd.recform == FH_VS)
116 properties.mode = FH_MODE_360_SPANNED;
117 properties.record_width = 8192;
121 msg (SE, _("RECFORM must be specified with MODE=360."));
129 if (properties.mode == FH_MODE_FIXED || cmd.n_lrecl[0] != LONG_MIN)
131 if (cmd.n_lrecl[0] == LONG_MIN)
132 msg (SE, _("The specified file mode requires LRECL. "
133 "Assuming %zu-character records."),
134 properties.record_width);
135 else if (cmd.n_lrecl[0] < 1 || cmd.n_lrecl[0] >= (1UL << 31))
136 msg (SE, _("Record length (%ld) must be between 1 and %lu bytes. "
137 "Assuming %d-character records."),
138 cmd.n_lrecl[0], (1UL << 31) - 1, properties.record_width);
140 properties.record_width = cmd.n_lrecl[0];
143 fh_create_file (handle_name, cmd.s_name, &properties);
146 fh_create_scratch (handle_name);
148 free_file_handle (&cmd);
152 free_file_handle (&cmd);
153 error_free_handle_name:
156 return CMD_CASCADING_FAILURE;
160 cmd_close_file_handle (struct lexer *lexer, struct dataset *ds UNUSED)
162 struct file_handle *handle;
164 if (!lex_force_id (lexer))
165 return CMD_CASCADING_FAILURE;
166 handle = fh_from_id (lex_tokcstr (lexer));
168 return CMD_CASCADING_FAILURE;
174 /* Returns the name for REFERENT. */
176 referent_name (enum fh_referent referent)
183 return _("inline file");
185 return _("scratch file");
191 /* Parses a file handle name, which may be a file name as a string
192 or a file handle name as an identifier. The allowed types of
193 file handle are restricted to those in REFERENT_MASK. Returns
194 the file handle when successful, a null pointer on failure.
196 The caller is responsible for fh_unref()'ing the returned
197 file handle when it is no longer needed. */
199 fh_parse (struct lexer *lexer, enum fh_referent referent_mask)
201 struct file_handle *handle;
203 if (lex_match_id (lexer, "INLINE"))
204 handle = fh_inline_file ();
207 if (lex_token (lexer) != T_ID && !lex_is_string (lexer))
209 lex_error (lexer, _("expecting a file name or handle name"));
214 if (lex_token (lexer) == T_ID)
215 handle = fh_from_id (lex_tokcstr (lexer));
218 if (lex_token (lexer) != T_ID || lex_tokcstr (lexer)[0] != '#'
219 || settings_get_syntax () != ENHANCED)
220 handle = fh_create_file (NULL, lex_tokcstr (lexer),
221 fh_default_properties ());
223 handle = fh_create_scratch (lex_tokcstr (lexer));
228 if (!(fh_get_referent (handle) & referent_mask))
230 msg (SE, _("Handle for %s not allowed here."),
231 referent_name (fh_get_referent (handle)));