/usr.bin/ctags/tree.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 133 lines · 89 code · 10 blank · 34 comment · 23 complexity · fc870660adeac0facf9be826635dd08d MD5 · raw file

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