Applied the last patches from Gary, cleanup, activated threading all user

* include/libxml/SAX.h include/libxml/globals.h include/libxml/parser.h
  include/libxml/parserInternals.h include/libxml/tree.h
  include/libxml/xmlerror.h HTMLparser.c SAX.c error.c globals.c
  nanoftp.c nanohttp.c parser.c parserInternals.c testDocbook.c
  testHTML.c testSAX.c tree.c uri.c xlink.c xmlmemory.c:
  Applied the last patches from Gary, cleanup, activated threading
  all user accessible global variables are now handled in globals.[ch]
  Still a bit rought but make tests passes with either
  --with-threads defined at configure time or not.
* Makefile.am example/Makefile.am: added globals.[ch] and threads
  linking options
Daniel
diff --git a/ChangeLog b/ChangeLog
index 5a7268b..b1e842b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+Sat Oct 13 11:08:20 CEST 2001 Daniel Veillard <daniel@veillard.com>
+
+	* include/libxml/SAX.h include/libxml/globals.h include/libxml/parser.h
+	  include/libxml/parserInternals.h include/libxml/tree.h
+	  include/libxml/xmlerror.h HTMLparser.c SAX.c error.c globals.c
+	  nanoftp.c nanohttp.c parser.c parserInternals.c testDocbook.c
+	  testHTML.c testSAX.c tree.c uri.c xlink.c xmlmemory.c:
+	  Applied the last patches from Gary, cleanup, activated threading
+	  all user accessible global variables are now handled in globals.[ch]
+	  Still a bit rought but make tests passes with either 
+	  --with-threads defined at configure time or not.
+	* Makefile.am example/Makefile.am: added globals.[ch] and threads
+	  linking options
+
 Fri Oct 12 19:25:55 CEST 2001 Daniel Veillard <daniel@veillard.com>
 
 	* Makefile.am include/libxml/Makefile.am
