portability fixes for header inclusion etc.
[bpt/guile.git] / libguile / vports.c
CommitLineData
0f2d19dd
JB
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
95b88819
GH
46#ifdef HAVE_STRING_H
47#include <string.h>
48#endif
49
0f2d19dd
JB
50\f
51
52/* {Ports - soft ports}
53 *
54 */
55
56
57#ifdef __STDC__
58static int
59prinsfpt (SCM exp, SCM port, int writing)
60#else
61static int
62prinsfpt (exp, port, writing)
63 SCM exp;
64 SCM port;
65 int writing;
66#endif
67{
68 scm_prinport (exp, port, "soft");
69 return !0;
70}
71
72/* sfputc sfwrite sfputs sfclose
73 * are called within a SCM_SYSCALL.
74 *
75 * So we need to set errno to 0 before returning. sfflush
76 * may be called within a SCM_SYSCALL. So we need to set errno to 0
77 * before returning.
78 */
79
80#ifdef __STDC__
81static int
82sfputc (int c, SCM p)
83#else
84static int
85sfputc (c, p)
86 int c;
87 SCM p;
88#endif
89{
90 scm_apply (SCM_VELTS (p)[0], SCM_MAKICHR (c), scm_listofnull);
91 errno = 0;
92 return c;
93}
94
95#ifdef __STDC__
96static scm_sizet
97sfwrite (char *str, scm_sizet siz, scm_sizet num, SCM p)
98#else
99static scm_sizet
100sfwrite (str, siz, num, p)
101 char *str;
102 scm_sizet siz;
103 scm_sizet num;
104 SCM p;
105#endif
106{
107 SCM sstr;
108 sstr = scm_makfromstr (str, siz * num, 0);
109 scm_apply (SCM_VELTS (p)[1], sstr, scm_listofnull);
110 errno = 0;
111 return num;
112}
113
114#ifdef __STDC__
115static int
116sfputs (char *s, SCM p)
117#else
118static int
119sfputs (s, p)
120 char *s;
121 SCM p;
122#endif
123{
124 sfwrite (s, 1, strlen (s), p);
125 return 0;
126}
127
128#ifdef __STDC__
129static int
130sfflush (SCM stream)
131#else
132static int
133sfflush (stream)
134 SCM stream;
135#endif
136{
137 SCM f = SCM_VELTS (stream)[2];
138 if (SCM_BOOL_F == f)
139 return 0;
140 f = scm_apply (f, SCM_EOL, SCM_EOL);
141 errno = 0;
142 return SCM_BOOL_F == f ? EOF : 0;
143}
144
145#ifdef __STDC__
146static int
147sfgetc (SCM p)
148#else
149static int
150sfgetc (p)
151 SCM p;
152#endif
153{
154 SCM ans;
155 ans = scm_apply (SCM_VELTS (p)[3], SCM_EOL, SCM_EOL);
156 errno = 0;
157 if (SCM_FALSEP (ans) || SCM_EOF_VAL == ans)
158 return EOF;
159 SCM_ASSERT (SCM_ICHRP (ans), ans, SCM_ARG1, "getc");
160 return SCM_ICHR (ans);
161}
162
163#ifdef __STDC__
164static int
165sfclose (SCM p)
166#else
167static int
168sfclose (p)
169 SCM p;
170#endif
171{
172 SCM f = SCM_VELTS (p)[4];
173 if (SCM_BOOL_F == f)
174 return 0;
175 f = scm_apply (f, SCM_EOL, SCM_EOL);
176 errno = 0;
177 return SCM_BOOL_F == f ? EOF : 0;
178}
179
180
181
182SCM_PROC(s_make_soft_port, "make-soft-port", 2, 0, 0, scm_make_soft_port);
183#ifdef __STDC__
184SCM
185scm_make_soft_port (SCM pv, SCM modes)
186#else
187SCM
188scm_make_soft_port (pv, modes)
189 SCM pv;
190 SCM modes;
191#endif
192{
193 struct scm_port_table * pt;
194 SCM z;
195 SCM_ASSERT (SCM_NIMP (pv) && SCM_VECTORP (pv) && 5 == SCM_LENGTH (pv), pv, SCM_ARG1, s_make_soft_port);
196 SCM_ASSERT (SCM_NIMP (modes) && SCM_STRINGP (modes), modes, SCM_ARG2, s_make_soft_port);
197 SCM_NEWCELL (z);
198 SCM_DEFER_INTS;
199 pt = scm_add_to_port_table (z);
200 SCM_CAR (z) = scm_tc16_sfport | scm_mode_bits (SCM_CHARS (modes));
201 SCM_SETPTAB_ENTRY (z, pt);
202 SCM_SETSTREAM (z, pv);
203 SCM_ALLOW_INTS;
204 return z;
205}
206
207#ifdef __STDC__
208static int
209noop0 (FILE *stream)
210#else
211static int
212noop0 (stream)
213 FILE *stream;
214#endif
215{
216 return 0;
217}
218
219
220scm_ptobfuns scm_sfptob =
221{
222 scm_markstream,
223 noop0,
224 prinsfpt,
225 0,
226 sfputc,
227 sfputs,
228 sfwrite,
229 sfflush,
230 sfgetc,
231 sfclose
232};
233
234
235#ifdef __STDC__
236void
237scm_init_vports (void)
238#else
239void
240scm_init_vports ()
241#endif
242{
243#include "vports.x"
244}
245