0eb1373f7da832f699a42c4202d21ac0cdc4e06d
[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 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_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 already refers to file %s.  "
69                  "File handles cannot be redefined within a session."),
70            handle_name, fh_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   switch (cmd.mode)
95     {
96     case FH_CHARACTER:
97       properties.mode = MODE_TEXT;
98       if (cmd.sbc_tabwidth)
99         properties.tab_width = cmd.n_tabwidth[0];
100       break;
101     case FH_IMAGE:
102       properties.mode = MODE_BINARY;
103       if (cmd.n_lrecl[0] == NOT_LONG)
104         msg (SE, _("Fixed-length records were specified on /RECFORM, but "
105                    "record length was not specified on /LRECL.  "
106                    "Assuming %d-character records."),
107              properties.record_width);
108       else if (cmd.n_lrecl[0] < 1)
109         msg (SE, _("Record length (%ld) must be at least one byte.  "
110                    "Assuming %d-character records."),
111              cmd.n_lrecl[0], properties.record_width);
112       else
113         properties.record_width = cmd.n_lrecl[0];
114       break;
115     default:
116       assert (0);
117     }
118
119   handle = fh_create (handle_name, cmd.s_name, &properties);
120
121   free_file_handle (&cmd);
122   return CMD_SUCCESS;
123
124  lossage:
125   free_file_handle (&cmd);
126   return CMD_FAILURE;
127 }
128
129 /* Parses a file handle name, which may be a filename as a string or
130    a file handle name as an identifier.  Returns the file handle or
131    NULL on failure. */
132 struct file_handle *
133 fh_parse (void)
134 {
135   struct file_handle *handle;
136
137   if (token != T_ID && token != T_STRING)
138     {
139       lex_error (_("expecting a file name or handle name"));
140       return NULL;
141     }
142
143   /* Check for named handles first, then go by filename. */
144   handle = NULL;
145   if (token == T_ID) 
146     handle = fh_from_name (tokid);
147   if (handle == NULL)
148     handle = fh_from_filename (ds_c_str (&tokstr));
149   if (handle == NULL) 
150     {
151       char *filename = ds_c_str (&tokstr);
152       char *handle_name = xmalloc (strlen (filename) + 3);
153       sprintf (handle_name, "\"%s\"", filename);
154       handle = fh_create (handle_name, filename, fh_default_properties ());
155       free (handle_name);
156     }
157
158   lex_get ();
159
160   return handle;
161 }
162
163 /*
164    Local variables:
165    mode: c
166    End:
167 */