[BACK]Return to tree.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ctags

Annotation of src/usr.bin/ctags/tree.c, Revision 1.2

1.2     ! deraadt     1: /*     $OpenBSD: tree.c,v 1.4 1995/03/26 20:14:11 glass 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.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
                     38: #if 0
                     39: static char sccsid[] = "@(#)tree.c     8.3 (Berkeley) 4/2/94";
                     40: #else
1.2     ! deraadt    41: static char rcsid[] = "$OpenBSD: tree.c,v 1.4 1995/03/26 20:14:11 glass Exp $";
1.1       deraadt    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, NULL);
                     91:        np->file = curfile;
                     92:        np->lno = ln;
                     93:        np->left = np->right = 0;
                     94:        if (!(np->pat = strdup(lbuf)))
                     95:                err(1, NULL);
                     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 if (cur_node->right)
                    127:                add_node(node, cur_node->right);
                    128:        else
                    129:                cur_node->right = node;
                    130: }
                    131:
                    132: static void
                    133: free_tree(node)
                    134:        NODE    *node;
                    135: {
                    136:        while (node) {
                    137:                if (node->right)
                    138:                        free_tree(node->right);
                    139:                free(node);
                    140:                node = node->left;
                    141:        }
                    142: }