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