Coverage Report

Created: 2025-07-01 06:27

/src/libxml2/shell.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * shell.c: The xmllint shell
3
 *
4
 * See Copyright for the status of this software.
5
 *
6
 * Author: Daniel Veillard
7
 */
8
9
#include "libxml.h"
10
11
#include <stdio.h>
12
#include <stdlib.h>
13
#include <string.h>
14
15
#ifdef _WIN32
16
  #include <io.h>
17
#else
18
  #include <unistd.h>
19
#endif
20
21
#ifdef HAVE_LIBREADLINE
22
#include <readline/readline.h>
23
#ifdef HAVE_LIBHISTORY
24
#include <readline/history.h>
25
#endif
26
#endif
27
28
#include <libxml/debugXML.h>
29
#include <libxml/HTMLtree.h>
30
#include <libxml/parser.h>
31
#include <libxml/uri.h>
32
#include <libxml/xpath.h>
33
#include <libxml/xpathInternals.h>
34
#ifdef LIBXML_RELAXNG_ENABLED
35
#include <libxml/relaxng.h>
36
#endif
37
38
#include "private/lint.h"
39
40
#ifndef STDIN_FILENO
41
  #define STDIN_FILENO 0
42
#endif
43
44
/*
45
 * TODO: Improvement/cleanups for the XML shell
46
 *     - allow to shell out an editor on a subpart
47
 *     - cleanup function registrations (with help) and calling
48
 *     - provide registration routines
49
 */
50
51
typedef struct _xmllintShellCtxt xmllintShellCtxt;
52
typedef xmllintShellCtxt *xmllintShellCtxtPtr;
53
struct _xmllintShellCtxt {
54
    char *filename;
55
    xmlDocPtr doc;
56
    xmlNodePtr node;
57
#ifdef LIBXML_XPATH_ENABLED
58
    xmlXPathContextPtr pctxt;
59
#endif
60
    int loaded;
61
    FILE *output;
62
};
63
64
/**
65
 * Count the children of `node`.
66
 *
67
 * @param node  the node to count
68
 * @returns the number of children of `node`.
69
 */
70
static int
71
0
xmllintLsCountNode(xmlNodePtr node) {
72
0
    int ret = 0;
73
0
    xmlNodePtr list = NULL;
74
75
0
    if (node == NULL)
76
0
  return(0);
77
78
0
    switch (node->type) {
79
0
  case XML_ELEMENT_NODE:
80
0
      list = node->children;
81
0
      break;
82
0
  case XML_DOCUMENT_NODE:
83
0
  case XML_HTML_DOCUMENT_NODE:
84
0
      list = ((xmlDocPtr) node)->children;
85
0
      break;
86
0
  case XML_ATTRIBUTE_NODE:
87
0
      list = ((xmlAttrPtr) node)->children;
88
0
      break;
89
0
  case XML_TEXT_NODE:
90
0
  case XML_CDATA_SECTION_NODE:
91
0
  case XML_PI_NODE:
92
0
  case XML_COMMENT_NODE:
93
0
      if (node->content != NULL) {
94
0
    ret = xmlStrlen(node->content);
95
0
            }
96
0
      break;
97
0
  case XML_ENTITY_REF_NODE:
98
0
  case XML_DOCUMENT_TYPE_NODE:
99
0
  case XML_ENTITY_NODE:
100
0
  case XML_DOCUMENT_FRAG_NODE:
101
0
  case XML_NOTATION_NODE:
102
0
  case XML_DTD_NODE:
103
0
        case XML_ELEMENT_DECL:
104
0
        case XML_ATTRIBUTE_DECL:
105
0
        case XML_ENTITY_DECL:
106
0
  case XML_NAMESPACE_DECL:
107
0
  case XML_XINCLUDE_START:
108
0
  case XML_XINCLUDE_END:
109
0
      ret = 1;
110
0
      break;
111
0
    }
112
0
    for (;list != NULL;ret++)
113
0
        list = list->next;
114
0
    return(ret);
115
0
}
116
117
/**
118
 * Dump to `output` the type and name of `node`.
119
 *
120
 * @param output  the FILE * for the output
121
 * @param node  the node to dump
122
 */
123
static void
124
0
xmllintLsOneNode(FILE *output, xmlNodePtr node) {
125
0
    if (output == NULL) return;
126
0
    if (node == NULL) {
127
0
  fprintf(output, "NULL\n");
128
0
  return;
129
0
    }
130
0
    switch (node->type) {
131
0
  case XML_ELEMENT_NODE:
132
0
      fprintf(output, "-");
133
0
      break;
134
0
  case XML_ATTRIBUTE_NODE:
135
0
      fprintf(output, "a");
136
0
      break;
137
0
  case XML_TEXT_NODE:
138
0
      fprintf(output, "t");
139
0
      break;
140
0
  case XML_CDATA_SECTION_NODE:
141
0
      fprintf(output, "C");
142
0
      break;
143
0
  case XML_ENTITY_REF_NODE:
144
0
      fprintf(output, "e");
145
0
      break;
146
0
  case XML_ENTITY_NODE:
147
0
      fprintf(output, "E");
148
0
      break;
149
0
  case XML_PI_NODE:
150
0
      fprintf(output, "p");
151
0
      break;
152
0
  case XML_COMMENT_NODE:
153
0
      fprintf(output, "c");
154
0
      break;
155
0
  case XML_DOCUMENT_NODE:
156
0
      fprintf(output, "d");
157
0
      break;
158
0
  case XML_HTML_DOCUMENT_NODE:
159
0
      fprintf(output, "h");
160
0
      break;
161
0
  case XML_DOCUMENT_TYPE_NODE:
162
0
      fprintf(output, "T");
163
0
      break;
164
0
  case XML_DOCUMENT_FRAG_NODE:
165
0
      fprintf(output, "F");
166
0
      break;
167
0
  case XML_NOTATION_NODE:
168
0
      fprintf(output, "N");
169
0
      break;
170
0
  case XML_NAMESPACE_DECL:
171
0
      fprintf(output, "n");
172
0
      break;
173
0
  default:
174
0
      fprintf(output, "?");
175
0
    }
176
0
    if (node->type != XML_NAMESPACE_DECL) {
177
0
  if (node->properties != NULL)
178
0
      fprintf(output, "a");
179
0
  else
180
0
      fprintf(output, "-");
181
0
  if (node->nsDef != NULL)
182
0
      fprintf(output, "n");
183
0
  else
184
0
      fprintf(output, "-");
185
0
    }
186
187
0
    fprintf(output, " %8d ", xmllintLsCountNode(node));
188
189
0
    switch (node->type) {
190
0
  case XML_ELEMENT_NODE:
191
0
      if (node->name != NULL) {
192
0
                if ((node->ns != NULL) && (node->ns->prefix != NULL))
193
0
                    fprintf(output, "%s:", node->ns->prefix);
194
0
    fprintf(output, "%s", (const char *) node->name);
195
0
            }
196
0
      break;
197
0
  case XML_ATTRIBUTE_NODE:
198
0
      if (node->name != NULL)
199
0
    fprintf(output, "%s", (const char *) node->name);
200
0
      break;
201
0
  case XML_TEXT_NODE:
202
#ifdef LIBXML_DEBUG_ENABLED
203
      if (node->content != NULL) {
204
    xmlDebugDumpString(output, node->content);
205
            }
206
#endif
207
0
      break;
208
0
  case XML_CDATA_SECTION_NODE:
209
0
      break;
210
0
  case XML_ENTITY_REF_NODE:
211
0
      if (node->name != NULL)
212
0
    fprintf(output, "%s", (const char *) node->name);
213
0
      break;
214
0
  case XML_ENTITY_NODE:
215
0
      if (node->name != NULL)
216
0
    fprintf(output, "%s", (const char *) node->name);
217
0
      break;
218
0
  case XML_PI_NODE:
219
0
      if (node->name != NULL)
220
0
    fprintf(output, "%s", (const char *) node->name);
221
0
      break;
222
0
  case XML_COMMENT_NODE:
223
0
      break;
224
0
  case XML_DOCUMENT_NODE:
225
0
      break;
226
0
  case XML_HTML_DOCUMENT_NODE:
227
0
      break;
228
0
  case XML_DOCUMENT_TYPE_NODE:
229
0
      break;
230
0
  case XML_DOCUMENT_FRAG_NODE:
231
0
      break;
232
0
  case XML_NOTATION_NODE:
233
0
      break;
234
0
  case XML_NAMESPACE_DECL: {
235
0
      xmlNsPtr ns = (xmlNsPtr) node;
236
237
0
      if (ns->prefix == NULL)
238
0
    fprintf(output, "default -> %s", (char *)ns->href);
239
0
      else
240
0
    fprintf(output, "%s -> %s", (char *)ns->prefix,
241
0
      (char *)ns->href);
242
0
      break;
243
0
  }
244
0
  default:
245
0
      if (node->name != NULL)
246
0
    fprintf(output, "%s", (const char *) node->name);
247
0
    }
248
0
    fprintf(output, "\n");
249
0
}
250
251
/**
252
 * Implements the XML shell function "ls"
253
 * Does an Unix like listing of the given node (like a directory)
254
 *
255
 * @param ctxt  the shell context
256
 * @param arg  unused
257
 * @param node  a node
258
 * @param node2  unused
259
 * @returns 0
260
 */
