]> git.draconx.ca Git - cdecl99.git/blobdiff - t/rendertest.c
Port to use getline.h from dxcommon.
[cdecl99.git] / t / rendertest.c
index 14faafb28990544f38645ccf24500120334efbac..e64526feb84d5f2a8bdac4b9acdb44177c3cb5dd 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Helper to verify the output rendering routines.
- * Copyright © 2023 Nick Bowler
+ * Copyright © 2023-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
 #include <getopt.h>
 #include <assert.h>
 
-#include <cdecl.h>
+#include "cdecl.h"
+
+#define PROGNAME "rendertest"
 #include "test.h"
 
-#define PROGNAME "outbuf"
-static const char *progname = PROGNAME;
 static const char sopts[] = "n:ECVH";
 static const struct option lopts[] = {
        { "count",   1, NULL, 'n' },
@@ -63,9 +63,8 @@ static int do_test(char *line, unsigned long n, int mode)
                decl = cdecl_parse_decl(line);
 
        if (!decl) {
-               fprintf(stderr, "%s: %s\n", progname, cdecl_get_error()->str);
-               fprintf(stderr, "%s: the failed input was: %s\n",
-                               progname, line);
+               print_error("%s", cdecl_get_error()->str);
+               print_error("the failed input was: %s", line);
                return -1;
        }
 
@@ -76,12 +75,12 @@ static int do_test(char *line, unsigned long n, int mode)
                rc = cdecl_declare(line, n, decl);
 
        if (n && ( rc < n ? line[rc] : line[n-1] ) != '\0') {
-               fprintf(stderr, "%s: output is not 0-terminated\n", progname);
+               print_error("output is not 0-terminated");
                return -1;
        }
 
        if (line[n] != '\a') {
-               fprintf(stderr, "%s: output overflow\n", progname);
+               print_error("output overflow");
                return -1;
        }
 
@@ -109,9 +108,8 @@ int main(int argc, char **argv)
                        mode = MODE_ENGLISH;
                        break;
                case 'n':
-                       if (!strict_strtoul(&n, optarg, 0) || n > (size_t)-2) {
-                               fprintf(stderr, "%s: invalid count: %s\n",
-                                       progname, optarg);
+                       if (!test_strtoul(&n, optarg) || n > (size_t)-2) {
+                               print_error("invalid count: %s", optarg);
                                return EXIT_FAILURE;
                        }
                        break;
@@ -127,12 +125,12 @@ int main(int argc, char **argv)
                }
        }
 
-       line = malloc_nofail((sz = n+1));
-       while (getline(&line, &sz, stdin) >= 0) {
-               char *c = strchr(line, '\n');
-               if (c)
-                       *c = '\0';
-
+       /*
+        * Ensure the preallocated buffer is more than one byte, otherwise we
+        * will hit a bug in AIX 7.2 getline and fall into an infinite loop.
+        */
+       line = malloc_nofail((sz = MAX(n+1, 10)));
+       while (test_getline(&line, &sz)) {
                if (do_test(line, n, mode) < 0)
                        ret = EXIT_FAILURE;
        }