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