libexpat/expat/xmlwf/xmlwf.c

868 lines
21 KiB
C
Raw Normal View History

/* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
See the file COPYING for copying permission.
*/
1998-04-05 11:11:29 -04:00
1997-11-11 00:52:10 -05:00
#include <stdio.h>
1998-02-04 00:25:27 -05:00
#include <stdlib.h>
1998-02-04 02:20:46 -05:00
#include <stddef.h>
1997-12-10 02:44:19 -05:00
#include <string.h>
1998-02-04 00:25:27 -05:00
#include "expat.h"
#include "codepage.h"
#include "internal.h" /* for UNUSED_P only */
#include "xmlfile.h"
#include "xmltchar.h"
1998-02-04 00:25:27 -05:00
#ifdef _MSC_VER
#include <crtdbg.h>
#endif
1997-11-11 00:52:10 -05:00
2007-06-03 11:00:09 -04:00
#if defined(__amigaos__) && defined(__USE_INLINE__)
2005-12-24 01:39:56 -05:00
#include <proto/expat.h>
#endif
/* This ensures proper sorting. */
#define NSSEP T('\001')
1998-08-26 05:10:50 -04:00
static void XMLCALL
characterData(void *userData, const XML_Char *s, int len)
1998-02-04 00:25:27 -05:00
{
FILE *fp = (FILE *)userData;
1998-02-04 00:25:27 -05:00
for (; len > 0; --len, ++s) {
switch (*s) {
1998-05-31 07:53:28 -04:00
case T('&'):
fputts(T("&amp;"), fp);
1998-02-04 00:25:27 -05:00
break;
1998-05-31 07:53:28 -04:00
case T('<'):
fputts(T("&lt;"), fp);
1998-02-04 00:25:27 -05:00
break;
1998-05-31 07:53:28 -04:00
case T('>'):
fputts(T("&gt;"), fp);
1998-02-04 00:25:27 -05:00
break;
#ifdef W3C14N
case 13:
fputts(T("&#xD;"), fp);
break;
#else
1998-05-31 07:53:28 -04:00
case T('"'):
fputts(T("&quot;"), fp);
1998-02-04 00:25:27 -05:00
break;
case 9:
case 10:
case 13:
1998-05-31 07:53:28 -04:00
ftprintf(fp, T("&#%d;"), *s);
1998-02-04 00:25:27 -05:00
break;
#endif
1998-02-04 00:25:27 -05:00
default:
1998-05-31 07:53:28 -04:00
puttc(*s, fp);
1998-02-04 00:25:27 -05:00
break;
}
}
}
static void
attributeValue(FILE *fp, const XML_Char *s)
{
puttc(T('='), fp);
puttc(T('"'), fp);
for (;;) {
switch (*s) {
case 0:
case NSSEP:
puttc(T('"'), fp);
return;
case T('&'):
fputts(T("&amp;"), fp);
break;
case T('<'):
fputts(T("&lt;"), fp);
break;
case T('"'):
fputts(T("&quot;"), fp);
break;
#ifdef W3C14N
case 9:
fputts(T("&#x9;"), fp);
break;
case 10:
fputts(T("&#xA;"), fp);
break;
case 13:
fputts(T("&#xD;"), fp);
break;
#else
case T('>'):
fputts(T("&gt;"), fp);
break;
case 9:
case 10:
case 13:
ftprintf(fp, T("&#%d;"), *s);
break;
#endif
default:
puttc(*s, fp);
break;
}
s++;
}
}
/* Lexicographically comparing UTF-8 encoded attribute values,
1998-02-04 00:25:27 -05:00
is equivalent to lexicographically comparing based on the character number. */
static int
attcmp(const void *att1, const void *att2)
1998-02-04 00:25:27 -05:00
{
1998-05-31 07:53:28 -04:00
return tcscmp(*(const XML_Char **)att1, *(const XML_Char **)att2);
1998-02-04 00:25:27 -05:00
}
static void XMLCALL
startElement(void *userData, const XML_Char *name, const XML_Char **atts)
1998-02-04 00:25:27 -05:00
{
int nAtts;
1998-05-31 07:53:28 -04:00
const XML_Char **p;
FILE *fp = (FILE *)userData;
1998-05-31 07:53:28 -04:00
puttc(T('<'), fp);
fputts(name, fp);
1998-02-04 00:25:27 -05:00
p = atts;
while (*p)
++p;
nAtts = (int)((p - atts) >> 1);
1998-02-04 00:25:27 -05:00
if (nAtts > 1)
1998-05-31 07:53:28 -04:00
qsort((void *)atts, nAtts, sizeof(XML_Char *) * 2, attcmp);
1998-02-04 00:25:27 -05:00
while (*atts) {
1998-05-31 07:53:28 -04:00
puttc(T(' '), fp);
fputts(*atts++, fp);
attributeValue(fp, *atts);
1998-02-04 00:25:27 -05:00
atts++;
}
1998-05-31 07:53:28 -04:00
puttc(T('>'), fp);
1998-02-04 00:25:27 -05:00
}
static void XMLCALL
endElement(void *userData, const XML_Char *name)
1998-02-04 00:25:27 -05:00
{
FILE *fp = (FILE *)userData;
1998-05-31 07:53:28 -04:00
puttc(T('<'), fp);
puttc(T('/'), fp);
fputts(name, fp);
puttc(T('>'), fp);
1998-02-04 00:25:27 -05:00
}
static int
nsattcmp(const void *p1, const void *p2)
{
const XML_Char *att1 = *(const XML_Char **)p1;
const XML_Char *att2 = *(const XML_Char **)p2;
int sep1 = (tcsrchr(att1, NSSEP) != 0);
int sep2 = (tcsrchr(att1, NSSEP) != 0);
if (sep1 != sep2)
return sep1 - sep2;
return tcscmp(att1, att2);
}
static void XMLCALL
startElementNS(void *userData, const XML_Char *name, const XML_Char **atts)
1998-08-26 05:10:50 -04:00
{
int nAtts;
int nsi;
const XML_Char **p;
FILE *fp = (FILE *)userData;
1998-08-26 05:10:50 -04:00
const XML_Char *sep;
puttc(T('<'), fp);
sep = tcsrchr(name, NSSEP);
if (sep) {
fputts(T("n1:"), fp);
1998-08-26 05:10:50 -04:00
fputts(sep + 1, fp);
fputts(T(" xmlns:n1"), fp);
attributeValue(fp, name);
nsi = 2;
1998-08-26 05:10:50 -04:00
}
else {
fputts(name, fp);
nsi = 1;
1998-08-26 05:10:50 -04:00
}
p = atts;
while (*p)
++p;
nAtts = (int)((p - atts) >> 1);
1998-08-26 05:10:50 -04:00
if (nAtts > 1)
qsort((void *)atts, nAtts, sizeof(XML_Char *) * 2, nsattcmp);
1998-08-26 05:10:50 -04:00
while (*atts) {
name = *atts++;
sep = tcsrchr(name, NSSEP);
puttc(T(' '), fp);
1998-08-26 05:10:50 -04:00
if (sep) {
ftprintf(fp, T("n%d:"), nsi);
fputts(sep + 1, fp);
1998-08-26 05:10:50 -04:00
}
else
fputts(name, fp);
attributeValue(fp, *atts);
if (sep) {
ftprintf(fp, T(" xmlns:n%d"), nsi++);
attributeValue(fp, name);
}
1998-08-26 05:10:50 -04:00
atts++;
}
puttc(T('>'), fp);
}
static void XMLCALL
endElementNS(void *userData, const XML_Char *name)
1998-08-26 05:10:50 -04:00
{
FILE *fp = (FILE *)userData;
1998-08-26 05:10:50 -04:00
const XML_Char *sep;
puttc(T('<'), fp);
puttc(T('/'), fp);
sep = tcsrchr(name, NSSEP);
if (sep) {
fputts(T("n1:"), fp);
1998-08-26 05:10:50 -04:00
fputts(sep + 1, fp);
}
else
fputts(name, fp);
puttc(T('>'), fp);
}
#ifndef W3C14N
static void XMLCALL
processingInstruction(void *userData, const XML_Char *target,
const XML_Char *data)
1998-02-04 00:25:27 -05:00
{
FILE *fp = (FILE *)userData;
1998-05-31 07:53:28 -04:00
puttc(T('<'), fp);
puttc(T('?'), fp);
fputts(target, fp);
puttc(T(' '), fp);
fputts(data, fp);
puttc(T('?'), fp);
puttc(T('>'), fp);
1998-02-04 00:25:27 -05:00
}
#endif /* not W3C14N */
static void XMLCALL
defaultCharacterData(void *userData, const XML_Char *UNUSED_P(s), int UNUSED_P(len))
1998-06-17 06:04:31 -04:00
{
XML_DefaultCurrent((XML_Parser) userData);
1998-06-21 02:53:13 -04:00
}
static void XMLCALL
defaultStartElement(void *userData, const XML_Char *UNUSED_P(name),
const XML_Char **UNUSED_P(atts))
1998-06-21 02:53:13 -04:00
{
XML_DefaultCurrent((XML_Parser) userData);
1998-06-21 02:53:13 -04:00
}
static void XMLCALL
defaultEndElement(void *userData, const XML_Char *UNUSED_P(name))
1998-06-21 02:53:13 -04:00
{
XML_DefaultCurrent((XML_Parser) userData);
1998-06-21 02:53:13 -04:00
}
static void XMLCALL
defaultProcessingInstruction(void *userData, const XML_Char *UNUSED_P(target),
const XML_Char *UNUSED_P(data))
1998-06-21 02:53:13 -04:00
{
XML_DefaultCurrent((XML_Parser) userData);
1998-06-21 02:53:13 -04:00
}
static void XMLCALL
nopCharacterData(void *UNUSED_P(userData), const XML_Char *UNUSED_P(s), int UNUSED_P(len))
1998-11-24 10:24:59 -05:00
{
}
static void XMLCALL
nopStartElement(void *UNUSED_P(userData), const XML_Char *UNUSED_P(name), const XML_Char **UNUSED_P(atts))
1998-11-24 10:24:59 -05:00
{
}
static void XMLCALL
nopEndElement(void *UNUSED_P(userData), const XML_Char *UNUSED_P(name))
1998-11-24 10:24:59 -05:00
{
}
static void XMLCALL
nopProcessingInstruction(void *UNUSED_P(userData), const XML_Char *UNUSED_P(target),
const XML_Char *UNUSED_P(data))
1998-11-24 10:24:59 -05:00
{
}
static void XMLCALL
markup(void *userData, const XML_Char *s, int len)
1998-06-21 02:53:13 -04:00
{
FILE *fp = (FILE *)XML_GetUserData((XML_Parser) userData);
1998-06-17 06:04:31 -04:00
for (; len > 0; --len, ++s)
1998-06-21 02:53:13 -04:00
puttc(*s, fp);
1998-06-17 06:04:31 -04:00
}
static void
metaLocation(XML_Parser parser)
{
1998-06-03 06:16:20 -04:00
const XML_Char *uri = XML_GetBase(parser);
if (uri)
ftprintf((FILE *)XML_GetUserData(parser), T(" uri=\"%s\""), uri);
ftprintf((FILE *)XML_GetUserData(parser),
T(" byte=\"%" XML_FMT_INT_MOD "d\" nbytes=\"%d\" \
line=\"%" XML_FMT_INT_MOD "u\" col=\"%" XML_FMT_INT_MOD "u\""),
XML_GetCurrentByteIndex(parser),
XML_GetCurrentByteCount(parser),
XML_GetCurrentLineNumber(parser),
XML_GetCurrentColumnNumber(parser));
}
static void
metaStartDocument(void *userData)
{
fputts(T("<document>\n"), (FILE *)XML_GetUserData((XML_Parser) userData));
}
static void
metaEndDocument(void *userData)
{
fputts(T("</document>\n"), (FILE *)XML_GetUserData((XML_Parser) userData));
}
static void XMLCALL
metaStartElement(void *userData, const XML_Char *name,
const XML_Char **atts)
{
XML_Parser parser = (XML_Parser) userData;
FILE *fp = (FILE *)XML_GetUserData(parser);
1999-03-25 22:53:05 -05:00
const XML_Char **specifiedAttsEnd
2000-04-14 06:53:33 -04:00
= atts + XML_GetSpecifiedAttributeCount(parser);
const XML_Char **idAttPtr;
int idAttIndex = XML_GetIdAttributeIndex(parser);
if (idAttIndex < 0)
idAttPtr = 0;
else
idAttPtr = atts + idAttIndex;
1998-06-03 06:16:20 -04:00
ftprintf(fp, T("<starttag name=\"%s\""), name);
metaLocation(parser);
if (*atts) {
fputts(T(">\n"), fp);
do {
1998-06-03 06:55:52 -04:00
ftprintf(fp, T("<attribute name=\"%s\" value=\""), atts[0]);
characterData(fp, atts[1], (int)tcslen(atts[1]));
1999-03-25 22:53:05 -05:00
if (atts >= specifiedAttsEnd)
fputts(T("\" defaulted=\"yes\"/>\n"), fp);
2000-04-14 06:53:33 -04:00
else if (atts == idAttPtr)
fputts(T("\" id=\"yes\"/>\n"), fp);
1999-03-25 22:53:05 -05:00
else
fputts(T("\"/>\n"), fp);
1998-06-03 06:16:20 -04:00
} while (*(atts += 2));
fputts(T("</starttag>\n"), fp);
}
else
1998-06-03 06:16:20 -04:00
fputts(T("/>\n"), fp);
}
static void XMLCALL
metaEndElement(void *userData, const XML_Char *name)
1998-06-03 06:16:20 -04:00
{
XML_Parser parser = (XML_Parser) userData;
FILE *fp = (FILE *)XML_GetUserData(parser);
1998-06-03 06:16:20 -04:00
ftprintf(fp, T("<endtag name=\"%s\""), name);
metaLocation(parser);
fputts(T("/>\n"), fp);
}
static void XMLCALL
metaProcessingInstruction(void *userData, const XML_Char *target,
const XML_Char *data)
1998-06-03 06:16:20 -04:00
{
XML_Parser parser = (XML_Parser) userData;
FILE *fp = (FILE *)XML_GetUserData(parser);
1998-06-03 06:16:20 -04:00
ftprintf(fp, T("<pi target=\"%s\" data=\""), target);
characterData(fp, data, (int)tcslen(data));
1998-06-03 06:16:20 -04:00
puttc(T('"'), fp);
metaLocation(parser);
fputts(T("/>\n"), fp);
}
static void XMLCALL
metaComment(void *userData, const XML_Char *data)
1998-09-24 00:12:12 -04:00
{
XML_Parser parser = (XML_Parser) userData;
FILE *fp = (FILE *)XML_GetUserData(parser);
1998-09-24 00:12:12 -04:00
fputts(T("<comment data=\""), fp);
characterData(fp, data, (int)tcslen(data));
1998-09-24 00:12:12 -04:00
puttc(T('"'), fp);
metaLocation(parser);
fputts(T("/>\n"), fp);
}
static void XMLCALL
metaStartCdataSection(void *userData)
1998-11-24 00:42:17 -05:00
{
XML_Parser parser = (XML_Parser) userData;
FILE *fp = (FILE *)XML_GetUserData(parser);
1998-11-24 00:42:17 -05:00
fputts(T("<startcdata"), fp);
metaLocation(parser);
fputts(T("/>\n"), fp);
}
static void XMLCALL
metaEndCdataSection(void *userData)
1998-11-24 00:42:17 -05:00
{
XML_Parser parser = (XML_Parser) userData;
FILE *fp = (FILE *)XML_GetUserData(parser);
1998-11-24 00:42:17 -05:00
fputts(T("<endcdata"), fp);
metaLocation(parser);
fputts(T("/>\n"), fp);
}
static void XMLCALL
metaCharacterData(void *userData, const XML_Char *s, int len)
{
XML_Parser parser = (XML_Parser) userData;
FILE *fp = (FILE *)XML_GetUserData(parser);
1998-06-03 06:16:20 -04:00
fputts(T("<chars str=\""), fp);
characterData(fp, s, len);
puttc(T('"'), fp);
metaLocation(parser);
fputts(T("/>\n"), fp);
}
static void XMLCALL
metaStartDoctypeDecl(void *userData,
const XML_Char *doctypeName,
const XML_Char *UNUSED_P(sysid),
const XML_Char *UNUSED_P(pubid),
int UNUSED_P(has_internal_subset))
1999-07-28 05:34:18 -04:00
{
XML_Parser parser = (XML_Parser) userData;
FILE *fp = (FILE *)XML_GetUserData(parser);
1999-07-28 05:34:18 -04:00
ftprintf(fp, T("<startdoctype name=\"%s\""), doctypeName);
metaLocation(parser);
fputts(T("/>\n"), fp);
}
static void XMLCALL
metaEndDoctypeDecl(void *userData)
1999-07-28 05:34:18 -04:00
{
XML_Parser parser = (XML_Parser) userData;
FILE *fp = (FILE *)XML_GetUserData(parser);
1999-07-28 05:34:18 -04:00
fputts(T("<enddoctype"), fp);
metaLocation(parser);
fputts(T("/>\n"), fp);
}
static void XMLCALL
metaNotationDecl(void *userData,
const XML_Char *notationName,
const XML_Char *UNUSED_P(base),
const XML_Char *systemId,
const XML_Char *publicId)
{
XML_Parser parser = (XML_Parser) userData;
FILE *fp = (FILE *)XML_GetUserData(parser);
1998-06-03 06:16:20 -04:00
ftprintf(fp, T("<notation name=\"%s\""), notationName);
if (publicId)
ftprintf(fp, T(" public=\"%s\""), publicId);
if (systemId) {
fputts(T(" system=\""), fp);
characterData(fp, systemId, (int)tcslen(systemId));
1998-06-03 06:16:20 -04:00
puttc(T('"'), fp);
}
metaLocation(parser);
fputts(T("/>\n"), fp);
}
2000-05-12 01:56:27 -04:00
static void XMLCALL
metaEntityDecl(void *userData,
const XML_Char *entityName,
int UNUSED_P(is_param),
const XML_Char *value,
int value_length,
const XML_Char *UNUSED_P(base),
const XML_Char *systemId,
const XML_Char *publicId,
const XML_Char *notationName)
2000-05-12 01:56:27 -04:00
{
XML_Parser parser = (XML_Parser) userData;
FILE *fp = (FILE *)XML_GetUserData(parser);
2000-05-12 01:56:27 -04:00
if (value) {
ftprintf(fp, T("<entity name=\"%s\""), entityName);
metaLocation(parser);
puttc(T('>'), fp);
characterData(fp, value, value_length);
fputts(T("</entity/>\n"), fp);
}
else if (notationName) {
ftprintf(fp, T("<entity name=\"%s\""), entityName);
if (publicId)
ftprintf(fp, T(" public=\"%s\""), publicId);
fputts(T(" system=\""), fp);
characterData(fp, systemId, (int)tcslen(systemId));
puttc(T('"'), fp);
ftprintf(fp, T(" notation=\"%s\""), notationName);
metaLocation(parser);
fputts(T("/>\n"), fp);
}
else {
ftprintf(fp, T("<entity name=\"%s\""), entityName);
if (publicId)
ftprintf(fp, T(" public=\"%s\""), publicId);
fputts(T(" system=\""), fp);
characterData(fp, systemId, (int)tcslen(systemId));
puttc(T('"'), fp);
metaLocation(parser);
fputts(T("/>\n"), fp);
}
2000-05-12 01:56:27 -04:00
}
static void XMLCALL
metaStartNamespaceDecl(void *userData,
const XML_Char *prefix,
const XML_Char *uri)
{
XML_Parser parser = (XML_Parser) userData;
FILE *fp = (FILE *)XML_GetUserData(parser);
fputts(T("<startns"), fp);
if (prefix)
ftprintf(fp, T(" prefix=\"%s\""), prefix);
if (uri) {
fputts(T(" ns=\""), fp);
characterData(fp, uri, (int)tcslen(uri));
fputts(T("\"/>\n"), fp);
}
else
fputts(T("/>\n"), fp);
}
static void XMLCALL
metaEndNamespaceDecl(void *userData, const XML_Char *prefix)
{
XML_Parser parser = (XML_Parser) userData;
FILE *fp = (FILE *)XML_GetUserData(parser);
if (!prefix)
fputts(T("<endns/>\n"), fp);
else
ftprintf(fp, T("<endns prefix=\"%s\"/>\n"), prefix);
}
1998-05-10 06:18:15 -04:00
static int XMLCALL
unknownEncodingConvert(void *data, const char *p)
1998-06-02 04:57:14 -04:00
{
return codepageConvert(*(int *)data, p);
}
static int XMLCALL
unknownEncoding(void *UNUSED_P(userData), const XML_Char *name, XML_Encoding *info)
{
int cp;
static const XML_Char prefixL[] = T("windows-");
static const XML_Char prefixU[] = T("WINDOWS-");
int i;
for (i = 0; prefixU[i]; i++)
1998-06-02 04:57:14 -04:00
if (name[i] != prefixU[i] && name[i] != prefixL[i])
return 0;
cp = 0;
1998-06-02 04:57:14 -04:00
for (; name[i]; i++) {
static const XML_Char digits[] = T("0123456789");
1998-06-02 04:57:14 -04:00
const XML_Char *s = tcschr(digits, name[i]);
if (!s)
return 0;
cp *= 10;
cp += (int)(s - digits);
if (cp >= 0x10000)
return 0;
}
1998-06-02 04:57:14 -04:00
if (!codepageMap(cp, info->map))
return 0;
info->convert = unknownEncodingConvert;
/* We could just cast the code page integer to a void *,
and avoid the use of release. */
info->release = free;
info->data = malloc(sizeof(int));
if (!info->data)
return 0;
*(int *)info->data = cp;
return 1;
}
static int XMLCALL
notStandalone(void *UNUSED_P(userData))
1999-04-19 22:52:43 -04:00
{
return 0;
}
static void
showVersion(XML_Char *prog)
{
XML_Char *s = prog;
XML_Char ch;
const XML_Feature *features = XML_GetFeatureList();
while ((ch = *s) != 0) {
if (ch == '/'
#if defined(WIN32)
|| ch == '\\'
#endif
)
prog = s + 1;
++s;
}
ftprintf(stdout, T("%s using %s\n"), prog, XML_ExpatVersion());
if (features != NULL && features[0].feature != XML_FEATURE_END) {
int i = 1;
ftprintf(stdout, T("%s"), features[0].name);
if (features[0].value)
ftprintf(stdout, T("=%ld"), features[0].value);
while (features[i].feature != XML_FEATURE_END) {
ftprintf(stdout, T(", %s"), features[i].name);
if (features[i].value)
ftprintf(stdout, T("=%ld"), features[i].value);
++i;
}
ftprintf(stdout, T("\n"));
}
}
static void
usage(const XML_Char *prog, int rc)
1998-02-04 00:25:27 -05:00
{
2002-04-19 12:19:33 -04:00
ftprintf(stderr,
T("usage: %s [-s] [-n] [-p] [-x] [-e encoding] [-w] [-d output-dir] [-c] [-m] [-r] [-t] [file ...]\n"), prog);
exit(rc);
1997-11-11 00:52:10 -05:00
}
int
tmain(int argc, XML_Char **argv)
1997-11-11 00:52:10 -05:00
{
1999-04-19 22:52:43 -04:00
int i, j;
const XML_Char *outputDir = NULL;
const XML_Char *encoding = NULL;
unsigned processFlags = XML_MAP_FILE;
int windowsCodePages = 0;
1998-06-17 06:04:31 -04:00
int outputType = 0;
1998-08-26 05:10:50 -04:00
int useNamespaces = 0;
1999-04-19 22:52:43 -04:00
int requireStandalone = 0;
enum XML_ParamEntityParsing paramEntityParsing =
XML_PARAM_ENTITY_PARSING_NEVER;
int useStdin = 0;
1997-12-10 02:44:19 -05:00
#ifdef _MSC_VER
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF);
#endif
1997-12-10 02:44:19 -05:00
1998-02-04 00:25:27 -05:00
i = 1;
1999-04-19 22:52:43 -04:00
j = 0;
while (i < argc) {
if (j == 0) {
if (argv[i][0] != T('-'))
break;
1999-04-19 22:52:43 -04:00
if (argv[i][1] == T('-') && argv[i][2] == T('\0')) {
i++;
break;
1999-04-19 22:52:43 -04:00
}
j++;
1998-02-04 00:25:27 -05:00
}
1999-04-19 22:52:43 -04:00
switch (argv[i][j]) {
case T('r'):
processFlags &= ~XML_MAP_FILE;
1998-02-04 00:25:27 -05:00
j++;
1999-04-19 22:52:43 -04:00
break;
case T('s'):
requireStandalone = 1;
j++;
break;
case T('n'):
1998-08-26 05:10:50 -04:00
useNamespaces = 1;
1998-08-22 18:40:45 -04:00
j++;
1999-04-19 22:52:43 -04:00
break;
1999-06-26 07:29:49 -04:00
case T('p'):
paramEntityParsing = XML_PARAM_ENTITY_PARSING_ALWAYS;
/* fall through */
1999-04-19 22:52:43 -04:00
case T('x'):
processFlags |= XML_EXTERNAL_ENTITIES;
1998-05-10 06:18:15 -04:00
j++;
1999-04-19 22:52:43 -04:00
break;
case T('w'):
windowsCodePages = 1;
j++;
1999-04-19 22:52:43 -04:00
break;
case T('m'):
1998-06-17 06:04:31 -04:00
outputType = 'm';
j++;
1999-04-19 22:52:43 -04:00
break;
case T('c'):
1998-06-17 06:04:31 -04:00
outputType = 'c';
1998-08-26 05:10:50 -04:00
useNamespaces = 0;
j++;
1999-04-19 22:52:43 -04:00
break;
case T('t'):
1998-11-24 10:24:59 -05:00
outputType = 't';
j++;
1999-04-19 22:52:43 -04:00
break;
case T('d'):
1998-05-31 07:53:28 -04:00
if (argv[i][j + 1] == T('\0')) {
if (++i == argc)
usage(argv[0], 2);
outputDir = argv[i];
1998-02-04 00:25:27 -05:00
}
else
outputDir = argv[i] + j + 1;
1998-02-04 00:25:27 -05:00
i++;
1999-04-19 22:52:43 -04:00
j = 0;
break;
case T('e'):
1998-05-31 07:53:28 -04:00
if (argv[i][j + 1] == T('\0')) {
if (++i == argc)
usage(argv[0], 2);
encoding = argv[i];
}
else
encoding = argv[i] + j + 1;
i++;
1999-04-19 22:52:43 -04:00
j = 0;
break;
case T('h'):
usage(argv[0], 0);
return 0;
case T('v'):
showVersion(argv[0]);
return 0;
1999-04-19 22:52:43 -04:00
case T('\0'):
if (j > 1) {
i++;
j = 0;
break;
1999-04-19 22:52:43 -04:00
}
/* fall through */
default:
usage(argv[0], 2);
1999-04-19 22:52:43 -04:00
}
1997-11-11 00:52:10 -05:00
}
if (i == argc) {
useStdin = 1;
processFlags &= ~XML_MAP_FILE;
i--;
}
1997-12-10 02:44:19 -05:00
for (; i < argc; i++) {
1998-02-04 00:25:27 -05:00
FILE *fp = 0;
1998-05-31 07:53:28 -04:00
XML_Char *outName = 0;
1998-02-04 00:25:27 -05:00
int result;
1998-08-26 05:10:50 -04:00
XML_Parser parser;
if (useNamespaces)
parser = XML_ParserCreateNS(encoding, NSSEP);
else
parser = XML_ParserCreate(encoding);
if (! parser) {
tperror("Could not instantiate parser");
exit(1);
}
1999-04-19 22:52:43 -04:00
if (requireStandalone)
XML_SetNotStandaloneHandler(parser, notStandalone);
1999-06-26 07:29:49 -04:00
XML_SetParamEntityParsing(parser, paramEntityParsing);
1999-03-07 03:44:14 -05:00
if (outputType == 't') {
/* This is for doing timings; this gives a more realistic estimate of
the parsing time. */
1999-03-07 03:44:14 -05:00
outputDir = 0;
XML_SetElementHandler(parser, nopStartElement, nopEndElement);
XML_SetCharacterDataHandler(parser, nopCharacterData);
XML_SetProcessingInstructionHandler(parser, nopProcessingInstruction);
}
else if (outputDir) {
const XML_Char * delim = T("/");
const XML_Char *file = useStdin ? T("STDIN") : argv[i];
if (!useStdin) {
/* Jump after last (back)slash */
const XML_Char * lastDelim = tcsrchr(file, delim[0]);
if (lastDelim)
file = lastDelim + 1;
#if defined(WIN32)
else {
const XML_Char * winDelim = T("\\");
lastDelim = tcsrchr(file, winDelim[0]);
if (lastDelim) {
file = lastDelim + 1;
delim = winDelim;
}
}
#endif
}
outName = (XML_Char *)malloc((tcslen(outputDir) + tcslen(file) + 2)
* sizeof(XML_Char));
1998-05-31 07:53:28 -04:00
tcscpy(outName, outputDir);
tcscat(outName, delim);
1998-05-31 07:53:28 -04:00
tcscat(outName, file);
fp = tfopen(outName, T("wb"));
1998-02-04 00:25:27 -05:00
if (!fp) {
tperror(outName);
exit(1);
1998-02-04 00:25:27 -05:00
}
1998-08-26 05:10:50 -04:00
setvbuf(fp, NULL, _IOFBF, 16384);
1998-05-31 07:53:28 -04:00
#ifdef XML_UNICODE
puttc(0xFEFF, fp);
#endif
1998-02-04 00:25:27 -05:00
XML_SetUserData(parser, fp);
1998-06-17 06:04:31 -04:00
switch (outputType) {
case 'm':
XML_UseParserAsHandlerArg(parser);
XML_SetElementHandler(parser, metaStartElement, metaEndElement);
XML_SetProcessingInstructionHandler(parser, metaProcessingInstruction);
XML_SetCommentHandler(parser, metaComment);
XML_SetCdataSectionHandler(parser, metaStartCdataSection,
metaEndCdataSection);
XML_SetCharacterDataHandler(parser, metaCharacterData);
XML_SetDoctypeDeclHandler(parser, metaStartDoctypeDecl,
metaEndDoctypeDecl);
XML_SetEntityDeclHandler(parser, metaEntityDecl);
XML_SetNotationDeclHandler(parser, metaNotationDecl);
XML_SetNamespaceDeclHandler(parser, metaStartNamespaceDecl,
metaEndNamespaceDecl);
metaStartDocument(parser);
break;
1998-06-17 06:04:31 -04:00
case 'c':
XML_UseParserAsHandlerArg(parser);
XML_SetDefaultHandler(parser, markup);
XML_SetElementHandler(parser, defaultStartElement, defaultEndElement);
XML_SetCharacterDataHandler(parser, defaultCharacterData);
XML_SetProcessingInstructionHandler(parser,
defaultProcessingInstruction);
break;
1998-06-17 06:04:31 -04:00
default:
if (useNamespaces)
XML_SetElementHandler(parser, startElementNS, endElementNS);
else
XML_SetElementHandler(parser, startElement, endElement);
XML_SetCharacterDataHandler(parser, characterData);
#ifndef W3C14N
XML_SetProcessingInstructionHandler(parser, processingInstruction);
#endif /* not W3C14N */
break;
}
}
if (windowsCodePages)
1998-06-02 04:57:14 -04:00
XML_SetUnknownEncodingHandler(parser, unknownEncoding, 0);
result = XML_ProcessFile(parser, useStdin ? NULL : argv[i], processFlags);
1998-02-04 00:25:27 -05:00
if (outputDir) {
1998-06-17 06:04:31 -04:00
if (outputType == 'm')
metaEndDocument(parser);
1998-02-04 00:25:27 -05:00
fclose(fp);
if (!result) {
tremove(outName);
exit(2);
}
1998-02-04 00:25:27 -05:00
free(outName);
}
XML_ParserFree(parser);
1997-11-11 00:52:10 -05:00
}
1998-02-04 00:25:27 -05:00
return 0;
1997-11-11 00:52:10 -05:00
}