Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 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 | * |
Daniel Veillard | c5d6434 | 2001-06-24 12:13:24 +0000 | [diff] [blame] | 7 | * Daniel Veillard <daniel@veillard.com> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 10 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11 | #ifdef LIBXML_DEBUG_ENABLED |
| 12 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13 | #include <string.h> |
| 14 | #ifdef HAVE_STDLIB_H |
| 15 | #include <stdlib.h> |
| 16 | #endif |
| 17 | #ifdef HAVE_STRING_H |
| 18 | #include <string.h> |
| 19 | #endif |
| 20 | #include <libxml/xmlmemory.h> |
| 21 | #include <libxml/tree.h> |
| 22 | #include <libxml/parser.h> |
Daniel Veillard | 567e1b4 | 2001-08-01 15:53:47 +0000 | [diff] [blame] | 23 | #include <libxml/parserInternals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 24 | #include <libxml/valid.h> |
| 25 | #include <libxml/debugXML.h> |
| 26 | #include <libxml/HTMLtree.h> |
| 27 | #include <libxml/HTMLparser.h> |
| 28 | #include <libxml/xmlerror.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 29 | #include <libxml/globals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 30 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 31 | /** |
| 32 | * xmlDebugDumpString: |
| 33 | * @output: the FILE * for the output |
| 34 | * @str: the string |
| 35 | * |
| 36 | * Dumps informations about the string, shorten it if necessary |
| 37 | */ |
| 38 | void |
| 39 | xmlDebugDumpString(FILE * output, const xmlChar * str) |
| 40 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 41 | int i; |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 42 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 43 | if (str == NULL) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 44 | fprintf(output, "(NULL)"); |
| 45 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 46 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 47 | for (i = 0; i < 40; i++) |
| 48 | if (str[i] == 0) |
| 49 | return; |
| 50 | else if (IS_BLANK(str[i])) |
| 51 | fputc(' ', output); |
| 52 | else if (str[i] >= 0x80) |
| 53 | fprintf(output, "#%X", str[i]); |
| 54 | else |
| 55 | fputc(str[i], output); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 56 | fprintf(output, "..."); |
| 57 | } |
| 58 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 59 | static void |
| 60 | xmlDebugDumpDtdNode(FILE *output, xmlDtdPtr dtd, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 61 | int i; |
| 62 | char shift[100]; |
| 63 | |
| 64 | for (i = 0;((i < depth) && (i < 25));i++) |
| 65 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 66 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 67 | |
| 68 | fprintf(output, shift); |
| 69 | |
| 70 | if (dtd->type != XML_DTD_NODE) { |
| 71 | fprintf(output, "PBM: not a DTD\n"); |
| 72 | return; |
| 73 | } |
| 74 | if (dtd->name != NULL) |
| 75 | fprintf(output, "DTD(%s)", dtd->name); |
| 76 | else |
| 77 | fprintf(output, "DTD"); |
| 78 | if (dtd->ExternalID != NULL) |
| 79 | fprintf(output, ", PUBLIC %s", dtd->ExternalID); |
| 80 | if (dtd->SystemID != NULL) |
| 81 | fprintf(output, ", SYSTEM %s", dtd->SystemID); |
| 82 | fprintf(output, "\n"); |
| 83 | /* |
| 84 | * Do a bit of checking |
| 85 | */ |
| 86 | if (dtd->parent == NULL) |
| 87 | fprintf(output, "PBM: Dtd has no parent\n"); |
| 88 | if (dtd->doc == NULL) |
| 89 | fprintf(output, "PBM: Dtd has no doc\n"); |
| 90 | if ((dtd->parent != NULL) && (dtd->doc != dtd->parent->doc)) |
| 91 | fprintf(output, "PBM: Dtd doc differs from parent's one\n"); |
| 92 | if (dtd->prev == NULL) { |
| 93 | if ((dtd->parent != NULL) && (dtd->parent->children != (xmlNodePtr)dtd)) |
| 94 | fprintf(output, "PBM: Dtd has no prev and not first of list\n"); |
| 95 | } else { |
| 96 | if (dtd->prev->next != (xmlNodePtr) dtd) |
| 97 | fprintf(output, "PBM: Dtd prev->next : back link wrong\n"); |
| 98 | } |
| 99 | if (dtd->next == NULL) { |
| 100 | if ((dtd->parent != NULL) && (dtd->parent->last != (xmlNodePtr) dtd)) |
| 101 | fprintf(output, "PBM: Dtd has no next and not last of list\n"); |
| 102 | } else { |
| 103 | if (dtd->next->prev != (xmlNodePtr) dtd) |
| 104 | fprintf(output, "PBM: Dtd next->prev : forward link wrong\n"); |
| 105 | } |
| 106 | } |
| 107 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 108 | static void |
| 109 | xmlDebugDumpAttrDecl(FILE *output, xmlAttributePtr attr, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 110 | int i; |
| 111 | char shift[100]; |
| 112 | |
| 113 | for (i = 0;((i < depth) && (i < 25));i++) |
| 114 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 115 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 116 | |
| 117 | fprintf(output, shift); |
| 118 | |
| 119 | if (attr->type != XML_ATTRIBUTE_DECL) { |
| 120 | fprintf(output, "PBM: not a Attr\n"); |
| 121 | return; |
| 122 | } |
| 123 | if (attr->name != NULL) |
| 124 | fprintf(output, "ATTRDECL(%s)", attr->name); |
| 125 | else |
| 126 | fprintf(output, "PBM ATTRDECL noname!!!"); |
| 127 | if (attr->elem != NULL) |
| 128 | fprintf(output, " for %s", attr->elem); |
| 129 | else |
| 130 | fprintf(output, " PBM noelem!!!"); |
| 131 | switch (attr->atype) { |
| 132 | case XML_ATTRIBUTE_CDATA: |
| 133 | fprintf(output, " CDATA"); |
| 134 | break; |
| 135 | case XML_ATTRIBUTE_ID: |
| 136 | fprintf(output, " ID"); |
| 137 | break; |
| 138 | case XML_ATTRIBUTE_IDREF: |
| 139 | fprintf(output, " IDREF"); |
| 140 | break; |
| 141 | case XML_ATTRIBUTE_IDREFS: |
| 142 | fprintf(output, " IDREFS"); |
| 143 | break; |
| 144 | case XML_ATTRIBUTE_ENTITY: |
| 145 | fprintf(output, " ENTITY"); |
| 146 | break; |
| 147 | case XML_ATTRIBUTE_ENTITIES: |
| 148 | fprintf(output, " ENTITIES"); |
| 149 | break; |
| 150 | case XML_ATTRIBUTE_NMTOKEN: |
| 151 | fprintf(output, " NMTOKEN"); |
| 152 | break; |
| 153 | case XML_ATTRIBUTE_NMTOKENS: |
| 154 | fprintf(output, " NMTOKENS"); |
| 155 | break; |
| 156 | case XML_ATTRIBUTE_ENUMERATION: |
| 157 | fprintf(output, " ENUMERATION"); |
| 158 | break; |
| 159 | case XML_ATTRIBUTE_NOTATION: |
| 160 | fprintf(output, " NOTATION "); |
| 161 | break; |
| 162 | } |
| 163 | if (attr->tree != NULL) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 164 | int indx; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 165 | xmlEnumerationPtr cur = attr->tree; |
| 166 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 167 | for (indx = 0;indx < 5; indx++) { |
| 168 | if (indx != 0) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 169 | fprintf(output, "|%s", cur->name); |
| 170 | else |
| 171 | fprintf(output, " (%s", cur->name); |
| 172 | cur = cur->next; |
| 173 | if (cur == NULL) break; |
| 174 | } |
| 175 | if (cur == NULL) |
| 176 | fprintf(output, ")"); |
| 177 | else |
| 178 | fprintf(output, "...)"); |
| 179 | } |
| 180 | switch (attr->def) { |
| 181 | case XML_ATTRIBUTE_NONE: |
| 182 | break; |
| 183 | case XML_ATTRIBUTE_REQUIRED: |
| 184 | fprintf(output, " REQUIRED"); |
| 185 | break; |
| 186 | case XML_ATTRIBUTE_IMPLIED: |
| 187 | fprintf(output, " IMPLIED"); |
| 188 | break; |
| 189 | case XML_ATTRIBUTE_FIXED: |
| 190 | fprintf(output, " FIXED"); |
| 191 | break; |
| 192 | } |
| 193 | if (attr->defaultValue != NULL) { |
| 194 | fprintf(output, "\""); |
| 195 | xmlDebugDumpString(output, attr->defaultValue); |
| 196 | fprintf(output, "\""); |
| 197 | } |
| 198 | printf("\n"); |
| 199 | |
| 200 | /* |
| 201 | * Do a bit of checking |
| 202 | */ |
| 203 | if (attr->parent == NULL) |
| 204 | fprintf(output, "PBM: Attr has no parent\n"); |
| 205 | if (attr->doc == NULL) |
| 206 | fprintf(output, "PBM: Attr has no doc\n"); |
| 207 | if ((attr->parent != NULL) && (attr->doc != attr->parent->doc)) |
| 208 | fprintf(output, "PBM: Attr doc differs from parent's one\n"); |
| 209 | if (attr->prev == NULL) { |
| 210 | if ((attr->parent != NULL) && (attr->parent->children != (xmlNodePtr)attr)) |
| 211 | fprintf(output, "PBM: Attr has no prev and not first of list\n"); |
| 212 | } else { |
| 213 | if (attr->prev->next != (xmlNodePtr) attr) |
| 214 | fprintf(output, "PBM: Attr prev->next : back link wrong\n"); |
| 215 | } |
| 216 | if (attr->next == NULL) { |
| 217 | if ((attr->parent != NULL) && (attr->parent->last != (xmlNodePtr) attr)) |
| 218 | fprintf(output, "PBM: Attr has no next and not last of list\n"); |
| 219 | } else { |
| 220 | if (attr->next->prev != (xmlNodePtr) attr) |
| 221 | fprintf(output, "PBM: Attr next->prev : forward link wrong\n"); |
| 222 | } |
| 223 | } |
| 224 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 225 | static void |
| 226 | xmlDebugDumpElemDecl(FILE *output, xmlElementPtr elem, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 227 | int i; |
| 228 | char shift[100]; |
| 229 | |
| 230 | for (i = 0;((i < depth) && (i < 25));i++) |
| 231 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 232 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 233 | |
| 234 | fprintf(output, shift); |
| 235 | |
| 236 | if (elem->type != XML_ELEMENT_DECL) { |
| 237 | fprintf(output, "PBM: not a Elem\n"); |
| 238 | return; |
| 239 | } |
| 240 | if (elem->name != NULL) { |
| 241 | fprintf(output, "ELEMDECL("); |
| 242 | xmlDebugDumpString(output, elem->name); |
| 243 | fprintf(output, ")"); |
| 244 | } else |
| 245 | fprintf(output, "PBM ELEMDECL noname!!!"); |
| 246 | switch (elem->etype) { |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 247 | case XML_ELEMENT_TYPE_UNDEFINED: |
| 248 | fprintf(output, ", UNDEFINED"); |
| 249 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 250 | case XML_ELEMENT_TYPE_EMPTY: |
| 251 | fprintf(output, ", EMPTY"); |
| 252 | break; |
| 253 | case XML_ELEMENT_TYPE_ANY: |
| 254 | fprintf(output, ", ANY"); |
| 255 | break; |
| 256 | case XML_ELEMENT_TYPE_MIXED: |
| 257 | fprintf(output, ", MIXED "); |
| 258 | break; |
| 259 | case XML_ELEMENT_TYPE_ELEMENT: |
| 260 | fprintf(output, ", MIXED "); |
| 261 | break; |
| 262 | } |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 263 | if ((elem->type != XML_ELEMENT_NODE) && |
| 264 | (elem->content != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 265 | char buf[5001]; |
| 266 | |
| 267 | buf[0] = 0; |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 268 | xmlSnprintfElementContent(buf, 5000, elem->content, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 269 | buf[5000] = 0; |
| 270 | fprintf(output, "%s", buf); |
| 271 | } |
| 272 | printf("\n"); |
| 273 | |
| 274 | /* |
| 275 | * Do a bit of checking |
| 276 | */ |
| 277 | if (elem->parent == NULL) |
| 278 | fprintf(output, "PBM: Elem has no parent\n"); |
| 279 | if (elem->doc == NULL) |
| 280 | fprintf(output, "PBM: Elem has no doc\n"); |
| 281 | if ((elem->parent != NULL) && (elem->doc != elem->parent->doc)) |
| 282 | fprintf(output, "PBM: Elem doc differs from parent's one\n"); |
| 283 | if (elem->prev == NULL) { |
| 284 | if ((elem->parent != NULL) && (elem->parent->children != (xmlNodePtr)elem)) |
| 285 | fprintf(output, "PBM: Elem has no prev and not first of list\n"); |
| 286 | } else { |
| 287 | if (elem->prev->next != (xmlNodePtr) elem) |
| 288 | fprintf(output, "PBM: Elem prev->next : back link wrong\n"); |
| 289 | } |
| 290 | if (elem->next == NULL) { |
| 291 | if ((elem->parent != NULL) && (elem->parent->last != (xmlNodePtr) elem)) |
| 292 | fprintf(output, "PBM: Elem has no next and not last of list\n"); |
| 293 | } else { |
| 294 | if (elem->next->prev != (xmlNodePtr) elem) |
| 295 | fprintf(output, "PBM: Elem next->prev : forward link wrong\n"); |
| 296 | } |
| 297 | } |
| 298 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 299 | static void |
| 300 | xmlDebugDumpEntityDecl(FILE *output, xmlEntityPtr ent, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 301 | int i; |
| 302 | char shift[100]; |
| 303 | |
| 304 | for (i = 0;((i < depth) && (i < 25));i++) |
| 305 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 306 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 307 | |
| 308 | fprintf(output, shift); |
| 309 | |
| 310 | if (ent->type != XML_ENTITY_DECL) { |
| 311 | fprintf(output, "PBM: not a Entity decl\n"); |
| 312 | return; |
| 313 | } |
| 314 | if (ent->name != NULL) { |
| 315 | fprintf(output, "ENTITYDECL("); |
| 316 | xmlDebugDumpString(output, ent->name); |
| 317 | fprintf(output, ")"); |
| 318 | } else |
| 319 | fprintf(output, "PBM ENTITYDECL noname!!!"); |
| 320 | switch (ent->etype) { |
| 321 | case XML_INTERNAL_GENERAL_ENTITY: |
| 322 | fprintf(output, ", internal\n"); |
| 323 | break; |
| 324 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 325 | fprintf(output, ", external parsed\n"); |
| 326 | break; |
| 327 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 328 | fprintf(output, ", unparsed\n"); |
| 329 | break; |
| 330 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 331 | fprintf(output, ", parameter\n"); |
| 332 | break; |
| 333 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 334 | fprintf(output, ", external parameter\n"); |
| 335 | break; |
| 336 | case XML_INTERNAL_PREDEFINED_ENTITY: |
| 337 | fprintf(output, ", predefined\n"); |
| 338 | break; |
| 339 | } |
| 340 | if (ent->ExternalID) { |
| 341 | fprintf(output, shift); |
| 342 | fprintf(output, " ExternalID=%s\n", ent->ExternalID); |
| 343 | } |
| 344 | if (ent->SystemID) { |
| 345 | fprintf(output, shift); |
| 346 | fprintf(output, " SystemID=%s\n", ent->SystemID); |
| 347 | } |
| 348 | if (ent->URI != NULL) { |
| 349 | fprintf(output, shift); |
| 350 | fprintf(output, " URI=%s\n", ent->URI); |
| 351 | } |
| 352 | if (ent->content) { |
| 353 | fprintf(output, shift); |
| 354 | fprintf(output, " content="); |
| 355 | xmlDebugDumpString(output, ent->content); |
| 356 | fprintf(output, "\n"); |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * Do a bit of checking |
| 361 | */ |
| 362 | if (ent->parent == NULL) |
| 363 | fprintf(output, "PBM: Ent has no parent\n"); |
| 364 | if (ent->doc == NULL) |
| 365 | fprintf(output, "PBM: Ent has no doc\n"); |
| 366 | if ((ent->parent != NULL) && (ent->doc != ent->parent->doc)) |
| 367 | fprintf(output, "PBM: Ent doc differs from parent's one\n"); |
| 368 | if (ent->prev == NULL) { |
| 369 | if ((ent->parent != NULL) && (ent->parent->children != (xmlNodePtr)ent)) |
| 370 | fprintf(output, "PBM: Ent has no prev and not first of list\n"); |
| 371 | } else { |
| 372 | if (ent->prev->next != (xmlNodePtr) ent) |
| 373 | fprintf(output, "PBM: Ent prev->next : back link wrong\n"); |
| 374 | } |
| 375 | if (ent->next == NULL) { |
| 376 | if ((ent->parent != NULL) && (ent->parent->last != (xmlNodePtr) ent)) |
| 377 | fprintf(output, "PBM: Ent has no next and not last of list\n"); |
| 378 | } else { |
| 379 | if (ent->next->prev != (xmlNodePtr) ent) |
| 380 | fprintf(output, "PBM: Ent next->prev : forward link wrong\n"); |
| 381 | } |
| 382 | } |
| 383 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 384 | static void |
| 385 | xmlDebugDumpNamespace(FILE *output, xmlNsPtr ns, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 386 | int i; |
| 387 | char shift[100]; |
| 388 | |
| 389 | for (i = 0;((i < depth) && (i < 25));i++) |
| 390 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 391 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 392 | |
| 393 | fprintf(output, shift); |
| 394 | if (ns->type != XML_NAMESPACE_DECL) { |
| 395 | fprintf(output, "invalid namespace node %d\n", ns->type); |
| 396 | return; |
| 397 | } |
| 398 | if (ns->href == NULL) { |
| 399 | if (ns->prefix != NULL) |
| 400 | fprintf(output, "incomplete namespace %s href=NULL\n", ns->prefix); |
| 401 | else |
| 402 | fprintf(output, "incomplete default namespace href=NULL\n"); |
| 403 | } else { |
| 404 | if (ns->prefix != NULL) |
| 405 | fprintf(output, "namespace %s href=", ns->prefix); |
| 406 | else |
| 407 | fprintf(output, "default namespace href="); |
| 408 | |
| 409 | xmlDebugDumpString(output, ns->href); |
| 410 | fprintf(output, "\n"); |
| 411 | } |
| 412 | } |
| 413 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 414 | static void |
| 415 | xmlDebugDumpNamespaceList(FILE *output, xmlNsPtr ns, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 416 | while (ns != NULL) { |
| 417 | xmlDebugDumpNamespace(output, ns, depth); |
| 418 | ns = ns->next; |
| 419 | } |
| 420 | } |
| 421 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 422 | static void |
| 423 | xmlDebugDumpEntity(FILE *output, xmlEntityPtr ent, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 424 | int i; |
| 425 | char shift[100]; |
| 426 | |
| 427 | for (i = 0;((i < depth) && (i < 25));i++) |
| 428 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 429 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 430 | |
| 431 | fprintf(output, shift); |
| 432 | switch (ent->etype) { |
| 433 | case XML_INTERNAL_GENERAL_ENTITY: |
| 434 | fprintf(output, "INTERNAL_GENERAL_ENTITY "); |
| 435 | break; |
| 436 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 437 | fprintf(output, "EXTERNAL_GENERAL_PARSED_ENTITY "); |
| 438 | break; |
| 439 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 440 | fprintf(output, "EXTERNAL_GENERAL_UNPARSED_ENTITY "); |
| 441 | break; |
| 442 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 443 | fprintf(output, "INTERNAL_PARAMETER_ENTITY "); |
| 444 | break; |
| 445 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 446 | fprintf(output, "EXTERNAL_PARAMETER_ENTITY "); |
| 447 | break; |
| 448 | default: |
| 449 | fprintf(output, "ENTITY_%d ! ", ent->etype); |
| 450 | } |
| 451 | fprintf(output, "%s\n", ent->name); |
| 452 | if (ent->ExternalID) { |
| 453 | fprintf(output, shift); |
| 454 | fprintf(output, "ExternalID=%s\n", ent->ExternalID); |
| 455 | } |
| 456 | if (ent->SystemID) { |
| 457 | fprintf(output, shift); |
| 458 | fprintf(output, "SystemID=%s\n", ent->SystemID); |
| 459 | } |
| 460 | if (ent->URI) { |
| 461 | fprintf(output, shift); |
| 462 | fprintf(output, "URI=%s\n", ent->URI); |
| 463 | } |
| 464 | if (ent->content) { |
| 465 | fprintf(output, shift); |
| 466 | fprintf(output, "content="); |
| 467 | xmlDebugDumpString(output, ent->content); |
| 468 | fprintf(output, "\n"); |
| 469 | } |
| 470 | } |
| 471 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 472 | /** |
| 473 | * xmlDebugDumpAttr: |
| 474 | * @output: the FILE * for the output |
| 475 | * @attr: the attribute |
| 476 | * @depth: the indentation level. |
| 477 | * |
| 478 | * Dumps debug information for the attribute |
| 479 | */ |
| 480 | void |
| 481 | xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 482 | int i; |
| 483 | char shift[100]; |
| 484 | |
| 485 | for (i = 0;((i < depth) && (i < 25));i++) |
| 486 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 487 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 488 | |
| 489 | fprintf(output, shift); |
| 490 | |
| 491 | fprintf(output, "ATTRIBUTE "); |
| 492 | xmlDebugDumpString(output, attr->name); |
| 493 | fprintf(output, "\n"); |
| 494 | if (attr->children != NULL) |
| 495 | xmlDebugDumpNodeList(output, attr->children, depth + 1); |
| 496 | |
| 497 | /* |
| 498 | * Do a bit of checking |
| 499 | */ |
| 500 | if (attr->parent == NULL) |
| 501 | fprintf(output, "PBM: Attr has no parent\n"); |
| 502 | if (attr->doc == NULL) |
| 503 | fprintf(output, "PBM: Attr has no doc\n"); |
| 504 | if ((attr->parent != NULL) && (attr->doc != attr->parent->doc)) |
| 505 | fprintf(output, "PBM: Attr doc differs from parent's one\n"); |
| 506 | if (attr->prev == NULL) { |
| 507 | if ((attr->parent != NULL) && (attr->parent->properties != attr)) |
| 508 | fprintf(output, "PBM: Attr has no prev and not first of list\n"); |
| 509 | } else { |
| 510 | if (attr->prev->next != attr) |
| 511 | fprintf(output, "PBM: Attr prev->next : back link wrong\n"); |
| 512 | } |
| 513 | if (attr->next != NULL) { |
| 514 | if (attr->next->prev != attr) |
| 515 | fprintf(output, "PBM: Attr next->prev : forward link wrong\n"); |
| 516 | } |
| 517 | } |
| 518 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 519 | /** |
| 520 | * xmlDebugDumpAttrList: |
| 521 | * @output: the FILE * for the output |
| 522 | * @attr: the attribute list |
| 523 | * @depth: the indentation level. |
| 524 | * |
| 525 | * Dumps debug information for the attribute list |
| 526 | */ |
| 527 | void |
| 528 | xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth) |
| 529 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 530 | while (attr != NULL) { |
| 531 | xmlDebugDumpAttr(output, attr, depth); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 532 | attr = attr->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 536 | /** |
| 537 | * xmlDebugDumpOneNode: |
| 538 | * @output: the FILE * for the output |
| 539 | * @node: the node |
| 540 | * @depth: the indentation level. |
| 541 | * |
| 542 | * Dumps debug information for the element node, it is not recursive |
| 543 | */ |
| 544 | void |
| 545 | xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth) |
| 546 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 547 | int i; |
| 548 | char shift[100]; |
| 549 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 550 | for (i = 0; ((i < depth) && (i < 25)); i++) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 551 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 552 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 553 | |
| 554 | switch (node->type) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 555 | case XML_ELEMENT_NODE: |
| 556 | fprintf(output, shift); |
| 557 | fprintf(output, "ELEMENT "); |
| 558 | if ((node->ns != NULL) && (node->ns->prefix != NULL)) { |
| 559 | xmlDebugDumpString(output, node->ns->prefix); |
| 560 | fprintf(output, ":"); |
| 561 | } |
| 562 | xmlDebugDumpString(output, node->name); |
| 563 | fprintf(output, "\n"); |
| 564 | break; |
| 565 | case XML_ATTRIBUTE_NODE: |
| 566 | fprintf(output, shift); |
| 567 | fprintf(output, "Error, ATTRIBUTE found here\n"); |
| 568 | break; |
| 569 | case XML_TEXT_NODE: |
| 570 | fprintf(output, shift); |
Daniel Veillard | b44025c | 2001-10-11 22:55:55 +0000 | [diff] [blame] | 571 | if (node->name == (const xmlChar *) xmlStringTextNoenc) |
Daniel Veillard | 567e1b4 | 2001-08-01 15:53:47 +0000 | [diff] [blame] | 572 | fprintf(output, "TEXT no enc\n"); |
| 573 | else |
| 574 | fprintf(output, "TEXT\n"); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 575 | break; |
| 576 | case XML_CDATA_SECTION_NODE: |
| 577 | fprintf(output, shift); |
| 578 | fprintf(output, "CDATA_SECTION\n"); |
| 579 | break; |
| 580 | case XML_ENTITY_REF_NODE: |
| 581 | fprintf(output, shift); |
| 582 | fprintf(output, "ENTITY_REF(%s)\n", node->name); |
| 583 | break; |
| 584 | case XML_ENTITY_NODE: |
| 585 | fprintf(output, shift); |
| 586 | fprintf(output, "ENTITY\n"); |
| 587 | break; |
| 588 | case XML_PI_NODE: |
| 589 | fprintf(output, shift); |
| 590 | fprintf(output, "PI %s\n", node->name); |
| 591 | break; |
| 592 | case XML_COMMENT_NODE: |
| 593 | fprintf(output, shift); |
| 594 | fprintf(output, "COMMENT\n"); |
| 595 | break; |
| 596 | case XML_DOCUMENT_NODE: |
| 597 | case XML_HTML_DOCUMENT_NODE: |
| 598 | fprintf(output, shift); |
| 599 | fprintf(output, "Error, DOCUMENT found here\n"); |
| 600 | break; |
| 601 | case XML_DOCUMENT_TYPE_NODE: |
| 602 | fprintf(output, shift); |
| 603 | fprintf(output, "DOCUMENT_TYPE\n"); |
| 604 | break; |
| 605 | case XML_DOCUMENT_FRAG_NODE: |
| 606 | fprintf(output, shift); |
| 607 | fprintf(output, "DOCUMENT_FRAG\n"); |
| 608 | break; |
| 609 | case XML_NOTATION_NODE: |
| 610 | fprintf(output, shift); |
| 611 | fprintf(output, "NOTATION\n"); |
| 612 | break; |
| 613 | case XML_DTD_NODE: |
| 614 | xmlDebugDumpDtdNode(output, (xmlDtdPtr) node, depth); |
| 615 | return; |
| 616 | case XML_ELEMENT_DECL: |
| 617 | xmlDebugDumpElemDecl(output, (xmlElementPtr) node, depth); |
| 618 | return; |
| 619 | case XML_ATTRIBUTE_DECL: |
| 620 | xmlDebugDumpAttrDecl(output, (xmlAttributePtr) node, depth); |
| 621 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 622 | case XML_ENTITY_DECL: |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 623 | xmlDebugDumpEntityDecl(output, (xmlEntityPtr) node, depth); |
| 624 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 625 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 626 | xmlDebugDumpNamespace(output, (xmlNsPtr) node, depth); |
| 627 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 628 | case XML_XINCLUDE_START: |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 629 | fprintf(output, shift); |
| 630 | fprintf(output, "INCLUDE START\n"); |
| 631 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 632 | case XML_XINCLUDE_END: |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 633 | fprintf(output, shift); |
| 634 | fprintf(output, "INCLUDE END\n"); |
| 635 | return; |
| 636 | default: |
| 637 | fprintf(output, shift); |
| 638 | fprintf(output, "NODE_%d !!!\n", node->type); |
| 639 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 640 | } |
| 641 | if (node->doc == NULL) { |
| 642 | fprintf(output, shift); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 643 | fprintf(output, "doc == NULL !!!\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 644 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 645 | if (node->nsDef != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 646 | xmlDebugDumpNamespaceList(output, node->nsDef, depth + 1); |
| 647 | if (node->properties != NULL) |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 648 | xmlDebugDumpAttrList(output, node->properties, depth + 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 649 | if (node->type != XML_ENTITY_REF_NODE) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 650 | if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) { |
| 651 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 652 | shift[2 * i + 2] = shift[2 * i + 3] = 0; |
| 653 | fprintf(output, shift); |
| 654 | fprintf(output, "content="); |
| 655 | #ifndef XML_USE_BUFFER_CONTENT |
| 656 | xmlDebugDumpString(output, node->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 657 | #else |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 658 | xmlDebugDumpString(output, xmlBufferContent(node->content)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 659 | #endif |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 660 | fprintf(output, "\n"); |
| 661 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 662 | } else { |
| 663 | xmlEntityPtr ent; |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 664 | |
| 665 | ent = xmlGetDocEntity(node->doc, node->name); |
| 666 | if (ent != NULL) |
| 667 | xmlDebugDumpEntity(output, ent, depth + 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 668 | } |
| 669 | /* |
| 670 | * Do a bit of checking |
| 671 | */ |
| 672 | if (node->parent == NULL) |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 673 | fprintf(output, "PBM: Node has no parent\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 674 | if (node->doc == NULL) |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 675 | fprintf(output, "PBM: Node has no doc\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 676 | if ((node->parent != NULL) && (node->doc != node->parent->doc)) |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 677 | fprintf(output, "PBM: Node doc differs from parent's one\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 678 | if (node->prev == NULL) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 679 | if ((node->parent != NULL) && (node->parent->children != node)) |
| 680 | fprintf(output, |
| 681 | "PBM: Node has no prev and not first of list\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 682 | } else { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 683 | if (node->prev->next != node) |
| 684 | fprintf(output, "PBM: Node prev->next : back link wrong\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 685 | } |
| 686 | if (node->next == NULL) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 687 | if ((node->parent != NULL) && (node->parent->last != node)) |
| 688 | fprintf(output, |
| 689 | "PBM: Node has no next and not last of list\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 690 | } else { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 691 | if (node->next->prev != node) |
| 692 | fprintf(output, "PBM: Node next->prev : forward link wrong\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 696 | /** |
| 697 | * xmlDebugDumpNode: |
| 698 | * @output: the FILE * for the output |
| 699 | * @node: the node |
| 700 | * @depth: the indentation level. |
| 701 | * |
| 702 | * Dumps debug information for the element node, it is recursive |
| 703 | */ |
| 704 | void |
| 705 | xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth) |
| 706 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 707 | xmlDebugDumpOneNode(output, node, depth); |
| 708 | if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 709 | xmlDebugDumpNodeList(output, node->children, depth + 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 712 | /** |
| 713 | * xmlDebugDumpNodeList: |
| 714 | * @output: the FILE * for the output |
| 715 | * @node: the node list |
| 716 | * @depth: the indentation level. |
| 717 | * |
| 718 | * Dumps debug information for the list of element node, it is recursive |
| 719 | */ |
| 720 | void |
| 721 | xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth) |
| 722 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 723 | while (node != NULL) { |
| 724 | xmlDebugDumpNode(output, node, depth); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 725 | node = node->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 726 | } |
| 727 | } |
| 728 | |
| 729 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 730 | /** |
| 731 | * xmlDebugDumpDocumentHead: |
| 732 | * @output: the FILE * for the output |
| 733 | * @doc: the document |
| 734 | * |
| 735 | * Dumps debug information cncerning the document, not recursive |
| 736 | */ |
| 737 | void |
| 738 | xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc) |
| 739 | { |
| 740 | if (output == NULL) |
| 741 | output = stdout; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 742 | if (doc == NULL) { |
| 743 | fprintf(output, "DOCUMENT == NULL !\n"); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 744 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | switch (doc->type) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 748 | case XML_ELEMENT_NODE: |
| 749 | fprintf(output, "Error, ELEMENT found here "); |
| 750 | break; |
| 751 | case XML_ATTRIBUTE_NODE: |
| 752 | fprintf(output, "Error, ATTRIBUTE found here\n"); |
| 753 | break; |
| 754 | case XML_TEXT_NODE: |
| 755 | fprintf(output, "Error, TEXT\n"); |
| 756 | break; |
| 757 | case XML_CDATA_SECTION_NODE: |
| 758 | fprintf(output, "Error, CDATA_SECTION\n"); |
| 759 | break; |
| 760 | case XML_ENTITY_REF_NODE: |
| 761 | fprintf(output, "Error, ENTITY_REF\n"); |
| 762 | break; |
| 763 | case XML_ENTITY_NODE: |
| 764 | fprintf(output, "Error, ENTITY\n"); |
| 765 | break; |
| 766 | case XML_PI_NODE: |
| 767 | fprintf(output, "Error, PI\n"); |
| 768 | break; |
| 769 | case XML_COMMENT_NODE: |
| 770 | fprintf(output, "Error, COMMENT\n"); |
| 771 | break; |
| 772 | case XML_DOCUMENT_NODE: |
| 773 | fprintf(output, "DOCUMENT\n"); |
| 774 | break; |
| 775 | case XML_HTML_DOCUMENT_NODE: |
| 776 | fprintf(output, "HTML DOCUMENT\n"); |
| 777 | break; |
| 778 | case XML_DOCUMENT_TYPE_NODE: |
| 779 | fprintf(output, "Error, DOCUMENT_TYPE\n"); |
| 780 | break; |
| 781 | case XML_DOCUMENT_FRAG_NODE: |
| 782 | fprintf(output, "Error, DOCUMENT_FRAG\n"); |
| 783 | break; |
| 784 | case XML_NOTATION_NODE: |
| 785 | fprintf(output, "Error, NOTATION\n"); |
| 786 | break; |
| 787 | default: |
| 788 | fprintf(output, "NODE_%d\n", doc->type); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 789 | } |
| 790 | if (doc->name != NULL) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 791 | fprintf(output, "name="); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 792 | xmlDebugDumpString(output, BAD_CAST doc->name); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 793 | fprintf(output, "\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 794 | } |
| 795 | if (doc->version != NULL) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 796 | fprintf(output, "version="); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 797 | xmlDebugDumpString(output, doc->version); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 798 | fprintf(output, "\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 799 | } |
| 800 | if (doc->encoding != NULL) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 801 | fprintf(output, "encoding="); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 802 | xmlDebugDumpString(output, doc->encoding); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 803 | fprintf(output, "\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 804 | } |
| 805 | if (doc->URL != NULL) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 806 | fprintf(output, "URL="); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 807 | xmlDebugDumpString(output, doc->URL); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 808 | fprintf(output, "\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 809 | } |
| 810 | if (doc->standalone) |
| 811 | fprintf(output, "standalone=true\n"); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 812 | if (doc->oldNs != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 813 | xmlDebugDumpNamespaceList(output, doc->oldNs, 0); |
| 814 | } |
| 815 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 816 | /** |
| 817 | * xmlDebugDumpDocument: |
| 818 | * @output: the FILE * for the output |
| 819 | * @doc: the document |
| 820 | * |
| 821 | * Dumps debug information for the document, it's recursive |
| 822 | */ |
| 823 | void |
| 824 | xmlDebugDumpDocument(FILE * output, xmlDocPtr doc) |
| 825 | { |
| 826 | if (output == NULL) |
| 827 | output = stdout; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 828 | if (doc == NULL) { |
| 829 | fprintf(output, "DOCUMENT == NULL !\n"); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 830 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 831 | } |
| 832 | xmlDebugDumpDocumentHead(output, doc); |
| 833 | if (((doc->type == XML_DOCUMENT_NODE) || |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 834 | (doc->type == XML_HTML_DOCUMENT_NODE)) && (doc->children != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 835 | xmlDebugDumpNodeList(output, doc->children, 1); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 836 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 837 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 838 | /** |
| 839 | * xmlDebugDumpDTD: |
| 840 | * @output: the FILE * for the output |
| 841 | * @dtd: the DTD |
| 842 | * |
| 843 | * Dumps debug information for the DTD |
| 844 | */ |
| 845 | void |
| 846 | xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd) |
| 847 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 848 | if (dtd == NULL) |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 849 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 850 | if (dtd->type != XML_DTD_NODE) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 851 | fprintf(output, "PBM: not a DTD\n"); |
| 852 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 853 | } |
| 854 | if (dtd->name != NULL) |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 855 | fprintf(output, "DTD(%s)", dtd->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 856 | else |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 857 | fprintf(output, "DTD"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 858 | if (dtd->ExternalID != NULL) |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 859 | fprintf(output, ", PUBLIC %s", dtd->ExternalID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 860 | if (dtd->SystemID != NULL) |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 861 | fprintf(output, ", SYSTEM %s", dtd->SystemID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 862 | fprintf(output, "\n"); |
| 863 | /* |
| 864 | * Do a bit of checking |
| 865 | */ |
| 866 | if ((dtd->parent != NULL) && (dtd->doc != dtd->parent->doc)) |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 867 | fprintf(output, "PBM: Dtd doc differs from parent's one\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 868 | if (dtd->prev == NULL) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 869 | if ((dtd->parent != NULL) |
| 870 | && (dtd->parent->children != (xmlNodePtr) dtd)) |
| 871 | fprintf(output, |
| 872 | "PBM: Dtd has no prev and not first of list\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 873 | } else { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 874 | if (dtd->prev->next != (xmlNodePtr) dtd) |
| 875 | fprintf(output, "PBM: Dtd prev->next : back link wrong\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 876 | } |
| 877 | if (dtd->next == NULL) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 878 | if ((dtd->parent != NULL) |
| 879 | && (dtd->parent->last != (xmlNodePtr) dtd)) |
| 880 | fprintf(output, "PBM: Dtd has no next and not last of list\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 881 | } else { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 882 | if (dtd->next->prev != (xmlNodePtr) dtd) |
| 883 | fprintf(output, "PBM: Dtd next->prev : forward link wrong\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 884 | } |
| 885 | if (dtd->children == NULL) |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 886 | fprintf(output, " DTD is empty\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 887 | else |
| 888 | xmlDebugDumpNodeList(output, dtd->children, 1); |
| 889 | } |
| 890 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 891 | static void |
| 892 | xmlDebugDumpEntityCallback(xmlEntityPtr cur, FILE *output) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 893 | fprintf(output, "%s : ", cur->name); |
| 894 | switch (cur->etype) { |
| 895 | case XML_INTERNAL_GENERAL_ENTITY: |
| 896 | fprintf(output, "INTERNAL GENERAL, "); |
| 897 | break; |
| 898 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 899 | fprintf(output, "EXTERNAL PARSED, "); |
| 900 | break; |
| 901 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 902 | fprintf(output, "EXTERNAL UNPARSED, "); |
| 903 | break; |
| 904 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 905 | fprintf(output, "INTERNAL PARAMETER, "); |
| 906 | break; |
| 907 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 908 | fprintf(output, "EXTERNAL PARAMETER, "); |
| 909 | break; |
| 910 | default: |
| 911 | fprintf(output, "UNKNOWN TYPE %d", |
| 912 | cur->etype); |
| 913 | } |
| 914 | if (cur->ExternalID != NULL) |
| 915 | fprintf(output, "ID \"%s\"", cur->ExternalID); |
| 916 | if (cur->SystemID != NULL) |
| 917 | fprintf(output, "SYSTEM \"%s\"", cur->SystemID); |
| 918 | if (cur->orig != NULL) |
| 919 | fprintf(output, "\n orig \"%s\"", cur->orig); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 920 | if ((cur->type != XML_ELEMENT_NODE) && |
| 921 | (cur->content != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 922 | fprintf(output, "\n content \"%s\"", cur->content); |
| 923 | fprintf(output, "\n"); |
| 924 | } |
| 925 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 926 | /** |
| 927 | * xmlDebugDumpEntities: |
| 928 | * @output: the FILE * for the output |
| 929 | * @doc: the document |
| 930 | * |
| 931 | * Dumps debug information for all the entities in use by the document |
| 932 | */ |
| 933 | void |
| 934 | xmlDebugDumpEntities(FILE * output, xmlDocPtr doc) |
| 935 | { |
| 936 | if (output == NULL) |
| 937 | output = stdout; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 938 | if (doc == NULL) { |
| 939 | fprintf(output, "DOCUMENT == NULL !\n"); |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 940 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | switch (doc->type) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 944 | case XML_ELEMENT_NODE: |
| 945 | fprintf(output, "Error, ELEMENT found here "); |
| 946 | break; |
| 947 | case XML_ATTRIBUTE_NODE: |
| 948 | fprintf(output, "Error, ATTRIBUTE found here\n"); |
| 949 | break; |
| 950 | case XML_TEXT_NODE: |
| 951 | fprintf(output, "Error, TEXT\n"); |
| 952 | break; |
| 953 | case XML_CDATA_SECTION_NODE: |
| 954 | fprintf(output, "Error, CDATA_SECTION\n"); |
| 955 | break; |
| 956 | case XML_ENTITY_REF_NODE: |
| 957 | fprintf(output, "Error, ENTITY_REF\n"); |
| 958 | break; |
| 959 | case XML_ENTITY_NODE: |
| 960 | fprintf(output, "Error, ENTITY\n"); |
| 961 | break; |
| 962 | case XML_PI_NODE: |
| 963 | fprintf(output, "Error, PI\n"); |
| 964 | break; |
| 965 | case XML_COMMENT_NODE: |
| 966 | fprintf(output, "Error, COMMENT\n"); |
| 967 | break; |
| 968 | case XML_DOCUMENT_NODE: |
| 969 | fprintf(output, "DOCUMENT\n"); |
| 970 | break; |
| 971 | case XML_HTML_DOCUMENT_NODE: |
| 972 | fprintf(output, "HTML DOCUMENT\n"); |
| 973 | break; |
| 974 | case XML_DOCUMENT_TYPE_NODE: |
| 975 | fprintf(output, "Error, DOCUMENT_TYPE\n"); |
| 976 | break; |
| 977 | case XML_DOCUMENT_FRAG_NODE: |
| 978 | fprintf(output, "Error, DOCUMENT_FRAG\n"); |
| 979 | break; |
| 980 | case XML_NOTATION_NODE: |
| 981 | fprintf(output, "Error, NOTATION\n"); |
| 982 | break; |
| 983 | default: |
| 984 | fprintf(output, "NODE_%d\n", doc->type); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 985 | } |
| 986 | if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 987 | xmlEntitiesTablePtr table = (xmlEntitiesTablePtr) |
| 988 | doc->intSubset->entities; |
| 989 | |
| 990 | fprintf(output, "Entities in internal subset\n"); |
| 991 | xmlHashScan(table, (xmlHashScanner) xmlDebugDumpEntityCallback, |
| 992 | output); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 993 | } else |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 994 | fprintf(output, "No entities in internal subset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 995 | if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) { |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 996 | xmlEntitiesTablePtr table = (xmlEntitiesTablePtr) |
| 997 | doc->extSubset->entities; |
| 998 | |
| 999 | fprintf(output, "Entities in external subset\n"); |
| 1000 | xmlHashScan(table, (xmlHashScanner) xmlDebugDumpEntityCallback, |
| 1001 | output); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1002 | } else |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1003 | fprintf(output, "No entities in external subset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1006 | int xmlLsCountNode(xmlNodePtr node) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1007 | int ret = 0; |
| 1008 | xmlNodePtr list = NULL; |
| 1009 | |
| 1010 | switch (node->type) { |
| 1011 | case XML_ELEMENT_NODE: |
| 1012 | list = node->children; |
| 1013 | break; |
| 1014 | case XML_DOCUMENT_NODE: |
| 1015 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 1016 | #ifdef LIBXML_DOCB_ENABLED |
| 1017 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1018 | #endif |
| 1019 | list = ((xmlDocPtr) node)->children; |
| 1020 | break; |
| 1021 | case XML_ATTRIBUTE_NODE: |
| 1022 | list = ((xmlAttrPtr) node)->children; |
| 1023 | break; |
| 1024 | case XML_TEXT_NODE: |
| 1025 | case XML_CDATA_SECTION_NODE: |
| 1026 | case XML_PI_NODE: |
| 1027 | case XML_COMMENT_NODE: |
| 1028 | if (node->content != NULL) { |
| 1029 | #ifndef XML_USE_BUFFER_CONTENT |
| 1030 | ret = xmlStrlen(node->content); |
| 1031 | #else |
| 1032 | ret = xmlBufferLength(node->content); |
| 1033 | #endif |
| 1034 | } |
| 1035 | break; |
| 1036 | case XML_ENTITY_REF_NODE: |
| 1037 | case XML_DOCUMENT_TYPE_NODE: |
| 1038 | case XML_ENTITY_NODE: |
| 1039 | case XML_DOCUMENT_FRAG_NODE: |
| 1040 | case XML_NOTATION_NODE: |
| 1041 | case XML_DTD_NODE: |
| 1042 | case XML_ELEMENT_DECL: |
| 1043 | case XML_ATTRIBUTE_DECL: |
| 1044 | case XML_ENTITY_DECL: |
| 1045 | case XML_NAMESPACE_DECL: |
| 1046 | case XML_XINCLUDE_START: |
| 1047 | case XML_XINCLUDE_END: |
| 1048 | ret = 1; |
| 1049 | break; |
| 1050 | } |
| 1051 | for (;list != NULL;ret++) |
| 1052 | list = list->next; |
| 1053 | return(ret); |
| 1054 | } |
| 1055 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1056 | void |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1057 | xmlLsOneNode(FILE *output, xmlNodePtr node) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1058 | switch (node->type) { |
| 1059 | case XML_ELEMENT_NODE: |
| 1060 | fprintf(output, "-"); |
| 1061 | break; |
| 1062 | case XML_ATTRIBUTE_NODE: |
| 1063 | fprintf(output, "a"); |
| 1064 | break; |
| 1065 | case XML_TEXT_NODE: |
| 1066 | fprintf(output, "t"); |
| 1067 | break; |
| 1068 | case XML_CDATA_SECTION_NODE: |
| 1069 | fprintf(output, "c"); |
| 1070 | break; |
| 1071 | case XML_ENTITY_REF_NODE: |
| 1072 | fprintf(output, "e"); |
| 1073 | break; |
| 1074 | case XML_ENTITY_NODE: |
| 1075 | fprintf(output, "E"); |
| 1076 | break; |
| 1077 | case XML_PI_NODE: |
| 1078 | fprintf(output, "p"); |
| 1079 | break; |
| 1080 | case XML_COMMENT_NODE: |
| 1081 | fprintf(output, "c"); |
| 1082 | break; |
| 1083 | case XML_DOCUMENT_NODE: |
| 1084 | fprintf(output, "d"); |
| 1085 | break; |
| 1086 | case XML_HTML_DOCUMENT_NODE: |
| 1087 | fprintf(output, "h"); |
| 1088 | break; |
| 1089 | case XML_DOCUMENT_TYPE_NODE: |
| 1090 | fprintf(output, "T"); |
| 1091 | break; |
| 1092 | case XML_DOCUMENT_FRAG_NODE: |
| 1093 | fprintf(output, "F"); |
| 1094 | break; |
| 1095 | case XML_NOTATION_NODE: |
| 1096 | fprintf(output, "N"); |
| 1097 | break; |
| 1098 | default: |
| 1099 | fprintf(output, "?"); |
| 1100 | } |
| 1101 | if (node->properties != NULL) |
| 1102 | fprintf(output, "a"); |
| 1103 | else |
| 1104 | fprintf(output, "-"); |
| 1105 | if (node->nsDef != NULL) |
| 1106 | fprintf(output, "n"); |
| 1107 | else |
| 1108 | fprintf(output, "-"); |
| 1109 | |
| 1110 | fprintf(output, " %8d ", xmlLsCountNode(node)); |
| 1111 | |
| 1112 | switch (node->type) { |
| 1113 | case XML_ELEMENT_NODE: |
| 1114 | if (node->name != NULL) |
| 1115 | fprintf(output, "%s", node->name); |
| 1116 | break; |
| 1117 | case XML_ATTRIBUTE_NODE: |
| 1118 | if (node->name != NULL) |
| 1119 | fprintf(output, "%s", node->name); |
| 1120 | break; |
| 1121 | case XML_TEXT_NODE: |
| 1122 | if (node->content != NULL) { |
| 1123 | #ifndef XML_USE_BUFFER_CONTENT |
| 1124 | xmlDebugDumpString(output, node->content); |
| 1125 | #else |
| 1126 | xmlDebugDumpString(output, xmlBufferContent(node->content)); |
| 1127 | #endif |
| 1128 | } |
| 1129 | break; |
| 1130 | case XML_CDATA_SECTION_NODE: |
| 1131 | break; |
| 1132 | case XML_ENTITY_REF_NODE: |
| 1133 | if (node->name != NULL) |
| 1134 | fprintf(output, "%s", node->name); |
| 1135 | break; |
| 1136 | case XML_ENTITY_NODE: |
| 1137 | if (node->name != NULL) |
| 1138 | fprintf(output, "%s", node->name); |
| 1139 | break; |
| 1140 | case XML_PI_NODE: |
| 1141 | if (node->name != NULL) |
| 1142 | fprintf(output, "%s", node->name); |
| 1143 | break; |
| 1144 | case XML_COMMENT_NODE: |
| 1145 | break; |
| 1146 | case XML_DOCUMENT_NODE: |
| 1147 | break; |
| 1148 | case XML_HTML_DOCUMENT_NODE: |
| 1149 | break; |
| 1150 | case XML_DOCUMENT_TYPE_NODE: |
| 1151 | break; |
| 1152 | case XML_DOCUMENT_FRAG_NODE: |
| 1153 | break; |
| 1154 | case XML_NOTATION_NODE: |
| 1155 | break; |
| 1156 | default: |
| 1157 | if (node->name != NULL) |
| 1158 | fprintf(output, "%s", node->name); |
| 1159 | } |
| 1160 | fprintf(output, "\n"); |
| 1161 | } |
| 1162 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1163 | /** |
| 1164 | * xmlBoolToText: |
| 1165 | * @bool : a bool to turn into text |
| 1166 | * |
| 1167 | * Convenient way to turn bool into text |
| 1168 | */ |
| 1169 | const char * |
| 1170 | xmlBoolToText(int bool) |
| 1171 | { |
| 1172 | if (bool) |
| 1173 | return("True"); |
| 1174 | else |
| 1175 | return("False"); |
| 1176 | } |
| 1177 | |
| 1178 | |
| 1179 | /** |
| 1180 | * xmlGetLineNo: |
| 1181 | * @node : valid node |
| 1182 | * |
| 1183 | * Get line number of node |
| 1184 | * |
| 1185 | * Returns the line number if sucessfull, -1 otherwise |
| 1186 | */ |
| 1187 | long |
| 1188 | xmlGetLineNo(xmlNodePtr node) |
| 1189 | { |
| 1190 | long result = -1; |
| 1191 | |
| 1192 | if (!node) |
| 1193 | return result; |
| 1194 | if (node->type == XML_ELEMENT_NODE) |
| 1195 | result = (long) node->content; |
| 1196 | else if ((node->prev != NULL) && |
| 1197 | (node->prev->type == XML_ELEMENT_NODE)) |
| 1198 | result = (long) node->prev->content; |
| 1199 | else if ((node->parent != NULL) && |
| 1200 | (node->parent->type == XML_ELEMENT_NODE)) |
| 1201 | result = (long) node->parent->content; |
| 1202 | |
| 1203 | return result; |
| 1204 | } |
| 1205 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1206 | /**************************************************************** |
| 1207 | * * |
| 1208 | * The XML shell related functions * |
| 1209 | * * |
| 1210 | ****************************************************************/ |
| 1211 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1212 | |
| 1213 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1214 | /* |
| 1215 | * TODO: Improvement/cleanups for the XML shell |
| 1216 | * - allow to shell out an editor on a subpart |
| 1217 | * - cleanup function registrations (with help) and calling |
| 1218 | * - provide registration routines |
| 1219 | */ |
| 1220 | |
| 1221 | /** |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1222 | * xmlShellPrintXpathError: |
| 1223 | * @errorType: valid xpath error id |
| 1224 | * @arg : the argument that cause xpath to fail |
| 1225 | * |
| 1226 | * Print the xpath error to libxml default error channel |
| 1227 | */ |
| 1228 | void |
| 1229 | xmlShellPrintXPathError(int errorType, const char *arg) |
| 1230 | { |
| 1231 | const char *default_arg = "Result"; |
| 1232 | |
| 1233 | if (!arg) |
| 1234 | arg = default_arg; |
| 1235 | |
| 1236 | switch (errorType) { |
| 1237 | case XPATH_UNDEFINED: |
| 1238 | xmlGenericError(xmlGenericErrorContext, |
| 1239 | "%s: no such node\n", arg); |
| 1240 | break; |
| 1241 | |
| 1242 | case XPATH_BOOLEAN: |
| 1243 | xmlGenericError(xmlGenericErrorContext, |
| 1244 | "%s is a Boolean\n", arg); |
| 1245 | break; |
| 1246 | case XPATH_NUMBER: |
| 1247 | xmlGenericError(xmlGenericErrorContext, |
| 1248 | "%s is a number\n", arg); |
| 1249 | break; |
| 1250 | case XPATH_STRING: |
| 1251 | xmlGenericError(xmlGenericErrorContext, |
| 1252 | "%s is a string\n", arg); |
| 1253 | break; |
| 1254 | case XPATH_POINT: |
| 1255 | xmlGenericError(xmlGenericErrorContext, |
| 1256 | "%s is a point\n", arg); |
| 1257 | break; |
| 1258 | case XPATH_RANGE: |
| 1259 | xmlGenericError(xmlGenericErrorContext, |
| 1260 | "%s is a range\n", arg); |
| 1261 | break; |
| 1262 | case XPATH_LOCATIONSET: |
| 1263 | xmlGenericError(xmlGenericErrorContext, |
| 1264 | "%s is a range\n", arg); |
| 1265 | break; |
| 1266 | case XPATH_USERS: |
| 1267 | xmlGenericError(xmlGenericErrorContext, |
| 1268 | "%s is user-defined\n", arg); |
| 1269 | break; |
| 1270 | case XPATH_XSLT_TREE: |
| 1271 | xmlGenericError(xmlGenericErrorContext, |
| 1272 | "%s is an XSLT value tree\n", arg); |
| 1273 | break; |
| 1274 | } |
| 1275 | xmlGenericError(xmlGenericErrorContext, |
| 1276 | "Try casting the result string function (xpath builtin)\n", |
| 1277 | arg); |
| 1278 | } |
| 1279 | |
| 1280 | |
| 1281 | /** |
| 1282 | * xmlShellPrintNode: |
| 1283 | * @node : a non-null node to print to stdout |
| 1284 | * |
| 1285 | * Print node to stdout |
| 1286 | */ |
| 1287 | void |
| 1288 | xmlShellPrintNode(xmlNodePtr node) |
| 1289 | { |
| 1290 | if (!node) |
| 1291 | return; |
| 1292 | |
| 1293 | if (node->type == XML_DOCUMENT_NODE) |
| 1294 | xmlDocDump(stdout, (xmlDocPtr) node); |
| 1295 | else if (node->type == XML_ATTRIBUTE_NODE) |
| 1296 | xmlDebugDumpAttrList(stdout, (xmlAttrPtr) node, 0); |
| 1297 | else |
| 1298 | xmlElemDump(stdout, node->doc, node); |
| 1299 | |
| 1300 | fprintf(stdout, "\n"); |
| 1301 | } |
| 1302 | |
| 1303 | |
| 1304 | /** |
| 1305 | * xmlShellPrintXPathResult: |
| 1306 | * list : a valid result generated by an xpath evaluation |
| 1307 | * |
| 1308 | * Prints result to stdout |
| 1309 | */ |
| 1310 | void |
| 1311 | xmlShellPrintXPathResult(xmlXPathObjectPtr list) |
| 1312 | { |
| 1313 | int i = 0; |
| 1314 | |
| 1315 | if (list != NULL) { |
| 1316 | switch (list->type) { |
| 1317 | case XPATH_NODESET:{ |
| 1318 | int indx; |
| 1319 | |
| 1320 | if (list->nodesetval) { |
| 1321 | for (indx = 0; indx < list->nodesetval->nodeNr; |
| 1322 | indx++) { |
| 1323 | if (i > 0) |
| 1324 | fprintf(stderr, " -------\n"); |
| 1325 | xmlShellPrintNode(list->nodesetval-> |
| 1326 | nodeTab[indx]); |
| 1327 | } |
| 1328 | } else { |
| 1329 | xmlGenericError(xmlGenericErrorContext, |
| 1330 | "Empty node set\n"); |
| 1331 | } |
| 1332 | break; |
| 1333 | } |
| 1334 | case XPATH_BOOLEAN: |
| 1335 | xmlGenericError(xmlGenericErrorContext, |
| 1336 | "Is a Boolean:%s\n", |
| 1337 | xmlBoolToText(list->boolval)); |
| 1338 | break; |
| 1339 | case XPATH_NUMBER: |
| 1340 | xmlGenericError(xmlGenericErrorContext, |
| 1341 | "Is a number:%0g\n", list->floatval); |
| 1342 | break; |
| 1343 | case XPATH_STRING: |
| 1344 | xmlGenericError(xmlGenericErrorContext, |
| 1345 | "Is a string:%s\n", list->stringval); |
| 1346 | break; |
| 1347 | |
| 1348 | default: |
| 1349 | xmlShellPrintXPathError(list->type, NULL); |
| 1350 | } |
| 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1355 | * xmlShellList: |
| 1356 | * @ctxt: the shell context |
| 1357 | * @arg: unused |
| 1358 | * @node: a node |
| 1359 | * @node2: unused |
| 1360 | * |
| 1361 | * Implements the XML shell function "ls" |
| 1362 | * Does an Unix like listing of the given node (like a directory) |
| 1363 | * |
| 1364 | * Returns 0 |
| 1365 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1366 | int |
| 1367 | xmlShellList(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 1368 | char *arg ATTRIBUTE_UNUSED, xmlNodePtr node, |
| 1369 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1370 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1371 | xmlNodePtr cur; |
| 1372 | |
| 1373 | if ((node->type == XML_DOCUMENT_NODE) || |
| 1374 | (node->type == XML_HTML_DOCUMENT_NODE)) { |
| 1375 | cur = ((xmlDocPtr) node)->children; |
| 1376 | } else if (node->children != NULL) { |
| 1377 | cur = node->children; |
| 1378 | } else { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1379 | xmlLsOneNode(stdout, node); |
| 1380 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1381 | } |
| 1382 | while (cur != NULL) { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1383 | xmlLsOneNode(stdout, cur); |
| 1384 | cur = cur->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1385 | } |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1386 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | /** |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 1390 | * xmlShellBase: |
| 1391 | * @ctxt: the shell context |
| 1392 | * @arg: unused |
| 1393 | * @node: a node |
| 1394 | * @node2: unused |
| 1395 | * |
| 1396 | * Implements the XML shell function "base" |
| 1397 | * dumps the current XML base of the node |
| 1398 | * |
| 1399 | * Returns 0 |
| 1400 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1401 | int |
| 1402 | xmlShellBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 1403 | char *arg ATTRIBUTE_UNUSED, xmlNodePtr node, |
| 1404 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1405 | { |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 1406 | xmlChar *base; |
| 1407 | |
| 1408 | base = xmlNodeGetBase(node->doc, node); |
| 1409 | |
| 1410 | if (base == NULL) { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1411 | printf(" No base found !!!\n"); |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 1412 | } else { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1413 | printf("%s\n", base); |
| 1414 | xmlFree(base); |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 1415 | } |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1416 | return (0); |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1420 | * xmlShellDir: |
| 1421 | * @ctxt: the shell context |
| 1422 | * @arg: unused |
| 1423 | * @node: a node |
| 1424 | * @node2: unused |
| 1425 | * |
| 1426 | * Implements the XML shell function "dir" |
| 1427 | * dumps informations about the node (namespace, attributes, content). |
| 1428 | * |
| 1429 | * Returns 0 |
| 1430 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1431 | int |
| 1432 | xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 1433 | char *arg ATTRIBUTE_UNUSED, xmlNodePtr node, |
| 1434 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1435 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1436 | if ((node->type == XML_DOCUMENT_NODE) || |
| 1437 | (node->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1438 | xmlDebugDumpDocumentHead(stdout, (xmlDocPtr) node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1439 | } else if (node->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1440 | xmlDebugDumpAttr(stdout, (xmlAttrPtr) node, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1441 | } else { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1442 | xmlDebugDumpOneNode(stdout, node, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1443 | } |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1444 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | /** |
| 1448 | * xmlShellCat: |
| 1449 | * @ctxt: the shell context |
| 1450 | * @arg: unused |
| 1451 | * @node: a node |
| 1452 | * @node2: unused |
| 1453 | * |
| 1454 | * Implements the XML shell function "cat" |
| 1455 | * dumps the serialization node content (XML or HTML). |
| 1456 | * |
| 1457 | * Returns 0 |
| 1458 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1459 | int |
| 1460 | xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED, |
| 1461 | xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1462 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1463 | if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) { |
| 1464 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1465 | if (node->type == XML_HTML_DOCUMENT_NODE) |
| 1466 | htmlDocDump(stdout, (htmlDocPtr) node); |
| 1467 | else |
| 1468 | htmlNodeDumpFile(stdout, ctxt->doc, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1469 | #else |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1470 | if (node->type == XML_DOCUMENT_NODE) |
| 1471 | xmlDocDump(stdout, (xmlDocPtr) node); |
| 1472 | else |
| 1473 | xmlElemDump(stdout, ctxt->doc, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1474 | #endif /* LIBXML_HTML_ENABLED */ |
| 1475 | } else { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1476 | if (node->type == XML_DOCUMENT_NODE) |
| 1477 | xmlDocDump(stdout, (xmlDocPtr) node); |
| 1478 | else |
| 1479 | xmlElemDump(stdout, ctxt->doc, node); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1480 | } |
| 1481 | printf("\n"); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1482 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | /** |
| 1486 | * xmlShellLoad: |
| 1487 | * @ctxt: the shell context |
| 1488 | * @filename: the file name |
| 1489 | * @node: unused |
| 1490 | * @node2: unused |
| 1491 | * |
| 1492 | * Implements the XML shell function "load" |
| 1493 | * loads a new document specified by the filename |
| 1494 | * |
| 1495 | * Returns 0 or -1 if loading failed |
| 1496 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1497 | int |
| 1498 | xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename, |
| 1499 | xmlNodePtr node ATTRIBUTE_UNUSED, |
| 1500 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1501 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1502 | xmlDocPtr doc; |
| 1503 | int html = 0; |
| 1504 | |
| 1505 | if (ctxt->doc != NULL) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1506 | html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1507 | |
| 1508 | if (html) { |
| 1509 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1510 | doc = htmlParseFile(filename, NULL); |
| 1511 | #else |
| 1512 | printf("HTML support not compiled in\n"); |
| 1513 | doc = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1514 | #endif /* LIBXML_HTML_ENABLED */ |
| 1515 | } else { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1516 | doc = xmlParseFile(filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1517 | } |
| 1518 | if (doc != NULL) { |
| 1519 | if (ctxt->loaded == 1) { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1520 | xmlFreeDoc(ctxt->doc); |
| 1521 | } |
| 1522 | ctxt->loaded = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1523 | #ifdef LIBXML_XPATH_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1524 | xmlXPathFreeContext(ctxt->pctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1525 | #endif /* LIBXML_XPATH_ENABLED */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1526 | xmlFree(ctxt->filename); |
| 1527 | ctxt->doc = doc; |
| 1528 | ctxt->node = (xmlNodePtr) doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1529 | #ifdef LIBXML_XPATH_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1530 | ctxt->pctxt = xmlXPathNewContext(doc); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1531 | #endif /* LIBXML_XPATH_ENABLED */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1532 | ctxt->filename = (char *) xmlStrdup((xmlChar *) filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1533 | } else |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1534 | return (-1); |
| 1535 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | /** |
| 1539 | * xmlShellWrite: |
| 1540 | * @ctxt: the shell context |
| 1541 | * @filename: the file name |
| 1542 | * @node: a node in the tree |
| 1543 | * @node2: unused |
| 1544 | * |
| 1545 | * Implements the XML shell function "write" |
| 1546 | * Write the current node to the filename, it saves the serailization |
| 1547 | * of the subtree under the @node specified |
| 1548 | * |
| 1549 | * Returns 0 or -1 in case of error |
| 1550 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1551 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1552 | xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node, |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1553 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1554 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1555 | if (node == NULL) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1556 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1557 | if ((filename == NULL) || (filename[0] == 0)) { |
| 1558 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1559 | "Write command requires a filename argument\n"); |
| 1560 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1561 | } |
| 1562 | #ifdef W_OK |
| 1563 | if (access((char *) filename, W_OK)) { |
| 1564 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1565 | "Cannot write to %s\n", filename); |
| 1566 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1567 | } |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1568 | #endif |
| 1569 | switch (node->type) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1570 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1571 | if (xmlSaveFile((char *) filename, ctxt->doc) < -1) { |
| 1572 | xmlGenericError(xmlGenericErrorContext, |
| 1573 | "Failed to write to %s\n", filename); |
| 1574 | return (-1); |
| 1575 | } |
| 1576 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1577 | case XML_HTML_DOCUMENT_NODE: |
| 1578 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1579 | if (htmlSaveFile((char *) filename, ctxt->doc) < 0) { |
| 1580 | xmlGenericError(xmlGenericErrorContext, |
| 1581 | "Failed to write to %s\n", filename); |
| 1582 | return (-1); |
| 1583 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1584 | #else |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1585 | if (xmlSaveFile((char *) filename, ctxt->doc) < -1) { |
| 1586 | xmlGenericError(xmlGenericErrorContext, |
| 1587 | "Failed to write to %s\n", filename); |
| 1588 | return (-1); |
| 1589 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1590 | #endif /* LIBXML_HTML_ENABLED */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1591 | break; |
| 1592 | default:{ |
| 1593 | FILE *f; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1594 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1595 | f = fopen((char *) filename, "w"); |
| 1596 | if (f == NULL) { |
| 1597 | xmlGenericError(xmlGenericErrorContext, |
| 1598 | "Failed to write to %s\n", filename); |
| 1599 | return (-1); |
| 1600 | } |
| 1601 | xmlElemDump(f, ctxt->doc, node); |
| 1602 | fclose(f); |
| 1603 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1604 | } |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1605 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | /** |
| 1609 | * xmlShellSave: |
| 1610 | * @ctxt: the shell context |
| 1611 | * @filename: the file name (optionnal) |
| 1612 | * @node: unused |
| 1613 | * @node2: unused |
| 1614 | * |
| 1615 | * Implements the XML shell function "save" |
| 1616 | * Write the current document to the filename, or it's original name |
| 1617 | * |
| 1618 | * Returns 0 or -1 in case of error |
| 1619 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1620 | int |
| 1621 | xmlShellSave(xmlShellCtxtPtr ctxt, char *filename, |
| 1622 | xmlNodePtr node ATTRIBUTE_UNUSED, |
| 1623 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1624 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1625 | if (ctxt->doc == NULL) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1626 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1627 | if ((filename == NULL) || (filename[0] == 0)) |
| 1628 | filename = ctxt->filename; |
| 1629 | #ifdef W_OK |
| 1630 | if (access((char *) filename, W_OK)) { |
| 1631 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1632 | "Cannot save to %s\n", filename); |
| 1633 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1634 | } |
| 1635 | #endif |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1636 | switch (ctxt->doc->type) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1637 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1638 | if (xmlSaveFile((char *) filename, ctxt->doc) < 0) { |
| 1639 | xmlGenericError(xmlGenericErrorContext, |
| 1640 | "Failed to save to %s\n", filename); |
| 1641 | } |
| 1642 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1643 | case XML_HTML_DOCUMENT_NODE: |
| 1644 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1645 | if (htmlSaveFile((char *) filename, ctxt->doc) < 0) { |
| 1646 | xmlGenericError(xmlGenericErrorContext, |
| 1647 | "Failed to save to %s\n", filename); |
| 1648 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1649 | #else |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1650 | if (xmlSaveFile((char *) filename, ctxt->doc) < 0) { |
| 1651 | xmlGenericError(xmlGenericErrorContext, |
| 1652 | "Failed to save to %s\n", filename); |
| 1653 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1654 | #endif /* LIBXML_HTML_ENABLED */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1655 | break; |
| 1656 | default: |
| 1657 | xmlGenericError(xmlGenericErrorContext, |
| 1658 | "To save to subparts of a document use the 'write' command\n"); |
| 1659 | return (-1); |
| 1660 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1661 | } |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1662 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | /** |
| 1666 | * xmlShellValidate: |
| 1667 | * @ctxt: the shell context |
| 1668 | * @dtd: the DTD URI (optionnal) |
| 1669 | * @node: unused |
| 1670 | * @node2: unused |
| 1671 | * |
| 1672 | * Implements the XML shell function "validate" |
| 1673 | * Validate the document, if a DTD path is provided, then the validation |
| 1674 | * is done against the given DTD. |
| 1675 | * |
| 1676 | * Returns 0 or -1 in case of error |
| 1677 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1678 | int |
| 1679 | xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd, |
| 1680 | xmlNodePtr node ATTRIBUTE_UNUSED, |
| 1681 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1682 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1683 | xmlValidCtxt vctxt; |
| 1684 | int res = -1; |
| 1685 | |
| 1686 | vctxt.userData = stderr; |
| 1687 | vctxt.error = (xmlValidityErrorFunc) fprintf; |
| 1688 | vctxt.warning = (xmlValidityWarningFunc) fprintf; |
| 1689 | |
| 1690 | if ((dtd == NULL) || (dtd[0] == 0)) { |
| 1691 | res = xmlValidateDocument(&vctxt, ctxt->doc); |
| 1692 | } else { |
| 1693 | xmlDtdPtr subset; |
| 1694 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1695 | subset = xmlParseDTD(NULL, (xmlChar *) dtd); |
| 1696 | if (subset != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1697 | res = xmlValidateDtd(&vctxt, ctxt->doc, subset); |
| 1698 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1699 | xmlFreeDtd(subset); |
| 1700 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1701 | } |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1702 | return (res); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | /** |
| 1706 | * xmlShellDu: |
| 1707 | * @ctxt: the shell context |
| 1708 | * @arg: unused |
| 1709 | * @tree: a node defining a subtree |
| 1710 | * @node2: unused |
| 1711 | * |
| 1712 | * Implements the XML shell function "du" |
| 1713 | * show the structure of the subtree under node @tree |
| 1714 | * If @tree is null, the command works on the current node. |
| 1715 | * |
| 1716 | * Returns 0 or -1 in case of error |
| 1717 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1718 | int |
| 1719 | xmlShellDu(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 1720 | char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree, |
| 1721 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1722 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1723 | xmlNodePtr node; |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1724 | int indent = 0, i; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1725 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1726 | if (tree == NULL) |
| 1727 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1728 | node = tree; |
| 1729 | while (node != NULL) { |
| 1730 | if ((node->type == XML_DOCUMENT_NODE) || |
| 1731 | (node->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1732 | printf("/\n"); |
| 1733 | } else if (node->type == XML_ELEMENT_NODE) { |
| 1734 | for (i = 0; i < indent; i++) |
| 1735 | printf(" "); |
| 1736 | printf("%s\n", node->name); |
| 1737 | } else { |
| 1738 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1739 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1740 | /* |
| 1741 | * Browse the full subtree, deep first |
| 1742 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1743 | |
| 1744 | if ((node->type == XML_DOCUMENT_NODE) || |
| 1745 | (node->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1746 | node = ((xmlDocPtr) node)->children; |
| 1747 | } else if ((node->children != NULL) |
| 1748 | && (node->type != XML_ENTITY_REF_NODE)) { |
| 1749 | /* deep first */ |
| 1750 | node = node->children; |
| 1751 | indent++; |
| 1752 | } else if ((node != tree) && (node->next != NULL)) { |
| 1753 | /* then siblings */ |
| 1754 | node = node->next; |
| 1755 | } else if (node != tree) { |
| 1756 | /* go up to parents->next if needed */ |
| 1757 | while (node != tree) { |
| 1758 | if (node->parent != NULL) { |
| 1759 | node = node->parent; |
| 1760 | indent--; |
| 1761 | } |
| 1762 | if ((node != tree) && (node->next != NULL)) { |
| 1763 | node = node->next; |
| 1764 | break; |
| 1765 | } |
| 1766 | if (node->parent == NULL) { |
| 1767 | node = NULL; |
| 1768 | break; |
| 1769 | } |
| 1770 | if (node == tree) { |
| 1771 | node = NULL; |
| 1772 | break; |
| 1773 | } |
| 1774 | } |
| 1775 | /* exit condition */ |
| 1776 | if (node == tree) |
| 1777 | node = NULL; |
| 1778 | } else |
| 1779 | node = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1780 | } |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1781 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | /** |
| 1785 | * xmlShellPwd: |
| 1786 | * @ctxt: the shell context |
| 1787 | * @buffer: the output buffer |
| 1788 | * @tree: a node |
| 1789 | * @node2: unused |
| 1790 | * |
| 1791 | * Implements the XML shell function "pwd" |
| 1792 | * Show the full path from the root to the node, if needed building |
| 1793 | * thumblers when similar elements exists at a given ancestor level. |
| 1794 | * The output is compatible with XPath commands. |
| 1795 | * |
| 1796 | * Returns 0 or -1 in case of error |
| 1797 | */ |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1798 | int |
| 1799 | xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer, |
| 1800 | xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 1801 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1802 | xmlNodePtr cur, tmp, next; |
| 1803 | char buf[500]; |
| 1804 | char sep; |
| 1805 | const char *name; |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1806 | char nametemp[100]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1807 | int occur = 0; |
| 1808 | |
| 1809 | buffer[0] = 0; |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1810 | if (node == NULL) |
| 1811 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1812 | cur = node; |
| 1813 | do { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1814 | name = ""; |
| 1815 | sep = '?'; |
| 1816 | occur = 0; |
| 1817 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 1818 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 1819 | sep = '/'; |
| 1820 | next = NULL; |
| 1821 | } else if (cur->type == XML_ELEMENT_NODE) { |
| 1822 | sep = '/'; |
| 1823 | name = (const char *) cur->name; |
| 1824 | if (cur->ns) { |
| 1825 | snprintf(nametemp, 99, "%s:%s", cur->ns->prefix, |
| 1826 | cur->name); |
| 1827 | name = nametemp; |
| 1828 | } |
| 1829 | next = cur->parent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1830 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1831 | /* |
| 1832 | * Thumbler index computation |
| 1833 | */ |
| 1834 | tmp = cur->prev; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1835 | while (tmp != NULL) { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1836 | if (xmlStrEqual(cur->name, tmp->name)) |
| 1837 | occur++; |
| 1838 | tmp = tmp->prev; |
| 1839 | } |
| 1840 | if (occur == 0) { |
| 1841 | tmp = cur->next; |
| 1842 | while (tmp != NULL) { |
| 1843 | if (xmlStrEqual(cur->name, tmp->name)) |
| 1844 | occur++; |
| 1845 | tmp = tmp->next; |
| 1846 | } |
| 1847 | if (occur != 0) |
| 1848 | occur = 1; |
| 1849 | } else |
| 1850 | occur++; |
| 1851 | } else if (cur->type == XML_ATTRIBUTE_NODE) { |
| 1852 | sep = '@'; |
| 1853 | name = (const char *) (((xmlAttrPtr) cur)->name); |
| 1854 | next = ((xmlAttrPtr) cur)->parent; |
| 1855 | } else { |
| 1856 | next = cur->parent; |
| 1857 | } |
| 1858 | if (occur == 0) |
| 1859 | snprintf(buf, sizeof(buf), "%c%s%s", sep, name, buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1860 | else |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1861 | snprintf(buf, sizeof(buf), "%c%s[%d]%s", |
| 1862 | sep, name, occur, buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1863 | buf[sizeof(buf) - 1] = 0; |
| 1864 | /* |
| 1865 | * This test prevents buffer overflow, because this routine |
| 1866 | * is only called by xmlShell, in which the second argument is |
| 1867 | * 500 chars long. |
| 1868 | * It is a dirty hack before a cleaner solution is found. |
| 1869 | * Documentation should mention that the second argument must |
| 1870 | * be at least 500 chars long, and could be stripped if too long. |
| 1871 | */ |
| 1872 | if (strlen(buffer) + strlen(buf) > 499) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1873 | break; |
| 1874 | strcpy(buffer, buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1875 | cur = next; |
| 1876 | } while (cur != NULL); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1877 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | /** |
| 1881 | * xmlShell |
| 1882 | * @doc: the initial document |
| 1883 | * @filename: the output buffer |
| 1884 | * @input: the line reading function |
| 1885 | * @output: the output FILE* |
| 1886 | * |
| 1887 | * Implements the XML shell |
| 1888 | * This allow to load, validate, view, modify and save a document |
| 1889 | * using a environment similar to a UNIX commandline. |
| 1890 | */ |
| 1891 | void |
| 1892 | xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input, |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1893 | FILE * output) |
| 1894 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1895 | char prompt[500] = "/ > "; |
| 1896 | char *cmdline = NULL, *cur; |
| 1897 | int nbargs; |
| 1898 | char command[100]; |
| 1899 | char arg[400]; |
| 1900 | int i; |
| 1901 | xmlShellCtxtPtr ctxt; |
| 1902 | xmlXPathObjectPtr list; |
| 1903 | |
| 1904 | if (doc == NULL) |
| 1905 | return; |
| 1906 | if (filename == NULL) |
| 1907 | return; |
| 1908 | if (input == NULL) |
| 1909 | return; |
| 1910 | if (output == NULL) |
| 1911 | return; |
| 1912 | ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt)); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1913 | if (ctxt == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1914 | return; |
| 1915 | ctxt->loaded = 0; |
| 1916 | ctxt->doc = doc; |
| 1917 | ctxt->input = input; |
| 1918 | ctxt->output = output; |
| 1919 | ctxt->filename = (char *) xmlStrdup((xmlChar *) filename); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1920 | ctxt->node = (xmlNodePtr) ctxt->doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1921 | |
| 1922 | #ifdef LIBXML_XPATH_ENABLED |
| 1923 | ctxt->pctxt = xmlXPathNewContext(ctxt->doc); |
| 1924 | if (ctxt->pctxt == NULL) { |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1925 | xmlFree(ctxt); |
| 1926 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1927 | } |
| 1928 | #endif /* LIBXML_XPATH_ENABLED */ |
| 1929 | while (1) { |
| 1930 | if (ctxt->node == (xmlNodePtr) ctxt->doc) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1931 | sprintf(prompt, "%s > ", "/"); |
| 1932 | else if (ctxt->node->name) |
| 1933 | snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1934 | else |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1935 | sprintf(prompt, "? > "); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1936 | prompt[sizeof(prompt) - 1] = 0; |
| 1937 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1938 | /* |
| 1939 | * Get a new command line |
| 1940 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1941 | cmdline = ctxt->input(prompt); |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1942 | if (cmdline == NULL) |
| 1943 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1944 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1945 | /* |
| 1946 | * Parse the command itself |
| 1947 | */ |
| 1948 | cur = cmdline; |
| 1949 | nbargs = 0; |
| 1950 | while ((*cur == ' ') || (*cur == '\t')) |
| 1951 | cur++; |
| 1952 | i = 0; |
| 1953 | while ((*cur != ' ') && (*cur != '\t') && |
| 1954 | (*cur != '\n') && (*cur != '\r')) { |
| 1955 | if (*cur == 0) |
| 1956 | break; |
| 1957 | command[i++] = *cur++; |
| 1958 | } |
| 1959 | command[i] = 0; |
| 1960 | if (i == 0) |
| 1961 | continue; |
| 1962 | nbargs++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1963 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1964 | /* |
| 1965 | * Parse the argument |
| 1966 | */ |
| 1967 | while ((*cur == ' ') || (*cur == '\t')) |
| 1968 | cur++; |
| 1969 | i = 0; |
| 1970 | while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) { |
| 1971 | if (*cur == 0) |
| 1972 | break; |
| 1973 | arg[i++] = *cur++; |
| 1974 | } |
| 1975 | arg[i] = 0; |
| 1976 | if (i != 0) |
| 1977 | nbargs++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1978 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1979 | /* |
| 1980 | * start interpreting the command |
| 1981 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1982 | if (!strcmp(command, "exit")) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1983 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1984 | if (!strcmp(command, "quit")) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1985 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1986 | if (!strcmp(command, "bye")) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 1987 | break; |
| 1988 | if (!strcmp(command, "validate")) { |
| 1989 | xmlShellValidate(ctxt, arg, NULL, NULL); |
| 1990 | } else if (!strcmp(command, "load")) { |
| 1991 | xmlShellLoad(ctxt, arg, NULL, NULL); |
| 1992 | } else if (!strcmp(command, "save")) { |
| 1993 | xmlShellSave(ctxt, arg, NULL, NULL); |
| 1994 | } else if (!strcmp(command, "write")) { |
| 1995 | xmlShellWrite(ctxt, arg, NULL, NULL); |
| 1996 | } else if (!strcmp(command, "free")) { |
| 1997 | if (arg[0] == 0) { |
| 1998 | xmlMemShow(stdout, 0); |
| 1999 | } else { |
| 2000 | int len = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2001 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2002 | sscanf(arg, "%d", &len); |
| 2003 | xmlMemShow(stdout, len); |
| 2004 | } |
| 2005 | } else if (!strcmp(command, "pwd")) { |
| 2006 | char dir[500]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2007 | |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2008 | if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL)) |
| 2009 | printf("%s\n", dir); |
| 2010 | } else if (!strcmp(command, "du")) { |
| 2011 | xmlShellDu(ctxt, NULL, ctxt->node, NULL); |
| 2012 | } else if (!strcmp(command, "base")) { |
| 2013 | xmlShellBase(ctxt, NULL, ctxt->node, NULL); |
| 2014 | } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) { |
| 2015 | int dir = (!strcmp(command, "dir")); |
| 2016 | |
| 2017 | if (arg[0] == 0) { |
| 2018 | if (dir) |
| 2019 | xmlShellDir(ctxt, NULL, ctxt->node, NULL); |
| 2020 | else |
| 2021 | xmlShellList(ctxt, NULL, ctxt->node, NULL); |
| 2022 | } else { |
| 2023 | ctxt->pctxt->node = ctxt->node; |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 2024 | #ifdef LIBXML_XPATH_ENABLED |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2025 | ctxt->pctxt->node = ctxt->node; |
| 2026 | list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt); |
| 2027 | #else |
| 2028 | list = NULL; |
| 2029 | #endif /* LIBXML_XPATH_ENABLED */ |
| 2030 | if (list != NULL) { |
| 2031 | switch (list->type) { |
| 2032 | case XPATH_UNDEFINED: |
| 2033 | xmlGenericError(xmlGenericErrorContext, |
| 2034 | "%s: no such node\n", arg); |
| 2035 | break; |
| 2036 | case XPATH_NODESET:{ |
| 2037 | int indx; |
| 2038 | |
| 2039 | for (indx = 0; |
| 2040 | indx < list->nodesetval->nodeNr; |
| 2041 | indx++) { |
| 2042 | if (dir) |
| 2043 | xmlShellDir(ctxt, NULL, |
| 2044 | list->nodesetval-> |
| 2045 | nodeTab[indx], NULL); |
| 2046 | else |
| 2047 | xmlShellList(ctxt, NULL, |
| 2048 | list->nodesetval-> |
| 2049 | nodeTab[indx], NULL); |
| 2050 | } |
| 2051 | break; |
| 2052 | } |
| 2053 | case XPATH_BOOLEAN: |
| 2054 | xmlGenericError(xmlGenericErrorContext, |
| 2055 | "%s is a Boolean\n", arg); |
| 2056 | break; |
| 2057 | case XPATH_NUMBER: |
| 2058 | xmlGenericError(xmlGenericErrorContext, |
| 2059 | "%s is a number\n", arg); |
| 2060 | break; |
| 2061 | case XPATH_STRING: |
| 2062 | xmlGenericError(xmlGenericErrorContext, |
| 2063 | "%s is a string\n", arg); |
| 2064 | break; |
| 2065 | case XPATH_POINT: |
| 2066 | xmlGenericError(xmlGenericErrorContext, |
| 2067 | "%s is a point\n", arg); |
| 2068 | break; |
| 2069 | case XPATH_RANGE: |
| 2070 | xmlGenericError(xmlGenericErrorContext, |
| 2071 | "%s is a range\n", arg); |
| 2072 | break; |
| 2073 | case XPATH_LOCATIONSET: |
| 2074 | xmlGenericError(xmlGenericErrorContext, |
| 2075 | "%s is a range\n", arg); |
| 2076 | break; |
| 2077 | case XPATH_USERS: |
| 2078 | xmlGenericError(xmlGenericErrorContext, |
| 2079 | "%s is user-defined\n", arg); |
| 2080 | break; |
| 2081 | case XPATH_XSLT_TREE: |
| 2082 | xmlGenericError(xmlGenericErrorContext, |
| 2083 | "%s is an XSLT value tree\n", |
| 2084 | arg); |
| 2085 | break; |
| 2086 | } |
| 2087 | #ifdef LIBXML_XPATH_ENABLED |
| 2088 | xmlXPathFreeObject(list); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 2089 | #endif |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2090 | } else { |
| 2091 | xmlGenericError(xmlGenericErrorContext, |
| 2092 | "%s: no such node\n", arg); |
| 2093 | } |
| 2094 | ctxt->pctxt->node = NULL; |
| 2095 | } |
| 2096 | } else if (!strcmp(command, "cd")) { |
| 2097 | if (arg[0] == 0) { |
| 2098 | ctxt->node = (xmlNodePtr) ctxt->doc; |
| 2099 | } else { |
| 2100 | #ifdef LIBXML_XPATH_ENABLED |
| 2101 | ctxt->pctxt->node = ctxt->node; |
| 2102 | list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt); |
| 2103 | #else |
| 2104 | list = NULL; |
| 2105 | #endif /* LIBXML_XPATH_ENABLED */ |
| 2106 | if (list != NULL) { |
| 2107 | switch (list->type) { |
| 2108 | case XPATH_UNDEFINED: |
| 2109 | xmlGenericError(xmlGenericErrorContext, |
| 2110 | "%s: no such node\n", arg); |
| 2111 | break; |
| 2112 | case XPATH_NODESET: |
| 2113 | if (list->nodesetval->nodeNr == 1) { |
| 2114 | ctxt->node = list->nodesetval->nodeTab[0]; |
| 2115 | } else |
| 2116 | xmlGenericError(xmlGenericErrorContext, |
| 2117 | "%s is a %d Node Set\n", |
| 2118 | arg, |
| 2119 | list->nodesetval->nodeNr); |
| 2120 | break; |
| 2121 | case XPATH_BOOLEAN: |
| 2122 | xmlGenericError(xmlGenericErrorContext, |
| 2123 | "%s is a Boolean\n", arg); |
| 2124 | break; |
| 2125 | case XPATH_NUMBER: |
| 2126 | xmlGenericError(xmlGenericErrorContext, |
| 2127 | "%s is a number\n", arg); |
| 2128 | break; |
| 2129 | case XPATH_STRING: |
| 2130 | xmlGenericError(xmlGenericErrorContext, |
| 2131 | "%s is a string\n", arg); |
| 2132 | break; |
| 2133 | case XPATH_POINT: |
| 2134 | xmlGenericError(xmlGenericErrorContext, |
| 2135 | "%s is a point\n", arg); |
| 2136 | break; |
| 2137 | case XPATH_RANGE: |
| 2138 | xmlGenericError(xmlGenericErrorContext, |
| 2139 | "%s is a range\n", arg); |
| 2140 | break; |
| 2141 | case XPATH_LOCATIONSET: |
| 2142 | xmlGenericError(xmlGenericErrorContext, |
| 2143 | "%s is a range\n", arg); |
| 2144 | break; |
| 2145 | case XPATH_USERS: |
| 2146 | xmlGenericError(xmlGenericErrorContext, |
| 2147 | "%s is user-defined\n", arg); |
| 2148 | break; |
| 2149 | case XPATH_XSLT_TREE: |
| 2150 | xmlGenericError(xmlGenericErrorContext, |
| 2151 | "%s is an XSLT value tree\n", |
| 2152 | arg); |
| 2153 | break; |
| 2154 | } |
| 2155 | #ifdef LIBXML_XPATH_ENABLED |
| 2156 | xmlXPathFreeObject(list); |
| 2157 | #endif |
| 2158 | } else { |
| 2159 | xmlGenericError(xmlGenericErrorContext, |
| 2160 | "%s: no such node\n", arg); |
| 2161 | } |
| 2162 | ctxt->pctxt->node = NULL; |
| 2163 | } |
| 2164 | } else if (!strcmp(command, "cat")) { |
| 2165 | if (arg[0] == 0) { |
| 2166 | xmlShellCat(ctxt, NULL, ctxt->node, NULL); |
| 2167 | } else { |
| 2168 | ctxt->pctxt->node = ctxt->node; |
| 2169 | #ifdef LIBXML_XPATH_ENABLED |
| 2170 | ctxt->pctxt->node = ctxt->node; |
| 2171 | list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt); |
| 2172 | #else |
| 2173 | list = NULL; |
| 2174 | #endif /* LIBXML_XPATH_ENABLED */ |
| 2175 | if (list != NULL) { |
| 2176 | switch (list->type) { |
| 2177 | case XPATH_UNDEFINED: |
| 2178 | xmlGenericError(xmlGenericErrorContext, |
| 2179 | "%s: no such node\n", arg); |
| 2180 | break; |
| 2181 | case XPATH_NODESET:{ |
| 2182 | int indx; |
| 2183 | |
| 2184 | for (indx = 0; |
| 2185 | indx < list->nodesetval->nodeNr; |
| 2186 | indx++) { |
| 2187 | if (i > 0) |
| 2188 | printf(" -------\n"); |
| 2189 | xmlShellCat(ctxt, NULL, |
| 2190 | list->nodesetval-> |
| 2191 | nodeTab[indx], NULL); |
| 2192 | } |
| 2193 | break; |
| 2194 | } |
| 2195 | case XPATH_BOOLEAN: |
| 2196 | xmlGenericError(xmlGenericErrorContext, |
| 2197 | "%s is a Boolean\n", arg); |
| 2198 | break; |
| 2199 | case XPATH_NUMBER: |
| 2200 | xmlGenericError(xmlGenericErrorContext, |
| 2201 | "%s is a number\n", arg); |
| 2202 | break; |
| 2203 | case XPATH_STRING: |
| 2204 | xmlGenericError(xmlGenericErrorContext, |
| 2205 | "%s is a string\n", arg); |
| 2206 | break; |
| 2207 | case XPATH_POINT: |
| 2208 | xmlGenericError(xmlGenericErrorContext, |
| 2209 | "%s is a point\n", arg); |
| 2210 | break; |
| 2211 | case XPATH_RANGE: |
| 2212 | xmlGenericError(xmlGenericErrorContext, |
| 2213 | "%s is a range\n", arg); |
| 2214 | break; |
| 2215 | case XPATH_LOCATIONSET: |
| 2216 | xmlGenericError(xmlGenericErrorContext, |
| 2217 | "%s is a range\n", arg); |
| 2218 | break; |
| 2219 | case XPATH_USERS: |
| 2220 | xmlGenericError(xmlGenericErrorContext, |
| 2221 | "%s is user-defined\n", arg); |
| 2222 | break; |
| 2223 | case XPATH_XSLT_TREE: |
| 2224 | xmlGenericError(xmlGenericErrorContext, |
| 2225 | "%s is an XSLT value tree\n", |
| 2226 | arg); |
| 2227 | break; |
| 2228 | } |
| 2229 | #ifdef LIBXML_XPATH_ENABLED |
| 2230 | xmlXPathFreeObject(list); |
| 2231 | #endif |
| 2232 | } else { |
| 2233 | xmlGenericError(xmlGenericErrorContext, |
| 2234 | "%s: no such node\n", arg); |
| 2235 | } |
| 2236 | ctxt->pctxt->node = NULL; |
| 2237 | } |
| 2238 | } else { |
| 2239 | xmlGenericError(xmlGenericErrorContext, |
| 2240 | "Unknown command %s\n", command); |
| 2241 | } |
| 2242 | free(cmdline); /* not xmlFree here ! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2243 | } |
| 2244 | #ifdef LIBXML_XPATH_ENABLED |
| 2245 | xmlXPathFreeContext(ctxt->pctxt); |
| 2246 | #endif /* LIBXML_XPATH_ENABLED */ |
| 2247 | if (ctxt->loaded) { |
| 2248 | xmlFreeDoc(ctxt->doc); |
| 2249 | } |
Daniel Veillard | b8c9be9 | 2001-07-09 16:01:19 +0000 | [diff] [blame] | 2250 | if (ctxt->filename != NULL) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2251 | xmlFree(ctxt->filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2252 | xmlFree(ctxt); |
| 2253 | if (cmdline != NULL) |
Daniel Veillard | 78d1209 | 2001-10-11 09:12:24 +0000 | [diff] [blame] | 2254 | free(cmdline); /* not xmlFree here ! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
| 2257 | #endif /* LIBXML_DEBUG_ENABLED */ |