1 | /*
|
---|
2 | * xsltproc.c: user program for the XSL Transformation 1.0 engine
|
---|
3 | *
|
---|
4 | * See Copyright for the status of this software.
|
---|
5 | *
|
---|
6 | * daniel@veillard.com
|
---|
7 | */
|
---|
8 |
|
---|
9 | #include "libxslt/libxslt.h"
|
---|
10 | #include "libexslt/exslt.h"
|
---|
11 | #include <stdio.h>
|
---|
12 | #ifdef HAVE_STRING_H
|
---|
13 | #include <string.h>
|
---|
14 | #endif
|
---|
15 | #ifdef HAVE_SYS_TIME_H
|
---|
16 | #include <sys/time.h>
|
---|
17 | #endif
|
---|
18 | #ifdef HAVE_TIME_H
|
---|
19 | #include <time.h>
|
---|
20 | #endif
|
---|
21 | #ifdef HAVE_SYS_STAT_H
|
---|
22 | #include <sys/stat.h>
|
---|
23 | #endif
|
---|
24 | #ifdef HAVE_UNISTD_H
|
---|
25 | #include <unistd.h>
|
---|
26 | #endif
|
---|
27 | #ifdef HAVE_STDLIB_H
|
---|
28 | #include <stdlib.h>
|
---|
29 | #endif
|
---|
30 | #ifdef HAVE_STDARG_H
|
---|
31 | #include <stdarg.h>
|
---|
32 | #endif
|
---|
33 | #include <libxml/xmlmemory.h>
|
---|
34 | #include <libxml/debugXML.h>
|
---|
35 | #include <libxml/HTMLtree.h>
|
---|
36 | #include <libxml/xmlIO.h>
|
---|
37 | #ifdef LIBXML_XINCLUDE_ENABLED
|
---|
38 | #include <libxml/xinclude.h>
|
---|
39 | #endif
|
---|
40 | #ifdef LIBXML_CATALOG_ENABLED
|
---|
41 | #include <libxml/catalog.h>
|
---|
42 | #endif
|
---|
43 | #include <libxml/parser.h>
|
---|
44 | #include <libxml/parserInternals.h>
|
---|
45 | #include <libxml/uri.h>
|
---|
46 |
|
---|
47 | #include <libxslt/xslt.h>
|
---|
48 | #include <libxslt/xsltInternals.h>
|
---|
49 | #include <libxslt/transform.h>
|
---|
50 | #include <libxslt/xsltutils.h>
|
---|
51 | #include <libxslt/extensions.h>
|
---|
52 | #include <libxslt/security.h>
|
---|
53 |
|
---|
54 | #include <libexslt/exsltconfig.h>
|
---|
55 |
|
---|
56 | #if defined(WIN32) && !defined (__CYGWIN__)
|
---|
57 | #if defined(_MSC_VER) || defined(__MINGW32__)
|
---|
58 | #include <winsock2.h>
|
---|
59 | #define gettimeofday(p1,p2)
|
---|
60 | #define snprintf _snprintf
|
---|
61 | #endif /* _MS_VER */
|
---|
62 | #else /* WIN32 */
|
---|
63 | #if defined(HAVE_SYS_TIME_H)
|
---|
64 | #include <sys/time.h>
|
---|
65 | #elif defined(HAVE_TIME_H)
|
---|
66 | #include <time.h>
|
---|
67 | #endif
|
---|
68 | #endif /* WIN32 */
|
---|
69 |
|
---|
70 | #ifdef HAVE_SYS_TIMEB_H
|
---|
71 | #include <sys/timeb.h>
|
---|
72 | #endif
|
---|
73 |
|
---|
74 | static int debug = 0;
|
---|
75 | static int repeat = 0;
|
---|
76 | static int timing = 0;
|
---|
77 | static int dumpextensions = 0;
|
---|
78 | static int novalid = 0;
|
---|
79 | static int nodtdattr = 0;
|
---|
80 | static int noout = 0;
|
---|
81 | static int nodict = 0;
|
---|
82 | #ifdef LIBXML_HTML_ENABLED
|
---|
83 | static int html = 0;
|
---|
84 | #endif
|
---|
85 | static char *encoding = NULL;
|
---|
86 | static int load_trace = 0;
|
---|
87 | #ifdef LIBXML_XINCLUDE_ENABLED
|
---|
88 | static int xinclude = 0;
|
---|
89 | static int xincludestyle = 0;
|
---|
90 | #endif
|
---|
91 | static int profile = 0;
|
---|
92 |
|
---|
93 | #define MAX_PARAMETERS 64
|
---|
94 | #define MAX_PATHS 64
|
---|
95 |
|
---|
96 | static int options = XSLT_PARSE_OPTIONS;
|
---|
97 | static const char *params[MAX_PARAMETERS + 1];
|
---|
98 | static int nbparams = 0;
|
---|
99 | static xmlChar *strparams[MAX_PARAMETERS + 1];
|
---|
100 | static int nbstrparams = 0;
|
---|
101 | static xmlChar *paths[MAX_PATHS + 1];
|
---|
102 | static int nbpaths = 0;
|
---|
103 | static char *output = NULL;
|
---|
104 | static int errorno = 0;
|
---|
105 | static const char *writesubtree = NULL;
|
---|
106 |
|
---|
107 | /*
|
---|
108 | * Entity loading control and customization.
|
---|
109 | */
|
---|
110 | static
|
---|
111 | void parsePath(const xmlChar *path) {
|
---|
112 | const xmlChar *cur;
|
---|
113 |
|
---|
114 | if (path == NULL)
|
---|
115 | return;
|
---|
116 | while (*path != 0) {
|
---|
117 | if (nbpaths >= MAX_PATHS) {
|
---|
118 | fprintf(stderr, "MAX_PATHS reached: too many paths\n");
|
---|
119 | return;
|
---|
120 | }
|
---|
121 | cur = path;
|
---|
122 | while ((*cur == ' ') || (*cur == ':'))
|
---|
123 | cur++;
|
---|
124 | path = cur;
|
---|
125 | while ((*cur != 0) && (*cur != ' ') && (*cur != ':'))
|
---|
126 | cur++;
|
---|
127 | if (cur != path) {
|
---|
128 | paths[nbpaths] = xmlStrndup(path, cur - path);
|
---|
129 | if (paths[nbpaths] != NULL)
|
---|
130 | nbpaths++;
|
---|
131 | path = cur;
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | xmlExternalEntityLoader defaultEntityLoader = NULL;
|
---|
137 |
|
---|
138 | static xmlParserInputPtr
|
---|
139 | xsltprocExternalEntityLoader(const char *URL, const char *ID,
|
---|
140 | xmlParserCtxtPtr ctxt) {
|
---|
141 | xmlParserInputPtr ret;
|
---|
142 | warningSAXFunc warning = NULL;
|
---|
143 |
|
---|
144 | int i;
|
---|
145 | const char *lastsegment = URL;
|
---|
146 | const char *iter = URL;
|
---|
147 |
|
---|
148 | if (nbpaths > 0) {
|
---|
149 | while (*iter != 0) {
|
---|
150 | if (*iter == '/')
|
---|
151 | lastsegment = iter + 1;
|
---|
152 | iter++;
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | if ((ctxt != NULL) && (ctxt->sax != NULL)) {
|
---|
157 | warning = ctxt->sax->warning;
|
---|
158 | ctxt->sax->warning = NULL;
|
---|
159 | }
|
---|
160 |
|
---|
161 | if (defaultEntityLoader != NULL) {
|
---|
162 | ret = defaultEntityLoader(URL, ID, ctxt);
|
---|
163 | if (ret != NULL) {
|
---|
164 | if (warning != NULL)
|
---|
165 | ctxt->sax->warning = warning;
|
---|
166 | if (load_trace) {
|
---|
167 | fprintf \
|
---|
168 | (stderr,
|
---|
169 | "Loaded URL=\"%s\" ID=\"%s\"\n",
|
---|
170 | URL ? URL : "(null)",
|
---|
171 | ID ? ID : "(null)");
|
---|
172 | }
|
---|
173 | return(ret);
|
---|
174 | }
|
---|
175 | }
|
---|
176 | for (i = 0;i < nbpaths;i++) {
|
---|
177 | xmlChar *newURL;
|
---|
178 |
|
---|
179 | newURL = xmlStrdup((const xmlChar *) paths[i]);
|
---|
180 | newURL = xmlStrcat(newURL, (const xmlChar *) "/");
|
---|
181 | newURL = xmlStrcat(newURL, (const xmlChar *) lastsegment);
|
---|
182 | if (newURL != NULL) {
|
---|
183 | ret = defaultEntityLoader((const char *)newURL, ID, ctxt);
|
---|
184 | if (ret != NULL) {
|
---|
185 | if (warning != NULL)
|
---|
186 | ctxt->sax->warning = warning;
|
---|
187 | if (load_trace) {
|
---|
188 | fprintf \
|
---|
189 | (stderr,
|
---|
190 | "Loaded URL=\"%s\" ID=\"%s\"\n",
|
---|
191 | newURL,
|
---|
192 | ID ? ID : "(null)");
|
---|
193 | }
|
---|
194 | xmlFree(newURL);
|
---|
195 | return(ret);
|
---|
196 | }
|
---|
197 | xmlFree(newURL);
|
---|
198 | }
|
---|
199 | }
|
---|
200 | if (warning != NULL) {
|
---|
201 | ctxt->sax->warning = warning;
|
---|
202 | if (URL != NULL)
|
---|
203 | warning(ctxt, "failed to load external entity \"%s\"\n", URL);
|
---|
204 | else if (ID != NULL)
|
---|
205 | warning(ctxt, "failed to load external entity \"%s\"\n", ID);
|
---|
206 | }
|
---|
207 | return(NULL);
|
---|
208 | }
|
---|
209 |
|
---|
210 | /*
|
---|
211 | * Internal timing routines to remove the necessity to have unix-specific
|
---|
212 | * function calls
|
---|
213 | */
|
---|
214 | #ifndef HAVE_GETTIMEOFDAY
|
---|
215 | #ifdef HAVE_SYS_TIMEB_H
|
---|
216 | #ifdef HAVE_SYS_TIME_H
|
---|
217 | #ifdef HAVE_FTIME
|
---|
218 |
|
---|
219 | int
|
---|
220 | my_gettimeofday(struct timeval *tvp, void *tzp)
|
---|
221 | {
|
---|
222 | struct timeb timebuffer;
|
---|
223 |
|
---|
224 | ftime(&timebuffer);
|
---|
225 | if (tvp) {
|
---|
226 | tvp->tv_sec = timebuffer.time;
|
---|
227 | tvp->tv_usec = timebuffer.millitm * 1000L;
|
---|
228 | }
|
---|
229 | return (0);
|
---|
230 | }
|
---|
231 | #define HAVE_GETTIMEOFDAY 1
|
---|
232 | #define gettimeofday my_gettimeofday
|
---|
233 |
|
---|
234 | #endif /* HAVE_FTIME */
|
---|
235 | #endif /* HAVE_SYS_TIME_H */
|
---|
236 | #endif /* HAVE_SYS_TIMEB_H */
|
---|
237 | #endif /* !HAVE_GETTIMEOFDAY */
|
---|
238 |
|
---|
239 | #if defined(HAVE_GETTIMEOFDAY)
|
---|
240 | static struct timeval begin, endtime;
|
---|
241 | /*
|
---|
242 | * startTimer: call where you want to start timing
|
---|
243 | */
|
---|
244 | static void startTimer(void)
|
---|
245 | {
|
---|
246 | gettimeofday(&begin,NULL);
|
---|
247 | }
|
---|
248 | /*
|
---|
249 | * endTimer: call where you want to stop timing and to print out a
|
---|
250 | * message about the timing performed; format is a printf
|
---|
251 | * type argument
|
---|
252 | */
|
---|
253 | static void endTimer(const char *format, ...)
|
---|
254 | {
|
---|
255 | long msec;
|
---|
256 | va_list ap;
|
---|
257 |
|
---|
258 | gettimeofday(&endtime, NULL);
|
---|
259 | msec = endtime.tv_sec - begin.tv_sec;
|
---|
260 | msec *= 1000;
|
---|
261 | msec += (endtime.tv_usec - begin.tv_usec) / 1000;
|
---|
262 |
|
---|
263 | #ifndef HAVE_STDARG_H
|
---|
264 | #error "endTimer required stdarg functions"
|
---|
265 | #endif
|
---|
266 | va_start(ap, format);
|
---|
267 | vfprintf(stderr,format,ap);
|
---|
268 | va_end(ap);
|
---|
269 |
|
---|
270 | fprintf(stderr, " took %ld ms\n", msec);
|
---|
271 | }
|
---|
272 | #elif defined(HAVE_TIME_H)
|
---|
273 | /*
|
---|
274 | * No gettimeofday function, so we have to make do with calling clock.
|
---|
275 | * This is obviously less accurate, but there's little we can do about
|
---|
276 | * that.
|
---|
277 | */
|
---|
278 | #ifndef CLOCKS_PER_SEC
|
---|
279 | #define CLOCKS_PER_SEC 100
|
---|
280 | #endif
|
---|
281 |
|
---|
282 | clock_t begin, endtime;
|
---|
283 | static void startTimer(void)
|
---|
284 | {
|
---|
285 | begin=clock();
|
---|
286 | }
|
---|
287 | static void endTimer(char *format, ...)
|
---|
288 | {
|
---|
289 | long msec;
|
---|
290 | va_list ap;
|
---|
291 |
|
---|
292 | endtime=clock();
|
---|
293 | msec = ((endtime-begin) * 1000) / CLOCKS_PER_SEC;
|
---|
294 |
|
---|
295 | #ifndef HAVE_STDARG_H
|
---|
296 | #error "endTimer required stdarg functions"
|
---|
297 | #endif
|
---|
298 | va_start(ap, format);
|
---|
299 | vfprintf(stderr,format,ap);
|
---|
300 | va_end(ap);
|
---|
301 | fprintf(stderr, " took %ld ms\n", msec);
|
---|
302 | }
|
---|
303 | #else
|
---|
304 | /*
|
---|
305 | * We don't have a gettimeofday or time.h, so we just don't do timing
|
---|
306 | */
|
---|
307 | static void startTimer(void)
|
---|
308 | {
|
---|
309 | /*
|
---|
310 | * Do nothing
|
---|
311 | */
|
---|
312 | }
|
---|
313 | static void endTimer(char *format, ...)
|
---|
314 | {
|
---|
315 | /*
|
---|
316 | * We cannot do anything because we don't have a timing function
|
---|
317 | */
|
---|
318 | #ifdef HAVE_STDARG_H
|
---|
319 | va_start(ap, format);
|
---|
320 | vfprintf(stderr,format,ap);
|
---|
321 | va_end(ap);
|
---|
322 | fprintf(stderr, " was not timed\n", msec);
|
---|
323 | #else
|
---|
324 | /* We don't have gettimeofday, time or stdarg.h, what crazy world is
|
---|
325 | * this ?!
|
---|
326 | */
|
---|
327 | #endif
|
---|
328 | }
|
---|
329 | #endif
|
---|
330 |
|
---|
331 | /*
|
---|
332 | * xsltSubtreeCheck:
|
---|
333 | *
|
---|
334 | * allow writes only on a subtree specified on the command line
|
---|
335 | */
|
---|
336 | static int
|
---|
337 | xsltSubtreeCheck(xsltSecurityPrefsPtr sec ATTRIBUTE_UNUSED,
|
---|
338 | xsltTransformContextPtr ctxt ATTRIBUTE_UNUSED,
|
---|
339 | const char *value ATTRIBUTE_UNUSED) {
|
---|
340 | int len, ret;
|
---|
341 |
|
---|
342 | if (writesubtree == NULL)
|
---|
343 | return(0);
|
---|
344 | if (value == NULL)
|
---|
345 | return(-1);
|
---|
346 |
|
---|
347 | len = xmlStrlen(BAD_CAST writesubtree);
|
---|
348 | ret = xmlStrncmp(BAD_CAST writesubtree, BAD_CAST value, len);
|
---|
349 | if (ret == 0)
|
---|
350 | return(1);
|
---|
351 | return(0);
|
---|
352 | }
|
---|
353 |
|
---|
354 | static void
|
---|
355 | xsltProcess(xmlDocPtr doc, xsltStylesheetPtr cur, const char *filename) {
|
---|
356 | xmlDocPtr res;
|
---|
357 | xsltTransformContextPtr ctxt;
|
---|
358 |
|
---|
359 |
|
---|
360 | #ifdef LIBXML_XINCLUDE_ENABLED
|
---|
361 | if (xinclude) {
|
---|
362 | if (timing)
|
---|
363 | startTimer();
|
---|
364 | #if LIBXML_VERSION >= 20603
|
---|
365 | xmlXIncludeProcessFlags(doc, XSLT_PARSE_OPTIONS);
|
---|
366 | #else
|
---|
367 | xmlXIncludeProcess(doc);
|
---|
368 | #endif
|
---|
369 | if (timing) {
|
---|
370 | endTimer("XInclude processing %s", filename);
|
---|
371 | }
|
---|
372 | }
|
---|
373 | #endif
|
---|
374 | if (timing)
|
---|
375 | startTimer();
|
---|
376 | if (output == NULL) {
|
---|
377 | if (repeat) {
|
---|
378 | int j;
|
---|
379 |
|
---|
380 | for (j = 1; j < repeat; j++) {
|
---|
381 | res = xsltApplyStylesheet(cur, doc, params);
|
---|
382 | xmlFreeDoc(res);
|
---|
383 | xmlFreeDoc(doc);
|
---|
384 | #ifdef LIBXML_HTML_ENABLED
|
---|
385 | if (html)
|
---|
386 | doc = htmlReadFile(filename, encoding, options);
|
---|
387 | else
|
---|
388 | #endif
|
---|
389 | doc = xmlReadFile(filename, encoding, options);
|
---|
390 | }
|
---|
391 | }
|
---|
392 | ctxt = xsltNewTransformContext(cur, doc);
|
---|
393 | if (ctxt == NULL)
|
---|
394 | return;
|
---|
395 | xsltSetCtxtParseOptions(ctxt, options);
|
---|
396 | #ifdef LIBXML_XINCLUDE_ENABLED
|
---|
397 | if (xinclude)
|
---|
398 | ctxt->xinclude = 1;
|
---|
399 | #endif
|
---|
400 | if (profile) {
|
---|
401 | res = xsltApplyStylesheetUser(cur, doc, params, NULL,
|
---|
402 | stderr, ctxt);
|
---|
403 | } else {
|
---|
404 | res = xsltApplyStylesheetUser(cur, doc, params, NULL,
|
---|
405 | NULL, ctxt);
|
---|
406 | }
|
---|
407 | if (ctxt->state == XSLT_STATE_ERROR)
|
---|
408 | errorno = 9;
|
---|
409 | else if (ctxt->state == XSLT_STATE_STOPPED)
|
---|
410 | errorno = 10;
|
---|
411 | xsltFreeTransformContext(ctxt);
|
---|
412 | if (timing) {
|
---|
413 | if (repeat)
|
---|
414 | endTimer("Applying stylesheet %d times", repeat);
|
---|
415 | else
|
---|
416 | endTimer("Applying stylesheet");
|
---|
417 | }
|
---|
418 | xmlFreeDoc(doc);
|
---|
419 | if (res == NULL) {
|
---|
420 | fprintf(stderr, "no result for %s\n", filename);
|
---|
421 | return;
|
---|
422 | }
|
---|
423 | if (noout) {
|
---|
424 | xmlFreeDoc(res);
|
---|
425 | return;
|
---|
426 | }
|
---|
427 | #ifdef LIBXML_DEBUG_ENABLED
|
---|
428 | if (debug)
|
---|
429 | xmlDebugDumpDocument(stdout, res);
|
---|
430 | else {
|
---|
431 | #endif
|
---|
432 | if (cur->methodURI == NULL) {
|
---|
433 | if (timing)
|
---|
434 | startTimer();
|
---|
435 | xsltSaveResultToFile(stdout, res, cur);
|
---|
436 | if (timing)
|
---|
437 | endTimer("Saving result");
|
---|
438 | } else {
|
---|
439 | if (xmlStrEqual
|
---|
440 | (cur->method, (const xmlChar *) "xhtml")) {
|
---|
441 | fprintf(stderr, "non standard output xhtml\n");
|
---|
442 | if (timing)
|
---|
443 | startTimer();
|
---|
444 | xsltSaveResultToFile(stdout, res, cur);
|
---|
445 | if (timing)
|
---|
446 | endTimer("Saving result");
|
---|
447 | } else {
|
---|
448 | fprintf(stderr,
|
---|
449 | "Unsupported non standard output %s\n",
|
---|
450 | cur->method);
|
---|
451 | errorno = 7;
|
---|
452 | }
|
---|
453 | }
|
---|
454 | #ifdef LIBXML_DEBUG_ENABLED
|
---|
455 | }
|
---|
456 | #endif
|
---|
457 |
|
---|
458 | xmlFreeDoc(res);
|
---|
459 | } else {
|
---|
460 | int ret;
|
---|
461 | ctxt = xsltNewTransformContext(cur, doc);
|
---|
462 | if (ctxt == NULL)
|
---|
463 | return;
|
---|
464 | if (profile) {
|
---|
465 | ret = xsltRunStylesheetUser(cur, doc, params, output,
|
---|
466 | NULL, NULL, stderr, ctxt);
|
---|
467 | } else {
|
---|
468 | ret = xsltRunStylesheetUser(cur, doc, params, output,
|
---|
469 | NULL, NULL, NULL, ctxt);
|
---|
470 | }
|
---|
471 | if (ret == -1)
|
---|
472 | errorno = 11;
|
---|
473 | else if (ctxt->state == XSLT_STATE_ERROR)
|
---|
474 | errorno = 9;
|
---|
475 | else if (ctxt->state == XSLT_STATE_STOPPED)
|
---|
476 | errorno = 10;
|
---|
477 | xsltFreeTransformContext(ctxt);
|
---|
478 | if (timing)
|
---|
479 | endTimer("Running stylesheet and saving result");
|
---|
480 | xmlFreeDoc(doc);
|
---|
481 | }
|
---|
482 | }
|
---|
483 |
|
---|
484 | static void usage(const char *name) {
|
---|
485 | printf("Usage: %s [options] stylesheet file [file ...]\n", name);
|
---|
486 | printf(" Options:\n");
|
---|
487 | printf("\t--version or -V: show the version of libxml and libxslt used\n");
|
---|
488 | printf("\t--verbose or -v: show logs of what's happening\n");
|
---|
489 | printf("\t--output file or -o file: save to a given file\n");
|
---|
490 | printf("\t--timing: display the time used\n");
|
---|
491 | printf("\t--repeat: run the transformation 20 times\n");
|
---|
492 | #ifdef LIBXML_DEBUG_ENABLED
|
---|
493 | printf("\t--debug: dump the tree of the result instead\n");
|
---|
494 | #endif
|
---|
495 | printf("\t--dumpextensions: dump the registered extension elements and functions to stdout\n");
|
---|
496 | printf("\t--novalid skip the Dtd loading phase\n");
|
---|
497 | printf("\t--nodtdattr do not default attributes from the DTD\n");
|
---|
498 | printf("\t--noout: do not dump the result\n");
|
---|
499 | printf("\t--maxdepth val : increase the maximum depth\n");
|
---|
500 | printf("\t--maxparserdepth val : increase the maximum parser depth\n");
|
---|
501 | #ifdef LIBXML_HTML_ENABLED
|
---|
502 | printf("\t--html: the input document is(are) an HTML file(s)\n");
|
---|
503 | #endif
|
---|
504 | printf("\t--encoding: the input document character encoding\n");
|
---|
505 | printf("\t--param name value : pass a (parameter,value) pair\n");
|
---|
506 | printf("\t value is an UTF8 XPath expression.\n");
|
---|
507 | printf("\t string values must be quoted like \"'string'\"\n or");
|
---|
508 | printf("\t use stringparam to avoid it\n");
|
---|
509 | printf("\t--stringparam name value : pass a (parameter, UTF8 string value) pair\n");
|
---|
510 | printf("\t--path 'paths': provide a set of paths for resources\n");
|
---|
511 | printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
|
---|
512 | printf("\t--nowrite : refuse to write to any file or resource\n");
|
---|
513 | printf("\t--nomkdir : refuse to create directories\n");
|
---|
514 | printf("\t--writesubtree path : allow file write only with the path subtree\n");
|
---|
515 | #ifdef LIBXML_CATALOG_ENABLED
|
---|
516 | printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
|
---|
517 | printf("\t otherwise XML Catalogs starting from \n");
|
---|
518 | printf("\t file:///etc/xml/catalog are activated by default\n");
|
---|
519 | #endif
|
---|
520 | #ifdef LIBXML_XINCLUDE_ENABLED
|
---|
521 | printf("\t--xinclude : do XInclude processing on document input\n");
|
---|
522 | printf("\t--xincludestyle : do XInclude processing on stylesheets\n");
|
---|
523 | #endif
|
---|
524 | printf("\t--load-trace : print trace of all external entites loaded\n");
|
---|
525 | printf("\t--profile or --norman : dump profiling informations \n");
|
---|
526 | printf("\nProject libxslt home page: http://xmlsoft.org/XSLT/\n");
|
---|
527 | printf("To report bugs and get help: http://xmlsoft.org/XSLT/bugs.html\n");
|
---|
528 | }
|
---|
529 |
|
---|
530 | int
|
---|
531 | main(int argc, char **argv)
|
---|
532 | {
|
---|
533 | int i;
|
---|
534 | xsltStylesheetPtr cur = NULL;
|
---|
535 | xmlDocPtr doc, style;
|
---|
536 | xsltSecurityPrefsPtr sec = NULL;
|
---|
537 |
|
---|
538 | if (argc <= 1) {
|
---|
539 | usage(argv[0]);
|
---|
540 | return (1);
|
---|
541 | }
|
---|
542 |
|
---|
543 | xmlInitMemory();
|
---|
544 |
|
---|
545 | LIBXML_TEST_VERSION
|
---|
546 |
|
---|
547 | sec = xsltNewSecurityPrefs();
|
---|
548 | xsltSetDefaultSecurityPrefs(sec);
|
---|
549 | defaultEntityLoader = xmlGetExternalEntityLoader();
|
---|
550 | xmlSetExternalEntityLoader(xsltprocExternalEntityLoader);
|
---|
551 |
|
---|
552 | for (i = 1; i < argc; i++) {
|
---|
553 | if (!strcmp(argv[i], "-"))
|
---|
554 | break;
|
---|
555 |
|
---|
556 | if (argv[i][0] != '-')
|
---|
557 | continue;
|
---|
558 | #ifdef LIBXML_DEBUG_ENABLED
|
---|
559 | if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) {
|
---|
560 | debug++;
|
---|
561 | } else
|
---|
562 | #endif
|
---|
563 | if ((!strcmp(argv[i], "-v")) ||
|
---|
564 | (!strcmp(argv[i], "-verbose")) ||
|
---|
565 | (!strcmp(argv[i], "--verbose"))) {
|
---|
566 | xsltSetGenericDebugFunc(stderr, NULL);
|
---|
567 | } else if ((!strcmp(argv[i], "-o")) ||
|
---|
568 | (!strcmp(argv[i], "-output")) ||
|
---|
569 | (!strcmp(argv[i], "--output"))) {
|
---|
570 | i++;
|
---|
571 | #if defined(WIN32) || defined (__CYGWIN__)
|
---|
572 | output = xmlCanonicPath(argv[i]);
|
---|
573 | if (output == NULL)
|
---|
574 | #endif
|
---|
575 | output = (char *) xmlStrdup((xmlChar *) argv[i]);
|
---|
576 | } else if ((!strcmp(argv[i], "-V")) ||
|
---|
577 | (!strcmp(argv[i], "-version")) ||
|
---|
578 | (!strcmp(argv[i], "--version"))) {
|
---|
579 | printf("Using libxml %s, libxslt %s and libexslt %s\n",
|
---|
580 | xmlParserVersion, xsltEngineVersion, exsltLibraryVersion);
|
---|
581 | printf
|
---|
582 | ("xsltproc was compiled against libxml %d, libxslt %d and libexslt %d\n",
|
---|
583 | LIBXML_VERSION, LIBXSLT_VERSION, LIBEXSLT_VERSION);
|
---|
584 | printf("libxslt %d was compiled against libxml %d\n",
|
---|
585 | xsltLibxsltVersion, xsltLibxmlVersion);
|
---|
586 | printf("libexslt %d was compiled against libxml %d\n",
|
---|
587 | exsltLibexsltVersion, exsltLibxmlVersion);
|
---|
588 | } else if ((!strcmp(argv[i], "-repeat"))
|
---|
589 | || (!strcmp(argv[i], "--repeat"))) {
|
---|
590 | if (repeat == 0)
|
---|
591 | repeat = 20;
|
---|
592 | else
|
---|
593 | repeat = 100;
|
---|
594 | } else if ((!strcmp(argv[i], "-novalid")) ||
|
---|
595 | (!strcmp(argv[i], "--novalid"))) {
|
---|
596 | novalid++;
|
---|
597 | } else if ((!strcmp(argv[i], "-nodtdattr")) ||
|
---|
598 | (!strcmp(argv[i], "--nodtdattr"))) {
|
---|
599 | nodtdattr++;
|
---|
600 | } else if ((!strcmp(argv[i], "-noout")) ||
|
---|
601 | (!strcmp(argv[i], "--noout"))) {
|
---|
602 | noout++;
|
---|
603 | #ifdef LIBXML_HTML_ENABLED
|
---|
604 | } else if ((!strcmp(argv[i], "-html")) ||
|
---|
605 | (!strcmp(argv[i], "--html"))) {
|
---|
606 | html++;
|
---|
607 | #endif
|
---|
608 | } else if ((!strcmp(argv[i], "-encoding")) ||
|
---|
609 | (!strcmp(argv[i], "--encoding"))) {
|
---|
610 | encoding = argv[++i];
|
---|
611 | } else if ((!strcmp(argv[i], "-timing")) ||
|
---|
612 | (!strcmp(argv[i], "--timing"))) {
|
---|
613 | timing++;
|
---|
614 | } else if ((!strcmp(argv[i], "-profile")) ||
|
---|
615 | (!strcmp(argv[i], "--profile"))) {
|
---|
616 | profile++;
|
---|
617 | } else if ((!strcmp(argv[i], "-nodict")) ||
|
---|
618 | (!strcmp(argv[i], "--nodict"))) {
|
---|
619 | nodict++;
|
---|
620 | } else if ((!strcmp(argv[i], "-norman")) ||
|
---|
621 | (!strcmp(argv[i], "--norman"))) {
|
---|
622 | profile++;
|
---|
623 | } else if ((!strcmp(argv[i], "-nonet")) ||
|
---|
624 | (!strcmp(argv[i], "--nonet"))) {
|
---|
625 | defaultEntityLoader = xmlNoNetExternalEntityLoader;
|
---|
626 | } else if ((!strcmp(argv[i], "-nowrite")) ||
|
---|
627 | (!strcmp(argv[i], "--nowrite"))) {
|
---|
628 | xsltSetSecurityPrefs(sec, XSLT_SECPREF_WRITE_FILE,
|
---|
629 | xsltSecurityForbid);
|
---|
630 | xsltSetSecurityPrefs(sec, XSLT_SECPREF_CREATE_DIRECTORY,
|
---|
631 | xsltSecurityForbid);
|
---|
632 | xsltSetSecurityPrefs(sec, XSLT_SECPREF_WRITE_NETWORK,
|
---|
633 | xsltSecurityForbid);
|
---|
634 | } else if ((!strcmp(argv[i], "-nomkdir")) ||
|
---|
635 | (!strcmp(argv[i], "--nomkdir"))) {
|
---|
636 | xsltSetSecurityPrefs(sec, XSLT_SECPREF_CREATE_DIRECTORY,
|
---|
637 | xsltSecurityForbid);
|
---|
638 | } else if ((!strcmp(argv[i], "-writesubtree")) ||
|
---|
639 | (!strcmp(argv[i], "--writesubtree"))) {
|
---|
640 | i++;
|
---|
641 | writesubtree = argv[i];
|
---|
642 | xsltSetSecurityPrefs(sec, XSLT_SECPREF_WRITE_FILE,
|
---|
643 | xsltSubtreeCheck);
|
---|
644 | } else if ((!strcmp(argv[i], "-path")) ||
|
---|
645 | (!strcmp(argv[i], "--path"))) {
|
---|
646 | i++;
|
---|
647 | parsePath(BAD_CAST argv[i]);
|
---|
648 | #ifdef LIBXML_CATALOG_ENABLED
|
---|
649 | } else if ((!strcmp(argv[i], "-catalogs")) ||
|
---|
650 | (!strcmp(argv[i], "--catalogs"))) {
|
---|
651 | const char *catalogs;
|
---|
652 |
|
---|
653 | catalogs = getenv("SGML_CATALOG_FILES");
|
---|
654 | if (catalogs == NULL) {
|
---|
655 | fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
|
---|
656 | } else {
|
---|
657 | xmlLoadCatalogs(catalogs);
|
---|
658 | }
|
---|
659 | #endif
|
---|
660 | #ifdef LIBXML_XINCLUDE_ENABLED
|
---|
661 | } else if ((!strcmp(argv[i], "-xinclude")) ||
|
---|
662 | (!strcmp(argv[i], "--xinclude"))) {
|
---|
663 | xinclude++;
|
---|
664 | } else if ((!strcmp(argv[i], "-xincludestyle")) ||
|
---|
665 | (!strcmp(argv[i], "--xincludestyle"))) {
|
---|
666 | xincludestyle++;
|
---|
667 | xsltSetXIncludeDefault(1);
|
---|
668 | #endif
|
---|
669 | } else if ((!strcmp(argv[i], "-load-trace")) ||
|
---|
670 | (!strcmp(argv[i], "--load-trace"))) {
|
---|
671 | load_trace++;
|
---|
672 | } else if ((!strcmp(argv[i], "-param")) ||
|
---|
673 | (!strcmp(argv[i], "--param"))) {
|
---|
674 | i++;
|
---|
675 | params[nbparams++] = argv[i++];
|
---|
676 | params[nbparams++] = argv[i];
|
---|
677 | if (nbparams >= MAX_PARAMETERS) {
|
---|
678 | fprintf(stderr, "too many params increase MAX_PARAMETERS \n");
|
---|
679 | return (2);
|
---|
680 | }
|
---|
681 | } else if ((!strcmp(argv[i], "-stringparam")) ||
|
---|
682 | (!strcmp(argv[i], "--stringparam"))) {
|
---|
683 | const xmlChar *string;
|
---|
684 | xmlChar *value;
|
---|
685 |
|
---|
686 | i++;
|
---|
687 | params[nbparams++] = argv[i++];
|
---|
688 | string = (const xmlChar *) argv[i];
|
---|
689 | if (xmlStrchr(string, '"')) {
|
---|
690 | if (xmlStrchr(string, '\'')) {
|
---|
691 | fprintf(stderr,
|
---|
692 | "stringparam contains both quote and double-quotes !\n");
|
---|
693 | return(8);
|
---|
694 | }
|
---|
695 | value = xmlStrdup((const xmlChar *)"'");
|
---|
696 | value = xmlStrcat(value, string);
|
---|
697 | value = xmlStrcat(value, (const xmlChar *)"'");
|
---|
698 | } else {
|
---|
699 | value = xmlStrdup((const xmlChar *)"\"");
|
---|
700 | value = xmlStrcat(value, string);
|
---|
701 | value = xmlStrcat(value, (const xmlChar *)"\"");
|
---|
702 | }
|
---|
703 |
|
---|
704 | params[nbparams++] = (const char *) value;
|
---|
705 | strparams[nbstrparams++] = value;
|
---|
706 | if (nbparams >= MAX_PARAMETERS) {
|
---|
707 | fprintf(stderr, "too many params increase MAX_PARAMETERS \n");
|
---|
708 | return (2);
|
---|
709 | }
|
---|
710 | } else if ((!strcmp(argv[i], "-maxdepth")) ||
|
---|
711 | (!strcmp(argv[i], "--maxdepth"))) {
|
---|
712 | int value;
|
---|
713 |
|
---|
714 | i++;
|
---|
715 | if (sscanf(argv[i], "%d", &value) == 1) {
|
---|
716 | if (value > 0)
|
---|
717 | xsltMaxDepth = value;
|
---|
718 | }
|
---|
719 | } else if ((!strcmp(argv[i], "-maxparserdepth")) ||
|
---|
720 | (!strcmp(argv[i], "--maxparserdepth"))) {
|
---|
721 | int value;
|
---|
722 |
|
---|
723 | i++;
|
---|
724 | if (sscanf(argv[i], "%d", &value) == 1) {
|
---|
725 | if (value > 0)
|
---|
726 | xmlParserMaxDepth = value;
|
---|
727 | }
|
---|
728 | } else if ((!strcmp(argv[i],"-dumpextensions"))||
|
---|
729 | (!strcmp(argv[i],"--dumpextensions"))) {
|
---|
730 | dumpextensions++;
|
---|
731 |
|
---|
732 | } else {
|
---|
733 | fprintf(stderr, "Unknown option %s\n", argv[i]);
|
---|
734 | usage(argv[0]);
|
---|
735 | return (3);
|
---|
736 | }
|
---|
737 | }
|
---|
738 | params[nbparams] = NULL;
|
---|
739 |
|
---|
740 | if (novalid != 0)
|
---|
741 | options = XML_PARSE_NOENT | XML_PARSE_NOCDATA;
|
---|
742 | else if (nodtdattr)
|
---|
743 | options = XML_PARSE_NOENT | XML_PARSE_DTDLOAD | XML_PARSE_NOCDATA;
|
---|
744 | if (nodict != 0)
|
---|
745 | options |= XML_PARSE_NODICT;
|
---|
746 |
|
---|
747 | /*
|
---|
748 | * Register the EXSLT extensions and the test module
|
---|
749 | */
|
---|
750 | exsltRegisterAll();
|
---|
751 | xsltRegisterTestModule();
|
---|
752 |
|
---|
753 | if (dumpextensions)
|
---|
754 | xsltDebugDumpExtensions(NULL);
|
---|
755 |
|
---|
756 | for (i = 1; i < argc; i++) {
|
---|
757 | if ((!strcmp(argv[i], "-maxdepth")) ||
|
---|
758 | (!strcmp(argv[i], "--maxdepth"))) {
|
---|
759 | i++;
|
---|
760 | continue;
|
---|
761 | } else if ((!strcmp(argv[i], "-maxparserdepth")) ||
|
---|
762 | (!strcmp(argv[i], "--maxparserdepth"))) {
|
---|
763 | i++;
|
---|
764 | continue;
|
---|
765 | } else if ((!strcmp(argv[i], "-o")) ||
|
---|
766 | (!strcmp(argv[i], "-output")) ||
|
---|
767 | (!strcmp(argv[i], "--output"))) {
|
---|
768 | i++;
|
---|
769 | continue;
|
---|
770 | } else if ((!strcmp(argv[i], "-encoding")) ||
|
---|
771 | (!strcmp(argv[i], "--encoding"))) {
|
---|
772 | i++;
|
---|
773 | continue;
|
---|
774 | } else if ((!strcmp(argv[i], "-writesubtree")) ||
|
---|
775 | (!strcmp(argv[i], "--writesubtree"))) {
|
---|
776 | i++;
|
---|
777 | continue;
|
---|
778 | } else if ((!strcmp(argv[i], "-path")) ||
|
---|
779 | (!strcmp(argv[i], "--path"))) {
|
---|
780 | i++;
|
---|
781 | continue;
|
---|
782 | }
|
---|
783 | if ((!strcmp(argv[i], "-param")) || (!strcmp(argv[i], "--param"))) {
|
---|
784 | i += 2;
|
---|
785 | continue;
|
---|
786 | }
|
---|
787 | if ((!strcmp(argv[i], "-stringparam")) ||
|
---|
788 | (!strcmp(argv[i], "--stringparam"))) {
|
---|
789 | i += 2;
|
---|
790 | continue;
|
---|
791 | }
|
---|
792 | if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
|
---|
793 | if (timing)
|
---|
794 | startTimer();
|
---|
795 | style = xmlReadFile((const char *) argv[i], NULL, options);
|
---|
796 | if (timing)
|
---|
797 | endTimer("Parsing stylesheet %s", argv[i]);
|
---|
798 | if (xincludestyle) {
|
---|
799 | if (style != NULL) {
|
---|
800 | if (timing)
|
---|
801 | startTimer();
|
---|
802 | #if LIBXML_VERSION >= 20603
|
---|
803 | xmlXIncludeProcessFlags(style, XSLT_PARSE_OPTIONS);
|
---|
804 | #else
|
---|
805 | xmlXIncludeProcess(style);
|
---|
806 | #endif
|
---|
807 | if (timing) {
|
---|
808 | endTimer("XInclude processing %s", argv[i]);
|
---|
809 | }
|
---|
810 | }
|
---|
811 | }
|
---|
812 | if (style == NULL) {
|
---|
813 | fprintf(stderr, "cannot parse %s\n", argv[i]);
|
---|
814 | cur = NULL;
|
---|
815 | errorno = 4;
|
---|
816 | } else {
|
---|
817 | cur = xsltLoadStylesheetPI(style);
|
---|
818 | if (cur != NULL) {
|
---|
819 | /* it is an embedded stylesheet */
|
---|
820 | xsltProcess(style, cur, argv[i]);
|
---|
821 | xsltFreeStylesheet(cur);
|
---|
822 | cur = NULL;
|
---|
823 | goto done;
|
---|
824 | }
|
---|
825 | cur = xsltParseStylesheetDoc(style);
|
---|
826 | if (cur != NULL) {
|
---|
827 | if (cur->errors != 0) {
|
---|
828 | errorno = 5;
|
---|
829 | goto done;
|
---|
830 | }
|
---|
831 | i++;
|
---|
832 | } else {
|
---|
833 | xmlFreeDoc(style);
|
---|
834 | errorno = 5;
|
---|
835 | goto done;
|
---|
836 | }
|
---|
837 | }
|
---|
838 | break;
|
---|
839 |
|
---|
840 | }
|
---|
841 | }
|
---|
842 |
|
---|
843 |
|
---|
844 | if ((cur != NULL) && (cur->errors == 0)) {
|
---|
845 | for (; i < argc; i++) {
|
---|
846 | doc = NULL;
|
---|
847 | if (timing)
|
---|
848 | startTimer();
|
---|
849 | #ifdef LIBXML_HTML_ENABLED
|
---|
850 | if (html)
|
---|
851 | doc = htmlReadFile(argv[i], encoding, options);
|
---|
852 | else
|
---|
853 | #endif
|
---|
854 | doc = xmlReadFile(argv[i], encoding, options);
|
---|
855 | if (doc == NULL) {
|
---|
856 | fprintf(stderr, "unable to parse %s\n", argv[i]);
|
---|
857 | errorno = 6;
|
---|
858 | continue;
|
---|
859 | }
|
---|
860 | if (timing)
|
---|
861 | endTimer("Parsing document %s", argv[i]);
|
---|
862 | xsltProcess(doc, cur, argv[i]);
|
---|
863 | }
|
---|
864 | }
|
---|
865 | done:
|
---|
866 | if (cur != NULL)
|
---|
867 | xsltFreeStylesheet(cur);
|
---|
868 | for (i = 0;i < nbstrparams;i++)
|
---|
869 | xmlFree(strparams[i]);
|
---|
870 | if (output != NULL)
|
---|
871 | xmlFree(output);
|
---|
872 | xsltFreeSecurityPrefs(sec);
|
---|
873 | xsltCleanupGlobals();
|
---|
874 | xmlCleanupParser();
|
---|
875 | xmlMemoryDump();
|
---|
876 | return(errorno);
|
---|
877 | }
|
---|
878 |
|
---|