diff --git a/HTMLparser.c b/HTMLparser.c
index 6817191..4b0193e 100644
--- a/HTMLparser.c
+++ b/HTMLparser.c
@@ -3557,6 +3557,8 @@
 htmlParseDocument(htmlParserCtxtPtr ctxt) {
     xmlDtdPtr dtd;
 
+    xmlInitParser();
+
     htmlDefaultSAXHandlerInit();
     ctxt->html = 1;
 
@@ -4669,6 +4671,8 @@
     htmlParserInputPtr inputStream;
     xmlParserInputBufferPtr buf;
 
+    xmlInitParser();
+
     buf = xmlAllocParserInputBuffer(enc);
     if (buf == NULL) return(NULL);
 
@@ -4745,6 +4749,8 @@
     htmlDocPtr ret;
     htmlParserCtxtPtr ctxt;
 
+    xmlInitParser();
+
     if (cur == NULL) return(NULL);
 
 
@@ -4869,6 +4875,8 @@
     htmlParserCtxtPtr ctxt;
     htmlSAXHandlerPtr oldsax = NULL;
 
+    xmlInitParser();
+
     ctxt = htmlCreateFileParserCtxt(filename, encoding);
     if (ctxt == NULL) return(NULL);
     if (sax != NULL) {
diff --git a/Makefile.am b/Makefile.am
index 89de4b8..7104fc0 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -23,14 +23,14 @@
 		parser.c tree.c hash.c list.c xmlIO.c xmlmemory.c uri.c  \
 		valid.c xlink.c HTMLparser.c HTMLtree.c debugXML.c xpath.c  \
 		xpointer.c xinclude.c nanohttp.c nanoftp.c DOCBparser.c \
-		catalog.c threads.c strio.c trio.c
+		catalog.c globals.c threads.c strio.c trio.c
 
 else
 libxml2_la_SOURCES = SAX.c entities.c encoding.c error.c parserInternals.c  \
 		parser.c tree.c hash.c list.c xmlIO.c xmlmemory.c uri.c  \
 		valid.c xlink.c HTMLparser.c HTMLtree.c debugXML.c xpath.c  \
 		xpointer.c xinclude.c nanohttp.c nanoftp.c DOCBparser.c \
-		catalog.c threads.c
+		catalog.c globals.c threads.c
 
 endif
 
diff --git a/SAX.c b/SAX.c
index be5c760..b7cf784 100644
--- a/SAX.c
+++ b/SAX.c
@@ -88,14 +88,6 @@
     return(ctxt->input->col);
 }
 
-/*
- * The default SAX Locator.
- */
-
-xmlSAXLocator xmlDefaultSAXLocator = {
-    getPublicId, getSystemId, getLineNumber, getColumnNumber
-};
-
 /**
  * isStandalone:
  * @ctx: the user data (XML parser context)
@@ -1667,38 +1659,51 @@
     }
 }
 
-/*
- * Default handler for XML, builds the DOM tree
+/**
+ * xmlDefaultSAXHandlerInit:
+ *
+ * Initialize the default SAX handler
  */
-xmlSAXHandler xmlDefaultSAXHandler = {
-    internalSubset,
-    isStandalone,
-    hasInternalSubset,
-    hasExternalSubset,
-    resolveEntity,
-    getEntity,
-    entityDecl,
-    notationDecl,
-    attributeDecl,
-    elementDecl,
-    unparsedEntityDecl,
-    setDocumentLocator,
-    startDocument,
-    endDocument,
-    startElement,
-    endElement,
-    reference,
-    characters,
-    characters,
-    processingInstruction,
-    comment,
-    xmlParserWarning,
-    xmlParserError,
-    xmlParserError,
-    getParameterEntity,
-    cdataBlock,
-    externalSubset,
-};
+void
+initxmlDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
+{
+    if(hdlr->initialized == 1)
+	return;
+
+    hdlr->internalSubset = internalSubset;
+    hdlr->externalSubset = externalSubset;
+    hdlr->isStandalone = isStandalone;
+    hdlr->hasInternalSubset = hasInternalSubset;
+    hdlr->hasExternalSubset = hasExternalSubset;
+    hdlr->resolveEntity = resolveEntity;
+    hdlr->getEntity = getEntity;
+    hdlr->getParameterEntity = getParameterEntity;
+    hdlr->entityDecl = entityDecl;
+    hdlr->attributeDecl = attributeDecl;
+    hdlr->elementDecl = elementDecl;
+    hdlr->notationDecl = notationDecl;
+    hdlr->unparsedEntityDecl = unparsedEntityDecl;
+    hdlr->setDocumentLocator = setDocumentLocator;
+    hdlr->startDocument = startDocument;
+    hdlr->endDocument = endDocument;
+    hdlr->startElement = startElement;
+    hdlr->endElement = endElement;
+    hdlr->reference = reference;
+    hdlr->characters = characters;
+    hdlr->cdataBlock = cdataBlock;
+    hdlr->ignorableWhitespace = characters;
+    hdlr->processingInstruction = processingInstruction;
+    hdlr->comment = comment;
+    /* if (xmlGetWarningsDefaultValue == 0) */
+    if (warning == 0)
+	hdlr->warning = NULL;
+    else
+	hdlr->warning = xmlParserWarning;
+    hdlr->error = xmlParserError;
+    hdlr->fatalError = xmlParserError;
+
+    hdlr->initialized = 1;
+}
 
 /**
  * xmlDefaultSAXHandlerInit:
@@ -1708,77 +1713,52 @@
 void
 xmlDefaultSAXHandlerInit(void)
 {
-    static int xmlSAXInitialized = 0;
-    if (xmlSAXInitialized)
-	return;
-
-    xmlDefaultSAXHandler.internalSubset = internalSubset;
-    xmlDefaultSAXHandler.externalSubset = externalSubset;
-    xmlDefaultSAXHandler.isStandalone = isStandalone;
-    xmlDefaultSAXHandler.hasInternalSubset = hasInternalSubset;
-    xmlDefaultSAXHandler.hasExternalSubset = hasExternalSubset;
-    xmlDefaultSAXHandler.resolveEntity = resolveEntity;
-    xmlDefaultSAXHandler.getEntity = getEntity;
-    xmlDefaultSAXHandler.getParameterEntity = getParameterEntity;
-    xmlDefaultSAXHandler.entityDecl = entityDecl;
-    xmlDefaultSAXHandler.attributeDecl = attributeDecl;
-    xmlDefaultSAXHandler.elementDecl = elementDecl;
-    xmlDefaultSAXHandler.notationDecl = notationDecl;
-    xmlDefaultSAXHandler.unparsedEntityDecl = unparsedEntityDecl;
-    xmlDefaultSAXHandler.setDocumentLocator = setDocumentLocator;
-    xmlDefaultSAXHandler.startDocument = startDocument;
-    xmlDefaultSAXHandler.endDocument = endDocument;
-    xmlDefaultSAXHandler.startElement = startElement;
-    xmlDefaultSAXHandler.endElement = endElement;
-    xmlDefaultSAXHandler.reference = reference;
-    xmlDefaultSAXHandler.characters = characters;
-    xmlDefaultSAXHandler.cdataBlock = cdataBlock;
-    xmlDefaultSAXHandler.ignorableWhitespace = characters;
-    xmlDefaultSAXHandler.processingInstruction = processingInstruction;
-    xmlDefaultSAXHandler.comment = comment;
-    if (xmlGetWarningsDefaultValue == 0)
-	xmlDefaultSAXHandler.warning = NULL;
-    else
-	xmlDefaultSAXHandler.warning = xmlParserWarning;
-    xmlDefaultSAXHandler.error = xmlParserError;
-    xmlDefaultSAXHandler.fatalError = xmlParserError;
-
-    xmlSAXInitialized = 1;
+	initxmlDefaultSAXHandler(&xmlDefaultSAXHandler, xmlGetWarningsDefaultValue);
 }
 
 #ifdef LIBXML_HTML_ENABLED
-/*
- * Default handler for HTML, builds the DOM tree
+
+/**
+ * htmlDefaultSAXHandlerInit:
+ *
+ * Initialize the default SAX handler
  */
-xmlSAXHandler htmlDefaultSAXHandler = {
-    internalSubset,
-    NULL,
-    NULL,
-    NULL,
-    NULL,
-    getEntity,
-    NULL,
-    NULL,
-    NULL,
-    NULL,
-    NULL,
-    setDocumentLocator,
-    startDocument,
-    endDocument,
-    startElement,
-    endElement,
-    NULL,
-    characters,
-    ignorableWhitespace,
-    NULL,
-    comment,
-    xmlParserWarning,
-    xmlParserError,
-    xmlParserError,
-    getParameterEntity,
-    cdataBlock,
-    NULL,
-};
+void
+inithtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
+{
+    if(hdlr->initialized == 1)
+	return;
+
+    hdlr->internalSubset = internalSubset;
+    hdlr->externalSubset = NULL;
+    hdlr->isStandalone = NULL;
+    hdlr->hasInternalSubset = NULL;
+    hdlr->hasExternalSubset = NULL;
+    hdlr->resolveEntity = NULL;
+    hdlr->getEntity = getEntity;
+    hdlr->getParameterEntity = NULL;
+    hdlr->entityDecl = NULL;
+    hdlr->attributeDecl = NULL;
+    hdlr->elementDecl = NULL;
+    hdlr->notationDecl = NULL;
+    hdlr->unparsedEntityDecl = NULL;
+    hdlr->setDocumentLocator = setDocumentLocator;
+    hdlr->startDocument = startDocument;
+    hdlr->endDocument = endDocument;
+    hdlr->startElement = startElement;
+    hdlr->endElement = endElement;
+    hdlr->reference = NULL;
+    hdlr->characters = characters;
+    hdlr->cdataBlock = cdataBlock;
+    hdlr->ignorableWhitespace = ignorableWhitespace;
+    hdlr->processingInstruction = NULL;
+    hdlr->comment = comment;
+    hdlr->warning = xmlParserWarning;
+    hdlr->error = xmlParserError;
+    hdlr->fatalError = xmlParserError;
+
+    hdlr->initialized = 1;
+}
 
 /**
  * htmlDefaultSAXHandlerInit:
@@ -1788,75 +1768,49 @@
 void
 htmlDefaultSAXHandlerInit(void)
 {
-    static int htmlSAXInitialized = 0;
-    if (htmlSAXInitialized)
-	return;
-
-    htmlDefaultSAXHandler.internalSubset = internalSubset;
-    htmlDefaultSAXHandler.externalSubset = NULL;
-    htmlDefaultSAXHandler.isStandalone = NULL;
-    htmlDefaultSAXHandler.hasInternalSubset = NULL;
-    htmlDefaultSAXHandler.hasExternalSubset = NULL;
-    htmlDefaultSAXHandler.resolveEntity = NULL;
-    htmlDefaultSAXHandler.getEntity = getEntity;
-    htmlDefaultSAXHandler.getParameterEntity = NULL;
-    htmlDefaultSAXHandler.entityDecl = NULL;
-    htmlDefaultSAXHandler.attributeDecl = NULL;
-    htmlDefaultSAXHandler.elementDecl = NULL;
-    htmlDefaultSAXHandler.notationDecl = NULL;
-    htmlDefaultSAXHandler.unparsedEntityDecl = NULL;
-    htmlDefaultSAXHandler.setDocumentLocator = setDocumentLocator;
-    htmlDefaultSAXHandler.startDocument = startDocument;
-    htmlDefaultSAXHandler.endDocument = endDocument;
-    htmlDefaultSAXHandler.startElement = startElement;
-    htmlDefaultSAXHandler.endElement = endElement;
-    htmlDefaultSAXHandler.reference = NULL;
-    htmlDefaultSAXHandler.characters = characters;
-    htmlDefaultSAXHandler.cdataBlock = cdataBlock;
-    htmlDefaultSAXHandler.ignorableWhitespace = ignorableWhitespace;
-    htmlDefaultSAXHandler.processingInstruction = NULL;
-    htmlDefaultSAXHandler.comment = comment;
-    htmlDefaultSAXHandler.warning = xmlParserWarning;
-    htmlDefaultSAXHandler.error = xmlParserError;
-    htmlDefaultSAXHandler.fatalError = xmlParserError;
-
-    htmlSAXInitialized = 1;
+	inithtmlDefaultSAXHandler(&htmlDefaultSAXHandler);
 }
+
 #endif /* LIBXML_HTML_ENABLED */
 
 #ifdef LIBXML_DOCB_ENABLED
-/*
- * Default handler for SGML DocBook, builds the DOM tree
- */
-xmlSAXHandler docbDefaultSAXHandler = {
-    internalSubset,
-    isStandalone,
-    hasInternalSubset,
-    hasExternalSubset,
-    resolveEntity,
-    getEntity,
-    entityDecl,
-    NULL,
-    NULL,
-    NULL,
-    NULL,
-    setDocumentLocator,
-    startDocument,
-    endDocument,
-    startElement,
-    endElement,
-    reference,
-    characters,
-    ignorableWhitespace,
-    NULL,
-    comment,
-    xmlParserWarning,
-    xmlParserError,
-    xmlParserError,
-    getParameterEntity,
-    NULL,
-    NULL,
-};
+
+void
+initdocbDefaultSAXHandler(xmlSAXHandler *hdlr)
+{
+    if(hdlr->initialized == 1)
+	return;
+
+    hdlr->internalSubset = internalSubset;
+    hdlr->externalSubset = NULL;
+    hdlr->isStandalone = isStandalone;
+    hdlr->hasInternalSubset = hasInternalSubset;
+    hdlr->hasExternalSubset = hasExternalSubset;
+    hdlr->resolveEntity = resolveEntity;
+    hdlr->getEntity = getEntity;
+    hdlr->getParameterEntity = NULL;
+    hdlr->entityDecl = entityDecl;
+    hdlr->attributeDecl = NULL;
+    hdlr->elementDecl = NULL;
+    hdlr->notationDecl = NULL;
+    hdlr->unparsedEntityDecl = NULL;
+    hdlr->setDocumentLocator = setDocumentLocator;
+    hdlr->startDocument = startDocument;
+    hdlr->endDocument = endDocument;
+    hdlr->startElement = startElement;
+    hdlr->endElement = endElement;
+    hdlr->reference = reference;
+    hdlr->characters = characters;
+    hdlr->cdataBlock = NULL;
+    hdlr->ignorableWhitespace = ignorableWhitespace;
+    hdlr->processingInstruction = NULL;
+    hdlr->comment = comment;
+    hdlr->warning = xmlParserWarning;
+    hdlr->error = xmlParserError;
+    hdlr->fatalError = xmlParserError;
+
+    hdlr->initialized = 1;
+}
 
 /**
  * docbDefaultSAXHandlerInit:
@@ -1866,39 +1820,7 @@
 void
 docbDefaultSAXHandlerInit(void)
 {
-    static int docbSAXInitialized = 0;
-    if (docbSAXInitialized)
-	return;
-
-    docbDefaultSAXHandler.internalSubset = internalSubset;
-    docbDefaultSAXHandler.externalSubset = NULL;
-    docbDefaultSAXHandler.isStandalone = isStandalone;
-    docbDefaultSAXHandler.hasInternalSubset = hasInternalSubset;
-    docbDefaultSAXHandler.hasExternalSubset = hasExternalSubset;
-    docbDefaultSAXHandler.resolveEntity = resolveEntity;
-    docbDefaultSAXHandler.getEntity = getEntity;
-    docbDefaultSAXHandler.getParameterEntity = NULL;
-    docbDefaultSAXHandler.entityDecl = entityDecl;
-    docbDefaultSAXHandler.attributeDecl = NULL;
-    docbDefaultSAXHandler.elementDecl = NULL;
-    docbDefaultSAXHandler.notationDecl = NULL;
-    docbDefaultSAXHandler.unparsedEntityDecl = NULL;
-    docbDefaultSAXHandler.setDocumentLocator = setDocumentLocator;
-    docbDefaultSAXHandler.startDocument = startDocument;
-    docbDefaultSAXHandler.endDocument = endDocument;
-    docbDefaultSAXHandler.startElement = startElement;
-    docbDefaultSAXHandler.endElement = endElement;
-    docbDefaultSAXHandler.reference = reference;
-    docbDefaultSAXHandler.characters = characters;
-    docbDefaultSAXHandler.cdataBlock = NULL;
-    docbDefaultSAXHandler.ignorableWhitespace = ignorableWhitespace;
-    docbDefaultSAXHandler.processingInstruction = NULL;
-    docbDefaultSAXHandler.comment = comment;
-    docbDefaultSAXHandler.warning = xmlParserWarning;
-    docbDefaultSAXHandler.error = xmlParserError;
-    docbDefaultSAXHandler.fatalError = xmlParserError;
-
-    docbSAXInitialized = 1;
+	initdocbDefaultSAXHandler(&docbDefaultSAXHandler);
 }
 
 #endif /* LIBXML_DOCB_ENABLED */
diff --git a/build_glob.py b/build_glob.py
index fbf45c1..33856cd 100755
--- a/build_glob.py
+++ b/build_glob.py
@@ -78,9 +78,9 @@
         writeline(global_functions_hdr,"(*(__"+fields[1]+"()))")
         writeline(global_functions_hdr,"#else")
         if len(fields) == 3:
-            writeline(global_functions_hdr,"extern "+fields[0]+" "+fields[1]+fields[2]+";")
+            writeline(global_functions_hdr,"LIBXML_DLL_IMPORT extern "+fields[0]+" "+fields[1]+fields[2]+";")
         else:
-            writeline(global_functions_hdr,"extern "+fields[0]+" "+fields[1]+";")
+            writeline(global_functions_hdr,"LIBXML_DLL_IMPORT extern "+fields[0]+" "+fields[1]+";")
         writeline(global_functions_hdr,"#endif")
         # Update the implementation file
         writeline(global_functions_impl)
@@ -96,7 +96,7 @@
         writeline(global_functions_impl, "    if (IS_MAIN_THREAD)")
         writeline(global_functions_impl, "\treturn (&"+fields[1]+");")
         writeline(global_functions_impl, "    else")
-        writeline(global_functions_impl, "\treturn (&get_glob_struct()->"+fields[1]+");")
+        writeline(global_functions_impl, "\treturn (&xmlGetGlobalState()->"+fields[1]+");")
         writeline(global_functions_impl, "}")
     # Terminate the header file with appropriate boilerplate
     writeline(global_functions_hdr)
diff --git a/error.c b/error.c
index 5235450..d663689 100644
--- a/error.c
+++ b/error.c
@@ -69,9 +69,14 @@
     va_end(args);
 }
 