261
static int
262
xmllintShellList(xmllintShellCtxtPtr ctxt,
263
             char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
264
             xmlNodePtr node2 ATTRIBUTE_UNUSED)
265
0
{
266
0
    xmlNodePtr cur;
267
0
    if (!ctxt)
268
0
        return (0);
269
0
    if (node == NULL) {
270
0
  fprintf(ctxt->output, "NULL\n");
271
0
  return (0);
272
0
    }
273
0
    if ((node->type == XML_DOCUMENT_NODE) ||
274
0
        (node->type == XML_HTML_DOCUMENT_NODE)) {
275
0
        cur = ((xmlDocPtr) node)->children;
276
0
    } else if (node->type == XML_NAMESPACE_DECL) {
277
0
        xmllintLsOneNode(ctxt->output, node);
278
0
        return (0);
279
0
    } else if (node->children != NULL) {
280
0
        cur = node->children;
281
0
    } else {
282
0
        xmllintLsOneNode(ctxt->output, node);
283
0
        return (0);
284
0
    }
285
0
    while (cur != NULL) {
286
0
        xmllintLsOneNode(ctxt->output, cur);
287
0
        cur = cur->next;
288
0
    }
289
0
    return (0);
290
0
}
291
292
/**
293
 * Implements the XML shell function "base"
294
 * dumps the current XML base of the node
295
 *
296
 * @param ctxt  the shell context
297
 * @param arg  unused
298
 * @param node  a node
299
 * @param node2  unused
300
 * @returns 0
301
 */
302
static int
303
xmllintShellBase(xmllintShellCtxtPtr ctxt,
304
             char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
305
             xmlNodePtr node2 ATTRIBUTE_UNUSED)
306
0
{
307
0
    xmlChar *base;
308
0
    if (!ctxt)
309
0
        return 0;
310
0
    if (node == NULL) {
311
0
  fprintf(ctxt->output, "NULL\n");
312
0
  return (0);
313
0
    }
314
315
0
    base = xmlNodeGetBase(node->doc, node);
316
317
0
    if (base == NULL) {
318
0
        fprintf(ctxt->output, " No base found !!!\n");
319
0
    } else {
320
0
        fprintf(ctxt->output, "%s\n", base);
321
0
        xmlFree(base);
322
0
    }
323
0
    return (0);
324
0
}
325
326
/**
327
 * Implements the XML shell function "setbase"
328
 * change the current XML base of the node
329
 *
330
 * @param ctxt  the shell context
331
 * @param arg  the new base
332
 * @param node  a node
333
 * @param node2  unused
334
 * @returns 0
335
 */
336
static int
337
xmllintShellSetBase(xmllintShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
338
             char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
339
             xmlNodePtr node2 ATTRIBUTE_UNUSED)
340
0
{
341
0
    xmlNodeSetBase(node, (xmlChar*) arg);
342
0
    return (0);
343
0
}
344
345
#ifdef LIBXML_XPATH_ENABLED
346
/**
347
 * Implements the XML shell function "setns"
348
 * register/unregister a prefix=namespace pair
349
 * on the XPath context
350
 *
351
 * @param ctxt  the shell context
352
 * @param arg  a string in prefix=nsuri format
353
 * @param node  unused
354
 * @param node2  unused
355
 * @returns 0 on success and a negative value otherwise.
356
 */
357
static int
358
xmllintShellRegisterNamespace(xmllintShellCtxtPtr ctxt, char *arg,
359
      xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr node2 ATTRIBUTE_UNUSED)
360
0
{
361
0
    xmlChar* nsListDup;
362
0
    xmlChar* prefix;
363
0
    xmlChar* href;
364
0
    xmlChar* next;
365
366
0
    nsListDup = xmlStrdup((xmlChar *) arg);
367
0
    next = nsListDup;
368
0
    while(next != NULL) {
369
  /* skip spaces */
370
  /*while((*next) == ' ') next++;*/
371
0
  if((*next) == '\0') break;
372
373
  /* find prefix */
374
0
  prefix = next;
375
0
  next = (xmlChar*)xmlStrchr(next, '=');
376
0
  if(next == NULL) {
377
0
      fprintf(ctxt->output, "setns: prefix=[nsuri] required\n");
378
0
      xmlFree(nsListDup);
379
0
      return(-1);
380
0
  }
381
0
  *(next++) = '\0';
382
383
  /* find href */
384
0
  href = next;
385
0
  next = (xmlChar*)xmlStrchr(next, ' ');
386
0
  if(next != NULL) {
387
0
      *(next++) = '\0';
388
0
  }
389
390
  /* do register namespace */
391
0
  if(xmlXPathRegisterNs(ctxt->pctxt, prefix, href) != 0) {
392
0
      fprintf(ctxt->output,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, href);
393
0
      xmlFree(nsListDup);
394
0
      return(-1);
395
0
  }
396
0
    }
397
398
0
    xmlFree(nsListDup);
399
0
    return(0);
400
0
}
401
/**
402
 * Implements the XML shell function "setrootns"
403
 * which registers all namespaces declarations found on the root element.
404
 *
405
 * @param ctxt  the shell context
406
 * @param arg  unused
407
 * @param root  the root element
408
 * @param node2  unused
409
 * @returns 0 on success and a negative value otherwise.
410
 */
411
static int
412
xmllintShellRegisterRootNamespaces(xmllintShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
413
      xmlNodePtr root, xmlNodePtr node2 ATTRIBUTE_UNUSED)
414
0
{
415
0
    xmlNsPtr ns;
416
417
0
    if ((root == NULL) || (root->type != XML_ELEMENT_NODE) ||
418
0
        (root->nsDef == NULL) || (ctxt == NULL) || (ctxt->pctxt == NULL))
419
0
  return(-1);
420
0
    ns = root->nsDef;
421
0
    while (ns != NULL) {
422
0
        if (ns->prefix == NULL)
423
0
      xmlXPathRegisterNs(ctxt->pctxt, BAD_CAST "defaultns", ns->href);
424
0
  else
425
0
      xmlXPathRegisterNs(ctxt->pctxt, ns->prefix, ns->href);
426
0
        ns = ns->next;
427
0
    }
428
0
    return(0);
429
0
}
430
#endif
431
432
/**
433
 * Implements the XML shell function "grep"
434
 * dumps information about the node (namespace, attributes, content).
435
 *
436
 * @param ctxt  the shell context
437
 * @param arg  the string or regular expression to find
438
 * @param node  a node
439
 * @param node2  unused
440
 * @returns 0
441
 */
