Blame testAutomata.c

Packit 423ecb
/*
Packit 423ecb
 * testRegexp.c: simple module for testing regular expressions
Packit 423ecb
 *
Packit 423ecb
 * See Copyright for the status of this software.
Packit 423ecb
 *
Packit 423ecb
 * Daniel Veillard <veillard@redhat.com>
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
#include "libxml.h"
Packit 423ecb
#ifdef LIBXML_AUTOMATA_ENABLED
Packit 423ecb
#include <string.h>
Packit 423ecb
Packit 423ecb
#include <libxml/tree.h>
Packit 423ecb
#include <libxml/xmlautomata.h>
Packit 423ecb
Packit 423ecb
static int scanNumber(char **ptr) {
Packit 423ecb
    int ret = 0;
Packit 423ecb
    char *cur;
Packit 423ecb
Packit 423ecb
    cur = *ptr;
Packit 423ecb
    while ((*cur >= '0') && (*cur <= '9')) {
Packit 423ecb
	ret = ret * 10 + (*cur - '0');
Packit 423ecb
	cur++;
Packit 423ecb
    }
Packit 423ecb
    *ptr = cur;
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
static void
Packit 423ecb
testRegexpFile(const char *filename) {
Packit 423ecb
    FILE *input;
Packit 423ecb
    char expr[5000];
Packit 423ecb
    int len;
Packit 423ecb
    int ret;
Packit 423ecb
    int i;
Packit 423ecb
    xmlAutomataPtr am;
Packit 423ecb
    xmlAutomataStatePtr states[1000];
Packit 423ecb
    xmlRegexpPtr regexp = NULL;
Packit 423ecb
    xmlRegExecCtxtPtr exec = NULL;
Packit 423ecb
Packit 423ecb
    for (i = 0;i<1000;i++)
Packit 423ecb
	states[i] = NULL;
Packit 423ecb
Packit 423ecb
    input = fopen(filename, "r");
Packit 423ecb
    if (input == NULL) {
Packit 423ecb
        xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"Cannot open %s for reading\n", filename);
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    am = xmlNewAutomata();
Packit 423ecb
    if (am == NULL) {
Packit 423ecb
        xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"Cannot create automata\n");
Packit 423ecb
	fclose(input);
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    states[0] = xmlAutomataGetInitState(am);
Packit 423ecb
    if (states[0] == NULL) {
Packit 423ecb
        xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"Cannot get start state\n");
Packit 423ecb
	xmlFreeAutomata(am);
Packit 423ecb
	fclose(input);
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    ret = 0;
Packit 423ecb
Packit 423ecb
    while (fgets(expr, 4500, input) != NULL) {
Packit 423ecb
	if (expr[0] == '#')
Packit 423ecb
	    continue;
Packit 423ecb
	len = strlen(expr);
Packit 423ecb
	len--;
Packit 423ecb
	while ((len >= 0) &&
Packit 423ecb
	       ((expr[len] == '\n') || (expr[len] == '\t') ||
Packit 423ecb
		(expr[len] == '\r') || (expr[len] == ' '))) len--;
Packit 423ecb
	expr[len + 1] = 0;
Packit 423ecb
	if (len >= 0) {
Packit 423ecb
	    if ((am != NULL) && (expr[0] == 't') && (expr[1] == ' ')) {
Packit 423ecb
		char *ptr = &expr[2];
Packit 423ecb
		int from, to;
Packit 423ecb
Packit 423ecb
		from = scanNumber(&ptr);
Packit 423ecb
		if (*ptr != ' ') {
Packit 423ecb
		    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
			    "Bad line %s\n", expr);
Packit 423ecb
		    break;
Packit 423ecb
		}
Packit 423ecb
		if (states[from] == NULL)
Packit 423ecb
		    states[from] = xmlAutomataNewState(am);
Packit 423ecb
		ptr++;
Packit 423ecb
		to = scanNumber(&ptr);
Packit 423ecb
		if (*ptr != ' ') {
Packit 423ecb
		    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
			    "Bad line %s\n", expr);
Packit 423ecb
		    break;
Packit 423ecb
		}
Packit 423ecb
		if (states[to] == NULL)
Packit 423ecb
		    states[to] = xmlAutomataNewState(am);
Packit 423ecb
		ptr++;
Packit 423ecb
		xmlAutomataNewTransition(am, states[from], states[to],
Packit 423ecb
			                 BAD_CAST ptr, NULL);
Packit 423ecb
	    } else if ((am != NULL) && (expr[0] == 'e') && (expr[1] == ' ')) {
Packit 423ecb
		char *ptr = &expr[2];
Packit 423ecb
		int from, to;
Packit 423ecb
Packit 423ecb
		from = scanNumber(&ptr);
Packit 423ecb
		if (*ptr != ' ') {
Packit 423ecb
		    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
			    "Bad line %s\n", expr);
Packit 423ecb
		    break;
Packit 423ecb
		}
Packit 423ecb
		if (states[from] == NULL)
Packit 423ecb
		    states[from] = xmlAutomataNewState(am);
Packit 423ecb
		ptr++;
Packit 423ecb
		to = scanNumber(&ptr);
Packit 423ecb
		if (states[to] == NULL)
Packit 423ecb
		    states[to] = xmlAutomataNewState(am);
Packit 423ecb
		xmlAutomataNewEpsilon(am, states[from], states[to]);
Packit 423ecb
	    } else if ((am != NULL) && (expr[0] == 'f') && (expr[1] == ' ')) {
Packit 423ecb
		char *ptr = &expr[2];
Packit 423ecb
		int state;
Packit 423ecb
Packit 423ecb
		state = scanNumber(&ptr);
Packit 423ecb
		if (states[state] == NULL) {
Packit 423ecb
		    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
			    "Bad state %d : %s\n", state, expr);
Packit 423ecb
		    break;
Packit 423ecb
		}
Packit 423ecb
		xmlAutomataSetFinalState(am, states[state]);
Packit 423ecb
	    } else if ((am != NULL) && (expr[0] == 'c') && (expr[1] == ' ')) {
Packit 423ecb
		char *ptr = &expr[2];
Packit 423ecb
		int from, to;
Packit 423ecb
		int min, max;
Packit 423ecb
Packit 423ecb
		from = scanNumber(&ptr);
Packit 423ecb
		if (*ptr != ' ') {
Packit 423ecb
		    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
			    "Bad line %s\n", expr);
Packit 423ecb
		    break;
Packit 423ecb
		}
Packit 423ecb
		if (states[from] == NULL)
Packit 423ecb
		    states[from] = xmlAutomataNewState(am);
Packit 423ecb
		ptr++;
Packit 423ecb
		to = scanNumber(&ptr);
Packit 423ecb
		if (*ptr != ' ') {
Packit 423ecb
		    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
			    "Bad line %s\n", expr);
Packit 423ecb
		    break;
Packit 423ecb
		}
Packit 423ecb
		if (states[to] == NULL)
Packit 423ecb
		    states[to] = xmlAutomataNewState(am);
Packit 423ecb
		ptr++;
Packit 423ecb
		min = scanNumber(&ptr);
Packit 423ecb
		if (*ptr != ' ') {
Packit 423ecb
		    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
			    "Bad line %s\n", expr);
Packit 423ecb
		    break;
Packit 423ecb
		}
Packit 423ecb
		ptr++;
Packit 423ecb
		max = scanNumber(&ptr);
Packit 423ecb
		if (*ptr != ' ') {
Packit 423ecb
		    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
			    "Bad line %s\n", expr);
Packit 423ecb
		    break;
Packit 423ecb
		}
Packit 423ecb
		ptr++;
Packit 423ecb
		xmlAutomataNewCountTrans(am, states[from], states[to],
Packit 423ecb
			                 BAD_CAST ptr, min, max, NULL);
Packit 423ecb
	    } else if ((am != NULL) && (expr[0] == '-') && (expr[1] == '-')) {
Packit 423ecb
		/* end of the automata */
