xref: /original-bsd/usr.bin/ctags/tree.c (revision 262b24ac)
1 /*
2  * Copyright (c) 1987 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)tree.c	5.2 (Berkeley) 12/31/88";
20 #endif /* not lint */
21 
22 #include <ctags.h>
23 #include <strings.h>
24 
25 /*
26  * pfnote --
27  *	enter a new node in the tree
28  */
29 pfnote(name,ln)
30 	char	*name;
31 	int	ln;
32 {
33 	extern NODE	*head;		/* head of the sorted binary tree */
34 	extern char	*curfile;	/* current input file name */
35 	register NODE	*np;
36 	register char	*fp;
37 	char	nbuf[MAXTOKEN],
38 		*malloc(), *savestr();
39 
40 	/*NOSTRICT*/
41 	if (!(np = (NODE *)malloc(sizeof(NODE)))) {
42 		fputs("ctags: too many entries to sort\n",stderr);
43 		put_entries(head);
44 		free_tree(head);
45 		/*NOSTRICT*/
46 		if (!(head = np = (NODE *)malloc(sizeof(NODE)))) {
47 			fputs("ctags: out of space.\n",stderr);
48 			exit(1);
49 		}
50 	}
51 	if (!xflag && !strcmp(name,"main")) {
52 		if (!(fp = rindex(curfile,'/')))
53 			fp = curfile;
54 		else
55 			++fp;
56 		(void)sprintf(nbuf,"M%s",fp);
57 		fp = rindex(nbuf,'.');
58 		if (fp && !fp[2])
59 			*fp = EOS;
60 		name = nbuf;
61 	}
62 	np->entry = savestr(name);
63 	np->file = curfile;
64 	np->lno = ln;
65 	np->left = np->right = 0;
66 	np->pat = savestr(lbuf);
67 	if (!head)
68 		head = np;
69 	else
70 		add_node(np,head);
71 }
72 
73 add_node(node,cur_node)
74 	register NODE	*node,
75 			*cur_node;
76 {
77 	extern int	wflag;			/* -w: suppress warnings */
78 	register int	dif;
79 
80 	dif = strcmp(node->entry,cur_node->entry);
81 	if (!dif) {
82 		if (node->file == cur_node->file) {
83 			if (!wflag)
84 				fprintf(stderr,"Duplicate entry in file %s, line %d: %s\nSecond entry ignored\n",node->file,lineno,node->entry);
85 			return;
86 		}
87 		if (!cur_node->been_warned)
88 			if (!wflag)
89 				fprintf(stderr,"Duplicate entry in files %s and %s: %s (Warning only)\n",node->file,cur_node->file,node->entry);
90 		cur_node->been_warned = YES;
91 	}
92 	else if (dif < 0)
93 		if (cur_node->left)
94 			add_node(node,cur_node->left);
95 		else
96 			cur_node->left = node;
97 	else if (cur_node->right)
98 		add_node(node,cur_node->right);
99 	else
100 		cur_node->right = node;
101 }
102 
103 free_tree(node)
104 	register NODE	*node;
105 {
106 	while (node) {
107 		if (node->right)
108 			free_tree(node->right);
109 		cfree(node);
110 		node = node->left;
111 	}
112 }
113