Repository navigation
Expand file tree
/
Copy pathmachine.c
More file actions
executable file
·137 lines (123 loc) · 2.53 KB
/
Copy pathmachine.c
File metadata and controls
executable file
·137 lines (123 loc) · 2.53 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* Copyright (c) 1985 Ceriel J.H. Jacobs */
# ifndef lint
static char rcsid[] = "$Header$";
# endif
# define _MACHINE_
# include <ctype.h>
# include "in_all.h"
# include "machine.h"
# include "getline.h"
# include "assert.h"
/*
* Add part of finite state machine to recognize the string s.
*/
STATIC int
addtomach(s, cnt, list) char *s; struct state **list; {
register struct state *l;
register int i = FSM_OKE; /* Return value */
register int j;
for (;;) {
l = *list;
if (!l) {
/*
* Create new list element
*/
*list = l = (struct state *) alloc(sizeof(*l), 0);
l->s_char = *s;
l->s_endstate = 0;
l->s_match = 0;
l->s_next = 0;
}
if (l->s_char == *s) {
/*
* Continue with next character
*/
if (!*++s) {
/*
* No next character
*/
j = l->s_endstate;
l->s_endstate = 1;
if (l->s_match || j) {
/*
* If the state already was an endstate,
* or has a successor, the currently
* added string is a prefix of an
* already recognized string
*/
return FSM_ISPREFIX;
}
l->s_cnt = cnt;
return i;
}
if (l->s_endstate) {
/*
* In this case, the currently added string has
* a prefix that is an already recognized
* string.
*/
i = FSM_HASPREFIX;
}
list = &(l->s_match);
continue;
}
list = &(l->s_next);
}
/* NOTREACHED */
}
/*
* Add a string to the FSM.
*/
int
addstring(s,cnt,machine) register char *s; struct state **machine; {
if (!s || !*s) {
return FSM_ISPREFIX;
}
return addtomach(s,cnt,machine);
}
/*
* Match string s with the finite state machine.
* If it matches, the number of characters actually matched is returned,
* and the count is put in the word pointed to by i.
* If the string is a prefix of a string that could be matched,
* FSM_ISPREFIX is returned. Otherwise, 0 is returned.
*/
int
match(s,i,mach) char *s; int *i; register struct state *mach; {
register char *s1 = s; /* Walk through string */
register struct state *mach1 = 0;
/* Keep track of previous state */
while (mach && *s1) {
if (mach->s_char == *s1) {
/*
* Current character matches. Carry on with next
* character and next state
*/
mach1 = mach;
mach = mach->s_match;
s1++;
continue;
}
mach = mach->s_next;
}
if (!mach1) {
/*
* No characters matched
*/
return 0;
}
if (mach1->s_endstate) {
/*
* The string matched
*/
*i = mach1->s_cnt;
return s1 - s;
}
if (!*s1) {
/*
* The string matched a prefix
*/
return FSM_ISPREFIX;
}
return 0;
}