Packit 423ecb
		regexp = xmlAutomataCompile(am);
Packit 423ecb
		xmlFreeAutomata(am);
Packit 423ecb
		am = NULL;
Packit 423ecb
		if (regexp == NULL) {
Packit 423ecb
		    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
			    "Failed to compile the automata");
Packit 423ecb
		    break;
Packit 423ecb
		}
Packit 423ecb
	    } else if ((expr[0] == '=') && (expr[1] == '>')) {
Packit 423ecb
		if (regexp == NULL) {
Packit 423ecb
		    printf("=> failed not compiled\n");
Packit 423ecb
		} else {
Packit 423ecb
		    if (exec == NULL)
Packit 423ecb
			exec = xmlRegNewExecCtxt(regexp, NULL, NULL);
Packit 423ecb
		    if (ret == 0) {
Packit 423ecb
			ret = xmlRegExecPushString(exec, NULL, NULL);
Packit 423ecb
		    }
Packit 423ecb
		    if (ret == 1)
Packit 423ecb
			printf("=> Passed\n");
Packit 423ecb
		    else if ((ret == 0) || (ret == -1))
Packit 423ecb
			printf("=> Failed\n");
Packit 423ecb
		    else if (ret < 0)
Packit 423ecb
			printf("=> Error\n");
Packit 423ecb
		    xmlRegFreeExecCtxt(exec);
Packit 423ecb
		    exec = NULL;
Packit 423ecb
		}
Packit 423ecb
		ret = 0;
Packit 423ecb
	    } else if (regexp != NULL) {
Packit 423ecb
		if (exec == NULL)
Packit 423ecb
		    exec = xmlRegNewExecCtxt(regexp, NULL, NULL);
Packit 423ecb
		ret = xmlRegExecPushString(exec, BAD_CAST expr, NULL);
Packit 423ecb
	    } else {
Packit 423ecb
		xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
			"Unexpected line %s\n", expr);
Packit 423ecb
	    }
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
    fclose(input);
Packit 423ecb
    if (regexp != NULL)
Packit 423ecb
	xmlRegFreeRegexp(regexp);
Packit 423ecb
    if (exec != NULL)
Packit 423ecb
	xmlRegFreeExecCtxt(exec);
