1 /* Copyright (C) 1995,1996,1997 Free Software Foundation, Inc.
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)
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.
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, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
58 /* {Ports - file ports}
62 /* should be called with SCM_DEFER_INTS active */
68 /* NOSETBUF was provided by scm to allow unbuffered ports to be
69 avoided on systems where ungetc didn't work correctly. See
70 comment in unif.c, which seems to be the only place where it
71 could still be a problem. */
73 /* SCM_SYSCALL (setbuf ((FILE *)SCM_STREAM (port), 0);); */
74 SCM_SYSCALL (setvbuf ((FILE *)SCM_STREAM (port
), 0, _IONBF
, 0););
76 return SCM_UNSPECIFIED
;
79 SCM_PROC (s_setvbuf
, "setvbuf", 2, 1, 0, scm_setvbuf
);
81 scm_setvbuf (SCM port
, SCM mode
, SCM size
)
86 SCM_ASSERT (SCM_NIMP (port
) && SCM_FPORTP (port
), port
, SCM_ARG1
, s_setvbuf
);
87 SCM_ASSERT (SCM_INUMP (mode
), mode
, SCM_ARG2
, s_setvbuf
);
88 if (SCM_UNBNDP (size
))
92 SCM_ASSERT (SCM_INUMP (size
), size
, SCM_ARG3
, s_setvbuf
);
93 csize
= SCM_INUM (size
);
95 cmode
= SCM_INUM (mode
);
96 if (csize
== 0 && cmode
== _IOFBF
)
99 SCM_SYSCALL (rv
= setvbuf ((FILE *)SCM_STREAM (port
), 0, cmode
, csize
));
101 scm_syserror (s_setvbuf
);
103 SCM_SETCAR (port
, SCM_CAR (port
) | SCM_BUF0
);
105 SCM_SETCAR (port
, (SCM_CAR (port
) & ~SCM_BUF0
));
107 return SCM_UNSPECIFIED
;
111 #define SET_FILE_FD_FIELD(F,D) ((F)->FD_SETTER = (D))
115 scm_setfileno (fs
, fd
)
119 #ifdef SET_FILE_FD_FIELD
120 SET_FILE_FD_FIELD(fs
, fd
);
122 scm_misc_error ("scm_setfileno", "Not fully implemented on this platform",
127 /* Move ports with the specified file descriptor to new descriptors,
128 * reseting the revealed count to 0.
129 * Should be called with SCM_DEFER_INTS active.
138 for (i
= 0; i
< scm_port_table_size
; i
++)
140 if (SCM_FPORTP (scm_port_table
[i
]->port
)
141 && fileno ((FILE *)SCM_STREAM (scm_port_table
[i
]->port
)) == fd
)
143 scm_setfileno ((FILE *)SCM_STREAM (scm_port_table
[i
]->port
), dup (fd
));
144 scm_set_port_revealed_x (scm_port_table
[i
]->port
, SCM_MAKINUM (0));
150 * Return a new port open on a given file.
152 * The mode string must match the pattern: [rwa+]** which
153 * is interpreted in the usual unix way.
155 * Return the new port.
157 SCM_PROC(s_open_file
, "open-file", 2, 0, 0, scm_open_file
);
160 scm_open_file (filename
, modes
)
169 SCM_ASSERT (SCM_NIMP (filename
) && SCM_ROSTRINGP (filename
), filename
, SCM_ARG1
, s_open_file
);
170 SCM_ASSERT (SCM_NIMP (modes
) && SCM_ROSTRINGP (modes
), modes
, SCM_ARG2
, s_open_file
);
171 if (SCM_SUBSTRP (filename
))
172 filename
= scm_makfromstr (SCM_ROCHARS (filename
), SCM_ROLENGTH (filename
), 0);
173 if (SCM_SUBSTRP (modes
))
174 modes
= scm_makfromstr (SCM_ROCHARS (modes
), SCM_ROLENGTH (modes
), 0);
176 file
= SCM_ROCHARS (filename
);
177 mode
= SCM_ROCHARS (modes
);
181 SCM_SYSCALL (f
= fopen (file
, mode
));
186 scm_syserror_msg (s_open_file
, "%s: %S",
187 scm_listify (scm_makfrom0str (strerror (errno
)),
194 struct scm_port_table
* pt
;
196 pt
= scm_add_to_port_table (port
);
197 SCM_SETPTAB_ENTRY (port
, pt
);
198 SCM_SETCAR (port
, scm_tc16_fport
| scm_mode_bits (mode
));
199 SCM_SETSTREAM (port
, (SCM
) f
);
200 if (SCM_BUF0
& SCM_CAR (port
))
202 SCM_PTAB_ENTRY (port
)->file_name
= filename
;
209 /* Build a Scheme port from an open stdio port, FILE.
210 MODE indicates whether FILE is open for reading or writing; it uses
211 the same notation as open-file's second argument.
212 If NAME is non-zero, use it as the port's filename.
214 scm_stdio_to_port sets the revealed count for FILE's file
215 descriptor to 1, so that FILE won't be closed when the port object
218 scm_stdio_to_port (file
, mode
, name
)
223 long mode_bits
= scm_mode_bits (mode
);
225 struct scm_port_table
* pt
;
230 pt
= scm_add_to_port_table (port
);
231 SCM_SETPTAB_ENTRY (port
, pt
);
232 SCM_SETCAR (port
, (scm_tc16_fport
| mode_bits
));
233 SCM_SETSTREAM (port
, (SCM
) file
);
234 if (SCM_BUF0
& SCM_CAR (port
))
236 SCM_PTAB_ENTRY (port
)->file_name
= scm_makfrom0str (name
);
239 scm_set_port_revealed_x (port
, SCM_MAKINUM (1));
245 static int prinfport
SCM_P ((SCM exp
, SCM port
, scm_print_state
*pstate
));
248 prinfport (exp
, port
, pstate
)
251 scm_print_state
*pstate
;
255 if (SCM_CLOSEDP (exp
))
261 name
= SCM_PTAB_ENTRY (exp
)->file_name
;
262 if (SCM_NIMP (name
) && SCM_ROSTRINGP (name
))
263 c
= SCM_ROCHARS (name
);
268 scm_prinport (exp
, port
, c
);
274 static int scm_fgetc
SCM_P ((FILE * s
));
287 * The fgets method must take a port as its argument, rather than
288 * the underlying file handle. The reason is that we also provide
289 * a generic fgets method for ports which can't use fgets(3) (e.g.
290 * string ports). This generic method calls the port's own
291 * fgetc method. In order for it to know how to get that method,
292 * we must pass the original Scheme port object.
295 static char * scm_fgets
SCM_P ((SCM port
));
304 char *p
; /* pointer to current buffer position */
305 int i
= 0; /* index into current buffer position */
306 int limit
= 80; /* current size of buffer */
308 f
= (FILE *) SCM_STREAM (port
);
312 buf
= (char *) scm_must_malloc (limit
* sizeof(char), "fgets");
316 if (fgets (p
, limit
- i
, f
) == NULL
) {
323 if (strlen(p
) < limit
- i
- 1)
326 buf
= (char *) scm_must_realloc (buf
,
327 sizeof(char) * limit
,
328 sizeof(char) * limit
* 2,
338 static scm_sizet pwrite
SCM_P ((char *ptr
, scm_sizet size
, nitems
, FILE *port
));
341 pwrite (ptr
, size
, nitems
, port
)
343 scm_sizet size
, nitems
;
346 scm_sizet len
= size
* nitems
;
353 #define ffwrite pwrite
355 #define ffwrite fwrite
359 /* This otherwise pointless code helps some poor
360 * crippled C compilers cope with life.
363 static int local_fclose
SCM_P ((FILE *fp
));
372 static int local_fflush
SCM_P ((FILE *fp
));
381 static int local_fputc
SCM_P ((int c
, FILE *fp
));
388 return fputc (c
, fp
);
391 static int local_fputs
SCM_P ((char *s
, FILE *fp
));
398 return fputs (s
, fp
);
401 static scm_sizet local_ffwrite
SCM_P ((void *ptr
, int size
, int nitems
, FILE *fp
));
404 local_ffwrite (ptr
, size
, nitems
, fp
)
410 return ffwrite (ptr
, size
, nitems
, fp
);
414 print_pipe_port (SCM exp
, SCM port
, scm_print_state
*pstate
)
416 scm_prinport (exp
, port
, "pipe");
422 /* On SunOS, there's no declaration for pclose in the headers, so
423 putting it directly in the initializer for scm_pipob doesn't really
424 fly. We could add an extern declaration for it, but then it'll
425 mismatch on some systems that do have a declaration. So we just
426 wrap it up this way. */
435 scm_ptobfuns scm_fptob
=
438 (int (*) SCM_P ((SCM
))) local_fclose
,
441 (int (*) SCM_P ((int, SCM
))) local_fputc
,
442 (int (*) SCM_P ((char *, SCM
))) local_fputs
,
443 (scm_sizet (*) SCM_P ((char *, scm_sizet
, scm_sizet
, SCM
))) local_ffwrite
,
444 (int (*) SCM_P ((SCM
))) local_fflush
,
445 (int (*) SCM_P ((SCM
))) scm_fgetc
,
446 (char * (*) SCM_P ((SCM
))) scm_fgets
,
447 (int (*) SCM_P ((SCM
))) local_fclose
451 scm_ptobfuns scm_pipob
=
454 (int (*) SCM_P ((SCM
))) local_pclose
,
457 (int (*) SCM_P ((int, SCM
))) local_fputc
,
458 (int (*) SCM_P ((char *, SCM
))) local_fputs
,
459 (scm_sizet (*) SCM_P ((char *, scm_sizet
, scm_sizet
, SCM
))) local_ffwrite
,
460 (int (*) SCM_P ((SCM
))) local_fflush
,
461 (int (*) SCM_P ((SCM
))) scm_fgetc
,
462 (char * (*) SCM_P ((SCM
))) scm_fgets
,
463 (int (*) SCM_P ((SCM
))) local_pclose
470 scm_sysintern ("_IOFBF", SCM_MAKINUM (_IOFBF
));
471 scm_sysintern ("_IOLBF", SCM_MAKINUM (_IOLBF
));
472 scm_sysintern ("_IONBF", SCM_MAKINUM (_IONBF
));