aboutsummaryrefslogtreecommitdiff
path: root/src/fsm-generate.c
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 05:23:31 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 05:24:55 +0000
commit3f36aca66e1ac6c872300059e8db691a473614d4 (patch)
tree48c65f463c487d281c7c4091c7ba01dd4a2ff8f1 /src/fsm-generate.c
downloadlab-02-master.tar.gz
publishHEADmaster
Diffstat (limited to 'src/fsm-generate.c')
-rw-r--r--src/fsm-generate.c74
1 files changed, 74 insertions, 0 deletions
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 @@
1// fsm-generate.c --- generate n pseudorandom strings using a finite state machine
2// Usage: fsm-generate [options]
3// Options:
4// --usage Print this usage string.
5// --count <count> Number of strings to generate (default = 1).
6// --seed <seed> Seed number for srand (default = current time).
7#include <stdlib.h>
8#include <stdio.h>
9#include <stdbool.h>
10#include <time.h>
11#include "utils.h"
12#include "string-builder.h"
13#include "fsm.h"
14#include "regex.h"
15
16const char *progname;
17
18void
19usage ()
20{
21 fprintf (stderr, "Usage: %s [options]\n", progname);
22 fprintf (stderr, "Options:\n");
23 fprintf (stderr, " --usage Print this usage string.\n");
24 fprintf (stderr, " --count <count> Number of strings to generate.\n");
25 fprintf (stderr, " --seed <seed> Seed number for srand.\n");
26}
27
28int
29main (int argc, char *argv[])
30{
31 UNUSED (argc);
32
33 progname = *argv++;
34
35 unsigned int count = 1;
36 int seed = time (NULL);
37
38 for (const char *arg = *argv++; arg; arg = *argv++) {
39 if (!strcmp (arg, "--usage")) {
40 usage (progname);
41 exit (EXIT_SUCCESS);
42 }
43
44 if (!strcmp (arg, "--count")) {
45 arg = *argv++;
46 if (!arg) break;
47 if (sscanf (arg, "%d", &count) != 1) {
48 usage (progname);
49 exit (EXIT_FAILURE);
50 }
51 }
52
53 if (!strcmp (arg, "--seed")) {
54 arg = *argv++;
55 if (!arg) break;
56 if (sscanf (arg, "%d", &seed) != 1) {
57 usage (progname);
58 exit (EXIT_FAILURE);
59 }
60 }
61 }
62
63 srand (seed);
64
65 for (unsigned int i = 0; i < count; ++i) {
66 const char *string = fsm_generate ();
67 if (!regex_match (string)) printf ("regex not matching: ");
68 if (! fsm_match (string)) printf ("fsm not matching: ");
69 puts (string);
70 }
71
72 return EXIT_SUCCESS;
73}
74// fsm-generate.c ends here