2 * xsltproc.c: user program for the XSL Transformation 1.0 engine
4 * See Copyright for the status of this software.
9 #include "libxslt/libxslt.h"
10 #include "libexslt/exslt.h"
15 #ifdef HAVE_SYS_TIME_H
21 #ifdef HAVE_SYS_STAT_H
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>
40 #ifdef LIBXML_CATALOG_ENABLED
41 #include <libxml/catalog.h>
43 #include <libxml/parser.h>
44 #include <libxml/parserInternals.h>
45 #include <libxml/uri.h>
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>
54 #include <libexslt/exsltconfig.h>
56 #if defined(WIN32) && !defined (__CYGWIN__)
57 #if defined(_MSC_VER) || defined(__MINGW32__)
59 #define gettimeofday(p1,p2)
60 #define snprintf _snprintf
63 #if defined(HAVE_SYS_TIME_H)
65 #elif defined(HAVE_TIME_H)
70 #ifdef HAVE_SYS_TIMEB_H
71 #include <sys/timeb.h>
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;
81 static int nodict = 0;
82 #ifdef LIBXML_HTML_ENABLED
85 static int load_trace = 0;
86 #ifdef LIBXML_XINCLUDE_ENABLED
87 static int xinclude = 0;
89 static int profile = 0;
91 #define MAX_PARAMETERS 64
94 static int options = XSLT_PARSE_OPTIONS;
95 static const char *params[MAX_PARAMETERS + 1];
96 static int nbparams = 0;
97 static xmlChar *strparams[MAX_PARAMETERS + 1];
98 static int nbstrparams = 0;
99 static xmlChar *paths[MAX_PATHS + 1];
100 static int nbpaths = 0;
101 static char *output = NULL;
102 static int errorno = 0;
103 static const char *writesubtree = NULL;
106 * Entity loading control and customization.
109 void parsePath(const xmlChar *path) {
115 if (nbpaths >= MAX_PATHS) {
116 fprintf(stderr, "MAX_PATHS reached: too many paths\n");
120 while ((*cur == ' ') || (*cur == ':'))
123 while ((*cur != 0) && (*cur != ' ') && (*cur != ':'))
126 paths[nbpaths] = xmlStrndup(path, cur - path);
127 if (paths[nbpaths] != NULL)
134 xmlExternalEntityLoader defaultEntityLoader = NULL;
136 static xmlParserInputPtr
137 xsltprocExternalEntityLoader(const char *URL, const char *ID,
138 xmlParserCtxtPtr ctxt) {
139 xmlParserInputPtr ret;
140 warningSAXFunc warning = NULL;
143 const char *lastsegment = URL;
144 const char *iter = URL;
149 lastsegment = iter + 1;
154 if ((ctxt != NULL) && (ctxt->sax != NULL)) {
155 warning = ctxt->sax->warning;
156 ctxt->sax->warning = NULL;
159 if (defaultEntityLoader != NULL) {
160 ret = defaultEntityLoader(URL, ID, ctxt);
163 ctxt->sax->warning = warning;
167 "Loaded URL=\"%s\" ID=\"%s\"\n",
168 URL ? URL : "(null)",
174 for (i = 0;i < nbpaths;i++) {
177 newURL = xmlStrdup((const xmlChar *) paths[i]);
178 newURL = xmlStrcat(newURL, (const xmlChar *) "/");
179 newURL = xmlStrcat(newURL, (const xmlChar *) lastsegment);
180 if (newURL != NULL) {
181 ret = defaultEntityLoader((const char *)newURL, ID, ctxt);
184 ctxt->sax->warning = warning;
188 "Loaded URL=\"%s\" ID=\"%s\"\n",
198 if (warning != NULL) {
199 ctxt->sax->warning = warning;
201 warning(ctxt, "failed to load external entity \"%s\"\n", URL);
203 warning(ctxt, "failed to load external entity \"%s\"\n", ID);
209 * Internal timing routines to remove the necessity to have unix-specific
212 #ifndef HAVE_GETTIMEOFDAY
213 #ifdef HAVE_SYS_TIMEB_H
214 #ifdef HAVE_SYS_TIME_H
218 my_gettimeofday(struct timeval *tvp, void *tzp)
220 struct timeb timebuffer;
224 tvp->tv_sec = timebuffer.time;
225 tvp->tv_usec = timebuffer.millitm * 1000L;
229 #define HAVE_GETTIMEOFDAY 1
230 #define gettimeofday my_gettimeofday
232 #endif /* HAVE_FTIME */
233 #endif /* HAVE_SYS_TIME_H */
234 #endif /* HAVE_SYS_TIMEB_H */
235 #endif /* !HAVE_GETTIMEOFDAY */
237 #if defined(HAVE_GETTIMEOFDAY)
238 static struct timeval begin, endtime;
240 * startTimer: call where you want to start timing
242 static void startTimer(void)
244 gettimeofday(&begin,NULL);
247 * endTimer: call where you want to stop timing and to print out a
248 * message about the timing performed; format is a printf
251 static void endTimer(const char *format, ...)
256 gettimeofday(&endtime, NULL);
257 msec = endtime.tv_sec - begin.tv_sec;
259 msec += (endtime.tv_usec - begin.tv_usec) / 1000;
261 #ifndef HAVE_STDARG_H
262 #error "endTimer required stdarg functions"
264 va_start(ap, format);
265 vfprintf(stderr,format,ap);
268 fprintf(stderr, " took %ld ms\n", msec);
270 #elif defined(HAVE_TIME_H)
272 * No gettimeofday function, so we have to make do with calling clock.
273 * This is obviously less accurate, but there's little we can do about
276 #ifndef CLOCKS_PER_SEC
277 #define CLOCKS_PER_SEC 100
280 clock_t begin, endtime;
281 static void startTimer(void)
285 static void endTimer(char *format, ...)
291 msec = ((endtime-begin) * 1000) / CLOCKS_PER_SEC;
293 #ifndef HAVE_STDARG_H
294 #error "endTimer required stdarg functions"
296 va_start(ap, format);
297 vfprintf(stderr,format,ap);
299 fprintf(stderr, " took %ld ms\n", msec);
303 * We don't have a gettimeofday or time.h, so we just don't do timing
305 static void startTimer(void)
311 static void endTimer(char *format, ...)
314 * We cannot do anything because we don't have a timing function
317 va_start(ap, format);
318 vfprintf(stderr,format,ap);
320 fprintf(stderr, " was not timed\n", msec);
322 /* We don't have gettimeofday, time or stdarg.h, what crazy world is
332 * allow writes only on a subtree specified on the command line
335 xsltSubtreeCheck(xsltSecurityPrefsPtr sec ATTRIBUTE_UNUSED,
336 xsltTransformContextPtr ctxt ATTRIBUTE_UNUSED,
337 const char *value ATTRIBUTE_UNUSED) {
340 if (writesubtree == NULL)
345 len = xmlStrlen(BAD_CAST writesubtree);
346 ret = xmlStrncmp(BAD_CAST writesubtree, BAD_CAST value, len);
353 xsltProcess(xmlDocPtr doc, xsltStylesheetPtr cur, const char *filename) {
355 xsltTransformContextPtr ctxt;
358 #ifdef LIBXML_XINCLUDE_ENABLED
362 #if LIBXML_VERSION >= 20603
363 xmlXIncludeProcessFlags(doc, XSLT_PARSE_OPTIONS);
365 xmlXIncludeProcess(doc);
368 endTimer("XInclude processing %s", filename);
374 if (output == NULL) {
378 for (j = 1; j < repeat; j++) {
379 res = xsltApplyStylesheet(cur, doc, params);
382 #ifdef LIBXML_HTML_ENABLED
384 doc = htmlReadFile(filename, NULL, options);
387 doc = xmlReadFile(filename, NULL, options);
390 ctxt = xsltNewTransformContext(cur, doc);
393 xsltSetCtxtParseOptions(ctxt, options);
397 res = xsltApplyStylesheetUser(cur, doc, params, NULL,
400 res = xsltApplyStylesheetUser(cur, doc, params, NULL,
403 if (ctxt->state == XSLT_STATE_ERROR)
405 else if (ctxt->state == XSLT_STATE_STOPPED)
407 xsltFreeTransformContext(ctxt);
410 endTimer("Applying stylesheet %d times", repeat);
412 endTimer("Applying stylesheet");
416 fprintf(stderr, "no result for %s\n", filename);
423 #ifdef LIBXML_DEBUG_ENABLED
425 xmlDebugDumpDocument(stdout, res);
428 if (cur->methodURI == NULL) {
431 xsltSaveResultToFile(stdout, res, cur);
433 endTimer("Saving result");
436 (cur->method, (const xmlChar *) "xhtml")) {
437 fprintf(stderr, "non standard output xhtml\n");
440 xsltSaveResultToFile(stdout, res, cur);
442 endTimer("Saving result");
445 "Unsupported non standard output %s\n",
450 #ifdef LIBXML_DEBUG_ENABLED
457 ctxt = xsltNewTransformContext(cur, doc);
461 xsltRunStylesheetUser(cur, doc, params, output,
462 NULL, NULL, stderr, ctxt);
464 xsltRunStylesheetUser(cur, doc, params, output,
465 NULL, NULL, NULL, ctxt);
467 if (ctxt->state == XSLT_STATE_ERROR)
469 else if (ctxt->state == XSLT_STATE_STOPPED)
471 xsltFreeTransformContext(ctxt);
473 endTimer("Running stylesheet and saving result");
478 static void usage(const char *name) {
479 printf("Usage: %s [options] stylesheet file [file ...]\n", name);
480 printf(" Options:\n");
481 printf("\t--version or -V: show the version of libxml and libxslt used\n");
482 printf("\t--verbose or -v: show logs of what's happening\n");
483 printf("\t--output file or -o file: save to a given file\n");
484 printf("\t--timing: display the time used\n");
485 printf("\t--repeat: run the transformation 20 times\n");
486 #ifdef LIBXML_DEBUG_ENABLED
487 printf("\t--debug: dump the tree of the result instead\n");
489 printf("\t--dumpextensions: dump the registered extension elements and functions to stdout\n");
490 printf("\t--novalid skip the Dtd loading phase\n");
491 printf("\t--nodtdattr do not default attributes from the DTD\n");
492 printf("\t--noout: do not dump the result\n");
493 printf("\t--maxdepth val : increase the maximum depth\n");
494 printf("\t--maxparserdepth val : increase the maximum parser depth\n");
495 #ifdef LIBXML_HTML_ENABLED
496 printf("\t--html: the input document is(are) an HTML file(s)\n");
498 printf("\t--param name value : pass a (parameter,value) pair\n");
499 printf("\t value is an UTF8 XPath expression.\n");
500 printf("\t string values must be quoted like \"'string'\"\n or");
501 printf("\t use stringparam to avoid it\n");
502 printf("\t--stringparam name value : pass a (parameter, UTF8 string value) pair\n");
503 printf("\t--path 'paths': provide a set of paths for resources\n");
504 printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
505 printf("\t--nowrite : refuse to write to any file or resource\n");
506 printf("\t--nomkdir : refuse to create directories\n");
507 printf("\t--writesubtree path : allow file write only with the path subtree\n");
508 #ifdef LIBXML_CATALOG_ENABLED
509 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
510 printf("\t otherwise XML Catalogs starting from \n");
511 printf("\t file:///etc/xml/catalog are activated by default\n");
513 #ifdef LIBXML_XINCLUDE_ENABLED
514 printf("\t--xinclude : do XInclude processing on document intput\n");
516 printf("\t--load-trace : print trace of all external entites loaded\n");
517 printf("\t--profile or --norman : dump profiling informations \n");
518 printf("\nProject libxslt home page: http://xmlsoft.org/XSLT/\n");
519 printf("To report bugs and get help: http://xmlsoft.org/XSLT/bugs.html\n");
523 main(int argc, char **argv)
526 xsltStylesheetPtr cur = NULL;
527 xmlDocPtr doc, style;
528 xsltSecurityPrefsPtr sec = NULL;
539 sec = xsltNewSecurityPrefs();
540 xsltSetDefaultSecurityPrefs(sec);
541 defaultEntityLoader = xmlGetExternalEntityLoader();
542 xmlSetExternalEntityLoader(xsltprocExternalEntityLoader);
544 for (i = 1; i < argc; i++) {
545 if (!strcmp(argv[i], "-"))
548 if (argv[i][0] != '-')
550 #ifdef LIBXML_DEBUG_ENABLED
551 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) {
555 if ((!strcmp(argv[i], "-v")) ||
556 (!strcmp(argv[i], "-verbose")) ||
557 (!strcmp(argv[i], "--verbose"))) {
558 xsltSetGenericDebugFunc(stderr, NULL);
559 } else if ((!strcmp(argv[i], "-o")) ||
560 (!strcmp(argv[i], "-output")) ||
561 (!strcmp(argv[i], "--output"))) {
563 #if defined(WIN32) || defined (__CYGWIN__)
564 output = xmlCanonicPath(argv[i]);
567 output = (char *) xmlStrdup((xmlChar *) argv[i]);
568 } else if ((!strcmp(argv[i], "-V")) ||
569 (!strcmp(argv[i], "-version")) ||
570 (!strcmp(argv[i], "--version"))) {
571 printf("Using libxml %s, libxslt %s and libexslt %s\n",
572 xmlParserVersion, xsltEngineVersion, exsltLibraryVersion);
574 ("xsltproc was compiled against libxml %d, libxslt %d and libexslt %d\n",
575 LIBXML_VERSION, LIBXSLT_VERSION, LIBEXSLT_VERSION);
576 printf("libxslt %d was compiled against libxml %d\n",
577 xsltLibxsltVersion, xsltLibxmlVersion);
578 printf("libexslt %d was compiled against libxml %d\n",
579 exsltLibexsltVersion, exsltLibxmlVersion);
580 } else if ((!strcmp(argv[i], "-repeat"))
581 || (!strcmp(argv[i], "--repeat"))) {
586 } else if ((!strcmp(argv[i], "-novalid")) ||
587 (!strcmp(argv[i], "--novalid"))) {
589 } else if ((!strcmp(argv[i], "-nodtdattr")) ||
590 (!strcmp(argv[i], "--nodtdattr"))) {
592 } else if ((!strcmp(argv[i], "-noout")) ||
593 (!strcmp(argv[i], "--noout"))) {
595 #ifdef LIBXML_HTML_ENABLED
596 } else if ((!strcmp(argv[i], "-html")) ||
597 (!strcmp(argv[i], "--html"))) {
600 } else if ((!strcmp(argv[i], "-timing")) ||
601 (!strcmp(argv[i], "--timing"))) {
603 } else if ((!strcmp(argv[i], "-profile")) ||
604 (!strcmp(argv[i], "--profile"))) {
606 } else if ((!strcmp(argv[i], "-nodict")) ||
607 (!strcmp(argv[i], "--nodict"))) {
609 } else if ((!strcmp(argv[i], "-norman")) ||
610 (!strcmp(argv[i], "--norman"))) {
612 } else if ((!strcmp(argv[i], "-nonet")) ||
613 (!strcmp(argv[i], "--nonet"))) {
614 defaultEntityLoader = xmlNoNetExternalEntityLoader;
615 } else if ((!strcmp(argv[i], "-nowrite")) ||
616 (!strcmp(argv[i], "--nowrite"))) {
617 xsltSetSecurityPrefs(sec, XSLT_SECPREF_WRITE_FILE,
619 xsltSetSecurityPrefs(sec, XSLT_SECPREF_CREATE_DIRECTORY,
621 xsltSetSecurityPrefs(sec, XSLT_SECPREF_WRITE_NETWORK,
623 } else if ((!strcmp(argv[i], "-nomkdir")) ||
624 (!strcmp(argv[i], "--nomkdir"))) {
625 xsltSetSecurityPrefs(sec, XSLT_SECPREF_CREATE_DIRECTORY,
627 } else if ((!strcmp(argv[i], "-writesubtree")) ||
628 (!strcmp(argv[i], "--writesubtree"))) {
630 writesubtree = argv[i];
631 xsltSetSecurityPrefs(sec, XSLT_SECPREF_WRITE_FILE,
633 } else if ((!strcmp(argv[i], "-path")) ||
634 (!strcmp(argv[i], "--path"))) {
636 parsePath(BAD_CAST argv[i]);
637 #ifdef LIBXML_CATALOG_ENABLED
638 } else if ((!strcmp(argv[i], "-catalogs")) ||
639 (!strcmp(argv[i], "--catalogs"))) {
640 const char *catalogs;
642 catalogs = getenv("SGML_CATALOG_FILES");
643 if (catalogs == NULL) {
644 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
646 xmlLoadCatalogs(catalogs);
649 #ifdef LIBXML_XINCLUDE_ENABLED
650 } else if ((!strcmp(argv[i], "-xinclude")) ||
651 (!strcmp(argv[i], "--xinclude"))) {
653 xsltSetXIncludeDefault(1);
655 } else if ((!strcmp(argv[i], "-load-trace")) ||
656 (!strcmp(argv[i], "--load-trace"))) {
658 } else if ((!strcmp(argv[i], "-param")) ||
659 (!strcmp(argv[i], "--param"))) {
661 params[nbparams++] = argv[i++];
662 params[nbparams++] = argv[i];
663 if (nbparams >= MAX_PARAMETERS) {
664 fprintf(stderr, "too many params increase MAX_PARAMETERS \n");
667 } else if ((!strcmp(argv[i], "-stringparam")) ||
668 (!strcmp(argv[i], "--stringparam"))) {
669 const xmlChar *string;
673 params[nbparams++] = argv[i++];
674 string = (const xmlChar *) argv[i];
675 if (xmlStrchr(string, '"')) {
676 if (xmlStrchr(string, '\'')) {
678 "stringparam contains both quote and double-quotes !\n");
681 value = xmlStrdup((const xmlChar *)"'");
682 value = xmlStrcat(value, string);
683 value = xmlStrcat(value, (const xmlChar *)"'");
685 value = xmlStrdup((const xmlChar *)"\"");
686 value = xmlStrcat(value, string);
687 value = xmlStrcat(value, (const xmlChar *)"\"");
690 params[nbparams++] = (const char *) value;
691 strparams[nbstrparams++] = value;
692 if (nbparams >= MAX_PARAMETERS) {
693 fprintf(stderr, "too many params increase MAX_PARAMETERS \n");
696 } else if ((!strcmp(argv[i], "-maxdepth")) ||
697 (!strcmp(argv[i], "--maxdepth"))) {
701 if (sscanf(argv[i], "%d", &value) == 1) {
703 xsltMaxDepth = value;
705 } else if ((!strcmp(argv[i], "-maxparserdepth")) ||
706 (!strcmp(argv[i], "--maxparserdepth"))) {
710 if (sscanf(argv[i], "%d", &value) == 1) {
712 xmlParserMaxDepth = value;
714 } else if ((!strcmp(argv[i],"-dumpextensions"))||
715 (!strcmp(argv[i],"--dumpextensions"))) {
719 fprintf(stderr, "Unknown option %s\n", argv[i]);
724 params[nbparams] = NULL;
727 options = XML_PARSE_NOENT | XML_PARSE_NOCDATA;
729 options = XML_PARSE_NOENT | XML_PARSE_DTDLOAD | XML_PARSE_NOCDATA;
731 options |= XML_PARSE_NODICT;
734 * Register the EXSLT extensions and the test module
737 xsltRegisterTestModule();
740 xsltDebugDumpExtensions(NULL);
742 for (i = 1; i < argc; i++) {
743 if ((!strcmp(argv[i], "-maxdepth")) ||
744 (!strcmp(argv[i], "--maxdepth"))) {
747 } else if ((!strcmp(argv[i], "-maxparserdepth")) ||
748 (!strcmp(argv[i], "--maxparserdepth"))) {
751 } else if ((!strcmp(argv[i], "-o")) ||
752 (!strcmp(argv[i], "-output")) ||
753 (!strcmp(argv[i], "--output"))) {
756 } else if ((!strcmp(argv[i], "-writesubtree")) ||
757 (!strcmp(argv[i], "--writesubtree"))) {
760 } else if ((!strcmp(argv[i], "-path")) ||
761 (!strcmp(argv[i], "--path"))) {
765 if ((!strcmp(argv[i], "-param")) || (!strcmp(argv[i], "--param"))) {
769 if ((!strcmp(argv[i], "-stringparam")) ||
770 (!strcmp(argv[i], "--stringparam"))) {
774 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
777 style = xmlReadFile((const char *) argv[i], NULL, options);
779 endTimer("Parsing stylesheet %s", argv[i]);
781 fprintf(stderr, "cannot parse %s\n", argv[i]);
785 cur = xsltLoadStylesheetPI(style);
787 /* it is an embedded stylesheet */
788 xsltProcess(style, cur, argv[i]);
789 xsltFreeStylesheet(cur);
793 cur = xsltParseStylesheetDoc(style);
795 if (cur->errors != 0) {
812 if ((cur != NULL) && (cur->errors == 0)) {
813 for (; i < argc; i++) {
817 #ifdef LIBXML_HTML_ENABLED
819 doc = htmlReadFile(argv[i], NULL, options);
822 doc = xmlReadFile(argv[i], NULL, options);
824 fprintf(stderr, "unable to parse %s\n", argv[i]);
829 endTimer("Parsing document %s", argv[i]);
830 xsltProcess(doc, cur, argv[i]);
835 xsltFreeStylesheet(cur);
836 for (i = 0;i < nbstrparams;i++)
837 xmlFree(strparams[i]);
840 xsltFreeSecurityPrefs(sec);
841 xsltCleanupGlobals();