magic-dump.c revision 1.2
1/* $OpenBSD: magic-dump.c,v 1.2 2016/05/01 10:56:03 nicm Exp $ */
2
3/*
4 * Copyright (c) 2015 Nicholas Marriott <nicm@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/types.h>
20
21#include <stdio.h>
22
23#include "magic.h"
24
25static void
26magic_dump_line(struct magic_line *ml, u_int depth)
27{
28	struct magic_line	*child;
29	u_int			 i;
30
31	printf("%u", ml->line);
32	for (i = 0; i < depth; i++)
33		printf(">");
34	if (ml->name != NULL)
35		printf(" %s %s\n", ml->type_string, ml->name);
36	else {
37		printf(" %s/%s%s%s%s [%u]%s\n", ml->type_string,
38		    ml->result == NULL ? "" : ml->result,
39		    ml->mimetype == NULL ? "" : " (",
40		    ml->mimetype == NULL ? "" : ml->mimetype,
41		    ml->mimetype == NULL ? "" : ")",
42		    ml->strength, ml->text ? " (text)" : "");
43	}
44
45	TAILQ_FOREACH(child, &ml->children, entry)
46		magic_dump_line(child, depth + 1);
47
48}
49
50void
51magic_dump(struct magic *m)
52{
53	struct magic_line	*ml;
54
55	RB_FOREACH(ml, magic_tree, &m->tree)
56		magic_dump_line(ml, 0);
57
58	RB_FOREACH(ml, magic_named_tree, &m->named)
59		magic_dump_line(ml, 0);
60}
61