From 3f36aca66e1ac6c872300059e8db691a473614d4 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sun, 8 Nov 2026 08:23:31 +0300 Subject: publish --- src/fsm-generate.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/fsm-generate.c (limited to 'src/fsm-generate.c') diff --git a/src/fsm-generate.c b/src/fsm-generate.c new file mode 100644 index 0000000..e6f01ce --- /dev/null +++ b/src/fsm-generate.c @@ -0,0 +1,74 @@ +// fsm-generate.c --- generate n pseudorandom strings using a finite state machine +// Usage: fsm-generate [options] +// Options: +// --usage Print this usage string. +// --count Number of strings to generate (default = 1). +// --seed Seed number for srand (default = current time). +#include +#include +#include +#include +#include "utils.h" +#include "string-builder.h" +#include "fsm.h" +#include "regex.h" + +const char *progname; + +void +usage () +{ + fprintf (stderr, "Usage: %s [options]\n", progname); + fprintf (stderr, "Options:\n"); + fprintf (stderr, " --usage Print this usage string.\n"); + fprintf (stderr, " --count Number of strings to generate.\n"); + fprintf (stderr, " --seed Seed number for srand.\n"); +} + +int +main (int argc, char *argv[]) +{ + UNUSED (argc); + + progname = *argv++; + + unsigned int count = 1; + int seed = time (NULL); + + for (const char *arg = *argv++; arg; arg = *argv++) { + if (!strcmp (arg, "--usage")) { + usage (progname); + exit (EXIT_SUCCESS); + } + + if (!strcmp (arg, "--count")) { + arg = *argv++; + if (!arg) break; + if (sscanf (arg, "%d", &count) != 1) { + usage (progname); + exit (EXIT_FAILURE); + } + } + + if (!strcmp (arg, "--seed")) { + arg = *argv++; + if (!arg) break; + if (sscanf (arg, "%d", &seed) != 1) { + usage (progname); + exit (EXIT_FAILURE); + } + } + } + + srand (seed); + + for (unsigned int i = 0; i < count; ++i) { + const char *string = fsm_generate (); + if (!regex_match (string)) printf ("regex not matching: "); + if (! fsm_match (string)) printf ("fsm not matching: "); + puts (string); + } + + return EXIT_SUCCESS; +} +// fsm-generate.c ends here -- cgit v1.3.1