]> git.draconx.ca Git - dxcommon.git/commitdiff
Import getline helper from cdecl99. master
authorNick Bowler <nbowler@draconx.ca>
Sun, 12 May 2024 18:05:47 +0000 (14:05 -0400)
committerNick Bowler <nbowler@draconx.ca>
Mon, 13 May 2024 02:06:54 +0000 (22:06 -0400)
This is pretty generally useful.  The common function is altered
from the original version to allow the caller to manage the error
conditions, so not completely a drop in replacement but pretty close.

Makefile.am
m4/getline.m4 [new file with mode: 0644]
src/getline.h [new file with mode: 0644]
t/.gitignore
t/getline.c [new file with mode: 0644]
t/libdummy.c
tests/functions.at
tests/libs.at

index be7305626f439b1dadd906283ad4580a28793e7d..377ca022e7bccc040600aa28b0973cbaa3500f49 100644 (file)
@@ -1,7 +1,7 @@
-# Copyright © 2015, 2019, 2021-2023 Nick Bowler
+# Copyright © 2015, 2019, 2021-2024 Nick Bowler
 #
 #
-# License WTFPL2: Do What The Fuck You Want To Public License, version 2.
-# This is free software: you are free to do what the fuck you want to.
+# License GPLv3+: GNU General Public License version 3 or any later version.
+# This is free software: you are free to change and redistribute it.
 # There is NO WARRANTY, to the extent permitted by law.
 
 ACLOCAL_AMFLAGS = -I m4
 # There is NO WARRANTY, to the extent permitted by law.
 
 ACLOCAL_AMFLAGS = -I m4
@@ -11,9 +11,9 @@ AM_CPPFLAGS = -I$(top_srcdir)/src $(STUB_INCLUDES)
 EXTRA_DIST = scripts/fix-gnulib.pl scripts/fix-ltdl.pl \
              scripts/bake-config.awk scripts/gen-options.awk \
              scripts/gen-strtab.awk scripts/gen-tree.awk scripts/join.awk \
 EXTRA_DIST = scripts/fix-gnulib.pl scripts/fix-ltdl.pl \
              scripts/bake-config.awk scripts/gen-options.awk \
              scripts/gen-strtab.awk scripts/gen-tree.awk scripts/join.awk \
-             scripts/pe-subsys.awk src/copysym.h src/help.h src/pack.h \
-             src/tap.h t/tapcheck.sh t/getopt/getopt.h t/nls/gettext.h \
-             t/nls/mbswidth.h tests/data/gnulib.mk
+             scripts/pe-subsys.awk src/copysym.h src/getline.h src/help.h \
+             src/pack.h src/tap.h t/tapcheck.sh t/getopt/getopt.h \
+             t/nls/gettext.h t/nls/mbswidth.h tests/data/gnulib.mk
 
 check_LIBRARIES = t/libdummy.a t/libempty.a
 
 
 check_LIBRARIES = t/libdummy.a t/libempty.a
 
@@ -60,6 +60,9 @@ libnlscopysym_a_SOURCES = src/copysym.c
 libnlscopysym_a_CPPFLAGS = $(AM_CPPFLAGS) -DENABLE_NLS
 libnlscopysym_a_SHORTNAME = nls
 
 libnlscopysym_a_CPPFLAGS = $(AM_CPPFLAGS) -DENABLE_NLS
 libnlscopysym_a_SHORTNAME = nls
 
+check_PROGRAMS += t/getline
+t_getline_SOURCES = t/getline.c src/tap.c
+
 DISTCLEANFILES =
 SUFFIXES =
 
 DISTCLEANFILES =
 SUFFIXES =
 
