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