-xmlGenericErrorFunc xmlGenericError = xmlGenericErrorDefaultFunc;
-void *xmlGenericErrorContext = NULL;
-
+void
+initGenericErrorDefaultFunc(xmlGenericErrorFunc *handler)
+{
+	if (handler == NULL)
+		xmlGenericError = xmlGenericErrorDefaultFunc;
+	else
+		(*handler) = xmlGenericErrorDefaultFunc;
+}
 
 /**
  * xmlSetGenericErrorFunc:
diff --git a/example/Makefile.am b/example/Makefile.am
index d802850..cb73e72 100644
--- a/example/Makefile.am
+++ b/example/Makefile.am
@@ -7,5 +7,5 @@
 	-I$(top_builddir)/include		\
 	-I$(top_srcdir)/include -I@srcdir@
 
-LDADD = $(top_builddir)/libxml2.la @Z_LIBS@ $(ICONV_LIBS) -lm
+LDADD = $(top_builddir)/libxml2.la @THREAD_LIBS@ @Z_LIBS@ $(ICONV_LIBS) -lm
 
diff --git a/globals.c b/globals.c
index c509287..6872017 100644
--- a/globals.c
+++ b/globals.c
@@ -13,8 +13,7 @@
 
 #include "libxml.h"
 
-#include <libxml/threads.h>
-#include <libxml/globals.h>
+#include <libxml/xmlmemory.h>
 
 /*
  * Helpful Macro
@@ -38,10 +37,16 @@
  ************************************************************************/
 
 const char *xmlParserVersion = LIBXML_VERSION_STRING;