442
static int
443
xmllintShellGrep(xmllintShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
444
            char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
445
0
{
446
0
    if (!ctxt)
447
0
        return (0);
448
0
    if (node == NULL)
449
0
  return (0);
450
0
    if (arg == NULL)
451
0
  return (0);
452
0
#ifdef LIBXML_REGEXP_ENABLED
453
0
    if ((xmlStrchr((xmlChar *) arg, '?')) ||
454
0
  (xmlStrchr((xmlChar *) arg, '*')) ||
455
0
  (xmlStrchr((xmlChar *) arg, '.')) ||
456
0
  (xmlStrchr((xmlChar *) arg, '['))) {
457
0
    }
458
0
#endif
459
0
    while (node != NULL) {
460
0
        if (node->type == XML_COMMENT_NODE) {
461
0
      if (xmlStrstr(node->content, (xmlChar *) arg)) {
462
0
    fprintf(ctxt->output, "%s : ", xmlGetNodePath(node));
463
0
                xmllintShellList(ctxt, NULL, node, NULL);
464
0
      }
465
0
        } else if (node->type == XML_TEXT_NODE) {
466
0
      if (xmlStrstr(node->content, (xmlChar *) arg)) {
467
0
    fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent));
468
0
                xmllintShellList(ctxt, NULL, node->parent, NULL);
469
0
      }
470
0
        }
471
472
        /*
473
         * Browse the full subtree, deep first
474
         */
475
476
0
        if ((node->type == XML_DOCUMENT_NODE) ||
477
0
            (node->type == XML_HTML_DOCUMENT_NODE)) {
478
0
            node = ((xmlDocPtr) node)->children;
479
0
        } else if ((node->children != NULL)
480
0
                   && (node->type != XML_ENTITY_REF_NODE)) {
481
            /* deep first */
482
0
            node = node->children;
483
0
        } else if (node->next != NULL) {
484
            /* then siblings */
485
0
            node = node->next;
486
0
        } else {
487
            /* go up to parents->next if needed */
488
0
            while (node != NULL) {
489
0
                if (node->parent != NULL) {
490
0
                    node = node->parent;
491
0
                }
492
0
                if (node->next != NULL) {
493
0
                    node = node->next;
494
0
                    break;
495
0
                }
496
0
                if (node->parent == NULL) {
497
0
                    node = NULL;
498
0
                    break;
499
0
                }
500
0
            }
501
0
  }
502
0
    }
503
0
    return (0);
504
0
}
505
506
/**
507
 * Implements the XML shell function "dir"
508
 * dumps information about the node (namespace, attributes, content).
509
 *
510
 * @param ctxt  the shell context
511
 * @param arg  unused
512
 * @param node  a node
513
 * @param node2  unused
514
 * @returns 0
515
 */
516
static int
517
xmllintShellDir(xmllintShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
518
            char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
519
            xmlNodePtr node2 ATTRIBUTE_UNUSED)
520
0
{
521
0
    if (!ctxt)
522
0
        return (0);
523
0
    if (node == NULL) {
524
0
  fprintf(ctxt->output, "NULL\n");
525
0
  return (0);
526
0
    }
527
#ifdef LIBXML_DEBUG_ENABLED
528
    if ((node->type == XML_DOCUMENT_NODE) ||
529
        (node->type == XML_HTML_DOCUMENT_NODE)) {
530
        xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node);
531
    } else if (node->type == XML_ATTRIBUTE_NODE) {
532
        xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0);
533
    } else {
534
        xmlDebugDumpOneNode(ctxt->output, node, 0);
535
    }
536
#endif
537
0
    return (0);
538
0
}
539
540
/**
541
 * Implements the XML shell function "dir"
542
 * dumps information about the node (namespace, attributes, content).
543
 *
544
 * @param ctxt  the shell context
545
 * @param value  the content as a string
546
 * @param node  a node
547
 * @param node2  unused
548
 * @returns 0
549
 */
550
static int
551
xmllintShellSetContent(xmllintShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
552
            char *value, xmlNodePtr node,
553
            xmlNodePtr node2 ATTRIBUTE_UNUSED)
554
0
{
555
0
    xmlNodePtr results;
556
0
    xmlParserErrors ret;
557
558
0
    if (!ctxt)
559
0
        return (0);
560
0
    if (node == NULL) {
561
0
  fprintf(ctxt->output, "NULL\n");
562
0
  return (0);
563
0
    }
564
0
    if (value == NULL) {
565
0
        fprintf(ctxt->output, "NULL\n");
566
0
  return (0);
567
0
    }
568
569
0
    ret = xmlParseInNodeContext(node, value, strlen(value), 0, &results);
570
0
    if (ret == XML_ERR_OK) {
571
0
  if (node->children != NULL) {
572
0
      xmlFreeNodeList(node->children);
573
0
      node->children = NULL;
574
0
      node->last = NULL;
575
0
  }
576
0
  xmlAddChildList(node, results);
577
0
    } else {
578
0
        fprintf(ctxt->output, "failed to parse content\n");
579
0
    }
580
0
    return (0);
581
0
}
582
583
#if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_RELAXNG_ENABLED)
584
static void
585
0
xmllintShellPrintf(void *ctx, const char *msg, ...) {
586
0
    xmllintShellCtxtPtr sctxt = ctx;
587
0
    va_list ap;
588
589
0
    va_start(ap, msg);
590
0
    vfprintf(sctxt->output, msg, ap);
591
0
    va_end(ap);
592
0
}
593
#endif /* defined(LIBXML_VALID_ENABLED) || defined(LIBXML_RELAXNG_ENABLED) */
594
595
#ifdef LIBXML_RELAXNG_ENABLED
596
/**
597
 * Implements the XML shell function "relaxng"
598
 * validating the instance against a Relax-NG schemas
599
 *
600
 * @param sctxt  the shell context
601
 * @param schemas  the path to the Relax-NG schemas
602
 * @param node  a node
603
 * @param node2  unused
604
 * @returns 0
605
 */
606
static int
607
xmllintShellRNGValidate(xmllintShellCtxtPtr sctxt, char *schemas,
608
            xmlNodePtr node ATTRIBUTE_UNUSED,
609
      xmlNodePtr node2 ATTRIBUTE_UNUSED)
610
0
{
611
0
    xmlRelaxNGPtr relaxngschemas;
612
0
    xmlRelaxNGParserCtxtPtr ctxt;
613
0
    xmlRelaxNGValidCtxtPtr vctxt;
614
0
    int ret;
615
616
0
    ctxt = xmlRelaxNGNewParserCtxt(schemas);
617
0
    xmlRelaxNGSetParserErrors(ctxt, xmllintShellPrintf, xmllintShellPrintf, sctxt);
618
0
    relaxngschemas = xmlRelaxNGParse(ctxt);
619
0
    xmlRelaxNGFreeParserCtxt(ctxt);
620
0
    if (relaxngschemas == NULL) {
621
0
  fprintf(sctxt->output,
622
0
    "Relax-NG schema %s failed to compile\n", schemas);
623
0
  return(-1);
624
0
    }
625
0
    vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
626
0
    xmlRelaxNGSetValidErrors(vctxt, xmllintShellPrintf, xmllintShellPrintf, sctxt);
627
0
    ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc);
628
0
    if (ret == 0) {
629
0
  fprintf(sctxt->output, "%s validates\n", sctxt->filename);
630
0
    } else if (ret > 0) {
631
0
  fprintf(sctxt->output, "%s fails to validate\n", sctxt->filename);
632
0
    } else {
633
0
  fprintf(sctxt->output, "%s validation generated an internal error\n",
634
0
         sctxt->filename);
635
0
    }
636
0
    xmlRelaxNGFreeValidCtxt(vctxt);
637
0
    if (relaxngschemas != NULL)
638
0
  xmlRelaxNGFree(relaxngschemas);
639
0
    return(0);
640
0
}
641
#endif
642
643
#ifdef LIBXML_OUTPUT_ENABLED
644
/**
645
 * Implements the XML shell function "cat"
646
 * dumps the serialization node content (XML or HTML).
647
 *
648
 * @param ctxt  the shell context
649
 * @param arg  unused
650
 * @param node  a node
651
 * @param node2  unused
652
 * @returns 0
653
 */
