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 "file-handle.h"
29 #include "line-buffer.h"
34 #include "file-handle-def.h"
37 #define _(msgid) gettext (msgid)
46 tabwidth=integer "x>=0" "%s must be nonnegative";
47 mode=mode:!character/image/scratch.
53 cmd_file_handle (void)
55 char handle_name[LONG_NAME_LEN + 1];
56 struct fh_properties properties = *fh_default_properties ();
58 struct cmd_file_handle cmd;
59 struct file_handle *handle;
62 return CMD_CASCADING_FAILURE;
63 str_copy_trunc (handle_name, sizeof handle_name, tokid);
65 handle = fh_from_name (handle_name);
68 msg (SE, _("File handle %s is already defined. "
69 "Use CLOSE FILE HANDLE before redefining a file handle."),
71 return CMD_CASCADING_FAILURE;
75 if (!lex_force_match ('/'))
76 return CMD_CASCADING_FAILURE;
78 if (!parse_file_handle (&cmd))
79 return CMD_CASCADING_FAILURE;
81 if (lex_end_of_command () != CMD_SUCCESS)
84 if (cmd.s_name == NULL && cmd.mode != FH_SCRATCH)
86 lex_sbc_missing ("NAME");
93 properties.mode = FH_MODE_TEXT;
95 properties.tab_width = cmd.n_tabwidth[0];
98 properties.mode = FH_MODE_BINARY;
99 if (cmd.n_lrecl[0] == NOT_LONG)
100 msg (SE, _("Fixed-length records were specified on /RECFORM, but "
101 "record length was not specified on /LRECL. "
102 "Assuming %d-character records."),
103 properties.record_width);
104 else if (cmd.n_lrecl[0] < 1)
105 msg (SE, _("Record length (%ld) must be at least one byte. "
106 "Assuming %d-character records."),
107 cmd.n_lrecl[0], properties.record_width);
109 properties.record_width = cmd.n_lrecl[0];
115 if (cmd.mode != FH_SCRATCH)
116 fh_create_file (handle_name, cmd.s_name, &properties);
118 fh_create_scratch (handle_name);
120 free_file_handle (&cmd);
124 free_file_handle (&cmd);
125 return CMD_CASCADING_FAILURE;
129 cmd_close_file_handle (void)
131 struct file_handle *handle;
133 if (!lex_force_id ())
134 return CMD_CASCADING_FAILURE;
135 handle = fh_from_name (tokid);
137 return CMD_CASCADING_FAILURE;
144 /* Returns the name for REFERENT. */
146 referent_name (enum fh_referent referent)
153 return _("inline file");
155 return _("scratch file");
161 /* Parses a file handle name, which may be a filename as a string
162 or a file handle name as an identifier. The allowed types of
163 file handle are restricted to those in REFERENT_MASK. Returns
164 the file handle when successful, a null pointer on failure. */
166 fh_parse (enum fh_referent referent_mask)
168 struct file_handle *handle;
170 if (lex_match_id ("INLINE"))
171 handle = fh_inline_file ();
174 if (token != T_ID && token != T_STRING)
176 lex_error (_("expecting a file name or handle name"));
182 handle = fh_from_name (tokid);
184 handle = fh_from_filename (ds_c_str (&tokstr));
187 if (token != T_ID || tokid[0] != '#' || get_syntax () != ENHANCED)
189 char *filename = ds_c_str (&tokstr);
190 char *handle_name = xasprintf ("\"%s\"", filename);
191 handle = fh_create_file (handle_name, filename,
192 fh_default_properties ());
196 handle = fh_create_scratch (tokid);
201 if (!(fh_get_referent (handle) & referent_mask))
203 msg (SE, _("Handle for %s not allowed here."),
204 referent_name (fh_get_referent (handle)));