summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Veillard <veillard@src.gnome.org>2001-10-08 15:01:59 +0000
committerDaniel Veillard <veillard@src.gnome.org>2001-10-08 15:01:59 +0000
commit82d753388039e0677a9ac13ca0fda19abc5e8f66 (patch)
tree01833e0be631d909b8ec7023b8354652de00f63d
parent0e229933d054d7e264d48d5146a0b21fd6813ba7 (diff)
downloadlibxml2-82d753388039e0677a9ac13ca0fda19abc5e8f66.tar.gz
adding SGML super catalog support adding one API and one flag --sgml to
* include/libxml/catalog.h catalog.c xmlcatalog.c: adding SGML super catalog support adding one API and one flag --sgml to xmlcatalog Daniel
-rw-r--r--ChangeLog6
-rw-r--r--catalog.c126
-rw-r--r--include/libxml/catalog.h1
-rw-r--r--xmlcatalog.c77
4 files changed, 177 insertions, 33 deletions
diff --git a/ChangeLog b/ChangeLog
index 35a3a6b8..4f9be55f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Mon Oct 8 17:00:16 CEST 2001 Daniel Veillard <daniel@veillard.com>
+
+ * include/libxml/catalog.h catalog.c xmlcatalog.c: adding SGML
+ super catalog support adding one API and one flag --sgml to
+ xmlcatalog
+
Sun Oct 7 16:43:57 MDT 2001 John Fleck <jfleck@inkstain.net>
* doc/xmlcatalog_man.xml, xmlcatalog.1
diff --git a/catalog.c b/catalog.c
index d08bd44e..eaaf0754 100644
--- a/catalog.c
+++ b/catalog.c
@@ -1719,7 +1719,7 @@ xmlGetSGMLCatalogEntryType(const xmlChar *name) {
}
static int
-xmlParseSGMLCatalog(const xmlChar *value, const char *file) {
+xmlParseSGMLCatalog(const xmlChar *value, const char *file, int super) {
const xmlChar *cur = value;
xmlChar *base = NULL;
int res;
@@ -1874,12 +1874,23 @@ xmlParseSGMLCatalog(const xmlChar *value, const char *file) {
}
} else if (type == SGML_CATA_CATALOG) {
- xmlChar *filename;
+ if (super) {
+ xmlCatalogEntryPtr entry;
- filename = xmlBuildURI(sysid, base);
- if (filename != NULL) {
- xmlLoadCatalog((const char *)filename);
- xmlFree(filename);
+ entry = xmlNewCatalogEntry(type, sysid, NULL,
+ XML_CATA_PREFER_NONE);
+ res = xmlHashAddEntry(xmlDefaultCatalog, sysid, entry);
+ if (res < 0) {
+ xmlFreeCatalogEntry(entry);
+ }
+ } else {
+ xmlChar *filename;
+
+ filename = xmlBuildURI(sysid, base);
+ if (filename != NULL) {
+ xmlLoadCatalog((const char *)filename);
+ xmlFree(filename);
+ }
}
}
/*
@@ -2005,6 +2016,91 @@ xmlInitializeCatalog(void) {
}
/**
+ * xmlLoadSGMLSuperCatalog:
+ * @filename: a file path
+ *
+ * Load an SGML super catalog. It won't expand CATALOG or DELEGATE
+ * references. This is only needed for manipulating SGML Super Catalogs
+ * like adding and removing CATALOG or DELEGATE entries.
+ *
+ * Returns 0 in case of success -1 in case of error
+ */
+int
+xmlLoadSGMLSuperCatalog(const char *filename)
+{
+#ifdef HAVE_STAT
+ int fd;
+#else
+ FILE *fd;
+#endif
+ int len, ret;
+ long size;
+
+#ifdef HAVE_STAT
+ struct stat info;
+#endif
+ xmlChar *content;
+
+ if (filename == NULL)
+ return (-1);
+
+ if (xmlDefaultCatalog == NULL)
+ xmlDefaultCatalog = xmlHashCreate(20);
+ if (xmlDefaultCatalog == NULL)
+ return (-1);
+
+#ifdef HAVE_STAT
+ if (stat(filename, &info) < 0)
+ return (-1);
+#endif
+
+#ifdef HAVE_STAT
+ if ((fd = open(filename, O_RDONLY)) < 0) {
+#else
+ if ((fd = fopen(filename, "rb")) == NULL) {
+#endif
+ catalNr--;
+ return (-1);
+ }
+#ifdef HAVE_STAT
+ size = info.st_size;
+#else
+ if (fseek(fd, 0, SEEK_END) || (size = ftell(fd)) == EOF || fseek(fd, 0, SEEK_SET)) { /* File operations denied? ok, just close and return failure */
+ fclose(fd);
+ return (-1);
+ }
+#endif
+ content = xmlMalloc(size + 10);
+ if (content == NULL) {
+ xmlGenericError(xmlGenericErrorContext,
+ "realloc of %d byte failed\n", size + 10);
+ catalNr--;
+ return (-1);
+ }
+#ifdef HAVE_STAT
+ len = read(fd, content, size);
+#else
+ len = fread(content, 1, size, fd);
+#endif
+ if (len < 0) {
+ xmlFree(content);
+ catalNr--;
+ return (-1);
+ }
+ content[len] = 0;
+#ifdef HAVE_STAT
+ close(fd);
+#else
+ fclose(fd);
+#endif
+
+ ret = xmlParseSGMLCatalog(content, filename, 1);
+
+ xmlFree(content);
+ return (ret);
+}
+
+/**
* xmlLoadCatalog:
* @filename: a file path
*
@@ -2113,7 +2209,7 @@ xmlLoadCatalog(const char *filename)
if ((content[0] == ' ') || (content[0] == '-') ||
((content[0] >= 'A') && (content[0] <= 'Z')) ||
((content[0] >= 'a') && (content[0] <= 'z')))
- ret = xmlParseSGMLCatalog(content, filename);
+ ret = xmlParseSGMLCatalog(content, filename, 0);
else {
xmlCatalogEntryPtr catal, tmp;
@@ -2465,6 +2561,15 @@ int
xmlCatalogAdd(const xmlChar *type, const xmlChar *orig, const xmlChar *replace) {
int res = -1;
+ if ((xmlDefaultCatalog != NULL) &&
+ (xmlStrEqual(type, BAD_CAST "sgmlcatalog"))) {
+ xmlCatalogEntryPtr entry;
+
+ entry = xmlNewCatalogEntry(SGML_CATA_CATALOG, replace, NULL,
+ XML_CATA_PREFER_NONE);
+ res = xmlHashAddEntry(xmlDefaultCatalog, replace, entry);
+ return(0);
+ }
if ((xmlDefaultXMLCatalogList == NULL) &&
(xmlStrEqual(type, BAD_CAST "catalog"))) {
xmlDefaultXMLCatalogList = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
@@ -2497,7 +2602,7 @@ xmlCatalogAdd(const xmlChar *type, const xmlChar *orig, const xmlChar *replace)
*
* Remove an entry from the catalog
*
- * Returns 0 if successful, -1 otherwise
+ * Returns the number of entries removed if successful, -1 otherwise
*/
int
xmlCatalogRemove(const xmlChar *value) {
@@ -2509,7 +2614,10 @@ xmlCatalogRemove(const xmlChar *value) {
if (xmlDefaultXMLCatalogList != NULL) {
res = xmlDelXMLCatalog(xmlDefaultXMLCatalogList, value);
} else if (xmlDefaultCatalog != NULL) {
- TODO
+ res = xmlHashRemoveEntry(xmlDefaultCatalog, value,
+ (xmlHashDeallocator) xmlFreeCatalogEntry);
+ if (res == 0)
+ res = 1;
}
return(res);
}
diff --git a/include/libxml/catalog.h b/include/libxml/catalog.h
index 4733da59..54685cde 100644
--- a/include/libxml/catalog.h
+++ b/include/libxml/catalog.h
@@ -56,6 +56,7 @@ typedef enum {
void xmlInitializeCatalog (void);
int xmlLoadCatalog (const char *filename);
+int xmlLoadSGMLSuperCatalog (const char *filename);
void xmlLoadCatalogs (const char *paths);
void xmlCatalogCleanup (void);
void xmlCatalogDump (FILE *out);
diff --git a/xmlcatalog.c b/xmlcatalog.c
index b293ec35..80635450 100644
--- a/xmlcatalog.c
+++ b/xmlcatalog.c
@@ -29,6 +29,7 @@
#include <libxml/parser.h>
static int shell = 0;
+static int sgml = 0;
static int noout = 0;
static int create = 0;
static int add = 0;
@@ -198,17 +199,26 @@ static void usershell(void) {
}
}
} else if (!strcmp(command, "add")) {
- if ((nbargs != 3) && (nbargs != 2)) {
- printf("add requires 2 or 3 arguments\n");
+ if (sgml) {
+ if (nbargs != 1) {
+ printf("add requires 1 argument\n");
+ } else {
+ ret = xmlCatalogAdd(BAD_CAST "sgmlcatalog", NULL,
+ BAD_CAST argv[0]);
+ }
} else {
- if (argv[2] == NULL)
- ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
- BAD_CAST argv[1]);
- else
- ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
- BAD_CAST argv[2]);
- if (ret != 0)
- printf("add command failed\n");
+ if ((nbargs != 3) && (nbargs != 2)) {
+ printf("add requires 2 or 3 arguments\n");
+ } else {
+ if (argv[2] == NULL)
+ ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
+ BAD_CAST argv[1]);
+ else
+ ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
+ BAD_CAST argv[2]);
+ if (ret != 0)
+ printf("add command failed\n");
+ }
}
} else if (!strcmp(command, "del")) {
if (nbargs != 1) {
@@ -280,6 +290,7 @@ static void usershell(void) {
static void usage(const char *name) {
printf("Usage : %s [options] catalogfile entities...\n", name);
printf("\tParse the catalog file and query it for the entities\n");
+ printf("\t--sgml : handle an SGML Super catalog\n");
printf("\t--shell : run a shell allowing interactive queries\n");
printf("\t--create : create a new catalog\n");
printf("\t--add 'type' 'orig' 'replace' : add an entry\n");
@@ -317,6 +328,9 @@ int main(int argc, char **argv) {
(!strcmp(argv[i], "--shell"))) {
shell++;
noout = 1;
+ } else if ((!strcmp(argv[i], "-sgml")) ||
+ (!strcmp(argv[i], "--sgml"))) {
+ sgml++;
} else if ((!strcmp(argv[i], "-create")) ||
(!strcmp(argv[i], "--create"))) {
create++;
@@ -325,7 +339,10 @@ int main(int argc, char **argv) {
convert++;
} else if ((!strcmp(argv[i], "-add")) ||
(!strcmp(argv[i], "--add"))) {
- i += 3;
+ if (sgml)
+ i += 1;
+ else
+ i += 3;
add++;
} else if ((!strcmp(argv[i], "-del")) ||
(!strcmp(argv[i], "--del"))) {
@@ -341,7 +358,10 @@ int main(int argc, char **argv) {
for (i = 1; i < argc; i++) {
if ((!strcmp(argv[i], "-add")) ||
(!strcmp(argv[i], "--add"))) {
- i += 3;
+ if (sgml)
+ i += 1;
+ else
+ i += 3;
continue;
} else if ((!strcmp(argv[i], "-del")) ||
(!strcmp(argv[i], "--del"))) {
@@ -350,8 +370,11 @@ int main(int argc, char **argv) {
} else if (argv[i][0] == '-')
continue;
filename = argv[i];
- ret = xmlLoadCatalog(argv[i]);
- if ((ret < 0) && (create)) {
+ if (sgml)
+ ret = xmlLoadSGMLSuperCatalog(argv[i]);
+ else
+ ret = xmlLoadCatalog(argv[i]);
+ if ((!sgml) && (ret < 0) && (create)) {
xmlCatalogAdd(BAD_CAST "catalog", BAD_CAST argv[i], NULL);
}
break;
@@ -369,16 +392,22 @@ int main(int argc, char **argv) {
continue;
if ((!strcmp(argv[i], "-add")) ||
(!strcmp(argv[i], "--add"))) {
- if ((argv[i + 3] == NULL) || (argv[i + 3][0] == 0))
- ret = xmlCatalogAdd(BAD_CAST argv[i + 1], NULL,
- BAD_CAST argv[i + 2]);
- else
- ret = xmlCatalogAdd(BAD_CAST argv[i + 1],
- BAD_CAST argv[i + 2],
- BAD_CAST argv[i + 3]);
- if (ret != 0)
- printf("add command failed\n");
- i += 3;
+ if (sgml) {
+ ret = xmlCatalogAdd(BAD_CAST "sgmlcatalog", NULL,
+ BAD_CAST argv[i + 1]);
+ i += 1;
+ } else {
+ if ((argv[i + 3] == NULL) || (argv[i + 3][0] == 0))
+ ret = xmlCatalogAdd(BAD_CAST argv[i + 1], NULL,
+ BAD_CAST argv[i + 2]);
+ else
+ ret = xmlCatalogAdd(BAD_CAST argv[i + 1],
+ BAD_CAST argv[i + 2],
+ BAD_CAST argv[i + 3]);
+ if (ret != 0)
+ printf("add command failed\n");
+ i += 3;
+ }
} else if ((!strcmp(argv[i], "-del")) ||
(!strcmp(argv[i], "--del"))) {
ret = xmlCatalogRemove(BAD_CAST argv[i + 1]);