/*
* 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"
+#include "getline.h"
-#define PROGNAME "outbuf"
-static const char *progname = PROGNAME;
static const char sopts[] = "n:ECVH";
static const struct option lopts[] = {
{ "count", 1, NULL, 'n' },
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;
}
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;
}
- printf("%lu %.*s\n", (unsigned long)rc, -(n>0), line);
+ printf(n > 0 ? "%lu %s\n" : "%lu\n", (unsigned long)rc, line);
cdecl_free(decl);
return 0;
}
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;
}
}
- 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 (do_getline(&line, &sz)) {
if (do_test(line, n, mode) < 0)
ret = EXIT_FAILURE;
}