+
 /*
  * Memory allocation routines
  */
 #if defined(DEBUG_MEMORY_LOCATION) | defined(DEBUG_MEMORY)
+extern void xmlMemFree(void *ptr);
+extern void * xmlMemMalloc(size_t size);
+extern void * xmlMemRealloc(void *ptr,size_t size);
+extern char * xmlMemoryStrdup(const char *str);
+
 xmlFreeFunc xmlFree = (xmlFreeFunc) xmlMemFree;
 xmlMallocFunc xmlMalloc = (xmlMallocFunc) xmlMemMalloc;
 xmlReallocFunc xmlRealloc = (xmlReallocFunc) xmlMemRealloc;
@@ -53,6 +58,36 @@
 xmlStrdupFunc xmlMemStrdup = (xmlStrdupFunc) strdup;
 #endif
 
+#include <libxml/threads.h>
+#include <libxml/globals.h>
+#include <libxml/SAX.h>
+
+#undef	docbDefaultSAXHandler
+#undef	htmlDefaultSAXHandler
+#undef	oldXMLWDcompatibility
+#undef	xmlBufferAllocScheme
+#undef	xmlDefaultBufferSize
+#undef	xmlDefaultSAXHandler
+#undef	xmlDefaultSAXLocator
+#undef	xmlDoValidityCheckingDefaultValue
+#undef	xmlGenericError
+#undef	xmlGenericErrorContext
+#undef	xmlGetWarningsDefaultValue
+#undef	xmlIndentTreeOutput
+#undef	xmlKeepBlanksDefaultValue
+#undef	xmlLineNumbersDefaultValue
+#undef	xmlLoadExtDtdDefaultValue
+#undef	xmlParserDebugEntities
+#undef	xmlParserVersion
+#undef	xmlPedanticParserDefaultValue
+#undef	xmlSaveNoEmptyTags
+#undef	xmlSubstituteEntitiesDefaultValue
+
+#undef	xmlFree
+#undef	xmlMalloc
+#undef	xmlMemStrdup
+#undef	xmlRealloc
+
 /*
  * Buffers stuff
  */
@@ -62,6 +97,7 @@
 /*
  * Parser defaults
  */
+
 int oldXMLWDcompatibility = 0; /* DEPRECATED */
 int xmlParserDebugEntities = 0;
 int xmlDoValidityCheckingDefaultValue = 0;
@@ -264,7 +300,7 @@
     if (IS_MAIN_THREAD)
 	return (&docbDefaultSAXHandler);
     else
-	return (&get_glob_struct()->docbDefaultSAXHandler);
+	return (&xmlGetGlobalState()->docbDefaultSAXHandler);
 }
 
 extern xmlSAXHandler htmlDefaultSAXHandler;
@@ -274,7 +310,7 @@
     if (IS_MAIN_THREAD)
 	return (&htmlDefaultSAXHandler);
     else
-	return (&get_glob_struct()->htmlDefaultSAXHandler);
+	return (&xmlGetGlobalState()->htmlDefaultSAXHandler);
 }
 
 extern int oldXMLWDcompatibility;
@@ -284,7 +320,7 @@
     if (IS_MAIN_THREAD)
 	return (&oldXMLWDcompatibility);
     else
-	return (&get_glob_struct()->oldXMLWDcompatibility);
+	return (&xmlGetGlobalState()->oldXMLWDcompatibility);
 }
 
 extern xmlBufferAllocationScheme xmlBufferAllocScheme;
@@ -294,7 +330,7 @@
     if (IS_MAIN_THREAD)
 	return (&xmlBufferAllocScheme);
     else
-	return (&get_glob_struct()->xmlBufferAllocScheme);
+	return (&xmlGetGlobalState()->xmlBufferAllocScheme);
 }
 
 extern int xmlDefaultBufferSize;
@@ -304,7 +340,7 @@
     if (IS_MAIN_THREAD)
 	return (&xmlDefaultBufferSize);
     else
-	return (&get_glob_struct()->xmlDefaultBufferSize);
+	return (&xmlGetGlobalState()->xmlDefaultBufferSize);
 }
 
 extern xmlSAXHandler xmlDefaultSAXHandler;
@@ -314,7 +350,7 @@
     if (IS_MAIN_THREAD)
 	return (&xmlDefaultSAXHandler);
     else
-	return (&get_glob_struct()->xmlDefaultSAXHandler);
+	return (&xmlGetGlobalState()->xmlDefaultSAXHandler);
 }
 
 extern xmlSAXLocator xmlDefaultSAXLocator;
@@ -324,7 +360,7 @@
     if (IS_MAIN_THREAD)
 	return (&xmlDefaultSAXLocator);
     else
-	return (&get_glob_struct()->xmlDefaultSAXLocator);
+	return (&xmlGetGlobalState()->xmlDefaultSAXLocator);
 }
 
 extern int xmlDoValidityCheckingDefaultValue;
@@ -334,7 +370,7 @@
     if (IS_MAIN_THREAD)
 	return (&xmlDoValidityCheckingDefaultValue);
     else
-	return (&get_glob_struct()->xmlDoValidityCheckingDefaultValue);
+	return (&xmlGetGlobalState()->xmlDoValidityCheckingDefaultValue);
 }
 
 extern xmlFreeFunc xmlFree;
@@ -344,7 +380,7 @@
     if (IS_MAIN_THREAD)
 	return (&xmlFree);
     else
-	return (&get_glob_struct()->xmlFree);
+	return (&xmlGetGlobalState()->xmlFree);
 }
 
 extern xmlGenericErrorFunc xmlGenericError;
@@ -354,7 +390,7 @@
     if (IS_MAIN_THREAD)
 	return (&xmlGenericError);
     else
-	return (&get_glob_struct()->xmlGenericError);
+	return (&xmlGetGlobalState()->xmlGenericError);
 }
 
 extern void * xmlGenericErrorContext;
@@ -364,7 +400,7 @@
     if (IS_MAIN_THREAD)
 	return (&xmlGenericErrorContext);
     else
-	return (&get_glob_struct()->xmlGenericErrorContext);
+	return (&xmlGetGlobalState()->xmlGenericErrorContext);
 }
 
 extern int xmlGetWarningsDefaultValue;
@@ -374,7 +410,7 @@
     if (IS_MAIN_THREAD)
 	return (&xmlGetWarningsDefaultValue);
     else
-	return (&get_glob_struct()->xmlGetWarningsDefaultValue);
+	return (&xmlGetGlobalState()->xmlGetWarningsDefaultValue);
 }
 
 extern int xmlIndentTreeOutput;
@@ -384,7 +420,7 @@
     if (IS_MAIN_THREAD)
 	return (&xmlIndentTreeOutput);
     else
-	return (&get_glob_struct()->xmlIndentTreeOutput);
+	return (&xmlGetGlobalState()->xmlIndentTreeOutput);
 }
 
 extern int xmlKeepBlanksDefaultValue;
@@ -394,7 +430,7 @@
     if (IS_MAIN_THREAD)
 	return (&xmlKeepBlanksDefaultValue);
     else
-	return (&get_glob_struct()->xmlKeepBlanksDefaultValue);
+	return (&xmlGetGlobalState()->xmlKeepBlanksDefaultValue);
 }
 
 extern int xmlLineNumbersDefaultValue;
@@ -404,7 +440,7 @@
     if (IS_MAIN_THREAD)
 	return (&xmlLineNumbersDefaultValue);
     else
