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: Tue, 31 Dec 2002 13:39:15 -0800

> Date: Tue, 31 Dec 2002 14:27:45 +0100 (CET)
> From: Bruno Haible <address@hidden>
> 
> Well, I made the change for two reasons:
>   1) the collisions,
>   2) the function names were not well chosen in the first place.

I agree with both points.  However, it'd be better to simply change
the names rather than try to support both the old (colliding) names
and the new (better-chosen, noncolliding) names, as that will avoid
confusion in the future, and it will let people use both gnulib and
the colliding names in the same module.

Also, nobody was invoking gnulib getstr directly as far as I know;
it was purely a helper function.  So it should be private anyway.

How about this untested patch to simplify things accordingly?  I don't
really know how the module stuff works yet, so the modules changes are
a bit of a guess.

2002-12-31  Paul Eggert  <address@hidden>

        * lib/getstr.h, lib/getstr.c, m4/getstr.m4, modules/getstr:
        Remove these files.
        * MODULES.html.sh: Remove getstr.
        * MODULES.txt: Likewise.  Merge its dependencies into getline.
        * lib/Makefile.am (libfetish_a_SOURCES): Remove getstr.c, getstr.h.
        * 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.
        * m4/getline.m4 (AM_FUNC_GETLINE): Require AC_HEADER_STDC.
        * modules/getline: Depend on unlocked-io, not getstr

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

Index: MODULES.html.sh
===================================================================
RCS file: /cvsroot/gnulib/gnulib/MODULES.html.sh,v
retrieving revision 1.1
diff -p -u -r1.1 MODULES.html.sh
--- MODULES.html.sh     31 Dec 2002 13:47:46 -0000      1.1
+++ MODULES.html.sh     31 Dec 2002 21:30:53 -0000
@@ -299,7 +299,6 @@ func_echo "$element"
 
 func_begin_table
 func_module diacrit
-func_module getstr
 func_module getline
 func_module linebuffer
 func_module obstack
Index: MODULES.txt
===================================================================
RCS file: /cvsroot/gnulib/gnulib/MODULES.txt,v
retrieving revision 1.5
diff -p -u -r1.5 MODULES.txt
--- MODULES.txt 31 Dec 2002 13:49:58 -0000      1.5
+++ MODULES.txt 31 Dec 2002 21:30:53 -0000
@@ -291,12 +291,11 @@ Header          Implementation        Au
 
 diacrit.h       diacrit.c             ---                         ---
 
-getstr.h        getstr.c              AC_STDC_HEADERS             unlocked-io
-
-getline.h       getline.c             getline.m4                  getstr
+getline.h       getline.c             getline.m4                  unlocked-io
                                       AM_FUNC_GETLINE
                                       AC_CHECK_FUNCS(getdelim)
                                       AC_GNU_SOURCE
+                                     AC_STDC_HEADERS
 
 linebuffer.h    linebuffer.c          ---                         xalloc
                                                                   unlocked-io
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     31 Dec 2002 21:30:53 -0000
@@ -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       31 Dec 2002 21:30:53 -0000
@@ -22,6 +22,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 +43,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/getstr.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/getstr.c,v
retrieving revision 1.3
diff -p -u -r1.3 getstr.c
--- lib/getstr.c        9 Dec 2001 22:08:19 -0000       1.3
+++ lib/getstr.c        31 Dec 2002 21:30:53 -0000
@@ -1,115 +0,0 @@
-/* getstr.c -- core function for GNU C library getline replacement function
-
-   Copyright (C) 1993, 1996-2001 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. */
-
-/* Written by Jan Brittenson, address@hidden  */
-
-#if HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <stdio.h>
-#include <sys/types.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.  */
-
-int
-getstr (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;
-}
Index: lib/getstr.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/getstr.h,v
retrieving revision 1.2
diff -p -u -r1.2 getstr.h
--- lib/getstr.h        31 Dec 2002 12:59:53 -0000      1.2
+++ lib/getstr.h        31 Dec 2002 21:30:53 -0000
@@ -1,22 +0,0 @@
-#ifndef GETSTR_H_
-# define GETSTR_H_ 1
-
-# include <stdio.h>
-
-# ifndef PARAMS
-#  if defined PROTOTYPES || (defined __STDC__ && __STDC__)
-#   define PARAMS(Args) Args
-#  else
-#   define PARAMS(Args) ()
-#  endif
-# endif
-
-/* Avoid collision with getstr() from libcurses.  */
-#define getstr getdelim2
-
-int
-getstr PARAMS ((char **lineptr, size_t *n, FILE *stream,
-               int delim1, int delim2,
-               size_t offset));
-
-#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    31 Dec 2002 21:30:53 -0000
@@ -47,7 +47,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    31 Dec 2002 21:30:53 -0000
@@ -35,9 +35,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 +42,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       31 Dec 2002 21:30:53 -0000
@@ -14,6 +14,8 @@ dnl have a function by that name in -lin
 dnl to do with the function we need.
 AC_DEFUN([AM_FUNC_GETLINE],
 [
+  dnl Prerequisites of lib/getline.c.
+  AC_REQUIRE([AC_HEADER_STDC])
   dnl Persuade glibc <stdio.h> to declare getline() and getdelim().
   AC_REQUIRE([AC_GNU_SOURCE])
 
Index: m4/getstr.m4
===================================================================
RCS file: /cvsroot/gnulib/gnulib/m4/getstr.m4,v
retrieving revision 1.1
diff -p -u -r1.1 getstr.m4
--- m4/getstr.m4        31 Dec 2002 13:42:06 -0000      1.1
+++ m4/getstr.m4        31 Dec 2002 21:30:53 -0000
@@ -1,13 +0,0 @@
-# getstr.m4 serial 1
-dnl Copyright (C) 2002 Free Software 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
-dnl that contains a configuration script generated by Autoconf, under
-dnl the same distribution terms as the rest of that program.
-
-AC_DEFUN([gl_GETSTR],
-[
-  dnl Prerequisites of lib/getstr.c.
-  AC_REQUIRE([AC_HEADER_STDC])
-])
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     31 Dec 2002 21:30:53 -0000
@@ -6,7 +6,7 @@ lib/getline.c
 m4/getline.m4
 
 Depends-on:
-getstr
+unlocked-io
 
 configure.ac:
 AM_FUNC_GETLINE
Index: modules/getstr
===================================================================
RCS file: /cvsroot/gnulib/gnulib/modules/getstr,v
retrieving revision 1.1
diff -p -u -r1.1 getstr
--- modules/getstr      31 Dec 2002 13:46:30 -0000      1.1
+++ modules/getstr      31 Dec 2002 21:30:53 -0000
@@ -1,19 +0,0 @@
-Description:
-
-Files:
-lib/getstr.h
-lib/getstr.c
-m4/getstr.m4
-
-Depends-on:
-unlocked-io
-
-configure.ac:
-gl_GETSTR
-
-Makefile.am:
-lib_SOURCES += getstr.h getstr.c
-
-Include:
-"getstr.h"
-



reply via email to

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