Annotation of src/usr.bin/ctags/tree.c, Revision 1.12
1.12 ! semarie 1: /* $OpenBSD: tree.c,v 1.11 2015/08/20 22:32:41 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.
1.5 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: #include <err.h>
34: #include <limits.h>
35: #include <stdio.h>
36: #include <stdlib.h>
37: #include <string.h>
1.3 deraadt 38: #include <sys/dirent.h>
1.1 deraadt 39:
40: #include "ctags.h"
41:
1.12 ! semarie 42: bool in_preload = NO;
! 43:
1.4 millert 44: static void add_node(NODE *, NODE *);
45: static void free_tree(NODE *);
1.1 deraadt 46:
47: /*
48: * pfnote --
49: * enter a new node in the tree
50: */
51: void
1.6 deraadt 52: pfnote(char *name, int ln)
1.1 deraadt 53: {
54: NODE *np;
55: char *fp;
1.3 deraadt 56: char nbuf[1+MAXNAMLEN+1];
1.1 deraadt 57:
58: /*NOSTRICT*/
1.11 deraadt 59: if (!(np = malloc(sizeof(NODE)))) {
1.1 deraadt 60: warnx("too many entries to sort");
61: put_entries(head);
62: free_tree(head);
63: /*NOSTRICT*/
1.11 deraadt 64: if (!(head = np = malloc(sizeof(NODE))))
1.7 tom 65: err(1, NULL);
1.1 deraadt 66: }
67: if (!xflag && !strcmp(name, "main")) {
68: if (!(fp = strrchr(curfile, '/')))
69: fp = curfile;
70: else
71: ++fp;
1.3 deraadt 72: (void)snprintf(nbuf, sizeof nbuf, "M%s", fp);
1.1 deraadt 73: fp = strrchr(nbuf, '.');
74: if (fp && !fp[2])
75: *fp = EOS;
76: name = nbuf;
77: }
78: if (!(np->entry = strdup(name)))
79: err(1, NULL);
80: np->file = curfile;
81: np->lno = ln;
82: np->left = np->right = 0;
1.9 otto 83: np->been_warned = NO;
1.12 ! semarie 84: np->dynfile = in_preload;
1.1 deraadt 85: if (!(np->pat = strdup(lbuf)))
86: err(1, NULL);
87: if (!head)
88: head = np;
89: else
90: add_node(np, head);
91: }
92:
93: static void
1.6 deraadt 94: add_node(NODE *node, NODE *cur_node)
1.1 deraadt 95: {
96: int dif;
97:
98: dif = strcmp(node->entry, cur_node->entry);
99: if (!dif) {
100: if (node->file == cur_node->file) {
101: if (!wflag)
1.8 pat 102: fprintf(stderr, "Duplicate entry in file %s, "
103: "line %d: %s\nSecond entry ignored\n",
104: node->file, lineno, node->entry);
1.1 deraadt 105: return;
106: }
107: if (!cur_node->been_warned)
108: if (!wflag)
1.8 pat 109: fprintf(stderr, "Duplicate entry in files %s "
110: "and %s: %s (Warning only)\n",
111: node->file, cur_node->file, node->entry);
1.1 deraadt 112: cur_node->been_warned = YES;
113: }
114: else if (dif < 0)
115: if (cur_node->left)
116: add_node(node, cur_node->left);
117: else
118: cur_node->left = node;
119: else if (cur_node->right)
120: add_node(node, cur_node->right);
121: else
122: cur_node->right = node;
123: }
124:
125: static void
1.6 deraadt 126: free_tree(NODE *node)
1.1 deraadt 127: {
1.8 pat 128: if (node) {
129: free_tree(node->left);
130: free_tree(node->right);
131:
132: free(node->entry);
133: free(node->pat);
1.12 ! semarie 134: if (node->dynfile == YES)
! 135: free(node->file);
1.1 deraadt 136: free(node);
137: }
138: }