1 /* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
2 See the file COPYING for copying permission.
19 #if defined(__amigaos__) && defined(__USE_INLINE__)
20 #include <proto/expat.h>
23 /* This ensures proper sorting. */
25 #define NSSEP T('\001')
28 characterData(void *userData, const XML_Char *s, int len)
30 FILE *fp = (FILE *)userData;
31 for (; len > 0; --len, ++s) {
34 fputts(T("&"), fp);
37 fputts(T("<"), fp);
40 fputts(T(">"), fp);
44 fputts(T("
"), fp);
48 fputts(T("""), fp);
53 ftprintf(fp, T("&#%d;"), *s);
64 attributeValue(FILE *fp, const XML_Char *s)
75 fputts(T("&"), fp);
78 fputts(T("<"), fp);
81 fputts(T("""), fp);
85 fputts(T("	"), fp);
88 fputts(T("
"), fp);
91 fputts(T("
"), fp);
95 fputts(T(">"), fp);
100 ftprintf(fp, T("&#%d;"), *s);
111 /* Lexicographically comparing UTF-8 encoded attribute values,
112 is equivalent to lexicographically comparing based on the character number. */
115 attcmp(const void *att1, const void *att2)
117 return tcscmp(*(const XML_Char **)att1, *(const XML_Char **)att2);
121 startElement(void *userData, const XML_Char *name, const XML_Char **atts)
125 FILE *fp = (FILE *)userData;
132 nAtts = (int)((p - atts) >> 1);
134 qsort((void *)atts, nAtts, sizeof(XML_Char *) * 2, attcmp);
138 attributeValue(fp, *atts);
145 endElement(void *userData, const XML_Char *name)
147 FILE *fp = (FILE *)userData;
155 nsattcmp(const void *p1, const void *p2)
157 const XML_Char *att1 = *(const XML_Char **)p1;
158 const XML_Char *att2 = *(const XML_Char **)p2;
159 int sep1 = (tcsrchr(att1, NSSEP) != 0);
160 int sep2 = (tcsrchr(att1, NSSEP) != 0);
163 return tcscmp(att1, att2);
167 startElementNS(void *userData, const XML_Char *name, const XML_Char **atts)
172 FILE *fp = (FILE *)userData;
176 sep = tcsrchr(name, NSSEP);
178 fputts(T("n1:"), fp);
180 fputts(T(" xmlns:n1"), fp);
181 attributeValue(fp, name);
192 nAtts = (int)((p - atts) >> 1);
194 qsort((void *)atts, nAtts, sizeof(XML_Char *) * 2, nsattcmp);
197 sep = tcsrchr(name, NSSEP);
200 ftprintf(fp, T("n%d:"), nsi);
205 attributeValue(fp, *atts);
207 ftprintf(fp, T(" xmlns:n%d"), nsi++);
208 attributeValue(fp, name);
216 endElementNS(void *userData, const XML_Char *name)
218 FILE *fp = (FILE *)userData;
222 sep = tcsrchr(name, NSSEP);
224 fputts(T("n1:"), fp);
235 processingInstruction(void *userData, const XML_Char *target,
236 const XML_Char *data)
238 FILE *fp = (FILE *)userData;
248 #endif /* not W3C14N */
251 defaultCharacterData(void *userData, const XML_Char *s, int len)
253 XML_DefaultCurrent((XML_Parser) userData);
257 defaultStartElement(void *userData, const XML_Char *name,
258 const XML_Char **atts)
260 XML_DefaultCurrent((XML_Parser) userData);
264 defaultEndElement(void *userData, const XML_Char *name)
266 XML_DefaultCurrent((XML_Parser) userData);
270 defaultProcessingInstruction(void *userData, const XML_Char *target,
271 const XML_Char *data)
273 XML_DefaultCurrent((XML_Parser) userData);
277 nopCharacterData(void *userData, const XML_Char *s, int len)
282 nopStartElement(void *userData, const XML_Char *name, const XML_Char **atts)
287 nopEndElement(void *userData, const XML_Char *name)
292 nopProcessingInstruction(void *userData, const XML_Char *target,
293 const XML_Char *data)
298 markup(void *userData, const XML_Char *s, int len)
300 FILE *fp = (FILE *)XML_GetUserData((XML_Parser) userData);
301 for (; len > 0; --len, ++s)
306 metaLocation(XML_Parser parser)
308 const XML_Char *uri = XML_GetBase(parser);
310 ftprintf((FILE *)XML_GetUserData(parser), T(" uri=\"%s\""), uri);
311 ftprintf((FILE *)XML_GetUserData(parser),
312 T(" byte=\"%" XML_FMT_INT_MOD "d\" nbytes=\"%d\" \
313 line=\"%" XML_FMT_INT_MOD "u\" col=\"%" XML_FMT_INT_MOD "u\""),
314 XML_GetCurrentByteIndex(parser),
315 XML_GetCurrentByteCount(parser),
316 XML_GetCurrentLineNumber(parser),
317 XML_GetCurrentColumnNumber(parser));
321 metaStartDocument(void *userData)
323 fputts(T("<document>\n"), (FILE *)XML_GetUserData((XML_Parser) userData));
327 metaEndDocument(void *userData)
329 fputts(T("</document>\n"), (FILE *)XML_GetUserData((XML_Parser) userData));
333 metaStartElement(void *userData, const XML_Char *name,
334 const XML_Char **atts)
336 XML_Parser parser = (XML_Parser) userData;
337 FILE *fp = (FILE *)XML_GetUserData(parser);
338 const XML_Char **specifiedAttsEnd
339 = atts + XML_GetSpecifiedAttributeCount(parser);
340 const XML_Char **idAttPtr;
341 int idAttIndex = XML_GetIdAttributeIndex(parser);
345 idAttPtr = atts + idAttIndex;
347 ftprintf(fp, T("<starttag name=\"%s\""), name);
348 metaLocation(parser);
350 fputts(T(">\n"), fp);
352 ftprintf(fp, T("<attribute name=\"%s\" value=\""), atts[0]);
353 characterData(fp, atts[1], (int)tcslen(atts[1]));
354 if (atts >= specifiedAttsEnd)
355 fputts(T("\" defaulted=\"yes\"/>\n"), fp);
356 else if (atts == idAttPtr)
357 fputts(T("\" id=\"yes\"/>\n"), fp);
359 fputts(T("\"/>\n"), fp);
360 } while (*(atts += 2));
361 fputts(T("</starttag>\n"), fp);
364 fputts(T("/>\n"), fp);
368 metaEndElement(void *userData, const XML_Char *name)
370 XML_Parser parser = (XML_Parser) userData;
371 FILE *fp = (FILE *)XML_GetUserData(parser);
372 ftprintf(fp, T("<endtag name=\"%s\""), name);
373 metaLocation(parser);
374 fputts(T("/>\n"), fp);
378 metaProcessingInstruction(void *userData, const XML_Char *target,
379 const XML_Char *data)
381 XML_Parser parser = (XML_Parser) userData;
382 FILE *fp = (FILE *)XML_GetUserData(parser);
383 ftprintf(fp, T("<pi target=\"%s\" data=\""), target);
384 characterData(fp, data, (int)tcslen(data));
386 metaLocation(parser);
387 fputts(T("/>\n"), fp);
391 metaComment(void *userData, const XML_Char *data)
393 XML_Parser parser = (XML_Parser) userData;
394 FILE *fp = (FILE *)XML_GetUserData(parser);
395 fputts(T("<comment data=\""), fp);
396 characterData(fp, data, (int)tcslen(data));
398 metaLocation(parser);
399 fputts(T("/>\n"), fp);
403 metaStartCdataSection(void *userData)
405 XML_Parser parser = (XML_Parser) userData;
406 FILE *fp = (FILE *)XML_GetUserData(parser);
407 fputts(T("<startcdata"), fp);
408 metaLocation(parser);
409 fputts(T("/>\n"), fp);
413 metaEndCdataSection(void *userData)
415 XML_Parser parser = (XML_Parser) userData;
416 FILE *fp = (FILE *)XML_GetUserData(parser);
417 fputts(T("<endcdata"), fp);
418 metaLocation(parser);
419 fputts(T("/>\n"), fp);
423 metaCharacterData(void *userData, const XML_Char *s, int len)
425 XML_Parser parser = (XML_Parser) userData;
426 FILE *fp = (FILE *)XML_GetUserData(parser);
427 fputts(T("<chars str=\""), fp);
428 characterData(fp, s, len);
430 metaLocation(parser);
431 fputts(T("/>\n"), fp);
435 metaStartDoctypeDecl(void *userData,
436 const XML_Char *doctypeName,
437 const XML_Char *sysid,
438 const XML_Char *pubid,
439 int has_internal_subset)
441 XML_Parser parser = (XML_Parser) userData;
442 FILE *fp = (FILE *)XML_GetUserData(parser);
443 ftprintf(fp, T("<startdoctype name=\"%s\""), doctypeName);
444 metaLocation(parser);
445 fputts(T("/>\n"), fp);
449 metaEndDoctypeDecl(void *userData)
451 XML_Parser parser = (XML_Parser) userData;
452 FILE *fp = (FILE *)XML_GetUserData(parser);
453 fputts(T("<enddoctype"), fp);
454 metaLocation(parser);
455 fputts(T("/>\n"), fp);
459 metaNotationDecl(void *userData,
460 const XML_Char *notationName,
461 const XML_Char *base,
462 const XML_Char *systemId,
463 const XML_Char *publicId)
465 XML_Parser parser = (XML_Parser) userData;
466 FILE *fp = (FILE *)XML_GetUserData(parser);
467 ftprintf(fp, T("<notation name=\"%s\""), notationName);
469 ftprintf(fp, T(" public=\"%s\""), publicId);
471 fputts(T(" system=\""), fp);
472 characterData(fp, systemId, (int)tcslen(systemId));
475 metaLocation(parser);
476 fputts(T("/>\n"), fp);
481 metaEntityDecl(void *userData,
482 const XML_Char *entityName,
484 const XML_Char *value,
486 const XML_Char *base,
487 const XML_Char *systemId,
488 const XML_Char *publicId,
489 const XML_Char *notationName)
491 XML_Parser parser = (XML_Parser) userData;
492 FILE *fp = (FILE *)XML_GetUserData(parser);
495 ftprintf(fp, T("<entity name=\"%s\""), entityName);
496 metaLocation(parser);
498 characterData(fp, value, value_length);
499 fputts(T("</entity/>\n"), fp);
501 else if (notationName) {
502 ftprintf(fp, T("<entity name=\"%s\""), entityName);
504 ftprintf(fp, T(" public=\"%s\""), publicId);
505 fputts(T(" system=\""), fp);
506 characterData(fp, systemId, (int)tcslen(systemId));
508 ftprintf(fp, T(" notation=\"%s\""), notationName);
509 metaLocation(parser);
510 fputts(T("/>\n"), fp);
513 ftprintf(fp, T("<entity name=\"%s\""), entityName);
515 ftprintf(fp, T(" public=\"%s\""), publicId);
516 fputts(T(" system=\""), fp);
517 characterData(fp, systemId, (int)tcslen(systemId));
519 metaLocation(parser);
520 fputts(T("/>\n"), fp);
525 metaStartNamespaceDecl(void *userData,
526 const XML_Char *prefix,
529 XML_Parser parser = (XML_Parser) userData;
530 FILE *fp = (FILE *)XML_GetUserData(parser);
531 fputts(T("<startns"), fp);
533 ftprintf(fp, T(" prefix=\"%s\""), prefix);
535 fputts(T(" ns=\""), fp);
536 characterData(fp, uri, (int)tcslen(uri));
537 fputts(T("\"/>\n"), fp);
540 fputts(T("/>\n"), fp);
544 metaEndNamespaceDecl(void *userData, const XML_Char *prefix)
546 XML_Parser parser = (XML_Parser) userData;
547 FILE *fp = (FILE *)XML_GetUserData(parser);
549 fputts(T("<endns/>\n"), fp);
551 ftprintf(fp, T("<endns prefix=\"%s\"/>\n"), prefix);
555 unknownEncodingConvert(void *data, const char *p)
557 return codepageConvert(*(int *)data, p);
561 unknownEncoding(void *userData, const XML_Char *name, XML_Encoding *info)
564 static const XML_Char prefixL[] = T("windows-");
565 static const XML_Char prefixU[] = T("WINDOWS-");
568 for (i = 0; prefixU[i]; i++)
569 if (name[i] != prefixU[i] && name[i] != prefixL[i])
573 for (; name[i]; i++) {
574 static const XML_Char digits[] = T("0123456789");
575 const XML_Char *s = tcschr(digits, name[i]);
579 cp += (int)(s - digits);
583 if (!codepageMap(cp, info->map))
585 info->convert = unknownEncodingConvert;
586 /* We could just cast the code page integer to a void *,
587 and avoid the use of release. */
588 info->release = free;
589 info->data = malloc(sizeof(int));
592 *(int *)info->data = cp;
597 notStandalone(void *userData)
603 showVersion(XML_Char *prog)
607 const XML_Feature *features = XML_GetFeatureList();
608 while ((ch = *s) != 0) {
610 #if (defined(WIN32) || defined(__WATCOMC__))
617 ftprintf(stdout, T("%s using %s\n"), prog, XML_ExpatVersion());
618 if (features != NULL && features[0].feature != XML_FEATURE_END) {
620 ftprintf(stdout, T("%s"), features[0].name);
621 if (features[0].value)
622 ftprintf(stdout, T("=%ld"), features[0].value);
623 while (features[i].feature != XML_FEATURE_END) {
624 ftprintf(stdout, T(", %s"), features[i].name);
625 if (features[i].value)
626 ftprintf(stdout, T("=%ld"), features[i].value);
629 ftprintf(stdout, T("\n"));
634 usage(const XML_Char *prog, int rc)
637 T("usage: %s [-n] [-p] [-r] [-s] [-w] [-x] [-d output-dir] "
638 "[-e encoding] file ...\n"), prog);
643 tmain(int argc, XML_Char **argv)
646 const XML_Char *outputDir = NULL;
647 const XML_Char *encoding = NULL;
648 unsigned processFlags = XML_MAP_FILE;
649 int windowsCodePages = 0;
651 int useNamespaces = 0;
652 int requireStandalone = 0;
653 enum XML_ParamEntityParsing paramEntityParsing =
654 XML_PARAM_ENTITY_PARSING_NEVER;
658 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF);
665 if (argv[i][0] != T('-'))
667 if (argv[i][1] == T('-') && argv[i][2] == T('\0')) {
673 switch (argv[i][j]) {
675 processFlags &= ~XML_MAP_FILE;
679 requireStandalone = 1;
687 paramEntityParsing = XML_PARAM_ENTITY_PARSING_ALWAYS;
690 processFlags |= XML_EXTERNAL_ENTITIES;
694 windowsCodePages = 1;
711 if (argv[i][j + 1] == T('\0')) {
717 outputDir = argv[i] + j + 1;
722 if (argv[i][j + 1] == T('\0')) {
728 encoding = argv[i] + j + 1;
736 showVersion(argv[0]);
751 processFlags &= ~XML_MAP_FILE;
754 for (; i < argc; i++) {
756 XML_Char *outName = 0;
760 parser = XML_ParserCreateNS(encoding, NSSEP);
762 parser = XML_ParserCreate(encoding);
763 if (requireStandalone)
764 XML_SetNotStandaloneHandler(parser, notStandalone);
765 XML_SetParamEntityParsing(parser, paramEntityParsing);
766 if (outputType == 't') {
767 /* This is for doing timings; this gives a more realistic estimate of
770 XML_SetElementHandler(parser, nopStartElement, nopEndElement);
771 XML_SetCharacterDataHandler(parser, nopCharacterData);
772 XML_SetProcessingInstructionHandler(parser, nopProcessingInstruction);
774 else if (outputDir) {
775 const XML_Char * delim = T("/");
776 const XML_Char *file = useStdin ? T("STDIN") : argv[i];
778 /* Jump after last (back)slash */
779 const XML_Char * lastDelim = tcsrchr(file, delim[0]);
781 file = lastDelim + 1;
782 #if (defined(WIN32) || defined(__WATCOMC__))
784 const XML_Char * winDelim = T("\\");
785 lastDelim = tcsrchr(file, winDelim[0]);
787 file = lastDelim + 1;
793 outName = (XML_Char *)malloc((tcslen(outputDir) + tcslen(file) + 2)
795 tcscpy(outName, outputDir);
796 tcscat(outName, delim);
797 tcscat(outName, file);
798 fp = tfopen(outName, T("wb"));
803 setvbuf(fp, NULL, _IOFBF, 16384);
807 XML_SetUserData(parser, fp);
808 switch (outputType) {
810 XML_UseParserAsHandlerArg(parser);
811 XML_SetElementHandler(parser, metaStartElement, metaEndElement);
812 XML_SetProcessingInstructionHandler(parser, metaProcessingInstruction);
813 XML_SetCommentHandler(parser, metaComment);
814 XML_SetCdataSectionHandler(parser, metaStartCdataSection,
815 metaEndCdataSection);
816 XML_SetCharacterDataHandler(parser, metaCharacterData);
817 XML_SetDoctypeDeclHandler(parser, metaStartDoctypeDecl,
819 XML_SetEntityDeclHandler(parser, metaEntityDecl);
820 XML_SetNotationDeclHandler(parser, metaNotationDecl);
821 XML_SetNamespaceDeclHandler(parser, metaStartNamespaceDecl,
822 metaEndNamespaceDecl);
823 metaStartDocument(parser);
826 XML_UseParserAsHandlerArg(parser);
827 XML_SetDefaultHandler(parser, markup);
828 XML_SetElementHandler(parser, defaultStartElement, defaultEndElement);
829 XML_SetCharacterDataHandler(parser, defaultCharacterData);
830 XML_SetProcessingInstructionHandler(parser,
831 defaultProcessingInstruction);
835 XML_SetElementHandler(parser, startElementNS, endElementNS);
837 XML_SetElementHandler(parser, startElement, endElement);
838 XML_SetCharacterDataHandler(parser, characterData);
840 XML_SetProcessingInstructionHandler(parser, processingInstruction);
841 #endif /* not W3C14N */
845 if (windowsCodePages)
846 XML_SetUnknownEncodingHandler(parser, unknownEncoding, 0);
847 result = XML_ProcessFile(parser, useStdin ? NULL : argv[i], processFlags);
849 if (outputType == 'm')
850 metaEndDocument(parser);
858 XML_ParserFree(parser);