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