-	return (&get_glob_struct()->xmlLineNumbersDefaultValue);
+	return (&xmlGetGlobalState()->xmlLineNumbersDefaultValue);
 }
 
 extern int xmlLoadExtDtdDefaultValue;
@@ -414,7 +450,7 @@
     if (IS_MAIN_THREAD)
 	return (&xmlLoadExtDtdDefaultValue);
     else
-	return (&get_glob_struct()->xmlLoadExtDtdDefaultValue);
+	return (&xmlGetGlobalState()->xmlLoadExtDtdDefaultValue);
 }
 
 extern xmlMallocFunc xmlMalloc;
@@ -424,7 +460,7 @@
     if (IS_MAIN_THREAD)
 	return (&xmlMalloc);
     else
-	return (&get_glob_struct()->xmlMalloc);
+	return (&xmlGetGlobalState()->xmlMalloc);
 }
 
 extern xmlStrdupFunc xmlMemStrdup;
@@ -434,7 +470,7 @@
     if (IS_MAIN_THREAD)
 	return (&xmlMemStrdup);
     else
-	return (&get_glob_struct()->xmlMemStrdup);
+	return (&xmlGetGlobalState()->xmlMemStrdup);
 }
 
 extern int xmlParserDebugEntities;
@@ -444,7 +480,7 @@
     if (IS_MAIN_THREAD)
 	return (&xmlParserDebugEntities);
     else
-	return (&get_glob_struct()->xmlParserDebugEntities);
+	return (&xmlGetGlobalState()->xmlParserDebugEntities);
 }
 
 extern const char * xmlParserVersion;
@@ -454,7 +490,7 @@
     if (IS_MAIN_THREAD)
 	return (&xmlParserVersion);
     else
-	return (&get_glob_struct()->xmlParserVersion);
+	return (&xmlGetGlobalState()->xmlParserVersion);
 }
 
 extern int xmlPedanticParserDefaultValue;
@@ -464,7 +500,7 @@
     if (IS_MAIN_THREAD)
 	return (&xmlPedanticParserDefaultValue);
     else
-	return (&get_glob_struct()->xmlPedanticParserDefaultValue);
+	return (&xmlGetGlobalState()->xmlPedanticParserDefaultValue);
 }
 
 extern xmlReallocFunc xmlRealloc;
@@ -474,7 +510,7 @@
     if (IS_MAIN_THREAD)
 	return (&xmlRealloc);
     else
-	return (&get_glob_struct()->xmlRealloc);
+	return (&xmlGetGlobalState()->xmlRealloc);
 }
 
 extern int xmlSaveNoEmptyTags;
@@ -484,7 +520,7 @@
     if (IS_MAIN_THREAD)
 	return (&xmlSaveNoEmptyTags);
     else
-	return (&get_glob_struct()->xmlSaveNoEmptyTags);
+	return (&xmlGetGlobalState()->xmlSaveNoEmptyTags);
 }
 
 extern int xmlSubstituteEntitiesDefaultValue;
@@ -494,5 +530,5 @@
     if (IS_MAIN_THREAD)
 	return (&xmlSubstituteEntitiesDefaultValue);
     else
-	return (&get_glob_struct()->xmlSubstituteEntitiesDefaultValue);
+	return (&xmlGetGlobalState()->xmlSubstituteEntitiesDefaultValue);
 }
diff --git a/include/libxml/SAX.h b/include/libxml/SAX.h
index 4472f83..01cc204 100644
--- a/include/libxml/SAX.h
+++ b/include/libxml/SAX.h
@@ -111,6 +111,14 @@
 						 const xmlChar *value,
 						 int len);
 
+void		initxmlDefaultSAXHandler	(xmlSAXHandler *hdlr,
+						 int warning);
+#ifdef LIBXML_HTML_ENABLED
+void		inithtmlDefaultSAXHandler	(xmlSAXHandler *hdlr);
+#endif
+#ifdef LIBXML_DOCB_ENABLED
+void		initdocbDefaultSAXHandler	(xmlSAXHandler *hdlr);
+#endif
 void		xmlDefaultSAXHandlerInit	(void);
 void		htmlDefaultSAXHandlerInit	(void);
 void		docbDefaultSAXHandlerInit	(void);
diff --git a/include/libxml/globals.h b/include/libxml/globals.h
index 94fdd38..499fa58 100644
--- a/include/libxml/globals.h
+++ b/include/libxml/globals.h
@@ -25,6 +25,7 @@
  * Externally global symbols which need to be protected for backwards
  * compatibility support.
  */
+
 #undef	docbDefaultSAXHandler
 #undef	htmlDefaultSAXHandler
 #undef	oldXMLWDcompatibility
@@ -48,42 +49,43 @@
 #undef	xmlPedanticParserDefaultValue
 #undef	xmlRealloc
 #undef	xmlSaveNoEmptyTags
-/* #undef	xmlStringComment */
-/* #undef	xmlStringText */
-/* #undef	xmlStringTextNoenc */
 #undef	xmlSubstituteEntitiesDefaultValue
 
 typedef struct _xmlGlobalState xmlGlobalState;
 typedef xmlGlobalState *xmlGlobalStatePtr;
 struct _xmlGlobalState 
 {
-	xmlSAXHandler docbDefaultSAXHandler;
-	xmlSAXHandler htmlDefaultSAXHandler;
-	int oldXMLWDcompatibility;
-	xmlBufferAllocationScheme xmlBufferAllocScheme;
-	int xmlDefaultBufferSize;
-	xmlSAXHandler xmlDefaultSAXHandler;
-	xmlSAXLocator xmlDefaultSAXLocator;
-	int xmlDoValidityCheckingDefaultValue;
+	const char *xmlParserVersion;
+
 	xmlFreeFunc xmlFree;
+	xmlMallocFunc xmlMalloc;
+	xmlStrdupFunc xmlMemStrdup;
+	xmlReallocFunc xmlRealloc;
+
 	xmlGenericErrorFunc xmlGenericError;
 	void *xmlGenericErrorContext;
+
+	xmlSAXLocator xmlDefaultSAXLocator;
+	xmlSAXHandler xmlDefaultSAXHandler;
+	xmlSAXHandler docbDefaultSAXHandler;
+	xmlSAXHandler htmlDefaultSAXHandler;
+
+	int oldXMLWDcompatibility;
+
+	xmlBufferAllocationScheme xmlBufferAllocScheme;
+	int xmlDefaultBufferSize;
+
+	int xmlSubstituteEntitiesDefaultValue;
+	int xmlDoValidityCheckingDefaultValue;
 	int xmlGetWarningsDefaultValue;
-	int xmlIndentTreeOutput;
 	int xmlKeepBlanksDefaultValue;
 	int xmlLineNumbersDefaultValue;
 	int xmlLoadExtDtdDefaultValue;
-	xmlMallocFunc xmlMalloc;
-	xmlStrdupFunc xmlMemStrdup;
 	int xmlParserDebugEntities;
-	const char *xmlParserVersion;
 	int xmlPedanticParserDefaultValue;
-	xmlReallocFunc xmlRealloc;
+
 	int xmlSaveNoEmptyTags;
-/* 	const xmlChar xmlStringComment[8]; */
-/* 	const xmlChar xmlStringText[5]; */
-/* 	const xmlChar xmlStringTextNoenc[10]; */
-	int xmlSubstituteEntitiesDefaultValue;
+	int xmlIndentTreeOutput;
 };
 
 void	xmlInitializeGlobalState(xmlGlobalStatePtr gs);
