Skip to content

Commit

Permalink
Centralise fsm_print() and friends.
Browse files Browse the repository at this point in the history
Now you call fsm_print(f, fsm, FSM_PRINT_*) rather than fsm_print_*().

This might look a bit cumbersome to the caller, but I'm doing it so we have a single entry point to put shared stuff. More on that later.
  • Loading branch information
katef committed Jul 10, 2024
1 parent 6b76c46 commit 0b84d75
Show file tree
Hide file tree
Showing 45 changed files with 564 additions and 384 deletions.
2 changes: 1 addition & 1 deletion examples/bm/libfsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ main(int argc, char *argv[])
printf("#include <time.h>\n");
printf("\n");

fsm_print_c(stdout, fsm);
fsm_print(stdout, fsm, FSM_PRINT_C);

printf("int\n");
printf("main(void)\n");
Expand Down
4 changes: 2 additions & 2 deletions examples/glob/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ match(const struct fsm *fsm, const char *s)
assert(fsm_all(fsm, fsm_isdfa));
assert(s != NULL);

if (1 != fsm_exec(fsm, fsm_sgetc, &s, &state)) {
if (1 != fsm_exec(fsm, fsm_sgetc, &s, &state, NULL)) {
return 0;
}

Expand Down Expand Up @@ -196,7 +196,7 @@ main(int argc, char *argv[])
}

if (!quiet) {
fsm_print_fsm(stdout, fsm);
fsm_print(stdout, fsm, FSM_PRINT_FSM);
}

matched = match(fsm, argv[1]);
Expand Down
126 changes: 50 additions & 76 deletions examples/iprange/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
#define VALUE_SEP ','

static struct fsm *fsm;
static struct fsm_state *fsmstart;
static fsm_state_t fsm_start;
static fsm_state_t fsm_none = (fsm_state_t) -1;

static unsigned char zeroes[16];
static unsigned char ones[16];
Expand All @@ -61,10 +62,10 @@ struct record {
unsigned id;

struct fsm *fsm;
struct fsm_state *start;
struct fsm_state *end;
fsm_state_t start;
fsm_state_t end;
struct {
struct fsm_state *s;
fsm_state_t s;
unsigned char c;
} regs[16];
};
Expand Down Expand Up @@ -120,21 +121,22 @@ get_id(char *rec, size_t reclen)
exit(-1);
}

