blob: 3549f938f1de969966916dec1718e01b43f437f8 [file] [log] [blame]
Daniel Veillard98bf67a2003-01-23 22:00:09 +00001/*
2 * testRelax.c : a small tester program for RelaxNG validation
3 *
4 * See Copyright for the status of this software.
5 *
6 * Daniel.Veillard@w3.org
7 */
8
9#include "libxml.h"
10#ifdef LIBXML_SCHEMAS_ENABLED
11
12#include <libxml/xmlversion.h>
13#include <libxml/parser.h>
14
15#include <stdio.h>
16#include <string.h>
17#include <stdarg.h>
18
19
20#ifdef HAVE_SYS_TYPES_H
21#include <sys/types.h>
22#endif
23#ifdef HAVE_SYS_STAT_H
24#include <sys/stat.h>
25#endif
26#ifdef HAVE_FCNTL_H
27#include <fcntl.h>
28#endif
29#ifdef HAVE_UNISTD_H
30#include <unistd.h>
31#endif
32#ifdef HAVE_STDLIB_H
33#include <stdlib.h>
34#endif
35#ifdef HAVE_SYS_MMAN_H
36#include <sys/mman.h>
37/* seems needed for Solaris */
38#ifndef MAP_FAILED
39#define MAP_FAILED ((void *) -1)
40#endif
41#endif
42
43#include <libxml/xmlmemory.h>
44#include <libxml/debugXML.h>
45#include <libxml/relaxng.h>
46
47#ifdef LIBXML_DEBUG_ENABLED
48static int debug = 0;
49#endif
50static int noout = 0;
Daniel Veillardfebcca42003-02-16 15:44:18 +000051static int tree = 0;
Daniel Richard G5706b6d2012-08-06 11:32:54 +080052#ifdef HAVE_MMAP
Daniel Veillard98bf67a2003-01-23 22:00:09 +000053static int memory = 0;
54#endif
55
56
57int main(int argc, char **argv) {
58 int i;
59 int files = 0;
60 xmlRelaxNGPtr schema = NULL;
61
62 for (i = 1; i < argc ; i++) {
63#ifdef LIBXML_DEBUG_ENABLED
64 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
65 debug++;
66 else
67#endif
Daniel Richard G5706b6d2012-08-06 11:32:54 +080068#ifdef HAVE_MMAP
Daniel Veillard98bf67a2003-01-23 22:00:09 +000069 if ((!strcmp(argv[i], "-memory")) || (!strcmp(argv[i], "--memory"))) {
70 memory++;
71 } else
72#endif
73 if ((!strcmp(argv[i], "-noout")) || (!strcmp(argv[i], "--noout"))) {
74 noout++;
Daniel Veillardfebcca42003-02-16 15:44:18 +000075 } else
76 if ((!strcmp(argv[i], "-tree")) || (!strcmp(argv[i], "--tree"))) {
77 tree++;
Daniel Veillard98bf67a2003-01-23 22:00:09 +000078 }
79 }
80 xmlLineNumbersDefault(1);
Daniel Veillard1ed7f362003-02-03 10:57:45 +000081 xmlSubstituteEntitiesDefault(1);
Daniel Veillard98bf67a2003-01-23 22:00:09 +000082 for (i = 1; i < argc ; i++) {
83 if (argv[i][0] != '-') {
84 if (schema == NULL) {
85 xmlRelaxNGParserCtxtPtr ctxt;
86
Daniel Richard G5706b6d2012-08-06 11:32:54 +080087#ifdef HAVE_MMAP
Daniel Veillard98bf67a2003-01-23 22:00:09 +000088 if (memory) {
89 int fd;
90 struct stat info;
91 const char *base;
Daniel Veillardf8e3db02012-09-11 13:26:36 +080092 if (stat(argv[i], &info) < 0)
Daniel Veillard98bf67a2003-01-23 22:00:09 +000093 break;
94 if ((fd = open(argv[i], O_RDONLY)) < 0)
95 break;
96 base = mmap(NULL, info.st_size, PROT_READ,
97 MAP_SHARED, fd, 0) ;
98 if (base == (void *) MAP_FAILED)
99 break;
100
101 ctxt = xmlRelaxNGNewMemParserCtxt((char *)base,info.st_size);
102
103 xmlRelaxNGSetParserErrors(ctxt,
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700104 xmlGenericError, xmlGenericError, NULL);
Daniel Veillard98bf67a2003-01-23 22:00:09 +0000105 schema = xmlRelaxNGParse(ctxt);
106 xmlRelaxNGFreeParserCtxt(ctxt);
107 munmap((char *) base, info.st_size);
108 } else
109#endif
110 {
111 ctxt = xmlRelaxNGNewParserCtxt(argv[i]);
112 xmlRelaxNGSetParserErrors(ctxt,
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700113 xmlGenericError, xmlGenericError, NULL);
Daniel Veillard98bf67a2003-01-23 22:00:09 +0000114 schema = xmlRelaxNGParse(ctxt);
115 xmlRelaxNGFreeParserCtxt(ctxt);
116 }
117 if (schema == NULL) {
118 printf("Relax-NG schema %s failed to compile\n", argv[i]);
119 files = -1;
120 break;
121 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000122#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard98bf67a2003-01-23 22:00:09 +0000123#ifdef LIBXML_DEBUG_ENABLED
124 if (debug)
125 xmlRelaxNGDump(stdout, schema);
126#endif
Daniel Veillardfebcca42003-02-16 15:44:18 +0000127 if (tree)
128 xmlRelaxNGDumpTree(stdout, schema);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000129#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard98bf67a2003-01-23 22:00:09 +0000130 } else {
131 xmlDocPtr doc;
132
Daniel Veillard87247e82004-01-13 20:42:02 +0000133 doc = xmlReadFile(argv[i],NULL,0);
Daniel Veillard98bf67a2003-01-23 22:00:09 +0000134
135 if (doc == NULL) {
136 fprintf(stderr, "Could not parse %s\n", argv[i]);
137 } else {
138 xmlRelaxNGValidCtxtPtr ctxt;
139 int ret;
140
141 ctxt = xmlRelaxNGNewValidCtxt(schema);
142 xmlRelaxNGSetValidErrors(ctxt,
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700143 xmlGenericError, xmlGenericError, NULL);
Daniel Veillard98bf67a2003-01-23 22:00:09 +0000144 ret = xmlRelaxNGValidateDoc(ctxt, doc);
145 if (ret == 0) {
146 printf("%s validates\n", argv[i]);
147 } else if (ret > 0) {
148 printf("%s fails to validate\n", argv[i]);
149 } else {
150 printf("%s validation generated an internal error\n",
151 argv[i]);
152 }
153 xmlRelaxNGFreeValidCtxt(ctxt);
154 xmlFreeDoc(doc);
155 }
156 }
157 files ++;
158 }
159 }
160 if (schema != NULL)
161 xmlRelaxNGFree(schema);
162 if (files == 0) {
163 printf("Usage : %s [--debug] [--noout] schemas XMLfiles ...\n",
164 argv[0]);
165 printf("\tParse the HTML files and output the result of the parsing\n");
166#ifdef LIBXML_DEBUG_ENABLED
167 printf("\t--debug : dump a debug tree of the in-memory document\n");
168#endif
169 printf("\t--noout : do not print the result\n");
Daniel Veillardfebcca42003-02-16 15:44:18 +0000170 printf("\t--tree : print the intermediate Relax-NG document tree\n");
Daniel Richard G5706b6d2012-08-06 11:32:54 +0800171#ifdef HAVE_MMAP
Daniel Veillard98bf67a2003-01-23 22:00:09 +0000172 printf("\t--memory : test the schemas in memory parsing\n");
173#endif
174 }
175 xmlRelaxNGCleanupTypes();
176 xmlCleanupParser();
177 xmlMemoryDump();
178
179 return(0);
180}
181
182#else
183#include <stdio.h>
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000184int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
Daniel Veillard98bf67a2003-01-23 22:00:09 +0000185 printf("%s : RelaxNG support not compiled in\n", argv[0]);
186 return(0);
187}
188#endif /* LIBXML_SCHEMAS_ENABLED */