tree.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "rc.h"
  10. #include "exec.h"
  11. #include "io.h"
  12. #include "fns.h"
  13. tree *treenodes;
  14. /*
  15. * create and clear a new tree node, and add it
  16. * to the node list.
  17. */
  18. tree*
  19. newtree(void)
  20. {
  21. tree *t = new(tree);
  22. t->iskw = 0;
  23. t->str = 0;
  24. t->child[0] = t->child[1] = t->child[2] = 0;
  25. t->next = treenodes;
  26. treenodes = t;
  27. return t;
  28. }
  29. void
  30. freenodes(void)
  31. {
  32. tree *t, *u;
  33. for(t = treenodes;t;t = u){
  34. u = t->next;
  35. if(t->str)
  36. efree(t->str);
  37. efree((char *)t);
  38. }
  39. treenodes = 0;
  40. }
  41. tree*
  42. tree1(int type, tree *c0)
  43. {
  44. return tree3(type, c0, (tree *)0, (tree *)0);
  45. }
  46. tree*
  47. tree2(int type, tree *c0, tree *c1)
  48. {
  49. return tree3(type, c0, c1, (tree *)0);
  50. }
  51. tree*
  52. tree3(int type, tree *c0, tree *c1, tree *c2)
  53. {
  54. tree *t;
  55. if(type==';'){
  56. if(c0==0)
  57. return c1;
  58. if(c1==0)
  59. return c0;
  60. }
  61. t = newtree();
  62. t->type = type;
  63. t->child[0] = c0;
  64. t->child[1] = c1;
  65. t->child[2] = c2;
  66. return t;
  67. }
  68. tree*
  69. mung1(tree *t, tree *c0)
  70. {
  71. t->child[0] = c0;
  72. return t;
  73. }
  74. tree*
  75. mung2(tree *t, tree *c0, tree *c1)
  76. {
  77. t->child[0] = c0;
  78. t->child[1] = c1;
  79. return t;
  80. }
  81. tree*
  82. mung3(tree *t, tree *c0, tree *c1, tree *c2)
  83. {
  84. t->child[0] = c0;
  85. t->child[1] = c1;
  86. t->child[2] = c2;
  87. return t;
  88. }
  89. tree*
  90. epimung(tree *comp, tree *epi)
  91. {
  92. tree *p;
  93. if(epi==0)
  94. return comp;
  95. for(p = epi;p->child[1];p = p->child[1]);
  96. p->child[1] = comp;
  97. return epi;
  98. }
  99. /*
  100. * Add a SIMPLE node at the root of t and percolate all the redirections
  101. * up to the root.
  102. */
  103. tree*
  104. simplemung(tree *t)
  105. {
  106. tree *u;
  107. struct io *s;
  108. t = tree1(SIMPLE, t);
  109. s = openstr();
  110. pfmt(s, "%t", t);
  111. t->str = strdup((char *)s->strp);
  112. closeio(s);
  113. for(u = t->child[0];u->type==ARGLIST;u = u->child[0]){
  114. if(u->child[1]->type==DUP
  115. || u->child[1]->type==REDIR){
  116. u->child[1]->child[1] = t;
  117. t = u->child[1];
  118. u->child[1] = 0;
  119. }
  120. }
  121. return t;
  122. }
  123. tree*
  124. token(char *str, int type)
  125. {
  126. tree *t = newtree();
  127. t->type = type;
  128. t->str = strdup(str);
  129. return t;
  130. }
  131. void
  132. freetree(tree *p)
  133. {
  134. if(p==0)
  135. return;
  136. freetree(p->child[0]);
  137. freetree(p->child[1]);
  138. freetree(p->child[2]);
  139. if(p->str)
  140. efree(p->str);
  141. efree((char *)p);
  142. }