summaryrefslogtreecommitdiff
path: root/sfeed_xmlenc.c
blob: 52a0be34bcd9bc9bbc974ee3b139cee909f9f28b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <sys/types.h>

#include <ctype.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

#include "util.h"
#include "xml.h"

static XMLParser parser;
static int isxmlpi, tags;

static void
xmltagstart(XMLParser *p, const char *tag, size_t taglen)
{
	/* optimization: try to find processing instruction at start */
	if (tags > 3)
		exit(1);
	isxmlpi = (!strncasecmp(tag, "?xml", taglen)) ? 1 : 0;
	tags++;
}

static void
xmltagend(XMLParser *p, const char *tag, size_t taglen, int isshort)
{
	isxmlpi = 0;
}

static void
xmlattr(XMLParser *p, const char *tag, size_t taglen, const char *name,
	size_t namelen, const char *value, size_t valuelen)
{
	if (isxmlpi && !strcasecmp(name, "encoding")) {
		if (*value) {
			/* output lowercase */
			for (; *value; value++)
				putchar(tolower((unsigned char)*value));
			putchar('\n');
		}
		exit(0);
	}
}

int
main(void)
{
	if (pledge("stdio", NULL) == -1)
		err(1, "pledge");

	parser.xmlattr = xmlattr;
	parser.xmltagend = xmltagend;
	parser.xmltagstart = xmltagstart;

	parser.getnext = getchar;
	xml_parse(&parser);

	return 1;
}