stop generating wrong result file with * in name fixing the include bug

* Makefile.am: stop generating wrong result file with * in name
* relaxng.c: fixing the include bug raised by Sebastian Rahtz
* result/relaxng/demo* test/relaxng/demo: added the tests from
  Sebastian reproducing the problem.
Daniel
diff --git a/ChangeLog b/ChangeLog
index 448ff95..718a65b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,8 +1,14 @@
+Mon Mar 10 14:10:47 CET 2003 Daniel Veillard <daniel@veillard.com>
+
+	* Makefile.am: stop generating wrong result file with * in name
+	* relaxng.c: fixing the include bug raised by Sebastian Rahtz
+	* result/relaxng/demo* test/relaxng/demo: added the tests from
+	  Sebastian reproducing the problem.
+
 Sun Mar  9 18:02:31 MST 2003 John Fleck <jfleck@inkstain.net>
 
-	* doc/xmllint.1
-	regenerating man page from xmllint.xml to pick up Aleksey's
-	change
+	* doc/xmllint.1: regenerating man page from xmllint.xml to pick
+	  up Aleksey's change
 
 Sun Mar  9 13:53:16 2003  Aleksey Sanin  <aleksey@aleksey.com>
 
diff --git a/Makefile.am b/Makefile.am
index 94894ff..98774df 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -665,6 +665,7 @@
 	  name=`basename $$i | sed 's+_.*++'`; \
 	  sno=`basename $$i | sed 's+.*_\(.*\).xsd+\1+'`; \
 	  for j in $(srcdir)/test/schemas/"$$name"_*.xml ; do \
+	      if [ -f $$j ] ; then \
 	      xno=`basename $$j | sed 's+.*_\(.*\).xml+\1+'`; \
 	      if [ ! -f $(srcdir)/result/schemas/"$$name"_"$$sno"_"$$xno" ]; \
 	      then \
@@ -683,7 +684,7 @@
 		       err.$$name;\
 		  grep Unimplemented err.$$name; \
 	          rm res.$$name err.$$name ; \
-	       fi ; \
+	       fi ; fi ;\
 	  done; done)
 
 Relaxtests: xmllint$(EXEEXT)
diff --git a/relaxng.c b/relaxng.c
index f8e0b9f..b1d4d47 100644
--- a/relaxng.c
+++ b/relaxng.c
@@ -53,6 +53,7 @@
 /* #define DEBUG_VALID 1 */
 /* #define DEBUG_INTERLEAVE 1 */
 /* #define DEBUG_LIST 1 */
+/* #define DEBUG_INCLUDE */
 
 #define UNBOUNDED (1 << 30)
 #define TODO 								\
