xmllint.c 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598
  1. /*
  2. * xmllint.c : a small tester program for XML input.
  3. *
  4. * See Copyright for the status of this software.
  5. *
  6. * daniel@veillard.com
  7. */
  8. #include "libxml.h"
  9. #include <string.h>
  10. #include <stdarg.h>
  11. #include <assert.h>
  12. #if defined (_WIN32) && !defined(__CYGWIN__)
  13. #if defined (_MSC_VER) || defined(__BORLANDC__)
  14. #include <winsock2.h>
  15. #pragma comment(lib, "ws2_32.lib")
  16. #define gettimeofday(p1,p2)
  17. #endif /* _MSC_VER */
  18. #endif /* _WIN32 */
  19. #ifdef HAVE_SYS_TIME_H
  20. #include <sys/time.h>
  21. #endif
  22. #ifdef HAVE_TIME_H
  23. #include <time.h>
  24. #endif
  25. #ifdef __MINGW32__
  26. #define _WINSOCKAPI_
  27. #include <wsockcompat.h>
  28. #include <winsock2.h>
  29. #undef XML_SOCKLEN_T
  30. #define XML_SOCKLEN_T unsigned int
  31. #endif
  32. #ifdef HAVE_SYS_TIMEB_H
  33. #include <sys/timeb.h>
  34. #endif
  35. #ifdef HAVE_SYS_TYPES_H
  36. #include <sys/types.h>
  37. #endif
  38. #ifdef HAVE_SYS_STAT_H
  39. #include <sys/stat.h>
  40. #endif
  41. #ifdef HAVE_FCNTL_H
  42. #include <fcntl.h>
  43. #endif
  44. #ifdef HAVE_UNISTD_H
  45. #include <unistd.h>
  46. #endif
  47. #ifdef HAVE_SYS_MMAN_H
  48. #include <sys/mman.h>
  49. /* seems needed for Solaris */
  50. #ifndef MAP_FAILED
  51. #define MAP_FAILED ((void *) -1)
  52. #endif
  53. #endif
  54. #ifdef HAVE_STDLIB_H
  55. #include <stdlib.h>
  56. #endif
  57. #ifdef HAVE_LIBREADLINE
  58. #include <readline/readline.h>
  59. #ifdef HAVE_LIBHISTORY
  60. #include <readline/history.h>
  61. #endif
  62. #endif
  63. #include <libxml/xmlmemory.h>
  64. #include <libxml/parser.h>
  65. #include <libxml/parserInternals.h>
  66. #include <libxml/HTMLparser.h>
  67. #include <libxml/HTMLtree.h>
  68. #include <libxml/tree.h>
  69. #include <libxml/xpath.h>
  70. #include <libxml/debugXML.h>
  71. #include <libxml/xmlerror.h>
  72. #ifdef LIBXML_XINCLUDE_ENABLED
  73. #include <libxml/xinclude.h>
  74. #endif
  75. #ifdef LIBXML_CATALOG_ENABLED
  76. #include <libxml/catalog.h>
  77. #endif
  78. #include <libxml/globals.h>
  79. #include <libxml/xmlreader.h>
  80. #ifdef LIBXML_SCHEMATRON_ENABLED
  81. #include <libxml/schematron.h>
  82. #endif
  83. #ifdef LIBXML_SCHEMAS_ENABLED
  84. #include <libxml/relaxng.h>
  85. #include <libxml/xmlschemas.h>
  86. #endif
  87. #ifdef LIBXML_PATTERN_ENABLED
  88. #include <libxml/pattern.h>
  89. #endif
  90. #ifdef LIBXML_C14N_ENABLED
  91. #include <libxml/c14n.h>
  92. #endif
  93. #ifdef LIBXML_OUTPUT_ENABLED
  94. #include <libxml/xmlsave.h>
  95. #endif
  96. #ifndef XML_XML_DEFAULT_CATALOG
  97. #define XML_XML_DEFAULT_CATALOG "file:///etc/xml/catalog"
  98. #endif
  99. typedef enum {
  100. XMLLINT_RETURN_OK = 0, /* No error */
  101. XMLLINT_ERR_UNCLASS, /* Unclassified */
  102. XMLLINT_ERR_DTD, /* Error in DTD */
  103. XMLLINT_ERR_VALID, /* Validation error */
  104. XMLLINT_ERR_RDFILE, /* CtxtReadFile error */
  105. XMLLINT_ERR_SCHEMACOMP, /* Schema compilation */
  106. XMLLINT_ERR_OUT, /* Error writing output */
  107. XMLLINT_ERR_SCHEMAPAT, /* Error in schema pattern */
  108. XMLLINT_ERR_RDREGIS, /* Error in Reader registration */
  109. XMLLINT_ERR_MEM /* Out of memory error */
  110. } xmllintReturnCode;
  111. #ifdef LIBXML_DEBUG_ENABLED
  112. static int shell = 0;
  113. static int debugent = 0;
  114. #endif
  115. static int debug = 0;
  116. static int maxmem = 0;
  117. #ifdef LIBXML_TREE_ENABLED
  118. static int copy = 0;
  119. #endif /* LIBXML_TREE_ENABLED */
  120. static int recovery = 0;
  121. static int noent = 0;
  122. static int noblanks = 0;
  123. static int noout = 0;
  124. static int nowrap = 0;
  125. #ifdef LIBXML_OUTPUT_ENABLED
  126. static int format = 0;
  127. static const char *output = NULL;
  128. static int compress = 0;
  129. static int oldout = 0;
  130. #endif /* LIBXML_OUTPUT_ENABLED */
  131. #ifdef LIBXML_VALID_ENABLED
  132. static int valid = 0;
  133. static int postvalid = 0;
  134. static char * dtdvalid = NULL;
  135. static char * dtdvalidfpi = NULL;
  136. #endif
  137. #ifdef LIBXML_SCHEMAS_ENABLED
  138. static char * relaxng = NULL;
  139. static xmlRelaxNGPtr relaxngschemas = NULL;
  140. static char * schema = NULL;
  141. static xmlSchemaPtr wxschemas = NULL;
  142. #endif
  143. #ifdef LIBXML_SCHEMATRON_ENABLED
  144. static char * schematron = NULL;
  145. static xmlSchematronPtr wxschematron = NULL;
  146. #endif
  147. static int repeat = 0;
  148. static int insert = 0;
  149. #if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED)
  150. static int html = 0;
  151. static int xmlout = 0;
  152. #endif
  153. static int htmlout = 0;
  154. #ifdef LIBXML_PUSH_ENABLED
  155. static int push = 0;
  156. #endif /* LIBXML_PUSH_ENABLED */
  157. #ifdef HAVE_SYS_MMAN_H
  158. static int memory = 0;
  159. #endif
  160. static int testIO = 0;
  161. static char *encoding = NULL;
  162. #ifdef LIBXML_XINCLUDE_ENABLED
  163. static int xinclude = 0;
  164. #endif
  165. static int dtdattrs = 0;
  166. static int loaddtd = 0;
  167. static xmllintReturnCode progresult = XMLLINT_RETURN_OK;
  168. static int timing = 0;
  169. static int generate = 0;
  170. static int dropdtd = 0;
  171. #ifdef LIBXML_CATALOG_ENABLED
  172. static int catalogs = 0;
  173. static int nocatalogs = 0;
  174. #endif
  175. #ifdef LIBXML_C14N_ENABLED
  176. static int canonical = 0;
  177. static int canonical_11 = 0;
  178. static int exc_canonical = 0;
  179. #endif
  180. #ifdef LIBXML_READER_ENABLED
  181. static int stream = 0;
  182. static int walker = 0;
  183. #endif /* LIBXML_READER_ENABLED */
  184. static int chkregister = 0;
  185. static int nbregister = 0;
  186. #ifdef LIBXML_SAX1_ENABLED
  187. static int sax1 = 0;
  188. #endif /* LIBXML_SAX1_ENABLED */
  189. #ifdef LIBXML_PATTERN_ENABLED
  190. static const char *pattern = NULL;
  191. static xmlPatternPtr patternc = NULL;
  192. static xmlStreamCtxtPtr patstream = NULL;
  193. #endif
  194. static int options = XML_PARSE_COMPACT;
  195. static int sax = 0;
  196. static int oldxml10 = 0;
  197. /************************************************************************
  198. * *
  199. * Entity loading control and customization. *
  200. * *
  201. ************************************************************************/
  202. #define MAX_PATHS 64
  203. #ifdef _WIN32
  204. # define PATH_SEPARATOR ';'
  205. #else
  206. # define PATH_SEPARATOR ':'
  207. #endif
  208. static xmlChar *paths[MAX_PATHS + 1];
  209. static int nbpaths = 0;
  210. static int load_trace = 0;
  211. static
  212. void parsePath(const xmlChar *path) {
  213. const xmlChar *cur;
  214. if (path == NULL)
  215. return;
  216. while (*path != 0) {
  217. if (nbpaths >= MAX_PATHS) {
  218. fprintf(stderr, "MAX_PATHS reached: too many paths\n");
  219. return;
  220. }
  221. cur = path;
  222. while ((*cur == ' ') || (*cur == PATH_SEPARATOR))
  223. cur++;
  224. path = cur;
  225. while ((*cur != 0) && (*cur != ' ') && (*cur != PATH_SEPARATOR))
  226. cur++;
  227. if (cur != path) {
  228. paths[nbpaths] = xmlStrndup(path, cur - path);
  229. if (paths[nbpaths] != NULL)
  230. nbpaths++;
  231. path = cur;
  232. }
  233. }
  234. }
  235. static xmlExternalEntityLoader defaultEntityLoader = NULL;
  236. static xmlParserInputPtr
  237. xmllintExternalEntityLoader(const char *URL, const char *ID,
  238. xmlParserCtxtPtr ctxt) {
  239. xmlParserInputPtr ret;
  240. warningSAXFunc warning = NULL;
  241. errorSAXFunc err = NULL;
  242. int i;
  243. const char *lastsegment = URL;
  244. const char *iter = URL;
  245. if ((nbpaths > 0) && (iter != NULL)) {
  246. while (*iter != 0) {
  247. if (*iter == '/')
  248. lastsegment = iter + 1;
  249. iter++;
  250. }
  251. }
  252. if ((ctxt != NULL) && (ctxt->sax != NULL)) {
  253. warning = ctxt->sax->warning;
  254. err = ctxt->sax->error;
  255. ctxt->sax->warning = NULL;
  256. ctxt->sax->error = NULL;
  257. }
  258. if (defaultEntityLoader != NULL) {
  259. ret = defaultEntityLoader(URL, ID, ctxt);
  260. if (ret != NULL) {
  261. if (warning != NULL)
  262. ctxt->sax->warning = warning;
  263. if (err != NULL)
  264. ctxt->sax->error = err;
  265. if (load_trace) {
  266. fprintf \
  267. (stderr,
  268. "Loaded URL=\"%s\" ID=\"%s\"\n",
  269. URL ? URL : "(null)",
  270. ID ? ID : "(null)");
  271. }
  272. return(ret);
  273. }
  274. }
  275. for (i = 0;i < nbpaths;i++) {
  276. xmlChar *newURL;
  277. newURL = xmlStrdup((const xmlChar *) paths[i]);
  278. newURL = xmlStrcat(newURL, (const xmlChar *) "/");
  279. newURL = xmlStrcat(newURL, (const xmlChar *) lastsegment);
  280. if (newURL != NULL) {
  281. ret = defaultEntityLoader((const char *)newURL, ID, ctxt);
  282. if (ret != NULL) {
  283. if (warning != NULL)
  284. ctxt->sax->warning = warning;
  285. if (err != NULL)
  286. ctxt->sax->error = err;
  287. if (load_trace) {
  288. fprintf \
  289. (stderr,
  290. "Loaded URL=\"%s\" ID=\"%s\"\n",
  291. newURL,
  292. ID ? ID : "(null)");
  293. }
  294. xmlFree(newURL);
  295. return(ret);
  296. }
  297. xmlFree(newURL);
  298. }
  299. }
  300. if (err != NULL)
  301. ctxt->sax->error = err;
  302. if (warning != NULL) {
  303. ctxt->sax->warning = warning;
  304. if (URL != NULL)
  305. warning(ctxt, "failed to load external entity \"%s\"\n", URL);
  306. else if (ID != NULL)
  307. warning(ctxt, "failed to load external entity \"%s\"\n", ID);
  308. }
  309. return(NULL);
  310. }
  311. /************************************************************************
  312. * *
  313. * Memory allocation consumption debugging *
  314. * *
  315. ************************************************************************/
  316. static void
  317. OOM(void)
  318. {
  319. fprintf(stderr, "Ran out of memory needs > %d bytes\n", maxmem);
  320. progresult = XMLLINT_ERR_MEM;
  321. }
  322. static void
  323. myFreeFunc(void *mem)
  324. {
  325. xmlMemFree(mem);
  326. }
  327. static void *
  328. myMallocFunc(size_t size)
  329. {
  330. void *ret;
  331. ret = xmlMemMalloc(size);
  332. if (ret != NULL) {
  333. if (xmlMemUsed() > maxmem) {
  334. OOM();
  335. xmlMemFree(ret);
  336. return (NULL);
  337. }
  338. }
  339. return (ret);
  340. }
  341. static void *
  342. myReallocFunc(void *mem, size_t size)
  343. {
  344. void *ret;
  345. ret = xmlMemRealloc(mem, size);
  346. if (ret != NULL) {
  347. if (xmlMemUsed() > maxmem) {
  348. OOM();
  349. xmlMemFree(ret);
  350. return (NULL);
  351. }
  352. }
  353. return (ret);
  354. }
  355. static char *
  356. myStrdupFunc(const char *str)
  357. {
  358. char *ret;
  359. ret = xmlMemoryStrdup(str);
  360. if (ret != NULL) {
  361. if (xmlMemUsed() > maxmem) {
  362. OOM();
  363. xmlFree(ret);
  364. return (NULL);
  365. }
  366. }
  367. return (ret);
  368. }
  369. /************************************************************************
  370. * *
  371. * Internal timing routines to remove the necessity to have *
  372. * unix-specific function calls. *
  373. * *
  374. ************************************************************************/
  375. #ifndef HAVE_GETTIMEOFDAY
  376. #ifdef HAVE_SYS_TIMEB_H
  377. #ifdef HAVE_SYS_TIME_H
  378. #ifdef HAVE_FTIME
  379. static int
  380. my_gettimeofday(struct timeval *tvp, void *tzp)
  381. {
  382. struct timeb timebuffer;
  383. ftime(&timebuffer);
  384. if (tvp) {
  385. tvp->tv_sec = timebuffer.time;
  386. tvp->tv_usec = timebuffer.millitm * 1000L;
  387. }
  388. return (0);
  389. }
  390. #define HAVE_GETTIMEOFDAY 1
  391. #define gettimeofday my_gettimeofday
  392. #endif /* HAVE_FTIME */
  393. #endif /* HAVE_SYS_TIME_H */
  394. #endif /* HAVE_SYS_TIMEB_H */
  395. #endif /* !HAVE_GETTIMEOFDAY */
  396. #if defined(HAVE_GETTIMEOFDAY)
  397. static struct timeval begin, end;
  398. /*
  399. * startTimer: call where you want to start timing
  400. */
  401. static void
  402. startTimer(void)
  403. {
  404. gettimeofday(&begin, NULL);
  405. }
  406. /*
  407. * endTimer: call where you want to stop timing and to print out a
  408. * message about the timing performed; format is a printf
  409. * type argument
  410. */
  411. static void XMLCDECL
  412. endTimer(const char *fmt, ...)
  413. {
  414. long msec;
  415. va_list ap;
  416. gettimeofday(&end, NULL);
  417. msec = end.tv_sec - begin.tv_sec;
  418. msec *= 1000;
  419. msec += (end.tv_usec - begin.tv_usec) / 1000;
  420. #ifndef HAVE_STDARG_H
  421. #error "endTimer required stdarg functions"
  422. #endif
  423. va_start(ap, fmt);
  424. vfprintf(stderr, fmt, ap);
  425. va_end(ap);
  426. fprintf(stderr, " took %ld ms\n", msec);
  427. }
  428. #elif defined(HAVE_TIME_H)
  429. /*
  430. * No gettimeofday function, so we have to make do with calling clock.
  431. * This is obviously less accurate, but there's little we can do about
  432. * that.
  433. */
  434. #ifndef CLOCKS_PER_SEC
  435. #define CLOCKS_PER_SEC 100
  436. #endif
  437. static clock_t begin, end;
  438. static void
  439. startTimer(void)
  440. {
  441. begin = clock();
  442. }
  443. static void XMLCDECL
  444. endTimer(const char *fmt, ...)
  445. {
  446. long msec;
  447. va_list ap;
  448. end = clock();
  449. msec = ((end - begin) * 1000) / CLOCKS_PER_SEC;
  450. #ifndef HAVE_STDARG_H
  451. #error "endTimer required stdarg functions"
  452. #endif
  453. va_start(ap, fmt);
  454. vfprintf(stderr, fmt, ap);
  455. va_end(ap);
  456. fprintf(stderr, " took %ld ms\n", msec);
  457. }
  458. #else
  459. /*
  460. * We don't have a gettimeofday or time.h, so we just don't do timing
  461. */
  462. static void
  463. startTimer(void)
  464. {
  465. /*
  466. * Do nothing
  467. */
  468. }
  469. static void XMLCDECL
  470. endTimer(char *format, ...)
  471. {
  472. /*
  473. * We cannot do anything because we don't have a timing function
  474. */
  475. #ifdef HAVE_STDARG_H
  476. va_start(ap, format);
  477. vfprintf(stderr, format, ap);
  478. va_end(ap);
  479. fprintf(stderr, " was not timed\n", msec);
  480. #else
  481. /* We don't have gettimeofday, time or stdarg.h, what crazy world is
  482. * this ?!
  483. */
  484. #endif
  485. }
  486. #endif
  487. /************************************************************************
  488. * *
  489. * HTML ouput *
  490. * *
  491. ************************************************************************/
  492. static char buffer[50000];
  493. static void
  494. xmlHTMLEncodeSend(void) {
  495. char *result;
  496. result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
  497. if (result) {
  498. xmlGenericError(xmlGenericErrorContext, "%s", result);
  499. xmlFree(result);
  500. }
  501. buffer[0] = 0;
  502. }
  503. /**
  504. * xmlHTMLPrintFileInfo:
  505. * @input: an xmlParserInputPtr input
  506. *
  507. * Displays the associated file and line informations for the current input
  508. */
  509. static void
  510. xmlHTMLPrintFileInfo(xmlParserInputPtr input) {
  511. int len;
  512. xmlGenericError(xmlGenericErrorContext, "<p>");
  513. len = strlen(buffer);
  514. if (input != NULL) {
  515. if (input->filename) {
  516. snprintf(&buffer[len], sizeof(buffer) - len, "%s:%d: ", input->filename,
  517. input->line);
  518. } else {
  519. snprintf(&buffer[len], sizeof(buffer) - len, "Entity: line %d: ", input->line);
  520. }
  521. }
  522. xmlHTMLEncodeSend();
  523. }
  524. /**
  525. * xmlHTMLPrintFileContext:
  526. * @input: an xmlParserInputPtr input
  527. *
  528. * Displays current context within the input content for error tracking
  529. */
  530. static void
  531. xmlHTMLPrintFileContext(xmlParserInputPtr input) {
  532. const xmlChar *cur, *base;
  533. int len;
  534. int n;
  535. if (input == NULL) return;
  536. xmlGenericError(xmlGenericErrorContext, "<pre>\n");
  537. cur = input->cur;
  538. base = input->base;
  539. while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
  540. cur--;
  541. }
  542. n = 0;
  543. while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
  544. cur--;
  545. if ((*cur == '\n') || (*cur == '\r')) cur++;
  546. base = cur;
  547. n = 0;
  548. while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
  549. len = strlen(buffer);
  550. snprintf(&buffer[len], sizeof(buffer) - len, "%c",
  551. (unsigned char) *cur++);
  552. n++;
  553. }
  554. len = strlen(buffer);
  555. snprintf(&buffer[len], sizeof(buffer) - len, "\n");
  556. cur = input->cur;
  557. while ((*cur == '\n') || (*cur == '\r'))
  558. cur--;
  559. n = 0;
  560. while ((cur != base) && (n++ < 80)) {
  561. len = strlen(buffer);
  562. snprintf(&buffer[len], sizeof(buffer) - len, " ");
  563. base++;
  564. }
  565. len = strlen(buffer);
  566. snprintf(&buffer[len], sizeof(buffer) - len, "^\n");
  567. xmlHTMLEncodeSend();
  568. xmlGenericError(xmlGenericErrorContext, "</pre>");
  569. }
  570. /**
  571. * xmlHTMLError:
  572. * @ctx: an XML parser context
  573. * @msg: the message to display/transmit
  574. * @...: extra parameters for the message display
  575. *
  576. * Display and format an error messages, gives file, line, position and
  577. * extra parameters.
  578. */
  579. static void XMLCDECL
  580. xmlHTMLError(void *ctx, const char *msg, ...)
  581. {
  582. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  583. xmlParserInputPtr input;
  584. va_list args;
  585. int len;
  586. buffer[0] = 0;
  587. input = ctxt->input;
  588. if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
  589. input = ctxt->inputTab[ctxt->inputNr - 2];
  590. }
  591. xmlHTMLPrintFileInfo(input);
  592. xmlGenericError(xmlGenericErrorContext, "<b>error</b>: ");
  593. va_start(args, msg);
  594. len = strlen(buffer);
  595. vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
  596. va_end(args);
  597. xmlHTMLEncodeSend();
  598. xmlGenericError(xmlGenericErrorContext, "</p>\n");
  599. xmlHTMLPrintFileContext(input);
  600. xmlHTMLEncodeSend();
  601. }
  602. /**
  603. * xmlHTMLWarning:
  604. * @ctx: an XML parser context
  605. * @msg: the message to display/transmit
  606. * @...: extra parameters for the message display
  607. *
  608. * Display and format a warning messages, gives file, line, position and
  609. * extra parameters.
  610. */
  611. static void XMLCDECL
  612. xmlHTMLWarning(void *ctx, const char *msg, ...)
  613. {
  614. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  615. xmlParserInputPtr input;
  616. va_list args;
  617. int len;
  618. buffer[0] = 0;
  619. input = ctxt->input;
  620. if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
  621. input = ctxt->inputTab[ctxt->inputNr - 2];
  622. }
  623. xmlHTMLPrintFileInfo(input);
  624. xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: ");
  625. va_start(args, msg);
  626. len = strlen(buffer);
  627. vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
  628. va_end(args);
  629. xmlHTMLEncodeSend();
  630. xmlGenericError(xmlGenericErrorContext, "</p>\n");
  631. xmlHTMLPrintFileContext(input);
  632. xmlHTMLEncodeSend();
  633. }
  634. /**
  635. * xmlHTMLValidityError:
  636. * @ctx: an XML parser context
  637. * @msg: the message to display/transmit
  638. * @...: extra parameters for the message display
  639. *
  640. * Display and format an validity error messages, gives file,
  641. * line, position and extra parameters.
  642. */
  643. static void XMLCDECL
  644. xmlHTMLValidityError(void *ctx, const char *msg, ...)
  645. {
  646. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  647. xmlParserInputPtr input;
  648. va_list args;
  649. int len;
  650. buffer[0] = 0;
  651. input = ctxt->input;
  652. if ((input->filename == NULL) && (ctxt->inputNr > 1))
  653. input = ctxt->inputTab[ctxt->inputNr - 2];
  654. xmlHTMLPrintFileInfo(input);
  655. xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: ");
  656. len = strlen(buffer);
  657. va_start(args, msg);
  658. vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
  659. va_end(args);
  660. xmlHTMLEncodeSend();
  661. xmlGenericError(xmlGenericErrorContext, "</p>\n");
  662. xmlHTMLPrintFileContext(input);
  663. xmlHTMLEncodeSend();
  664. progresult = XMLLINT_ERR_VALID;
  665. }
  666. /**
  667. * xmlHTMLValidityWarning:
  668. * @ctx: an XML parser context
  669. * @msg: the message to display/transmit
  670. * @...: extra parameters for the message display
  671. *
  672. * Display and format a validity warning messages, gives file, line,
  673. * position and extra parameters.
  674. */
  675. static void XMLCDECL
  676. xmlHTMLValidityWarning(void *ctx, const char *msg, ...)
  677. {
  678. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  679. xmlParserInputPtr input;
  680. va_list args;
  681. int len;
  682. buffer[0] = 0;
  683. input = ctxt->input;
  684. if ((input->filename == NULL) && (ctxt->inputNr > 1))
  685. input = ctxt->inputTab[ctxt->inputNr - 2];
  686. xmlHTMLPrintFileInfo(input);
  687. xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: ");
  688. va_start(args, msg);
  689. len = strlen(buffer);
  690. vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
  691. va_end(args);
  692. xmlHTMLEncodeSend();
  693. xmlGenericError(xmlGenericErrorContext, "</p>\n");
  694. xmlHTMLPrintFileContext(input);
  695. xmlHTMLEncodeSend();
  696. }
  697. /************************************************************************
  698. * *
  699. * Shell Interface *
  700. * *
  701. ************************************************************************/
  702. #ifdef LIBXML_DEBUG_ENABLED
  703. #ifdef LIBXML_XPATH_ENABLED
  704. /**
  705. * xmlShellReadline:
  706. * @prompt: the prompt value
  707. *
  708. * Read a string
  709. *
  710. * Returns a pointer to it or NULL on EOF the caller is expected to
  711. * free the returned string.
  712. */
  713. static char *
  714. xmlShellReadline(char *prompt) {
  715. #ifdef HAVE_LIBREADLINE
  716. char *line_read;
  717. /* Get a line from the user. */
  718. line_read = readline (prompt);
  719. /* If the line has any text in it, save it on the history. */
  720. if (line_read && *line_read)
  721. add_history (line_read);
  722. return (line_read);
  723. #else
  724. char line_read[501];
  725. char *ret;
  726. int len;
  727. if (prompt != NULL)
  728. fprintf(stdout, "%s", prompt);
  729. if (!fgets(line_read, 500, stdin))
  730. return(NULL);
  731. line_read[500] = 0;
  732. len = strlen(line_read);
  733. ret = (char *) malloc(len + 1);
  734. if (ret != NULL) {
  735. memcpy (ret, line_read, len + 1);
  736. }
  737. return(ret);
  738. #endif
  739. }
  740. #endif /* LIBXML_XPATH_ENABLED */
  741. #endif /* LIBXML_DEBUG_ENABLED */
  742. /************************************************************************
  743. * *
  744. * I/O Interfaces *
  745. * *
  746. ************************************************************************/
  747. static int myRead(FILE *f, char * buf, int len) {
  748. return(fread(buf, 1, len, f));
  749. }
  750. static void myClose(FILE *f) {
  751. if (f != stdin) {
  752. fclose(f);
  753. }
  754. }
  755. /************************************************************************
  756. * *
  757. * SAX based tests *
  758. * *
  759. ************************************************************************/
  760. /*
  761. * empty SAX block
  762. */
  763. static xmlSAXHandler emptySAXHandlerStruct = {
  764. NULL, /* internalSubset */
  765. NULL, /* isStandalone */
  766. NULL, /* hasInternalSubset */
  767. NULL, /* hasExternalSubset */
  768. NULL, /* resolveEntity */
  769. NULL, /* getEntity */
  770. NULL, /* entityDecl */
  771. NULL, /* notationDecl */
  772. NULL, /* attributeDecl */
  773. NULL, /* elementDecl */
  774. NULL, /* unparsedEntityDecl */
  775. NULL, /* setDocumentLocator */
  776. NULL, /* startDocument */
  777. NULL, /* endDocument */
  778. NULL, /* startElement */
  779. NULL, /* endElement */
  780. NULL, /* reference */
  781. NULL, /* characters */
  782. NULL, /* ignorableWhitespace */
  783. NULL, /* processingInstruction */
  784. NULL, /* comment */
  785. NULL, /* xmlParserWarning */
  786. NULL, /* xmlParserError */
  787. NULL, /* xmlParserError */
  788. NULL, /* getParameterEntity */
  789. NULL, /* cdataBlock; */
  790. NULL, /* externalSubset; */
  791. XML_SAX2_MAGIC,
  792. NULL,
  793. NULL, /* startElementNs */
  794. NULL, /* endElementNs */
  795. NULL /* xmlStructuredErrorFunc */
  796. };
  797. static xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
  798. extern xmlSAXHandlerPtr debugSAXHandler;
  799. static int callbacks;
  800. /**
  801. * isStandaloneDebug:
  802. * @ctxt: An XML parser context
  803. *
  804. * Is this document tagged standalone ?
  805. *
  806. * Returns 1 if true
  807. */
  808. static int
  809. isStandaloneDebug(void *ctx ATTRIBUTE_UNUSED)
  810. {
  811. callbacks++;
  812. if (noout)
  813. return(0);
  814. fprintf(stdout, "SAX.isStandalone()\n");
  815. return(0);
  816. }
  817. /**
  818. * hasInternalSubsetDebug:
  819. * @ctxt: An XML parser context
  820. *
  821. * Does this document has an internal subset
  822. *
  823. * Returns 1 if true
  824. */
  825. static int
  826. hasInternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED)
  827. {
  828. callbacks++;
  829. if (noout)
  830. return(0);
  831. fprintf(stdout, "SAX.hasInternalSubset()\n");
  832. return(0);
  833. }
  834. /**
  835. * hasExternalSubsetDebug:
  836. * @ctxt: An XML parser context
  837. *
  838. * Does this document has an external subset
  839. *
  840. * Returns 1 if true
  841. */
  842. static int
  843. hasExternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED)
  844. {
  845. callbacks++;
  846. if (noout)
  847. return(0);
  848. fprintf(stdout, "SAX.hasExternalSubset()\n");
  849. return(0);
  850. }
  851. /**
  852. * internalSubsetDebug:
  853. * @ctxt: An XML parser context
  854. *
  855. * Does this document has an internal subset
  856. */
  857. static void
  858. internalSubsetDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
  859. const xmlChar *ExternalID, const xmlChar *SystemID)
  860. {
  861. callbacks++;
  862. if (noout)
  863. return;
  864. fprintf(stdout, "SAX.internalSubset(%s,", name);
  865. if (ExternalID == NULL)
  866. fprintf(stdout, " ,");
  867. else
  868. fprintf(stdout, " %s,", ExternalID);
  869. if (SystemID == NULL)
  870. fprintf(stdout, " )\n");
  871. else
  872. fprintf(stdout, " %s)\n", SystemID);
  873. }
  874. /**
  875. * externalSubsetDebug:
  876. * @ctxt: An XML parser context
  877. *
  878. * Does this document has an external subset
  879. */
  880. static void
  881. externalSubsetDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
  882. const xmlChar *ExternalID, const xmlChar *SystemID)
  883. {
  884. callbacks++;
  885. if (noout)
  886. return;
  887. fprintf(stdout, "SAX.externalSubset(%s,", name);
  888. if (ExternalID == NULL)
  889. fprintf(stdout, " ,");
  890. else
  891. fprintf(stdout, " %s,", ExternalID);
  892. if (SystemID == NULL)
  893. fprintf(stdout, " )\n");
  894. else
  895. fprintf(stdout, " %s)\n", SystemID);
  896. }
  897. /**
  898. * resolveEntityDebug:
  899. * @ctxt: An XML parser context
  900. * @publicId: The public ID of the entity
  901. * @systemId: The system ID of the entity
  902. *
  903. * Special entity resolver, better left to the parser, it has
  904. * more context than the application layer.
  905. * The default behaviour is to NOT resolve the entities, in that case
  906. * the ENTITY_REF nodes are built in the structure (and the parameter
  907. * values).
  908. *
  909. * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
  910. */
  911. static xmlParserInputPtr
  912. resolveEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *publicId, const xmlChar *systemId)
  913. {
  914. callbacks++;
  915. if (noout)
  916. return(NULL);
  917. /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
  918. fprintf(stdout, "SAX.resolveEntity(");
  919. if (publicId != NULL)
  920. fprintf(stdout, "%s", (char *)publicId);
  921. else
  922. fprintf(stdout, " ");
  923. if (systemId != NULL)
  924. fprintf(stdout, ", %s)\n", (char *)systemId);
  925. else
  926. fprintf(stdout, ", )\n");
  927. return(NULL);
  928. }
  929. /**
  930. * getEntityDebug:
  931. * @ctxt: An XML parser context
  932. * @name: The entity name
  933. *
  934. * Get an entity by name
  935. *
  936. * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
  937. */
  938. static xmlEntityPtr
  939. getEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
  940. {
  941. callbacks++;
  942. if (noout)
  943. return(NULL);
  944. fprintf(stdout, "SAX.getEntity(%s)\n", name);
  945. return(NULL);
  946. }
  947. /**
  948. * getParameterEntityDebug:
  949. * @ctxt: An XML parser context
  950. * @name: The entity name
  951. *
  952. * Get a parameter entity by name
  953. *
  954. * Returns the xmlParserInputPtr
  955. */
  956. static xmlEntityPtr
  957. getParameterEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
  958. {
  959. callbacks++;
  960. if (noout)
  961. return(NULL);
  962. fprintf(stdout, "SAX.getParameterEntity(%s)\n", name);
  963. return(NULL);
  964. }
  965. /**
  966. * entityDeclDebug:
  967. * @ctxt: An XML parser context
  968. * @name: the entity name
  969. * @type: the entity type
  970. * @publicId: The public ID of the entity
  971. * @systemId: The system ID of the entity
  972. * @content: the entity value (without processing).
  973. *
  974. * An entity definition has been parsed
  975. */
  976. static void
  977. entityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type,
  978. const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
  979. {
  980. const xmlChar *nullstr = BAD_CAST "(null)";
  981. /* not all libraries handle printing null pointers nicely */
  982. if (publicId == NULL)
  983. publicId = nullstr;
  984. if (systemId == NULL)
  985. systemId = nullstr;
  986. if (content == NULL)
  987. content = (xmlChar *)nullstr;
  988. callbacks++;
  989. if (noout)
  990. return;
  991. fprintf(stdout, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
  992. name, type, publicId, systemId, content);
  993. }
  994. /**
  995. * attributeDeclDebug:
  996. * @ctxt: An XML parser context
  997. * @name: the attribute name
  998. * @type: the attribute type
  999. *
  1000. * An attribute definition has been parsed
  1001. */
  1002. static void
  1003. attributeDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar * elem,
  1004. const xmlChar * name, int type, int def,
  1005. const xmlChar * defaultValue, xmlEnumerationPtr tree)
  1006. {
  1007. callbacks++;
  1008. if (noout)
  1009. return;
  1010. if (defaultValue == NULL)
  1011. fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, NULL, ...)\n",
  1012. elem, name, type, def);
  1013. else
  1014. fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
  1015. elem, name, type, def, defaultValue);
  1016. xmlFreeEnumeration(tree);
  1017. }
  1018. /**
  1019. * elementDeclDebug:
  1020. * @ctxt: An XML parser context
  1021. * @name: the element name
  1022. * @type: the element type
  1023. * @content: the element value (without processing).
  1024. *
  1025. * An element definition has been parsed
  1026. */
  1027. static void
  1028. elementDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type,
  1029. xmlElementContentPtr content ATTRIBUTE_UNUSED)
  1030. {
  1031. callbacks++;
  1032. if (noout)
  1033. return;
  1034. fprintf(stdout, "SAX.elementDecl(%s, %d, ...)\n",
  1035. name, type);
  1036. }
  1037. /**
  1038. * notationDeclDebug:
  1039. * @ctxt: An XML parser context
  1040. * @name: The name of the notation
  1041. * @publicId: The public ID of the entity
  1042. * @systemId: The system ID of the entity
  1043. *
  1044. * What to do when a notation declaration has been parsed.
  1045. */
  1046. static void
  1047. notationDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
  1048. const xmlChar *publicId, const xmlChar *systemId)
  1049. {
  1050. callbacks++;
  1051. if (noout)
  1052. return;
  1053. fprintf(stdout, "SAX.notationDecl(%s, %s, %s)\n",
  1054. (char *) name, (char *) publicId, (char *) systemId);
  1055. }
  1056. /**
  1057. * unparsedEntityDeclDebug:
  1058. * @ctxt: An XML parser context
  1059. * @name: The name of the entity
  1060. * @publicId: The public ID of the entity
  1061. * @systemId: The system ID of the entity
  1062. * @notationName: the name of the notation
  1063. *
  1064. * What to do when an unparsed entity declaration is parsed
  1065. */
  1066. static void
  1067. unparsedEntityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
  1068. const xmlChar *publicId, const xmlChar *systemId,
  1069. const xmlChar *notationName)
  1070. {
  1071. const xmlChar *nullstr = BAD_CAST "(null)";
  1072. if (publicId == NULL)
  1073. publicId = nullstr;
  1074. if (systemId == NULL)
  1075. systemId = nullstr;
  1076. if (notationName == NULL)
  1077. notationName = nullstr;
  1078. callbacks++;
  1079. if (noout)
  1080. return;
  1081. fprintf(stdout, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
  1082. (char *) name, (char *) publicId, (char *) systemId,
  1083. (char *) notationName);
  1084. }
  1085. /**
  1086. * setDocumentLocatorDebug:
  1087. * @ctxt: An XML parser context
  1088. * @loc: A SAX Locator
  1089. *
  1090. * Receive the document locator at startup, actually xmlDefaultSAXLocator
  1091. * Everything is available on the context, so this is useless in our case.
  1092. */
  1093. static void
  1094. setDocumentLocatorDebug(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
  1095. {
  1096. callbacks++;
  1097. if (noout)
  1098. return;
  1099. fprintf(stdout, "SAX.setDocumentLocator()\n");
  1100. }
  1101. /**
  1102. * startDocumentDebug:
  1103. * @ctxt: An XML parser context
  1104. *
  1105. * called when the document start being processed.
  1106. */
  1107. static void
  1108. startDocumentDebug(void *ctx ATTRIBUTE_UNUSED)
  1109. {
  1110. callbacks++;
  1111. if (noout)
  1112. return;
  1113. fprintf(stdout, "SAX.startDocument()\n");
  1114. }
  1115. /**
  1116. * endDocumentDebug:
  1117. * @ctxt: An XML parser context
  1118. *
  1119. * called when the document end has been detected.
  1120. */
  1121. static void
  1122. endDocumentDebug(void *ctx ATTRIBUTE_UNUSED)
  1123. {
  1124. callbacks++;
  1125. if (noout)
  1126. return;
  1127. fprintf(stdout, "SAX.endDocument()\n");
  1128. }
  1129. /**
  1130. * startElementDebug:
  1131. * @ctxt: An XML parser context
  1132. * @name: The element name
  1133. *
  1134. * called when an opening tag has been processed.
  1135. */
  1136. static void
  1137. startElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, const xmlChar **atts)
  1138. {
  1139. int i;
  1140. callbacks++;
  1141. if (noout)
  1142. return;
  1143. fprintf(stdout, "SAX.startElement(%s", (char *) name);
  1144. if (atts != NULL) {
  1145. for (i = 0;(atts[i] != NULL);i++) {
  1146. fprintf(stdout, ", %s='", atts[i++]);
  1147. if (atts[i] != NULL)
  1148. fprintf(stdout, "%s'", atts[i]);
  1149. }
  1150. }
  1151. fprintf(stdout, ")\n");
  1152. }
  1153. /**
  1154. * endElementDebug:
  1155. * @ctxt: An XML parser context
  1156. * @name: The element name
  1157. *
  1158. * called when the end of an element has been detected.
  1159. */
  1160. static void
  1161. endElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
  1162. {
  1163. callbacks++;
  1164. if (noout)
  1165. return;
  1166. fprintf(stdout, "SAX.endElement(%s)\n", (char *) name);
  1167. }
  1168. /**
  1169. * charactersDebug:
  1170. * @ctxt: An XML parser context
  1171. * @ch: a xmlChar string
  1172. * @len: the number of xmlChar
  1173. *
  1174. * receiving some chars from the parser.
  1175. * Question: how much at a time ???
  1176. */
  1177. static void
  1178. charactersDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
  1179. {
  1180. char out[40];
  1181. int i;
  1182. callbacks++;
  1183. if (noout)
  1184. return;
  1185. for (i = 0;(i<len) && (i < 30);i++)
  1186. out[i] = ch[i];
  1187. out[i] = 0;
  1188. fprintf(stdout, "SAX.characters(%s, %d)\n", out, len);
  1189. }
  1190. /**
  1191. * referenceDebug:
  1192. * @ctxt: An XML parser context
  1193. * @name: The entity name
  1194. *
  1195. * called when an entity reference is detected.
  1196. */
  1197. static void
  1198. referenceDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
  1199. {
  1200. callbacks++;
  1201. if (noout)
  1202. return;
  1203. fprintf(stdout, "SAX.reference(%s)\n", name);
  1204. }
  1205. /**
  1206. * ignorableWhitespaceDebug:
  1207. * @ctxt: An XML parser context
  1208. * @ch: a xmlChar string
  1209. * @start: the first char in the string
  1210. * @len: the number of xmlChar
  1211. *
  1212. * receiving some ignorable whitespaces from the parser.
  1213. * Question: how much at a time ???
  1214. */
  1215. static void
  1216. ignorableWhitespaceDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
  1217. {
  1218. char out[40];
  1219. int i;
  1220. callbacks++;
  1221. if (noout)
  1222. return;
  1223. for (i = 0;(i<len) && (i < 30);i++)
  1224. out[i] = ch[i];
  1225. out[i] = 0;
  1226. fprintf(stdout, "SAX.ignorableWhitespace(%s, %d)\n", out, len);
  1227. }
  1228. /**
  1229. * processingInstructionDebug:
  1230. * @ctxt: An XML parser context
  1231. * @target: the target name
  1232. * @data: the PI data's
  1233. * @len: the number of xmlChar
  1234. *
  1235. * A processing instruction has been parsed.
  1236. */
  1237. static void
  1238. processingInstructionDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *target,
  1239. const xmlChar *data)
  1240. {
  1241. callbacks++;
  1242. if (noout)
  1243. return;
  1244. if (data != NULL)
  1245. fprintf(stdout, "SAX.processingInstruction(%s, %s)\n",
  1246. (char *) target, (char *) data);
  1247. else
  1248. fprintf(stdout, "SAX.processingInstruction(%s, NULL)\n",
  1249. (char *) target);
  1250. }
  1251. /**
  1252. * cdataBlockDebug:
  1253. * @ctx: the user data (XML parser context)
  1254. * @value: The pcdata content
  1255. * @len: the block length
  1256. *
  1257. * called when a pcdata block has been parsed
  1258. */
  1259. static void
  1260. cdataBlockDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value, int len)
  1261. {
  1262. callbacks++;
  1263. if (noout)
  1264. return;
  1265. fprintf(stdout, "SAX.pcdata(%.20s, %d)\n",
  1266. (char *) value, len);
  1267. }
  1268. /**
  1269. * commentDebug:
  1270. * @ctxt: An XML parser context
  1271. * @value: the comment content
  1272. *
  1273. * A comment has been parsed.
  1274. */
  1275. static void
  1276. commentDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value)
  1277. {
  1278. callbacks++;
  1279. if (noout)
  1280. return;
  1281. fprintf(stdout, "SAX.comment(%s)\n", value);
  1282. }
  1283. /**
  1284. * warningDebug:
  1285. * @ctxt: An XML parser context
  1286. * @msg: the message to display/transmit
  1287. * @...: extra parameters for the message display
  1288. *
  1289. * Display and format a warning messages, gives file, line, position and
  1290. * extra parameters.
  1291. */
  1292. static void XMLCDECL
  1293. warningDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
  1294. {
  1295. va_list args;
  1296. callbacks++;
  1297. if (noout)
  1298. return;
  1299. va_start(args, msg);
  1300. fprintf(stdout, "SAX.warning: ");
  1301. vfprintf(stdout, msg, args);
  1302. va_end(args);
  1303. }
  1304. /**
  1305. * errorDebug:
  1306. * @ctxt: An XML parser context
  1307. * @msg: the message to display/transmit
  1308. * @...: extra parameters for the message display
  1309. *
  1310. * Display and format a error messages, gives file, line, position and
  1311. * extra parameters.
  1312. */
  1313. static void XMLCDECL
  1314. errorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
  1315. {
  1316. va_list args;
  1317. callbacks++;
  1318. if (noout)
  1319. return;
  1320. va_start(args, msg);
  1321. fprintf(stdout, "SAX.error: ");
  1322. vfprintf(stdout, msg, args);
  1323. va_end(args);
  1324. }
  1325. /**
  1326. * fatalErrorDebug:
  1327. * @ctxt: An XML parser context
  1328. * @msg: the message to display/transmit
  1329. * @...: extra parameters for the message display
  1330. *
  1331. * Display and format a fatalError messages, gives file, line, position and
  1332. * extra parameters.
  1333. */
  1334. static void XMLCDECL
  1335. fatalErrorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
  1336. {
  1337. va_list args;
  1338. callbacks++;
  1339. if (noout)
  1340. return;
  1341. va_start(args, msg);
  1342. fprintf(stdout, "SAX.fatalError: ");
  1343. vfprintf(stdout, msg, args);
  1344. va_end(args);
  1345. }
  1346. static xmlSAXHandler debugSAXHandlerStruct = {
  1347. internalSubsetDebug,
  1348. isStandaloneDebug,
  1349. hasInternalSubsetDebug,
  1350. hasExternalSubsetDebug,
  1351. resolveEntityDebug,
  1352. getEntityDebug,
  1353. entityDeclDebug,
  1354. notationDeclDebug,
  1355. attributeDeclDebug,
  1356. elementDeclDebug,
  1357. unparsedEntityDeclDebug,
  1358. setDocumentLocatorDebug,
  1359. startDocumentDebug,
  1360. endDocumentDebug,
  1361. startElementDebug,
  1362. endElementDebug,
  1363. referenceDebug,
  1364. charactersDebug,
  1365. ignorableWhitespaceDebug,
  1366. processingInstructionDebug,
  1367. commentDebug,
  1368. warningDebug,
  1369. errorDebug,
  1370. fatalErrorDebug,
  1371. getParameterEntityDebug,
  1372. cdataBlockDebug,
  1373. externalSubsetDebug,
  1374. 1,
  1375. NULL,
  1376. NULL,
  1377. NULL,
  1378. NULL
  1379. };
  1380. xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;
  1381. /*
  1382. * SAX2 specific callbacks
  1383. */
  1384. /**
  1385. * startElementNsDebug:
  1386. * @ctxt: An XML parser context
  1387. * @name: The element name
  1388. *
  1389. * called when an opening tag has been processed.
  1390. */
  1391. static void
  1392. startElementNsDebug(void *ctx ATTRIBUTE_UNUSED,
  1393. const xmlChar *localname,
  1394. const xmlChar *prefix,
  1395. const xmlChar *URI,
  1396. int nb_namespaces,
  1397. const xmlChar **namespaces,
  1398. int nb_attributes,
  1399. int nb_defaulted,
  1400. const xmlChar **attributes)
  1401. {
  1402. int i;
  1403. callbacks++;
  1404. if (noout)
  1405. return;
  1406. fprintf(stdout, "SAX.startElementNs(%s", (char *) localname);
  1407. if (prefix == NULL)
  1408. fprintf(stdout, ", NULL");
  1409. else
  1410. fprintf(stdout, ", %s", (char *) prefix);
  1411. if (URI == NULL)
  1412. fprintf(stdout, ", NULL");
  1413. else
  1414. fprintf(stdout, ", '%s'", (char *) URI);
  1415. fprintf(stdout, ", %d", nb_namespaces);
  1416. if (namespaces != NULL) {
  1417. for (i = 0;i < nb_namespaces * 2;i++) {
  1418. fprintf(stdout, ", xmlns");
  1419. if (namespaces[i] != NULL)
  1420. fprintf(stdout, ":%s", namespaces[i]);
  1421. i++;
  1422. fprintf(stdout, "='%s'", namespaces[i]);
  1423. }
  1424. }
  1425. fprintf(stdout, ", %d, %d", nb_attributes, nb_defaulted);
  1426. if (attributes != NULL) {
  1427. for (i = 0;i < nb_attributes * 5;i += 5) {
  1428. if (attributes[i + 1] != NULL)
  1429. fprintf(stdout, ", %s:%s='", attributes[i + 1], attributes[i]);
  1430. else
  1431. fprintf(stdout, ", %s='", attributes[i]);
  1432. fprintf(stdout, "%.4s...', %d", attributes[i + 3],
  1433. (int)(attributes[i + 4] - attributes[i + 3]));
  1434. }
  1435. }
  1436. fprintf(stdout, ")\n");
  1437. }
  1438. /**
  1439. * endElementDebug:
  1440. * @ctxt: An XML parser context
  1441. * @name: The element name
  1442. *
  1443. * called when the end of an element has been detected.
  1444. */
  1445. static void
  1446. endElementNsDebug(void *ctx ATTRIBUTE_UNUSED,
  1447. const xmlChar *localname,
  1448. const xmlChar *prefix,
  1449. const xmlChar *URI)
  1450. {
  1451. callbacks++;
  1452. if (noout)
  1453. return;
  1454. fprintf(stdout, "SAX.endElementNs(%s", (char *) localname);
  1455. if (prefix == NULL)
  1456. fprintf(stdout, ", NULL");
  1457. else
  1458. fprintf(stdout, ", %s", (char *) prefix);
  1459. if (URI == NULL)
  1460. fprintf(stdout, ", NULL)\n");
  1461. else
  1462. fprintf(stdout, ", '%s')\n", (char *) URI);
  1463. }
  1464. static xmlSAXHandler debugSAX2HandlerStruct = {
  1465. internalSubsetDebug,
  1466. isStandaloneDebug,
  1467. hasInternalSubsetDebug,
  1468. hasExternalSubsetDebug,
  1469. resolveEntityDebug,
  1470. getEntityDebug,
  1471. entityDeclDebug,
  1472. notationDeclDebug,
  1473. attributeDeclDebug,
  1474. elementDeclDebug,
  1475. unparsedEntityDeclDebug,
  1476. setDocumentLocatorDebug,
  1477. startDocumentDebug,
  1478. endDocumentDebug,
  1479. NULL,
  1480. NULL,
  1481. referenceDebug,
  1482. charactersDebug,
  1483. ignorableWhitespaceDebug,
  1484. processingInstructionDebug,
  1485. commentDebug,
  1486. warningDebug,
  1487. errorDebug,
  1488. fatalErrorDebug,
  1489. getParameterEntityDebug,
  1490. cdataBlockDebug,
  1491. externalSubsetDebug,
  1492. XML_SAX2_MAGIC,
  1493. NULL,
  1494. startElementNsDebug,
  1495. endElementNsDebug,
  1496. NULL
  1497. };
  1498. static xmlSAXHandlerPtr debugSAX2Handler = &debugSAX2HandlerStruct;
  1499. static void
  1500. testSAX(const char *filename) {
  1501. xmlSAXHandlerPtr handler;
  1502. const char *user_data = "user_data"; /* mostly for debugging */
  1503. xmlParserInputBufferPtr buf = NULL;
  1504. xmlParserInputPtr inputStream;
  1505. xmlParserCtxtPtr ctxt = NULL;
  1506. xmlSAXHandlerPtr old_sax = NULL;
  1507. callbacks = 0;
  1508. if (noout) {
  1509. handler = emptySAXHandler;
  1510. #ifdef LIBXML_SAX1_ENABLED
  1511. } else if (sax1) {
  1512. handler = debugSAXHandler;
  1513. #endif
  1514. } else {
  1515. handler = debugSAX2Handler;
  1516. }
  1517. /*
  1518. * it's not the simplest code but the most generic in term of I/O
  1519. */
  1520. buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
  1521. if (buf == NULL) {
  1522. goto error;
  1523. }
  1524. #ifdef LIBXML_SCHEMAS_ENABLED
  1525. if (wxschemas != NULL) {
  1526. int ret;
  1527. xmlSchemaValidCtxtPtr vctxt;
  1528. vctxt = xmlSchemaNewValidCtxt(wxschemas);
  1529. xmlSchemaSetValidErrors(vctxt,
  1530. (xmlSchemaValidityErrorFunc) fprintf,
  1531. (xmlSchemaValidityWarningFunc) fprintf,
  1532. stderr);
  1533. ret = xmlSchemaValidateStream(vctxt, buf, 0, handler,
  1534. (void *)user_data);
  1535. if (repeat == 0) {
  1536. if (ret == 0) {
  1537. fprintf(stderr, "%s validates\n", filename);
  1538. } else if (ret > 0) {
  1539. fprintf(stderr, "%s fails to validate\n", filename);
  1540. progresult = XMLLINT_ERR_VALID;
  1541. } else {
  1542. fprintf(stderr, "%s validation generated an internal error\n",
  1543. filename);
  1544. progresult = XMLLINT_ERR_VALID;
  1545. }
  1546. }
  1547. xmlSchemaFreeValidCtxt(vctxt);
  1548. } else
  1549. #endif
  1550. {
  1551. /*
  1552. * Create the parser context amd hook the input
  1553. */
  1554. ctxt = xmlNewParserCtxt();
  1555. if (ctxt == NULL) {
  1556. xmlFreeParserInputBuffer(buf);
  1557. goto error;
  1558. }
  1559. old_sax = ctxt->sax;
  1560. ctxt->sax = handler;
  1561. ctxt->userData = (void *) user_data;
  1562. inputStream = xmlNewIOInputStream(ctxt, buf, XML_CHAR_ENCODING_NONE);
  1563. if (inputStream == NULL) {
  1564. xmlFreeParserInputBuffer(buf);
  1565. goto error;
  1566. }
  1567. inputPush(ctxt, inputStream);
  1568. /* do the parsing */
  1569. xmlParseDocument(ctxt);
  1570. if (ctxt->myDoc != NULL) {
  1571. fprintf(stderr, "SAX generated a doc !\n");
  1572. xmlFreeDoc(ctxt->myDoc);
  1573. ctxt->myDoc = NULL;
  1574. }
  1575. }
  1576. error:
  1577. if (ctxt != NULL) {
  1578. ctxt->sax = old_sax;
  1579. xmlFreeParserCtxt(ctxt);
  1580. }
  1581. }
  1582. /************************************************************************
  1583. * *
  1584. * Stream Test processing *
  1585. * *
  1586. ************************************************************************/
  1587. #ifdef LIBXML_READER_ENABLED
  1588. static void processNode(xmlTextReaderPtr reader) {
  1589. const xmlChar *name, *value;
  1590. int type, empty;
  1591. type = xmlTextReaderNodeType(reader);
  1592. empty = xmlTextReaderIsEmptyElement(reader);
  1593. if (debug) {
  1594. name = xmlTextReaderConstName(reader);
  1595. if (name == NULL)
  1596. name = BAD_CAST "--";
  1597. value = xmlTextReaderConstValue(reader);
  1598. printf("%d %d %s %d %d",
  1599. xmlTextReaderDepth(reader),
  1600. type,
  1601. name,
  1602. empty,
  1603. xmlTextReaderHasValue(reader));
  1604. if (value == NULL)
  1605. printf("\n");
  1606. else {
  1607. printf(" %s\n", value);
  1608. }
  1609. }
  1610. #ifdef LIBXML_PATTERN_ENABLED
  1611. if (patternc) {
  1612. xmlChar *path = NULL;
  1613. int match = -1;
  1614. if (type == XML_READER_TYPE_ELEMENT) {
  1615. /* do the check only on element start */
  1616. match = xmlPatternMatch(patternc, xmlTextReaderCurrentNode(reader));
  1617. if (match) {
  1618. #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
  1619. path = xmlGetNodePath(xmlTextReaderCurrentNode(reader));
  1620. printf("Node %s matches pattern %s\n", path, pattern);
  1621. #else
  1622. printf("Node %s matches pattern %s\n",
  1623. xmlTextReaderConstName(reader), pattern);
  1624. #endif
  1625. }
  1626. }
  1627. if (patstream != NULL) {
  1628. int ret;
  1629. if (type == XML_READER_TYPE_ELEMENT) {
  1630. ret = xmlStreamPush(patstream,
  1631. xmlTextReaderConstLocalName(reader),
  1632. xmlTextReaderConstNamespaceUri(reader));
  1633. if (ret < 0) {
  1634. fprintf(stderr, "xmlStreamPush() failure\n");
  1635. xmlFreeStreamCtxt(patstream);
  1636. patstream = NULL;
  1637. } else if (ret != match) {
  1638. #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
  1639. if (path == NULL) {
  1640. path = xmlGetNodePath(
  1641. xmlTextReaderCurrentNode(reader));
  1642. }
  1643. #endif
  1644. fprintf(stderr,
  1645. "xmlPatternMatch and xmlStreamPush disagree\n");
  1646. if (path != NULL)
  1647. fprintf(stderr, " pattern %s node %s\n",
  1648. pattern, path);
  1649. else
  1650. fprintf(stderr, " pattern %s node %s\n",
  1651. pattern, xmlTextReaderConstName(reader));
  1652. }
  1653. }
  1654. if ((type == XML_READER_TYPE_END_ELEMENT) ||
  1655. ((type == XML_READER_TYPE_ELEMENT) && (empty))) {
  1656. ret = xmlStreamPop(patstream);
  1657. if (ret < 0) {
  1658. fprintf(stderr, "xmlStreamPop() failure\n");
  1659. xmlFreeStreamCtxt(patstream);
  1660. patstream = NULL;
  1661. }
  1662. }
  1663. }
  1664. if (path != NULL)
  1665. xmlFree(path);
  1666. }
  1667. #endif
  1668. }
  1669. static void streamFile(char *filename) {
  1670. xmlTextReaderPtr reader;
  1671. int ret;
  1672. #ifdef HAVE_SYS_MMAN_H
  1673. int fd = -1;
  1674. struct stat info;
  1675. const char *base = NULL;
  1676. xmlParserInputBufferPtr input = NULL;
  1677. if (memory) {
  1678. if (stat(filename, &info) < 0)
  1679. return;
  1680. if ((fd = open(filename, O_RDONLY)) < 0)
  1681. return;
  1682. base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
  1683. if (base == (void *) MAP_FAILED)
  1684. return;
  1685. reader = xmlReaderForMemory(base, info.st_size, filename,
  1686. NULL, options);
  1687. } else
  1688. #endif
  1689. reader = xmlReaderForFile(filename, NULL, options);
  1690. #ifdef LIBXML_PATTERN_ENABLED
  1691. if (pattern != NULL) {
  1692. patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
  1693. if (patternc == NULL) {
  1694. xmlGenericError(xmlGenericErrorContext,
  1695. "Pattern %s failed to compile\n", pattern);
  1696. progresult = XMLLINT_ERR_SCHEMAPAT;
  1697. pattern = NULL;
  1698. }
  1699. }
  1700. if (patternc != NULL) {
  1701. patstream = xmlPatternGetStreamCtxt(patternc);
  1702. if (patstream != NULL) {
  1703. ret = xmlStreamPush(patstream, NULL, NULL);
  1704. if (ret < 0) {
  1705. fprintf(stderr, "xmlStreamPush() failure\n");
  1706. xmlFreeStreamCtxt(patstream);
  1707. patstream = NULL;
  1708. }
  1709. }
  1710. }
  1711. #endif
  1712. if (reader != NULL) {
  1713. #ifdef LIBXML_VALID_ENABLED
  1714. if (valid)
  1715. xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
  1716. else
  1717. #endif /* LIBXML_VALID_ENABLED */
  1718. xmlTextReaderSetParserProp(reader, XML_PARSER_LOADDTD, 1);
  1719. #ifdef LIBXML_SCHEMAS_ENABLED
  1720. if (relaxng != NULL) {
  1721. if ((timing) && (!repeat)) {
  1722. startTimer();
  1723. }
  1724. ret = xmlTextReaderRelaxNGValidate(reader, relaxng);
  1725. if (ret < 0) {
  1726. xmlGenericError(xmlGenericErrorContext,
  1727. "Relax-NG schema %s failed to compile\n", relaxng);
  1728. progresult = XMLLINT_ERR_SCHEMACOMP;
  1729. relaxng = NULL;
  1730. }
  1731. if ((timing) && (!repeat)) {
  1732. endTimer("Compiling the schemas");
  1733. }
  1734. }
  1735. if (schema != NULL) {
  1736. if ((timing) && (!repeat)) {
  1737. startTimer();
  1738. }
  1739. ret = xmlTextReaderSchemaValidate(reader, schema);
  1740. if (ret < 0) {
  1741. xmlGenericError(xmlGenericErrorContext,
  1742. "XSD schema %s failed to compile\n", schema);
  1743. progresult = XMLLINT_ERR_SCHEMACOMP;
  1744. schema = NULL;
  1745. }
  1746. if ((timing) && (!repeat)) {
  1747. endTimer("Compiling the schemas");
  1748. }
  1749. }
  1750. #endif
  1751. /*
  1752. * Process all nodes in sequence
  1753. */
  1754. if ((timing) && (!repeat)) {
  1755. startTimer();
  1756. }
  1757. ret = xmlTextReaderRead(reader);
  1758. while (ret == 1) {
  1759. if ((debug)
  1760. #ifdef LIBXML_PATTERN_ENABLED
  1761. || (patternc)
  1762. #endif
  1763. )
  1764. processNode(reader);
  1765. ret = xmlTextReaderRead(reader);
  1766. }
  1767. if ((timing) && (!repeat)) {
  1768. #ifdef LIBXML_SCHEMAS_ENABLED
  1769. if (relaxng != NULL)
  1770. endTimer("Parsing and validating");
  1771. else
  1772. #endif
  1773. #ifdef LIBXML_VALID_ENABLED
  1774. if (valid)
  1775. endTimer("Parsing and validating");
  1776. else
  1777. #endif
  1778. endTimer("Parsing");
  1779. }
  1780. #ifdef LIBXML_VALID_ENABLED
  1781. if (valid) {
  1782. if (xmlTextReaderIsValid(reader) != 1) {
  1783. xmlGenericError(xmlGenericErrorContext,
  1784. "Document %s does not validate\n", filename);
  1785. progresult = XMLLINT_ERR_VALID;
  1786. }
  1787. }
  1788. #endif /* LIBXML_VALID_ENABLED */
  1789. #ifdef LIBXML_SCHEMAS_ENABLED
  1790. if ((relaxng != NULL) || (schema != NULL)) {
  1791. if (xmlTextReaderIsValid(reader) != 1) {
  1792. fprintf(stderr, "%s fails to validate\n", filename);
  1793. progresult = XMLLINT_ERR_VALID;
  1794. } else {
  1795. fprintf(stderr, "%s validates\n", filename);
  1796. }
  1797. }
  1798. #endif
  1799. /*
  1800. * Done, cleanup and status
  1801. */
  1802. xmlFreeTextReader(reader);
  1803. if (ret != 0) {
  1804. fprintf(stderr, "%s : failed to parse\n", filename);
  1805. progresult = XMLLINT_ERR_UNCLASS;
  1806. }
  1807. } else {
  1808. fprintf(stderr, "Unable to open %s\n", filename);
  1809. progresult = XMLLINT_ERR_UNCLASS;
  1810. }
  1811. #ifdef LIBXML_PATTERN_ENABLED
  1812. if (patstream != NULL) {
  1813. xmlFreeStreamCtxt(patstream);
  1814. patstream = NULL;
  1815. }
  1816. #endif
  1817. #ifdef HAVE_SYS_MMAN_H
  1818. if (memory) {
  1819. xmlFreeParserInputBuffer(input);
  1820. munmap((char *) base, info.st_size);
  1821. close(fd);
  1822. }
  1823. #endif
  1824. }
  1825. static void walkDoc(xmlDocPtr doc) {
  1826. xmlTextReaderPtr reader;
  1827. int ret;
  1828. #ifdef LIBXML_PATTERN_ENABLED
  1829. xmlNodePtr root;
  1830. const xmlChar *namespaces[22];
  1831. int i;
  1832. xmlNsPtr ns;
  1833. root = xmlDocGetRootElement(doc);
  1834. for (ns = root->nsDef, i = 0;ns != NULL && i < 20;ns=ns->next) {
  1835. namespaces[i++] = ns->href;
  1836. namespaces[i++] = ns->prefix;
  1837. }
  1838. namespaces[i++] = NULL;
  1839. namespaces[i] = NULL;
  1840. if (pattern != NULL) {
  1841. patternc = xmlPatterncompile((const xmlChar *) pattern, doc->dict,
  1842. 0, &namespaces[0]);
  1843. if (patternc == NULL) {
  1844. xmlGenericError(xmlGenericErrorContext,
  1845. "Pattern %s failed to compile\n", pattern);
  1846. progresult = XMLLINT_ERR_SCHEMAPAT;
  1847. pattern = NULL;
  1848. }
  1849. }
  1850. if (patternc != NULL) {
  1851. patstream = xmlPatternGetStreamCtxt(patternc);
  1852. if (patstream != NULL) {
  1853. ret = xmlStreamPush(patstream, NULL, NULL);
  1854. if (ret < 0) {
  1855. fprintf(stderr, "xmlStreamPush() failure\n");
  1856. xmlFreeStreamCtxt(patstream);
  1857. patstream = NULL;
  1858. }
  1859. }
  1860. }
  1861. #endif /* LIBXML_PATTERN_ENABLED */
  1862. reader = xmlReaderWalker(doc);
  1863. if (reader != NULL) {
  1864. if ((timing) && (!repeat)) {
  1865. startTimer();
  1866. }
  1867. ret = xmlTextReaderRead(reader);
  1868. while (ret == 1) {
  1869. if ((debug)
  1870. #ifdef LIBXML_PATTERN_ENABLED
  1871. || (patternc)
  1872. #endif
  1873. )
  1874. processNode(reader);
  1875. ret = xmlTextReaderRead(reader);
  1876. }
  1877. if ((timing) && (!repeat)) {
  1878. endTimer("walking through the doc");
  1879. }
  1880. xmlFreeTextReader(reader);
  1881. if (ret != 0) {
  1882. fprintf(stderr, "failed to walk through the doc\n");
  1883. progresult = XMLLINT_ERR_UNCLASS;
  1884. }
  1885. } else {
  1886. fprintf(stderr, "Failed to crate a reader from the document\n");
  1887. progresult = XMLLINT_ERR_UNCLASS;
  1888. }
  1889. #ifdef LIBXML_PATTERN_ENABLED
  1890. if (patstream != NULL) {
  1891. xmlFreeStreamCtxt(patstream);
  1892. patstream = NULL;
  1893. }
  1894. #endif
  1895. }
  1896. #endif /* LIBXML_READER_ENABLED */
  1897. /************************************************************************
  1898. * *
  1899. * Tree Test processing *
  1900. * *
  1901. ************************************************************************/
  1902. static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
  1903. xmlDocPtr doc = NULL;
  1904. #ifdef LIBXML_TREE_ENABLED
  1905. xmlDocPtr tmp;
  1906. #endif /* LIBXML_TREE_ENABLED */
  1907. if ((timing) && (!repeat))
  1908. startTimer();
  1909. #ifdef LIBXML_TREE_ENABLED
  1910. if (filename == NULL) {
  1911. if (generate) {
  1912. xmlNodePtr n;
  1913. doc = xmlNewDoc(BAD_CAST "1.0");
  1914. n = xmlNewDocNode(doc, NULL, BAD_CAST "info", NULL);
  1915. xmlNodeSetContent(n, BAD_CAST "abc");
  1916. xmlDocSetRootElement(doc, n);
  1917. }
  1918. }
  1919. #endif /* LIBXML_TREE_ENABLED */
  1920. #ifdef LIBXML_HTML_ENABLED
  1921. #ifdef LIBXML_PUSH_ENABLED
  1922. else if ((html) && (push)) {
  1923. FILE *f;
  1924. #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
  1925. f = fopen(filename, "rb");
  1926. #else
  1927. f = fopen(filename, "r");
  1928. #endif
  1929. if (f != NULL) {
  1930. int res, size = 3;
  1931. char chars[4096];
  1932. htmlParserCtxtPtr ctxt;
  1933. /* if (repeat) */
  1934. size = 4096;
  1935. res = fread(chars, 1, 4, f);
  1936. if (res > 0) {
  1937. ctxt = htmlCreatePushParserCtxt(NULL, NULL,
  1938. chars, res, filename, XML_CHAR_ENCODING_NONE);
  1939. while ((res = fread(chars, 1, size, f)) > 0) {
  1940. htmlParseChunk(ctxt, chars, res, 0);
  1941. }
  1942. htmlParseChunk(ctxt, chars, 0, 1);
  1943. doc = ctxt->myDoc;
  1944. htmlFreeParserCtxt(ctxt);
  1945. }
  1946. fclose(f);
  1947. }
  1948. }
  1949. #endif /* LIBXML_PUSH_ENABLED */
  1950. #ifdef HAVE_SYS_MMAN_H
  1951. else if ((html) && (memory)) {
  1952. int fd;
  1953. struct stat info;
  1954. const char *base;
  1955. if (stat(filename, &info) < 0)
  1956. return;
  1957. if ((fd = open(filename, O_RDONLY)) < 0)
  1958. return;
  1959. base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
  1960. if (base == (void *) MAP_FAILED)
  1961. return;
  1962. doc = htmlReadMemory((char *) base, info.st_size, filename,
  1963. NULL, options);
  1964. munmap((char *) base, info.st_size);
  1965. close(fd);
  1966. }
  1967. #endif
  1968. else if (html) {
  1969. doc = htmlReadFile(filename, NULL, options);
  1970. }
  1971. #endif /* LIBXML_HTML_ENABLED */
  1972. else {
  1973. #ifdef LIBXML_PUSH_ENABLED
  1974. /*
  1975. * build an XML tree from a string;
  1976. */
  1977. if (push) {
  1978. FILE *f;
  1979. /* '-' Usually means stdin -<sven@zen.org> */
  1980. if ((filename[0] == '-') && (filename[1] == 0)) {
  1981. f = stdin;
  1982. } else {
  1983. #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
  1984. f = fopen(filename, "rb");
  1985. #else
  1986. f = fopen(filename, "r");
  1987. #endif
  1988. }
  1989. if (f != NULL) {
  1990. int ret;
  1991. int res, size = 1024;
  1992. char chars[1024];
  1993. xmlParserCtxtPtr ctxt;
  1994. /* if (repeat) size = 1024; */
  1995. res = fread(chars, 1, 4, f);
  1996. if (res > 0) {
  1997. ctxt = xmlCreatePushParserCtxt(NULL, NULL,
  1998. chars, res, filename);
  1999. xmlCtxtUseOptions(ctxt, options);
  2000. while ((res = fread(chars, 1, size, f)) > 0) {
  2001. xmlParseChunk(ctxt, chars, res, 0);
  2002. }
  2003. xmlParseChunk(ctxt, chars, 0, 1);
  2004. doc = ctxt->myDoc;
  2005. ret = ctxt->wellFormed;
  2006. xmlFreeParserCtxt(ctxt);
  2007. if (!ret) {
  2008. xmlFreeDoc(doc);
  2009. doc = NULL;
  2010. }
  2011. }
  2012. if (f != stdin)
  2013. fclose(f);
  2014. }
  2015. } else
  2016. #endif /* LIBXML_PUSH_ENABLED */
  2017. if (testIO) {
  2018. if ((filename[0] == '-') && (filename[1] == 0)) {
  2019. doc = xmlReadFd(0, NULL, NULL, options);
  2020. } else {
  2021. FILE *f;
  2022. #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
  2023. f = fopen(filename, "rb");
  2024. #else
  2025. f = fopen(filename, "r");
  2026. #endif
  2027. if (f != NULL) {
  2028. if (rectxt == NULL)
  2029. doc = xmlReadIO((xmlInputReadCallback) myRead,
  2030. (xmlInputCloseCallback) myClose, f,
  2031. filename, NULL, options);
  2032. else
  2033. doc = xmlCtxtReadIO(rectxt,
  2034. (xmlInputReadCallback) myRead,
  2035. (xmlInputCloseCallback) myClose, f,
  2036. filename, NULL, options);
  2037. } else
  2038. doc = NULL;
  2039. }
  2040. } else if (htmlout) {
  2041. xmlParserCtxtPtr ctxt;
  2042. if (rectxt == NULL)
  2043. ctxt = xmlNewParserCtxt();
  2044. else
  2045. ctxt = rectxt;
  2046. if (ctxt == NULL) {
  2047. doc = NULL;
  2048. } else {
  2049. ctxt->sax->error = xmlHTMLError;
  2050. ctxt->sax->warning = xmlHTMLWarning;
  2051. ctxt->vctxt.error = xmlHTMLValidityError;
  2052. ctxt->vctxt.warning = xmlHTMLValidityWarning;
  2053. doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
  2054. if (rectxt == NULL)
  2055. xmlFreeParserCtxt(ctxt);
  2056. }
  2057. #ifdef HAVE_SYS_MMAN_H
  2058. } else if (memory) {
  2059. int fd;
  2060. struct stat info;
  2061. const char *base;
  2062. if (stat(filename, &info) < 0)
  2063. return;
  2064. if ((fd = open(filename, O_RDONLY)) < 0)
  2065. return;
  2066. base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
  2067. if (base == (void *) MAP_FAILED)
  2068. return;
  2069. if (rectxt == NULL)
  2070. doc = xmlReadMemory((char *) base, info.st_size,
  2071. filename, NULL, options);
  2072. else
  2073. doc = xmlCtxtReadMemory(rectxt, (char *) base, info.st_size,
  2074. filename, NULL, options);
  2075. munmap((char *) base, info.st_size);
  2076. close(fd);
  2077. #endif
  2078. #ifdef LIBXML_VALID_ENABLED
  2079. } else if (valid) {
  2080. xmlParserCtxtPtr ctxt = NULL;
  2081. if (rectxt == NULL)
  2082. ctxt = xmlNewParserCtxt();
  2083. else
  2084. ctxt = rectxt;
  2085. if (ctxt == NULL) {
  2086. doc = NULL;
  2087. } else {
  2088. doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
  2089. if (ctxt->valid == 0)
  2090. progresult = XMLLINT_ERR_RDFILE;
  2091. if (rectxt == NULL)
  2092. xmlFreeParserCtxt(ctxt);
  2093. }
  2094. #endif /* LIBXML_VALID_ENABLED */
  2095. } else {
  2096. if (rectxt != NULL)
  2097. doc = xmlCtxtReadFile(rectxt, filename, NULL, options);
  2098. else {
  2099. #ifdef LIBXML_SAX1_ENABLED
  2100. if (sax1)
  2101. doc = xmlParseFile(filename);
  2102. else
  2103. #endif /* LIBXML_SAX1_ENABLED */
  2104. doc = xmlReadFile(filename, NULL, options);
  2105. }
  2106. }
  2107. }
  2108. /*
  2109. * If we don't have a document we might as well give up. Do we
  2110. * want an error message here? <sven@zen.org> */
  2111. if (doc == NULL) {
  2112. progresult = XMLLINT_ERR_UNCLASS;
  2113. return;
  2114. }
  2115. if ((timing) && (!repeat)) {
  2116. endTimer("Parsing");
  2117. }
  2118. /*
  2119. * Remove DOCTYPE nodes
  2120. */
  2121. if (dropdtd) {
  2122. xmlDtdPtr dtd;
  2123. dtd = xmlGetIntSubset(doc);
  2124. if (dtd != NULL) {
  2125. xmlUnlinkNode((xmlNodePtr)dtd);
  2126. xmlFreeDtd(dtd);
  2127. }
  2128. }
  2129. #ifdef LIBXML_XINCLUDE_ENABLED
  2130. if (xinclude) {
  2131. if ((timing) && (!repeat)) {
  2132. startTimer();
  2133. }
  2134. if (xmlXIncludeProcessFlags(doc, options) < 0)
  2135. progresult = XMLLINT_ERR_UNCLASS;
  2136. if ((timing) && (!repeat)) {
  2137. endTimer("Xinclude processing");
  2138. }
  2139. }
  2140. #endif
  2141. #ifdef LIBXML_DEBUG_ENABLED
  2142. #ifdef LIBXML_XPATH_ENABLED
  2143. /*
  2144. * shell interaction
  2145. */
  2146. if (shell) {
  2147. xmlXPathOrderDocElems(doc);
  2148. xmlShell(doc, filename, xmlShellReadline, stdout);
  2149. }
  2150. #endif
  2151. #endif
  2152. #ifdef LIBXML_TREE_ENABLED
  2153. /*
  2154. * test intermediate copy if needed.
  2155. */
  2156. if (copy) {
  2157. tmp = doc;
  2158. if (timing) {
  2159. startTimer();
  2160. }
  2161. doc = xmlCopyDoc(doc, 1);
  2162. if (timing) {
  2163. endTimer("Copying");
  2164. }
  2165. if (timing) {
  2166. startTimer();
  2167. }
  2168. xmlFreeDoc(tmp);
  2169. if (timing) {
  2170. endTimer("Freeing original");
  2171. }
  2172. }
  2173. #endif /* LIBXML_TREE_ENABLED */
  2174. #ifdef LIBXML_VALID_ENABLED
  2175. if ((insert) && (!html)) {
  2176. const xmlChar* list[256];
  2177. int nb, i;
  2178. xmlNodePtr node;
  2179. if (doc->children != NULL) {
  2180. node = doc->children;
  2181. while ((node != NULL) && (node->last == NULL)) node = node->next;
  2182. if (node != NULL) {
  2183. nb = xmlValidGetValidElements(node->last, NULL, list, 256);
  2184. if (nb < 0) {
  2185. fprintf(stderr, "could not get valid list of elements\n");
  2186. } else if (nb == 0) {
  2187. fprintf(stderr, "No element can be inserted under root\n");
  2188. } else {
  2189. fprintf(stderr, "%d element types can be inserted under root:\n",
  2190. nb);
  2191. for (i = 0;i < nb;i++) {
  2192. fprintf(stderr, "%s\n", (char *) list[i]);
  2193. }
  2194. }
  2195. }
  2196. }
  2197. }else
  2198. #endif /* LIBXML_VALID_ENABLED */
  2199. #ifdef LIBXML_READER_ENABLED
  2200. if (walker) {
  2201. walkDoc(doc);
  2202. }
  2203. #endif /* LIBXML_READER_ENABLED */
  2204. #ifdef LIBXML_OUTPUT_ENABLED
  2205. if (noout == 0) {
  2206. int ret;
  2207. /*
  2208. * print it.
  2209. */
  2210. #ifdef LIBXML_DEBUG_ENABLED
  2211. if (!debug) {
  2212. #endif
  2213. if ((timing) && (!repeat)) {
  2214. startTimer();
  2215. }
  2216. #ifdef LIBXML_HTML_ENABLED
  2217. if ((html) && (!xmlout)) {
  2218. if (compress) {
  2219. htmlSaveFile(output ? output : "-", doc);
  2220. }
  2221. else if (encoding != NULL) {
  2222. if ( format ) {
  2223. htmlSaveFileFormat(output ? output : "-", doc, encoding, 1);
  2224. }
  2225. else {
  2226. htmlSaveFileFormat(output ? output : "-", doc, encoding, 0);
  2227. }
  2228. }
  2229. else if (format) {
  2230. htmlSaveFileFormat(output ? output : "-", doc, NULL, 1);
  2231. }
  2232. else {
  2233. FILE *out;
  2234. if (output == NULL)
  2235. out = stdout;
  2236. else {
  2237. out = fopen(output,"wb");
  2238. }
  2239. if (out != NULL) {
  2240. if (htmlDocDump(out, doc) < 0)
  2241. progresult = XMLLINT_ERR_OUT;
  2242. if (output != NULL)
  2243. fclose(out);
  2244. } else {
  2245. fprintf(stderr, "failed to open %s\n", output);
  2246. progresult = XMLLINT_ERR_OUT;
  2247. }
  2248. }
  2249. if ((timing) && (!repeat)) {
  2250. endTimer("Saving");
  2251. }
  2252. } else
  2253. #endif
  2254. #ifdef LIBXML_C14N_ENABLED
  2255. if (canonical) {
  2256. xmlChar *result = NULL;
  2257. int size;
  2258. size = xmlC14NDocDumpMemory(doc, NULL, XML_C14N_1_0, NULL, 1, &result);
  2259. if (size >= 0) {
  2260. write(1, result, size);
  2261. xmlFree(result);
  2262. } else {
  2263. fprintf(stderr, "Failed to canonicalize\n");
  2264. progresult = XMLLINT_ERR_OUT;
  2265. }
  2266. } else if (canonical) {
  2267. xmlChar *result = NULL;
  2268. int size;
  2269. size = xmlC14NDocDumpMemory(doc, NULL, XML_C14N_1_1, NULL, 1, &result);
  2270. if (size >= 0) {
  2271. write(1, result, size);
  2272. xmlFree(result);
  2273. } else {
  2274. fprintf(stderr, "Failed to canonicalize\n");
  2275. progresult = XMLLINT_ERR_OUT;
  2276. }
  2277. } else
  2278. if (exc_canonical) {
  2279. xmlChar *result = NULL;
  2280. int size;
  2281. size = xmlC14NDocDumpMemory(doc, NULL, XML_C14N_EXCLUSIVE_1_0, NULL, 1, &result);
  2282. if (size >= 0) {
  2283. write(1, result, size);
  2284. xmlFree(result);
  2285. } else {
  2286. fprintf(stderr, "Failed to canonicalize\n");
  2287. progresult = XMLLINT_ERR_OUT;
  2288. }
  2289. } else
  2290. #endif
  2291. #ifdef HAVE_SYS_MMAN_H
  2292. if (memory) {
  2293. xmlChar *result;
  2294. int len;
  2295. if (encoding != NULL) {
  2296. if ( format ) {
  2297. xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
  2298. } else {
  2299. xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
  2300. }
  2301. } else {
  2302. if (format)
  2303. xmlDocDumpFormatMemory(doc, &result, &len, 1);
  2304. else
  2305. xmlDocDumpMemory(doc, &result, &len);
  2306. }
  2307. if (result == NULL) {
  2308. fprintf(stderr, "Failed to save\n");
  2309. progresult = XMLLINT_ERR_OUT;
  2310. } else {
  2311. write(1, result, len);
  2312. xmlFree(result);
  2313. }
  2314. } else
  2315. #endif /* HAVE_SYS_MMAN_H */
  2316. if (compress) {
  2317. xmlSaveFile(output ? output : "-", doc);
  2318. } else if (oldout) {
  2319. if (encoding != NULL) {
  2320. if ( format ) {
  2321. ret = xmlSaveFormatFileEnc(output ? output : "-", doc,
  2322. encoding, 1);
  2323. }
  2324. else {
  2325. ret = xmlSaveFileEnc(output ? output : "-", doc,
  2326. encoding);
  2327. }
  2328. if (ret < 0) {
  2329. fprintf(stderr, "failed save to %s\n",
  2330. output ? output : "-");
  2331. progresult = XMLLINT_ERR_OUT;
  2332. }
  2333. } else if (format) {
  2334. ret = xmlSaveFormatFile(output ? output : "-", doc, 1);
  2335. if (ret < 0) {
  2336. fprintf(stderr, "failed save to %s\n",
  2337. output ? output : "-");
  2338. progresult = XMLLINT_ERR_OUT;
  2339. }
  2340. } else {
  2341. FILE *out;
  2342. if (output == NULL)
  2343. out = stdout;
  2344. else {
  2345. out = fopen(output,"wb");
  2346. }
  2347. if (out != NULL) {
  2348. if (xmlDocDump(out, doc) < 0)
  2349. progresult = XMLLINT_ERR_OUT;
  2350. if (output != NULL)
  2351. fclose(out);
  2352. } else {
  2353. fprintf(stderr, "failed to open %s\n", output);
  2354. progresult = XMLLINT_ERR_OUT;
  2355. }
  2356. }
  2357. } else {
  2358. xmlSaveCtxtPtr ctxt;
  2359. int saveOpts = 0;
  2360. if (format)
  2361. saveOpts |= XML_SAVE_FORMAT;
  2362. if (xmlout)
  2363. saveOpts |= XML_SAVE_AS_XML;
  2364. if (output == NULL)
  2365. ctxt = xmlSaveToFd(1, encoding, saveOpts);
  2366. else
  2367. ctxt = xmlSaveToFilename(output, encoding, saveOpts);
  2368. if (ctxt != NULL) {
  2369. if (xmlSaveDoc(ctxt, doc) < 0) {
  2370. fprintf(stderr, "failed save to %s\n",
  2371. output ? output : "-");
  2372. progresult = XMLLINT_ERR_OUT;
  2373. }
  2374. xmlSaveClose(ctxt);
  2375. } else {
  2376. progresult = XMLLINT_ERR_OUT;
  2377. }
  2378. }
  2379. if ((timing) && (!repeat)) {
  2380. endTimer("Saving");
  2381. }
  2382. #ifdef LIBXML_DEBUG_ENABLED
  2383. } else {
  2384. FILE *out;
  2385. if (output == NULL)
  2386. out = stdout;
  2387. else {
  2388. out = fopen(output,"wb");
  2389. }
  2390. if (out != NULL) {
  2391. xmlDebugDumpDocument(out, doc);
  2392. if (output != NULL)
  2393. fclose(out);
  2394. } else {
  2395. fprintf(stderr, "failed to open %s\n", output);
  2396. progresult = XMLLINT_ERR_OUT;
  2397. }
  2398. }
  2399. #endif
  2400. }
  2401. #endif /* LIBXML_OUTPUT_ENABLED */
  2402. #ifdef LIBXML_VALID_ENABLED
  2403. /*
  2404. * A posteriori validation test
  2405. */
  2406. if ((dtdvalid != NULL) || (dtdvalidfpi != NULL)) {
  2407. xmlDtdPtr dtd;
  2408. if ((timing) && (!repeat)) {
  2409. startTimer();
  2410. }
  2411. if (dtdvalid != NULL)
  2412. dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
  2413. else
  2414. dtd = xmlParseDTD((const xmlChar *)dtdvalidfpi, NULL);
  2415. if ((timing) && (!repeat)) {
  2416. endTimer("Parsing DTD");
  2417. }
  2418. if (dtd == NULL) {
  2419. if (dtdvalid != NULL)
  2420. xmlGenericError(xmlGenericErrorContext,
  2421. "Could not parse DTD %s\n", dtdvalid);
  2422. else
  2423. xmlGenericError(xmlGenericErrorContext,
  2424. "Could not parse DTD %s\n", dtdvalidfpi);
  2425. progresult = XMLLINT_ERR_DTD;
  2426. } else {
  2427. xmlValidCtxtPtr cvp;
  2428. if ((cvp = xmlNewValidCtxt()) == NULL) {
  2429. xmlGenericError(xmlGenericErrorContext,
  2430. "Couldn't allocate validation context\n");
  2431. exit(-1);
  2432. }
  2433. cvp->userData = (void *) stderr;
  2434. cvp->error = (xmlValidityErrorFunc) fprintf;
  2435. cvp->warning = (xmlValidityWarningFunc) fprintf;
  2436. if ((timing) && (!repeat)) {
  2437. startTimer();
  2438. }
  2439. if (!xmlValidateDtd(cvp, doc, dtd)) {
  2440. if (dtdvalid != NULL)
  2441. xmlGenericError(xmlGenericErrorContext,
  2442. "Document %s does not validate against %s\n",
  2443. filename, dtdvalid);
  2444. else
  2445. xmlGenericError(xmlGenericErrorContext,
  2446. "Document %s does not validate against %s\n",
  2447. filename, dtdvalidfpi);
  2448. progresult = XMLLINT_ERR_VALID;
  2449. }
  2450. if ((timing) && (!repeat)) {
  2451. endTimer("Validating against DTD");
  2452. }
  2453. xmlFreeValidCtxt(cvp);
  2454. xmlFreeDtd(dtd);
  2455. }
  2456. } else if (postvalid) {
  2457. xmlValidCtxtPtr cvp;
  2458. if ((cvp = xmlNewValidCtxt()) == NULL) {
  2459. xmlGenericError(xmlGenericErrorContext,
  2460. "Couldn't allocate validation context\n");
  2461. exit(-1);
  2462. }
  2463. if ((timing) && (!repeat)) {
  2464. startTimer();
  2465. }
  2466. cvp->userData = (void *) stderr;
  2467. cvp->error = (xmlValidityErrorFunc) fprintf;
  2468. cvp->warning = (xmlValidityWarningFunc) fprintf;
  2469. if (!xmlValidateDocument(cvp, doc)) {
  2470. xmlGenericError(xmlGenericErrorContext,
  2471. "Document %s does not validate\n", filename);
  2472. progresult = XMLLINT_ERR_VALID;
  2473. }
  2474. if ((timing) && (!repeat)) {
  2475. endTimer("Validating");
  2476. }
  2477. xmlFreeValidCtxt(cvp);
  2478. }
  2479. #endif /* LIBXML_VALID_ENABLED */
  2480. #ifdef LIBXML_SCHEMATRON_ENABLED
  2481. if (wxschematron != NULL) {
  2482. xmlSchematronValidCtxtPtr ctxt;
  2483. int ret;
  2484. int flag;
  2485. if ((timing) && (!repeat)) {
  2486. startTimer();
  2487. }
  2488. if (debug)
  2489. flag = XML_SCHEMATRON_OUT_XML;
  2490. else
  2491. flag = XML_SCHEMATRON_OUT_TEXT;
  2492. if (noout)
  2493. flag |= XML_SCHEMATRON_OUT_QUIET;
  2494. ctxt = xmlSchematronNewValidCtxt(wxschematron, flag);
  2495. #if 0
  2496. xmlSchematronSetValidErrors(ctxt,
  2497. (xmlSchematronValidityErrorFunc) fprintf,
  2498. (xmlSchematronValidityWarningFunc) fprintf,
  2499. stderr);
  2500. #endif
  2501. ret = xmlSchematronValidateDoc(ctxt, doc);
  2502. if (ret == 0) {
  2503. fprintf(stderr, "%s validates\n", filename);
  2504. } else if (ret > 0) {
  2505. fprintf(stderr, "%s fails to validate\n", filename);
  2506. progresult = XMLLINT_ERR_VALID;
  2507. } else {
  2508. fprintf(stderr, "%s validation generated an internal error\n",
  2509. filename);
  2510. progresult = XMLLINT_ERR_VALID;
  2511. }
  2512. xmlSchematronFreeValidCtxt(ctxt);
  2513. if ((timing) && (!repeat)) {
  2514. endTimer("Validating");
  2515. }
  2516. }
  2517. #endif
  2518. #ifdef LIBXML_SCHEMAS_ENABLED
  2519. if (relaxngschemas != NULL) {
  2520. xmlRelaxNGValidCtxtPtr ctxt;
  2521. int ret;
  2522. if ((timing) && (!repeat)) {
  2523. startTimer();
  2524. }
  2525. ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
  2526. xmlRelaxNGSetValidErrors(ctxt,
  2527. (xmlRelaxNGValidityErrorFunc) fprintf,
  2528. (xmlRelaxNGValidityWarningFunc) fprintf,
  2529. stderr);
  2530. ret = xmlRelaxNGValidateDoc(ctxt, doc);
  2531. if (ret == 0) {
  2532. fprintf(stderr, "%s validates\n", filename);
  2533. } else if (ret > 0) {
  2534. fprintf(stderr, "%s fails to validate\n", filename);
  2535. progresult = XMLLINT_ERR_VALID;
  2536. } else {
  2537. fprintf(stderr, "%s validation generated an internal error\n",
  2538. filename);
  2539. progresult = XMLLINT_ERR_VALID;
  2540. }
  2541. xmlRelaxNGFreeValidCtxt(ctxt);
  2542. if ((timing) && (!repeat)) {
  2543. endTimer("Validating");
  2544. }
  2545. } else if (wxschemas != NULL) {
  2546. xmlSchemaValidCtxtPtr ctxt;
  2547. int ret;
  2548. if ((timing) && (!repeat)) {
  2549. startTimer();
  2550. }
  2551. ctxt = xmlSchemaNewValidCtxt(wxschemas);
  2552. xmlSchemaSetValidErrors(ctxt,
  2553. (xmlSchemaValidityErrorFunc) fprintf,
  2554. (xmlSchemaValidityWarningFunc) fprintf,
  2555. stderr);
  2556. ret = xmlSchemaValidateDoc(ctxt, doc);
  2557. if (ret == 0) {
  2558. fprintf(stderr, "%s validates\n", filename);
  2559. } else if (ret > 0) {
  2560. fprintf(stderr, "%s fails to validate\n", filename);
  2561. progresult = XMLLINT_ERR_VALID;
  2562. } else {
  2563. fprintf(stderr, "%s validation generated an internal error\n",
  2564. filename);
  2565. progresult = XMLLINT_ERR_VALID;
  2566. }
  2567. xmlSchemaFreeValidCtxt(ctxt);
  2568. if ((timing) && (!repeat)) {
  2569. endTimer("Validating");
  2570. }
  2571. }
  2572. #endif
  2573. #ifdef LIBXML_DEBUG_ENABLED
  2574. #if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED)
  2575. if ((debugent) && (!html))
  2576. xmlDebugDumpEntities(stderr, doc);
  2577. #endif
  2578. #endif
  2579. /*
  2580. * free it.
  2581. */
  2582. if ((timing) && (!repeat)) {
  2583. startTimer();
  2584. }
  2585. xmlFreeDoc(doc);
  2586. if ((timing) && (!repeat)) {
  2587. endTimer("Freeing");
  2588. }
  2589. }
  2590. /************************************************************************
  2591. * *
  2592. * Usage and Main *
  2593. * *
  2594. ************************************************************************/
  2595. static void showVersion(const char *name) {
  2596. fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion);
  2597. fprintf(stderr, " compiled with: ");
  2598. if (xmlHasFeature(XML_WITH_THREAD)) fprintf(stderr, "Threads ");
  2599. if (xmlHasFeature(XML_WITH_TREE)) fprintf(stderr, "Tree ");
  2600. if (xmlHasFeature(XML_WITH_OUTPUT)) fprintf(stderr, "Output ");
  2601. if (xmlHasFeature(XML_WITH_PUSH)) fprintf(stderr, "Push ");
  2602. if (xmlHasFeature(XML_WITH_READER)) fprintf(stderr, "Reader ");
  2603. if (xmlHasFeature(XML_WITH_PATTERN)) fprintf(stderr, "Patterns ");
  2604. if (xmlHasFeature(XML_WITH_WRITER)) fprintf(stderr, "Writer ");
  2605. if (xmlHasFeature(XML_WITH_SAX1)) fprintf(stderr, "SAXv1 ");
  2606. if (xmlHasFeature(XML_WITH_FTP)) fprintf(stderr, "FTP ");
  2607. if (xmlHasFeature(XML_WITH_HTTP)) fprintf(stderr, "HTTP ");
  2608. if (xmlHasFeature(XML_WITH_VALID)) fprintf(stderr, "DTDValid ");
  2609. if (xmlHasFeature(XML_WITH_HTML)) fprintf(stderr, "HTML ");
  2610. if (xmlHasFeature(XML_WITH_LEGACY)) fprintf(stderr, "Legacy ");
  2611. if (xmlHasFeature(XML_WITH_C14N)) fprintf(stderr, "C14N ");
  2612. if (xmlHasFeature(XML_WITH_CATALOG)) fprintf(stderr, "Catalog ");
  2613. if (xmlHasFeature(XML_WITH_XPATH)) fprintf(stderr, "XPath ");
  2614. if (xmlHasFeature(XML_WITH_XPTR)) fprintf(stderr, "XPointer ");
  2615. if (xmlHasFeature(XML_WITH_XINCLUDE)) fprintf(stderr, "XInclude ");
  2616. if (xmlHasFeature(XML_WITH_ICONV)) fprintf(stderr, "Iconv ");
  2617. if (xmlHasFeature(XML_WITH_ISO8859X)) fprintf(stderr, "ISO8859X ");
  2618. if (xmlHasFeature(XML_WITH_UNICODE)) fprintf(stderr, "Unicode ");
  2619. if (xmlHasFeature(XML_WITH_REGEXP)) fprintf(stderr, "Regexps ");
  2620. if (xmlHasFeature(XML_WITH_AUTOMATA)) fprintf(stderr, "Automata ");
  2621. if (xmlHasFeature(XML_WITH_EXPR)) fprintf(stderr, "Expr ");
  2622. if (xmlHasFeature(XML_WITH_SCHEMAS)) fprintf(stderr, "Schemas ");
  2623. if (xmlHasFeature(XML_WITH_SCHEMATRON)) fprintf(stderr, "Schematron ");
  2624. if (xmlHasFeature(XML_WITH_MODULES)) fprintf(stderr, "Modules ");
  2625. if (xmlHasFeature(XML_WITH_DEBUG)) fprintf(stderr, "Debug ");
  2626. if (xmlHasFeature(XML_WITH_DEBUG_MEM)) fprintf(stderr, "MemDebug ");
  2627. if (xmlHasFeature(XML_WITH_DEBUG_RUN)) fprintf(stderr, "RunDebug ");
  2628. if (xmlHasFeature(XML_WITH_ZLIB)) fprintf(stderr, "Zlib ");
  2629. fprintf(stderr, "\n");
  2630. }
  2631. static void usage(const char *name) {
  2632. printf("Usage : %s [options] XMLfiles ...\n", name);
  2633. #ifdef LIBXML_OUTPUT_ENABLED
  2634. printf("\tParse the XML files and output the result of the parsing\n");
  2635. #else
  2636. printf("\tParse the XML files\n");
  2637. #endif /* LIBXML_OUTPUT_ENABLED */
  2638. printf("\t--version : display the version of the XML library used\n");
  2639. #ifdef LIBXML_DEBUG_ENABLED
  2640. printf("\t--debug : dump a debug tree of the in-memory document\n");
  2641. printf("\t--shell : run a navigating shell\n");
  2642. printf("\t--debugent : debug the entities defined in the document\n");
  2643. #else
  2644. #ifdef LIBXML_READER_ENABLED
  2645. printf("\t--debug : dump the nodes content when using --stream\n");
  2646. #endif /* LIBXML_READER_ENABLED */
  2647. #endif
  2648. #ifdef LIBXML_TREE_ENABLED
  2649. printf("\t--copy : used to test the internal copy implementation\n");
  2650. #endif /* LIBXML_TREE_ENABLED */
  2651. printf("\t--recover : output what was parsable on broken XML documents\n");
  2652. printf("\t--huge : remove any internal arbitrary parser limits\n");
  2653. printf("\t--noent : substitute entity references by their value\n");
  2654. printf("\t--noout : don't output the result tree\n");
  2655. printf("\t--path 'paths': provide a set of paths for resources\n");
  2656. printf("\t--load-trace : print trace of all external entites loaded\n");
  2657. printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
  2658. printf("\t--nocompact : do not generate compact text nodes\n");
  2659. printf("\t--htmlout : output results as HTML\n");
  2660. printf("\t--nowrap : do not put HTML doc wrapper\n");
  2661. #ifdef LIBXML_VALID_ENABLED
  2662. printf("\t--valid : validate the document in addition to std well-formed check\n");
  2663. printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
  2664. printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
  2665. printf("\t--dtdvalidfpi FPI : same but name the DTD with a Public Identifier\n");
  2666. #endif /* LIBXML_VALID_ENABLED */
  2667. printf("\t--timing : print some timings\n");
  2668. printf("\t--output file or -o file: save to a given file\n");
  2669. printf("\t--repeat : repeat 100 times, for timing or profiling\n");
  2670. printf("\t--insert : ad-hoc test for valid insertions\n");
  2671. #ifdef LIBXML_OUTPUT_ENABLED
  2672. #ifdef HAVE_ZLIB_H
  2673. printf("\t--compress : turn on gzip compression of output\n");
  2674. #endif
  2675. #endif /* LIBXML_OUTPUT_ENABLED */
  2676. #ifdef LIBXML_HTML_ENABLED
  2677. printf("\t--html : use the HTML parser\n");
  2678. printf("\t--xmlout : force to use the XML serializer when using --html\n");
  2679. #endif
  2680. #ifdef LIBXML_PUSH_ENABLED
  2681. printf("\t--push : use the push mode of the parser\n");
  2682. #endif /* LIBXML_PUSH_ENABLED */
  2683. #ifdef HAVE_SYS_MMAN_H
  2684. printf("\t--memory : parse from memory\n");
  2685. #endif
  2686. printf("\t--maxmem nbbytes : limits memory allocation to nbbytes bytes\n");
  2687. printf("\t--nowarning : do not emit warnings from parser/validator\n");
  2688. printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
  2689. printf("\t--nocdata : replace cdata section with text nodes\n");
  2690. #ifdef LIBXML_OUTPUT_ENABLED
  2691. printf("\t--format : reformat/reindent the input\n");
  2692. printf("\t--encode encoding : output in the given encoding\n");
  2693. printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
  2694. #endif /* LIBXML_OUTPUT_ENABLED */
  2695. printf("\t--c14n : save in W3C canonical format v1.0 (with comments)\n");
  2696. printf("\t--c14n11 : save in W3C canonical format v1.1 (with comments)\n");
  2697. printf("\t--exc-c14n : save in W3C exclusive canonical format (with comments)\n");
  2698. #ifdef LIBXML_C14N_ENABLED
  2699. #endif /* LIBXML_C14N_ENABLED */
  2700. printf("\t--nsclean : remove redundant namespace declarations\n");
  2701. printf("\t--testIO : test user I/O support\n");
  2702. #ifdef LIBXML_CATALOG_ENABLED
  2703. printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
  2704. printf("\t otherwise XML Catalogs starting from \n");
  2705. printf("\t %s are activated by default\n", XML_XML_DEFAULT_CATALOG);
  2706. printf("\t--nocatalogs: deactivate all catalogs\n");
  2707. #endif
  2708. printf("\t--auto : generate a small doc on the fly\n");
  2709. #ifdef LIBXML_XINCLUDE_ENABLED
  2710. printf("\t--xinclude : do XInclude processing\n");
  2711. printf("\t--noxincludenode : same but do not generate XInclude nodes\n");
  2712. printf("\t--nofixup-base-uris : do not fixup xml:base uris\n");
  2713. #endif
  2714. printf("\t--loaddtd : fetch external DTD\n");
  2715. printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
  2716. #ifdef LIBXML_READER_ENABLED
  2717. printf("\t--stream : use the streaming interface to process very large files\n");
  2718. printf("\t--walker : create a reader and walk though the resulting doc\n");
  2719. #endif /* LIBXML_READER_ENABLED */
  2720. #ifdef LIBXML_PATTERN_ENABLED
  2721. printf("\t--pattern pattern_value : test the pattern support\n");
  2722. #endif
  2723. printf("\t--chkregister : verify the node registration code\n");
  2724. #ifdef LIBXML_SCHEMAS_ENABLED
  2725. printf("\t--relaxng schema : do RelaxNG validation against the schema\n");
  2726. printf("\t--schema schema : do validation against the WXS schema\n");
  2727. #endif
  2728. #ifdef LIBXML_SCHEMATRON_ENABLED
  2729. printf("\t--schematron schema : do validation against a schematron\n");
  2730. #endif
  2731. #ifdef LIBXML_SAX1_ENABLED
  2732. printf("\t--sax1: use the old SAX1 interfaces for processing\n");
  2733. #endif
  2734. printf("\t--sax: do not build a tree but work just at the SAX level\n");
  2735. printf("\t--oldxml10: use XML-1.0 parsing rules before the 5th edition\n");
  2736. printf("\nLibxml project home page: http://xmlsoft.org/\n");
  2737. printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
  2738. }
  2739. static void registerNode(xmlNodePtr node)
  2740. {
  2741. node->_private = malloc(sizeof(long));
  2742. *(long*)node->_private = (long) 0x81726354;
  2743. nbregister++;
  2744. }
  2745. static void deregisterNode(xmlNodePtr node)
  2746. {
  2747. assert(node->_private != NULL);
  2748. assert(*(long*)node->_private == (long) 0x81726354);
  2749. free(node->_private);
  2750. nbregister--;
  2751. }
  2752. int
  2753. main(int argc, char **argv) {
  2754. int i, acount;
  2755. int files = 0;
  2756. int version = 0;
  2757. const char* indent;
  2758. if (argc <= 1) {
  2759. usage(argv[0]);
  2760. return(1);
  2761. }
  2762. LIBXML_TEST_VERSION
  2763. for (i = 1; i < argc ; i++) {
  2764. if (!strcmp(argv[i], "-"))
  2765. break;
  2766. if (argv[i][0] != '-')
  2767. continue;
  2768. if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
  2769. debug++;
  2770. else
  2771. #ifdef LIBXML_DEBUG_ENABLED
  2772. if ((!strcmp(argv[i], "-shell")) ||
  2773. (!strcmp(argv[i], "--shell"))) {
  2774. shell++;
  2775. noout = 1;
  2776. } else
  2777. #endif
  2778. #ifdef LIBXML_TREE_ENABLED
  2779. if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
  2780. copy++;
  2781. else
  2782. #endif /* LIBXML_TREE_ENABLED */
  2783. if ((!strcmp(argv[i], "-recover")) ||
  2784. (!strcmp(argv[i], "--recover"))) {
  2785. recovery++;
  2786. options |= XML_PARSE_RECOVER;
  2787. } else if ((!strcmp(argv[i], "-huge")) ||
  2788. (!strcmp(argv[i], "--huge"))) {
  2789. options |= XML_PARSE_HUGE;
  2790. } else if ((!strcmp(argv[i], "-noent")) ||
  2791. (!strcmp(argv[i], "--noent"))) {
  2792. noent++;
  2793. options |= XML_PARSE_NOENT;
  2794. } else if ((!strcmp(argv[i], "-nsclean")) ||
  2795. (!strcmp(argv[i], "--nsclean"))) {
  2796. options |= XML_PARSE_NSCLEAN;
  2797. } else if ((!strcmp(argv[i], "-nocdata")) ||
  2798. (!strcmp(argv[i], "--nocdata"))) {
  2799. options |= XML_PARSE_NOCDATA;
  2800. } else if ((!strcmp(argv[i], "-nodict")) ||
  2801. (!strcmp(argv[i], "--nodict"))) {
  2802. options |= XML_PARSE_NODICT;
  2803. } else if ((!strcmp(argv[i], "-version")) ||
  2804. (!strcmp(argv[i], "--version"))) {
  2805. showVersion(argv[0]);
  2806. version = 1;
  2807. } else if ((!strcmp(argv[i], "-noout")) ||
  2808. (!strcmp(argv[i], "--noout")))
  2809. noout++;
  2810. #ifdef LIBXML_OUTPUT_ENABLED
  2811. else if ((!strcmp(argv[i], "-o")) ||
  2812. (!strcmp(argv[i], "-output")) ||
  2813. (!strcmp(argv[i], "--output"))) {
  2814. i++;
  2815. output = argv[i];
  2816. }
  2817. #endif /* LIBXML_OUTPUT_ENABLED */
  2818. else if ((!strcmp(argv[i], "-htmlout")) ||
  2819. (!strcmp(argv[i], "--htmlout")))
  2820. htmlout++;
  2821. else if ((!strcmp(argv[i], "-nowrap")) ||
  2822. (!strcmp(argv[i], "--nowrap")))
  2823. nowrap++;
  2824. #ifdef LIBXML_HTML_ENABLED
  2825. else if ((!strcmp(argv[i], "-html")) ||
  2826. (!strcmp(argv[i], "--html"))) {
  2827. html++;
  2828. }
  2829. else if ((!strcmp(argv[i], "-xmlout")) ||
  2830. (!strcmp(argv[i], "--xmlout"))) {
  2831. xmlout++;
  2832. }
  2833. #endif /* LIBXML_HTML_ENABLED */
  2834. else if ((!strcmp(argv[i], "-loaddtd")) ||
  2835. (!strcmp(argv[i], "--loaddtd"))) {
  2836. loaddtd++;
  2837. options |= XML_PARSE_DTDLOAD;
  2838. } else if ((!strcmp(argv[i], "-dtdattr")) ||
  2839. (!strcmp(argv[i], "--dtdattr"))) {
  2840. loaddtd++;
  2841. dtdattrs++;
  2842. options |= XML_PARSE_DTDATTR;
  2843. }
  2844. #ifdef LIBXML_VALID_ENABLED
  2845. else if ((!strcmp(argv[i], "-valid")) ||
  2846. (!strcmp(argv[i], "--valid"))) {
  2847. valid++;
  2848. options |= XML_PARSE_DTDVALID;
  2849. } else if ((!strcmp(argv[i], "-postvalid")) ||
  2850. (!strcmp(argv[i], "--postvalid"))) {
  2851. postvalid++;
  2852. loaddtd++;
  2853. options |= XML_PARSE_DTDLOAD;
  2854. } else if ((!strcmp(argv[i], "-dtdvalid")) ||
  2855. (!strcmp(argv[i], "--dtdvalid"))) {
  2856. i++;
  2857. dtdvalid = argv[i];
  2858. loaddtd++;
  2859. options |= XML_PARSE_DTDLOAD;
  2860. } else if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
  2861. (!strcmp(argv[i], "--dtdvalidfpi"))) {
  2862. i++;
  2863. dtdvalidfpi = argv[i];
  2864. loaddtd++;
  2865. options |= XML_PARSE_DTDLOAD;
  2866. }
  2867. #endif /* LIBXML_VALID_ENABLED */
  2868. else if ((!strcmp(argv[i], "-dropdtd")) ||
  2869. (!strcmp(argv[i], "--dropdtd")))
  2870. dropdtd++;
  2871. else if ((!strcmp(argv[i], "-insert")) ||
  2872. (!strcmp(argv[i], "--insert")))
  2873. insert++;
  2874. else if ((!strcmp(argv[i], "-timing")) ||
  2875. (!strcmp(argv[i], "--timing")))
  2876. timing++;
  2877. else if ((!strcmp(argv[i], "-auto")) ||
  2878. (!strcmp(argv[i], "--auto")))
  2879. generate++;
  2880. else if ((!strcmp(argv[i], "-repeat")) ||
  2881. (!strcmp(argv[i], "--repeat"))) {
  2882. if (repeat)
  2883. repeat *= 10;
  2884. else
  2885. repeat = 100;
  2886. }
  2887. #ifdef LIBXML_PUSH_ENABLED
  2888. else if ((!strcmp(argv[i], "-push")) ||
  2889. (!strcmp(argv[i], "--push")))
  2890. push++;
  2891. #endif /* LIBXML_PUSH_ENABLED */
  2892. #ifdef HAVE_SYS_MMAN_H
  2893. else if ((!strcmp(argv[i], "-memory")) ||
  2894. (!strcmp(argv[i], "--memory")))
  2895. memory++;
  2896. #endif
  2897. else if ((!strcmp(argv[i], "-testIO")) ||
  2898. (!strcmp(argv[i], "--testIO")))
  2899. testIO++;
  2900. #ifdef LIBXML_XINCLUDE_ENABLED
  2901. else if ((!strcmp(argv[i], "-xinclude")) ||
  2902. (!strcmp(argv[i], "--xinclude"))) {
  2903. xinclude++;
  2904. options |= XML_PARSE_XINCLUDE;
  2905. }
  2906. else if ((!strcmp(argv[i], "-noxincludenode")) ||
  2907. (!strcmp(argv[i], "--noxincludenode"))) {
  2908. xinclude++;
  2909. options |= XML_PARSE_XINCLUDE;
  2910. options |= XML_PARSE_NOXINCNODE;
  2911. }
  2912. else if ((!strcmp(argv[i], "-nofixup-base-uris")) ||
  2913. (!strcmp(argv[i], "--nofixup-base-uris"))) {
  2914. xinclude++;
  2915. options |= XML_PARSE_XINCLUDE;
  2916. options |= XML_PARSE_NOBASEFIX;
  2917. }
  2918. #endif
  2919. #ifdef LIBXML_OUTPUT_ENABLED
  2920. #ifdef HAVE_ZLIB_H
  2921. else if ((!strcmp(argv[i], "-compress")) ||
  2922. (!strcmp(argv[i], "--compress"))) {
  2923. compress++;
  2924. xmlSetCompressMode(9);
  2925. }
  2926. #endif
  2927. #endif /* LIBXML_OUTPUT_ENABLED */
  2928. else if ((!strcmp(argv[i], "-nowarning")) ||
  2929. (!strcmp(argv[i], "--nowarning"))) {
  2930. xmlGetWarningsDefaultValue = 0;
  2931. xmlPedanticParserDefault(0);
  2932. options |= XML_PARSE_NOWARNING;
  2933. }
  2934. else if ((!strcmp(argv[i], "-pedantic")) ||
  2935. (!strcmp(argv[i], "--pedantic"))) {
  2936. xmlGetWarningsDefaultValue = 1;
  2937. xmlPedanticParserDefault(1);
  2938. options |= XML_PARSE_PEDANTIC;
  2939. }
  2940. #ifdef LIBXML_DEBUG_ENABLED
  2941. else if ((!strcmp(argv[i], "-debugent")) ||
  2942. (!strcmp(argv[i], "--debugent"))) {
  2943. debugent++;
  2944. xmlParserDebugEntities = 1;
  2945. }
  2946. #endif
  2947. #ifdef LIBXML_C14N_ENABLED
  2948. else if ((!strcmp(argv[i], "-c14n")) ||
  2949. (!strcmp(argv[i], "--c14n"))) {
  2950. canonical++;
  2951. options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
  2952. }
  2953. else if ((!strcmp(argv[i], "-c14n11")) ||
  2954. (!strcmp(argv[i], "--c14n11"))) {
  2955. canonical_11++;
  2956. options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
  2957. }
  2958. else if ((!strcmp(argv[i], "-exc-c14n")) ||
  2959. (!strcmp(argv[i], "--exc-c14n"))) {
  2960. exc_canonical++;
  2961. options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
  2962. }
  2963. #endif
  2964. #ifdef LIBXML_CATALOG_ENABLED
  2965. else if ((!strcmp(argv[i], "-catalogs")) ||
  2966. (!strcmp(argv[i], "--catalogs"))) {
  2967. catalogs++;
  2968. } else if ((!strcmp(argv[i], "-nocatalogs")) ||
  2969. (!strcmp(argv[i], "--nocatalogs"))) {
  2970. nocatalogs++;
  2971. }
  2972. #endif
  2973. else if ((!strcmp(argv[i], "-encode")) ||
  2974. (!strcmp(argv[i], "--encode"))) {
  2975. i++;
  2976. encoding = argv[i];
  2977. /*
  2978. * OK it's for testing purposes
  2979. */
  2980. xmlAddEncodingAlias("UTF-8", "DVEnc");
  2981. }
  2982. else if ((!strcmp(argv[i], "-noblanks")) ||
  2983. (!strcmp(argv[i], "--noblanks"))) {
  2984. noblanks++;
  2985. xmlKeepBlanksDefault(0);
  2986. }
  2987. else if ((!strcmp(argv[i], "-maxmem")) ||
  2988. (!strcmp(argv[i], "--maxmem"))) {
  2989. i++;
  2990. if (sscanf(argv[i], "%d", &maxmem) == 1) {
  2991. xmlMemSetup(myFreeFunc, myMallocFunc, myReallocFunc,
  2992. myStrdupFunc);
  2993. } else {
  2994. maxmem = 0;
  2995. }
  2996. }
  2997. else if ((!strcmp(argv[i], "-format")) ||
  2998. (!strcmp(argv[i], "--format"))) {
  2999. noblanks++;
  3000. #ifdef LIBXML_OUTPUT_ENABLED
  3001. format++;
  3002. #endif /* LIBXML_OUTPUT_ENABLED */
  3003. xmlKeepBlanksDefault(0);
  3004. }
  3005. #ifdef LIBXML_READER_ENABLED
  3006. else if ((!strcmp(argv[i], "-stream")) ||
  3007. (!strcmp(argv[i], "--stream"))) {
  3008. stream++;
  3009. }
  3010. else if ((!strcmp(argv[i], "-walker")) ||
  3011. (!strcmp(argv[i], "--walker"))) {
  3012. walker++;
  3013. noout++;
  3014. }
  3015. #endif /* LIBXML_READER_ENABLED */
  3016. #ifdef LIBXML_SAX1_ENABLED
  3017. else if ((!strcmp(argv[i], "-sax1")) ||
  3018. (!strcmp(argv[i], "--sax1"))) {
  3019. sax1++;
  3020. options |= XML_PARSE_SAX1;
  3021. }
  3022. #endif /* LIBXML_SAX1_ENABLED */
  3023. else if ((!strcmp(argv[i], "-sax")) ||
  3024. (!strcmp(argv[i], "--sax"))) {
  3025. sax++;
  3026. }
  3027. else if ((!strcmp(argv[i], "-chkregister")) ||
  3028. (!strcmp(argv[i], "--chkregister"))) {
  3029. chkregister++;
  3030. #ifdef LIBXML_SCHEMAS_ENABLED
  3031. } else if ((!strcmp(argv[i], "-relaxng")) ||
  3032. (!strcmp(argv[i], "--relaxng"))) {
  3033. i++;
  3034. relaxng = argv[i];
  3035. noent++;
  3036. options |= XML_PARSE_NOENT;
  3037. } else if ((!strcmp(argv[i], "-schema")) ||
  3038. (!strcmp(argv[i], "--schema"))) {
  3039. i++;
  3040. schema = argv[i];
  3041. noent++;
  3042. #endif
  3043. #ifdef LIBXML_SCHEMATRON_ENABLED
  3044. } else if ((!strcmp(argv[i], "-schematron")) ||
  3045. (!strcmp(argv[i], "--schematron"))) {
  3046. i++;
  3047. schematron = argv[i];
  3048. noent++;
  3049. #endif
  3050. } else if ((!strcmp(argv[i], "-nonet")) ||
  3051. (!strcmp(argv[i], "--nonet"))) {
  3052. options |= XML_PARSE_NONET;
  3053. xmlSetExternalEntityLoader(xmlNoNetExternalEntityLoader);
  3054. } else if ((!strcmp(argv[i], "-nocompact")) ||
  3055. (!strcmp(argv[i], "--nocompact"))) {
  3056. options &= ~XML_PARSE_COMPACT;
  3057. } else if ((!strcmp(argv[i], "-load-trace")) ||
  3058. (!strcmp(argv[i], "--load-trace"))) {
  3059. load_trace++;
  3060. } else if ((!strcmp(argv[i], "-path")) ||
  3061. (!strcmp(argv[i], "--path"))) {
  3062. i++;
  3063. parsePath(BAD_CAST argv[i]);
  3064. #ifdef LIBXML_PATTERN_ENABLED
  3065. } else if ((!strcmp(argv[i], "-pattern")) ||
  3066. (!strcmp(argv[i], "--pattern"))) {
  3067. i++;
  3068. pattern = argv[i];
  3069. #endif
  3070. } else if ((!strcmp(argv[i], "-oldxml10")) ||
  3071. (!strcmp(argv[i], "--oldxml10"))) {
  3072. oldxml10++;
  3073. options |= XML_PARSE_OLD10;
  3074. } else {
  3075. fprintf(stderr, "Unknown option %s\n", argv[i]);
  3076. usage(argv[0]);
  3077. return(1);
  3078. }
  3079. }
  3080. #ifdef LIBXML_CATALOG_ENABLED
  3081. if (nocatalogs == 0) {
  3082. if (catalogs) {
  3083. const char *catal;
  3084. catal = getenv("SGML_CATALOG_FILES");
  3085. if (catal != NULL) {
  3086. xmlLoadCatalogs(catal);
  3087. } else {
  3088. fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
  3089. }
  3090. }
  3091. }
  3092. #endif
  3093. #ifdef LIBXML_SAX1_ENABLED
  3094. if (sax1)
  3095. xmlSAXDefaultVersion(1);
  3096. else
  3097. xmlSAXDefaultVersion(2);
  3098. #endif /* LIBXML_SAX1_ENABLED */
  3099. if (chkregister) {
  3100. xmlRegisterNodeDefault(registerNode);
  3101. xmlDeregisterNodeDefault(deregisterNode);
  3102. }
  3103. indent = getenv("XMLLINT_INDENT");
  3104. if(indent != NULL) {
  3105. xmlTreeIndentString = indent;
  3106. }
  3107. defaultEntityLoader = xmlGetExternalEntityLoader();
  3108. xmlSetExternalEntityLoader(xmllintExternalEntityLoader);
  3109. xmlLineNumbersDefault(1);
  3110. if (loaddtd != 0)
  3111. xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
  3112. if (dtdattrs)
  3113. xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
  3114. if (noent != 0) xmlSubstituteEntitiesDefault(1);
  3115. #ifdef LIBXML_VALID_ENABLED
  3116. if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
  3117. #endif /* LIBXML_VALID_ENABLED */
  3118. if ((htmlout) && (!nowrap)) {
  3119. xmlGenericError(xmlGenericErrorContext,
  3120. "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
  3121. xmlGenericError(xmlGenericErrorContext,
  3122. "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
  3123. xmlGenericError(xmlGenericErrorContext,
  3124. "<html><head><title>%s output</title></head>\n",
  3125. argv[0]);
  3126. xmlGenericError(xmlGenericErrorContext,
  3127. "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
  3128. argv[0]);
  3129. }
  3130. #ifdef LIBXML_SCHEMATRON_ENABLED
  3131. if ((schematron != NULL) && (sax == 0)
  3132. #ifdef LIBXML_READER_ENABLED
  3133. && (stream == 0)
  3134. #endif /* LIBXML_READER_ENABLED */
  3135. ) {
  3136. xmlSchematronParserCtxtPtr ctxt;
  3137. /* forces loading the DTDs */
  3138. xmlLoadExtDtdDefaultValue |= 1;
  3139. options |= XML_PARSE_DTDLOAD;
  3140. if (timing) {
  3141. startTimer();
  3142. }
  3143. ctxt = xmlSchematronNewParserCtxt(schematron);
  3144. #if 0
  3145. xmlSchematronSetParserErrors(ctxt,
  3146. (xmlSchematronValidityErrorFunc) fprintf,
  3147. (xmlSchematronValidityWarningFunc) fprintf,
  3148. stderr);
  3149. #endif
  3150. wxschematron = xmlSchematronParse(ctxt);
  3151. if (wxschematron == NULL) {
  3152. xmlGenericError(xmlGenericErrorContext,
  3153. "Schematron schema %s failed to compile\n", schematron);
  3154. progresult = XMLLINT_ERR_SCHEMACOMP;
  3155. schematron = NULL;
  3156. }
  3157. xmlSchematronFreeParserCtxt(ctxt);
  3158. if (timing) {
  3159. endTimer("Compiling the schemas");
  3160. }
  3161. }
  3162. #endif
  3163. #ifdef LIBXML_SCHEMAS_ENABLED
  3164. if ((relaxng != NULL) && (sax == 0)
  3165. #ifdef LIBXML_READER_ENABLED
  3166. && (stream == 0)
  3167. #endif /* LIBXML_READER_ENABLED */
  3168. ) {
  3169. xmlRelaxNGParserCtxtPtr ctxt;
  3170. /* forces loading the DTDs */
  3171. xmlLoadExtDtdDefaultValue |= 1;
  3172. options |= XML_PARSE_DTDLOAD;
  3173. if (timing) {
  3174. startTimer();
  3175. }
  3176. ctxt = xmlRelaxNGNewParserCtxt(relaxng);
  3177. xmlRelaxNGSetParserErrors(ctxt,
  3178. (xmlRelaxNGValidityErrorFunc) fprintf,
  3179. (xmlRelaxNGValidityWarningFunc) fprintf,
  3180. stderr);
  3181. relaxngschemas = xmlRelaxNGParse(ctxt);
  3182. if (relaxngschemas == NULL) {
  3183. xmlGenericError(xmlGenericErrorContext,
  3184. "Relax-NG schema %s failed to compile\n", relaxng);
  3185. progresult = XMLLINT_ERR_SCHEMACOMP;
  3186. relaxng = NULL;
  3187. }
  3188. xmlRelaxNGFreeParserCtxt(ctxt);
  3189. if (timing) {
  3190. endTimer("Compiling the schemas");
  3191. }
  3192. } else if ((schema != NULL)
  3193. #ifdef LIBXML_READER_ENABLED
  3194. && (stream == 0)
  3195. #endif
  3196. ) {
  3197. xmlSchemaParserCtxtPtr ctxt;
  3198. if (timing) {
  3199. startTimer();
  3200. }
  3201. ctxt = xmlSchemaNewParserCtxt(schema);
  3202. xmlSchemaSetParserErrors(ctxt,
  3203. (xmlSchemaValidityErrorFunc) fprintf,
  3204. (xmlSchemaValidityWarningFunc) fprintf,
  3205. stderr);
  3206. wxschemas = xmlSchemaParse(ctxt);
  3207. if (wxschemas == NULL) {
  3208. xmlGenericError(xmlGenericErrorContext,
  3209. "WXS schema %s failed to compile\n", schema);
  3210. progresult = XMLLINT_ERR_SCHEMACOMP;
  3211. schema = NULL;
  3212. }
  3213. xmlSchemaFreeParserCtxt(ctxt);
  3214. if (timing) {
  3215. endTimer("Compiling the schemas");
  3216. }
  3217. }
  3218. #endif /* LIBXML_SCHEMAS_ENABLED */
  3219. #ifdef LIBXML_PATTERN_ENABLED
  3220. if ((pattern != NULL)
  3221. #ifdef LIBXML_READER_ENABLED
  3222. && (walker == 0)
  3223. #endif
  3224. ) {
  3225. patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
  3226. if (patternc == NULL) {
  3227. xmlGenericError(xmlGenericErrorContext,
  3228. "Pattern %s failed to compile\n", pattern);
  3229. progresult = XMLLINT_ERR_SCHEMAPAT;
  3230. pattern = NULL;
  3231. }
  3232. }
  3233. #endif /* LIBXML_PATTERN_ENABLED */
  3234. for (i = 1; i < argc ; i++) {
  3235. if ((!strcmp(argv[i], "-encode")) ||
  3236. (!strcmp(argv[i], "--encode"))) {
  3237. i++;
  3238. continue;
  3239. } else if ((!strcmp(argv[i], "-o")) ||
  3240. (!strcmp(argv[i], "-output")) ||
  3241. (!strcmp(argv[i], "--output"))) {
  3242. i++;
  3243. continue;
  3244. }
  3245. #ifdef LIBXML_VALID_ENABLED
  3246. if ((!strcmp(argv[i], "-dtdvalid")) ||
  3247. (!strcmp(argv[i], "--dtdvalid"))) {
  3248. i++;
  3249. continue;
  3250. }
  3251. if ((!strcmp(argv[i], "-path")) ||
  3252. (!strcmp(argv[i], "--path"))) {
  3253. i++;
  3254. continue;
  3255. }
  3256. if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
  3257. (!strcmp(argv[i], "--dtdvalidfpi"))) {
  3258. i++;
  3259. continue;
  3260. }
  3261. #endif /* LIBXML_VALID_ENABLED */
  3262. if ((!strcmp(argv[i], "-relaxng")) ||
  3263. (!strcmp(argv[i], "--relaxng"))) {
  3264. i++;
  3265. continue;
  3266. }
  3267. if ((!strcmp(argv[i], "-maxmem")) ||
  3268. (!strcmp(argv[i], "--maxmem"))) {
  3269. i++;
  3270. continue;
  3271. }
  3272. if ((!strcmp(argv[i], "-schema")) ||
  3273. (!strcmp(argv[i], "--schema"))) {
  3274. i++;
  3275. continue;
  3276. }
  3277. if ((!strcmp(argv[i], "-schematron")) ||
  3278. (!strcmp(argv[i], "--schematron"))) {
  3279. i++;
  3280. continue;
  3281. }
  3282. #ifdef LIBXML_PATTERN_ENABLED
  3283. if ((!strcmp(argv[i], "-pattern")) ||
  3284. (!strcmp(argv[i], "--pattern"))) {
  3285. i++;
  3286. continue;
  3287. }
  3288. #endif
  3289. if ((timing) && (repeat))
  3290. startTimer();
  3291. /* Remember file names. "-" means stdin. <sven@zen.org> */
  3292. if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
  3293. if (repeat) {
  3294. xmlParserCtxtPtr ctxt = NULL;
  3295. for (acount = 0;acount < repeat;acount++) {
  3296. #ifdef LIBXML_READER_ENABLED
  3297. if (stream != 0) {
  3298. streamFile(argv[i]);
  3299. } else {
  3300. #endif /* LIBXML_READER_ENABLED */
  3301. if (sax) {
  3302. testSAX(argv[i]);
  3303. } else {
  3304. if (ctxt == NULL)
  3305. ctxt = xmlNewParserCtxt();
  3306. parseAndPrintFile(argv[i], ctxt);
  3307. }
  3308. #ifdef LIBXML_READER_ENABLED
  3309. }
  3310. #endif /* LIBXML_READER_ENABLED */
  3311. }
  3312. if (ctxt != NULL)
  3313. xmlFreeParserCtxt(ctxt);
  3314. } else {
  3315. nbregister = 0;
  3316. #ifdef LIBXML_READER_ENABLED
  3317. if (stream != 0)
  3318. streamFile(argv[i]);
  3319. else
  3320. #endif /* LIBXML_READER_ENABLED */
  3321. if (sax) {
  3322. testSAX(argv[i]);
  3323. } else {
  3324. parseAndPrintFile(argv[i], NULL);
  3325. }
  3326. if ((chkregister) && (nbregister != 0)) {
  3327. fprintf(stderr, "Registration count off: %d\n", nbregister);
  3328. progresult = XMLLINT_ERR_RDREGIS;
  3329. }
  3330. }
  3331. files ++;
  3332. if ((timing) && (repeat)) {
  3333. endTimer("%d iterations", repeat);
  3334. }
  3335. }
  3336. }
  3337. if (generate)
  3338. parseAndPrintFile(NULL, NULL);
  3339. if ((htmlout) && (!nowrap)) {
  3340. xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
  3341. }
  3342. if ((files == 0) && (!generate) && (version == 0)) {
  3343. usage(argv[0]);
  3344. }
  3345. #ifdef LIBXML_SCHEMATRON_ENABLED
  3346. if (wxschematron != NULL)
  3347. xmlSchematronFree(wxschematron);
  3348. #endif
  3349. #ifdef LIBXML_SCHEMAS_ENABLED
  3350. if (relaxngschemas != NULL)
  3351. xmlRelaxNGFree(relaxngschemas);
  3352. if (wxschemas != NULL)
  3353. xmlSchemaFree(wxschemas);
  3354. xmlRelaxNGCleanupTypes();
  3355. #endif
  3356. #ifdef LIBXML_PATTERN_ENABLED
  3357. if (patternc != NULL)
  3358. xmlFreePattern(patternc);
  3359. #endif
  3360. xmlCleanupParser();
  3361. xmlMemoryDump();
  3362. return(progresult);
  3363. }