2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7 * and NAI Labs, the Security Research Division of Network Associates, Inc.
8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9 * DARPA CHATS research program.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The names of the authors may not be used to endorse or promote
20 * products derived from this software without specific prior written
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 #include <sys/queue.h>
54 LIST_HEAD(, node) children;
55 LIST_ENTRY(node) siblings;
77 LIST_HEAD(, ref) refs = LIST_HEAD_INITIALIZER(refs);
84 np = calloc(1, sizeof *np);
85 np->cont = sbuf_new_auto();
87 np->key = sbuf_new_auto();
89 LIST_INIT(&np->children);
97 printf("%*.*s", n, n, "");
101 StartElement(void *userData, const char *name, const char **attr)
108 if (!strcmp(name, "FreeBSD")) {
115 for (i = 0; attr[i]; i += 2) {
116 if (!strcmp(attr[i], "id"))
117 np->id = strdup(attr[i+1]);
118 else if (!strcmp(attr[i], "ref"))
119 np->ref = strdup(attr[i+1]);
121 np->name = strdup(name);
122 sbuf_cat(np->key, name);
123 sbuf_cat(np->key, "::");
124 np->parent = mt->cur;
125 LIST_INSERT_HEAD(&mt->cur->children, np, siblings);
130 EndElement(void *userData, const char *name __unused)
140 sbuf_finish(mt->cur->cont);
141 LIST_FOREACH(np, &mt->cur->children, siblings) {
142 if (strcmp(np->name, "name"))
144 sbuf_cat(mt->cur->key, sbuf_data(np->cont));
147 sbuf_finish(mt->cur->key);
148 mt->cur = mt->cur->parent;
152 CharData(void *userData , const XML_Char *s , int len)
162 while (isspace(*b) && b < e)
164 while (isspace(*e) && e > b)
167 sbuf_bcat(mt->cur->cont, b, e - b + 1);
170 static struct mytree *
171 dofile(char *filename)
180 parser = XML_ParserCreate(NULL);
181 mt = calloc(1, sizeof *mt);
182 mt->top = new_node();
183 mt->top->name = "(top)";
184 mt->top->parent = mt->top;
186 sbuf_finish(mt->top->key);
187 sbuf_finish(mt->top->cont);
188 XML_SetUserData(parser, mt);
189 XML_SetElementHandler(parser, StartElement, EndElement);
190 XML_SetCharacterDataHandler(parser, CharData);
191 fd = open(filename, O_RDONLY);
195 p = mmap(NULL, st.st_size, PROT_READ, MAP_NOCORE|MAP_PRIVATE, fd, 0);
196 i = XML_Parse(parser, p, st.st_size, 1);
198 errx(1, "XML_Parse complained -> %d", i);
199 munmap(p, st.st_size);
201 XML_ParserFree(parser);
202 sbuf_finish(mt->top->cont);
210 print_node(struct node *np)
212 printf("\"%s\" -- \"%s\" -- \"%s\"", np->name, sbuf_data(np->cont), sbuf_data(np->key));
214 printf(" id=\"%s\"", np->id);
216 printf(" ref=\"%s\"", np->ref);
221 print_tree(struct node *np, int n)
225 indent(n); printf("%s id=%s ref=%s\n", np->name, np->id, np->ref);
226 LIST_FOREACH(np1, &np->children, siblings)
227 print_tree(np1, n + 2);
231 sort_node(struct node *np)
233 struct node *np1, *np2;
236 LIST_FOREACH(np1, &np->children, siblings)
239 np1 = LIST_FIRST(&np->children);
244 np2 = LIST_NEXT(np1, siblings);
247 if (strcmp(sbuf_data(np1->key), sbuf_data(np2->key)) > 0) {
248 LIST_REMOVE(np2, siblings);
249 LIST_INSERT_BEFORE(np1, np2, siblings);
259 refcmp(char *r1, char *r2)
263 LIST_FOREACH(r, &refs, next) {
264 if (!strcmp(r1, r->k1))
265 return (strcmp(r2, r->k2));
267 r = calloc(1, sizeof(*r));
270 LIST_INSERT_HEAD(&refs, r, next);
272 fprintf(fsubs, "s/%s/%s/g\n", r1, r2);
278 static int compare_node2(struct node *n1, struct node *n2, int in);
281 compare_node(struct node *n1, struct node *n2, int in)
284 struct node *n1a, *n2a;
286 i = strcmp(n1->name, n2->name);
289 if (n1->id && n2->id)
290 i = refcmp(n1->id, n2->id);
291 else if (n1->id || n2->id)
295 if (n1->ref && n2->ref)
296 i = refcmp(n1->ref, n2->ref);
297 else if (n1->ref || n2->ref)
301 if (!strcmp(n1->name, "ref"))
302 i = refcmp(sbuf_data(n1->cont), sbuf_data(n2->cont));
304 i = strcmp(sbuf_data(n1->cont), sbuf_data(n2->cont));
307 n1a = LIST_FIRST(&n1->children);
308 n2a = LIST_FIRST(&n2->children);
310 if (n1a == NULL && n2a == NULL)
312 if (n1a != NULL && n2a == NULL) {
319 if (n1a == NULL && n2a != NULL) {
326 i = compare_node2(n1a, n2a, in + 2);
329 n1a = LIST_NEXT(n1a, siblings);
330 n2a = LIST_NEXT(n2a, siblings);
336 compare_node2(struct node *n1, struct node *n2, int in)
340 i = compare_node(n1, n2, in);
355 main(int argc, char **argv)
357 struct mytree *t1, *t2;
360 fsubs = fopen("_.subs", "w");
361 setbuf(stdout, NULL);
362 setbuf(stderr, NULL);
364 errx(1, "usage: %s file1 file2", argv[0]);
366 t1 = dofile(argv[1]);
368 errx(2, "XML parser error on file %s", argv[1]);
370 t2 = dofile(argv[2]);
372 errx(2, "XML parser error on file %s", argv[2]);
374 i = compare_node(t1->top, t2->top, 0);