debugXML.c 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423
  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 */