@@ -967,6 +968,72 @@
 }
 
 /**
+ * xmlRelaxNGRemoveRedefine:
+ * @ctxt: the parser context
+ * @URL:  the normalized URL
+ * @target:  the included target
+ * @name:  the define name to eliminate
+ *
+ * Applies the elimination algorithm of 4.7
+ *
+ * Returns 0 in case of error, 1 in case of success.
+ */
+static int
+xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,
+			 const xmlChar *URL ATTRIBUTE_UNUSED,
+	                 xmlNodePtr target, const xmlChar *name) {
+    int found = 0;
+    xmlNodePtr tmp, tmp2;
+    xmlChar *name2;
+
+#ifdef DEBUG_INCLUDE
+    xmlGenericError(xmlGenericErrorContext,
+		"Elimination of <include> %s from %s\n", name, URL);
+#endif
+    tmp = target;
+    while (tmp != NULL) {
+	tmp2 = tmp->next;
+	if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) {
+	    found = 1;
+	    xmlUnlinkNode(tmp);
+	    xmlFreeNode(tmp);
+	} else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) {
+	    name2 = xmlGetProp(tmp, BAD_CAST "name");
+	    xmlRelaxNGNormExtSpace(name2);
+	    if (name2 != NULL) {
+		if (xmlStrEqual(name, name2)) {
+		    found = 1;
+		    xmlUnlinkNode(tmp);
+		    xmlFreeNode(tmp);
+		}
+		xmlFree(name2);
+	    }
+	} else if (IS_RELAXNG(tmp, "include")) {
+	    xmlChar *href = NULL;
+	    xmlRelaxNGDocumentPtr inc = tmp->_private;
+
+	    if ((inc != NULL) && (inc->doc != NULL) &&
+		(inc->doc->children != NULL)) {
+
+		if (xmlStrEqual(inc->doc->children->name, BAD_CAST "grammar")) {
+#ifdef DEBUG_INCLUDE
+		    href = xmlGetProp(tmp, BAD_CAST "href");
+#endif
+		    if (xmlRelaxNGRemoveRedefine(ctxt, href,
+				inc->doc->children->children, name) == 1) {
+			found = 1;
+		    }
+		    if (href != NULL)
+			xmlFree(href);
+		}
+	    }
+	}
+	tmp = tmp2;
+    }
+    return(found);
+}
+
+/**
  * xmlRelaxNGLoadInclude:
  * @ctxt: the parser context
  * @URL:  the normalized URL
@@ -985,7 +1052,12 @@
     xmlRelaxNGIncludePtr ret = NULL;
     xmlDocPtr doc;
     int i;
-    xmlNodePtr root, tmp, tmp2, cur;
+    xmlNodePtr root, cur;
+
+#ifdef DEBUG_INCLUDE
+    xmlGenericError(xmlGenericErrorContext,
+		    "xmlRelaxNGLoadInclude(%s)\n", URL);
+#endif
 
     /*
      * check against recursion in the stack
@@ -1013,6 +1085,11 @@
 	return (NULL);
     }
 
+#ifdef DEBUG_INCLUDE
+    xmlGenericError(xmlGenericErrorContext,
+		    "Parsed %s Okay\n", URL);
+#endif
+
     /*
      * Allocate the document structures and register it first.
      */
@@ -1052,6 +1129,11 @@
      * Some preprocessing of the document content, this include recursing
      * in the include stack.
      */
