18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
49b50d902SRodney W. Grimes * Copyright (c) 1987, 1993, 1994
59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved.
69b50d902SRodney W. Grimes *
79b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes * are met:
109b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes * without specific prior written permission.
189b50d902SRodney W. Grimes *
199b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes * SUCH DAMAGE.
309b50d902SRodney W. Grimes */
319b50d902SRodney W. Grimes
329f5b04e9SDavid Malone #include <sys/cdefs.h>
339b50d902SRodney W. Grimes #include <err.h>
349b50d902SRodney W. Grimes #include <limits.h>
359b50d902SRodney W. Grimes #include <stdio.h>
369b50d902SRodney W. Grimes #include <stdlib.h>
379b50d902SRodney W. Grimes #include <string.h>
389b50d902SRodney W. Grimes
399b50d902SRodney W. Grimes #include "ctags.h"
409b50d902SRodney W. Grimes
41f1bb2cd2SWarner Losh static void add_node(NODE *, NODE *);
42f1bb2cd2SWarner Losh static void free_tree(NODE *);
439b50d902SRodney W. Grimes
449b50d902SRodney W. Grimes /*
459b50d902SRodney W. Grimes * pfnote --
469b50d902SRodney W. Grimes * enter a new node in the tree
479b50d902SRodney W. Grimes */
489b50d902SRodney W. Grimes void
pfnote(const char * name,int ln)49c10b37bfSDavid Malone pfnote(const char *name, int ln)
509b50d902SRodney W. Grimes {
519b50d902SRodney W. Grimes NODE *np;
529b50d902SRodney W. Grimes char *fp;
539b50d902SRodney W. Grimes char nbuf[MAXTOKEN];
549b50d902SRodney W. Grimes
559b50d902SRodney W. Grimes /*NOSTRICT*/
569b50d902SRodney W. Grimes if (!(np = (NODE *)malloc(sizeof(NODE)))) {
579b50d902SRodney W. Grimes warnx("too many entries to sort");
589b50d902SRodney W. Grimes put_entries(head);
599b50d902SRodney W. Grimes free_tree(head);
609b50d902SRodney W. Grimes /*NOSTRICT*/
619b50d902SRodney W. Grimes if (!(head = np = (NODE *)malloc(sizeof(NODE))))
6293b3633bSPhilippe Charnier errx(1, "out of space");
639b50d902SRodney W. Grimes }
649b50d902SRodney W. Grimes if (!xflag && !strcmp(name, "main")) {
659b50d902SRodney W. Grimes if (!(fp = strrchr(curfile, '/')))
669b50d902SRodney W. Grimes fp = curfile;
679b50d902SRodney W. Grimes else
689b50d902SRodney W. Grimes ++fp;
69e58bac2eSTim J. Robbins (void)snprintf(nbuf, sizeof(nbuf), "M%s", fp);
709b50d902SRodney W. Grimes fp = strrchr(nbuf, '.');
719b50d902SRodney W. Grimes if (fp && !fp[2])
729b50d902SRodney W. Grimes *fp = EOS;
739b50d902SRodney W. Grimes name = nbuf;
749b50d902SRodney W. Grimes }
759b50d902SRodney W. Grimes if (!(np->entry = strdup(name)))
769b50d902SRodney W. Grimes err(1, NULL);
779b50d902SRodney W. Grimes np->file = curfile;
789b50d902SRodney W. Grimes np->lno = ln;
799b50d902SRodney W. Grimes np->left = np->right = 0;
809b50d902SRodney W. Grimes if (!(np->pat = strdup(lbuf)))
819b50d902SRodney W. Grimes err(1, NULL);
829b50d902SRodney W. Grimes if (!head)
839b50d902SRodney W. Grimes head = np;
849b50d902SRodney W. Grimes else
859b50d902SRodney W. Grimes add_node(np, head);
869b50d902SRodney W. Grimes }
879b50d902SRodney W. Grimes
889b50d902SRodney W. Grimes static void
add_node(NODE * node,NODE * cur_node)89c10b37bfSDavid Malone add_node(NODE *node, NODE *cur_node)
909b50d902SRodney W. Grimes {
919b50d902SRodney W. Grimes int dif;
929b50d902SRodney W. Grimes
93df111ddeSTim J. Robbins dif = strcoll(node->entry, cur_node->entry);
949b50d902SRodney W. Grimes if (!dif) {
959b50d902SRodney W. Grimes if (node->file == cur_node->file) {
969b50d902SRodney W. Grimes if (!wflag)
979b50d902SRodney W. Grimes fprintf(stderr, "Duplicate entry in file %s, line %d: %s\nSecond entry ignored\n", node->file, lineno, node->entry);
989b50d902SRodney W. Grimes return;
999b50d902SRodney W. Grimes }
1009b50d902SRodney W. Grimes if (!cur_node->been_warned)
1019b50d902SRodney W. Grimes if (!wflag)
1029b50d902SRodney W. Grimes fprintf(stderr, "Duplicate entry in files %s and %s: %s (Warning only)\n", node->file, cur_node->file, node->entry);
10387b0195aSCollin Funk cur_node->been_warned = true;
1049b50d902SRodney W. Grimes }
1059b50d902SRodney W. Grimes else if (dif < 0)
1069b50d902SRodney W. Grimes if (cur_node->left)
1079b50d902SRodney W. Grimes add_node(node, cur_node->left);
1089b50d902SRodney W. Grimes else
1099b50d902SRodney W. Grimes cur_node->left = node;
1109b50d902SRodney W. Grimes else if (cur_node->right)
1119b50d902SRodney W. Grimes add_node(node, cur_node->right);
1129b50d902SRodney W. Grimes else
1139b50d902SRodney W. Grimes cur_node->right = node;
1149b50d902SRodney W. Grimes }
1159b50d902SRodney W. Grimes
1169b50d902SRodney W. Grimes static void
free_tree(NODE * node)117c10b37bfSDavid Malone free_tree(NODE *node)
1189b50d902SRodney W. Grimes {
119f6155525SRalf S. Engelschall NODE *node_next;
1209b50d902SRodney W. Grimes while (node) {
1219b50d902SRodney W. Grimes if (node->right)
1229b50d902SRodney W. Grimes free_tree(node->right);
123f6155525SRalf S. Engelschall node_next = node->left;
1249b50d902SRodney W. Grimes free(node);
125f6155525SRalf S. Engelschall node = node_next;
1269b50d902SRodney W. Grimes }
1279b50d902SRodney W. Grimes }
128