formatting
[unix-history] / usr / src / old / make / main.c
CommitLineData
a781a4b6 1static char *sccsid = "@(#)main.c 4.5 (Berkeley) 83/03/03";
8f5e70e1
BJ
2# include "defs"
3/*
4command make to update programs.
5Flags: 'd' print out debugging comments
6 'p' print out a version of the input graph
7 's' silent mode--don't print out commands
8 'f' the next argument is the name of the description file;
9 "makefile" is the default
10 'i' ignore error codes from the shell
11 'S' stop after any command fails (normally do parallel work)
12 'n' don't issue, just print, commands
13 't' touch (update time of) files but don't issue command
14 'q' don't do anything, but check if object is up to date;
15 returns exit code 0 if up to date, -1 if not
16*/
17
18struct nameblock *mainname = NULL;
19struct nameblock *firstname = NULL;
20struct lineblock *sufflist = NULL;
21struct varblock *firstvar = NULL;
22struct pattern *firstpat = NULL;
b342e078 23struct dirhdr *firstod = NULL;
8f5e70e1
BJ
24
25#include <signal.h>
26int sigivalue = 0;
27int sigqvalue = 0;
28int waitpid = 0;
29
30int dbgflag = NO;
31int prtrflag = NO;
32int silflag = NO;
33int noexflag = NO;
34int keepgoing = NO;
35int noruleflag = NO;
36int touchflag = NO;
37int questflag = NO;
38int ndocoms = NO;
39int ignerr = NO; /* default is to stop on error */
40int okdel = YES;
41int inarglist;
42#ifdef pwb
43char *prompt = ">"; /* other systems -- pick what you want */
44#else
45char *prompt = ""; /* other systems -- pick what you want */
46#endif
47int nopdir = 0;
48char junkname[20];
49char funny[128];
6738d7a9 50char options[26 + 1] = { '-' };
8f5e70e1
BJ
51
52main(argc,argv)
53int argc;
54char *argv[];
55{
56register struct nameblock *p;
57int i, j;
58int descset, nfargs;
59TIMETYPE tjunk;
60char c, *s;
61static char onechar[2] = "X";
62#ifdef unix
63int intrupt();
6738d7a9 64char *op = options + 1;
8f5e70e1
BJ
65
66
67
68#endif
69
70#ifdef METERFILE
71meter(METERFILE);
72#endif
73
74descset = 0;
75
76funny['\0'] = (META | TERMINAL);
77for(s = "=|^();&<>*?[]:$`'\"\\\n" ; *s ; ++s)
78 funny[*s] |= META;
79for(s = "\n\t :;&>|" ; *s ; ++s)
80 funny[*s] |= TERMINAL;
81
82
83inarglist = 1;
84for(i=1; i<argc; ++i)
85 if(argv[i]!=0 && argv[i][0]!='-' && eqsign(argv[i]))
86 argv[i] = 0;
87
88setvar("$","$");
89inarglist = 0;
90
6738d7a9
SL
91for (i=1; i<argc; ++i)
92 if (argv[i]!=0 && argv[i][0]=='-') {
93 for (j=1 ; (c=argv[i][j])!='\0' ; ++j) {
94 *op++ = c;
95 switch (c) {
96
97 case 'd':
98 dbgflag = YES;
99 break;
100
101 case 'p':
102 prtrflag = YES;
103 break;
104
105 case 's':
106 silflag = YES;
107 break;
108
109 case 'i':
110 ignerr = YES;
111 break;
112
113 case 'S':
114 keepgoing = NO;
115 break;
116
117 case 'k':
118 keepgoing = YES;
119 break;
120
121 case 'n':
122 noexflag = YES;
123 break;
124
125 case 'r':
126 noruleflag = YES;
127 break;
128
129 case 't':
130 touchflag = YES;
131 break;
132
133 case 'q':
134 questflag = YES;
135 break;
136
137 case 'f':
138 op--; /* don't pass this one */
139 if(i >= argc-1)
140 fatal("No description argument after -f flag");
141 if( rddescf(argv[i+1]) )
8f5e70e1 142 fatal1("Cannot open %s", argv[i+1]);
6738d7a9
SL
143 argv[i+1] = 0;
144 ++descset;
145 break;
146
147 default:
148 onechar[0] = c; /* to make lint happy */
149 fatal1("Unknown flag argument %s", onechar);
150 }
8f5e70e1 151 }
6738d7a9 152 argv[i] = 0;
8f5e70e1
BJ
153 }
154
6738d7a9 155*op++ = '\0';
83d9028e
SL
156if (strcmp(options, "-") == 0)
157 *options = '\0';
6738d7a9
SL
158setvar("MFLAGS", options); /* MFLAGS=options to make */
159
8f5e70e1
BJ
160if( !descset )
161#ifdef unix
162 if( rddescf("makefile") ) rddescf("Makefile");
163#endif
164#ifdef gcos
165 rddescf("makefile");
166#endif
167
168if(prtrflag) printdesc(NO);
169
170if( srchname(".IGNORE") ) ++ignerr;
171if( srchname(".SILENT") ) silflag = 1;
172if(p=srchname(".SUFFIXES")) sufflist = p->linep;
173if( !sufflist ) fprintf(stderr,"No suffix list.\n");
174
175#ifdef unix
176sigivalue = (int) signal(SIGINT, SIG_IGN) & 01;
177sigqvalue = (int) signal(SIGQUIT, SIG_IGN) & 01;
178enbint(intrupt);
179#endif
180
181nfargs = 0;
182
183for(i=1; i<argc; ++i)
184 if((s=argv[i]) != 0)
185 {
186 if((p=srchname(s)) == 0)
187 {
188 p = makename(s);
189 }
190 ++nfargs;
191 doname(p, 0, &tjunk);
192 if(dbgflag) printdesc(YES);
193 }
194
195/*
196If no file arguments have been encountered, make the first
197name encountered that doesn't start with a dot
198*/
199
200if(nfargs == 0)
201 if(mainname == 0)
202 fatal("No arguments or description file");
203 else {
204 doname(mainname, 0, &tjunk);
205 if(dbgflag) printdesc(YES);
206 }
207
208exit(0);
209}
210
a781a4b6 211#include <sys/stat.h>
8f5e70e1
BJ
212
213#ifdef unix
214intrupt()
215{
216struct varblock *varptr();
217char *p;
218TIMETYPE exists();
a781a4b6 219struct stat sbuf;
8f5e70e1
BJ
220
221if(okdel && !noexflag && !touchflag &&
a781a4b6
SL
222 (p = varptr("@")->varval) &&
223 (stat(p, &sbuf) >= 0 && (sbuf.st_mode&S_IFMT) == S_IFREG) &&
224 !isprecious(p) )
8f5e70e1
BJ
225 {
226 fprintf(stderr, "\n*** %s removed.", p);
227 unlink(p);
228 }
229
230if(junkname[0])
231 unlink(junkname);
232fprintf(stderr, "\n");
233exit(2);
234}
235
236
237
238
239isprecious(p)
240char *p;
241{
242register struct lineblock *lp;
243register struct depblock *dp;
244register struct nameblock *np;
245
246if(np = srchname(".PRECIOUS"))
247 for(lp = np->linep ; lp ; lp = lp->nxtlineblock)
248 for(dp = lp->depp ; dp ; dp = dp->nxtdepblock)
249 if(! unequal(p, dp->depname->namep))
250 return(YES);
251
252return(NO);
253}
254
255
256enbint(k)
257int (*k)();
258{
259if(sigivalue == 0)
260 signal(SIGINT,k);
261if(sigqvalue == 0)
262 signal(SIGQUIT,k);
263}
264#endif
265\f
266extern char *builtin[];
267
268char **linesptr = builtin;
269
270FILE * fin;
271int firstrd = 0;
272
273
274rddescf(descfile)
275char *descfile;
276{
277FILE * k;
278
279/* read and parse description */
280
281if( !firstrd++ )
282 {
283 if( !noruleflag )
284 rdd1( (FILE *) NULL);
285
286#ifdef pwb
287 {
288 char *nlog, s[100];
289 nlog = logdir();
290 if ( (k=fopen( concat(nlog,"/makecomm",s), "r")) != NULL)
291 rdd1(k);
292 else if ( (k=fopen( concat(nlog,"/Makecomm",s), "r")) != NULL)
293 rdd1(k);
294
295 if ( (k=fopen("makecomm", "r")) != NULL)
296 rdd1(k);
297 else if ( (k=fopen("Makecomm", "r")) != NULL)
298 rdd1(k);
299 }
300#endif
301
302 }
303if(! unequal(descfile, "-"))
304 return( rdd1(stdin) );
305
306if( (k = fopen(descfile,"r")) != NULL)
307 return( rdd1(k) );
308
309return(1);
310}
311
312
313
314
315rdd1(k)
316FILE * k;
317{
318extern int yylineno;
319extern char *zznextc;
320
321fin = k;
322yylineno = 0;
323zznextc = 0;
324
325if( yyparse() )
326 fatal("Description file error");
327
328if(fin != NULL)
329 fclose(fin);
330
331return(0);
332}
333\f
334printdesc(prntflag)
335int prntflag;
336{
337struct nameblock *p;
338struct depblock *dp;
339struct varblock *vp;
b342e078 340struct dirhdr *od;
8f5e70e1
BJ
341struct shblock *sp;
342struct lineblock *lp;
343
344#ifdef unix
345if(prntflag)
346 {
347 printf("Open directories:\n");
348 for (od = firstod; od; od = od->nxtopendir)
b342e078 349 printf("\t%d: %s\n", od->dirfc->dd_fd, od->dirn);
8f5e70e1
BJ
350 }
351#endif
352
353if(firstvar != 0) printf("Macros:\n");
354for(vp = firstvar; vp ; vp = vp->nxtvarblock)
355 printf("\t%s = %s\n" , vp->varname , vp->varval);
356
357for(p = firstname; p; p = p->nxtnameblock)
358 {
359 printf("\n\n%s",p->namep);
360 if(p->linep != 0) printf(":");
361 if(prntflag) printf(" done=%d",p->done);
362 if(p==mainname) printf(" (MAIN NAME)");
363 for(lp = p->linep ; lp ; lp = lp->nxtlineblock)
364 {
365 if( dp = lp->depp )
366 {
367 printf("\n depends on:");
368 for(; dp ; dp = dp->nxtdepblock)
369 if(dp->depname != 0)
370 printf(" %s ", dp->depname->namep);
371 }
372
373 if(sp = lp->shp)
374 {
375 printf("\n commands:\n");
376 for( ; sp!=0 ; sp = sp->nxtshblock)
377 printf("\t%s\n", sp->shbp);
378 }
379 }
380 }
381printf("\n");
382fflush(stdout);
383}