Annotation of src/usr.bin/ctags/tree.c, Revision 1.4
1.4 ! millert 1: /* $OpenBSD: tree.c,v 1.3 2000/07/25 19:28:30 deraadt Exp $ */
1.1 deraadt 2: /* $NetBSD: tree.c,v 1.4 1995/03/26 20:14:11 glass Exp $ */
3:
4: /*
5: * Copyright (c) 1987, 1993, 1994
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: #if 0
39: static char sccsid[] = "@(#)tree.c 8.3 (Berkeley) 4/2/94";
40: #else
1.4 ! millert 41: static char rcsid[] = "$OpenBSD: tree.c,v 1.3 2000/07/25 19:28:30 deraadt Exp $";
1.1 deraadt 42: #endif
43: #endif /* not lint */
44:
45: #include <err.h>
46: #include <limits.h>
47: #include <stdio.h>
48: #include <stdlib.h>
49: #include <string.h>
1.3 deraadt 50: #include <sys/dirent.h>
1.1 deraadt 51:
52: #include "ctags.h"
53:
1.4 ! millert 54: static void add_node(NODE *, NODE *);
! 55: static void free_tree(NODE *);
1.1 deraadt 56:
57: /*
58: * pfnote --
59: * enter a new node in the tree
60: */
61: void
62: pfnote(name, ln)
63: char *name;
64: int ln;
65: {
66: NODE *np;
67: char *fp;
1.3 deraadt 68: char nbuf[1+MAXNAMLEN+1];
1.1 deraadt 69:
70: /*NOSTRICT*/
71: if (!(np = (NODE *)malloc(sizeof(NODE)))) {
72: warnx("too many entries to sort");
73: put_entries(head);
74: free_tree(head);
75: /*NOSTRICT*/
76: if (!(head = np = (NODE *)malloc(sizeof(NODE))))
77: err(1, "out of space");
78: }
79: if (!xflag && !strcmp(name, "main")) {
80: if (!(fp = strrchr(curfile, '/')))
81: fp = curfile;
82: else
83: ++fp;
1.3 deraadt 84: (void)snprintf(nbuf, sizeof nbuf, "M%s", fp);
1.1 deraadt 85: fp = strrchr(nbuf, '.');
86: if (fp && !fp[2])
87: *fp = EOS;
88: name = nbuf;
89: }
90: if (!(np->entry = strdup(name)))
91: err(1, NULL);
92: np->file = curfile;
93: np->lno = ln;
94: np->left = np->right = 0;
95: if (!(np->pat = strdup(lbuf)))
96: err(1, NULL);
97: if (!head)
98: head = np;
99: else
100: add_node(np, head);
101: }
102:
103: static void
104: add_node(node, cur_node)
105: NODE *node,
106: *cur_node;
107: {
108: int dif;
109:
110: dif = strcmp(node->entry, cur_node->entry);
111: if (!dif) {
112: if (node->file == cur_node->file) {
113: if (!wflag)
114: fprintf(stderr, "Duplicate entry in file %s, line %d: %s\nSecond entry ignored\n", node->file, lineno, node->entry);
115: return;
116: }
117: if (!cur_node->been_warned)
118: if (!wflag)
119: fprintf(stderr, "Duplicate entry in files %s and %s: %s (Warning only)\n", node->file, cur_node->file, node->entry);
120: cur_node->been_warned = YES;
121: }
122: else if (dif < 0)
123: if (cur_node->left)
124: add_node(node, cur_node->left);
125: else
126: cur_node->left = node;
127: else if (cur_node->right)
128: add_node(node, cur_node->right);
129: else
130: cur_node->right = node;
131: }
132:
133: static void
134: free_tree(node)
135: NODE *node;
136: {
137: while (node) {
138: if (node->right)
139: free_tree(node->right);
140: free(node);
141: node = node->left;
142: }
143: }