1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include <language/data-io/file-handle.h>
22 #include <libpspp/message.h>
25 #include <libpspp/alloc.h>
26 #include <data/file-name.h>
27 #include <language/command.h>
28 #include <language/lexer/lexer.h>
29 #include <language/line-buffer.h>
30 #include <libpspp/assertion.h>
31 #include <libpspp/message.h>
32 #include <libpspp/magic.h>
33 #include <libpspp/str.h>
34 #include <data/variable.h>
35 #include <data/file-handle-def.h>
38 #define _(msgid) gettext (msgid)
47 tabwidth=integer "x>=0" "%s must be nonnegative";
48 mode=mode:!character/image/scratch.
54 cmd_file_handle (struct dataset *ds)
56 char handle_name[LONG_NAME_LEN + 1];
57 struct fh_properties properties = *fh_default_properties ();
59 struct cmd_file_handle cmd;
60 struct file_handle *handle;
63 return CMD_CASCADING_FAILURE;
64 str_copy_trunc (handle_name, sizeof handle_name, tokid);
66 handle = fh_from_name (handle_name);
69 msg (SE, _("File handle %s is already defined. "
70 "Use CLOSE FILE HANDLE before redefining a file handle."),
72 return CMD_CASCADING_FAILURE;
76 if (!lex_force_match ('/'))
77 return CMD_CASCADING_FAILURE;
79 if (!parse_file_handle (ds, &cmd, NULL))
80 return CMD_CASCADING_FAILURE;
82 if (lex_end_of_command () != CMD_SUCCESS)
85 if (cmd.s_name == NULL && cmd.mode != FH_SCRATCH)
87 lex_sbc_missing ("NAME");
94 properties.mode = FH_MODE_TEXT;
96 properties.tab_width = cmd.n_tabwidth[0];
99 properties.mode = FH_MODE_BINARY;
100 if (cmd.n_lrecl[0] == NOT_LONG)
101 msg (SE, _("Fixed-length records were specified on /RECFORM, but "
102 "record length was not specified on /LRECL. "
103 "Assuming %d-character records."),
104 properties.record_width);
105 else if (cmd.n_lrecl[0] < 1)
106 msg (SE, _("Record length (%ld) must be at least one byte. "
107 "Assuming %d-character records."),
108 cmd.n_lrecl[0], properties.record_width);
110 properties.record_width = cmd.n_lrecl[0];
116 if (cmd.mode != FH_SCRATCH)
117 fh_create_file (handle_name, cmd.s_name, &properties);
119 fh_create_scratch (handle_name);
121 free_file_handle (&cmd);
125 free_file_handle (&cmd);
126 return CMD_CASCADING_FAILURE;
130 cmd_close_file_handle (struct dataset *ds UNUSED)
132 struct file_handle *handle;
134 if (!lex_force_id ())
135 return CMD_CASCADING_FAILURE;
136 handle = fh_from_name (tokid);
138 return CMD_CASCADING_FAILURE;
145 /* Returns the name for REFERENT. */
147 referent_name (enum fh_referent referent)
154 return _("inline file");
156 return _("scratch file");
162 /* Parses a file handle name, which may be a file name as a string
163 or a file handle name as an identifier. The allowed types of
164 file handle are restricted to those in REFERENT_MASK. Returns
165 the file handle when successful, a null pointer on failure. */
167 fh_parse (enum fh_referent referent_mask)
169 struct file_handle *handle;
171 if (lex_match_id ("INLINE"))
172 handle = fh_inline_file ();
175 if (token != T_ID && token != T_STRING)
177 lex_error (_("expecting a file name or handle name"));
183 handle = fh_from_name (tokid);
185 handle = fh_from_file_name (ds_cstr (&tokstr));
188 if (token != T_ID || tokid[0] != '#' || get_syntax () != ENHANCED)
190 char *file_name = ds_cstr (&tokstr);
191 char *handle_name = xasprintf ("\"%s\"", file_name);
192 handle = fh_create_file (handle_name, file_name,
193 fh_default_properties ());
197 handle = fh_create_scratch (tokid);
202 if (!(fh_get_referent (handle) & referent_mask))
204 msg (SE, _("Handle for %s not allowed here."),
205 referent_name (fh_get_referent (handle)));