Separated the abstraction of a file handle, from its implementation and
[pspp-builds.git] / src / file-handle.q
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 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 "file-handle.h"
22 #include "error.h"
23 #include <errno.h>
24 #include <stdlib.h>
25 #include "alloc.h"
26 #include "filename.h"
27 #include "command.h"
28 #include "lexer.h"
29 #include "getl.h"
30 #include "error.h"
31 #include "magic.h"
32 #include "var.h"
33 #include "linked-list.h"
34 #include "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.
48 */
49 /* (declarations) */
50 /* (functions) */
51
52
53 int
54 cmd_file_handle (void)
55 {
56   char handle_name[LONG_NAME_LEN + 1];
57
58   struct cmd_file_handle cmd;
59   struct file_handle *handle;
60
61   if (!lex_force_id ())
62     return CMD_FAILURE;
63   str_copy_trunc (handle_name, sizeof handle_name, tokid);
64
65   handle = get_handle_with_name (handle_name);
66   if (handle != NULL)
67     {
68       msg (SE, _("File handle %s already refers to file %s.  "
69                  "File handles cannot be redefined within a session."),
70            handle_name, handle_get_filename(handle));
71       return CMD_FAILURE;
72     }
73
74   lex_get ();
75   if (!lex_force_match ('/'))
76     return CMD_FAILURE;
77
78   if (!parse_file_handle (&cmd))
79     return CMD_FAILURE;
80
81   if (token != '.')
82     {
83       lex_error (_("expecting end of command"));
84       goto lossage;
85     }
86
87   if (cmd.s_name == NULL)
88     {
89       msg (SE, _("The FILE HANDLE required subcommand NAME "
90                  "is not present."));
91       goto lossage;
92     }
93
94
95   enum file_handle_mode mode = MODE_TEXT;
96   size_t length = 1024;
97   size_t tab_width = 4;
98
99
100   switch (cmd.mode)
101     {
102     case FH_CHARACTER:
103       mode = MODE_TEXT;
104       if (cmd.sbc_tabwidth)
105         tab_width = cmd.n_tabwidth[0];
106       else
107         tab_width = 4;
108       break;
109     case FH_IMAGE:
110       mode = MODE_BINARY;
111       if (cmd.n_lrecl[0] == NOT_LONG)
112         {
113           msg (SE, _("Fixed-length records were specified on /RECFORM, but "
114                      "record length was not specified on /LRECL.  "
115                      "Assuming 1024-character records."));
116           length = 1024;
117         }
118       else if (cmd.n_lrecl[0] < 1)
119         {
120           msg (SE, _("Record length (%ld) must be at least one byte.  "
121                      "1-character records will be assumed."), cmd.n_lrecl[0]);
122           length = 1;
123         }
124       else
125         length = cmd.n_lrecl[0];
126       break;
127     default:
128       assert (0);
129     }
130
131   handle = create_file_handle (handle_name, cmd.s_name, 
132                                mode, length, tab_width);
133
134
135   return CMD_SUCCESS;
136
137  lossage:
138   free_file_handle (&cmd);
139   return CMD_FAILURE;
140 }
141
142
143
144 static struct linked_list *handle_list;
145
146
147 /* Parses a file handle name, which may be a filename as a string or
148    a file handle name as an identifier.  Returns the file handle or
149    NULL on failure. */
150 struct file_handle *
151 fh_parse (void)
152 {
153   struct file_handle *handle;
154
155   if (token != T_ID && token != T_STRING)
156     {
157       lex_error (_("expecting a file name or handle name"));
158       return NULL;
159     }
160
161   /* Check for named handles first, then go by filename. */
162   handle = NULL;
163   if (token == T_ID) 
164     handle = get_handle_with_name (tokid);
165   if (handle == NULL)
166     handle = get_handle_for_filename (ds_c_str (&tokstr));
167   if (handle == NULL) 
168     {
169       char *filename = ds_c_str (&tokstr);
170       char *handle_name = xmalloc (strlen (filename) + 3);
171       sprintf (handle_name, "\"%s\"", filename);
172       handle = create_file_handle_with_defaults (handle_name, filename);
173       ll_push_front(handle_list, handle);
174       free (handle_name);
175     }
176
177   lex_get ();
178
179
180   return handle;
181 }
182
183
184
185 void 
186 fh_init(void)
187 {
188   handle_list = ll_create(destroy_file_handle,0);
189 }
190
191 void 
192 fh_done(void)
193 {
194   if ( handle_list )  
195   {
196     ll_destroy(handle_list);
197     handle_list = 0;
198   }
199 }
200
201
202 /*
203    Local variables:
204    mode: c
205    End:
206 */