1 /* Copyright (c) 1985 Ceriel J.H. Jacobs */
4 static char rcsid
[] = "$Header$";
16 * Add part of finite state machine to recognize the string s.
20 addtomach(s
, cnt
, list
) char *s
; struct state
**list
; {
22 register struct state
*l
;
23 register int i
= FSM_OKE
; /* Return value */
30 * Create new list element
32 *list
= l
= (struct state
*) alloc(sizeof(*l
), 0);
38 if (l
->s_char
== *s
) {
40 * Continue with next character
48 if (l
->s_match
|| j
) {
50 * If the state already was an endstate,
51 * or has a successor, the currently
52 * added string is a prefix of an
53 * already recognized string
62 * In this case, the currently added string has
63 * a prefix that is an already recognized
77 * Add a string to the FSM.
81 addstring(s
,cnt
,machine
) register char *s
; struct state
**machine
; {
86 return addtomach(s
,cnt
,machine
);
90 * Match string s with the finite state machine.
91 * If it matches, the number of characters actually matched is returned,
92 * and the count is put in the word pointed to by i.
93 * If the string is a prefix of a string that could be matched,
94 * FSM_ISPREFIX is returned. Otherwise, 0 is returned.
98 match(s
,i
,mach
) char *s
; int *i
; register struct state
*mach
; {
100 register char *s1
= s
; /* Walk through string */
101 register struct state
*mach1
= 0;
102 /* Keep track of previous state */
104 while (mach
&& *s1
) {
105 if (mach
->s_char
== *s1
) {
107 * Current character matches. Carry on with next
108 * character and next state
111 mach
= mach
->s_match
;
119 * No characters matched
123 if (mach1
->s_endstate
) {
132 * The string matched a prefix