diff --git a/m4/getline.m4 b/m4/getline.m4
new file mode 100644 (file)
index 0000000..8356a62
--- /dev/null
@@ -0,0 +1,31 @@
+# Copyright © 2024 Nick Bowler
+#
+# 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 3 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, see <https://www.gnu.org/licenses/>.
+
+# DX_CHECK_GETLINE
+#
+# Check whether or not the getline function is available.  If it is, the macro
+# HAVE_GETLINE is defined to 1 and the cache variable dx_cv_have_getline is set
+# to "yes".  Otherwise, dx_cv_have_getline is set to "no".
+AC_DEFUN([DX_CHECK_GETLINE],
+[AC_CACHE_CHECK([for getline], [dx_cv_have_getline],
+  [AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <stdio.h>
+], [ssize_t (*x)() = getline;
+char *p = 0;
+size_t n = 0;
+return getline(&p, &n, stdin);
+])], [dx_cv_have_getline=yes], [dx_cv_have_getline=no])])
+AS_CASE([$dx_cv_have_getline], [yes],
+  [AC_DEFINE([HAVE_GETLINE], [1],
+    [Define to 1 if the getline function is available.])])])
diff --git a/src/getline.h b/src/getline.h
new file mode 100644 (file)
index 0000000..b0f4999
--- /dev/null
@@ -0,0 +1,121 @@
+/*
+ * Copyright © 2024 Nick Bowler
+ *
+ * getline-like function which removes trailing newline (if any).
+ *
+ * If HAVE_GETLINE is not defined (or defined to 0) then a standard C
+ * implementation is used.  Othewrise, the POSIX getline function
+ * is called.
+ *
+ * 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 3 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, see <https://www.gnu.org/licenses/>.
+ */
+#ifndef DX_GETLINE_H_
+#define DX_GETLINE_H_
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+
+/*
+ * Size of the initial buffer allocated internally by the fallback
+ * implementation when *linebuf is NULL.
+ */
+#ifndef DX_GETLINE_INITIAL_ALLOC
+#  define DX_GETLINE_INITIAL_ALLOC 75
+#endif
+
+enum {
+       DX_GETLINE_OK     =  1,
+       DX_GETLINE_EOF    =  0,
+       DX_GETLINE_ERROR  = -1,
+       DX_GETLINE_ENOMEM = -2
+};
+
+/*
+ * Wrapper around getline with standard C fallback.
+ *
+ * Note that for portability to some getline implementations (e.g., FreeBSD)
+ * both *linebuf and *n should be set to zero on the initial call.
+ *
+ * If pre-allocating a buffer, ensure that its size is more than 1 byte,
+ * otherwise AIX 7.2 getline fails to work correctly.
+ *
+ * Returns 1 (DX_GETLINE_OK) if a line was read or 0 (DX_GETLINE_EOF) if
+ * no line could be read because the end of file was reached.
+ *
+ * On failure, returns a negative value.  If the C library input call failed
+ * then the return value is DX_GETLINE_ERROR and the reason for the failure
+ * should be available in errno.
+ *
+ * For the standard C fallback only, a return value of DX_GETLINE_ENOMEM
+ * indicates that the buffer allocation could not be expanded to fit the
+ * input line.
+ */
+static inline int dx_getline(char **linebuf, size_t *n, FILE *f)
+{
+#if HAVE_GETLINE
+       ssize_t rc;
+
+       if ((rc = getline(linebuf, n, f)) < 0) {
+               if (ferror(f))
+                       return DX_GETLINE_ERROR;
+               return DX_GETLINE_EOF;
+       }
+
+       if (rc-- && (*linebuf)[rc] == '\n')
+               (*linebuf)[rc] = '\0';
+
+       return DX_GETLINE_OK;
+#else
+       char *work = *linebuf;
+       size_t pos = 0;
+       size_t sz;
+
+       if (!work) {
+               sz = DX_GETLINE_INITIAL_ALLOC;
+               goto initial_alloc;
+       }
+
+       for (sz = *n;;) {
+               if (!fgets(&work[pos], sz - pos, f)) {
+                       if (ferror(f))
+                               return DX_GETLINE_ERROR;
+
+                       return pos ? DX_GETLINE_OK : DX_GETLINE_ERROR;
+               }
+
+               pos += strlen(&work[pos]);
+               if (work[pos-1] == '\n') {
+                       work[pos-1] = '\0';
+                       return DX_GETLINE_OK;
+               }
+
+               if (sz > INT_MAX/2 || sz > ((size_t)-1)/4)
+                       break;
+
+               sz = ((sz*4) + 2) / 3;
+initial_alloc:
+               work = realloc(work, sz);
+               if (!work)
+                       break;
+               *linebuf = work;
+               *n = sz;
+       }
+
+       return DX_GETLINE_ENOMEM;
+#endif
+}
+
+#endif
index 39c659aef698cb426880fcf719dd2b0cd1b69b95..862ed4fe9d5a5131a6df720305a75dfc8b2506f1 100644 (file)
@@ -1,4 +1,5 @@
 /copysym
 /copysym
