1 /* Copyright (C) 1995, 1996, 1997, 1998, 1999 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. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
70 SCM_DEFINE (scm_read_delimited_x
, "%read-delimited!", 3, 3, 0,
71 (SCM delims
, SCM buf
, SCM gobble
, SCM port
, SCM start
, SCM end
),
72 "Read characters from @var{port} into @var{buf} until one of the\n"
73 "characters in the @var{delims} string is encountered. If @var{gobble?}\n"
74 "is true, store the delimiter character in @var{buf} as well; otherwise,\n"
75 "discard it. If @var{port} is not specified, use the value of\n"
76 "@code{(current-input-port)}. If @var{start} or @var{end} are specified,\n"
77 "store data only into the substring of @var{buf} bounded by @var{start}\n"
78 "and @var{end} (which default to the beginning and end of the buffer,\n"
80 "Return a pair consisting of the delimiter that terminated the string and\n"
81 "the number of characters read. If reading stopped at the end of file,\n"
82 "the delimiter returned is the @var{eof-object}; if the buffer was filled\n"
83 "without encountering a delimiter, this value is @var{#f}.")
84 #define FUNC_NAME s_scm_read_delimited_x
94 SCM_VALIDATE_ROSTRING_COPY (1,delims
,cdelims
);
95 num_delims
= SCM_ROLENGTH (delims
);
96 SCM_VALIDATE_STRING_COPY (2,buf
,cbuf
);
97 cend
= SCM_LENGTH (buf
);
98 if (SCM_UNBNDP (port
))
101 SCM_VALIDATE_OPINPORT (4,port
);
103 SCM_VALIDATE_INUM_DEF_COPY (5,start
,0,cstart
);
104 SCM_ASSERT_RANGE(5, start
, cstart
>= 0 && cstart
< cend
);
106 SCM_VALIDATE_INUM_DEF_COPY (6,end
,cend
,tend
);
107 SCM_ASSERT_RANGE(6, end
, tend
> cstart
&& tend
<= cend
);
111 for (j
= cstart
; j
< cend
; j
++)
116 for (k
= 0; k
< num_delims
; k
++)
120 if (SCM_FALSEP (gobble
))
121 scm_ungetc (c
, port
);
123 return scm_cons (SCM_MAKE_CHAR (c
),
124 scm_long2num (j
- cstart
));
128 return scm_cons (SCM_EOF_VAL
,
129 scm_long2num (j
- cstart
));
133 return scm_cons (SCM_BOOL_F
, scm_long2num (j
- cstart
));
137 static unsigned char *
138 scm_do_read_line (SCM port
, int *len_p
)
140 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
143 /* I thought reading lines was simple. Mercy me. */
145 /* The common case: the buffer contains a complete line.
146 This needs to be fast. */
147 if ((end
= memchr (pt
->read_pos
, '\n', (pt
->read_end
- pt
->read_pos
)))
150 int buf_len
= (end
+ 1) - pt
->read_pos
;
151 /* Allocate a buffer of the perfect size. */
152 unsigned char *buf
= scm_must_malloc (buf_len
+ 1, "%read-line");
154 memcpy (buf
, pt
->read_pos
, buf_len
);
155 pt
->read_pos
+= buf_len
;
163 /* The buffer contains no newlines. */
165 /* When live, len is always the number of characters in the
166 current buffer that are part of the current line. */
167 int len
= (pt
->read_end
- pt
->read_pos
);
168 int buf_size
= (len
< 50) ? 60 : len
* 2;
169 /* Invariant: buf always has buf_size + 1 characters allocated;
170 the `+ 1' is for the final '\0'. */
171 unsigned char *buf
= scm_must_malloc (buf_size
+ 1, "%read-line");
176 if (buf_len
+ len
> buf_size
)
178 int new_size
= (buf_len
+ len
) * 2;
179 buf
= scm_must_realloc (buf
, buf_size
+ 1, new_size
+ 1,
184 /* Copy what we've got out of the port, into our buffer. */
185 memcpy (buf
+ buf_len
, pt
->read_pos
, len
);
189 /* If we had seen a newline, we're done now. */
193 /* Get more characters. */
194 if (scm_fill_input (port
) == EOF
)
196 /* If we're missing a final newline in the file, return
197 what we did get, sans newline. */
205 /* Search the buffer for newlines. */
206 if ((end
= memchr (pt
->read_pos
, '\n',
207 (len
= (pt
->read_end
- pt
->read_pos
))))
209 len
= (end
- pt
->read_pos
) + 1;
212 /* I wonder how expensive this realloc is. */
213 buf
= scm_must_realloc (buf
, buf_size
+ 1, buf_len
+ 1, "%read-line");
223 * truncates any terminating newline from its input, and returns
224 * a cons of the string read and its terminating character. Doing
225 * so makes it easy to implement the hairy `read-line' options
226 * efficiently in Scheme.
229 SCM_DEFINE (scm_read_line
, "%read-line", 0, 1, 0,
231 "Read a newline-terminated line from @var{port}, allocating storage as\n"
232 "necessary. The newline terminator (if any) is removed from the string,\n"
233 "and a pair consisting of the line and its delimiter is returned. The\n"
234 "delimiter may be either a newline or the @var{eof-object}; if\n"
235 "@code{%read-line} is called at the end of file, it returns the pair\n"
236 "@code{(#<eof> . #<eof>)}.")
237 #define FUNC_NAME s_scm_read_line
244 if (SCM_UNBNDP (port
))
246 SCM_VALIDATE_OPINPORT (1,port
);
248 pt
= SCM_PTAB_ENTRY (port
);
249 if (pt
->rw_active
== SCM_PORT_WRITE
)
250 scm_ptobs
[SCM_PTOBNUM (port
)].flush (port
);
252 s
= scm_do_read_line (port
, &slen
);
255 term
= line
= SCM_EOF_VAL
;
258 if (s
[slen
-1] == '\n')
260 term
= SCM_MAKE_CHAR ('\n');
262 line
= scm_take_str (s
, slen
-1);
263 scm_done_malloc (-1);
268 /* Fix: we should check for eof on the port before assuming this. */
270 line
= scm_take_str (s
, slen
);
271 SCM_COL (port
) += slen
;
276 pt
->rw_active
= SCM_PORT_READ
;
278 return scm_cons (line
, term
);
282 SCM_DEFINE (scm_write_line
, "write-line", 1, 1, 0,
284 "Display @var{obj} and a newline character to @var{port}. If @var{port}\n"
285 "is not specified, @code{(current-output-port)} is used. This function\n"
286 "is equivalent to:\n\n"
288 "(display obj [port])\n"
291 #define FUNC_NAME s_scm_write_line
293 scm_display (obj
, port
);
294 return scm_newline (port
);
298 SCM_DEFINE (scm_ftell
, "ftell", 1, 0, 0,
300 "Returns an integer representing the current position of @var{fd/port},\n"
301 "measured from the beginning. Equivalent to:\n"
303 "(seek port 0 SEEK_CUR)\n"
305 #define FUNC_NAME s_scm_ftell
307 return scm_seek (object
, SCM_INUM0
, SCM_MAKINUM (SEEK_CUR
));
311 SCM_DEFINE (scm_fseek
, "fseek", 3, 0, 0,
312 (SCM object
, SCM offset
, SCM whence
),
313 "Obsolete. Almost the same as seek, above, but the return value is\n"
315 #define FUNC_NAME s_scm_fseek
317 scm_seek (object
, offset
, whence
);
318 return SCM_UNSPECIFIED
;
322 SCM_DEFINE (scm_redirect_port
, "redirect-port", 2, 0, 0,
324 "This procedure takes two ports and duplicates the underlying file\n"
325 "descriptor from @var{old-port} into @var{new-port}. The\n"
326 "current file descriptor in @var{new-port} will be closed.\n"
327 "After the redirection the two ports will share a file position\n"
328 "and file status flags.\n\n"
329 "The return value is unspecified.\n\n"
330 "Unexpected behaviour can result if both ports are subsequently used\n"
331 "and the original and/or duplicate ports are buffered.\n\n"
332 "This procedure does not have any side effects on other ports or\n"
334 #define FUNC_NAME s_scm_redirect_port
336 int ans
, oldfd
, newfd
;
337 struct scm_fport
*fp
;
339 old
= SCM_COERCE_OUTPORT (old
);
340 new = SCM_COERCE_OUTPORT (new);
342 SCM_VALIDATE_OPFPORT (1,old
);
343 SCM_VALIDATE_OPFPORT (2,new);
344 oldfd
= SCM_FPORT_FDES (old
);
345 fp
= SCM_FSTREAM (new);
349 scm_port
*pt
= SCM_PTAB_ENTRY (new);
350 scm_port
*old_pt
= SCM_PTAB_ENTRY (old
);
351 scm_ptob_descriptor
*ptob
= &scm_ptobs
[SCM_PTOBNUM (new)];
353 /* must flush to old fdes. */
354 if (pt
->rw_active
== SCM_PORT_WRITE
)
356 else if (pt
->rw_active
== SCM_PORT_READ
)
358 ans
= dup2 (oldfd
, newfd
);
361 pt
->rw_random
= old_pt
->rw_random
;
362 /* continue using existing buffers, even if inappropriate. */
364 return SCM_UNSPECIFIED
;
368 SCM_DEFINE (scm_dup_to_fdes
, "dup->fdes", 1, 1, 0,
369 (SCM fd_or_port
, SCM fd
),
370 "Returns an integer file descriptor.")
371 #define FUNC_NAME s_scm_dup_to_fdes
373 int oldfd
, newfd
, rv
;
375 fd_or_port
= SCM_COERCE_OUTPORT (fd_or_port
);
377 if (SCM_INUMP (fd_or_port
))
378 oldfd
= SCM_INUM (fd_or_port
);
381 SCM_VALIDATE_OPFPORT (1,fd_or_port
);
382 oldfd
= SCM_FPORT_FDES (fd_or_port
);
390 fd
= SCM_MAKINUM (newfd
);
394 SCM_VALIDATE_INUM_COPY (2, fd
, newfd
);
397 scm_evict_ports (newfd
); /* see scsh manual. */
398 rv
= dup2 (oldfd
, newfd
);
407 SCM_DEFINE (scm_fileno
, "fileno", 1, 0, 0,
409 "Returns the integer file descriptor underlying @var{port}.\n"
410 "Does not change its revealed count.")
411 #define FUNC_NAME s_scm_fileno
413 port
= SCM_COERCE_OUTPORT (port
);
414 SCM_VALIDATE_OPFPORT (1,port
);
415 return SCM_MAKINUM (SCM_FPORT_FDES (port
));
419 /* GJB:FIXME:: why does this not throw
420 an error if the arg is not a port?
421 This proc as is would be better names isattyport?
422 if it is not going to assume that the arg is a port */
423 SCM_DEFINE (scm_isatty_p
, "isatty?", 1, 0, 0,
425 "Returns @code{#t} if @var{port} is using a serial\n"
426 "non-file device, otherwise @code{#f}.")
427 #define FUNC_NAME s_scm_isatty_p
431 port
= SCM_COERCE_OUTPORT (port
);
433 if (!SCM_OPFPORTP (port
))
436 rv
= isatty (SCM_FPORT_FDES (port
));
443 SCM_DEFINE (scm_fdopen
, "fdopen", 2, 0, 0,
444 (SCM fdes
, SCM modes
),
445 "Returns a new port based on the file descriptor @var{fdes}.\n"
446 "Modes are given by the string @var{modes}. The revealed count of the port\n"
447 "is initialized to zero. The modes string is the same as that accepted\n"
448 "by @ref{File Ports, open-file}.")
449 #define FUNC_NAME s_scm_fdopen
451 SCM_VALIDATE_INUM (1,fdes
);
452 SCM_VALIDATE_ROSTRING (2,modes
);
453 SCM_COERCE_SUBSTR (modes
);
455 return scm_fdes_to_port (SCM_INUM (fdes
), SCM_ROCHARS (modes
), SCM_BOOL_F
);
461 /* Move a port's underlying file descriptor to a given value.
462 * Returns #f if fdes is already the given value.
464 * MOVE->FDES is implemented in Scheme and calls this primitive.
466 SCM_DEFINE (scm_primitive_move_to_fdes
, "primitive-move->fdes", 2, 0, 0,
468 "Moves the underlying file descriptor for @var{port} to the integer\n"
469 "value @var{fdes} without changing the revealed count of @var{port}.\n"
470 "Any other ports already using this descriptor will be automatically\n"
471 "shifted to new descriptors and their revealed counts reset to zero.\n"
472 "The return value is @code{#f} if the file descriptor already had the\n"
473 "required value or @code{#t} if it was moved.")
474 #define FUNC_NAME s_scm_primitive_move_to_fdes
476 struct scm_fport
*stream
;
481 port
= SCM_COERCE_OUTPORT (port
);
483 SCM_VALIDATE_OPFPORT (1,port
);
484 SCM_VALIDATE_INUM (2,fd
);
485 stream
= SCM_FSTREAM (port
);
486 old_fd
= stream
->fdes
;
487 new_fd
= SCM_INUM (fd
);
488 if (old_fd
== new_fd
)
492 scm_evict_ports (new_fd
);
493 rv
= dup2 (old_fd
, new_fd
);
496 stream
->fdes
= new_fd
;
497 SCM_SYSCALL (close (old_fd
));
502 /* Return a list of ports using a given file descriptor. */
503 SCM_DEFINE (scm_fdes_to_ports
, "fdes->ports", 1, 0, 0,
505 "Returns a list of existing ports which have @var{fdes} as an\n"
506 "underlying file descriptor, without changing their revealed counts.")
507 #define FUNC_NAME s_scm_fdes_to_ports
509 SCM result
= SCM_EOL
;
513 SCM_VALIDATE_INUM_COPY (1,fd
,int_fd
);
515 for (i
= 0; i
< scm_port_table_size
; i
++)
517 if (SCM_OPFPORTP (scm_port_table
[i
]->port
)
518 && ((struct scm_fport
*) scm_port_table
[i
]->stream
)->fdes
== int_fd
)
519 result
= scm_cons (scm_port_table
[i
]->port
, result
);
529 scm_add_feature ("i/o-extensions");