654
static int
655
xmllintShellCat(xmllintShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
656
            xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
657
0
{
658
0
    if (!ctxt)
659
0
        return (0);
660
0
    if (node == NULL) {
661
0
  fprintf(ctxt->output, "NULL\n");
662
0
  return (0);
663
0
    }
664
0
    if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
665
0
#ifdef LIBXML_HTML_ENABLED
666
0
        if (node->type == XML_HTML_DOCUMENT_NODE)
667
0
            htmlDocDump(ctxt->output, (htmlDocPtr) node);
668
0
        else
669
0
            htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
670
#else
671
        if (node->type == XML_DOCUMENT_NODE)
672
            xmlDocDump(ctxt->output, (xmlDocPtr) node);
673
        else
674
            xmlElemDump(ctxt->output, ctxt->doc, node);
675
#endif /* LIBXML_HTML_ENABLED */
676
0
    } else {
677
0
        if (node->type == XML_DOCUMENT_NODE)
678
0
            xmlDocDump(ctxt->output, (xmlDocPtr) node);
679
0
        else
680
0
            xmlElemDump(ctxt->output, ctxt->doc, node);
681
0
    }
682
0
    fprintf(ctxt->output, "\n");
683
0
    return (0);
684
0
}
685
#endif /* LIBXML_OUTPUT_ENABLED */
686
687
/**
688
 * Implements the XML shell function "load"
689
 * loads a new document specified by the filename
690
 *
691
 * @param ctxt  the shell context
692
 * @param filename  the file name
693
 * @param node  unused
694
 * @param node2  unused
695
 * @returns 0 or -1 if loading failed
696
 */
697
static int
698
xmllintShellLoad(xmllintShellCtxtPtr ctxt, char *filename,
699
             xmlNodePtr node ATTRIBUTE_UNUSED,
700
             xmlNodePtr node2 ATTRIBUTE_UNUSED)
701
0
{
702
0
    xmlDocPtr doc;
703
0
    int html = 0;
704
705
0
    if ((ctxt == NULL) || (filename == NULL)) return(-1);
706
0
    if (ctxt->doc != NULL)
707
0
        html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
708
709
0
    if (html) {
710
0
#ifdef LIBXML_HTML_ENABLED
711
0
        doc = htmlParseFile(filename, NULL);
712
#else
713
        fprintf(ctxt->output, "HTML support not compiled in\n");
714
        doc = NULL;
715
#endif /* LIBXML_HTML_ENABLED */
716
0
    } else {
717
0
        doc = xmlReadFile(filename,NULL,0);
718
0
    }
719
0
    if (doc != NULL) {
720
0
        if (ctxt->loaded == 1) {
721
0
            xmlFreeDoc(ctxt->doc);
722
0
        }
723
0
        ctxt->loaded = 1;
724
0
#ifdef LIBXML_XPATH_ENABLED
725
0
        xmlXPathFreeContext(ctxt->pctxt);
726
0
#endif /* LIBXML_XPATH_ENABLED */
727
0
        xmlFree(ctxt->filename);
728
0
        ctxt->doc = doc;
729
0
        ctxt->node = (xmlNodePtr) doc;
730
0
#ifdef LIBXML_XPATH_ENABLED
731
0
        ctxt->pctxt = xmlXPathNewContext(doc);
732
0
#endif /* LIBXML_XPATH_ENABLED */
733
0
        ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename);
734
0
    } else
735
0
        return (-1);
736
0
    return (0);
737
0
}
738
739
#ifdef LIBXML_OUTPUT_ENABLED
740
/**
741
 * Implements the XML shell function "write"
742
 * Write the current node to the filename, it saves the serialization
743
 * of the subtree under the `node` specified
744
 *
745
 * @param ctxt  the shell context
746
 * @param filename  the file name
747
 * @param node  a node in the tree
748
 * @param node2  unused
749
 * @returns 0 or -1 in case of error
750
 */
751
static int
752
xmllintShellWrite(xmllintShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
753
              xmlNodePtr node2 ATTRIBUTE_UNUSED)
754
0
{
755
0
    if (node == NULL)
756
0
        return (-1);
757
0
    if ((filename == NULL) || (filename[0] == 0)) {
758
0
        return (-1);
759
0
    }
760
0
    switch (node->type) {
761
0
        case XML_DOCUMENT_NODE:
762
0
            if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
763
0
                fprintf(ctxt->output,
764
0
                                "Failed to write to %s\n", filename);
765
0
                return (-1);
766
0
            }
767
0
            break;
768
0
        case XML_HTML_DOCUMENT_NODE:
769
0
#ifdef LIBXML_HTML_ENABLED
770
0
            if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
771
0
                fprintf(ctxt->output,
772
0
                                "Failed to write to %s\n", filename);
773
0
                return (-1);
774
0
            }
775
#else
776
            if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
777
                fprintf(ctxt->output,
778
                                "Failed to write to %s\n", filename);
779
                return (-1);
780
            }
781
#endif /* LIBXML_HTML_ENABLED */
782
0
            break;
783
0
        default:{
784
0
                FILE *f;
785
786
0
                f = fopen((char *) filename, "wb");
787
0
                if (f == NULL) {
788
0
                    fprintf(ctxt->output,
789
0
                                    "Failed to write to %s\n", filename);
790
0
                    return (-1);
791
0
                }
792
0
                xmlElemDump(f, ctxt->doc, node);
793
0
                fclose(f);
794
0
            }
795
0
    }
796
0
    return (0);
797
0
}
798
799
/**
800
 * Implements the XML shell function "save"
801
 * Write the current document to the filename, or it's original name
802
 *
803
 * @param ctxt  the shell context
804
 * @param filename  the file name (optional)
805
 * @param node  unused
806
 * @param node2  unused
807
 * @returns 0 or -1 in case of error
808
 */
809
static int
810
xmllintShellSave(xmllintShellCtxtPtr ctxt, char *filename,
811
             xmlNodePtr node ATTRIBUTE_UNUSED,
812
             xmlNodePtr node2 ATTRIBUTE_UNUSED)
813
0
{
814
0
    if ((ctxt == NULL) || (ctxt->doc == NULL))
815
0
        return (-1);
816
0
    if ((filename == NULL) || (filename[0] == 0))
817
0
        filename = ctxt->filename;
818
0
    if (filename == NULL)
819
0
        return (-1);
820
0
    switch (ctxt->doc->type) {
821
0
        case XML_DOCUMENT_NODE:
822
0
            if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
823
0
                fprintf(ctxt->output,
824
0
                                "Failed to save to %s\n", filename);
825
0
            }
826
0
            break;
827
0
        case XML_HTML_DOCUMENT_NODE:
828
0
#ifdef LIBXML_HTML_ENABLED
829
0
            if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
830
0
                fprintf(ctxt->output,
831
0
                                "Failed to save to %s\n", filename);
832
0
            }
833
#else
834
            if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
835
                fprintf(ctxt->output,
836
                                "Failed to save to %s\n", filename);
837
            }
838
#endif /* LIBXML_HTML_ENABLED */
839
0
            break;
840
0
        default:
841
0
            fprintf(ctxt->output,
842
0
      "To save to subparts of a document use the 'write' command\n");
843
0
            return (-1);
844
845
0
    }
846
0
    return (0);
847
0
}
848
#endif /* LIBXML_OUTPUT_ENABLED */
849
850
#ifdef LIBXML_VALID_ENABLED
851
/**
852
 * Implements the XML shell function "validate"
853
 * Validate the document, if a DTD path is provided, then the validation
854
 * is done against the given DTD.
855
 *
856
 * @param ctxt  the shell context
857
 * @param dtd  the DTD URI (optional)
858
 * @param node  unused
859
 * @param node2  unused
860
 * @returns 0 or -1 in case of error
861
 */
862
static int
863
xmllintShellValidate(xmllintShellCtxtPtr ctxt, char *dtd,
864
                 xmlNodePtr node ATTRIBUTE_UNUSED,
865
                 xmlNodePtr node2 ATTRIBUTE_UNUSED)
