1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 #include <language/data-io/file-handle.h>
21 #include <libpspp/message.h>
24 #include <libpspp/alloc.h>
25 #include <data/file-name.h>
26 #include <language/command.h>
27 #include <language/lexer/lexer.h>
28 #include <libpspp/assertion.h>
29 #include <libpspp/message.h>
30 #include <libpspp/magic.h>
31 #include <libpspp/str.h>
32 #include <data/variable.h>
33 #include <data/file-handle-def.h>
36 #define _(msgid) gettext (msgid)
45 tabwidth=integer "x>=0" "%s must be nonnegative";
46 mode=mode:!character/image/scratch.
52 cmd_file_handle (struct lexer *lexer, struct dataset *ds)
54 char handle_name[LONG_NAME_LEN + 1];
55 struct fh_properties properties = *fh_default_properties ();
57 struct cmd_file_handle cmd;
58 struct file_handle *handle;
60 if (!lex_force_id (lexer))
61 return CMD_CASCADING_FAILURE;
62 str_copy_trunc (handle_name, sizeof handle_name, lex_tokid (lexer));
64 handle = fh_from_id (handle_name);
67 msg (SE, _("File handle %s is already defined. "
68 "Use CLOSE FILE HANDLE before redefining a file handle."),
70 return CMD_CASCADING_FAILURE;
74 if (!lex_force_match (lexer, '/'))
75 return CMD_CASCADING_FAILURE;
77 if (!parse_file_handle (lexer, ds, &cmd, NULL))
78 return CMD_CASCADING_FAILURE;
80 if (lex_end_of_command (lexer) != CMD_SUCCESS)
83 if (cmd.s_name == NULL && cmd.mode != FH_SCRATCH)
85 lex_sbc_missing (lexer, "NAME");
92 properties.mode = FH_MODE_TEXT;
94 properties.tab_width = cmd.n_tabwidth[0];
97 properties.mode = FH_MODE_BINARY;
98 if (cmd.n_lrecl[0] == NOT_LONG)
99 msg (SE, _("Fixed-length records were specified on /RECFORM, but "
100 "record length was not specified on /LRECL. "
101 "Assuming %u-character records."),
102 (unsigned int) properties.record_width);
103 else if (cmd.n_lrecl[0] < 1)
104 msg (SE, _("Record length (%ld) must be at least one byte. "
105 "Assuming %u-character records."),
106 cmd.n_lrecl[0], (unsigned int) properties.record_width);
108 properties.record_width = cmd.n_lrecl[0];
114 if (cmd.mode != FH_SCRATCH)
115 fh_create_file (handle_name, cmd.s_name, &properties);
117 fh_create_scratch (handle_name);
119 free_file_handle (&cmd);
123 free_file_handle (&cmd);
124 return CMD_CASCADING_FAILURE;
128 cmd_close_file_handle (struct lexer *lexer, struct dataset *ds UNUSED)
130 struct file_handle *handle;
132 if (!lex_force_id (lexer))
133 return CMD_CASCADING_FAILURE;
134 handle = fh_from_id (lex_tokid (lexer));
136 return CMD_CASCADING_FAILURE;
143 /* Returns the name for REFERENT. */
145 referent_name (enum fh_referent referent)
152 return _("inline file");
154 return _("scratch file");
160 /* Parses a file handle name, which may be a file name as a string
161 or a file handle name as an identifier. The allowed types of
162 file handle are restricted to those in REFERENT_MASK. Returns
163 the file handle when successful, a null pointer on failure. */
165 fh_parse (struct lexer *lexer, enum fh_referent referent_mask)
167 struct file_handle *handle;
169 if (lex_match_id (lexer, "INLINE"))
170 handle = fh_inline_file ();
173 if (lex_token (lexer) != T_ID && lex_token (lexer) != T_STRING)
175 lex_error (lexer, _("expecting a file name or handle name"));
180 if (lex_token (lexer) == T_ID)
181 handle = fh_from_id (lex_tokid (lexer));
183 handle = fh_from_file_name (ds_cstr (lex_tokstr (lexer)));
186 if (lex_token (lexer) != T_ID || lex_tokid (lexer)[0] != '#' || get_syntax () != ENHANCED)
187 handle = fh_create_file (NULL, ds_cstr (lex_tokstr (lexer)),
188 fh_default_properties ());
190 handle = fh_create_scratch (lex_tokid (lexer));
195 if (!(fh_get_referent (handle) & referent_mask))
197 msg (SE, _("Handle for %s not allowed here."),
198 referent_name (fh_get_referent (handle)));