sfeed_xmlenc.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <strings.h>
  4. #include "util.h"
  5. #include "xml.h"
  6. static XMLParser parser;
  7. static int tags;
  8. static void
  9. xmltagstart(XMLParser *p, const char *t, size_t tl)
  10. {
  11. /* optimization: try to find a processing instruction only at the
  12. start of the data at the first few starting tags. */
  13. if (tags++ > 3)
  14. exit(0);
  15. }
  16. static void
  17. xmlattr(XMLParser *p, const char *t, size_t tl, const char *n, size_t nl,
  18. const char *v, size_t vl)
  19. {
  20. if (strcasecmp(t, "?xml") || strcasecmp(n, "encoding"))
  21. return;
  22. for (; *v; v++) {
  23. if (ISALPHA((unsigned char)*v) ||
  24. ISDIGIT((unsigned char)*v) ||
  25. *v == '.' || *v == ':' || *v == '-' || *v == '_')
  26. putchar(TOLOWER((unsigned char)*v));
  27. }
  28. }
  29. static void
  30. xmlattrend(XMLParser *p, const char *t, size_t tl, const char *n, size_t nl)
  31. {
  32. if (strcasecmp(t, "?xml") || strcasecmp(n, "encoding"))
  33. return;
  34. putchar('\n');
  35. exit(0);
  36. }
  37. int
  38. main(void)
  39. {
  40. if (pledge("stdio", NULL) == -1)
  41. err(1, "pledge");
  42. parser.xmlattr = xmlattr;
  43. parser.xmlattrentity = xmlattr; /* no entity conversion */
  44. parser.xmlattrend = xmlattrend;
  45. parser.xmltagstart = xmltagstart;
  46. /* NOTE: getnext is defined in xml.h for inline optimization */
  47. xml_parse(&parser);
  48. checkfileerror(stdin, "<stdin>", 'r');
  49. checkfileerror(stdout, "<stdout>", 'w');
  50. return 0;
  51. }