]> git.draconx.ca Git - cdecl99.git/blob - t/rendertest.c
Avoid gnulib getline module.
[cdecl99.git] / t / rendertest.c
1 /*
2  * Helper to verify the output rendering routines.
3  * Copyright © 2023-2024 Nick Bowler
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <getopt.h>
24 #include <assert.h>
25
26 #include "cdecl.h"
27
28 #define PROGNAME "rendertest"
29 #include "test.h"
30 #include "getline.h"
31
32 static const char sopts[] = "n:ECVH";
33 static const struct option lopts[] = {
34         { "count",   1, NULL, 'n' },
35         { "cdecl",   0, NULL, 'C' },
36         { "english", 0, NULL, 'E' },
37         { "version", 0, NULL, 'V' },
38         { "help",    0, NULL, 'H' },
39         { 0 }
40 };
41
42 static void print_usage(FILE *f)
43 {
44         fprintf(f, "Usage: %s [options]\n", progname);
45 }
46
47 static void print_help(void)
48 {
49         print_usage(stdout);
50         puts("Helper application to test output rendering.\n");
51         test_print_options(lopts);
52 }
53
54 enum { MODE_CDECL, MODE_ENGLISH };
55
56 static int do_test(char *line, unsigned long n, int mode)
57 {
58         struct cdecl *decl;
59         size_t rc;
60
61         if (mode)
62                 decl = cdecl_parse_english(line);
63         else
64                 decl = cdecl_parse_decl(line);
65
66         if (!decl) {
67                 print_error("%s", cdecl_get_error()->str);
68                 print_error("the failed input was: %s", line);
69                 return -1;
70         }
71
72         memset(line, '\a', n+1);
73         if (mode)
74                 rc = cdecl_explain(line, n, decl);
75         else
76                 rc = cdecl_declare(line, n, decl);
77
78         if (n && ( rc < n ? line[rc] : line[n-1] ) != '\0') {
79                 print_error("output is not 0-terminated");
80                 return -1;
81         }
82
83         if (line[n] != '\a') {
84                 print_error("output overflow");
85                 return -1;
86         }
87
88         printf(n > 0 ? "%lu %s\n" : "%lu\n", (unsigned long)rc, line);
89         cdecl_free(decl);
90         return 0;
91 }
92
93 int main(int argc, char **argv)
94 {
95         int opt, mode = MODE_CDECL, ret = EXIT_SUCCESS;
96         unsigned long n = 60;
97         char *line;
98         size_t sz;
99
100         if (argc > 0)
101                 progname = argv[0];
102
103         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
104                 switch (opt) {
105                 case 'C':
106                         mode = MODE_CDECL;
107                         break;
108                 case 'E':
109                         mode = MODE_ENGLISH;
110                         break;
111                 case 'n':
112                         if (!test_strtoul(&n, optarg) || n > (size_t)-2) {
113                                 print_error("invalid count: %s", optarg);
114                                 return EXIT_FAILURE;
115                         }
116                         break;
117                 case 'V':
118                         test_print_version(PROGNAME);
119                         return EXIT_SUCCESS;
120                 case 'H':
121                         print_help();
122                         return EXIT_SUCCESS;
123                 default:
124                         print_usage(stderr);
125                         return EXIT_FAILURE;
126                 }
127         }
128
129         /*
130          * Ensure the preallocated buffer is more than one byte, otherwise we
131          * will hit a bug in AIX 7.2 getline and fall into an infinite loop.
132          */
133         line = malloc_nofail((sz = MAX(n+1, 10)));
134         while (do_getline(&line, &sz)) {
135                 if (do_test(line, n, mode) < 0)
136                         ret = EXIT_FAILURE;
137         }
138
139         free(line);
140         return ret;
141 }