+/getline
 /helpdesc
 /helpopt
 /helpopt2
 /helpdesc
 /helpopt
 /helpopt2
diff --git a/t/getline.c b/t/getline.c
new file mode 100644 (file)
index 0000000..912e571
--- /dev/null
@@ -0,0 +1,123 @@
+/*
+ * Copyright © 2024 Nick Bowler
+ *
+ * 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 3 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, see <https://www.gnu.org/licenses/>.
+ */
+
+void tap_diag(const char *fmt, ...);
+
+#undef HAVE_GETLINE
+#define DX_GETLINE_INITIAL_ALLOC 2
+#include "getline.h"
+#include "tap.h"
+
+#include <errno.h>
+
+#define LONGLINE "this line is pretty long and may require scaling the buffer multiple times"
+
+static const char testdata[] =
+       "\n"
+       LONGLINE "\n"
+       LONGLINE "\n"
+       LONGLINE "\n"
+       "hellorld\n";
+
+int main(void)
+{
+       char *line = NULL;
+       size_t long_size;
+       size_t n = 0;
+       FILE *f;
+
+       if (!(f = tmpfile()))
+               tap_bail_out("tmpfile failed: %s\n", strerror(errno));
+
+       if (fwrite(testdata, sizeof testdata-1, 1, f) != 1)
+               tap_bail_out("fwrite failed: %s\n", strerror(errno));
+
+       if (fflush(f) != 0)
+               tap_bail_out("fflush failed: %s\n", strerror(errno));
+
+       if (fseek(f, 0, SEEK_SET) != 0)
+               tap_bail_out("fseek failed: %s\n", strerror(errno));
+
+       /* First line: empty */
+       tap_result(dx_getline(&line, &n, f) != 0, "dx_getline");
+       if (!tap_result(n == 2, "alloc size")) {
+               tap_diag("Failed, unexpected result");
+               tap_diag("   Received: %lu", (unsigned long)n);
+               tap_diag("   Expected: 2");
+       }
+       if (!tap_result(line[0] == 0, "returned string")) {
+               tap_diag("Failed, unexpected result");
+               tap_diag("   Received: \"%.*s\"", (int)n, line);
+               tap_diag("   Expected: \"\"");
+       }
+
+       /* Next line: long line (buffer needs expanding) */
+       tap_result(dx_getline(&line, &n, f) != 0, "dx_getline");
+       if (!tap_result(n > sizeof LONGLINE, "alloc size")) {
+               tap_diag("Failed, unexpected result");
+               tap_diag("   Received: %lu", (unsigned long)n);
+               tap_diag("   Expected: >%u", (unsigned)(sizeof LONGLINE));
+       }
+       if (!tap_result(!strcmp(line, LONGLINE), "returned string")) {
+               tap_diag("Failed, unexpected result");
+               tap_diag("   Received: \"%.*s\"", (int)n, line);
+               tap_diag("   Expected: \"" LONGLINE "\"");
+       }
+
+       /* Next line: long line (buffer does not need expanding) */
+       long_size = n;
+       tap_result(dx_getline(&line, &n, f) != 0, "dx_getline");
+       if (!tap_result(n == long_size, "alloc size")) {
+               tap_diag("Failed, unexpected result");
+               tap_diag("   Received: %lu", (unsigned long)n);
+               tap_diag("   Expected: %lu", (unsigned long)long_size);
+       }
+       if (!tap_result(!strcmp(line, LONGLINE), "returned string")) {
+               tap_diag("Failed, unexpected result");
+               tap_diag("   Received: \"%.*s\"", (int)n, line);
+               tap_diag("   Expected: \"" LONGLINE "\"");
+       }
+
+       /* Next line: long line (new buffer allocation) */
+       free(line); line = NULL; n = 0;
+       tap_result(dx_getline(&line, &n, f) != 0, "dx_getline");
+       if (!tap_result(n == long_size, "alloc size")) {
+               tap_diag("Failed, unexpected result");
+               tap_diag("   Received: %lu", (unsigned long)n);
+               tap_diag("   Expected: %lu", (unsigned long)long_size);
+       }
+       if (!tap_result(!strcmp(line, LONGLINE), "returned string")) {
+               tap_diag("Failed, unexpected result");
+               tap_diag("   Received: \"%.*s\"", (int)n, line);
+               tap_diag("   Expected: \"" LONGLINE "\"");
+       }
+
+       /* Next line: short line */
+       tap_result(dx_getline(&line, &n, f) != 0, "dx_getline");
+       if (!tap_result(n == long_size, "alloc size")) {
+               tap_diag("Failed, unexpected result");
+               tap_diag("   Received: %lu", (unsigned long)n);
+               tap_diag("   Expected: %lu", (unsigned long)long_size);
+       }
+       if (!tap_result(!strcmp(line, "hellorld"), "returned string")) {
+               tap_diag("Failed, unexpected result");
+               tap_diag("   Received: \"%.*s\"", (int)n, line);
+               tap_diag("   Expected: \"hellorld\"");
+       }
+
+       tap_done();
+}
index 1bfa48afe7896d1d0b79ff8b5f9c210bb9d34efd..e51fbc771642dc4cba18b34993de496a2dd45df2 100644 (file)
@@ -15,4 +15,4 @@ struct MEVENT;
 int getmouse_nc_(struct MEVENT *mev) { return 0; }
 int getmouse_bogus_(void *p) { return 0; }
 
 int getmouse_nc_(struct MEVENT *mev) { return 0; }
 int getmouse_bogus_(void *p) { return 0; }
 
