Eliminated global variable current_dataset.
[pspp-builds.git] / src / language / data-io / file-handle.q
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include <language/data-io/file-handle.h>
22 #include <libpspp/message.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <libpspp/alloc.h>
26 #include <data/file-name.h>
27 #include <language/command.h>
28 #include <language/lexer/lexer.h>
29 #include <language/line-buffer.h>
30 #include <libpspp/assertion.h>
31 #include <libpspp/message.h>
32 #include <libpspp/magic.h>
33 #include <libpspp/str.h>
34 #include <data/variable.h>
35 #include <data/file-handle-def.h>
36
37 #include "gettext.h"
38 #define _(msgid) gettext (msgid)
39
40 /* (headers) */
41
42
43 /* (specification)
44    "FILE HANDLE" (fh_):
45      name=string;
46      lrecl=integer;
47      tabwidth=integer "x>=0" "%s must be nonnegative";
48      mode=mode:!character/image/scratch.
49 */
50 /* (declarations) */
51 /* (functions) */
52
53 int
54 cmd_file_handle (struct dataset *ds)
55 {
56   char handle_name[LONG_NAME_LEN + 1];
57   struct fh_properties properties = *fh_default_properties ();
58
59   struct cmd_file_handle cmd;
60   struct file_handle *handle;
61
62   if (!lex_force_id ())
63     return CMD_CASCADING_FAILURE;
64   str_copy_trunc (handle_name, sizeof handle_name, tokid);
65
66   handle = fh_from_name (handle_name);
67   if (handle != NULL)
68     {
69       msg (SE, _("File handle %s is already defined.  "
70                  "Use CLOSE FILE HANDLE before redefining a file handle."),
71            handle_name);
72       return CMD_CASCADING_FAILURE;
73     }
74
75   lex_get ();
76   if (!lex_force_match ('/'))
77     return CMD_CASCADING_FAILURE;
78
79   if (!parse_file_handle (ds, &cmd, NULL))
80     return CMD_CASCADING_FAILURE;
81
82   if (lex_end_of_command () != CMD_SUCCESS)
83     goto lossage;
84
85   if (cmd.s_name == NULL && cmd.mode != FH_SCRATCH)
86     {
87       lex_sbc_missing ("NAME");
88       goto lossage;
89     }
90
91   switch (cmd.mode)
92     {
93     case FH_CHARACTER:
94       properties.mode = FH_MODE_TEXT;
95       if (cmd.sbc_tabwidth)
96         properties.tab_width = cmd.n_tabwidth[0];
97       break;
98     case FH_IMAGE:
99       properties.mode = FH_MODE_BINARY;
100       if (cmd.n_lrecl[0] == NOT_LONG)
101         msg (SE, _("Fixed-length records were specified on /RECFORM, but "
102                    "record length was not specified on /LRECL.  "
103                    "Assuming %d-character records."),
104              properties.record_width);
105       else if (cmd.n_lrecl[0] < 1)
106         msg (SE, _("Record length (%ld) must be at least one byte.  "
107                    "Assuming %d-character records."),
108              cmd.n_lrecl[0], properties.record_width);
109       else
110         properties.record_width = cmd.n_lrecl[0];
111       break;
112     default:
113       NOT_REACHED ();
114     }
115
116   if (cmd.mode != FH_SCRATCH)
117     fh_create_file (handle_name, cmd.s_name, &properties);
118   else
119     fh_create_scratch (handle_name);
120
121   free_file_handle (&cmd);
122   return CMD_SUCCESS;
123
124  lossage:
125   free_file_handle (&cmd);
126   return CMD_CASCADING_FAILURE;
127 }
128
129 int
130 cmd_close_file_handle (struct dataset *ds UNUSED) 
131 {
132   struct file_handle *handle;
133
134   if (!lex_force_id ())
135     return CMD_CASCADING_FAILURE;
136   handle = fh_from_name (tokid);
137   if (handle == NULL)
138     return CMD_CASCADING_FAILURE;
139
140   fh_free (handle);
141
142   return CMD_SUCCESS;
143 }
144
145 /* Returns the name for REFERENT. */
146 static const char *
147 referent_name (enum fh_referent referent) 
148 {
149   switch (referent) 
150     {
151     case FH_REF_FILE:
152       return _("file");
153     case FH_REF_INLINE:
154       return _("inline file");
155     case FH_REF_SCRATCH:
156       return _("scratch file");
157     default:
158       NOT_REACHED ();
159     }
160 }
161
162 /* Parses a file handle name, which may be a file name as a string
163    or a file handle name as an identifier.  The allowed types of
164    file handle are restricted to those in REFERENT_MASK.  Returns
165    the file handle when successful, a null pointer on failure. */
166 struct file_handle *
167 fh_parse (enum fh_referent referent_mask)
168 {
169   struct file_handle *handle;
170
171   if (lex_match_id ("INLINE")) 
172     handle = fh_inline_file ();
173   else 
174     {
175       if (token != T_ID && token != T_STRING)
176         {
177           lex_error (_("expecting a file name or handle name"));
178           return NULL;
179         }
180
181       handle = NULL;
182       if (token == T_ID) 
183         handle = fh_from_name (tokid);
184       if (handle == NULL) 
185         handle = fh_from_file_name (ds_cstr (&tokstr)); 
186       if (handle == NULL)
187         {
188           if (token != T_ID || tokid[0] != '#' || get_syntax () != ENHANCED) 
189             {
190               char *file_name = ds_cstr (&tokstr);
191               char *handle_name = xasprintf ("\"%s\"", file_name);
192               handle = fh_create_file (handle_name, file_name,
193                                        fh_default_properties ());
194               free (handle_name);
195             }
196           else
197             handle = fh_create_scratch (tokid);
198         }
199       lex_get ();
200     }
201
202   if (!(fh_get_referent (handle) & referent_mask)) 
203     {
204       msg (SE, _("Handle for %s not allowed here."),
205            referent_name (fh_get_referent (handle)));
206       return NULL;
207     }
208
209   return handle;
210 }
211
212 /*
213    Local variables:
214    mode: c
215    End:
216 */