@@ -100,7 +102,7 @@
 #define docbDefaultSAXHandler \
 (*(__docbDefaultSAXHandler()))
 #else
-extern xmlSAXHandler docbDefaultSAXHandler;
+LIBXML_DLL_IMPORT extern xmlSAXHandler docbDefaultSAXHandler;
 #endif
 
 #ifdef LIBXML_THREAD_ENABLED
@@ -108,7 +110,7 @@
 #define htmlDefaultSAXHandler \
 (*(__htmlDefaultSAXHandler()))
 #else
-extern xmlSAXHandler htmlDefaultSAXHandler;
+LIBXML_DLL_IMPORT extern xmlSAXHandler htmlDefaultSAXHandler;
 #endif
 
 #ifdef LIBXML_THREAD_ENABLED
@@ -116,7 +118,7 @@
 #define oldXMLWDcompatibility \
 (*(__oldXMLWDcompatibility()))
 #else
-extern int oldXMLWDcompatibility;
+LIBXML_DLL_IMPORT extern int oldXMLWDcompatibility;
 #endif
 
 #ifdef LIBXML_THREAD_ENABLED
@@ -124,7 +126,7 @@
 #define xmlBufferAllocScheme \
 (*(__xmlBufferAllocScheme()))
 #else
-extern xmlBufferAllocationScheme xmlBufferAllocScheme;
+LIBXML_DLL_IMPORT extern xmlBufferAllocationScheme xmlBufferAllocScheme;
 #endif
 
 #ifdef LIBXML_THREAD_ENABLED
@@ -132,7 +134,7 @@
 #define xmlDefaultBufferSize \
 (*(__xmlDefaultBufferSize()))
 #else
-extern int xmlDefaultBufferSize;
+LIBXML_DLL_IMPORT extern int xmlDefaultBufferSize;
 #endif
 
 #ifdef LIBXML_THREAD_ENABLED
@@ -140,7 +142,7 @@
 #define xmlDefaultSAXHandler \
 (*(__xmlDefaultSAXHandler()))
 #else
-extern xmlSAXHandler xmlDefaultSAXHandler;
+LIBXML_DLL_IMPORT extern xmlSAXHandler xmlDefaultSAXHandler;
 #endif
 
 #ifdef LIBXML_THREAD_ENABLED
@@ -148,7 +150,7 @@
 #define xmlDefaultSAXLocator \
 (*(__xmlDefaultSAXLocator()))
 #else
-extern xmlSAXLocator xmlDefaultSAXLocator;
+LIBXML_DLL_IMPORT extern xmlSAXLocator xmlDefaultSAXLocator;
 #endif
 
 #ifdef LIBXML_THREAD_ENABLED
@@ -156,7 +158,7 @@
 #define xmlDoValidityCheckingDefaultValue \
 (*(__xmlDoValidityCheckingDefaultValue()))
 #else
-extern int xmlDoValidityCheckingDefaultValue;
+LIBXML_DLL_IMPORT extern int xmlDoValidityCheckingDefaultValue;
 #endif
 
 #ifdef LIBXML_THREAD_ENABLED
@@ -164,7 +166,7 @@
 #define xmlFree \
 (*(__xmlFree()))
 #else
-extern xmlFreeFunc xmlFree;
+LIBXML_DLL_IMPORT extern xmlFreeFunc xmlFree;
 #endif
 
 #ifdef LIBXML_THREAD_ENABLED
@@ -172,7 +174,7 @@
 #define xmlGenericError \
 (*(__xmlGenericError()))
 #else
-extern xmlGenericErrorFunc xmlGenericError;
+LIBXML_DLL_IMPORT extern xmlGenericErrorFunc xmlGenericError;
 #endif
 
 #ifdef LIBXML_THREAD_ENABLED
@@ -180,7 +182,7 @@
 #define xmlGenericErrorContext \
 (*(__xmlGenericErrorContext()))
 #else
-extern void * xmlGenericErrorContext;
+LIBXML_DLL_IMPORT extern void * xmlGenericErrorContext;
 #endif
 
 #ifdef LIBXML_THREAD_ENABLED
@@ -188,7 +190,7 @@
 #define xmlGetWarningsDefaultValue \
 (*(__xmlGetWarningsDefaultValue()))
 #else
-extern int xmlGetWarningsDefaultValue;
+LIBXML_DLL_IMPORT extern int xmlGetWarningsDefaultValue;
 #endif
 
 #ifdef LIBXML_THREAD_ENABLED
@@ -196,7 +198,7 @@
 #define xmlIndentTreeOutput \
 (*(__xmlIndentTreeOutput()))
 #else
-extern int xmlIndentTreeOutput;
+LIBXML_DLL_IMPORT extern int xmlIndentTreeOutput;
 #endif
 
 #ifdef LIBXML_THREAD_ENABLED
@@ -204,7 +206,7 @@
 #define xmlKeepBlanksDefaultValue \
 (*(__xmlKeepBlanksDefaultValue()))
 #else
-extern int xmlKeepBlanksDefaultValue;
+LIBXML_DLL_IMPORT extern int xmlKeepBlanksDefaultValue;
 #endif
 
 #ifdef LIBXML_THREAD_ENABLED
@@ -212,7 +214,7 @@
 #define xmlLineNumbersDefaultValue \
 (*(__xmlLineNumbersDefaultValue()))
 #else
-extern int xmlLineNumbersDefaultValue;
+LIBXML_DLL_IMPORT extern int xmlLineNumbersDefaultValue;
 #endif
 
 #ifdef LIBXML_THREAD_ENABLED
@@ -220,7 +222,7 @@
 #define xmlLoadExtDtdDefaultValue \
 (*(__xmlLoadExtDtdDefaultValue()))
 #else
-extern int xmlLoadExtDtdDefaultValue;
+LIBXML_DLL_IMPORT extern int xmlLoadExtDtdDefaultValue;
 #endif
 
 #ifdef LIBXML_THREAD_ENABLED
@@ -228,7 +230,7 @@
 #define xmlMalloc \
 (*(__xmlMalloc()))
 #else
-extern xmlMallocFunc xmlMalloc;
+LIBXML_DLL_IMPORT extern xmlMallocFunc xmlMalloc;
 #endif
 
 #ifdef LIBXML_THREAD_ENABLED
@@ -236,7 +238,7 @@
 #define xmlMemStrdup \
 (*(__xmlMemStrdup()))
 #else
-extern xmlStrdupFunc xmlMemStrdup;
+LIBXML_DLL_IMPORT extern xmlStrdupFunc xmlMemStrdup;
 #endif
 
 #ifdef LIBXML_THREAD_ENABLED
@@ -244,7 +246,7 @@
 #define xmlParserDebugEntities \
 (*(__xmlParserDebugEntities()))
 #else
-extern int xmlParserDebugEntities;
+LIBXML_DLL_IMPORT extern int xmlParserDebugEntities;
 #endif
 
 #ifdef LIBXML_THREAD_ENABLED
@@ -252,7 +254,7 @@
 #define xmlParserVersion \
 (*(__xmlParserVersion()))
 #else
-extern const char * xmlParserVersion;
+LIBXML_DLL_IMPORT extern const char * xmlParserVersion;
 #endif
 
 #ifdef LIBXML_THREAD_ENABLED
@@ -260,7 +262,7 @@
 #define xmlPedanticParserDefaultValue \
 (*(__xmlPedanticParserDefaultValue()))
 #else
