Merge remote-tracking branch 'origin/stable-2.0'
[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 struct do_free_data
536 {
537 scm_t_ptob_descriptor *ptob;
538 SCM port;
539 };
540
541 static SCM
542 do_free (void *body_data)
543 {
544 struct do_free_data *data = body_data;
545
546 /* `close' is for explicit `close-port' by user. `free' is for this
547 purpose: ports collected by the GC. */
548 data->ptob->free (data->port);
549
550 return SCM_BOOL_T;
551 }
552
553 /* Finalize the object (a port) pointed to by PTR. */
554 static void
555 finalize_port (GC_PTR ptr, GC_PTR data)
556 {
557 SCM port = SCM_PACK_POINTER (ptr);
558
559 if (!SCM_PORTP (port))
560 abort ();
561
562 if (SCM_OPENP (port))
563 {
564 struct do_free_data data;
565
566 SCM_CLR_PORT_OPEN_FLAG (port);
567
568 data.ptob = SCM_PORT_DESCRIPTOR (port);
569 data.port = port;
570
571 scm_internal_catch (SCM_BOOL_T, do_free, &data,
572 scm_handle_by_message_noexit, NULL);
573
574 scm_gc_ports_collected++;
575 }
576 }
577
578
579 \f
580
581 SCM
582 scm_c_make_port_with_encoding (scm_t_bits tag, unsigned long mode_bits,
583 const char *encoding,
584 scm_t_string_failed_conversion_handler handler,
585 scm_t_bits stream)
586 {
587 SCM ret;
588 scm_t_port *entry;
589 scm_t_ptob_descriptor *ptob;
590
591 entry = (scm_t_port *) scm_gc_calloc (sizeof (scm_t_port), "port");
592 ptob = scm_c_port_type_ref (SCM_TC2PTOBNUM (tag));
593
594 ret = scm_words (tag | mode_bits, 3);
595 SCM_SET_CELL_WORD_1 (ret, (scm_t_bits) entry);
596 SCM_SET_CELL_WORD_2 (ret, (scm_t_bits) ptob);
597
598 entry->lock = scm_gc_malloc_pointerless (sizeof (*entry->lock), "port lock");
599 scm_i_pthread_mutex_init (entry->lock, scm_i_pthread_mutexattr_recursive);
600
601 entry->file_name = SCM_BOOL_F;
602 entry->rw_active = SCM_PORT_NEITHER;
603 entry->port = ret;
604 entry->stream = stream;
605 entry->encoding = encoding ? scm_gc_strdup (encoding, "port") : NULL;
606 if (encoding && strcmp (encoding, "UTF-8") == 0)
607 entry->encoding_mode = SCM_PORT_ENCODING_MODE_UTF8;
608 else if (!encoding || strcmp (encoding, "ISO-8859-1") == 0)
609 entry->encoding_mode = SCM_PORT_ENCODING_MODE_LATIN1;
610 else
611 entry->encoding_mode = SCM_PORT_ENCODING_MODE_ICONV;
612 entry->ilseq_handler = handler;
613 entry->iconv_descriptors = NULL;
614
615 if (SCM_PORT_DESCRIPTOR (ret)->free)
616 scm_i_set_finalizer (SCM2PTR (ret), finalize_port, NULL);
617
618 if (SCM_PORT_DESCRIPTOR (ret)->flags & SCM_PORT_TYPE_HAS_FLUSH)
619 scm_weak_set_add_x (scm_i_port_weak_set, ret);
620
621 return ret;
622 }
623
624 SCM
625 scm_c_make_port (scm_t_bits tag, unsigned long mode_bits, scm_t_bits stream)
626 {
627 return scm_c_make_port_with_encoding (tag, mode_bits,
628 scm_i_default_port_encoding (),
629 scm_i_get_conversion_strategy (SCM_BOOL_F),
630 stream);
631 }
632
633 SCM
634 scm_new_port_table_entry (scm_t_bits tag)
635 {
636 return scm_c_make_port (tag, 0, 0);
637 }
638
639 \f
640
641 /* Predicates. */
642
643 SCM_DEFINE (scm_port_p, "port?", 1, 0, 0,
644 (SCM x),
645 "Return a boolean indicating whether @var{x} is a port.\n"
646 "Equivalent to @code{(or (input-port? @var{x}) (output-port?\n"
647 "@var{x}))}.")
648 #define FUNC_NAME s_scm_port_p
649 {
650 return scm_from_bool (SCM_PORTP (x));
651 }
652 #undef FUNC_NAME
653
654 SCM_DEFINE (scm_input_port_p, "input-port?", 1, 0, 0,
655 (SCM x),
656 "Return @code{#t} if @var{x} is an input port, otherwise return\n"
657 "@code{#f}. Any object satisfying this predicate also satisfies\n"
658 "@code{port?}.")
659 #define FUNC_NAME s_scm_input_port_p
660 {
661 return scm_from_bool (SCM_INPUT_PORT_P (x));
662 }
663 #undef FUNC_NAME
664
665 SCM_DEFINE (scm_output_port_p, "output-port?", 1, 0, 0,
666 (SCM x),
667 "Return @code{#t} if @var{x} is an output port, otherwise return\n"
668 "@code{#f}. Any object satisfying this predicate also satisfies\n"
669 "@code{port?}.")
670 #define FUNC_NAME s_scm_output_port_p
671 {
672 x = SCM_COERCE_OUTPORT (x);
673 return scm_from_bool (SCM_OUTPUT_PORT_P (x));
674 }
675 #undef FUNC_NAME
676
677 SCM_DEFINE (scm_port_closed_p, "port-closed?", 1, 0, 0,
678 (SCM port),
679 "Return @code{#t} if @var{port} is closed or @code{#f} if it is\n"
680 "open.")
681 #define FUNC_NAME s_scm_port_closed_p
682 {
683 SCM_VALIDATE_PORT (1, port);
684 return scm_from_bool (!SCM_OPPORTP (port));
685 }
686 #undef FUNC_NAME
687
688 SCM_DEFINE (scm_eof_object_p, "eof-object?", 1, 0, 0,
689 (SCM x),
690 "Return @code{#t} if @var{x} is an end-of-file object; otherwise\n"
691 "return @code{#f}.")
692 #define FUNC_NAME s_scm_eof_object_p
693 {
694 return scm_from_bool (SCM_EOF_OBJECT_P (x));
695 }
696 #undef FUNC_NAME
697
698
699 \f
700
701 /* Closing ports. */
702
703 static void close_iconv_descriptors (scm_t_iconv_descriptors *id);
704
705 /* scm_close_port
706 * Call the close operation on a port object.
707 * see also scm_close.
708 */
709 SCM_DEFINE (scm_close_port, "close-port", 1, 0, 0,
710 (SCM port),
711 "Close the specified port object. Return @code{#t} if it\n"
712 "successfully closes a port or @code{#f} if it was already\n"
713 "closed. An exception may be raised if an error occurs, for\n"
714 "example when flushing buffered output. See also @ref{Ports and\n"
715 "File Descriptors, close}, for a procedure which can close file\n"
716 "descriptors.")
717 #define FUNC_NAME s_scm_close_port
718 {
719 scm_t_port *p;
720 int rv;
721
722 port = SCM_COERCE_OUTPORT (port);
723
724 SCM_VALIDATE_PORT (1, port);
725 if (SCM_CLOSEDP (port))
726 return SCM_BOOL_F;
727
728 p = SCM_PTAB_ENTRY (port);
729 SCM_CLR_PORT_OPEN_FLAG (port);
730
731 if (SCM_PORT_DESCRIPTOR (port)->flags & SCM_PORT_TYPE_HAS_FLUSH)
732 scm_weak_set_remove_x (scm_i_port_weak_set, port);
733
734 if (SCM_PORT_DESCRIPTOR (port)->close)
735 /* Note! This may throw an exception. Anything after this point
736 should be resilient to non-local exits. */
737 rv = SCM_PORT_DESCRIPTOR (port)->close (port);
738 else
739 rv = 0;
740
741 if (p->iconv_descriptors)
742 {
743 /* If we don't get here, the iconv_descriptors finalizer will
744 clean up. */
745 close_iconv_descriptors (p->iconv_descriptors);
746 p->iconv_descriptors = NULL;
747 }
748
749 return scm_from_bool (rv >= 0);
750 }
751 #undef FUNC_NAME
752
753 SCM_DEFINE (scm_close_input_port, "close-input-port", 1, 0, 0,
754 (SCM port),
755 "Close the specified input port object. The routine has no effect if\n"
756 "the file has already been closed. An exception may be raised if an\n"
757 "error occurs. The value returned is unspecified.\n\n"
758 "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
759 "which can close file descriptors.")
760 #define FUNC_NAME s_scm_close_input_port
761 {
762 SCM_VALIDATE_INPUT_PORT (1, port);
763 scm_close_port (port);
764 return SCM_UNSPECIFIED;
765 }
766 #undef FUNC_NAME
767
768 SCM_DEFINE (scm_close_output_port, "close-output-port", 1, 0, 0,
769 (SCM port),
770 "Close the specified output port object. The routine has no effect if\n"
771 "the file has already been closed. An exception may be raised if an\n"
772 "error occurs. The value returned is unspecified.\n\n"
773 "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
774 "which can close file descriptors.")
775 #define FUNC_NAME s_scm_close_output_port
776 {
777 port = SCM_COERCE_OUTPORT (port);
778 SCM_VALIDATE_OUTPUT_PORT (1, port);
779 scm_close_port (port);
780 return SCM_UNSPECIFIED;
781 }
782 #undef FUNC_NAME
783
784
785 \f
786
787 /* Encoding characters to byte streams, and decoding byte streams to
788 characters. */
789
790 /* A fluid specifying the default encoding for newly created ports. If it is
791 a string, that is the encoding. If it is #f, it is in the "native"
792 (Latin-1) encoding. */
793 SCM_VARIABLE (default_port_encoding_var, "%default-port-encoding");
794
795 static int scm_port_encoding_init = 0;
796
797 /* Use ENCODING as the default encoding for future ports. */
798 void
799 scm_i_set_default_port_encoding (const char *encoding)
800 {
801 if (!scm_port_encoding_init
802 || !scm_is_fluid (SCM_VARIABLE_REF (default_port_encoding_var)))
803 scm_misc_error (NULL, "tried to set port encoding fluid before it is initialized",
804 SCM_EOL);
805
806 if (encoding == NULL
807 || !strcmp (encoding, "ASCII")
808 || !strcmp (encoding, "ANSI_X3.4-1968")
809 || !strcmp (encoding, "ISO-8859-1"))
810 scm_fluid_set_x (SCM_VARIABLE_REF (default_port_encoding_var), SCM_BOOL_F);
811 else
812 scm_fluid_set_x (SCM_VARIABLE_REF (default_port_encoding_var),
813 scm_from_locale_string (encoding));
814 }
815
816 /* Return the name of the default encoding for newly created ports; a
817 return value of NULL means "ISO-8859-1". */
818 const char *
819 scm_i_default_port_encoding (void)
820 {
821 if (!scm_port_encoding_init)
822 return NULL;
823 else if (!scm_is_fluid (SCM_VARIABLE_REF (default_port_encoding_var)))
824 return NULL;
825 else
826 {
827 SCM encoding;
828
829 encoding = scm_fluid_ref (SCM_VARIABLE_REF (default_port_encoding_var));
830 if (!scm_is_string (encoding))
831 return NULL;
832 else
833 return scm_i_string_chars (encoding);
834 }
835 }
836
837 static void
838 finalize_iconv_descriptors (GC_PTR ptr, GC_PTR data)
839 {
840 close_iconv_descriptors (ptr);
841 }
842
843 static scm_t_iconv_descriptors *
844 open_iconv_descriptors (const char *encoding, int reading, int writing)
845 {
846 scm_t_iconv_descriptors *id;
847 iconv_t input_cd, output_cd;
848
849 input_cd = (iconv_t) -1;
850 output_cd = (iconv_t) -1;
851
852 if (reading)
853 {
854 /* Open an input iconv conversion descriptor, from ENCODING
855 to UTF-8. We choose UTF-8, not UTF-32, because iconv
856 implementations can typically convert from anything to
857 UTF-8, but not to UTF-32 (see
858 <http://lists.gnu.org/archive/html/bug-libunistring/2010-09/msg00007.html>). */
859
860 /* Assume opening an iconv descriptor causes about 16 KB of
861 allocation. */
862 scm_gc_register_allocation (16 * 1024);
863
864 input_cd = iconv_open ("UTF-8", encoding);
865 if (input_cd == (iconv_t) -1)
866 goto invalid_encoding;
867 }
868
869 if (writing)
870 {
871 /* Assume opening an iconv descriptor causes about 16 KB of
872 allocation. */
873 scm_gc_register_allocation (16 * 1024);
874
875 output_cd = iconv_open (encoding, "UTF-8");
876 if (output_cd == (iconv_t) -1)
877 {
878 if (input_cd != (iconv_t) -1)
879 iconv_close (input_cd);
880 goto invalid_encoding;
881 }
882 }
883
884 id = scm_gc_malloc_pointerless (sizeof (*id), "iconv descriptors");
885 id->input_cd = input_cd;
886 id->output_cd = output_cd;
887
888 /* Register a finalizer to close the descriptors. */
889 scm_i_set_finalizer (id, finalize_iconv_descriptors, NULL);
890
891 return id;
892
893 invalid_encoding:
894 {
895 SCM err;
896 err = scm_from_locale_string (encoding);
897 scm_misc_error ("open_iconv_descriptors",
898 "invalid or unknown character encoding ~s",
899 scm_list_1 (err));
900 }
901 }
902
903 static void
904 close_iconv_descriptors (scm_t_iconv_descriptors *id)
905 {
906 if (id->input_cd != (iconv_t) -1)
907 iconv_close (id->input_cd);
908 if (id->output_cd != (iconv_t) -1)
909 iconv_close (id->output_cd);
910 id->input_cd = (void *) -1;
911 id->output_cd = (void *) -1;
912 }
913
914 scm_t_iconv_descriptors *
915 scm_i_port_iconv_descriptors (SCM port)
916 {
917 scm_t_port *pt;
918
919 pt = SCM_PTAB_ENTRY (port);
920
921 assert (pt->encoding_mode == SCM_PORT_ENCODING_MODE_ICONV);
922
923 if (!pt->iconv_descriptors)
924 {
925 if (!pt->encoding)
926 pt->encoding = "ISO-8859-1";
927 pt->iconv_descriptors =
928 open_iconv_descriptors (pt->encoding,
929 SCM_INPUT_PORT_P (port),
930 SCM_OUTPUT_PORT_P (port));
931 }
932
933 return pt->iconv_descriptors;
934 }
935
936 void
937 scm_i_set_port_encoding_x (SCM port, const char *encoding)
938 {
939 scm_t_port *pt;
940 scm_t_iconv_descriptors *prev;
941
942 /* Set the character encoding for this port. */
943 pt = SCM_PTAB_ENTRY (port);
944 prev = pt->iconv_descriptors;
945
946 if (encoding && strcmp (encoding, "UTF-8") == 0)
947 {
948 pt->encoding = "UTF-8";
949 pt->encoding_mode = SCM_PORT_ENCODING_MODE_UTF8;
950 pt->iconv_descriptors = NULL;
951 }
952 else if (!encoding || strcmp (encoding, "ISO-8859-1") == 0)
953 {
954 pt->encoding = "ISO-8859-1";
955 pt->encoding_mode = SCM_PORT_ENCODING_MODE_LATIN1;
956 pt->iconv_descriptors = NULL;
957 }
958 else
959 {
960 /* Open descriptors before mutating the port. */
961 pt->iconv_descriptors =
962 open_iconv_descriptors (encoding,
963 SCM_INPUT_PORT_P (port),
964 SCM_OUTPUT_PORT_P (port));
965 pt->encoding = scm_gc_strdup (encoding, "port");
966 pt->encoding_mode = SCM_PORT_ENCODING_MODE_ICONV;
967 }
968
969 if (prev)
970 close_iconv_descriptors (prev);
971 }
972
973 SCM_DEFINE (scm_port_encoding, "port-encoding", 1, 0, 0,
974 (SCM port),
975 "Returns, as a string, the character encoding that @var{port}\n"
976 "uses to interpret its input and output.\n")
977 #define FUNC_NAME s_scm_port_encoding
978 {
979 scm_t_port *pt;
980 const char *enc;
981
982 SCM_VALIDATE_PORT (1, port);
983
984 pt = SCM_PTAB_ENTRY (port);
985 enc = pt->encoding;
986 if (enc)
987 return scm_from_locale_string (pt->encoding);
988 else
989 return SCM_BOOL_F;
990 }
991 #undef FUNC_NAME
992
993 SCM_DEFINE (scm_set_port_encoding_x, "set-port-encoding!", 2, 0, 0,
994 (SCM port, SCM enc),
995 "Sets the character encoding that will be used to interpret all\n"
996 "port I/O. New ports are created with the encoding\n"
997 "appropriate for the current locale if @code{setlocale} has \n"
998 "been called or ISO-8859-1 otherwise\n"
999 "and this procedure can be used to modify that encoding.\n")
1000 #define FUNC_NAME s_scm_set_port_encoding_x
1001 {
1002 char *enc_str;
1003
1004 SCM_VALIDATE_PORT (1, port);
1005 SCM_VALIDATE_STRING (2, enc);
1006
1007 enc_str = scm_to_locale_string (enc);
1008 scm_i_set_port_encoding_x (port, enc_str);
1009 free (enc_str);
1010
1011 return SCM_UNSPECIFIED;
1012 }
1013 #undef FUNC_NAME
1014
1015
1016 /* This determines how conversions handle unconvertible characters. */
1017 SCM_GLOBAL_VARIABLE (scm_conversion_strategy, "%port-conversion-strategy");
1018 static int scm_conversion_strategy_init = 0;
1019
1020 scm_t_string_failed_conversion_handler
1021 scm_i_get_conversion_strategy (SCM port)
1022 {
1023 SCM encoding;
1024
1025 if (scm_is_false (port))
1026 {
1027 if (!scm_conversion_strategy_init
1028 || !scm_is_fluid (SCM_VARIABLE_REF (scm_conversion_strategy)))
1029 return SCM_FAILED_CONVERSION_QUESTION_MARK;
1030 else
1031 {
1032 encoding = scm_fluid_ref (SCM_VARIABLE_REF (scm_conversion_strategy));
1033 if (scm_is_false (encoding))
1034 return SCM_FAILED_CONVERSION_QUESTION_MARK;
1035 else
1036 return (scm_t_string_failed_conversion_handler) scm_to_int (encoding);
1037 }
1038 }
1039 else
1040 {
1041 scm_t_port *pt;
1042 pt = SCM_PTAB_ENTRY (port);
1043 return pt->ilseq_handler;
1044 }
1045
1046 }
1047
1048 void
1049 scm_i_set_conversion_strategy_x (SCM port,
1050 scm_t_string_failed_conversion_handler handler)
1051 {
1052 SCM strategy;
1053 scm_t_port *pt;
1054
1055 strategy = scm_from_int ((int) handler);
1056
1057 if (scm_is_false (port))
1058 {
1059 /* Set the default encoding for future ports. */
1060 if (!scm_conversion_strategy_init
1061 || !scm_is_fluid (SCM_VARIABLE_REF (scm_conversion_strategy)))
1062 scm_misc_error (NULL, "tried to set conversion strategy fluid before it is initialized",
1063 SCM_EOL);
1064 scm_fluid_set_x (SCM_VARIABLE_REF (scm_conversion_strategy), strategy);
1065 }
1066 else
1067 {
1068 /* Set the character encoding for this port. */
1069 pt = SCM_PTAB_ENTRY (port);
1070 pt->ilseq_handler = handler;
1071 }
1072 }
1073
1074 SCM_DEFINE (scm_port_conversion_strategy, "port-conversion-strategy",
1075 1, 0, 0, (SCM port),
1076 "Returns the behavior of the port when handling a character that\n"
1077 "is not representable in the port's current encoding.\n"
1078 "It returns the symbol @code{error} if unrepresentable characters\n"
1079 "should cause exceptions, @code{substitute} if the port should\n"
1080 "try to replace unrepresentable characters with question marks or\n"
1081 "approximate characters, or @code{escape} if unrepresentable\n"
1082 "characters should be converted to string escapes.\n"
1083 "\n"
1084 "If @var{port} is @code{#f}, then the current default behavior\n"
1085 "will be returned. New ports will have this default behavior\n"
1086 "when they are created.\n")
1087 #define FUNC_NAME s_scm_port_conversion_strategy
1088 {
1089 scm_t_string_failed_conversion_handler h;
1090
1091 SCM_VALIDATE_OPPORT (1, port);
1092
1093 if (!scm_is_false (port))
1094 {
1095 SCM_VALIDATE_OPPORT (1, port);
1096 }
1097
1098 h = scm_i_get_conversion_strategy (port);
1099 if (h == SCM_FAILED_CONVERSION_ERROR)
1100 return scm_from_latin1_symbol ("error");
1101 else if (h == SCM_FAILED_CONVERSION_QUESTION_MARK)
1102 return scm_from_latin1_symbol ("substitute");
1103 else if (h == SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE)
1104 return scm_from_latin1_symbol ("escape");
1105 else
1106 abort ();
1107
1108 /* Never gets here. */
1109 return SCM_UNDEFINED;
1110 }
1111 #undef FUNC_NAME
1112
1113 SCM_DEFINE (scm_set_port_conversion_strategy_x, "set-port-conversion-strategy!",
1114 2, 0, 0,
1115 (SCM port, SCM sym),
1116 "Sets the behavior of the interpreter when outputting a character\n"
1117 "that is not representable in the port's current encoding.\n"
1118 "@var{sym} can be either @code{'error}, @code{'substitute}, or\n"
1119 "@code{'escape}. If it is @code{'error}, an error will be thrown\n"
1120 "when an unconvertible character is encountered. If it is\n"
1121 "@code{'substitute}, then unconvertible characters will \n"
1122 "be replaced with approximate characters, or with question marks\n"
1123 "if no approximately correct character is available.\n"
1124 "If it is @code{'escape},\n"
1125 "it will appear as a hex escape when output.\n"
1126 "\n"
1127 "If @var{port} is an open port, the conversion error behavior\n"
1128 "is set for that port. If it is @code{#f}, it is set as the\n"
1129 "default behavior for any future ports that get created in\n"
1130 "this thread.\n")
1131 #define FUNC_NAME s_scm_set_port_conversion_strategy_x
1132 {
1133 SCM err;
1134 SCM qm;
1135 SCM esc;
1136
1137 if (!scm_is_false (port))
1138 {
1139 SCM_VALIDATE_OPPORT (1, port);
1140 }
1141
1142 err = scm_from_latin1_symbol ("error");
1143 if (scm_is_true (scm_eqv_p (sym, err)))
1144 {
1145 scm_i_set_conversion_strategy_x (port, SCM_FAILED_CONVERSION_ERROR);
1146 return SCM_UNSPECIFIED;
1147 }
1148
1149 qm = scm_from_latin1_symbol ("substitute");
1150 if (scm_is_true (scm_eqv_p (sym, qm)))
1151 {
1152 scm_i_set_conversion_strategy_x (port,
1153 SCM_FAILED_CONVERSION_QUESTION_MARK);
1154 return SCM_UNSPECIFIED;
1155 }
1156
1157 esc = scm_from_latin1_symbol ("escape");
1158 if (scm_is_true (scm_eqv_p (sym, esc)))
1159 {
1160 scm_i_set_conversion_strategy_x (port,
1161 SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE);
1162 return SCM_UNSPECIFIED;
1163 }
1164
1165 SCM_MISC_ERROR ("unknown conversion behavior ~s", scm_list_1 (sym));
1166
1167 return SCM_UNSPECIFIED;
1168 }
1169 #undef FUNC_NAME
1170
1171
1172 \f
1173
1174 /* The port lock. */
1175
1176 static void
1177 lock_port (void *mutex)
1178 {
1179 scm_i_pthread_mutex_lock (mutex);
1180 }
1181
1182 static void
1183 unlock_port (void *mutex)
1184 {
1185 scm_i_pthread_mutex_unlock (mutex);
1186 }
1187
1188 void
1189 scm_dynwind_lock_port (SCM port)
1190 #define FUNC_NAME "dynwind-lock-port"
1191 {
1192 scm_i_pthread_mutex_t *lock;
1193 SCM_VALIDATE_OPPORT (SCM_ARG1, port);
1194 scm_c_lock_port (port, &lock);
1195 if (lock)
1196 {
1197 scm_dynwind_unwind_handler (unlock_port, lock, SCM_F_WIND_EXPLICITLY);
1198 scm_dynwind_rewind_handler (lock_port, lock, 0);
1199 }
1200 }
1201 #undef FUNC_NAME
1202
1203
1204 \f
1205
1206 /* Input. */
1207
1208 int
1209 scm_get_byte_or_eof (SCM port)
1210 {
1211 scm_i_pthread_mutex_t *lock;
1212 int ret;
1213
1214 scm_c_lock_port (port, &lock);
1215 ret = scm_get_byte_or_eof_unlocked (port);
1216 if (lock)
1217 scm_i_pthread_mutex_unlock (lock);
1218
1219 return ret;
1220 }
1221
1222 int
1223 scm_peek_byte_or_eof (SCM port)
1224 {
1225 scm_i_pthread_mutex_t *lock;
1226 int ret;
1227
1228 scm_c_lock_port (port, &lock);
1229 ret = scm_peek_byte_or_eof_unlocked (port);
1230 if (lock)
1231 scm_i_pthread_mutex_unlock (lock);
1232
1233 return ret;
1234 }
1235
1236 /* scm_c_read
1237 *
1238 * Used by an application to read arbitrary number of bytes from an
1239 * SCM port. Same semantics as libc read, except that scm_c_read only
1240 * returns less than SIZE bytes if at end-of-file.
1241 *
1242 * Warning: Doesn't update port line and column counts! */
1243
1244 /* This structure, and the following swap_buffer function, are used
1245 for temporarily swapping a port's own read buffer, and the buffer
1246 that the caller of scm_c_read provides. */
1247 struct port_and_swap_buffer
1248 {
1249 scm_t_port *pt;
1250 unsigned char *buffer;
1251 size_t size;
1252 };
1253
1254 static void
1255 swap_buffer (void *data)
1256 {
1257 struct port_and_swap_buffer *psb = (struct port_and_swap_buffer *) data;
1258 unsigned char *old_buf = psb->pt->read_buf;
1259 size_t old_size = psb->pt->read_buf_size;
1260
1261 /* Make the port use (buffer, size) from the struct. */
1262 psb->pt->read_pos = psb->pt->read_buf = psb->pt->read_end = psb->buffer;
1263 psb->pt->read_buf_size = psb->size;
1264
1265 /* Save the port's old (buffer, size) in the struct. */
1266 psb->buffer = old_buf;
1267 psb->size = old_size;
1268 }
1269
1270 size_t
1271 scm_c_read_unlocked (SCM port, void *buffer, size_t size)
1272 #define FUNC_NAME "scm_c_read"
1273 {
1274 scm_t_port *pt;
1275 size_t n_read = 0, n_available;
1276 struct port_and_swap_buffer psb;
1277
1278 SCM_VALIDATE_OPINPORT (1, port);
1279
1280 pt = SCM_PTAB_ENTRY (port);
1281 if (pt->rw_active == SCM_PORT_WRITE)
1282 SCM_PORT_DESCRIPTOR (port)->flush (port);
1283
1284 if (pt->rw_random)
1285 pt->rw_active = SCM_PORT_READ;
1286
1287 /* Take bytes first from the port's read buffer. */
1288 if (pt->read_pos < pt->read_end)
1289 {
1290 n_available = min (size, pt->read_end - pt->read_pos);
1291 memcpy (buffer, pt->read_pos, n_available);
1292 buffer = (char *) buffer + n_available;
1293 pt->read_pos += n_available;
1294 n_read += n_available;
1295 size -= n_available;
1296 }
1297
1298 /* Avoid the scm_dynwind_* costs if we now have enough data. */
1299 if (size == 0)
1300 return n_read;
1301
1302 /* Now we will call scm_fill_input repeatedly until we have read the
1303 requested number of bytes. (Note that a single scm_fill_input
1304 call does not guarantee to fill the whole of the port's read
1305 buffer.) */
1306 if (pt->read_buf_size <= 1 && pt->encoding == NULL)
1307 {
1308 /* The port that we are reading from is unbuffered - i.e. does
1309 not have its own persistent buffer - but we have a buffer,
1310 provided by our caller, that is the right size for the data
1311 that is wanted. For the following scm_fill_input calls,
1312 therefore, we use the buffer in hand as the port's read
1313 buffer.
1314
1315 We need to make sure that the port's normal (1 byte) buffer
1316 is reinstated in case one of the scm_fill_input () calls
1317 throws an exception; we use the scm_dynwind_* API to achieve
1318 that.
1319
1320 A consequence of this optimization is that the fill_input
1321 functions can't unget characters. That'll push data to the
1322 pushback buffer instead of this psb buffer. */
1323 #if SCM_DEBUG == 1
1324 unsigned char *pback = pt->putback_buf;
1325 #endif
1326 psb.pt = pt;
1327 psb.buffer = buffer;
1328 psb.size = size;
1329 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
1330 scm_dynwind_rewind_handler (swap_buffer, &psb, SCM_F_WIND_EXPLICITLY);
1331 scm_dynwind_unwind_handler (swap_buffer, &psb, SCM_F_WIND_EXPLICITLY);
1332
1333 /* Call scm_fill_input until we have all the bytes that we need,
1334 or we hit EOF. */
1335 while (pt->read_buf_size && (scm_fill_input_unlocked (port) != EOF))
1336 {
1337 pt->read_buf_size -= (pt->read_end - pt->read_pos);
1338 pt->read_pos = pt->read_buf = pt->read_end;
1339 }
1340 #if SCM_DEBUG == 1
1341 if (pback != pt->putback_buf
1342 || pt->read_buf - (unsigned char *) buffer < 0)
1343 scm_misc_error (FUNC_NAME,
1344 "scm_c_read must not call a fill function that pushes "
1345 "back characters onto an unbuffered port", SCM_EOL);
1346 #endif
1347 n_read += pt->read_buf - (unsigned char *) buffer;
1348
1349 /* Reinstate the port's normal buffer. */
1350 scm_dynwind_end ();
1351 }
1352 else
1353 {
1354 /* The port has its own buffer. It is important that we use it,
1355 even if it happens to be smaller than our caller's buffer, so
1356 that a custom port implementation's entry points (in
1357 particular, fill_input) can rely on the buffer always being
1358 the same as they first set up. */
1359 while (size && (scm_fill_input_unlocked (port) != EOF))
1360 {
1361 n_available = min (size, pt->read_end - pt->read_pos);
1362 memcpy (buffer, pt->read_pos, n_available);
1363 buffer = (char *) buffer + n_available;
1364 pt->read_pos += n_available;
1365 n_read += n_available;
1366 size -= n_available;
1367 }
1368 }
1369
1370 return n_read;
1371 }
1372 #undef FUNC_NAME
1373
1374 size_t
1375 scm_c_read (SCM port, void *buffer, size_t size)
1376 {
1377 scm_i_pthread_mutex_t *lock;
1378 size_t ret;
1379
1380 scm_c_lock_port (port, &lock);
1381 ret = scm_c_read_unlocked (port, buffer, size);
1382 if (lock)
1383 scm_i_pthread_mutex_unlock (lock);
1384
1385
1386 return ret;
1387 }
1388
1389 /* Update the line and column number of PORT after consumption of C. */
1390 static inline void
1391 update_port_lf (scm_t_wchar c, SCM port)
1392 {
1393 switch (c)
1394 {
1395 case '\a':
1396 case EOF:
1397 break;
1398 case '\b':
1399 SCM_DECCOL (port);
1400 break;
1401 case '\n':
1402 SCM_INCLINE (port);
1403 break;
1404 case '\r':
1405 SCM_ZEROCOL (port);
1406 break;
1407 case '\t':
1408 SCM_TABCOL (port);
1409 break;
1410 default:
1411 SCM_INCCOL (port);
1412 break;
1413 }
1414 }
1415
1416 #define SCM_MBCHAR_BUF_SIZE (4)
1417
1418 /* Convert the SIZE-byte UTF-8 sequence in UTF8_BUF to a codepoint.
1419 UTF8_BUF is assumed to contain a valid UTF-8 sequence. */
1420 static scm_t_wchar
1421 utf8_to_codepoint (const scm_t_uint8 *utf8_buf, size_t size)
1422 {
1423 scm_t_wchar codepoint;
1424
1425 if (utf8_buf[0] <= 0x7f)
1426 {
1427 assert (size == 1);
1428 codepoint = utf8_buf[0];
1429 }
1430 else if ((utf8_buf[0] & 0xe0) == 0xc0)
1431 {
1432 assert (size == 2);
1433 codepoint = ((scm_t_wchar) utf8_buf[0] & 0x1f) << 6UL
1434 | (utf8_buf[1] & 0x3f);
1435 }
1436 else if ((utf8_buf[0] & 0xf0) == 0xe0)
1437 {
1438 assert (size == 3);
1439 codepoint = ((scm_t_wchar) utf8_buf[0] & 0x0f) << 12UL
1440 | ((scm_t_wchar) utf8_buf[1] & 0x3f) << 6UL
1441 | (utf8_buf[2] & 0x3f);
1442 }
1443 else
1444 {
1445 assert (size == 4);
1446 codepoint = ((scm_t_wchar) utf8_buf[0] & 0x07) << 18UL
1447 | ((scm_t_wchar) utf8_buf[1] & 0x3f) << 12UL
1448 | ((scm_t_wchar) utf8_buf[2] & 0x3f) << 6UL
1449 | (utf8_buf[3] & 0x3f);
1450 }
1451
1452 return codepoint;
1453 }
1454
1455 /* Read a UTF-8 sequence from PORT. On success, return 0 and set
1456 *CODEPOINT to the codepoint that was read, fill BUF with its UTF-8
1457 representation, and set *LEN to the length in bytes. Return
1458 `EILSEQ' on error. */
1459 static int
1460 get_utf8_codepoint (SCM port, scm_t_wchar *codepoint,
1461 scm_t_uint8 buf[SCM_MBCHAR_BUF_SIZE], size_t *len)
1462 {
1463 #define ASSERT_NOT_EOF(b) \
1464 if (SCM_UNLIKELY ((b) == EOF)) \
1465 goto invalid_seq
1466 #define CONSUME_PEEKED_BYTE() \
1467 pt->read_pos++
1468
1469 int byte;
1470 scm_t_port *pt;
1471
1472 *len = 0;
1473 pt = SCM_PTAB_ENTRY (port);
1474
1475 byte = scm_get_byte_or_eof_unlocked (port);
1476 if (byte == EOF)
1477 {
1478 *codepoint = EOF;
1479 return 0;
1480 }
1481
1482 buf[0] = (scm_t_uint8) byte;
1483 *len = 1;
1484
1485 if (buf[0] <= 0x7f)
1486 /* 1-byte form. */
1487 *codepoint = buf[0];
1488 else if (buf[0] >= 0xc2 && buf[0] <= 0xdf)
1489 {
1490 /* 2-byte form. */
1491 byte = scm_peek_byte_or_eof_unlocked (port);
1492 ASSERT_NOT_EOF (byte);
1493
1494 if (SCM_UNLIKELY ((byte & 0xc0) != 0x80))
1495 goto invalid_seq;
1496
1497 CONSUME_PEEKED_BYTE ();
1498 buf[1] = (scm_t_uint8) byte;
1499 *len = 2;
1500
1501 *codepoint = ((scm_t_wchar) buf[0] & 0x1f) << 6UL
1502 | (buf[1] & 0x3f);
1503 }
1504 else if ((buf[0] & 0xf0) == 0xe0)
1505 {
1506 /* 3-byte form. */
1507 byte = scm_peek_byte_or_eof_unlocked (port);
1508 ASSERT_NOT_EOF (byte);
1509
1510 if (SCM_UNLIKELY ((byte & 0xc0) != 0x80
1511 || (buf[0] == 0xe0 && byte < 0xa0)
1512 || (buf[0] == 0xed && byte > 0x9f)))
1513 goto invalid_seq;
1514
1515 CONSUME_PEEKED_BYTE ();
1516 buf[1] = (scm_t_uint8) byte;
1517 *len = 2;
1518
1519 byte = scm_peek_byte_or_eof_unlocked (port);
1520 ASSERT_NOT_EOF (byte);
1521
1522 if (SCM_UNLIKELY ((byte & 0xc0) != 0x80))
1523 goto invalid_seq;
1524
1525 CONSUME_PEEKED_BYTE ();
1526 buf[2] = (scm_t_uint8) byte;
1527 *len = 3;
1528
1529 *codepoint = ((scm_t_wchar) buf[0] & 0x0f) << 12UL
1530 | ((scm_t_wchar) buf[1] & 0x3f) << 6UL
1531 | (buf[2] & 0x3f);
1532 }
1533 else if (buf[0] >= 0xf0 && buf[0] <= 0xf4)
1534 {
1535 /* 4-byte form. */
1536 byte = scm_peek_byte_or_eof_unlocked (port);
1537 ASSERT_NOT_EOF (byte);
1538
1539 if (SCM_UNLIKELY (((byte & 0xc0) != 0x80)
1540 || (buf[0] == 0xf0 && byte < 0x90)
1541 || (buf[0] == 0xf4 && byte > 0x8f)))
1542 goto invalid_seq;
1543
1544 CONSUME_PEEKED_BYTE ();
1545 buf[1] = (scm_t_uint8) byte;
1546 *len = 2;
1547
1548 byte = scm_peek_byte_or_eof_unlocked (port);
1549 ASSERT_NOT_EOF (byte);
1550
1551 if (SCM_UNLIKELY ((byte & 0xc0) != 0x80))
1552 goto invalid_seq;
1553
1554 CONSUME_PEEKED_BYTE ();
1555 buf[2] = (scm_t_uint8) byte;
1556 *len = 3;
1557
1558 byte = scm_peek_byte_or_eof_unlocked (port);
1559 ASSERT_NOT_EOF (byte);
1560
1561 if (SCM_UNLIKELY ((byte & 0xc0) != 0x80))
1562 goto invalid_seq;
1563
1564 CONSUME_PEEKED_BYTE ();
1565 buf[3] = (scm_t_uint8) byte;
1566 *len = 4;
1567
1568 *codepoint = ((scm_t_wchar) buf[0] & 0x07) << 18UL
1569 | ((scm_t_wchar) buf[1] & 0x3f) << 12UL
1570 | ((scm_t_wchar) buf[2] & 0x3f) << 6UL
1571 | (buf[3] & 0x3f);
1572 }
1573 else
1574 goto invalid_seq;
1575
1576 return 0;
1577
1578 invalid_seq:
1579 /* Here we could choose the consume the faulty byte when it's not a
1580 valid starting byte, but it's not a requirement. What Section 3.9
1581 of Unicode 6.0.0 mandates, though, is to not consume a byte that
1582 would otherwise be a valid starting byte. */
1583
1584 return EILSEQ;
1585
1586 #undef CONSUME_PEEKED_BYTE
1587 #undef ASSERT_NOT_EOF
1588 }
1589
1590 /* Read an ISO-8859-1 codepoint (a byte) from PORT. On success, return
1591 *0 and set CODEPOINT to the codepoint that was read, fill BUF with
1592 *its UTF-8 representation, and set *LEN to the length in bytes.
1593 *Return `EILSEQ' on error. */
1594 static int
1595 get_latin1_codepoint (SCM port, scm_t_wchar *codepoint,
1596 char buf[SCM_MBCHAR_BUF_SIZE], size_t *len)
1597 {
1598 *codepoint = scm_get_byte_or_eof_unlocked (port);
1599
1600 if (*codepoint == EOF)
1601 *len = 0;
1602 else
1603 {
1604 *len = 1;
1605 buf[0] = *codepoint;
1606 }
1607 return 0;
1608 }
1609
1610 /* Likewise, read a byte sequence from PORT, passing it through its
1611 input conversion descriptor. */
1612 static int
1613 get_iconv_codepoint (SCM port, scm_t_wchar *codepoint,
1614 char buf[SCM_MBCHAR_BUF_SIZE], size_t *len)
1615 {
1616 scm_t_iconv_descriptors *id;
1617 int err, byte_read;
1618 size_t bytes_consumed, output_size;
1619 char *output;
1620 scm_t_uint8 utf8_buf[SCM_MBCHAR_BUF_SIZE];
1621
1622 id = scm_i_port_iconv_descriptors (port);
1623
1624 for (output_size = 0, output = (char *) utf8_buf,
1625 bytes_consumed = 0, err = 0;
1626 err == 0 && output_size == 0
1627 && (bytes_consumed == 0 || byte_read != EOF);
1628 bytes_consumed++)
1629 {
1630 char *input;
1631 size_t input_left, output_left, done;
1632
1633 byte_read = scm_get_byte_or_eof_unlocked (port);
1634 if (byte_read == EOF)
1635 {
1636 if (bytes_consumed == 0)
1637 {
1638 *codepoint = (scm_t_wchar) EOF;
1639 *len = 0;
1640 return 0;
1641 }
1642 else
1643 continue;
1644 }
1645
1646 buf[bytes_consumed] = byte_read;
1647
1648 input = buf;
1649 input_left = bytes_consumed + 1;
1650 output_left = sizeof (utf8_buf);
1651
1652 done = iconv (id->input_cd, &input, &input_left, &output, &output_left);
1653 if (done == (size_t) -1)
1654 {
1655 err = errno;
1656 if (err == EINVAL)
1657 /* Missing input: keep trying. */
1658 err = 0;
1659 }
1660 else
1661 output_size = sizeof (utf8_buf) - output_left;
1662 }
1663
1664 if (SCM_UNLIKELY (output_size == 0))
1665 /* An unterminated sequence. */
1666 err = EILSEQ;
1667 else if (SCM_LIKELY (err == 0))
1668 {
1669 /* Convert the UTF8_BUF sequence to a Unicode code point. */
1670 *codepoint = utf8_to_codepoint (utf8_buf, output_size);
1671 *len = bytes_consumed;
1672 }
1673
1674 return err;
1675 }
1676
1677 /* Read a codepoint from PORT and return it in *CODEPOINT. Fill BUF
1678 with the byte representation of the codepoint in PORT's encoding, and
1679 set *LEN to the length in bytes of that representation. Return 0 on
1680 success and an errno value on error. */
1681 static int
1682 get_codepoint (SCM port, scm_t_wchar *codepoint,
1683 char buf[SCM_MBCHAR_BUF_SIZE], size_t *len)
1684 {
1685 int err;
1686 scm_t_port *pt = SCM_PTAB_ENTRY (port);
1687
1688 if (pt->encoding_mode == SCM_PORT_ENCODING_MODE_UTF8)
1689 err = get_utf8_codepoint (port, codepoint, (scm_t_uint8 *) buf, len);
1690 else if (pt->encoding_mode == SCM_PORT_ENCODING_MODE_LATIN1)
1691 err = get_latin1_codepoint (port, codepoint, buf, len);
1692 else
1693 err = get_iconv_codepoint (port, codepoint, buf, len);
1694
1695 if (SCM_LIKELY (err == 0))
1696 update_port_lf (*codepoint, port);
1697 else if (pt->ilseq_handler == SCM_ICONVEH_QUESTION_MARK)
1698 {
1699 *codepoint = '?';
1700 err = 0;
1701 update_port_lf (*codepoint, port);
1702 }
1703
1704 return err;
1705 }
1706
1707 /* Read a codepoint from PORT and return it. */
1708 scm_t_wchar
1709 scm_getc_unlocked (SCM port)
1710 #define FUNC_NAME "scm_getc"
1711 {
1712 int err;
1713 size_t len;
1714 scm_t_wchar codepoint;
1715 char buf[SCM_MBCHAR_BUF_SIZE];
1716
1717 err = get_codepoint (port, &codepoint, buf, &len);
1718 if (SCM_UNLIKELY (err != 0))
1719 /* At this point PORT should point past the invalid encoding, as per
1720 R6RS-lib Section 8.2.4. */
1721 scm_decoding_error (FUNC_NAME, err, "input decoding error", port);
1722
1723 return codepoint;
1724 }
1725 #undef FUNC_NAME
1726
1727 scm_t_wchar
1728 scm_getc (SCM port)
1729 {
1730 scm_i_pthread_mutex_t *lock;
1731 scm_t_wchar ret;
1732
1733 scm_c_lock_port (port, &lock);
1734 ret = scm_getc_unlocked (port);
1735 if (lock)
1736 scm_i_pthread_mutex_unlock (lock);
1737
1738
1739 return ret;
1740 }
1741
1742 SCM_DEFINE (scm_read_char, "read-char", 0, 1, 0,
1743 (SCM port),
1744 "Return the next character available from @var{port}, updating\n"
1745 "@var{port} to point to the following character. If no more\n"
1746 "characters are available, the end-of-file object is returned.\n"
1747 "\n"
1748 "When @var{port}'s data cannot be decoded according to its\n"
1749 "character encoding, a @code{decoding-error} is raised and\n"
1750 "@var{port} points past the erroneous byte sequence.\n")
1751 #define FUNC_NAME s_scm_read_char
1752 {
1753 scm_t_wchar c;
1754 if (SCM_UNBNDP (port))
1755 port = scm_current_input_port ();
1756 SCM_VALIDATE_OPINPORT (1, port);
1757 c = scm_getc_unlocked (port);
1758 if (EOF == c)
1759 return SCM_EOF_VAL;
1760 return SCM_MAKE_CHAR (c);
1761 }
1762 #undef FUNC_NAME
1763
1764
1765 \f
1766
1767 /* Pushback. */
1768
1769 void
1770 scm_unget_byte_unlocked (int c, SCM port)
1771 #define FUNC_NAME "scm_unget_byte"
1772 {
1773 scm_t_port *pt = SCM_PTAB_ENTRY (port);
1774
1775 if (pt->read_buf == pt->putback_buf)
1776 /* already using the put-back buffer. */
1777 {
1778 /* enlarge putback_buf if necessary. */
1779 if (pt->read_end == pt->read_buf + pt->read_buf_size
1780 && pt->read_buf == pt->read_pos)
1781 {
1782 size_t new_size = pt->read_buf_size * 2;
1783 unsigned char *tmp = (unsigned char *)
1784 scm_gc_realloc (pt->putback_buf, pt->read_buf_size, new_size,
1785 "putback buffer");
1786
1787 pt->read_pos = pt->read_buf = pt->putback_buf = tmp;
1788 pt->read_end = pt->read_buf + pt->read_buf_size;
1789 pt->read_buf_size = pt->putback_buf_size = new_size;
1790 }
1791
1792 /* shift any existing bytes to buffer + 1. */
1793 if (pt->read_pos == pt->read_end)
1794 pt->read_end = pt->read_buf + 1;
1795 else if (pt->read_pos != pt->read_buf + 1)
1796 {
1797 int count = pt->read_end - pt->read_pos;
1798
1799 memmove (pt->read_buf + 1, pt->read_pos, count);
1800 pt->read_end = pt->read_buf + 1 + count;
1801 }
1802
1803 pt->read_pos = pt->read_buf;
1804 }
1805 else
1806 /* switch to the put-back buffer. */
1807 {
1808 if (pt->putback_buf == NULL)
1809 {
1810 pt->putback_buf
1811 = (unsigned char *) scm_gc_malloc_pointerless
1812 (SCM_INITIAL_PUTBACK_BUF_SIZE, "putback buffer");
1813 pt->putback_buf_size = SCM_INITIAL_PUTBACK_BUF_SIZE;
1814 }
1815
1816 pt->saved_read_buf = pt->read_buf;
1817 pt->saved_read_pos = pt->read_pos;
1818 pt->saved_read_end = pt->read_end;
1819 pt->saved_read_buf_size = pt->read_buf_size;
1820
1821 pt->read_pos = pt->read_buf = pt->putback_buf;
1822 pt->read_end = pt->read_buf + 1;
1823 pt->read_buf_size = pt->putback_buf_size;
1824 }
1825
1826 *pt->read_buf = c;
1827
1828 if (pt->rw_random)
1829 pt->rw_active = SCM_PORT_READ;
1830 }
1831 #undef FUNC_NAME
1832
1833 void
1834 scm_unget_byte (int c, SCM port)
1835 {
1836 scm_i_pthread_mutex_t *lock;
1837 scm_c_lock_port (port, &lock);
1838 scm_unget_byte_unlocked (c, port);
1839 if (lock)
1840 scm_i_pthread_mutex_unlock (lock);
1841
1842 }
1843
1844 void
1845 scm_ungetc_unlocked (scm_t_wchar c, SCM port)
1846 #define FUNC_NAME "scm_ungetc"
1847 {
1848 scm_t_port *pt = SCM_PTAB_ENTRY (port);
1849 char *result;
1850 char result_buf[10];
1851 const char *encoding;
1852 size_t len;
1853 int i;
1854
1855 if (pt->encoding != NULL)
1856 encoding = pt->encoding;
1857 else
1858 encoding = "ISO-8859-1";
1859
1860 len = sizeof (result_buf);
1861 result = u32_conv_to_encoding (encoding,
1862 (enum iconv_ilseq_handler) pt->ilseq_handler,
1863 (uint32_t *) &c, 1, NULL,
1864 result_buf, &len);
1865
1866 if (SCM_UNLIKELY (result == NULL || len == 0))
1867 scm_encoding_error (FUNC_NAME, errno,
1868 "conversion to port encoding failed",
1869 SCM_BOOL_F, SCM_MAKE_CHAR (c));
1870
1871 for (i = len - 1; i >= 0; i--)
1872 scm_unget_byte_unlocked (result[i], port);
1873
1874 if (SCM_UNLIKELY (result != result_buf))
1875 free (result);
1876
1877 if (c == '\n')
1878 {
1879 /* What should col be in this case?
1880 * We'll leave it at -1.
1881 */
1882 SCM_LINUM (port) -= 1;
1883 }
1884 else
1885 SCM_COL(port) -= 1;
1886 }
1887 #undef FUNC_NAME
1888
1889 void
1890 scm_ungetc (scm_t_wchar c, SCM port)
1891 {
1892 scm_i_pthread_mutex_t *lock;
1893 scm_c_lock_port (port, &lock);
1894 scm_ungetc_unlocked (c, port);
1895 if (lock)
1896 scm_i_pthread_mutex_unlock (lock);
1897
1898 }
1899
1900 void
1901 scm_ungets_unlocked (const char *s, int n, SCM port)
1902 {
1903 /* This is simple minded and inefficient, but unreading strings is
1904 * probably not a common operation, and remember that line and
1905 * column numbers have to be handled...
1906 *
1907 * Please feel free to write an optimized version!
1908 */
1909 while (n--)
1910 scm_ungetc_unlocked (s[n], port);
1911 }
1912
1913 void
1914 scm_ungets (const char *s, int n, SCM port)
1915 {
1916 scm_i_pthread_mutex_t *lock;
1917 scm_c_lock_port (port, &lock);
1918 scm_ungets_unlocked (s, n, port);
1919 if (lock)
1920 scm_i_pthread_mutex_unlock (lock);
1921
1922 }
1923
1924 SCM_DEFINE (scm_peek_char, "peek-char", 0, 1, 0,
1925 (SCM port),
1926 "Return the next character available from @var{port},\n"
1927 "@emph{without} updating @var{port} to point to the following\n"
1928 "character. If no more characters are available, the\n"
1929 "end-of-file object is returned.\n"
1930 "\n"
1931 "The value returned by\n"
1932 "a call to @code{peek-char} is the same as the value that would\n"
1933 "have been returned by a call to @code{read-char} on the same\n"
1934 "port. The only difference is that the very next call to\n"
1935 "@code{read-char} or @code{peek-char} on that @var{port} will\n"
1936 "return the value returned by the preceding call to\n"
1937 "@code{peek-char}. In particular, a call to @code{peek-char} on\n"
1938 "an interactive port will hang waiting for input whenever a call\n"
1939 "to @code{read-char} would have hung.\n"
1940 "\n"
1941 "As for @code{read-char}, a @code{decoding-error} may be raised\n"
1942 "if such a situation occurs. However, unlike with @code{read-char},\n"
1943 "@var{port} still points at the beginning of the erroneous byte\n"
1944 "sequence when the error is raised.\n")
1945 #define FUNC_NAME s_scm_peek_char
1946 {
1947 int err;
1948 SCM result;
1949 scm_t_wchar c;
1950 char bytes[SCM_MBCHAR_BUF_SIZE];
1951 long column, line, i;
1952 size_t len;
1953
1954 if (SCM_UNBNDP (port))
1955 port = scm_current_input_port ();
1956 SCM_VALIDATE_OPINPORT (1, port);
1957
1958 column = SCM_COL (port);
1959 line = SCM_LINUM (port);
1960
1961 err = get_codepoint (port, &c, bytes, &len);
1962
1963 for (i = len - 1; i >= 0; i--)
1964 scm_unget_byte_unlocked (bytes[i], port);
1965
1966 SCM_COL (port) = column;
1967 SCM_LINUM (port) = line;
1968
1969 if (SCM_UNLIKELY (err != 0))
1970 {
1971 scm_decoding_error (FUNC_NAME, err, "input decoding error", port);
1972
1973 /* Shouldn't happen since `catch' always aborts to prompt. */
1974 result = SCM_BOOL_F;
1975 }
1976 else if (c == EOF)
1977 result = SCM_EOF_VAL;
1978 else
1979 result = SCM_MAKE_CHAR (c);
1980
1981 return result;
1982 }
1983 #undef FUNC_NAME
1984
1985 SCM_DEFINE (scm_unread_char, "unread-char", 1, 1, 0,
1986 (SCM cobj, SCM port),
1987 "Place character @var{cobj} in @var{port} so that it will be\n"
1988 "read by the next read operation. If called multiple times, the\n"
1989 "unread characters will be read again in last-in first-out\n"
1990 "order. If @var{port} is not supplied, the current input port\n"
1991 "is used.")
1992 #define FUNC_NAME s_scm_unread_char
1993 {
1994 int c;
1995
1996 SCM_VALIDATE_CHAR (1, cobj);
1997 if (SCM_UNBNDP (port))
1998 port = scm_current_input_port ();
1999 SCM_VALIDATE_OPINPORT (2, port);
2000
2001 c = SCM_CHAR (cobj);
2002
2003 scm_ungetc_unlocked (c, port);
2004 return cobj;
2005 }
2006 #undef FUNC_NAME
2007
2008 SCM_DEFINE (scm_unread_string, "unread-string", 2, 0, 0,
2009 (SCM str, SCM port),
2010 "Place the string @var{str} in @var{port} so that its characters will be\n"
2011 "read in subsequent read operations. If called multiple times, the\n"
2012 "unread characters will be read again in last-in first-out order. If\n"
2013 "@var{port} is not supplied, the current-input-port is used.")
2014 #define FUNC_NAME s_scm_unread_string
2015 {
2016 int n;
2017 SCM_VALIDATE_STRING (1, str);
2018 if (SCM_UNBNDP (port))
2019 port = scm_current_input_port ();
2020 SCM_VALIDATE_OPINPORT (2, port);
2021
2022 n = scm_i_string_length (str);
2023
2024 while (n--)
2025 scm_ungetc_unlocked (scm_i_string_ref (str, n), port);
2026
2027 return str;
2028 }
2029 #undef FUNC_NAME
2030
2031
2032 \f
2033
2034 /* Manipulating the buffers. */
2035
2036 /* This routine does not take any locks, as it is usually called as part
2037 of a port implementation. */
2038 void
2039 scm_port_non_buffer (scm_t_port *pt)
2040 {
2041 pt->read_pos = pt->read_buf = pt->read_end = &pt->shortbuf;
2042 pt->write_buf = pt->write_pos = &pt->shortbuf;
2043 pt->read_buf_size = pt->write_buf_size = 1;
2044 pt->write_end = pt->write_buf + pt->write_buf_size;
2045 }
2046
2047 /* this should only be called when the read buffer is empty. it
2048 tries to refill the read buffer. it returns the first char from
2049 the port, which is either EOF or *(pt->read_pos). */
2050 int
2051 scm_fill_input_unlocked (SCM port)
2052 {
2053 scm_t_port *pt = SCM_PTAB_ENTRY (port);
2054
2055 assert (pt->read_pos == pt->read_end);
2056
2057 if (pt->read_buf == pt->putback_buf)
2058 {
2059 /* finished reading put-back chars. */
2060 pt->read_buf = pt->saved_read_buf;
2061 pt->read_pos = pt->saved_read_pos;
2062 pt->read_end = pt->saved_read_end;
2063 pt->read_buf_size = pt->saved_read_buf_size;
2064 if (pt->read_pos < pt->read_end)
2065 return *(pt->read_pos);
2066 }
2067 return SCM_PORT_DESCRIPTOR (port)->fill_input (port);
2068 }
2069
2070 int
2071 scm_fill_input (SCM port)
2072 {
2073 scm_i_pthread_mutex_t *lock;
2074 int ret;
2075
2076 scm_c_lock_port (port, &lock);
2077 ret = scm_fill_input_unlocked (port);
2078 if (lock)
2079 scm_i_pthread_mutex_unlock (lock);
2080
2081
2082 return ret;
2083 }
2084
2085 /* move up to read_len chars from port's putback and/or read buffers
2086 into memory starting at dest. returns the number of chars moved. */
2087 size_t
2088 scm_take_from_input_buffers (SCM port, char *dest, size_t read_len)
2089 {
2090 scm_t_port *pt = SCM_PTAB_ENTRY (port);
2091 size_t chars_read = 0;
2092 size_t from_buf = min (pt->read_end - pt->read_pos, read_len);
2093
2094 if (from_buf > 0)
2095 {
2096 memcpy (dest, pt->read_pos, from_buf);
2097 pt->read_pos += from_buf;
2098 chars_read += from_buf;
2099 read_len -= from_buf;
2100 dest += from_buf;
2101 }
2102
2103 /* if putback was active, try the real input buffer too. */
2104 if (pt->read_buf == pt->putback_buf)
2105 {
2106 from_buf = min (pt->saved_read_end - pt->saved_read_pos, read_len);
2107 if (from_buf > 0)
2108 {
2109 memcpy (dest, pt->saved_read_pos, from_buf);
2110 pt->saved_read_pos += from_buf;
2111 chars_read += from_buf;
2112 }
2113 }
2114 return chars_read;
2115 }
2116
2117 /* Clear a port's read buffers, returning the contents. */
2118 SCM_DEFINE (scm_drain_input, "drain-input", 1, 0, 0,
2119 (SCM port),
2120 "This procedure clears a port's input buffers, similar\n"
2121 "to the way that force-output clears the output buffer. The\n"
2122 "contents of the buffers are returned as a single string, e.g.,\n"
2123 "\n"
2124 "@lisp\n"
2125 "(define p (open-input-file ...))\n"
2126 "(drain-input p) => empty string, nothing buffered yet.\n"
2127 "(unread-char (read-char p) p)\n"
2128 "(drain-input p) => initial chars from p, up to the buffer size.\n"
2129 "@end lisp\n\n"
2130 "Draining the buffers may be useful for cleanly finishing\n"
2131 "buffered I/O so that the file descriptor can be used directly\n"
2132 "for further input.")
2133 #define FUNC_NAME s_scm_drain_input
2134 {
2135 SCM result;
2136 char *data;
2137 scm_t_port *pt;
2138 long count;
2139
2140 SCM_VALIDATE_OPINPORT (1, port);
2141 pt = SCM_PTAB_ENTRY (port);
2142
2143 count = pt->read_end - pt->read_pos;
2144 if (pt->read_buf == pt->putback_buf)
2145 count += pt->saved_read_end - pt->saved_read_pos;
2146
2147 if (count)
2148 {
2149 result = scm_i_make_string (count, &data, 0);
2150 scm_take_from_input_buffers (port, data, count);
2151 }
2152 else
2153 result = scm_nullstr;
2154
2155 return result;
2156 }
2157 #undef FUNC_NAME
2158
2159 void
2160 scm_end_input_unlocked (SCM port)
2161 {
2162 long offset;
2163 scm_t_port *pt = SCM_PTAB_ENTRY (port);
2164
2165 if (pt->read_buf == pt->putback_buf)
2166 {
2167 offset = pt->read_end - pt->read_pos;
2168 pt->read_buf = pt->saved_read_buf;
2169 pt->read_pos = pt->saved_read_pos;
2170 pt->read_end = pt->saved_read_end;
2171 pt->read_buf_size = pt->saved_read_buf_size;
2172 }
2173 else
2174 offset = 0;
2175
2176 SCM_PORT_DESCRIPTOR (port)->end_input (port, offset);
2177 }
2178
2179 void
2180 scm_end_input (SCM port)
2181 {
2182 scm_i_pthread_mutex_t *lock;
2183 scm_c_lock_port (port, &lock);
2184 scm_end_input_unlocked (port);
2185 if (lock)
2186 scm_i_pthread_mutex_unlock (lock);
2187
2188 }
2189
2190 SCM_DEFINE (scm_force_output, "force-output", 0, 1, 0,
2191 (SCM port),
2192 "Flush the specified output port, or the current output port if @var{port}\n"
2193 "is omitted. The current output buffer contents are passed to the\n"
2194 "underlying port implementation (e.g., in the case of fports, the\n"
2195 "data will be written to the file and the output buffer will be cleared.)\n"
2196 "It has no effect on an unbuffered port.\n\n"
2197 "The return value is unspecified.")
2198 #define FUNC_NAME s_scm_force_output
2199 {
2200 if (SCM_UNBNDP (port))
2201 port = scm_current_output_port ();
2202 else
2203 {
2204 port = SCM_COERCE_OUTPORT (port);
2205 SCM_VALIDATE_OPOUTPORT (1, port);
2206 }
2207 scm_flush_unlocked (port);
2208 return SCM_UNSPECIFIED;
2209 }
2210 #undef FUNC_NAME
2211
2212 void
2213 scm_flush_unlocked (SCM port)
2214 {
2215 SCM_PORT_DESCRIPTOR (port)->flush (port);
2216 }
2217
2218 void
2219 scm_flush (SCM port)
2220 {
2221 scm_i_pthread_mutex_t *lock;
2222 scm_c_lock_port (port, &lock);
2223 scm_flush_unlocked (port);
2224 if (lock)
2225 scm_i_pthread_mutex_unlock (lock);
2226
2227 }
2228
2229
2230 \f
2231
2232 /* Output. */
2233
2234 void
2235 scm_putc (char c, SCM port)
2236 {
2237 scm_i_pthread_mutex_t *lock;
2238 scm_c_lock_port (port, &lock);
2239 scm_putc_unlocked (c, port);
2240 if (lock)
2241 scm_i_pthread_mutex_unlock (lock);
2242
2243 }
2244
2245 void
2246 scm_puts (const char *s, SCM port)
2247 {
2248 scm_i_pthread_mutex_t *lock;
2249 scm_c_lock_port (port, &lock);
2250 scm_puts_unlocked (s, port);
2251 if (lock)
2252 scm_i_pthread_mutex_unlock (lock);
2253
2254 }
2255
2256 /* scm_c_write
2257 *
2258 * Used by an application to write arbitrary number of bytes to an SCM
2259 * port. Similar semantics as libc write. However, unlike libc
2260 * write, scm_c_write writes the requested number of bytes and has no
2261 * return value.
2262 *
2263 * Warning: Doesn't update port line and column counts!
2264 */
2265 void
2266 scm_c_write_unlocked (SCM port, const void *ptr, size_t size)
2267 #define FUNC_NAME "scm_c_write"
2268 {
2269 scm_t_port *pt;
2270 scm_t_ptob_descriptor *ptob;
2271
2272 SCM_VALIDATE_OPOUTPORT (1, port);
2273
2274 pt = SCM_PTAB_ENTRY (port);
2275 ptob = SCM_PORT_DESCRIPTOR (port);
2276
2277 if (pt->rw_active == SCM_PORT_READ)
2278 scm_end_input_unlocked (port);
2279
2280 ptob->write (port, ptr, size);
2281
2282 if (pt->rw_random)
2283 pt->rw_active = SCM_PORT_WRITE;
2284 }
2285 #undef FUNC_NAME
2286
2287 void
2288 scm_c_write (SCM port, const void *ptr, size_t size)
2289 {
2290 scm_i_pthread_mutex_t *lock;
2291 scm_c_lock_port (port, &lock);
2292 scm_c_write_unlocked (port, ptr, size);
2293 if (lock)
2294 scm_i_pthread_mutex_unlock (lock);
2295
2296 }
2297
2298 /* scm_lfwrite
2299 *
2300 * This function differs from scm_c_write; it updates port line and
2301 * column. */
2302 void
2303 scm_lfwrite_unlocked (const char *ptr, size_t size, SCM port)
2304 {
2305 scm_t_port *pt = SCM_PTAB_ENTRY (port);
2306 scm_t_ptob_descriptor *ptob = SCM_PORT_DESCRIPTOR (port);
2307
2308 if (pt->rw_active == SCM_PORT_READ)
2309 scm_end_input_unlocked (port);
2310
2311 ptob->write (port, ptr, size);
2312
2313 for (; size; ptr++, size--)
2314 update_port_lf ((scm_t_wchar) (unsigned char) *ptr, port);
2315
2316 if (pt->rw_random)
2317 pt->rw_active = SCM_PORT_WRITE;
2318 }
2319
2320 void
2321 scm_lfwrite (const char *ptr, size_t size, SCM port)
2322 {
2323 scm_i_pthread_mutex_t *lock;
2324 scm_c_lock_port (port, &lock);
2325 scm_lfwrite_unlocked (ptr, size, port);
2326 if (lock)
2327 scm_i_pthread_mutex_unlock (lock);
2328
2329 }
2330
2331 /* Write STR to PORT from START inclusive to END exclusive. */
2332 void
2333 scm_lfwrite_substr (SCM str, size_t start, size_t end, SCM port)
2334 {
2335 scm_t_port *pt = SCM_PTAB_ENTRY (port);
2336
2337 if (pt->rw_active == SCM_PORT_READ)
2338 scm_end_input_unlocked (port);
2339
2340 if (end == (size_t) -1)
2341 end = scm_i_string_length (str);
2342
2343 scm_display (scm_c_substring (str, start, end), port);
2344
2345 if (pt->rw_random)
2346 pt->rw_active = SCM_PORT_WRITE;
2347 }
2348
2349
2350 \f
2351
2352 /* Querying and setting positions, and character availability. */
2353
2354 SCM_DEFINE (scm_char_ready_p, "char-ready?", 0, 1, 0,
2355 (SCM port),
2356 "Return @code{#t} if a character is ready on input @var{port}\n"
2357 "and return @code{#f} otherwise. If @code{char-ready?} returns\n"
2358 "@code{#t} then the next @code{read-char} operation on\n"
2359 "@var{port} is guaranteed not to hang. If @var{port} is a file\n"
2360 "port at end of file then @code{char-ready?} returns @code{#t}.\n"
2361 "\n"
2362 "@code{char-ready?} exists to make it possible for a\n"
2363 "program to accept characters from interactive ports without\n"
2364 "getting stuck waiting for input. Any input editors associated\n"
2365 "with such ports must make sure that characters whose existence\n"
2366 "has been asserted by @code{char-ready?} cannot be rubbed out.\n"
2367 "If @code{char-ready?} were to return @code{#f} at end of file,\n"
2368 "a port at end of file would be indistinguishable from an\n"
2369 "interactive port that has no ready characters.")
2370 #define FUNC_NAME s_scm_char_ready_p
2371 {
2372 scm_t_port *pt;
2373
2374 if (SCM_UNBNDP (port))
2375 port = scm_current_input_port ();
2376 /* It's possible to close the current input port, so validate even in
2377 this case. */
2378 SCM_VALIDATE_OPINPORT (1, port);
2379
2380 pt = SCM_PTAB_ENTRY (port);
2381
2382 /* if the current read buffer is filled, or the
2383 last pushed-back char has been read and the saved buffer is
2384 filled, result is true. */
2385 if (pt->read_pos < pt->read_end
2386 || (pt->read_buf == pt->putback_buf
2387 && pt->saved_read_pos < pt->saved_read_end))
2388 return SCM_BOOL_T;
2389 else
2390 {
2391 scm_t_ptob_descriptor *ptob = SCM_PORT_DESCRIPTOR (port);
2392
2393 if (ptob->input_waiting)
2394 return scm_from_bool(ptob->input_waiting (port));
2395 else
2396 return SCM_BOOL_T;
2397 }
2398 }
2399 #undef FUNC_NAME
2400
2401 SCM_DEFINE (scm_seek, "seek", 3, 0, 0,
2402 (SCM fd_port, SCM offset, SCM whence),
2403 "Sets the current position of @var{fd_port} to the integer\n"
2404 "@var{offset}, which is interpreted according to the value of\n"
2405 "@var{whence}.\n"
2406 "\n"
2407 "One of the following variables should be supplied for\n"
2408 "@var{whence}:\n"
2409 "@defvar SEEK_SET\n"
2410 "Seek from the beginning of the file.\n"
2411 "@end defvar\n"
2412 "@defvar SEEK_CUR\n"
2413 "Seek from the current position.\n"
2414 "@end defvar\n"
2415 "@defvar SEEK_END\n"
2416 "Seek from the end of the file.\n"
2417 "@end defvar\n"
2418 "If @var{fd_port} is a file descriptor, the underlying system\n"
2419 "call is @code{lseek}. @var{port} may be a string port.\n"
2420 "\n"
2421 "The value returned is the new position in the file. This means\n"
2422 "that the current position of a port can be obtained using:\n"
2423 "@lisp\n"
2424 "(seek port 0 SEEK_CUR)\n"
2425 "@end lisp")
2426 #define FUNC_NAME s_scm_seek
2427 {
2428 int how;
2429
2430 fd_port = SCM_COERCE_OUTPORT (fd_port);
2431
2432 how = scm_to_int (whence);
2433 if (how != SEEK_SET && how != SEEK_CUR && how != SEEK_END)
2434 SCM_OUT_OF_RANGE (3, whence);
2435
2436 if (SCM_OPPORTP (fd_port))
2437 {
2438 scm_t_ptob_descriptor *ptob = SCM_PORT_DESCRIPTOR (fd_port);
2439 off_t_or_off64_t off = scm_to_off_t_or_off64_t (offset);
2440 off_t_or_off64_t rv;
2441
2442 if (!ptob->seek)
2443 SCM_MISC_ERROR ("port is not seekable",
2444 scm_cons (fd_port, SCM_EOL));
2445 else
2446 rv = ptob->seek (fd_port, off, how);
2447 return scm_from_off_t_or_off64_t (rv);
2448 }
2449 else /* file descriptor?. */
2450 {
2451 off_t_or_off64_t off = scm_to_off_t_or_off64_t (offset);
2452 off_t_or_off64_t rv;
2453 rv = lseek_or_lseek64 (scm_to_int (fd_port), off, how);
2454 if (rv == -1)
2455 SCM_SYSERROR;
2456 return scm_from_off_t_or_off64_t (rv);
2457 }
2458 }
2459 #undef FUNC_NAME
2460
2461 #ifndef O_BINARY
2462 #define O_BINARY 0
2463 #endif
2464
2465 /* Mingw has ftruncate(), perhaps implemented above using chsize, but
2466 doesn't have the filename version truncate(), hence this code. */
2467 #if HAVE_FTRUNCATE && ! HAVE_TRUNCATE
2468 static int
2469 truncate (const char *file, off_t length)
2470 {
2471 int ret, fdes;
2472
2473 fdes = open (file, O_BINARY | O_WRONLY);
2474 if (fdes == -1)
2475 return -1;
2476
2477 ret = ftruncate (fdes, length);
2478 if (ret == -1)
2479 {
2480 int save_errno = errno;
2481 close (fdes);
2482 errno = save_errno;
2483 return -1;
2484 }
2485
2486 return close (fdes);
2487 }
2488 #endif /* HAVE_FTRUNCATE && ! HAVE_TRUNCATE */
2489
2490 SCM_DEFINE (scm_truncate_file, "truncate-file", 1, 1, 0,
2491 (SCM object, SCM length),
2492 "Truncate file @var{object} to @var{length} bytes. @var{object}\n"
2493 "can be a filename string, a port object, or an integer file\n"
2494 "descriptor.\n"
2495 "The return value is unspecified.\n"
2496 "\n"
2497 "For a port or file descriptor @var{length} can be omitted, in\n"
2498 "which case the file is truncated at the current position (per\n"
2499 "@code{ftell} above).\n"
2500 "\n"
2501 "On most systems a file can be extended by giving a length\n"
2502 "greater than the current size, but this is not mandatory in the\n"
2503 "POSIX standard.")
2504 #define FUNC_NAME s_scm_truncate_file
2505 {
2506 int rv;
2507
2508 /* "object" can be a port, fdes or filename.
2509
2510 Negative "length" makes no sense, but it's left to truncate() or
2511 ftruncate() to give back an error for that (normally EINVAL).
2512 */
2513
2514 if (SCM_UNBNDP (length))
2515 {
2516 /* must supply length if object is a filename. */
2517 if (scm_is_string (object))
2518 SCM_MISC_ERROR("must supply length if OBJECT is a filename", SCM_EOL);
2519
2520 length = scm_seek (object, SCM_INUM0, scm_from_int (SEEK_CUR));
2521 }
2522
2523 object = SCM_COERCE_OUTPORT (object);
2524 if (scm_is_integer (object))
2525 {
2526 off_t_or_off64_t c_length = scm_to_off_t_or_off64_t (length);
2527 SCM_SYSCALL (rv = ftruncate_or_ftruncate64 (scm_to_int (object),
2528 c_length));
2529 }
2530 else if (SCM_OPOUTPORTP (object))
2531 {
2532 off_t_or_off64_t c_length = scm_to_off_t_or_off64_t (length);
2533 scm_t_port *pt = SCM_PTAB_ENTRY (object);
2534 scm_t_ptob_descriptor *ptob = SCM_PORT_DESCRIPTOR (object);
2535
2536 if (!ptob->truncate)
2537 SCM_MISC_ERROR ("port is not truncatable", SCM_EOL);
2538 if (pt->rw_active == SCM_PORT_READ)
2539 scm_end_input_unlocked (object);
2540 else if (pt->rw_active == SCM_PORT_WRITE)
2541 ptob->flush (object);
2542
2543 ptob->truncate (object, c_length);
2544 rv = 0;
2545 }
2546 else
2547 {
2548 off_t_or_off64_t c_length = scm_to_off_t_or_off64_t (length);
2549 char *str = scm_to_locale_string (object);
2550 int eno;
2551 SCM_SYSCALL (rv = truncate_or_truncate64 (str, c_length));
2552 eno = errno;
2553 free (str);
2554 errno = eno;
2555 }
2556 if (rv == -1)
2557 SCM_SYSERROR;
2558 return SCM_UNSPECIFIED;
2559 }
2560 #undef FUNC_NAME
2561
2562 SCM_DEFINE (scm_port_line, "port-line", 1, 0, 0,
2563 (SCM port),
2564 "Return the current line number for @var{port}.\n"
2565 "\n"
2566 "The first line of a file is 0. But you might want to add 1\n"
2567 "when printing line numbers, since starting from 1 is\n"
2568 "traditional in error messages, and likely to be more natural to\n"
2569 "non-programmers.")
2570 #define FUNC_NAME s_scm_port_line
2571 {
2572 port = SCM_COERCE_OUTPORT (port);
2573 SCM_VALIDATE_OPENPORT (1, port);
2574 return scm_from_long (SCM_LINUM (port));
2575 }
2576 #undef FUNC_NAME
2577
2578 SCM_DEFINE (scm_set_port_line_x, "set-port-line!", 2, 0, 0,
2579 (SCM port, SCM line),
2580 "Set the current line number for @var{port} to @var{line}. The\n"
2581 "first line of a file is 0.")
2582 #define FUNC_NAME s_scm_set_port_line_x
2583 {
2584 port = SCM_COERCE_OUTPORT (port);
2585 SCM_VALIDATE_OPENPORT (1, port);
2586 SCM_PTAB_ENTRY (port)->line_number = scm_to_long (line);
2587 return SCM_UNSPECIFIED;
2588 }
2589 #undef FUNC_NAME
2590
2591 SCM_DEFINE (scm_port_column, "port-column", 1, 0, 0,
2592 (SCM port),
2593 "Return the current column number of @var{port}.\n"
2594 "If the number is\n"
2595 "unknown, the result is #f. Otherwise, the result is a 0-origin integer\n"
2596 "- i.e. the first character of the first line is line 0, column 0.\n"
2597 "(However, when you display a file position, for example in an error\n"
2598 "message, we recommend you add 1 to get 1-origin integers. This is\n"
2599 "because lines and column numbers traditionally start with 1, and that is\n"
2600 "what non-programmers will find most natural.)")
2601 #define FUNC_NAME s_scm_port_column
2602 {
2603 port = SCM_COERCE_OUTPORT (port);
2604 SCM_VALIDATE_OPENPORT (1, port);
2605 return scm_from_int (SCM_COL (port));
2606 }
2607 #undef FUNC_NAME
2608
2609 SCM_DEFINE (scm_set_port_column_x, "set-port-column!", 2, 0, 0,
2610 (SCM port, SCM column),
2611 "Set the current column of @var{port}. Before reading the first\n"
2612 "character on a line the column should be 0.")
2613 #define FUNC_NAME s_scm_set_port_column_x
2614 {
2615 port = SCM_COERCE_OUTPORT (port);
2616 SCM_VALIDATE_OPENPORT (1, port);
2617 SCM_PTAB_ENTRY (port)->column_number = scm_to_int (column);
2618 return SCM_UNSPECIFIED;
2619 }
2620 #undef FUNC_NAME
2621
2622 SCM_DEFINE (scm_port_filename, "port-filename", 1, 0, 0,
2623 (SCM port),
2624 "Return the filename associated with @var{port}, or @code{#f}\n"
2625 "if no filename is associated with the port.")
2626 #define FUNC_NAME s_scm_port_filename
2627 {
2628 port = SCM_COERCE_OUTPORT (port);
2629 SCM_VALIDATE_OPENPORT (1, port);
2630 return SCM_FILENAME (port);
2631 }
2632 #undef FUNC_NAME
2633
2634 SCM_DEFINE (scm_set_port_filename_x, "set-port-filename!", 2, 0, 0,
2635 (SCM port, SCM filename),
2636 "Change the filename associated with @var{port}, using the current input\n"
2637 "port if none is specified. Note that this does not change the port's\n"
2638 "source of data, but only the value that is returned by\n"
2639 "@code{port-filename} and reported in diagnostic output.")
2640 #define FUNC_NAME s_scm_set_port_filename_x
2641 {
2642 port = SCM_COERCE_OUTPORT (port);
2643 SCM_VALIDATE_OPENPORT (1, port);
2644 /* We allow the user to set the filename to whatever he likes. */
2645 SCM_SET_FILENAME (port, filename);
2646 return SCM_UNSPECIFIED;
2647 }
2648 #undef FUNC_NAME
2649
2650
2651 \f
2652
2653 /* Implementation helpers for port printing functions. */
2654
2655 void
2656 scm_print_port_mode (SCM exp, SCM port)
2657 {
2658 scm_puts_unlocked (SCM_CLOSEDP (exp)
2659 ? "closed: "
2660 : (SCM_RDNG & SCM_CELL_WORD_0 (exp)
2661 ? (SCM_WRTNG & SCM_CELL_WORD_0 (exp)
2662 ? "input-output: "
2663 : "input: ")
2664 : (SCM_WRTNG & SCM_CELL_WORD_0 (exp)
2665 ? "output: "
2666 : "bogus: ")),
2667 port);
2668 }
2669
2670 int
2671 scm_port_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
2672 {
2673 char *type = SCM_PTOBNAME (SCM_PTOBNUM (exp));
2674 if (!type)
2675 type = "port";
2676 scm_puts_unlocked ("#<", port);
2677 scm_print_port_mode (exp, port);
2678 scm_puts_unlocked (type, port);
2679 scm_putc_unlocked (' ', port);
2680 scm_uintprint (SCM_CELL_WORD_1 (exp), 16, port);
2681 scm_putc_unlocked ('>', port);
2682 return 1;
2683 }
2684
2685
2686 \f
2687
2688 /* Iterating over all ports. */
2689
2690 struct for_each_data
2691 {
2692 void (*proc) (void *data, SCM p);
2693 void *data;
2694 };
2695
2696 static SCM
2697 for_each_trampoline (void *data, SCM port, SCM result)
2698 {
2699 struct for_each_data *d = data;
2700
2701 d->proc (d->data, port);
2702
2703 return result;
2704 }
2705
2706 void
2707 scm_c_port_for_each (void (*proc)(void *data, SCM p), void *data)
2708 {
2709 struct for_each_data d;
2710
2711 d.proc = proc;
2712 d.data = data;
2713
2714 scm_c_weak_set_fold (for_each_trampoline, &d, SCM_EOL,
2715 scm_i_port_weak_set);
2716 }
2717
2718 static void
2719 scm_for_each_trampoline (void *data, SCM port)
2720 {
2721 scm_call_1 (SCM_PACK_POINTER (data), port);
2722 }
2723
2724 SCM_DEFINE (scm_port_for_each, "port-for-each", 1, 0, 0,
2725 (SCM proc),
2726 "Apply @var{proc} to each port in the Guile port table\n"
2727 "in turn. The return value is unspecified. More specifically,\n"
2728 "@var{proc} is applied exactly once to every port that exists\n"
2729 "in the system at the time @code{port-for-each} is invoked.\n"
2730 "Changes to the port table while @code{port-for-each} is running\n"
2731 "have no effect as far as @code{port-for-each} is concerned.")
2732 #define FUNC_NAME s_scm_port_for_each
2733 {
2734 SCM_VALIDATE_PROC (1, proc);
2735
2736 scm_c_port_for_each (scm_for_each_trampoline, SCM_UNPACK_POINTER (proc));
2737
2738 return SCM_UNSPECIFIED;
2739 }
2740 #undef FUNC_NAME
2741
2742 static void
2743 flush_output_port (void *closure, SCM port)
2744 {
2745 if (SCM_OPOUTPORTP (port))
2746 scm_flush_unlocked (port);
2747 }
2748
2749 SCM_DEFINE (scm_flush_all_ports, "flush-all-ports", 0, 0, 0,
2750 (),
2751 "Equivalent to calling @code{force-output} on\n"
2752 "all open output ports. The return value is unspecified.")
2753 #define FUNC_NAME s_scm_flush_all_ports
2754 {
2755 scm_c_port_for_each (&flush_output_port, NULL);
2756 return SCM_UNSPECIFIED;
2757 }
2758 #undef FUNC_NAME
2759
2760
2761 \f
2762
2763 /* Void ports. */
2764
2765 scm_t_bits scm_tc16_void_port = 0;
2766
2767 static int fill_input_void_port (SCM port SCM_UNUSED)
2768 {
2769 return EOF;
2770 }
2771
2772 static void
2773 write_void_port (SCM port SCM_UNUSED,
2774 const void *data SCM_UNUSED,
2775 size_t size SCM_UNUSED)
2776 {
2777 }
2778
2779 static SCM
2780 scm_i_void_port (long mode_bits)
2781 {
2782 SCM ret;
2783
2784 ret = scm_c_make_port (scm_tc16_void_port, mode_bits, 0);
2785
2786 scm_port_non_buffer (SCM_PTAB_ENTRY (ret));
2787
2788 return ret;
2789 }
2790
2791 SCM
2792 scm_void_port (char *mode_str)
2793 {
2794 return scm_i_void_port (scm_mode_bits (mode_str));
2795 }
2796
2797 SCM_DEFINE (scm_sys_make_void_port, "%make-void-port", 1, 0, 0,
2798 (SCM mode),
2799 "Create and return a new void port. A void port acts like\n"
2800 "@file{/dev/null}. The @var{mode} argument\n"
2801 "specifies the input/output modes for this port: see the\n"
2802 "documentation for @code{open-file} in @ref{File Ports}.")
2803 #define FUNC_NAME s_scm_sys_make_void_port
2804 {
2805 return scm_i_void_port (scm_i_mode_bits (mode));
2806 }
2807 #undef FUNC_NAME
2808
2809
2810 \f
2811
2812 /* Initialization. */
2813
2814 void
2815 scm_init_ports ()
2816 {
2817 /* lseek() symbols. */
2818 scm_c_define ("SEEK_SET", scm_from_int (SEEK_SET));
2819 scm_c_define ("SEEK_CUR", scm_from_int (SEEK_CUR));
2820 scm_c_define ("SEEK_END", scm_from_int (SEEK_END));
2821
2822 scm_tc16_void_port = scm_make_port_type ("void", fill_input_void_port,
2823 write_void_port);
2824
2825 cur_inport_fluid = scm_make_fluid ();
2826 cur_outport_fluid = scm_make_fluid ();
2827 cur_errport_fluid = scm_make_fluid ();
2828 cur_loadport_fluid = scm_make_fluid ();
2829
2830 scm_i_port_weak_set = scm_c_make_weak_set (31);
2831
2832 #include "libguile/ports.x"
2833
2834 /* Use Latin-1 as the default port encoding. */
2835 SCM_VARIABLE_SET (default_port_encoding_var,
2836 scm_make_fluid_with_default (SCM_BOOL_F));
2837 scm_port_encoding_init = 1;
2838
2839 SCM_VARIABLE_SET (scm_conversion_strategy,
2840 scm_make_fluid_with_default
2841 (scm_from_int ((int) SCM_FAILED_CONVERSION_QUESTION_MARK)));
2842 scm_conversion_strategy_init = 1;
2843
2844 /* These bindings are used when boot-9 turns `current-input-port' et
2845 al into parameters. They are then removed from the guile module. */
2846 scm_c_define ("%current-input-port-fluid", cur_inport_fluid);
2847 scm_c_define ("%current-output-port-fluid", cur_outport_fluid);
2848 scm_c_define ("%current-error-port-fluid", cur_errport_fluid);
2849 }
2850
2851 /*
2852 Local Variables:
2853 c-file-style: "gnu"
2854 End:
2855 */