* __scm.h, alist.c, async.c, async.h, backtrace.h, chars.c,
[bpt/guile.git] / libguile / genio.c
CommitLineData
7dc6e754 1/* Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
0f2d19dd
JB
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
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
0f2d19dd
JB
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
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.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
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.
37 *
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.
82892bed 40 * If you do not wish that, delete this exception notice. */
0f2d19dd 41\f
0f2d19dd
JB
42#include <stdio.h>
43#include "_scm.h"
20e6290e 44#include "chars.h"
44e8413c
MD
45#ifdef GUILE_ISELECT
46#include "filesys.h"
47#endif
20e6290e
JB
48
49#include "genio.h"
0f2d19dd 50
95b88819
GH
51#ifdef HAVE_STRING_H
52#include <string.h>
53#endif
0f2d19dd
JB
54\f
55
b7f3516f 56void
0f2d19dd
JB
57scm_putc (c, port)
58 int c;
59 SCM port;
0f2d19dd
JB
60{
61 scm_sizet i = SCM_PTOBNUM (port);
0f88a8f3 62 SCM_SYSCALL ((scm_ptobs[i].fputc) (c, port));
0f2d19dd
JB
63}
64
b7f3516f 65void
0f2d19dd
JB
66scm_puts (s, port)
67 char *s;
68 SCM port;
0f2d19dd
JB
69{
70 scm_sizet i = SCM_PTOBNUM (port);
0f88a8f3 71 SCM_SYSCALL ((scm_ptobs[i].fputs) (s, port));
0f2d19dd
JB
72#ifdef TRANSCRIPT_SUPPORT
73 if (scm_trans && (port == def_outp || port == cur_errp))
74 SCM_SYSCALL (fputs (s, scm_trans));
75#endif
76}
77
b7f3516f
TT
78void
79scm_lfwrite (ptr, size, port)
0f2d19dd
JB
80 char *ptr;
81 scm_sizet size;
0f2d19dd 82 SCM port;
0f2d19dd 83{
0f2d19dd 84 scm_sizet i = SCM_PTOBNUM (port);
0f88a8f3 85 SCM_SYSCALL (scm_ptobs[i].fwrite (ptr, size, 1, port));
0f2d19dd
JB
86#ifdef TRANSCRIPT_SUPPORT
87 if (scm_trans && (port == def_outp || port == cur_errp))
b7f3516f 88 SCM_SYSCALL (fwrite (ptr, size, 1, scm_trans));
0f2d19dd 89#endif
0f2d19dd
JB
90}
91
92
d68fee48
JB
93void
94scm_fflush (port)
95 SCM port;
96{
97 scm_sizet i = SCM_PTOBNUM (port);
0f88a8f3 98 (scm_ptobs[i].fflush) (port);
d68fee48
JB
99}
100
0f2d19dd
JB
101\f
102
b7f3516f 103int
0f2d19dd
JB
104scm_getc (port)
105 SCM port;
0f2d19dd 106{
1717856b 107 SCM f;
0f2d19dd
JB
108 int c;
109 scm_sizet i;
110
0f2d19dd
JB
111 /* One char may be stored in the high bits of (car port) orre@nada.kth.se. */
112 if (SCM_CRDYP (port))
113 {
114 c = SCM_CGETUN (port);
b7f3516f 115 SCM_CLRDY (port); /* Clear ungetted char */
0f2d19dd 116 }
b7f3516f 117 else
0f2d19dd 118 {
b7f3516f
TT
119 f = SCM_STREAM (port);
120 i = SCM_PTOBNUM (port);
44e8413c
MD
121#ifdef GUILE_ISELECT
122 if (SCM_FPORTP (port) && !scm_input_waiting_p ((FILE *) f, "scm_getc"))
123 {
124 int n;
125 SELECT_TYPE readfds;
126 int fd = fileno ((FILE *) f);
44e8413c
MD
127 do
128 {
a3ec616e 129 FD_ZERO (&readfds);
44e8413c
MD
130 FD_SET (fd, &readfds);
131 n = scm_internal_select (fd + 1, &readfds, NULL, NULL, NULL);
132 }
133 while (n == -1 && errno == EINTR);
134 }
135#endif
0f88a8f3 136 SCM_SYSCALL (c = (scm_ptobs[i].fgetc) (port));
b7f3516f 137 }
0f2d19dd 138
b7f3516f
TT
139 if (c == '\n')
140 {
141 SCM_INCLINE (port);
142 }
143 else if (c == '\t')
144 {
145 SCM_TABCOL (port);
146 }
147 else
148 {
149 SCM_INCCOL (port);
0f2d19dd 150 }
b7f3516f
TT
151
152 return c;
0f2d19dd
JB
153}
154
1717856b 155
0f2d19dd 156void
b7f3516f 157scm_ungetc (c, port)
0f2d19dd
JB
158 int c;
159 SCM port;
0f2d19dd 160{
b7f3516f 161/* SCM_ASSERT(!SCM_CRDYP(port), port, SCM_ARG2, "too many scm_ungetc");*/
0f2d19dd
JB
162 SCM_CUNGET (c, port);
163 if (c == '\n')
164 {
165 /* What should col be in this case?
166 * We'll leave it at -1.
167 */
168 SCM_LINUM (port) -= 1;
169 }
170 else
171 SCM_COL(port) -= 1;
172}
173
174
3cb988bd 175char *
848f2a01 176scm_do_read_line (port, len)
3cb988bd 177 SCM port;
848f2a01 178 int *len;
3cb988bd
TP
179{
180 char *s;
181 scm_sizet i;
182
183 i = SCM_PTOBNUM (port);
848f2a01 184 SCM_SYSCALL (s = (scm_ptobs[i].fgets) (port, len));
5ead5a91 185
4eebd8fc
JB
186 /* We should never get an empty string. Every line has a newline at
187 the end, except for the last one. If the last line has no
188 newline and is empty, then that's just an ordinary EOF, and we
189 should have s == NULL. But this seems obscure to me, so we check
190 this here, to protect ourselves from odd port implementations. */
191 if (s && *len <= 0)
192 abort ();
193
5ead5a91
JB
194 /* If we're not at EOF, and there was a newline at the end of the
195 string, increment the line counter. */
4eebd8fc 196 if (s && s[*len - 1] == '\n')
af77c5fd
JB
197 SCM_INCLINE(port);
198
3cb988bd
TP
199 return s;
200}