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
|
#include "mk.h"
#define MKFILE "mkfile"
int debug;
Rule *rules, *metarules;
int nflag = 0;
int tflag = 0;
int iflag = 0;
int kflag = 0;
int aflag = 0;
int uflag = 0;
char *explain = 0;
Word *target1;
int nreps = 1;
Job *jobs;
Biobuf bout;
Rule *patrule;
void badusage(void);
#ifdef PROF
short buf[10000];
#endif
int
main(int argc, char **argv)
{
Word *w;
char *s, *temp;
char *files[256], **f = files, **ff;
int sflag = 0;
int i;
int tfd = -1;
Biobuf tb;
Bufblock *buf;
Bufblock *whatif;
/*
* start with a copy of the current environment variables
* instead of sharing them
*/
Binit(&bout, 1, OWRITE);
buf = newbuf();
whatif = 0;
USED(argc);
for(argv++; *argv && (**argv == '-'); argv++)
{
bufcpy(buf, argv[0], strlen(argv[0]));
insert(buf, ' ');
switch(argv[0][1])
{
case 'a':
aflag = 1;
break;
case 'd':
if(*(s = &argv[0][2]))
while(*s) switch(*s++)
{
case 'p': debug |= D_PARSE; break;
case 'g': debug |= D_GRAPH; break;
case 'e': debug |= D_EXEC; break;
}
else
debug = 0xFFFF;
break;
case 'e':
explain = &argv[0][2];
break;
case 'f':
if(*++argv == 0)
badusage();
*f++ = *argv;
bufcpy(buf, argv[0], strlen(argv[0]));
insert(buf, ' ');
break;
case 'i':
iflag = 1;
break;
case 'k':
kflag = 1;
break;
case 'n':
nflag = 1;
break;
case 's':
sflag = 1;
break;
case 't':
tflag = 1;
break;
case 'u':
uflag = 1;
break;
case 'w':
if(whatif == 0)
whatif = newbuf();
else
insert(whatif, ' ');
if(argv[0][2])
bufcpy(whatif, &argv[0][2], strlen(&argv[0][2]));
else {
if(*++argv == 0)
badusage();
bufcpy(whatif, &argv[0][0], strlen(&argv[0][0]));
}
break;
default:
badusage();
}
}
#ifdef PROF
{
extern etext();
monitor(main, etext, buf, sizeof buf, 300);
}
#endif
if(aflag)
iflag = 1;
usage();
syminit();
initshell();
initenv();
usage();
/*
assignment args become null strings
*/
temp = 0;
for(i = 0; argv[i]; i++) if(utfrune(argv[i], '=')){
bufcpy(buf, argv[i], strlen(argv[i]));
insert(buf, ' ');
if(tfd < 0){
temp = maketmp(&tfd);
if(temp == 0) {
fprint(2, "temp file: %r\n");
Exit();
}
Binit(&tb, tfd, OWRITE);
}
Bprint(&tb, "%s\n", argv[i]);
*argv[i] = 0;
}
if(tfd >= 0){
Bflush(&tb);
LSEEK(tfd, 0L, 0);
parse("command line args", tfd, 1);
remove(temp);
}
if (buf->current != buf->start) {
buf->current--;
insert(buf, 0);
}
symlook("MKFLAGS", S_VAR, (void *) stow(buf->start));
buf->current = buf->start;
for(i = 0; argv[i]; i++){
if(*argv[i] == 0) continue;
if(i)
insert(buf, ' ');
bufcpy(buf, argv[i], strlen(argv[i]));
}
insert(buf, 0);
symlook("MKARGS", S_VAR, (void *) stow(buf->start));
freebuf(buf);
if(f == files){
if(access(MKFILE, 4) == 0)
parse(MKFILE, open(MKFILE, 0), 0);
} else
for(ff = files; ff < f; ff++)
parse(*ff, open(*ff, 0), 0);
if(DEBUG(D_PARSE)){
dumpw("default targets", target1);
dumpr("rules", rules);
dumpr("metarules", metarules);
dumpv("variables");
}
if(whatif){
insert(whatif, 0);
timeinit(whatif->start);
freebuf(whatif);
}
execinit();
/* skip assignment args */
while(*argv && (**argv == 0))
argv++;
catchnotes();
if(*argv == 0){
if(target1)
for(w = target1; w; w = w->next)
mk(w->s);
else {
fprint(2, "mk: nothing to mk\n");
Exit();
}
} else {
if(sflag){
for(; *argv; argv++)
if(**argv)
mk(*argv);
} else {
Word *head, *tail, *t;
/* fake a new rule with all the args as prereqs */
tail = 0;
t = 0;
for(; *argv; argv++)
if(**argv){
if(tail == 0)
tail = t = newword(*argv);
else {
t->next = newword(*argv);
t = t->next;
}
}
if(tail->next == 0)
mk(tail->s);
else {
head = newword("command line arguments");
addrules(head, tail, strdup(""), VIR, mkinline, 0);
mk(head->s);
}
}
}
if(uflag)
prusage();
exits(0);
return 0;
}
void
badusage(void)
{
fprint(2, "Usage: mk [-f file] [-n] [-a] [-e] [-t] [-k] [-i] [-d[egp]] [targets ...]\n");
Exit();
}
void *
Malloc(int n)
{
register void *s;
s = malloc(n);
if(!s) {
fprint(2, "mk: cannot alloc %d bytes\n", n);
Exit();
}
return(s);
}
void *
Realloc(void *s, int n)
{
if(s)
s = realloc(s, n);
else
s = malloc(n);
if(!s) {
fprint(2, "mk: cannot alloc %d bytes\n", n);
Exit();
}
return(s);
}
void
assert(char *s, int n)
{
if(!n){
fprint(2, "mk: Assertion ``%s'' failed.\n", s);
Exit();
}
}
void
regerror(char *s)
{
if(patrule)
fprint(2, "mk: %s:%d: regular expression error; %s\n",
patrule->file, patrule->line, s);
else
fprint(2, "mk: %s:%d: regular expression error; %s\n",
infile, mkinline, s);
Exit();
}
|