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