Add aux data parameter to q2c parse_<command>() and custom parser
[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/message.h>
31 #include <libpspp/magic.h>
32 #include <libpspp/str.h>
33 #include <data/variable.h>
34 #include <data/file-handle-def.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 "x>=0" "%s must be nonnegative";
47      mode=mode:!character/image/scratch.
48 */
49 /* (declarations) */
50 /* (functions) */
51
52 int
53 cmd_file_handle (void)
54 {
55   char handle_name[LONG_NAME_LEN + 1];
56   struct fh_properties properties = *fh_default_properties ();
57
58   struct cmd_file_handle cmd;
59   struct file_handle *handle;
60
61   if (!lex_force_id ())
62     return CMD_CASCADING_FAILURE;
63   str_copy_trunc (handle_name, sizeof handle_name, tokid);
64
65   handle = fh_from_name (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       return CMD_CASCADING_FAILURE;
72     }
73
74   lex_get ();
75   if (!lex_force_match ('/'))
76     return CMD_CASCADING_FAILURE;
77
78   if (!parse_file_handle (&cmd, NULL))
79     return CMD_CASCADING_FAILURE;
80
81   if (lex_end_of_command () != CMD_SUCCESS)
82     goto lossage;
83
84   if (cmd.s_name == NULL && cmd.mode != FH_SCRATCH)
85     {
86       lex_sbc_missing ("NAME");
87       goto lossage;
88     }
89
90   switch (cmd.mode)
91     {
92     case FH_CHARACTER:
93       properties.mode = FH_MODE_TEXT;
94       if (cmd.sbc_tabwidth)
95         properties.tab_width = cmd.n_tabwidth[0];
96       break;
97     case FH_IMAGE:
98       properties.mode = FH_MODE_BINARY;
99       if (cmd.n_lrecl[0] == NOT_LONG)
100         msg (SE, _("Fixed-length records were specified on /RECFORM, but "
101                    "record length was not specified on /LRECL.  "
102                    "Assuming %d-character records."),
103              properties.record_width);
104       else if (cmd.n_lrecl[0] < 1)
105         msg (SE, _("Record length (%ld) must be at least one byte.  "
106                    "Assuming %d-character records."),
107              cmd.n_lrecl[0], properties.record_width);
108       else
109         properties.record_width = cmd.n_lrecl[0];
110       break;
111     default:
112       assert (0);
113     }
114
115   if (cmd.mode != FH_SCRATCH)
116     fh_create_file (handle_name, cmd.s_name, &properties);
117   else
118     fh_create_scratch (handle_name);
119
120   free_file_handle (&cmd);
121   return CMD_SUCCESS;
122
123  lossage:
124   free_file_handle (&cmd);
125   return CMD_CASCADING_FAILURE;
126 }
127
128 int
129 cmd_close_file_handle (void) 
130 {
131   struct file_handle *handle;
132
133   if (!lex_force_id ())
134     return CMD_CASCADING_FAILURE;
135   handle = fh_from_name (tokid);
136   if (handle == NULL)
137     return CMD_CASCADING_FAILURE;
138
139   fh_free (handle);
140
141   return CMD_SUCCESS;
142 }
143
144 /* Returns the name for REFERENT. */
145 static const char *
146 referent_name (enum fh_referent referent) 
147 {
148   switch (referent) 
149     {
150     case FH_REF_FILE:
151       return _("file");
152     case FH_REF_INLINE:
153       return _("inline file");
154     case FH_REF_SCRATCH:
155       return _("scratch file");
156     default:
157       abort ();
158     }
159 }
160
161 /* Parses a file handle name, which may be a file name as a string
162    or a file handle name as an identifier.  The allowed types of
163    file handle are restricted to those in REFERENT_MASK.  Returns
164    the file handle when successful, a null pointer on failure. */
165 struct file_handle *
166 fh_parse (enum fh_referent referent_mask)
167 {
168   struct file_handle *handle;
169
170   if (lex_match_id ("INLINE")) 
171     handle = fh_inline_file ();
172   else 
173     {
174       if (token != T_ID && token != T_STRING)
175         {
176           lex_error (_("expecting a file name or handle name"));
177           return NULL;
178         }
179
180       handle = NULL;
181       if (token == T_ID) 
182         handle = fh_from_name (tokid);
183       if (handle == NULL) 
184         handle = fh_from_file_name (ds_cstr (&tokstr)); 
185       if (handle == NULL)
186         {
187           if (token != T_ID || tokid[0] != '#' || get_syntax () != ENHANCED) 
188             {
189               char *file_name = ds_cstr (&tokstr);
190               char *handle_name = xasprintf ("\"%s\"", file_name);
191               handle = fh_create_file (handle_name, file_name,
192                                        fh_default_properties ());
193               free (handle_name);
194             }
195           else
196             handle = fh_create_scratch (tokid);
197         }
198       lex_get ();
199     }
200
201   if (!(fh_get_referent (handle) & referent_mask)) 
202     {
203       msg (SE, _("Handle for %s not allowed here."),
204            referent_name (fh_get_referent (handle)));
205       return NULL;
206     }
207
208   return handle;
209 }
210
211 /*
212    Local variables:
213    mode: c
214    End:
215 */