Commit | Line | Data |
---|---|---|
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 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 | ||
3e2043c4 | 141 | /* |
848f2a01 TP |
142 | * %read-line uses a port's fgets method for fast line i/o. It |
143 | * truncates any terminating newline from its input, and returns | |
144 | * a cons of the string read and its terminating character. Doing | |
145 | * so makes it easy to implement the hairy `read-line' options | |
146 | * efficiently in Scheme. | |
3e2043c4 TP |
147 | */ |
148 | ||
3cb988bd TP |
149 | SCM_PROC (s_read_line, "%read-line", 0, 1, 0, scm_read_line); |
150 | ||
151 | SCM | |
152 | scm_read_line (port) | |
153 | SCM port; | |
154 | { | |
155 | char *s; | |
848f2a01 TP |
156 | int slen; |
157 | SCM line, term; | |
3cb988bd TP |
158 | |
159 | if (SCM_UNBNDP (port)) | |
160 | port = scm_cur_inp; | |
161 | else | |
162 | { | |
163 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port), | |
164 | port, SCM_ARG1, s_read_line); | |
165 | } | |
166 | ||
848f2a01 TP |
167 | s = scm_do_read_line (port, &slen); |
168 | ||
3e2043c4 | 169 | if (s == NULL) |
848f2a01 | 170 | term = line = SCM_EOF_VAL; |
3e2043c4 TP |
171 | else |
172 | { | |
848f2a01 TP |
173 | if (s[slen-1] == '\n') |
174 | { | |
175 | term = SCM_MAKICHR ('\n'); | |
176 | line = scm_makfromstr (s, slen-1, 0); | |
177 | } | |
178 | else | |
179 | { | |
180 | /* Fix: we should check for eof on the port before assuming this. */ | |
181 | term = SCM_EOF_VAL; | |
182 | line = scm_makfromstr (s, slen, 0); | |
183 | } | |
3e2043c4 TP |
184 | free (s); |
185 | } | |
848f2a01 TP |
186 | |
187 | return scm_cons (line, term); | |
3cb988bd TP |
188 | } |
189 | ||
1146b6cd GH |
190 | SCM_PROC (s_write_line, "write-line", 1, 1, 0, scm_write_line); |
191 | ||
192 | SCM | |
193 | scm_write_line (obj, port) | |
194 | SCM obj; | |
195 | SCM port; | |
196 | { | |
197 | scm_display (obj, port); | |
198 | return scm_newline (port); | |
199 | } | |
200 | ||
063e05be | 201 | SCM_PROC (s_ftell, "ftell", 1, 0, 0, scm_ftell); |
1cc91f1b | 202 | |
0f2d19dd | 203 | SCM |
6afcd3b2 GH |
204 | scm_ftell (object) |
205 | SCM object; | |
0f2d19dd JB |
206 | { |
207 | long pos; | |
78446828 MV |
208 | |
209 | object = SCM_COERCE_OUTPORT (object); | |
210 | ||
6afcd3b2 GH |
211 | SCM_DEFER_INTS; |
212 | if (SCM_NIMP (object) && SCM_OPFPORTP (object)) | |
213 | { | |
214 | SCM_SYSCALL (pos = ftell ((FILE *)SCM_STREAM (object))); | |
215 | if (pos > 0 && SCM_CRDYP (object)) | |
e6a207b3 | 216 | pos -= SCM_N_READY_CHARS (object); |
6afcd3b2 GH |
217 | } |
218 | else | |
219 | { | |
220 | SCM_ASSERT (SCM_INUMP (object), object, SCM_ARG1, s_ftell); | |
221 | SCM_SYSCALL (pos = lseek (SCM_INUM (object), 0, SEEK_CUR)); | |
222 | } | |
0f2d19dd | 223 | if (pos < 0) |
063e05be | 224 | scm_syserror (s_ftell); |
6afcd3b2 | 225 | SCM_ALLOW_INTS; |
8588fa12 | 226 | return scm_long2num (pos); |
0f2d19dd JB |
227 | } |
228 | ||
229 | ||
230 | ||
063e05be | 231 | SCM_PROC (s_fseek, "fseek", 3, 0, 0, scm_fseek); |
1cc91f1b | 232 | |
0f2d19dd | 233 | SCM |
6afcd3b2 GH |
234 | scm_fseek (object, offset, whence) |
235 | SCM object; | |
0f2d19dd JB |
236 | SCM offset; |
237 | SCM whence; | |
0f2d19dd JB |
238 | { |
239 | int rv; | |
8588fa12 GH |
240 | long loff; |
241 | ||
78446828 MV |
242 | object = SCM_COERCE_OUTPORT (object); |
243 | ||
063e05be | 244 | loff = scm_num2long (offset, (char *)SCM_ARG2, s_fseek); |
6afcd3b2 GH |
245 | SCM_ASSERT (SCM_INUMP (whence), whence, SCM_ARG3, s_fseek); |
246 | SCM_DEFER_INTS; | |
247 | if (SCM_NIMP (object) && SCM_OPFPORTP (object)) | |
248 | { | |
249 | SCM_CLRDY (object); /* Clear ungetted char */ | |
250 | rv = fseek ((FILE *)SCM_STREAM (object), loff, SCM_INUM (whence)); | |
251 | } | |
252 | else | |
253 | { | |
254 | SCM_ASSERT (SCM_INUMP (object), object, SCM_ARG1, s_fseek); | |
255 | rv = lseek (SCM_INUM (object), loff, SCM_INUM (whence)); | |
256 | } | |
257 | if (rv < 0) | |
063e05be | 258 | scm_syserror (s_fseek); |
6afcd3b2 | 259 | SCM_ALLOW_INTS; |
02b754d3 | 260 | return SCM_UNSPECIFIED; |
0f2d19dd JB |
261 | } |
262 | ||
063e05be | 263 | SCM_PROC (s_redirect_port, "redirect-port", 2, 0, 0, scm_redirect_port); |
1cc91f1b | 264 | |
0f2d19dd | 265 | SCM |
9c29ac66 GH |
266 | scm_redirect_port (old, new) |
267 | SCM old; | |
268 | SCM new; | |
0f2d19dd JB |
269 | { |
270 | int ans, oldfd, newfd; | |
9c29ac66 | 271 | |
78446828 MV |
272 | old = SCM_COERCE_OUTPORT (old); |
273 | new = SCM_COERCE_OUTPORT (new); | |
274 | ||
0f2d19dd | 275 | SCM_DEFER_INTS; |
9c29ac66 GH |
276 | SCM_ASSERT (SCM_NIMP (old) && SCM_OPPORTP (old), old, SCM_ARG1, s_redirect_port); |
277 | SCM_ASSERT (SCM_NIMP (new) && SCM_OPPORTP (new), new, SCM_ARG2, s_redirect_port); | |
278 | oldfd = fileno ((FILE *)SCM_STREAM (old)); | |
02b754d3 | 279 | if (oldfd == -1) |
063e05be | 280 | scm_syserror (s_redirect_port); |
9c29ac66 | 281 | newfd = fileno ((FILE *)SCM_STREAM (new)); |
02b754d3 | 282 | if (newfd == -1) |
063e05be | 283 | scm_syserror (s_redirect_port); |
02b754d3 GH |
284 | SCM_SYSCALL (ans = dup2 (oldfd, newfd)); |
285 | if (ans == -1) | |
063e05be | 286 | scm_syserror (s_redirect_port); |
0f2d19dd | 287 | SCM_ALLOW_INTS; |
02b754d3 | 288 | return SCM_UNSPECIFIED; |
0f2d19dd JB |
289 | } |
290 | ||
7a6f1ffa | 291 | SCM_PROC (s_dup_to_fdes, "dup->fdes", 1, 1, 0, scm_dup_to_fdes); |
a9488d12 | 292 | SCM |
7a6f1ffa | 293 | scm_dup_to_fdes (SCM fd_or_port, SCM fd) |
a9488d12 GH |
294 | { |
295 | int oldfd, newfd, rv; | |
296 | ||
78446828 MV |
297 | fd_or_port = SCM_COERCE_OUTPORT (fd_or_port); |
298 | ||
a9488d12 GH |
299 | SCM_DEFER_INTS; |
300 | if (SCM_INUMP (fd_or_port)) | |
301 | oldfd = SCM_INUM (fd_or_port); | |
302 | else | |
303 | { | |
304 | SCM_ASSERT (SCM_NIMP (fd_or_port) && SCM_OPPORTP (fd_or_port), | |
7a6f1ffa | 305 | fd_or_port, SCM_ARG1, s_dup_to_fdes); |
a9488d12 GH |
306 | oldfd = fileno ((FILE *)SCM_STREAM (fd_or_port)); |
307 | if (oldfd == -1) | |
7a6f1ffa | 308 | scm_syserror (s_dup_to_fdes); |
a9488d12 | 309 | } |
7a6f1ffa GH |
310 | |
311 | if (SCM_UNBNDP (fd)) | |
e38303a2 | 312 | { |
7a6f1ffa GH |
313 | SCM_SYSCALL (newfd = dup (oldfd)); |
314 | if (newfd == -1) | |
468b2cf3 | 315 | scm_syserror (s_dup_to_fdes); |
7a6f1ffa GH |
316 | fd = SCM_MAKINUM (newfd); |
317 | } | |
318 | else | |
319 | { | |
320 | SCM_ASSERT (SCM_INUMP (fd), fd, SCM_ARG2, s_dup_to_fdes); | |
321 | newfd = SCM_INUM (fd); | |
322 | if (oldfd != newfd) | |
323 | { | |
324 | scm_evict_ports (newfd); /* see scsh manual. */ | |
325 | SCM_SYSCALL (rv = dup2 (oldfd, newfd)); | |
326 | if (rv == -1) | |
327 | scm_syserror (s_dup_to_fdes); | |
328 | } | |
e38303a2 | 329 | } |
a9488d12 | 330 | SCM_ALLOW_INTS; |
e38303a2 | 331 | return fd; |
a9488d12 GH |
332 | } |
333 | ||
063e05be | 334 | SCM_PROC (s_fileno, "fileno", 1, 0, 0, scm_fileno); |
1cc91f1b | 335 | |
0f2d19dd | 336 | SCM |
063e05be | 337 | scm_fileno (port) |
0f2d19dd | 338 | SCM port; |
0f2d19dd JB |
339 | { |
340 | int fd; | |
78446828 MV |
341 | |
342 | port = SCM_COERCE_OUTPORT (port); | |
343 | ||
063e05be | 344 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPFPORTP (port), port, SCM_ARG1, s_fileno); |
0f2d19dd | 345 | fd = fileno ((FILE *)SCM_STREAM (port)); |
02b754d3 | 346 | if (fd == -1) |
063e05be | 347 | scm_syserror (s_fileno); |
02b754d3 | 348 | return SCM_MAKINUM (fd); |
0f2d19dd JB |
349 | } |
350 | ||
063e05be | 351 | SCM_PROC (s_isatty, "isatty?", 1, 0, 0, scm_isatty_p); |
1cc91f1b | 352 | |
0f2d19dd | 353 | SCM |
063e05be | 354 | scm_isatty_p (port) |
0f2d19dd | 355 | SCM port; |
0f2d19dd JB |
356 | { |
357 | int rv; | |
78446828 MV |
358 | |
359 | port = SCM_COERCE_OUTPORT (port); | |
360 | ||
10ccfad7 MD |
361 | if (!(SCM_NIMP (port) && SCM_OPFPORTP (port))) |
362 | return SCM_BOOL_F; | |
0f2d19dd JB |
363 | rv = fileno ((FILE *)SCM_STREAM (port)); |
364 | if (rv == -1) | |
063e05be | 365 | scm_syserror (s_isatty); |
02b754d3 GH |
366 | rv = isatty (rv); |
367 | return rv ? SCM_BOOL_T : SCM_BOOL_F; | |
0f2d19dd JB |
368 | } |
369 | ||
370 | ||
371 | ||
063e05be | 372 | SCM_PROC (s_fdopen, "fdopen", 2, 0, 0, scm_fdopen); |
1cc91f1b | 373 | |
0f2d19dd | 374 | SCM |
063e05be | 375 | scm_fdopen (fdes, modes) |
0f2d19dd JB |
376 | SCM fdes; |
377 | SCM modes; | |
0f2d19dd JB |
378 | { |
379 | FILE *f; | |
380 | SCM port; | |
381 | ||
063e05be | 382 | SCM_ASSERT (SCM_INUMP (fdes), fdes, SCM_ARG1, s_fdopen); |
ae2fa5bc GH |
383 | SCM_ASSERT (SCM_NIMP (modes) && SCM_ROSTRINGP (modes), modes, SCM_ARG2, |
384 | s_fdopen); | |
89958ad0 | 385 | SCM_COERCE_SUBSTR (modes); |
0f2d19dd | 386 | SCM_DEFER_INTS; |
ae2fa5bc | 387 | f = fdopen (SCM_INUM (fdes), SCM_ROCHARS (modes)); |
0f2d19dd | 388 | if (f == NULL) |
063e05be | 389 | scm_syserror (s_fdopen); |
bc45012d | 390 | port = scm_stdio_to_port (f, SCM_ROCHARS (modes), SCM_BOOL_F); |
0f2d19dd JB |
391 | SCM_ALLOW_INTS; |
392 | return port; | |
393 | } | |
394 | ||
395 | ||
396 | ||
397 | /* Move a port's underlying file descriptor to a given value. | |
8b13c6b3 GH |
398 | * Returns #f if fdes is already the given value. |
399 | * #t if fdes moved. | |
0f2d19dd JB |
400 | * MOVE->FDES is implemented in Scheme and calls this primitive. |
401 | */ | |
063e05be | 402 | SCM_PROC (s_primitive_move_to_fdes, "primitive-move->fdes", 2, 0, 0, scm_primitive_move_to_fdes); |
1cc91f1b | 403 | |
0f2d19dd | 404 | SCM |
063e05be | 405 | scm_primitive_move_to_fdes (port, fd) |
0f2d19dd JB |
406 | SCM port; |
407 | SCM fd; | |
0f2d19dd JB |
408 | { |
409 | FILE *stream; | |
410 | int old_fd; | |
411 | int new_fd; | |
412 | int rv; | |
413 | ||
78446828 MV |
414 | port = SCM_COERCE_OUTPORT (port); |
415 | ||
063e05be GH |
416 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPFPORTP (port), port, SCM_ARG1, s_primitive_move_to_fdes); |
417 | SCM_ASSERT (SCM_INUMP (fd), fd, SCM_ARG2, s_primitive_move_to_fdes); | |
0f2d19dd JB |
418 | SCM_DEFER_INTS; |
419 | stream = (FILE *)SCM_STREAM (port); | |
420 | old_fd = fileno (stream); | |
421 | new_fd = SCM_INUM (fd); | |
422 | if (old_fd == new_fd) | |
423 | { | |
424 | SCM_ALLOW_INTS; | |
8b13c6b3 | 425 | return SCM_BOOL_F; |
0f2d19dd JB |
426 | } |
427 | scm_evict_ports (new_fd); | |
428 | rv = dup2 (old_fd, new_fd); | |
429 | if (rv == -1) | |
063e05be | 430 | scm_syserror (s_primitive_move_to_fdes); |
0f2d19dd JB |
431 | scm_setfileno (stream, new_fd); |
432 | SCM_SYSCALL (close (old_fd)); | |
433 | SCM_ALLOW_INTS; | |
8b13c6b3 | 434 | return SCM_BOOL_T; |
0f2d19dd JB |
435 | } |
436 | ||
0f2d19dd JB |
437 | /* Return a list of ports using a given file descriptor. */ |
438 | SCM_PROC(s_fdes_to_ports, "fdes->ports", 1, 0, 0, scm_fdes_to_ports); | |
1cc91f1b | 439 | |
0f2d19dd JB |
440 | SCM |
441 | scm_fdes_to_ports (fd) | |
442 | SCM fd; | |
0f2d19dd JB |
443 | { |
444 | SCM result = SCM_EOL; | |
445 | int int_fd; | |
446 | int i; | |
447 | ||
448 | SCM_ASSERT (SCM_INUMP (fd), fd, SCM_ARG1, s_fdes_to_ports); | |
449 | int_fd = SCM_INUM (fd); | |
450 | ||
451 | SCM_DEFER_INTS; | |
452 | for (i = 0; i < scm_port_table_size; i++) | |
453 | { | |
454 | if (SCM_FPORTP (scm_port_table[i]->port) | |
455 | && fileno ((FILE *)SCM_STREAM (scm_port_table[i]->port)) == int_fd) | |
456 | result = scm_cons (scm_port_table[i]->port, result); | |
457 | } | |
458 | SCM_ALLOW_INTS; | |
459 | return result; | |
460 | } | |
461 | ||
1cc91f1b | 462 | |
0f2d19dd JB |
463 | void |
464 | scm_init_ioext () | |
0f2d19dd JB |
465 | { |
466 | /* fseek() symbols. */ | |
467 | scm_sysintern ("SEEK_SET", SCM_MAKINUM (SEEK_SET)); | |
468 | scm_sysintern ("SEEK_CUR", SCM_MAKINUM (SEEK_CUR)); | |
469 | scm_sysintern ("SEEK_END", SCM_MAKINUM (SEEK_END)); | |
470 | ||
0f2d19dd JB |
471 | #include "ioext.x" |
472 | } | |
473 |