-void dx_closelog(void) { }
+void dx_link_stub(void) { }
index 01a96a7e2b4b6df9b23f77045a9ea65c73f489c7..bede78eb4cfbbf7eb7486884954dc84bb667f796 100644 (file)
@@ -1,8 +1,8 @@
-dnl Copyright © 2015, 2021-2024 Nick Bowler
-dnl
-dnl License WTFPL2: Do What The Fuck You Want To Public License, version 2.
-dnl This is free software: you are free to do what the fuck you want to.
-dnl There is NO WARRANTY, to the extent permitted by law.
+# Copyright © 2015, 2021-2024 Nick Bowler
+#
+# License GPLv3+: GNU General Public License version 3 or any later version.
+# This is free software: you are free to change and redistribute it.
+# There is NO WARRANTY, to the extent permitted by law.
 
 AT_BANNER([Binary packing functions])
 
 
 AT_BANNER([Binary packing functions])
 
@@ -175,3 +175,4 @@ AT_CLEANUP
 AT_BANNER([Miscellaneous functions])
 
 TEST_TAP_SIMPLE([copyright_symbol], [copysym], [], [])
 AT_BANNER([Miscellaneous functions])
 
 TEST_TAP_SIMPLE([copyright_symbol], [copysym], [], [])
+TEST_TAP_SIMPLE([do_getline], [getline], [], [getline])
index f7cf534739fb5abcdcf29111df8c1c4b7d23d11d..8a7be8d59b0a7cd5a45484d7265a21b62ba983fe 100644 (file)
@@ -1,17 +1,17 @@
-dnl Copyright © 2019-2020, 2022-2023 Nick Bowler
-dnl
-dnl License WTFPL2: Do What The Fuck You Want To Public License, version 2.
-dnl This is free software: you are free to do what the fuck you want to.
-dnl There is NO WARRANTY, to the extent permitted by law.
+# Copyright © 2019-2020, 2022-2024 Nick Bowler
+#
+# License GPLv3+: GNU General Public License version 3 or any later version.
+# This is free software: you are free to change and redistribute it.
+# There is NO WARRANTY, to the extent permitted by law.
 
 AT_BANNER([Library tests])
 
 
 AT_BANNER([Library tests])
 
