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