From 13a2b0b906b3c31365277a41f3fb00beae0dfc14 Mon Sep 17 00:00:00 2001 From: jbj Date: Fri, 21 Feb 2003 05:13:48 +0000 Subject: [PATCH] Splint fiddles. CVS patchset: 6069 CVS date: 2003/02/21 05:13:48 --- expat/xmlwf/.splintrc | 74 ++++++++ expat/xmlwf/Makefile.am | 4 + expat/xmlwf/codepage.h | 11 +- expat/xmlwf/filemap.h | 20 +- expat/xmlwf/unixfilemap.c | 19 +- expat/xmlwf/xmlfile.c | 138 ++++++++------ expat/xmlwf/xmlfile.h | 11 +- expat/xmlwf/xmlmime.c | 107 ++++++----- expat/xmlwf/xmlmime.h | 11 +- expat/xmlwf/xmlwf.c | 460 ++++++++++++++++++++++++++++++---------------- 10 files changed, 563 insertions(+), 292 deletions(-) create mode 100644 expat/xmlwf/.splintrc diff --git a/expat/xmlwf/.splintrc b/expat/xmlwf/.splintrc new file mode 100644 index 0000000..7c30739 --- /dev/null +++ b/expat/xmlwf/.splintrc @@ -0,0 +1,74 @@ +-I. -I.. -I../lib -DHAVE_CONFIG_H + +#+partial ++forcehints + +-warnunixlib +-warnposix + ++unixlib + +-unrecogcomments # XXX ignore doxygen markings + ++strict # lclint level + +# --- in progress ++bounds +-boundsread +-boundswrite + +-declundef +-enummemuse # 6 +-fcnuse # 61 +-fielduse +-exportconst # 48 +-exportfcn # 67 +-exporttype # 9 + +-aliasunique # 1 +-boolops # 11 +-branchstate # 2 +-compdef # 5 +-evalorder # 10 +-exitarg +-maintype +-nullpass # 17 +-nullstate # 3 +-paramuse # 32 +-predboolptr # 26 +-retalias # 3 +-retvalint +-sizeoftype # 5 +-type # 3 +-usereleased # 1 ++voidabstract + +-kepttrans # 1 +-temptrans # 2 + +-loopswitchbreak # 33 + +# --- not-yet at strict level +-bitwisesigned # 86 +-elseifcomplete # 2 +-forblock # 6 +-ifblock # 231 +-incondefs # 64 +-namechecks # tedious ANSI compliance checks +-ptrarith # tedious + +-mustdefine # 25 +-shiftimplementation # 148 ctype.h + +-strictops # 37 +-whileblock # 16 + +# --- not-yet at checks level +-mustfree # 26 +-usedef # 31 + +# --- not-yet at standard level ++boolint # 38 ++charint # 147 ++ignorequals # 49 ++matchanyintegral # 77 diff --git a/expat/xmlwf/Makefile.am b/expat/xmlwf/Makefile.am index 6fa9675..0d1cec0 100644 --- a/expat/xmlwf/Makefile.am +++ b/expat/xmlwf/Makefile.am @@ -16,3 +16,7 @@ bin_PROGRAMS = xmlwf xmlwf_SOURCES = \ codepage.c unixfilemap.c xmlfile.c xmlmime.c xmlwf.c + +.PHONY: lint +lint: + $(LINT) $(DEFS) $(INCLUDES) $(xmlwf_SOURCES) diff --git a/expat/xmlwf/codepage.h b/expat/xmlwf/codepage.h index b36a550..41b013e 100644 --- a/expat/xmlwf/codepage.h +++ b/expat/xmlwf/codepage.h @@ -1,7 +1,8 @@ -/* -Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd -See the file COPYING for copying permission. +/* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd + See the file COPYING for copying permission. */ -int codepageMap(int cp, int *map); -int codepageConvert(int cp, const char *p); +int codepageMap(int cp, int *map) + /*@*/; +int codepageConvert(int cp, const char *p) + /*@*/; diff --git a/expat/xmlwf/filemap.h b/expat/xmlwf/filemap.h index ecf0175..d63b556 100644 --- a/expat/xmlwf/filemap.h +++ b/expat/xmlwf/filemap.h @@ -1,17 +1,21 @@ -/* -Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd -See the file COPYING for copying permission. +/* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd + See the file COPYING for copying permission. */ - #include #ifdef XML_UNICODE int filemap(const wchar_t *name, - void (*processor)(const void *, size_t, const wchar_t *, void *arg), - void *arg); + void (*processor)(const void *, size_t, + const wchar_t *, void *arg), + void *arg) + /*@globals fileSystem, internalState @*/ + /*@modifies fileSystem, internalState @*/; #else int filemap(const char *name, - void (*processor)(const void *, size_t, const char *, void *arg), - void *arg); + void (*processor)(const void *, size_t, + const char *, void *arg), + void *arg) + /*@globals fileSystem, internalState @*/ + /*@modifies fileSystem, internalState @*/; #endif diff --git a/expat/xmlwf/unixfilemap.c b/expat/xmlwf/unixfilemap.c index 4016dd0..35a9884 100644 --- a/expat/xmlwf/unixfilemap.c +++ b/expat/xmlwf/unixfilemap.c @@ -1,6 +1,5 @@ -/* -Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd -See the file COPYING for copying permission. +/* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd + See the file COPYING for copying permission. */ #include @@ -10,6 +9,7 @@ See the file COPYING for copying permission. #include #include #include +#include #ifndef MAP_FILE #define MAP_FILE 0 @@ -17,9 +17,10 @@ See the file COPYING for copying permission. #include "filemap.h" -int filemap(const char *name, - void (*processor)(const void *, size_t, const char *, void *arg), - void *arg) +int +filemap(const char *name, + void (*processor)(const void *, size_t, const char *, void *arg), + void *arg) { int fd; size_t nbytes; @@ -41,16 +42,18 @@ int filemap(const char *name, fprintf(stderr, "%s: not a regular file\n", name); return 0; } - + nbytes = sb.st_size; p = (void *)mmap((caddr_t)0, (size_t)nbytes, PROT_READ, - MAP_FILE|MAP_PRIVATE, fd, (off_t)0); + MAP_FILE|MAP_PRIVATE, fd, (off_t)0); if (p == (void *)-1) { perror(name); close(fd); return 0; } +/*@-noeffect@*/ processor(p, nbytes, name, arg); +/*@=noeffect@*/ munmap((caddr_t)p, nbytes); close(fd); return 1; diff --git a/expat/xmlwf/xmlfile.c b/expat/xmlwf/xmlfile.c index ba79b23..c442a1e 100644 --- a/expat/xmlwf/xmlfile.c +++ b/expat/xmlwf/xmlfile.c @@ -1,6 +1,5 @@ -/* -Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd -See the file COPYING for copying permission. +/* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd + See the file COPYING for copying permission. */ #include @@ -8,6 +7,11 @@ See the file COPYING for copying permission. #include #include #include +#ifdef COMPILED_FROM_DSP +#include "winconfig.h" +#else +#include "expat_config.h" +#endif #include "expat.h" #include "xmlfile.h" #include "xmltchar.h" @@ -17,7 +21,7 @@ See the file COPYING for copying permission. #include #endif -#ifdef _POSIX_SOURCE +#ifdef HAVE_UNISTD_H #include #endif @@ -36,36 +40,37 @@ See the file COPYING for copying permission. #endif - typedef struct { XML_Parser parser; int *retPtr; } PROCESS_ARGS; -static -void reportError(XML_Parser parser, const XML_Char *filename) +static void +reportError(XML_Parser parser, const XML_Char *filename) + /*@globals fileSystem @*/ + /*@modifies parser, fileSystem @*/ { int code = XML_GetErrorCode(parser); const XML_Char *message = XML_ErrorString(code); if (message) ftprintf(stdout, T("%s:%d:%d: %s\n"), - filename, - XML_GetErrorLineNumber(parser), - XML_GetErrorColumnNumber(parser), - message); + filename, + XML_GetErrorLineNumber(parser), + XML_GetErrorColumnNumber(parser), + message); else ftprintf(stderr, T("%s: (unknown message %d)\n"), filename, code); } -static -void processFile(const void *data, - size_t size, - const XML_Char *filename, - void *args) +static void +processFile(const void *data, size_t size, + const XML_Char *filename, void *args) + /*@globals fileSystem @*/ + /*@modifies fileSystem @*/ { XML_Parser parser = ((PROCESS_ARGS *)args)->parser; int *retPtr = ((PROCESS_ARGS *)args)->retPtr; - if (!XML_Parse(parser, data, size, 1)) { + if (XML_Parse(parser, data, size, 1) == XML_STATUS_ERROR) { reportError(parser, filename); *retPtr = 0; } @@ -75,16 +80,19 @@ void processFile(const void *data, #ifdef WIN32 -static -int isAsciiLetter(XML_Char c) +static int +isAsciiLetter(XML_Char c) + /*@*/ { return (T('a') <= c && c <= T('z')) || (T('A') <= c && c <= T('Z')); } #endif /* WIN32 */ -static -const XML_Char *resolveSystemId(const XML_Char *base, const XML_Char *systemId, XML_Char **toFree) +static const XML_Char * +resolveSystemId(const XML_Char *base, const XML_Char *systemId, + XML_Char **toFree) + /*@modifies *toFree @*/ { XML_Char *s; *toFree = 0; @@ -96,7 +104,8 @@ const XML_Char *resolveSystemId(const XML_Char *base, const XML_Char *systemId, #endif ) return systemId; - *toFree = (XML_Char *)malloc((tcslen(base) + tcslen(systemId) + 2)*sizeof(XML_Char)); + *toFree = (XML_Char *)malloc((tcslen(base) + tcslen(systemId) + 2) + * sizeof(XML_Char)); if (!*toFree) return systemId; tcscpy(*toFree, base); @@ -111,12 +120,14 @@ const XML_Char *resolveSystemId(const XML_Char *base, const XML_Char *systemId, return *toFree; } -static -int externalEntityRefFilemap(XML_Parser parser, - const XML_Char *context, - const XML_Char *base, - const XML_Char *systemId, - const XML_Char *publicId) +static int +externalEntityRefFilemap(XML_Parser parser, + const XML_Char *context, + const XML_Char *base, + const XML_Char *systemId, + const XML_Char *publicId) + /*@globals fileSystem, internalState @*/ + /*@modifies fileSystem, internalState @*/ { int result; XML_Char *s; @@ -126,7 +137,7 @@ int externalEntityRefFilemap(XML_Parser parser, args.retPtr = &result; args.parser = entParser; filename = resolveSystemId(base, systemId, &s); - XML_SetBase(entParser, filename); + (void) XML_SetBase(entParser, filename); if (!filemap(filename, processFile, &args)) result = 0; free(s); @@ -134,63 +145,78 @@ int externalEntityRefFilemap(XML_Parser parser, return result; } -static -int processStream(const XML_Char *filename, XML_Parser parser) +static int +processStream(const XML_Char *filename, XML_Parser parser) + /*@globals fileSystem, internalState @*/ + /*@modifies parser, fileSystem, internalState @*/ { - int fd = topen(filename, O_BINARY|O_RDONLY); - if (fd < 0) { - tperror(filename); - return 0; + /* 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; + } } for (;;) { int nread; char *buf = XML_GetBuffer(parser, READ_SIZE); if (!buf) { - close(fd); - ftprintf(stderr, T("%s: out of memory\n"), filename); + if (filename != NULL) + close(fd); + ftprintf(stderr, T("%s: out of memory\n"), + filename != NULL ? filename : "xmlwf"); return 0; } nread = read(fd, buf, READ_SIZE); if (nread < 0) { - tperror(filename); - close(fd); + tperror(filename != NULL ? filename : "STDIN"); + if (filename != NULL) + close(fd); return 0; } - if (!XML_ParseBuffer(parser, nread, nread == 0)) { - reportError(parser, filename); - close(fd); + if (XML_ParseBuffer(parser, nread, nread == 0) == XML_STATUS_ERROR) { + reportError(parser, filename != NULL ? filename : "STDIN"); + if (filename != NULL) + close(fd); return 0; } if (nread == 0) { - close(fd); + if (filename != NULL) + close(fd); break;; } } return 1; } -static -int externalEntityRefStream(XML_Parser parser, - const XML_Char *context, - const XML_Char *base, - const XML_Char *systemId, - const XML_Char *publicId) +static int +externalEntityRefStream(XML_Parser parser, + const XML_Char *context, + const XML_Char *base, + const XML_Char *systemId, + const XML_Char *publicId) + /*@globals fileSystem, internalState @*/ + /*@modifies fileSystem, internalState @*/ { 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); + (void) XML_SetBase(entParser, filename); ret = processStream(filename, entParser); free(s); XML_ParserFree(entParser); return ret; } -int XML_ProcessFile(XML_Parser parser, - const XML_Char *filename, - unsigned flags) +int +XML_ProcessFile(XML_Parser parser, + const XML_Char *filename, + unsigned flags) { int result; @@ -201,9 +227,9 @@ int XML_ProcessFile(XML_Parser parser, if (flags & XML_EXTERNAL_ENTITIES) XML_SetExternalEntityRefHandler(parser, - (flags & XML_MAP_FILE) - ? externalEntityRefFilemap - : externalEntityRefStream); + (flags & XML_MAP_FILE) + ? externalEntityRefFilemap + : externalEntityRefStream); if (flags & XML_MAP_FILE) { PROCESS_ARGS args; args.retPtr = &result; diff --git a/expat/xmlwf/xmlfile.h b/expat/xmlwf/xmlfile.h index 5d6bdce..685ed24 100644 --- a/expat/xmlwf/xmlfile.h +++ b/expat/xmlwf/xmlfile.h @@ -1,11 +1,12 @@ -/* -Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd -See the file COPYING for copying permission. +/* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd + See the file COPYING for copying permission. */ #define XML_MAP_FILE 01 #define XML_EXTERNAL_ENTITIES 02 extern int XML_ProcessFile(XML_Parser parser, - const XML_Char *filename, - unsigned flags); + const XML_Char *filename, + unsigned flags) + /*@globals fileSystem, internalState @*/ + /*@modifies parser, fileSystem, internalState @*/; diff --git a/expat/xmlwf/xmlmime.c b/expat/xmlwf/xmlmime.c index 4b32c86..f3db356 100644 --- a/expat/xmlwf/xmlmime.c +++ b/expat/xmlwf/xmlmime.c @@ -1,8 +1,10 @@ #include #include "xmlmime.h" -static -const char *getTok(const char **pp) +/*@null@*/ +static const char * +getTok(const char **pp) + /*@modifies *pp @*/ { /* inComment means one level of nesting; inComment+1 means two levels etc */ enum { inAtom, inString, init, inComment }; @@ -12,57 +14,57 @@ const char *getTok(const char **pp) switch (**pp) { case '\0': if (state == inAtom) - return tokStart; + return tokStart; return 0; case ' ': case '\r': case '\t': case '\n': if (state == inAtom) - return tokStart; + return tokStart; break; case '(': if (state == inAtom) - return tokStart; + return tokStart; if (state != inString) - state++; + state++; break; case ')': if (state > init) - --state; + --state; else if (state != inString) - return 0; + return 0; break; case ';': case '/': case '=': if (state == inAtom) - return tokStart; + return tokStart; if (state == init) - return (*pp)++; + return (*pp)++; break; case '\\': ++*pp; if (**pp == '\0') - return 0; + return 0; break; case '"': switch (state) { case inString: - ++*pp; - return tokStart; + ++*pp; + return tokStart; case inAtom: - return tokStart; + return tokStart; case init: - tokStart = *pp; - state = inString; - break; + tokStart = *pp; + state = inString; + /*@innerbreak@*/ break; } break; default: if (state == init) { - tokStart = *pp; - state = inAtom; + tokStart = *pp; + state = inAtom; } break; } @@ -73,8 +75,9 @@ const char *getTok(const char **pp) /* key must be lowercase ASCII */ -static -int matchkey(const char *start, const char *end, const char *key) +static int +matchkey(const char *start, const char *end, const char *key) + /*@*/ { if (!start) return 0; @@ -84,7 +87,8 @@ int matchkey(const char *start, const char *end, const char *key) return *key == '\0'; } -void getXMLCharset(const char *buf, char *charset) +void +getXMLCharset(const char *buf, char *charset) { const char *next, *p; @@ -108,34 +112,34 @@ void getXMLCharset(const char *buf, char *charset) if (*p == ';') { p = getTok(&next); if (matchkey(p, next, "charset")) { - p = getTok(&next); - if (p && *p == '=') { - p = getTok(&next); - if (p) { - char *s = charset; - if (*p == '"') { - while (++p != next - 1) { - if (*p == '\\') - ++p; - if (s == charset + CHARSET_MAX - 1) { - charset[0] = '\0'; - break; - } - *s++ = *p; - } - *s++ = '\0'; - } - else { - if (next - p > CHARSET_MAX - 1) - break; - while (p != next) - *s++ = *p++; - *s = 0; - break; - } - } - } - break; + p = getTok(&next); + if (p && *p == '=') { + p = getTok(&next); + if (p) { + char *s = charset; + if (*p == '"') { + while (++p != next - 1) { + if (*p == '\\') + ++p; + if (s == charset + CHARSET_MAX - 1) { + charset[0] = '\0'; + /*@innerbreak@*/ break; + } + *s++ = *p; + } + *s++ = '\0'; + } + else { + if (next - p > CHARSET_MAX - 1) + break; + while (p != next) + *s++ = *p++; + *s = 0; + break; + } + } + } + break; } } else @@ -147,7 +151,8 @@ void getXMLCharset(const char *buf, char *charset) #include -int main(int argc, char **argv) +int +main(int argc, char *argv[]) { char buf[CHARSET_MAX]; if (argc <= 1) diff --git a/expat/xmlwf/xmlmime.h b/expat/xmlwf/xmlmime.h index 79654b6..8cd0db4 100644 --- a/expat/xmlwf/xmlmime.h +++ b/expat/xmlwf/xmlmime.h @@ -1,4 +1,3 @@ - #ifdef __cplusplus extern "C" { #endif @@ -8,11 +7,13 @@ extern "C" { #define CHARSET_MAX 41 /* Figure out the charset to use from the ContentType. -buf contains the body of the header field (the part after "Content-Type:"). -charset gets the charset to use. It must be at least CHARSET_MAX chars long. -charset will be empty if the default charset should be used. */ + buf contains the body of the header field (the part after "Content-Type:"). + charset gets the charset to use. It must be at least CHARSET_MAX chars + long. charset will be empty if the default charset should be used. +*/ -void getXMLCharset(const char *buf, char *charset); +void getXMLCharset(const char *buf, char *charset) + /*@modifies buf, charset @*/; #ifdef __cplusplus } diff --git a/expat/xmlwf/xmlwf.c b/expat/xmlwf/xmlwf.c index 891c0db..1c13dc0 100644 --- a/expat/xmlwf/xmlwf.c +++ b/expat/xmlwf/xmlwf.c @@ -1,6 +1,5 @@ -/* -Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd -See the file COPYING for copying permission. +/* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd + See the file COPYING for copying permission. */ #include @@ -21,7 +20,10 @@ See the file COPYING for copying permission. #define NSSEP T('\001') -static void characterData(void *userData, const XML_Char *s, int len) +static void +characterData(void *userData, const XML_Char *s, int len) + /*@globals fileSystem @*/ + /*@modifies userData, fileSystem @*/ { FILE *fp = userData; for (; len > 0; --len, ++s) { @@ -56,7 +58,10 @@ static void characterData(void *userData, const XML_Char *s, int len) } } -static void attributeValue(FILE *fp, const XML_Char *s) +static void +attributeValue(FILE *fp, const XML_Char *s) + /*@globals fileSystem @*/ + /*@modifies fp, fileSystem @*/ { puttc(T('='), fp); puttc(T('"'), fp); @@ -106,12 +111,17 @@ static void attributeValue(FILE *fp, const XML_Char *s) /* Lexicographically comparing UTF-8 encoded attribute values, is equivalent to lexicographically comparing based on the character number. */ -static int attcmp(const void *att1, const void *att2) +static int +attcmp(const void *att1, const void *att2) + /*@*/ { return tcscmp(*(const XML_Char **)att1, *(const XML_Char **)att2); } -static void startElement(void *userData, const XML_Char *name, const XML_Char **atts) +static void +startElement(void *userData, const XML_Char *name, const XML_Char **atts) + /*@globals fileSystem @*/ + /*@modifies userData, *atts, fileSystem @*/ { int nAtts; const XML_Char **p; @@ -134,7 +144,10 @@ static void startElement(void *userData, const XML_Char *name, const XML_Char ** puttc(T('>'), fp); } -static void endElement(void *userData, const XML_Char *name) +static void +endElement(void *userData, const XML_Char *name) + /*@globals fileSystem @*/ + /*@modifies userData, fileSystem @*/ { FILE *fp = userData; puttc(T('<'), fp); @@ -143,7 +156,9 @@ static void endElement(void *userData, const XML_Char *name) puttc(T('>'), fp); } -static int nsattcmp(const void *p1, const void *p2) +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; @@ -154,7 +169,10 @@ static int nsattcmp(const void *p1, const void *p2) return tcscmp(att1, att2); } -static void startElementNS(void *userData, const XML_Char *name, const XML_Char **atts) +static void +startElementNS(void *userData, const XML_Char *name, const XML_Char **atts) + /*@globals fileSystem @*/ + /*@modifies userData, *atts, fileSystem @*/ { int nAtts; int nsi; @@ -202,7 +220,10 @@ static void startElementNS(void *userData, const XML_Char *name, const XML_Char puttc(T('>'), fp); } -static void endElementNS(void *userData, const XML_Char *name) +static void +endElementNS(void *userData, const XML_Char *name) + /*@globals fileSystem @*/ + /*@modifies userData, fileSystem @*/ { FILE *fp = userData; const XML_Char *sep; @@ -220,7 +241,11 @@ static void endElementNS(void *userData, const XML_Char *name) #ifndef W3C14N -static void processingInstruction(void *userData, const XML_Char *target, const XML_Char *data) +static void +processingInstruction(void *userData, const XML_Char *target, + const XML_Char *data) + /*@globals fileSystem @*/ + /*@modifies userData, fileSystem @*/ { FILE *fp = userData; puttc(T('<'), fp); @@ -234,78 +259,110 @@ static void processingInstruction(void *userData, const XML_Char *target, const #endif /* not W3C14N */ -static void defaultCharacterData(XML_Parser parser, const XML_Char *s, int len) +static void +defaultCharacterData(void *userData, const XML_Char *s, int len) + /*@modifies userData @*/ { - XML_DefaultCurrent(parser); + XML_DefaultCurrent((XML_Parser) userData); } -static void defaultStartElement(XML_Parser parser, const XML_Char *name, const XML_Char **atts) +static void +defaultStartElement(void *userData, const XML_Char *name, + const XML_Char **atts) + /*@modifies userData @*/ { - XML_DefaultCurrent(parser); + XML_DefaultCurrent((XML_Parser) userData); } -static void defaultEndElement(XML_Parser parser, const XML_Char *name) +static void +defaultEndElement(void *userData, const XML_Char *name) + /*@modifies userData @*/ { - XML_DefaultCurrent(parser); + XML_DefaultCurrent((XML_Parser) userData); } -static void defaultProcessingInstruction(XML_Parser parser, const XML_Char *target, const XML_Char *data) +static void +defaultProcessingInstruction(void *userData, const XML_Char *target, + const XML_Char *data) + /*@modifies userData @*/ { - XML_DefaultCurrent(parser); + XML_DefaultCurrent((XML_Parser) userData); } -static void nopCharacterData(XML_Parser parser, const XML_Char *s, int len) +static void +nopCharacterData(void *userData, const XML_Char *s, int len) + /*@*/ { } -static void nopStartElement(XML_Parser parser, const XML_Char *name, const XML_Char **atts) +static void +nopStartElement(void *userData, const XML_Char *name, const XML_Char **atts) + /*@*/ { } -static void nopEndElement(XML_Parser parser, const XML_Char *name) +static void +nopEndElement(void *userData, const XML_Char *name) + /*@*/ { } -static void nopProcessingInstruction(XML_Parser parser, const XML_Char *target, const XML_Char *data) +static void +nopProcessingInstruction(void *userData, const XML_Char *target, + const XML_Char *data) + /*@*/ { } -static void markup(XML_Parser parser, const XML_Char *s, int len) +static void +markup(void *userData, const XML_Char *s, int len) + /*@globals fileSystem @*/ + /*@modifies fileSystem @*/ { - FILE *fp = XML_GetUserData(parser); + FILE *fp = XML_GetUserData((XML_Parser) userData); for (; len > 0; --len, ++s) puttc(*s, fp); } -static -void metaLocation(XML_Parser parser) +static void +metaLocation(XML_Parser parser) + /*@globals fileSystem @*/ + /*@modifies parser, fileSystem @*/ { const XML_Char *uri = XML_GetBase(parser); if (uri) ftprintf(XML_GetUserData(parser), T(" uri=\"%s\""), uri); ftprintf(XML_GetUserData(parser), T(" byte=\"%ld\" nbytes=\"%d\" line=\"%d\" col=\"%d\""), - XML_GetCurrentByteIndex(parser), - XML_GetCurrentByteCount(parser), - XML_GetCurrentLineNumber(parser), - XML_GetCurrentColumnNumber(parser)); + XML_GetCurrentByteIndex(parser), + XML_GetCurrentByteCount(parser), + XML_GetCurrentLineNumber(parser), + XML_GetCurrentColumnNumber(parser)); } -static -void metaStartDocument(XML_Parser parser) +static void +metaStartDocument(void *userData) + /*@globals fileSystem @*/ + /*@modifies fileSystem @*/ { - fputts(T("\n"), XML_GetUserData(parser)); + fputts(T("\n"), XML_GetUserData((XML_Parser) userData)); } -static -void metaEndDocument(XML_Parser parser) +static void +metaEndDocument(void *userData) + /*@globals fileSystem @*/ + /*@modifies fileSystem @*/ { - fputts(T("\n"), XML_GetUserData(parser)); + fputts(T("\n"), XML_GetUserData((XML_Parser) userData)); } -static -void metaStartElement(XML_Parser parser, const XML_Char *name, const XML_Char **atts) +static void +metaStartElement(void *userData, const XML_Char *name, + const XML_Char **atts) + /*@globals fileSystem @*/ + /*@modifies userData, fileSystem @*/ { + XML_Parser parser = (XML_Parser) userData; FILE *fp = XML_GetUserData(parser); const XML_Char **specifiedAttsEnd = atts + XML_GetSpecifiedAttributeCount(parser); @@ -324,11 +381,11 @@ void metaStartElement(XML_Parser parser, const XML_Char *name, const XML_Char ** ftprintf(fp, T("= specifiedAttsEnd) - fputts(T("\" defaulted=\"yes\"/>\n"), fp); + fputts(T("\" defaulted=\"yes\"/>\n"), fp); else if (atts == idAttPtr) - fputts(T("\" id=\"yes\"/>\n"), fp); + fputts(T("\" id=\"yes\"/>\n"), fp); else - fputts(T("\"/>\n"), fp); + fputts(T("\"/>\n"), fp); } while (*(atts += 2)); fputts(T("\n"), fp); } @@ -336,18 +393,25 @@ void metaStartElement(XML_Parser parser, const XML_Char *name, const XML_Char ** fputts(T("/>\n"), fp); } -static -void metaEndElement(XML_Parser parser, const XML_Char *name) +static void +metaEndElement(void *userData, const XML_Char *name) + /*@globals fileSystem @*/ + /*@modifies userData, fileSystem @*/ { + XML_Parser parser = (XML_Parser) userData; FILE *fp = XML_GetUserData(parser); ftprintf(fp, T("\n"), fp); } -static -void metaProcessingInstruction(XML_Parser parser, const XML_Char *target, const XML_Char *data) +static void +metaProcessingInstruction(void *userData, const XML_Char *target, + const XML_Char *data) + /*@globals fileSystem @*/ + /*@modifies userData, fileSystem @*/ { + XML_Parser parser = (XML_Parser) userData; FILE *fp = XML_GetUserData(parser); ftprintf(fp, T("\n"), fp); } -static -void metaComment(XML_Parser parser, const XML_Char *data) +static void +metaComment(void *userData, const XML_Char *data) + /*@globals fileSystem @*/ + /*@modifies userData, fileSystem @*/ { + XML_Parser parser = (XML_Parser) userData; FILE *fp = XML_GetUserData(parser); fputts(T("\n"), fp); } -static -void metaStartCdataSection(XML_Parser parser) +static void +metaStartCdataSection(void *userData) + /*@globals fileSystem @*/ + /*@modifies userData, fileSystem @*/ { + XML_Parser parser = (XML_Parser) userData; FILE *fp = XML_GetUserData(parser); fputts(T("\n"), fp); } -static -void metaEndCdataSection(XML_Parser parser) +static void +metaEndCdataSection(void *userData) + /*@globals fileSystem @*/ + /*@modifies userData, fileSystem @*/ { + XML_Parser parser = (XML_Parser) userData; FILE *fp = XML_GetUserData(parser); fputts(T("\n"), fp); } -static -void metaCharacterData(XML_Parser parser, const XML_Char *s, int len) +static void +metaCharacterData(void *userData, const XML_Char *s, int len) + /*@globals fileSystem @*/ + /*@modifies userData, fileSystem @*/ { + XML_Parser parser = (XML_Parser) userData; FILE *fp = XML_GetUserData(parser); fputts(T("\n"), fp); } -static -void metaStartDoctypeDecl(XML_Parser parser, - const XML_Char *doctypeName, - const XML_Char *sysid, - const XML_Char *pubid, - int has_internal_subset) +static void +metaStartDoctypeDecl(void *userData, + const XML_Char *doctypeName, + const XML_Char *sysid, + const XML_Char *pubid, + int has_internal_subset) + /*@globals fileSystem @*/ + /*@modifies userData, fileSystem @*/ { + XML_Parser parser = (XML_Parser) userData; FILE *fp = XML_GetUserData(parser); ftprintf(fp, T("\n"), fp); } -static -void metaEndDoctypeDecl(XML_Parser parser) +static void +metaEndDoctypeDecl(void *userData) + /*@globals fileSystem @*/ + /*@modifies userData, fileSystem @*/ { + XML_Parser parser = (XML_Parser) userData; FILE *fp = XML_GetUserData(parser); fputts(T("\n"), fp); } -static -void metaNotationDecl(XML_Parser parser, - const XML_Char *notationName, - const XML_Char *base, - const XML_Char *systemId, - const XML_Char *publicId) +static void +metaNotationDecl(void *userData, + const XML_Char *notationName, + const XML_Char *base, + const XML_Char *systemId, + const XML_Char *publicId) + /*@globals fileSystem @*/ + /*@modifies userData, fileSystem @*/ { + XML_Parser parser = (XML_Parser) userData; FILE *fp = XML_GetUserData(parser); ftprintf(fp, T("\n"), fp); } -static -void metaEndNamespaceDecl(XML_Parser parser, const XML_Char *prefix) +static void +metaEndNamespaceDecl(void *userData, const XML_Char *prefix) + /*@globals fileSystem @*/ + /*@modifies fileSystem @*/ { + XML_Parser parser = (XML_Parser) userData; FILE *fp = XML_GetUserData(parser); if (!prefix) fputts(T("\n"), fp); @@ -510,16 +604,16 @@ void metaEndNamespaceDecl(XML_Parser parser, const XML_Char *prefix) ftprintf(fp, T("\n"), prefix); } -static -int unknownEncodingConvert(void *data, const char *p) +static int +unknownEncodingConvert(void *data, const char *p) + /*@*/ { return codepageConvert(*(int *)data, p); } -static -int unknownEncoding(void *userData, - const XML_Char *name, - XML_Encoding *info) +static int +unknownEncoding(void *userData, const XML_Char *name, XML_Encoding *info) + /*@modifies info @*/ { int cp; static const XML_Char prefixL[] = T("windows-"); @@ -554,30 +648,73 @@ int unknownEncoding(void *userData, return 1; } -static -int notStandalone(void *userData) +static int +notStandalone(void *userData) + /*@*/ { return 0; } -static -void usage(const XML_Char *prog) +static void +showVersion(XML_Char *prog) + /*@globals fileSystem @*/ + /*@modifies fileSystem @*/ +{ + XML_Char *s = prog; + XML_Char ch; + const XML_Feature *features = XML_GetFeatureList(); + while ((ch = *s) != 0) { + if (ch == '/' +#ifdef 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")); + } +} + +/*@exits@*/ +static void +usage(const XML_Char *prog, int rc) + /*@globals fileSystem @*/ + /*@modifies fileSystem @*/ { - ftprintf(stderr, T("usage: %s [-n] [-p] [-r] [-s] [-w] [-x] [-d output-dir] [-e encoding] file ...\n"), prog); - exit(1); + ftprintf(stderr, + T("usage: %s [-n] [-p] [-r] [-s] [-w] [-x] [-d output-dir] " + "[-e encoding] file ...\n"), prog); + exit(rc); } -int tmain(int argc, XML_Char **argv) +int +tmain(int argc, XML_Char **argv) + /*@globals fileSystem, internalState @*/ + /*@modifies fileSystem, internalState @*/ { int i, j; - const XML_Char *outputDir = 0; - const XML_Char *encoding = 0; + const XML_Char *outputDir = NULL; + const XML_Char *encoding = NULL; unsigned processFlags = XML_MAP_FILE; int windowsCodePages = 0; int outputType = 0; int useNamespaces = 0; int requireStandalone = 0; int paramEntityParsing = XML_PARAM_ENTITY_PARSING_NEVER; + int useStdin = 0; #ifdef _MSC_VER _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF); @@ -588,10 +725,10 @@ int tmain(int argc, XML_Char **argv) while (i < argc) { if (j == 0) { if (argv[i][0] != T('-')) - break; + break; if (argv[i][1] == T('-') && argv[i][2] == T('\0')) { - i++; - break; + i++; + break; } j++; } @@ -610,7 +747,7 @@ int tmain(int argc, XML_Char **argv) break; case T('p'): paramEntityParsing = XML_PARAM_ENTITY_PARSING_ALWAYS; - /* fall through */ + /*@fallthrough@*/ case T('x'): processFlags |= XML_EXTERNAL_ENTITIES; j++; @@ -634,39 +771,49 @@ int tmain(int argc, XML_Char **argv) break; case T('d'): if (argv[i][j + 1] == T('\0')) { - if (++i == argc) - usage(argv[0]); - outputDir = argv[i]; + if (++i == argc) + usage(argv[0], 2); + outputDir = argv[i]; } else - outputDir = argv[i] + j + 1; + outputDir = argv[i] + j + 1; i++; j = 0; break; case T('e'): if (argv[i][j + 1] == T('\0')) { - if (++i == argc) - usage(argv[0]); - encoding = argv[i]; + if (++i == argc) + usage(argv[0], 2); + encoding = argv[i]; } else - encoding = argv[i] + j + 1; + encoding = argv[i] + j + 1; i++; j = 0; break; + case T('h'): + usage(argv[0], 0); + /*@notreached@*/ + return 0; + case T('v'): + showVersion(argv[0]); + return 0; case T('\0'): if (j > 1) { - i++; - j = 0; - break; + i++; + j = 0; + break; } - /* fall through */ + /*@fallthrough@*/ default: - usage(argv[0]); + usage(argv[0], 2); } } - if (i == argc) - usage(argv[0]); + if (i == argc) { + useStdin = 1; + processFlags &= ~XML_MAP_FILE; + i--; + } for (; i < argc; i++) { FILE *fp = 0; XML_Char *outName = 0; @@ -681,28 +828,29 @@ int tmain(int argc, XML_Char **argv) XML_SetParamEntityParsing(parser, paramEntityParsing); if (outputType == 't') { /* This is for doing timings; this gives a more realistic estimate of - the parsing time. */ + the parsing time. */ outputDir = 0; XML_SetElementHandler(parser, nopStartElement, nopEndElement); XML_SetCharacterDataHandler(parser, nopCharacterData); XML_SetProcessingInstructionHandler(parser, nopProcessingInstruction); } else if (outputDir) { - const XML_Char *file = argv[i]; + const XML_Char *file = useStdin ? T("STDIN") : argv[i]; if (tcsrchr(file, T('/'))) - file = tcsrchr(file, T('/')) + 1; + file = tcsrchr(file, T('/')) + 1; #ifdef WIN32 if (tcsrchr(file, T('\\'))) - file = tcsrchr(file, T('\\')) + 1; + file = tcsrchr(file, T('\\')) + 1; #endif - outName = malloc((tcslen(outputDir) + tcslen(file) + 2) * sizeof(XML_Char)); + outName = malloc((tcslen(outputDir) + tcslen(file) + 2) + * sizeof(XML_Char)); tcscpy(outName, outputDir); tcscat(outName, T("/")); tcscat(outName, file); fp = tfopen(outName, T("wb")); if (!fp) { - tperror(outName); - exit(1); + tperror(outName); + exit(1); } setvbuf(fp, NULL, _IOFBF, 16384); #ifdef XML_UNICODE @@ -711,46 +859,50 @@ int tmain(int argc, XML_Char **argv) XML_SetUserData(parser, fp); 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; + 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; case 'c': - XML_UseParserAsHandlerArg(parser); - XML_SetDefaultHandler(parser, markup); - XML_SetElementHandler(parser, defaultStartElement, defaultEndElement); - XML_SetCharacterDataHandler(parser, defaultCharacterData); - XML_SetProcessingInstructionHandler(parser, defaultProcessingInstruction); - break; + XML_UseParserAsHandlerArg(parser); + XML_SetDefaultHandler(parser, markup); + XML_SetElementHandler(parser, defaultStartElement, defaultEndElement); + XML_SetCharacterDataHandler(parser, defaultCharacterData); + XML_SetProcessingInstructionHandler(parser, + defaultProcessingInstruction); + break; default: - if (useNamespaces) - XML_SetElementHandler(parser, startElementNS, endElementNS); - else - XML_SetElementHandler(parser, startElement, endElement); - XML_SetCharacterDataHandler(parser, characterData); + if (useNamespaces) + XML_SetElementHandler(parser, startElementNS, endElementNS); + else + XML_SetElementHandler(parser, startElement, endElement); + XML_SetCharacterDataHandler(parser, characterData); #ifndef W3C14N - XML_SetProcessingInstructionHandler(parser, processingInstruction); + XML_SetProcessingInstructionHandler(parser, processingInstruction); #endif /* not W3C14N */ - break; + break; } } if (windowsCodePages) XML_SetUnknownEncodingHandler(parser, unknownEncoding, 0); - result = XML_ProcessFile(parser, argv[i], processFlags); + result = XML_ProcessFile(parser, useStdin ? NULL : argv[i], processFlags); if (outputDir) { if (outputType == 'm') - metaEndDocument(parser); + metaEndDocument(parser); fclose(fp); if (!result) - tremove(outName); + tremove(outName); free(outName); } XML_ParserFree(parser); -- 2.7.4