1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
|
%{#include "defs.h"
%}
%term NAME SHELLINE START MACRODEF COLON DOUBLECOLON GREATER AMPER AMPERAMPER
%union
{
struct shblock *yshblock;
depblkp ydepblock;
nameblkp ynameblock;
}
%type <yshblock> SHELLINE, shlist, shellist
%type <ynameblock> NAME, namelist
%type <ydepblock> deplist, dlist
%%
%{
struct depblock *pp;
static struct shblock *prevshp;
static struct nameblock *lefts[NLEFTS];
struct nameblock *leftp;
static int nlefts;
struct lineblock *lp, *lpp;
static struct depblock *prevdep;
static int sepc;
static int allnowait;
static struct fstack
{
FILE *fin;
char *fname;
int lineno;
} filestack[MAXINCLUDE];
static int ninclude = 0;
%}
file:
| file comline
;
comline: START
| MACRODEF
| START namelist deplist shellist = {
while( --nlefts >= 0)
{
wildp wp;
leftp = lefts[nlefts];
if(wp = iswild(leftp->namep))
{
leftp->septype = SOMEDEPS;
if(lastwild)
lastwild->next = wp;
else
firstwild = wp;
lastwild = wp;
}
if(leftp->septype == 0)
leftp->septype = sepc;
else if(leftp->septype != sepc)
{
if(! wp)
fprintf(stderr,
"Inconsistent rules lines for `%s'\n",
leftp->namep);
}
else if(sepc==ALLDEPS && leftp->namep[0]!='.' && $4!=0)
{
for(lp=leftp->linep; lp->nxtlineblock; lp=lp->nxtlineblock)
if(lp->shp)
fprintf(stderr,
"Multiple rules lines for `%s'\n",
leftp->namep);
}
lp = ALLOC(lineblock);
lp->nxtlineblock = NULL;
lp->depp = $3;
lp->shp = $4;
if(wp)
wp->linep = lp;
if(equal(leftp->namep, ".SUFFIXES") && $3==0)
leftp->linep = 0;
else if(leftp->linep == 0)
leftp->linep = lp;
else {
for(lpp = leftp->linep; lpp->nxtlineblock;
lpp = lpp->nxtlineblock) ;
if(sepc==ALLDEPS && leftp->namep[0]=='.')
lpp->shp = 0;
lpp->nxtlineblock = lp;
}
}
}
| error
;
namelist: NAME = { lefts[0] = $1; nlefts = 1; }
| namelist NAME = { lefts[nlefts++] = $2;
if(nlefts>=NLEFTS) fatal("Too many lefts"); }
;
deplist:
{
char junk[100];
sprintf(junk, "%s:%d", filestack[ninclude-1].fname, yylineno);
fatal1("Must be a separator on rules line %s", junk);
}
| dlist
;
dlist: sepchar = { prevdep = 0; $$ = 0; allnowait = NO; }
| sepchar AMPER = { prevdep = 0; $$ = 0; allnowait = YES; }
| dlist NAME = {
pp = ALLOC(depblock);
pp->nxtdepblock = NULL;
pp->depname = $2;
pp->nowait = allnowait;
if(prevdep == 0) $$ = pp;
else prevdep->nxtdepblock = pp;
prevdep = pp;
}
| dlist AMPER = { if(prevdep) prevdep->nowait = YES; }
| dlist AMPERAMPER
;
sepchar: COLON = { sepc = ALLDEPS; }
| DOUBLECOLON = { sepc = SOMEDEPS; }
;
shellist: = {$$ = 0; }
| shlist = { $$ = $1; }
;
shlist: SHELLINE = { $$ = $1; prevshp = $1; }
| shlist SHELLINE = { $$ = $1;
prevshp->nxtshblock = $2;
prevshp = $2;
}
;
%%
static char *zznextc; /* null if need another line;
otherwise points to next char */
static int yylineno;
static FILE * fin;
static int retsh(char *);
static int nextlin(void);
static int isinclude(char *);
int yyparse(void);
int
parse(char *name)
{
FILE *stream;
if(name == CHNULL)
{
stream = NULL;
name = "(builtin-rules)";
}
else if(equal(name, "-"))
{
stream = stdin;
name = "(stdin)";
}
else if( (stream = fopen(name, "r")) == NULL)
return NO;
filestack[0].fname = copys(name);
ninclude = 1;
fin = stream;
yylineno = 0;
zznextc = 0;
if( yyparse() )
fatal("Description file error");
if(fin)
fclose(fin);
return YES;
}
int
yylex(void)
{
char *p;
char *q;
char word[INMAX];
if(! zznextc )
return nextlin() ;
while( isspace(*zznextc) )
++zznextc;
switch(*zznextc)
{
case '\0':
return nextlin() ;
case '|':
if(zznextc[1]==':')
{
zznextc += 2;
return DOUBLECOLON;
}
break;
case ':':
if(*++zznextc == ':')
{
++zznextc;
return DOUBLECOLON;
}
return COLON;
case '>':
++zznextc;
return GREATER;
case '&':
if(*++zznextc == '&')
{
++zznextc;
return AMPERAMPER;
}
return AMPER;
case ';':
return retsh(zznextc) ;
}
p = zznextc;
q = word;
while( ! ( funny[*p] & TERMINAL) )
*q++ = *p++;
if(p != zznextc)
{
*q = '\0';
if((yylval.ynameblock=srchname(word))==0)
yylval.ynameblock = makename(word);
zznextc = p;
return NAME;
}
else {
char junk[100];
sprintf(junk, "Bad character %c (octal %o), line %d of file %s",
*zznextc, *zznextc, yylineno, filestack[ninclude-1].fname);
fatal(junk);
}
return 0; /* never executed */
}
static int
retsh(char *q)
{
register char *p;
struct shblock *sp;
for(p=q+1 ; *p==' '||*p=='\t' ; ++p) ;
sp = ALLOC(shblock);
sp->nxtshblock = NULL;
sp->shbp = (fin ? copys(p) : p );
yylval.yshblock = sp;
zznextc = 0;
return SHELLINE;
}
static int
nextlin(void)
{
static char yytext[INMAX];
static char *yytextl = yytext+INMAX;
char *text, templin[INMAX];
char c;
char *p, *t;
char lastch, *lastchp;
extern char **linesptr;
int incom;
int kc;
again:
incom = NO;
zznextc = 0;
if(fin == NULL)
{
if( (text = *linesptr++) == 0)
return 0;
++yylineno;
}
else {
for(p = text = yytext ; p<yytextl ; *p++ = kc)
switch(kc = getc(fin))
{
case '\t':
if(p == yytext)
incom = YES;
break;
case ';':
incom = YES;
break;
case '#':
if(! incom)
kc = '\0';
break;
case '\n':
++yylineno;
if(p==yytext || p[-1]!='\\')
{
*p = '\0';
goto endloop;
}
p[-1] = ' ';
while( (kc=getc(fin))=='\t' || kc==' ' || kc=='\n')
if(kc == '\n')
++yylineno;
if(kc != EOF)
break;
case EOF:
*p = '\0';
if(ninclude > 1)
{
register struct fstack *stp;
fclose(fin);
--ninclude;
stp = filestack + ninclude;
fin = stp->fin;
yylineno = stp->lineno;
free(stp->fname);
goto again;
}
return 0;
}
fatal("line too long");
}
endloop:
if((c = text[0]) == '\t')
return retsh(text) ;
if(isalpha(c) || isdigit(c) || c==' ' || c=='.'|| c=='_')
for(p=text+1; *p!='\0'; )
if(*p == ':')
break;
else if(*p++ == '=')
{
eqsign(text);
return MACRODEF;
}
/* substitute for macros on dependency line up to the semicolon if any */
for(t = yytext ; *t!='\0' && *t!=';' ; ++t)
;
lastchp = t;
lastch = *t;
*t = '\0'; /* replace the semi with a null so subst will stop */
subst(yytext, templin); /* Substitute for macros on dependency lines */
if(lastch) /* copy the stuff after the semicolon */
{
*lastchp = lastch;
strcat(templin, lastchp);
}
strcpy(yytext, templin);
/* process include files after macro substitution */
if(strncmp(text, "include", 7) == 0) {
if (isinclude(text+7))
goto again;
}
for(p = zznextc = text ; *p ; ++p )
if(*p!=' ' && *p!='\t')
return START;
goto again;
}
static int
isinclude(char *s)
{
char *t;
struct fstack *p;
for(t=s; *t==' ' || *t=='\t' ; ++t)
;
if(t == s)
return NO;
for(s = t; *s!='\n' && *s!='#' && *s!='\0' ; ++s)
if(*s == ':')
return NO;
*s = '\0';
if(ninclude >= MAXINCLUDE)
fatal("include depth exceeded");
p = filestack + ninclude;
p->fin = fin;
p->lineno = yylineno;
p->fname = copys(t);
if( (fin = fopen(t, "r")) == NULL)
fatal1("Cannot open include file %s", t);
yylineno = 0;
++ninclude;
return YES;
}
int
yyerror(char *s, ...)
{
char buf[100];
sprintf(buf, "line %d of file %s: %s",
yylineno, filestack[ninclude-1].fname, s);
fatal(buf);
}
|