#define PROGNAME "randomdecl"
static const char *progname = PROGNAME;
-static const char sopts[] = "s:n:VH";
+static const char sopts[] = "s:n:ECVH";
static const struct option lopts[] = {
{ "seed", 1, NULL, 's' },
{ "count", 1, NULL, 'n' },
+ { "cdecl", 0, NULL, 'C' },
+ { "english", 0, NULL, 'E' },
{ "version", 0, NULL, 'V' },
{ "help", 0, NULL, 'H' },
{ 0 }
};
+enum {
+ MODE_CDECL,
+ MODE_ENGLISH,
+};
+
static void print_usage(FILE *f)
{
fprintf(f, "Usage: %s [options]\n", progname);
struct gen_rng *rng;
struct cdecl *decl;
unsigned long count = 0;
- int opt;
+ int opt, mode = MODE_CDECL;
if (argc > 0)
progname = argv[0];
case 'n':
count_str = optarg;
break;
+ case 'C':
+ mode = MODE_CDECL;
+ break;
+ case 'E':
+ mode = MODE_ENGLISH;
+ break;
case 'V':
test_print_version(PROGNAME);
return EXIT_SUCCESS;
for (unsigned long i = 0; !count || i < count; i++) {
decl = random_decl(rng);
- test_print_decl(decl);
+
+ if (mode == MODE_ENGLISH) {
+ test_explain_decl(decl);
+ } else {
+ test_print_decl(decl);
+ }
gen_free_declspecs(decl->specifiers);
gen_free_declarators(decl->declarators);
void *realloc_nofail(void *ptr, size_t size);
void test_print_specifiers(struct cdecl_declspec *spec);
void test_print_decl(struct cdecl *decl);
+void test_explain_decl(struct cdecl *decl);
bool strict_strtoul(unsigned long *val, const char *str, int base);
#include <cdecl.h>
#include "test.h"
+static size_t printbuf_size;
+static char *printbuf;
+
void *realloc_nofail(void *ptr, size_t size)
{
void *p;
void test_print_decl(struct cdecl *decl)
{
- static size_t bufsz;
- static char *buf;
size_t rc;
retry:
- rc = cdecl_declare(buf, bufsz, decl);
- if (rc >= bufsz) {
- bufsz = rc + 1;
- buf = realloc_nofail(buf, bufsz);
+ rc = cdecl_declare(printbuf, printbuf_size, decl);
+ if (rc >= printbuf_size) {
+ printbuf_size = rc + 1;
+ printbuf = realloc_nofail(printbuf, printbuf_size);
+ goto retry;
+ }
+
+ printf("%s\n", printbuf);
+}
+
+void test_explain_decl(struct cdecl *decl)
+{
+ size_t rc;
+
+retry:
+ rc = cdecl_explain(printbuf, printbuf_size, decl);
+ if (rc >= printbuf_size) {
+ printbuf_size = rc + 1;
+ printbuf = realloc_nofail(printbuf, printbuf_size);
goto retry;
}
- printf("%s\n", buf);
+ printf("%s\n", printbuf);
}
bool strict_strtoul(unsigned long *val, const char *str, int base)