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