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