386BSD 0.1 development
[unix-history] / usr / src / usr.bin / ctags / tree.c
CommitLineData
d9e9dbd6
WJ
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, 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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char sccsid[] = "@(#)tree.c 5.5 (Berkeley) 2/26/91";
36#endif /* not lint */
37
38#include <errno.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include "ctags.h"
43
44/*
45 * pfnote --
46 * enter a new node in the tree
47 */
48pfnote(name,ln)
49 char *name;
50 int ln;
51{
52 extern NODE *head; /* head of the sorted binary tree */
53 extern char *curfile; /* current input file name */
54 register NODE *np;
55 register char *fp;
56 char nbuf[MAXTOKEN];
57
58 /*NOSTRICT*/
59 if (!(np = (NODE *)malloc(sizeof(NODE)))) {
60 fputs("ctags: too many entries to sort\n",stderr);
61 put_entries(head);
62 free_tree(head);
63 /*NOSTRICT*/
64 if (!(head = np = (NODE *)malloc(sizeof(NODE)))) {
65 fputs("ctags: out of space.\n",stderr);
66 exit(1);
67 }
68 }
69 if (!xflag && !strcmp(name,"main")) {
70 if (!(fp = rindex(curfile,'/')))
71 fp = curfile;
72 else
73 ++fp;
74 (void)sprintf(nbuf,"M%s",fp);
75 fp = rindex(nbuf,'.');
76 if (fp && !fp[2])
77 *fp = EOS;
78 name = nbuf;
79 }
80 if (!(np->entry = strdup(name))) {
81 (void)fprintf(stderr, "ctags: %s\n", strerror(errno));
82 exit(1);
83 }
84 np->file = curfile;
85 np->lno = ln;
86 np->left = np->right = 0;
87 if (!(np->pat = strdup(lbuf))) {
88 (void)fprintf(stderr, "ctags: %s\n", strerror(errno));
89 exit(1);
90 }
91 if (!head)
92 head = np;
93 else
94 add_node(np,head);
95}
96
97add_node(node,cur_node)
98 register NODE *node,
99 *cur_node;
100{
101 extern int wflag; /* -w: suppress warnings */
102 register int dif;
103
104 dif = strcmp(node->entry,cur_node->entry);
105 if (!dif) {
106 if (node->file == cur_node->file) {
107 if (!wflag)
108 fprintf(stderr,"Duplicate entry in file %s, line %d: %s\nSecond entry ignored\n",node->file,lineno,node->entry);
109 return;
110 }
111 if (!cur_node->been_warned)
112 if (!wflag)
113 fprintf(stderr,"Duplicate entry in files %s and %s: %s (Warning only)\n",node->file,cur_node->file,node->entry);
114 cur_node->been_warned = YES;
115 }
116 else if (dif < 0)
117 if (cur_node->left)
118 add_node(node,cur_node->left);
119 else
120 cur_node->left = node;
121 else if (cur_node->right)
122 add_node(node,cur_node->right);
123 else
124 cur_node->right = node;
125}
126
127free_tree(node)
128 register NODE *node;
129{
130 while (node) {
131 if (node->right)
132 free_tree(node->right);
133 cfree(node);
134 node = node->left;
135 }
136}