-extern int xmlPedanticParserDefaultValue;
+LIBXML_DLL_IMPORT extern int xmlPedanticParserDefaultValue;
 #endif
 
 #ifdef LIBXML_THREAD_ENABLED
@@ -268,7 +270,7 @@
 #define xmlRealloc \
 (*(__xmlRealloc()))
 #else
-extern xmlReallocFunc xmlRealloc;
+LIBXML_DLL_IMPORT extern xmlReallocFunc xmlRealloc;
 #endif
 
 #ifdef LIBXML_THREAD_ENABLED
@@ -276,7 +278,7 @@
 #define xmlSaveNoEmptyTags \
 (*(__xmlSaveNoEmptyTags()))
 #else
-extern int xmlSaveNoEmptyTags;
+LIBXML_DLL_IMPORT extern int xmlSaveNoEmptyTags;
 #endif
 
 #ifdef LIBXML_THREAD_ENABLED
@@ -284,7 +286,7 @@
 #define xmlSubstituteEntitiesDefaultValue \
 (*(__xmlSubstituteEntitiesDefaultValue()))
 #else
-extern int xmlSubstituteEntitiesDefaultValue;
+LIBXML_DLL_IMPORT extern int xmlSubstituteEntitiesDefaultValue;
 #endif
 
 #ifdef __cplusplus
diff --git a/include/libxml/parser.h b/include/libxml/parser.h
index 37244a6..115612c 100644
--- a/include/libxml/parser.h
+++ b/include/libxml/parser.h
@@ -13,7 +13,9 @@
 #include <libxml/valid.h>
 #include <libxml/xmlIO.h>
 #include <libxml/entities.h>
-
+#if defined(_REENTRANT) || (_POSIX_C_SOURCE - 0 >= 199506L)
+#include <pthread.h>
+#endif
 
 #ifdef __cplusplus
 extern "C" {
@@ -319,6 +321,7 @@
     getParameterEntitySAXFunc getParameterEntity;
     cdataBlockSAXFunc cdataBlock;
     externalSubsetSAXFunc externalSubset;
+    int initialized;
 };
 
 /**
@@ -571,6 +574,12 @@
 		xmlLoadExternalEntity	(const char *URL,
 					 const char *ID,
 					 xmlParserCtxtPtr context);
+#include <libxml/globals.h>
+/*
+ * Parser Locking
+ */
+int	xmlLockContext(xmlParserCtxtPtr ctxt);
+int	xmlUnlockContext(xmlParserCtxtPtr ctxt);
 
 #ifdef __cplusplus
 }
diff --git a/include/libxml/parserInternals.h b/include/libxml/parserInternals.h
index 9ebc48e..0f62a8c 100644
--- a/include/libxml/parserInternals.h
+++ b/include/libxml/parserInternals.h
@@ -174,19 +174,6 @@
     while ((*p) && (*(p) != '<')) (p)++
 
 /**
- * Global vaiables affecting the default parser behaviour.
- */
-
-LIBXML_DLL_IMPORT extern int xmlParserDebugEntities;
-LIBXML_DLL_IMPORT extern int xmlGetWarningsDefaultValue;
-LIBXML_DLL_IMPORT extern int xmlParserDebugEntities;
-LIBXML_DLL_IMPORT extern int xmlSubstituteEntitiesDefaultValue;
-LIBXML_DLL_IMPORT extern int xmlDoValidityCheckingDefaultValue;
-LIBXML_DLL_IMPORT extern int xmlLoadExtDtdDefaultValue;
-LIBXML_DLL_IMPORT extern int xmlPedanticParserDefaultValue;
-LIBXML_DLL_IMPORT extern int xmlKeepBlanksDefaultValue;
-
-/**
  * Global variables used for predefined strings
  */
 LIBXML_DLL_IMPORT extern const xmlChar xmlStringText[];
diff --git a/include/libxml/tree.h b/include/libxml/tree.h
index 4d4e719..b2e466b 100644
--- a/include/libxml/tree.h
+++ b/include/libxml/tree.h
@@ -25,6 +25,8 @@
 extern "C" {
 #endif
 
+#define BASE_BUFFER_SIZE 4000
+
 /**
  * XML_XML_NAMESPACE:
  *
diff --git a/include/libxml/xmlerror.h b/include/libxml/xmlerror.h
index 5b43734..aa5078f 100644
--- a/include/libxml/xmlerror.h
+++ b/include/libxml/xmlerror.h
@@ -2,6 +2,7 @@
 #define __XML_ERROR_H__
 
 #include <libxml/parser.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -144,18 +145,12 @@
 typedef void (*xmlGenericErrorFunc) (void *ctx, const char *msg, ...);
 
 /*
- * Those are the default error function and associated context to use
- * when when there is an error and no parsing or validity context available
- */
-
-LIBXML_DLL_IMPORT extern xmlGenericErrorFunc xmlGenericError;
-LIBXML_DLL_IMPORT extern void *xmlGenericErrorContext;
-
-/*
- * Use the following function to reset the two previous global variables.
+ * Use the following function to reset the two global variables
+ * xmlGenericError and xmlGenericErrorContext.
  */
 void	xmlSetGenericErrorFunc	(void *ctx,
 				 xmlGenericErrorFunc handler);
+void	initGenericErrorDefaultFunc(xmlGenericErrorFunc *handler);
 
 /*
  * Default message routines used by SAX and Valid context for error
diff --git a/nanoftp.c b/nanoftp.c
index 1d078fd..a83e897 100644
--- a/nanoftp.c
+++ b/nanoftp.c
@@ -56,8 +56,9 @@
 #endif
 
 #include <libxml/xmlmemory.h>
-#include <libxml/nanoftp.h>
+#include <libxml/parser.h>
 #include <libxml/xmlerror.h>
+#include <libxml/nanoftp.h>
 
 /* #define DEBUG_FTP 1  */
 #ifdef STANDALONE
diff --git a/nanohttp.c b/nanohttp.c
index 908103b..9c4b84f 100644
--- a/nanohttp.c
+++ b/nanohttp.c
@@ -63,6 +63,7 @@
 #define SOCKET int
 #endif
 
+#include <libxml/globals.h>
 #include <libxml/xmlerror.h>
 #include <libxml/xmlmemory.h>
 #include <libxml/parser.h> /* for xmlStr(n)casecmp() */
diff --git a/parser.c b/parser.c
index 31eb8ff..1d64452 100644
--- a/parser.c
+++ b/parser.c
@@ -41,6 +41,8 @@
 #include <stdlib.h>
 #include <string.h>
 #include <libxml/xmlmemory.h>
+#include <libxml/threads.h>
+#include <libxml/globals.h>
 #include <libxml/tree.h>
 #include <libxml/parser.h>
 #include <libxml/parserInternals.h>
@@ -78,11 +80,6 @@
 #define XML_PARSER_BUFFER_SIZE 100
 
 /*
- * Various global defaults for parsing
- */
-int xmlParserDebugEntities = 0;
-
-/*
  * List of XML prefixed PI allowed by W3C specs
  */
 
@@ -10191,6 +10188,8 @@
 xmlInitParser(void) {
     if (xmlParserInitialized) return;
 
+    xmlInitThreads();
+    initGenericErrorDefaultFunc(NULL);
     xmlInitCharEncodingHandlers();
     xmlInitializePredefinedEntities();
     xmlDefaultSAXHandlerInit();
@@ -10217,11 +10216,11 @@
 
 void
 xmlCleanupParser(void) {
-    xmlParserInitialized = 0;
     xmlCleanupCharEncodingHandlers();
     xmlCleanupPredefinedEntities();
 #ifdef LIBXML_CATALOG_ENABLED
     xmlCatalogCleanup();
 #endif
+    xmlCleanupThreads();
+    xmlParserInitialized = 0;
 }
-
diff --git a/parserInternals.c b/parserInternals.c
index 4a1a77a..4edb111 100644
--- a/parserInternals.c
+++ b/parserInternals.c
@@ -56,27 +56,12 @@
 /*
  * Various global defaults for parsing
  */
-int xmlGetWarningsDefaultValue = 1;
 #ifdef VMS
 int xmlSubstituteEntitiesDefaultVal = 0;
 #define xmlSubstituteEntitiesDefaultValue xmlSubstituteEntitiesDefaultVal 
 int xmlDoValidityCheckingDefaultVal = 0;
 #define xmlDoValidityCheckingDefaultValue xmlDoValidityCheckingDefaultVal
-#else
-int xmlSubstituteEntitiesDefaultValue = 0;
-int xmlDoValidityCheckingDefaultValue = 0;
 #endif
-int xmlLoadExtDtdDefaultValue = 0;
-int xmlPedanticParserDefaultValue = 0;
-int xmlLineNumbersDefaultValue = 0;
-int xmlKeepBlanksDefaultValue = 1;
-
-/************************************************************************
- *									*
- * 		Version and Features handling				*
- *									*
- ************************************************************************/
-const char *xmlParserVersion = LIBXML_VERSION_STRING;
 
 /**
  * xmlCheckVersion:
diff --git a/testDocbook.c b/testDocbook.c
index cea9f93..8822fa3 100644
--- a/testDocbook.c
+++ b/testDocbook.c
@@ -74,7 +74,8 @@
     NULL, /* xmlParserError */
     NULL, /* getParameterEntity */
     NULL, /* cdataBlock */
-    NULL  /* externalSubset */
+    NULL, /* externalSubset */
+    1
 };
 
 xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
@@ -609,7 +610,8 @@
     fatalErrorDebug,
     getParameterEntityDebug,
     cdataBlockDebug,
-    externalSubsetDebug
+    externalSubsetDebug,
+    1
 };
 
 xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;
