maintainer changed: was lord, now jimb; first import
[bpt/guile.git] / libguile / fports.c
1 /* Copyright (C) 1995,1996 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
19 *
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
25 *
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
28 *
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
36 *
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
40 */
41 \f
42
43 #include <stdio.h>
44 #include "_scm.h"
45 #ifdef HAVE_UNISTD_H
46 #include <unistd.h>
47 #else
48 char *ttyname ();
49 char *tmpnam ();
50 scm_sizet fwrite ();
51 #endif
52 #ifdef HAVE_STRING_H
53 #include "string.h"
54 #endif
55
56
57 #ifdef __IBMC__
58 #include <io.h>
59 #include <direct.h>
60 #define ttyname(x) "CON:"
61 #else
62 #ifndef MSDOS
63 #ifndef ultrix
64 #ifndef vms
65 #ifdef _DCC
66 #include <ioctl.h>
67 #define setbuf(stream, buf) setvbuf(stream, buf, _IONBF, 0)
68 #else
69 #ifdef MWC
70 #include <sys/io.h>
71 #else
72 #ifndef THINK_C
73 #ifndef ARM_ULIB
74 #include <sys/ioctl.h>
75 #endif
76 #endif
77 #endif
78 #endif
79 #endif
80 #endif
81 #endif
82 #endif
83 \f
84
85 /* {Ports - file ports}
86 *
87 */
88
89 /* should be called with SCM_DEFER_INTS active */
90 #ifdef __STDC__
91 SCM
92 scm_setbuf0 (SCM port)
93 #else
94 SCM
95 scm_setbuf0 (port)
96 SCM port;
97 #endif
98 {
99 #ifndef NOSETBUF
100 #ifndef MSDOS
101 #ifdef FIONREAD
102 #ifndef ultrix
103 SCM_SYSCALL (setbuf ((FILE *)SCM_STREAM (port), 0););
104 #endif
105 #endif
106 #endif
107 #endif
108 return SCM_UNSPECIFIED;
109 }
110
111 /* Return the flags that characterize a port based on the mode
112 * string used to open a file for that port.
113 *
114 * See PORT FLAGS in scm.h
115 */
116 #ifdef __STDC__
117 long
118 scm_mode_bits (char *modes)
119 #else
120 long
121 scm_mode_bits (modes)
122 char *modes;
123 #endif
124 {
125 return (SCM_OPN
126 | (strchr (modes, 'r') || strchr (modes, '+') ? SCM_RDNG : 0)
127 | ( strchr (modes, 'w')
128 || strchr (modes, 'a')
129 || strchr (modes, '+') ? SCM_WRTNG : 0)
130 | (strchr (modes, '0') ? SCM_BUF0 : 0));
131 }
132
133
134 /* scm_open_file
135 * Return a new port open on a given file.
136 *
137 * The mode string must match the pattern: [rwa+]** which
138 * is interpreted in the usual unix way.
139 *
140 * Return the new port.
141 */
142
143 #ifdef __STDC__
144 SCM
145 scm_mkfile (char * name, char * modes)
146 #else
147 SCM
148 scm_mkfile (name, modes)
149 char * name;
150 char * modes;
151 #endif
152 {
153 register SCM port;
154 FILE *f;
155 SCM_NEWCELL (port);
156 SCM_DEFER_INTS;
157 SCM_SYSCALL (f = fopen (name, modes));
158 if (!f)
159 {
160 SCM_ALLOW_INTS;
161 port = SCM_BOOL_F;
162 }
163 else
164 {
165 struct scm_port_table * pt;
166 pt = scm_add_to_port_table (port);
167 SCM_SETPTAB_ENTRY (port, pt);
168 if (SCM_BUF0 & (SCM_CAR (port) = scm_tc16_fport | scm_mode_bits (modes)))
169 scm_setbuf0 (port);
170 SCM_SETSTREAM (port, (SCM)f);
171 SCM_PTAB_ENTRY (port)->file_name = scm_makfrom0str (name);
172 SCM_ALLOW_INTS;
173 }
174 return port;
175 }
176
177 SCM_PROC(s_open_file, "open-file", 2, 0, 0, scm_open_file);
178 #ifdef __STDC__
179 SCM
180 scm_open_file (SCM filename, SCM modes)
181 #else
182 SCM
183 scm_open_file (filename, modes)
184 SCM filename;
185 SCM modes;
186 #endif
187 {
188 SCM port;
189 SCM_ASSERT (SCM_NIMP (filename) && SCM_ROSTRINGP (filename), filename, SCM_ARG1, s_open_file);
190 SCM_ASSERT (SCM_NIMP (modes) && SCM_ROSTRINGP (modes), modes, SCM_ARG2, s_open_file);
191 if (SCM_SUBSTRP (filename))
192 filename = scm_makfromstr (SCM_ROCHARS (filename), SCM_ROLENGTH (filename), 0);
193 if (SCM_SUBSTRP (modes))
194 modes = scm_makfromstr (SCM_ROCHARS (modes), SCM_ROLENGTH (modes), 0);
195 port = scm_mkfile (SCM_ROCHARS (filename), SCM_ROCHARS (modes));
196 /* Force the compiler to keep filename and modes alive:
197 */
198 if (port == SCM_BOOL_F)
199 scm_cons (filename, modes);
200 return port;
201 }
202
203 /* Return the mode flags from an open port.
204 * Some modes such as "append" are only used when opening
205 * a file and are not returned here.
206 */
207
208 SCM_PROC(s_port_mode, "port-mode", 1, 0, 0, scm_port_mode);
209 #ifdef __STDC__
210 SCM
211 scm_port_mode (SCM port)
212 #else
213 SCM
214 scm_port_mode (port)
215 SCM port;
216 #endif
217 {
218 char modes[3];
219 modes[0] = '\0';
220 SCM_ASSERT (SCM_NIMP (port) && SCM_OPPORTP (port), port, SCM_ARG1, s_port_mode);
221 if (SCM_CAR (port) & SCM_RDNG) {
222 if (SCM_CAR (port) & SCM_WRTNG)
223 strcpy (modes, "r+");
224 else
225 strcpy (modes, "r");
226 }
227 else if (SCM_CAR (port) & SCM_WRTNG)
228 strcpy (modes, "w");
229 if (SCM_CAR (port) & SCM_BUF0)
230 strcat (modes, "0");
231 return scm_makfromstr (modes, strlen (modes), 0);
232 }
233
234
235 #ifdef __STDC__
236 static int
237 prinfport (SCM exp, SCM port, int writing)
238 #else
239 static int
240 prinfport (exp, port, writing)
241 SCM exp;
242 SCM port;
243 int writing;
244 #endif
245 {
246 SCM name;
247 char * c;
248 if (SCM_CLOSEDP (exp))
249 {
250 c = "file";
251 }
252 else
253 {
254 name = SCM_PTAB_ENTRY (exp)->file_name;
255 if (SCM_NIMP (name) && SCM_ROSTRINGP (name))
256 c = SCM_ROCHARS (name);
257 else
258 c = "file";
259 }
260
261 scm_prinport (exp, port, c);
262 return !0;
263 }
264
265
266 #ifdef __STDC__
267 static int
268 scm_fgetc (FILE * s)
269 #else
270 static int
271 scm_fgetc (s)
272 FILE * s;
273 #endif
274 {
275 if (feof (s))
276 return EOF;
277 else
278 return fgetc (s);
279 }
280
281 #ifdef vms
282 #ifdef __STDC__
283 static scm_sizet
284 pwrite (char *ptr, scm_sizet size, nitems, FILE *port)
285 #else
286 static scm_sizet
287 pwrite (ptr, size, nitems, port)
288 char *ptr;
289 scm_sizet size, nitems;
290 FILE *port;
291 #endif
292 {
293 scm_sizet len = size * nitems;
294 scm_sizet i = 0;
295 for (; i < len; i++)
296 putc (ptr[i], port);
297 return len;
298 }
299
300 #define ffwrite pwrite
301 #else
302 #define ffwrite fwrite
303 #endif
304
305 \f
306 /* This otherwise pointless code helps some poor
307 * crippled C compilers cope with life.
308 */
309 static int
310 local_fclose (fp)
311 FILE * fp;
312 {
313 return fclose (fp);
314 }
315
316 static int
317 local_fflush (fp)
318 FILE * fp;
319 {
320 return fflush (fp);
321 }
322
323 static int
324 local_fputc (c, fp)
325 int c;
326 FILE * fp;
327 {
328 return fputc (c, fp);
329 }
330
331 static int
332 local_fputs (s, fp)
333 char * s;
334 FILE * fp;
335 {
336 return fputs (s, fp);
337 }
338
339 static scm_sizet
340 local_ffwrite (ptr, size, nitems, fp)
341 void * ptr;
342 int size;
343 int nitems;
344 FILE * fp;
345 {
346 return ffwrite (ptr, size, nitems, fp);
347 }
348
349 \f
350 scm_ptobfuns scm_fptob =
351 {
352 scm_mark0,
353 local_fclose,
354 prinfport,
355 0,
356 local_fputc,
357 local_fputs,
358 local_ffwrite,
359 local_fflush,
360 scm_fgetc,
361 local_fclose
362 };
363
364 /* {Pipe ports}
365 */
366 scm_ptobfuns scm_pipob =
367 {
368 scm_mark0,
369 0, /* replaced by pclose in scm_init_ioext() */
370 0, /* replaced by prinpipe in scm_init_ioext() */
371 0,
372 local_fputc,
373 local_fputs,
374 local_ffwrite,
375 local_fflush,
376 scm_fgetc,
377 0
378 }; /* replaced by pclose in scm_init_ioext() */
379
380
381 #ifdef __STDC__
382 void
383 scm_init_fports (void)
384 #else
385 void
386 scm_init_fports ()
387 #endif
388 {
389 #include "fports.x"
390 }
391