Imported Upstream version 2.9.5_rc1
[platform/upstream/libxml2.git] / xmllint.c
index 40a2194..c175730 100644 (file)
--- a/xmllint.c
+++ b/xmllint.c
@@ -168,8 +168,9 @@ static int nodefdtd = 0;
 #endif
 #ifdef LIBXML_PUSH_ENABLED
 static int push = 0;
+static int pushsize = 4096;
 #endif /* LIBXML_PUSH_ENABLED */
-#ifdef HAVE_SYS_MMAN_H
+#ifdef HAVE_MMAP
 static int memory = 0;
 #endif
 static int testIO = 0;
@@ -209,7 +210,7 @@ static xmlStreamCtxtPtr patstream = NULL;
 #ifdef LIBXML_XPATH_ENABLED
 static const char *xpathquery = NULL;
 #endif
-static int options = XML_PARSE_COMPACT;
+static int options = XML_PARSE_COMPACT | XML_PARSE_BIG_LINES;
 static int sax = 0;
 static int oldxml10 = 0;
 
@@ -448,7 +449,7 @@ startTimer(void)
  *           message about the timing performed; format is a printf
  *           type argument
  */
-static void XMLCDECL
+static void XMLCDECL LIBXML_ATTR_FORMAT(1,2)
 endTimer(const char *fmt, ...)
 {
     long msec;
@@ -484,7 +485,7 @@ startTimer(void)
 {
     begin = clock();
 }
-static void XMLCDECL
+static void XMLCDECL LIBXML_ATTR_FORMAT(1,2)
 endTimer(const char *fmt, ...)
 {
     long msec;
@@ -513,7 +514,7 @@ startTimer(void)
      * Do nothing
      */
 }
-static void XMLCDECL
+static void XMLCDECL LIBXML_ATTR_FORMAT(1,2)
 endTimer(char *format, ...)
 {
     /*
@@ -633,7 +634,7 @@ xmlHTMLPrintFileContext(xmlParserInputPtr input) {
  * Display and format an error messages, gives file, line, position and
  * extra parameters.
  */
-static void XMLCDECL
+static void XMLCDECL LIBXML_ATTR_FORMAT(2,3)
 xmlHTMLError(void *ctx, const char *msg, ...)
 {
     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
@@ -670,7 +671,7 @@ xmlHTMLError(void *ctx, const char *msg, ...)
  * Display and format a warning messages, gives file, line, position and
  * extra parameters.
  */
-static void XMLCDECL
+static void XMLCDECL LIBXML_ATTR_FORMAT(2,3)
 xmlHTMLWarning(void *ctx, const char *msg, ...)
 {
     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
@@ -708,7 +709,7 @@ xmlHTMLWarning(void *ctx, const char *msg, ...)
  * Display and format an validity error messages, gives file,
  * line, position and extra parameters.
  */
-static void XMLCDECL
+static void XMLCDECL LIBXML_ATTR_FORMAT(2,3)
 xmlHTMLValidityError(void *ctx, const char *msg, ...)
 {
     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
@@ -745,7 +746,7 @@ xmlHTMLValidityError(void *ctx, const char *msg, ...)
  * Display and format a validity warning messages, gives file, line,
  * position and extra parameters.
  */
-static void XMLCDECL
+static void XMLCDECL LIBXML_ATTR_FORMAT(2,3)
 xmlHTMLValidityWarning(void *ctx, const char *msg, ...)
 {
     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
@@ -808,6 +809,7 @@ xmlShellReadline(char *prompt) {
 
     if (prompt != NULL)
        fprintf(stdout, "%s", prompt);
+    fflush(stdout);
     if (!fgets(line_read, 500, stdin))
         return(NULL);
     line_read[500] = 0;
@@ -1409,7 +1411,7 @@ commentDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value)
  * Display and format a warning messages, gives file, line, position and
  * extra parameters.
  */
-static void XMLCDECL
+static void XMLCDECL LIBXML_ATTR_FORMAT(2,3)
 warningDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
 {
     va_list args;
@@ -1432,7 +1434,7 @@ warningDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
  * Display and format a error messages, gives file, line, position and
  * extra parameters.
  */
-static void XMLCDECL
+static void XMLCDECL LIBXML_ATTR_FORMAT(2,3)
 errorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
 {
     va_list args;
@@ -1455,7 +1457,7 @@ errorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
  * Display and format a fatalError messages, gives file, line, position and
  * extra parameters.
  */
-static void XMLCDECL
+static void XMLCDECL LIBXML_ATTR_FORMAT(2,3)
 fatalErrorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
 {
     va_list args;
@@ -1669,6 +1671,7 @@ testSAX(const char *filename) {
                (xmlSchemaValidityErrorFunc) fprintf,
                (xmlSchemaValidityWarningFunc) fprintf,
                stderr);
+       xmlSchemaValidateSetFilename(vctxt, filename);
 
        ret = xmlSchemaValidateStream(vctxt, buf, 0, handler,
                                      (void *)user_data);
@@ -1823,7 +1826,7 @@ static void processNode(xmlTextReaderPtr reader) {
 static void streamFile(char *filename) {
     xmlTextReaderPtr reader;
     int ret;
-#ifdef HAVE_SYS_MMAN_H
+#ifdef HAVE_MMAP
     int fd = -1;
     struct stat info;
     const char *base = NULL;
@@ -1835,8 +1838,12 @@ static void streamFile(char *filename) {
        if ((fd = open(filename, O_RDONLY)) < 0)
            return;
        base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
-       if (base == (void *) MAP_FAILED)
+       if (base == (void *) MAP_FAILED) {
+           close(fd);
+           fprintf(stderr, "mmap failure for file %s\n", filename);
+           progresult = XMLLINT_ERR_RDFILE;
            return;
+       }
 
        reader = xmlReaderForMemory(base, info.st_size, filename,
                                    NULL, options);
@@ -1873,7 +1880,8 @@ static void streamFile(char *filename) {
            xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
        else
 #endif /* LIBXML_VALID_ENABLED */
-           xmlTextReaderSetParserProp(reader, XML_PARSER_LOADDTD, 1);
+           if (loaddtd)
+               xmlTextReaderSetParserProp(reader, XML_PARSER_LOADDTD, 1);
 #ifdef LIBXML_SCHEMAS_ENABLED
        if (relaxng != NULL) {
            if ((timing) && (!repeat)) {
@@ -1974,7 +1982,7 @@ static void streamFile(char *filename) {
        patstream = NULL;
     }
 #endif
-#ifdef HAVE_SYS_MMAN_H
+#ifdef HAVE_MMAP
     if (memory) {
         xmlFreeParserInputBuffer(input);
        munmap((char *) base, info.st_size);
@@ -1994,6 +2002,12 @@ static void walkDoc(xmlDocPtr doc) {
     xmlNsPtr ns;
 
     root = xmlDocGetRootElement(doc);
+    if (root == NULL ) {
+        xmlGenericError(xmlGenericErrorContext,
+                "Document does not have a root element");
+        progresult = XMLLINT_ERR_UNCLASS;
+        return;
+    }
     for (ns = root->nsDef, i = 0;ns != NULL && i < 20;ns=ns->next) {
         namespaces[i++] = ns->href;
         namespaces[i++] = ns->prefix;
@@ -2139,7 +2153,7 @@ static void doXPathQuery(xmlDocPtr doc, const char *query) {
         progresult = XMLLINT_ERR_MEM;
         return;
     }
-    ctxt->node = xmlDocGetRootElement(doc);
+    ctxt->node = (xmlNodePtr) doc;
     res = xmlXPathEval(BAD_CAST query, ctxt);
     xmlXPathFreeContext(ctxt);
 
@@ -2187,21 +2201,22 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
 
 #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
        f = fopen(filename, "rb");
+#elif defined(__OS400__)
+       f = fopen(filename, "rb");
 #else
        f = fopen(filename, "r");
 #endif
         if (f != NULL) {
-            int res, size = 3;
+            int res;
             char chars[4096];
             htmlParserCtxtPtr ctxt;
 
-            /* if (repeat) */
-                size = 4096;
             res = fread(chars, 1, 4, f);
             if (res > 0) {
                 ctxt = htmlCreatePushParserCtxt(NULL, NULL,
                             chars, res, filename, XML_CHAR_ENCODING_NONE);
-                while ((res = fread(chars, 1, size, f)) > 0) {
+                xmlCtxtUseOptions(ctxt, options);
+                while ((res = fread(chars, 1, pushsize, f)) > 0) {
                     htmlParseChunk(ctxt, chars, res, 0);
                 }
                 htmlParseChunk(ctxt, chars, 0, 1);
@@ -2212,7 +2227,7 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
         }
     }
 #endif /* LIBXML_PUSH_ENABLED */
-#ifdef HAVE_SYS_MMAN_H
+#ifdef HAVE_MMAP
     else if ((html) && (memory)) {
        int fd;
        struct stat info;
@@ -2222,8 +2237,12 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
        if ((fd = open(filename, O_RDONLY)) < 0)
            return;
        base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
-       if (base == (void *) MAP_FAILED)
+       if (base == (void *) MAP_FAILED) {
+           close(fd);
+           fprintf(stderr, "mmap failure for file %s\n", filename);
+           progresult = XMLLINT_ERR_RDFILE;
            return;
+       }
 
        doc = htmlReadMemory((char *) base, info.st_size, filename,
                             NULL, options);
@@ -2250,6 +2269,8 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
            } else {
 #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
                f = fopen(filename, "rb");
+#elif defined(__OS400__)
+               f = fopen(filename, "rb");
 #else
                f = fopen(filename, "r");
 #endif
@@ -2291,6 +2312,8 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
 
 #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
                f = fopen(filename, "rb");
+#elif defined(__OS400__)
+               f = fopen(filename, "rb");
 #else
                f = fopen(filename, "r");
 #endif
@@ -2327,7 +2350,7 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
                if (rectxt == NULL)
                    xmlFreeParserCtxt(ctxt);
            }
-#ifdef HAVE_SYS_MMAN_H
+#ifdef HAVE_MMAP
        } else if (memory) {
            int fd;
            struct stat info;
@@ -2337,8 +2360,12 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
            if ((fd = open(filename, O_RDONLY)) < 0)
                return;
            base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
-           if (base == (void *) MAP_FAILED)
+           if (base == (void *) MAP_FAILED) {
+               close(fd);
+               fprintf(stderr, "mmap failure for file %s\n", filename);
+               progresult = XMLLINT_ERR_RDFILE;
                return;
+           }
 
            if (rectxt == NULL)
                doc = xmlReadMemory((char *) base, info.st_size,
@@ -2560,7 +2587,7 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
                    fprintf(stderr, "Failed to canonicalize\n");
                    progresult = XMLLINT_ERR_OUT;
                }
-           } else if (canonical) {
+           } else if (canonical_11) {
                xmlChar *result = NULL;
                int size;
 
@@ -2591,7 +2618,7 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
                }
            } else
 #endif
-#ifdef HAVE_SYS_MMAN_H
+#ifdef HAVE_MMAP
            if (memory) {
                xmlChar *result;
                int len;
@@ -2619,7 +2646,7 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
                }
 
            } else
-#endif /* HAVE_SYS_MMAN_H */
+#endif /* HAVE_MMAP */
            if (compress) {
                xmlSaveFile(output ? output : "-", doc);
            } else if (oldout) {
@@ -2947,6 +2974,7 @@ static void showVersion(const char *name) {
     if (xmlHasFeature(XML_WITH_XPTR)) fprintf(stderr, "XPointer ");
     if (xmlHasFeature(XML_WITH_XINCLUDE)) fprintf(stderr, "XInclude ");
     if (xmlHasFeature(XML_WITH_ICONV)) fprintf(stderr, "Iconv ");
+    if (xmlHasFeature(XML_WITH_ICU)) fprintf(stderr, "ICU ");
     if (xmlHasFeature(XML_WITH_ISO8859X)) fprintf(stderr, "ISO8859X ");
     if (xmlHasFeature(XML_WITH_UNICODE)) fprintf(stderr, "Unicode ");
     if (xmlHasFeature(XML_WITH_REGEXP)) fprintf(stderr, "Regexps ");
@@ -2963,128 +2991,133 @@ static void showVersion(const char *name) {
     fprintf(stderr, "\n");
 }
 
-static void usage(const char *name) {
-    printf("Usage : %s [options] XMLfiles ...\n", name);
+static void usage(FILE *f, const char *name) {
+    fprintf(f, "Usage : %s [options] XMLfiles ...\n", name);
 #ifdef LIBXML_OUTPUT_ENABLED
-    printf("\tParse the XML files and output the result of the parsing\n");
+    fprintf(f, "\tParse the XML files and output the result of the parsing\n");
 #else
-    printf("\tParse the XML files\n");
+    fprintf(f, "\tParse the XML files\n");
 #endif /* LIBXML_OUTPUT_ENABLED */
-    printf("\t--version : display the version of the XML library used\n");
+    fprintf(f, "\t--version : display the version of the XML library used\n");
 #ifdef LIBXML_DEBUG_ENABLED
-    printf("\t--debug : dump a debug tree of the in-memory document\n");
-    printf("\t--shell : run a navigating shell\n");
-    printf("\t--debugent : debug the entities defined in the document\n");
+    fprintf(f, "\t--debug : dump a debug tree of the in-memory document\n");
+    fprintf(f, "\t--shell : run a navigating shell\n");
+    fprintf(f, "\t--debugent : debug the entities defined in the document\n");
 #else
 #ifdef LIBXML_READER_ENABLED
-    printf("\t--debug : dump the nodes content when using --stream\n");
+    fprintf(f, "\t--debug : dump the nodes content when using --stream\n");
 #endif /* LIBXML_READER_ENABLED */
 #endif
 #ifdef LIBXML_TREE_ENABLED
-    printf("\t--copy : used to test the internal copy implementation\n");
+    fprintf(f, "\t--copy : used to test the internal copy implementation\n");
 #endif /* LIBXML_TREE_ENABLED */
-    printf("\t--recover : output what was parsable on broken XML documents\n");
-    printf("\t--huge : remove any internal arbitrary parser limits\n");
-    printf("\t--noent : substitute entity references by their value\n");
-    printf("\t--noenc : ignore any encoding specified inside the document\n");
-    printf("\t--noout : don't output the result tree\n");
-    printf("\t--path 'paths': provide a set of paths for resources\n");
-    printf("\t--load-trace : print trace of all external entites loaded\n");
-    printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
-    printf("\t--nocompact : do not generate compact text nodes\n");
-    printf("\t--htmlout : output results as HTML\n");
-    printf("\t--nowrap : do not put HTML doc wrapper\n");
+    fprintf(f, "\t--recover : output what was parsable on broken XML documents\n");
+    fprintf(f, "\t--huge : remove any internal arbitrary parser limits\n");
+    fprintf(f, "\t--noent : substitute entity references by their value\n");
+    fprintf(f, "\t--noenc : ignore any encoding specified inside the document\n");
+    fprintf(f, "\t--noout : don't output the result tree\n");
+    fprintf(f, "\t--path 'paths': provide a set of paths for resources\n");
+    fprintf(f, "\t--load-trace : print trace of all external entities loaded\n");
+    fprintf(f, "\t--nonet : refuse to fetch DTDs or entities over network\n");
+    fprintf(f, "\t--nocompact : do not generate compact text nodes\n");
+    fprintf(f, "\t--htmlout : output results as HTML\n");
+    fprintf(f, "\t--nowrap : do not put HTML doc wrapper\n");
 #ifdef LIBXML_VALID_ENABLED
-    printf("\t--valid : validate the document in addition to std well-formed check\n");
-    printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
-    printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
-    printf("\t--dtdvalidfpi FPI : same but name the DTD with a Public Identifier\n");
+    fprintf(f, "\t--valid : validate the document in addition to std well-formed check\n");
+    fprintf(f, "\t--postvalid : do a posteriori validation, i.e after parsing\n");
+    fprintf(f, "\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
+    fprintf(f, "\t--dtdvalidfpi FPI : same but name the DTD with a Public Identifier\n");
 #endif /* LIBXML_VALID_ENABLED */
-    printf("\t--timing : print some timings\n");
-    printf("\t--output file or -o file: save to a given file\n");
-    printf("\t--repeat : repeat 100 times, for timing or profiling\n");
-    printf("\t--insert : ad-hoc test for valid insertions\n");
+    fprintf(f, "\t--timing : print some timings\n");
+    fprintf(f, "\t--output file or -o file: save to a given file\n");
+    fprintf(f, "\t--repeat : repeat 100 times, for timing or profiling\n");
+    fprintf(f, "\t--insert : ad-hoc test for valid insertions\n");
 #ifdef LIBXML_OUTPUT_ENABLED
 #ifdef HAVE_ZLIB_H
-    printf("\t--compress : turn on gzip compression of output\n");
+    fprintf(f, "\t--compress : turn on gzip compression of output\n");
 #endif
 #endif /* LIBXML_OUTPUT_ENABLED */
 #ifdef LIBXML_HTML_ENABLED
-    printf("\t--html : use the HTML parser\n");
-    printf("\t--xmlout : force to use the XML serializer when using --html\n");
-    printf("\t--nodefdtd : do not default HTML doctype\n");
+    fprintf(f, "\t--html : use the HTML parser\n");
+    fprintf(f, "\t--xmlout : force to use the XML serializer when using --html\n");
+    fprintf(f, "\t--nodefdtd : do not default HTML doctype\n");
 #endif
 #ifdef LIBXML_PUSH_ENABLED
-    printf("\t--push : use the push mode of the parser\n");
+    fprintf(f, "\t--push : use the push mode of the parser\n");
+    fprintf(f, "\t--pushsmall : use the push mode of the parser using tiny increments\n");
 #endif /* LIBXML_PUSH_ENABLED */
-#ifdef HAVE_SYS_MMAN_H
-    printf("\t--memory : parse from memory\n");
+#ifdef HAVE_MMAP
+    fprintf(f, "\t--memory : parse from memory\n");
 #endif
-    printf("\t--maxmem nbbytes : limits memory allocation to nbbytes bytes\n");
-    printf("\t--nowarning : do not emit warnings from parser/validator\n");
-    printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
-    printf("\t--nocdata : replace cdata section with text nodes\n");
+    fprintf(f, "\t--maxmem nbbytes : limits memory allocation to nbbytes bytes\n");
+    fprintf(f, "\t--nowarning : do not emit warnings from parser/validator\n");
+    fprintf(f, "\t--noblanks : drop (ignorable?) blanks spaces\n");
+    fprintf(f, "\t--nocdata : replace cdata section with text nodes\n");
 #ifdef LIBXML_OUTPUT_ENABLED
-    printf("\t--format : reformat/reindent the input\n");
-    printf("\t--encode encoding : output in the given encoding\n");
-    printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
-    printf("\t--pretty STYLE : pretty-print in a particular style\n");
-    printf("\t                 0 Do not pretty print\n");
-    printf("\t                 1 Format the XML content, as --format\n");
-    printf("\t                 2 Add whitespace inside tags, preserving content\n");
+    fprintf(f, "\t--format : reformat/reindent the output\n");
+    fprintf(f, "\t--encode encoding : output in the given encoding\n");
+    fprintf(f, "\t--dropdtd : remove the DOCTYPE of the input docs\n");
+    fprintf(f, "\t--pretty STYLE : pretty-print in a particular style\n");
+    fprintf(f, "\t                 0 Do not pretty print\n");
+    fprintf(f, "\t                 1 Format the XML content, as --format\n");
+    fprintf(f, "\t                 2 Add whitespace inside tags, preserving content\n");
 #endif /* LIBXML_OUTPUT_ENABLED */
-    printf("\t--c14n : save in W3C canonical format v1.0 (with comments)\n");
-    printf("\t--c14n11 : save in W3C canonical format v1.1 (with comments)\n");
-    printf("\t--exc-c14n : save in W3C exclusive canonical format (with comments)\n");
+    fprintf(f, "\t--c14n : save in W3C canonical format v1.0 (with comments)\n");
+    fprintf(f, "\t--c14n11 : save in W3C canonical format v1.1 (with comments)\n");
+    fprintf(f, "\t--exc-c14n : save in W3C exclusive canonical format (with comments)\n");
 #ifdef LIBXML_C14N_ENABLED
 #endif /* LIBXML_C14N_ENABLED */
-    printf("\t--nsclean : remove redundant namespace declarations\n");
-    printf("\t--testIO : test user I/O support\n");
+    fprintf(f, "\t--nsclean : remove redundant namespace declarations\n");
+    fprintf(f, "\t--testIO : test user I/O support\n");
 #ifdef LIBXML_CATALOG_ENABLED
-    printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
-    printf("\t             otherwise XML Catalogs starting from \n");
-    printf("\t         %s are activated by default\n", XML_XML_DEFAULT_CATALOG);
-    printf("\t--nocatalogs: deactivate all catalogs\n");
+    fprintf(f, "\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
+    fprintf(f, "\t             otherwise XML Catalogs starting from \n");
+    fprintf(f, "\t         %s are activated by default\n", XML_XML_DEFAULT_CATALOG);
+    fprintf(f, "\t--nocatalogs: deactivate all catalogs\n");
 #endif
-    printf("\t--auto : generate a small doc on the fly\n");
+    fprintf(f, "\t--auto : generate a small doc on the fly\n");
 #ifdef LIBXML_XINCLUDE_ENABLED
-    printf("\t--xinclude : do XInclude processing\n");
-    printf("\t--noxincludenode : same but do not generate XInclude nodes\n");
-    printf("\t--nofixup-base-uris : do not fixup xml:base uris\n");
+    fprintf(f, "\t--xinclude : do XInclude processing\n");
+    fprintf(f, "\t--noxincludenode : same but do not generate XInclude nodes\n");
+    fprintf(f, "\t--nofixup-base-uris : do not fixup xml:base uris\n");
 #endif
-    printf("\t--loaddtd : fetch external DTD\n");
-    printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
+    fprintf(f, "\t--loaddtd : fetch external DTD\n");
+    fprintf(f, "\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
 #ifdef LIBXML_READER_ENABLED
-    printf("\t--stream : use the streaming interface to process very large files\n");
-    printf("\t--walker : create a reader and walk though the resulting doc\n");
+    fprintf(f, "\t--stream : use the streaming interface to process very large files\n");
+    fprintf(f, "\t--walker : create a reader and walk though the resulting doc\n");
 #endif /* LIBXML_READER_ENABLED */
 #ifdef LIBXML_PATTERN_ENABLED
-    printf("\t--pattern pattern_value : test the pattern support\n");
+    fprintf(f, "\t--pattern pattern_value : test the pattern support\n");
 #endif
-    printf("\t--chkregister : verify the node registration code\n");
+    fprintf(f, "\t--chkregister : verify the node registration code\n");
 #ifdef LIBXML_SCHEMAS_ENABLED
-    printf("\t--relaxng schema : do RelaxNG validation against the schema\n");
-    printf("\t--schema schema : do validation against the WXS schema\n");
+    fprintf(f, "\t--relaxng schema : do RelaxNG validation against the schema\n");
+    fprintf(f, "\t--schema schema : do validation against the WXS schema\n");
 #endif
 #ifdef LIBXML_SCHEMATRON_ENABLED
-    printf("\t--schematron schema : do validation against a schematron\n");
+    fprintf(f, "\t--schematron schema : do validation against a schematron\n");
 #endif
 #ifdef LIBXML_SAX1_ENABLED
-    printf("\t--sax1: use the old SAX1 interfaces for processing\n");
+    fprintf(f, "\t--sax1: use the old SAX1 interfaces for processing\n");
 #endif
-    printf("\t--sax: do not build a tree but work just at the SAX level\n");
-    printf("\t--oldxml10: use XML-1.0 parsing rules before the 5th edition\n");
+    fprintf(f, "\t--sax: do not build a tree but work just at the SAX level\n");
+    fprintf(f, "\t--oldxml10: use XML-1.0 parsing rules before the 5th edition\n");
 #ifdef LIBXML_XPATH_ENABLED
-    printf("\t--xpath expr: evaluate the XPath expression, inply --noout\n");
+    fprintf(f, "\t--xpath expr: evaluate the XPath expression, imply --noout\n");
 #endif
 
-    printf("\nLibxml project home page: http://xmlsoft.org/\n");
-    printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
+    fprintf(f, "\nLibxml project home page: http://xmlsoft.org/\n");
+    fprintf(f, "To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
 }
 
 static void registerNode(xmlNodePtr node)
 {
     node->_private = malloc(sizeof(long));
+    if (node->_private == NULL) {
+        fprintf(stderr, "Out of memory in xmllint:registerNode()\n");
+       exit(XMLLINT_ERR_MEM);
+    }
     *(long*)node->_private = (long) 0x81726354;
     nbregister++;
 }
@@ -3105,7 +3138,7 @@ main(int argc, char **argv) {
     const char* indent;
 
     if (argc <= 1) {
-       usage(argv[0]);
+       usage(stderr, argv[0]);
        return(1);
     }
     LIBXML_TEST_VERSION
@@ -3246,8 +3279,13 @@ main(int argc, char **argv) {
        else if ((!strcmp(argv[i], "-push")) ||
                 (!strcmp(argv[i], "--push")))
            push++;
+       else if ((!strcmp(argv[i], "-pushsmall")) ||
+                (!strcmp(argv[i], "--pushsmall"))) {
+           push++;
+            pushsize = 10;
+        }
 #endif /* LIBXML_PUSH_ENABLED */
-#ifdef HAVE_SYS_MMAN_H
+#ifdef HAVE_MMAP
        else if ((!strcmp(argv[i], "-memory")) ||
                 (!strcmp(argv[i], "--memory")))
            memory++;
@@ -3339,8 +3377,9 @@ main(int argc, char **argv) {
         }
        else if ((!strcmp(argv[i], "-noblanks")) ||
                 (!strcmp(argv[i], "--noblanks"))) {
-            noblanks++;
-            xmlKeepBlanksDefault(0);
+           noblanks++;
+           xmlKeepBlanksDefault(0);
+           options |= XML_PARSE_NOBLANKS;
         }
        else if ((!strcmp(argv[i], "-maxmem")) ||
                 (!strcmp(argv[i], "--maxmem"))) {
@@ -3364,11 +3403,13 @@ main(int argc, char **argv) {
                 (!strcmp(argv[i], "--pretty"))) {
             i++;
 #ifdef LIBXML_OUTPUT_ENABLED
-            format = atoi(argv[i]);
-            if (format == 1) {
-                noblanks++;
-                xmlKeepBlanksDefault(0);
-            }
+       if (argv[i] != NULL) {
+                format = atoi(argv[i]);
+                if (format == 1) {
+                    noblanks++;
+                    xmlKeepBlanksDefault(0);
+                }
+       }
 #endif /* LIBXML_OUTPUT_ENABLED */
        }
 #ifdef LIBXML_READER_ENABLED
@@ -3449,7 +3490,7 @@ main(int argc, char **argv) {
            options |= XML_PARSE_OLD10;
        } else {
            fprintf(stderr, "Unknown option %s\n", argv[i]);
-           usage(argv[0]);
+           usage(stderr, argv[0]);
            return(1);
        }
     }
@@ -3743,7 +3784,7 @@ main(int argc, char **argv) {
        xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
     }
     if ((files == 0) && (!generate) && (version == 0)) {
-       usage(argv[0]);
+       usage(stderr, argv[0]);
     }
 #ifdef LIBXML_SCHEMATRON_ENABLED
     if (wxschematron != NULL)