Commit | Line | Data |
---|---|---|
1146b6cd | 1 | /* Copyright (C) 1995, 1996, 1997 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 JB |
41 | \f |
42 | ||
43 | ||
44 | #include <stdio.h> | |
0f2d19dd | 45 | #include "_scm.h" |
1146b6cd GH |
46 | #include "genio.h" |
47 | #include "read.h" | |
20e6290e | 48 | #include "fports.h" |
1146b6cd GH |
49 | #include "unif.h" |
50 | #include "chars.h" | |
20e6290e JB |
51 | |
52 | #include "ioext.h" | |
0f2d19dd | 53 | |
95b88819 GH |
54 | #ifdef HAVE_STRING_H |
55 | #include <string.h> | |
56 | #endif | |
57 | #ifdef HAVE_UNISTD_H | |
58 | #include <unistd.h> | |
59 | #endif | |
0f2d19dd JB |
60 | \f |
61 | ||
1146b6cd GH |
62 | SCM_PROC (s_read_delimited_x, "%read-delimited!", 3, 3, 0, scm_read_delimited_x); |
63 | ||
64 | SCM | |
65 | scm_read_delimited_x (delims, buf, gobble, port, start, end) | |
66 | SCM delims; | |
67 | SCM buf; | |
68 | SCM gobble; | |
69 | SCM port; | |
70 | SCM start; | |
71 | SCM end; | |
72 | { | |
73 | long j; | |
74 | char *cbuf; | |
75 | long cstart; | |
76 | long cend; | |
77 | int c; | |
78 | char *cdelims; | |
79 | int num_delims; | |
80 | ||
ae2fa5bc | 81 | SCM_ASSERT (SCM_NIMP (delims) && SCM_ROSTRINGP (delims), |
1146b6cd | 82 | delims, SCM_ARG1, s_read_delimited_x); |
ae2fa5bc GH |
83 | cdelims = SCM_ROCHARS (delims); |
84 | num_delims = SCM_ROLENGTH (delims); | |
1146b6cd GH |
85 | SCM_ASSERT (SCM_NIMP (buf) && SCM_STRINGP (buf), |
86 | buf, SCM_ARG2, s_read_delimited_x); | |
87 | cbuf = SCM_CHARS (buf); | |
88 | cend = SCM_LENGTH (buf); | |
89 | if (SCM_UNBNDP (port)) | |
90 | port = scm_cur_inp; | |
91 | else | |
92 | { | |
93 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port), | |
94 | port, SCM_ARG1, s_read_delimited_x); | |
95 | } | |
96 | ||
97 | if (SCM_UNBNDP (start)) | |
98 | cstart = 0; | |
99 | else | |
100 | { | |
101 | cstart = scm_num2long (start, | |
102 | (char *) SCM_ARG5, s_read_delimited_x); | |
103 | if (cstart < 0 || cstart >= cend) | |
104 | scm_out_of_range (s_read_delimited_x, start); | |
105 | ||
106 | if (!SCM_UNBNDP (end)) | |
107 | { | |
108 | long tend = scm_num2long (end, (char *) SCM_ARG6, | |
109 | s_read_delimited_x); | |
110 | if (tend <= cstart || tend > cend) | |
111 | scm_out_of_range (s_read_delimited_x, end); | |
112 | cend = tend; | |
113 | } | |
114 | } | |
115 | ||
116 | for (j = cstart; j < cend; j++) | |
117 | { | |
118 | int k; | |
119 | ||
b7f3516f | 120 | c = scm_getc (port); |
1146b6cd GH |
121 | for (k = 0; k < num_delims; k++) |
122 | { | |
123 | if (cdelims[k] == c) | |
124 | { | |
125 | if (SCM_FALSEP (gobble)) | |
b7f3516f | 126 | scm_ungetc (c, port); |
1146b6cd GH |
127 | |
128 | return scm_cons (SCM_MAKICHR (c), | |
129 | scm_long2num (j - cstart)); | |
130 | } | |
131 | } | |
132 | if (c == EOF) | |
133 | return scm_cons (SCM_EOF_VAL, | |
134 | scm_long2num (j - cstart)); | |
135 | ||
136 | cbuf[j] = c; | |
137 | } | |
138 | return scm_cons (SCM_BOOL_F, scm_long2num (j - cstart)); | |
139 | } | |
140 | ||
3cb988bd TP |
141 | SCM_PROC (s_read_line, "%read-line", 0, 1, 0, scm_read_line); |
142 | ||
143 | SCM | |
144 | scm_read_line (port) | |
145 | SCM port; | |
146 | { | |
147 | char *s; | |
148 | ||
149 | if (SCM_UNBNDP (port)) | |
150 | port = scm_cur_inp; | |
151 | else | |
152 | { | |
153 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port), | |
154 | port, SCM_ARG1, s_read_line); | |
155 | } | |
156 | ||
b7f3516f | 157 | s = scm_do_read_line (port); |
3cb988bd TP |
158 | return (s == NULL ? SCM_EOF_VAL : scm_makfrom0str (s)); |
159 | } | |
160 | ||
1146b6cd GH |
161 | SCM_PROC (s_write_line, "write-line", 1, 1, 0, scm_write_line); |
162 | ||
163 | SCM | |
164 | scm_write_line (obj, port) | |
165 | SCM obj; | |
166 | SCM port; | |
167 | { | |
168 | scm_display (obj, port); | |
169 | return scm_newline (port); | |
170 | } | |
171 | ||
063e05be | 172 | SCM_PROC (s_ftell, "ftell", 1, 0, 0, scm_ftell); |
1cc91f1b | 173 | |
0f2d19dd | 174 | SCM |
6afcd3b2 GH |
175 | scm_ftell (object) |
176 | SCM object; | |
0f2d19dd JB |
177 | { |
178 | long pos; | |
6afcd3b2 GH |
179 | SCM_DEFER_INTS; |
180 | if (SCM_NIMP (object) && SCM_OPFPORTP (object)) | |
181 | { | |
182 | SCM_SYSCALL (pos = ftell ((FILE *)SCM_STREAM (object))); | |
183 | if (pos > 0 && SCM_CRDYP (object)) | |
184 | pos--; | |
185 | } | |
186 | else | |
187 | { | |
188 | SCM_ASSERT (SCM_INUMP (object), object, SCM_ARG1, s_ftell); | |
189 | SCM_SYSCALL (pos = lseek (SCM_INUM (object), 0, SEEK_CUR)); | |
190 | } | |
0f2d19dd | 191 | if (pos < 0) |
063e05be | 192 | scm_syserror (s_ftell); |
6afcd3b2 | 193 | SCM_ALLOW_INTS; |
8588fa12 | 194 | return scm_long2num (pos); |
0f2d19dd JB |
195 | } |
196 | ||
197 | ||
198 | ||
063e05be | 199 | SCM_PROC (s_fseek, "fseek", 3, 0, 0, scm_fseek); |
1cc91f1b | 200 | |
0f2d19dd | 201 | SCM |
6afcd3b2 GH |
202 | scm_fseek (object, offset, whence) |
203 | SCM object; | |
0f2d19dd JB |
204 | SCM offset; |
205 | SCM whence; | |
0f2d19dd JB |
206 | { |
207 | int rv; | |
8588fa12 GH |
208 | long loff; |
209 | ||
063e05be | 210 | loff = scm_num2long (offset, (char *)SCM_ARG2, s_fseek); |
6afcd3b2 GH |
211 | SCM_ASSERT (SCM_INUMP (whence), whence, SCM_ARG3, s_fseek); |
212 | SCM_DEFER_INTS; | |
213 | if (SCM_NIMP (object) && SCM_OPFPORTP (object)) | |
214 | { | |
215 | SCM_CLRDY (object); /* Clear ungetted char */ | |
216 | rv = fseek ((FILE *)SCM_STREAM (object), loff, SCM_INUM (whence)); | |
217 | } | |
218 | else | |
219 | { | |
220 | SCM_ASSERT (SCM_INUMP (object), object, SCM_ARG1, s_fseek); | |
221 | rv = lseek (SCM_INUM (object), loff, SCM_INUM (whence)); | |
222 | } | |
223 | if (rv < 0) | |
063e05be | 224 | scm_syserror (s_fseek); |
6afcd3b2 | 225 | SCM_ALLOW_INTS; |
02b754d3 | 226 | return SCM_UNSPECIFIED; |
0f2d19dd JB |
227 | } |
228 | ||
063e05be | 229 | SCM_PROC (s_freopen, "freopen", 3, 0, 0, scm_freopen); |
1cc91f1b | 230 | |
0f2d19dd | 231 | SCM |
063e05be | 232 | scm_freopen (filename, modes, port) |
0f2d19dd JB |
233 | SCM filename; |
234 | SCM modes; | |
235 | SCM port; | |
0f2d19dd JB |
236 | { |
237 | FILE *f; | |
ae2fa5bc GH |
238 | SCM_ASSERT (SCM_NIMP (filename) && SCM_ROSTRINGP (filename), filename, |
239 | SCM_ARG1, s_freopen); | |
240 | SCM_ASSERT (SCM_NIMP (modes) && SCM_ROSTRINGP (modes), modes, SCM_ARG2, | |
241 | s_freopen); | |
89958ad0 JB |
242 | |
243 | SCM_COERCE_SUBSTR (filename); | |
244 | SCM_COERCE_SUBSTR (modes); | |
0f2d19dd | 245 | SCM_DEFER_INTS; |
063e05be | 246 | SCM_ASSERT (SCM_NIMP (port) && SCM_FPORTP (port), port, SCM_ARG3, s_freopen); |
ae2fa5bc GH |
247 | SCM_SYSCALL (f = freopen (SCM_ROCHARS (filename), SCM_ROCHARS (modes), |
248 | (FILE *)SCM_STREAM (port))); | |
0f2d19dd JB |
249 | if (!f) |
250 | { | |
251 | SCM p; | |
252 | p = port; | |
253 | port = SCM_MAKINUM (errno); | |
898a256f | 254 | SCM_SETAND_CAR (p, ~SCM_OPN); |
0f2d19dd JB |
255 | scm_remove_from_port_table (p); |
256 | } | |
257 | else | |
258 | { | |
ae2fa5bc | 259 | SCM_SETCAR (port, scm_tc16_fport | scm_mode_bits (SCM_ROCHARS (modes))); |
0f2d19dd | 260 | SCM_SETSTREAM (port, (SCM)f); |
ae2fa5bc | 261 | SCM_SETCAR (port, scm_tc16_fport | scm_mode_bits (SCM_ROCHARS (modes))); |
898a256f | 262 | if (SCM_BUF0 & SCM_CAR (port)) |
0f2d19dd JB |
263 | scm_setbuf0 (port); |
264 | } | |
265 | SCM_ALLOW_INTS; | |
266 | return port; | |
267 | } | |
268 | ||
063e05be | 269 | SCM_PROC (s_redirect_port, "redirect-port", 2, 0, 0, scm_redirect_port); |
1cc91f1b | 270 | |
0f2d19dd | 271 | SCM |
9c29ac66 GH |
272 | scm_redirect_port (old, new) |
273 | SCM old; | |
274 | SCM new; | |
0f2d19dd JB |
275 | { |
276 | int ans, oldfd, newfd; | |
9c29ac66 | 277 | |
0f2d19dd | 278 | SCM_DEFER_INTS; |
9c29ac66 GH |
279 | SCM_ASSERT (SCM_NIMP (old) && SCM_OPPORTP (old), old, SCM_ARG1, s_redirect_port); |
280 | SCM_ASSERT (SCM_NIMP (new) && SCM_OPPORTP (new), new, SCM_ARG2, s_redirect_port); | |
281 | oldfd = fileno ((FILE *)SCM_STREAM (old)); | |
02b754d3 | 282 | if (oldfd == -1) |
063e05be | 283 | scm_syserror (s_redirect_port); |
9c29ac66 | 284 | newfd = fileno ((FILE *)SCM_STREAM (new)); |
02b754d3 | 285 | if (newfd == -1) |
063e05be | 286 | scm_syserror (s_redirect_port); |
02b754d3 GH |
287 | SCM_SYSCALL (ans = dup2 (oldfd, newfd)); |
288 | if (ans == -1) | |
063e05be | 289 | scm_syserror (s_redirect_port); |
0f2d19dd | 290 | SCM_ALLOW_INTS; |
02b754d3 | 291 | return SCM_UNSPECIFIED; |
0f2d19dd JB |
292 | } |
293 | ||
7a6f1ffa | 294 | SCM_PROC (s_dup_to_fdes, "dup->fdes", 1, 1, 0, scm_dup_to_fdes); |
a9488d12 | 295 | SCM |
7a6f1ffa | 296 | scm_dup_to_fdes (SCM fd_or_port, SCM fd) |
a9488d12 GH |
297 | { |
298 | int oldfd, newfd, rv; | |
299 | ||
300 | SCM_DEFER_INTS; | |
301 | if (SCM_INUMP (fd_or_port)) | |
302 | oldfd = SCM_INUM (fd_or_port); | |
303 | else | |
304 | { | |
305 | SCM_ASSERT (SCM_NIMP (fd_or_port) && SCM_OPPORTP (fd_or_port), | |
7a6f1ffa | 306 | fd_or_port, SCM_ARG1, s_dup_to_fdes); |
a9488d12 GH |
307 | oldfd = fileno ((FILE *)SCM_STREAM (fd_or_port)); |
308 | if (oldfd == -1) | |
7a6f1ffa | 309 | scm_syserror (s_dup_to_fdes); |
a9488d12 | 310 | } |
7a6f1ffa GH |
311 | |
312 | if (SCM_UNBNDP (fd)) | |
e38303a2 | 313 | { |
7a6f1ffa GH |
314 | SCM_SYSCALL (newfd = dup (oldfd)); |
315 | if (newfd == -1) | |
468b2cf3 | 316 | scm_syserror (s_dup_to_fdes); |
7a6f1ffa GH |
317 | fd = SCM_MAKINUM (newfd); |
318 | } | |
319 | else | |
320 | { | |
321 | SCM_ASSERT (SCM_INUMP (fd), fd, SCM_ARG2, s_dup_to_fdes); | |
322 | newfd = SCM_INUM (fd); | |
323 | if (oldfd != newfd) | |
324 | { | |
325 | scm_evict_ports (newfd); /* see scsh manual. */ | |
326 | SCM_SYSCALL (rv = dup2 (oldfd, newfd)); | |
327 | if (rv == -1) | |
328 | scm_syserror (s_dup_to_fdes); | |
329 | } | |
e38303a2 | 330 | } |
a9488d12 | 331 | SCM_ALLOW_INTS; |
e38303a2 | 332 | return fd; |
a9488d12 GH |
333 | } |
334 | ||
063e05be | 335 | SCM_PROC (s_fileno, "fileno", 1, 0, 0, scm_fileno); |
1cc91f1b | 336 | |
0f2d19dd | 337 | SCM |
063e05be | 338 | scm_fileno (port) |
0f2d19dd | 339 | SCM port; |
0f2d19dd JB |
340 | { |
341 | int fd; | |
063e05be | 342 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPFPORTP (port), port, SCM_ARG1, s_fileno); |
0f2d19dd | 343 | fd = fileno ((FILE *)SCM_STREAM (port)); |
02b754d3 | 344 | if (fd == -1) |
063e05be | 345 | scm_syserror (s_fileno); |
02b754d3 | 346 | return SCM_MAKINUM (fd); |
0f2d19dd JB |
347 | } |
348 | ||
063e05be | 349 | SCM_PROC (s_isatty, "isatty?", 1, 0, 0, scm_isatty_p); |
1cc91f1b | 350 | |
0f2d19dd | 351 | SCM |
063e05be | 352 | scm_isatty_p (port) |
0f2d19dd | 353 | SCM port; |
0f2d19dd JB |
354 | { |
355 | int rv; | |
063e05be | 356 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPFPORTP (port), port, SCM_ARG1, s_isatty); |
0f2d19dd JB |
357 | rv = fileno ((FILE *)SCM_STREAM (port)); |
358 | if (rv == -1) | |
063e05be | 359 | scm_syserror (s_isatty); |
02b754d3 GH |
360 | rv = isatty (rv); |
361 | return rv ? SCM_BOOL_T : SCM_BOOL_F; | |
0f2d19dd JB |
362 | } |
363 | ||
364 | ||
365 | ||
063e05be | 366 | SCM_PROC (s_fdopen, "fdopen", 2, 0, 0, scm_fdopen); |
1cc91f1b | 367 | |
0f2d19dd | 368 | SCM |
063e05be | 369 | scm_fdopen (fdes, modes) |
0f2d19dd JB |
370 | SCM fdes; |
371 | SCM modes; | |
0f2d19dd JB |
372 | { |
373 | FILE *f; | |
374 | SCM port; | |
8b13c6b3 | 375 | struct scm_port_table * pt; |
0f2d19dd | 376 | |
063e05be | 377 | SCM_ASSERT (SCM_INUMP (fdes), fdes, SCM_ARG1, s_fdopen); |
ae2fa5bc GH |
378 | SCM_ASSERT (SCM_NIMP (modes) && SCM_ROSTRINGP (modes), modes, SCM_ARG2, |
379 | s_fdopen); | |
89958ad0 | 380 | SCM_COERCE_SUBSTR (modes); |
8b13c6b3 | 381 | SCM_NEWCELL (port); |
0f2d19dd | 382 | SCM_DEFER_INTS; |
ae2fa5bc | 383 | f = fdopen (SCM_INUM (fdes), SCM_ROCHARS (modes)); |
0f2d19dd | 384 | if (f == NULL) |
063e05be | 385 | scm_syserror (s_fdopen); |
8b13c6b3 GH |
386 | pt = scm_add_to_port_table (port); |
387 | SCM_SETPTAB_ENTRY (port, pt); | |
ae2fa5bc | 388 | SCM_SETCAR (port, scm_tc16_fport | scm_mode_bits (SCM_ROCHARS (modes))); |
e38303a2 | 389 | SCM_SETSTREAM (port, (SCM)f); |
898a256f | 390 | if (SCM_BUF0 & SCM_CAR (port)) |
8b13c6b3 | 391 | scm_setbuf0 (port); |
0f2d19dd JB |
392 | SCM_ALLOW_INTS; |
393 | return port; | |
394 | } | |
395 | ||
396 | ||
397 | ||
398 | /* Move a port's underlying file descriptor to a given value. | |
8b13c6b3 GH |
399 | * Returns #f if fdes is already the given value. |
400 | * #t if fdes moved. | |
0f2d19dd JB |
401 | * MOVE->FDES is implemented in Scheme and calls this primitive. |
402 | */ | |
063e05be | 403 | SCM_PROC (s_primitive_move_to_fdes, "primitive-move->fdes", 2, 0, 0, scm_primitive_move_to_fdes); |
1cc91f1b | 404 | |
0f2d19dd | 405 | SCM |
063e05be | 406 | scm_primitive_move_to_fdes (port, fd) |
0f2d19dd JB |
407 | SCM port; |
408 | SCM fd; | |
0f2d19dd JB |
409 | { |
410 | FILE *stream; | |
411 | int old_fd; | |
412 | int new_fd; | |
413 | int rv; | |
414 | ||
063e05be GH |
415 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPFPORTP (port), port, SCM_ARG1, s_primitive_move_to_fdes); |
416 | SCM_ASSERT (SCM_INUMP (fd), fd, SCM_ARG2, s_primitive_move_to_fdes); | |
0f2d19dd JB |
417 | SCM_DEFER_INTS; |
418 | stream = (FILE *)SCM_STREAM (port); | |
419 | old_fd = fileno (stream); | |
420 | new_fd = SCM_INUM (fd); | |
421 | if (old_fd == new_fd) | |
422 | { | |
423 | SCM_ALLOW_INTS; | |
8b13c6b3 | 424 | return SCM_BOOL_F; |
0f2d19dd JB |
425 | } |
426 | scm_evict_ports (new_fd); | |
427 | rv = dup2 (old_fd, new_fd); | |
428 | if (rv == -1) | |
063e05be | 429 | scm_syserror (s_primitive_move_to_fdes); |
0f2d19dd JB |
430 | scm_setfileno (stream, new_fd); |
431 | SCM_SYSCALL (close (old_fd)); | |
432 | SCM_ALLOW_INTS; | |
8b13c6b3 | 433 | return SCM_BOOL_T; |
0f2d19dd JB |
434 | } |
435 | ||
0f2d19dd JB |
436 | /* Return a list of ports using a given file descriptor. */ |
437 | SCM_PROC(s_fdes_to_ports, "fdes->ports", 1, 0, 0, scm_fdes_to_ports); | |
1cc91f1b | 438 | |
0f2d19dd JB |
439 | SCM |
440 | scm_fdes_to_ports (fd) | |
441 | SCM fd; | |
0f2d19dd JB |
442 | { |
443 | SCM result = SCM_EOL; | |
444 | int int_fd; | |
445 | int i; | |
446 | ||
447 | SCM_ASSERT (SCM_INUMP (fd), fd, SCM_ARG1, s_fdes_to_ports); | |
448 | int_fd = SCM_INUM (fd); | |
449 | ||
450 | SCM_DEFER_INTS; | |
451 | for (i = 0; i < scm_port_table_size; i++) | |
452 | { | |
453 | if (SCM_FPORTP (scm_port_table[i]->port) | |
454 | && fileno ((FILE *)SCM_STREAM (scm_port_table[i]->port)) == int_fd) | |
455 | result = scm_cons (scm_port_table[i]->port, result); | |
456 | } | |
457 | SCM_ALLOW_INTS; | |
458 | return result; | |
459 | } | |
460 | ||
1cc91f1b | 461 | |
0f2d19dd JB |
462 | void |
463 | scm_init_ioext () | |
0f2d19dd JB |
464 | { |
465 | /* fseek() symbols. */ | |
466 | scm_sysintern ("SEEK_SET", SCM_MAKINUM (SEEK_SET)); | |
467 | scm_sysintern ("SEEK_CUR", SCM_MAKINUM (SEEK_CUR)); | |
468 | scm_sysintern ("SEEK_END", SCM_MAKINUM (SEEK_END)); | |
469 | ||
0f2d19dd JB |
470 | #include "ioext.x" |
471 | } | |
472 |