aboutsummaryrefslogtreecommitdiff
path: root/src/fsm-generate.c
blob: e6f01cefd843f346895a523c4c70b5d4864cc0cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// fsm-generate.c --- generate n pseudorandom strings using a finite state machine
// Usage: fsm-generate [options]
// Options:
//   --usage              Print this usage string.
//   --count <count>      Number of strings to generate (default = 1).
//   --seed <seed>        Seed number for srand (default = current time).
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#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 <count>      Number of strings to generate.\n");
  fprintf (stderr, "  --seed <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