bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-gnulib] getstr(), readline()


From: Paul Eggert
Subject: Re: [Bug-gnulib] getstr(), readline()
Date: Sun, 12 Jan 2003 23:16:40 -0800 (PST)

> Date: Tue, 7 Jan 2003 14:35:02 +0100 (CET)
> From: Bruno Haible <address@hidden>
> 
> Similarly, I think we should now remove lib/Makefile.am. All its
> information is contained in the module descriptions, and
> "gnulib-tool --create-testdir" generates a lib/Makefile.am just fine.

OK, but how would you bootstrap after a CVS checkin?  I haven't played
with that, and I'm a bit reluctant to mess with it without testing it
since lib/Makefile.am is more or less a coreutils thing, so for now I
left in the lib/Makefile.am change.  I installed the following patch,
which reflects your other comments.

2003-01-12  Paul Eggert  <address@hidden>

        Finish renaming getstr -> getdelim2 and readline -> readlinebuffer,
        to avoid collisions with libcurses and libreadline.

        * getstr: Remove.
        * MODULES.html.sh: Remove getstr.

        * lib/Makefile.am (libfetish_a_SOURCES): Remove getstr.c, getstr.h.
        * lib/getstr.h, lib/getstr.c: Remove.
        * lib/getline.c: Include "getline.h", to check interface.
        Move body of old getstr.c here: this defines MIN_CHUNK and
        declares getdelim2, which is renamed from getstr.
        (getline, getdelim): Adjust to renaming of getstr -> getdelim2.

        * lib/linebuffer.c (readlinebuffer): Renamed from readline.
        All uses changed.
        * lib/linebuffer.h: Likewise.
        (readline): Remove backward-compatibility macro.

        * m4/getstr.m4: Remove.
        * m4/getline.m4 (gl_PREREQ_GETLINE): Require AC_HEADER_STDC.

        * modules/getline: Depend on unlocked-io, not getstr.

Index: MODULES.html.sh
===================================================================
RCS file: /cvsroot/gnulib/gnulib/MODULES.html.sh,v
retrieving revision 1.4
diff -p -u -r1.4 MODULES.html.sh
--- MODULES.html.sh     9 Jan 2003 13:22:53 -0000       1.4
+++ MODULES.html.sh     13 Jan 2003 07:02:06 -0000
@@ -323,7 +323,6 @@ func_echo "$element"
 
 func_begin_table
 func_module diacrit
-func_module getstr
 func_module getline
 func_module linebuffer
 func_module obstack
Index: lib/Makefile.am
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/Makefile.am,v
retrieving revision 1.134
diff -p -u -r1.134 Makefile.am
--- lib/Makefile.am     11 Dec 2002 20:34:41 -0000      1.134
+++ lib/Makefile.am     13 Jan 2003 07:02:07 -0000
@@ -1,7 +1,7 @@
 ## Makefile for gnulib/lib                             -*-Makefile-*-
 
-# Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002 Free
-# Software Foundation, Inc.
+# Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
+# Free Software Foundation, Inc.
 
 ## This program is free software; you can redistribute it and/or modify
 ## it under the terms of the GNU General Public License as published by
@@ -62,7 +62,6 @@ libfetish_a_SOURCES = \
   full-write.c full-write.h \
   getline.h \
   getpagesize.h \
-  getstr.c getstr.h \
   gettime.c \
   gettext.h \
   getugroups.c \
Index: lib/getline.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/getline.c,v
retrieving revision 1.10
diff -p -u -r1.10 getline.c
--- lib/getline.c       21 Jun 2000 09:56:42 -0000      1.10
+++ lib/getline.c       13 Jan 2003 07:02:07 -0000
@@ -1,20 +1,21 @@
 /* getline.c -- Replacement for GNU C library function getline
 
-Copyright (C) 1993, 1996, 1997, 1998, 2000 Free Software Foundation, Inc.
+   Copyright (C) 1993, 1996, 1997, 1998, 2000, 2003 Free Software
+   Foundation, Inc.
 
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License as
-published by the Free Software Foundation; either version 2 of the
-License, or (at your option) any later version.
-
-This program is distributed in the hope that it will be useful, but
-WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 /* Written by Jan Brittenson, address@hidden  */
 
