0ac59caa0c337cf7470a376bda727c44bbfec40f
[pspp] / src / language / data-io / file-handle.q
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "data/file-handle-def.h"
20
21 #include <limits.h>
22 #include <errno.h>
23 #include <stdlib.h>
24
25 #include "data/file-name.h"
26 #include "data/session.h"
27 #include "data/variable.h"
28 #include "language/command.h"
29 #include "language/data-io/file-handle.h"
30 #include "language/lexer/lexer.h"
31 #include "libpspp/assertion.h"
32 #include "libpspp/cast.h"
33 #include "libpspp/message.h"
34 #include "libpspp/str.h"
35
36 #include "gl/xalloc.h"
37
38 #include "gettext.h"
39 #define _(msgid) gettext (msgid)
40
41 /* (headers) */
42
43
44 /* (specification)
45    "FILE HANDLE" (fh_):
46      name=string;
47      lrecl=integer;
48      tabwidth=integer;
49      mode=mode:!character/binary/image/360;
50      ends=ends:lf/crlf;
51      recform=recform:fixed/f/variable/v/spanned/vs;
52      encoding=string.
53 */
54 /* (declarations) */
55 /* (functions) */
56
57 int
58 cmd_file_handle (struct lexer *lexer, struct dataset *ds)
59 {
60   struct fh_properties properties;
61   struct cmd_file_handle cmd;
62   struct file_handle *handle;
63   enum cmd_result result;
64   char *handle_name;
65
66   result = CMD_CASCADING_FAILURE;
67   if (!lex_force_id (lexer))
68     goto exit;
69
70   handle_name = xstrdup (lex_tokcstr (lexer));
71   handle = fh_from_id (handle_name);
72   if (handle != NULL)
73     {
74       msg (SE, _("File handle %s is already defined.  "
75                  "Use %s before redefining a file handle."),
76            handle_name, "CLOSE FILE HANDLE");
77       goto exit_free_handle_name;
78     }
79
80   lex_get (lexer);
81   if (!lex_force_match (lexer, T_SLASH))
82     goto exit_free_handle_name;
83
84   if (!parse_file_handle (lexer, ds, &cmd, NULL))
85     goto exit_free_handle_name;
86
87   if (lex_end_of_command (lexer) != CMD_SUCCESS)
88     goto exit_free_cmd;
89
90   properties = *fh_default_properties ();
91   if (cmd.s_name == NULL)
92     {
93       lex_sbc_missing ("NAME");
94       goto exit_free_cmd;
95     }
96
97   switch (cmd.mode)
98     {
99     case FH_CHARACTER:
100       properties.mode = FH_MODE_TEXT;
101       if (cmd.sbc_tabwidth)
102         {
103           if (cmd.n_tabwidth[0] >= 0)
104             properties.tab_width = cmd.n_tabwidth[0];
105           else
106             msg (SE, _("%s must not be negative."), "TABWIDTH");
107         }
108       if (cmd.ends == FH_LF)
109         properties.line_ends = FH_END_LF;
110       else if (cmd.ends == FH_CRLF)
111         properties.line_ends = FH_END_CRLF;
112       break;
113     case FH_IMAGE:
114       properties.mode = FH_MODE_FIXED;
115       break;
116     case FH_BINARY:
117       properties.mode = FH_MODE_VARIABLE;
118       break;
119     case FH_360:
120       properties.encoding = CONST_CAST (char *, "EBCDIC-US");
121       if (cmd.recform == FH_FIXED || cmd.recform == FH_F)
122         properties.mode = FH_MODE_FIXED;
123       else if (cmd.recform == FH_VARIABLE || cmd.recform == FH_V)
124         {
125           properties.mode = FH_MODE_360_VARIABLE;
126           properties.record_width = 8192;
127         }
128       else if (cmd.recform == FH_SPANNED || cmd.recform == FH_VS)
129         {
130           properties.mode = FH_MODE_360_SPANNED;
131           properties.record_width = 8192;
132         }
133       else
134         {
135           msg (SE, _("%s must be specified with %s."), "RECFORM", "MODE=360");
136           goto exit_free_cmd;
137         }
138       break;
139     default:
140       NOT_REACHED ();
141     }
142
143   if (properties.mode == FH_MODE_FIXED || cmd.n_lrecl[0] != LONG_MIN)
144     {
145       if (cmd.n_lrecl[0] == LONG_MIN)
146         msg (SE, _("The specified file mode requires LRECL.  "
147                    "Assuming %zu-character records."),
148              properties.record_width);
149       else if (cmd.n_lrecl[0] < 1 || cmd.n_lrecl[0] >= (1UL << 31))
150         msg (SE, _("Record length (%ld) must be between 1 and %lu bytes.  "
151                    "Assuming %zu-character records."),
152              cmd.n_lrecl[0], (1UL << 31) - 1, properties.record_width);
153       else
154         properties.record_width = cmd.n_lrecl[0];
155     }
156
157   if (cmd.s_encoding != NULL)
158     properties.encoding = cmd.s_encoding;
159
160   fh_create_file (handle_name, cmd.s_name, &properties);
161
162   result = CMD_SUCCESS;
163
164 exit_free_cmd:
165   free_file_handle (&cmd);
166 exit_free_handle_name:
167   free (handle_name);
168 exit:
169   return result;
170 }
171
172 int
173 cmd_close_file_handle (struct lexer *lexer, struct dataset *ds UNUSED)
174 {
175   struct file_handle *handle;
176
177   if (!lex_force_id (lexer))
178     return CMD_CASCADING_FAILURE;
179   handle = fh_from_id (lex_tokcstr (lexer));
180   if (handle == NULL)
181     return CMD_CASCADING_FAILURE;
182
183   fh_unname (handle);
184   return CMD_SUCCESS;
185 }
186
187 /* Returns the name for REFERENT. */
188 static const char *
189 referent_name (enum fh_referent referent)
190 {
191   switch (referent)
192     {
193     case FH_REF_FILE:
194       return _("file");
195     case FH_REF_INLINE:
196       return _("inline file");
197     case FH_REF_DATASET:
198       return _("dataset");
199     default:
200       NOT_REACHED ();
201     }
202 }
203
204 /* Parses a file handle name:
205
206       - If SESSION is nonnull, then the parsed syntax may be the name of a
207         dataset within SESSION.  Dataset names take precedence over file handle
208         names.
209
210       - If REFERENT_MASK includes FH_REF_FILE, the parsed syntax may be a file
211         name as a string or a file handle name as an identifier.
212
213       - If REFERENT_MASK includes FH_REF_INLINE, the parsed syntax may be the
214         identifier INLINE to represent inline data.
215
216    Returns the file handle when successful, a null pointer on failure.
217
218    The caller is responsible for fh_unref()'ing the returned file handle when
219    it is no longer needed. */
220 struct file_handle *
221 fh_parse (struct lexer *lexer, enum fh_referent referent_mask,
222           struct session *session)
223 {
224   struct file_handle *handle;
225
226   if (session != NULL && lex_token (lexer) == T_ID)
227     {
228       struct dataset *ds;
229
230       ds = session_lookup_dataset (session, lex_tokcstr (lexer));
231       if (ds != NULL)
232         {
233           lex_get (lexer);
234           return fh_create_dataset (ds);
235         }
236     }
237
238   if (lex_match_id (lexer, "INLINE"))
239     handle = fh_inline_file ();
240   else
241     {
242       if (lex_token (lexer) != T_ID && !lex_is_string (lexer))
243         {
244           lex_error (lexer, _("expecting a file name or handle name"));
245           return NULL;
246         }
247
248       handle = NULL;
249       if (lex_token (lexer) == T_ID)
250         handle = fh_from_id (lex_tokcstr (lexer));
251       if (handle == NULL)
252             handle = fh_create_file (NULL, lex_tokcstr (lexer),
253                                      fh_default_properties ());
254       lex_get (lexer);
255     }
256
257   if (!(fh_get_referent (handle) & referent_mask))
258     {
259       msg (SE, _("Handle for %s not allowed here."),
260            referent_name (fh_get_referent (handle)));
261       fh_unref (handle);
262       return NULL;
263     }
264
265   return handle;
266 }
267
268 /*
269    Local variables:
270    mode: c
271    End:
272 */