PageRenderTime 69ms CodeModel.GetById 41ms RepoModel.GetById 0ms app.codeStats 0ms

/xsltproc/xsltproc.c

https://bitbucket.org/vaporoid/libxslt
C | 904 lines | 811 code | 49 blank | 44 comment | 175 complexity | c0f0371097aa7f5408782c300e40a36c 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. int ret;
  329. if (timing)
  330. startTimer();
  331. #if LIBXML_VERSION >= 20603
  332. ret = xmlXIncludeProcessFlags(doc, XSLT_PARSE_OPTIONS);
  333. #else
  334. ret = xmlXIncludeProcess(doc);
  335. #endif
  336. if (timing) {
  337. endTimer("XInclude processing %s", filename);
  338. }
  339. if (ret < 0) {
  340. errorno = 6;
  341. return;
  342. }
  343. }
  344. #endif
  345. if (timing)
  346. startTimer();
  347. if (output == NULL) {
  348. if (repeat) {
  349. int j;
  350. for (j = 1; j < repeat; j++) {
  351. res = xsltApplyStylesheet(cur, doc, params);
  352. xmlFreeDoc(res);
  353. xmlFreeDoc(doc);
  354. #ifdef LIBXML_HTML_ENABLED
  355. if (html)
  356. doc = htmlReadFile(filename, encoding, options);
  357. else
  358. #endif
  359. doc = xmlReadFile(filename, encoding, options);
  360. }
  361. }
  362. ctxt = xsltNewTransformContext(cur, doc);
  363. if (ctxt == NULL)
  364. return;
  365. xsltSetCtxtParseOptions(ctxt, options);
  366. #ifdef LIBXML_XINCLUDE_ENABLED
  367. if (xinclude)
  368. ctxt->xinclude = 1;
  369. #endif
  370. if (profile) {
  371. res = xsltApplyStylesheetUser(cur, doc, params, NULL,
  372. stderr, ctxt);
  373. } else {
  374. res = xsltApplyStylesheetUser(cur, doc, params, NULL,
  375. NULL, ctxt);
  376. }
  377. if (ctxt->state == XSLT_STATE_ERROR)
  378. errorno = 9;
  379. else if (ctxt->state == XSLT_STATE_STOPPED)
  380. errorno = 10;
  381. xsltFreeTransformContext(ctxt);
  382. if (timing) {
  383. if (repeat)
  384. endTimer("Applying stylesheet %d times", repeat);
  385. else
  386. endTimer("Applying stylesheet");
  387. }
  388. xmlFreeDoc(doc);
  389. if (res == NULL) {
  390. fprintf(stderr, "no result for %s\n", filename);
  391. return;
  392. }
  393. if (noout) {
  394. xmlFreeDoc(res);
  395. return;
  396. }
  397. #ifdef LIBXML_DEBUG_ENABLED
  398. if (debug)
  399. xmlDebugDumpDocument(stdout, res);
  400. else {
  401. #endif
  402. if (cur->methodURI == NULL) {
  403. if (timing)
  404. startTimer();
  405. xsltSaveResultToFile(stdout, res, cur);
  406. if (timing)
  407. endTimer("Saving result");
  408. } else {
  409. if (xmlStrEqual
  410. (cur->method, (const xmlChar *) "xhtml")) {
  411. fprintf(stderr, "non standard output xhtml\n");
  412. if (timing)
  413. startTimer();
  414. xsltSaveResultToFile(stdout, res, cur);
  415. if (timing)
  416. endTimer("Saving result");
  417. } else {
  418. fprintf(stderr,
  419. "Unsupported non standard output %s\n",
  420. cur->method);
  421. errorno = 7;
  422. }
  423. }
  424. #ifdef LIBXML_DEBUG_ENABLED
  425. }
  426. #endif
  427. xmlFreeDoc(res);
  428. } else {
  429. int ret;
  430. ctxt = xsltNewTransformContext(cur, doc);
  431. if (ctxt == NULL)
  432. return;
  433. xsltSetCtxtParseOptions(ctxt, options);
  434. #ifdef LIBXML_XINCLUDE_ENABLED
  435. if (xinclude)
  436. ctxt->xinclude = 1;
  437. #endif
  438. ctxt->maxTemplateDepth = xsltMaxDepth;
  439. ctxt->maxTemplateVars = xsltMaxVars;
  440. if (profile) {
  441. ret = xsltRunStylesheetUser(cur, doc, params, output,
  442. NULL, NULL, stderr, ctxt);
  443. } else {
  444. ret = xsltRunStylesheetUser(cur, doc, params, output,
  445. NULL, NULL, NULL, ctxt);
  446. }
  447. if (ret == -1)
  448. errorno = 11;
  449. else if (ctxt->state == XSLT_STATE_ERROR)
  450. errorno = 9;
  451. else if (ctxt->state == XSLT_STATE_STOPPED)
  452. errorno = 10;
  453. xsltFreeTransformContext(ctxt);
  454. if (timing)
  455. endTimer("Running stylesheet and saving result");
  456. xmlFreeDoc(doc);
  457. }
  458. }
  459. static void usage(const char *name) {
  460. printf("Usage: %s [options] stylesheet file [file ...]\n", name);
  461. printf(" Options:\n");
  462. printf("\t--version or -V: show the version of libxml and libxslt used\n");
  463. printf("\t--verbose or -v: show logs of what's happening\n");
  464. printf("\t--output file or -o file: save to a given file\n");
  465. printf("\t--timing: display the time used\n");
  466. printf("\t--repeat: run the transformation 20 times\n");
  467. #ifdef LIBXML_DEBUG_ENABLED
  468. printf("\t--debug: dump the tree of the result instead\n");
  469. #endif
  470. printf("\t--dumpextensions: dump the registered extension elements and functions to stdout\n");
  471. printf("\t--novalid skip the DTD loading phase\n");
  472. printf("\t--nodtdattr do not default attributes from the DTD\n");
  473. printf("\t--noout: do not dump the result\n");
  474. printf("\t--maxdepth val : increase the maximum depth (default %d)\n", xsltMaxDepth);
  475. printf("\t--maxvars val : increase the maximum variables (default %d)\n", xsltMaxVars);
  476. printf("\t--maxparserdepth val : increase the maximum parser depth\n");
  477. #ifdef LIBXML_HTML_ENABLED
  478. printf("\t--html: the input document is(are) an HTML file(s)\n");
  479. #endif
  480. printf("\t--encoding: the input document character encoding\n");
  481. printf("\t--param name value : pass a (parameter,value) pair\n");
  482. printf("\t value is an UTF8 XPath expression.\n");
  483. printf("\t string values must be quoted like \"'string'\"\n or");
  484. printf("\t use stringparam to avoid it\n");
  485. printf("\t--stringparam name value : pass a (parameter, UTF8 string value) pair\n");
  486. printf("\t--path 'paths': provide a set of paths for resources\n");
  487. printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
  488. printf("\t--nowrite : refuse to write to any file or resource\n");
  489. printf("\t--nomkdir : refuse to create directories\n");
  490. printf("\t--writesubtree path : allow file write only with the path subtree\n");
  491. #ifdef LIBXML_CATALOG_ENABLED
  492. printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
  493. printf("\t otherwise XML Catalogs starting from \n");
  494. printf("\t file:///etc/xml/catalog are activated by default\n");
  495. #endif
  496. #ifdef LIBXML_XINCLUDE_ENABLED
  497. printf("\t--xinclude : do XInclude processing on document input\n");
  498. printf("\t--xincludestyle : do XInclude processing on stylesheets\n");
  499. #endif
  500. printf("\t--load-trace : print trace of all external entites loaded\n");
  501. printf("\t--profile or --norman : dump profiling informations \n");
  502. printf("\nProject libxslt home page: http://xmlsoft.org/XSLT/\n");
  503. printf("To report bugs and get help: http://xmlsoft.org/XSLT/bugs.html\n");
  504. }
  505. int
  506. main(int argc, char **argv)
  507. {
  508. int i;
  509. xsltStylesheetPtr cur = NULL;
  510. xmlDocPtr doc, style;
  511. xsltSecurityPrefsPtr sec = NULL;
  512. if (argc <= 1) {
  513. usage(argv[0]);
  514. return (1);
  515. }
  516. xmlInitMemory();
  517. LIBXML_TEST_VERSION
  518. sec = xsltNewSecurityPrefs();
  519. xsltSetDefaultSecurityPrefs(sec);
  520. defaultEntityLoader = xmlGetExternalEntityLoader();
  521. xmlSetExternalEntityLoader(xsltprocExternalEntityLoader);
  522. for (i = 1; i < argc; i++) {
  523. if (!strcmp(argv[i], "-"))
  524. break;
  525. if (argv[i][0] != '-')
  526. continue;
  527. #ifdef LIBXML_DEBUG_ENABLED
  528. if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) {
  529. debug++;
  530. } else
  531. #endif
  532. if ((!strcmp(argv[i], "-v")) ||
  533. (!strcmp(argv[i], "-verbose")) ||
  534. (!strcmp(argv[i], "--verbose"))) {
  535. xsltSetGenericDebugFunc(stderr, NULL);
  536. } else if ((!strcmp(argv[i], "-o")) ||
  537. (!strcmp(argv[i], "-output")) ||
  538. (!strcmp(argv[i], "--output"))) {
  539. i++;
  540. #if defined(WIN32) || defined (__CYGWIN__)
  541. output = xmlCanonicPath(argv[i]);
  542. if (output == NULL)
  543. #endif
  544. output = (char *) xmlStrdup((xmlChar *) argv[i]);
  545. } else if ((!strcmp(argv[i], "-V")) ||
  546. (!strcmp(argv[i], "-version")) ||
  547. (!strcmp(argv[i], "--version"))) {
  548. printf("Using libxml %s, libxslt %s and libexslt %s\n",
  549. xmlParserVersion, xsltEngineVersion, exsltLibraryVersion);
  550. printf
  551. ("xsltproc was compiled against libxml %d, libxslt %d and libexslt %d\n",
  552. LIBXML_VERSION, LIBXSLT_VERSION, LIBEXSLT_VERSION);
  553. printf("libxslt %d was compiled against libxml %d\n",
  554. xsltLibxsltVersion, xsltLibxmlVersion);
  555. printf("libexslt %d was compiled against libxml %d\n",
  556. exsltLibexsltVersion, exsltLibxmlVersion);
  557. } else if ((!strcmp(argv[i], "-repeat"))
  558. || (!strcmp(argv[i], "--repeat"))) {
  559. if (repeat == 0)
  560. repeat = 20;
  561. else
  562. repeat = 100;
  563. } else if ((!strcmp(argv[i], "-novalid")) ||
  564. (!strcmp(argv[i], "--novalid"))) {
  565. novalid++;
  566. } else if ((!strcmp(argv[i], "-nodtdattr")) ||
  567. (!strcmp(argv[i], "--nodtdattr"))) {
  568. nodtdattr++;
  569. } else if ((!strcmp(argv[i], "-noout")) ||
  570. (!strcmp(argv[i], "--noout"))) {
  571. noout++;
  572. #ifdef LIBXML_HTML_ENABLED
  573. } else if ((!strcmp(argv[i], "-html")) ||
  574. (!strcmp(argv[i], "--html"))) {
  575. html++;
  576. #endif
  577. } else if ((!strcmp(argv[i], "-encoding")) ||
  578. (!strcmp(argv[i], "--encoding"))) {
  579. encoding = argv[++i];
  580. } else if ((!strcmp(argv[i], "-timing")) ||
  581. (!strcmp(argv[i], "--timing"))) {
  582. timing++;
  583. } else if ((!strcmp(argv[i], "-profile")) ||
  584. (!strcmp(argv[i], "--profile"))) {
  585. profile++;
  586. } else if ((!strcmp(argv[i], "-nodict")) ||
  587. (!strcmp(argv[i], "--nodict"))) {
  588. nodict++;
  589. } else if ((!strcmp(argv[i], "-norman")) ||
  590. (!strcmp(argv[i], "--norman"))) {
  591. profile++;
  592. } else if ((!strcmp(argv[i], "-nonet")) ||
  593. (!strcmp(argv[i], "--nonet"))) {
  594. defaultEntityLoader = xmlNoNetExternalEntityLoader;
  595. } else if ((!strcmp(argv[i], "-nowrite")) ||
  596. (!strcmp(argv[i], "--nowrite"))) {
  597. xsltSetSecurityPrefs(sec, XSLT_SECPREF_WRITE_FILE,
  598. xsltSecurityForbid);
  599. xsltSetSecurityPrefs(sec, XSLT_SECPREF_CREATE_DIRECTORY,
  600. xsltSecurityForbid);
  601. xsltSetSecurityPrefs(sec, XSLT_SECPREF_WRITE_NETWORK,
  602. xsltSecurityForbid);
  603. } else if ((!strcmp(argv[i], "-nomkdir")) ||
  604. (!strcmp(argv[i], "--nomkdir"))) {
  605. xsltSetSecurityPrefs(sec, XSLT_SECPREF_CREATE_DIRECTORY,
  606. xsltSecurityForbid);
  607. } else if ((!strcmp(argv[i], "-writesubtree")) ||
  608. (!strcmp(argv[i], "--writesubtree"))) {
  609. i++;
  610. writesubtree = argv[i];
  611. xsltSetSecurityPrefs(sec, XSLT_SECPREF_WRITE_FILE,
  612. xsltSubtreeCheck);
  613. } else if ((!strcmp(argv[i], "-path")) ||
  614. (!strcmp(argv[i], "--path"))) {
  615. i++;
  616. parsePath(BAD_CAST argv[i]);
  617. #ifdef LIBXML_CATALOG_ENABLED
  618. } else if ((!strcmp(argv[i], "-catalogs")) ||
  619. (!strcmp(argv[i], "--catalogs"))) {
  620. const char *catalogs;
  621. catalogs = getenv("SGML_CATALOG_FILES");
  622. if (catalogs == NULL) {
  623. fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
  624. } else {
  625. xmlLoadCatalogs(catalogs);
  626. }
  627. #endif
  628. #ifdef LIBXML_XINCLUDE_ENABLED
  629. } else if ((!strcmp(argv[i], "-xinclude")) ||
  630. (!strcmp(argv[i], "--xinclude"))) {
  631. xinclude++;
  632. } else if ((!strcmp(argv[i], "-xincludestyle")) ||
  633. (!strcmp(argv[i], "--xincludestyle"))) {
  634. xincludestyle++;
  635. xsltSetXIncludeDefault(1);
  636. #endif
  637. } else if ((!strcmp(argv[i], "-load-trace")) ||
  638. (!strcmp(argv[i], "--load-trace"))) {
  639. load_trace++;
  640. } else if ((!strcmp(argv[i], "-param")) ||
  641. (!strcmp(argv[i], "--param"))) {
  642. i++;
  643. params[nbparams++] = argv[i++];
  644. params[nbparams++] = argv[i];
  645. if (nbparams >= MAX_PARAMETERS) {
  646. fprintf(stderr, "too many params increase MAX_PARAMETERS \n");
  647. return (2);
  648. }
  649. } else if ((!strcmp(argv[i], "-stringparam")) ||
  650. (!strcmp(argv[i], "--stringparam"))) {
  651. const xmlChar *string;
  652. xmlChar *value;
  653. i++;
  654. params[nbparams++] = argv[i++];
  655. string = (const xmlChar *) argv[i];
  656. if (xmlStrchr(string, '"')) {
  657. if (xmlStrchr(string, '\'')) {
  658. fprintf(stderr,
  659. "stringparam contains both quote and double-quotes !\n");
  660. return(8);
  661. }
  662. value = xmlStrdup((const xmlChar *)"'");
  663. value = xmlStrcat(value, string);
  664. value = xmlStrcat(value, (const xmlChar *)"'");
  665. } else {
  666. value = xmlStrdup((const xmlChar *)"\"");
  667. value = xmlStrcat(value, string);
  668. value = xmlStrcat(value, (const xmlChar *)"\"");
  669. }
  670. params[nbparams++] = (const char *) value;
  671. strparams[nbstrparams++] = value;
  672. if (nbparams >= MAX_PARAMETERS) {
  673. fprintf(stderr, "too many params increase MAX_PARAMETERS \n");
  674. return (2);
  675. }
  676. } else if ((!strcmp(argv[i], "-maxdepth")) ||
  677. (!strcmp(argv[i], "--maxdepth"))) {
  678. int value;
  679. i++;
  680. if (sscanf(argv[i], "%d", &value) == 1) {
  681. if (value > 0)
  682. xsltMaxDepth = value;
  683. }
  684. } else if ((!strcmp(argv[i], "-maxvars")) ||
  685. (!strcmp(argv[i], "--maxvars"))) {
  686. int value;
  687. i++;
  688. if (sscanf(argv[i], "%d", &value) == 1) {
  689. if (value > 0)
  690. xsltMaxVars = value;
  691. }
  692. } else if ((!strcmp(argv[i], "-maxparserdepth")) ||
  693. (!strcmp(argv[i], "--maxparserdepth"))) {
  694. int value;
  695. i++;
  696. if (sscanf(argv[i], "%d", &value) == 1) {
  697. if (value > 0)
  698. xmlParserMaxDepth = value;
  699. }
  700. } else if ((!strcmp(argv[i],"-dumpextensions"))||
  701. (!strcmp(argv[i],"--dumpextensions"))) {
  702. dumpextensions++;
  703. } else {
  704. fprintf(stderr, "Unknown option %s\n", argv[i]);
  705. usage(argv[0]);
  706. return (3);
  707. }
  708. }
  709. params[nbparams] = NULL;
  710. if (novalid != 0)
  711. options = XML_PARSE_NOENT | XML_PARSE_NOCDATA;
  712. else if (nodtdattr)
  713. options = XML_PARSE_NOENT | XML_PARSE_DTDLOAD | XML_PARSE_NOCDATA;
  714. if (nodict != 0)
  715. options |= XML_PARSE_NODICT;
  716. /*
  717. * Register the EXSLT extensions and the test module
  718. */
  719. exsltRegisterAll();
  720. xsltRegisterTestModule();
  721. if (dumpextensions)
  722. xsltDebugDumpExtensions(NULL);
  723. for (i = 1; i < argc; i++) {
  724. if ((!strcmp(argv[i], "-maxdepth")) ||
  725. (!strcmp(argv[i], "--maxdepth"))) {
  726. i++;
  727. continue;
  728. } else if ((!strcmp(argv[i], "-maxparserdepth")) ||
  729. (!strcmp(argv[i], "--maxparserdepth"))) {
  730. i++;
  731. continue;
  732. } else if ((!strcmp(argv[i], "-o")) ||
  733. (!strcmp(argv[i], "-output")) ||
  734. (!strcmp(argv[i], "--output"))) {
  735. i++;
  736. continue;
  737. } else if ((!strcmp(argv[i], "-encoding")) ||
  738. (!strcmp(argv[i], "--encoding"))) {
  739. i++;
  740. continue;
  741. } else if ((!strcmp(argv[i], "-writesubtree")) ||
  742. (!strcmp(argv[i], "--writesubtree"))) {
  743. i++;
  744. continue;
  745. } else if ((!strcmp(argv[i], "-path")) ||
  746. (!strcmp(argv[i], "--path"))) {
  747. i++;
  748. continue;
  749. }
  750. if ((!strcmp(argv[i], "-param")) || (!strcmp(argv[i], "--param"))) {
  751. i += 2;
  752. continue;
  753. }
  754. if ((!strcmp(argv[i], "-stringparam")) ||
  755. (!strcmp(argv[i], "--stringparam"))) {
  756. i += 2;
  757. continue;
  758. }
  759. if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
  760. if (timing)
  761. startTimer();
  762. style = xmlReadFile((const char *) argv[i], NULL, options);
  763. if (timing)
  764. endTimer("Parsing stylesheet %s", argv[i]);
  765. #ifdef LIBXML_XINCLUDE_ENABLED
  766. if (xincludestyle) {
  767. if (style != NULL) {
  768. if (timing)
  769. startTimer();
  770. #if LIBXML_VERSION >= 20603
  771. xmlXIncludeProcessFlags(style, XSLT_PARSE_OPTIONS);
  772. #else
  773. xmlXIncludeProcess(style);
  774. #endif
  775. if (timing) {
  776. endTimer("XInclude processing %s", argv[i]);
  777. }
  778. }
  779. }
  780. #endif
  781. if (style == NULL) {
  782. fprintf(stderr, "cannot parse %s\n", argv[i]);
  783. cur = NULL;
  784. errorno = 4;
  785. } else {
  786. cur = xsltLoadStylesheetPI(style);
  787. if (cur != NULL) {
  788. /* it is an embedded stylesheet */
  789. xsltProcess(style, cur, argv[i]);
  790. xsltFreeStylesheet(cur);
  791. cur = NULL;
  792. goto done;
  793. }
  794. cur = xsltParseStylesheetDoc(style);
  795. if (cur != NULL) {
  796. if (cur->errors != 0) {
  797. errorno = 5;
  798. goto done;
  799. }
  800. i++;
  801. } else {
  802. xmlFreeDoc(style);
  803. errorno = 5;
  804. goto done;
  805. }
  806. }
  807. break;
  808. }
  809. }
  810. if ((cur != NULL) && (cur->errors == 0)) {
  811. for (; i < argc; i++) {
  812. doc = NULL;
  813. if (timing)
  814. startTimer();
  815. #ifdef LIBXML_HTML_ENABLED
  816. if (html)
  817. doc = htmlReadFile(argv[i], encoding, options);
  818. else
  819. #endif
  820. doc = xmlReadFile(argv[i], encoding, options);
  821. if (doc == NULL) {
  822. fprintf(stderr, "unable to parse %s\n", argv[i]);
  823. errorno = 6;
  824. continue;
  825. }
  826. if (timing)
  827. endTimer("Parsing document %s", argv[i]);
  828. xsltProcess(doc, cur, argv[i]);
  829. }
  830. }
  831. done:
  832. if (cur != NULL)
  833. xsltFreeStylesheet(cur);
  834. for (i = 0;i < nbstrparams;i++)
  835. xmlFree(strparams[i]);
  836. if (output != NULL)
  837. xmlFree(output);
  838. xsltFreeSecurityPrefs(sec);
  839. xsltCleanupGlobals();
  840. xmlCleanupParser();
  841. xmlMemoryDump();
  842. return(errorno);
  843. }