866
0
{
867
0
    xmlValidCtxt vctxt;
868
0
    int res = -1;
869
870
0
    if ((ctxt == NULL) || (ctxt->doc == NULL)) return(-1);
871
0
    memset(&vctxt, 0, sizeof(vctxt));
872
0
    vctxt.error = xmllintShellPrintf;
873
0
    vctxt.warning = xmllintShellPrintf;
874
0
    vctxt.userData = ctxt;
875
876
0
    if ((dtd == NULL) || (dtd[0] == 0)) {
877
0
        res = xmlValidateDocument(&vctxt, ctxt->doc);
878
0
    } else {
879
0
        xmlDtdPtr subset;
880
881
0
        subset = xmlParseDTD(NULL, (xmlChar *) dtd);
882
0
        if (subset != NULL) {
883
0
            res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
884
885
0
            xmlFreeDtd(subset);
886
0
        }
887
0
    }
888
0
    return (res);
889
0
}
890
#endif /* LIBXML_VALID_ENABLED */
891
892
/**
893
 * Implements the XML shell function "du"
894
 * show the structure of the subtree under node `tree`
895
 * If `tree` is null, the command works on the current node.
896
 *
897
 * @param ctxt  the shell context
898
 * @param arg  unused
899
 * @param tree  a node defining a subtree
900
 * @param node2  unused
901
 * @returns 0 or -1 in case of error
902
 */
903
static int
904
xmllintShellDu(xmllintShellCtxtPtr ctxt,
905
           char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
906
           xmlNodePtr node2 ATTRIBUTE_UNUSED)
907
0
{
908
0
    xmlNodePtr node;
909
0
    int indent = 0, i;
910
911
0
    if (!ctxt)
912
0
  return (-1);
913
914
0
    if (tree == NULL)
915
0
        return (-1);
916
0
    node = tree;
917
0
    while (node != NULL) {
918
0
        if ((node->type == XML_DOCUMENT_NODE) ||
919
0
            (node->type == XML_HTML_DOCUMENT_NODE)) {
920
0
            fprintf(ctxt->output, "/\n");
921
0
        } else if (node->type == XML_ELEMENT_NODE) {
922
0
            for (i = 0; i < indent; i++)
923
0
                fprintf(ctxt->output, "  ");
924
0
            if ((node->ns) && (node->ns->prefix))
925
0
                fprintf(ctxt->output, "%s:", node->ns->prefix);
926
0
            fprintf(ctxt->output, "%s\n", node->name);
927
0
        } else {
928
0
        }
929
930
        /*
931
         * Browse the full subtree, deep first
932
         */
933
934
0
        if ((node->type == XML_DOCUMENT_NODE) ||
935
0
            (node->type == XML_HTML_DOCUMENT_NODE)) {
936
0
            node = ((xmlDocPtr) node)->children;
937
0
        } else if ((node->children != NULL)
938
0
                   && (node->type != XML_ENTITY_REF_NODE)) {
939
            /* deep first */
940
0
            node = node->children;
941
0
            indent++;
942
0
        } else if ((node != tree) && (node->next != NULL)) {
943
            /* then siblings */
944
0
            node = node->next;
945
0
        } else if (node != tree) {
946
            /* go up to parents->next if needed */
947
0
            while (node != tree) {
948
0
                if (node->parent != NULL) {
949
0
                    node = node->parent;
950
0
                    indent--;
951
0
                }
952
0
                if ((node != tree) && (node->next != NULL)) {
953
0
                    node = node->next;
954
0
                    break;
955
0
                }
956
0
                if (node->parent == NULL) {
957
0
                    node = NULL;
958
0
                    break;
959
0
                }
960
0
                if (node == tree) {
961
0
                    node = NULL;
962
0
                    break;
963
0
                }
964
0
            }
965
            /* exit condition */
966
0
            if (node == tree)
967
0
                node = NULL;
968
0
        } else
969
0
            node = NULL;
970
0
    }
971
0
    return (0);
972
0
}
973
974
/**
975
 * Implements the XML shell function "pwd"
976
 * Show the full path from the root to the node, if needed building
977
 * thumblers when similar elements exists at a given ancestor level.
978
 * The output is compatible with XPath commands.
979
 *
980
 * @param ctxt  the shell context
981
 * @param buffer  the output buffer
982
 * @param node  a node
983
 * @param node2  unused
984
 * @returns 0 or -1 in case of error
985
 */