Packit 423ecb
    if (am != NULL)
Packit 423ecb
	xmlFreeAutomata(am);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
int main(int argc, char **argv) {
Packit 423ecb
Packit 423ecb
    xmlInitMemory();
Packit 423ecb
Packit 423ecb
    if (argc == 1) {
Packit 423ecb
	int ret;
Packit 423ecb
	xmlAutomataPtr am;
Packit 423ecb
	xmlAutomataStatePtr start, cur;
Packit 423ecb
	xmlRegexpPtr regexp;
Packit 423ecb
	xmlRegExecCtxtPtr exec;
Packit 423ecb
Packit 423ecb
	am = xmlNewAutomata();
Packit 423ecb
	start = xmlAutomataGetInitState(am);
Packit 423ecb
Packit 423ecb
	/* generate a[ba]*a */
Packit 423ecb
	cur = xmlAutomataNewTransition(am, start, NULL, BAD_CAST"a", NULL);
Packit 423ecb
	xmlAutomataNewTransition(am, cur, cur, BAD_CAST"b", NULL);
Packit 423ecb
	xmlAutomataNewTransition(am, cur, cur, BAD_CAST"a", NULL);
Packit 423ecb
	cur = xmlAutomataNewCountTrans(am, cur, NULL, BAD_CAST"a", 2, 3, NULL);
Packit 423ecb
	xmlAutomataSetFinalState(am, cur);
Packit 423ecb
Packit 423ecb
	/* compile it in a regexp and free the automata */
Packit 423ecb
	regexp = xmlAutomataCompile(am);
Packit 423ecb
	xmlFreeAutomata(am);
Packit 423ecb
Packit 423ecb
	/* test the regexp */
Packit 423ecb
	xmlRegexpPrint(stdout, regexp);
Packit 423ecb
	exec = xmlRegNewExecCtxt(regexp, NULL, NULL);
Packit 423ecb
	ret = xmlRegExecPushString(exec, BAD_CAST"a", NULL);
Packit 423ecb
	if (ret == 1)
Packit 423ecb
	    printf("final\n");
Packit 423ecb
	else if (ret < 0)
Packit 423ecb
	    printf("error\n");
Packit 423ecb
	ret =xmlRegExecPushString(exec, BAD_CAST"a", NULL);
Packit 423ecb
	if (ret == 1)
Packit 423ecb
	    printf("final\n");
Packit 423ecb
	else if (ret < 0)
Packit 423ecb
	    printf("error\n");
Packit 423ecb
	ret =xmlRegExecPushString(exec, BAD_CAST"b", NULL);
Packit 423ecb
	if (ret == 1)
Packit 423ecb
	    printf("final\n");
Packit 423ecb
	else if (ret < 0)
Packit 423ecb
	    printf("error\n");
Packit 423ecb
	ret =xmlRegExecPushString(exec, BAD_CAST"a", NULL);
Packit 423ecb
	if (ret == 1)
Packit 423ecb
	    printf("final\n");
Packit 423ecb
	else if (ret < 0)
Packit 423ecb
	    printf("error\n");
Packit 423ecb
	ret =xmlRegExecPushString(exec, BAD_CAST"a", NULL);
Packit 423ecb
	if (ret == 1)
Packit 423ecb
	    printf("final\n");
Packit 423ecb
	else if (ret < 0)
Packit 423ecb
	    printf("error\n");
Packit 423ecb
	ret =xmlRegExecPushString(exec, BAD_CAST"a", NULL);
Packit 423ecb
	if (ret == 1)
Packit 423ecb
	    printf("final\n");
Packit 423ecb
	else if (ret < 0)
Packit 423ecb
	    printf("error\n");
Packit 423ecb
	ret =xmlRegExecPushString(exec, BAD_CAST"a", NULL);
Packit 423ecb
	if (ret == 1)
Packit 423ecb
	    printf("final\n");
Packit 423ecb
	else if (ret < 0)
Packit 423ecb
	    printf("error\n");
Packit 423ecb
	if (ret == 0) {
Packit 423ecb
	    ret = xmlRegExecPushString(exec, NULL, NULL);
Packit 423ecb
	    if (ret == 1)
Packit 423ecb
		printf("final\n");
Packit 423ecb
	    else if (ret < 0)
Packit 423ecb
		printf("error\n");
Packit 423ecb
	}
Packit 423ecb
	xmlRegFreeExecCtxt(exec);
Packit 423ecb
Packit 423ecb
	/* free the regexp */
Packit 423ecb
	xmlRegFreeRegexp(regexp);
Packit 423ecb
    } else {
Packit 423ecb
	int i;
Packit 423ecb
Packit 423ecb
	for (i = 1;i < argc;i++)
Packit 423ecb
	    testRegexpFile(argv[i]);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    xmlCleanupParser();
Packit 423ecb
    xmlMemoryDump();
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#else
Packit 423ecb
#include <stdio.h>
Packit 423ecb
int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
Packit 423ecb
    printf("%s : Automata support not compiled in\n", argv[0]);
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
#endif /* LIBXML_AUTOMATA_ENABLED */