xref: /original-bsd/usr.bin/ctags/tree.c (revision 3413c235)
1 /*
2  * Copyright (c) 1987, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)tree.c	8.2 (Berkeley) 04/01/94";
10 #endif /* not lint */
11 
12 #include <err.h>
13 #include <limits.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 
18 #include "ctags.h"
19 
20 static void	add_node __P((NODE *, NODE *));
21 static void	free_tree __P((NODE *));
22 
23 /*
24  * pfnote --
25  *	enter a new node in the tree
26  */
27 void
28 pfnote(name, ln)
29 	char	*name;
30 	int	ln;
31 {
32 	NODE	*np;
33 	char	*fp;
34 	char	nbuf[MAXTOKEN];
35 
36 	/*NOSTRICT*/
37 	if (!(np = (NODE *)malloc(sizeof(NODE)))) {
38 		warnx("too many entries to sort");
39 		put_entries(head);
40 		free_tree(head);
41 		/*NOSTRICT*/
42 		if (!(head = np = (NODE *)malloc(sizeof(NODE))))
43 			err(1, "out of space");
44 	}
45 	if (!xflag && !strcmp(name, "main")) {
46 		if (!(fp = strrchr(curfile, '/')))
47 			fp = curfile;
48 		else
49 			++fp;
50 		(void)sprintf(nbuf, "M%s", fp);
51 		fp = strrchr(nbuf, '.');
52 		if (fp && !fp[2])
53 			*fp = EOS;
54 		name = nbuf;
55 	}
56 	if (!(np->entry = strdup(name)))
57 		err(1, NULL);
58 	np->file = curfile;
59 	np->lno = ln;
60 	np->left = np->right = 0;
61 	if (!(np->pat = strdup(lbuf)))
62 		err(1, NULL);
63 	if (!head)
64 		head = np;
65 	else
66 		add_node(np, head);
67 }
68 
69 static void
70 add_node(node, cur_node)
71 	NODE	*node,
72 		*cur_node;
73 {
74 	int	dif;
75 
76 	dif = strcmp(node->entry, cur_node->entry);
77 	if (!dif) {
78 		if (node->file == cur_node->file) {
79 			if (!wflag)
80 				fprintf(stderr, "Duplicate entry in file %s, line %d: %s\nSecond entry ignored\n", node->file, lineno, node->entry);
81 			return;
82 		}
83 		if (!cur_node->been_warned)
84 			if (!wflag)
85 				fprintf(stderr, "Duplicate entry in files %s and %s: %s (Warning only)\n", node->file, cur_node->file, node->entry);
86 		cur_node->been_warned = YES;
87 	}
88 	else if (dif < 0)
89 		if (cur_node->left)
90 			add_node(node, cur_node->left);
91 		else
92 			cur_node->left = node;
93 	else if (cur_node->right)
94 		add_node(node, cur_node->right);
95 	else
96 		cur_node->right = node;
97 }
98 
99 static void
100 free_tree(node)
101 	NODE	*node;
102 {
103 	while (node) {
104 		if (node->right)
105 			free_tree(node->right);
106 		free(node);
107 		node = node->left;
108 	}
109 }
110