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