FILE HANDLE: Improve error messages and coding style.
[pspp] / src / language / data-io / file-handle.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-2000, 2006, 2010-2013, 2016 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 int
42 cmd_file_handle (struct lexer *lexer, struct dataset *ds UNUSED)
43 {
44   enum cmd_result result = CMD_CASCADING_FAILURE;
45   char *handle_name = NULL;
46   char *file_name = NULL;
47   int lrecl = 0;
48   int tabwidth = -1;
49   enum { MODE_DEFAULT, MODE_CHARACTER, MODE_BINARY, MODE_IMAGE, MODE_360 }
50       mode = MODE_DEFAULT;
51   int ends = -1;
52   enum { RECFORM_FIXED = 1, RECFORM_VARIABLE, RECFORM_SPANNED } recform = 0;
53   char *encoding = NULL;
54
55   if (!lex_force_id (lexer))
56     goto exit;
57
58   handle_name = xstrdup (lex_tokcstr (lexer));
59   struct file_handle *fh = fh_from_id (handle_name);
60   if (fh)
61     {
62       fh_unref (fh);
63       lex_error (lexer, _("File handle %s is already defined.  "
64                           "Use %s before redefining a file handle."),
65                  handle_name, "CLOSE FILE HANDLE");
66       goto exit;
67     }
68
69   lex_get (lexer);
70   if (!lex_force_match (lexer, T_SLASH))
71     goto exit;
72
73   int mode_start = 0;
74   int mode_end = 0;
75   while (lex_token (lexer) != T_ENDCMD)
76     {
77       if (lex_match_id (lexer, "NAME"))
78         {
79           if (file_name)
80             {
81               lex_sbc_only_once (lexer, "NAME");
82               goto exit;
83             }
84
85           lex_match (lexer, T_EQUALS);
86           if (!lex_force_string (lexer))
87             goto exit;
88           free (file_name);
89           file_name = ss_xstrdup (lex_tokss (lexer));
90           lex_get (lexer);
91         }
92       else if (lex_match_id (lexer, "LRECL"))
93         {
94           if (lrecl)
95             {
96               lex_sbc_only_once (lexer, "LRECL");
97               goto exit;
98             }
99
100           lex_match (lexer, T_EQUALS);
101           if (!lex_force_int_range (lexer, "LRECL", 1, (1UL << 31) - 1))
102             goto exit;
103           lrecl = lex_integer (lexer);
104           lex_get (lexer);
105         }
106       else if (lex_match_id (lexer, "TABWIDTH"))
107         {
108           if (tabwidth >= 0)
109             {
110               lex_sbc_only_once (lexer, "TABWIDTH");
111               goto exit;
112             }
113           lex_match (lexer, T_EQUALS);
114
115           if (!lex_force_int_range (lexer, "TABWIDTH", 1, INT_MAX))
116             goto exit;
117           tabwidth = lex_integer (lexer);
118           lex_get (lexer);
119         }
120       else if (lex_match_id (lexer, "MODE"))
121         {
122           if (mode != MODE_DEFAULT)
123             {
124               lex_sbc_only_once (lexer, "MODE");
125               goto exit;
126             }
127           mode_start = lex_ofs (lexer) - 1;
128           lex_match (lexer, T_EQUALS);
129
130           if (lex_match_id (lexer, "CHARACTER"))
131             mode = MODE_CHARACTER;
132           else if (lex_match_id (lexer, "BINARY"))
133             mode = MODE_BINARY;
134           else if (lex_match_id (lexer, "IMAGE"))
135             mode = MODE_IMAGE;
136           else if (lex_match_int (lexer, 360))
137             mode = MODE_360;
138           else
139             {
140               lex_error_expecting (lexer, "CHARACTER", "BINARY",
141                                    "IMAGE", "360");
142               goto exit;
143             }
144           mode_end = lex_ofs (lexer) - 1;
145         }
146       else if (lex_match_id (lexer, "ENDS"))
147         {
148           if (ends >= 0)
149             {
150               lex_sbc_only_once (lexer, "ENDS");
151               goto exit;
152             }
153           lex_match (lexer, T_EQUALS);
154
155           if (lex_match_id (lexer, "LF"))
156             ends = FH_END_LF;
157           else if (lex_match_id (lexer, "CRLF"))
158             ends = FH_END_CRLF;
159           else
160             {
161               lex_error_expecting (lexer, "LF", "CRLF");
162               goto exit;
163             }
164         }
165       else if (lex_match_id (lexer, "RECFORM"))
166         {
167           if (recform)
168             {
169               lex_sbc_only_once (lexer, "RECFORM");
170               goto exit;
171             }
172           lex_match (lexer, T_EQUALS);
173           if (lex_match_id (lexer, "FIXED") || lex_match_id (lexer, "F"))
174             recform = RECFORM_FIXED;
175           else if (lex_match_id (lexer, "VARIABLE")
176                    || lex_match_id (lexer, "V"))
177             recform = RECFORM_VARIABLE;
178           else if (lex_match_id (lexer, "SPANNED")
179                    || lex_match_id (lexer, "VS"))
180             recform = RECFORM_SPANNED;
181           else
182             {
183               lex_error_expecting (lexer, "FIXED", "VARIABLE", "SPANNED");
184               goto exit;
185             }
186         }
187       else if (lex_match_id (lexer, "ENCODING"))
188         {
189           if (encoding)
190             {
191               lex_sbc_only_once (lexer, "ENCODING");
192               goto exit;
193             }
194
195           lex_match (lexer, T_EQUALS);
196           if (!lex_force_string (lexer))
197             goto exit;
198           free (encoding);
199           encoding = ss_xstrdup (lex_tokss (lexer));
200           lex_get (lexer);
201         }
202       if (!lex_match (lexer, T_SLASH))
203         break;
204     }
205
206   if (lex_end_of_command (lexer) != CMD_SUCCESS)
207     goto exit;
208
209   struct fh_properties properties = *fh_default_properties ();
210   if (file_name == NULL)
211     {
212       lex_sbc_missing (lexer, "NAME");
213       goto exit;
214     }
215
216   switch (mode)
217     {
218     case MODE_DEFAULT:
219     case MODE_CHARACTER:
220       properties.mode = FH_MODE_TEXT;
221       if (tabwidth >= 0)
222         properties.tab_width = tabwidth;
223       if (ends)
224         properties.line_ends = ends;
225       break;
226     case MODE_IMAGE:
227       properties.mode = FH_MODE_FIXED;
228       break;
229     case MODE_BINARY:
230       properties.mode = FH_MODE_VARIABLE;
231       break;
232     case MODE_360:
233       properties.encoding = CONST_CAST (char *, "EBCDIC-US");
234       if (recform == RECFORM_FIXED)
235         properties.mode = FH_MODE_FIXED;
236       else if (recform == RECFORM_VARIABLE)
237         {
238           properties.mode = FH_MODE_360_VARIABLE;
239           properties.record_width = 8192;
240         }
241       else if (recform == RECFORM_SPANNED)
242         {
243           properties.mode = FH_MODE_360_SPANNED;
244           properties.record_width = 8192;
245         }
246       else
247         {
248           lex_ofs_error (lexer, mode_start, mode_end,
249                          _("%s must be specified with %s."),
250                          "RECFORM", "MODE=360");
251           goto exit;
252         }
253       break;
254     default:
255       NOT_REACHED ();
256     }
257
258   if (properties.mode == FH_MODE_FIXED || lrecl)
259     {
260       if (!lrecl)
261         msg (SE, _("The specified file mode requires LRECL.  "
262                    "Assuming %zu-character records."),
263              properties.record_width);
264       else
265         properties.record_width = lrecl;
266     }
267
268   if (encoding)
269     properties.encoding = encoding;
270
271   fh_create_file (handle_name, file_name, lex_get_encoding (lexer),
272                   &properties);
273
274   result = CMD_SUCCESS;
275
276 exit:
277   free (handle_name);
278   free (file_name);
279   free (encoding);
280   return result;
281 }
282
283 int
284 cmd_close_file_handle (struct lexer *lexer, struct dataset *ds UNUSED)
285 {
286   struct file_handle *handle;
287
288   if (!lex_force_id (lexer))
289     return CMD_CASCADING_FAILURE;
290   handle = fh_from_id (lex_tokcstr (lexer));
291   if (handle == NULL)
292     return CMD_CASCADING_FAILURE;
293
294   fh_unname (handle);
295   return CMD_SUCCESS;
296 }
297
298 /* Returns the name for REFERENT. */
299 static const char *
300 referent_name (enum fh_referent referent)
301 {
302   switch (referent)
303     {
304     case FH_REF_FILE:
305       return _("file");
306     case FH_REF_INLINE:
307       return _("inline file");
308     case FH_REF_DATASET:
309       return _("dataset");
310     default:
311       NOT_REACHED ();
312     }
313 }
314
315 /* Parses a file handle name:
316
317       - If SESSION is nonnull, then the parsed syntax may be the name of a
318         dataset within SESSION.  Dataset names take precedence over file handle
319         names.
320
321       - If REFERENT_MASK includes FH_REF_FILE, the parsed syntax may be a file
322         name as a string or a file handle name as an identifier.
323
324       - If REFERENT_MASK includes FH_REF_INLINE, the parsed syntax may be the
325         identifier INLINE to represent inline data.
326
327    Returns the file handle when successful, a null pointer on failure.
328
329    The caller is responsible for fh_unref()'ing the returned file handle when
330    it is no longer needed. */
331 struct file_handle *
332 fh_parse (struct lexer *lexer, enum fh_referent referent_mask,
333           struct session *session)
334 {
335   if (session != NULL && lex_token (lexer) == T_ID)
336     {
337       struct dataset *ds;
338
339       ds = session_lookup_dataset (session, lex_tokcstr (lexer));
340       if (ds != NULL)
341         {
342           lex_get (lexer);
343           return fh_create_dataset (ds);
344         }
345     }
346
347   int start_ofs = lex_ofs (lexer);
348   struct file_handle *handle;
349   if (lex_match_id (lexer, "INLINE"))
350     handle = fh_inline_file ();
351   else
352     {
353       if (lex_token (lexer) != T_ID && !lex_is_string (lexer))
354         {
355           lex_error (lexer,
356                      _("Syntax error expecting a file name or handle name."));
357           return NULL;
358         }
359
360       handle = NULL;
361       if (lex_token (lexer) == T_ID)
362         handle = fh_from_id (lex_tokcstr (lexer));
363       if (handle == NULL)
364         handle = fh_create_file (NULL, lex_tokcstr (lexer), lex_get_encoding (lexer),
365                                      fh_default_properties ());
366       lex_get (lexer);
367     }
368
369   if (!(fh_get_referent (handle) & referent_mask))
370     {
371       lex_ofs_error (lexer, start_ofs, lex_ofs (lexer) - 1,
372                      _("Handle for %s not allowed here."),
373                      referent_name (fh_get_referent (handle)));
374       fh_unref (handle);
375       return NULL;
376     }
377
378   return handle;
379 }