ports: avoid adding port table entries and finalizers if possible
[bpt/guile.git] / libguile / ports.c
1 /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004,
2 * 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
18 */
19
20
21 \f
22 /* Headers. */
23
24 #define _LARGEFILE64_SOURCE /* ask for stat64 etc */
25
26 #ifdef HAVE_CONFIG_H
27 # include <config.h>
28 #endif
29
30 #include <stdio.h>
31 #include <errno.h>
32 #include <fcntl.h> /* for chsize on mingw */
33 #include <assert.h>
34 #include <iconv.h>
35 #include <uniconv.h>
36 #include <unistr.h>
37 #include <striconveh.h>
38
39 #include <assert.h>
40
41 #include "libguile/_scm.h"
42 #include "libguile/async.h"
43 #include "libguile/deprecation.h"
44 #include "libguile/eval.h"
45 #include "libguile/fports.h" /* direct access for seek and truncate */
46 #include "libguile/goops.h"
47 #include "libguile/smob.h"
48 #include "libguile/chars.h"
49 #include "libguile/dynwind.h"
50
51 #include "libguile/keywords.h"
52 #include "libguile/hashtab.h"
53 #include "libguile/root.h"
54 #include "libguile/strings.h"
55 #include "libguile/mallocs.h"
56 #include "libguile/validate.h"
57 #include "libguile/ports.h"
58 #include "libguile/vectors.h"
59 #include "libguile/weak-set.h"
60 #include "libguile/fluids.h"
61 #include "libguile/eq.h"
62
63 #ifdef HAVE_STRING_H
64 #include <string.h>
65 #endif
66
67 #ifdef HAVE_IO_H
68 #include <io.h>
69 #endif
70
71 #ifdef HAVE_UNISTD_H
72 #include <unistd.h>
73 #endif
74
75 #ifdef HAVE_SYS_IOCTL_H
76 #include <sys/ioctl.h>
77 #endif
78
79 /* Mingw (version 3.4.5, circa 2006) has ftruncate as an alias for chsize
80 already, but have this code here in case that wasn't so in past versions,
81 or perhaps to help other minimal DOS environments.
82
83 gnulib ftruncate.c has code using fcntl F_CHSIZE and F_FREESP, which
84 might be possibilities if we've got other systems without ftruncate. */
85
86 #if defined HAVE_CHSIZE && ! defined HAVE_FTRUNCATE
87 #define ftruncate(fd, size) chsize (fd, size)
88 #undef HAVE_FTRUNCATE
89 #define HAVE_FTRUNCATE 1
90 #endif
91
92 \f
93 /* The port kind table --- a dynamically resized array of port types. */
94
95
96 /* scm_ptobs scm_numptob
97 * implement a dynamically resized array of ptob records.
98 * Indexes into this table are used when generating type
99 * tags for smobjects (if you know a tag you can get an index and conversely).
100 */
101 static scm_t_ptob_descriptor **scm_ptobs = NULL;
102 static long scm_numptob = 0; /* Number of port types. */
103 static long scm_ptobs_size = 0; /* Number of slots in the port type
104 table. */
105 static scm_i_pthread_mutex_t scm_ptobs_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
106
107 long
108 scm_c_num_port_types (void)
109 {
110 long ret;
111
112 scm_i_pthread_mutex_lock (&scm_ptobs_lock);
113 ret = scm_numptob;
114 scm_i_pthread_mutex_unlock (&scm_ptobs_lock);
115
116 return ret;
117 }
118
119 scm_t_ptob_descriptor*
120 scm_c_port_type_ref (long ptobnum)
121 {
122 scm_t_ptob_descriptor *ret = NULL;
123
124 scm_i_pthread_mutex_lock (&scm_ptobs_lock);
125
126 if (0 <= ptobnum && ptobnum < scm_numptob)
127 ret = scm_ptobs[ptobnum];
128
129 scm_i_pthread_mutex_unlock (&scm_ptobs_lock);
130
131 if (!ret)
132 scm_out_of_range ("scm_c_port_type_ref", scm_from_long (ptobnum));
133
134 return ret;
135 }
136
137 long
138 scm_c_port_type_add_x (scm_t_ptob_descriptor *desc)
139 {
140 long ret = -1;
141
142 scm_i_pthread_mutex_lock (&scm_ptobs_lock);
143
144 if (scm_numptob + 1 < SCM_I_MAX_PORT_TYPE_COUNT)
145 {
146 if (scm_numptob == scm_ptobs_size)
147 {
148 unsigned long old_size = scm_ptobs_size;
149 scm_t_ptob_descriptor **old_ptobs = scm_ptobs;
150
151 /* Currently there are only 9 predefined port types, so one
152 resize will cover it. */
153 scm_ptobs_size = old_size + 10;
154
155 if (scm_ptobs_size >= SCM_I_MAX_PORT_TYPE_COUNT)
156 scm_ptobs_size = SCM_I_MAX_PORT_TYPE_COUNT;
157
158 scm_ptobs = scm_gc_malloc (sizeof (*scm_ptobs) * scm_ptobs_size,
159 "scm_ptobs");
160
161 memcpy (scm_ptobs, old_ptobs, sizeof (*scm_ptobs) * scm_numptob);
162 }
163
164 ret = scm_numptob++;
165 scm_ptobs[ret] = desc;
166 }
167
168 scm_i_pthread_mutex_unlock (&scm_ptobs_lock);
169
170 if (ret < 0)
171 scm_out_of_range ("scm_c_port_type_add_x", scm_from_long (scm_numptob));
172
173 return ret;
174 }
175
176 /*
177 * We choose to use an interface similar to the smob interface with
178 * fill_input and write as standard fields, passed to the port
179 * type constructor, and optional fields set by setters.
180 */
181
182 static void
183 flush_port_default (SCM port SCM_UNUSED)
184 {
185 }
186
187 static void
188 end_input_default (SCM port SCM_UNUSED, int offset SCM_UNUSED)
189 {
190 }
191
192 scm_t_bits
193 scm_make_port_type (char *name,
194 int (*fill_input) (SCM port),
195 void (*write) (SCM port, const void *data, size_t size))
196 {
197 scm_t_ptob_descriptor *desc;
198 long ptobnum;
199
200 desc = scm_gc_malloc_pointerless (sizeof (*desc), "port-type");
201 memset (desc, 0, sizeof (*desc));
202
203 desc->name = name;
204 desc->print = scm_port_print;
205 desc->write = write;
206 desc->flush = flush_port_default;
207 desc->end_input = end_input_default;
208 desc->fill_input = fill_input;
209
210 ptobnum = scm_c_port_type_add_x (desc);
211
212 /* Make a class object if GOOPS is present. */
213 if (SCM_UNPACK (scm_port_class[0]) != 0)
214 scm_make_port_classes (ptobnum, name);
215
216 return scm_tc7_port + ptobnum * 256;
217 }
218
219 void
220 scm_set_port_mark (scm_t_bits tc, SCM (*mark) (SCM))
221 {
222 scm_c_port_type_ref (SCM_TC2PTOBNUM (tc))->mark = mark;
223 }
224
225 void
226 scm_set_port_free (scm_t_bits tc, size_t (*free) (SCM))
227 {
228 scm_c_port_type_ref (SCM_TC2PTOBNUM (tc))->free = free;
229 }
230
231 void
232 scm_set_port_print (scm_t_bits tc, int (*print) (SCM exp, SCM port,
233 scm_print_state *pstate))
234 {
235 scm_c_port_type_ref (SCM_TC2PTOBNUM (tc))->print = print;
236 }
237
238 void
239 scm_set_port_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM))
240 {
241 scm_c_port_type_ref (SCM_TC2PTOBNUM (tc))->equalp = equalp;
242 }
243
244 void
245 scm_set_port_close (scm_t_bits tc, int (*close) (SCM))
246 {
247 scm_c_port_type_ref (SCM_TC2PTOBNUM (tc))->close = close;
248 }
249
250 void
251 scm_set_port_flush (scm_t_bits tc, void (*flush) (SCM port))
252 {
253 scm_t_ptob_descriptor *ptob = scm_c_port_type_ref (SCM_TC2PTOBNUM (tc));
254 ptob->flush = flush;
255 ptob->flags |= SCM_PORT_TYPE_HAS_FLUSH;
256 }
257
258 void
259 scm_set_port_end_input (scm_t_bits tc, void (*end_input) (SCM port, int offset))
260 {
261 scm_c_port_type_ref (SCM_TC2PTOBNUM (tc))->end_input = end_input;
262 }
263
264 void
265 scm_set_port_seek (scm_t_bits tc, scm_t_off (*seek) (SCM, scm_t_off, int))
266 {
267 scm_c_port_type_ref (SCM_TC2PTOBNUM (tc))->seek = seek;
268 }
269
270 void
271 scm_set_port_truncate (scm_t_bits tc, void (*truncate) (SCM, scm_t_off))
272 {
273 scm_c_port_type_ref (SCM_TC2PTOBNUM (tc))->truncate = truncate;
274 }
275
276 void
277 scm_set_port_input_waiting (scm_t_bits tc, int (*input_waiting) (SCM))
278 {
279 scm_c_port_type_ref (SCM_TC2PTOBNUM (tc))->input_waiting = input_waiting;
280 }
281
282 \f
283
284 /* Standard ports --- current input, output, error, and more(!). */
285
286 static SCM cur_inport_fluid = SCM_BOOL_F;
287 static SCM cur_outport_fluid = SCM_BOOL_F;
288 static SCM cur_errport_fluid = SCM_BOOL_F;
289 static SCM cur_loadport_fluid = SCM_BOOL_F;
290
291 SCM_DEFINE (scm_current_input_port, "current-input-port", 0, 0, 0,
292 (),
293 "Return the current input port. This is the default port used\n"
294 "by many input procedures. Initially, @code{current-input-port}\n"
295 "returns the @dfn{standard input} in Unix and C terminology.")
296 #define FUNC_NAME s_scm_current_input_port
297 {
298 if (scm_is_true (cur_inport_fluid))
299 return scm_fluid_ref (cur_inport_fluid);
300 else
301 return SCM_BOOL_F;
302 }
303 #undef FUNC_NAME
304
305 SCM_DEFINE (scm_current_output_port, "current-output-port", 0, 0, 0,
306 (),
307 "Return the current output port. This is the default port used\n"
308 "by many output procedures. Initially,\n"
309 "@code{current-output-port} returns the @dfn{standard output} in\n"
310 "Unix and C terminology.")
311 #define FUNC_NAME s_scm_current_output_port
312 {
313 if (scm_is_true (cur_outport_fluid))
314 return scm_fluid_ref (cur_outport_fluid);
315 else
316 return SCM_BOOL_F;
317 }
318 #undef FUNC_NAME
319
320 SCM_DEFINE (scm_current_error_port, "current-error-port", 0, 0, 0,
321 (),
322 "Return the port to which errors and warnings should be sent (the\n"
323 "@dfn{standard error} in Unix and C terminology).")
324 #define FUNC_NAME s_scm_current_error_port
325 {
326 if (scm_is_true (cur_errport_fluid))
327 return scm_fluid_ref (cur_errport_fluid);
328 else
329 return SCM_BOOL_F;
330 }
331 #undef FUNC_NAME
332
333 SCM
334 scm_current_warning_port (void)
335 {
336 static SCM cwp_var = SCM_BOOL_F;
337
338 if (scm_is_false (cwp_var))
339 cwp_var = scm_c_private_lookup ("guile", "current-warning-port");
340
341 return scm_call_0 (scm_variable_ref (cwp_var));
342 }
343
344 SCM_DEFINE (scm_current_load_port, "current-load-port", 0, 0, 0,
345 (),
346 "Return the current-load-port.\n"
347 "The load port is used internally by @code{primitive-load}.")
348 #define FUNC_NAME s_scm_current_load_port
349 {
350 return scm_fluid_ref (cur_loadport_fluid);
351 }
352 #undef FUNC_NAME
353
354 SCM_DEFINE (scm_set_current_input_port, "set-current-input-port", 1, 0, 0,
355 (SCM port),
356 "@deffnx {Scheme Procedure} set-current-output-port port\n"
357 "@deffnx {Scheme Procedure} set-current-error-port port\n"
358 "Change the ports returned by @code{current-input-port},\n"
359 "@code{current-output-port} and @code{current-error-port}, respectively,\n"
360 "so that they use the supplied @var{port} for input or output.")
361 #define FUNC_NAME s_scm_set_current_input_port
362 {
363 SCM oinp = scm_fluid_ref (cur_inport_fluid);
364 SCM_VALIDATE_OPINPORT (1, port);
365 scm_fluid_set_x (cur_inport_fluid, port);
366 return oinp;
367 }
368 #undef FUNC_NAME
369
370
371 SCM_DEFINE (scm_set_current_output_port, "set-current-output-port", 1, 0, 0,
372 (SCM port),
373 "Set the current default output port to @var{port}.")
374 #define FUNC_NAME s_scm_set_current_output_port
375 {
376 SCM ooutp = scm_fluid_ref (cur_outport_fluid);
377 port = SCM_COERCE_OUTPORT (port);
378 SCM_VALIDATE_OPOUTPORT (1, port);
379 scm_fluid_set_x (cur_outport_fluid, port);
380 return ooutp;
381 }
382 #undef FUNC_NAME
383
384
385 SCM_DEFINE (scm_set_current_error_port, "set-current-error-port", 1, 0, 0,
386 (SCM port),
387 "Set the current default error port to @var{port}.")
388 #define FUNC_NAME s_scm_set_current_error_port
389 {
390 SCM oerrp = scm_fluid_ref (cur_errport_fluid);
391 port = SCM_COERCE_OUTPORT (port);
392 SCM_VALIDATE_OPOUTPORT (1, port);
393 scm_fluid_set_x (cur_errport_fluid, port);
394 return oerrp;
395 }
396 #undef FUNC_NAME
397
398
399 SCM
400 scm_set_current_warning_port (SCM port)
401 {
402 static SCM cwp_var = SCM_BOOL_F;
403
404 if (scm_is_false (cwp_var))
405 cwp_var = scm_c_private_lookup ("guile", "current-warning-port");
406
407 return scm_call_1 (scm_variable_ref (cwp_var), port);
408 }
409
410
411 void
412 scm_dynwind_current_input_port (SCM port)
413 #define FUNC_NAME NULL
414 {
415 SCM_VALIDATE_OPINPORT (1, port);
416 scm_dynwind_fluid (cur_inport_fluid, port);
417 }
418 #undef FUNC_NAME
419
420 void
421 scm_dynwind_current_output_port (SCM port)
422 #define FUNC_NAME NULL
423 {
424 port = SCM_COERCE_OUTPORT (port);
425 SCM_VALIDATE_OPOUTPORT (1, port);
426 scm_dynwind_fluid (cur_outport_fluid, port);
427 }
428 #undef FUNC_NAME
429
430 void
431 scm_dynwind_current_error_port (SCM port)
432 #define FUNC_NAME NULL
433 {
434 port = SCM_COERCE_OUTPORT (port);
435 SCM_VALIDATE_OPOUTPORT (1, port);
436 scm_dynwind_fluid (cur_errport_fluid, port);
437 }
438 #undef FUNC_NAME
439
440 void
441 scm_i_dynwind_current_load_port (SCM port)
442 {
443 scm_dynwind_fluid (cur_loadport_fluid, port);
444 }
445
446
447 \f
448
449 /* Retrieving a port's mode. */
450
451 /* Return the flags that characterize a port based on the mode
452 * string used to open a file for that port.
453 *
454 * See PORT FLAGS in scm.h
455 */
456
457 static long
458 scm_i_mode_bits_n (SCM modes)
459 {
460 return (SCM_OPN
461 | (scm_i_string_contains_char (modes, 'r')
462 || scm_i_string_contains_char (modes, '+') ? SCM_RDNG : 0)
463 | (scm_i_string_contains_char (modes, 'w')
464 || scm_i_string_contains_char (modes, 'a')
465 || scm_i_string_contains_char (modes, '+') ? SCM_WRTNG : 0)
466 | (scm_i_string_contains_char (modes, '0') ? SCM_BUF0 : 0)
467 | (scm_i_string_contains_char (modes, 'l') ? SCM_BUFLINE : 0));
468 }
469
470 long
471 scm_mode_bits (char *modes)
472 {
473 return scm_i_mode_bits (scm_from_locale_string (modes));
474 }
475
476 long
477 scm_i_mode_bits (SCM modes)
478 {
479 long bits;
480
481 if (!scm_is_string (modes))
482 scm_wrong_type_arg_msg (NULL, 0, modes, "string");
483
484 bits = scm_i_mode_bits_n (modes);
485 scm_remember_upto_here_1 (modes);
486 return bits;
487 }
488
489 /* Return the mode flags from an open port.
490 * Some modes such as "append" are only used when opening
491 * a file and are not returned here. */
492
493 SCM_DEFINE (scm_port_mode, "port-mode", 1, 0, 0,
494 (SCM port),
495 "Return the port modes associated with the open port @var{port}.\n"
496 "These will not necessarily be identical to the modes used when\n"
497 "the port was opened, since modes such as \"append\" which are\n"
498 "used only during port creation are not retained.")
499 #define FUNC_NAME s_scm_port_mode
500 {
501 char modes[4];
502 modes[0] = '\0';
503
504 port = SCM_COERCE_OUTPORT (port);
505 SCM_VALIDATE_OPPORT (1, port);
506 if (SCM_CELL_WORD_0 (port) & SCM_RDNG) {
507 if (SCM_CELL_WORD_0 (port) & SCM_WRTNG)
508 strcpy (modes, "r+");
509 else
510 strcpy (modes, "r");
511 }
512 else if (SCM_CELL_WORD_0 (port) & SCM_WRTNG)
513 strcpy (modes, "w");
514 if (SCM_CELL_WORD_0 (port) & SCM_BUF0)
515 strcat (modes, "0");
516
517 return scm_from_latin1_string (modes);
518 }
519 #undef FUNC_NAME
520
521
522 \f
523
524 /* The port table --- a weak set of all ports.
525
526 We need a global registry of ports to flush them all at exit, and to
527 get all the ports matching a file descriptor. */
528 SCM scm_i_port_weak_set;
529
530
531 \f
532
533 /* Port finalization. */
534
535 static void finalize_port (GC_PTR, GC_PTR);
536
537 /* Register a finalizer for PORT. */
538 static SCM_C_INLINE_KEYWORD void
539 register_finalizer_for_port (SCM port)
540 {
541 GC_finalization_proc prev_finalizer;
542 GC_PTR prev_finalization_data;
543
544 /* Register a finalizer for PORT so that its iconv CDs get freed and
545 optionally its type's `free' function gets called. */
546 GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (port), finalize_port, 0,
547 &prev_finalizer,
548 &prev_finalization_data);
549 }
550
551 /* Finalize the object (a port) pointed to by PTR. */
552 static void
553 finalize_port (GC_PTR ptr, GC_PTR data)
554 {
555 SCM port = SCM_PACK_POINTER (ptr);
556
557 if (!SCM_PORTP (port))
558 abort ();
559
560 if (SCM_OPENP (port))
561 {
562 if (SCM_REVEALED (port) > 0)
563 /* Keep "revealed" ports alive and re-register a finalizer. */
564 register_finalizer_for_port (port);
565 else
566 {
567 scm_t_ptob_descriptor *ptob = SCM_PORT_DESCRIPTOR (port);
568
569 if (ptob->free)
570 /* Yes, I really do mean `free' rather than `close'. `close'
571 is for explicit `close-port' by user. */
572 ptob->free (port);
573
574 SCM_SETSTREAM (port, 0);
575 SCM_CLR_PORT_OPEN_FLAG (port);
576
577 scm_gc_ports_collected++;
578 }
579 }
580 }
581
582
583 \f
584
585 SCM
586 scm_c_make_port_with_encoding (scm_t_bits tag, unsigned long mode_bits,
587 const char *encoding,
588 scm_t_string_failed_conversion_handler handler,
589 scm_t_bits stream)
590 {
591 SCM ret;
592 scm_t_port *entry;
593 scm_t_ptob_descriptor *ptob;
594
595 entry = (scm_t_port *) scm_gc_calloc (sizeof (scm_t_port), "port");
596 ptob = scm_c_port_type_ref (SCM_TC2PTOBNUM (tag));
597
598 ret = scm_words (tag | mode_bits, 3);
599 SCM_SET_CELL_WORD_1 (ret, (scm_t_bits) entry);
600 SCM_SET_CELL_WORD_2 (ret, (scm_t_bits) ptob);
601
602 entry->lock = scm_gc_malloc_pointerless (sizeof (*entry->lock), "port lock");
603 scm_i_pthread_mutex_init (entry->lock, scm_i_pthread_mutexattr_recursive);
604
605 entry->file_name = SCM_BOOL_F;
606 entry->rw_active = SCM_PORT_NEITHER;
607 entry->port = ret;
608 entry->stream = stream;
609 entry->encoding = encoding ? scm_gc_strdup (encoding, "port") : NULL;
610 if (encoding && strcmp (encoding, "UTF-8") == 0)
611 entry->encoding_mode = SCM_PORT_ENCODING_MODE_UTF8;
612 else
613 entry->encoding_mode = SCM_PORT_ENCODING_MODE_ICONV;
614 entry->ilseq_handler = handler;
615 entry->iconv_descriptors = NULL;
616
617 if (SCM_PORT_DESCRIPTOR (ret)->flags & SCM_PORT_TYPE_HAS_FLUSH)
618 scm_weak_set_add_x (scm_i_port_weak_set, ret);
619
620 if (SCM_PORT_DESCRIPTOR (ret)->free)
621 register_finalizer_for_port (ret);
622
623 return ret;
624 }
625
626 SCM
627 scm_c_make_port (scm_t_bits tag, unsigned long mode_bits, scm_t_bits stream)
628 {
629 return scm_c_make_port_with_encoding (tag, mode_bits,
630 scm_i_default_port_encoding (),
631 scm_i_get_conversion_strategy (SCM_BOOL_F),
632 stream);
633 }
634
635 SCM
636 scm_new_port_table_entry (scm_t_bits tag)
637 {
638 return scm_c_make_port (tag, 0, 0);
639 }
640
641 \f
642
643 /* Predicates. */
644
645 SCM_DEFINE (scm_port_p, "port?", 1, 0, 0,
646 (SCM x),
647 "Return a boolean indicating whether @var{x} is a port.\n"
648 "Equivalent to @code{(or (input-port? @var{x}) (output-port?\n"
649 "@var{x}))}.")
650 #define FUNC_NAME s_scm_port_p
651 {
652 return scm_from_bool (SCM_PORTP (x));
653 }
654 #undef FUNC_NAME
655
656 SCM_DEFINE (scm_input_port_p, "input-port?", 1, 0, 0,
657 (SCM x),
658 "Return @code{#t} if @var{x} is an input port, otherwise return\n"
659 "@code{#f}. Any object satisfying this predicate also satisfies\n"
660 "@code{port?}.")
661 #define FUNC_NAME s_scm_input_port_p
662 {
663 return scm_from_bool (SCM_INPUT_PORT_P (x));
664 }
665 #undef FUNC_NAME
666
667 SCM_DEFINE (scm_output_port_p, "output-port?", 1, 0, 0,
668 (SCM x),
669 "Return @code{#t} if @var{x} is an output port, otherwise return\n"
670 "@code{#f}. Any object satisfying this predicate also satisfies\n"
671 "@code{port?}.")
672 #define FUNC_NAME s_scm_output_port_p
673 {
674 x = SCM_COERCE_OUTPORT (x);
675 return scm_from_bool (SCM_OUTPUT_PORT_P (x));
676 }
677 #undef FUNC_NAME
678
679 SCM_DEFINE (scm_port_closed_p, "port-closed?", 1, 0, 0,
680 (SCM port),
681 "Return @code{#t} if @var{port} is closed or @code{#f} if it is\n"
682 "open.")
683 #define FUNC_NAME s_scm_port_closed_p
684 {
685 SCM_VALIDATE_PORT (1, port);
686 return scm_from_bool (!SCM_OPPORTP (port));
687 }
688 #undef FUNC_NAME
689
690 SCM_DEFINE (scm_eof_object_p, "eof-object?", 1, 0, 0,
691 (SCM x),
692 "Return @code{#t} if @var{x} is an end-of-file object; otherwise\n"
693 "return @code{#f}.")
694 #define FUNC_NAME s_scm_eof_object_p
695 {
696 return scm_from_bool (SCM_EOF_OBJECT_P (x));
697 }
698 #undef FUNC_NAME
699
700
701 \f
702
703 /* Closing ports. */
704
705 static void close_iconv_descriptors (scm_t_iconv_descriptors *id);
706
707 /* scm_close_port
708 * Call the close operation on a port object.
709 * see also scm_close.
710 */
711 SCM_DEFINE (scm_close_port, "close-port", 1, 0, 0,
712 (SCM port),
713 "Close the specified port object. Return @code{#t} if it\n"
714 "successfully closes a port or @code{#f} if it was already\n"
715 "closed. An exception may be raised if an error occurs, for\n"
716 "example when flushing buffered output. See also @ref{Ports and\n"
717 "File Descriptors, close}, for a procedure which can close file\n"
718 "descriptors.")
719 #define FUNC_NAME s_scm_close_port
720 {
721 scm_t_port *p;
722 int rv;
723
724 port = SCM_COERCE_OUTPORT (port);
725
726 SCM_VALIDATE_PORT (1, port);
727 if (SCM_CLOSEDP (port))
728 return SCM_BOOL_F;
729 if (SCM_PORT_DESCRIPTOR (port)->close)
730 rv = SCM_PORT_DESCRIPTOR (port)->close (port);
731 else
732 rv = 0;
733
734 p = SCM_PTAB_ENTRY (port);
735
736 scm_port_non_buffer (p);
737 SCM_SETPTAB_ENTRY (port, 0);
738
739 if (SCM_PORT_DESCRIPTOR (port)->flags & SCM_PORT_TYPE_HAS_FLUSH)
740 scm_weak_set_remove_x (scm_i_port_weak_set, port);
741
742 p->putback_buf = NULL;
743 p->putback_buf_size = 0;
744
745 if (p->iconv_descriptors)
746 {
747 close_iconv_descriptors (p->iconv_descriptors);
748 p->iconv_descriptors = NULL;
749 }
750
751 SCM_CLR_PORT_OPEN_FLAG (port);
752
753 return scm_from_bool (rv >= 0);
754 }
755 #undef FUNC_NAME
756
757 SCM_DEFINE (scm_close_input_port, "close-input-port", 1, 0, 0,
758 (SCM port),
759 "Close the specified input port object. The routine has no effect if\n"
760 "the file has already been closed. An exception may be raised if an\n"
761 "error occurs. The value returned is unspecified.\n\n"
762 "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
763 "which can close file descriptors.")
764 #define FUNC_NAME s_scm_close_input_port
765 {
766 SCM_VALIDATE_INPUT_PORT (1, port);
767 scm_close_port (port);
768 return SCM_UNSPECIFIED;
769 }
770 #undef FUNC_NAME
771
772 SCM_DEFINE (scm_close_output_port, "close-output-port", 1, 0, 0,
773 (SCM port),
774 "Close the specified output port object. The routine has no effect if\n"
775 "the file has already been closed. An exception may be raised if an\n"
776 "error occurs. The value returned is unspecified.\n\n"
777 "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
778 "which can close file descriptors.")
779 #define FUNC_NAME s_scm_close_output_port
780 {
781 port = SCM_COERCE_OUTPORT (port);
782 SCM_VALIDATE_OUTPUT_PORT (1, port);
783 scm_close_port (port);
784 return SCM_UNSPECIFIED;
785 }
786 #undef FUNC_NAME
787
788
789 \f
790
791 /* Encoding characters to byte streams, and decoding byte streams to
792 characters. */
793
794 /* A fluid specifying the default encoding for newly created ports. If it is
795 a string, that is the encoding. If it is #f, it is in the "native"
796 (Latin-1) encoding. */
797 SCM_VARIABLE (default_port_encoding_var, "%default-port-encoding");
798
799 static int scm_port_encoding_init = 0;
800
801 /* Use ENCODING as the default encoding for future ports. */
802 void
803 scm_i_set_default_port_encoding (const char *encoding)
804 {
805 if (!scm_port_encoding_init
806 || !scm_is_fluid (SCM_VARIABLE_REF (default_port_encoding_var)))
807 scm_misc_error (NULL, "tried to set port encoding fluid before it is initialized",
808 SCM_EOL);
809
810 if (encoding == NULL
811 || !strcmp (encoding, "ASCII")
812 || !strcmp (encoding, "ANSI_X3.4-1968")
813 || !strcmp (encoding, "ISO-8859-1"))
814 scm_fluid_set_x (SCM_VARIABLE_REF (default_port_encoding_var), SCM_BOOL_F);
815 else
816 scm_fluid_set_x (SCM_VARIABLE_REF (default_port_encoding_var),
817 scm_from_locale_string (encoding));
818 }
819
820 /* Return the name of the default encoding for newly created ports; a
821 return value of NULL means "ISO-8859-1". */
822 const char *
823 scm_i_default_port_encoding (void)
824 {
825 if (!scm_port_encoding_init)
826 return NULL;
827 else if (!scm_is_fluid (SCM_VARIABLE_REF (default_port_encoding_var)))
828 return NULL;
829 else
830 {
831 SCM encoding;
832
833 encoding = scm_fluid_ref (SCM_VARIABLE_REF (default_port_encoding_var));
834 if (!scm_is_string (encoding))
835 return NULL;
836 else
837 return scm_i_string_chars (encoding);
838 }
839 }
840
841 static void
842 finalize_iconv_descriptors (GC_PTR ptr, GC_PTR data)
843 {
844 close_iconv_descriptors (ptr);
845 }
846
847 static scm_t_iconv_descriptors *
848 open_iconv_descriptors (const char *encoding, int reading, int writing)
849 {
850 scm_t_iconv_descriptors *id;
851 iconv_t input_cd, output_cd;
852
853 input_cd = (iconv_t) -1;
854 output_cd = (iconv_t) -1;
855
856 if (reading)
857 {
858 /* Open an input iconv conversion descriptor, from ENCODING
859 to UTF-8. We choose UTF-8, not UTF-32, because iconv
860 implementations can typically convert from anything to
861 UTF-8, but not to UTF-32 (see
862 <http://lists.gnu.org/archive/html/bug-libunistring/2010-09/msg00007.html>). */
863
864 /* Assume opening an iconv descriptor causes about 16 KB of
865 allocation. */
866 scm_gc_register_allocation (16 * 1024);
867
868 input_cd = iconv_open ("UTF-8", encoding);
869 if (input_cd == (iconv_t) -1)
870 goto invalid_encoding;
871 }
872
873 if (writing)
874 {
875 /* Assume opening an iconv descriptor causes about 16 KB of
876 allocation. */
877 scm_gc_register_allocation (16 * 1024);
878
879 output_cd = iconv_open (encoding, "UTF-8");
880 if (output_cd == (iconv_t) -1)
881 {
882 if (input_cd != (iconv_t) -1)
883 iconv_close (input_cd);
884 goto invalid_encoding;
885 }
886 }
887
888 id = scm_gc_malloc_pointerless (sizeof (*id), "iconv descriptors");
889 id->input_cd = input_cd;
890 id->output_cd = output_cd;
891
892 {
893 GC_finalization_proc prev_finalizer;
894 GC_PTR prev_finalization_data;
895
896 /* Register a finalizer to close the descriptors. */
897 GC_REGISTER_FINALIZER_NO_ORDER (id, finalize_iconv_descriptors, 0,
898 &prev_finalizer, &prev_finalization_data);
899 }
900
901 return id;
902
903 invalid_encoding:
904 {
905 SCM err;
906 err = scm_from_locale_string (encoding);
907 scm_misc_error ("open_iconv_descriptors",
908 "invalid or unknown character encoding ~s",
909 scm_list_1 (err));
910 }
911 }
912
913 static void
914 close_iconv_descriptors (scm_t_iconv_descriptors *id)
915 {
916 if (id->input_cd != (iconv_t) -1)
917 iconv_close (id->input_cd);
918 if (id->output_cd != (iconv_t) -1)
919 iconv_close (id->output_cd);
920 id->input_cd = (void *) -1;
921 id->output_cd = (void *) -1;
922 }
923
924 scm_t_iconv_descriptors *
925 scm_i_port_iconv_descriptors (SCM port)
926 {
927 scm_t_port *pt;
928
929 pt = SCM_PTAB_ENTRY (port);
930
931 assert (pt->encoding_mode == SCM_PORT_ENCODING_MODE_ICONV);
932
933 if (!pt->iconv_descriptors)
934 {
935 if (!pt->encoding)
936 pt->encoding = "ISO-8859-1";
937 pt->iconv_descriptors =
938 open_iconv_descriptors (pt->encoding,
939 SCM_INPUT_PORT_P (port),
940 SCM_OUTPUT_PORT_P (port));
941 }
942
943 return pt->iconv_descriptors;
944 }
945
946 void
947 scm_i_set_port_encoding_x (SCM port, const char *encoding)
948 {
949 scm_t_port *pt;
950 scm_t_iconv_descriptors *prev;
951
952 /* Set the character encoding for this port. */
953 pt = SCM_PTAB_ENTRY (port);
954 prev = pt->iconv_descriptors;
955
956 if (encoding == NULL)
957 encoding = "ISO-8859-1";
958
959 if (strcmp (encoding, "UTF-8") == 0)
960 {
961 pt->encoding = "UTF-8";
962 pt->encoding_mode = SCM_PORT_ENCODING_MODE_UTF8;
963 pt->iconv_descriptors = NULL;
964 }
965 else
966 {
967 /* Open descriptors before mutating the port. */
968 pt->iconv_descriptors =
969 open_iconv_descriptors (encoding,
970 SCM_INPUT_PORT_P (port),
971 SCM_OUTPUT_PORT_P (port));
972 pt->encoding = scm_gc_strdup (encoding, "port");
973 pt->encoding_mode = SCM_PORT_ENCODING_MODE_ICONV;
974 }
975
976 if (prev)
977 close_iconv_descriptors (prev);
978 }
979
980 SCM_DEFINE (scm_port_encoding, "port-encoding", 1, 0, 0,
981 (SCM port),
982 "Returns, as a string, the character encoding that @var{port}\n"
983 "uses to interpret its input and output.\n")
984 #define FUNC_NAME s_scm_port_encoding
985 {
986 scm_t_port *pt;
987 const char *enc;
988
989 SCM_VALIDATE_PORT (1, port);
990
991 pt = SCM_PTAB_ENTRY (port);
992 enc = pt->encoding;
993 if (enc)
994 return scm_from_locale_string (pt->encoding);
995 else
996 return SCM_BOOL_F;
997 }
998 #undef FUNC_NAME
999
1000 SCM_DEFINE (scm_set_port_encoding_x, "set-port-encoding!", 2, 0, 0,
1001 (SCM port, SCM enc),
1002 "Sets the character encoding that will be used to interpret all\n"
1003 "port I/O. New ports are created with the encoding\n"
1004 "appropriate for the current locale if @code{setlocale} has \n"
1005 "been called or ISO-8859-1 otherwise\n"
1006 "and this procedure can be used to modify that encoding.\n")
1007 #define FUNC_NAME s_scm_set_port_encoding_x
1008 {
1009 char *enc_str;
1010
1011 SCM_VALIDATE_PORT (1, port);
1012 SCM_VALIDATE_STRING (2, enc);
1013
1014 enc_str = scm_to_locale_string (enc);
1015 scm_i_set_port_encoding_x (port, enc_str);
1016 free (enc_str);
1017
1018 return SCM_UNSPECIFIED;
1019 }
1020 #undef FUNC_NAME
1021
1022
1023 /* This determines how conversions handle unconvertible characters. */
1024 SCM_GLOBAL_VARIABLE (scm_conversion_strategy, "%port-conversion-strategy");
1025 static int scm_conversion_strategy_init = 0;
1026
1027 scm_t_string_failed_conversion_handler
1028 scm_i_get_conversion_strategy (SCM port)
1029 {
1030 SCM encoding;
1031
1032 if (scm_is_false (port))
1033 {
1034 if (!scm_conversion_strategy_init
1035 || !scm_is_fluid (SCM_VARIABLE_REF (scm_conversion_strategy)))
1036 return SCM_FAILED_CONVERSION_QUESTION_MARK;
1037 else
1038 {
1039 encoding = scm_fluid_ref (SCM_VARIABLE_REF (scm_conversion_strategy));
1040 if (scm_is_false (encoding))
1041 return SCM_FAILED_CONVERSION_QUESTION_MARK;
1042 else
1043 return (scm_t_string_failed_conversion_handler) scm_to_int (encoding);
1044 }
1045 }
1046 else
1047 {
1048 scm_t_port *pt;
1049 pt = SCM_PTAB_ENTRY (port);
1050 return pt->ilseq_handler;
1051 }
1052
1053 }
1054
1055 void
1056 scm_i_set_conversion_strategy_x (SCM port,
1057 scm_t_string_failed_conversion_handler handler)
1058 {
1059 SCM strategy;
1060 scm_t_port *pt;
1061
1062 strategy = scm_from_int ((int) handler);
1063
1064 if (scm_is_false (port))
1065 {
1066 /* Set the default encoding for future ports. */
1067 if (!scm_conversion_strategy_init
1068 || !scm_is_fluid (SCM_VARIABLE_REF (scm_conversion_strategy)))
1069 scm_misc_error (NULL, "tried to set conversion strategy fluid before it is initialized",
1070 SCM_EOL);
1071 scm_fluid_set_x (SCM_VARIABLE_REF (scm_conversion_strategy), strategy);
1072 }
1073 else
1074 {
1075 /* Set the character encoding for this port. */
1076 pt = SCM_PTAB_ENTRY (port);
1077 pt->ilseq_handler = handler;
1078 }
1079 }
1080
1081 SCM_DEFINE (scm_port_conversion_strategy, "port-conversion-strategy",
1082 1, 0, 0, (SCM port),
1083 "Returns the behavior of the port when handling a character that\n"
1084 "is not representable in the port's current encoding.\n"
1085 "It returns the symbol @code{error} if unrepresentable characters\n"
1086 "should cause exceptions, @code{substitute} if the port should\n"
1087 "try to replace unrepresentable characters with question marks or\n"
1088 "approximate characters, or @code{escape} if unrepresentable\n"
1089 "characters should be converted to string escapes.\n"
1090 "\n"
1091 "If @var{port} is @code{#f}, then the current default behavior\n"
1092 "will be returned. New ports will have this default behavior\n"
1093 "when they are created.\n")
1094 #define FUNC_NAME s_scm_port_conversion_strategy
1095 {
1096 scm_t_string_failed_conversion_handler h;
1097
1098 SCM_VALIDATE_OPPORT (1, port);
1099
1100 if (!scm_is_false (port))
1101 {
1102 SCM_VALIDATE_OPPORT (1, port);
1103 }
1104
1105 h = scm_i_get_conversion_strategy (port);
1106 if (h == SCM_FAILED_CONVERSION_ERROR)
1107 return scm_from_latin1_symbol ("error");
1108 else if (h == SCM_FAILED_CONVERSION_QUESTION_MARK)
1109 return scm_from_latin1_symbol ("substitute");
1110 else if (h == SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE)
1111 return scm_from_latin1_symbol ("escape");
1112 else
1113 abort ();
1114
1115 /* Never gets here. */
1116 return SCM_UNDEFINED;
1117 }
1118 #undef FUNC_NAME
1119
1120 SCM_DEFINE (scm_set_port_conversion_strategy_x, "set-port-conversion-strategy!",
1121 2, 0, 0,
1122 (SCM port, SCM sym),
1123 "Sets the behavior of the interpreter when outputting a character\n"
1124 "that is not representable in the port's current encoding.\n"
1125 "@var{sym} can be either @code{'error}, @code{'substitute}, or\n"
1126 "@code{'escape}. If it is @code{'error}, an error will be thrown\n"
1127 "when an unconvertible character is encountered. If it is\n"
1128 "@code{'substitute}, then unconvertible characters will \n"
1129 "be replaced with approximate characters, or with question marks\n"
1130 "if no approximately correct character is available.\n"
1131 "If it is @code{'escape},\n"
1132 "it will appear as a hex escape when output.\n"
1133 "\n"
1134 "If @var{port} is an open port, the conversion error behavior\n"
1135 "is set for that port. If it is @code{#f}, it is set as the\n"
1136 "default behavior for any future ports that get created in\n"
1137 "this thread.\n")
1138 #define FUNC_NAME s_scm_set_port_conversion_strategy_x
1139 {
1140 SCM err;
1141 SCM qm;
1142 SCM esc;
1143
1144 if (!scm_is_false (port))
1145 {
1146 SCM_VALIDATE_OPPORT (1, port);
1147 }
1148
1149 err = scm_from_latin1_symbol ("error");
1150 if (scm_is_true (scm_eqv_p (sym, err)))
1151 {
1152 scm_i_set_conversion_strategy_x (port, SCM_FAILED_CONVERSION_ERROR);
1153 return SCM_UNSPECIFIED;
1154 }
1155
1156 qm = scm_from_latin1_symbol ("substitute");
1157 if (scm_is_true (scm_eqv_p (sym, qm)))
1158 {
1159 scm_i_set_conversion_strategy_x (port,
1160 SCM_FAILED_CONVERSION_QUESTION_MARK);
1161 return SCM_UNSPECIFIED;
1162 }
1163
1164 esc = scm_from_latin1_symbol ("escape");
1165 if (scm_is_true (scm_eqv_p (sym, esc)))
1166 {
1167 scm_i_set_conversion_strategy_x (port,
1168 SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE);
1169 return SCM_UNSPECIFIED;
1170 }
1171
1172 SCM_MISC_ERROR ("unknown conversion behavior ~s", scm_list_1 (sym));
1173
1174 return SCM_UNSPECIFIED;
1175 }
1176 #undef FUNC_NAME
1177
1178
1179 \f
1180
1181 /* The port lock. */
1182
1183 static void
1184 lock_port (void *mutex)
1185 {
1186 scm_i_pthread_mutex_lock (mutex);
1187 }
1188
1189 static void
1190 unlock_port (void *mutex)
1191 {
1192 scm_i_pthread_mutex_unlock (mutex);
1193 }
1194
1195 void
1196 scm_dynwind_lock_port (SCM port)
1197 #define FUNC_NAME "dynwind-lock-port"
1198 {
1199 scm_i_pthread_mutex_t *lock;
1200 SCM_VALIDATE_OPPORT (SCM_ARG1, port);
1201 scm_c_lock_port (port, &lock);
1202 if (lock)
1203 {
1204 scm_dynwind_unwind_handler (unlock_port, lock, SCM_F_WIND_EXPLICITLY);
1205 scm_dynwind_rewind_handler (lock_port, lock, 0);
1206 }
1207 }
1208 #undef FUNC_NAME
1209
1210
1211 \f
1212
1213 /* Revealed counts --- an oddity inherited from SCSH. */
1214
1215 /* Find a port in the table and return its revealed count.
1216 Also used by the garbage collector.
1217 */
1218 int
1219 scm_revealed_count (SCM port)
1220 {
1221 scm_i_pthread_mutex_t *lock;
1222 int ret;
1223
1224 scm_c_lock_port (port, &lock);
1225 ret = SCM_REVEALED (port);
1226 if (lock)
1227 scm_i_pthread_mutex_unlock (lock);
1228
1229 return ret;
1230 }
1231
1232 SCM_DEFINE (scm_port_revealed, "port-revealed", 1, 0, 0,
1233 (SCM port),
1234 "Return the revealed count for @var{port}.")
1235 #define FUNC_NAME s_scm_port_revealed
1236 {
1237 port = SCM_COERCE_OUTPORT (port);
1238 SCM_VALIDATE_OPENPORT (1, port);
1239 return scm_from_int (scm_revealed_count (port));
1240 }
1241 #undef FUNC_NAME
1242
1243 /* Set the revealed count for a port. */
1244 SCM_DEFINE (scm_set_port_revealed_x, "set-port-revealed!", 2, 0, 0,
1245 (SCM port, SCM rcount),
1246 "Sets the revealed count for a port to a given value.\n"
1247 "The return value is unspecified.")
1248 #define FUNC_NAME s_scm_set_port_revealed_x
1249 {
1250 int r;
1251 scm_i_pthread_mutex_t *lock;
1252
1253 /* FIXME: It doesn't make sense to manipulate revealed counts on ports
1254 without a free function. */
1255
1256 port = SCM_COERCE_OUTPORT (port);
1257 SCM_VALIDATE_OPENPORT (1, port);
1258 r = scm_to_int (rcount);
1259 scm_c_lock_port (port, &lock);
1260 SCM_REVEALED (port) = r;
1261 if (lock)
1262 scm_i_pthread_mutex_unlock (lock);
1263 return SCM_UNSPECIFIED;
1264 }
1265 #undef FUNC_NAME
1266
1267 /* Set the revealed count for a port. */
1268 SCM_DEFINE (scm_adjust_port_revealed_x, "adjust-port-revealed!", 2, 0, 0,
1269 (SCM port, SCM addend),
1270 "Add @var{addend} to the revealed count of @var{port}.\n"
1271 "The return value is unspecified.")
1272 #define FUNC_NAME s_scm_adjust_port_revealed_x
1273 {
1274 scm_i_pthread_mutex_t *lock;
1275 int a;
1276 port = SCM_COERCE_OUTPORT (port);
1277 SCM_VALIDATE_OPENPORT (1, port);
1278 a = scm_to_int (addend);
1279 scm_c_lock_port (port, &lock);
1280 SCM_REVEALED (port) += a;
1281 if (lock)
1282 scm_i_pthread_mutex_unlock (lock);
1283 return SCM_UNSPECIFIED;
1284 }
1285 #undef FUNC_NAME
1286
1287
1288 \f
1289
1290 /* Input. */
1291
1292 int
1293 scm_get_byte_or_eof (SCM port)
1294 {
1295 scm_i_pthread_mutex_t *lock;
1296 int ret;
1297
1298 scm_c_lock_port (port, &lock);
1299 ret = scm_get_byte_or_eof_unlocked (port);
1300 if (lock)
1301 scm_i_pthread_mutex_unlock (lock);
1302
1303 return ret;
1304 }
1305
1306 int
1307 scm_peek_byte_or_eof (SCM port)
1308 {
1309 scm_i_pthread_mutex_t *lock;
1310 int ret;
1311
1312 scm_c_lock_port (port, &lock);
1313 ret = scm_peek_byte_or_eof_unlocked (port);
1314 if (lock)
1315 scm_i_pthread_mutex_unlock (lock);
1316
1317 return ret;
1318 }
1319
1320 /* scm_c_read
1321 *
1322 * Used by an application to read arbitrary number of bytes from an
1323 * SCM port. Same semantics as libc read, except that scm_c_read only
1324 * returns less than SIZE bytes if at end-of-file.
1325 *
1326 * Warning: Doesn't update port line and column counts! */
1327
1328 /* This structure, and the following swap_buffer function, are used
1329 for temporarily swapping a port's own read buffer, and the buffer
1330 that the caller of scm_c_read provides. */
1331 struct port_and_swap_buffer
1332 {
1333 scm_t_port *pt;
1334 unsigned char *buffer;
1335 size_t size;
1336 };
1337
1338 static void
1339 swap_buffer (void *data)
1340 {
1341 struct port_and_swap_buffer *psb = (struct port_and_swap_buffer *) data;
1342 unsigned char *old_buf = psb->pt->read_buf;
1343 size_t old_size = psb->pt->read_buf_size;
1344
1345 /* Make the port use (buffer, size) from the struct. */
1346 psb->pt->read_pos = psb->pt->read_buf = psb->pt->read_end = psb->buffer;
1347 psb->pt->read_buf_size = psb->size;
1348
1349 /* Save the port's old (buffer, size) in the struct. */
1350 psb->buffer = old_buf;
1351 psb->size = old_size;
1352 }
1353
1354 size_t
1355 scm_c_read_unlocked (SCM port, void *buffer, size_t size)
1356 #define FUNC_NAME "scm_c_read"
1357 {
1358 scm_t_port *pt;
1359 size_t n_read = 0, n_available;
1360 struct port_and_swap_buffer psb;
1361
1362 SCM_VALIDATE_OPINPORT (1, port);
1363
1364 pt = SCM_PTAB_ENTRY (port);
1365 if (pt->rw_active == SCM_PORT_WRITE)
1366 SCM_PORT_DESCRIPTOR (port)->flush (port);
1367
1368 if (pt->rw_random)
1369 pt->rw_active = SCM_PORT_READ;
1370
1371 /* Take bytes first from the port's read buffer. */
1372 if (pt->read_pos < pt->read_end)
1373 {
1374 n_available = min (size, pt->read_end - pt->read_pos);
1375 memcpy (buffer, pt->read_pos, n_available);
1376 buffer = (char *) buffer + n_available;
1377 pt->read_pos += n_available;
1378 n_read += n_available;
1379 size -= n_available;
1380 }
1381
1382 /* Avoid the scm_dynwind_* costs if we now have enough data. */
1383 if (size == 0)
1384 return n_read;
1385
1386 /* Now we will call scm_fill_input repeatedly until we have read the
1387 requested number of bytes. (Note that a single scm_fill_input
1388 call does not guarantee to fill the whole of the port's read
1389 buffer.) */
1390 if (pt->read_buf_size <= 1 && pt->encoding == NULL)
1391 {
1392 /* The port that we are reading from is unbuffered - i.e. does
1393 not have its own persistent buffer - but we have a buffer,
1394 provided by our caller, that is the right size for the data
1395 that is wanted. For the following scm_fill_input calls,
1396 therefore, we use the buffer in hand as the port's read
1397 buffer.
1398
1399 We need to make sure that the port's normal (1 byte) buffer
1400 is reinstated in case one of the scm_fill_input () calls
1401 throws an exception; we use the scm_dynwind_* API to achieve
1402 that.
1403
1404 A consequence of this optimization is that the fill_input
1405 functions can't unget characters. That'll push data to the
1406 pushback buffer instead of this psb buffer. */
1407 #if SCM_DEBUG == 1
1408 unsigned char *pback = pt->putback_buf;
1409 #endif
1410 psb.pt = pt;
1411 psb.buffer = buffer;
1412 psb.size = size;
1413 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
1414 scm_dynwind_rewind_handler (swap_buffer, &psb, SCM_F_WIND_EXPLICITLY);
1415 scm_dynwind_unwind_handler (swap_buffer, &psb, SCM_F_WIND_EXPLICITLY);
1416
1417 /* Call scm_fill_input until we have all the bytes that we need,
1418 or we hit EOF. */
1419 while (pt->read_buf_size && (scm_fill_input_unlocked (port) != EOF))
1420 {
1421 pt->read_buf_size -= (pt->read_end - pt->read_pos);
1422 pt->read_pos = pt->read_buf = pt->read_end;
1423 }
1424 #if SCM_DEBUG == 1
1425 if (pback != pt->putback_buf
1426 || pt->read_buf - (unsigned char *) buffer < 0)
1427 scm_misc_error (FUNC_NAME,
1428 "scm_c_read must not call a fill function that pushes "
1429 "back characters onto an unbuffered port", SCM_EOL);
1430 #endif
1431 n_read += pt->read_buf - (unsigned char *) buffer;
1432
1433 /* Reinstate the port's normal buffer. */
1434 scm_dynwind_end ();
1435 }
1436 else
1437 {
1438 /* The port has its own buffer. It is important that we use it,
1439 even if it happens to be smaller than our caller's buffer, so
1440 that a custom port implementation's entry points (in
1441 particular, fill_input) can rely on the buffer always being
1442 the same as they first set up. */
1443 while (size && (scm_fill_input_unlocked (port) != EOF))
1444 {
1445 n_available = min (size, pt->read_end - pt->read_pos);
1446 memcpy (buffer, pt->read_pos, n_available);
1447 buffer = (char *) buffer + n_available;
1448 pt->read_pos += n_available;
1449 n_read += n_available;
1450 size -= n_available;
1451 }
1452 }
1453
1454 return n_read;
1455 }
1456 #undef FUNC_NAME
1457
1458 size_t
1459 scm_c_read (SCM port, void *buffer, size_t size)
1460 {
1461 scm_i_pthread_mutex_t *lock;
1462 size_t ret;
1463
1464 scm_c_lock_port (port, &lock);
1465 ret = scm_c_read_unlocked (port, buffer, size);
1466 if (lock)
1467 scm_i_pthread_mutex_unlock (lock);
1468
1469
1470 return ret;
1471 }
1472
1473 /* Update the line and column number of PORT after consumption of C. */
1474 static inline void
1475 update_port_lf (scm_t_wchar c, SCM port)
1476 {
1477 switch (c)
1478 {
1479 case '\a':
1480 case EOF:
1481 break;
1482 case '\b':
1483 SCM_DECCOL (port);
1484 break;
1485 case '\n':
1486 SCM_INCLINE (port);
1487 break;
1488 case '\r':
1489 SCM_ZEROCOL (port);
1490 break;
1491 case '\t':
1492 SCM_TABCOL (port);
1493 break;
1494 default:
1495 SCM_INCCOL (port);
1496 break;
1497 }
1498 }
1499
1500 #define SCM_MBCHAR_BUF_SIZE (4)
1501
1502 /* Convert the SIZE-byte UTF-8 sequence in UTF8_BUF to a codepoint.
1503 UTF8_BUF is assumed to contain a valid UTF-8 sequence. */
1504 static scm_t_wchar
1505 utf8_to_codepoint (const scm_t_uint8 *utf8_buf, size_t size)
1506 {
1507 scm_t_wchar codepoint;
1508
1509 if (utf8_buf[0] <= 0x7f)
1510 {
1511 assert (size == 1);
1512 codepoint = utf8_buf[0];
1513 }
1514 else if ((utf8_buf[0] & 0xe0) == 0xc0)
1515 {
1516 assert (size == 2);
1517 codepoint = ((scm_t_wchar) utf8_buf[0] & 0x1f) << 6UL
1518 | (utf8_buf[1] & 0x3f);
1519 }
1520 else if ((utf8_buf[0] & 0xf0) == 0xe0)
1521 {
1522 assert (size == 3);
1523 codepoint = ((scm_t_wchar) utf8_buf[0] & 0x0f) << 12UL
1524 | ((scm_t_wchar) utf8_buf[1] & 0x3f) << 6UL
1525 | (utf8_buf[2] & 0x3f);
1526 }
1527 else
1528 {
1529 assert (size == 4);
1530 codepoint = ((scm_t_wchar) utf8_buf[0] & 0x07) << 18UL
1531 | ((scm_t_wchar) utf8_buf[1] & 0x3f) << 12UL
1532 | ((scm_t_wchar) utf8_buf[2] & 0x3f) << 6UL
1533 | (utf8_buf[3] & 0x3f);
1534 }
1535
1536 return codepoint;
1537 }
1538
1539 /* Read a UTF-8 sequence from PORT. On success, return 0 and set
1540 *CODEPOINT to the codepoint that was read, fill BUF with its UTF-8
1541 representation, and set *LEN to the length in bytes. Return
1542 `EILSEQ' on error. */
1543 static int
1544 get_utf8_codepoint (SCM port, scm_t_wchar *codepoint,
1545 scm_t_uint8 buf[SCM_MBCHAR_BUF_SIZE], size_t *len)
1546 {
1547 #define ASSERT_NOT_EOF(b) \
1548 if (SCM_UNLIKELY ((b) == EOF)) \
1549 goto invalid_seq
1550 #define CONSUME_PEEKED_BYTE() \
1551 pt->read_pos++
1552
1553 int byte;
1554 scm_t_port *pt;
1555
1556 *len = 0;
1557 pt = SCM_PTAB_ENTRY (port);
1558
1559 byte = scm_get_byte_or_eof_unlocked (port);
1560 if (byte == EOF)
1561 {
1562 *codepoint = EOF;
1563 return 0;
1564 }
1565
1566 buf[0] = (scm_t_uint8) byte;
1567 *len = 1;
1568
1569 if (buf[0] <= 0x7f)
1570 /* 1-byte form. */
1571 *codepoint = buf[0];
1572 else if (buf[0] >= 0xc2 && buf[0] <= 0xdf)
1573 {
1574 /* 2-byte form. */
1575 byte = scm_peek_byte_or_eof_unlocked (port);
1576 ASSERT_NOT_EOF (byte);
1577
1578 if (SCM_UNLIKELY ((byte & 0xc0) != 0x80))
1579 goto invalid_seq;
1580
1581 CONSUME_PEEKED_BYTE ();
1582 buf[1] = (scm_t_uint8) byte;
1583 *len = 2;
1584
1585 *codepoint = ((scm_t_wchar) buf[0] & 0x1f) << 6UL
1586 | (buf[1] & 0x3f);
1587 }
1588 else if ((buf[0] & 0xf0) == 0xe0)
1589 {
1590 /* 3-byte form. */
1591 byte = scm_peek_byte_or_eof_unlocked (port);
1592 ASSERT_NOT_EOF (byte);
1593
1594 if (SCM_UNLIKELY ((byte & 0xc0) != 0x80
1595 || (buf[0] == 0xe0 && byte < 0xa0)
1596 || (buf[0] == 0xed && byte > 0x9f)))
1597 goto invalid_seq;
1598
1599 CONSUME_PEEKED_BYTE ();
1600 buf[1] = (scm_t_uint8) byte;
1601 *len = 2;
1602
1603 byte = scm_peek_byte_or_eof_unlocked (port);
1604 ASSERT_NOT_EOF (byte);
1605
1606 if (SCM_UNLIKELY ((byte & 0xc0) != 0x80))
1607 goto invalid_seq;
1608
1609 CONSUME_PEEKED_BYTE ();
1610 buf[2] = (scm_t_uint8) byte;
1611 *len = 3;
1612
1613 *codepoint = ((scm_t_wchar) buf[0] & 0x0f) << 12UL
1614 | ((scm_t_wchar) buf[1] & 0x3f) << 6UL
1615 | (buf[2] & 0x3f);
1616 }
1617 else if (buf[0] >= 0xf0 && buf[0] <= 0xf4)
1618 {
1619 /* 4-byte form. */
1620 byte = scm_peek_byte_or_eof_unlocked (port);
1621 ASSERT_NOT_EOF (byte);
1622
1623 if (SCM_UNLIKELY (((byte & 0xc0) != 0x80)
1624 || (buf[0] == 0xf0 && byte < 0x90)
1625 || (buf[0] == 0xf4 && byte > 0x8f)))
1626 goto invalid_seq;
1627
1628 CONSUME_PEEKED_BYTE ();
1629 buf[1] = (scm_t_uint8) byte;
1630 *len = 2;
1631
1632 byte = scm_peek_byte_or_eof_unlocked (port);
1633 ASSERT_NOT_EOF (byte);
1634
1635 if (SCM_UNLIKELY ((byte & 0xc0) != 0x80))
1636 goto invalid_seq;
1637
1638 CONSUME_PEEKED_BYTE ();
1639 buf[2] = (scm_t_uint8) byte;
1640 *len = 3;
1641
1642 byte = scm_peek_byte_or_eof_unlocked (port);
1643 ASSERT_NOT_EOF (byte);
1644
1645 if (SCM_UNLIKELY ((byte & 0xc0) != 0x80))
1646 goto invalid_seq;
1647
1648 CONSUME_PEEKED_BYTE ();
1649 buf[3] = (scm_t_uint8) byte;
1650 *len = 4;
1651
1652 *codepoint = ((scm_t_wchar) buf[0] & 0x07) << 18UL
1653 | ((scm_t_wchar) buf[1] & 0x3f) << 12UL
1654 | ((scm_t_wchar) buf[2] & 0x3f) << 6UL
1655 | (buf[3] & 0x3f);
1656 }
1657 else
1658 goto invalid_seq;
1659
1660 return 0;
1661
1662 invalid_seq:
1663 /* Here we could choose the consume the faulty byte when it's not a
1664 valid starting byte, but it's not a requirement. What Section 3.9
1665 of Unicode 6.0.0 mandates, though, is to not consume a byte that
1666 would otherwise be a valid starting byte. */
1667
1668 return EILSEQ;
1669
1670 #undef CONSUME_PEEKED_BYTE
1671 #undef ASSERT_NOT_EOF
1672 }
1673
1674 /* Likewise, read a byte sequence from PORT, passing it through its
1675 input conversion descriptor. */
1676 static int
1677 get_iconv_codepoint (SCM port, scm_t_wchar *codepoint,
1678 char buf[SCM_MBCHAR_BUF_SIZE], size_t *len)
1679 {
1680 scm_t_iconv_descriptors *id;
1681 int err, byte_read;
1682 size_t bytes_consumed, output_size;
1683 char *output;
1684 scm_t_uint8 utf8_buf[SCM_MBCHAR_BUF_SIZE];
1685
1686 id = scm_i_port_iconv_descriptors (port);
1687
1688 for (output_size = 0, output = (char *) utf8_buf,
1689 bytes_consumed = 0, err = 0;
1690 err == 0 && output_size == 0
1691 && (bytes_consumed == 0 || byte_read != EOF);
1692 bytes_consumed++)
1693 {
1694 char *input;
1695 size_t input_left, output_left, done;
1696
1697 byte_read = scm_get_byte_or_eof_unlocked (port);
1698 if (byte_read == EOF)
1699 {
1700 if (bytes_consumed == 0)
1701 {
1702 *codepoint = (scm_t_wchar) EOF;
1703 *len = 0;
1704 return 0;
1705 }
1706 else
1707 continue;
1708 }
1709
1710 buf[bytes_consumed] = byte_read;
1711
1712 input = buf;
1713 input_left = bytes_consumed + 1;
1714 output_left = sizeof (utf8_buf);
1715
1716 done = iconv (id->input_cd, &input, &input_left, &output, &output_left);
1717 if (done == (size_t) -1)
1718 {
1719 err = errno;
1720 if (err == EINVAL)
1721 /* Missing input: keep trying. */
1722 err = 0;
1723 }
1724 else
1725 output_size = sizeof (utf8_buf) - output_left;
1726 }
1727
1728 if (SCM_UNLIKELY (output_size == 0))
1729 /* An unterminated sequence. */
1730 err = EILSEQ;
1731 else if (SCM_LIKELY (err == 0))
1732 {
1733 /* Convert the UTF8_BUF sequence to a Unicode code point. */
1734 *codepoint = utf8_to_codepoint (utf8_buf, output_size);
1735 *len = bytes_consumed;
1736 }
1737
1738 return err;
1739 }
1740
1741 /* Read a codepoint from PORT and return it in *CODEPOINT. Fill BUF
1742 with the byte representation of the codepoint in PORT's encoding, and
1743 set *LEN to the length in bytes of that representation. Return 0 on
1744 success and an errno value on error. */
1745 static int
1746 get_codepoint (SCM port, scm_t_wchar *codepoint,
1747 char buf[SCM_MBCHAR_BUF_SIZE], size_t *len)
1748 {
1749 int err;
1750 scm_t_port *pt = SCM_PTAB_ENTRY (port);
1751
1752 if (pt->encoding_mode == SCM_PORT_ENCODING_MODE_UTF8)
1753 err = get_utf8_codepoint (port, codepoint, (scm_t_uint8 *) buf, len);
1754 else
1755 err = get_iconv_codepoint (port, codepoint, buf, len);
1756
1757 if (SCM_LIKELY (err == 0))
1758 update_port_lf (*codepoint, port);
1759 else if (pt->ilseq_handler == SCM_ICONVEH_QUESTION_MARK)
1760 {
1761 *codepoint = '?';
1762 err = 0;
1763 update_port_lf (*codepoint, port);
1764 }
1765
1766 return err;
1767 }
1768
1769 /* Read a codepoint from PORT and return it. */
1770 scm_t_wchar
1771 scm_getc_unlocked (SCM port)
1772 #define FUNC_NAME "scm_getc"
1773 {
1774 int err;
1775 size_t len;
1776 scm_t_wchar codepoint;
1777 char buf[SCM_MBCHAR_BUF_SIZE];
1778
1779 err = get_codepoint (port, &codepoint, buf, &len);
1780 if (SCM_UNLIKELY (err != 0))
1781 /* At this point PORT should point past the invalid encoding, as per
1782 R6RS-lib Section 8.2.4. */
1783 scm_decoding_error (FUNC_NAME, err, "input decoding error", port);
1784
1785 return codepoint;
1786 }
1787 #undef FUNC_NAME
1788
1789 scm_t_wchar
1790 scm_getc (SCM port)
1791 {
1792 scm_i_pthread_mutex_t *lock;
1793 scm_t_wchar ret;
1794
1795 scm_c_lock_port (port, &lock);
1796 ret = scm_getc_unlocked (port);
1797 if (lock)
1798 scm_i_pthread_mutex_unlock (lock);
1799
1800
1801 return ret;
1802 }
1803
1804 SCM_DEFINE (scm_read_char, "read-char", 0, 1, 0,
1805 (SCM port),
1806 "Return the next character available from @var{port}, updating\n"
1807 "@var{port} to point to the following character. If no more\n"
1808 "characters are available, the end-of-file object is returned.\n"
1809 "\n"
1810 "When @var{port}'s data cannot be decoded according to its\n"
1811 "character encoding, a @code{decoding-error} is raised and\n"
1812 "@var{port} points past the erroneous byte sequence.\n")
1813 #define FUNC_NAME s_scm_read_char
1814 {
1815 scm_t_wchar c;
1816 if (SCM_UNBNDP (port))
1817 port = scm_current_input_port ();
1818 SCM_VALIDATE_OPINPORT (1, port);
1819 c = scm_getc_unlocked (port);
1820 if (EOF == c)
1821 return SCM_EOF_VAL;
1822 return SCM_MAKE_CHAR (c);
1823 }
1824 #undef FUNC_NAME
1825
1826
1827 \f
1828
1829 /* Pushback. */
1830
1831 void
1832 scm_unget_byte_unlocked (int c, SCM port)
1833 #define FUNC_NAME "scm_unget_byte"
1834 {
1835 scm_t_port *pt = SCM_PTAB_ENTRY (port);
1836
1837 if (pt->read_buf == pt->putback_buf)
1838 /* already using the put-back buffer. */
1839 {
1840 /* enlarge putback_buf if necessary. */
1841 if (pt->read_end == pt->read_buf + pt->read_buf_size
1842 && pt->read_buf == pt->read_pos)
1843 {
1844 size_t new_size = pt->read_buf_size * 2;
1845 unsigned char *tmp = (unsigned char *)
1846 scm_gc_realloc (pt->putback_buf, pt->read_buf_size, new_size,
1847 "putback buffer");
1848
1849 pt->read_pos = pt->read_buf = pt->putback_buf = tmp;
1850 pt->read_end = pt->read_buf + pt->read_buf_size;
1851 pt->read_buf_size = pt->putback_buf_size = new_size;
1852 }
1853
1854 /* shift any existing bytes to buffer + 1. */
1855 if (pt->read_pos == pt->read_end)
1856 pt->read_end = pt->read_buf + 1;
1857 else if (pt->read_pos != pt->read_buf + 1)
1858 {
1859 int count = pt->read_end - pt->read_pos;
1860
1861 memmove (pt->read_buf + 1, pt->read_pos, count);
1862 pt->read_end = pt->read_buf + 1 + count;
1863 }
1864
1865 pt->read_pos = pt->read_buf;
1866 }
1867 else
1868 /* switch to the put-back buffer. */
1869 {
1870 if (pt->putback_buf == NULL)
1871 {
1872 pt->putback_buf
1873 = (unsigned char *) scm_gc_malloc_pointerless
1874 (SCM_INITIAL_PUTBACK_BUF_SIZE, "putback buffer");
1875 pt->putback_buf_size = SCM_INITIAL_PUTBACK_BUF_SIZE;
1876 }
1877
1878 pt->saved_read_buf = pt->read_buf;
1879 pt->saved_read_pos = pt->read_pos;
1880 pt->saved_read_end = pt->read_end;
1881 pt->saved_read_buf_size = pt->read_buf_size;
1882
1883 pt->read_pos = pt->read_buf = pt->putback_buf;
1884 pt->read_end = pt->read_buf + 1;
1885 pt->read_buf_size = pt->putback_buf_size;
1886 }
1887
1888 *pt->read_buf = c;
1889
1890 if (pt->rw_random)
1891 pt->rw_active = SCM_PORT_READ;
1892 }
1893 #undef FUNC_NAME
1894
1895 void
1896 scm_unget_byte (int c, SCM port)
1897 {
1898 scm_i_pthread_mutex_t *lock;
1899 scm_c_lock_port (port, &lock);
1900 scm_unget_byte_unlocked (c, port);
1901 if (lock)
1902 scm_i_pthread_mutex_unlock (lock);
1903
1904 }
1905
1906 void
1907 scm_ungetc_unlocked (scm_t_wchar c, SCM port)
1908 #define FUNC_NAME "scm_ungetc"
1909 {
1910 scm_t_port *pt = SCM_PTAB_ENTRY (port);
1911 char *result;
1912 char result_buf[10];
1913 const char *encoding;
1914 size_t len;
1915 int i;
1916
1917 if (pt->encoding != NULL)
1918 encoding = pt->encoding;
1919 else
1920 encoding = "ISO-8859-1";
1921
1922 len = sizeof (result_buf);
1923 result = u32_conv_to_encoding (encoding,
1924 (enum iconv_ilseq_handler) pt->ilseq_handler,
1925 (uint32_t *) &c, 1, NULL,
1926 result_buf, &len);
1927
1928 if (SCM_UNLIKELY (result == NULL || len == 0))
1929 scm_encoding_error (FUNC_NAME, errno,
1930 "conversion to port encoding failed",
1931 SCM_BOOL_F, SCM_MAKE_CHAR (c));
1932
1933 for (i = len - 1; i >= 0; i--)
1934 scm_unget_byte_unlocked (result[i], port);
1935
1936 if (SCM_UNLIKELY (result != result_buf))
1937 free (result);
1938
1939 if (c == '\n')
1940 {
1941 /* What should col be in this case?
1942 * We'll leave it at -1.
1943 */
1944 SCM_LINUM (port) -= 1;
1945 }
1946 else
1947 SCM_COL(port) -= 1;
1948 }
1949 #undef FUNC_NAME
1950
1951 void
1952 scm_ungetc (scm_t_wchar c, SCM port)
1953 {
1954 scm_i_pthread_mutex_t *lock;
1955 scm_c_lock_port (port, &lock);
1956 scm_ungetc_unlocked (c, port);
1957 if (lock)
1958 scm_i_pthread_mutex_unlock (lock);
1959
1960 }
1961
1962 void
1963 scm_ungets_unlocked (const char *s, int n, SCM port)
1964 {
1965 /* This is simple minded and inefficient, but unreading strings is
1966 * probably not a common operation, and remember that line and
1967 * column numbers have to be handled...
1968 *
1969 * Please feel free to write an optimized version!
1970 */
1971 while (n--)
1972 scm_ungetc_unlocked (s[n], port);
1973 }
1974
1975 void
1976 scm_ungets (const char *s, int n, SCM port)
1977 {
1978 scm_i_pthread_mutex_t *lock;
1979 scm_c_lock_port (port, &lock);
1980 scm_ungets_unlocked (s, n, port);
1981 if (lock)
1982 scm_i_pthread_mutex_unlock (lock);
1983
1984 }
1985
1986 SCM_DEFINE (scm_peek_char, "peek-char", 0, 1, 0,
1987 (SCM port),
1988 "Return the next character available from @var{port},\n"
1989 "@emph{without} updating @var{port} to point to the following\n"
1990 "character. If no more characters are available, the\n"
1991 "end-of-file object is returned.\n"
1992 "\n"
1993 "The value returned by\n"
1994 "a call to @code{peek-char} is the same as the value that would\n"
1995 "have been returned by a call to @code{read-char} on the same\n"
1996 "port. The only difference is that the very next call to\n"
1997 "@code{read-char} or @code{peek-char} on that @var{port} will\n"
1998 "return the value returned by the preceding call to\n"
1999 "@code{peek-char}. In particular, a call to @code{peek-char} on\n"
2000 "an interactive port will hang waiting for input whenever a call\n"
2001 "to @code{read-char} would have hung.\n"
2002 "\n"
2003 "As for @code{read-char}, a @code{decoding-error} may be raised\n"
2004 "if such a situation occurs. However, unlike with @code{read-char},\n"
2005 "@var{port} still points at the beginning of the erroneous byte\n"
2006 "sequence when the error is raised.\n")
2007 #define FUNC_NAME s_scm_peek_char
2008 {
2009 int err;
2010 SCM result;
2011 scm_t_wchar c;
2012 char bytes[SCM_MBCHAR_BUF_SIZE];
2013 long column, line, i;
2014 size_t len;
2015
2016 if (SCM_UNBNDP (port))
2017 port = scm_current_input_port ();
2018 SCM_VALIDATE_OPINPORT (1, port);
2019
2020 column = SCM_COL (port);
2021 line = SCM_LINUM (port);
2022
2023 err = get_codepoint (port, &c, bytes, &len);
2024
2025 for (i = len - 1; i >= 0; i--)
2026 scm_unget_byte_unlocked (bytes[i], port);
2027
2028 SCM_COL (port) = column;
2029 SCM_LINUM (port) = line;
2030
2031 if (SCM_UNLIKELY (err != 0))
2032 {
2033 scm_decoding_error (FUNC_NAME, err, "input decoding error", port);
2034
2035 /* Shouldn't happen since `catch' always aborts to prompt. */
2036 result = SCM_BOOL_F;
2037 }
2038 else if (c == EOF)
2039 result = SCM_EOF_VAL;
2040 else
2041 result = SCM_MAKE_CHAR (c);
2042
2043 return result;
2044 }
2045 #undef FUNC_NAME
2046
2047 SCM_DEFINE (scm_unread_char, "unread-char", 1, 1, 0,
2048 (SCM cobj, SCM port),
2049 "Place character @var{cobj} in @var{port} so that it will be\n"
2050 "read by the next read operation. If called multiple times, the\n"
2051 "unread characters will be read again in last-in first-out\n"
2052 "order. If @var{port} is not supplied, the current input port\n"
2053 "is used.")
2054 #define FUNC_NAME s_scm_unread_char
2055 {
2056 int c;
2057
2058 SCM_VALIDATE_CHAR (1, cobj);
2059 if (SCM_UNBNDP (port))
2060 port = scm_current_input_port ();
2061 SCM_VALIDATE_OPINPORT (2, port);
2062
2063 c = SCM_CHAR (cobj);
2064
2065 scm_ungetc_unlocked (c, port);
2066 return cobj;
2067 }
2068 #undef FUNC_NAME
2069
2070 SCM_DEFINE (scm_unread_string, "unread-string", 2, 0, 0,
2071 (SCM str, SCM port),
2072 "Place the string @var{str} in @var{port} so that its characters will be\n"
2073 "read in subsequent read operations. If called multiple times, the\n"
2074 "unread characters will be read again in last-in first-out order. If\n"
2075 "@var{port} is not supplied, the current-input-port is used.")
2076 #define FUNC_NAME s_scm_unread_string
2077 {
2078 int n;
2079 SCM_VALIDATE_STRING (1, str);
2080 if (SCM_UNBNDP (port))
2081 port = scm_current_input_port ();
2082 SCM_VALIDATE_OPINPORT (2, port);
2083
2084 n = scm_i_string_length (str);
2085
2086 while (n--)
2087 scm_ungetc_unlocked (scm_i_string_ref (str, n), port);
2088
2089 return str;
2090 }
2091 #undef FUNC_NAME
2092
2093
2094 \f
2095
2096 /* Manipulating the buffers. */
2097
2098 /* This routine does not take any locks, as it is usually called as part
2099 of a port implementation. */
2100 void
2101 scm_port_non_buffer (scm_t_port *pt)
2102 {
2103 pt->read_pos = pt->read_buf = pt->read_end = &pt->shortbuf;
2104 pt->write_buf = pt->write_pos = &pt->shortbuf;
2105 pt->read_buf_size = pt->write_buf_size = 1;
2106 pt->write_end = pt->write_buf + pt->write_buf_size;
2107 }
2108
2109 /* this should only be called when the read buffer is empty. it
2110 tries to refill the read buffer. it returns the first char from
2111 the port, which is either EOF or *(pt->read_pos). */
2112 int
2113 scm_fill_input_unlocked (SCM port)
2114 {
2115 scm_t_port *pt = SCM_PTAB_ENTRY (port);
2116
2117 assert (pt->read_pos == pt->read_end);
2118
2119 if (pt->read_buf == pt->putback_buf)
2120 {
2121 /* finished reading put-back chars. */
2122 pt->read_buf = pt->saved_read_buf;
2123 pt->read_pos = pt->saved_read_pos;
2124 pt->read_end = pt->saved_read_end;
2125 pt->read_buf_size = pt->saved_read_buf_size;
2126 if (pt->read_pos < pt->read_end)
2127 return *(pt->read_pos);
2128 }
2129 return SCM_PORT_DESCRIPTOR (port)->fill_input (port);
2130 }
2131
2132 int
2133 scm_fill_input (SCM port)
2134 {
2135 scm_i_pthread_mutex_t *lock;
2136 int ret;
2137
2138 scm_c_lock_port (port, &lock);
2139 ret = scm_fill_input_unlocked (port);
2140 if (lock)
2141 scm_i_pthread_mutex_unlock (lock);
2142
2143
2144 return ret;
2145 }
2146
2147 /* move up to read_len chars from port's putback and/or read buffers
2148 into memory starting at dest. returns the number of chars moved. */
2149 size_t
2150 scm_take_from_input_buffers (SCM port, char *dest, size_t read_len)
2151 {
2152 scm_t_port *pt = SCM_PTAB_ENTRY (port);
2153 size_t chars_read = 0;
2154 size_t from_buf = min (pt->read_end - pt->read_pos, read_len);
2155
2156 if (from_buf > 0)
2157 {
2158 memcpy (dest, pt->read_pos, from_buf);
2159 pt->read_pos += from_buf;
2160 chars_read += from_buf;
2161 read_len -= from_buf;
2162 dest += from_buf;
2163 }
2164
2165 /* if putback was active, try the real input buffer too. */
2166 if (pt->read_buf == pt->putback_buf)
2167 {
2168 from_buf = min (pt->saved_read_end - pt->saved_read_pos, read_len);
2169 if (from_buf > 0)
2170 {
2171 memcpy (dest, pt->saved_read_pos, from_buf);
2172 pt->saved_read_pos += from_buf;
2173 chars_read += from_buf;
2174 }
2175 }
2176 return chars_read;
2177 }
2178
2179 /* Clear a port's read buffers, returning the contents. */
2180 SCM_DEFINE (scm_drain_input, "drain-input", 1, 0, 0,
2181 (SCM port),
2182 "This procedure clears a port's input buffers, similar\n"
2183 "to the way that force-output clears the output buffer. The\n"
2184 "contents of the buffers are returned as a single string, e.g.,\n"
2185 "\n"
2186 "@lisp\n"
2187 "(define p (open-input-file ...))\n"
2188 "(drain-input p) => empty string, nothing buffered yet.\n"
2189 "(unread-char (read-char p) p)\n"
2190 "(drain-input p) => initial chars from p, up to the buffer size.\n"
2191 "@end lisp\n\n"
2192 "Draining the buffers may be useful for cleanly finishing\n"
2193 "buffered I/O so that the file descriptor can be used directly\n"
2194 "for further input.")
2195 #define FUNC_NAME s_scm_drain_input
2196 {
2197 SCM result;
2198 char *data;
2199 scm_t_port *pt;
2200 long count;
2201
2202 SCM_VALIDATE_OPINPORT (1, port);
2203 pt = SCM_PTAB_ENTRY (port);
2204
2205 count = pt->read_end - pt->read_pos;
2206 if (pt->read_buf == pt->putback_buf)
2207 count += pt->saved_read_end - pt->saved_read_pos;
2208
2209 if (count)
2210 {
2211 result = scm_i_make_string (count, &data, 0);
2212 scm_take_from_input_buffers (port, data, count);
2213 }
2214 else
2215 result = scm_nullstr;
2216
2217 return result;
2218 }
2219 #undef FUNC_NAME
2220
2221 void
2222 scm_end_input_unlocked (SCM port)
2223 {
2224 long offset;
2225 scm_t_port *pt = SCM_PTAB_ENTRY (port);
2226
2227 if (pt->read_buf == pt->putback_buf)
2228 {
2229 offset = pt->read_end - pt->read_pos;
2230 pt->read_buf = pt->saved_read_buf;
2231 pt->read_pos = pt->saved_read_pos;
2232 pt->read_end = pt->saved_read_end;
2233 pt->read_buf_size = pt->saved_read_buf_size;
2234 }
2235 else
2236 offset = 0;
2237
2238 SCM_PORT_DESCRIPTOR (port)->end_input (port, offset);
2239 }
2240
2241 void
2242 scm_end_input (SCM port)
2243 {
2244 scm_i_pthread_mutex_t *lock;
2245 scm_c_lock_port (port, &lock);
2246 scm_end_input_unlocked (port);
2247 if (lock)
2248 scm_i_pthread_mutex_unlock (lock);
2249
2250 }
2251
2252 SCM_DEFINE (scm_force_output, "force-output", 0, 1, 0,
2253 (SCM port),
2254 "Flush the specified output port, or the current output port if @var{port}\n"
2255 "is omitted. The current output buffer contents are passed to the\n"
2256 "underlying port implementation (e.g., in the case of fports, the\n"
2257 "data will be written to the file and the output buffer will be cleared.)\n"
2258 "It has no effect on an unbuffered port.\n\n"
2259 "The return value is unspecified.")
2260 #define FUNC_NAME s_scm_force_output
2261 {
2262 if (SCM_UNBNDP (port))
2263 port = scm_current_output_port ();
2264 else
2265 {
2266 port = SCM_COERCE_OUTPORT (port);
2267 SCM_VALIDATE_OPOUTPORT (1, port);
2268 }
2269 scm_flush_unlocked (port);
2270 return SCM_UNSPECIFIED;
2271 }
2272 #undef FUNC_NAME
2273
2274 void
2275 scm_flush_unlocked (SCM port)
2276 {
2277 SCM_PORT_DESCRIPTOR (port)->flush (port);
2278 }
2279
2280 void
2281 scm_flush (SCM port)
2282 {
2283 scm_i_pthread_mutex_t *lock;
2284 scm_c_lock_port (port, &lock);
2285 scm_flush_unlocked (port);
2286 if (lock)
2287 scm_i_pthread_mutex_unlock (lock);
2288
2289 }
2290
2291
2292 \f
2293
2294 /* Output. */
2295
2296 void
2297 scm_putc (char c, SCM port)
2298 {
2299 scm_i_pthread_mutex_t *lock;
2300 scm_c_lock_port (port, &lock);
2301 scm_putc_unlocked (c, port);
2302 if (lock)
2303 scm_i_pthread_mutex_unlock (lock);
2304
2305 }
2306
2307 void
2308 scm_puts (const char *s, SCM port)
2309 {
2310 scm_i_pthread_mutex_t *lock;
2311 scm_c_lock_port (port, &lock);
2312 scm_puts_unlocked (s, port);
2313 if (lock)
2314 scm_i_pthread_mutex_unlock (lock);
2315
2316 }
2317
2318 /* scm_c_write
2319 *
2320 * Used by an application to write arbitrary number of bytes to an SCM
2321 * port. Similar semantics as libc write. However, unlike libc
2322 * write, scm_c_write writes the requested number of bytes and has no
2323 * return value.
2324 *
2325 * Warning: Doesn't update port line and column counts!
2326 */
2327 void
2328 scm_c_write_unlocked (SCM port, const void *ptr, size_t size)
2329 #define FUNC_NAME "scm_c_write"
2330 {
2331 scm_t_port *pt;
2332 scm_t_ptob_descriptor *ptob;
2333
2334 SCM_VALIDATE_OPOUTPORT (1, port);
2335
2336 pt = SCM_PTAB_ENTRY (port);
2337 ptob = SCM_PORT_DESCRIPTOR (port);
2338
2339 if (pt->rw_active == SCM_PORT_READ)
2340 scm_end_input_unlocked (port);
2341
2342 ptob->write (port, ptr, size);
2343
2344 if (pt->rw_random)
2345 pt->rw_active = SCM_PORT_WRITE;
2346 }
2347 #undef FUNC_NAME
2348
2349 void
2350 scm_c_write (SCM port, const void *ptr, size_t size)
2351 {
2352 scm_i_pthread_mutex_t *lock;
2353 scm_c_lock_port (port, &lock);
2354 scm_c_write_unlocked (port, ptr, size);
2355 if (lock)
2356 scm_i_pthread_mutex_unlock (lock);
2357
2358 }
2359
2360 /* scm_lfwrite
2361 *
2362 * This function differs from scm_c_write; it updates port line and
2363 * column. */
2364 void
2365 scm_lfwrite_unlocked (const char *ptr, size_t size, SCM port)
2366 {
2367 scm_t_port *pt = SCM_PTAB_ENTRY (port);
2368 scm_t_ptob_descriptor *ptob = SCM_PORT_DESCRIPTOR (port);
2369
2370 if (pt->rw_active == SCM_PORT_READ)
2371 scm_end_input_unlocked (port);
2372
2373 ptob->write (port, ptr, size);
2374
2375 for (; size; ptr++, size--)
2376 update_port_lf ((scm_t_wchar) (unsigned char) *ptr, port);
2377
2378 if (pt->rw_random)
2379 pt->rw_active = SCM_PORT_WRITE;
2380 }
2381
2382 void
2383 scm_lfwrite (const char *ptr, size_t size, SCM port)
2384 {
2385 scm_i_pthread_mutex_t *lock;
2386 scm_c_lock_port (port, &lock);
2387 scm_lfwrite_unlocked (ptr, size, port);
2388 if (lock)
2389 scm_i_pthread_mutex_unlock (lock);
2390
2391 }
2392
2393 /* Write STR to PORT from START inclusive to END exclusive. */
2394 void
2395 scm_lfwrite_substr (SCM str, size_t start, size_t end, SCM port)
2396 {
2397 scm_t_port *pt = SCM_PTAB_ENTRY (port);
2398
2399 if (pt->rw_active == SCM_PORT_READ)
2400 scm_end_input_unlocked (port);
2401
2402 if (end == (size_t) -1)
2403 end = scm_i_string_length (str);
2404
2405 scm_display (scm_c_substring (str, start, end), port);
2406
2407 if (pt->rw_random)
2408 pt->rw_active = SCM_PORT_WRITE;
2409 }
2410
2411
2412 \f
2413
2414 /* Querying and setting positions, and character availability. */
2415
2416 SCM_DEFINE (scm_char_ready_p, "char-ready?", 0, 1, 0,
2417 (SCM port),
2418 "Return @code{#t} if a character is ready on input @var{port}\n"
2419 "and return @code{#f} otherwise. If @code{char-ready?} returns\n"
2420 "@code{#t} then the next @code{read-char} operation on\n"
2421 "@var{port} is guaranteed not to hang. If @var{port} is a file\n"
2422 "port at end of file then @code{char-ready?} returns @code{#t}.\n"
2423 "\n"
2424 "@code{char-ready?} exists to make it possible for a\n"
2425 "program to accept characters from interactive ports without\n"
2426 "getting stuck waiting for input. Any input editors associated\n"
2427 "with such ports must make sure that characters whose existence\n"
2428 "has been asserted by @code{char-ready?} cannot be rubbed out.\n"
2429 "If @code{char-ready?} were to return @code{#f} at end of file,\n"
2430 "a port at end of file would be indistinguishable from an\n"
2431 "interactive port that has no ready characters.")
2432 #define FUNC_NAME s_scm_char_ready_p
2433 {
2434 scm_t_port *pt;
2435
2436 if (SCM_UNBNDP (port))
2437 port = scm_current_input_port ();
2438 /* It's possible to close the current input port, so validate even in
2439 this case. */
2440 SCM_VALIDATE_OPINPORT (1, port);
2441
2442 pt = SCM_PTAB_ENTRY (port);
2443
2444 /* if the current read buffer is filled, or the
2445 last pushed-back char has been read and the saved buffer is
2446 filled, result is true. */
2447 if (pt->read_pos < pt->read_end
2448 || (pt->read_buf == pt->putback_buf
2449 && pt->saved_read_pos < pt->saved_read_end))
2450 return SCM_BOOL_T;
2451 else
2452 {
2453 scm_t_ptob_descriptor *ptob = SCM_PORT_DESCRIPTOR (port);
2454
2455 if (ptob->input_waiting)
2456 return scm_from_bool(ptob->input_waiting (port));
2457 else
2458 return SCM_BOOL_T;
2459 }
2460 }
2461 #undef FUNC_NAME
2462
2463 SCM_DEFINE (scm_seek, "seek", 3, 0, 0,
2464 (SCM fd_port, SCM offset, SCM whence),
2465 "Sets the current position of @var{fd_port} to the integer\n"
2466 "@var{offset}, which is interpreted according to the value of\n"
2467 "@var{whence}.\n"
2468 "\n"
2469 "One of the following variables should be supplied for\n"
2470 "@var{whence}:\n"
2471 "@defvar SEEK_SET\n"
2472 "Seek from the beginning of the file.\n"
2473 "@end defvar\n"
2474 "@defvar SEEK_CUR\n"
2475 "Seek from the current position.\n"
2476 "@end defvar\n"
2477 "@defvar SEEK_END\n"
2478 "Seek from the end of the file.\n"
2479 "@end defvar\n"
2480 "If @var{fd_port} is a file descriptor, the underlying system\n"
2481 "call is @code{lseek}. @var{port} may be a string port.\n"
2482 "\n"
2483 "The value returned is the new position in the file. This means\n"
2484 "that the current position of a port can be obtained using:\n"
2485 "@lisp\n"
2486 "(seek port 0 SEEK_CUR)\n"
2487 "@end lisp")
2488 #define FUNC_NAME s_scm_seek
2489 {
2490 int how;
2491
2492 fd_port = SCM_COERCE_OUTPORT (fd_port);
2493
2494 how = scm_to_int (whence);
2495 if (how != SEEK_SET && how != SEEK_CUR && how != SEEK_END)
2496 SCM_OUT_OF_RANGE (3, whence);
2497
2498 if (SCM_OPPORTP (fd_port))
2499 {
2500 scm_t_ptob_descriptor *ptob = SCM_PORT_DESCRIPTOR (fd_port);
2501 off_t_or_off64_t off = scm_to_off_t_or_off64_t (offset);
2502 off_t_or_off64_t rv;
2503
2504 if (!ptob->seek)
2505 SCM_MISC_ERROR ("port is not seekable",
2506 scm_cons (fd_port, SCM_EOL));
2507 else
2508 rv = ptob->seek (fd_port, off, how);
2509 return scm_from_off_t_or_off64_t (rv);
2510 }
2511 else /* file descriptor?. */
2512 {
2513 off_t_or_off64_t off = scm_to_off_t_or_off64_t (offset);
2514 off_t_or_off64_t rv;
2515 rv = lseek_or_lseek64 (scm_to_int (fd_port), off, how);
2516 if (rv == -1)
2517 SCM_SYSERROR;
2518 return scm_from_off_t_or_off64_t (rv);
2519 }
2520 }
2521 #undef FUNC_NAME
2522
2523 #ifndef O_BINARY
2524 #define O_BINARY 0
2525 #endif
2526
2527 /* Mingw has ftruncate(), perhaps implemented above using chsize, but
2528 doesn't have the filename version truncate(), hence this code. */
2529 #if HAVE_FTRUNCATE && ! HAVE_TRUNCATE
2530 static int
2531 truncate (const char *file, off_t length)
2532 {
2533 int ret, fdes;
2534
2535 fdes = open (file, O_BINARY | O_WRONLY);
2536 if (fdes == -1)
2537 return -1;
2538
2539 ret = ftruncate (fdes, length);
2540 if (ret == -1)
2541 {
2542 int save_errno = errno;
2543 close (fdes);
2544 errno = save_errno;
2545 return -1;
2546 }
2547
2548 return close (fdes);
2549 }
2550 #endif /* HAVE_FTRUNCATE && ! HAVE_TRUNCATE */
2551
2552 SCM_DEFINE (scm_truncate_file, "truncate-file", 1, 1, 0,
2553 (SCM object, SCM length),
2554 "Truncate file @var{object} to @var{length} bytes. @var{object}\n"
2555 "can be a filename string, a port object, or an integer file\n"
2556 "descriptor.\n"
2557 "The return value is unspecified.\n"
2558 "\n"
2559 "For a port or file descriptor @var{length} can be omitted, in\n"
2560 "which case the file is truncated at the current position (per\n"
2561 "@code{ftell} above).\n"
2562 "\n"
2563 "On most systems a file can be extended by giving a length\n"
2564 "greater than the current size, but this is not mandatory in the\n"
2565 "POSIX standard.")
2566 #define FUNC_NAME s_scm_truncate_file
2567 {
2568 int rv;
2569
2570 /* "object" can be a port, fdes or filename.
2571
2572 Negative "length" makes no sense, but it's left to truncate() or
2573 ftruncate() to give back an error for that (normally EINVAL).
2574 */
2575
2576 if (SCM_UNBNDP (length))
2577 {
2578 /* must supply length if object is a filename. */
2579 if (scm_is_string (object))
2580 SCM_MISC_ERROR("must supply length if OBJECT is a filename", SCM_EOL);
2581
2582 length = scm_seek (object, SCM_INUM0, scm_from_int (SEEK_CUR));
2583 }
2584
2585 object = SCM_COERCE_OUTPORT (object);
2586 if (scm_is_integer (object))
2587 {
2588 off_t_or_off64_t c_length = scm_to_off_t_or_off64_t (length);
2589 SCM_SYSCALL (rv = ftruncate_or_ftruncate64 (scm_to_int (object),
2590 c_length));
2591 }
2592 else if (SCM_OPOUTPORTP (object))
2593 {
2594 off_t_or_off64_t c_length = scm_to_off_t_or_off64_t (length);
2595 scm_t_port *pt = SCM_PTAB_ENTRY (object);
2596 scm_t_ptob_descriptor *ptob = SCM_PORT_DESCRIPTOR (object);
2597
2598 if (!ptob->truncate)
2599 SCM_MISC_ERROR ("port is not truncatable", SCM_EOL);
2600 if (pt->rw_active == SCM_PORT_READ)
2601 scm_end_input_unlocked (object);
2602 else if (pt->rw_active == SCM_PORT_WRITE)
2603 ptob->flush (object);
2604
2605 ptob->truncate (object, c_length);
2606 rv = 0;
2607 }
2608 else
2609 {
2610 off_t_or_off64_t c_length = scm_to_off_t_or_off64_t (length);
2611 char *str = scm_to_locale_string (object);
2612 int eno;
2613 SCM_SYSCALL (rv = truncate_or_truncate64 (str, c_length));
2614 eno = errno;
2615 free (str);
2616 errno = eno;
2617 }
2618 if (rv == -1)
2619 SCM_SYSERROR;
2620 return SCM_UNSPECIFIED;
2621 }
2622 #undef FUNC_NAME
2623
2624 SCM_DEFINE (scm_port_line, "port-line", 1, 0, 0,
2625 (SCM port),
2626 "Return the current line number for @var{port}.\n"
2627 "\n"
2628 "The first line of a file is 0. But you might want to add 1\n"
2629 "when printing line numbers, since starting from 1 is\n"
2630 "traditional in error messages, and likely to be more natural to\n"
2631 "non-programmers.")
2632 #define FUNC_NAME s_scm_port_line
2633 {
2634 port = SCM_COERCE_OUTPORT (port);
2635 SCM_VALIDATE_OPENPORT (1, port);
2636 return scm_from_long (SCM_LINUM (port));
2637 }
2638 #undef FUNC_NAME
2639
2640 SCM_DEFINE (scm_set_port_line_x, "set-port-line!", 2, 0, 0,
2641 (SCM port, SCM line),
2642 "Set the current line number for @var{port} to @var{line}. The\n"
2643 "first line of a file is 0.")
2644 #define FUNC_NAME s_scm_set_port_line_x
2645 {
2646 port = SCM_COERCE_OUTPORT (port);
2647 SCM_VALIDATE_OPENPORT (1, port);
2648 SCM_PTAB_ENTRY (port)->line_number = scm_to_long (line);
2649 return SCM_UNSPECIFIED;
2650 }
2651 #undef FUNC_NAME
2652
2653 SCM_DEFINE (scm_port_column, "port-column", 1, 0, 0,
2654 (SCM port),
2655 "Return the current column number of @var{port}.\n"
2656 "If the number is\n"
2657 "unknown, the result is #f. Otherwise, the result is a 0-origin integer\n"
2658 "- i.e. the first character of the first line is line 0, column 0.\n"
2659 "(However, when you display a file position, for example in an error\n"
2660 "message, we recommend you add 1 to get 1-origin integers. This is\n"
2661 "because lines and column numbers traditionally start with 1, and that is\n"
2662 "what non-programmers will find most natural.)")
2663 #define FUNC_NAME s_scm_port_column
2664 {
2665 port = SCM_COERCE_OUTPORT (port);
2666 SCM_VALIDATE_OPENPORT (1, port);
2667 return scm_from_int (SCM_COL (port));
2668 }
2669 #undef FUNC_NAME
2670
2671 SCM_DEFINE (scm_set_port_column_x, "set-port-column!", 2, 0, 0,
2672 (SCM port, SCM column),
2673 "Set the current column of @var{port}. Before reading the first\n"
2674 "character on a line the column should be 0.")
2675 #define FUNC_NAME s_scm_set_port_column_x
2676 {
2677 port = SCM_COERCE_OUTPORT (port);
2678 SCM_VALIDATE_OPENPORT (1, port);
2679 SCM_PTAB_ENTRY (port)->column_number = scm_to_int (column);
2680 return SCM_UNSPECIFIED;
2681 }
2682 #undef FUNC_NAME
2683
2684 SCM_DEFINE (scm_port_filename, "port-filename", 1, 0, 0,
2685 (SCM port),
2686 "Return the filename associated with @var{port}, or @code{#f}\n"
2687 "if no filename is associated with the port.")
2688 #define FUNC_NAME s_scm_port_filename
2689 {
2690 port = SCM_COERCE_OUTPORT (port);
2691 SCM_VALIDATE_OPENPORT (1, port);
2692 return SCM_FILENAME (port);
2693 }
2694 #undef FUNC_NAME
2695
2696 SCM_DEFINE (scm_set_port_filename_x, "set-port-filename!", 2, 0, 0,
2697 (SCM port, SCM filename),
2698 "Change the filename associated with @var{port}, using the current input\n"
2699 "port if none is specified. Note that this does not change the port's\n"
2700 "source of data, but only the value that is returned by\n"
2701 "@code{port-filename} and reported in diagnostic output.")
2702 #define FUNC_NAME s_scm_set_port_filename_x
2703 {
2704 port = SCM_COERCE_OUTPORT (port);
2705 SCM_VALIDATE_OPENPORT (1, port);
2706 /* We allow the user to set the filename to whatever he likes. */
2707 SCM_SET_FILENAME (port, filename);
2708 return SCM_UNSPECIFIED;
2709 }
2710 #undef FUNC_NAME
2711
2712
2713 \f
2714
2715 /* Implementation helpers for port printing functions. */
2716
2717 void
2718 scm_print_port_mode (SCM exp, SCM port)
2719 {
2720 scm_puts_unlocked (SCM_CLOSEDP (exp)
2721 ? "closed: "
2722 : (SCM_RDNG & SCM_CELL_WORD_0 (exp)
2723 ? (SCM_WRTNG & SCM_CELL_WORD_0 (exp)
2724 ? "input-output: "
2725 : "input: ")
2726 : (SCM_WRTNG & SCM_CELL_WORD_0 (exp)
2727 ? "output: "
2728 : "bogus: ")),
2729 port);
2730 }
2731
2732 int
2733 scm_port_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
2734 {
2735 char *type = SCM_PTOBNAME (SCM_PTOBNUM (exp));
2736 if (!type)
2737 type = "port";
2738 scm_puts_unlocked ("#<", port);
2739 scm_print_port_mode (exp, port);
2740 scm_puts_unlocked (type, port);
2741 scm_putc_unlocked (' ', port);
2742 scm_uintprint (SCM_CELL_WORD_1 (exp), 16, port);
2743 scm_putc_unlocked ('>', port);
2744 return 1;
2745 }
2746
2747
2748 \f
2749
2750 /* Iterating over all ports. */
2751
2752 struct for_each_data
2753 {
2754 void (*proc) (void *data, SCM p);
2755 void *data;
2756 };
2757
2758 static SCM
2759 for_each_trampoline (void *data, SCM port, SCM result)
2760 {
2761 struct for_each_data *d = data;
2762
2763 d->proc (d->data, port);
2764
2765 return result;
2766 }
2767
2768 void
2769 scm_c_port_for_each (void (*proc)(void *data, SCM p), void *data)
2770 {
2771 struct for_each_data d;
2772
2773 d.proc = proc;
2774 d.data = data;
2775
2776 scm_c_weak_set_fold (for_each_trampoline, &d, SCM_EOL,
2777 scm_i_port_weak_set);
2778 }
2779
2780 static void
2781 scm_for_each_trampoline (void *data, SCM port)
2782 {
2783 scm_call_1 (SCM_PACK_POINTER (data), port);
2784 }
2785
2786 SCM_DEFINE (scm_port_for_each, "port-for-each", 1, 0, 0,
2787 (SCM proc),
2788 "Apply @var{proc} to each port in the Guile port table\n"
2789 "in turn. The return value is unspecified. More specifically,\n"
2790 "@var{proc} is applied exactly once to every port that exists\n"
2791 "in the system at the time @code{port-for-each} is invoked.\n"
2792 "Changes to the port table while @code{port-for-each} is running\n"
2793 "have no effect as far as @code{port-for-each} is concerned.")
2794 #define FUNC_NAME s_scm_port_for_each
2795 {
2796 SCM_VALIDATE_PROC (1, proc);
2797
2798 scm_c_port_for_each (scm_for_each_trampoline, SCM_UNPACK_POINTER (proc));
2799
2800 return SCM_UNSPECIFIED;
2801 }
2802 #undef FUNC_NAME
2803
2804 static void
2805 flush_output_port (void *closure, SCM port)
2806 {
2807 if (SCM_OPOUTPORTP (port))
2808 scm_flush_unlocked (port);
2809 }
2810
2811 SCM_DEFINE (scm_flush_all_ports, "flush-all-ports", 0, 0, 0,
2812 (),
2813 "Equivalent to calling @code{force-output} on\n"
2814 "all open output ports. The return value is unspecified.")
2815 #define FUNC_NAME s_scm_flush_all_ports
2816 {
2817 scm_c_port_for_each (&flush_output_port, NULL);
2818 return SCM_UNSPECIFIED;
2819 }
2820 #undef FUNC_NAME
2821
2822
2823 \f
2824
2825 /* Void ports. */
2826
2827 scm_t_bits scm_tc16_void_port = 0;
2828
2829 static int fill_input_void_port (SCM port SCM_UNUSED)
2830 {
2831 return EOF;
2832 }
2833
2834 static void
2835 write_void_port (SCM port SCM_UNUSED,
2836 const void *data SCM_UNUSED,
2837 size_t size SCM_UNUSED)
2838 {
2839 }
2840
2841 static SCM
2842 scm_i_void_port (long mode_bits)
2843 {
2844 SCM ret;
2845
2846 ret = scm_c_make_port (scm_tc16_void_port, mode_bits, 0);
2847
2848 scm_port_non_buffer (SCM_PTAB_ENTRY (ret));
2849
2850 return ret;
2851 }
2852
2853 SCM
2854 scm_void_port (char *mode_str)
2855 {
2856 return scm_i_void_port (scm_mode_bits (mode_str));
2857 }
2858
2859 SCM_DEFINE (scm_sys_make_void_port, "%make-void-port", 1, 0, 0,
2860 (SCM mode),
2861 "Create and return a new void port. A void port acts like\n"
2862 "@file{/dev/null}. The @var{mode} argument\n"
2863 "specifies the input/output modes for this port: see the\n"
2864 "documentation for @code{open-file} in @ref{File Ports}.")
2865 #define FUNC_NAME s_scm_sys_make_void_port
2866 {
2867 return scm_i_void_port (scm_i_mode_bits (mode));
2868 }
2869 #undef FUNC_NAME
2870
2871
2872 \f
2873
2874 /* Initialization. */
2875
2876 void
2877 scm_init_ports ()
2878 {
2879 /* lseek() symbols. */
2880 scm_c_define ("SEEK_SET", scm_from_int (SEEK_SET));
2881 scm_c_define ("SEEK_CUR", scm_from_int (SEEK_CUR));
2882 scm_c_define ("SEEK_END", scm_from_int (SEEK_END));
2883
2884 scm_tc16_void_port = scm_make_port_type ("void", fill_input_void_port,
2885 write_void_port);
2886
2887 cur_inport_fluid = scm_make_fluid ();
2888 cur_outport_fluid = scm_make_fluid ();
2889 cur_errport_fluid = scm_make_fluid ();
2890 cur_loadport_fluid = scm_make_fluid ();
2891
2892 scm_i_port_weak_set = scm_c_make_weak_set (31);
2893
2894 #include "libguile/ports.x"
2895
2896 /* Use Latin-1 as the default port encoding. */
2897 SCM_VARIABLE_SET (default_port_encoding_var,
2898 scm_make_fluid_with_default (SCM_BOOL_F));
2899 scm_port_encoding_init = 1;
2900
2901 SCM_VARIABLE_SET (scm_conversion_strategy,
2902 scm_make_fluid_with_default
2903 (scm_from_int ((int) SCM_FAILED_CONVERSION_QUESTION_MARK)));
2904 scm_conversion_strategy_init = 1;
2905
2906 /* These bindings are used when boot-9 turns `current-input-port' et
2907 al into parameters. They are then removed from the guile module. */
2908 scm_c_define ("%current-input-port-fluid", cur_inport_fluid);
2909 scm_c_define ("%current-output-port-fluid", cur_outport_fluid);
2910 scm_c_define ("%current-error-port-fluid", cur_errport_fluid);
2911 }
2912
2913 /*
2914 Local Variables:
2915 c-file-style: "gnu"
2916 End:
2917 */