2002-07-01 11:13:02 -04:00
|
|
|
/* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
|
|
|
|
See the file COPYING for copying permission.
|
2000-04-21 00:20:31 -04:00
|
|
|
*/
|
1998-11-25 03:32:06 -05:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
2002-06-12 22:52:09 -04:00
|
|
|
#ifdef COMPILED_FROM_DSP
|
|
|
|
#include "winconfig.h"
|
|
|
|
#else
|
2003-10-16 00:51:11 -04:00
|
|
|
#ifdef HAVE_EXPAT_CONFIG_H
|
2002-06-12 22:52:09 -04:00
|
|
|
#include "expat_config.h"
|
|
|
|
#endif
|
2003-10-16 00:51:11 -04:00
|
|
|
#endif
|
2000-09-18 12:26:23 -04:00
|
|
|
#include "expat.h"
|
1998-11-25 03:32:06 -05:00
|
|
|
#include "xmlfile.h"
|
|
|
|
#include "xmltchar.h"
|
|
|
|
#include "filemap.h"
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
2002-06-12 22:52:09 -04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
1998-11-25 03:32:06 -05:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef O_BINARY
|
|
|
|
#ifdef _O_BINARY
|
|
|
|
#define O_BINARY _O_BINARY
|
|
|
|
#else
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
#define READ_SIZE 16
|
|
|
|
#else
|
|
|
|
#define READ_SIZE (1024*8)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
XML_Parser parser;
|
|
|
|
int *retPtr;
|
|
|
|
} PROCESS_ARGS;
|
|
|
|
|
2002-04-19 17:41:54 -04:00
|
|
|
static void
|
|
|
|
reportError(XML_Parser parser, const XML_Char *filename)
|
1998-11-25 03:32:06 -05:00
|
|
|
{
|
|
|
|
int code = XML_GetErrorCode(parser);
|
|
|
|
const XML_Char *message = XML_ErrorString(code);
|
|
|
|
if (message)
|
2000-05-09 23:13:58 -04:00
|
|
|
ftprintf(stdout, T("%s:%d:%d: %s\n"),
|
2002-07-01 11:13:02 -04:00
|
|
|
filename,
|
|
|
|
XML_GetErrorLineNumber(parser),
|
|
|
|
XML_GetErrorColumnNumber(parser),
|
|
|
|
message);
|
1998-11-25 03:32:06 -05:00
|
|
|
else
|
|
|
|
ftprintf(stderr, T("%s: (unknown message %d)\n"), filename, code);
|
|
|
|
}
|
|
|
|
|
2002-04-19 17:41:54 -04:00
|
|
|
static void
|
|
|
|
processFile(const void *data, size_t size,
|
2002-07-01 11:13:02 -04:00
|
|
|
const XML_Char *filename, void *args)
|
1998-11-25 03:32:06 -05:00
|
|
|
{
|
|
|
|
XML_Parser parser = ((PROCESS_ARGS *)args)->parser;
|
|
|
|
int *retPtr = ((PROCESS_ARGS *)args)->retPtr;
|
2002-08-02 15:40:09 -04:00
|
|
|
if (XML_Parse(parser, data, size, 1) == XML_STATUS_ERROR) {
|
1998-11-25 03:32:06 -05:00
|
|
|
reportError(parser, filename);
|
|
|
|
*retPtr = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*retPtr = 1;
|
|
|
|
}
|
|
|
|
|
2000-05-09 23:13:58 -04:00
|
|
|
#ifdef WIN32
|
|
|
|
|
2002-04-19 17:41:54 -04:00
|
|
|
static int
|
|
|
|
isAsciiLetter(XML_Char c)
|
1998-11-25 03:32:06 -05:00
|
|
|
{
|
|
|
|
return (T('a') <= c && c <= T('z')) || (T('A') <= c && c <= T('Z'));
|
|
|
|
}
|
|
|
|
|
2000-05-09 23:13:58 -04:00
|
|
|
#endif /* WIN32 */
|
|
|
|
|
2002-04-19 17:41:54 -04:00
|
|
|
static const XML_Char *
|
|
|
|
resolveSystemId(const XML_Char *base, const XML_Char *systemId,
|
2002-07-01 11:13:02 -04:00
|
|
|
XML_Char **toFree)
|
1998-11-25 03:32:06 -05:00
|
|
|
{
|
|
|
|
XML_Char *s;
|
|
|
|
*toFree = 0;
|
|
|
|
if (!base
|
|
|
|
|| *systemId == T('/')
|
|
|
|
#ifdef WIN32
|
|
|
|
|| *systemId == T('\\')
|
|
|
|
|| (isAsciiLetter(systemId[0]) && systemId[1] == T(':'))
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
return systemId;
|
2002-04-19 17:41:54 -04:00
|
|
|
*toFree = (XML_Char *)malloc((tcslen(base) + tcslen(systemId) + 2)
|
2002-07-01 11:13:02 -04:00
|
|
|
* sizeof(XML_Char));
|
1998-11-25 03:32:06 -05:00
|
|
|
if (!*toFree)
|
|
|
|
return systemId;
|
|
|
|
tcscpy(*toFree, base);
|
|
|
|
s = *toFree;
|
|
|
|
if (tcsrchr(s, T('/')))
|
|
|
|
s = tcsrchr(s, T('/')) + 1;
|
|
|
|
#ifdef WIN32
|
|
|
|
if (tcsrchr(s, T('\\')))
|
|
|
|
s = tcsrchr(s, T('\\')) + 1;
|
|
|
|
#endif
|
|
|
|
tcscpy(s, systemId);
|
|
|
|
return *toFree;
|
|
|
|
}
|
|
|
|
|
2002-04-19 17:41:54 -04:00
|
|
|
static int
|
|
|
|
externalEntityRefFilemap(XML_Parser parser,
|
2002-07-01 11:13:02 -04:00
|
|
|
const XML_Char *context,
|
|
|
|
const XML_Char *base,
|
|
|
|
const XML_Char *systemId,
|
|
|
|
const XML_Char *publicId)
|
1998-11-25 03:32:06 -05:00
|
|
|
{
|
|
|
|
int result;
|
|
|
|
XML_Char *s;
|
|
|
|
const XML_Char *filename;
|
|
|
|
XML_Parser entParser = XML_ExternalEntityParserCreate(parser, context, 0);
|
|
|
|
PROCESS_ARGS args;
|
|
|
|
args.retPtr = &result;
|
|
|
|
args.parser = entParser;
|
|
|
|
filename = resolveSystemId(base, systemId, &s);
|
|
|
|
XML_SetBase(entParser, filename);
|
|
|
|
if (!filemap(filename, processFile, &args))
|
|
|
|
result = 0;
|
|
|
|
free(s);
|
|
|
|
XML_ParserFree(entParser);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2002-04-19 17:41:54 -04:00
|
|
|
static int
|
|
|
|
processStream(const XML_Char *filename, XML_Parser parser)
|
1998-11-25 03:32:06 -05:00
|
|
|
{
|
2002-04-19 17:41:54 -04:00
|
|
|
/* passing NULL for filename means read intput from stdin */
|
|
|
|
int fd = 0; /* 0 is the fileno for stdin */
|
|
|
|
|
|
|
|
if (filename != NULL) {
|
|
|
|
fd = topen(filename, O_BINARY|O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
tperror(filename);
|
|
|
|
return 0;
|
|
|
|
}
|
1998-11-25 03:32:06 -05:00
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
int nread;
|
|
|
|
char *buf = XML_GetBuffer(parser, READ_SIZE);
|
|
|
|
if (!buf) {
|
2002-04-19 17:41:54 -04:00
|
|
|
if (filename != NULL)
|
2002-07-01 11:13:02 -04:00
|
|
|
close(fd);
|
2002-04-19 17:41:54 -04:00
|
|
|
ftprintf(stderr, T("%s: out of memory\n"),
|
2002-07-01 11:13:02 -04:00
|
|
|
filename != NULL ? filename : "xmlwf");
|
1998-11-25 03:32:06 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
nread = read(fd, buf, READ_SIZE);
|
|
|
|
if (nread < 0) {
|
2002-04-19 17:41:54 -04:00
|
|
|
tperror(filename != NULL ? filename : "STDIN");
|
|
|
|
if (filename != NULL)
|
2002-07-01 11:13:02 -04:00
|
|
|
close(fd);
|
1998-11-25 03:32:06 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2002-08-02 15:40:09 -04:00
|
|
|
if (XML_ParseBuffer(parser, nread, nread == 0) == XML_STATUS_ERROR) {
|
2002-04-19 17:41:54 -04:00
|
|
|
reportError(parser, filename != NULL ? filename : "STDIN");
|
|
|
|
if (filename != NULL)
|
2002-07-01 11:13:02 -04:00
|
|
|
close(fd);
|
1998-11-25 03:32:06 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (nread == 0) {
|
2002-04-19 17:41:54 -04:00
|
|
|
if (filename != NULL)
|
2002-07-01 11:13:02 -04:00
|
|
|
close(fd);
|
1998-11-25 03:32:06 -05:00
|
|
|
break;;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-04-19 17:41:54 -04:00
|
|
|
static int
|
|
|
|
externalEntityRefStream(XML_Parser parser,
|
2002-07-01 11:13:02 -04:00
|
|
|
const XML_Char *context,
|
|
|
|
const XML_Char *base,
|
|
|
|
const XML_Char *systemId,
|
|
|
|
const XML_Char *publicId)
|
1998-11-25 03:32:06 -05:00
|
|
|
{
|
|
|
|
XML_Char *s;
|
|
|
|
const XML_Char *filename;
|
|
|
|
int ret;
|
|
|
|
XML_Parser entParser = XML_ExternalEntityParserCreate(parser, context, 0);
|
|
|
|
filename = resolveSystemId(base, systemId, &s);
|
|
|
|
XML_SetBase(entParser, filename);
|
|
|
|
ret = processStream(filename, entParser);
|
|
|
|
free(s);
|
|
|
|
XML_ParserFree(entParser);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2002-04-19 17:41:54 -04:00
|
|
|
int
|
|
|
|
XML_ProcessFile(XML_Parser parser,
|
2002-07-01 11:13:02 -04:00
|
|
|
const XML_Char *filename,
|
|
|
|
unsigned flags)
|
1998-11-25 03:32:06 -05:00
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
|
|
|
if (!XML_SetBase(parser, filename)) {
|
|
|
|
ftprintf(stderr, T("%s: out of memory"), filename);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & XML_EXTERNAL_ENTITIES)
|
|
|
|
XML_SetExternalEntityRefHandler(parser,
|
2002-07-01 11:13:02 -04:00
|
|
|
(flags & XML_MAP_FILE)
|
|
|
|
? externalEntityRefFilemap
|
|
|
|
: externalEntityRefStream);
|
1998-11-25 03:32:06 -05:00
|
|
|
if (flags & XML_MAP_FILE) {
|
|
|
|
PROCESS_ARGS args;
|
|
|
|
args.retPtr = &result;
|
|
|
|
args.parser = parser;
|
|
|
|
if (!filemap(filename, processFile, &args))
|
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result = processStream(filename, parser);
|
|
|
|
return result;
|
|
|
|
}
|