debugXML.c 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263
  1. /*
  2. * debugXML.c : This is a set of routines used for debugging the tree
  3. * produced by the XML parser.
  4. *
  5. * See Copyright for the status of this software.
  6. *
  7. * Daniel Veillard <daniel@veillard.com>
  8. */
  9. #define IN_LIBXML
  10. #include "libxml.h"
  11. #ifdef LIBXML_DEBUG_ENABLED
  12. #include <string.h>
  13. #ifdef HAVE_STDLIB_H
  14. #include <stdlib.h>
  15. #endif
  16. #ifdef HAVE_STRING_H
  17. #include <string.h>
  18. #endif
  19. #include <libxml/xmlmemory.h>
  20. #include <libxml/tree.h>
  21. #include <libxml/parser.h>
  22. #include <libxml/parserInternals.h>
  23. #include <libxml/valid.h>
  24. #include <libxml/debugXML.h>
  25. #include <libxml/HTMLtree.h>
  26. #include <libxml/HTMLparser.h>
  27. #include <libxml/xmlerror.h>
  28. #include <libxml/globals.h>
  29. #include <libxml/xpathInternals.h>
  30. #include <libxml/uri.h>
  31. #ifdef LIBXML_SCHEMAS_ENABLED
  32. #include <libxml/relaxng.h>
  33. #endif
  34. #define DUMP_TEXT_TYPE 1
  35. typedef struct _xmlDebugCtxt xmlDebugCtxt;
  36. typedef xmlDebugCtxt *xmlDebugCtxtPtr;
  37. struct _xmlDebugCtxt {
  38. FILE *output; /* the output file */
  39. char shift[101]; /* used for indenting */
  40. int depth; /* current depth */
  41. xmlDocPtr doc; /* current document */
  42. xmlNodePtr node; /* current node */
  43. xmlDictPtr dict; /* the doc dictionnary */
  44. int check; /* do just checkings */
  45. int errors; /* number of errors found */
  46. int nodict; /* if the document has no dictionnary */
  47. int options; /* options */
  48. };
  49. static void xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node);
  50. static void
  51. xmlCtxtDumpInitCtxt(xmlDebugCtxtPtr ctxt)
  52. {
  53. int i;
  54. ctxt->depth = 0;
  55. ctxt->check = 0;
  56. ctxt->errors = 0;
  57. ctxt->output = stdout;
  58. ctxt->doc = NULL;
  59. ctxt->node = NULL;
  60. ctxt->dict = NULL;
  61. ctxt->nodict = 0;
  62. ctxt->options = 0;
  63. for (i = 0; i < 100; i++)
  64. ctxt->shift[i] = ' ';
  65. ctxt->shift[100] = 0;
  66. }
  67. static void
  68. xmlCtxtDumpCleanCtxt(xmlDebugCtxtPtr ctxt ATTRIBUTE_UNUSED)
  69. {
  70. /* remove the ATTRIBUTE_UNUSED when this is added */
  71. }
  72. /**
  73. * xmlNsCheckScope:
  74. * @node: the node
  75. * @ns: the namespace node
  76. *
  77. * Check that a given namespace is in scope on a node.
  78. *
  79. * Returns 1 if in scope, -1 in case of argument error,
  80. * -2 if the namespace is not in scope, and -3 if not on
  81. * an ancestor node.
  82. */
  83. static int
  84. xmlNsCheckScope(xmlNodePtr node, xmlNsPtr ns)
  85. {
  86. xmlNsPtr cur;
  87. if ((node == NULL) || (ns == NULL))
  88. return(-1);
  89. if ((node->type != XML_ELEMENT_NODE) &&
  90. (node->type != XML_ATTRIBUTE_NODE) &&
  91. (node->type != XML_DOCUMENT_NODE) &&
  92. (node->type != XML_TEXT_NODE) &&
  93. (node->type != XML_HTML_DOCUMENT_NODE) &&
  94. (node->type != XML_XINCLUDE_START))
  95. return(-2);
  96. while ((node != NULL) &&
  97. ((node->type == XML_ELEMENT_NODE) ||
  98. (node->type == XML_ATTRIBUTE_NODE) ||
  99. (node->type == XML_TEXT_NODE) ||
  100. (node->type == XML_XINCLUDE_START))) {
  101. if ((node->type == XML_ELEMENT_NODE) ||
  102. (node->type == XML_XINCLUDE_START)) {
  103. cur = node->nsDef;
  104. while (cur != NULL) {
  105. if (cur == ns)
  106. return(1);
  107. if (xmlStrEqual(cur->prefix, ns->prefix))
  108. return(-2);
  109. cur = cur->next;
  110. }
  111. }
  112. node = node->parent;
  113. }
  114. /* the xml namespace may be declared on the document node */
  115. if ((node != NULL) &&
  116. ((node->type == XML_DOCUMENT_NODE) ||
  117. (node->type == XML_HTML_DOCUMENT_NODE))) {
  118. xmlNsPtr oldNs = ((xmlDocPtr) node)->oldNs;
  119. if (oldNs == ns)
  120. return(1);
  121. }
  122. return(-3);
  123. }
  124. static void
  125. xmlCtxtDumpSpaces(xmlDebugCtxtPtr ctxt)
  126. {
  127. if (ctxt->check)
  128. return;
  129. if ((ctxt->output != NULL) && (ctxt->depth > 0)) {
  130. if (ctxt->depth < 50)
  131. fprintf(ctxt->output, "%s", &ctxt->shift[100 - 2 * ctxt->depth]);
  132. else
  133. fprintf(ctxt->output, "%s", ctxt->shift);
  134. }
  135. }
  136. /**
  137. * xmlDebugErr:
  138. * @ctxt: a debug context
  139. * @error: the error code
  140. *
  141. * Handle a debug error.
  142. */
  143. static void
  144. xmlDebugErr(xmlDebugCtxtPtr ctxt, int error, const char *msg)
  145. {
  146. ctxt->errors++;
  147. __xmlRaiseError(NULL, NULL, NULL,
  148. NULL, ctxt->node, XML_FROM_CHECK,
  149. error, XML_ERR_ERROR, NULL, 0,
  150. NULL, NULL, NULL, 0, 0,
  151. "%s", msg);
  152. }
  153. static void
  154. xmlDebugErr2(xmlDebugCtxtPtr ctxt, int error, const char *msg, int extra)
  155. {
  156. ctxt->errors++;
  157. __xmlRaiseError(NULL, NULL, NULL,
  158. NULL, ctxt->node, XML_FROM_CHECK,
  159. error, XML_ERR_ERROR, NULL, 0,
  160. NULL, NULL, NULL, 0, 0,
  161. msg, extra);
  162. }
  163. static void
  164. xmlDebugErr3(xmlDebugCtxtPtr ctxt, int error, const char *msg, const char *extra)
  165. {
  166. ctxt->errors++;
  167. __xmlRaiseError(NULL, NULL, NULL,
  168. NULL, ctxt->node, XML_FROM_CHECK,
  169. error, XML_ERR_ERROR, NULL, 0,
  170. NULL, NULL, NULL, 0, 0,
  171. msg, extra);
  172. }
  173. /**
  174. * xmlCtxtNsCheckScope:
  175. * @ctxt: the debugging context
  176. * @node: the node
  177. * @ns: the namespace node
  178. *
  179. * Report if a given namespace is is not in scope.
  180. */
  181. static void
  182. xmlCtxtNsCheckScope(xmlDebugCtxtPtr ctxt, xmlNodePtr node, xmlNsPtr ns)
  183. {
  184. int ret;
  185. ret = xmlNsCheckScope(node, ns);
  186. if (ret == -2) {
  187. if (ns->prefix == NULL)
  188. xmlDebugErr(ctxt, XML_CHECK_NS_SCOPE,
  189. "Reference to default namespace not in scope\n");
  190. else
  191. xmlDebugErr3(ctxt, XML_CHECK_NS_SCOPE,
  192. "Reference to namespace '%s' not in scope\n",
  193. (char *) ns->prefix);
  194. }
  195. if (ret == -3) {
  196. if (ns->prefix == NULL)
  197. xmlDebugErr(ctxt, XML_CHECK_NS_ANCESTOR,
  198. "Reference to default namespace not on ancestor\n");
  199. else
  200. xmlDebugErr3(ctxt, XML_CHECK_NS_ANCESTOR,
  201. "Reference to namespace '%s' not on ancestor\n",
  202. (char *) ns->prefix);
  203. }
  204. }
  205. /**
  206. * xmlCtxtCheckString:
  207. * @ctxt: the debug context
  208. * @str: the string
  209. *
  210. * Do debugging on the string, currently it just checks the UTF-8 content
  211. */
  212. static void
  213. xmlCtxtCheckString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
  214. {
  215. if (str == NULL) return;
  216. if (ctxt->check) {
  217. if (!xmlCheckUTF8(str)) {
  218. xmlDebugErr3(ctxt, XML_CHECK_NOT_UTF8,
  219. "String is not UTF-8 %s", (const char *) str);
  220. }
  221. }
  222. }
  223. /**
  224. * xmlCtxtCheckName:
  225. * @ctxt: the debug context
  226. * @name: the name
  227. *
  228. * Do debugging on the name, for example the dictionnary status and
  229. * conformance to the Name production.
  230. */
  231. static void
  232. xmlCtxtCheckName(xmlDebugCtxtPtr ctxt, const xmlChar * name)
  233. {
  234. if (ctxt->check) {
  235. if (name == NULL) {
  236. xmlDebugErr(ctxt, XML_CHECK_NO_NAME, "Name is NULL");
  237. return;
  238. }
  239. if (xmlValidateName(name, 0)) {
  240. xmlDebugErr3(ctxt, XML_CHECK_NOT_NCNAME,
  241. "Name is not an NCName '%s'", (const char *) name);
  242. }
  243. if ((ctxt->dict != NULL) &&
  244. (!xmlDictOwns(ctxt->dict, name)) &&
  245. ((ctxt->doc == NULL) ||
  246. ((ctxt->doc->parseFlags & (XML_PARSE_SAX1 | XML_PARSE_NODICT)) == 0))) {
  247. xmlDebugErr3(ctxt, XML_CHECK_OUTSIDE_DICT,
  248. "Name is not from the document dictionnary '%s'",
  249. (const char *) name);
  250. }
  251. }
  252. }
  253. static void
  254. xmlCtxtGenericNodeCheck(xmlDebugCtxtPtr ctxt, xmlNodePtr node) {
  255. xmlDocPtr doc;
  256. xmlDictPtr dict;
  257. doc = node->doc;
  258. if (node->parent == NULL)
  259. xmlDebugErr(ctxt, XML_CHECK_NO_PARENT,
  260. "Node has no parent\n");
  261. if (node->doc == NULL) {
  262. xmlDebugErr(ctxt, XML_CHECK_NO_DOC,
  263. "Node has no doc\n");
  264. dict = NULL;
  265. } else {
  266. dict = doc->dict;
  267. if ((dict == NULL) && (ctxt->nodict == 0)) {
  268. #if 0
  269. /* desactivated right now as it raises too many errors */
  270. if (doc->type == XML_DOCUMENT_NODE)
  271. xmlDebugErr(ctxt, XML_CHECK_NO_DICT,
  272. "Document has no dictionnary\n");
  273. #endif
  274. ctxt->nodict = 1;
  275. }
  276. if (ctxt->doc == NULL)
  277. ctxt->doc = doc;
  278. if (ctxt->dict == NULL) {
  279. ctxt->dict = dict;
  280. }
  281. }
  282. if ((node->parent != NULL) && (node->doc != node->parent->doc) &&
  283. (!xmlStrEqual(node->name, BAD_CAST "pseudoroot")))
  284. xmlDebugErr(ctxt, XML_CHECK_WRONG_DOC,
  285. "Node doc differs from parent's one\n");
  286. if (node->prev == NULL) {
  287. if (node->type == XML_ATTRIBUTE_NODE) {
  288. if ((node->parent != NULL) &&
  289. (node != (xmlNodePtr) node->parent->properties))
  290. xmlDebugErr(ctxt, XML_CHECK_NO_PREV,
  291. "Attr has no prev and not first of attr list\n");
  292. } else if ((node->parent != NULL) && (node->parent->children != node))
  293. xmlDebugErr(ctxt, XML_CHECK_NO_PREV,
  294. "Node has no prev and not first of parent list\n");
  295. } else {
  296. if (node->prev->next != node)
  297. xmlDebugErr(ctxt, XML_CHECK_WRONG_PREV,
  298. "Node prev->next : back link wrong\n");
  299. }
  300. if (node->next == NULL) {
  301. if ((node->parent != NULL) && (node->type != XML_ATTRIBUTE_NODE) &&
  302. (node->parent->last != node) &&
  303. (node->parent->type == XML_ELEMENT_NODE))
  304. xmlDebugErr(ctxt, XML_CHECK_NO_NEXT,
  305. "Node has no next and not last of parent list\n");
  306. } else {
  307. if (node->next->prev != node)
  308. xmlDebugErr(ctxt, XML_CHECK_WRONG_NEXT,
  309. "Node next->prev : forward link wrong\n");
  310. if (node->next->parent != node->parent)
  311. xmlDebugErr(ctxt, XML_CHECK_WRONG_PARENT,
  312. "Node next->prev : forward link wrong\n");
  313. }
  314. if (node->type == XML_ELEMENT_NODE) {
  315. xmlNsPtr ns;
  316. ns = node->nsDef;
  317. while (ns != NULL) {
  318. xmlCtxtNsCheckScope(ctxt, node, ns);
  319. ns = ns->next;
  320. }
  321. if (node->ns != NULL)
  322. xmlCtxtNsCheckScope(ctxt, node, node->ns);
  323. } else if (node->type == XML_ATTRIBUTE_NODE) {
  324. if (node->ns != NULL)
  325. xmlCtxtNsCheckScope(ctxt, node, node->ns);
  326. }
  327. if ((node->type != XML_ELEMENT_NODE) &&
  328. (node->type != XML_ATTRIBUTE_NODE) &&
  329. (node->type != XML_ELEMENT_DECL) &&
  330. (node->type != XML_ATTRIBUTE_DECL) &&
  331. (node->type != XML_DTD_NODE) &&
  332. (node->type != XML_ELEMENT_DECL) &&
  333. (node->type != XML_HTML_DOCUMENT_NODE) &&
  334. (node->type != XML_DOCUMENT_NODE)) {
  335. if (node->content != NULL)
  336. xmlCtxtCheckString(ctxt, (const xmlChar *) node->content);
  337. }
  338. switch (node->type) {
  339. case XML_ELEMENT_NODE:
  340. case XML_ATTRIBUTE_NODE:
  341. xmlCtxtCheckName(ctxt, node->name);
  342. break;
  343. case XML_TEXT_NODE:
  344. if ((node->name == xmlStringText) ||
  345. (node->name == xmlStringTextNoenc))
  346. break;
  347. /* some case of entity substitution can lead to this */
  348. if ((ctxt->dict != NULL) &&
  349. (node->name == xmlDictLookup(ctxt->dict, BAD_CAST "nbktext",
  350. 7)))
  351. break;
  352. xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
  353. "Text node has wrong name '%s'",
  354. (const char *) node->name);
  355. break;
  356. case XML_COMMENT_NODE:
  357. if (node->name == xmlStringComment)
  358. break;
  359. xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
  360. "Comment node has wrong name '%s'",
  361. (const char *) node->name);
  362. break;
  363. case XML_PI_NODE:
  364. xmlCtxtCheckName(ctxt, node->name);
  365. break;
  366. case XML_CDATA_SECTION_NODE:
  367. if (node->name == NULL)
  368. break;
  369. xmlDebugErr3(ctxt, XML_CHECK_NAME_NOT_NULL,
  370. "CData section has non NULL name '%s'",
  371. (const char *) node->name);
  372. break;
  373. case XML_ENTITY_REF_NODE:
  374. case XML_ENTITY_NODE:
  375. case XML_DOCUMENT_TYPE_NODE:
  376. case XML_DOCUMENT_FRAG_NODE:
  377. case XML_NOTATION_NODE:
  378. case XML_DTD_NODE:
  379. case XML_ELEMENT_DECL:
  380. case XML_ATTRIBUTE_DECL:
  381. case XML_ENTITY_DECL:
  382. case XML_NAMESPACE_DECL:
  383. case XML_XINCLUDE_START:
  384. case XML_XINCLUDE_END:
  385. #ifdef LIBXML_DOCB_ENABLED
  386. case XML_DOCB_DOCUMENT_NODE:
  387. #endif
  388. case XML_DOCUMENT_NODE:
  389. case XML_HTML_DOCUMENT_NODE:
  390. break;
  391. }
  392. }
  393. static void
  394. xmlCtxtDumpString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
  395. {
  396. int i;
  397. if (ctxt->check) {
  398. return;
  399. }
  400. /* TODO: check UTF8 content of the string */
  401. if (str == NULL) {
  402. fprintf(ctxt->output, "(NULL)");
  403. return;
  404. }
  405. for (i = 0; i < 40; i++)
  406. if (str[i] == 0)
  407. return;
  408. else if (IS_BLANK_CH(str[i]))
  409. fputc(' ', ctxt->output);
  410. else if (str[i] >= 0x80)
  411. fprintf(ctxt->output, "#%X", str[i]);
  412. else
  413. fputc(str[i], ctxt->output);
  414. fprintf(ctxt->output, "...");
  415. }
  416. static void
  417. xmlCtxtDumpDtdNode(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
  418. {
  419. xmlCtxtDumpSpaces(ctxt);
  420. if (dtd == NULL) {
  421. if (!ctxt->check)
  422. fprintf(ctxt->output, "DTD node is NULL\n");
  423. return;
  424. }
  425. if (dtd->type != XML_DTD_NODE) {
  426. xmlDebugErr(ctxt, XML_CHECK_NOT_DTD,
  427. "Node is not a DTD");
  428. return;
  429. }
  430. if (!ctxt->check) {
  431. if (dtd->name != NULL)
  432. fprintf(ctxt->output, "DTD(%s)", (char *) dtd->name);
  433. else
  434. fprintf(ctxt->output, "DTD");
  435. if (dtd->ExternalID != NULL)
  436. fprintf(ctxt->output, ", PUBLIC %s", (char *) dtd->ExternalID);
  437. if (dtd->SystemID != NULL)
  438. fprintf(ctxt->output, ", SYSTEM %s", (char *) dtd->SystemID);
  439. fprintf(ctxt->output, "\n");
  440. }
  441. /*
  442. * Do a bit of checking
  443. */
  444. xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) dtd);
  445. }
  446. static void
  447. xmlCtxtDumpAttrDecl(xmlDebugCtxtPtr ctxt, xmlAttributePtr attr)
  448. {
  449. xmlCtxtDumpSpaces(ctxt);
  450. if (attr == NULL) {
  451. if (!ctxt->check)
  452. fprintf(ctxt->output, "Attribute declaration is NULL\n");
  453. return;
  454. }
  455. if (attr->type != XML_ATTRIBUTE_DECL) {
  456. xmlDebugErr(ctxt, XML_CHECK_NOT_ATTR_DECL,
  457. "Node is not an attribute declaration");
  458. return;
  459. }
  460. if (attr->name != NULL) {
  461. if (!ctxt->check)
  462. fprintf(ctxt->output, "ATTRDECL(%s)", (char *) attr->name);
  463. } else
  464. xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
  465. "Node attribute declaration has no name");
  466. if (attr->elem != NULL) {
  467. if (!ctxt->check)
  468. fprintf(ctxt->output, " for %s", (char *) attr->elem);
  469. } else
  470. xmlDebugErr(ctxt, XML_CHECK_NO_ELEM,
  471. "Node attribute declaration has no element name");
  472. if (!ctxt->check) {
  473. switch (attr->atype) {
  474. case XML_ATTRIBUTE_CDATA:
  475. fprintf(ctxt->output, " CDATA");
  476. break;
  477. case XML_ATTRIBUTE_ID:
  478. fprintf(ctxt->output, " ID");
  479. break;
  480. case XML_ATTRIBUTE_IDREF:
  481. fprintf(ctxt->output, " IDREF");
  482. break;
  483. case XML_ATTRIBUTE_IDREFS:
  484. fprintf(ctxt->output, " IDREFS");
  485. break;
  486. case XML_ATTRIBUTE_ENTITY:
  487. fprintf(ctxt->output, " ENTITY");
  488. break;
  489. case XML_ATTRIBUTE_ENTITIES:
  490. fprintf(ctxt->output, " ENTITIES");
  491. break;
  492. case XML_ATTRIBUTE_NMTOKEN:
  493. fprintf(ctxt->output, " NMTOKEN");
  494. break;
  495. case XML_ATTRIBUTE_NMTOKENS:
  496. fprintf(ctxt->output, " NMTOKENS");
  497. break;
  498. case XML_ATTRIBUTE_ENUMERATION:
  499. fprintf(ctxt->output, " ENUMERATION");
  500. break;
  501. case XML_ATTRIBUTE_NOTATION:
  502. fprintf(ctxt->output, " NOTATION ");
  503. break;
  504. }
  505. if (attr->tree != NULL) {
  506. int indx;
  507. xmlEnumerationPtr cur = attr->tree;
  508. for (indx = 0; indx < 5; indx++) {
  509. if (indx != 0)
  510. fprintf(ctxt->output, "|%s", (char *) cur->name);
  511. else
  512. fprintf(ctxt->output, " (%s", (char *) cur->name);
  513. cur = cur->next;
  514. if (cur == NULL)
  515. break;
  516. }
  517. if (cur == NULL)
  518. fprintf(ctxt->output, ")");
  519. else
  520. fprintf(ctxt->output, "...)");
  521. }
  522. switch (attr->def) {
  523. case XML_ATTRIBUTE_NONE:
  524. break;
  525. case XML_ATTRIBUTE_REQUIRED:
  526. fprintf(ctxt->output, " REQUIRED");
  527. break;
  528. case XML_ATTRIBUTE_IMPLIED:
  529. fprintf(ctxt->output, " IMPLIED");
  530. break;
  531. case XML_ATTRIBUTE_FIXED:
  532. fprintf(ctxt->output, " FIXED");
  533. break;
  534. }
  535. if (attr->defaultValue != NULL) {
  536. fprintf(ctxt->output, "\"");
  537. xmlCtxtDumpString(ctxt, attr->defaultValue);
  538. fprintf(ctxt->output, "\"");
  539. }
  540. fprintf(ctxt->output, "\n");
  541. }
  542. /*
  543. * Do a bit of checking
  544. */
  545. xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
  546. }
  547. static void
  548. xmlCtxtDumpElemDecl(xmlDebugCtxtPtr ctxt, xmlElementPtr elem)
  549. {
  550. xmlCtxtDumpSpaces(ctxt);
  551. if (elem == NULL) {
  552. if (!ctxt->check)
  553. fprintf(ctxt->output, "Element declaration is NULL\n");
  554. return;
  555. }
  556. if (elem->type != XML_ELEMENT_DECL) {
  557. xmlDebugErr(ctxt, XML_CHECK_NOT_ELEM_DECL,
  558. "Node is not an element declaration");
  559. return;
  560. }
  561. if (elem->name != NULL) {
  562. if (!ctxt->check) {
  563. fprintf(ctxt->output, "ELEMDECL(");
  564. xmlCtxtDumpString(ctxt, elem->name);
  565. fprintf(ctxt->output, ")");
  566. }
  567. } else
  568. xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
  569. "Element declaration has no name");
  570. if (!ctxt->check) {
  571. switch (elem->etype) {
  572. case XML_ELEMENT_TYPE_UNDEFINED:
  573. fprintf(ctxt->output, ", UNDEFINED");
  574. break;
  575. case XML_ELEMENT_TYPE_EMPTY:
  576. fprintf(ctxt->output, ", EMPTY");
  577. break;
  578. case XML_ELEMENT_TYPE_ANY:
  579. fprintf(ctxt->output, ", ANY");
  580. break;
  581. case XML_ELEMENT_TYPE_MIXED:
  582. fprintf(ctxt->output, ", MIXED ");
  583. break;
  584. case XML_ELEMENT_TYPE_ELEMENT:
  585. fprintf(ctxt->output, ", MIXED ");
  586. break;
  587. }
  588. if ((elem->type != XML_ELEMENT_NODE) && (elem->content != NULL)) {
  589. char buf[5001];
  590. buf[0] = 0;
  591. xmlSnprintfElementContent(buf, 5000, elem->content, 1);
  592. buf[5000] = 0;
  593. fprintf(ctxt->output, "%s", buf);
  594. }
  595. fprintf(ctxt->output, "\n");
  596. }
  597. /*
  598. * Do a bit of checking
  599. */
  600. xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) elem);
  601. }
  602. static void
  603. xmlCtxtDumpEntityDecl(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
  604. {
  605. xmlCtxtDumpSpaces(ctxt);
  606. if (ent == NULL) {
  607. if (!ctxt->check)
  608. fprintf(ctxt->output, "Entity declaration is NULL\n");
  609. return;
  610. }
  611. if (ent->type != XML_ENTITY_DECL) {
  612. xmlDebugErr(ctxt, XML_CHECK_NOT_ENTITY_DECL,
  613. "Node is not an entity declaration");
  614. return;
  615. }
  616. if (ent->name != NULL) {
  617. if (!ctxt->check) {
  618. fprintf(ctxt->output, "ENTITYDECL(");
  619. xmlCtxtDumpString(ctxt, ent->name);
  620. fprintf(ctxt->output, ")");
  621. }
  622. } else
  623. xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
  624. "Entity declaration has no name");
  625. if (!ctxt->check) {
  626. switch (ent->etype) {
  627. case XML_INTERNAL_GENERAL_ENTITY:
  628. fprintf(ctxt->output, ", internal\n");
  629. break;
  630. case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
  631. fprintf(ctxt->output, ", external parsed\n");
  632. break;
  633. case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
  634. fprintf(ctxt->output, ", unparsed\n");
  635. break;
  636. case XML_INTERNAL_PARAMETER_ENTITY:
  637. fprintf(ctxt->output, ", parameter\n");
  638. break;
  639. case XML_EXTERNAL_PARAMETER_ENTITY:
  640. fprintf(ctxt->output, ", external parameter\n");
  641. break;
  642. case XML_INTERNAL_PREDEFINED_ENTITY:
  643. fprintf(ctxt->output, ", predefined\n");
  644. break;
  645. }
  646. if (ent->ExternalID) {
  647. xmlCtxtDumpSpaces(ctxt);
  648. fprintf(ctxt->output, " ExternalID=%s\n",
  649. (char *) ent->ExternalID);
  650. }
  651. if (ent->SystemID) {
  652. xmlCtxtDumpSpaces(ctxt);
  653. fprintf(ctxt->output, " SystemID=%s\n",
  654. (char *) ent->SystemID);
  655. }
  656. if (ent->URI != NULL) {
  657. xmlCtxtDumpSpaces(ctxt);
  658. fprintf(ctxt->output, " URI=%s\n", (char *) ent->URI);
  659. }
  660. if (ent->content) {
  661. xmlCtxtDumpSpaces(ctxt);
  662. fprintf(ctxt->output, " content=");
  663. xmlCtxtDumpString(ctxt, ent->content);
  664. fprintf(ctxt->output, "\n");
  665. }
  666. }
  667. /*
  668. * Do a bit of checking
  669. */
  670. xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) ent);
  671. }
  672. static void
  673. xmlCtxtDumpNamespace(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
  674. {
  675. xmlCtxtDumpSpaces(ctxt);
  676. if (ns == NULL) {
  677. if (!ctxt->check)
  678. fprintf(ctxt->output, "namespace node is NULL\n");
  679. return;
  680. }
  681. if (ns->type != XML_NAMESPACE_DECL) {
  682. xmlDebugErr(ctxt, XML_CHECK_NOT_NS_DECL,
  683. "Node is not a namespace declaration");
  684. return;
  685. }
  686. if (ns->href == NULL) {
  687. if (ns->prefix != NULL)
  688. xmlDebugErr3(ctxt, XML_CHECK_NO_HREF,
  689. "Incomplete namespace %s href=NULL\n",
  690. (char *) ns->prefix);
  691. else
  692. xmlDebugErr(ctxt, XML_CHECK_NO_HREF,
  693. "Incomplete default namespace href=NULL\n");
  694. } else {
  695. if (!ctxt->check) {
  696. if (ns->prefix != NULL)
  697. fprintf(ctxt->output, "namespace %s href=",
  698. (char *) ns->prefix);
  699. else
  700. fprintf(ctxt->output, "default namespace href=");
  701. xmlCtxtDumpString(ctxt, ns->href);
  702. fprintf(ctxt->output, "\n");
  703. }
  704. }
  705. }
  706. static void
  707. xmlCtxtDumpNamespaceList(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
  708. {
  709. while (ns != NULL) {
  710. xmlCtxtDumpNamespace(ctxt, ns);
  711. ns = ns->next;
  712. }
  713. }
  714. static void
  715. xmlCtxtDumpEntity(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
  716. {
  717. xmlCtxtDumpSpaces(ctxt);
  718. if (ent == NULL) {
  719. if (!ctxt->check)
  720. fprintf(ctxt->output, "Entity is NULL\n");
  721. return;
  722. }
  723. if (!ctxt->check) {
  724. switch (ent->etype) {
  725. case XML_INTERNAL_GENERAL_ENTITY:
  726. fprintf(ctxt->output, "INTERNAL_GENERAL_ENTITY ");
  727. break;
  728. case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
  729. fprintf(ctxt->output, "EXTERNAL_GENERAL_PARSED_ENTITY ");
  730. break;
  731. case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
  732. fprintf(ctxt->output, "EXTERNAL_GENERAL_UNPARSED_ENTITY ");
  733. break;
  734. case XML_INTERNAL_PARAMETER_ENTITY:
  735. fprintf(ctxt->output, "INTERNAL_PARAMETER_ENTITY ");
  736. break;
  737. case XML_EXTERNAL_PARAMETER_ENTITY:
  738. fprintf(ctxt->output, "EXTERNAL_PARAMETER_ENTITY ");
  739. break;
  740. default:
  741. fprintf(ctxt->output, "ENTITY_%d ! ", (int) ent->etype);
  742. }
  743. fprintf(ctxt->output, "%s\n", ent->name);
  744. if (ent->ExternalID) {
  745. xmlCtxtDumpSpaces(ctxt);
  746. fprintf(ctxt->output, "ExternalID=%s\n",
  747. (char *) ent->ExternalID);
  748. }
  749. if (ent->SystemID) {
  750. xmlCtxtDumpSpaces(ctxt);
  751. fprintf(ctxt->output, "SystemID=%s\n", (char *) ent->SystemID);
  752. }
  753. if (ent->URI) {
  754. xmlCtxtDumpSpaces(ctxt);
  755. fprintf(ctxt->output, "URI=%s\n", (char *) ent->URI);
  756. }
  757. if (ent->content) {
  758. xmlCtxtDumpSpaces(ctxt);
  759. fprintf(ctxt->output, "content=");
  760. xmlCtxtDumpString(ctxt, ent->content);
  761. fprintf(ctxt->output, "\n");
  762. }
  763. }
  764. }
  765. /**
  766. * xmlCtxtDumpAttr:
  767. * @output: the FILE * for the output
  768. * @attr: the attribute
  769. * @depth: the indentation level.
  770. *
  771. * Dumps debug information for the attribute
  772. */
  773. static void
  774. xmlCtxtDumpAttr(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
  775. {
  776. xmlCtxtDumpSpaces(ctxt);
  777. if (attr == NULL) {
  778. if (!ctxt->check)
  779. fprintf(ctxt->output, "Attr is NULL");
  780. return;
  781. }
  782. if (!ctxt->check) {
  783. fprintf(ctxt->output, "ATTRIBUTE ");
  784. xmlCtxtDumpString(ctxt, attr->name);
  785. fprintf(ctxt->output, "\n");
  786. if (attr->children != NULL) {
  787. ctxt->depth++;
  788. xmlCtxtDumpNodeList(ctxt, attr->children);
  789. ctxt->depth--;
  790. }
  791. }
  792. if (attr->name == NULL)
  793. xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
  794. "Attribute has no name");
  795. /*
  796. * Do a bit of checking
  797. */
  798. xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
  799. }
  800. /**
  801. * xmlCtxtDumpAttrList:
  802. * @output: the FILE * for the output
  803. * @attr: the attribute list
  804. * @depth: the indentation level.
  805. *
  806. * Dumps debug information for the attribute list
  807. */
  808. static void
  809. xmlCtxtDumpAttrList(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
  810. {
  811. while (attr != NULL) {
  812. xmlCtxtDumpAttr(ctxt, attr);
  813. attr = attr->next;
  814. }
  815. }
  816. /**
  817. * xmlCtxtDumpOneNode:
  818. * @output: the FILE * for the output
  819. * @node: the node
  820. * @depth: the indentation level.
  821. *
  822. * Dumps debug information for the element node, it is not recursive
  823. */
  824. static void
  825. xmlCtxtDumpOneNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
  826. {
  827. if (node == NULL) {
  828. if (!ctxt->check) {
  829. xmlCtxtDumpSpaces(ctxt);
  830. fprintf(ctxt->output, "node is NULL\n");
  831. }
  832. return;
  833. }
  834. ctxt->node = node;
  835. switch (node->type) {
  836. case XML_ELEMENT_NODE:
  837. if (!ctxt->check) {
  838. xmlCtxtDumpSpaces(ctxt);
  839. fprintf(ctxt->output, "ELEMENT ");
  840. if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
  841. xmlCtxtDumpString(ctxt, node->ns->prefix);
  842. fprintf(ctxt->output, ":");
  843. }
  844. xmlCtxtDumpString(ctxt, node->name);
  845. fprintf(ctxt->output, "\n");
  846. }
  847. break;
  848. case XML_ATTRIBUTE_NODE:
  849. if (!ctxt->check)
  850. xmlCtxtDumpSpaces(ctxt);
  851. fprintf(ctxt->output, "Error, ATTRIBUTE found here\n");
  852. xmlCtxtGenericNodeCheck(ctxt, node);
  853. return;
  854. case XML_TEXT_NODE:
  855. if (!ctxt->check) {
  856. xmlCtxtDumpSpaces(ctxt);
  857. if (node->name == (const xmlChar *) xmlStringTextNoenc)
  858. fprintf(ctxt->output, "TEXT no enc");
  859. else
  860. fprintf(ctxt->output, "TEXT");
  861. if (ctxt->options & DUMP_TEXT_TYPE) {
  862. if (node->content == (xmlChar *) &(node->properties))
  863. fprintf(ctxt->output, " compact\n");
  864. else if (xmlDictOwns(ctxt->dict, node->content) == 1)
  865. fprintf(ctxt->output, " interned\n");
  866. else
  867. fprintf(ctxt->output, "\n");
  868. } else
  869. fprintf(ctxt->output, "\n");
  870. }
  871. break;
  872. case XML_CDATA_SECTION_NODE:
  873. if (!ctxt->check) {
  874. xmlCtxtDumpSpaces(ctxt);
  875. fprintf(ctxt->output, "CDATA_SECTION\n");
  876. }
  877. break;
  878. case XML_ENTITY_REF_NODE:
  879. if (!ctxt->check) {
  880. xmlCtxtDumpSpaces(ctxt);
  881. fprintf(ctxt->output, "ENTITY_REF(%s)\n",
  882. (char *) node->name);
  883. }
  884. break;
  885. case XML_ENTITY_NODE:
  886. if (!ctxt->check) {
  887. xmlCtxtDumpSpaces(ctxt);
  888. fprintf(ctxt->output, "ENTITY\n");
  889. }
  890. break;
  891. case XML_PI_NODE:
  892. if (!ctxt->check) {
  893. xmlCtxtDumpSpaces(ctxt);
  894. fprintf(ctxt->output, "PI %s\n", (char *) node->name);
  895. }
  896. break;
  897. case XML_COMMENT_NODE:
  898. if (!ctxt->check) {
  899. xmlCtxtDumpSpaces(ctxt);
  900. fprintf(ctxt->output, "COMMENT\n");
  901. }
  902. break;
  903. case XML_DOCUMENT_NODE:
  904. case XML_HTML_DOCUMENT_NODE:
  905. if (!ctxt->check) {
  906. xmlCtxtDumpSpaces(ctxt);
  907. }
  908. fprintf(ctxt->output, "Error, DOCUMENT found here\n");
  909. xmlCtxtGenericNodeCheck(ctxt, node);
  910. return;
  911. case XML_DOCUMENT_TYPE_NODE:
  912. if (!ctxt->check) {
  913. xmlCtxtDumpSpaces(ctxt);
  914. fprintf(ctxt->output, "DOCUMENT_TYPE\n");
  915. }
  916. break;
  917. case XML_DOCUMENT_FRAG_NODE:
  918. if (!ctxt->check) {
  919. xmlCtxtDumpSpaces(ctxt);
  920. fprintf(ctxt->output, "DOCUMENT_FRAG\n");
  921. }
  922. break;
  923. case XML_NOTATION_NODE:
  924. if (!ctxt->check) {
  925. xmlCtxtDumpSpaces(ctxt);
  926. fprintf(ctxt->output, "NOTATION\n");
  927. }
  928. break;
  929. case XML_DTD_NODE:
  930. xmlCtxtDumpDtdNode(ctxt, (xmlDtdPtr) node);
  931. return;
  932. case XML_ELEMENT_DECL:
  933. xmlCtxtDumpElemDecl(ctxt, (xmlElementPtr) node);
  934. return;
  935. case XML_ATTRIBUTE_DECL:
  936. xmlCtxtDumpAttrDecl(ctxt, (xmlAttributePtr) node);
  937. return;
  938. case XML_ENTITY_DECL:
  939. xmlCtxtDumpEntityDecl(ctxt, (xmlEntityPtr) node);
  940. return;
  941. case XML_NAMESPACE_DECL:
  942. xmlCtxtDumpNamespace(ctxt, (xmlNsPtr) node);
  943. return;
  944. case XML_XINCLUDE_START:
  945. if (!ctxt->check) {
  946. xmlCtxtDumpSpaces(ctxt);
  947. fprintf(ctxt->output, "INCLUDE START\n");
  948. }
  949. return;
  950. case XML_XINCLUDE_END:
  951. if (!ctxt->check) {
  952. xmlCtxtDumpSpaces(ctxt);
  953. fprintf(ctxt->output, "INCLUDE END\n");
  954. }
  955. return;
  956. default:
  957. if (!ctxt->check)
  958. xmlCtxtDumpSpaces(ctxt);
  959. xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
  960. "Unknown node type %d\n", node->type);
  961. return;
  962. }
  963. if (node->doc == NULL) {
  964. if (!ctxt->check) {
  965. xmlCtxtDumpSpaces(ctxt);
  966. }
  967. fprintf(ctxt->output, "PBM: doc == NULL !!!\n");
  968. }
  969. ctxt->depth++;
  970. if ((node->type == XML_ELEMENT_NODE) && (node->nsDef != NULL))
  971. xmlCtxtDumpNamespaceList(ctxt, node->nsDef);
  972. if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL))
  973. xmlCtxtDumpAttrList(ctxt, node->properties);
  974. if (node->type != XML_ENTITY_REF_NODE) {
  975. if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) {
  976. if (!ctxt->check) {
  977. xmlCtxtDumpSpaces(ctxt);
  978. fprintf(ctxt->output, "content=");
  979. xmlCtxtDumpString(ctxt, node->content);
  980. fprintf(ctxt->output, "\n");
  981. }
  982. }
  983. } else {
  984. xmlEntityPtr ent;
  985. ent = xmlGetDocEntity(node->doc, node->name);
  986. if (ent != NULL)
  987. xmlCtxtDumpEntity(ctxt, ent);
  988. }
  989. ctxt->depth--;
  990. /*
  991. * Do a bit of checking
  992. */
  993. xmlCtxtGenericNodeCheck(ctxt, node);
  994. }
  995. /**
  996. * xmlCtxtDumpNode:
  997. * @output: the FILE * for the output
  998. * @node: the node
  999. * @depth: the indentation level.
  1000. *
  1001. * Dumps debug information for the element node, it is recursive
  1002. */
  1003. static void
  1004. xmlCtxtDumpNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
  1005. {
  1006. if (node == NULL) {
  1007. if (!ctxt->check) {
  1008. xmlCtxtDumpSpaces(ctxt);
  1009. fprintf(ctxt->output, "node is NULL\n");
  1010. }
  1011. return;
  1012. }
  1013. xmlCtxtDumpOneNode(ctxt, node);
  1014. if ((node->type != XML_NAMESPACE_DECL) &&
  1015. (node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
  1016. ctxt->depth++;
  1017. xmlCtxtDumpNodeList(ctxt, node->children);
  1018. ctxt->depth--;
  1019. }
  1020. }
  1021. /**
  1022. * xmlCtxtDumpNodeList:
  1023. * @output: the FILE * for the output
  1024. * @node: the node list
  1025. * @depth: the indentation level.
  1026. *
  1027. * Dumps debug information for the list of element node, it is recursive
  1028. */
  1029. static void
  1030. xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
  1031. {
  1032. while (node != NULL) {
  1033. xmlCtxtDumpNode(ctxt, node);
  1034. node = node->next;
  1035. }
  1036. }
  1037. static void
  1038. xmlCtxtDumpDocHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
  1039. {
  1040. if (doc == NULL) {
  1041. if (!ctxt->check)
  1042. fprintf(ctxt->output, "DOCUMENT == NULL !\n");
  1043. return;
  1044. }
  1045. ctxt->node = (xmlNodePtr) doc;
  1046. switch (doc->type) {
  1047. case XML_ELEMENT_NODE:
  1048. xmlDebugErr(ctxt, XML_CHECK_FOUND_ELEMENT,
  1049. "Misplaced ELEMENT node\n");
  1050. break;
  1051. case XML_ATTRIBUTE_NODE:
  1052. xmlDebugErr(ctxt, XML_CHECK_FOUND_ATTRIBUTE,
  1053. "Misplaced ATTRIBUTE node\n");
  1054. break;
  1055. case XML_TEXT_NODE:
  1056. xmlDebugErr(ctxt, XML_CHECK_FOUND_TEXT,
  1057. "Misplaced TEXT node\n");
  1058. break;
  1059. case XML_CDATA_SECTION_NODE:
  1060. xmlDebugErr(ctxt, XML_CHECK_FOUND_CDATA,
  1061. "Misplaced CDATA node\n");
  1062. break;
  1063. case XML_ENTITY_REF_NODE:
  1064. xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITYREF,
  1065. "Misplaced ENTITYREF node\n");
  1066. break;
  1067. case XML_ENTITY_NODE:
  1068. xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITY,
  1069. "Misplaced ENTITY node\n");
  1070. break;
  1071. case XML_PI_NODE:
  1072. xmlDebugErr(ctxt, XML_CHECK_FOUND_PI,
  1073. "Misplaced PI node\n");
  1074. break;
  1075. case XML_COMMENT_NODE:
  1076. xmlDebugErr(ctxt, XML_CHECK_FOUND_COMMENT,
  1077. "Misplaced COMMENT node\n");
  1078. break;
  1079. case XML_DOCUMENT_NODE:
  1080. if (!ctxt->check)
  1081. fprintf(ctxt->output, "DOCUMENT\n");
  1082. break;
  1083. case XML_HTML_DOCUMENT_NODE:
  1084. if (!ctxt->check)
  1085. fprintf(ctxt->output, "HTML DOCUMENT\n");
  1086. break;
  1087. case XML_DOCUMENT_TYPE_NODE:
  1088. xmlDebugErr(ctxt, XML_CHECK_FOUND_DOCTYPE,
  1089. "Misplaced DOCTYPE node\n");
  1090. break;
  1091. case XML_DOCUMENT_FRAG_NODE:
  1092. xmlDebugErr(ctxt, XML_CHECK_FOUND_FRAGMENT,
  1093. "Misplaced FRAGMENT node\n");
  1094. break;
  1095. case XML_NOTATION_NODE:
  1096. xmlDebugErr(ctxt, XML_CHECK_FOUND_NOTATION,
  1097. "Misplaced NOTATION node\n");
  1098. break;
  1099. default:
  1100. xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
  1101. "Unknown node type %d\n", doc->type);
  1102. }
  1103. }
  1104. /**
  1105. * xmlCtxtDumpDocumentHead:
  1106. * @output: the FILE * for the output
  1107. * @doc: the document
  1108. *
  1109. * Dumps debug information cncerning the document, not recursive
  1110. */
  1111. static void
  1112. xmlCtxtDumpDocumentHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
  1113. {
  1114. if (doc == NULL) return;
  1115. xmlCtxtDumpDocHead(ctxt, doc);
  1116. if (!ctxt->check) {
  1117. if (doc->name != NULL) {
  1118. fprintf(ctxt->output, "name=");
  1119. xmlCtxtDumpString(ctxt, BAD_CAST doc->name);
  1120. fprintf(ctxt->output, "\n");
  1121. }
  1122. if (doc->version != NULL) {
  1123. fprintf(ctxt->output, "version=");
  1124. xmlCtxtDumpString(ctxt, doc->version);
  1125. fprintf(ctxt->output, "\n");
  1126. }
  1127. if (doc->encoding != NULL) {
  1128. fprintf(ctxt->output, "encoding=");
  1129. xmlCtxtDumpString(ctxt, doc->encoding);
  1130. fprintf(ctxt->output, "\n");
  1131. }
  1132. if (doc->URL != NULL) {
  1133. fprintf(ctxt->output, "URL=");
  1134. xmlCtxtDumpString(ctxt, doc->URL);
  1135. fprintf(ctxt->output, "\n");
  1136. }
  1137. if (doc->standalone)
  1138. fprintf(ctxt->output, "standalone=true\n");
  1139. }
  1140. if (doc->oldNs != NULL)
  1141. xmlCtxtDumpNamespaceList(ctxt, doc->oldNs);
  1142. }
  1143. /**
  1144. * xmlCtxtDumpDocument:
  1145. * @output: the FILE * for the output
  1146. * @doc: the document
  1147. *
  1148. * Dumps debug information for the document, it's recursive
  1149. */
  1150. static void
  1151. xmlCtxtDumpDocument(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
  1152. {
  1153. if (doc == NULL) {
  1154. if (!ctxt->check)
  1155. fprintf(ctxt->output, "DOCUMENT == NULL !\n");
  1156. return;
  1157. }
  1158. xmlCtxtDumpDocumentHead(ctxt, doc);
  1159. if (((doc->type == XML_DOCUMENT_NODE) ||
  1160. (doc->type == XML_HTML_DOCUMENT_NODE))
  1161. && (doc->children != NULL)) {
  1162. ctxt->depth++;
  1163. xmlCtxtDumpNodeList(ctxt, doc->children);
  1164. ctxt->depth--;
  1165. }
  1166. }
  1167. static void
  1168. xmlCtxtDumpEntityCallback(xmlEntityPtr cur, xmlDebugCtxtPtr ctxt)
  1169. {
  1170. if (cur == NULL) {
  1171. if (!ctxt->check)
  1172. fprintf(ctxt->output, "Entity is NULL");
  1173. return;
  1174. }
  1175. if (!ctxt->check) {
  1176. fprintf(ctxt->output, "%s : ", (char *) cur->name);
  1177. switch (cur->etype) {
  1178. case XML_INTERNAL_GENERAL_ENTITY:
  1179. fprintf(ctxt->output, "INTERNAL GENERAL, ");
  1180. break;
  1181. case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
  1182. fprintf(ctxt->output, "EXTERNAL PARSED, ");
  1183. break;
  1184. case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
  1185. fprintf(ctxt->output, "EXTERNAL UNPARSED, ");
  1186. break;
  1187. case XML_INTERNAL_PARAMETER_ENTITY:
  1188. fprintf(ctxt->output, "INTERNAL PARAMETER, ");
  1189. break;
  1190. case XML_EXTERNAL_PARAMETER_ENTITY:
  1191. fprintf(ctxt->output, "EXTERNAL PARAMETER, ");
  1192. break;
  1193. default:
  1194. xmlDebugErr2(ctxt, XML_CHECK_ENTITY_TYPE,
  1195. "Unknown entity type %d\n", cur->etype);
  1196. }
  1197. if (cur->ExternalID != NULL)
  1198. fprintf(ctxt->output, "ID \"%s\"", (char *) cur->ExternalID);
  1199. if (cur->SystemID != NULL)
  1200. fprintf(ctxt->output, "SYSTEM \"%s\"", (char *) cur->SystemID);
  1201. if (cur->orig != NULL)
  1202. fprintf(ctxt->output, "\n orig \"%s\"", (char *) cur->orig);
  1203. if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL))
  1204. fprintf(ctxt->output, "\n content \"%s\"",
  1205. (char *) cur->content);
  1206. fprintf(ctxt->output, "\n");
  1207. }
  1208. }
  1209. /**
  1210. * xmlCtxtDumpEntities:
  1211. * @output: the FILE * for the output
  1212. * @doc: the document
  1213. *
  1214. * Dumps debug information for all the entities in use by the document
  1215. */
  1216. static void
  1217. xmlCtxtDumpEntities(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
  1218. {
  1219. if (doc == NULL) return;
  1220. xmlCtxtDumpDocHead(ctxt, doc);
  1221. if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
  1222. xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
  1223. doc->intSubset->entities;
  1224. if (!ctxt->check)
  1225. fprintf(ctxt->output, "Entities in internal subset\n");
  1226. xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
  1227. ctxt);
  1228. } else
  1229. fprintf(ctxt->output, "No entities in internal subset\n");
  1230. if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
  1231. xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
  1232. doc->extSubset->entities;
  1233. if (!ctxt->check)
  1234. fprintf(ctxt->output, "Entities in external subset\n");
  1235. xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
  1236. ctxt);
  1237. } else if (!ctxt->check)
  1238. fprintf(ctxt->output, "No entities in external subset\n");
  1239. }
  1240. /**
  1241. * xmlCtxtDumpDTD:
  1242. * @output: the FILE * for the output
  1243. * @dtd: the DTD
  1244. *
  1245. * Dumps debug information for the DTD
  1246. */
  1247. static void
  1248. xmlCtxtDumpDTD(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
  1249. {
  1250. if (dtd == NULL) {
  1251. if (!ctxt->check)
  1252. fprintf(ctxt->output, "DTD is NULL\n");
  1253. return;
  1254. }
  1255. xmlCtxtDumpDtdNode(ctxt, dtd);
  1256. if (dtd->children == NULL)
  1257. fprintf(ctxt->output, " DTD is empty\n");
  1258. else {
  1259. ctxt->depth++;
  1260. xmlCtxtDumpNodeList(ctxt, dtd->children);
  1261. ctxt->depth--;
  1262. }
  1263. }
  1264. /************************************************************************
  1265. * *
  1266. * Public entry points for dump *
  1267. * *
  1268. ************************************************************************/
  1269. /**
  1270. * xmlDebugDumpString:
  1271. * @output: the FILE * for the output
  1272. * @str: the string
  1273. *
  1274. * Dumps informations about the string, shorten it if necessary
  1275. */
  1276. void
  1277. xmlDebugDumpString(FILE * output, const xmlChar * str)
  1278. {
  1279. int i;
  1280. if (output == NULL)
  1281. output = stdout;
  1282. if (str == NULL) {
  1283. fprintf(output, "(NULL)");
  1284. return;
  1285. }
  1286. for (i = 0; i < 40; i++)
  1287. if (str[i] == 0)
  1288. return;
  1289. else if (IS_BLANK_CH(str[i]))
  1290. fputc(' ', output);
  1291. else if (str[i] >= 0x80)
  1292. fprintf(output, "#%X", str[i]);
  1293. else
  1294. fputc(str[i], output);
  1295. fprintf(output, "...");
  1296. }
  1297. /**
  1298. * xmlDebugDumpAttr:
  1299. * @output: the FILE * for the output
  1300. * @attr: the attribute
  1301. * @depth: the indentation level.
  1302. *
  1303. * Dumps debug information for the attribute
  1304. */
  1305. void
  1306. xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) {
  1307. xmlDebugCtxt ctxt;
  1308. if (output == NULL) return;
  1309. xmlCtxtDumpInitCtxt(&ctxt);
  1310. ctxt.output = output;
  1311. ctxt.depth = depth;
  1312. xmlCtxtDumpAttr(&ctxt, attr);
  1313. xmlCtxtDumpCleanCtxt(&ctxt);
  1314. }
  1315. /**
  1316. * xmlDebugDumpEntities:
  1317. * @output: the FILE * for the output
  1318. * @doc: the document
  1319. *
  1320. * Dumps debug information for all the entities in use by the document
  1321. */
  1322. void
  1323. xmlDebugDumpEntities(FILE * output, xmlDocPtr doc)
  1324. {
  1325. xmlDebugCtxt ctxt;
  1326. if (output == NULL) return;
  1327. xmlCtxtDumpInitCtxt(&ctxt);
  1328. ctxt.output = output;
  1329. xmlCtxtDumpEntities(&ctxt, doc);
  1330. xmlCtxtDumpCleanCtxt(&ctxt);
  1331. }
  1332. /**
  1333. * xmlDebugDumpAttrList:
  1334. * @output: the FILE * for the output
  1335. * @attr: the attribute list
  1336. * @depth: the indentation level.
  1337. *
  1338. * Dumps debug information for the attribute list
  1339. */
  1340. void
  1341. xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth)
  1342. {
  1343. xmlDebugCtxt ctxt;
  1344. if (output == NULL) return;
  1345. xmlCtxtDumpInitCtxt(&ctxt);
  1346. ctxt.output = output;
  1347. ctxt.depth = depth;
  1348. xmlCtxtDumpAttrList(&ctxt, attr);
  1349. xmlCtxtDumpCleanCtxt(&ctxt);
  1350. }
  1351. /**
  1352. * xmlDebugDumpOneNode:
  1353. * @output: the FILE * for the output
  1354. * @node: the node
  1355. * @depth: the indentation level.
  1356. *
  1357. * Dumps debug information for the element node, it is not recursive
  1358. */
  1359. void
  1360. xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth)
  1361. {
  1362. xmlDebugCtxt ctxt;
  1363. if (output == NULL) return;
  1364. xmlCtxtDumpInitCtxt(&ctxt);
  1365. ctxt.output = output;
  1366. ctxt.depth = depth;
  1367. xmlCtxtDumpOneNode(&ctxt, node);
  1368. xmlCtxtDumpCleanCtxt(&ctxt);
  1369. }
  1370. /**
  1371. * xmlDebugDumpNode:
  1372. * @output: the FILE * for the output
  1373. * @node: the node
  1374. * @depth: the indentation level.
  1375. *
  1376. * Dumps debug information for the element node, it is recursive
  1377. */
  1378. void
  1379. xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth)
  1380. {
  1381. xmlDebugCtxt ctxt;
  1382. if (output == NULL)
  1383. output = stdout;
  1384. xmlCtxtDumpInitCtxt(&ctxt);
  1385. ctxt.output = output;
  1386. ctxt.depth = depth;
  1387. xmlCtxtDumpNode(&ctxt, node);
  1388. xmlCtxtDumpCleanCtxt(&ctxt);
  1389. }
  1390. /**
  1391. * xmlDebugDumpNodeList:
  1392. * @output: the FILE * for the output
  1393. * @node: the node list
  1394. * @depth: the indentation level.
  1395. *
  1396. * Dumps debug information for the list of element node, it is recursive
  1397. */
  1398. void
  1399. xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth)
  1400. {
  1401. xmlDebugCtxt ctxt;
  1402. if (output == NULL)
  1403. output = stdout;
  1404. xmlCtxtDumpInitCtxt(&ctxt);
  1405. ctxt.output = output;
  1406. ctxt.depth = depth;
  1407. xmlCtxtDumpNodeList(&ctxt, node);
  1408. xmlCtxtDumpCleanCtxt(&ctxt);
  1409. }
  1410. /**
  1411. * xmlDebugDumpDocumentHead:
  1412. * @output: the FILE * for the output
  1413. * @doc: the document
  1414. *
  1415. * Dumps debug information cncerning the document, not recursive
  1416. */
  1417. void
  1418. xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc)
  1419. {
  1420. xmlDebugCtxt ctxt;
  1421. if (output == NULL)
  1422. output = stdout;
  1423. xmlCtxtDumpInitCtxt(&ctxt);
  1424. ctxt.options |= DUMP_TEXT_TYPE;
  1425. ctxt.output = output;
  1426. xmlCtxtDumpDocumentHead(&ctxt, doc);
  1427. xmlCtxtDumpCleanCtxt(&ctxt);
  1428. }
  1429. /**
  1430. * xmlDebugDumpDocument:
  1431. * @output: the FILE * for the output
  1432. * @doc: the document
  1433. *
  1434. * Dumps debug information for the document, it's recursive
  1435. */
  1436. void
  1437. xmlDebugDumpDocument(FILE * output, xmlDocPtr doc)
  1438. {
  1439. xmlDebugCtxt ctxt;
  1440. if (output == NULL)
  1441. output = stdout;
  1442. xmlCtxtDumpInitCtxt(&ctxt);
  1443. ctxt.options |= DUMP_TEXT_TYPE;
  1444. ctxt.output = output;
  1445. xmlCtxtDumpDocument(&ctxt, doc);
  1446. xmlCtxtDumpCleanCtxt(&ctxt);
  1447. }
  1448. /**
  1449. * xmlDebugDumpDTD:
  1450. * @output: the FILE * for the output
  1451. * @dtd: the DTD
  1452. *
  1453. * Dumps debug information for the DTD
  1454. */
  1455. void
  1456. xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd)
  1457. {
  1458. xmlDebugCtxt ctxt;
  1459. if (output == NULL)
  1460. output = stdout;
  1461. xmlCtxtDumpInitCtxt(&ctxt);
  1462. ctxt.options |= DUMP_TEXT_TYPE;
  1463. ctxt.output = output;
  1464. xmlCtxtDumpDTD(&ctxt, dtd);
  1465. xmlCtxtDumpCleanCtxt(&ctxt);
  1466. }
  1467. /************************************************************************
  1468. * *
  1469. * Public entry points for checkings *
  1470. * *
  1471. ************************************************************************/
  1472. /**
  1473. * xmlDebugCheckDocument:
  1474. * @output: the FILE * for the output
  1475. * @doc: the document
  1476. *
  1477. * Check the document for potential content problems, and output
  1478. * the errors to @output
  1479. *
  1480. * Returns the number of errors found
  1481. */
  1482. int
  1483. xmlDebugCheckDocument(FILE * output, xmlDocPtr doc)
  1484. {
  1485. xmlDebugCtxt ctxt;
  1486. if (output == NULL)
  1487. output = stdout;
  1488. xmlCtxtDumpInitCtxt(&ctxt);
  1489. ctxt.output = output;
  1490. ctxt.check = 1;
  1491. xmlCtxtDumpDocument(&ctxt, doc);
  1492. xmlCtxtDumpCleanCtxt(&ctxt);
  1493. return(ctxt.errors);
  1494. }
  1495. /************************************************************************
  1496. * *
  1497. * Helpers for Shell *
  1498. * *
  1499. ************************************************************************/
  1500. /**
  1501. * xmlLsCountNode:
  1502. * @node: the node to count
  1503. *
  1504. * Count the children of @node.
  1505. *
  1506. * Returns the number of children of @node.
  1507. */
  1508. int
  1509. xmlLsCountNode(xmlNodePtr node) {
  1510. int ret = 0;
  1511. xmlNodePtr list = NULL;
  1512. if (node == NULL)
  1513. return(0);
  1514. switch (node->type) {
  1515. case XML_ELEMENT_NODE:
  1516. list = node->children;
  1517. break;
  1518. case XML_DOCUMENT_NODE:
  1519. case XML_HTML_DOCUMENT_NODE:
  1520. #ifdef LIBXML_DOCB_ENABLED
  1521. case XML_DOCB_DOCUMENT_NODE:
  1522. #endif
  1523. list = ((xmlDocPtr) node)->children;
  1524. break;
  1525. case XML_ATTRIBUTE_NODE:
  1526. list = ((xmlAttrPtr) node)->children;
  1527. break;
  1528. case XML_TEXT_NODE:
  1529. case XML_CDATA_SECTION_NODE:
  1530. case XML_PI_NODE:
  1531. case XML_COMMENT_NODE:
  1532. if (node->content != NULL) {
  1533. ret = xmlStrlen(node->content);
  1534. }
  1535. break;
  1536. case XML_ENTITY_REF_NODE:
  1537. case XML_DOCUMENT_TYPE_NODE:
  1538. case XML_ENTITY_NODE:
  1539. case XML_DOCUMENT_FRAG_NODE:
  1540. case XML_NOTATION_NODE:
  1541. case XML_DTD_NODE:
  1542. case XML_ELEMENT_DECL:
  1543. case XML_ATTRIBUTE_DECL:
  1544. case XML_ENTITY_DECL:
  1545. case XML_NAMESPACE_DECL:
  1546. case XML_XINCLUDE_START:
  1547. case XML_XINCLUDE_END:
  1548. ret = 1;
  1549. break;
  1550. }
  1551. for (;list != NULL;ret++)
  1552. list = list->next;
  1553. return(ret);
  1554. }
  1555. /**
  1556. * xmlLsOneNode:
  1557. * @output: the FILE * for the output
  1558. * @node: the node to dump
  1559. *
  1560. * Dump to @output the type and name of @node.
  1561. */
  1562. void
  1563. xmlLsOneNode(FILE *output, xmlNodePtr node) {
  1564. if (output == NULL) return;
  1565. if (node == NULL) {
  1566. fprintf(output, "NULL\n");
  1567. return;
  1568. }
  1569. switch (node->type) {
  1570. case XML_ELEMENT_NODE:
  1571. fprintf(output, "-");
  1572. break;
  1573. case XML_ATTRIBUTE_NODE:
  1574. fprintf(output, "a");
  1575. break;
  1576. case XML_TEXT_NODE:
  1577. fprintf(output, "t");
  1578. break;
  1579. case XML_CDATA_SECTION_NODE:
  1580. fprintf(output, "C");
  1581. break;
  1582. case XML_ENTITY_REF_NODE:
  1583. fprintf(output, "e");
  1584. break;
  1585. case XML_ENTITY_NODE:
  1586. fprintf(output, "E");
  1587. break;
  1588. case XML_PI_NODE:
  1589. fprintf(output, "p");
  1590. break;
  1591. case XML_COMMENT_NODE:
  1592. fprintf(output, "c");
  1593. break;
  1594. case XML_DOCUMENT_NODE:
  1595. fprintf(output, "d");
  1596. break;
  1597. case XML_HTML_DOCUMENT_NODE:
  1598. fprintf(output, "h");
  1599. break;
  1600. case XML_DOCUMENT_TYPE_NODE:
  1601. fprintf(output, "T");
  1602. break;
  1603. case XML_DOCUMENT_FRAG_NODE:
  1604. fprintf(output, "F");
  1605. break;
  1606. case XML_NOTATION_NODE:
  1607. fprintf(output, "N");
  1608. break;
  1609. case XML_NAMESPACE_DECL:
  1610. fprintf(output, "n");
  1611. break;
  1612. default:
  1613. fprintf(output, "?");
  1614. }
  1615. if (node->type != XML_NAMESPACE_DECL) {
  1616. if (node->properties != NULL)
  1617. fprintf(output, "a");
  1618. else
  1619. fprintf(output, "-");
  1620. if (node->nsDef != NULL)
  1621. fprintf(output, "n");
  1622. else
  1623. fprintf(output, "-");
  1624. }
  1625. fprintf(output, " %8d ", xmlLsCountNode(node));
  1626. switch (node->type) {
  1627. case XML_ELEMENT_NODE:
  1628. if (node->name != NULL)
  1629. fprintf(output, "%s", (const char *) node->name);
  1630. break;
  1631. case XML_ATTRIBUTE_NODE:
  1632. if (node->name != NULL)
  1633. fprintf(output, "%s", (const char *) node->name);
  1634. break;
  1635. case XML_TEXT_NODE:
  1636. if (node->content != NULL) {
  1637. xmlDebugDumpString(output, node->content);
  1638. }
  1639. break;
  1640. case XML_CDATA_SECTION_NODE:
  1641. break;
  1642. case XML_ENTITY_REF_NODE:
  1643. if (node->name != NULL)
  1644. fprintf(output, "%s", (const char *) node->name);
  1645. break;
  1646. case XML_ENTITY_NODE:
  1647. if (node->name != NULL)
  1648. fprintf(output, "%s", (const char *) node->name);
  1649. break;
  1650. case XML_PI_NODE:
  1651. if (node->name != NULL)
  1652. fprintf(output, "%s", (const char *) node->name);
  1653. break;
  1654. case XML_COMMENT_NODE:
  1655. break;
  1656. case XML_DOCUMENT_NODE:
  1657. break;
  1658. case XML_HTML_DOCUMENT_NODE:
  1659. break;
  1660. case XML_DOCUMENT_TYPE_NODE:
  1661. break;
  1662. case XML_DOCUMENT_FRAG_NODE:
  1663. break;
  1664. case XML_NOTATION_NODE:
  1665. break;
  1666. case XML_NAMESPACE_DECL: {
  1667. xmlNsPtr ns = (xmlNsPtr) node;
  1668. if (ns->prefix == NULL)
  1669. fprintf(output, "default -> %s", (char *)ns->href);
  1670. else
  1671. fprintf(output, "%s -> %s", (char *)ns->prefix,
  1672. (char *)ns->href);
  1673. break;
  1674. }
  1675. default:
  1676. if (node->name != NULL)
  1677. fprintf(output, "%s", (const char *) node->name);
  1678. }
  1679. fprintf(output, "\n");
  1680. }
  1681. /**
  1682. * xmlBoolToText:
  1683. * @boolval: a bool to turn into text
  1684. *
  1685. * Convenient way to turn bool into text
  1686. *
  1687. * Returns a pointer to either "True" or "False"
  1688. */
  1689. const char *
  1690. xmlBoolToText(int boolval)
  1691. {
  1692. if (boolval)
  1693. return("True");
  1694. else
  1695. return("False");
  1696. }
  1697. #ifdef LIBXML_XPATH_ENABLED
  1698. /****************************************************************
  1699. * *
  1700. * The XML shell related functions *
  1701. * *
  1702. ****************************************************************/
  1703. /*
  1704. * TODO: Improvement/cleanups for the XML shell
  1705. * - allow to shell out an editor on a subpart
  1706. * - cleanup function registrations (with help) and calling
  1707. * - provide registration routines
  1708. */
  1709. /**
  1710. * xmlShellPrintXPathError:
  1711. * @errorType: valid xpath error id
  1712. * @arg: the argument that cause xpath to fail
  1713. *
  1714. * Print the xpath error to libxml default error channel
  1715. */
  1716. void
  1717. xmlShellPrintXPathError(int errorType, const char *arg)
  1718. {
  1719. const char *default_arg = "Result";
  1720. if (!arg)
  1721. arg = default_arg;
  1722. switch (errorType) {
  1723. case XPATH_UNDEFINED:
  1724. xmlGenericError(xmlGenericErrorContext,
  1725. "%s: no such node\n", arg);
  1726. break;
  1727. case XPATH_BOOLEAN:
  1728. xmlGenericError(xmlGenericErrorContext,
  1729. "%s is a Boolean\n", arg);
  1730. break;
  1731. case XPATH_NUMBER:
  1732. xmlGenericError(xmlGenericErrorContext,
  1733. "%s is a number\n", arg);
  1734. break;
  1735. case XPATH_STRING:
  1736. xmlGenericError(xmlGenericErrorContext,
  1737. "%s is a string\n", arg);
  1738. break;
  1739. case XPATH_POINT:
  1740. xmlGenericError(xmlGenericErrorContext,
  1741. "%s is a point\n", arg);
  1742. break;
  1743. case XPATH_RANGE:
  1744. xmlGenericError(xmlGenericErrorContext,
  1745. "%s is a range\n", arg);
  1746. break;
  1747. case XPATH_LOCATIONSET:
  1748. xmlGenericError(xmlGenericErrorContext,
  1749. "%s is a range\n", arg);
  1750. break;
  1751. case XPATH_USERS:
  1752. xmlGenericError(xmlGenericErrorContext,
  1753. "%s is user-defined\n", arg);
  1754. break;
  1755. case XPATH_XSLT_TREE:
  1756. xmlGenericError(xmlGenericErrorContext,
  1757. "%s is an XSLT value tree\n", arg);
  1758. break;
  1759. }
  1760. #if 0
  1761. xmlGenericError(xmlGenericErrorContext,
  1762. "Try casting the result string function (xpath builtin)\n",
  1763. arg);
  1764. #endif
  1765. }
  1766. #ifdef LIBXML_OUTPUT_ENABLED
  1767. /**
  1768. * xmlShellPrintNodeCtxt:
  1769. * @ctxt : a non-null shell context
  1770. * @node : a non-null node to print to the output FILE
  1771. *
  1772. * Print node to the output FILE
  1773. */
  1774. static void
  1775. xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node)
  1776. {
  1777. FILE *fp;
  1778. if (!node)
  1779. return;
  1780. if (ctxt == NULL)
  1781. fp = stdout;
  1782. else
  1783. fp = ctxt->output;
  1784. if (node->type == XML_DOCUMENT_NODE)
  1785. xmlDocDump(fp, (xmlDocPtr) node);
  1786. else if (node->type == XML_ATTRIBUTE_NODE)
  1787. xmlDebugDumpAttrList(fp, (xmlAttrPtr) node, 0);
  1788. else
  1789. xmlElemDump(fp, node->doc, node);
  1790. fprintf(fp, "\n");
  1791. }
  1792. /**
  1793. * xmlShellPrintNode:
  1794. * @node : a non-null node to print to the output FILE
  1795. *
  1796. * Print node to the output FILE
  1797. */
  1798. void
  1799. xmlShellPrintNode(xmlNodePtr node)
  1800. {
  1801. xmlShellPrintNodeCtxt(NULL, node);
  1802. }
  1803. #endif /* LIBXML_OUTPUT_ENABLED */
  1804. /**
  1805. * xmlShellPrintXPathResultCtxt:
  1806. * @ctxt: a valid shell context
  1807. * @list: a valid result generated by an xpath evaluation
  1808. *
  1809. * Prints result to the output FILE
  1810. */
  1811. static void
  1812. xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list)
  1813. {
  1814. if (!ctxt)
  1815. return;
  1816. if (list != NULL) {
  1817. switch (list->type) {
  1818. case XPATH_NODESET:{
  1819. #ifdef LIBXML_OUTPUT_ENABLED
  1820. int indx;
  1821. if (list->nodesetval) {
  1822. for (indx = 0; indx < list->nodesetval->nodeNr;
  1823. indx++) {
  1824. xmlShellPrintNodeCtxt(ctxt,
  1825. list->nodesetval->nodeTab[indx]);
  1826. }
  1827. } else {
  1828. xmlGenericError(xmlGenericErrorContext,
  1829. "Empty node set\n");
  1830. }
  1831. break;
  1832. #else
  1833. xmlGenericError(xmlGenericErrorContext,
  1834. "Node set\n");
  1835. #endif /* LIBXML_OUTPUT_ENABLED */
  1836. }
  1837. case XPATH_BOOLEAN:
  1838. xmlGenericError(xmlGenericErrorContext,
  1839. "Is a Boolean:%s\n",
  1840. xmlBoolToText(list->boolval));
  1841. break;
  1842. case XPATH_NUMBER:
  1843. xmlGenericError(xmlGenericErrorContext,
  1844. "Is a number:%0g\n", list->floatval);
  1845. break;
  1846. case XPATH_STRING:
  1847. xmlGenericError(xmlGenericErrorContext,
  1848. "Is a string:%s\n", list->stringval);
  1849. break;
  1850. default:
  1851. xmlShellPrintXPathError(list->type, NULL);
  1852. }
  1853. }
  1854. }
  1855. /**
  1856. * xmlShellPrintXPathResult:
  1857. * @list: a valid result generated by an xpath evaluation
  1858. *
  1859. * Prints result to the output FILE
  1860. */
  1861. void
  1862. xmlShellPrintXPathResult(xmlXPathObjectPtr list)
  1863. {
  1864. xmlShellPrintXPathResultCtxt(NULL, list);
  1865. }
  1866. /**
  1867. * xmlShellList:
  1868. * @ctxt: the shell context
  1869. * @arg: unused
  1870. * @node: a node
  1871. * @node2: unused
  1872. *
  1873. * Implements the XML shell function "ls"
  1874. * Does an Unix like listing of the given node (like a directory)
  1875. *
  1876. * Returns 0
  1877. */
  1878. int
  1879. xmlShellList(xmlShellCtxtPtr ctxt,
  1880. char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
  1881. xmlNodePtr node2 ATTRIBUTE_UNUSED)
  1882. {
  1883. xmlNodePtr cur;
  1884. if (!ctxt)
  1885. return (0);
  1886. if (node == NULL) {
  1887. fprintf(ctxt->output, "NULL\n");
  1888. return (0);
  1889. }
  1890. if ((node->type == XML_DOCUMENT_NODE) ||
  1891. (node->type == XML_HTML_DOCUMENT_NODE)) {
  1892. cur = ((xmlDocPtr) node)->children;
  1893. } else if (node->type == XML_NAMESPACE_DECL) {
  1894. xmlLsOneNode(ctxt->output, node);
  1895. return (0);
  1896. } else if (node->children != NULL) {
  1897. cur = node->children;
  1898. } else {
  1899. xmlLsOneNode(ctxt->output, node);
  1900. return (0);
  1901. }
  1902. while (cur != NULL) {
  1903. xmlLsOneNode(ctxt->output, cur);
  1904. cur = cur->next;
  1905. }
  1906. return (0);
  1907. }
  1908. /**
  1909. * xmlShellBase:
  1910. * @ctxt: the shell context
  1911. * @arg: unused
  1912. * @node: a node
  1913. * @node2: unused
  1914. *
  1915. * Implements the XML shell function "base"
  1916. * dumps the current XML base of the node
  1917. *
  1918. * Returns 0
  1919. */
  1920. int
  1921. xmlShellBase(xmlShellCtxtPtr ctxt,
  1922. char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
  1923. xmlNodePtr node2 ATTRIBUTE_UNUSED)
  1924. {
  1925. xmlChar *base;
  1926. if (!ctxt)
  1927. return 0;
  1928. if (node == NULL) {
  1929. fprintf(ctxt->output, "NULL\n");
  1930. return (0);
  1931. }
  1932. base = xmlNodeGetBase(node->doc, node);
  1933. if (base == NULL) {
  1934. fprintf(ctxt->output, " No base found !!!\n");
  1935. } else {
  1936. fprintf(ctxt->output, "%s\n", base);
  1937. xmlFree(base);
  1938. }
  1939. return (0);
  1940. }
  1941. #ifdef LIBXML_TREE_ENABLED
  1942. /**
  1943. * xmlShellSetBase:
  1944. * @ctxt: the shell context
  1945. * @arg: the new base
  1946. * @node: a node
  1947. * @node2: unused
  1948. *
  1949. * Implements the XML shell function "setbase"
  1950. * change the current XML base of the node
  1951. *
  1952. * Returns 0
  1953. */
  1954. static int
  1955. xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
  1956. char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
  1957. xmlNodePtr node2 ATTRIBUTE_UNUSED)
  1958. {
  1959. xmlNodeSetBase(node, (xmlChar*) arg);
  1960. return (0);
  1961. }
  1962. #endif
  1963. #ifdef LIBXML_XPATH_ENABLED
  1964. /**
  1965. * xmlShellRegisterNamespace:
  1966. * @ctxt: the shell context
  1967. * @arg: a string in prefix=nsuri format
  1968. * @node: unused
  1969. * @node2: unused
  1970. *
  1971. * Implements the XML shell function "setns"
  1972. * register/unregister a prefix=namespace pair
  1973. * on the XPath context
  1974. *
  1975. * Returns 0 on success and a negative value otherwise.
  1976. */
  1977. static int
  1978. xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt, char *arg,
  1979. xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr node2 ATTRIBUTE_UNUSED)
  1980. {
  1981. xmlChar* nsListDup;
  1982. xmlChar* prefix;
  1983. xmlChar* href;
  1984. xmlChar* next;
  1985. nsListDup = xmlStrdup((xmlChar *) arg);
  1986. next = nsListDup;
  1987. while(next != NULL) {
  1988. /* skip spaces */
  1989. /*while((*next) == ' ') next++;*/
  1990. if((*next) == '\0') break;
  1991. /* find prefix */
  1992. prefix = next;
  1993. next = (xmlChar*)xmlStrchr(next, '=');
  1994. if(next == NULL) {
  1995. fprintf(ctxt->output, "setns: prefix=[nsuri] required\n");
  1996. xmlFree(nsListDup);
  1997. return(-1);
  1998. }
  1999. *(next++) = '\0';
  2000. /* find href */
  2001. href = next;
  2002. next = (xmlChar*)xmlStrchr(next, ' ');
  2003. if(next != NULL) {
  2004. *(next++) = '\0';
  2005. }
  2006. /* do register namespace */
  2007. if(xmlXPathRegisterNs(ctxt->pctxt, prefix, href) != 0) {
  2008. fprintf(ctxt->output,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, href);
  2009. xmlFree(nsListDup);
  2010. return(-1);
  2011. }
  2012. }
  2013. xmlFree(nsListDup);
  2014. return(0);
  2015. }
  2016. /**
  2017. * xmlShellRegisterRootNamespaces:
  2018. * @ctxt: the shell context
  2019. * @arg: unused
  2020. * @node: the root element
  2021. * @node2: unused
  2022. *
  2023. * Implements the XML shell function "setrootns"
  2024. * which registers all namespaces declarations found on the root element.
  2025. *
  2026. * Returns 0 on success and a negative value otherwise.
  2027. */
  2028. static int
  2029. xmlShellRegisterRootNamespaces(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
  2030. xmlNodePtr root, xmlNodePtr node2 ATTRIBUTE_UNUSED)
  2031. {
  2032. xmlNsPtr ns;
  2033. if ((root == NULL) || (root->type != XML_ELEMENT_NODE) ||
  2034. (root->nsDef == NULL) || (ctxt == NULL) || (ctxt->pctxt == NULL))
  2035. return(-1);
  2036. ns = root->nsDef;
  2037. while (ns != NULL) {
  2038. if (ns->prefix == NULL)
  2039. xmlXPathRegisterNs(ctxt->pctxt, BAD_CAST "defaultns", ns->href);
  2040. else
  2041. xmlXPathRegisterNs(ctxt->pctxt, ns->prefix, ns->href);
  2042. ns = ns->next;
  2043. }
  2044. return(0);
  2045. }
  2046. #endif
  2047. /**
  2048. * xmlShellGrep:
  2049. * @ctxt: the shell context
  2050. * @arg: the string or regular expression to find
  2051. * @node: a node
  2052. * @node2: unused
  2053. *
  2054. * Implements the XML shell function "grep"
  2055. * dumps informations about the node (namespace, attributes, content).
  2056. *
  2057. * Returns 0
  2058. */
  2059. static int
  2060. xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
  2061. char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
  2062. {
  2063. if (!ctxt)
  2064. return (0);
  2065. if (node == NULL)
  2066. return (0);
  2067. if (arg == NULL)
  2068. return (0);
  2069. #ifdef LIBXML_REGEXP_ENABLED
  2070. if ((xmlStrchr((xmlChar *) arg, '?')) ||
  2071. (xmlStrchr((xmlChar *) arg, '*')) ||
  2072. (xmlStrchr((xmlChar *) arg, '.')) ||
  2073. (xmlStrchr((xmlChar *) arg, '['))) {
  2074. }
  2075. #endif
  2076. while (node != NULL) {
  2077. if (node->type == XML_COMMENT_NODE) {
  2078. if (xmlStrstr(node->content, (xmlChar *) arg)) {
  2079. fprintf(ctxt->output, "%s : ", xmlGetNodePath(node));
  2080. xmlShellList(ctxt, NULL, node, NULL);
  2081. }
  2082. } else if (node->type == XML_TEXT_NODE) {
  2083. if (xmlStrstr(node->content, (xmlChar *) arg)) {
  2084. fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent));
  2085. xmlShellList(ctxt, NULL, node->parent, NULL);
  2086. }
  2087. }
  2088. /*
  2089. * Browse the full subtree, deep first
  2090. */
  2091. if ((node->type == XML_DOCUMENT_NODE) ||
  2092. (node->type == XML_HTML_DOCUMENT_NODE)) {
  2093. node = ((xmlDocPtr) node)->children;
  2094. } else if ((node->children != NULL)
  2095. && (node->type != XML_ENTITY_REF_NODE)) {
  2096. /* deep first */
  2097. node = node->children;
  2098. } else if (node->next != NULL) {
  2099. /* then siblings */
  2100. node = node->next;
  2101. } else {
  2102. /* go up to parents->next if needed */
  2103. while (node != NULL) {
  2104. if (node->parent != NULL) {
  2105. node = node->parent;
  2106. }
  2107. if (node->next != NULL) {
  2108. node = node->next;
  2109. break;
  2110. }
  2111. if (node->parent == NULL) {
  2112. node = NULL;
  2113. break;
  2114. }
  2115. }
  2116. }
  2117. }
  2118. return (0);
  2119. }
  2120. /**
  2121. * xmlShellDir:
  2122. * @ctxt: the shell context
  2123. * @arg: unused
  2124. * @node: a node
  2125. * @node2: unused
  2126. *
  2127. * Implements the XML shell function "dir"
  2128. * dumps informations about the node (namespace, attributes, content).
  2129. *
  2130. * Returns 0
  2131. */
  2132. int
  2133. xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
  2134. char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
  2135. xmlNodePtr node2 ATTRIBUTE_UNUSED)
  2136. {
  2137. if (!ctxt)
  2138. return (0);
  2139. if (node == NULL) {
  2140. fprintf(ctxt->output, "NULL\n");
  2141. return (0);
  2142. }
  2143. if ((node->type == XML_DOCUMENT_NODE) ||
  2144. (node->type == XML_HTML_DOCUMENT_NODE)) {
  2145. xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node);
  2146. } else if (node->type == XML_ATTRIBUTE_NODE) {
  2147. xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0);
  2148. } else {
  2149. xmlDebugDumpOneNode(ctxt->output, node, 0);
  2150. }
  2151. return (0);
  2152. }
  2153. /**
  2154. * xmlShellSetContent:
  2155. * @ctxt: the shell context
  2156. * @value: the content as a string
  2157. * @node: a node
  2158. * @node2: unused
  2159. *
  2160. * Implements the XML shell function "dir"
  2161. * dumps informations about the node (namespace, attributes, content).
  2162. *
  2163. * Returns 0
  2164. */
  2165. static int
  2166. xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
  2167. char *value, xmlNodePtr node,
  2168. xmlNodePtr node2 ATTRIBUTE_UNUSED)
  2169. {
  2170. xmlNodePtr results;
  2171. xmlParserErrors ret;
  2172. if (!ctxt)
  2173. return (0);
  2174. if (node == NULL) {
  2175. fprintf(ctxt->output, "NULL\n");
  2176. return (0);
  2177. }
  2178. if (value == NULL) {
  2179. fprintf(ctxt->output, "NULL\n");
  2180. return (0);
  2181. }
  2182. ret = xmlParseInNodeContext(node, value, strlen(value), 0, &results);
  2183. if (ret == XML_ERR_OK) {
  2184. if (node->children != NULL) {
  2185. xmlFreeNodeList(node->children);
  2186. node->children = NULL;
  2187. node->last = NULL;
  2188. }
  2189. xmlAddChildList(node, results);
  2190. } else {
  2191. fprintf(ctxt->output, "failed to parse content\n");
  2192. }
  2193. return (0);
  2194. }
  2195. #ifdef LIBXML_SCHEMAS_ENABLED
  2196. /**
  2197. * xmlShellRNGValidate:
  2198. * @ctxt: the shell context
  2199. * @schemas: the path to the Relax-NG schemas
  2200. * @node: a node
  2201. * @node2: unused
  2202. *
  2203. * Implements the XML shell function "relaxng"
  2204. * validating the instance against a Relax-NG schemas
  2205. *
  2206. * Returns 0
  2207. */
  2208. static int
  2209. xmlShellRNGValidate(xmlShellCtxtPtr sctxt, char *schemas,
  2210. xmlNodePtr node ATTRIBUTE_UNUSED,
  2211. xmlNodePtr node2 ATTRIBUTE_UNUSED)
  2212. {
  2213. xmlRelaxNGPtr relaxngschemas;
  2214. xmlRelaxNGParserCtxtPtr ctxt;
  2215. xmlRelaxNGValidCtxtPtr vctxt;
  2216. int ret;
  2217. ctxt = xmlRelaxNGNewParserCtxt(schemas);
  2218. xmlRelaxNGSetParserErrors(ctxt,
  2219. (xmlRelaxNGValidityErrorFunc) fprintf,
  2220. (xmlRelaxNGValidityWarningFunc) fprintf,
  2221. stderr);
  2222. relaxngschemas = xmlRelaxNGParse(ctxt);
  2223. xmlRelaxNGFreeParserCtxt(ctxt);
  2224. if (relaxngschemas == NULL) {
  2225. xmlGenericError(xmlGenericErrorContext,
  2226. "Relax-NG schema %s failed to compile\n", schemas);
  2227. return(-1);
  2228. }
  2229. vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
  2230. xmlRelaxNGSetValidErrors(vctxt,
  2231. (xmlRelaxNGValidityErrorFunc) fprintf,
  2232. (xmlRelaxNGValidityWarningFunc) fprintf,
  2233. stderr);
  2234. ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc);
  2235. if (ret == 0) {
  2236. fprintf(stderr, "%s validates\n", sctxt->filename);
  2237. } else if (ret > 0) {
  2238. fprintf(stderr, "%s fails to validate\n", sctxt->filename);
  2239. } else {
  2240. fprintf(stderr, "%s validation generated an internal error\n",
  2241. sctxt->filename);
  2242. }
  2243. xmlRelaxNGFreeValidCtxt(vctxt);
  2244. if (relaxngschemas != NULL)
  2245. xmlRelaxNGFree(relaxngschemas);
  2246. return(0);
  2247. }
  2248. #endif
  2249. #ifdef LIBXML_OUTPUT_ENABLED
  2250. /**
  2251. * xmlShellCat:
  2252. * @ctxt: the shell context
  2253. * @arg: unused
  2254. * @node: a node
  2255. * @node2: unused
  2256. *
  2257. * Implements the XML shell function "cat"
  2258. * dumps the serialization node content (XML or HTML).
  2259. *
  2260. * Returns 0
  2261. */
  2262. int
  2263. xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
  2264. xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
  2265. {
  2266. if (!ctxt)
  2267. return (0);
  2268. if (node == NULL) {
  2269. fprintf(ctxt->output, "NULL\n");
  2270. return (0);
  2271. }
  2272. if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
  2273. #ifdef LIBXML_HTML_ENABLED
  2274. if (node->type == XML_HTML_DOCUMENT_NODE)
  2275. htmlDocDump(ctxt->output, (htmlDocPtr) node);
  2276. else
  2277. htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
  2278. #else
  2279. if (node->type == XML_DOCUMENT_NODE)
  2280. xmlDocDump(ctxt->output, (xmlDocPtr) node);
  2281. else
  2282. xmlElemDump(ctxt->output, ctxt->doc, node);
  2283. #endif /* LIBXML_HTML_ENABLED */
  2284. } else {
  2285. if (node->type == XML_DOCUMENT_NODE)
  2286. xmlDocDump(ctxt->output, (xmlDocPtr) node);
  2287. else
  2288. xmlElemDump(ctxt->output, ctxt->doc, node);
  2289. }
  2290. fprintf(ctxt->output, "\n");
  2291. return (0);
  2292. }
  2293. #endif /* LIBXML_OUTPUT_ENABLED */
  2294. /**
  2295. * xmlShellLoad:
  2296. * @ctxt: the shell context
  2297. * @filename: the file name
  2298. * @node: unused
  2299. * @node2: unused
  2300. *
  2301. * Implements the XML shell function "load"
  2302. * loads a new document specified by the filename
  2303. *
  2304. * Returns 0 or -1 if loading failed
  2305. */
  2306. int
  2307. xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
  2308. xmlNodePtr node ATTRIBUTE_UNUSED,
  2309. xmlNodePtr node2 ATTRIBUTE_UNUSED)
  2310. {
  2311. xmlDocPtr doc;
  2312. int html = 0;
  2313. if ((ctxt == NULL) || (filename == NULL)) return(-1);
  2314. if (ctxt->doc != NULL)
  2315. html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
  2316. if (html) {
  2317. #ifdef LIBXML_HTML_ENABLED
  2318. doc = htmlParseFile(filename, NULL);
  2319. #else
  2320. fprintf(ctxt->output, "HTML support not compiled in\n");
  2321. doc = NULL;
  2322. #endif /* LIBXML_HTML_ENABLED */
  2323. } else {
  2324. doc = xmlReadFile(filename,NULL,0);
  2325. }
  2326. if (doc != NULL) {
  2327. if (ctxt->loaded == 1) {
  2328. xmlFreeDoc(ctxt->doc);
  2329. }
  2330. ctxt->loaded = 1;
  2331. #ifdef LIBXML_XPATH_ENABLED
  2332. xmlXPathFreeContext(ctxt->pctxt);
  2333. #endif /* LIBXML_XPATH_ENABLED */
  2334. xmlFree(ctxt->filename);
  2335. ctxt->doc = doc;
  2336. ctxt->node = (xmlNodePtr) doc;
  2337. #ifdef LIBXML_XPATH_ENABLED
  2338. ctxt->pctxt = xmlXPathNewContext(doc);
  2339. #endif /* LIBXML_XPATH_ENABLED */
  2340. ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename);
  2341. } else
  2342. return (-1);
  2343. return (0);
  2344. }
  2345. #ifdef LIBXML_OUTPUT_ENABLED
  2346. /**
  2347. * xmlShellWrite:
  2348. * @ctxt: the shell context
  2349. * @filename: the file name
  2350. * @node: a node in the tree
  2351. * @node2: unused
  2352. *
  2353. * Implements the XML shell function "write"
  2354. * Write the current node to the filename, it saves the serialization
  2355. * of the subtree under the @node specified
  2356. *
  2357. * Returns 0 or -1 in case of error
  2358. */
  2359. int
  2360. xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
  2361. xmlNodePtr node2 ATTRIBUTE_UNUSED)
  2362. {
  2363. if (node == NULL)
  2364. return (-1);
  2365. if ((filename == NULL) || (filename[0] == 0)) {
  2366. return (-1);
  2367. }
  2368. #ifdef W_OK
  2369. if (access((char *) filename, W_OK)) {
  2370. xmlGenericError(xmlGenericErrorContext,
  2371. "Cannot write to %s\n", filename);
  2372. return (-1);
  2373. }
  2374. #endif
  2375. switch (node->type) {
  2376. case XML_DOCUMENT_NODE:
  2377. if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
  2378. xmlGenericError(xmlGenericErrorContext,
  2379. "Failed to write to %s\n", filename);
  2380. return (-1);
  2381. }
  2382. break;
  2383. case XML_HTML_DOCUMENT_NODE:
  2384. #ifdef LIBXML_HTML_ENABLED
  2385. if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
  2386. xmlGenericError(xmlGenericErrorContext,
  2387. "Failed to write to %s\n", filename);
  2388. return (-1);
  2389. }
  2390. #else
  2391. if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
  2392. xmlGenericError(xmlGenericErrorContext,
  2393. "Failed to write to %s\n", filename);
  2394. return (-1);
  2395. }
  2396. #endif /* LIBXML_HTML_ENABLED */
  2397. break;
  2398. default:{
  2399. FILE *f;
  2400. f = fopen((char *) filename, "w");
  2401. if (f == NULL) {
  2402. xmlGenericError(xmlGenericErrorContext,
  2403. "Failed to write to %s\n", filename);
  2404. return (-1);
  2405. }
  2406. xmlElemDump(f, ctxt->doc, node);
  2407. fclose(f);
  2408. }
  2409. }
  2410. return (0);
  2411. }
  2412. /**
  2413. * xmlShellSave:
  2414. * @ctxt: the shell context
  2415. * @filename: the file name (optional)
  2416. * @node: unused
  2417. * @node2: unused
  2418. *
  2419. * Implements the XML shell function "save"
  2420. * Write the current document to the filename, or it's original name
  2421. *
  2422. * Returns 0 or -1 in case of error
  2423. */
  2424. int
  2425. xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
  2426. xmlNodePtr node ATTRIBUTE_UNUSED,
  2427. xmlNodePtr node2 ATTRIBUTE_UNUSED)
  2428. {
  2429. if ((ctxt == NULL) || (ctxt->doc == NULL))
  2430. return (-1);
  2431. if ((filename == NULL) || (filename[0] == 0))
  2432. filename = ctxt->filename;
  2433. if (filename == NULL)
  2434. return (-1);
  2435. #ifdef W_OK
  2436. if (access((char *) filename, W_OK)) {
  2437. xmlGenericError(xmlGenericErrorContext,
  2438. "Cannot save to %s\n", filename);
  2439. return (-1);
  2440. }
  2441. #endif
  2442. switch (ctxt->doc->type) {
  2443. case XML_DOCUMENT_NODE:
  2444. if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
  2445. xmlGenericError(xmlGenericErrorContext,
  2446. "Failed to save to %s\n", filename);
  2447. }
  2448. break;
  2449. case XML_HTML_DOCUMENT_NODE:
  2450. #ifdef LIBXML_HTML_ENABLED
  2451. if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
  2452. xmlGenericError(xmlGenericErrorContext,
  2453. "Failed to save to %s\n", filename);
  2454. }
  2455. #else
  2456. if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
  2457. xmlGenericError(xmlGenericErrorContext,
  2458. "Failed to save to %s\n", filename);
  2459. }
  2460. #endif /* LIBXML_HTML_ENABLED */
  2461. break;
  2462. default:
  2463. xmlGenericError(xmlGenericErrorContext,
  2464. "To save to subparts of a document use the 'write' command\n");
  2465. return (-1);
  2466. }
  2467. return (0);
  2468. }
  2469. #endif /* LIBXML_OUTPUT_ENABLED */
  2470. #ifdef LIBXML_VALID_ENABLED
  2471. /**
  2472. * xmlShellValidate:
  2473. * @ctxt: the shell context
  2474. * @dtd: the DTD URI (optional)
  2475. * @node: unused
  2476. * @node2: unused
  2477. *
  2478. * Implements the XML shell function "validate"
  2479. * Validate the document, if a DTD path is provided, then the validation
  2480. * is done against the given DTD.
  2481. *
  2482. * Returns 0 or -1 in case of error
  2483. */
  2484. int
  2485. xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
  2486. xmlNodePtr node ATTRIBUTE_UNUSED,
  2487. xmlNodePtr node2 ATTRIBUTE_UNUSED)
  2488. {
  2489. xmlValidCtxt vctxt;
  2490. int res = -1;
  2491. if ((ctxt == NULL) || (ctxt->doc == NULL)) return(-1);
  2492. vctxt.userData = stderr;
  2493. vctxt.error = (xmlValidityErrorFunc) fprintf;
  2494. vctxt.warning = (xmlValidityWarningFunc) fprintf;
  2495. if ((dtd == NULL) || (dtd[0] == 0)) {
  2496. res = xmlValidateDocument(&vctxt, ctxt->doc);
  2497. } else {
  2498. xmlDtdPtr subset;
  2499. subset = xmlParseDTD(NULL, (xmlChar *) dtd);
  2500. if (subset != NULL) {
  2501. res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
  2502. xmlFreeDtd(subset);
  2503. }
  2504. }
  2505. return (res);
  2506. }
  2507. #endif /* LIBXML_VALID_ENABLED */
  2508. /**
  2509. * xmlShellDu:
  2510. * @ctxt: the shell context
  2511. * @arg: unused
  2512. * @tree: a node defining a subtree
  2513. * @node2: unused
  2514. *
  2515. * Implements the XML shell function "du"
  2516. * show the structure of the subtree under node @tree
  2517. * If @tree is null, the command works on the current node.
  2518. *
  2519. * Returns 0 or -1 in case of error
  2520. */
  2521. int
  2522. xmlShellDu(xmlShellCtxtPtr ctxt,
  2523. char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
  2524. xmlNodePtr node2 ATTRIBUTE_UNUSED)
  2525. {
  2526. xmlNodePtr node;
  2527. int indent = 0, i;
  2528. if (!ctxt)
  2529. return (-1);
  2530. if (tree == NULL)
  2531. return (-1);
  2532. node = tree;
  2533. while (node != NULL) {
  2534. if ((node->type == XML_DOCUMENT_NODE) ||
  2535. (node->type == XML_HTML_DOCUMENT_NODE)) {
  2536. fprintf(ctxt->output, "/\n");
  2537. } else if (node->type == XML_ELEMENT_NODE) {
  2538. for (i = 0; i < indent; i++)
  2539. fprintf(ctxt->output, " ");
  2540. fprintf(ctxt->output, "%s\n", node->name);
  2541. } else {
  2542. }
  2543. /*
  2544. * Browse the full subtree, deep first
  2545. */
  2546. if ((node->type == XML_DOCUMENT_NODE) ||
  2547. (node->type == XML_HTML_DOCUMENT_NODE)) {
  2548. node = ((xmlDocPtr) node)->children;
  2549. } else if ((node->children != NULL)
  2550. && (node->type != XML_ENTITY_REF_NODE)) {
  2551. /* deep first */
  2552. node = node->children;
  2553. indent++;
  2554. } else if ((node != tree) && (node->next != NULL)) {
  2555. /* then siblings */
  2556. node = node->next;
  2557. } else if (node != tree) {
  2558. /* go up to parents->next if needed */
  2559. while (node != tree) {
  2560. if (node->parent != NULL) {
  2561. node = node->parent;
  2562. indent--;
  2563. }
  2564. if ((node != tree) && (node->next != NULL)) {
  2565. node = node->next;
  2566. break;
  2567. }
  2568. if (node->parent == NULL) {
  2569. node = NULL;
  2570. break;
  2571. }
  2572. if (node == tree) {
  2573. node = NULL;
  2574. break;
  2575. }
  2576. }
  2577. /* exit condition */
  2578. if (node == tree)
  2579. node = NULL;
  2580. } else
  2581. node = NULL;
  2582. }
  2583. return (0);
  2584. }
  2585. /**
  2586. * xmlShellPwd:
  2587. * @ctxt: the shell context
  2588. * @buffer: the output buffer
  2589. * @node: a node
  2590. * @node2: unused
  2591. *
  2592. * Implements the XML shell function "pwd"
  2593. * Show the full path from the root to the node, if needed building
  2594. * thumblers when similar elements exists at a given ancestor level.
  2595. * The output is compatible with XPath commands.
  2596. *
  2597. * Returns 0 or -1 in case of error
  2598. */
  2599. int
  2600. xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
  2601. xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
  2602. {
  2603. xmlChar *path;
  2604. if ((node == NULL) || (buffer == NULL))
  2605. return (-1);
  2606. path = xmlGetNodePath(node);
  2607. if (path == NULL)
  2608. return (-1);
  2609. /*
  2610. * This test prevents buffer overflow, because this routine
  2611. * is only called by xmlShell, in which the second argument is
  2612. * 500 chars long.
  2613. * It is a dirty hack before a cleaner solution is found.
  2614. * Documentation should mention that the second argument must
  2615. * be at least 500 chars long, and could be stripped if too long.
  2616. */
  2617. snprintf(buffer, 499, "%s", path);
  2618. buffer[499] = '0';
  2619. xmlFree(path);
  2620. return (0);
  2621. }
  2622. /**
  2623. * xmlShell:
  2624. * @doc: the initial document
  2625. * @filename: the output buffer
  2626. * @input: the line reading function
  2627. * @output: the output FILE*, defaults to stdout if NULL
  2628. *
  2629. * Implements the XML shell
  2630. * This allow to load, validate, view, modify and save a document
  2631. * using a environment similar to a UNIX commandline.
  2632. */
  2633. void
  2634. xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
  2635. FILE * output)
  2636. {
  2637. char prompt[500] = "/ > ";
  2638. char *cmdline = NULL, *cur;
  2639. char command[100];
  2640. char arg[400];
  2641. int i;
  2642. xmlShellCtxtPtr ctxt;
  2643. xmlXPathObjectPtr list;
  2644. if (doc == NULL)
  2645. return;
  2646. if (filename == NULL)
  2647. return;
  2648. if (input == NULL)
  2649. return;
  2650. if (output == NULL)
  2651. output = stdout;
  2652. ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
  2653. if (ctxt == NULL)
  2654. return;
  2655. ctxt->loaded = 0;
  2656. ctxt->doc = doc;
  2657. ctxt->input = input;
  2658. ctxt->output = output;
  2659. ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
  2660. ctxt->node = (xmlNodePtr) ctxt->doc;
  2661. #ifdef LIBXML_XPATH_ENABLED
  2662. ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
  2663. if (ctxt->pctxt == NULL) {
  2664. xmlFree(ctxt);
  2665. return;
  2666. }
  2667. #endif /* LIBXML_XPATH_ENABLED */
  2668. while (1) {
  2669. if (ctxt->node == (xmlNodePtr) ctxt->doc)
  2670. snprintf(prompt, sizeof(prompt), "%s > ", "/");
  2671. else if ((ctxt->node != NULL) && (ctxt->node->name))
  2672. snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
  2673. else
  2674. snprintf(prompt, sizeof(prompt), "? > ");
  2675. prompt[sizeof(prompt) - 1] = 0;
  2676. /*
  2677. * Get a new command line
  2678. */
  2679. cmdline = ctxt->input(prompt);
  2680. if (cmdline == NULL)
  2681. break;
  2682. /*
  2683. * Parse the command itself
  2684. */
  2685. cur = cmdline;
  2686. while ((*cur == ' ') || (*cur == '\t'))
  2687. cur++;
  2688. i = 0;
  2689. while ((*cur != ' ') && (*cur != '\t') &&
  2690. (*cur != '\n') && (*cur != '\r')) {
  2691. if (*cur == 0)
  2692. break;
  2693. command[i++] = *cur++;
  2694. }
  2695. command[i] = 0;
  2696. if (i == 0)
  2697. continue;
  2698. /*
  2699. * Parse the argument
  2700. */
  2701. while ((*cur == ' ') || (*cur == '\t'))
  2702. cur++;
  2703. i = 0;
  2704. while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
  2705. if (*cur == 0)
  2706. break;
  2707. arg[i++] = *cur++;
  2708. }
  2709. arg[i] = 0;
  2710. /*
  2711. * start interpreting the command
  2712. */
  2713. if (!strcmp(command, "exit"))
  2714. break;
  2715. if (!strcmp(command, "quit"))
  2716. break;
  2717. if (!strcmp(command, "bye"))
  2718. break;
  2719. if (!strcmp(command, "help")) {
  2720. fprintf(ctxt->output, "\tbase display XML base of the node\n");
  2721. fprintf(ctxt->output, "\tsetbase URI change the XML base of the node\n");
  2722. fprintf(ctxt->output, "\tbye leave shell\n");
  2723. fprintf(ctxt->output, "\tcat [node] display node or current node\n");
  2724. fprintf(ctxt->output, "\tcd [path] change directory to path or to root\n");
  2725. fprintf(ctxt->output, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n");
  2726. fprintf(ctxt->output, "\tdu [path] show the structure of the subtree under path or the current node\n");
  2727. fprintf(ctxt->output, "\texit leave shell\n");
  2728. fprintf(ctxt->output, "\thelp display this help\n");
  2729. fprintf(ctxt->output, "\tfree display memory usage\n");
  2730. fprintf(ctxt->output, "\tload [name] load a new document with name\n");
  2731. fprintf(ctxt->output, "\tls [path] list contents of path or the current directory\n");
  2732. fprintf(ctxt->output, "\tset xml_fragment replace the current node content with the fragment parsed in context\n");
  2733. #ifdef LIBXML_XPATH_ENABLED
  2734. fprintf(ctxt->output, "\txpath expr evaluate the XPath expression in that context and print the result\n");
  2735. fprintf(ctxt->output, "\tsetns nsreg register a namespace to a prefix in the XPath evaluation context\n");
  2736. fprintf(ctxt->output, "\t format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)\n");
  2737. fprintf(ctxt->output, "\tsetrootns register all namespace found on the root element\n");
  2738. fprintf(ctxt->output, "\t the default namespace if any uses 'defaultns' prefix\n");
  2739. #endif /* LIBXML_XPATH_ENABLED */
  2740. fprintf(ctxt->output, "\tpwd display current working directory\n");
  2741. fprintf(ctxt->output, "\tquit leave shell\n");
  2742. #ifdef LIBXML_OUTPUT_ENABLED
  2743. fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n");
  2744. fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
  2745. #endif /* LIBXML_OUTPUT_ENABLED */
  2746. #ifdef LIBXML_VALID_ENABLED
  2747. fprintf(ctxt->output, "\tvalidate check the document for errors\n");
  2748. #endif /* LIBXML_VALID_ENABLED */
  2749. #ifdef LIBXML_SCHEMAS_ENABLED
  2750. fprintf(ctxt->output, "\trelaxng rng validate the document agaisnt the Relax-NG schemas\n");
  2751. #endif
  2752. fprintf(ctxt->output, "\tgrep string search for a string in the subtree\n");
  2753. #ifdef LIBXML_VALID_ENABLED
  2754. } else if (!strcmp(command, "validate")) {
  2755. xmlShellValidate(ctxt, arg, NULL, NULL);
  2756. #endif /* LIBXML_VALID_ENABLED */
  2757. } else if (!strcmp(command, "load")) {
  2758. xmlShellLoad(ctxt, arg, NULL, NULL);
  2759. #ifdef LIBXML_SCHEMAS_ENABLED
  2760. } else if (!strcmp(command, "relaxng")) {
  2761. xmlShellRNGValidate(ctxt, arg, NULL, NULL);
  2762. #endif
  2763. #ifdef LIBXML_OUTPUT_ENABLED
  2764. } else if (!strcmp(command, "save")) {
  2765. xmlShellSave(ctxt, arg, NULL, NULL);
  2766. } else if (!strcmp(command, "write")) {
  2767. if ((arg == NULL) || (arg[0] == 0))
  2768. xmlGenericError(xmlGenericErrorContext,
  2769. "Write command requires a filename argument\n");
  2770. else
  2771. xmlShellWrite(ctxt, arg, NULL, NULL);
  2772. #endif /* LIBXML_OUTPUT_ENABLED */
  2773. } else if (!strcmp(command, "grep")) {
  2774. xmlShellGrep(ctxt, arg, ctxt->node, NULL);
  2775. } else if (!strcmp(command, "free")) {
  2776. if (arg[0] == 0) {
  2777. xmlMemShow(ctxt->output, 0);
  2778. } else {
  2779. int len = 0;
  2780. sscanf(arg, "%d", &len);
  2781. xmlMemShow(ctxt->output, len);
  2782. }
  2783. } else if (!strcmp(command, "pwd")) {
  2784. char dir[500];
  2785. if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
  2786. fprintf(ctxt->output, "%s\n", dir);
  2787. } else if (!strcmp(command, "du")) {
  2788. xmlShellDu(ctxt, NULL, ctxt->node, NULL);
  2789. } else if (!strcmp(command, "base")) {
  2790. xmlShellBase(ctxt, NULL, ctxt->node, NULL);
  2791. } else if (!strcmp(command, "set")) {
  2792. xmlShellSetContent(ctxt, arg, ctxt->node, NULL);
  2793. #ifdef LIBXML_XPATH_ENABLED
  2794. } else if (!strcmp(command, "setns")) {
  2795. if (arg[0] == 0) {
  2796. xmlGenericError(xmlGenericErrorContext,
  2797. "setns: prefix=[nsuri] required\n");
  2798. } else {
  2799. xmlShellRegisterNamespace(ctxt, arg, NULL, NULL);
  2800. }
  2801. } else if (!strcmp(command, "setrootns")) {
  2802. xmlNodePtr root;
  2803. root = xmlDocGetRootElement(ctxt->doc);
  2804. xmlShellRegisterRootNamespaces(ctxt, NULL, root, NULL);
  2805. } else if (!strcmp(command, "xpath")) {
  2806. if (arg[0] == 0) {
  2807. xmlGenericError(xmlGenericErrorContext,
  2808. "xpath: expression required\n");
  2809. } else {
  2810. ctxt->pctxt->node = ctxt->node;
  2811. list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
  2812. xmlXPathDebugDumpObject(ctxt->output, list, 0);
  2813. xmlXPathFreeObject(list);
  2814. }
  2815. #endif /* LIBXML_XPATH_ENABLED */
  2816. #ifdef LIBXML_TREE_ENABLED
  2817. } else if (!strcmp(command, "setbase")) {
  2818. xmlShellSetBase(ctxt, arg, ctxt->node, NULL);
  2819. #endif
  2820. } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
  2821. int dir = (!strcmp(command, "dir"));
  2822. if (arg[0] == 0) {
  2823. if (dir)
  2824. xmlShellDir(ctxt, NULL, ctxt->node, NULL);
  2825. else
  2826. xmlShellList(ctxt, NULL, ctxt->node, NULL);
  2827. } else {
  2828. ctxt->pctxt->node = ctxt->node;
  2829. #ifdef LIBXML_XPATH_ENABLED
  2830. ctxt->pctxt->node = ctxt->node;
  2831. list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
  2832. #else
  2833. list = NULL;
  2834. #endif /* LIBXML_XPATH_ENABLED */
  2835. if (list != NULL) {
  2836. switch (list->type) {
  2837. case XPATH_UNDEFINED:
  2838. xmlGenericError(xmlGenericErrorContext,
  2839. "%s: no such node\n", arg);
  2840. break;
  2841. case XPATH_NODESET:{
  2842. int indx;
  2843. if (list->nodesetval == NULL)
  2844. break;
  2845. for (indx = 0;
  2846. indx < list->nodesetval->nodeNr;
  2847. indx++) {
  2848. if (dir)
  2849. xmlShellDir(ctxt, NULL,
  2850. list->nodesetval->
  2851. nodeTab[indx], NULL);
  2852. else
  2853. xmlShellList(ctxt, NULL,
  2854. list->nodesetval->
  2855. nodeTab[indx], NULL);
  2856. }
  2857. break;
  2858. }
  2859. case XPATH_BOOLEAN:
  2860. xmlGenericError(xmlGenericErrorContext,
  2861. "%s is a Boolean\n", arg);
  2862. break;
  2863. case XPATH_NUMBER:
  2864. xmlGenericError(xmlGenericErrorContext,
  2865. "%s is a number\n", arg);
  2866. break;
  2867. case XPATH_STRING:
  2868. xmlGenericError(xmlGenericErrorContext,
  2869. "%s is a string\n", arg);
  2870. break;
  2871. case XPATH_POINT:
  2872. xmlGenericError(xmlGenericErrorContext,
  2873. "%s is a point\n", arg);
  2874. break;
  2875. case XPATH_RANGE:
  2876. xmlGenericError(xmlGenericErrorContext,
  2877. "%s is a range\n", arg);
  2878. break;
  2879. case XPATH_LOCATIONSET:
  2880. xmlGenericError(xmlGenericErrorContext,
  2881. "%s is a range\n", arg);
  2882. break;
  2883. case XPATH_USERS:
  2884. xmlGenericError(xmlGenericErrorContext,
  2885. "%s is user-defined\n", arg);
  2886. break;
  2887. case XPATH_XSLT_TREE:
  2888. xmlGenericError(xmlGenericErrorContext,
  2889. "%s is an XSLT value tree\n",
  2890. arg);
  2891. break;
  2892. }
  2893. #ifdef LIBXML_XPATH_ENABLED
  2894. xmlXPathFreeObject(list);
  2895. #endif
  2896. } else {
  2897. xmlGenericError(xmlGenericErrorContext,
  2898. "%s: no such node\n", arg);
  2899. }
  2900. ctxt->pctxt->node = NULL;
  2901. }
  2902. } else if (!strcmp(command, "cd")) {
  2903. if (arg[0] == 0) {
  2904. ctxt->node = (xmlNodePtr) ctxt->doc;
  2905. } else {
  2906. #ifdef LIBXML_XPATH_ENABLED
  2907. ctxt->pctxt->node = ctxt->node;
  2908. list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
  2909. #else
  2910. list = NULL;
  2911. #endif /* LIBXML_XPATH_ENABLED */
  2912. if (list != NULL) {
  2913. switch (list->type) {
  2914. case XPATH_UNDEFINED:
  2915. xmlGenericError(xmlGenericErrorContext,
  2916. "%s: no such node\n", arg);
  2917. break;
  2918. case XPATH_NODESET:
  2919. if (list->nodesetval != NULL) {
  2920. if (list->nodesetval->nodeNr == 1) {
  2921. ctxt->node = list->nodesetval->nodeTab[0];
  2922. if ((ctxt->node != NULL) &&
  2923. (ctxt->node->type ==
  2924. XML_NAMESPACE_DECL)) {
  2925. xmlGenericError(xmlGenericErrorContext,
  2926. "cannot cd to namespace\n");
  2927. ctxt->node = NULL;
  2928. }
  2929. } else
  2930. xmlGenericError(xmlGenericErrorContext,
  2931. "%s is a %d Node Set\n",
  2932. arg,
  2933. list->nodesetval->nodeNr);
  2934. } else
  2935. xmlGenericError(xmlGenericErrorContext,
  2936. "%s is an empty Node Set\n",
  2937. arg);
  2938. break;
  2939. case XPATH_BOOLEAN:
  2940. xmlGenericError(xmlGenericErrorContext,
  2941. "%s is a Boolean\n", arg);
  2942. break;
  2943. case XPATH_NUMBER:
  2944. xmlGenericError(xmlGenericErrorContext,
  2945. "%s is a number\n", arg);
  2946. break;
  2947. case XPATH_STRING:
  2948. xmlGenericError(xmlGenericErrorContext,
  2949. "%s is a string\n", arg);
  2950. break;
  2951. case XPATH_POINT:
  2952. xmlGenericError(xmlGenericErrorContext,
  2953. "%s is a point\n", arg);
  2954. break;
  2955. case XPATH_RANGE:
  2956. xmlGenericError(xmlGenericErrorContext,
  2957. "%s is a range\n", arg);
  2958. break;
  2959. case XPATH_LOCATIONSET:
  2960. xmlGenericError(xmlGenericErrorContext,
  2961. "%s is a range\n", arg);
  2962. break;
  2963. case XPATH_USERS:
  2964. xmlGenericError(xmlGenericErrorContext,
  2965. "%s is user-defined\n", arg);
  2966. break;
  2967. case XPATH_XSLT_TREE:
  2968. xmlGenericError(xmlGenericErrorContext,
  2969. "%s is an XSLT value tree\n",
  2970. arg);
  2971. break;
  2972. }
  2973. #ifdef LIBXML_XPATH_ENABLED
  2974. xmlXPathFreeObject(list);
  2975. #endif
  2976. } else {
  2977. xmlGenericError(xmlGenericErrorContext,
  2978. "%s: no such node\n", arg);
  2979. }
  2980. ctxt->pctxt->node = NULL;
  2981. }
  2982. #ifdef LIBXML_OUTPUT_ENABLED
  2983. } else if (!strcmp(command, "cat")) {
  2984. if (arg[0] == 0) {
  2985. xmlShellCat(ctxt, NULL, ctxt->node, NULL);
  2986. } else {
  2987. ctxt->pctxt->node = ctxt->node;
  2988. #ifdef LIBXML_XPATH_ENABLED
  2989. ctxt->pctxt->node = ctxt->node;
  2990. list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
  2991. #else
  2992. list = NULL;
  2993. #endif /* LIBXML_XPATH_ENABLED */
  2994. if (list != NULL) {
  2995. switch (list->type) {
  2996. case XPATH_UNDEFINED:
  2997. xmlGenericError(xmlGenericErrorContext,
  2998. "%s: no such node\n", arg);
  2999. break;
  3000. case XPATH_NODESET:{
  3001. int indx;
  3002. if (list->nodesetval == NULL)
  3003. break;
  3004. for (indx = 0;
  3005. indx < list->nodesetval->nodeNr;
  3006. indx++) {
  3007. if (i > 0)
  3008. fprintf(ctxt->output, " -------\n");
  3009. xmlShellCat(ctxt, NULL,
  3010. list->nodesetval->
  3011. nodeTab[indx], NULL);
  3012. }
  3013. break;
  3014. }
  3015. case XPATH_BOOLEAN:
  3016. xmlGenericError(xmlGenericErrorContext,
  3017. "%s is a Boolean\n", arg);
  3018. break;
  3019. case XPATH_NUMBER:
  3020. xmlGenericError(xmlGenericErrorContext,
  3021. "%s is a number\n", arg);
  3022. break;
  3023. case XPATH_STRING:
  3024. xmlGenericError(xmlGenericErrorContext,
  3025. "%s is a string\n", arg);
  3026. break;
  3027. case XPATH_POINT:
  3028. xmlGenericError(xmlGenericErrorContext,
  3029. "%s is a point\n", arg);
  3030. break;
  3031. case XPATH_RANGE:
  3032. xmlGenericError(xmlGenericErrorContext,
  3033. "%s is a range\n", arg);
  3034. break;
  3035. case XPATH_LOCATIONSET:
  3036. xmlGenericError(xmlGenericErrorContext,
  3037. "%s is a range\n", arg);
  3038. break;
  3039. case XPATH_USERS:
  3040. xmlGenericError(xmlGenericErrorContext,
  3041. "%s is user-defined\n", arg);
  3042. break;
  3043. case XPATH_XSLT_TREE:
  3044. xmlGenericError(xmlGenericErrorContext,
  3045. "%s is an XSLT value tree\n",
  3046. arg);
  3047. break;
  3048. }
  3049. #ifdef LIBXML_XPATH_ENABLED
  3050. xmlXPathFreeObject(list);
  3051. #endif
  3052. } else {
  3053. xmlGenericError(xmlGenericErrorContext,
  3054. "%s: no such node\n", arg);
  3055. }
  3056. ctxt->pctxt->node = NULL;
  3057. }
  3058. #endif /* LIBXML_OUTPUT_ENABLED */
  3059. } else {
  3060. xmlGenericError(xmlGenericErrorContext,
  3061. "Unknown command %s\n", command);
  3062. }
  3063. free(cmdline); /* not xmlFree here ! */
  3064. cmdline = NULL;
  3065. }
  3066. #ifdef LIBXML_XPATH_ENABLED
  3067. xmlXPathFreeContext(ctxt->pctxt);
  3068. #endif /* LIBXML_XPATH_ENABLED */
  3069. if (ctxt->loaded) {
  3070. xmlFreeDoc(ctxt->doc);
  3071. }
  3072. if (ctxt->filename != NULL)
  3073. xmlFree(ctxt->filename);
  3074. xmlFree(ctxt);
  3075. if (cmdline != NULL)
  3076. free(cmdline); /* not xmlFree here ! */
  3077. }
  3078. #endif /* LIBXML_XPATH_ENABLED */
  3079. #define bottom_debugXML
  3080. #include "elfgcchack.h"
  3081. #endif /* LIBXML_DEBUG_ENABLED */