PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/libxslt-1.1.26/xsltproc/xsltproc.c

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