summaryrefslogtreecommitdiffstats
path: root/libxml2/testRegexp.c
blob: 21dac945bc26aa483bdb9cbca73fb9096a8cb39d (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
 * testRegexp.c: simple module for testing regular expressions
 *
 * See Copyright for the status of this software.
 *
 * Daniel Veillard <veillard@redhat.com>
 */

#include "libxml.h"
#ifdef LIBXML_REGEXP_ENABLED
#include <string.h>

#include <libxml/tree.h>
#include <libxml/xmlregexp.h>

static int repeat = 0;
static int debug = 0;

static void testRegexp(xmlRegexpPtr comp, const char *value) {
    int ret;

    ret = xmlRegexpExec(comp, (const xmlChar *) value);
    if (ret == 1)
	printf("%s: Ok\n", value);
    else if (ret == 0)
	printf("%s: Fail\n", value);
    else
	printf("%s: Error: %d\n", value, ret);
    if (repeat) {
	int j;
	for (j = 0;j < 999999;j++)
	    xmlRegexpExec(comp, (const xmlChar *) value);
    }
}

static void
testRegexpFile(const char *filename) {
    xmlRegexpPtr comp = NULL;
    FILE *input;
    char expression[5000];
    int len;

    input = fopen(filename, "r");
    if (input == NULL) {
        xmlGenericError(xmlGenericErrorContext,
		"Cannot open %s for reading\n", filename);
	return;
    }
    while (fgets(expression, 4500, input) != NULL) {
	len = strlen(expression);
	len--;
	while ((len >= 0) &&
	       ((expression[len] == '\n') || (expression[len] == '\t') ||
		(expression[len] == '\r') || (expression[len] == ' '))) len--;
	expression[len + 1] = 0;
	if (len >= 0) {
	    if (expression[0] == '#')
		continue;
	    if ((expression[0] == '=') && (expression[1] == '>')) {
		char *pattern = &expression[2];

		if (comp != NULL) {
		    xmlRegFreeRegexp(comp);
		    comp = NULL;
		}
		printf("Regexp: %s\n", pattern) ;
		comp = xmlRegexpCompile((const xmlChar *) pattern);
		if (comp == NULL) {
		    printf("   failed to compile\n");
		    break;
		}
	    } else if (comp == NULL) {
		printf("Regexp: %s\n", expression) ;
		comp = xmlRegexpCompile((const xmlChar *) expression);
		if (comp == NULL) {
		    printf("   failed to compile\n");
		    break;
		}
	    } else if (comp != NULL) {
		testRegexp(comp, expression);
	    }
	}
    }
    fclose(input);
    if (comp != NULL)
	xmlRegFreeRegexp(comp);
}

#ifdef LIBXML_EXPR_ENABLED
static void
runFileTest(xmlExpCtxtPtr ctxt, const char *filename) {
    xmlExpNodePtr expr = NULL, sub;
    FILE *input;
    char expression[5000];
    int len;

    input = fopen(filename, "r");
    if (input == NULL) {
        xmlGenericError(xmlGenericErrorContext,
		"Cannot open %s for reading\n", filename);
	return;
    }
    while (fgets(expression, 4500, input) != NULL) {
	len = strlen(expression);
	len--;
	while ((len >= 0) &&
	       ((expression[len] == '\n') || (expression[len] == '\t') ||
		(expression[len] == '\r') || (expression[len] == ' '))) len--;
	expression[len + 1] = 0;
	if (len >= 0) {
	    if (expression[0] == '#')
		continue;
	    if ((expression[0] == '=') && (expression[1] == '>')) {
		char *str = &expression[2];

		if (expr != NULL) {
		    xmlExpFree(ctxt, expr);
		    if (xmlExpCtxtNbNodes(ctxt) != 0)
		        printf(" Parse/free of Expression leaked %d\n",
			       xmlExpCtxtNbNodes(ctxt));
		    expr = NULL;
		}
		printf("Expression: %s\n", str) ;
		expr = xmlExpParse(ctxt, str);
		if (expr == NULL) {
		    printf("   parsing Failed\n");
		    break;
		}
	    } else if (expr != NULL) {
	        int expect = -1;
		int nodes1, nodes2;

		if (expression[0] == '0')
		    expect = 0;
		if (expression[0] == '1')
		    expect = 1;
		printf("Subexp: %s", expression + 2) ;
		nodes1 = xmlExpCtxtNbNodes(ctxt);
		sub = xmlExpParse(ctxt, expression + 2);
		if (sub == NULL) {
		    printf("   parsing Failed\n");
		    break;
		} else {
		    int ret;

		    nodes2 = xmlExpCtxtNbNodes(ctxt);
		    ret = xmlExpSubsume(ctxt, expr, sub);

		    if ((expect == 1) && (ret == 1)) {
			printf(" => accept, Ok\n");
		    } else if ((expect == 0) && (ret == 0)) {
		        printf(" => reject, Ok\n");
		    } else if ((expect == 1) && (ret == 0)) {
			printf(" => reject, Failed\n");
		    } else if ((expect == 0) && (ret == 1)) {
			printf(" => accept, Failed\n");
		    } else {
		        printf(" => fail internally\n");
		    }
		    if (xmlExpCtxtNbNodes(ctxt) > nodes2) {
		        printf(" Subsume leaked %d\n",
			       xmlExpCtxtNbNodes(ctxt) - nodes2);
			nodes1 += xmlExpCtxtNbNodes(ctxt) - nodes2;
		    }
		    xmlExpFree(ctxt, sub);
		    if (xmlExpCtxtNbNodes(ctxt) > nodes1) {
		        printf(" Parse/free leaked %d\n",
			       xmlExpCtxtNbNodes(ctxt) - nodes1);
		    }
		}

	    }
	}
    }
    if (expr != NULL) {
	xmlExpFree(ctxt, expr);
	if (xmlExpCtxtNbNodes(ctxt) != 0)
	    printf(" Parse/free of Expression leaked %d\n",
		   xmlExpCtxtNbNodes(ctxt));
    }
    fclose(input);
}

static void
testReduce(xmlExpCtxtPtr ctxt, xmlExpNodePtr expr, const char *tst) {
    xmlBufferPtr xmlExpBuf;
    xmlExpNodePtr sub, deriv;
    xmlExpBuf = xmlBufferCreate();

    sub = xmlExpParse(ctxt, tst);
    if (sub == NULL) {
        printf("Subset %s failed to parse\n", tst);
	return;
    }
    xmlExpDump(xmlExpBuf, sub);
    printf("Subset parsed as: %s\n",
           (const char *) xmlBufferContent(xmlExpBuf));
    deriv = xmlExpExpDerive(ctxt, expr, sub);
    if (deriv == NULL) {
        printf("Derivation led to an internal error, report this !\n");
	return;
    } else {
        xmlBufferEmpty(xmlExpBuf);
	xmlExpDump(xmlExpBuf, deriv);
	if (xmlExpIsNillable(deriv))
	    printf("Resulting nillable derivation: %s\n",
	           (const char *) xmlBufferContent(xmlExpBuf));
	else
	    printf("Resulting derivation: %s\n",
	           (const char *) xmlBufferContent(xmlExpBuf));
	xmlExpFree(ctxt, deriv);
    }
    xmlExpFree(ctxt, sub);
}

static void
exprDebug(xmlExpCtxtPtr ctxt, xmlExpNodePtr expr) {
    xmlBufferPtr xmlExpBuf;
    xmlExpNodePtr deriv;
    const char *list[40];
    int ret;

    xmlExpBuf = xmlBufferCreate();

    if (expr == NULL) {
        printf("Failed to parse\n");
	return;
    }
    xmlExpDump(xmlExpBuf, expr);
    printf("Parsed as: %s\n", (const char *) xmlBufferContent(xmlExpBuf));
    printf("Max token input = %d\n", xmlExpMaxToken(expr));
    if (xmlExpIsNillable(expr) == 1)
	printf("Is nillable\n");
    ret = xmlExpGetLanguage(ctxt, expr, (const xmlChar **) &list[0], 40);
    if (ret < 0)
	printf("Failed to get list: %d\n", ret);
    else {
	int i;

	printf("Language has %d strings, testing string derivations\n", ret);
	for (i = 0;i < ret;i++) {
	    deriv = xmlExpStringDerive(ctxt, expr, BAD_CAST list[i], -1);
	    if (deriv == NULL) {
		printf("  %s -> derivation failed\n", list[i]);
	    } else {
		xmlBufferEmpty(xmlExpBuf);
		xmlExpDump(xmlExpBuf, deriv);
		printf("  %s -> %s\n", list[i],
		       (const char *) xmlBufferContent(xmlExpBuf));
	    }
	    xmlExpFree(ctxt, deriv);
	}
    }
    xmlBufferFree(xmlExpBuf);
}
#endif

static void usage(const char *name) {
    fprintf(stderr, "Usage: %s [flags]\n", name);
    fprintf(stderr, "Testing tool for libxml2 string and pattern regexps\n");
    fprintf(stderr, "   --debug: switch on debugging\n");
    fprintf(stderr, "   --repeat: loop on the operation\n");
#ifdef LIBXML_EXPR_ENABLED
    fprintf(stderr, "   --expr: test xmlExp and not xmlRegexp\n");
#endif
    fprintf(stderr, "   --input filename: use the given filename for regexp\n");
    fprintf(stderr, "   --input filename: use the given filename for exp\n");
}

int main(int argc, char **argv) {
    xmlRegexpPtr comp = NULL;
#ifdef LIBXML_EXPR_ENABLED
    xmlExpNodePtr expr = NULL;
    int use_exp = 0;
    xmlExpCtxtPtr ctxt = NULL;
#endif
    const char *pattern = NULL;
    char *filename = NULL;
    int i;

    xmlInitMemory();

    if (argc <= 1) {
	usage(argv[0]);
	return(1);
    }
    for (i = 1; i < argc ; i++) {
	if (!strcmp(argv[i], "-"))
	    break;

	if (argv[i][0] != '-')
	    continue;
	if (!strcmp(argv[i], "--"))
	    break;

	if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) {
	    debug++;
	} else if ((!strcmp(argv[i], "-repeat")) ||
	         (!strcmp(argv[i], "--repeat"))) {
	    repeat++;
#ifdef LIBXML_EXPR_ENABLED
	} else if ((!strcmp(argv[i], "-expr")) ||
	         (!strcmp(argv[i], "--expr"))) {
	    use_exp++;
#endif
	} else if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "-f")) ||
		   (!strcmp(argv[i], "--input")))
	    filename = argv[++i];
        else {
	    fprintf(stderr, "Unknown option %s\n", argv[i]);
	    usage(argv[0]);
	}
    }

