xmllint.c 101 KB

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