diff --git a/testHTML.c b/testHTML.c
index 102e3c6..6ec56a2 100644
--- a/testHTML.c
+++ b/testHTML.c
@@ -73,7 +73,8 @@
     NULL, /* xmlParserError */
     NULL, /* getParameterEntity */
     NULL, /* cdataBlock */
-    NULL  /* externalSubset */
+    NULL, /* externalSubset */
+    1
 };
 
 xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
@@ -591,7 +592,8 @@
     fatalErrorDebug,
     getParameterEntityDebug,
     cdataDebug,
-    NULL
+    NULL,
+    1
 };
 
 xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;
diff --git a/testSAX.c b/testSAX.c
index 47b100a..a6a0d79 100644
--- a/testSAX.c
+++ b/testSAX.c
@@ -31,6 +31,7 @@
 #endif
 
 
+#include <libxml/globals.h>
 #include <libxml/xmlerror.h>
 #include <libxml/parser.h>
 #include <libxml/parserInternals.h> /* only for xmlNewInputFromFile() */
@@ -71,7 +72,8 @@
     NULL, /* xmlParserError */
     NULL, /* getParameterEntity */
     NULL, /* cdataBlock; */
-    NULL  /* externalSubset; */
+    NULL,  /* externalSubset; */
+    1
 };
 
 xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
@@ -597,7 +599,8 @@
     fatalErrorDebug,
     getParameterEntityDebug,
     cdataBlockDebug,
-    externalSubsetDebug
+    externalSubsetDebug,
+    1
 };
 
 xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;
diff --git a/tree.c b/tree.c
index 799d9bf..99c3989 100644
--- a/tree.c
+++ b/tree.c
@@ -38,28 +38,19 @@
 
 /************************************************************************
  *									*
- *			Deprecated					*
- *									*
- ************************************************************************/
-int oldXMLWDcompatibility = 0;
- 
-/************************************************************************
- *									*
  * 		A few static variables and macros			*
  *									*
  ************************************************************************/
-
+/* #undef xmlStringText */
 const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 };
+/* #undef xmlStringTextNoenc */
 const xmlChar xmlStringTextNoenc[] =
               { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 };
+/* #undef xmlStringComment */
 const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 };
 
-int xmlIndentTreeOutput = 0;
-xmlBufferAllocationScheme xmlBufferAllocScheme = XML_BUFFER_ALLOC_EXACT;
-
 static int xmlCompressMode = 0;
 static int xmlCheckDTD = 1;
-int xmlSaveNoEmptyTags = 0;
 
 #define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) {		\
     xmlNodePtr ulccur = (n)->children;					\
@@ -4744,10 +4735,6 @@
  *									*
  ************************************************************************/
 
-#define BASE_BUFFER_SIZE 4000
-
-int xmlDefaultBufferSize = BASE_BUFFER_SIZE;
-
 /**
  * xmlBufferCreate:
  *
diff --git a/uri.c b/uri.c
index d386e1d..dd77210 100644
--- a/uri.c
+++ b/uri.c
@@ -14,6 +14,7 @@
 
 #include <libxml/xmlmemory.h>
 #include <libxml/uri.h>
+#include <libxml/globals.h>
 #include <libxml/xmlerror.h>
 
 /************************************************************************
diff --git a/xlink.c b/xlink.c
index cb37302..2631961 100644
--- a/xlink.c
+++ b/xlink.c
@@ -45,8 +45,8 @@
  *								*
  ****************************************************************/
  
-xlinkHandlerPtr xlinkDefaultHandler = NULL;
-xlinkNodeDetectFunc	xlinkDefaultDetect = NULL;
+static xlinkHandlerPtr xlinkDefaultHandler = NULL;
+static xlinkNodeDetectFunc	xlinkDefaultDetect = NULL;
 
 /**
  * xlinkGetDefaultHandler:
diff --git a/xmlmemory.c b/xmlmemory.c
index f689236..aa82c4e 100644
--- a/xmlmemory.c
+++ b/xmlmemory.c
@@ -26,6 +26,7 @@
 
 
 #include <libxml/xmlmemory.h>
+#include <libxml/globals.h>
 #include <libxml/xmlerror.h>
 
 void xmlMallocBreakpoint(void);
@@ -657,18 +658,6 @@
  *								*
  ****************************************************************/
 
-#if defined(DEBUG_MEMORY_LOCATION) | defined(DEBUG_MEMORY)
-xmlFreeFunc xmlFree = (xmlFreeFunc) xmlMemFree;
-xmlMallocFunc xmlMalloc = (xmlMallocFunc) xmlMemMalloc;
-xmlReallocFunc xmlRealloc = (xmlReallocFunc) xmlMemRealloc;
-xmlStrdupFunc xmlMemStrdup = (xmlStrdupFunc) xmlMemoryStrdup;
-#else
-xmlFreeFunc xmlFree = (xmlFreeFunc) free;
-xmlMallocFunc xmlMalloc = (xmlMallocFunc) malloc;
-xmlReallocFunc xmlRealloc = (xmlReallocFunc) realloc;
-xmlStrdupFunc xmlMemStrdup = (xmlStrdupFunc) strdup;
-#endif
-
 /**
  * xmlInitMemory:
  *