]> git.draconx.ca Git - dxcommon.git/blob - t/helpdesc.c
DX_C_ALIGNAS: Work around bash-5 parsing bug.
[dxcommon.git] / t / helpdesc.c
1 /*
2  * Read some text from standard input and format it with help_print_desc,
3  * for testing.  Each pair of program arguments is converted to an int and
4  * passed as the two integer arguments to help_print_desc.
5  */
6 #include "help.h"
7 #include "tap.h"
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <limits.h>
13 #include <errno.h>
14
15 static char buf[1000];
16
17 int arg_to_int(const char *s)
18 {
19         char *end;
20         long val;
21
22         errno = 0;
23         val = strtol(s, &end, 0);
24         if (*end != 0)
25                 tap_bail_out("%s: numeric argument expected", s);
26         else if (val < INT_MIN || val > INT_MAX || errno == ERANGE)
27                 tap_bail_out("%s: %s", s, strerror(ERANGE));
28         else if (errno)
29                 tap_bail_out("%s: %s", s, strerror(errno));
30
31         return val;
32 }
33
34 int main(int argc, char **argv)
35 {
36         long a, b;
37         size_t len;
38         int i;
39
40         len = fread(buf, 1, sizeof buf - 1, stdin);
41         if (len == sizeof buf - 1)
42                 tap_bail_out("too much input text");
43         if (ferror(stdin))
44                 tap_bail_out("error reading from stdin: %s", strerror(errno));
45
46         for (i = 1; i < argc; i += 2) {
47                 int indent = arg_to_int(argv[i]);
48                 int sub = i+1 < argc ? arg_to_int(argv[i+1]) : 0;
49
50                 help_print_desc(NULL, buf, indent, sub);
51         }
52
53         return 0;
54 }