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 |
---|
35 | static 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 | */ |
---|
48 | pfnote(name,ln,type) |
---|
49 | char *name; |
---|
50 | int ln; |
---|
51 | int type; |
---|
52 | { |
---|
53 | extern NODE *head; /* head of the sorted binary tree */ |
---|
54 | extern char *curfile; /* current input file name */ |
---|
55 | register NODE *np; |
---|
56 | register char *fp; |
---|
57 | char nbuf[MAXTOKEN]; |
---|
58 | |
---|
59 | /*NOSTRICT*/ |
---|
60 | if (!(np = (NODE *)malloc(sizeof(NODE)))) { |
---|
61 | fputs("ctags: too many entries to sort\n",stderr); |
---|
62 | put_entries(head); |
---|
63 | free_tree(head); |
---|
64 | /*NOSTRICT*/ |
---|
65 | if (!(head = np = (NODE *)malloc(sizeof(NODE)))) { |
---|
66 | fputs("ctags: out of space.\n",stderr); |
---|
67 | exit(1); |
---|
68 | } |
---|
69 | } |
---|
70 | if (!xflag && !strcmp(name,"main")) { |
---|
71 | if (!(fp = rindex(curfile,'/'))) |
---|
72 | fp = curfile; |
---|
73 | else |
---|
74 | ++fp; |
---|
75 | (void)sprintf(nbuf,"M%s",fp); |
---|
76 | fp = rindex(nbuf,'.'); |
---|
77 | if (fp && !fp[2]) |
---|
78 | *fp = EOS; |
---|
79 | name = nbuf; |
---|
80 | } |
---|
81 | if (!(np->entry = strdup(name))) { |
---|
82 | (void)fprintf(stderr, "ctags: %s\n", strerror(errno)); |
---|
83 | exit(1); |
---|
84 | } |
---|
85 | np->file = curfile; |
---|
86 | np->lno = ln; np->symbtype= type; |
---|
87 | np->left = np->right = 0; |
---|
88 | if (!(np->pat = strdup(lbuf))) { |
---|
89 | (void)fprintf(stderr, "ctags: %s\n", strerror(errno)); |
---|
90 | exit(1); |
---|
91 | } |
---|
92 | if (!head) |
---|
93 | head = np; |
---|
94 | else |
---|
95 | add_node(np,head); |
---|
96 | } |
---|
97 | |
---|
98 | add_node(node,cur_node) |
---|
99 | register NODE *node, |
---|
100 | *cur_node; |
---|
101 | { |
---|
102 | extern int wflag; /* -w: suppress warnings */ |
---|
103 | register int dif; |
---|
104 | |
---|
105 | dif = strcmp(node->entry,cur_node->entry); |
---|
106 | if (!dif) { |
---|
107 | if (node->file == cur_node->file) { |
---|
108 | if (!wflag) |
---|
109 | fprintf(stderr,"Duplicate entry in file %s, line %d: %s\nSecond entry ignored\n",node->file,lineno,node->entry); |
---|
110 | return; |
---|
111 | } |
---|
112 | if (!cur_node->been_warned) |
---|
113 | if (!wflag) |
---|
114 | fprintf(stderr,"Duplicate entry in files %s and %s: %s (Warning only)\n",node->file,cur_node->file,node->entry); |
---|
115 | cur_node->been_warned = YES; |
---|
116 | } |
---|
117 | else if (dif < 0) |
---|
118 | if (cur_node->left) |
---|
119 | add_node(node,cur_node->left); |
---|
120 | else |
---|
121 | cur_node->left = node; |
---|
122 | else if (cur_node->right) |
---|
123 | add_node(node,cur_node->right); |
---|
124 | else |
---|
125 | cur_node->right = node; |
---|
126 | } |
---|
127 | |
---|
128 | free_tree(node) |
---|
129 | register NODE *node; |
---|
130 | { |
---|
131 | NODE *nl; |
---|
132 | while (node) { |
---|
133 | if (node->right) |
---|
134 | free_tree(node->right); |
---|
135 | nl= node->left; free(node); |
---|
136 | node = nl; |
---|
137 | } |
---|
138 | } |
---|