debugXML.c 102 KB

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