986
static int
987
xmllintShellPwd(xmllintShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
988
            xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
989
0
{
990
0
    xmlChar *path;
991
992
0
    if ((node == NULL) || (buffer == NULL))
993
0
        return (-1);
994
995
0
    path = xmlGetNodePath(node);
996
0
    if (path == NULL)
997
0
  return (-1);
998
999
    /*
1000
     * This test prevents buffer overflow, because this routine
1001
     * is only called by xmllintShell, in which the second argument is
1002
     * 500 chars long.
1003
     * It is a dirty hack before a cleaner solution is found.
1004
     * Documentation should mention that the second argument must
1005
     * be at least 500 chars long, and could be stripped if too long.
1006
     */
1007
0
    snprintf(buffer, 499, "%s", path);
1008
0
    buffer[499] = '0';
1009
0
    xmlFree(path);
1010
1011
0
    return (0);
1012
0
}
1013
1014
/**
1015
 * Read a string
1016
 *
1017
 * @param prompt  the prompt value
1018
 * @returns a pointer to it or NULL on EOF the caller is expected to
1019
 *     free the returned string.
1020
 */
1021
static char *
1022
0
xmllintShellReadline(char *prompt) {
1023
0
    char buf[501];
1024
0
    char *ret;
1025
0
    int len;
1026
1027
#ifdef HAVE_LIBREADLINE
1028
    if (isatty(STDIN_FILENO)) {
1029
        char *line_read;
1030
1031
        /* Get a line from the user. */
1032
        line_read = readline (prompt);
1033
1034
#ifdef HAVE_LIBHISTORY
1035
        /* If the line has any text in it, save it on the history. */
1036
        if (line_read && *line_read)
1037
           add_history (line_read);
1038
#endif
1039
1040
        return (line_read);
1041
    }
1042
#endif
1043
1044
0
    if (prompt != NULL)
1045
0
       fprintf(stdout, "%s", prompt);
1046
0
    fflush(stdout);
1047
0
    if (!fgets(buf, 500, stdin))
1048
0
        return(NULL);
1049
0
    buf[500] = 0;
1050
0
    len = strlen(buf);
1051
0
    ret = (char *) malloc(len + 1);
1052
0
    if (ret != NULL) {
1053
0
       memcpy (ret, buf, len + 1);
1054
0
    }
1055
0
    return(ret);
1056
0
}
1057
1058
/**
1059
 * Implements the XML shell
1060
 * This allow to load, validate, view, modify and save a document
1061
 * using a environment similar to a UNIX commandline.
1062
 *
1063
 * @param doc  the initial document
1064
 * @param filename  the output buffer
1065
 * @param output  the output FILE*, defaults to stdout if NULL
1066
 */
1067
void
1068
xmllintShell(xmlDoc *doc, const char *filename, FILE * output)
1069
0
{
1070
0
    char prompt[500] = "/ > ";
1071
0
    char *cmdline = NULL, *cur;
1072
0
    char command[100];
1073
0
    char arg[400];
1074
0
    int i;
1075
0
    xmllintShellCtxtPtr ctxt;
1076
0
#ifdef LIBXML_XPATH_ENABLED
1077
0
    xmlXPathObjectPtr list;
1078
0
#endif
1079
1080
0
    if (doc == NULL)
1081
0
        return;
1082
0
    if (filename == NULL)
1083
0
        return;
1084
0
    if (output == NULL)
1085
0
        output = stdout;
1086
0
    ctxt = (xmllintShellCtxtPtr) xmlMalloc(sizeof(xmllintShellCtxt));
1087
0
    if (ctxt == NULL)
1088
0
        return;
1089
0
    ctxt->loaded = 0;
1090
0
    ctxt->doc = doc;
1091
0
    ctxt->output = output;
1092
0
    ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
1093
0
    ctxt->node = (xmlNodePtr) ctxt->doc;
1094
1095
0
#ifdef LIBXML_XPATH_ENABLED
1096
0
    ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
1097
0
    if (ctxt->pctxt == NULL) {
1098
0
        xmlFree(ctxt);
1099
0
        return;
1100
0
    }
1101
0
#endif /* LIBXML_XPATH_ENABLED */
1102
0
    while (1) {
1103
0
        if (ctxt->node == (xmlNodePtr) ctxt->doc)
1104
0
            snprintf(prompt, sizeof(prompt), "%s > ", "/");
1105
0
        else if ((ctxt->node != NULL) && (ctxt->node->name) &&
1106
0
                 (ctxt->node->ns) && (ctxt->node->ns->prefix))
1107
0
            snprintf(prompt, sizeof(prompt), "%s:%s > ",
1108
0
                     (ctxt->node->ns->prefix), ctxt->node->name);
1109
0
        else if ((ctxt->node != NULL) && (ctxt->node->name))
1110
0
            snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
1111
0
        else
1112
0
            snprintf(prompt, sizeof(prompt), "? > ");
1113
0
        prompt[sizeof(prompt) - 1] = 0;
1114
1115
        /*
1116
         * Get a new command line
1117
         */
1118
0
        cmdline = xmllintShellReadline(prompt);
1119
0
        if (cmdline == NULL)
1120
0
            break;
1121
1122
        /*
1123
         * Parse the command itself
1124
         */
1125
0
        cur = cmdline;
1126
0
        while ((*cur == ' ') || (*cur == '\t'))
1127
0
            cur++;
1128
0
        i = 0;
1129
0
        while ((*cur != ' ') && (*cur != '\t') &&
1130
0
               (*cur != '\n') && (*cur != '\r')) {
1131
0
            if (*cur == 0)
1132
0
                break;
1133
0
            command[i++] = *cur++;
1134
0
        }
1135
0
        command[i] = 0;
1136
0
        if (i == 0)
1137
0
            continue;
1138
1139
        /*
1140
         * Parse the argument
1141
         */
1142
0
        while ((*cur == ' ') || (*cur == '\t'))
1143
0
            cur++;
1144
0
        i = 0;
1145
0
        while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
1146
0
            if (*cur == 0)
1147
0
                break;
1148
0
            arg[i++] = *cur++;
1149
0
        }
1150
0
        arg[i] = 0;
1151
1152
        /*
1153
         * start interpreting the command
1154
         */
1155
0
        if (!strcmp(command, "exit"))
1156
0
            break;
1157
0
        if (!strcmp(command, "quit"))
1158
0
            break;
1159
0
        if (!strcmp(command, "bye"))
1160
0
            break;
1161
0
    if (!strcmp(command, "help")) {
1162
0
      fprintf(ctxt->output, "\tbase         display XML base of the node\n");
1163
0
      fprintf(ctxt->output, "\tsetbase URI  change the XML base of the node\n");
1164
0
      fprintf(ctxt->output, "\tbye          leave shell\n");
1165
0
      fprintf(ctxt->output, "\tcat [node]   display node or current node\n");
1166
0
      fprintf(ctxt->output, "\tcd [path]    change directory to path or to root\n");
1167
0
      fprintf(ctxt->output, "\tdir [path]   dumps information about the node (namespace, attributes, content)\n");
1168
0
      fprintf(ctxt->output, "\tdu [path]    show the structure of the subtree under path or the current node\n");
1169
0
      fprintf(ctxt->output, "\texit         leave shell\n");
1170
0
      fprintf(ctxt->output, "\thelp         display this help\n");
1171
0
      fprintf(ctxt->output, "\tfree         display memory usage\n");
1172
0
      fprintf(ctxt->output, "\tload [name]  load a new document with name\n");
1173
0
      fprintf(ctxt->output, "\tls [path]    list contents of path or the current directory\n");
1174
0
      fprintf(ctxt->output, "\tset xml_fragment replace the current node content with the fragment parsed in context\n");
1175
0
#ifdef LIBXML_XPATH_ENABLED
1176
0
      fprintf(ctxt->output, "\txpath expr   evaluate the XPath expression in that context and print the result\n");
1177
0
      fprintf(ctxt->output, "\tsetns nsreg  register a namespace to a prefix in the XPath evaluation context\n");
1178
0
      fprintf(ctxt->output, "\t             format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)\n");
1179
0
      fprintf(ctxt->output, "\tsetrootns    register all namespace found on the root element\n");
1180
0
      fprintf(ctxt->output, "\t             the default namespace if any uses 'defaultns' prefix\n");
1181
0
#endif /* LIBXML_XPATH_ENABLED */
1182
0
      fprintf(ctxt->output, "\tpwd          display current working directory\n");
1183
0
      fprintf(ctxt->output, "\twhereis      display absolute path of [path] or current working directory\n");
1184
0
      fprintf(ctxt->output, "\tquit         leave shell\n");
1185
0
#ifdef LIBXML_OUTPUT_ENABLED
1186
0
      fprintf(ctxt->output, "\tsave [name]  save this document to name or the original name\n");
1187
0
      fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
1188
0
#endif /* LIBXML_OUTPUT_ENABLED */
1189
0
#ifdef LIBXML_VALID_ENABLED
1190
0
      fprintf(ctxt->output, "\tvalidate     check the document for errors\n");
1191
0
#endif /* LIBXML_VALID_ENABLED */
1192
0
#ifdef LIBXML_RELAXNG_ENABLED
1193
0
      fprintf(ctxt->output, "\trelaxng rng  validate the document against the Relax-NG schemas\n");
1194
0
#endif
1195
0
      fprintf(ctxt->output, "\tgrep string  search for a string in the subtree\n");
1196
0
#ifdef LIBXML_VALID_ENABLED
1197
0
        } else if (!strcmp(command, "validate")) {
1198
0
            xmllintShellValidate(ctxt, arg, NULL, NULL);
1199
0
#endif /* LIBXML_VALID_ENABLED */
1200
0
        } else if (!strcmp(command, "load")) {
1201
0
            xmllintShellLoad(ctxt, arg, NULL, NULL);
1202
0
#ifdef LIBXML_RELAXNG_ENABLED
1203
0
        } else if (!strcmp(command, "relaxng")) {
1204
0
            xmllintShellRNGValidate(ctxt, arg, NULL, NULL);
1205
0
#endif
1206
0
#ifdef LIBXML_OUTPUT_ENABLED
1207
0
        } else if (!strcmp(command, "save")) {
1208
0
            xmllintShellSave(ctxt, arg, NULL, NULL);
1209
0
        } else if (!strcmp(command, "write")) {
1210
0
      if (arg[0] == 0)
1211
0
    fprintf(ctxt->output,
1212
0
                        "Write command requires a filename argument\n");
1213
0
      else
1214
0
    xmllintShellWrite(ctxt, arg, ctxt->node, NULL);
1215
0
#endif /* LIBXML_OUTPUT_ENABLED */
1216
0
        } else if (!strcmp(command, "grep")) {
1217
0
            xmllintShellGrep(ctxt, arg, ctxt->node, NULL);
1218
0
        } else if (!strcmp(command, "pwd")) {
1219
0
            char dir[500];
1220
1221
0
            if (!xmllintShellPwd(ctxt, dir, ctxt->node, NULL))
1222
0
                fprintf(ctxt->output, "%s\n", dir);
1223
0
        } else if (!strcmp(command, "du")) {
1224
0
            if (arg[0] == 0) {
1225
0
                xmllintShellDu(ctxt, NULL, ctxt->node, NULL);
1226
0
            } else {
1227
0
#ifdef LIBXML_XPATH_ENABLED
1228
0
                ctxt->pctxt->node = ctxt->node;
1229
0
                list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
1230
0
                if (list != NULL) {
1231
0
                    switch (list->type) {
1232
0
                        case XPATH_UNDEFINED:
1233
0
                            fprintf(ctxt->output,
1234
0
                                            "%s: no such node\n", arg);
1235
0
                            break;
1236
0
                        case XPATH_NODESET:{
1237
0
                            int indx;
1238
1239
0
                            if (list->nodesetval == NULL)
1240
0
                                break;
1241
1242
0
                            for (indx = 0;
1243
0
                                 indx < list->nodesetval->nodeNr;
1244
0
                                 indx++)
1245
0
                                xmllintShellDu(ctxt, NULL,
1246
0
                                           list->nodesetval->
1247
0
                                           nodeTab[indx], NULL);
1248
0
                            break;
1249
0
                        }
1250
0
                        case XPATH_BOOLEAN:
1251
0
                            fprintf(ctxt->output,
1252
0
                                            "%s is a Boolean\n", arg);
1253
0
                            break;
1254
0
                        case XPATH_NUMBER:
1255
0
                            fprintf(ctxt->output,
1256
0
                                            "%s is a number\n", arg);
1257
0
                            break;
1258
0
                        case XPATH_STRING:
1259
0
                            fprintf(ctxt->output,
1260
0
                                            "%s is a string\n", arg);
1261
0
                            break;
1262
0
                        case XPATH_USERS:
1263
0
                            fprintf(ctxt->output,
1264
0
                                            "%s is user-defined\n", arg);
1265
0
                            break;
1266
0
                        case XPATH_XSLT_TREE:
1267
0
                            fprintf(ctxt->output,
1268
0
                                            "%s is an XSLT value tree\n",
1269
0
                                            arg);
1270
0
                            break;
1271
0
                    }
1272
0
                    xmlXPathFreeObject(list);
1273
0
                } else {
1274
0
                    fprintf(ctxt->output,
1275
0
                                    "%s: no such node\n", arg);
1276
0
                }
1277
0
                ctxt->pctxt->node = NULL;
1278
0
#endif /* LIBXML_XPATH_ENABLED */
1279
0
            }
1280
0
        } else if (!strcmp(command, "base")) {
1281
0
            xmllintShellBase(ctxt, NULL, ctxt->node, NULL);
1282
0
        } else if (!strcmp(command, "set")) {
1283
0
      xmllintShellSetContent(ctxt, arg, ctxt->node, NULL);
1284
0
#ifdef LIBXML_XPATH_ENABLED
1285
0
        } else if (!strcmp(command, "setns")) {
1286
0
            if (arg[0] == 0) {
1287
0
    fprintf(ctxt->output,
1288
0
        "setns: prefix=[nsuri] required\n");
1289
0
            } else {
1290
0
                xmllintShellRegisterNamespace(ctxt, arg, NULL, NULL);
1291
0
            }
1292
0
        } else if (!strcmp(command, "setrootns")) {
1293
0
      xmlNodePtr root;
1294
1295
0
      root = xmlDocGetRootElement(ctxt->doc);
1296
0
      xmllintShellRegisterRootNamespaces(ctxt, NULL, root, NULL);
1297
#ifdef LIBXML_DEBUG_ENABLED
1298
        } else if (!strcmp(command, "xpath")) {
1299
            if (arg[0] == 0) {
1300
    fprintf(ctxt->output,
1301
        "xpath: expression required\n");
1302
      } else {
1303
                ctxt->pctxt->node = ctxt->node;
1304
                list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
1305
    xmlXPathDebugDumpObject(ctxt->output, list, 0);
1306
    xmlXPathFreeObject(list);
1307
      }
1308
#endif /* LIBXML_DEBUG_ENABLED */
1309
0
#endif /* LIBXML_XPATH_ENABLED */
1310
0
        } else if (!strcmp(command, "setbase")) {
1311
0
            xmllintShellSetBase(ctxt, arg, ctxt->node, NULL);
1312
0
        } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
1313
0
            int dir = (!strcmp(command, "dir"));
1314
1315
0
            if (arg[0] == 0) {
1316
0
                if (dir)
1317
0
                    xmllintShellDir(ctxt, NULL, ctxt->node, NULL);
1318
0
                else
1319
0
                    xmllintShellList(ctxt, NULL, ctxt->node, NULL);
1320
0
            } else {
1321
0
#ifdef LIBXML_XPATH_ENABLED
1322
0
                ctxt->pctxt->node = ctxt->node;
1323
0
                list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
1324
0
                if (list != NULL) {
1325
0
                    switch (list->type) {
1326
0
                        case XPATH_UNDEFINED:
1327
0
                            fprintf(ctxt->output,
1328
0
                                            "%s: no such node\n", arg);
1329
0
                            break;
1330
0
                        case XPATH_NODESET:{
1331
0
                                int indx;
1332
1333
0
        if (list->nodesetval == NULL)
1334
0
            break;
1335
1336
0
                                for (indx = 0;
1337
0
                                     indx < list->nodesetval->nodeNr;
1338
0
                                     indx++) {
1339
0
                                    if (dir)
1340
0
                                        xmllintShellDir(ctxt, NULL,
1341
0
                                                    list->nodesetval->
1342
0
                                                    nodeTab[indx], NULL);
1343
0
                                    else
1344
0
                                        xmllintShellList(ctxt, NULL,
1345
0
                                                     list->nodesetval->
1346
0
                                                     nodeTab[indx], NULL);
1347
0
                                }
1348
0
                                break;
1349
0
                            }
1350
0
                        case XPATH_BOOLEAN:
1351
0
                            fprintf(ctxt->output,
1352
0
                                            "%s is a Boolean\n", arg);
1353
0
                            break;
1354
0
                        case XPATH_NUMBER:
1355
0
                            fprintf(ctxt->output,
1356
0
                                            "%s is a number\n", arg);
1357
0
                            break;
1358
0
                        case XPATH_STRING:
1359
0
                            fprintf(ctxt->output,
1360
0
                                            "%s is a string\n", arg);
1361
0
                            break;
1362
0
                        case XPATH_USERS:
1363
0
                            fprintf(ctxt->output,
1364
0
                                            "%s is user-defined\n", arg);
1365
0
                            break;
1366
0
                        case XPATH_XSLT_TREE:
1367
0
                            fprintf(ctxt->output,
1368
0
                                            "%s is an XSLT value tree\n",
1369
0
                                            arg);
1370
0
                            break;
1371
0
                    }
1372
0
                    xmlXPathFreeObject(list);
1373
0
                } else {
1374
0
                    fprintf(ctxt->output,
1375
0
                                    "%s: no such node\n", arg);
1376
0
                }
1377
0
                ctxt->pctxt->node = NULL;
1378
0
#endif /* LIBXML_XPATH_ENABLED */
1379
0
            }
1380
0
        } else if (!strcmp(command, "whereis")) {
1381
0
            char dir[500];
1382
1383
0
            if (arg[0] == 0) {
1384
0
                if (!xmllintShellPwd(ctxt, dir, ctxt->node, NULL))
1385
0
                    fprintf(ctxt->output, "%s\n", dir);
1386
0
            } else {
1387
0
#ifdef LIBXML_XPATH_ENABLED
1388
0
                ctxt->pctxt->node = ctxt->node;
1389
0
                list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
1390
0
                if (list != NULL) {
1391
0
                    switch (list->type) {
1392
0
                        case XPATH_UNDEFINED:
1393
0
                            fprintf(ctxt->output,
1394
0
                                            "%s: no such node\n", arg);
1395
0
                            break;
1396
0
                        case XPATH_NODESET:{
1397
0
                                int indx;
1398
1399
0
        if (list->nodesetval == NULL)
1400
0
            break;
1401
1402
0
                                for (indx = 0;
1403
0
                                     indx < list->nodesetval->nodeNr;
1404
0
                                     indx++) {
1405
0
                                    if (!xmllintShellPwd(ctxt, dir, list->nodesetval->
1406
0
                                                     nodeTab[indx], NULL))
1407
0
                                        fprintf(ctxt->output, "%s\n", dir);
1408
0
                                }
1409
0
                                break;
1410
0
                            }
1411
0
                        case XPATH_BOOLEAN:
1412
0
                            fprintf(ctxt->output,
1413
0
                                            "%s is a Boolean\n", arg);
1414
0
                            break;
1415
0
                        case XPATH_NUMBER:
1416
0
                            fprintf(ctxt->output,
1417
0
                                            "%s is a number\n", arg);
1418
0
                            break;
1419
0
                        case XPATH_STRING:
1420
0
                            fprintf(ctxt->output,
1421
0
                                            "%s is a string\n", arg);
1422
0
                            break;
1423
0
                        case XPATH_USERS:
1424
0
                            fprintf(ctxt->output,
1425
0
                                            "%s is user-defined\n", arg);
1426
0
                            break;
1427
0
                        case XPATH_XSLT_TREE:
1428
0
                            fprintf(ctxt->output,
1429
0
                                            "%s is an XSLT value tree\n",
1430
0
                                            arg);
1431
0
                            break;
1432
0
                    }
1433
0
                    xmlXPathFreeObject(list);
1434
0
                } else {
1435
0
                    fprintf(ctxt->output,
1436
0
                                    "%s: no such node\n", arg);
1437
0
                }
1438
0
                ctxt->pctxt->node = NULL;
1439
0
#endif /* LIBXML_XPATH_ENABLED */
1440
0
            }
1441
0
        } else if (!strcmp(command, "cd")) {
1442
0
            if (arg[0] == 0) {
1443
0
                ctxt->node = (xmlNodePtr) ctxt->doc;
1444
0
            } else {
1445
0
#ifdef LIBXML_XPATH_ENABLED
1446
0
                int l;
1447
1448
0
                ctxt->pctxt->node = ctxt->node;
1449
0
    l = strlen(arg);
1450
0
    if ((l >= 2) && (arg[l - 1] == '/'))
1451
0
        arg[l - 1] = 0;
1452
0
                list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
1453
0
                if (list != NULL) {
1454
0
                    switch (list->type) {
1455
0
                        case XPATH_UNDEFINED:
1456
0
                            fprintf(ctxt->output,
1457
0
                                            "%s: no such node\n", arg);
1458
0
                            break;
1459
0
                        case XPATH_NODESET:
1460
0
                            if (list->nodesetval != NULL) {
1461
0
        if (list->nodesetval->nodeNr == 1) {
1462
0
            ctxt->node = list->nodesetval->nodeTab[0];
1463
0
            if ((ctxt->node != NULL) &&
1464
0
                (ctxt->node->type ==
1465
0
           XML_NAMESPACE_DECL)) {
1466
0
          fprintf(ctxt->output,
1467
0
                "cannot cd to namespace\n");
1468
0
          ctxt->node = NULL;
1469
0
            }
1470
0
        } else
1471
0
            fprintf(ctxt->output,
1472
0
                "%s is a %d Node Set\n",
1473
0
                arg,
1474
0
                list->nodesetval->nodeNr);
1475
0
                            } else
1476
0
                                fprintf(ctxt->output,
1477
0
                                                "%s is an empty Node Set\n",
1478
0
                                                arg);
1479
0
                            break;
1480
0
                        case XPATH_BOOLEAN:
1481
0
                            fprintf(ctxt->output,
1482
0
                                            "%s is a Boolean\n", arg);
1483
0
                            break;
1484
0
                        case XPATH_NUMBER:
1485
0
                            fprintf(ctxt->output,
1486
0
                                            "%s is a number\n", arg);
1487
0
                            break;
1488
0
                        case XPATH_STRING:
1489
0
                            fprintf(ctxt->output,
1490
0
                                            "%s is a string\n", arg);
1491
0
                            break;
1492
0
                        case XPATH_USERS:
1493
0
                            fprintf(ctxt->output,
1494
0
                                            "%s is user-defined\n", arg);
1495
0
                            break;
1496
0
                        case XPATH_XSLT_TREE:
1497
0
                            fprintf(ctxt->output,
1498
0
                                            "%s is an XSLT value tree\n",
1499
0
                                            arg);
1500
0
                            break;
1501
0
                    }
1502
0
                    xmlXPathFreeObject(list);
1503
0
                } else {
1504
0
                    fprintf(ctxt->output,
1505
0
                                    "%s: no such node\n", arg);
1506
0
                }
1507
0
                ctxt->pctxt->node = NULL;
1508
0
#endif /* LIBXML_XPATH_ENABLED */
1509
0
            }
1510
0
#ifdef LIBXML_OUTPUT_ENABLED
1511
0
        } else if (!strcmp(command, "cat")) {
1512
0
            if (arg[0] == 0) {
1513
0
                xmllintShellCat(ctxt, NULL, ctxt->node, NULL);
1514
0
            } else {
1515
0
#ifdef LIBXML_XPATH_ENABLED
1516
0
                ctxt->pctxt->node = ctxt->node;
1517
0
                list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
1518
0
                if (list != NULL) {
1519
0
                    switch (list->type) {
1520
0
                        case XPATH_UNDEFINED:
1521
0
                            fprintf(ctxt->output,
1522
0
                                            "%s: no such node\n", arg);
1523
0
                            break;
1524
0
                        case XPATH_NODESET:{
1525
0
                                int indx;
1526
1527
0
        if (list->nodesetval == NULL)
1528
0
            break;
1529
1530
0
                                for (indx = 0;
1531
0
                                     indx < list->nodesetval->nodeNr;
1532
0
                                     indx++) {
1533
0
                                    if (i > 0)
1534
0
                                        fprintf(ctxt->output, " -------\n");
1535
0
                                    xmllintShellCat(ctxt, NULL,
1536
0
                                                list->nodesetval->
1537
0
                                                nodeTab[indx], NULL);
1538
0
                                }
1539
0
                                break;
1540
0
                            }
1541
0
                        case XPATH_BOOLEAN:
1542
0
                            fprintf(ctxt->output,
1543
0
                                            "%s is a Boolean\n", arg);
1544
0
                            break;
1545
0
                        case XPATH_NUMBER:
1546
0
                            fprintf(ctxt->output,
1547
0
                                            "%s is a number\n", arg);
1548
0
                            break;
1549
0
                        case XPATH_STRING:
1550
0
                            fprintf(ctxt->output,
1551
0
                                            "%s is a string\n", arg);
1552
0
                            break;
1553
0
                        case XPATH_USERS:
1554
0
                            fprintf(ctxt->output,
1555
0
                                            "%s is user-defined\n", arg);
1556
0
                            break;
1557
0
                        case XPATH_XSLT_TREE:
1558
0
                            fprintf(ctxt->output,
1559
0
                                            "%s is an XSLT value tree\n",
1560
0
                                            arg);
1561
0
                            break;
1562
0
                    }
1563
0
                    xmlXPathFreeObject(list);
1564
0
                } else {
1565
0
                    fprintf(ctxt->output,
1566
0
                                    "%s: no such node\n", arg);
1567
0
                }
1568
0
                ctxt->pctxt->node = NULL;
1569
0
#endif /* LIBXML_XPATH_ENABLED */
1570
0
            }
1571
0
#endif /* LIBXML_OUTPUT_ENABLED */
1572
0
        } else {
1573
0
            fprintf(ctxt->output,
1574
0
                            "Unknown command %s\n", command);
1575
0
        }
1576
0
        free(cmdline);          /* not xmlFree here ! */
1577
0
  cmdline = NULL;
1578
0
    }
1579
0
#ifdef LIBXML_XPATH_ENABLED
1580
0
    xmlXPathFreeContext(ctxt->pctxt);
1581
0
#endif /* LIBXML_XPATH_ENABLED */
1582
0
    if (ctxt->loaded) {
1583
0
        xmlFreeDoc(ctxt->doc);
1584
0
    }
1585
0
    if (ctxt->filename != NULL)
1586
0
        xmlFree(ctxt->filename);
1587
0
    xmlFree(ctxt);
1588
0
    if (cmdline != NULL)
1589
0
        free(cmdline);          /* not xmlFree here ! */
1590
0
}