@@ -22,6 +23,8 @@ Foundation, Inc., 59 Temple Place - Suit
 # include <config.h>
 #endif
 
+#include "getline.h"
+
 /* The `getdelim' function is only declared if the following symbol
    is defined.  */
 #ifndef _GNU_SOURCE
@@ -41,17 +44,105 @@ getline (char **lineptr, size_t *n, FILE
 
 #else /* ! have getdelim */
 
-# include "getstr.h"
+# if STDC_HEADERS
+#  include <stdlib.h>
+# else
+char *malloc (), *realloc ();
+# endif
+
+#include "unlocked-io.h"
+
+/* Always add at least this many bytes when extending the buffer.  */
+#define MIN_CHUNK 64
+
+/* Read up to (and including) a delimiter DELIM1 from STREAM into *LINEPTR
+   + OFFSET (and NUL-terminate it).  If DELIM2 is non-zero, then read up
+   and including the first occurrence of DELIM1 or DELIM2.  *LINEPTR is
+   a pointer returned from malloc (or NULL), pointing to *N characters of
+   space.  It is realloc'd as necessary.  Return the number of characters
+   read (not including the NUL terminator), or -1 on error or EOF.  */
+
+static int
+getdelim2 (char **lineptr, size_t *n, FILE *stream, int delim1, int delim2,
+          size_t offset)
+{
+  size_t nchars_avail;         /* Allocated but unused chars in *LINEPTR.  */
+  char *read_pos;              /* Where we're reading into *LINEPTR. */
+  int ret;
+
+  if (!lineptr || !n || !stream)
+    return -1;
+
+  if (!*lineptr)
+    {
+      *n = MIN_CHUNK;
+      *lineptr = malloc (*n);
+      if (!*lineptr)
+       return -1;
+    }
+
+  if (*n < offset)
+    return -1;
+
+  nchars_avail = *n - offset;
+  read_pos = *lineptr + offset;
+
+  for (;;)
+    {
+      register int c = getc (stream);
+
+      /* We always want at least one char left in the buffer, since we
+        always (unless we get an error while reading the first char)
+        NUL-terminate the line buffer.  */
+
+      if (nchars_avail < 2)
+       {
+         if (*n > MIN_CHUNK)
+           *n *= 2;
+         else
+           *n += MIN_CHUNK;
+
+         nchars_avail = *n + *lineptr - read_pos;
+         *lineptr = realloc (*lineptr, *n);
+         if (!*lineptr)
+           return -1;
+         read_pos = *n - nchars_avail + *lineptr;
+       }
+
+      if (c == EOF || ferror (stream))
+       {
+         /* Return partial line, if any.  */
+         if (read_pos == *lineptr)
+           return -1;
+         else
+           break;
+       }
+
+      *read_pos++ = c;
+      nchars_avail--;
+
+      if (c == delim1 || (delim2 && c == delim2))
+       /* Return the line.  */
+       break;
+    }
+
+  /* Done - NUL terminate and return the number of chars read.  */
+  *read_pos = '\0';
+
+  ret = read_pos - (*lineptr + offset);
+  return ret;
+}
+
 
 int
 getline (char **lineptr, size_t *n, FILE *stream)
 {
-  return getstr (lineptr, n, stream, '\n', 0, 0);
+  return getdelim2 (lineptr, n, stream, '\n', 0, 0);
 }
 
 int
 getdelim (char **lineptr, size_t *n, int delimiter, FILE *stream)
 {
-  return getstr (lineptr, n, stream, delimiter, 0, 0);
+  return getdelim2 (lineptr, n, stream, delimiter, 0, 0);
 }
 #endif
Index: lib/linebuffer.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/linebuffer.c,v
retrieving revision 1.12
diff -p -u -r1.12 linebuffer.c
--- lib/linebuffer.c    10 Dec 2001 00:13:36 -0000      1.12
+++ lib/linebuffer.c    13 Jan 2003 07:02:07 -0000
@@ -1,5 +1,7 @@
 /* linebuffer.c -- read arbitrarily long lines
-   Copyright (C) 1986, 1991, 1998, 1999, 2001 Free Software Foundation, Inc.
+
+   Copyright (C) 1986, 1991, 1998, 1999, 2001, 2003 Free Software
+   Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -47,7 +49,7 @@ initbuffer (struct linebuffer *linebuffe
    Return NULL upon error, or when STREAM is empty.
    Otherwise, return LINEBUFFER.  */
 struct linebuffer *
-readline (struct linebuffer *linebuffer, FILE *stream)
+readlinebuffer (struct linebuffer *linebuffer, FILE *stream)
 {
   int c;
   char *buffer = linebuffer->buffer;
Index: lib/linebuffer.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/linebuffer.h,v
retrieving revision 1.11
diff -p -u -r1.11 linebuffer.h
--- lib/linebuffer.h    31 Dec 2002 12:59:53 -0000      1.11
+++ lib/linebuffer.h    13 Jan 2003 07:02:07 -0000
@@ -1,5 +1,7 @@
 /* linebuffer.h -- declarations for reading arbitrarily long lines
-   Copyright (C) 1986, 1991, 1998, 1999, 2002 Free Software Foundation, Inc.
+
+   Copyright (C) 1986, 1991, 1998, 1999, 2002, 2003 Free Software
+   Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -35,9 +37,6 @@ struct linebuffer
 #  endif
 # endif
 
-/* Avoid collision with readline() from libreadline.  */
-#define readline readlinebuffer
-
 /* Initialize linebuffer LINEBUFFER for use. */
 void initbuffer PARAMS ((struct linebuffer *linebuffer));
 
@@ -45,8 +44,8 @@ void initbuffer PARAMS ((struct linebuff
    Keep the newline; append a newline if it's the last line of a file
    that ends in a non-newline character.  Do not null terminate.
    Return LINEBUFFER, except at end of file return 0.  */
-struct linebuffer *readline PARAMS ((struct linebuffer *linebuffer,
-                                    FILE *stream));
+struct linebuffer *readlinebuffer PARAMS ((struct linebuffer *linebuffer,
+                                          FILE *stream));
 
 /* Free linebuffer LINEBUFFER and its data, all allocated with malloc. */
 void freebuffer PARAMS ((struct linebuffer *));
Index: m4/getline.m4
===================================================================
RCS file: /cvsroot/gnulib/gnulib/m4/getline.m4,v
retrieving revision 1.10
diff -p -u -r1.10 getline.m4
--- m4/getline.m4       31 Dec 2002 13:43:06 -0000      1.10
+++ m4/getline.m4       13 Jan 2003 07:02:08 -0000
@@ -1,5 +1,8 @@
-# getline.m4 serial 7
-dnl Copyright (C) 1998-2002 Free Software Foundation, Inc.
+# getline.m4 serial 8
+
+dnl Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software
+dnl Foundation, Inc.
+
 dnl This file is free software, distributed under the terms of the GNU
 dnl General Public License.  As a special exception to the GNU General
 dnl Public License, this file may be distributed as part of a program
@@ -60,5 +63,6 @@ AC_DEFUN([AM_FUNC_GETLINE],
 # Prerequisites of lib/getline.c.
 AC_DEFUN([gl_PREREQ_GETLINE],
 [
+  AC_REQUIRE([AC_HEADER_STDC])
   AC_CHECK_FUNCS(getdelim)
 ])
Index: modules/getline
===================================================================
RCS file: /cvsroot/gnulib/gnulib/modules/getline,v
retrieving revision 1.1
diff -p -u -r1.1 getline
--- modules/getline     31 Dec 2002 13:46:30 -0000      1.1
+++ modules/getline     13 Jan 2003 07:02:08 -0000
@@ -6,7 +6,7 @@ lib/getline.c
 m4/getline.m4
 
 Depends-on:
-getstr
+unlocked-io
 
 configure.ac:
 AM_FUNC_GETLINE




reply via email to

[Prev in Thread] Current Thread [Next in Thread]