-dnl TEST_DUMMY_PKGCONFIG([cflags], [libs])
-dnl
-dnl Create a hack pkg-config script in the current working directory which
-dnl responds to --cflags and --libs with the provided values.  The macro
-dnl arguments should each be a single shell word, suitable for the right
-dnl hand side of a shell assignment.
+# TEST_DUMMY_PKGCONFIG([cflags], [libs])
+#
+# Create a hack pkg-config script in the current working directory which
+# responds to --cflags and --libs with the provided values.  The macro
+# arguments should each be a single shell word, suitable for the right
+# hand side of a shell assignment.
 m4_define([TEST_DUMMY_PKGCONFIG],
 [[cat >pkg-config <<EOF
 #!/bin/sh
 m4_define([TEST_DUMMY_PKGCONFIG],
 [[cat >pkg-config <<EOF
 #!/bin/sh
@@ -361,11 +361,11 @@ AT_SETUP([DX_CHECK_SYSLOG])
 AT_DATA([syslog.h],
 [[/* since syslog is normally in the standard C library, to make negative
 link tests possible we use a dummy external name */
 AT_DATA([syslog.h],
 [[/* since syslog is normally in the standard C library, to make negative
 link tests possible we use a dummy external name */
-extern void dx_closelog(void);
+extern void dx_link_stub(void);
 
 
-static void openlog(const char *, int, int) { dx_closelog(); }
-static void syslog(int, const char *, ...) { dx_closelog(); }
-static void closelog(void) { dx_closelog(); }
+static void openlog(const char *, int, int) { dx_link_stub(); }
+static void syslog(int, const char *, ...) { dx_link_stub(); }
+static void closelog(void) { dx_link_stub(); }
 
 #define LOG_PID  1
 #define LOG_USER 2
 
 #define LOG_PID  1
 #define LOG_USER 2
@@ -395,3 +395,50 @@ yes
 ])
 
 AT_CLEANUP
 ])
 
 AT_CLEANUP
+
+AT_SETUP([DX_CHECK_GETLINE])
+
+AT_DATA([stdio.h],
+[[/* since getline is normally in the standard C library, to make negative link
+tests possible we use a dummy external name */
+extern void dx_link_stub(void);
+typedef unsigned long size_t;
+typedef long ssize_t;
+typedef int FILE;
+
+static FILE stdin[1];
+
+#if !NO_GETLINE_DECL
+static size_t getline(char **line, size_t *n, FILE *f) { dx_link_stub(); }
+#endif
+]])
+
+AT_DATA([test.in],
+[[@dx_cv_have_getline@
+@DEFS@
+]])
+
+TEST_CONFIGURE_AC([[AC_PROG_CC
+CPPFLAGS="-I$srcdir $CPPFLAGS"
+DX_CHECK_GETLINE
+AC_SUBST([dx_cv_have_getline])
+AC_CONFIG_FILES([test])
+]])
+TEST_AUTORECONF
+
+TEST_CONFIGURE
+AT_CHECK([sed '[s/-DPACKAGE\([^\\ ]*\\.\)*[^ ]* *//g]' test], [0], [no
+
+])
+
+TEST_CONFIGURE([LIBS="$builddir/t/libdummy.a"])
+AT_CHECK([sed '[s/-DPACKAGE\([^\\ ]*\\.\)*[^ ]* *//g]' test], [0], [yes
+-DHAVE_GETLINE=1
+])
+
+TEST_CONFIGURE([CPPFLAGS="-DNO_GETLINE_DECL" LIBS="$builddir/t/libdummy.a"])
+AT_CHECK([sed '[s/-DPACKAGE\([^\\ ]*\\.\)*[^ ]* *//g]' test], [0], [no
+
+])
+
+AT_CLEANUP