applied patch from Alfred Mickautsch for better DTD support. fixed bug

* xmlwriter.c include/libxml/xmlwriter.h doc/* : applied patch from
  Alfred Mickautsch for better DTD support.
* SAX2.c HTMLparser.c parser.c xinclude.c xmllint.c xmlreader.c
  xmlschemas.c: fixed bug #137867 i.e. fixed properly the way
  reference counting is handled in the XML parser which had the
  side effect of removing a lot of hazardous cruft added to try
  to fix the problems associated as they popped up.
* xmlIO.c: FILE * close fixup for stderr/stdout
Daniel
diff --git a/doc/libxml2-api.xml b/doc/libxml2-api.xml
index 1162e1a..3546b0c 100644
--- a/doc/libxml2-api.xml
+++ b/doc/libxml2-api.xml
@@ -2709,20 +2709,17 @@
      <description>text writing API for XML </description>
      <author>Alfred Mickautsch &lt;alfred@mickautsch.de&gt; </author>
      <exports symbol='xmlTextWriterWriteProcessingInstruction' type='macro'/>
-     <exports symbol='xmlTextWriterEndDTDEntity' type='macro'/>
-     <exports symbol='xmlTextWriterEndDTDElement' type='macro'/>
-     <exports symbol='xmlTextWriterEndDTDAttlist' type='macro'/>
      <exports symbol='xmlTextWriterWriteDocType' type='macro'/>
      <exports symbol='xmlTextWriter' type='typedef'/>
      <exports symbol='xmlTextWriterPtr' type='typedef'/>
      <exports symbol='xmlTextWriterStartDocument' type='function'/>
-     <exports symbol='xmlTextWriterEndDocument' type='function'/>
+     <exports symbol='xmlTextWriterEndPI' type='function'/>
      <exports symbol='xmlTextWriterWriteBase64' type='function'/>
      <exports symbol='xmlTextWriterSetIndentString' type='function'/>
      <exports symbol='xmlTextWriterStartAttribute' type='function'/>
      <exports symbol='xmlTextWriterEndComment' type='function'/>
      <exports symbol='xmlTextWriterWriteRawLen' type='function'/>
-     <exports symbol='xmlTextWriterWriteVFormatDTD' type='function'/>
+     <exports symbol='xmlTextWriterWriteDTDExternalEntityContents' type='function'/>
      <exports symbol='xmlTextWriterWriteVFormatCDATA' type='function'/>
      <exports symbol='xmlTextWriterStartAttributeNS' type='function'/>
      <exports symbol='xmlNewTextWriterPushParser' type='function'/>
@@ -2734,10 +2731,11 @@
      <exports symbol='xmlTextWriterSetIndent' type='function'/>
      <exports symbol='xmlTextWriterWriteFormatPI' type='function'/>
      <exports symbol='xmlTextWriterWriteDTDAttlist' type='function'/>
-     <exports symbol='xmlTextWriterEndPI' type='function'/>
      <exports symbol='xmlTextWriterStartComment' type='function'/>
+     <exports symbol='xmlTextWriterWriteVFormatDTD' type='function'/>
      <exports symbol='xmlTextWriterEndCDATA' type='function'/>
      <exports symbol='xmlTextWriterStartElementNS' type='function'/>
+     <exports symbol='xmlTextWriterEndDTDEntity' type='function'/>
      <exports symbol='xmlNewTextWriter' type='function'/>
      <exports symbol='xmlFreeTextWriter' type='function'/>
      <exports symbol='xmlTextWriterWriteVFormatDTDAttlist' type='function'/>
@@ -2745,9 +2743,11 @@
      <exports symbol='xmlTextWriterStartElement' type='function'/>
      <exports symbol='xmlTextWriterWriteDTDExternalEntity' type='function'/>
      <exports symbol='xmlTextWriterWriteFormatRaw' type='function'/>
+     <exports symbol='xmlTextWriterWritePI' type='function'/>
      <exports symbol='xmlTextWriterWriteCDATA' type='function'/>
      <exports symbol='xmlTextWriterWriteVFormatDTDInternalEntity' type='function'/>
      <exports symbol='xmlTextWriterWriteVFormatAttribute' type='function'/>
+     <exports symbol='xmlTextWriterEndDTDElement' type='function'/>
      <exports symbol='xmlTextWriterWriteComment' type='function'/>
      <exports symbol='xmlTextWriterWriteElement' type='function'/>
      <exports symbol='xmlTextWriterEndElement' type='function'/>
@@ -2757,9 +2757,9 @@
      <exports symbol='xmlTextWriterWriteVFormatElement' type='function'/>
      <exports symbol='xmlTextWriterWriteFormatComment' type='function'/>
      <exports symbol='xmlTextWriterWriteAttributeNS' type='function'/>
-     <exports symbol='xmlTextWriterWritePI' type='function'/>
-     <exports symbol='xmlTextWriterWriteVFormatRaw' type='function'/>
      <exports symbol='xmlTextWriterWriteFormatDTDInternalEntity' type='function'/>
+     <exports symbol='xmlTextWriterWriteVFormatRaw' type='function'/>
+     <exports symbol='xmlTextWriterEndDocument' type='function'/>
      <exports symbol='xmlTextWriterWriteVFormatString' type='function'/>
      <exports symbol='xmlTextWriterWriteDTDInternalEntity' type='function'/>
      <exports symbol='xmlTextWriterWriteVFormatElementNS' type='function'/>
@@ -2774,6 +2774,7 @@
      <exports symbol='xmlTextWriterWriteAttribute' type='function'/>
      <exports symbol='xmlNewTextWriterDoc' type='function'/>
      <exports symbol='xmlTextWriterWriteFormatDTDElement' type='function'/>
+     <exports symbol='xmlTextWriterEndDTDAttlist' type='function'/>
      <exports symbol='xmlNewTextWriterTree' type='function'/>
      <exports symbol='xmlNewTextWriterMemory' type='function'/>
      <exports symbol='xmlTextWriterWriteFormatCDATA' type='function'/>
@@ -3550,15 +3551,6 @@
     <macro name='xmlRootNode' file='tree'>
       <info>Macro for compatibility naming layer with libxml1. Maps to &quot;children&quot;.</info>
     </macro>
-    <macro name='xmlTextWriterEndDTDAttlist' file='xmlwriter'>
-      <info>This macro maps to xmlTextWriterEndDTD</info>
-    </macro>
-    <macro name='xmlTextWriterEndDTDElement' file='xmlwriter'>
-      <info>This macro maps to xmlTextWriterEndDTD</info>
-    </macro>
-    <macro name='xmlTextWriterEndDTDEntity' file='xmlwriter'>
-      <info>This macro maps to xmlTextWriterEndDTD</info>
-    </macro>
     <macro name='xmlTextWriterWriteDocType' file='xmlwriter'>
       <info>this macro maps to xmlTextWriterWriteDTD</info>
     </macro>
@@ -7227,7 +7219,7 @@
       <info>Do a copy of the document info. If recursive, the content tree will be copied too as well as DTD, namespaces and entities.</info>
       <return type='xmlDocPtr' info='a new #xmlDocPtr, or NULL in case of error.'/>
       <arg name='doc' type='xmlDocPtr' info='the document'/>
-      <arg name='recursive' type='int' info='if 1 do a recursive copy.'/>
+      <arg name='recursive' type='int' info='if not zero do a recursive copy.'/>
     </function>
     <function name='xmlCopyDtd' file='tree'>
       <info>Do a copy of the dtd.</info>
@@ -7274,7 +7266,7 @@
       <info>Do a copy of the node.</info>
       <return type='xmlNodePtr' info='a new #xmlNodePtr, or NULL in case of error.'/>
       <arg name='node' type='const xmlNodePtr' info='the node'/>
-      <arg name='recursive' type='int' info='if 1 do a recursive copy.'/>
+      <arg name='extended' type='int' info='if 1 do a recursive copy (properties, namespaces and children when applicable) if 2 copy properties and namespaces (when applicable)'/>
     </function>
     <function name='xmlCopyNodeList' file='tree'>
       <info>Do a recursive copy of the node list.</info>
@@ -7599,7 +7591,7 @@
       <return type='xmlNodePtr' info='a new #xmlNodePtr, or NULL in case of error.'/>
       <arg name='node' type='const xmlNodePtr' info='the node'/>
       <arg name='doc' type='xmlDocPtr' info='the document'/>
-      <arg name='recursive' type='int' info='if 1 do a recursive copy.'/>
+      <arg name='extended' type='int' info='if 1 do a recursive copy (properties, namespaces and children when applicable) if 2 copy properties and namespaces (when applicable)'/>
     </function>
     <function name='xmlDocDump' file='tree'>
       <info>Dump an XML document to an open FILE.</info>
@@ -10989,8 +10981,8 @@
       <arg name='format' type='int' info='should formatting spaces be added.'/>
     </function>
     <function name='xmlSaveFormatFileTo' file='tree'>
-      <info>Dump an XML document to an I/O buffer.</info>
-      <return type='int' info='the number of bytes written or -1 in case of failure. Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 or xmlKeepBlanksDefault(0) was called'/>
+      <info>Dump an XML document to an I/O buffer. NOTE: the I/O buffer is closed as part of the call.</info>
+      <return type='int' info='the number of bytes written or -1 in case of failure.'/>
       <arg name='buf' type='xmlOutputBufferPtr' info='an output I/O buffer'/>
       <arg name='cur' type='xmlDocPtr' info='the document'/>
       <arg name='encoding' type='const char *' info='the encoding if any assuming the I/O layer handles the trancoding'/>
@@ -12015,6 +12007,21 @@
       <return type='int' info='the bytes written (may be 0 because of buffering) or -1 in case of error'/>
       <arg name='writer' type='xmlTextWriterPtr' info='the xmlTextWriterPtr'/>
     </function>
+    <function name='xmlTextWriterEndDTDAttlist' file='xmlwriter'>
+      <info>End an xml DTD attribute list.</info>
+      <return type='int' info='the bytes written (may be 0 because of buffering) or -1 in case of error'/>
+      <arg name='writer' type='xmlTextWriterPtr' info='the xmlTextWriterPtr'/>
+    </function>
+    <function name='xmlTextWriterEndDTDElement' file='xmlwriter'>
+      <info>End an xml DTD element.</info>
+      <return type='int' info='the bytes written (may be 0 because of buffering) or -1 in case of error'/>
+      <arg name='writer' type='xmlTextWriterPtr' info='the xmlTextWriterPtr'/>
+    </function>
+    <function name='xmlTextWriterEndDTDEntity' file='xmlwriter'>
+      <info>End an xml DTD entity.</info>
+      <return type='int' info='the bytes written (may be 0 because of buffering) or -1 in case of error'/>
+      <arg name='writer' type='xmlTextWriterPtr' info='the xmlTextWriterPtr'/>
+    </function>
     <function name='xmlTextWriterEndDocument' file='xmlwriter'>
       <info>End an xml document. All open elements are closed</info>
       <return type='int' info='the bytes written (may be 0 because of buffering) or -1 in case of error'/>
@@ -12210,7 +12217,7 @@
       <arg name='content' type='const xmlChar *' info='content of the entity'/>
     </function>
     <function name='xmlTextWriterWriteDTDExternalEntity' file='xmlwriter'>
-      <info>Write a DTD internal entity.</info>
+      <info>Write a DTD external entity. The entity must have been started with xmlTextWriterStartDTDEntity</info>
       <return type='int' info='the bytes written (may be 0 because of buffering) or -1 in case of error'/>
       <arg name='writer' type='xmlTextWriterPtr' info='the xmlTextWriterPtr'/>
       <arg name='pe' type='int' info='TRUE if this is a parameter entity, FALSE if not'/>
@@ -12219,6 +12226,14 @@
       <arg name='sysid' type='const xmlChar *' info='the system identifier, which is the URI of the DTD'/>
       <arg name='ndataid' type='const xmlChar *' info='the xml notation name.'/>
     </function>
+    <function name='xmlTextWriterWriteDTDExternalEntityContents' file='xmlwriter'>
+      <info>Write the contents of a DTD external entity.</info>
+      <return type='int' info='the bytes written (may be 0 because of buffering) or -1 in case of error'/>
+      <arg name='writer' type='xmlTextWriterPtr' info='the xmlTextWriterPtr'/>
+      <arg name='pubid' type='const xmlChar *' info='the public identifier, which is an alternative to the system identifier'/>
+      <arg name='sysid' type='const xmlChar *' info='the system identifier, which is the URI of the DTD'/>
+      <arg name='ndataid' type='const xmlChar *' info='the xml notation name.'/>
+    </function>
     <function name='xmlTextWriterWriteDTDInternalEntity' file='xmlwriter'>
       <info>Write a DTD internal entity.</info>
       <return type='int' info='the bytes written (may be 0 because of buffering) or -1 in case of error'/>