r->start = fsm_addstate(r->fsm);
if (r->start == NULL) {
if (!fsm_addstate(r->fsm, &r->start)) {
perror("fsm_addstate");
exit(-1);
}
fsm_setstart(r->fsm, r->start);

r->end = fsm_addstate(r->fsm);
if (r->end == NULL) {
if (!fsm_addstate(r->fsm, &r->end)) {
perror("fsm_addstate");
exit(-1);
}
fsm_setend(r->fsm, r->end, 1);

memset(r->regs, 0, sizeof r->regs);
for (size_t i = 0; i < sizeof r->regs; i++) {
r->regs[i].c = '\0';
r->regs[i].s = fsm_none;
}

RB_INSERT(recmap, &recmap, r);

Expand Down Expand Up @@ -170,7 +172,7 @@ usage(void)
exit(-1);
}

struct fsm_state *
static fsm_state_t
get_from(struct record *r, unsigned oct)
{
if (oct == 0) {
Expand All @@ -180,15 +182,16 @@ get_from(struct record *r, unsigned oct)
return r->regs[oct - 1].s;
}

struct fsm_state *
static fsm_state_t
get_to(struct record *r, unsigned oct, unsigned noct, const unsigned char *octs)
{
if (r->regs[oct].s != NULL && r->regs[oct].c == octs[oct]) {
fsm_state_t to;

if (r->regs[oct].s != fsm_none && r->regs[oct].c == octs[oct]) {
return r->regs[oct].s;
}

struct fsm_state *to = fsm_addstate(r->fsm);
if (to == NULL) {
if (!fsm_addstate(r->fsm, &to)) {
perror("fsm_addstate");
exit(-1);
}
Expand All @@ -197,7 +200,7 @@ get_to(struct record *r, unsigned oct, unsigned noct, const unsigned char *octs)
r->regs[oct].c = octs[oct];

for (unsigned i = oct + 1; i < noct; i++) {
r->regs[i].s = NULL;
r->regs[i].s = fsm_none;
}

return to;
Expand All @@ -207,8 +210,7 @@ static void
gen_edge_range(struct record *r, unsigned oct, const unsigned char *octs,
unsigned range_start, unsigned range_end, unsigned noct, unsigned final)
{
struct fsm_state *from, *to;
struct fsm_edge *e;
fsm_state_t from, to;

from = get_from(r, oct);
if (oct > 0) {
Expand All @@ -225,8 +227,7 @@ gen_edge_range(struct record *r, unsigned oct, const unsigned char *octs,
}

for (unsigned i = range_start; i <= range_end; i++) {
e = fsm_addedge_literal(r->fsm, from, to, (unsigned char) i);
if (e == NULL) {
if (!fsm_addedge_literal(r->fsm, from, to, (unsigned char) i)) {
perror("fsm_addedge_literal");
exit(-1);
}
Expand Down Expand Up @@ -420,47 +421,19 @@ important(unsigned n)
}
}

static void
carryopaque(const struct fsm *src_fsm, const fsm_state_t *src_set, size_t n,
struct fsm *dst_fsm, struct fsm_state *dst_state)
static int
leaf(FILE *f, const fsm_end_id_t *ids, size_t count, const void *leaf_opaque)
{
void *o = NULL;
size_t i;

assert(src_fsm != NULL);
assert(src_set != NULL);
assert(n > 0);
assert(dst_fsm != NULL);
assert(fsm_isend(dst_fsm, dst_state));
assert(fsm_getopaque(dst_fsm, dst_state) == NULL);

for (i = 0; i < n; i++) {
/*
* The opaque data is attached to end states only, so we skip
* non-end states here.
*/
if (!fsm_isend(src_fsm, src_set[i])) {
continue;
}

assert(fsm_getopaque(src_fsm, src_set[i]) != NULL);
const struct record *r;

if (o == NULL) {
o = fsm_getopaque(src_fsm, src_set[i]);
fsm_setopaque(dst_fsm, dst_state, o);
continue;
}
(void) leaf_opaque;

assert(o == fsm_getopaque(src_fsm, src_set[i]));
if (count != 1) {
fprintf(f, "endid conflict\n");
exit(EXIT_FAILURE);
}
}

static int
leaf(FILE *f, const void *state_opaque, const void *leaf_opaque)
{
const struct record *r = state_opaque;

(void) leaf_opaque;
r = (const void *) (intptr_t) ids[0]; /* XXX */

if (r == NULL) {
fprintf(f, "return -1;");
Expand All @@ -473,19 +446,27 @@ leaf(FILE *f, const void *state_opaque, const void *leaf_opaque)
}

static int
endleaf_dot(FILE *f, const void *state_opaque, const void *endleaf_opaque)
endleaf_dot(FILE *f, const fsm_end_id_t *ids, size_t count, const void *endleaf_opaque)
{
const struct record *r;

assert(f != NULL);
assert(state_opaque != NULL);
assert(endleaf_opaque == NULL);

(void) endleaf_opaque;

r = state_opaque;
fprintf(f, "label = <");

for (size_t i = 0; i < count; i++) {
r = (const void *) (intptr_t) ids[i]; /* XXX */

fprintf(f, "%s", r->rec); /* XXX: escape */
if (i + 1 < count) {
fprintf(f, ", ");
}
}

fprintf(f, "label = <%s>", r->rec); /* XXX: escape */
fprintf(f, ">");

return 0;
}
Expand Down Expand Up @@ -556,8 +537,7 @@ main(int argc, char **argv)
return -1;
}

fsmstart = fsm_addstate(fsm);
if (fsmstart == NULL) {
if (!fsm_addstate(fsm, &fsm_start)) {
perror("fsm_addstate");
return -1;
}
Expand Down Expand Up @@ -661,17 +641,17 @@ main(int argc, char **argv)
exit(-1);
}

fsm_setendopaque(r->fsm, r);
fsm_setendid(r->fsm, (intptr_t) r); /* XXX */

(void) fsm_getstart(r->fsm, &start);

fsm = fsm_merge(fsm, r->fsm);
fsm = fsm_merge(fsm, r->fsm, NULL);
if (fsm == NULL) {
perror("fsm_merge");
exit(-1);
}

if (!fsm_addedge_epsilon(fsm, fsmstart, start)) {
if (!fsm_addedge_epsilon(fsm, fsm_start, start)) {
perror("fsm_addedge_epsilon");
exit(-1);
}
Expand All @@ -686,7 +666,7 @@ main(int argc, char **argv)
}
}

fsm_setstart(fsm, fsmstart);
fsm_setstart(fsm, fsm_start);

if (progress) {
tend = time(NULL);
Expand All @@ -696,15 +676,9 @@ main(int argc, char **argv)
tstart = time(NULL);
}

{
opt.carryopaque = carryopaque;

if (!fsm_determinise(fsm) == 0) {
perror("fsm_determinise");
exit(-1);
}

opt.carryopaque = NULL;
if (!fsm_determinise(fsm) == 0) {
perror("fsm_determinise");
exit(-1);
}

if (progress) {
Expand All @@ -717,11 +691,11 @@ main(int argc, char **argv)
opt.cp = "c";
opt.leaf = leaf;
opt.leaf_opaque = NULL;
fsm_print_c(stdout, fsm);
fsm_print(stdout, fsm, FSM_PRINT_C);
} else if (odot) {
opt.endleaf = endleaf_dot;
opt.endleaf_opaque = NULL;
fsm_print_dot(stdout, fsm);
fsm_print(stdout, fsm, FSM_PRINT_DOT);
}
}

12 changes: 6 additions & 6 deletions examples/utf8dfa/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ main(int argc, char *argv[])
{
struct fsm *fsm;
fsm_state_t start;
fsm_print *print;
enum fsm_print_lang lang;

opt.anonymous_states = 1;
opt.consolidate_edges = 1;
opt.always_hex = 0;
opt.comments = 0;
opt.case_ranges = 0;

print = fsm_print_api;
lang = FSM_PRINT_API;

{
int c;
Expand All @@ -139,11 +139,11 @@ main(int argc, char *argv[])

case 'l':
if (strcmp(optarg, "dot") == 0) {
print = fsm_print_dot;
lang = FSM_PRINT_DOT;
} else if (strcmp(optarg, "api") == 0) {
print = fsm_print_api;
lang = FSM_PRINT_API;
} else if (strcmp(optarg, "c") == 0) {
print = fsm_print_c;
lang = FSM_PRINT_C;
} else {
fprintf(stderr, "Invalid language '%s'", optarg);
exit(1);
Expand Down Expand Up @@ -220,7 +220,7 @@ main(int argc, char *argv[])
exit(1);
}

print(stdout, fsm);
fsm_print(stdout, fsm, lang);

fsm_free(fsm);

Expand Down
14 changes: 6 additions & 8 deletions examples/words/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int main(int argc, char *argv[]) {
fsm_state_t start;
char s[BUFSIZ];
int (*dmf)(struct fsm *);
fsm_print *print;
enum fsm_print_lang lang;
unsigned long ms, mt;
int timing;
int native = 0;
Expand All @@ -40,7 +40,7 @@ int main(int argc, char *argv[]) {
opt.consolidate_edges = 1;

dmf = NULL;
print = NULL;
lang = FSM_PRINT_NONE;
timing = 0;

{
Expand All @@ -55,8 +55,8 @@ int main(int argc, char *argv[]) {
case 'n': native = 1; break;
case 'N': unanchored = 1; break;

case 'c': print = fsm_print_dot; break;
case 'f': print = fsm_print_fsm; break;
case 'c': lang = FSM_PRINT_DOT; break;
case 'f': lang = FSM_PRINT_FSM; break;

case 't':
timing = 1;
Expand Down Expand Up @@ -116,7 +116,7 @@ int main(int argc, char *argv[]) {
}

if (ahocorasick) {
if (!re_strings_add_str(g, s)) {
if (!re_strings_add_str(g, s, NULL)) {
perror("re_strings_add_str");
exit(EXIT_FAILURE);
}
Expand Down Expand Up @@ -211,9 +211,7 @@ int main(int argc, char *argv[]) {
+ ((long) post.tv_nsec - (long) pre.tv_nsec) / 1000000;
}

if (print != NULL) {
print(stdout, fsm);
}
fsm_print(stdout, fsm, lang);

if (timing) {
printf("construction, reduction, total: %lu, %lu, %lu\n", ms, mt, ms + mt);
Expand Down
Loading

0 comments on commit 0b84d75

Please sign in to comment.