* numbers.h (SCM_MAKINUM, SCM_I_MAKINUM): Renamed SCM_MAKINUM to
[bpt/guile.git] / libguile / ports.c
1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2003, 2004 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library 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 GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18
19 \f
20 /* Headers. */
21
22 #if HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <errno.h>
28
29 #include "libguile/_scm.h"
30 #include "libguile/eval.h"
31 #include "libguile/objects.h"
32 #include "libguile/smob.h"
33 #include "libguile/chars.h"
34 #include "libguile/dynwind.h"
35
36 #include "libguile/keywords.h"
37 #include "libguile/root.h"
38 #include "libguile/strings.h"
39 #include "libguile/mallocs.h"
40 #include "libguile/validate.h"
41 #include "libguile/ports.h"
42
43 #ifdef HAVE_STRING_H
44 #include <string.h>
45 #endif
46
47 #ifdef HAVE_MALLOC_H
48 #include <malloc.h>
49 #endif
50
51 #ifdef HAVE_IO_H
52 #include <io.h>
53 #endif
54
55 #ifdef HAVE_UNISTD_H
56 #include <unistd.h>
57 #endif
58
59 #ifdef HAVE_SYS_IOCTL_H
60 #include <sys/ioctl.h>
61 #endif
62
63 #ifdef __MINGW32__
64 #include <fcntl.h>
65 #define ftruncate(fd, size) chsize (fd, size)
66 #endif
67
68 \f
69 /* The port kind table --- a dynamically resized array of port types. */
70
71
72 /* scm_ptobs scm_numptob
73 * implement a dynamicly resized array of ptob records.
74 * Indexes into this table are used when generating type
75 * tags for smobjects (if you know a tag you can get an index and conversely).
76 */
77 scm_t_ptob_descriptor *scm_ptobs;
78 long scm_numptob;
79
80 /* GC marker for a port with stream of SCM type. */
81 SCM
82 scm_markstream (SCM ptr)
83 {
84 int openp;
85 openp = SCM_CELL_WORD_0 (ptr) & SCM_OPN;
86 if (openp)
87 return SCM_PACK (SCM_STREAM (ptr));
88 else
89 return SCM_BOOL_F;
90 }
91
92 /*
93 * We choose to use an interface similar to the smob interface with
94 * fill_input and write as standard fields, passed to the port
95 * type constructor, and optional fields set by setters.
96 */
97
98 static void
99 flush_port_default (SCM port SCM_UNUSED)
100 {
101 }
102
103 static void
104 end_input_default (SCM port SCM_UNUSED, int offset SCM_UNUSED)
105 {
106 }
107
108 static size_t
109 scm_port_free0 (SCM port)
110 {
111 return 0;
112 }
113
114 scm_t_bits
115 scm_make_port_type (char *name,
116 int (*fill_input) (SCM port),
117 void (*write) (SCM port, const void *data, size_t size))
118 {
119 char *tmp;
120 if (255 <= scm_numptob)
121 goto ptoberr;
122 SCM_DEFER_INTS;
123 SCM_SYSCALL (tmp = (char *) realloc ((char *) scm_ptobs,
124 (1 + scm_numptob)
125 * sizeof (scm_t_ptob_descriptor)));
126 if (tmp)
127 {
128 scm_ptobs = (scm_t_ptob_descriptor *) tmp;
129
130 scm_ptobs[scm_numptob].name = name;
131 scm_ptobs[scm_numptob].mark = 0;
132 scm_ptobs[scm_numptob].free = scm_port_free0;
133 scm_ptobs[scm_numptob].print = scm_port_print;
134 scm_ptobs[scm_numptob].equalp = 0;
135 scm_ptobs[scm_numptob].close = 0;
136
137 scm_ptobs[scm_numptob].write = write;
138 scm_ptobs[scm_numptob].flush = flush_port_default;
139
140 scm_ptobs[scm_numptob].end_input = end_input_default;
141 scm_ptobs[scm_numptob].fill_input = fill_input;
142 scm_ptobs[scm_numptob].input_waiting = 0;
143
144 scm_ptobs[scm_numptob].seek = 0;
145 scm_ptobs[scm_numptob].truncate = 0;
146
147 scm_numptob++;
148 }
149 SCM_ALLOW_INTS;
150 if (!tmp)
151 {
152 ptoberr:
153 scm_memory_error ("scm_make_port_type");
154 }
155 /* Make a class object if Goops is present */
156 if (scm_port_class)
157 scm_make_port_classes (scm_numptob - 1, SCM_PTOBNAME (scm_numptob - 1));
158 return scm_tc7_port + (scm_numptob - 1) * 256;
159 }
160
161 void
162 scm_set_port_mark (long tc, SCM (*mark) (SCM))
163 {
164 scm_ptobs[SCM_TC2PTOBNUM (tc)].mark = mark;
165 }
166
167 void
168 scm_set_port_free (long tc, size_t (*free) (SCM))
169 {
170 scm_ptobs[SCM_TC2PTOBNUM (tc)].free = free;
171 }
172
173 void
174 scm_set_port_print (long tc, int (*print) (SCM exp, SCM port,
175 scm_print_state *pstate))
176 {
177 scm_ptobs[SCM_TC2PTOBNUM (tc)].print = print;
178 }
179
180 void
181 scm_set_port_equalp (long tc, SCM (*equalp) (SCM, SCM))
182 {
183 scm_ptobs[SCM_TC2PTOBNUM (tc)].equalp = equalp;
184 }
185
186 void
187 scm_set_port_flush (long tc, void (*flush) (SCM port))
188 {
189 scm_ptobs[SCM_TC2PTOBNUM (tc)].flush = flush;
190 }
191
192 void
193 scm_set_port_end_input (long tc, void (*end_input) (SCM port, int offset))
194 {
195 scm_ptobs[SCM_TC2PTOBNUM (tc)].end_input = end_input;
196 }
197
198 void
199 scm_set_port_close (long tc, int (*close) (SCM))
200 {
201 scm_ptobs[SCM_TC2PTOBNUM (tc)].close = close;
202 }
203
204 void
205 scm_set_port_seek (long tc, off_t (*seek) (SCM port,
206 off_t OFFSET,
207 int WHENCE))
208 {
209 scm_ptobs[SCM_TC2PTOBNUM (tc)].seek = seek;
210 }
211
212 void
213 scm_set_port_truncate (long tc, void (*truncate) (SCM port, off_t length))
214 {
215 scm_ptobs[SCM_TC2PTOBNUM (tc)].truncate = truncate;
216 }
217
218 void
219 scm_set_port_input_waiting (long tc, int (*input_waiting) (SCM))
220 {
221 scm_ptobs[SCM_TC2PTOBNUM (tc)].input_waiting = input_waiting;
222 }
223
224 \f
225
226 SCM_DEFINE (scm_char_ready_p, "char-ready?", 0, 1, 0,
227 (SCM port),
228 "Return @code{#t} if a character is ready on input @var{port}\n"
229 "and return @code{#f} otherwise. If @code{char-ready?} returns\n"
230 "@code{#t} then the next @code{read-char} operation on\n"
231 "@var{port} is guaranteed not to hang. If @var{port} is a file\n"
232 "port at end of file then @code{char-ready?} returns @code{#t}.\n"
233 "\n"
234 "@code{char-ready?} exists to make it possible for a\n"
235 "program to accept characters from interactive ports without\n"
236 "getting stuck waiting for input. Any input editors associated\n"
237 "with such ports must make sure that characters whose existence\n"
238 "has been asserted by @code{char-ready?} cannot be rubbed out.\n"
239 "If @code{char-ready?} were to return @code{#f} at end of file,\n"
240 "a port at end of file would be indistinguishable from an\n"
241 "interactive port that has no ready characters.")
242 #define FUNC_NAME s_scm_char_ready_p
243 {
244 scm_t_port *pt;
245
246 if (SCM_UNBNDP (port))
247 port = scm_cur_inp;
248 else
249 SCM_VALIDATE_OPINPORT (1, port);
250
251 pt = SCM_PTAB_ENTRY (port);
252
253 /* if the current read buffer is filled, or the
254 last pushed-back char has been read and the saved buffer is
255 filled, result is true. */
256 if (pt->read_pos < pt->read_end
257 || (pt->read_buf == pt->putback_buf
258 && pt->saved_read_pos < pt->saved_read_end))
259 return SCM_BOOL_T;
260 else
261 {
262 scm_t_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (port)];
263
264 if (ptob->input_waiting)
265 return scm_from_bool(ptob->input_waiting (port));
266 else
267 return SCM_BOOL_T;
268 }
269 }
270 #undef FUNC_NAME
271
272 /* move up to read_len chars from port's putback and/or read buffers
273 into memory starting at dest. returns the number of chars moved. */
274 size_t scm_take_from_input_buffers (SCM port, char *dest, size_t read_len)
275 {
276 scm_t_port *pt = SCM_PTAB_ENTRY (port);
277 size_t chars_read = 0;
278 size_t from_buf = min (pt->read_end - pt->read_pos, read_len);
279
280 if (from_buf > 0)
281 {
282 memcpy (dest, pt->read_pos, from_buf);
283 pt->read_pos += from_buf;
284 chars_read += from_buf;
285 read_len -= from_buf;
286 dest += from_buf;
287 }
288
289 /* if putback was active, try the real input buffer too. */
290 if (pt->read_buf == pt->putback_buf)
291 {
292 from_buf = min (pt->saved_read_end - pt->saved_read_pos, read_len);
293 if (from_buf > 0)
294 {
295 memcpy (dest, pt->saved_read_pos, from_buf);
296 pt->saved_read_pos += from_buf;
297 chars_read += from_buf;
298 }
299 }
300 return chars_read;
301 }
302
303 /* Clear a port's read buffers, returning the contents. */
304 SCM_DEFINE (scm_drain_input, "drain-input", 1, 0, 0,
305 (SCM port),
306 "This procedure clears a port's input buffers, similar\n"
307 "to the way that force-output clears the output buffer. The\n"
308 "contents of the buffers are returned as a single string, e.g.,\n"
309 "\n"
310 "@lisp\n"
311 "(define p (open-input-file ...))\n"
312 "(drain-input p) => empty string, nothing buffered yet.\n"
313 "(unread-char (read-char p) p)\n"
314 "(drain-input p) => initial chars from p, up to the buffer size.\n"
315 "@end lisp\n\n"
316 "Draining the buffers may be useful for cleanly finishing\n"
317 "buffered I/O so that the file descriptor can be used directly\n"
318 "for further input.")
319 #define FUNC_NAME s_scm_drain_input
320 {
321 SCM result;
322 scm_t_port *pt;
323 long count;
324
325 SCM_VALIDATE_OPINPORT (1, port);
326 pt = SCM_PTAB_ENTRY (port);
327
328 count = pt->read_end - pt->read_pos;
329 if (pt->read_buf == pt->putback_buf)
330 count += pt->saved_read_end - pt->saved_read_pos;
331
332 result = scm_allocate_string (count);
333 scm_take_from_input_buffers (port, SCM_STRING_CHARS (result), count);
334
335 return result;
336 }
337 #undef FUNC_NAME
338
339 \f
340 /* Standard ports --- current input, output, error, and more(!). */
341
342 SCM_DEFINE (scm_current_input_port, "current-input-port", 0, 0, 0,
343 (),
344 "Return the current input port. This is the default port used\n"
345 "by many input procedures. Initially, @code{current-input-port}\n"
346 "returns the @dfn{standard input} in Unix and C terminology.")
347 #define FUNC_NAME s_scm_current_input_port
348 {
349 return scm_cur_inp;
350 }
351 #undef FUNC_NAME
352
353 SCM_DEFINE (scm_current_output_port, "current-output-port", 0, 0, 0,
354 (),
355 "Return the current output port. This is the default port used\n"
356 "by many output procedures. Initially,\n"
357 "@code{current-output-port} returns the @dfn{standard output} in\n"
358 "Unix and C terminology.")
359 #define FUNC_NAME s_scm_current_output_port
360 {
361 return scm_cur_outp;
362 }
363 #undef FUNC_NAME
364
365 SCM_DEFINE (scm_current_error_port, "current-error-port", 0, 0, 0,
366 (),
367 "Return the port to which errors and warnings should be sent (the\n"
368 "@dfn{standard error} in Unix and C terminology).")
369 #define FUNC_NAME s_scm_current_error_port
370 {
371 return scm_cur_errp;
372 }
373 #undef FUNC_NAME
374
375 SCM_DEFINE (scm_current_load_port, "current-load-port", 0, 0, 0,
376 (),
377 "Return the current-load-port.\n"
378 "The load port is used internally by @code{primitive-load}.")
379 #define FUNC_NAME s_scm_current_load_port
380 {
381 return scm_cur_loadp;
382 }
383 #undef FUNC_NAME
384
385 SCM_DEFINE (scm_set_current_input_port, "set-current-input-port", 1, 0, 0,
386 (SCM port),
387 "@deffnx {Scheme Procedure} set-current-output-port port\n"
388 "@deffnx {Scheme Procedure} set-current-error-port port\n"
389 "Change the ports returned by @code{current-input-port},\n"
390 "@code{current-output-port} and @code{current-error-port}, respectively,\n"
391 "so that they use the supplied @var{port} for input or output.")
392 #define FUNC_NAME s_scm_set_current_input_port
393 {
394 SCM oinp = scm_cur_inp;
395 SCM_VALIDATE_OPINPORT (1, port);
396 scm_cur_inp = port;
397 return oinp;
398 }
399 #undef FUNC_NAME
400
401
402 SCM_DEFINE (scm_set_current_output_port, "set-current-output-port", 1, 0, 0,
403 (SCM port),
404 "Set the current default output port to @var{port}.")
405 #define FUNC_NAME s_scm_set_current_output_port
406 {
407 SCM ooutp = scm_cur_outp;
408 port = SCM_COERCE_OUTPORT (port);
409 SCM_VALIDATE_OPOUTPORT (1, port);
410 scm_cur_outp = port;
411 return ooutp;
412 }
413 #undef FUNC_NAME
414
415
416 SCM_DEFINE (scm_set_current_error_port, "set-current-error-port", 1, 0, 0,
417 (SCM port),
418 "Set the current default error port to @var{port}.")
419 #define FUNC_NAME s_scm_set_current_error_port
420 {
421 SCM oerrp = scm_cur_errp;
422 port = SCM_COERCE_OUTPORT (port);
423 SCM_VALIDATE_OPOUTPORT (1, port);
424 scm_cur_errp = port;
425 return oerrp;
426 }
427 #undef FUNC_NAME
428
429 typedef struct {
430 SCM value;
431 SCM (*getter) (void);
432 SCM (*setter) (SCM);
433 } swap_data;
434
435 static void
436 swap_port (SCM scm_data)
437 {
438 swap_data *d = (swap_data *)SCM_MALLOCDATA (scm_data);
439 SCM t;
440
441 t = d->getter ();
442 d->setter (d->value);
443 d->value = t;
444 }
445
446 static void
447 scm_frame_current_foo_port (SCM port,
448 SCM (*getter) (void), SCM (*setter) (SCM))
449 {
450 SCM scm_data = scm_malloc_obj (sizeof (swap_data));
451 swap_data *data = (swap_data *)SCM_MALLOCDATA (scm_data);
452 data->value = port;
453 data->getter = getter;
454 data->setter = setter;
455
456 scm_frame_rewind_handler_with_scm (swap_port, scm_data,
457 SCM_F_WIND_EXPLICITLY);
458 scm_frame_unwind_handler_with_scm (swap_port, scm_data,
459 SCM_F_WIND_EXPLICITLY);
460 }
461
462 void
463 scm_frame_current_input_port (SCM port)
464 {
465 scm_frame_current_foo_port (port,
466 scm_current_input_port,
467 scm_set_current_input_port);
468 }
469
470 void
471 scm_frame_current_output_port (SCM port)
472 {
473 scm_frame_current_foo_port (port,
474 scm_current_output_port,
475 scm_set_current_output_port);
476 }
477
478 void
479 scm_frame_current_error_port (SCM port)
480 {
481 scm_frame_current_foo_port (port,
482 scm_current_error_port,
483 scm_set_current_error_port);
484 }
485
486 \f
487 /* The port table --- an array of pointers to ports. */
488
489 scm_t_port **scm_i_port_table;
490
491 long scm_i_port_table_size = 0; /* Number of ports in scm_i_port_table. */
492 long scm_i_port_table_room = 20; /* Size of the array. */
493
494 SCM_GLOBAL_MUTEX (scm_i_port_table_mutex);
495
496 /* This function is not and should not be thread safe. */
497
498 SCM
499 scm_new_port_table_entry (scm_t_bits tag)
500 #define FUNC_NAME "scm_new_port_table_entry"
501 {
502 /*
503 We initialize the cell to empty, this is in case scm_gc_calloc
504 triggers GC ; we don't want the GC to scan a half-finished Z.
505 */
506
507 SCM z = scm_cons (SCM_EOL, SCM_EOL);
508 scm_t_port *entry = (scm_t_port *) scm_gc_calloc (sizeof (scm_t_port), "port");
509 if (scm_i_port_table_size == scm_i_port_table_room)
510 {
511 /* initial malloc is in gc.c. this doesn't use scm_gc_malloc etc.,
512 since it can never be freed during gc. */
513 void *newt = scm_realloc ((char *) scm_i_port_table,
514 (size_t) (sizeof (scm_t_port *)
515 * scm_i_port_table_room * 2));
516 scm_i_port_table = (scm_t_port **) newt;
517 scm_i_port_table_room *= 2;
518 }
519
520 entry->entry = scm_i_port_table_size;
521
522 entry->file_name = SCM_BOOL_F;
523 entry->rw_active = SCM_PORT_NEITHER;
524
525 scm_i_port_table[scm_i_port_table_size] = entry;
526 scm_i_port_table_size++;
527
528 entry->port = z;
529 SCM_SET_CELL_TYPE(z, tag);
530 SCM_SETPTAB_ENTRY(z, entry);
531
532 return z;
533 }
534 #undef FUNC_NAME
535
536 #if SCM_ENABLE_DEPRECATED==1
537 SCM_API scm_t_port *
538 scm_add_to_port_table (SCM port)
539 {
540 SCM z = scm_new_port_table_entry (scm_tc7_port);
541 scm_t_port * pt = SCM_PTAB_ENTRY(z);
542
543 pt->port = port;
544 SCM_SETCAR(z, SCM_EOL);
545 SCM_SETCDR(z, SCM_EOL);
546 SCM_SETPTAB_ENTRY (port, pt);
547 return pt;
548 }
549 #endif
550
551
552 /* Remove a port from the table and destroy it. */
553
554 /* This function is not and should not be thread safe. */
555
556 void
557 scm_remove_from_port_table (SCM port)
558 #define FUNC_NAME "scm_remove_from_port_table"
559 {
560 scm_t_port *p = SCM_PTAB_ENTRY (port);
561 long i = p->entry;
562
563 if (i >= scm_i_port_table_size)
564 SCM_MISC_ERROR ("Port not in table: ~S", scm_list_1 (port));
565 if (p->putback_buf)
566 scm_gc_free (p->putback_buf, p->putback_buf_size, "putback buffer");
567 scm_gc_free (p, sizeof (scm_t_port), "port");
568 /* Since we have just freed slot i we can shrink the table by moving
569 the last entry to that slot... */
570 if (i < scm_i_port_table_size - 1)
571 {
572 scm_i_port_table[i] = scm_i_port_table[scm_i_port_table_size - 1];
573 scm_i_port_table[i]->entry = i;
574 }
575 SCM_SETPTAB_ENTRY (port, 0);
576 scm_i_port_table_size--;
577 }
578 #undef FUNC_NAME
579
580
581 #ifdef GUILE_DEBUG
582 /* Functions for debugging. */
583
584 SCM_DEFINE (scm_pt_size, "pt-size", 0, 0, 0,
585 (),
586 "Return the number of ports in the port table. @code{pt-size}\n"
587 "is only included in @code{--enable-guile-debug} builds.")
588 #define FUNC_NAME s_scm_pt_size
589 {
590 return SCM_I_MAKINUM (scm_i_port_table_size);
591 }
592 #undef FUNC_NAME
593
594 SCM_DEFINE (scm_pt_member, "pt-member", 1, 0, 0,
595 (SCM index),
596 "Return the port at @var{index} in the port table.\n"
597 "@code{pt-member} is only included in\n"
598 "@code{--enable-guile-debug} builds.")
599 #define FUNC_NAME s_scm_pt_member
600 {
601 long i;
602 SCM_VALIDATE_INUM_COPY (1, index, i);
603 if (i < 0 || i >= scm_i_port_table_size)
604 return SCM_BOOL_F;
605 else
606 return scm_i_port_table[i]->port;
607 }
608 #undef FUNC_NAME
609 #endif
610
611 void
612 scm_port_non_buffer (scm_t_port *pt)
613 {
614 pt->read_pos = pt->read_buf = pt->read_end = &pt->shortbuf;
615 pt->write_buf = pt->write_pos = &pt->shortbuf;
616 pt->read_buf_size = pt->write_buf_size = 1;
617 pt->write_end = pt->write_buf + pt->write_buf_size;
618 }
619
620 \f
621 /* Revealed counts --- an oddity inherited from SCSH. */
622
623 /* Find a port in the table and return its revealed count.
624 Also used by the garbage collector.
625 */
626
627 int
628 scm_revealed_count (SCM port)
629 {
630 return SCM_REVEALED(port);
631 }
632
633
634
635 /* Return the revealed count for a port. */
636
637 SCM_DEFINE (scm_port_revealed, "port-revealed", 1, 0, 0,
638 (SCM port),
639 "Return the revealed count for @var{port}.")
640 #define FUNC_NAME s_scm_port_revealed
641 {
642 port = SCM_COERCE_OUTPORT (port);
643 SCM_VALIDATE_OPENPORT (1, port);
644 return SCM_I_MAKINUM (scm_revealed_count (port));
645 }
646 #undef FUNC_NAME
647
648 /* Set the revealed count for a port. */
649 SCM_DEFINE (scm_set_port_revealed_x, "set-port-revealed!", 2, 0, 0,
650 (SCM port, SCM rcount),
651 "Sets the revealed count for a port to a given value.\n"
652 "The return value is unspecified.")
653 #define FUNC_NAME s_scm_set_port_revealed_x
654 {
655 port = SCM_COERCE_OUTPORT (port);
656 SCM_VALIDATE_OPENPORT (1, port);
657 SCM_VALIDATE_INUM (2, rcount);
658 SCM_REVEALED (port) = SCM_INUM (rcount);
659 return SCM_UNSPECIFIED;
660 }
661 #undef FUNC_NAME
662
663
664 \f
665 /* Retrieving a port's mode. */
666
667 /* Return the flags that characterize a port based on the mode
668 * string used to open a file for that port.
669 *
670 * See PORT FLAGS in scm.h
671 */
672
673 long
674 scm_mode_bits (char *modes)
675 {
676 return (SCM_OPN
677 | (strchr (modes, 'r') || strchr (modes, '+') ? SCM_RDNG : 0)
678 | ( strchr (modes, 'w')
679 || strchr (modes, 'a')
680 || strchr (modes, '+') ? SCM_WRTNG : 0)
681 | (strchr (modes, '0') ? SCM_BUF0 : 0)
682 | (strchr (modes, 'l') ? SCM_BUFLINE : 0));
683 }
684
685
686 /* Return the mode flags from an open port.
687 * Some modes such as "append" are only used when opening
688 * a file and are not returned here. */
689
690 SCM_DEFINE (scm_port_mode, "port-mode", 1, 0, 0,
691 (SCM port),
692 "Return the port modes associated with the open port @var{port}.\n"
693 "These will not necessarily be identical to the modes used when\n"
694 "the port was opened, since modes such as \"append\" which are\n"
695 "used only during port creation are not retained.")
696 #define FUNC_NAME s_scm_port_mode
697 {
698 char modes[4];
699 modes[0] = '\0';
700
701 port = SCM_COERCE_OUTPORT (port);
702 SCM_VALIDATE_OPPORT (1, port);
703 if (SCM_CELL_WORD_0 (port) & SCM_RDNG) {
704 if (SCM_CELL_WORD_0 (port) & SCM_WRTNG)
705 strcpy (modes, "r+");
706 else
707 strcpy (modes, "r");
708 }
709 else if (SCM_CELL_WORD_0 (port) & SCM_WRTNG)
710 strcpy (modes, "w");
711 if (SCM_CELL_WORD_0 (port) & SCM_BUF0)
712 strcat (modes, "0");
713 return scm_mem2string (modes, strlen (modes));
714 }
715 #undef FUNC_NAME
716
717
718 \f
719 /* Closing ports. */
720
721 /* scm_close_port
722 * Call the close operation on a port object.
723 * see also scm_close.
724 */
725 SCM_DEFINE (scm_close_port, "close-port", 1, 0, 0,
726 (SCM port),
727 "Close the specified port object. Return @code{#t} if it\n"
728 "successfully closes a port or @code{#f} if it was already\n"
729 "closed. An exception may be raised if an error occurs, for\n"
730 "example when flushing buffered output. See also @ref{Ports and\n"
731 "File Descriptors, close}, for a procedure which can close file\n"
732 "descriptors.")
733 #define FUNC_NAME s_scm_close_port
734 {
735 size_t i;
736 int rv;
737
738 port = SCM_COERCE_OUTPORT (port);
739
740 SCM_VALIDATE_PORT (1, port);
741 if (SCM_CLOSEDP (port))
742 return SCM_BOOL_F;
743 i = SCM_PTOBNUM (port);
744 if (scm_ptobs[i].close)
745 rv = (scm_ptobs[i].close) (port);
746 else
747 rv = 0;
748 scm_mutex_lock (&scm_i_port_table_mutex);
749 scm_remove_from_port_table (port);
750 scm_mutex_unlock (&scm_i_port_table_mutex);
751 SCM_CLR_PORT_OPEN_FLAG (port);
752 return scm_from_bool (rv >= 0);
753 }
754 #undef FUNC_NAME
755
756 SCM_DEFINE (scm_close_input_port, "close-input-port", 1, 0, 0,
757 (SCM port),
758 "Close the specified input port object. The routine has no effect if\n"
759 "the file has already been closed. An exception may be raised if an\n"
760 "error occurs. The value returned is unspecified.\n\n"
761 "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
762 "which can close file descriptors.")
763 #define FUNC_NAME s_scm_close_input_port
764 {
765 SCM_VALIDATE_INPUT_PORT (1, port);
766 scm_close_port (port);
767 return SCM_UNSPECIFIED;
768 }
769 #undef FUNC_NAME
770
771 SCM_DEFINE (scm_close_output_port, "close-output-port", 1, 0, 0,
772 (SCM port),
773 "Close the specified output port object. The routine has no effect if\n"
774 "the file has already been closed. An exception may be raised if an\n"
775 "error occurs. The value returned is unspecified.\n\n"
776 "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
777 "which can close file descriptors.")
778 #define FUNC_NAME s_scm_close_output_port
779 {
780 port = SCM_COERCE_OUTPORT (port);
781 SCM_VALIDATE_OUTPUT_PORT (1, port);
782 scm_close_port (port);
783 return SCM_UNSPECIFIED;
784 }
785 #undef FUNC_NAME
786
787 void
788 scm_c_port_for_each (void (*proc)(void *data, SCM p), void *data)
789 {
790 long i;
791 SCM ports;
792
793 /* Even without pre-emptive multithreading, running arbitrary code
794 while scanning the port table is unsafe because the port table
795 can change arbitrarily (from a GC, for example). So we build a
796 list in advance while blocking the GC. -mvo */
797
798 scm_mutex_lock (&scm_i_port_table_mutex);
799 scm_block_gc++;
800 ports = SCM_EOL;
801 for (i = 0; i < scm_i_port_table_size; i++)
802 ports = scm_cons (scm_i_port_table[i]->port, ports);
803 scm_block_gc--;
804 scm_mutex_unlock (&scm_i_port_table_mutex);
805
806 while (ports != SCM_EOL)
807 {
808 proc (data, SCM_CAR (ports));
809 ports = SCM_CDR (ports);
810 }
811 }
812
813 SCM_DEFINE (scm_port_for_each, "port-for-each", 1, 0, 0,
814 (SCM proc),
815 "Apply @var{proc} to each port in the Guile port table\n"
816 "in turn. The return value is unspecified. More specifically,\n"
817 "@var{proc} is applied exactly once to every port that exists\n"
818 "in the system at the time @var{port-for-each} is invoked.\n"
819 "Changes to the port table while @var{port-for-each} is running\n"
820 "have no effect as far as @var{port-for-each} is concerned.")
821 #define FUNC_NAME s_scm_port_for_each
822 {
823 SCM_VALIDATE_PROC (1, proc);
824
825 scm_c_port_for_each ((void (*)(void*,SCM))scm_call_1, proc);
826 return SCM_UNSPECIFIED;
827 }
828 #undef FUNC_NAME
829
830
831 \f
832 /* Utter miscellany. Gosh, we should clean this up some time. */
833
834 SCM_DEFINE (scm_input_port_p, "input-port?", 1, 0, 0,
835 (SCM x),
836 "Return @code{#t} if @var{x} is an input port, otherwise return\n"
837 "@code{#f}. Any object satisfying this predicate also satisfies\n"
838 "@code{port?}.")
839 #define FUNC_NAME s_scm_input_port_p
840 {
841 return scm_from_bool (SCM_INPUT_PORT_P (x));
842 }
843 #undef FUNC_NAME
844
845 SCM_DEFINE (scm_output_port_p, "output-port?", 1, 0, 0,
846 (SCM x),
847 "Return @code{#t} if @var{x} is an output port, otherwise return\n"
848 "@code{#f}. Any object satisfying this predicate also satisfies\n"
849 "@code{port?}.")
850 #define FUNC_NAME s_scm_output_port_p
851 {
852 x = SCM_COERCE_OUTPORT (x);
853 return scm_from_bool (SCM_OUTPUT_PORT_P (x));
854 }
855 #undef FUNC_NAME
856
857 SCM_DEFINE (scm_port_p, "port?", 1, 0, 0,
858 (SCM x),
859 "Return a boolean indicating whether @var{x} is a port.\n"
860 "Equivalent to @code{(or (input-port? @var{x}) (output-port?\n"
861 "@var{x}))}.")
862 #define FUNC_NAME s_scm_port_p
863 {
864 return scm_from_bool (SCM_PORTP (x));
865 }
866 #undef FUNC_NAME
867
868 SCM_DEFINE (scm_port_closed_p, "port-closed?", 1, 0, 0,
869 (SCM port),
870 "Return @code{#t} if @var{port} is closed or @code{#f} if it is\n"
871 "open.")
872 #define FUNC_NAME s_scm_port_closed_p
873 {
874 SCM_VALIDATE_PORT (1, port);
875 return scm_from_bool (!SCM_OPPORTP (port));
876 }
877 #undef FUNC_NAME
878
879 SCM_DEFINE (scm_eof_object_p, "eof-object?", 1, 0, 0,
880 (SCM x),
881 "Return @code{#t} if @var{x} is an end-of-file object; otherwise\n"
882 "return @code{#f}.")
883 #define FUNC_NAME s_scm_eof_object_p
884 {
885 return scm_from_bool(SCM_EOF_OBJECT_P (x));
886 }
887 #undef FUNC_NAME
888
889 SCM_DEFINE (scm_force_output, "force-output", 0, 1, 0,
890 (SCM port),
891 "Flush the specified output port, or the current output port if @var{port}\n"
892 "is omitted. The current output buffer contents are passed to the\n"
893 "underlying port implementation (e.g., in the case of fports, the\n"
894 "data will be written to the file and the output buffer will be cleared.)\n"
895 "It has no effect on an unbuffered port.\n\n"
896 "The return value is unspecified.")
897 #define FUNC_NAME s_scm_force_output
898 {
899 if (SCM_UNBNDP (port))
900 port = scm_cur_outp;
901 else
902 {
903 port = SCM_COERCE_OUTPORT (port);
904 SCM_VALIDATE_OPOUTPORT (1, port);
905 }
906 scm_flush (port);
907 return SCM_UNSPECIFIED;
908 }
909 #undef FUNC_NAME
910
911 SCM_DEFINE (scm_flush_all_ports, "flush-all-ports", 0, 0, 0,
912 (),
913 "Equivalent to calling @code{force-output} on\n"
914 "all open output ports. The return value is unspecified.")
915 #define FUNC_NAME s_scm_flush_all_ports
916 {
917 size_t i;
918
919 scm_mutex_lock (&scm_i_port_table_mutex);
920 for (i = 0; i < scm_i_port_table_size; i++)
921 {
922 if (SCM_OPOUTPORTP (scm_i_port_table[i]->port))
923 scm_flush (scm_i_port_table[i]->port);
924 }
925 scm_mutex_unlock (&scm_i_port_table_mutex);
926 return SCM_UNSPECIFIED;
927 }
928 #undef FUNC_NAME
929
930 SCM_DEFINE (scm_read_char, "read-char", 0, 1, 0,
931 (SCM port),
932 "Return the next character available from @var{port}, updating\n"
933 "@var{port} to point to the following character. If no more\n"
934 "characters are available, the end-of-file object is returned.")
935 #define FUNC_NAME s_scm_read_char
936 {
937 int c;
938 if (SCM_UNBNDP (port))
939 port = scm_cur_inp;
940 SCM_VALIDATE_OPINPORT (1, port);
941 c = scm_getc (port);
942 if (EOF == c)
943 return SCM_EOF_VAL;
944 return SCM_MAKE_CHAR (c);
945 }
946 #undef FUNC_NAME
947
948 /* this should only be called when the read buffer is empty. it
949 tries to refill the read buffer. it returns the first char from
950 the port, which is either EOF or *(pt->read_pos). */
951 int
952 scm_fill_input (SCM port)
953 {
954 scm_t_port *pt = SCM_PTAB_ENTRY (port);
955
956 if (pt->read_buf == pt->putback_buf)
957 {
958 /* finished reading put-back chars. */
959 pt->read_buf = pt->saved_read_buf;
960 pt->read_pos = pt->saved_read_pos;
961 pt->read_end = pt->saved_read_end;
962 pt->read_buf_size = pt->saved_read_buf_size;
963 if (pt->read_pos < pt->read_end)
964 return *(pt->read_pos);
965 }
966 return scm_ptobs[SCM_PTOBNUM (port)].fill_input (port);
967 }
968
969 int
970 scm_getc (SCM port)
971 {
972 int c;
973 scm_t_port *pt = SCM_PTAB_ENTRY (port);
974
975 if (pt->rw_active == SCM_PORT_WRITE)
976 /* may be marginally faster than calling scm_flush. */
977 scm_ptobs[SCM_PTOBNUM (port)].flush (port);
978
979 if (pt->rw_random)
980 pt->rw_active = SCM_PORT_READ;
981
982 if (pt->read_pos >= pt->read_end)
983 {
984 if (scm_fill_input (port) == EOF)
985 return EOF;
986 }
987
988 c = *(pt->read_pos++);
989
990 switch (c)
991 {
992 case '\n':
993 SCM_INCLINE (port);
994 break;
995 case '\t':
996 SCM_TABCOL (port);
997 break;
998 default:
999 SCM_INCCOL (port);
1000 break;
1001 }
1002
1003 return c;
1004 }
1005
1006 void
1007 scm_putc (char c, SCM port)
1008 {
1009 scm_lfwrite (&c, 1, port);
1010 }
1011
1012 void
1013 scm_puts (const char *s, SCM port)
1014 {
1015 scm_lfwrite (s, strlen (s), port);
1016 }
1017
1018 /* scm_lfwrite
1019 *
1020 * This function differs from scm_c_write; it updates port line and
1021 * column. */
1022
1023 void
1024 scm_lfwrite (const char *ptr, size_t size, SCM port)
1025 {
1026 scm_t_port *pt = SCM_PTAB_ENTRY (port);
1027 scm_t_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (port)];
1028
1029 if (pt->rw_active == SCM_PORT_READ)
1030 scm_end_input (port);
1031
1032 ptob->write (port, ptr, size);
1033
1034 for (; size; ptr++, size--) {
1035 if (*ptr == '\n') {
1036 SCM_INCLINE(port);
1037 }
1038 else if (*ptr == '\t') {
1039 SCM_TABCOL(port);
1040 }
1041 else {
1042 SCM_INCCOL(port);
1043 }
1044 }
1045
1046 if (pt->rw_random)
1047 pt->rw_active = SCM_PORT_WRITE;
1048 }
1049
1050 /* scm_c_read
1051 *
1052 * Used by an application to read arbitrary number of bytes from an
1053 * SCM port. Same semantics as libc read, except that scm_c_read only
1054 * returns less than SIZE bytes if at end-of-file.
1055 *
1056 * Warning: Doesn't update port line and column counts! */
1057
1058 size_t
1059 scm_c_read (SCM port, void *buffer, size_t size)
1060 {
1061 scm_t_port *pt = SCM_PTAB_ENTRY (port);
1062 size_t n_read = 0, n_available;
1063
1064 if (pt->rw_active == SCM_PORT_WRITE)
1065 scm_ptobs[SCM_PTOBNUM (port)].flush (port);
1066
1067 if (pt->rw_random)
1068 pt->rw_active = SCM_PORT_READ;
1069
1070 if (SCM_READ_BUFFER_EMPTY_P (pt))
1071 {
1072 if (scm_fill_input (port) == EOF)
1073 return 0;
1074 }
1075
1076 n_available = pt->read_end - pt->read_pos;
1077
1078 while (n_available < size)
1079 {
1080 memcpy (buffer, pt->read_pos, n_available);
1081 buffer = (char *) buffer + n_available;
1082 pt->read_pos += n_available;
1083 n_read += n_available;
1084
1085 if (SCM_READ_BUFFER_EMPTY_P (pt))
1086 {
1087 if (scm_fill_input (port) == EOF)
1088 return n_read;
1089 }
1090
1091 size -= n_available;
1092 n_available = pt->read_end - pt->read_pos;
1093 }
1094
1095 memcpy (buffer, pt->read_pos, size);
1096 pt->read_pos += size;
1097
1098 return n_read + size;
1099 }
1100
1101 /* scm_c_write
1102 *
1103 * Used by an application to write arbitrary number of bytes to an SCM
1104 * port. Similar semantics as libc write. However, unlike libc
1105 * write, scm_c_write writes the requested number of bytes and has no
1106 * return value.
1107 *
1108 * Warning: Doesn't update port line and column counts!
1109 */
1110
1111 void
1112 scm_c_write (SCM port, const void *ptr, size_t size)
1113 {
1114 scm_t_port *pt = SCM_PTAB_ENTRY (port);
1115 scm_t_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (port)];
1116
1117 if (pt->rw_active == SCM_PORT_READ)
1118 scm_end_input (port);
1119
1120 ptob->write (port, ptr, size);
1121
1122 if (pt->rw_random)
1123 pt->rw_active = SCM_PORT_WRITE;
1124 }
1125
1126 void
1127 scm_flush (SCM port)
1128 {
1129 long i = SCM_PTOBNUM (port);
1130 (scm_ptobs[i].flush) (port);
1131 }
1132
1133 void
1134 scm_end_input (SCM port)
1135 {
1136 long offset;
1137 scm_t_port *pt = SCM_PTAB_ENTRY (port);
1138
1139 if (pt->read_buf == pt->putback_buf)
1140 {
1141 offset = pt->read_end - pt->read_pos;
1142 pt->read_buf = pt->saved_read_buf;
1143 pt->read_pos = pt->saved_read_pos;
1144 pt->read_end = pt->saved_read_end;
1145 pt->read_buf_size = pt->saved_read_buf_size;
1146 }
1147 else
1148 offset = 0;
1149
1150 scm_ptobs[SCM_PTOBNUM (port)].end_input (port, offset);
1151 }
1152
1153 \f
1154
1155
1156 void
1157 scm_ungetc (int c, SCM port)
1158 #define FUNC_NAME "scm_ungetc"
1159 {
1160 scm_t_port *pt = SCM_PTAB_ENTRY (port);
1161
1162 if (pt->read_buf == pt->putback_buf)
1163 /* already using the put-back buffer. */
1164 {
1165 /* enlarge putback_buf if necessary. */
1166 if (pt->read_end == pt->read_buf + pt->read_buf_size
1167 && pt->read_buf == pt->read_pos)
1168 {
1169 size_t new_size = pt->read_buf_size * 2;
1170 unsigned char *tmp = (unsigned char *)
1171 scm_gc_realloc (pt->putback_buf, pt->read_buf_size, new_size,
1172 "putback buffer");
1173
1174 pt->read_pos = pt->read_buf = pt->putback_buf = tmp;
1175 pt->read_end = pt->read_buf + pt->read_buf_size;
1176 pt->read_buf_size = pt->putback_buf_size = new_size;
1177 }
1178
1179 /* shift any existing bytes to buffer + 1. */
1180 if (pt->read_pos == pt->read_end)
1181 pt->read_end = pt->read_buf + 1;
1182 else if (pt->read_pos != pt->read_buf + 1)
1183 {
1184 int count = pt->read_end - pt->read_pos;
1185
1186 memmove (pt->read_buf + 1, pt->read_pos, count);
1187 pt->read_end = pt->read_buf + 1 + count;
1188 }
1189
1190 pt->read_pos = pt->read_buf;
1191 }
1192 else
1193 /* switch to the put-back buffer. */
1194 {
1195 if (pt->putback_buf == NULL)
1196 {
1197 pt->putback_buf
1198 = (unsigned char *) scm_gc_malloc (SCM_INITIAL_PUTBACK_BUF_SIZE,
1199 "putback buffer");
1200 pt->putback_buf_size = SCM_INITIAL_PUTBACK_BUF_SIZE;
1201 }
1202
1203 pt->saved_read_buf = pt->read_buf;
1204 pt->saved_read_pos = pt->read_pos;
1205 pt->saved_read_end = pt->read_end;
1206 pt->saved_read_buf_size = pt->read_buf_size;
1207
1208 pt->read_pos = pt->read_buf = pt->putback_buf;
1209 pt->read_end = pt->read_buf + 1;
1210 pt->read_buf_size = pt->putback_buf_size;
1211 }
1212
1213 *pt->read_buf = c;
1214
1215 if (pt->rw_random)
1216 pt->rw_active = SCM_PORT_READ;
1217
1218 if (c == '\n')
1219 {
1220 /* What should col be in this case?
1221 * We'll leave it at -1.
1222 */
1223 SCM_LINUM (port) -= 1;
1224 }
1225 else
1226 SCM_COL(port) -= 1;
1227 }
1228 #undef FUNC_NAME
1229
1230
1231 void
1232 scm_ungets (const char *s, int n, SCM port)
1233 {
1234 /* This is simple minded and inefficient, but unreading strings is
1235 * probably not a common operation, and remember that line and
1236 * column numbers have to be handled...
1237 *
1238 * Please feel free to write an optimized version!
1239 */
1240 while (n--)
1241 scm_ungetc (s[n], port);
1242 }
1243
1244
1245 SCM_DEFINE (scm_peek_char, "peek-char", 0, 1, 0,
1246 (SCM port),
1247 "Return the next character available from @var{port},\n"
1248 "@emph{without} updating @var{port} to point to the following\n"
1249 "character. If no more characters are available, the\n"
1250 "end-of-file object is returned.\n"
1251 "\n"
1252 "The value returned by\n"
1253 "a call to @code{peek-char} is the same as the value that would\n"
1254 "have been returned by a call to @code{read-char} on the same\n"
1255 "port. The only difference is that the very next call to\n"
1256 "@code{read-char} or @code{peek-char} on that @var{port} will\n"
1257 "return the value returned by the preceding call to\n"
1258 "@code{peek-char}. In particular, a call to @code{peek-char} on\n"
1259 "an interactive port will hang waiting for input whenever a call\n"
1260 "to @code{read-char} would have hung.")
1261 #define FUNC_NAME s_scm_peek_char
1262 {
1263 int c, column;
1264 if (SCM_UNBNDP (port))
1265 port = scm_cur_inp;
1266 else
1267 SCM_VALIDATE_OPINPORT (1, port);
1268 column = SCM_COL(port);
1269 c = scm_getc (port);
1270 if (EOF == c)
1271 return SCM_EOF_VAL;
1272 scm_ungetc (c, port);
1273 SCM_COL(port) = column;
1274 return SCM_MAKE_CHAR (c);
1275 }
1276 #undef FUNC_NAME
1277
1278 SCM_DEFINE (scm_unread_char, "unread-char", 1, 1, 0,
1279 (SCM cobj, SCM port),
1280 "Place @var{char} in @var{port} so that it will be read by the\n"
1281 "next read operation. If called multiple times, the unread characters\n"
1282 "will be read again in last-in first-out order. If @var{port} is\n"
1283 "not supplied, the current input port is used.")
1284 #define FUNC_NAME s_scm_unread_char
1285 {
1286 int c;
1287
1288 SCM_VALIDATE_CHAR (1, cobj);
1289 if (SCM_UNBNDP (port))
1290 port = scm_cur_inp;
1291 else
1292 SCM_VALIDATE_OPINPORT (2, port);
1293
1294 c = SCM_CHAR (cobj);
1295
1296 scm_ungetc (c, port);
1297 return cobj;
1298 }
1299 #undef FUNC_NAME
1300
1301 SCM_DEFINE (scm_unread_string, "unread-string", 2, 0, 0,
1302 (SCM str, SCM port),
1303 "Place the string @var{str} in @var{port} so that its characters will be\n"
1304 "read in subsequent read operations. If called multiple times, the\n"
1305 "unread characters will be read again in last-in first-out order. If\n"
1306 "@var{port} is not supplied, the current-input-port is used.")
1307 #define FUNC_NAME s_scm_unread_string
1308 {
1309 SCM_VALIDATE_STRING (1, str);
1310 if (SCM_UNBNDP (port))
1311 port = scm_cur_inp;
1312 else
1313 SCM_VALIDATE_OPINPORT (2, port);
1314
1315 scm_ungets (SCM_STRING_CHARS (str), SCM_STRING_LENGTH (str), port);
1316
1317 return str;
1318 }
1319 #undef FUNC_NAME
1320
1321 SCM_DEFINE (scm_seek, "seek", 3, 0, 0,
1322 (SCM fd_port, SCM offset, SCM whence),
1323 "Sets the current position of @var{fd/port} to the integer\n"
1324 "@var{offset}, which is interpreted according to the value of\n"
1325 "@var{whence}.\n"
1326 "\n"
1327 "One of the following variables should be supplied for\n"
1328 "@var{whence}:\n"
1329 "@defvar SEEK_SET\n"
1330 "Seek from the beginning of the file.\n"
1331 "@end defvar\n"
1332 "@defvar SEEK_CUR\n"
1333 "Seek from the current position.\n"
1334 "@end defvar\n"
1335 "@defvar SEEK_END\n"
1336 "Seek from the end of the file.\n"
1337 "@end defvar\n"
1338 "If @var{fd/port} is a file descriptor, the underlying system\n"
1339 "call is @code{lseek}. @var{port} may be a string port.\n"
1340 "\n"
1341 "The value returned is the new position in the file. This means\n"
1342 "that the current position of a port can be obtained using:\n"
1343 "@lisp\n"
1344 "(seek port 0 SEEK_CUR)\n"
1345 "@end lisp")
1346 #define FUNC_NAME s_scm_seek
1347 {
1348 off_t off;
1349 off_t rv;
1350 int how;
1351
1352 fd_port = SCM_COERCE_OUTPORT (fd_port);
1353
1354 off = SCM_NUM2LONG (2, offset);
1355 SCM_VALIDATE_INUM_COPY (3, whence, how);
1356 if (how != SEEK_SET && how != SEEK_CUR && how != SEEK_END)
1357 SCM_OUT_OF_RANGE (3, whence);
1358 if (SCM_OPPORTP (fd_port))
1359 {
1360 scm_t_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (fd_port);
1361
1362 if (!ptob->seek)
1363 SCM_MISC_ERROR ("port is not seekable",
1364 scm_cons (fd_port, SCM_EOL));
1365 else
1366 rv = ptob->seek (fd_port, off, how);
1367 }
1368 else /* file descriptor?. */
1369 {
1370 SCM_VALIDATE_INUM (1, fd_port);
1371 rv = lseek (SCM_INUM (fd_port), off, how);
1372 if (rv == -1)
1373 SCM_SYSERROR;
1374 }
1375 return scm_long2num (rv);
1376 }
1377 #undef FUNC_NAME
1378
1379 #ifdef __MINGW32__
1380 /* Define this function since it is not supported under Windows. */
1381 static int truncate (char *file, int length)
1382 {
1383 int ret = -1, fdes;
1384 if ((fdes = open (file, O_BINARY | O_WRONLY)) != -1)
1385 {
1386 ret = chsize (fdes, length);
1387 close (fdes);
1388 }
1389 return ret;
1390 }
1391 #endif /* __MINGW32__ */
1392
1393 SCM_DEFINE (scm_truncate_file, "truncate-file", 1, 1, 0,
1394 (SCM object, SCM length),
1395 "Truncates the object referred to by @var{object} to at most\n"
1396 "@var{length} bytes. @var{object} can be a string containing a\n"
1397 "file name or an integer file descriptor or a port.\n"
1398 "@var{length} may be omitted if @var{object} is not a file name,\n"
1399 "in which case the truncation occurs at the current port.\n"
1400 "position. The return value is unspecified.")
1401 #define FUNC_NAME s_scm_truncate_file
1402 {
1403 int rv;
1404 off_t c_length;
1405
1406 /* object can be a port, fdes or filename. */
1407
1408 if (SCM_UNBNDP (length))
1409 {
1410 /* must supply length if object is a filename. */
1411 if (SCM_STRINGP (object))
1412 SCM_MISC_ERROR("must supply length if OBJECT is a filename", SCM_EOL);
1413
1414 length = scm_seek (object, SCM_INUM0, SCM_I_MAKINUM (SEEK_CUR));
1415 }
1416 c_length = SCM_NUM2LONG (2, length);
1417 if (c_length < 0)
1418 SCM_MISC_ERROR ("negative offset", SCM_EOL);
1419
1420 object = SCM_COERCE_OUTPORT (object);
1421 if (SCM_INUMP (object))
1422 {
1423 SCM_SYSCALL (rv = ftruncate (SCM_INUM (object), c_length));
1424 }
1425 else if (SCM_OPOUTPORTP (object))
1426 {
1427 scm_t_port *pt = SCM_PTAB_ENTRY (object);
1428 scm_t_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (object);
1429
1430 if (!ptob->truncate)
1431 SCM_MISC_ERROR ("port is not truncatable", SCM_EOL);
1432 if (pt->rw_active == SCM_PORT_READ)
1433 scm_end_input (object);
1434 else if (pt->rw_active == SCM_PORT_WRITE)
1435 ptob->flush (object);
1436
1437 ptob->truncate (object, c_length);
1438 rv = 0;
1439 }
1440 else
1441 {
1442 SCM_VALIDATE_STRING (1, object);
1443 SCM_SYSCALL (rv = truncate (SCM_STRING_CHARS (object), c_length));
1444 }
1445 if (rv == -1)
1446 SCM_SYSERROR;
1447 return SCM_UNSPECIFIED;
1448 }
1449 #undef FUNC_NAME
1450
1451 SCM_DEFINE (scm_port_line, "port-line", 1, 0, 0,
1452 (SCM port),
1453 "Return the current line number for @var{port}.\n"
1454 "\n"
1455 "The first line of a file is 0. But you might want to add 1\n"
1456 "when printing line numbers, since starting from 1 is\n"
1457 "traditional in error messages, and likely to be more natural to\n"
1458 "non-programmers.")
1459 #define FUNC_NAME s_scm_port_line
1460 {
1461 port = SCM_COERCE_OUTPORT (port);
1462 SCM_VALIDATE_OPENPORT (1, port);
1463 return SCM_I_MAKINUM (SCM_LINUM (port));
1464 }
1465 #undef FUNC_NAME
1466
1467 SCM_DEFINE (scm_set_port_line_x, "set-port-line!", 2, 0, 0,
1468 (SCM port, SCM line),
1469 "Set the current line number for @var{port} to @var{line}. The\n"
1470 "first line of a file is 0.")
1471 #define FUNC_NAME s_scm_set_port_line_x
1472 {
1473 port = SCM_COERCE_OUTPORT (port);
1474 SCM_VALIDATE_OPENPORT (1, port);
1475 SCM_VALIDATE_INUM (2, line);
1476 SCM_PTAB_ENTRY (port)->line_number = SCM_INUM (line);
1477 return SCM_UNSPECIFIED;
1478 }
1479 #undef FUNC_NAME
1480
1481 SCM_DEFINE (scm_port_column, "port-column", 1, 0, 0,
1482 (SCM port),
1483 "Return the current column number of @var{port}.\n"
1484 "If the number is\n"
1485 "unknown, the result is #f. Otherwise, the result is a 0-origin integer\n"
1486 "- i.e. the first character of the first line is line 0, column 0.\n"
1487 "(However, when you display a file position, for example in an error\n"
1488 "message, we recommend you add 1 to get 1-origin integers. This is\n"
1489 "because lines and column numbers traditionally start with 1, and that is\n"
1490 "what non-programmers will find most natural.)")
1491 #define FUNC_NAME s_scm_port_column
1492 {
1493 port = SCM_COERCE_OUTPORT (port);
1494 SCM_VALIDATE_OPENPORT (1, port);
1495 return SCM_I_MAKINUM (SCM_COL (port));
1496 }
1497 #undef FUNC_NAME
1498
1499 SCM_DEFINE (scm_set_port_column_x, "set-port-column!", 2, 0, 0,
1500 (SCM port, SCM column),
1501 "Set the current column of @var{port}. Before reading the first\n"
1502 "character on a line the column should be 0.")
1503 #define FUNC_NAME s_scm_set_port_column_x
1504 {
1505 port = SCM_COERCE_OUTPORT (port);
1506 SCM_VALIDATE_OPENPORT (1, port);
1507 SCM_VALIDATE_INUM (2, column);
1508 SCM_PTAB_ENTRY (port)->column_number = SCM_INUM (column);
1509 return SCM_UNSPECIFIED;
1510 }
1511 #undef FUNC_NAME
1512
1513 SCM_DEFINE (scm_port_filename, "port-filename", 1, 0, 0,
1514 (SCM port),
1515 "Return the filename associated with @var{port}. This function returns\n"
1516 "the strings \"standard input\", \"standard output\" and \"standard error\"\n"
1517 "when called on the current input, output and error ports respectively.")
1518 #define FUNC_NAME s_scm_port_filename
1519 {
1520 port = SCM_COERCE_OUTPORT (port);
1521 SCM_VALIDATE_OPENPORT (1, port);
1522 return SCM_FILENAME (port);
1523 }
1524 #undef FUNC_NAME
1525
1526 SCM_DEFINE (scm_set_port_filename_x, "set-port-filename!", 2, 0, 0,
1527 (SCM port, SCM filename),
1528 "Change the filename associated with @var{port}, using the current input\n"
1529 "port if none is specified. Note that this does not change the port's\n"
1530 "source of data, but only the value that is returned by\n"
1531 "@code{port-filename} and reported in diagnostic output.")
1532 #define FUNC_NAME s_scm_set_port_filename_x
1533 {
1534 port = SCM_COERCE_OUTPORT (port);
1535 SCM_VALIDATE_OPENPORT (1, port);
1536 /* We allow the user to set the filename to whatever he likes. */
1537 SCM_SET_FILENAME (port, filename);
1538 return SCM_UNSPECIFIED;
1539 }
1540 #undef FUNC_NAME
1541
1542 #ifndef ttyname
1543 extern char * ttyname();
1544 #endif
1545
1546 void
1547 scm_print_port_mode (SCM exp, SCM port)
1548 {
1549 scm_puts (SCM_CLOSEDP (exp)
1550 ? "closed: "
1551 : (SCM_RDNG & SCM_CELL_WORD_0 (exp)
1552 ? (SCM_WRTNG & SCM_CELL_WORD_0 (exp)
1553 ? "input-output: "
1554 : "input: ")
1555 : (SCM_WRTNG & SCM_CELL_WORD_0 (exp)
1556 ? "output: "
1557 : "bogus: ")),
1558 port);
1559 }
1560
1561 int
1562 scm_port_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
1563 {
1564 char *type = SCM_PTOBNAME (SCM_PTOBNUM (exp));
1565 if (!type)
1566 type = "port";
1567 scm_puts ("#<", port);
1568 scm_print_port_mode (exp, port);
1569 scm_puts (type, port);
1570 scm_putc (' ', port);
1571 scm_intprint (SCM_CELL_WORD_1 (exp), 16, port);
1572 scm_putc ('>', port);
1573 return 1;
1574 }
1575
1576 void
1577 scm_ports_prehistory ()
1578 {
1579 scm_numptob = 0;
1580 scm_ptobs = (scm_t_ptob_descriptor *) scm_malloc (sizeof (scm_t_ptob_descriptor));
1581 }
1582
1583 \f
1584
1585 /* Void ports. */
1586
1587 scm_t_bits scm_tc16_void_port = 0;
1588
1589 static int fill_input_void_port (SCM port SCM_UNUSED)
1590 {
1591 return EOF;
1592 }
1593
1594 static void
1595 write_void_port (SCM port SCM_UNUSED,
1596 const void *data SCM_UNUSED,
1597 size_t size SCM_UNUSED)
1598 {
1599 }
1600
1601 SCM
1602 scm_void_port (char *mode_str)
1603 {
1604 scm_mutex_lock (&scm_i_port_table_mutex);
1605 {
1606 int mode_bits = scm_mode_bits (mode_str);
1607 SCM answer = scm_new_port_table_entry (scm_tc16_void_port);
1608 scm_t_port * pt = SCM_PTAB_ENTRY(answer);
1609
1610 scm_port_non_buffer (pt);
1611
1612 SCM_SETSTREAM (answer, 0);
1613 SCM_SET_CELL_TYPE (answer, scm_tc16_void_port | mode_bits);
1614 scm_mutex_unlock (&scm_i_port_table_mutex);
1615 return answer;
1616 }
1617 }
1618
1619 SCM_DEFINE (scm_sys_make_void_port, "%make-void-port", 1, 0, 0,
1620 (SCM mode),
1621 "Create and return a new void port. A void port acts like\n"
1622 "@file{/dev/null}. The @var{mode} argument\n"
1623 "specifies the input/output modes for this port: see the\n"
1624 "documentation for @code{open-file} in @ref{File Ports}.")
1625 #define FUNC_NAME s_scm_sys_make_void_port
1626 {
1627 SCM_VALIDATE_STRING (1, mode);
1628 return scm_void_port (SCM_STRING_CHARS (mode));
1629 }
1630 #undef FUNC_NAME
1631
1632 \f
1633 /* Initialization. */
1634
1635 void
1636 scm_init_ports ()
1637 {
1638 /* lseek() symbols. */
1639 scm_c_define ("SEEK_SET", SCM_I_MAKINUM (SEEK_SET));
1640 scm_c_define ("SEEK_CUR", SCM_I_MAKINUM (SEEK_CUR));
1641 scm_c_define ("SEEK_END", SCM_I_MAKINUM (SEEK_END));
1642
1643 scm_tc16_void_port = scm_make_port_type ("void", fill_input_void_port,
1644 write_void_port);
1645 #include "libguile/ports.x"
1646 }
1647
1648 /*
1649 Local Variables:
1650 c-file-style: "gnu"
1651 End:
1652 */