Commit | Line | Data |
---|---|---|
6516bc59 | 1 | /* |
374464e3 KB |
2 | * Copyright (c) 1987, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
ffa8a268 | 4 | * |
a3bb5b82 | 5 | * %sccs.include.redist.c% |
6516bc59 KB |
6 | */ |
7 | ||
8 | #ifndef lint | |
d3acad8d | 9 | static char sccsid[] = "@(#)tree.c 8.2 (Berkeley) %G%"; |
ffa8a268 | 10 | #endif /* not lint */ |
6516bc59 | 11 | |
d3acad8d JSP |
12 | #include <err.h> |
13 | #include <limits.h> | |
6c2ce1d3 KB |
14 | #include <stdio.h> |
15 | #include <stdlib.h> | |
38dde0cd | 16 | #include <string.h> |
d3acad8d | 17 | |
6c2ce1d3 | 18 | #include "ctags.h" |
6516bc59 | 19 | |
d3acad8d JSP |
20 | static void add_node __P((NODE *, NODE *)); |
21 | static void free_tree __P((NODE *)); | |
22 | ||
6516bc59 KB |
23 | /* |
24 | * pfnote -- | |
25 | * enter a new node in the tree | |
26 | */ | |
d3acad8d JSP |
27 | void |
28 | pfnote(name, ln) | |
6516bc59 KB |
29 | char *name; |
30 | int ln; | |
31 | { | |
d3acad8d JSP |
32 | NODE *np; |
33 | char *fp; | |
6c2ce1d3 | 34 | char nbuf[MAXTOKEN]; |
6516bc59 KB |
35 | |
36 | /*NOSTRICT*/ | |
37 | if (!(np = (NODE *)malloc(sizeof(NODE)))) { | |
d3acad8d | 38 | warnx("too many entries to sort"); |
6516bc59 KB |
39 | put_entries(head); |
40 | free_tree(head); | |
41 | /*NOSTRICT*/ | |
d3acad8d JSP |
42 | if (!(head = np = (NODE *)malloc(sizeof(NODE)))) |
43 | err(1, "out of space"); | |
6516bc59 | 44 | } |
d3acad8d JSP |
45 | if (!xflag && !strcmp(name, "main")) { |
46 | if (!(fp = strrchr(curfile, '/'))) | |
6516bc59 KB |
47 | fp = curfile; |
48 | else | |
49 | ++fp; | |
d3acad8d JSP |
50 | (void)sprintf(nbuf, "M%s", fp); |
51 | fp = strrchr(nbuf, '.'); | |
6516bc59 KB |
52 | if (fp && !fp[2]) |
53 | *fp = EOS; | |
54 | name = nbuf; | |
55 | } | |
d3acad8d JSP |
56 | if (!(np->entry = strdup(name))) |
57 | err(1, NULL); | |
6516bc59 KB |
58 | np->file = curfile; |
59 | np->lno = ln; | |
60 | np->left = np->right = 0; | |
d3acad8d JSP |
61 | if (!(np->pat = strdup(lbuf))) |
62 | err(1, NULL); | |
6516bc59 KB |
63 | if (!head) |
64 | head = np; | |
65 | else | |
d3acad8d | 66 | add_node(np, head); |
6516bc59 KB |
67 | } |
68 | ||
d3acad8d JSP |
69 | static void |
70 | add_node(node, cur_node) | |
71 | NODE *node, | |
72 | *cur_node; | |
6516bc59 | 73 | { |
d3acad8d | 74 | int dif; |
6516bc59 | 75 | |
d3acad8d | 76 | dif = strcmp(node->entry, cur_node->entry); |
6516bc59 KB |
77 | if (!dif) { |
78 | if (node->file == cur_node->file) { | |
79 | if (!wflag) | |
d3acad8d | 80 | fprintf(stderr, "Duplicate entry in file %s, line %d: %s\nSecond entry ignored\n", node->file, lineno, node->entry); |
6516bc59 KB |
81 | return; |
82 | } | |
83 | if (!cur_node->been_warned) | |
84 | if (!wflag) | |
d3acad8d | 85 | fprintf(stderr, "Duplicate entry in files %s and %s: %s (Warning only)\n", node->file, cur_node->file, node->entry); |
6516bc59 KB |
86 | cur_node->been_warned = YES; |
87 | } | |
88 | else if (dif < 0) | |
89 | if (cur_node->left) | |
d3acad8d | 90 | add_node(node, cur_node->left); |
6516bc59 KB |
91 | else |
92 | cur_node->left = node; | |
93 | else if (cur_node->right) | |
d3acad8d | 94 | add_node(node, cur_node->right); |
6516bc59 KB |
95 | else |
96 | cur_node->right = node; | |
97 | } | |
98 | ||
d3acad8d | 99 | static void |
6516bc59 | 100 | free_tree(node) |
d3acad8d | 101 | NODE *node; |
6516bc59 KB |
102 | { |
103 | while (node) { | |
104 | if (node->right) | |
105 | free_tree(node->right); | |
f15bf86f | 106 | free(node); |
6516bc59 KB |
107 | node = node->left; |
108 | } | |
109 | } |