#ifdef LIBXML_EXPR_ENABLED
    if (use_exp)
	ctxt = xmlExpNewCtxt(0, NULL);
#endif

    if (filename != NULL) {
#ifdef LIBXML_EXPR_ENABLED
        if (use_exp)
	    runFileTest(ctxt, filename);
	else
#endif
	    testRegexpFile(filename);
    } else {
        int  data = 0;
#ifdef LIBXML_EXPR_ENABLED

        if (use_exp) {
	    for (i = 1; i < argc ; i++) {
	        if (strcmp(argv[i], "--") == 0)
		    data = 1;
		else if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0) ||
		    (data == 1)) {
		    if (pattern == NULL) {
			pattern = argv[i];
			printf("Testing expr %s:\n", pattern);
			expr = xmlExpParse(ctxt, pattern);
			if (expr == NULL) {
			    printf("   failed to compile\n");
			    break;
			}
			if (debug) {
			    exprDebug(ctxt, expr);
			}
		    } else {
			testReduce(ctxt, expr, argv[i]);
		    }
		}
	    }
	    if (expr != NULL) {
		xmlExpFree(ctxt, expr);
		expr = NULL;
	    }
	} else
#endif
        {
	    for (i = 1; i < argc ; i++) {
	        if (strcmp(argv[i], "--") == 0)
		    data = 1;
		else if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0) ||
		         (data == 1)) {
		    if (pattern == NULL) {
			pattern = argv[i];
			printf("Testing %s:\n", pattern);
			comp = xmlRegexpCompile((const xmlChar *) pattern);
			if (comp == NULL) {
			    printf("   failed to compile\n");
			    break;
			}
			if (debug)
			    xmlRegexpPrint(stdout, comp);
		    } else {
			testRegexp(comp, argv[i]);
		    }
		}
	    }
	    if (comp != NULL)
		xmlRegFreeRegexp(comp);
        }
    }
#ifdef LIBXML_EXPR_ENABLED
    if (ctxt != NULL) {
	printf("Ops: %d nodes, %d cons\n",
	       xmlExpCtxtNbNodes(ctxt), xmlExpCtxtNbCons(ctxt));
	xmlExpFreeCtxt(ctxt);
    }
#endif
    xmlCleanupParser();
    xmlMemoryDump();
    return(0);
}

#else
#include <stdio.h>
int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
    printf("%s : Regexp support not compiled in\n", argv[0]);
    return(0);
}
#endif /* LIBXML_REGEXP_ENABLED */