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