-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfb2ms.c
155 lines (138 loc) · 2.59 KB
/
fb2ms.c
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <err.h>
#include <expat.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "element.h"
#include "handler.h"
#include "util.h"
#define LEN(X) (sizeof X / sizeof X[0])
Elements els;
int repeat = 0;
char *curdata = 0;
int linkflag = 0;
static void
givedata()
{
int i;
if(curdata == 0)
return;
for(i = 0; i < LEN(handlers); i++) {
if(strcmp(handlers[i].name, curel(&els)) == 0) {
if(handlers[i].fdata == 0)
break;
handlers[i].fdata(curdata);
free(curdata);
curdata = 0;
}
}
}
static void XMLCALL
startel(void *ud, const XML_Char *name, const XML_Char **atts)
{
int i;
givedata();
addel(&els, name);
repeat = 0;
for(i = 0; i < LEN(handlers); i++) {
if(strcmp(handlers[i].name, name) == 0) {
if(handlers[i].fstart == 0)
break;
handlers[i].fstart((char **)atts);
}
}
}
static void XMLCALL
endel(void *ud, const XML_Char *name)
{
int i;
givedata();
for(i = 0; i < LEN(handlers); i++) {
if(strcmp(handlers[i].name, name) == 0) {
if(handlers[i].fend == 0)
break;
handlers[i].fend();
}
}
rmel(&els);
repeat = 0;
}
static void
datahandler(void *ud, const XML_Char *s, int l)
{
int i, newlen;
for(i = 0; i < LEN(handlers); i++) {
if(strcmp(handlers[i].name, curel(&els)) == 0) {
if(handlers[i].fdata == 0)
break;
if(repeat) {
newlen = strlen(curdata) + l + 1;
curdata = erealloc(curdata, newlen + 1);
strncat(curdata, s, l);
curdata[newlen] = '\0';
}
else {
curdata = emalloc(l + 1);
strncpy(curdata, s, l);
curdata[l] = '\0';
}
repeat = 1;
}
}
}
static int
fb2ms(FILE *f)
{
char buf[BUFSIZ];
int done;
int len;
XML_Parser parser;
parser = XML_ParserCreate("UTF-8");
XML_SetElementHandler(parser, startel, endel);
XML_SetCharacterDataHandler(parser, datahandler);
do {
len = fread(buf, 1, sizeof(buf), f);
done = len < sizeof(buf);
if(XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR)
return 1;
} while(!done);
XML_ParserFree(parser);
return 0;
}
static void
usage(char *pn)
{
fprintf(stderr, "usage: %s [-l] [file ...]\n", pn);
exit(1);
}
int
main(int argc, char *argv[])
{
FILE *f;
int i, ch;
memset(&els, 0, sizeof(Elements));
while((ch = getopt(argc, argv, "l")) != -1) {
switch(ch) {
case 'l':
linkflag = 1;
break;
default:
usage(argv[0]);
}
}
if(argc - optind == 0) {
if(fb2ms(stdin) != 0)
warnx("XML parsing error");
return 0;
}
for(i = optind; i < argc; i++) {
f = fopen(argv[i], "r");
if(f == 0) {
warn("%s", argv[i]);
continue;
}
if(fb2ms(f) != 0)
warnx("%s: XML parsing error", argv[i]);
}
return 0;
}