xmllint.c 100 KB

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