1 /* $NetBSD: tree.c,v 1.7 2002/01/31 19:26:35 tv Exp $ */ 2 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 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #if defined(__RCSID) && !defined(lint) 38 #if 0 39 static char sccsid[] = "@(#)tree.c 8.3 (Berkeley) 4/2/94"; 40 #else 41 __RCSID("$NetBSD: tree.c,v 1.7 2002/01/31 19:26:35 tv Exp $"); 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> 50 51 #include "ctags.h" 52 53 static void add_node __P((NODE *, NODE *)); 54 static void free_tree __P((NODE *)); 55 56 /* 57 * pfnote -- 58 * enter a new node in the tree 59 */ 60 void 61 pfnote(name, ln) 62 char *name; 63 int ln; 64 { 65 NODE *np; 66 char *fp; 67 char nbuf[MAXTOKEN]; 68 69 /*NOSTRICT*/ 70 if (!(np = (NODE *)malloc(sizeof(NODE)))) { 71 warnx("too many entries to sort"); 72 put_entries(head); 73 free_tree(head); 74 /*NOSTRICT*/ 75 if (!(head = np = (NODE *)malloc(sizeof(NODE)))) 76 err(1, "out of space"); 77 } 78 if (!xflag && !strcmp(name, "main")) { 79 if (!(fp = strrchr(curfile, '/'))) 80 fp = curfile; 81 else 82 ++fp; 83 (void)sprintf(nbuf, "M%s", fp); 84 fp = strrchr(nbuf, '.'); 85 if (fp && !fp[2]) 86 *fp = EOS; 87 name = nbuf; 88 } 89 if (!(np->entry = strdup(name))) 90 err(1, "strdup"); 91 np->file = curfile; 92 np->lno = ln; 93 np->left = np->right = 0; 94 if (!(np->pat = strdup(lbuf))) 95 err(1, "strdup"); 96 if (!head) 97 head = np; 98 else 99 add_node(np, head); 100 } 101 102 static void 103 add_node(node, cur_node) 104 NODE *node, 105 *cur_node; 106 { 107 int dif; 108 109 dif = strcmp(node->entry, cur_node->entry); 110 if (!dif) { 111 if (node->file == cur_node->file) { 112 if (!wflag) 113 fprintf(stderr, "Duplicate entry in file %s, line %d: %s\nSecond entry ignored\n", node->file, lineno, node->entry); 114 return; 115 } 116 if (!cur_node->been_warned) 117 if (!wflag) 118 fprintf(stderr, "Duplicate entry in files %s and %s: %s (Warning only)\n", node->file, cur_node->file, node->entry); 119 cur_node->been_warned = YES; 120 } 121 else if (dif < 0) { 122 if (cur_node->left) 123 add_node(node, cur_node->left); 124 else 125 cur_node->left = node; 126 } else { 127 if (cur_node->right) 128 add_node(node, cur_node->right); 129 else 130 cur_node->right = node; 131 } 132 } 133 134 static void 135 free_tree(node) 136 NODE *node; 137 { 138 while (node) { 139 if (node->right) 140 free_tree(node->right); 141 free(node); 142 node = node->left; 143 } 144 } 145