xref: /dragonfly/usr.bin/ctags/tree.c (revision c311ab13)
1 /*
2  * Copyright (c) 1987, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)tree.c	8.3 (Berkeley) 4/2/94
30  * $FreeBSD: src/usr.bin/ctags/tree.c,v 1.2.6.2 2002/07/30 00:55:07 tjr Exp $
31  * $DragonFly: src/usr.bin/ctags/tree.c,v 1.2 2003/06/17 04:29:25 dillon Exp $
32  */
33 
34 #include <err.h>
35 #include <limits.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 #include "ctags.h"
41 
42 static void	add_node(NODE *, NODE *);
43 static void	free_tree(NODE *);
44 
45 /*
46  * pfnote --
47  *	enter a new node in the tree
48  */
49 void
50 pfnote(const char *name, int ln)
51 {
52 	NODE	*np;
53 	char	*fp;
54 	char	nbuf[MAXTOKEN];
55 
56 	/*NOSTRICT*/
57 	if (!(np = (NODE *)malloc(sizeof(NODE)))) {
58 		warnx("too many entries to sort");
59 		put_entries(head);
60 		free_tree(head);
61 		/*NOSTRICT*/
62 		if (!(head = np = (NODE *)malloc(sizeof(NODE))))
63 			errx(1, "out of space");
64 	}
65 	if (!xflag && !strcmp(name, "main")) {
66 		if (!(fp = strrchr(curfile, '/')))
67 			fp = curfile;
68 		else
69 			++fp;
70 		(void)snprintf(nbuf, sizeof(nbuf), "M%s", fp);
71 		fp = strrchr(nbuf, '.');
72 		if (fp && !fp[2])
73 			*fp = EOS;
74 		name = nbuf;
75 	}
76 	if (!(np->entry = strdup(name)))
77 		err(1, NULL);
78 	np->file = curfile;
79 	np->lno = ln;
80 	np->left = np->right = 0;
81 	if (!(np->pat = strdup(lbuf)))
82 		err(1, NULL);
83 	if (!head)
84 		head = np;
85 	else
86 		add_node(np, head);
87 }
88 
89 static void
90 add_node(NODE *node, NODE *cur_node)
91 {
92 	int	dif;
93 
94 	dif = strcoll(node->entry, cur_node->entry);
95 	if (!dif) {
96 		if (node->file == cur_node->file) {
97 			if (!wflag)
98 				fprintf(stderr, "Duplicate entry in file %s, line %d: %s\nSecond entry ignored\n", node->file, lineno, node->entry);
99 			return;
100 		}
101 		if (!cur_node->been_warned)
102 			if (!wflag)
103 				fprintf(stderr, "Duplicate entry in files %s and %s: %s (Warning only)\n", node->file, cur_node->file, node->entry);
104 		cur_node->been_warned = YES;
105 	}
106 	else if (dif < 0)
107 		if (cur_node->left)
108 			add_node(node, cur_node->left);
109 		else
110 			cur_node->left = node;
111 	else if (cur_node->right)
112 		add_node(node, cur_node->right);
113 	else
114 		cur_node->right = node;
115 }
116 
117 static void
118 free_tree(NODE *node)
119 {
120 	while (node) {
121 		if (node->right)
122 			free_tree(node->right);
123 		free(node);
124 		node = node->left;
125 	}
126 }
127