#include <ncbi.h>
#include <dfa.h>

/*************************************************************************
 *	dfa_nextstate(S, ch, pos, report)
 *************************************************************************/
DFA_StatePtr _cdecl
dfa_nextstate(S, ch, pos, report)
	DFA_StatePtr	S;
	int	ch;
	size_t	pos;
	/* Call-back function to report matches */
	int	(_cdecl *report) PROTO((size_t pos, DFA_PatID patID));
{
	DFA_AcceptPtr	ap;
	DFA_PatlistPtr	plp;

	if (S == NULL) {
		dfaerrno = dfaErrNullParm;
		return NULL;
	}

	S = S->next[ch]; /* go to the next state upon input of the letter ch */

	if (DFA_ISACCEPTING(S)) {
		if ((ap = (DFA_AcceptPtr)DFA_UNMUNGED(S)) == NULL)
			return NULL; /* machine has halted */
		S = ap->retState;
		plp = &ap->pl;
		if (report != NULL)
			do {
				if ((*report)(pos, plp->patID) != 0) {
					dfaerrno = dfaErrReport;
					return NULL;
				}
			} while ((plp = plp->chain) != NULL);
	}
	return S;
}

