From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Andreas Rottmann Newsgroups: gmane.lisp.guile.devel Subject: Implementing R6RS `transcoded-port' Date: Sun, 25 Jul 2010 21:12:22 +0200 Message-ID: <87eiere5pl.fsf@delenn.lan> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: dough.gmane.org 1280085289 19568 80.91.229.12 (25 Jul 2010 19:14:49 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Sun, 25 Jul 2010 19:14:49 +0000 (UTC) To: Guile Development Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Sun Jul 25 21:14:44 2010 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Od6ej-0002cI-5p for guile-devel@m.gmane.org; Sun, 25 Jul 2010 21:14:40 +0200 Original-Received: from localhost ([127.0.0.1]:51166 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Od6eg-0006Vd-5S for guile-devel@m.gmane.org; Sun, 25 Jul 2010 15:14:34 -0400 Original-Received: from [140.186.70.92] (port=58761 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Od6ea-0006TI-TT for guile-devel@gnu.org; Sun, 25 Jul 2010 15:14:30 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1Od6ci-0000qq-Ls for guile-devel@gnu.org; Sun, 25 Jul 2010 15:12:33 -0400 Original-Received: from mail.gmx.net ([213.165.64.20]:40819) by eggs.gnu.org with smtp (Exim 4.69) (envelope-from ) id 1Od6ci-0000qV-6h for guile-devel@gnu.org; Sun, 25 Jul 2010 15:12:32 -0400 Original-Received: (qmail invoked by alias); 25 Jul 2010 19:12:29 -0000 Original-Received: from 83-215-154-5.hage.dyn.salzburg-online.at (EHLO nathot.lan) [83.215.154.5] by mail.gmx.net (mp057) with SMTP; 25 Jul 2010 21:12:29 +0200 X-Authenticated: #3102804 X-Provags-ID: V01U2FsdGVkX18PydRd4N4P+WbHI9Z6QxXGVLKtfgleUbuK9viEJ2 sUaZRUviCRa1X7 Original-Received: from localhost (localhost.localdomain [127.0.0.1]) by nathot.lan (Postfix) with ESMTP id 893DC3A695 for ; Sun, 25 Jul 2010 21:12:28 +0200 (CEST) Original-Received: from nathot.lan ([127.0.0.1]) by localhost (nathot.lan [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id xLl4kcYymZyX for ; Sun, 25 Jul 2010 21:12:23 +0200 (CEST) Original-Received: from delenn.lan (delenn.lan [192.168.3.11]) by nathot.lan (Postfix) with ESMTP id 909963A693 for ; Sun, 25 Jul 2010 21:12:23 +0200 (CEST) Original-Received: by delenn.lan (Postfix, from userid 1000) id 4D94674ECF; Sun, 25 Jul 2010 21:12:23 +0200 (CEST) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux) X-Y-GMX-Trusted: 0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:10731 Archived-At: Hi! I've done a bit of work on (rnrs io ports), and a big stumbling block is `transcoded-port': ,---- | (transcoded-port binary-port transcoder) procedure=20 |=20 | The transcoded-port procedure returns a new textual port with the | specified transcoder. Otherwise the new textual port=E2=80=99s state is | largely the same as that of binary-port. If binary-port is an input | port, the new textual port will be an input port and will transcode | the bytes that have not yet been read from binary-port. If binary-port | is an output port, the new textual port will be an output port and | will transcode output characters into bytes that are written to the | byte sink represented by binary-port. |=20 | As a side effect, however, transcoded-port closes binary-port in a | special way that allows the new textual port to continue to use the | byte source or sink represented by binary-port, even though | binary-port itself is closed and cannot be used by the input and | output operations described in this chapter. `---- According to this specification, one essentially needs to "copy" a port (including buffers), mark the source of the copy as closed and adjust the encoding of the copy. I've been trying to do so with the following code, but that crashes in GC -- somehow the whole scm_t_port attached to the SCM port seems to get cleared to 0, and I don't see why. I'd be glad if someone could take a look at the code and perhaps hint me in the right direction. First the test program: ;; --------8<-------------- (import (rnrs) (only (guile) gc)) (do ((i 0 (+ i 1))) ((>=3D i 5)) (let* ((stdout (standard-output-port)) (stdout-t (transcoded-port stdout (native-transcoder)))) (display "Hello!\n" stdout-t))) (gc) ;; <-- crashes here ;; --------8<-------------- And the C code used to implement `transcoded-port' -- I'm probably doing something stupid in here: SCM_DEFINE (scm_transcoded_port, "transcoded-port", 2, 0, 0, (SCM port, SCM transcoder), "") #define FUNC_NAME s_scm_transcoded_port { SCM codec; port =3D SCM_COERCE_OUTPORT (port); =20=20 SCM_VALIDATE_PORT (SCM_ARG1, port); SCM_VALIDATE_STRUCT (SCM_ARG1, transcoder); codec =3D scm_struct_ref (transcoder, scm_from_int8 (2)); =20=20 scm_i_scm_pthread_mutex_lock (&scm_i_port_table_mutex); { scm_t_bits type =3D SCM_CELL_TYPE (port); SCM modes =3D scm_port_mode (port); SCM result =3D scm_new_port_table_entry (type & 0xffff); scm_t_port * result_pt =3D SCM_PTAB_ENTRY (result); scm_t_port * pt =3D SCM_PTAB_ENTRY (port); printf ("XXX: created port %p from %p\n", result_pt, pt); =20=20=20=20 result_pt->revealed =3D pt->revealed; SCM_SETSTREAM (result, SCM_STREAM (port)); result_pt->file_name =3D pt->file_name; result_pt->line_number =3D pt->line_number; result_pt->column_number =3D pt->column_number; =20=20=20=20 result_pt->read_buf =3D pt->read_buf; result_pt->read_pos =3D pt->read_pos; result_pt->read_end =3D pt->read_end; result_pt->read_buf_size =3D pt->read_buf_size; =20=20=20=20 result_pt->saved_read_buf =3D pt->saved_read_buf; result_pt->saved_read_pos =3D pt->saved_read_pos; result_pt->saved_read_end =3D pt->saved_read_end; result_pt->saved_read_buf_size =3D pt->saved_read_buf_size; result_pt->write_buf =3D pt->write_buf; result_pt->write_pos =3D pt->write_pos; result_pt->write_end =3D pt->write_end; result_pt->write_buf_size =3D pt->write_buf_size; result_pt->shortbuf =3D pt->shortbuf; result_pt->rw_random =3D pt->rw_random; result_pt->rw_active =3D pt->rw_active; result_pt->putback_buf =3D pt->putback_buf; result_pt->putback_buf_size =3D pt->putback_buf_size;=20 =20=20=20 scm_i_remove_port (port); SCM_CLR_PORT_OPEN_FLAG (port); SCM_SET_CELL_TYPE (result, (type & 0xffff) | scm_i_mode_bits (modes)); scm_i_pthread_mutex_unlock (&scm_i_port_table_mutex); scm_set_port_encoding_x (result, codec); =20=20=20=20 return result; } } #undef FUNC_NAME Regards, Rotty --=20 Andreas Rottmann --