+#ifdef DEBUG_INCLUDE
+    xmlGenericError(xmlGenericErrorContext,
+		    "cleanup of %s\n", URL);
+#endif
+
     doc = xmlRelaxNGCleanupDoc(ctxt, doc);
     if (doc == NULL) {
 	ctxt->inc = NULL;
@@ -1063,6 +1145,10 @@
      */
     xmlRelaxNGIncludePop(ctxt);
 
+#ifdef DEBUG_INCLUDE
+    xmlGenericError(xmlGenericErrorContext,
+		    "Checking of %s\n", URL);
+#endif
     /*
      * Check that the top element is a grammar
      */
@@ -1091,16 +1177,7 @@
 	if (IS_RELAXNG(cur, "start")) {
 	    int found = 0;
 
-	    tmp = root->children;
-	    while (tmp != NULL) {
-		tmp2 = tmp->next;
-		if (IS_RELAXNG(tmp, "start")) {
-		    found = 1;
-		    xmlUnlinkNode(tmp);
-		    xmlFreeNode(tmp);
-		}
-		tmp = tmp2;
-	    }
+	    found = xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
 	    if (!found) {
 		if (ctxt->error != NULL)
 		    ctxt->error(ctxt->userData,
@@ -1109,7 +1186,7 @@
 		ctxt->nbErrors++;
 	    }
 	} else if (IS_RELAXNG(cur, "define")) {
-	    xmlChar *name, *name2;
+	    xmlChar *name;
 
 	    name = xmlGetProp(cur, BAD_CAST "name");
 	    if (name == NULL) {
@@ -1119,26 +1196,11 @@
 				URL);
 		ctxt->nbErrors++;
 	    } else {
-		int found = 0;
+		int found;
 
 		xmlRelaxNGNormExtSpace(name);
-		tmp = root->children;
-		while (tmp != NULL) {
-		    tmp2 = tmp->next;
-		    if (IS_RELAXNG(tmp, "define")) {
-			name2 = xmlGetProp(tmp, BAD_CAST "name");
-			xmlRelaxNGNormExtSpace(name2);
-			if (name2 != NULL) {
-			    if (xmlStrEqual(name, name2)) {
-				found = 1;
-				xmlUnlinkNode(tmp);
-				xmlFreeNode(tmp);
-			    }
-			    xmlFree(name2);
-			}
-		    }
-		    tmp = tmp2;
-		}
+		found = xmlRelaxNGRemoveRedefine(ctxt, URL,
+			                         root->children, name);
 		if (!found) {
 		    if (ctxt->error != NULL)
 			ctxt->error(ctxt->userData,
diff --git a/result/relaxng/demo2_err b/result/relaxng/demo2_err
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/result/relaxng/demo2_err
diff --git a/result/relaxng/demo2_valid b/result/relaxng/demo2_valid
new file mode 100644
index 0000000..9bdaeb9
--- /dev/null
+++ b/result/relaxng/demo2_valid
@@ -0,0 +1 @@
+./test/relaxng/demo2.rng validates
diff --git a/result/relaxng/demo3_err b/result/relaxng/demo3_err
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/result/relaxng/demo3_err
diff --git a/result/relaxng/demo3_valid b/result/relaxng/demo3_valid
new file mode 100644
index 0000000..ccced98
--- /dev/null
+++ b/result/relaxng/demo3_valid
@@ -0,0 +1 @@
+./test/relaxng/demo3.rng validates
diff --git a/result/relaxng/demo_err b/result/relaxng/demo_err
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/result/relaxng/demo_err
diff --git a/result/relaxng/demo_valid b/result/relaxng/demo_valid
new file mode 100644
index 0000000..4f397f8
--- /dev/null
+++ b/result/relaxng/demo_valid
@@ -0,0 +1 @@
+./test/relaxng/demo.rng validates
diff --git a/test/relaxng/demo.rng b/test/relaxng/demo.rng
new file mode 100644
index 0000000..1a7f17c
--- /dev/null
+++ b/test/relaxng/demo.rng
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="iso-8859-1"?>
+<grammar xmlns="http://relaxng.org/ns/structure/1.0"
+ datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
+
+<include href="demo2.rng">
+<define name="TEI.prose"><ref name="INCLUDE"/></define>
+</include>
+</grammar>
+
+
+
diff --git a/test/relaxng/demo.xml b/test/relaxng/demo.xml
new file mode 100644
index 0000000..6b0cac1
--- /dev/null
+++ b/test/relaxng/demo.xml
@@ -0,0 +1 @@
+<TEI.2>hello</TEI.2>
\ No newline at end of file
diff --git a/test/relaxng/demo2.rng b/test/relaxng/demo2.rng
new file mode 100644
index 0000000..f528f73
--- /dev/null
+++ b/test/relaxng/demo2.rng
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<grammar xmlns="http://relaxng.org/ns/structure/1.0" xmlns:t="http://www.thaiopensource.com/ns/annotations" xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
+
+   <start>
+         <ref name="TEI.2"/>
+   </start>
+   <define name="IGNORE">
+      <notAllowed/>
+   </define>
+   <define name="INCLUDE">
+      <empty/>
+   </define>
+
+
+  <include href="demo3.rng"/>
+
+   <define name="TEI.2">
+      <element name="TEI.2">
+         <text/>
+      </element>
+   </define>
+
+</grammar>
\ No newline at end of file
diff --git a/test/relaxng/demo3.rng b/test/relaxng/demo3.rng
new file mode 100644
index 0000000..73e1eb6
--- /dev/null
+++ b/test/relaxng/demo3.rng
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<grammar xmlns="http://relaxng.org/ns/structure/1.0" xmlns:t="http://www.thaiopensource.com/ns/annotations" xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
+
+   <define name="TEI.prose" combine="interleave">
+      <ref name="IGNORE"/>
+   </define>
+
+</grammar>
\ No newline at end of file