From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: ludo@gnu.org (Ludovic =?utf-8?Q?Court=C3=A8s?=) Newsgroups: gmane.lisp.guile.devel Subject: [PATCH] Recognize '\r' for line buffering purposes Date: Fri, 16 May 2014 11:39:39 +0200 Message-ID: <87r43udpus.fsf@gnu.org> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1400233296 26817 80.91.229.3 (16 May 2014 09:41:36 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 16 May 2014 09:41:36 +0000 (UTC) To: guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Fri May 16 11:41:30 2014 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1WlEdt-00065R-Bl for guile-devel@m.gmane.org; Fri, 16 May 2014 11:41:29 +0200 Original-Received: from localhost ([::1]:34456 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WlEds-0006qr-Td for guile-devel@m.gmane.org; Fri, 16 May 2014 05:41:28 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:60512) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WlEcV-0004lQ-1r for guile-devel@gnu.org; Fri, 16 May 2014 05:40:08 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WlEcN-0001pz-UE for guile-devel@gnu.org; Fri, 16 May 2014 05:40:03 -0400 Original-Received: from plane.gmane.org ([80.91.229.3]:60946) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WlEcN-0001pm-LL for guile-devel@gnu.org; Fri, 16 May 2014 05:39:55 -0400 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1WlEcK-0001bM-Qw for guile-devel@gnu.org; Fri, 16 May 2014 11:39:52 +0200 Original-Received: from pluto.bordeaux.inria.fr ([193.50.110.57]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 16 May 2014 11:39:52 +0200 Original-Received: from ludo by pluto.bordeaux.inria.fr with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 16 May 2014 11:39:52 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 112 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: pluto.bordeaux.inria.fr X-URL: http://www.fdn.fr/~lcourtes/ X-Revolutionary-Date: 27 =?utf-8?Q?Flor=C3=A9al?= an 222 de la =?utf-8?Q?R?= =?utf-8?Q?=C3=A9volution?= X-PGP-Key-ID: 0xEA52ECF4 X-PGP-Key: http://www.fdn.fr/~lcourtes/ludovic.asc X-PGP-Fingerprint: 83C4 F8E5 10A3 3B4C 5BEA D15D 77DD 95E2 EA52 ECF4 X-OS: x86_64-unknown-linux-gnu User-Agent: Gnus/5.130009 (Ma Gnus v0.9) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:lb+egN6NgpY4PpyzZL6QqDtuFxM= X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.91.229.3 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:17145 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit WDYT? I’ll push it shortly if there are no objections. Ludo’. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Recognize-r-as-a-line-ending-for-line-buffering-purp.patch Content-Description: the patch >From 8cc67b1087c32b4156668bfce8856306d7c23e1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludovic=20Court=C3=A8s?= Date: Fri, 16 May 2014 11:38:17 +0200 Subject: [PATCH] Recognize '\r' as a line ending for line-buffering purposes. * libguile/fports.c (contains_newline): New function. (fport_write): Use it when PORT has the SCM_BUFLINE flag. * test-suite/tests/ports.test ("pipe, fdopen, and _IOLBF"): New test. --- libguile/fports.c | 25 +++++++++++++++++++++++-- test-suite/tests/ports.test | 23 +++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/libguile/fports.c b/libguile/fports.c index 29edc51..981e2af 100644 --- a/libguile/fports.c +++ b/libguile/fports.c @@ -736,6 +736,24 @@ fport_truncate (SCM port, scm_t_off length) scm_syserror ("ftruncate"); } +/* Return true if STR contains a newline character. */ +static int +contains_newline (const char *str, size_t len) +{ + size_t i; + + for (i = 0; i < len; i++) + { + /* Honor both characters regardless of the end-of-line style. On + Unix-style systems, carriage return goes to the beginning of + the line, so it can be considered a line ending. */ + if (str[i] == '\n' || str[i] == '\r') + return 1; + } + + return 0; +} + static void fport_write (SCM port, const void *data, size_t size) #define FUNC_NAME "fport_write" @@ -793,8 +811,11 @@ fport_write (SCM port, const void *data, size_t size) } } - /* handle line buffering. */ - if ((SCM_CELL_WORD_0 (port) & SCM_BUFLINE) && memchr (data, '\n', size)) + /* Handle line buffering. */ + if ((SCM_CELL_WORD_0 (port) & SCM_BUFLINE) + && contains_newline (data, size)) + /* XXX: We're flushing the whole buffer, including what's after + the newline character(s). */ fport_flush (port); } } diff --git a/test-suite/tests/ports.test b/test-suite/tests/ports.test index c1a185f..f453aa4 100644 --- a/test-suite/tests/ports.test +++ b/test-suite/tests/ports.test @@ -623,6 +623,29 @@ (equal? in-string "Mommy, why does everybody have a bomb?\n"))) (delete-file filename)) +(pass-if-equal "pipe, fdopen, and _IOLBF" + "foo\nbar\r" + (let ((in+out (pipe)) + (pid (primitive-fork))) + (if (zero? pid) + (dynamic-wind + (const #t) + (lambda () + (close (car in+out)) + (let ((port (fdopen (fileno (cdr in+out)) "wl"))) + ;; Strings containing '\n' or '\r' should be flushed; + ;; others should be kept in PORT's buffer. + (display "foo\n" port) + (display "bar\r" port) + (display "this will be kept in PORT's buffer" port))) + (lambda () + (primitive-_exit 0))) + (begin + (close (cdr in+out)) + (let ((str (read-all (car in+out)))) + (waitpid pid) + str))))) + ;;;; Void ports. These are so trivial we don't test them. -- 1.8.4 --=-=-=--