GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/ctags/tree.c Lines: 38 58 65.5 %
Date: 2017-11-07 Branches: 23 38 60.5 %

Line Branch Exec Source
1
/*	$OpenBSD: tree.c,v 1.13 2015/09/29 03:19:24 guenther Exp $	*/
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. Neither the name of the University nor the names of its contributors
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>
38
#include <sys/dirent.h>
39
40
#include "ctags.h"
41
42
bool	in_preload = NO;
43
44
static void	add_node(NODE *, NODE *);
45
static void	free_tree(NODE *);
46
47
/*
48
 * pfnote --
49
 *	enter a new node in the tree
50
 */
51
void
52
pfnote(char *name, int ln)
53
{
54
	NODE	*np;
55
	char	*fp;
56
14472
	char	nbuf[1+MAXNAMLEN+1];
57
58
7236
	if (!(np = malloc(sizeof(NODE)))) {
59
		warnx("too many entries to sort");
60
		put_entries(head);
61
		free_tree(head);
62
		if (!(head = np = malloc(sizeof(NODE))))
63
			err(1, NULL);
64
	}
65

14472
	if (!xflag && !strcmp(name, "main")) {
66
6
		if (!(fp = strrchr(curfile, '/')))
67
			fp = curfile;
68
		else
69
6
			++fp;
70
6
		(void)snprintf(nbuf, sizeof nbuf, "M%s", fp);
71
6
		fp = strrchr(nbuf, '.');
72

12
		if (fp && !fp[2])
73
6
			*fp = EOS;
74
		name = nbuf;
75
6
	}
76
7236
	if (!(np->entry = strdup(name)))
77
		err(1, NULL);
78
7236
	np->file = curfile;
79
7236
	np->lno = ln;
80
7236
	np->left = np->right = 0;
81
7236
	np->been_warned = NO;
82
7236
	np->dynfile = in_preload;
83
7236
	if (!(np->pat = strdup(lbuf)))
84
		err(1, NULL);
85
7236
	if (!head)
86
3
		head = np;
87
	else
88
7233
		add_node(np, head);
89
7236
}
90
91
static void
92
add_node(NODE *node, NODE *cur_node)
93
{
94
	int	dif;
95
96
233424
	dif = strcmp(node->entry, cur_node->entry);
97
116712
	if (!dif) {
98
357
		if (node->file == cur_node->file) {
99
96
			if (!wflag)
100
				fprintf(stderr, "Duplicate entry in file %s, "
101
				    "line %d: %s\nSecond entry ignored\n",
102
				    node->file, lineno, node->entry);
103
96
			return;
104
		}
105
522
		if (!cur_node->been_warned)
106
261
			if (!wflag)
107
				fprintf(stderr, "Duplicate entry in files %s "
108
				    "and %s: %s (Warning only)\n",
109
				    node->file, cur_node->file, node->entry);
110
261
		cur_node->been_warned = YES;
111
261
	}
112
116355
	else if (dif < 0)
113
43404
		if (cur_node->left)
114
40314
			add_node(node, cur_node->left);
115
		else
116
3090
			cur_node->left = node;
117
72951
	else if (cur_node->right)
118
69165
		add_node(node, cur_node->right);
119
	else
120
3786
		cur_node->right = node;
121
233328
}
122
123
static void
124
free_tree(NODE *node)
125
{
126
	if (node) {
127
		free_tree(node->left);
128
		free_tree(node->right);
129
130
		free(node->entry);
131
		free(node->pat);
132
		if (node->dynfile == YES)
133
			free(node->file);
134
		free(node);
135
	}
136
}