rule.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "mk.h"
  2. static Rule *lr, *lmr;
  3. static rcmp(Rule *r, char *target, Word *tail);
  4. static int nrules = 0;
  5. void
  6. addrule(char *head, Word *tail, char *body, Word *ahead, int attr, int hline, char *prog)
  7. {
  8. Rule *r;
  9. Rule *rr;
  10. Symtab *sym;
  11. int reuse;
  12. r = 0;
  13. reuse = 0;
  14. if(sym = symlook(head, S_TARGET, 0)){
  15. for(r = sym->u.ptr; r; r = r->chain)
  16. if(rcmp(r, head, tail) == 0){
  17. reuse = 1;
  18. break;
  19. }
  20. }
  21. if(r == 0)
  22. r = (Rule *)Malloc(sizeof(Rule));
  23. r->target = head;
  24. r->tail = tail;
  25. r->recipe = body;
  26. r->line = hline;
  27. r->file = infile;
  28. r->attr = attr;
  29. r->alltargets = ahead;
  30. r->prog = prog;
  31. r->rule = nrules++;
  32. if(!reuse){
  33. rr = symlook(head, S_TARGET, r)->u.ptr;
  34. if(rr != r){
  35. r->chain = rr->chain;
  36. rr->chain = r;
  37. } else
  38. r->chain = 0;
  39. }
  40. if(!reuse)
  41. r->next = 0;
  42. if((attr&REGEXP) || charin(head, "%&")){
  43. r->attr |= META;
  44. if(reuse)
  45. return;
  46. if(attr&REGEXP){
  47. patrule = r;
  48. r->pat = regcomp(head);
  49. }
  50. if(metarules == 0)
  51. metarules = lmr = r;
  52. else {
  53. lmr->next = r;
  54. lmr = r;
  55. }
  56. } else {
  57. if(reuse)
  58. return;
  59. r->pat = 0;
  60. if(rules == 0)
  61. rules = lr = r;
  62. else {
  63. lr->next = r;
  64. lr = r;
  65. }
  66. }
  67. }
  68. void
  69. dumpr(char *s, Rule *r)
  70. {
  71. Bprint(&bout, "%s: start=%p\n", s, r);
  72. for(; r; r = r->next){
  73. Bprint(&bout, "\tRule %p: %s:%d attr=%x next=%p chain=%p alltarget='%s'",
  74. r, r->file, r->line, r->attr, r->next, r->chain, wtos(r->alltargets, ' '));
  75. if(r->prog)
  76. Bprint(&bout, " prog='%s'", r->prog);
  77. Bprint(&bout, "\n\ttarget=%s: %s\n", r->target, wtos(r->tail,' '));
  78. Bprint(&bout, "\trecipe@%p='%s'\n", r->recipe, r->recipe);
  79. }
  80. }
  81. static int
  82. rcmp(Rule *r, char *target, Word *tail)
  83. {
  84. Word *w;
  85. if(strcmp(r->target, target))
  86. return 1;
  87. for(w = r->tail; w && tail; w = w->next, tail = tail->next)
  88. if(strcmp(w->s, tail->s))
  89. return 1;
  90. return(w || tail);
  91. }
  92. char *
  93. rulecnt(void)
  94. {
  95. char *s;
  96. s = Malloc(nrules);
  97. memset(s, 0, nrules);
  98. return(s);
  99. }