-# 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
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
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 =
[no/no|*/c89|*/c99],
[AC_CACHE_CHECK([if $CC supports _Alignas], [dx_cv_have_alignas],
[dx_cv_have_alignas=no
-for _dx_alignas
+for _dx_alignas dnl Eat newline to work around bash-5 parsing bug.
in '_Alignas(X)' '__attribute__((__aligned__(X)))' '__declspec(align(X))'
do
AC_COMPUTE_INT([_dx_tmp],
--- /dev/null
+# 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.])])])
--- /dev/null
+/*
+ * 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
/copysym
+/getline
/helpdesc
/helpopt
/helpopt2
--- /dev/null
+/*
+ * 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();
+}
int getmouse_nc_(struct MEVENT *mev) { return 0; }
int getmouse_bogus_(void *p) { return 0; }
-void dx_closelog(void) { }
+void dx_link_stub(void) { }
-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([Miscellaneous functions])
TEST_TAP_SIMPLE([copyright_symbol], [copysym], [], [])
+TEST_TAP_SIMPLE([do_getline], [getline], [], [getline])
-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])
-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
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
])
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
-AT_COPYRIGHT([dnl
-Copyright © 2015, 2019-2023 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.
+AT_COPYRIGHT([Copyright (C) 2015, 2019-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.])
m4_include([snippet/at-compat.at])
AT_COLOR_TESTS
m4_divert_push([PREPARE_TESTS])dnl
+# Avoid inheriting the user's preference for Automake silent rules from the
+# environment, as this changes make output leading to failed tests.
+AS_UNSET([enable_silent_rules])
+
# Reduce influence from the toplevel "make" invocation on test cases.
AS_UNSET([MAKEFLAGS])
AS_UNSET([MAKELEVEL])