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